Skip to content

Commit 575313f

Browse files
Waiman-Longhtejun
authored andcommitted
cgroup: Make debug cgroup support v2 and thread mode
Besides supporting cgroup v2 and thread mode, the following changes are also made: 1) current_* cgroup files now resides only at the root as we don't need duplicated files of the same function all over the cgroup hierarchy. 2) The cgroup_css_links_read() function is modified to report the number of tasks that are skipped because of overflow. 3) The number of extra unaccounted references are displayed. 4) The current_css_set_read() function now prints out the addresses of the css'es associated with the current css_set. 5) A new cgroup_subsys_states file is added to display the css objects associated with a cgroup. 6) A new cgroup_masks file is added to display the various controller bit masks in the cgroup. tj: Dropped thread mode related information for now so that debug controller changes aren't blocked on the thread mode. Signed-off-by: Waiman Long <longman@redhat.com> Signed-off-by: Tejun Heo <tj@kernel.org>
1 parent 23b0be4 commit 575313f

1 file changed

Lines changed: 153 additions & 17 deletions

File tree

‎kernel/cgroup/debug.c‎

Lines changed: 153 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,37 @@ static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
3636
return cgroup_task_count(css->cgroup);
3737
}
3838

39-
static u64 current_css_set_read(struct cgroup_subsys_state *css,
40-
struct cftype *cft)
39+
static int current_css_set_read(struct seq_file *seq, void *v)
4140
{
42-
return (u64)(unsigned long)current->cgroups;
41+
struct css_set *cset;
42+
struct cgroup_subsys *ss;
43+
struct cgroup_subsys_state *css;
44+
int i, refcnt;
45+
46+
mutex_lock(&cgroup_mutex);
47+
spin_lock_irq(&css_set_lock);
48+
rcu_read_lock();
49+
cset = rcu_dereference(current->cgroups);
50+
refcnt = refcount_read(&cset->refcount);
51+
seq_printf(seq, "css_set %pK %d", cset, refcnt);
52+
if (refcnt > cset->nr_tasks)
53+
seq_printf(seq, " +%d", refcnt - cset->nr_tasks);
54+
seq_puts(seq, "\n");
55+
56+
/*
57+
* Print the css'es stored in the current css_set.
58+
*/
59+
for_each_subsys(ss, i) {
60+
css = cset->subsys[ss->id];
61+
if (!css)
62+
continue;
63+
seq_printf(seq, "%2d: %-4s\t- %lx[%d]\n", ss->id, ss->name,
64+
(unsigned long)css, css->id);
65+
}
66+
rcu_read_unlock();
67+
spin_unlock_irq(&css_set_lock);
68+
mutex_unlock(&cgroup_mutex);
69+
return 0;
4370
}
4471

4572
static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
@@ -84,31 +111,126 @@ static int cgroup_css_links_read(struct seq_file *seq, void *v)
84111
{
85112
struct cgroup_subsys_state *css = seq_css(seq);
86113
struct cgrp_cset_link *link;
114+
int dead_cnt = 0, extra_refs = 0;
87115

88116
spin_lock_irq(&css_set_lock);
89117
list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
90118
struct css_set *cset = link->cset;
91119
struct task_struct *task;
92120
int count = 0;
121+
int refcnt = refcount_read(&cset->refcount);
93122

94-
seq_printf(seq, "css_set %pK\n", cset);
123+
seq_printf(seq, " %d", refcnt);
124+
if (refcnt - cset->nr_tasks > 0) {
125+
int extra = refcnt - cset->nr_tasks;
126+
127+
seq_printf(seq, " +%d", extra);
128+
/*
129+
* Take out the one additional reference in
130+
* init_css_set.
131+
*/
132+
if (cset == &init_css_set)
133+
extra--;
134+
extra_refs += extra;
135+
}
136+
seq_puts(seq, "\n");
95137

96138
list_for_each_entry(task, &cset->tasks, cg_list) {
97-
if (count++ > MAX_TASKS_SHOWN_PER_CSS)
98-
goto overflow;
99-
seq_printf(seq, " task %d\n", task_pid_vnr(task));
139+
if (count++ <= MAX_TASKS_SHOWN_PER_CSS)
140+
seq_printf(seq, " task %d\n",
141+
task_pid_vnr(task));
100142
}
101143

102144
list_for_each_entry(task, &cset->mg_tasks, cg_list) {
103-
if (count++ > MAX_TASKS_SHOWN_PER_CSS)
104-
goto overflow;
105-
seq_printf(seq, " task %d\n", task_pid_vnr(task));
145+
if (count++ <= MAX_TASKS_SHOWN_PER_CSS)
146+
seq_printf(seq, " task %d\n",
147+
task_pid_vnr(task));
106148
}
107-
continue;
108-
overflow:
109-
seq_puts(seq, " ...\n");
149+
/* show # of overflowed tasks */
150+
if (count > MAX_TASKS_SHOWN_PER_CSS)
151+
seq_printf(seq, " ... (%d)\n",
152+
count - MAX_TASKS_SHOWN_PER_CSS);
153+
154+
if (cset->dead) {
155+
seq_puts(seq, " [dead]\n");
156+
dead_cnt++;
157+
}
158+
159+
WARN_ON(count != cset->nr_tasks);
110160
}
111161
spin_unlock_irq(&css_set_lock);
162+
163+
if (!dead_cnt && !extra_refs)
164+
return 0;
165+
166+
seq_puts(seq, "\n");
167+
if (extra_refs)
168+
seq_printf(seq, "extra references = %d\n", extra_refs);
169+
if (dead_cnt)
170+
seq_printf(seq, "dead css_sets = %d\n", dead_cnt);
171+
172+
return 0;
173+
}
174+
175+
static int cgroup_subsys_states_read(struct seq_file *seq, void *v)
176+
{
177+
struct cgroup *cgrp = seq_css(seq)->cgroup;
178+
struct cgroup_subsys *ss;
179+
struct cgroup_subsys_state *css;
180+
char pbuf[16];
181+
int i;
182+
183+
mutex_lock(&cgroup_mutex);
184+
for_each_subsys(ss, i) {
185+
css = rcu_dereference_check(cgrp->subsys[ss->id], true);
186+
if (!css)
187+
continue;
188+
189+
pbuf[0] = '\0';
190+
191+
/* Show the parent CSS if applicable*/
192+
if (css->parent)
193+
snprintf(pbuf, sizeof(pbuf) - 1, " P=%d",
194+
css->parent->id);
195+
seq_printf(seq, "%2d: %-4s\t- %lx[%d] %d%s\n", ss->id, ss->name,
196+
(unsigned long)css, css->id,
197+
atomic_read(&css->online_cnt), pbuf);
198+
}
199+
mutex_unlock(&cgroup_mutex);
200+
return 0;
201+
}
202+
203+
static int cgroup_masks_read(struct seq_file *seq, void *v)
204+
{
205+
struct cgroup *cgrp = seq_css(seq)->cgroup;
206+
struct cgroup_subsys *ss;
207+
int i, j;
208+
struct {
209+
u16 *mask;
210+
char *name;
211+
} mask_list[] = {
212+
{ &cgrp->subtree_control, "subtree_control" },
213+
{ &cgrp->subtree_ss_mask, "subtree_ss_mask" },
214+
};
215+
216+
mutex_lock(&cgroup_mutex);
217+
for (i = 0; i < ARRAY_SIZE(mask_list); i++) {
218+
u16 mask = *mask_list[i].mask;
219+
bool first = true;
220+
221+
seq_printf(seq, "%-17s: ", mask_list[i].name);
222+
for_each_subsys(ss, j) {
223+
if (!(mask & (1 << ss->id)))
224+
continue;
225+
if (!first)
226+
seq_puts(seq, ", ");
227+
seq_puts(seq, ss->name);
228+
first = false;
229+
}
230+
seq_putc(seq, '\n');
231+
}
232+
233+
mutex_unlock(&cgroup_mutex);
112234
return 0;
113235
}
114236

@@ -126,24 +248,37 @@ static struct cftype debug_files[] = {
126248

127249
{
128250
.name = "current_css_set",
129-
.read_u64 = current_css_set_read,
251+
.seq_show = current_css_set_read,
252+
.flags = CFTYPE_ONLY_ON_ROOT,
130253
},
131254

132255
{
133256
.name = "current_css_set_refcount",
134257
.read_u64 = current_css_set_refcount_read,
258+
.flags = CFTYPE_ONLY_ON_ROOT,
135259
},
136260

137261
{
138262
.name = "current_css_set_cg_links",
139263
.seq_show = current_css_set_cg_links_read,
264+
.flags = CFTYPE_ONLY_ON_ROOT,
140265
},
141266

142267
{
143268
.name = "cgroup_css_links",
144269
.seq_show = cgroup_css_links_read,
145270
},
146271

272+
{
273+
.name = "cgroup_subsys_states",
274+
.seq_show = cgroup_subsys_states_read,
275+
},
276+
277+
{
278+
.name = "cgroup_masks",
279+
.seq_show = cgroup_masks_read,
280+
},
281+
147282
{
148283
.name = "releasable",
149284
.read_u64 = releasable_read,
@@ -153,7 +288,8 @@ static struct cftype debug_files[] = {
153288
};
154289

155290
struct cgroup_subsys debug_cgrp_subsys = {
156-
.css_alloc = debug_css_alloc,
157-
.css_free = debug_css_free,
158-
.legacy_cftypes = debug_files,
291+
.css_alloc = debug_css_alloc,
292+
.css_free = debug_css_free,
293+
.legacy_cftypes = debug_files,
294+
.dfl_cftypes = debug_files,
159295
};

0 commit comments

Comments
 (0)