1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
223b0be48SWaiman Long /*
323b0be48SWaiman Long * Debug controller
423b0be48SWaiman Long *
523b0be48SWaiman Long * WARNING: This controller is for cgroup core debugging only.
623b0be48SWaiman Long * Its interfaces are unstable and subject to changes at any time.
723b0be48SWaiman Long */
8a28f8f5eSWaiman Long #include <linux/ctype.h>
9a28f8f5eSWaiman Long #include <linux/mm.h>
10a28f8f5eSWaiman Long #include <linux/slab.h>
11a28f8f5eSWaiman Long
12a28f8f5eSWaiman Long #include "cgroup-internal.h"
13a28f8f5eSWaiman Long
14a28f8f5eSWaiman Long static struct cgroup_subsys_state *
debug_css_alloc(struct cgroup_subsys_state * parent_css)15a28f8f5eSWaiman Long debug_css_alloc(struct cgroup_subsys_state *parent_css)
16a28f8f5eSWaiman Long {
17a28f8f5eSWaiman Long struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
18a28f8f5eSWaiman Long
19a28f8f5eSWaiman Long if (!css)
20a28f8f5eSWaiman Long return ERR_PTR(-ENOMEM);
21a28f8f5eSWaiman Long
22a28f8f5eSWaiman Long return css;
23a28f8f5eSWaiman Long }
24a28f8f5eSWaiman Long
debug_css_free(struct cgroup_subsys_state * css)25a28f8f5eSWaiman Long static void debug_css_free(struct cgroup_subsys_state *css)
26a28f8f5eSWaiman Long {
27a28f8f5eSWaiman Long kfree(css);
28a28f8f5eSWaiman Long }
29a28f8f5eSWaiman Long
30a28f8f5eSWaiman Long /*
31a28f8f5eSWaiman Long * debug_taskcount_read - return the number of tasks in a cgroup.
32a28f8f5eSWaiman Long * @cgrp: the cgroup in question
33a28f8f5eSWaiman Long */
debug_taskcount_read(struct cgroup_subsys_state * css,struct cftype * cft)34a28f8f5eSWaiman Long static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
35a28f8f5eSWaiman Long struct cftype *cft)
36a28f8f5eSWaiman Long {
37a28f8f5eSWaiman Long return cgroup_task_count(css->cgroup);
38a28f8f5eSWaiman Long }
39a28f8f5eSWaiman Long
current_css_set_read(struct seq_file * seq,void * v)40575313f4SWaiman Long static int current_css_set_read(struct seq_file *seq, void *v)
41a28f8f5eSWaiman Long {
42b6053d40STejun Heo struct kernfs_open_file *of = seq->private;
43575313f4SWaiman Long struct css_set *cset;
44575313f4SWaiman Long struct cgroup_subsys *ss;
45575313f4SWaiman Long struct cgroup_subsys_state *css;
46575313f4SWaiman Long int i, refcnt;
47575313f4SWaiman Long
48b6053d40STejun Heo if (!cgroup_kn_lock_live(of->kn, false))
49b6053d40STejun Heo return -ENODEV;
50b6053d40STejun Heo
51575313f4SWaiman Long spin_lock_irq(&css_set_lock);
52575313f4SWaiman Long rcu_read_lock();
53ddf7005fSWang Long cset = task_css_set(current);
54575313f4SWaiman Long refcnt = refcount_read(&cset->refcount);
55575313f4SWaiman Long seq_printf(seq, "css_set %pK %d", cset, refcnt);
56575313f4SWaiman Long if (refcnt > cset->nr_tasks)
57575313f4SWaiman Long seq_printf(seq, " +%d", refcnt - cset->nr_tasks);
58575313f4SWaiman Long seq_puts(seq, "\n");
59575313f4SWaiman Long
60575313f4SWaiman Long /*
61575313f4SWaiman Long * Print the css'es stored in the current css_set.
62575313f4SWaiman Long */
63575313f4SWaiman Long for_each_subsys(ss, i) {
64575313f4SWaiman Long css = cset->subsys[ss->id];
65575313f4SWaiman Long if (!css)
66575313f4SWaiman Long continue;
67*1900da52SFuqian Huang seq_printf(seq, "%2d: %-4s\t- %p[%d]\n", ss->id, ss->name,
68*1900da52SFuqian Huang css, css->id);
69575313f4SWaiman Long }
70575313f4SWaiman Long rcu_read_unlock();
71575313f4SWaiman Long spin_unlock_irq(&css_set_lock);
72b6053d40STejun Heo cgroup_kn_unlock(of->kn);
73575313f4SWaiman Long return 0;
74a28f8f5eSWaiman Long }
75a28f8f5eSWaiman Long
current_css_set_refcount_read(struct cgroup_subsys_state * css,struct cftype * cft)76a28f8f5eSWaiman Long static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
77a28f8f5eSWaiman Long struct cftype *cft)
78a28f8f5eSWaiman Long {
79a28f8f5eSWaiman Long u64 count;
80a28f8f5eSWaiman Long
81a28f8f5eSWaiman Long rcu_read_lock();
82a28f8f5eSWaiman Long count = refcount_read(&task_css_set(current)->refcount);
83a28f8f5eSWaiman Long rcu_read_unlock();
84a28f8f5eSWaiman Long return count;
85a28f8f5eSWaiman Long }
86a28f8f5eSWaiman Long
current_css_set_cg_links_read(struct seq_file * seq,void * v)87a28f8f5eSWaiman Long static int current_css_set_cg_links_read(struct seq_file *seq, void *v)
88a28f8f5eSWaiman Long {
89a28f8f5eSWaiman Long struct cgrp_cset_link *link;
90a28f8f5eSWaiman Long struct css_set *cset;
91a28f8f5eSWaiman Long char *name_buf;
92a28f8f5eSWaiman Long
93a28f8f5eSWaiman Long name_buf = kmalloc(NAME_MAX + 1, GFP_KERNEL);
94a28f8f5eSWaiman Long if (!name_buf)
95a28f8f5eSWaiman Long return -ENOMEM;
96a28f8f5eSWaiman Long
97a28f8f5eSWaiman Long spin_lock_irq(&css_set_lock);
98a28f8f5eSWaiman Long rcu_read_lock();
99ddf7005fSWang Long cset = task_css_set(current);
100a28f8f5eSWaiman Long list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
101a28f8f5eSWaiman Long struct cgroup *c = link->cgrp;
102a28f8f5eSWaiman Long
103a28f8f5eSWaiman Long cgroup_name(c, name_buf, NAME_MAX + 1);
104a28f8f5eSWaiman Long seq_printf(seq, "Root %d group %s\n",
105a28f8f5eSWaiman Long c->root->hierarchy_id, name_buf);
106a28f8f5eSWaiman Long }
107a28f8f5eSWaiman Long rcu_read_unlock();
108a28f8f5eSWaiman Long spin_unlock_irq(&css_set_lock);
109a28f8f5eSWaiman Long kfree(name_buf);
110a28f8f5eSWaiman Long return 0;
111a28f8f5eSWaiman Long }
112a28f8f5eSWaiman Long
113a28f8f5eSWaiman Long #define MAX_TASKS_SHOWN_PER_CSS 25
cgroup_css_links_read(struct seq_file * seq,void * v)114a28f8f5eSWaiman Long static int cgroup_css_links_read(struct seq_file *seq, void *v)
115a28f8f5eSWaiman Long {
116a28f8f5eSWaiman Long struct cgroup_subsys_state *css = seq_css(seq);
117a28f8f5eSWaiman Long struct cgrp_cset_link *link;
1187a0cf0e7SWaiman Long int dead_cnt = 0, extra_refs = 0, threaded_csets = 0;
119a28f8f5eSWaiman Long
120a28f8f5eSWaiman Long spin_lock_irq(&css_set_lock);
1217a0cf0e7SWaiman Long
122a28f8f5eSWaiman Long list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
123a28f8f5eSWaiman Long struct css_set *cset = link->cset;
124a28f8f5eSWaiman Long struct task_struct *task;
125a28f8f5eSWaiman Long int count = 0;
126575313f4SWaiman Long int refcnt = refcount_read(&cset->refcount);
127a28f8f5eSWaiman Long
1287a0cf0e7SWaiman Long /*
1297a0cf0e7SWaiman Long * Print out the proc_cset and threaded_cset relationship
1307a0cf0e7SWaiman Long * and highlight difference between refcount and task_count.
1317a0cf0e7SWaiman Long */
1327a0cf0e7SWaiman Long seq_printf(seq, "css_set %pK", cset);
1337a0cf0e7SWaiman Long if (rcu_dereference_protected(cset->dom_cset, 1) != cset) {
1347a0cf0e7SWaiman Long threaded_csets++;
1357a0cf0e7SWaiman Long seq_printf(seq, "=>%pK", cset->dom_cset);
1367a0cf0e7SWaiman Long }
1377a0cf0e7SWaiman Long if (!list_empty(&cset->threaded_csets)) {
1387a0cf0e7SWaiman Long struct css_set *tcset;
1397a0cf0e7SWaiman Long int idx = 0;
1407a0cf0e7SWaiman Long
1417a0cf0e7SWaiman Long list_for_each_entry(tcset, &cset->threaded_csets,
1427a0cf0e7SWaiman Long threaded_csets_node) {
1437a0cf0e7SWaiman Long seq_puts(seq, idx ? "," : "<=");
1447a0cf0e7SWaiman Long seq_printf(seq, "%pK", tcset);
1457a0cf0e7SWaiman Long idx++;
1467a0cf0e7SWaiman Long }
1477a0cf0e7SWaiman Long } else {
148575313f4SWaiman Long seq_printf(seq, " %d", refcnt);
149575313f4SWaiman Long if (refcnt - cset->nr_tasks > 0) {
150575313f4SWaiman Long int extra = refcnt - cset->nr_tasks;
151575313f4SWaiman Long
152575313f4SWaiman Long seq_printf(seq, " +%d", extra);
153575313f4SWaiman Long /*
154575313f4SWaiman Long * Take out the one additional reference in
155575313f4SWaiman Long * init_css_set.
156575313f4SWaiman Long */
157575313f4SWaiman Long if (cset == &init_css_set)
158575313f4SWaiman Long extra--;
159575313f4SWaiman Long extra_refs += extra;
160575313f4SWaiman Long }
1617a0cf0e7SWaiman Long }
162575313f4SWaiman Long seq_puts(seq, "\n");
163a28f8f5eSWaiman Long
164a28f8f5eSWaiman Long list_for_each_entry(task, &cset->tasks, cg_list) {
165575313f4SWaiman Long if (count++ <= MAX_TASKS_SHOWN_PER_CSS)
166575313f4SWaiman Long seq_printf(seq, " task %d\n",
167575313f4SWaiman Long task_pid_vnr(task));
168a28f8f5eSWaiman Long }
169a28f8f5eSWaiman Long
170a28f8f5eSWaiman Long list_for_each_entry(task, &cset->mg_tasks, cg_list) {
171575313f4SWaiman Long if (count++ <= MAX_TASKS_SHOWN_PER_CSS)
172575313f4SWaiman Long seq_printf(seq, " task %d\n",
173575313f4SWaiman Long task_pid_vnr(task));
174a28f8f5eSWaiman Long }
175575313f4SWaiman Long /* show # of overflowed tasks */
176575313f4SWaiman Long if (count > MAX_TASKS_SHOWN_PER_CSS)
177575313f4SWaiman Long seq_printf(seq, " ... (%d)\n",
178575313f4SWaiman Long count - MAX_TASKS_SHOWN_PER_CSS);
179575313f4SWaiman Long
180575313f4SWaiman Long if (cset->dead) {
181575313f4SWaiman Long seq_puts(seq, " [dead]\n");
182575313f4SWaiman Long dead_cnt++;
183575313f4SWaiman Long }
184575313f4SWaiman Long
185575313f4SWaiman Long WARN_ON(count != cset->nr_tasks);
186a28f8f5eSWaiman Long }
187a28f8f5eSWaiman Long spin_unlock_irq(&css_set_lock);
188575313f4SWaiman Long
1897a0cf0e7SWaiman Long if (!dead_cnt && !extra_refs && !threaded_csets)
190575313f4SWaiman Long return 0;
191575313f4SWaiman Long
192575313f4SWaiman Long seq_puts(seq, "\n");
1937a0cf0e7SWaiman Long if (threaded_csets)
1947a0cf0e7SWaiman Long seq_printf(seq, "threaded css_sets = %d\n", threaded_csets);
195575313f4SWaiman Long if (extra_refs)
196575313f4SWaiman Long seq_printf(seq, "extra references = %d\n", extra_refs);
197575313f4SWaiman Long if (dead_cnt)
198575313f4SWaiman Long seq_printf(seq, "dead css_sets = %d\n", dead_cnt);
199575313f4SWaiman Long
200575313f4SWaiman Long return 0;
201575313f4SWaiman Long }
202575313f4SWaiman Long
cgroup_subsys_states_read(struct seq_file * seq,void * v)203575313f4SWaiman Long static int cgroup_subsys_states_read(struct seq_file *seq, void *v)
204575313f4SWaiman Long {
205b6053d40STejun Heo struct kernfs_open_file *of = seq->private;
206b6053d40STejun Heo struct cgroup *cgrp;
207575313f4SWaiman Long struct cgroup_subsys *ss;
208575313f4SWaiman Long struct cgroup_subsys_state *css;
209575313f4SWaiman Long char pbuf[16];
210575313f4SWaiman Long int i;
211575313f4SWaiman Long
212b6053d40STejun Heo cgrp = cgroup_kn_lock_live(of->kn, false);
213b6053d40STejun Heo if (!cgrp)
214b6053d40STejun Heo return -ENODEV;
215b6053d40STejun Heo
216575313f4SWaiman Long for_each_subsys(ss, i) {
217575313f4SWaiman Long css = rcu_dereference_check(cgrp->subsys[ss->id], true);
218575313f4SWaiman Long if (!css)
219575313f4SWaiman Long continue;
220575313f4SWaiman Long
221575313f4SWaiman Long pbuf[0] = '\0';
222575313f4SWaiman Long
223575313f4SWaiman Long /* Show the parent CSS if applicable*/
224575313f4SWaiman Long if (css->parent)
225575313f4SWaiman Long snprintf(pbuf, sizeof(pbuf) - 1, " P=%d",
226575313f4SWaiman Long css->parent->id);
227*1900da52SFuqian Huang seq_printf(seq, "%2d: %-4s\t- %p[%d] %d%s\n", ss->id, ss->name,
228*1900da52SFuqian Huang css, css->id,
229575313f4SWaiman Long atomic_read(&css->online_cnt), pbuf);
230575313f4SWaiman Long }
231b6053d40STejun Heo
232b6053d40STejun Heo cgroup_kn_unlock(of->kn);
233575313f4SWaiman Long return 0;
234575313f4SWaiman Long }
235575313f4SWaiman Long
cgroup_masks_read_one(struct seq_file * seq,const char * name,u16 mask)2362866c0b4STejun Heo static void cgroup_masks_read_one(struct seq_file *seq, const char *name,
2372866c0b4STejun Heo u16 mask)
238575313f4SWaiman Long {
239575313f4SWaiman Long struct cgroup_subsys *ss;
2402866c0b4STejun Heo int ssid;
241575313f4SWaiman Long bool first = true;
242575313f4SWaiman Long
2432866c0b4STejun Heo seq_printf(seq, "%-17s: ", name);
2442866c0b4STejun Heo for_each_subsys(ss, ssid) {
2452866c0b4STejun Heo if (!(mask & (1 << ssid)))
246575313f4SWaiman Long continue;
247575313f4SWaiman Long if (!first)
248575313f4SWaiman Long seq_puts(seq, ", ");
249575313f4SWaiman Long seq_puts(seq, ss->name);
250575313f4SWaiman Long first = false;
251575313f4SWaiman Long }
252575313f4SWaiman Long seq_putc(seq, '\n');
253575313f4SWaiman Long }
254575313f4SWaiman Long
cgroup_masks_read(struct seq_file * seq,void * v)2552866c0b4STejun Heo static int cgroup_masks_read(struct seq_file *seq, void *v)
2562866c0b4STejun Heo {
257b6053d40STejun Heo struct kernfs_open_file *of = seq->private;
258b6053d40STejun Heo struct cgroup *cgrp;
2592866c0b4STejun Heo
260b6053d40STejun Heo cgrp = cgroup_kn_lock_live(of->kn, false);
261b6053d40STejun Heo if (!cgrp)
262b6053d40STejun Heo return -ENODEV;
263b6053d40STejun Heo
2642866c0b4STejun Heo cgroup_masks_read_one(seq, "subtree_control", cgrp->subtree_control);
2652866c0b4STejun Heo cgroup_masks_read_one(seq, "subtree_ss_mask", cgrp->subtree_ss_mask);
266b6053d40STejun Heo
267b6053d40STejun Heo cgroup_kn_unlock(of->kn);
268a28f8f5eSWaiman Long return 0;
269a28f8f5eSWaiman Long }
270a28f8f5eSWaiman Long
releasable_read(struct cgroup_subsys_state * css,struct cftype * cft)271a28f8f5eSWaiman Long static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
272a28f8f5eSWaiman Long {
273a28f8f5eSWaiman Long return (!cgroup_is_populated(css->cgroup) &&
274a28f8f5eSWaiman Long !css_has_online_children(&css->cgroup->self));
275a28f8f5eSWaiman Long }
276a28f8f5eSWaiman Long
2778cc38fa7STejun Heo static struct cftype debug_legacy_files[] = {
278a28f8f5eSWaiman Long {
279a28f8f5eSWaiman Long .name = "taskcount",
280a28f8f5eSWaiman Long .read_u64 = debug_taskcount_read,
281a28f8f5eSWaiman Long },
282a28f8f5eSWaiman Long
283a28f8f5eSWaiman Long {
284a28f8f5eSWaiman Long .name = "current_css_set",
285575313f4SWaiman Long .seq_show = current_css_set_read,
286575313f4SWaiman Long .flags = CFTYPE_ONLY_ON_ROOT,
287a28f8f5eSWaiman Long },
288a28f8f5eSWaiman Long
289a28f8f5eSWaiman Long {
290a28f8f5eSWaiman Long .name = "current_css_set_refcount",
291a28f8f5eSWaiman Long .read_u64 = current_css_set_refcount_read,
292575313f4SWaiman Long .flags = CFTYPE_ONLY_ON_ROOT,
293a28f8f5eSWaiman Long },
294a28f8f5eSWaiman Long
295a28f8f5eSWaiman Long {
296a28f8f5eSWaiman Long .name = "current_css_set_cg_links",
297a28f8f5eSWaiman Long .seq_show = current_css_set_cg_links_read,
298575313f4SWaiman Long .flags = CFTYPE_ONLY_ON_ROOT,
299a28f8f5eSWaiman Long },
300a28f8f5eSWaiman Long
301a28f8f5eSWaiman Long {
302a28f8f5eSWaiman Long .name = "cgroup_css_links",
303a28f8f5eSWaiman Long .seq_show = cgroup_css_links_read,
304a28f8f5eSWaiman Long },
305a28f8f5eSWaiman Long
306a28f8f5eSWaiman Long {
307575313f4SWaiman Long .name = "cgroup_subsys_states",
308575313f4SWaiman Long .seq_show = cgroup_subsys_states_read,
309575313f4SWaiman Long },
310575313f4SWaiman Long
311575313f4SWaiman Long {
312575313f4SWaiman Long .name = "cgroup_masks",
313575313f4SWaiman Long .seq_show = cgroup_masks_read,
314575313f4SWaiman Long },
315575313f4SWaiman Long
316575313f4SWaiman Long {
317a28f8f5eSWaiman Long .name = "releasable",
318a28f8f5eSWaiman Long .read_u64 = releasable_read,
319a28f8f5eSWaiman Long },
320a28f8f5eSWaiman Long
321a28f8f5eSWaiman Long { } /* terminate */
322a28f8f5eSWaiman Long };
323a28f8f5eSWaiman Long
3248cc38fa7STejun Heo static struct cftype debug_files[] = {
3258cc38fa7STejun Heo {
3268cc38fa7STejun Heo .name = "taskcount",
3278cc38fa7STejun Heo .read_u64 = debug_taskcount_read,
3288cc38fa7STejun Heo },
3298cc38fa7STejun Heo
3308cc38fa7STejun Heo {
3318cc38fa7STejun Heo .name = "current_css_set",
3328cc38fa7STejun Heo .seq_show = current_css_set_read,
3338cc38fa7STejun Heo .flags = CFTYPE_ONLY_ON_ROOT,
3348cc38fa7STejun Heo },
3358cc38fa7STejun Heo
3368cc38fa7STejun Heo {
3378cc38fa7STejun Heo .name = "current_css_set_refcount",
3388cc38fa7STejun Heo .read_u64 = current_css_set_refcount_read,
3398cc38fa7STejun Heo .flags = CFTYPE_ONLY_ON_ROOT,
3408cc38fa7STejun Heo },
3418cc38fa7STejun Heo
3428cc38fa7STejun Heo {
3438cc38fa7STejun Heo .name = "current_css_set_cg_links",
3448cc38fa7STejun Heo .seq_show = current_css_set_cg_links_read,
3458cc38fa7STejun Heo .flags = CFTYPE_ONLY_ON_ROOT,
3468cc38fa7STejun Heo },
3478cc38fa7STejun Heo
3488cc38fa7STejun Heo {
3498cc38fa7STejun Heo .name = "css_links",
3508cc38fa7STejun Heo .seq_show = cgroup_css_links_read,
3518cc38fa7STejun Heo },
3528cc38fa7STejun Heo
3538cc38fa7STejun Heo {
3548cc38fa7STejun Heo .name = "csses",
3558cc38fa7STejun Heo .seq_show = cgroup_subsys_states_read,
3568cc38fa7STejun Heo },
3578cc38fa7STejun Heo
3588cc38fa7STejun Heo {
3598cc38fa7STejun Heo .name = "masks",
3608cc38fa7STejun Heo .seq_show = cgroup_masks_read,
3618cc38fa7STejun Heo },
3628cc38fa7STejun Heo
3638cc38fa7STejun Heo { } /* terminate */
3648cc38fa7STejun Heo };
3658cc38fa7STejun Heo
366a28f8f5eSWaiman Long struct cgroup_subsys debug_cgrp_subsys = {
367a28f8f5eSWaiman Long .css_alloc = debug_css_alloc,
368a28f8f5eSWaiman Long .css_free = debug_css_free,
3698cc38fa7STejun Heo .legacy_cftypes = debug_legacy_files,
370a28f8f5eSWaiman Long };
3718cc38fa7STejun Heo
3728cc38fa7STejun Heo /*
3738cc38fa7STejun Heo * On v2, debug is an implicit controller enabled by "cgroup_debug" boot
3748cc38fa7STejun Heo * parameter.
3758cc38fa7STejun Heo */
enable_debug_cgroup(void)3765cf8114dSWaiman Long void __init enable_debug_cgroup(void)
3778cc38fa7STejun Heo {
3788cc38fa7STejun Heo debug_cgrp_subsys.dfl_cftypes = debug_files;
3798cc38fa7STejun Heo debug_cgrp_subsys.implicit_on_dfl = true;
3808cfd8147STejun Heo debug_cgrp_subsys.threaded = true;
3818cc38fa7STejun Heo }
382