Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Waiman Long | 23b0be4 | 2017-06-13 17:18:03 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Debug controller |
| 4 | * |
| 5 | * WARNING: This controller is for cgroup core debugging only. |
| 6 | * Its interfaces are unstable and subject to changes at any time. |
| 7 | */ |
Waiman Long | a28f8f5 | 2017-06-13 17:18:02 -0400 | [diff] [blame] | 8 | #include <linux/ctype.h> |
| 9 | #include <linux/mm.h> |
| 10 | #include <linux/slab.h> |
| 11 | |
| 12 | #include "cgroup-internal.h" |
| 13 | |
| 14 | static struct cgroup_subsys_state * |
| 15 | debug_css_alloc(struct cgroup_subsys_state *parent_css) |
| 16 | { |
| 17 | struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL); |
| 18 | |
| 19 | if (!css) |
| 20 | return ERR_PTR(-ENOMEM); |
| 21 | |
| 22 | return css; |
| 23 | } |
| 24 | |
| 25 | static void debug_css_free(struct cgroup_subsys_state *css) |
| 26 | { |
| 27 | kfree(css); |
| 28 | } |
| 29 | |
| 30 | /* |
| 31 | * debug_taskcount_read - return the number of tasks in a cgroup. |
| 32 | * @cgrp: the cgroup in question |
| 33 | */ |
| 34 | static u64 debug_taskcount_read(struct cgroup_subsys_state *css, |
| 35 | struct cftype *cft) |
| 36 | { |
| 37 | return cgroup_task_count(css->cgroup); |
| 38 | } |
| 39 | |
Waiman Long | 575313f | 2017-06-13 17:18:04 -0400 | [diff] [blame] | 40 | static int current_css_set_read(struct seq_file *seq, void *v) |
Waiman Long | a28f8f5 | 2017-06-13 17:18:02 -0400 | [diff] [blame] | 41 | { |
Tejun Heo | b6053d4 | 2017-06-14 16:01:41 -0400 | [diff] [blame] | 42 | struct kernfs_open_file *of = seq->private; |
Waiman Long | 575313f | 2017-06-13 17:18:04 -0400 | [diff] [blame] | 43 | struct css_set *cset; |
| 44 | struct cgroup_subsys *ss; |
| 45 | struct cgroup_subsys_state *css; |
| 46 | int i, refcnt; |
| 47 | |
Tejun Heo | b6053d4 | 2017-06-14 16:01:41 -0400 | [diff] [blame] | 48 | if (!cgroup_kn_lock_live(of->kn, false)) |
| 49 | return -ENODEV; |
| 50 | |
Waiman Long | 575313f | 2017-06-13 17:18:04 -0400 | [diff] [blame] | 51 | spin_lock_irq(&css_set_lock); |
| 52 | rcu_read_lock(); |
Wang Long | ddf7005 | 2017-11-19 16:08:37 -0500 | [diff] [blame] | 53 | cset = task_css_set(current); |
Waiman Long | 575313f | 2017-06-13 17:18:04 -0400 | [diff] [blame] | 54 | refcnt = refcount_read(&cset->refcount); |
| 55 | seq_printf(seq, "css_set %pK %d", cset, refcnt); |
| 56 | if (refcnt > cset->nr_tasks) |
| 57 | seq_printf(seq, " +%d", refcnt - cset->nr_tasks); |
| 58 | seq_puts(seq, "\n"); |
| 59 | |
| 60 | /* |
| 61 | * Print the css'es stored in the current css_set. |
| 62 | */ |
| 63 | for_each_subsys(ss, i) { |
| 64 | css = cset->subsys[ss->id]; |
| 65 | if (!css) |
| 66 | continue; |
Fuqian Huang | 1900da5 | 2019-04-21 19:47:27 +0800 | [diff] [blame] | 67 | seq_printf(seq, "%2d: %-4s\t- %p[%d]\n", ss->id, ss->name, |
| 68 | css, css->id); |
Waiman Long | 575313f | 2017-06-13 17:18:04 -0400 | [diff] [blame] | 69 | } |
| 70 | rcu_read_unlock(); |
| 71 | spin_unlock_irq(&css_set_lock); |
Tejun Heo | b6053d4 | 2017-06-14 16:01:41 -0400 | [diff] [blame] | 72 | cgroup_kn_unlock(of->kn); |
Waiman Long | 575313f | 2017-06-13 17:18:04 -0400 | [diff] [blame] | 73 | return 0; |
Waiman Long | a28f8f5 | 2017-06-13 17:18:02 -0400 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css, |
| 77 | struct cftype *cft) |
| 78 | { |
| 79 | u64 count; |
| 80 | |
| 81 | rcu_read_lock(); |
| 82 | count = refcount_read(&task_css_set(current)->refcount); |
| 83 | rcu_read_unlock(); |
| 84 | return count; |
| 85 | } |
| 86 | |
| 87 | static int current_css_set_cg_links_read(struct seq_file *seq, void *v) |
| 88 | { |
| 89 | struct cgrp_cset_link *link; |
| 90 | struct css_set *cset; |
| 91 | char *name_buf; |
| 92 | |
| 93 | name_buf = kmalloc(NAME_MAX + 1, GFP_KERNEL); |
| 94 | if (!name_buf) |
| 95 | return -ENOMEM; |
| 96 | |
| 97 | spin_lock_irq(&css_set_lock); |
| 98 | rcu_read_lock(); |
Wang Long | ddf7005 | 2017-11-19 16:08:37 -0500 | [diff] [blame] | 99 | cset = task_css_set(current); |
Waiman Long | a28f8f5 | 2017-06-13 17:18:02 -0400 | [diff] [blame] | 100 | list_for_each_entry(link, &cset->cgrp_links, cgrp_link) { |
| 101 | struct cgroup *c = link->cgrp; |
| 102 | |
| 103 | cgroup_name(c, name_buf, NAME_MAX + 1); |
| 104 | seq_printf(seq, "Root %d group %s\n", |
| 105 | c->root->hierarchy_id, name_buf); |
| 106 | } |
| 107 | rcu_read_unlock(); |
| 108 | spin_unlock_irq(&css_set_lock); |
| 109 | kfree(name_buf); |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | #define MAX_TASKS_SHOWN_PER_CSS 25 |
| 114 | static int cgroup_css_links_read(struct seq_file *seq, void *v) |
| 115 | { |
| 116 | struct cgroup_subsys_state *css = seq_css(seq); |
| 117 | struct cgrp_cset_link *link; |
Waiman Long | 7a0cf0e | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 118 | int dead_cnt = 0, extra_refs = 0, threaded_csets = 0; |
Waiman Long | a28f8f5 | 2017-06-13 17:18:02 -0400 | [diff] [blame] | 119 | |
| 120 | spin_lock_irq(&css_set_lock); |
Waiman Long | 7a0cf0e | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 121 | |
Waiman Long | a28f8f5 | 2017-06-13 17:18:02 -0400 | [diff] [blame] | 122 | list_for_each_entry(link, &css->cgroup->cset_links, cset_link) { |
| 123 | struct css_set *cset = link->cset; |
| 124 | struct task_struct *task; |
| 125 | int count = 0; |
Waiman Long | 575313f | 2017-06-13 17:18:04 -0400 | [diff] [blame] | 126 | int refcnt = refcount_read(&cset->refcount); |
Waiman Long | a28f8f5 | 2017-06-13 17:18:02 -0400 | [diff] [blame] | 127 | |
Waiman Long | 7a0cf0e | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 128 | /* |
| 129 | * Print out the proc_cset and threaded_cset relationship |
| 130 | * and highlight difference between refcount and task_count. |
| 131 | */ |
| 132 | seq_printf(seq, "css_set %pK", cset); |
| 133 | if (rcu_dereference_protected(cset->dom_cset, 1) != cset) { |
| 134 | threaded_csets++; |
| 135 | seq_printf(seq, "=>%pK", cset->dom_cset); |
| 136 | } |
| 137 | if (!list_empty(&cset->threaded_csets)) { |
| 138 | struct css_set *tcset; |
| 139 | int idx = 0; |
Waiman Long | 575313f | 2017-06-13 17:18:04 -0400 | [diff] [blame] | 140 | |
Waiman Long | 7a0cf0e | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 141 | list_for_each_entry(tcset, &cset->threaded_csets, |
| 142 | threaded_csets_node) { |
| 143 | seq_puts(seq, idx ? "," : "<="); |
| 144 | seq_printf(seq, "%pK", tcset); |
| 145 | idx++; |
| 146 | } |
| 147 | } else { |
| 148 | seq_printf(seq, " %d", refcnt); |
| 149 | if (refcnt - cset->nr_tasks > 0) { |
| 150 | int extra = refcnt - cset->nr_tasks; |
| 151 | |
| 152 | seq_printf(seq, " +%d", extra); |
| 153 | /* |
| 154 | * Take out the one additional reference in |
| 155 | * init_css_set. |
| 156 | */ |
| 157 | if (cset == &init_css_set) |
| 158 | extra--; |
| 159 | extra_refs += extra; |
| 160 | } |
Waiman Long | 575313f | 2017-06-13 17:18:04 -0400 | [diff] [blame] | 161 | } |
| 162 | seq_puts(seq, "\n"); |
Waiman Long | a28f8f5 | 2017-06-13 17:18:02 -0400 | [diff] [blame] | 163 | |
| 164 | list_for_each_entry(task, &cset->tasks, cg_list) { |
Waiman Long | 575313f | 2017-06-13 17:18:04 -0400 | [diff] [blame] | 165 | if (count++ <= MAX_TASKS_SHOWN_PER_CSS) |
| 166 | seq_printf(seq, " task %d\n", |
| 167 | task_pid_vnr(task)); |
Waiman Long | a28f8f5 | 2017-06-13 17:18:02 -0400 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | list_for_each_entry(task, &cset->mg_tasks, cg_list) { |
Waiman Long | 575313f | 2017-06-13 17:18:04 -0400 | [diff] [blame] | 171 | if (count++ <= MAX_TASKS_SHOWN_PER_CSS) |
| 172 | seq_printf(seq, " task %d\n", |
| 173 | task_pid_vnr(task)); |
Waiman Long | a28f8f5 | 2017-06-13 17:18:02 -0400 | [diff] [blame] | 174 | } |
Waiman Long | 575313f | 2017-06-13 17:18:04 -0400 | [diff] [blame] | 175 | /* show # of overflowed tasks */ |
| 176 | if (count > MAX_TASKS_SHOWN_PER_CSS) |
| 177 | seq_printf(seq, " ... (%d)\n", |
| 178 | count - MAX_TASKS_SHOWN_PER_CSS); |
| 179 | |
| 180 | if (cset->dead) { |
| 181 | seq_puts(seq, " [dead]\n"); |
| 182 | dead_cnt++; |
| 183 | } |
| 184 | |
| 185 | WARN_ON(count != cset->nr_tasks); |
Waiman Long | a28f8f5 | 2017-06-13 17:18:02 -0400 | [diff] [blame] | 186 | } |
| 187 | spin_unlock_irq(&css_set_lock); |
Waiman Long | 575313f | 2017-06-13 17:18:04 -0400 | [diff] [blame] | 188 | |
Waiman Long | 7a0cf0e | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 189 | if (!dead_cnt && !extra_refs && !threaded_csets) |
Waiman Long | 575313f | 2017-06-13 17:18:04 -0400 | [diff] [blame] | 190 | return 0; |
| 191 | |
| 192 | seq_puts(seq, "\n"); |
Waiman Long | 7a0cf0e | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 193 | if (threaded_csets) |
| 194 | seq_printf(seq, "threaded css_sets = %d\n", threaded_csets); |
Waiman Long | 575313f | 2017-06-13 17:18:04 -0400 | [diff] [blame] | 195 | if (extra_refs) |
| 196 | seq_printf(seq, "extra references = %d\n", extra_refs); |
| 197 | if (dead_cnt) |
| 198 | seq_printf(seq, "dead css_sets = %d\n", dead_cnt); |
| 199 | |
| 200 | return 0; |
| 201 | } |
| 202 | |
| 203 | static int cgroup_subsys_states_read(struct seq_file *seq, void *v) |
| 204 | { |
Tejun Heo | b6053d4 | 2017-06-14 16:01:41 -0400 | [diff] [blame] | 205 | struct kernfs_open_file *of = seq->private; |
| 206 | struct cgroup *cgrp; |
Waiman Long | 575313f | 2017-06-13 17:18:04 -0400 | [diff] [blame] | 207 | struct cgroup_subsys *ss; |
| 208 | struct cgroup_subsys_state *css; |
| 209 | char pbuf[16]; |
| 210 | int i; |
| 211 | |
Tejun Heo | b6053d4 | 2017-06-14 16:01:41 -0400 | [diff] [blame] | 212 | cgrp = cgroup_kn_lock_live(of->kn, false); |
| 213 | if (!cgrp) |
| 214 | return -ENODEV; |
| 215 | |
Waiman Long | 575313f | 2017-06-13 17:18:04 -0400 | [diff] [blame] | 216 | for_each_subsys(ss, i) { |
| 217 | css = rcu_dereference_check(cgrp->subsys[ss->id], true); |
| 218 | if (!css) |
| 219 | continue; |
| 220 | |
| 221 | pbuf[0] = '\0'; |
| 222 | |
| 223 | /* Show the parent CSS if applicable*/ |
| 224 | if (css->parent) |
| 225 | snprintf(pbuf, sizeof(pbuf) - 1, " P=%d", |
| 226 | css->parent->id); |
Fuqian Huang | 1900da5 | 2019-04-21 19:47:27 +0800 | [diff] [blame] | 227 | seq_printf(seq, "%2d: %-4s\t- %p[%d] %d%s\n", ss->id, ss->name, |
| 228 | css, css->id, |
Waiman Long | 575313f | 2017-06-13 17:18:04 -0400 | [diff] [blame] | 229 | atomic_read(&css->online_cnt), pbuf); |
| 230 | } |
Tejun Heo | b6053d4 | 2017-06-14 16:01:41 -0400 | [diff] [blame] | 231 | |
| 232 | cgroup_kn_unlock(of->kn); |
Waiman Long | 575313f | 2017-06-13 17:18:04 -0400 | [diff] [blame] | 233 | return 0; |
| 234 | } |
| 235 | |
Tejun Heo | 2866c0b | 2017-06-14 16:01:36 -0400 | [diff] [blame] | 236 | static void cgroup_masks_read_one(struct seq_file *seq, const char *name, |
| 237 | u16 mask) |
| 238 | { |
| 239 | struct cgroup_subsys *ss; |
| 240 | int ssid; |
| 241 | bool first = true; |
| 242 | |
| 243 | seq_printf(seq, "%-17s: ", name); |
| 244 | for_each_subsys(ss, ssid) { |
| 245 | if (!(mask & (1 << ssid))) |
| 246 | continue; |
| 247 | if (!first) |
| 248 | seq_puts(seq, ", "); |
| 249 | seq_puts(seq, ss->name); |
| 250 | first = false; |
| 251 | } |
| 252 | seq_putc(seq, '\n'); |
| 253 | } |
| 254 | |
Waiman Long | 575313f | 2017-06-13 17:18:04 -0400 | [diff] [blame] | 255 | static int cgroup_masks_read(struct seq_file *seq, void *v) |
| 256 | { |
Tejun Heo | b6053d4 | 2017-06-14 16:01:41 -0400 | [diff] [blame] | 257 | struct kernfs_open_file *of = seq->private; |
| 258 | struct cgroup *cgrp; |
Waiman Long | 575313f | 2017-06-13 17:18:04 -0400 | [diff] [blame] | 259 | |
Tejun Heo | b6053d4 | 2017-06-14 16:01:41 -0400 | [diff] [blame] | 260 | cgrp = cgroup_kn_lock_live(of->kn, false); |
| 261 | if (!cgrp) |
| 262 | return -ENODEV; |
| 263 | |
Tejun Heo | 2866c0b | 2017-06-14 16:01:36 -0400 | [diff] [blame] | 264 | cgroup_masks_read_one(seq, "subtree_control", cgrp->subtree_control); |
| 265 | cgroup_masks_read_one(seq, "subtree_ss_mask", cgrp->subtree_ss_mask); |
Tejun Heo | b6053d4 | 2017-06-14 16:01:41 -0400 | [diff] [blame] | 266 | |
| 267 | cgroup_kn_unlock(of->kn); |
Waiman Long | a28f8f5 | 2017-06-13 17:18:02 -0400 | [diff] [blame] | 268 | return 0; |
| 269 | } |
| 270 | |
| 271 | static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft) |
| 272 | { |
| 273 | return (!cgroup_is_populated(css->cgroup) && |
| 274 | !css_has_online_children(&css->cgroup->self)); |
| 275 | } |
| 276 | |
Tejun Heo | 8cc38fa | 2017-06-14 16:01:32 -0400 | [diff] [blame] | 277 | static struct cftype debug_legacy_files[] = { |
Waiman Long | a28f8f5 | 2017-06-13 17:18:02 -0400 | [diff] [blame] | 278 | { |
| 279 | .name = "taskcount", |
| 280 | .read_u64 = debug_taskcount_read, |
| 281 | }, |
| 282 | |
| 283 | { |
| 284 | .name = "current_css_set", |
Waiman Long | 575313f | 2017-06-13 17:18:04 -0400 | [diff] [blame] | 285 | .seq_show = current_css_set_read, |
| 286 | .flags = CFTYPE_ONLY_ON_ROOT, |
Waiman Long | a28f8f5 | 2017-06-13 17:18:02 -0400 | [diff] [blame] | 287 | }, |
| 288 | |
| 289 | { |
| 290 | .name = "current_css_set_refcount", |
| 291 | .read_u64 = current_css_set_refcount_read, |
Waiman Long | 575313f | 2017-06-13 17:18:04 -0400 | [diff] [blame] | 292 | .flags = CFTYPE_ONLY_ON_ROOT, |
Waiman Long | a28f8f5 | 2017-06-13 17:18:02 -0400 | [diff] [blame] | 293 | }, |
| 294 | |
| 295 | { |
| 296 | .name = "current_css_set_cg_links", |
| 297 | .seq_show = current_css_set_cg_links_read, |
Waiman Long | 575313f | 2017-06-13 17:18:04 -0400 | [diff] [blame] | 298 | .flags = CFTYPE_ONLY_ON_ROOT, |
Waiman Long | a28f8f5 | 2017-06-13 17:18:02 -0400 | [diff] [blame] | 299 | }, |
| 300 | |
| 301 | { |
| 302 | .name = "cgroup_css_links", |
| 303 | .seq_show = cgroup_css_links_read, |
| 304 | }, |
| 305 | |
| 306 | { |
Waiman Long | 575313f | 2017-06-13 17:18:04 -0400 | [diff] [blame] | 307 | .name = "cgroup_subsys_states", |
| 308 | .seq_show = cgroup_subsys_states_read, |
| 309 | }, |
| 310 | |
| 311 | { |
| 312 | .name = "cgroup_masks", |
| 313 | .seq_show = cgroup_masks_read, |
| 314 | }, |
| 315 | |
| 316 | { |
Waiman Long | a28f8f5 | 2017-06-13 17:18:02 -0400 | [diff] [blame] | 317 | .name = "releasable", |
| 318 | .read_u64 = releasable_read, |
| 319 | }, |
| 320 | |
| 321 | { } /* terminate */ |
| 322 | }; |
| 323 | |
Tejun Heo | 8cc38fa | 2017-06-14 16:01:32 -0400 | [diff] [blame] | 324 | static struct cftype debug_files[] = { |
| 325 | { |
| 326 | .name = "taskcount", |
| 327 | .read_u64 = debug_taskcount_read, |
| 328 | }, |
| 329 | |
| 330 | { |
| 331 | .name = "current_css_set", |
| 332 | .seq_show = current_css_set_read, |
| 333 | .flags = CFTYPE_ONLY_ON_ROOT, |
| 334 | }, |
| 335 | |
| 336 | { |
| 337 | .name = "current_css_set_refcount", |
| 338 | .read_u64 = current_css_set_refcount_read, |
| 339 | .flags = CFTYPE_ONLY_ON_ROOT, |
| 340 | }, |
| 341 | |
| 342 | { |
| 343 | .name = "current_css_set_cg_links", |
| 344 | .seq_show = current_css_set_cg_links_read, |
| 345 | .flags = CFTYPE_ONLY_ON_ROOT, |
| 346 | }, |
| 347 | |
| 348 | { |
| 349 | .name = "css_links", |
| 350 | .seq_show = cgroup_css_links_read, |
| 351 | }, |
| 352 | |
| 353 | { |
| 354 | .name = "csses", |
| 355 | .seq_show = cgroup_subsys_states_read, |
| 356 | }, |
| 357 | |
| 358 | { |
| 359 | .name = "masks", |
| 360 | .seq_show = cgroup_masks_read, |
| 361 | }, |
| 362 | |
| 363 | { } /* terminate */ |
| 364 | }; |
| 365 | |
Waiman Long | a28f8f5 | 2017-06-13 17:18:02 -0400 | [diff] [blame] | 366 | struct cgroup_subsys debug_cgrp_subsys = { |
Waiman Long | 575313f | 2017-06-13 17:18:04 -0400 | [diff] [blame] | 367 | .css_alloc = debug_css_alloc, |
| 368 | .css_free = debug_css_free, |
Tejun Heo | 8cc38fa | 2017-06-14 16:01:32 -0400 | [diff] [blame] | 369 | .legacy_cftypes = debug_legacy_files, |
Waiman Long | a28f8f5 | 2017-06-13 17:18:02 -0400 | [diff] [blame] | 370 | }; |
Tejun Heo | 8cc38fa | 2017-06-14 16:01:32 -0400 | [diff] [blame] | 371 | |
| 372 | /* |
| 373 | * On v2, debug is an implicit controller enabled by "cgroup_debug" boot |
| 374 | * parameter. |
| 375 | */ |
Waiman Long | 5cf8114 | 2018-11-08 10:08:46 -0500 | [diff] [blame] | 376 | void __init enable_debug_cgroup(void) |
Tejun Heo | 8cc38fa | 2017-06-14 16:01:32 -0400 | [diff] [blame] | 377 | { |
| 378 | debug_cgrp_subsys.dfl_cftypes = debug_files; |
| 379 | debug_cgrp_subsys.implicit_on_dfl = true; |
Tejun Heo | 8cfd814 | 2017-07-21 11:14:51 -0400 | [diff] [blame] | 380 | debug_cgrp_subsys.threaded = true; |
Tejun Heo | 8cc38fa | 2017-06-14 16:01:32 -0400 | [diff] [blame] | 381 | } |