1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Generic process-grouping system. 4 * 5 * Based originally on the cpuset system, extracted by Paul Menage 6 * Copyright (C) 2006 Google, Inc 7 * 8 * Notifications support 9 * Copyright (C) 2009 Nokia Corporation 10 * Author: Kirill A. Shutemov 11 * 12 * Copyright notices from the original cpuset code: 13 * -------------------------------------------------- 14 * Copyright (C) 2003 BULL SA. 15 * Copyright (C) 2004-2006 Silicon Graphics, Inc. 16 * 17 * Portions derived from Patrick Mochel's sysfs code. 18 * sysfs is Copyright (c) 2001-3 Patrick Mochel 19 * 20 * 2003-10-10 Written by Simon Derr. 21 * 2003-10-22 Updates by Stephen Hemminger. 22 * 2004 May-July Rework by Paul Jackson. 23 * --------------------------------------------------- 24 */ 25 26 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 27 28 #include "cgroup-internal.h" 29 30 #include <linux/bpf-cgroup.h> 31 #include <linux/cred.h> 32 #include <linux/errno.h> 33 #include <linux/init_task.h> 34 #include <linux/kernel.h> 35 #include <linux/magic.h> 36 #include <linux/mutex.h> 37 #include <linux/mount.h> 38 #include <linux/pagemap.h> 39 #include <linux/proc_fs.h> 40 #include <linux/rcupdate.h> 41 #include <linux/sched.h> 42 #include <linux/sched/task.h> 43 #include <linux/slab.h> 44 #include <linux/spinlock.h> 45 #include <linux/percpu-rwsem.h> 46 #include <linux/string.h> 47 #include <linux/hashtable.h> 48 #include <linux/idr.h> 49 #include <linux/kthread.h> 50 #include <linux/atomic.h> 51 #include <linux/cpuset.h> 52 #include <linux/proc_ns.h> 53 #include <linux/nsproxy.h> 54 #include <linux/file.h> 55 #include <linux/fs_parser.h> 56 #include <linux/sched/cputime.h> 57 #include <linux/sched/deadline.h> 58 #include <linux/psi.h> 59 #include <linux/nstree.h> 60 #include <linux/irq_work.h> 61 #include <net/sock.h> 62 63 #define CREATE_TRACE_POINTS 64 #include <trace/events/cgroup.h> 65 66 #define CGROUP_FILE_NAME_MAX (MAX_CGROUP_TYPE_NAMELEN + \ 67 MAX_CFTYPE_NAME + 2) 68 /* let's not notify more than 100 times per second */ 69 #define CGROUP_FILE_NOTIFY_MIN_INTV DIV_ROUND_UP(HZ, 100) 70 71 /* 72 * cgroup_mutex is the master lock. Any modification to cgroup or its 73 * hierarchy must be performed while holding it. 74 * 75 * css_set_lock protects task->cgroups pointer, the list of css_set 76 * objects, and the chain of tasks off each css_set. 77 * 78 * These locks are exported if CONFIG_PROVE_RCU so that accessors in 79 * cgroup.h can use them for lockdep annotations. 80 */ 81 DEFINE_MUTEX(cgroup_mutex); 82 DEFINE_SPINLOCK(css_set_lock); 83 84 #if (defined CONFIG_PROVE_RCU || defined CONFIG_LOCKDEP) 85 EXPORT_SYMBOL_GPL(cgroup_mutex); 86 EXPORT_SYMBOL_GPL(css_set_lock); 87 #endif 88 89 struct blocking_notifier_head cgroup_lifetime_notifier = 90 BLOCKING_NOTIFIER_INIT(cgroup_lifetime_notifier); 91 92 DEFINE_SPINLOCK(trace_cgroup_path_lock); 93 char trace_cgroup_path[TRACE_CGROUP_PATH_LEN]; 94 static bool cgroup_debug __read_mostly; 95 96 /* 97 * Protects cgroup_idr and css_idr so that IDs can be released without 98 * grabbing cgroup_mutex. 99 */ 100 static DEFINE_SPINLOCK(cgroup_idr_lock); 101 102 DEFINE_PERCPU_RWSEM(cgroup_threadgroup_rwsem); 103 104 #define cgroup_assert_mutex_or_rcu_locked() \ 105 RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \ 106 !lockdep_is_held(&cgroup_mutex), \ 107 "cgroup_mutex or RCU read lock required"); 108 109 /* 110 * cgroup destruction makes heavy use of work items and there can be a lot 111 * of concurrent destructions. Use a separate workqueue so that cgroup 112 * destruction work items don't end up filling up max_active of system_percpu_wq 113 * which may lead to deadlock. 114 * 115 * A cgroup destruction should enqueue work sequentially to: 116 * cgroup_offline_wq: use for css offline work 117 * cgroup_release_wq: use for css release work 118 * cgroup_free_wq: use for free work 119 * 120 * Rationale for using separate workqueues: 121 * The cgroup root free work may depend on completion of other css offline 122 * operations. If all tasks were enqueued to a single workqueue, this could 123 * create a deadlock scenario where: 124 * - Free work waits for other css offline work to complete. 125 * - But other css offline work is queued after free work in the same queue. 126 * 127 * Example deadlock scenario with single workqueue (cgroup_destroy_wq): 128 * 1. umount net_prio 129 * 2. net_prio root destruction enqueues work to cgroup_destroy_wq (CPUx) 130 * 3. perf_event CSS A offline enqueues work to same cgroup_destroy_wq (CPUx) 131 * 4. net_prio cgroup_destroy_root->cgroup_lock_and_drain_offline. 132 * 5. net_prio root destruction blocks waiting for perf_event CSS A offline, 133 * which can never complete as it's behind in the same queue and 134 * workqueue's max_active is 1. 135 */ 136 static struct workqueue_struct *cgroup_offline_wq; 137 static struct workqueue_struct *cgroup_release_wq; 138 static struct workqueue_struct *cgroup_free_wq; 139 140 /* generate an array of cgroup subsystem pointers */ 141 #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys, 142 struct cgroup_subsys *cgroup_subsys[] = { 143 #include <linux/cgroup_subsys.h> 144 }; 145 #undef SUBSYS 146 147 /* array of cgroup subsystem names */ 148 #define SUBSYS(_x) [_x ## _cgrp_id] = #_x, 149 static const char *cgroup_subsys_name[] = { 150 #include <linux/cgroup_subsys.h> 151 }; 152 #undef SUBSYS 153 154 /* array of static_keys for cgroup_subsys_enabled() and cgroup_subsys_on_dfl() */ 155 #define SUBSYS(_x) \ 156 DEFINE_STATIC_KEY_TRUE(_x ## _cgrp_subsys_enabled_key); \ 157 DEFINE_STATIC_KEY_TRUE(_x ## _cgrp_subsys_on_dfl_key); \ 158 EXPORT_SYMBOL_GPL(_x ## _cgrp_subsys_enabled_key); \ 159 EXPORT_SYMBOL_GPL(_x ## _cgrp_subsys_on_dfl_key); 160 #include <linux/cgroup_subsys.h> 161 #undef SUBSYS 162 163 #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys_enabled_key, 164 static struct static_key_true *cgroup_subsys_enabled_key[] = { 165 #include <linux/cgroup_subsys.h> 166 }; 167 #undef SUBSYS 168 169 #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys_on_dfl_key, 170 static struct static_key_true *cgroup_subsys_on_dfl_key[] = { 171 #include <linux/cgroup_subsys.h> 172 }; 173 #undef SUBSYS 174 175 static DEFINE_PER_CPU(struct css_rstat_cpu, root_rstat_cpu); 176 static DEFINE_PER_CPU(struct cgroup_rstat_base_cpu, root_rstat_base_cpu); 177 178 /* the default hierarchy */ 179 struct cgroup_root cgrp_dfl_root = { 180 .cgrp.self.rstat_cpu = &root_rstat_cpu, 181 .cgrp.rstat_base_cpu = &root_rstat_base_cpu, 182 }; 183 EXPORT_SYMBOL_GPL(cgrp_dfl_root); 184 185 /* 186 * The default hierarchy always exists but is hidden until mounted for the 187 * first time. This is for backward compatibility. 188 */ 189 bool cgrp_dfl_visible; 190 191 /* some controllers are not supported in the default hierarchy */ 192 static u32 cgrp_dfl_inhibit_ss_mask; 193 194 /* some controllers are implicitly enabled on the default hierarchy */ 195 static u32 cgrp_dfl_implicit_ss_mask; 196 197 /* some controllers can be threaded on the default hierarchy */ 198 static u32 cgrp_dfl_threaded_ss_mask; 199 200 /* The list of hierarchy roots */ 201 LIST_HEAD(cgroup_roots); 202 static int cgroup_root_count; 203 204 /* hierarchy ID allocation and mapping, protected by cgroup_mutex */ 205 static DEFINE_IDR(cgroup_hierarchy_idr); 206 207 /* 208 * Assign a monotonically increasing serial number to csses. It guarantees 209 * cgroups with bigger numbers are newer than those with smaller numbers. 210 * Also, as csses are always appended to the parent's ->children list, it 211 * guarantees that sibling csses are always sorted in the ascending serial 212 * number order on the list. Protected by cgroup_mutex. 213 */ 214 static u64 css_serial_nr_next = 1; 215 216 /* 217 * These bitmasks identify subsystems with specific features to avoid 218 * having to do iterative checks repeatedly. 219 */ 220 static u32 have_fork_callback __read_mostly; 221 static u32 have_exit_callback __read_mostly; 222 static u32 have_release_callback __read_mostly; 223 static u32 have_canfork_callback __read_mostly; 224 225 static bool have_favordynmods __ro_after_init = IS_ENABLED(CONFIG_CGROUP_FAVOR_DYNMODS); 226 227 /* 228 * Write protected by cgroup_mutex and write-lock of cgroup_threadgroup_rwsem, 229 * read protected by either. 230 * 231 * Can only be turned on, but not turned off. 232 */ 233 bool cgroup_enable_per_threadgroup_rwsem __read_mostly; 234 235 /* cgroup namespace for init task */ 236 struct cgroup_namespace init_cgroup_ns = { 237 .ns = NS_COMMON_INIT(init_cgroup_ns), 238 .user_ns = &init_user_ns, 239 .root_cset = &init_css_set, 240 }; 241 242 static struct file_system_type cgroup2_fs_type; 243 static struct cftype cgroup_base_files[]; 244 static struct cftype cgroup_psi_files[]; 245 246 /* cgroup optional features */ 247 enum cgroup_opt_features { 248 #ifdef CONFIG_PSI 249 OPT_FEATURE_PRESSURE, 250 #endif 251 OPT_FEATURE_COUNT 252 }; 253 254 static const char *cgroup_opt_feature_names[OPT_FEATURE_COUNT] = { 255 #ifdef CONFIG_PSI 256 "pressure", 257 #endif 258 }; 259 260 static u16 cgroup_feature_disable_mask __read_mostly; 261 262 static int cgroup_apply_control(struct cgroup *cgrp); 263 static void cgroup_finalize_control(struct cgroup *cgrp, int ret); 264 static void css_task_iter_skip(struct css_task_iter *it, 265 struct task_struct *task); 266 static int cgroup_destroy_locked(struct cgroup *cgrp); 267 static struct cgroup_subsys_state *css_create(struct cgroup *cgrp, 268 struct cgroup_subsys *ss); 269 static void css_release(struct percpu_ref *ref); 270 static void kill_css(struct cgroup_subsys_state *css); 271 static int cgroup_addrm_files(struct cgroup_subsys_state *css, 272 struct cgroup *cgrp, struct cftype cfts[], 273 bool is_add); 274 static void cgroup_rt_init(void); 275 276 #ifdef CONFIG_DEBUG_CGROUP_REF 277 #define CGROUP_REF_FN_ATTRS noinline 278 #define CGROUP_REF_EXPORT(fn) EXPORT_SYMBOL_GPL(fn); 279 #include <linux/cgroup_refcnt.h> 280 #endif 281 282 /** 283 * cgroup_ssid_enabled - cgroup subsys enabled test by subsys ID 284 * @ssid: subsys ID of interest 285 * 286 * cgroup_subsys_enabled() can only be used with literal subsys names which 287 * is fine for individual subsystems but unsuitable for cgroup core. This 288 * is slower static_key_enabled() based test indexed by @ssid. 289 */ 290 bool cgroup_ssid_enabled(int ssid) 291 { 292 if (!CGROUP_HAS_SUBSYS_CONFIG) 293 return false; 294 295 return static_key_enabled(cgroup_subsys_enabled_key[ssid]); 296 } 297 298 /** 299 * cgroup_on_dfl - test whether a cgroup is on the default hierarchy 300 * @cgrp: the cgroup of interest 301 * 302 * The default hierarchy is the v2 interface of cgroup and this function 303 * can be used to test whether a cgroup is on the default hierarchy for 304 * cases where a subsystem should behave differently depending on the 305 * interface version. 306 * 307 * List of changed behaviors: 308 * 309 * - Mount options "noprefix", "xattr", "clone_children", "release_agent" 310 * and "name" are disallowed. 311 * 312 * - When mounting an existing superblock, mount options should match. 313 * 314 * - rename(2) is disallowed. 315 * 316 * - "tasks" is removed. Everything should be at process granularity. Use 317 * "cgroup.procs" instead. 318 * 319 * - "cgroup.procs" is not sorted. pids will be unique unless they got 320 * recycled in-between reads. 321 * 322 * - "release_agent" and "notify_on_release" are removed. Replacement 323 * notification mechanism will be implemented. 324 * 325 * - "cgroup.clone_children" is removed. 326 * 327 * - "cgroup.subtree_populated" is available. Its value is 0 if the cgroup 328 * and its descendants contain no task; otherwise, 1. The file also 329 * generates kernfs notification which can be monitored through poll and 330 * [di]notify when the value of the file changes. 331 * 332 * - cpuset: tasks will be kept in empty cpusets when hotplug happens and 333 * take masks of ancestors with non-empty cpus/mems, instead of being 334 * moved to an ancestor. 335 * 336 * - cpuset: a task can be moved into an empty cpuset, and again it takes 337 * masks of ancestors. 338 * 339 * - blkcg: blk-throttle becomes properly hierarchical. 340 */ 341 bool cgroup_on_dfl(const struct cgroup *cgrp) 342 { 343 return cgrp->root == &cgrp_dfl_root; 344 } 345 346 /* IDR wrappers which synchronize using cgroup_idr_lock */ 347 static int cgroup_idr_alloc(struct idr *idr, void *ptr, int start, int end, 348 gfp_t gfp_mask) 349 { 350 int ret; 351 352 idr_preload(gfp_mask); 353 spin_lock_bh(&cgroup_idr_lock); 354 ret = idr_alloc(idr, ptr, start, end, gfp_mask & ~__GFP_DIRECT_RECLAIM); 355 spin_unlock_bh(&cgroup_idr_lock); 356 idr_preload_end(); 357 return ret; 358 } 359 360 static void *cgroup_idr_replace(struct idr *idr, void *ptr, int id) 361 { 362 void *ret; 363 364 spin_lock_bh(&cgroup_idr_lock); 365 ret = idr_replace(idr, ptr, id); 366 spin_unlock_bh(&cgroup_idr_lock); 367 return ret; 368 } 369 370 static void cgroup_idr_remove(struct idr *idr, int id) 371 { 372 spin_lock_bh(&cgroup_idr_lock); 373 idr_remove(idr, id); 374 spin_unlock_bh(&cgroup_idr_lock); 375 } 376 377 static bool cgroup_has_tasks(struct cgroup *cgrp) 378 { 379 return cgrp->nr_populated_csets; 380 } 381 382 static bool cgroup_is_threaded(struct cgroup *cgrp) 383 { 384 return cgrp->dom_cgrp != cgrp; 385 } 386 387 /* can @cgrp host both domain and threaded children? */ 388 static bool cgroup_is_mixable(struct cgroup *cgrp) 389 { 390 /* 391 * Root isn't under domain level resource control exempting it from 392 * the no-internal-process constraint, so it can serve as a thread 393 * root and a parent of resource domains at the same time. 394 */ 395 return !cgroup_parent(cgrp); 396 } 397 398 /* can @cgrp become a thread root? Should always be true for a thread root */ 399 static bool cgroup_can_be_thread_root(struct cgroup *cgrp) 400 { 401 /* mixables don't care */ 402 if (cgroup_is_mixable(cgrp)) 403 return true; 404 405 /* domain roots can't be nested under threaded */ 406 if (cgroup_is_threaded(cgrp)) 407 return false; 408 409 /* can only have either domain or threaded children */ 410 if (cgrp->nr_populated_domain_children) 411 return false; 412 413 /* and no domain controllers can be enabled */ 414 if (cgrp->subtree_control & ~cgrp_dfl_threaded_ss_mask) 415 return false; 416 417 return true; 418 } 419 420 /* is @cgrp root of a threaded subtree? */ 421 static bool cgroup_is_thread_root(struct cgroup *cgrp) 422 { 423 /* thread root should be a domain */ 424 if (cgroup_is_threaded(cgrp)) 425 return false; 426 427 /* a domain w/ threaded children is a thread root */ 428 if (cgrp->nr_threaded_children) 429 return true; 430 431 /* 432 * A domain which has tasks and explicit threaded controllers 433 * enabled is a thread root. 434 */ 435 if (cgroup_has_tasks(cgrp) && 436 (cgrp->subtree_control & cgrp_dfl_threaded_ss_mask)) 437 return true; 438 439 return false; 440 } 441 442 /* a domain which isn't connected to the root w/o brekage can't be used */ 443 static bool cgroup_is_valid_domain(struct cgroup *cgrp) 444 { 445 /* the cgroup itself can be a thread root */ 446 if (cgroup_is_threaded(cgrp)) 447 return false; 448 449 /* but the ancestors can't be unless mixable */ 450 while ((cgrp = cgroup_parent(cgrp))) { 451 if (!cgroup_is_mixable(cgrp) && cgroup_is_thread_root(cgrp)) 452 return false; 453 if (cgroup_is_threaded(cgrp)) 454 return false; 455 } 456 457 return true; 458 } 459 460 /* subsystems visibly enabled on a cgroup */ 461 static u32 cgroup_control(struct cgroup *cgrp) 462 { 463 struct cgroup *parent = cgroup_parent(cgrp); 464 u32 root_ss_mask = cgrp->root->subsys_mask; 465 466 if (parent) { 467 u32 ss_mask = parent->subtree_control; 468 469 /* threaded cgroups can only have threaded controllers */ 470 if (cgroup_is_threaded(cgrp)) 471 ss_mask &= cgrp_dfl_threaded_ss_mask; 472 return ss_mask; 473 } 474 475 if (cgroup_on_dfl(cgrp)) 476 root_ss_mask &= ~(cgrp_dfl_inhibit_ss_mask | 477 cgrp_dfl_implicit_ss_mask); 478 return root_ss_mask; 479 } 480 481 /* subsystems enabled on a cgroup */ 482 static u32 cgroup_ss_mask(struct cgroup *cgrp) 483 { 484 struct cgroup *parent = cgroup_parent(cgrp); 485 486 if (parent) { 487 u32 ss_mask = parent->subtree_ss_mask; 488 489 /* threaded cgroups can only have threaded controllers */ 490 if (cgroup_is_threaded(cgrp)) 491 ss_mask &= cgrp_dfl_threaded_ss_mask; 492 return ss_mask; 493 } 494 495 return cgrp->root->subsys_mask; 496 } 497 498 /** 499 * cgroup_e_css_by_mask - obtain a cgroup's effective css for the specified ss 500 * @cgrp: the cgroup of interest 501 * @ss: the subsystem of interest (%NULL returns @cgrp->self) 502 * 503 * Similar to cgroup_css() but returns the effective css, which is defined 504 * as the matching css of the nearest ancestor including self which has @ss 505 * enabled. If @ss is associated with the hierarchy @cgrp is on, this 506 * function is guaranteed to return non-NULL css. 507 */ 508 static struct cgroup_subsys_state *cgroup_e_css_by_mask(struct cgroup *cgrp, 509 struct cgroup_subsys *ss) 510 { 511 lockdep_assert_held(&cgroup_mutex); 512 513 if (!ss) 514 return &cgrp->self; 515 516 /* 517 * This function is used while updating css associations and thus 518 * can't test the csses directly. Test ss_mask. 519 */ 520 while (!(cgroup_ss_mask(cgrp) & (1 << ss->id))) { 521 cgrp = cgroup_parent(cgrp); 522 if (!cgrp) 523 return NULL; 524 } 525 526 return cgroup_css(cgrp, ss); 527 } 528 529 /** 530 * cgroup_e_css - obtain a cgroup's effective css for the specified subsystem 531 * @cgrp: the cgroup of interest 532 * @ss: the subsystem of interest 533 * 534 * Find and get the effective css of @cgrp for @ss. The effective css is 535 * defined as the matching css of the nearest ancestor including self which 536 * has @ss enabled. If @ss is not mounted on the hierarchy @cgrp is on, 537 * the root css is returned, so this function always returns a valid css. 538 * 539 * The returned css is not guaranteed to be online, and therefore it is the 540 * callers responsibility to try get a reference for it. 541 */ 542 struct cgroup_subsys_state *cgroup_e_css(struct cgroup *cgrp, 543 struct cgroup_subsys *ss) 544 { 545 struct cgroup_subsys_state *css; 546 547 if (!CGROUP_HAS_SUBSYS_CONFIG) 548 return NULL; 549 550 do { 551 css = cgroup_css(cgrp, ss); 552 553 if (css) 554 return css; 555 cgrp = cgroup_parent(cgrp); 556 } while (cgrp); 557 558 return init_css_set.subsys[ss->id]; 559 } 560 561 /** 562 * cgroup_get_e_css - get a cgroup's effective css for the specified subsystem 563 * @cgrp: the cgroup of interest 564 * @ss: the subsystem of interest 565 * 566 * Find and get the effective css of @cgrp for @ss. The effective css is 567 * defined as the matching css of the nearest ancestor including self which 568 * has @ss enabled. If @ss is not mounted on the hierarchy @cgrp is on, 569 * the root css is returned, so this function always returns a valid css. 570 * The returned css must be put using css_put(). 571 */ 572 struct cgroup_subsys_state *cgroup_get_e_css(struct cgroup *cgrp, 573 struct cgroup_subsys *ss) 574 { 575 struct cgroup_subsys_state *css; 576 577 if (!CGROUP_HAS_SUBSYS_CONFIG) 578 return NULL; 579 580 rcu_read_lock(); 581 582 do { 583 css = cgroup_css(cgrp, ss); 584 585 if (css && css_tryget_online(css)) 586 goto out_unlock; 587 cgrp = cgroup_parent(cgrp); 588 } while (cgrp); 589 590 css = init_css_set.subsys[ss->id]; 591 css_get(css); 592 out_unlock: 593 rcu_read_unlock(); 594 return css; 595 } 596 EXPORT_SYMBOL_GPL(cgroup_get_e_css); 597 598 static void cgroup_get_live(struct cgroup *cgrp) 599 { 600 WARN_ON_ONCE(cgroup_is_dead(cgrp)); 601 cgroup_get(cgrp); 602 } 603 604 /** 605 * __cgroup_task_count - count the number of tasks in a cgroup. The caller 606 * is responsible for taking the css_set_lock. 607 * @cgrp: the cgroup in question 608 */ 609 int __cgroup_task_count(const struct cgroup *cgrp) 610 { 611 int count = 0; 612 struct cgrp_cset_link *link; 613 614 lockdep_assert_held(&css_set_lock); 615 616 list_for_each_entry(link, &cgrp->cset_links, cset_link) 617 count += link->cset->nr_tasks; 618 619 return count; 620 } 621 622 /** 623 * cgroup_task_count - count the number of tasks in a cgroup. 624 * @cgrp: the cgroup in question 625 */ 626 int cgroup_task_count(const struct cgroup *cgrp) 627 { 628 int count; 629 630 spin_lock_irq(&css_set_lock); 631 count = __cgroup_task_count(cgrp); 632 spin_unlock_irq(&css_set_lock); 633 634 return count; 635 } 636 637 static struct cgroup *kn_priv(struct kernfs_node *kn) 638 { 639 struct kernfs_node *parent; 640 /* 641 * The parent can not be replaced due to KERNFS_ROOT_INVARIANT_PARENT. 642 * Therefore it is always safe to dereference this pointer outside of a 643 * RCU section. 644 */ 645 parent = rcu_dereference_check(kn->__parent, 646 kernfs_root_flags(kn) & KERNFS_ROOT_INVARIANT_PARENT); 647 return parent->priv; 648 } 649 650 struct cgroup_subsys_state *of_css(struct kernfs_open_file *of) 651 { 652 struct cgroup *cgrp = kn_priv(of->kn); 653 struct cftype *cft = of_cft(of); 654 655 /* 656 * This is open and unprotected implementation of cgroup_css(). 657 * seq_css() is only called from a kernfs file operation which has 658 * an active reference on the file. Because all the subsystem 659 * files are drained before a css is disassociated with a cgroup, 660 * the matching css from the cgroup's subsys table is guaranteed to 661 * be and stay valid until the enclosing operation is complete. 662 */ 663 if (CGROUP_HAS_SUBSYS_CONFIG && cft->ss) 664 return rcu_dereference_raw(cgrp->subsys[cft->ss->id]); 665 else 666 return &cgrp->self; 667 } 668 EXPORT_SYMBOL_GPL(of_css); 669 670 /** 671 * for_each_css - iterate all css's of a cgroup 672 * @css: the iteration cursor 673 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end 674 * @cgrp: the target cgroup to iterate css's of 675 * 676 * Should be called under cgroup_mutex. 677 */ 678 #define for_each_css(css, ssid, cgrp) \ 679 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \ 680 if (!((css) = rcu_dereference_check( \ 681 (cgrp)->subsys[(ssid)], \ 682 lockdep_is_held(&cgroup_mutex)))) { } \ 683 else 684 685 /** 686 * do_each_subsys_mask - filter for_each_subsys with a bitmask 687 * @ss: the iteration cursor 688 * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end 689 * @ss_mask: the bitmask 690 * 691 * The block will only run for cases where the ssid-th bit (1 << ssid) of 692 * @ss_mask is set. 693 */ 694 #define do_each_subsys_mask(ss, ssid, ss_mask) do { \ 695 unsigned long __ss_mask = (ss_mask); \ 696 if (!CGROUP_HAS_SUBSYS_CONFIG) { \ 697 (ssid) = 0; \ 698 break; \ 699 } \ 700 for_each_set_bit(ssid, &__ss_mask, CGROUP_SUBSYS_COUNT) { \ 701 (ss) = cgroup_subsys[ssid]; \ 702 { 703 704 #define while_each_subsys_mask() \ 705 } \ 706 } \ 707 } while (false) 708 709 /* 710 * The default css_set - used by init and its children prior to any 711 * hierarchies being mounted. It contains a pointer to the root state 712 * for each subsystem. Also used to anchor the list of css_sets. Not 713 * reference-counted, to improve performance when child cgroups 714 * haven't been created. 715 */ 716 struct css_set init_css_set = { 717 .refcount = REFCOUNT_INIT(1), 718 .dom_cset = &init_css_set, 719 .tasks = LIST_HEAD_INIT(init_css_set.tasks), 720 .mg_tasks = LIST_HEAD_INIT(init_css_set.mg_tasks), 721 .dying_tasks = LIST_HEAD_INIT(init_css_set.dying_tasks), 722 .task_iters = LIST_HEAD_INIT(init_css_set.task_iters), 723 .threaded_csets = LIST_HEAD_INIT(init_css_set.threaded_csets), 724 .cgrp_links = LIST_HEAD_INIT(init_css_set.cgrp_links), 725 .mg_src_preload_node = LIST_HEAD_INIT(init_css_set.mg_src_preload_node), 726 .mg_dst_preload_node = LIST_HEAD_INIT(init_css_set.mg_dst_preload_node), 727 .mg_node = LIST_HEAD_INIT(init_css_set.mg_node), 728 729 /* 730 * The following field is re-initialized when this cset gets linked 731 * in cgroup_init(). However, let's initialize the field 732 * statically too so that the default cgroup can be accessed safely 733 * early during boot. 734 */ 735 .dfl_cgrp = &cgrp_dfl_root.cgrp, 736 }; 737 738 static int css_set_count = 1; /* 1 for init_css_set */ 739 740 static bool css_set_threaded(struct css_set *cset) 741 { 742 return cset->dom_cset != cset; 743 } 744 745 /** 746 * css_set_populated - does a css_set contain any tasks? 747 * @cset: target css_set 748 * 749 * css_set_populated() should be the same as !!cset->nr_tasks at steady 750 * state. However, css_set_populated() can be called while a task is being 751 * added to or removed from the linked list before the nr_tasks is 752 * properly updated. Hence, we can't just look at ->nr_tasks here. 753 */ 754 static bool css_set_populated(struct css_set *cset) 755 { 756 lockdep_assert_held(&css_set_lock); 757 758 return !list_empty(&cset->tasks) || !list_empty(&cset->mg_tasks); 759 } 760 761 /** 762 * cgroup_update_populated - update the populated count of a cgroup 763 * @cgrp: the target cgroup 764 * @populated: inc or dec populated count 765 * 766 * One of the css_sets associated with @cgrp is either getting its first 767 * task or losing the last. Update @cgrp->nr_populated_* accordingly. The 768 * count is propagated towards root so that a given cgroup's 769 * nr_populated_children is zero iff none of its descendants contain any 770 * tasks. 771 * 772 * @cgrp's interface file "cgroup.populated" is zero if both 773 * @cgrp->nr_populated_csets and @cgrp->nr_populated_children are zero and 774 * 1 otherwise. When the sum changes from or to zero, userland is notified 775 * that the content of the interface file has changed. This can be used to 776 * detect when @cgrp and its descendants become populated or empty. 777 */ 778 static void cgroup_update_populated(struct cgroup *cgrp, bool populated) 779 { 780 struct cgroup *child = NULL; 781 int adj = populated ? 1 : -1; 782 783 lockdep_assert_held(&css_set_lock); 784 785 do { 786 bool was_populated = cgroup_is_populated(cgrp); 787 788 if (!child) { 789 cgrp->nr_populated_csets += adj; 790 } else { 791 if (cgroup_is_threaded(child)) 792 cgrp->nr_populated_threaded_children += adj; 793 else 794 cgrp->nr_populated_domain_children += adj; 795 } 796 797 if (was_populated == cgroup_is_populated(cgrp)) 798 break; 799 800 cgroup1_check_for_release(cgrp); 801 TRACE_CGROUP_PATH(notify_populated, cgrp, 802 cgroup_is_populated(cgrp)); 803 cgroup_file_notify(&cgrp->events_file); 804 805 child = cgrp; 806 cgrp = cgroup_parent(cgrp); 807 } while (cgrp); 808 } 809 810 /** 811 * css_set_update_populated - update populated state of a css_set 812 * @cset: target css_set 813 * @populated: whether @cset is populated or depopulated 814 * 815 * @cset is either getting the first task or losing the last. Update the 816 * populated counters of all associated cgroups accordingly. 817 */ 818 static void css_set_update_populated(struct css_set *cset, bool populated) 819 { 820 struct cgrp_cset_link *link; 821 822 lockdep_assert_held(&css_set_lock); 823 824 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) 825 cgroup_update_populated(link->cgrp, populated); 826 } 827 828 /* 829 * @task is leaving, advance task iterators which are pointing to it so 830 * that they can resume at the next position. Advancing an iterator might 831 * remove it from the list, use safe walk. See css_task_iter_skip() for 832 * details. 833 */ 834 static void css_set_skip_task_iters(struct css_set *cset, 835 struct task_struct *task) 836 { 837 struct css_task_iter *it, *pos; 838 839 list_for_each_entry_safe(it, pos, &cset->task_iters, iters_node) 840 css_task_iter_skip(it, task); 841 } 842 843 /** 844 * css_set_move_task - move a task from one css_set to another 845 * @task: task being moved 846 * @from_cset: css_set @task currently belongs to (may be NULL) 847 * @to_cset: new css_set @task is being moved to (may be NULL) 848 * @use_mg_tasks: move to @to_cset->mg_tasks instead of ->tasks 849 * 850 * Move @task from @from_cset to @to_cset. If @task didn't belong to any 851 * css_set, @from_cset can be NULL. If @task is being disassociated 852 * instead of moved, @to_cset can be NULL. 853 * 854 * This function automatically handles populated counter updates and 855 * css_task_iter adjustments but the caller is responsible for managing 856 * @from_cset and @to_cset's reference counts. 857 */ 858 static void css_set_move_task(struct task_struct *task, 859 struct css_set *from_cset, struct css_set *to_cset, 860 bool use_mg_tasks) 861 { 862 lockdep_assert_held(&css_set_lock); 863 864 if (to_cset && !css_set_populated(to_cset)) 865 css_set_update_populated(to_cset, true); 866 867 if (from_cset) { 868 WARN_ON_ONCE(list_empty(&task->cg_list)); 869 870 css_set_skip_task_iters(from_cset, task); 871 list_del_init(&task->cg_list); 872 if (!css_set_populated(from_cset)) 873 css_set_update_populated(from_cset, false); 874 } else { 875 WARN_ON_ONCE(!list_empty(&task->cg_list)); 876 } 877 878 if (to_cset) { 879 /* 880 * We are synchronized through cgroup_threadgroup_rwsem 881 * against PF_EXITING setting such that we can't race 882 * against cgroup_task_dead()/cgroup_task_free() dropping 883 * the css_set. 884 */ 885 WARN_ON_ONCE(task->flags & PF_EXITING); 886 887 cgroup_move_task(task, to_cset); 888 list_add_tail(&task->cg_list, use_mg_tasks ? &to_cset->mg_tasks : 889 &to_cset->tasks); 890 } 891 } 892 893 /* 894 * hash table for cgroup groups. This improves the performance to find 895 * an existing css_set. This hash doesn't (currently) take into 896 * account cgroups in empty hierarchies. 897 */ 898 #define CSS_SET_HASH_BITS 7 899 static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS); 900 901 static unsigned long css_set_hash(struct cgroup_subsys_state **css) 902 { 903 unsigned long key = 0UL; 904 struct cgroup_subsys *ss; 905 int i; 906 907 for_each_subsys(ss, i) 908 key += (unsigned long)css[i]; 909 key = (key >> 16) ^ key; 910 911 return key; 912 } 913 914 void put_css_set_locked(struct css_set *cset) 915 { 916 struct cgrp_cset_link *link, *tmp_link; 917 struct cgroup_subsys *ss; 918 int ssid; 919 920 lockdep_assert_held(&css_set_lock); 921 922 if (!refcount_dec_and_test(&cset->refcount)) 923 return; 924 925 WARN_ON_ONCE(!list_empty(&cset->threaded_csets)); 926 927 /* This css_set is dead. Unlink it and release cgroup and css refs */ 928 for_each_subsys(ss, ssid) { 929 list_del(&cset->e_cset_node[ssid]); 930 css_put(cset->subsys[ssid]); 931 } 932 hash_del(&cset->hlist); 933 css_set_count--; 934 935 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) { 936 list_del(&link->cset_link); 937 list_del(&link->cgrp_link); 938 if (cgroup_parent(link->cgrp)) 939 cgroup_put(link->cgrp); 940 kfree(link); 941 } 942 943 if (css_set_threaded(cset)) { 944 list_del(&cset->threaded_csets_node); 945 put_css_set_locked(cset->dom_cset); 946 } 947 948 kfree_rcu(cset, rcu_head); 949 } 950 951 /** 952 * compare_css_sets - helper function for find_existing_css_set(). 953 * @cset: candidate css_set being tested 954 * @old_cset: existing css_set for a task 955 * @new_cgrp: cgroup that's being entered by the task 956 * @template: desired set of css pointers in css_set (pre-calculated) 957 * 958 * Returns true if "cset" matches "old_cset" except for the hierarchy 959 * which "new_cgrp" belongs to, for which it should match "new_cgrp". 960 */ 961 static bool compare_css_sets(struct css_set *cset, 962 struct css_set *old_cset, 963 struct cgroup *new_cgrp, 964 struct cgroup_subsys_state *template[]) 965 { 966 struct cgroup *new_dfl_cgrp; 967 struct list_head *l1, *l2; 968 969 /* 970 * On the default hierarchy, there can be csets which are 971 * associated with the same set of cgroups but different csses. 972 * Let's first ensure that csses match. 973 */ 974 if (memcmp(template, cset->subsys, sizeof(cset->subsys))) 975 return false; 976 977 978 /* @cset's domain should match the default cgroup's */ 979 if (cgroup_on_dfl(new_cgrp)) 980 new_dfl_cgrp = new_cgrp; 981 else 982 new_dfl_cgrp = old_cset->dfl_cgrp; 983 984 if (new_dfl_cgrp->dom_cgrp != cset->dom_cset->dfl_cgrp) 985 return false; 986 987 /* 988 * Compare cgroup pointers in order to distinguish between 989 * different cgroups in hierarchies. As different cgroups may 990 * share the same effective css, this comparison is always 991 * necessary. 992 */ 993 l1 = &cset->cgrp_links; 994 l2 = &old_cset->cgrp_links; 995 while (1) { 996 struct cgrp_cset_link *link1, *link2; 997 struct cgroup *cgrp1, *cgrp2; 998 999 l1 = l1->next; 1000 l2 = l2->next; 1001 /* See if we reached the end - both lists are equal length. */ 1002 if (l1 == &cset->cgrp_links) { 1003 BUG_ON(l2 != &old_cset->cgrp_links); 1004 break; 1005 } else { 1006 BUG_ON(l2 == &old_cset->cgrp_links); 1007 } 1008 /* Locate the cgroups associated with these links. */ 1009 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link); 1010 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link); 1011 cgrp1 = link1->cgrp; 1012 cgrp2 = link2->cgrp; 1013 /* Hierarchies should be linked in the same order. */ 1014 BUG_ON(cgrp1->root != cgrp2->root); 1015 1016 /* 1017 * If this hierarchy is the hierarchy of the cgroup 1018 * that's changing, then we need to check that this 1019 * css_set points to the new cgroup; if it's any other 1020 * hierarchy, then this css_set should point to the 1021 * same cgroup as the old css_set. 1022 */ 1023 if (cgrp1->root == new_cgrp->root) { 1024 if (cgrp1 != new_cgrp) 1025 return false; 1026 } else { 1027 if (cgrp1 != cgrp2) 1028 return false; 1029 } 1030 } 1031 return true; 1032 } 1033 1034 /** 1035 * find_existing_css_set - init css array and find the matching css_set 1036 * @old_cset: the css_set that we're using before the cgroup transition 1037 * @cgrp: the cgroup that we're moving into 1038 * @template: out param for the new set of csses, should be clear on entry 1039 */ 1040 static struct css_set *find_existing_css_set(struct css_set *old_cset, 1041 struct cgroup *cgrp, 1042 struct cgroup_subsys_state **template) 1043 { 1044 struct cgroup_root *root = cgrp->root; 1045 struct cgroup_subsys *ss; 1046 struct css_set *cset; 1047 unsigned long key; 1048 int i; 1049 1050 /* 1051 * Build the set of subsystem state objects that we want to see in the 1052 * new css_set. While subsystems can change globally, the entries here 1053 * won't change, so no need for locking. 1054 */ 1055 for_each_subsys(ss, i) { 1056 if (root->subsys_mask & (1UL << i)) { 1057 /* 1058 * @ss is in this hierarchy, so we want the 1059 * effective css from @cgrp. 1060 */ 1061 template[i] = cgroup_e_css_by_mask(cgrp, ss); 1062 } else { 1063 /* 1064 * @ss is not in this hierarchy, so we don't want 1065 * to change the css. 1066 */ 1067 template[i] = old_cset->subsys[i]; 1068 } 1069 } 1070 1071 key = css_set_hash(template); 1072 hash_for_each_possible(css_set_table, cset, hlist, key) { 1073 if (!compare_css_sets(cset, old_cset, cgrp, template)) 1074 continue; 1075 1076 /* This css_set matches what we need */ 1077 return cset; 1078 } 1079 1080 /* No existing cgroup group matched */ 1081 return NULL; 1082 } 1083 1084 static void free_cgrp_cset_links(struct list_head *links_to_free) 1085 { 1086 struct cgrp_cset_link *link, *tmp_link; 1087 1088 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) { 1089 list_del(&link->cset_link); 1090 kfree(link); 1091 } 1092 } 1093 1094 /** 1095 * allocate_cgrp_cset_links - allocate cgrp_cset_links 1096 * @count: the number of links to allocate 1097 * @tmp_links: list_head the allocated links are put on 1098 * 1099 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links 1100 * through ->cset_link. Returns 0 on success or -errno. 1101 */ 1102 static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links) 1103 { 1104 struct cgrp_cset_link *link; 1105 int i; 1106 1107 INIT_LIST_HEAD(tmp_links); 1108 1109 for (i = 0; i < count; i++) { 1110 link = kzalloc_obj(*link); 1111 if (!link) { 1112 free_cgrp_cset_links(tmp_links); 1113 return -ENOMEM; 1114 } 1115 list_add(&link->cset_link, tmp_links); 1116 } 1117 return 0; 1118 } 1119 1120 /** 1121 * link_css_set - a helper function to link a css_set to a cgroup 1122 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links() 1123 * @cset: the css_set to be linked 1124 * @cgrp: the destination cgroup 1125 */ 1126 static void link_css_set(struct list_head *tmp_links, struct css_set *cset, 1127 struct cgroup *cgrp) 1128 { 1129 struct cgrp_cset_link *link; 1130 1131 BUG_ON(list_empty(tmp_links)); 1132 1133 if (cgroup_on_dfl(cgrp)) 1134 cset->dfl_cgrp = cgrp; 1135 1136 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link); 1137 link->cset = cset; 1138 link->cgrp = cgrp; 1139 1140 /* 1141 * Always add links to the tail of the lists so that the lists are 1142 * in chronological order. 1143 */ 1144 list_move_tail(&link->cset_link, &cgrp->cset_links); 1145 list_add_tail(&link->cgrp_link, &cset->cgrp_links); 1146 1147 if (cgroup_parent(cgrp)) 1148 cgroup_get_live(cgrp); 1149 } 1150 1151 /** 1152 * find_css_set - return a new css_set with one cgroup updated 1153 * @old_cset: the baseline css_set 1154 * @cgrp: the cgroup to be updated 1155 * 1156 * Return a new css_set that's equivalent to @old_cset, but with @cgrp 1157 * substituted into the appropriate hierarchy. 1158 */ 1159 static struct css_set *find_css_set(struct css_set *old_cset, 1160 struct cgroup *cgrp) 1161 { 1162 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { }; 1163 struct css_set *cset; 1164 struct list_head tmp_links; 1165 struct cgrp_cset_link *link; 1166 struct cgroup_subsys *ss; 1167 unsigned long key; 1168 int ssid; 1169 1170 lockdep_assert_held(&cgroup_mutex); 1171 1172 /* First see if we already have a cgroup group that matches 1173 * the desired set */ 1174 spin_lock_irq(&css_set_lock); 1175 cset = find_existing_css_set(old_cset, cgrp, template); 1176 if (cset) 1177 get_css_set(cset); 1178 spin_unlock_irq(&css_set_lock); 1179 1180 if (cset) 1181 return cset; 1182 1183 cset = kzalloc_obj(*cset); 1184 if (!cset) 1185 return NULL; 1186 1187 /* Allocate all the cgrp_cset_link objects that we'll need */ 1188 if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) { 1189 kfree(cset); 1190 return NULL; 1191 } 1192 1193 refcount_set(&cset->refcount, 1); 1194 cset->dom_cset = cset; 1195 INIT_LIST_HEAD(&cset->tasks); 1196 INIT_LIST_HEAD(&cset->mg_tasks); 1197 INIT_LIST_HEAD(&cset->dying_tasks); 1198 INIT_LIST_HEAD(&cset->task_iters); 1199 INIT_LIST_HEAD(&cset->threaded_csets); 1200 INIT_HLIST_NODE(&cset->hlist); 1201 INIT_LIST_HEAD(&cset->cgrp_links); 1202 INIT_LIST_HEAD(&cset->mg_src_preload_node); 1203 INIT_LIST_HEAD(&cset->mg_dst_preload_node); 1204 INIT_LIST_HEAD(&cset->mg_node); 1205 1206 /* Copy the set of subsystem state objects generated in 1207 * find_existing_css_set() */ 1208 memcpy(cset->subsys, template, sizeof(cset->subsys)); 1209 1210 spin_lock_irq(&css_set_lock); 1211 /* Add reference counts and links from the new css_set. */ 1212 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) { 1213 struct cgroup *c = link->cgrp; 1214 1215 if (c->root == cgrp->root) 1216 c = cgrp; 1217 link_css_set(&tmp_links, cset, c); 1218 } 1219 1220 BUG_ON(!list_empty(&tmp_links)); 1221 1222 css_set_count++; 1223 1224 /* Add @cset to the hash table */ 1225 key = css_set_hash(cset->subsys); 1226 hash_add(css_set_table, &cset->hlist, key); 1227 1228 for_each_subsys(ss, ssid) { 1229 struct cgroup_subsys_state *css = cset->subsys[ssid]; 1230 1231 list_add_tail(&cset->e_cset_node[ssid], 1232 &css->cgroup->e_csets[ssid]); 1233 css_get(css); 1234 } 1235 1236 spin_unlock_irq(&css_set_lock); 1237 1238 /* 1239 * If @cset should be threaded, look up the matching dom_cset and 1240 * link them up. We first fully initialize @cset then look for the 1241 * dom_cset. It's simpler this way and safe as @cset is guaranteed 1242 * to stay empty until we return. 1243 */ 1244 if (cgroup_is_threaded(cset->dfl_cgrp)) { 1245 struct css_set *dcset; 1246 1247 dcset = find_css_set(cset, cset->dfl_cgrp->dom_cgrp); 1248 if (!dcset) { 1249 put_css_set(cset); 1250 return NULL; 1251 } 1252 1253 spin_lock_irq(&css_set_lock); 1254 cset->dom_cset = dcset; 1255 list_add_tail(&cset->threaded_csets_node, 1256 &dcset->threaded_csets); 1257 spin_unlock_irq(&css_set_lock); 1258 } 1259 1260 return cset; 1261 } 1262 1263 struct cgroup_root *cgroup_root_from_kf(struct kernfs_root *kf_root) 1264 { 1265 struct cgroup *root_cgrp = kernfs_root_to_node(kf_root)->priv; 1266 1267 return root_cgrp->root; 1268 } 1269 1270 void cgroup_favor_dynmods(struct cgroup_root *root, bool favor) 1271 { 1272 bool favoring = root->flags & CGRP_ROOT_FAVOR_DYNMODS; 1273 1274 /* 1275 * see the comment above CGRP_ROOT_FAVOR_DYNMODS definition. 1276 * favordynmods can flip while task is between 1277 * cgroup_threadgroup_change_begin() and end(), so down_write global 1278 * cgroup_threadgroup_rwsem to synchronize them. 1279 * 1280 * Once cgroup_enable_per_threadgroup_rwsem is enabled, holding 1281 * cgroup_threadgroup_rwsem doesn't exlude tasks between 1282 * cgroup_thread_group_change_begin() and end() and thus it's unsafe to 1283 * turn off. As the scenario is unlikely, simply disallow disabling once 1284 * enabled and print out a warning. 1285 */ 1286 percpu_down_write(&cgroup_threadgroup_rwsem); 1287 if (favor && !favoring) { 1288 cgroup_enable_per_threadgroup_rwsem = true; 1289 rcu_sync_enter(&cgroup_threadgroup_rwsem.rss); 1290 root->flags |= CGRP_ROOT_FAVOR_DYNMODS; 1291 } else if (!favor && favoring) { 1292 if (cgroup_enable_per_threadgroup_rwsem) 1293 pr_warn_once("cgroup favordynmods: per threadgroup rwsem mechanism can't be disabled\n"); 1294 rcu_sync_exit(&cgroup_threadgroup_rwsem.rss); 1295 root->flags &= ~CGRP_ROOT_FAVOR_DYNMODS; 1296 } 1297 percpu_up_write(&cgroup_threadgroup_rwsem); 1298 } 1299 1300 static int cgroup_init_root_id(struct cgroup_root *root) 1301 { 1302 int id; 1303 1304 lockdep_assert_held(&cgroup_mutex); 1305 1306 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, 0, 0, GFP_KERNEL); 1307 if (id < 0) 1308 return id; 1309 1310 root->hierarchy_id = id; 1311 return 0; 1312 } 1313 1314 static void cgroup_exit_root_id(struct cgroup_root *root) 1315 { 1316 lockdep_assert_held(&cgroup_mutex); 1317 1318 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id); 1319 } 1320 1321 void cgroup_free_root(struct cgroup_root *root) 1322 { 1323 kfree_rcu(root, rcu); 1324 } 1325 1326 static void cgroup_destroy_root(struct cgroup_root *root) 1327 { 1328 struct cgroup *cgrp = &root->cgrp; 1329 struct cgrp_cset_link *link, *tmp_link; 1330 int ret; 1331 1332 trace_cgroup_destroy_root(root); 1333 1334 cgroup_lock_and_drain_offline(&cgrp_dfl_root.cgrp); 1335 1336 BUG_ON(atomic_read(&root->nr_cgrps)); 1337 BUG_ON(!list_empty(&cgrp->self.children)); 1338 1339 ret = blocking_notifier_call_chain(&cgroup_lifetime_notifier, 1340 CGROUP_LIFETIME_OFFLINE, cgrp); 1341 WARN_ON_ONCE(notifier_to_errno(ret)); 1342 1343 /* Rebind all subsystems back to the default hierarchy */ 1344 WARN_ON(rebind_subsystems(&cgrp_dfl_root, root->subsys_mask)); 1345 1346 /* 1347 * Release all the links from cset_links to this hierarchy's 1348 * root cgroup 1349 */ 1350 spin_lock_irq(&css_set_lock); 1351 1352 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) { 1353 list_del(&link->cset_link); 1354 list_del(&link->cgrp_link); 1355 kfree(link); 1356 } 1357 1358 spin_unlock_irq(&css_set_lock); 1359 1360 WARN_ON_ONCE(list_empty(&root->root_list)); 1361 list_del_rcu(&root->root_list); 1362 cgroup_root_count--; 1363 1364 if (!have_favordynmods) 1365 cgroup_favor_dynmods(root, false); 1366 1367 cgroup_exit_root_id(root); 1368 1369 cgroup_unlock(); 1370 1371 kernfs_destroy_root(root->kf_root); 1372 cgroup_free_root(root); 1373 } 1374 1375 /* 1376 * Returned cgroup is without refcount but it's valid as long as cset pins it. 1377 */ 1378 static inline struct cgroup *__cset_cgroup_from_root(struct css_set *cset, 1379 struct cgroup_root *root) 1380 { 1381 struct cgroup *res_cgroup = NULL; 1382 1383 if (cset == &init_css_set) { 1384 res_cgroup = &root->cgrp; 1385 } else if (root == &cgrp_dfl_root) { 1386 res_cgroup = cset->dfl_cgrp; 1387 } else { 1388 struct cgrp_cset_link *link; 1389 lockdep_assert_held(&css_set_lock); 1390 1391 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) { 1392 struct cgroup *c = link->cgrp; 1393 1394 if (c->root == root) { 1395 res_cgroup = c; 1396 break; 1397 } 1398 } 1399 } 1400 1401 /* 1402 * If cgroup_mutex is not held, the cgrp_cset_link will be freed 1403 * before we remove the cgroup root from the root_list. Consequently, 1404 * when accessing a cgroup root, the cset_link may have already been 1405 * freed, resulting in a NULL res_cgroup. However, by holding the 1406 * cgroup_mutex, we ensure that res_cgroup can't be NULL. 1407 * If we don't hold cgroup_mutex in the caller, we must do the NULL 1408 * check. 1409 */ 1410 return res_cgroup; 1411 } 1412 1413 /* 1414 * look up cgroup associated with current task's cgroup namespace on the 1415 * specified hierarchy 1416 */ 1417 static struct cgroup * 1418 current_cgns_cgroup_from_root(struct cgroup_root *root) 1419 { 1420 struct cgroup *res = NULL; 1421 struct css_set *cset; 1422 1423 lockdep_assert_held(&css_set_lock); 1424 1425 rcu_read_lock(); 1426 1427 cset = current->nsproxy->cgroup_ns->root_cset; 1428 res = __cset_cgroup_from_root(cset, root); 1429 1430 rcu_read_unlock(); 1431 1432 /* 1433 * The namespace_sem is held by current, so the root cgroup can't 1434 * be umounted. Therefore, we can ensure that the res is non-NULL. 1435 */ 1436 WARN_ON_ONCE(!res); 1437 return res; 1438 } 1439 1440 /* 1441 * Look up cgroup associated with current task's cgroup namespace on the default 1442 * hierarchy. 1443 * 1444 * Unlike current_cgns_cgroup_from_root(), this doesn't need locks: 1445 * - Internal rcu_read_lock is unnecessary because we don't dereference any rcu 1446 * pointers. 1447 * - css_set_lock is not needed because we just read cset->dfl_cgrp. 1448 * - As a bonus returned cgrp is pinned with the current because it cannot 1449 * switch cgroup_ns asynchronously. 1450 */ 1451 static struct cgroup *current_cgns_cgroup_dfl(void) 1452 { 1453 struct css_set *cset; 1454 1455 if (current->nsproxy) { 1456 cset = current->nsproxy->cgroup_ns->root_cset; 1457 return __cset_cgroup_from_root(cset, &cgrp_dfl_root); 1458 } else { 1459 /* 1460 * NOTE: This function may be called from bpf_cgroup_from_id() 1461 * on a task which has already passed exit_nsproxy_namespaces() 1462 * and nsproxy == NULL. Fall back to cgrp_dfl_root which will 1463 * make all cgroups visible for lookups. 1464 */ 1465 return &cgrp_dfl_root.cgrp; 1466 } 1467 } 1468 1469 /* look up cgroup associated with given css_set on the specified hierarchy */ 1470 static struct cgroup *cset_cgroup_from_root(struct css_set *cset, 1471 struct cgroup_root *root) 1472 { 1473 lockdep_assert_held(&css_set_lock); 1474 1475 return __cset_cgroup_from_root(cset, root); 1476 } 1477 1478 /* 1479 * Return the cgroup for "task" from the given hierarchy. Must be 1480 * called with css_set_lock held to prevent task's groups from being modified. 1481 * Must be called with either cgroup_mutex or rcu read lock to prevent the 1482 * cgroup root from being destroyed. 1483 */ 1484 struct cgroup *task_cgroup_from_root(struct task_struct *task, 1485 struct cgroup_root *root) 1486 { 1487 /* 1488 * No need to lock the task - since we hold css_set_lock the 1489 * task can't change groups. 1490 */ 1491 return cset_cgroup_from_root(task_css_set(task), root); 1492 } 1493 1494 /* 1495 * A task must hold cgroup_mutex to modify cgroups. 1496 * 1497 * Any task can increment and decrement the count field without lock. 1498 * So in general, code holding cgroup_mutex can't rely on the count 1499 * field not changing. However, if the count goes to zero, then only 1500 * cgroup_attach_task() can increment it again. Because a count of zero 1501 * means that no tasks are currently attached, therefore there is no 1502 * way a task attached to that cgroup can fork (the other way to 1503 * increment the count). So code holding cgroup_mutex can safely 1504 * assume that if the count is zero, it will stay zero. Similarly, if 1505 * a task holds cgroup_mutex on a cgroup with zero count, it 1506 * knows that the cgroup won't be removed, as cgroup_rmdir() 1507 * needs that mutex. 1508 * 1509 * A cgroup can only be deleted if both its 'count' of using tasks 1510 * is zero, and its list of 'children' cgroups is empty. Since all 1511 * tasks in the system use _some_ cgroup, and since there is always at 1512 * least one task in the system (init, pid == 1), therefore, root cgroup 1513 * always has either children cgroups and/or using tasks. So we don't 1514 * need a special hack to ensure that root cgroup cannot be deleted. 1515 * 1516 * P.S. One more locking exception. RCU is used to guard the 1517 * update of a tasks cgroup pointer by cgroup_attach_task() 1518 */ 1519 1520 static struct kernfs_syscall_ops cgroup_kf_syscall_ops; 1521 1522 static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft, 1523 char *buf) 1524 { 1525 struct cgroup_subsys *ss = cft->ss; 1526 1527 if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) && 1528 !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) { 1529 const char *dbg = (cft->flags & CFTYPE_DEBUG) ? ".__DEBUG__." : ""; 1530 1531 snprintf(buf, CGROUP_FILE_NAME_MAX, "%s%s.%s", 1532 dbg, cgroup_on_dfl(cgrp) ? ss->name : ss->legacy_name, 1533 cft->name); 1534 } else { 1535 strscpy(buf, cft->name, CGROUP_FILE_NAME_MAX); 1536 } 1537 return buf; 1538 } 1539 1540 /** 1541 * cgroup_file_mode - deduce file mode of a control file 1542 * @cft: the control file in question 1543 * 1544 * S_IRUGO for read, S_IWUSR for write. 1545 */ 1546 static umode_t cgroup_file_mode(const struct cftype *cft) 1547 { 1548 umode_t mode = 0; 1549 1550 if (cft->read_u64 || cft->read_s64 || cft->seq_show) 1551 mode |= S_IRUGO; 1552 1553 if (cft->write_u64 || cft->write_s64 || cft->write) { 1554 if (cft->flags & CFTYPE_WORLD_WRITABLE) 1555 mode |= S_IWUGO; 1556 else 1557 mode |= S_IWUSR; 1558 } 1559 1560 return mode; 1561 } 1562 1563 /** 1564 * cgroup_calc_subtree_ss_mask - calculate subtree_ss_mask 1565 * @subtree_control: the new subtree_control mask to consider 1566 * @this_ss_mask: available subsystems 1567 * 1568 * On the default hierarchy, a subsystem may request other subsystems to be 1569 * enabled together through its ->depends_on mask. In such cases, more 1570 * subsystems than specified in "cgroup.subtree_control" may be enabled. 1571 * 1572 * This function calculates which subsystems need to be enabled if 1573 * @subtree_control is to be applied while restricted to @this_ss_mask. 1574 */ 1575 static u32 cgroup_calc_subtree_ss_mask(u32 subtree_control, u32 this_ss_mask) 1576 { 1577 u32 cur_ss_mask = subtree_control; 1578 struct cgroup_subsys *ss; 1579 int ssid; 1580 1581 lockdep_assert_held(&cgroup_mutex); 1582 1583 cur_ss_mask |= cgrp_dfl_implicit_ss_mask; 1584 1585 while (true) { 1586 u32 new_ss_mask = cur_ss_mask; 1587 1588 do_each_subsys_mask(ss, ssid, cur_ss_mask) { 1589 new_ss_mask |= ss->depends_on; 1590 } while_each_subsys_mask(); 1591 1592 /* 1593 * Mask out subsystems which aren't available. This can 1594 * happen only if some depended-upon subsystems were bound 1595 * to non-default hierarchies. 1596 */ 1597 new_ss_mask &= this_ss_mask; 1598 1599 if (new_ss_mask == cur_ss_mask) 1600 break; 1601 cur_ss_mask = new_ss_mask; 1602 } 1603 1604 return cur_ss_mask; 1605 } 1606 1607 /** 1608 * cgroup_kn_unlock - unlocking helper for cgroup kernfs methods 1609 * @kn: the kernfs_node being serviced 1610 * 1611 * This helper undoes cgroup_kn_lock_live() and should be invoked before 1612 * the method finishes if locking succeeded. Note that once this function 1613 * returns the cgroup returned by cgroup_kn_lock_live() may become 1614 * inaccessible any time. If the caller intends to continue to access the 1615 * cgroup, it should pin it before invoking this function. 1616 */ 1617 void cgroup_kn_unlock(struct kernfs_node *kn) 1618 { 1619 struct cgroup *cgrp; 1620 1621 if (kernfs_type(kn) == KERNFS_DIR) 1622 cgrp = kn->priv; 1623 else 1624 cgrp = kn_priv(kn); 1625 1626 cgroup_unlock(); 1627 1628 kernfs_unbreak_active_protection(kn); 1629 cgroup_put(cgrp); 1630 } 1631 1632 /** 1633 * cgroup_kn_lock_live - locking helper for cgroup kernfs methods 1634 * @kn: the kernfs_node being serviced 1635 * @drain_offline: perform offline draining on the cgroup 1636 * 1637 * This helper is to be used by a cgroup kernfs method currently servicing 1638 * @kn. It breaks the active protection, performs cgroup locking and 1639 * verifies that the associated cgroup is alive. Returns the cgroup if 1640 * alive; otherwise, %NULL. A successful return should be undone by a 1641 * matching cgroup_kn_unlock() invocation. If @drain_offline is %true, the 1642 * cgroup is drained of offlining csses before return. 1643 * 1644 * Any cgroup kernfs method implementation which requires locking the 1645 * associated cgroup should use this helper. It avoids nesting cgroup 1646 * locking under kernfs active protection and allows all kernfs operations 1647 * including self-removal. 1648 */ 1649 struct cgroup *cgroup_kn_lock_live(struct kernfs_node *kn, bool drain_offline) 1650 { 1651 struct cgroup *cgrp; 1652 1653 if (kernfs_type(kn) == KERNFS_DIR) 1654 cgrp = kn->priv; 1655 else 1656 cgrp = kn_priv(kn); 1657 1658 /* 1659 * We're gonna grab cgroup_mutex which nests outside kernfs 1660 * active_ref. cgroup liveliness check alone provides enough 1661 * protection against removal. Ensure @cgrp stays accessible and 1662 * break the active_ref protection. 1663 */ 1664 if (!cgroup_tryget(cgrp)) 1665 return NULL; 1666 kernfs_break_active_protection(kn); 1667 1668 if (drain_offline) 1669 cgroup_lock_and_drain_offline(cgrp); 1670 else 1671 cgroup_lock(); 1672 1673 if (!cgroup_is_dead(cgrp)) 1674 return cgrp; 1675 1676 cgroup_kn_unlock(kn); 1677 return NULL; 1678 } 1679 1680 static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft) 1681 { 1682 char name[CGROUP_FILE_NAME_MAX]; 1683 1684 lockdep_assert_held(&cgroup_mutex); 1685 1686 if (cft->file_offset) { 1687 struct cgroup_subsys_state *css = cgroup_css(cgrp, cft->ss); 1688 struct cgroup_file *cfile = (void *)css + cft->file_offset; 1689 1690 spin_lock_irq(&cfile->lock); 1691 WRITE_ONCE(cfile->kn, NULL); 1692 spin_unlock_irq(&cfile->lock); 1693 1694 timer_delete_sync(&cfile->notify_timer); 1695 } 1696 1697 kernfs_remove_by_name(cgrp->kn, cgroup_file_name(cgrp, cft, name)); 1698 } 1699 1700 /** 1701 * css_clear_dir - remove subsys files in a cgroup directory 1702 * @css: target css 1703 */ 1704 static void css_clear_dir(struct cgroup_subsys_state *css) 1705 { 1706 struct cgroup *cgrp = css->cgroup; 1707 struct cftype *cfts; 1708 1709 if (!(css->flags & CSS_VISIBLE)) 1710 return; 1711 1712 css->flags &= ~CSS_VISIBLE; 1713 1714 if (css_is_self(css)) { 1715 if (cgroup_on_dfl(cgrp)) { 1716 cgroup_addrm_files(css, cgrp, 1717 cgroup_base_files, false); 1718 if (cgroup_psi_enabled()) 1719 cgroup_addrm_files(css, cgrp, 1720 cgroup_psi_files, false); 1721 } else { 1722 cgroup_addrm_files(css, cgrp, 1723 cgroup1_base_files, false); 1724 } 1725 } else { 1726 list_for_each_entry(cfts, &css->ss->cfts, node) 1727 cgroup_addrm_files(css, cgrp, cfts, false); 1728 } 1729 } 1730 1731 /** 1732 * css_populate_dir - create subsys files in a cgroup directory 1733 * @css: target css 1734 * 1735 * On failure, no file is added. 1736 */ 1737 static int css_populate_dir(struct cgroup_subsys_state *css) 1738 { 1739 struct cgroup *cgrp = css->cgroup; 1740 struct cftype *cfts, *failed_cfts; 1741 int ret; 1742 1743 if (css->flags & CSS_VISIBLE) 1744 return 0; 1745 1746 if (css_is_self(css)) { 1747 if (cgroup_on_dfl(cgrp)) { 1748 ret = cgroup_addrm_files(css, cgrp, 1749 cgroup_base_files, true); 1750 if (ret < 0) 1751 return ret; 1752 1753 if (cgroup_psi_enabled()) { 1754 ret = cgroup_addrm_files(css, cgrp, 1755 cgroup_psi_files, true); 1756 if (ret < 0) { 1757 cgroup_addrm_files(css, cgrp, 1758 cgroup_base_files, false); 1759 return ret; 1760 } 1761 } 1762 } else { 1763 ret = cgroup_addrm_files(css, cgrp, 1764 cgroup1_base_files, true); 1765 if (ret < 0) 1766 return ret; 1767 } 1768 } else { 1769 list_for_each_entry(cfts, &css->ss->cfts, node) { 1770 ret = cgroup_addrm_files(css, cgrp, cfts, true); 1771 if (ret < 0) { 1772 failed_cfts = cfts; 1773 goto err; 1774 } 1775 } 1776 } 1777 1778 css->flags |= CSS_VISIBLE; 1779 1780 return 0; 1781 err: 1782 list_for_each_entry(cfts, &css->ss->cfts, node) { 1783 if (cfts == failed_cfts) 1784 break; 1785 cgroup_addrm_files(css, cgrp, cfts, false); 1786 } 1787 return ret; 1788 } 1789 1790 int rebind_subsystems(struct cgroup_root *dst_root, u32 ss_mask) 1791 { 1792 struct cgroup *dcgrp = &dst_root->cgrp; 1793 struct cgroup_subsys *ss; 1794 int ssid, ret; 1795 u32 dfl_disable_ss_mask = 0; 1796 1797 lockdep_assert_held(&cgroup_mutex); 1798 1799 do_each_subsys_mask(ss, ssid, ss_mask) { 1800 /* 1801 * If @ss has non-root csses attached to it, can't move. 1802 * If @ss is an implicit controller, it is exempt from this 1803 * rule and can be stolen. 1804 */ 1805 if (css_next_child(NULL, cgroup_css(&ss->root->cgrp, ss)) && 1806 !ss->implicit_on_dfl) 1807 return -EBUSY; 1808 1809 /* can't move between two non-dummy roots either */ 1810 if (ss->root != &cgrp_dfl_root && dst_root != &cgrp_dfl_root) 1811 return -EBUSY; 1812 1813 /* 1814 * Collect ssid's that need to be disabled from default 1815 * hierarchy. 1816 */ 1817 if (ss->root == &cgrp_dfl_root) 1818 dfl_disable_ss_mask |= 1 << ssid; 1819 1820 } while_each_subsys_mask(); 1821 1822 if (dfl_disable_ss_mask) { 1823 struct cgroup *scgrp = &cgrp_dfl_root.cgrp; 1824 1825 /* 1826 * Controllers from default hierarchy that need to be rebound 1827 * are all disabled together in one go. 1828 */ 1829 cgrp_dfl_root.subsys_mask &= ~dfl_disable_ss_mask; 1830 WARN_ON(cgroup_apply_control(scgrp)); 1831 cgroup_finalize_control(scgrp, 0); 1832 } 1833 1834 do_each_subsys_mask(ss, ssid, ss_mask) { 1835 struct cgroup_root *src_root = ss->root; 1836 struct cgroup *scgrp = &src_root->cgrp; 1837 struct cgroup_subsys_state *css = cgroup_css(scgrp, ss); 1838 struct css_set *cset, *cset_pos; 1839 struct css_task_iter *it; 1840 1841 WARN_ON(!css || cgroup_css(dcgrp, ss)); 1842 1843 if (src_root != &cgrp_dfl_root) { 1844 /* disable from the source */ 1845 src_root->subsys_mask &= ~(1 << ssid); 1846 WARN_ON(cgroup_apply_control(scgrp)); 1847 cgroup_finalize_control(scgrp, 0); 1848 } 1849 1850 /* rebind */ 1851 RCU_INIT_POINTER(scgrp->subsys[ssid], NULL); 1852 rcu_assign_pointer(dcgrp->subsys[ssid], css); 1853 ss->root = dst_root; 1854 1855 spin_lock_irq(&css_set_lock); 1856 css->cgroup = dcgrp; 1857 WARN_ON(!list_empty(&dcgrp->e_csets[ss->id])); 1858 list_for_each_entry_safe(cset, cset_pos, &scgrp->e_csets[ss->id], 1859 e_cset_node[ss->id]) { 1860 list_move_tail(&cset->e_cset_node[ss->id], 1861 &dcgrp->e_csets[ss->id]); 1862 /* 1863 * all css_sets of scgrp together in same order to dcgrp, 1864 * patch in-flight iterators to preserve correct iteration. 1865 * since the iterator is always advanced right away and 1866 * finished when it->cset_pos meets it->cset_head, so only 1867 * update it->cset_head is enough here. 1868 */ 1869 list_for_each_entry(it, &cset->task_iters, iters_node) 1870 if (it->cset_head == &scgrp->e_csets[ss->id]) 1871 it->cset_head = &dcgrp->e_csets[ss->id]; 1872 } 1873 spin_unlock_irq(&css_set_lock); 1874 1875 /* default hierarchy doesn't enable controllers by default */ 1876 dst_root->subsys_mask |= 1 << ssid; 1877 if (dst_root == &cgrp_dfl_root) { 1878 static_branch_enable(cgroup_subsys_on_dfl_key[ssid]); 1879 } else { 1880 dcgrp->subtree_control |= 1 << ssid; 1881 static_branch_disable(cgroup_subsys_on_dfl_key[ssid]); 1882 } 1883 1884 ret = cgroup_apply_control(dcgrp); 1885 if (ret) 1886 pr_warn("partial failure to rebind %s controller (err=%d)\n", 1887 ss->name, ret); 1888 1889 if (ss->bind) 1890 ss->bind(css); 1891 } while_each_subsys_mask(); 1892 1893 kernfs_activate(dcgrp->kn); 1894 return 0; 1895 } 1896 1897 int cgroup_show_path(struct seq_file *sf, struct kernfs_node *kf_node, 1898 struct kernfs_root *kf_root) 1899 { 1900 int len = 0; 1901 char *buf = NULL; 1902 struct cgroup_root *kf_cgroot = cgroup_root_from_kf(kf_root); 1903 struct cgroup *ns_cgroup; 1904 1905 buf = kmalloc(PATH_MAX, GFP_KERNEL); 1906 if (!buf) 1907 return -ENOMEM; 1908 1909 spin_lock_irq(&css_set_lock); 1910 ns_cgroup = current_cgns_cgroup_from_root(kf_cgroot); 1911 len = kernfs_path_from_node(kf_node, ns_cgroup->kn, buf, PATH_MAX); 1912 spin_unlock_irq(&css_set_lock); 1913 1914 if (len == -E2BIG) 1915 len = -ERANGE; 1916 else if (len > 0) { 1917 seq_escape(sf, buf, " \t\n\\"); 1918 len = 0; 1919 } 1920 kfree(buf); 1921 return len; 1922 } 1923 1924 enum cgroup2_param { 1925 Opt_nsdelegate, 1926 Opt_favordynmods, 1927 Opt_memory_localevents, 1928 Opt_memory_recursiveprot, 1929 Opt_memory_hugetlb_accounting, 1930 Opt_pids_localevents, 1931 nr__cgroup2_params 1932 }; 1933 1934 static const struct fs_parameter_spec cgroup2_fs_parameters[] = { 1935 fsparam_flag("nsdelegate", Opt_nsdelegate), 1936 fsparam_flag("favordynmods", Opt_favordynmods), 1937 fsparam_flag("memory_localevents", Opt_memory_localevents), 1938 fsparam_flag("memory_recursiveprot", Opt_memory_recursiveprot), 1939 fsparam_flag("memory_hugetlb_accounting", Opt_memory_hugetlb_accounting), 1940 fsparam_flag("pids_localevents", Opt_pids_localevents), 1941 {} 1942 }; 1943 1944 static int cgroup2_parse_param(struct fs_context *fc, struct fs_parameter *param) 1945 { 1946 struct cgroup_fs_context *ctx = cgroup_fc2context(fc); 1947 struct fs_parse_result result; 1948 int opt; 1949 1950 opt = fs_parse(fc, cgroup2_fs_parameters, param, &result); 1951 if (opt < 0) 1952 return opt; 1953 1954 switch (opt) { 1955 case Opt_nsdelegate: 1956 ctx->flags |= CGRP_ROOT_NS_DELEGATE; 1957 return 0; 1958 case Opt_favordynmods: 1959 ctx->flags |= CGRP_ROOT_FAVOR_DYNMODS; 1960 return 0; 1961 case Opt_memory_localevents: 1962 ctx->flags |= CGRP_ROOT_MEMORY_LOCAL_EVENTS; 1963 return 0; 1964 case Opt_memory_recursiveprot: 1965 ctx->flags |= CGRP_ROOT_MEMORY_RECURSIVE_PROT; 1966 return 0; 1967 case Opt_memory_hugetlb_accounting: 1968 ctx->flags |= CGRP_ROOT_MEMORY_HUGETLB_ACCOUNTING; 1969 return 0; 1970 case Opt_pids_localevents: 1971 ctx->flags |= CGRP_ROOT_PIDS_LOCAL_EVENTS; 1972 return 0; 1973 } 1974 return -EINVAL; 1975 } 1976 1977 struct cgroup_of_peak *of_peak(struct kernfs_open_file *of) 1978 { 1979 struct cgroup_file_ctx *ctx = of->priv; 1980 1981 return &ctx->peak; 1982 } 1983 1984 static void apply_cgroup_root_flags(unsigned int root_flags) 1985 { 1986 if (current->nsproxy->cgroup_ns == &init_cgroup_ns) { 1987 if (root_flags & CGRP_ROOT_NS_DELEGATE) 1988 cgrp_dfl_root.flags |= CGRP_ROOT_NS_DELEGATE; 1989 else 1990 cgrp_dfl_root.flags &= ~CGRP_ROOT_NS_DELEGATE; 1991 1992 cgroup_favor_dynmods(&cgrp_dfl_root, 1993 root_flags & CGRP_ROOT_FAVOR_DYNMODS); 1994 1995 if (root_flags & CGRP_ROOT_MEMORY_LOCAL_EVENTS) 1996 cgrp_dfl_root.flags |= CGRP_ROOT_MEMORY_LOCAL_EVENTS; 1997 else 1998 cgrp_dfl_root.flags &= ~CGRP_ROOT_MEMORY_LOCAL_EVENTS; 1999 2000 if (root_flags & CGRP_ROOT_MEMORY_RECURSIVE_PROT) 2001 cgrp_dfl_root.flags |= CGRP_ROOT_MEMORY_RECURSIVE_PROT; 2002 else 2003 cgrp_dfl_root.flags &= ~CGRP_ROOT_MEMORY_RECURSIVE_PROT; 2004 2005 if (root_flags & CGRP_ROOT_MEMORY_HUGETLB_ACCOUNTING) 2006 cgrp_dfl_root.flags |= CGRP_ROOT_MEMORY_HUGETLB_ACCOUNTING; 2007 else 2008 cgrp_dfl_root.flags &= ~CGRP_ROOT_MEMORY_HUGETLB_ACCOUNTING; 2009 2010 if (root_flags & CGRP_ROOT_PIDS_LOCAL_EVENTS) 2011 cgrp_dfl_root.flags |= CGRP_ROOT_PIDS_LOCAL_EVENTS; 2012 else 2013 cgrp_dfl_root.flags &= ~CGRP_ROOT_PIDS_LOCAL_EVENTS; 2014 } 2015 } 2016 2017 static int cgroup_show_options(struct seq_file *seq, struct kernfs_root *kf_root) 2018 { 2019 if (cgrp_dfl_root.flags & CGRP_ROOT_NS_DELEGATE) 2020 seq_puts(seq, ",nsdelegate"); 2021 if (cgrp_dfl_root.flags & CGRP_ROOT_FAVOR_DYNMODS) 2022 seq_puts(seq, ",favordynmods"); 2023 if (cgrp_dfl_root.flags & CGRP_ROOT_MEMORY_LOCAL_EVENTS) 2024 seq_puts(seq, ",memory_localevents"); 2025 if (cgrp_dfl_root.flags & CGRP_ROOT_MEMORY_RECURSIVE_PROT) 2026 seq_puts(seq, ",memory_recursiveprot"); 2027 if (cgrp_dfl_root.flags & CGRP_ROOT_MEMORY_HUGETLB_ACCOUNTING) 2028 seq_puts(seq, ",memory_hugetlb_accounting"); 2029 if (cgrp_dfl_root.flags & CGRP_ROOT_PIDS_LOCAL_EVENTS) 2030 seq_puts(seq, ",pids_localevents"); 2031 return 0; 2032 } 2033 2034 static int cgroup_reconfigure(struct fs_context *fc) 2035 { 2036 struct cgroup_fs_context *ctx = cgroup_fc2context(fc); 2037 2038 apply_cgroup_root_flags(ctx->flags); 2039 return 0; 2040 } 2041 2042 static void init_cgroup_housekeeping(struct cgroup *cgrp) 2043 { 2044 struct cgroup_subsys *ss; 2045 int ssid; 2046 2047 INIT_LIST_HEAD(&cgrp->self.sibling); 2048 INIT_LIST_HEAD(&cgrp->self.children); 2049 INIT_LIST_HEAD(&cgrp->cset_links); 2050 INIT_LIST_HEAD(&cgrp->pidlists); 2051 mutex_init(&cgrp->pidlist_mutex); 2052 cgrp->self.cgroup = cgrp; 2053 cgrp->self.flags |= CSS_ONLINE; 2054 cgrp->dom_cgrp = cgrp; 2055 cgrp->max_descendants = INT_MAX; 2056 cgrp->max_depth = INT_MAX; 2057 prev_cputime_init(&cgrp->prev_cputime); 2058 2059 for_each_subsys(ss, ssid) 2060 INIT_LIST_HEAD(&cgrp->e_csets[ssid]); 2061 2062 #ifdef CONFIG_CGROUP_BPF 2063 for (int i = 0; i < ARRAY_SIZE(cgrp->bpf.revisions); i++) 2064 cgrp->bpf.revisions[i] = 1; 2065 #endif 2066 2067 init_waitqueue_head(&cgrp->offline_waitq); 2068 init_waitqueue_head(&cgrp->dying_populated_waitq); 2069 INIT_WORK(&cgrp->release_agent_work, cgroup1_release_agent); 2070 } 2071 2072 void init_cgroup_root(struct cgroup_fs_context *ctx) 2073 { 2074 struct cgroup_root *root = ctx->root; 2075 struct cgroup *cgrp = &root->cgrp; 2076 2077 INIT_LIST_HEAD_RCU(&root->root_list); 2078 atomic_set(&root->nr_cgrps, 1); 2079 cgrp->root = root; 2080 init_cgroup_housekeeping(cgrp); 2081 2082 /* DYNMODS must be modified through cgroup_favor_dynmods() */ 2083 root->flags = ctx->flags & ~CGRP_ROOT_FAVOR_DYNMODS; 2084 if (ctx->release_agent) 2085 strscpy(root->release_agent_path, ctx->release_agent, PATH_MAX); 2086 if (ctx->name) 2087 strscpy(root->name, ctx->name, MAX_CGROUP_ROOT_NAMELEN); 2088 if (ctx->cpuset_clone_children) 2089 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags); 2090 } 2091 2092 int cgroup_setup_root(struct cgroup_root *root, u32 ss_mask) 2093 { 2094 LIST_HEAD(tmp_links); 2095 struct cgroup *root_cgrp = &root->cgrp; 2096 struct kernfs_syscall_ops *kf_sops; 2097 struct css_set *cset; 2098 int i, ret; 2099 2100 lockdep_assert_held(&cgroup_mutex); 2101 2102 ret = percpu_ref_init(&root_cgrp->self.refcnt, css_release, 2103 0, GFP_KERNEL); 2104 if (ret) 2105 goto out; 2106 2107 /* 2108 * We're accessing css_set_count without locking css_set_lock here, 2109 * but that's OK - it can only be increased by someone holding 2110 * cgroup_lock, and that's us. Later rebinding may disable 2111 * controllers on the default hierarchy and thus create new csets, 2112 * which can't be more than the existing ones. Allocate 2x. 2113 */ 2114 ret = allocate_cgrp_cset_links(2 * css_set_count, &tmp_links); 2115 if (ret) 2116 goto cancel_ref; 2117 2118 ret = cgroup_init_root_id(root); 2119 if (ret) 2120 goto cancel_ref; 2121 2122 kf_sops = root == &cgrp_dfl_root ? 2123 &cgroup_kf_syscall_ops : &cgroup1_kf_syscall_ops; 2124 2125 root->kf_root = kernfs_create_root(kf_sops, 2126 KERNFS_ROOT_CREATE_DEACTIVATED | 2127 KERNFS_ROOT_SUPPORT_EXPORTOP | 2128 KERNFS_ROOT_SUPPORT_USER_XATTR | 2129 KERNFS_ROOT_INVARIANT_PARENT, 2130 root_cgrp); 2131 if (IS_ERR(root->kf_root)) { 2132 ret = PTR_ERR(root->kf_root); 2133 goto exit_root_id; 2134 } 2135 root_cgrp->kn = kernfs_root_to_node(root->kf_root); 2136 WARN_ON_ONCE(cgroup_ino(root_cgrp) != 1); 2137 root_cgrp->ancestors[0] = root_cgrp; 2138 2139 ret = css_populate_dir(&root_cgrp->self); 2140 if (ret) 2141 goto destroy_root; 2142 2143 ret = css_rstat_init(&root_cgrp->self); 2144 if (ret) 2145 goto destroy_root; 2146 2147 ret = rebind_subsystems(root, ss_mask); 2148 if (ret) 2149 goto exit_stats; 2150 2151 ret = blocking_notifier_call_chain(&cgroup_lifetime_notifier, 2152 CGROUP_LIFETIME_ONLINE, root_cgrp); 2153 WARN_ON_ONCE(notifier_to_errno(ret)); 2154 2155 trace_cgroup_setup_root(root); 2156 2157 /* 2158 * There must be no failure case after here, since rebinding takes 2159 * care of subsystems' refcounts, which are explicitly dropped in 2160 * the failure exit path. 2161 */ 2162 list_add_rcu(&root->root_list, &cgroup_roots); 2163 cgroup_root_count++; 2164 2165 /* 2166 * Link the root cgroup in this hierarchy into all the css_set 2167 * objects. 2168 */ 2169 spin_lock_irq(&css_set_lock); 2170 hash_for_each(css_set_table, i, cset, hlist) { 2171 link_css_set(&tmp_links, cset, root_cgrp); 2172 if (css_set_populated(cset)) 2173 cgroup_update_populated(root_cgrp, true); 2174 } 2175 spin_unlock_irq(&css_set_lock); 2176 2177 BUG_ON(!list_empty(&root_cgrp->self.children)); 2178 BUG_ON(atomic_read(&root->nr_cgrps) != 1); 2179 2180 ret = 0; 2181 goto out; 2182 2183 exit_stats: 2184 css_rstat_exit(&root_cgrp->self); 2185 destroy_root: 2186 kernfs_destroy_root(root->kf_root); 2187 root->kf_root = NULL; 2188 exit_root_id: 2189 cgroup_exit_root_id(root); 2190 cancel_ref: 2191 percpu_ref_exit(&root_cgrp->self.refcnt); 2192 out: 2193 free_cgrp_cset_links(&tmp_links); 2194 return ret; 2195 } 2196 2197 int cgroup_do_get_tree(struct fs_context *fc) 2198 { 2199 struct cgroup_fs_context *ctx = cgroup_fc2context(fc); 2200 int ret; 2201 2202 ctx->kfc.root = ctx->root->kf_root; 2203 if (fc->fs_type == &cgroup2_fs_type) 2204 ctx->kfc.magic = CGROUP2_SUPER_MAGIC; 2205 else 2206 ctx->kfc.magic = CGROUP_SUPER_MAGIC; 2207 ret = kernfs_get_tree(fc); 2208 2209 /* 2210 * In non-init cgroup namespace, instead of root cgroup's dentry, 2211 * we return the dentry corresponding to the cgroupns->root_cgrp. 2212 */ 2213 if (!ret && ctx->ns != &init_cgroup_ns) { 2214 struct dentry *nsdentry; 2215 struct super_block *sb = fc->root->d_sb; 2216 struct cgroup *cgrp; 2217 2218 cgroup_lock(); 2219 spin_lock_irq(&css_set_lock); 2220 2221 cgrp = cset_cgroup_from_root(ctx->ns->root_cset, ctx->root); 2222 2223 spin_unlock_irq(&css_set_lock); 2224 cgroup_unlock(); 2225 2226 nsdentry = kernfs_node_dentry(cgrp->kn, sb); 2227 dput(fc->root); 2228 if (IS_ERR(nsdentry)) { 2229 deactivate_locked_super(sb); 2230 ret = PTR_ERR(nsdentry); 2231 nsdentry = NULL; 2232 } 2233 fc->root = nsdentry; 2234 } 2235 2236 if (!ctx->kfc.new_sb_created) 2237 cgroup_put(&ctx->root->cgrp); 2238 2239 return ret; 2240 } 2241 2242 /* 2243 * Destroy a cgroup filesystem context. 2244 */ 2245 static void cgroup_fs_context_free(struct fs_context *fc) 2246 { 2247 struct cgroup_fs_context *ctx = cgroup_fc2context(fc); 2248 2249 kfree(ctx->name); 2250 kfree(ctx->release_agent); 2251 put_cgroup_ns(ctx->ns); 2252 kernfs_free_fs_context(fc); 2253 kfree(ctx); 2254 } 2255 2256 static int cgroup_get_tree(struct fs_context *fc) 2257 { 2258 struct cgroup_fs_context *ctx = cgroup_fc2context(fc); 2259 int ret; 2260 2261 WRITE_ONCE(cgrp_dfl_visible, true); 2262 cgroup_get_live(&cgrp_dfl_root.cgrp); 2263 ctx->root = &cgrp_dfl_root; 2264 2265 ret = cgroup_do_get_tree(fc); 2266 if (!ret) 2267 apply_cgroup_root_flags(ctx->flags); 2268 return ret; 2269 } 2270 2271 static const struct fs_context_operations cgroup_fs_context_ops = { 2272 .free = cgroup_fs_context_free, 2273 .parse_param = cgroup2_parse_param, 2274 .get_tree = cgroup_get_tree, 2275 .reconfigure = cgroup_reconfigure, 2276 }; 2277 2278 static const struct fs_context_operations cgroup1_fs_context_ops = { 2279 .free = cgroup_fs_context_free, 2280 .parse_param = cgroup1_parse_param, 2281 .get_tree = cgroup1_get_tree, 2282 .reconfigure = cgroup1_reconfigure, 2283 }; 2284 2285 /* 2286 * Initialise the cgroup filesystem creation/reconfiguration context. Notably, 2287 * we select the namespace we're going to use. 2288 */ 2289 static int cgroup_init_fs_context(struct fs_context *fc) 2290 { 2291 struct cgroup_fs_context *ctx; 2292 2293 ctx = kzalloc_obj(struct cgroup_fs_context); 2294 if (!ctx) 2295 return -ENOMEM; 2296 2297 ctx->ns = current->nsproxy->cgroup_ns; 2298 get_cgroup_ns(ctx->ns); 2299 fc->fs_private = &ctx->kfc; 2300 if (fc->fs_type == &cgroup2_fs_type) 2301 fc->ops = &cgroup_fs_context_ops; 2302 else 2303 fc->ops = &cgroup1_fs_context_ops; 2304 put_user_ns(fc->user_ns); 2305 fc->user_ns = get_user_ns(ctx->ns->user_ns); 2306 fc->global = true; 2307 2308 if (have_favordynmods) 2309 ctx->flags |= CGRP_ROOT_FAVOR_DYNMODS; 2310 2311 return 0; 2312 } 2313 2314 static void cgroup_kill_sb(struct super_block *sb) 2315 { 2316 struct kernfs_root *kf_root = kernfs_root_from_sb(sb); 2317 struct cgroup_root *root = cgroup_root_from_kf(kf_root); 2318 2319 /* 2320 * If @root doesn't have any children, start killing it. 2321 * This prevents new mounts by disabling percpu_ref_tryget_live(). 2322 * 2323 * And don't kill the default root. 2324 */ 2325 if (list_empty(&root->cgrp.self.children) && root != &cgrp_dfl_root && 2326 !percpu_ref_is_dying(&root->cgrp.self.refcnt)) 2327 percpu_ref_kill(&root->cgrp.self.refcnt); 2328 cgroup_put(&root->cgrp); 2329 kernfs_kill_sb(sb); 2330 } 2331 2332 struct file_system_type cgroup_fs_type = { 2333 .name = "cgroup", 2334 .init_fs_context = cgroup_init_fs_context, 2335 .parameters = cgroup1_fs_parameters, 2336 .kill_sb = cgroup_kill_sb, 2337 .fs_flags = FS_USERNS_MOUNT, 2338 }; 2339 2340 static struct file_system_type cgroup2_fs_type = { 2341 .name = "cgroup2", 2342 .init_fs_context = cgroup_init_fs_context, 2343 .parameters = cgroup2_fs_parameters, 2344 .kill_sb = cgroup_kill_sb, 2345 .fs_flags = FS_USERNS_MOUNT, 2346 }; 2347 2348 #ifdef CONFIG_CPUSETS_V1 2349 enum cpuset_param { 2350 Opt_cpuset_v2_mode, 2351 }; 2352 2353 static const struct fs_parameter_spec cpuset_fs_parameters[] = { 2354 fsparam_flag ("cpuset_v2_mode", Opt_cpuset_v2_mode), 2355 {} 2356 }; 2357 2358 static int cpuset_parse_param(struct fs_context *fc, struct fs_parameter *param) 2359 { 2360 struct cgroup_fs_context *ctx = cgroup_fc2context(fc); 2361 struct fs_parse_result result; 2362 int opt; 2363 2364 opt = fs_parse(fc, cpuset_fs_parameters, param, &result); 2365 if (opt < 0) 2366 return opt; 2367 2368 switch (opt) { 2369 case Opt_cpuset_v2_mode: 2370 ctx->flags |= CGRP_ROOT_CPUSET_V2_MODE; 2371 return 0; 2372 } 2373 return -EINVAL; 2374 } 2375 2376 static const struct fs_context_operations cpuset_fs_context_ops = { 2377 .get_tree = cgroup1_get_tree, 2378 .free = cgroup_fs_context_free, 2379 .parse_param = cpuset_parse_param, 2380 }; 2381 2382 /* 2383 * This is ugly, but preserves the userspace API for existing cpuset 2384 * users. If someone tries to mount the "cpuset" filesystem, we 2385 * silently switch it to mount "cgroup" instead 2386 */ 2387 static int cpuset_init_fs_context(struct fs_context *fc) 2388 { 2389 char *agent = kstrdup("/sbin/cpuset_release_agent", GFP_USER); 2390 struct cgroup_fs_context *ctx; 2391 int err; 2392 2393 err = cgroup_init_fs_context(fc); 2394 if (err) { 2395 kfree(agent); 2396 return err; 2397 } 2398 2399 fc->ops = &cpuset_fs_context_ops; 2400 2401 ctx = cgroup_fc2context(fc); 2402 ctx->subsys_mask = 1 << cpuset_cgrp_id; 2403 ctx->flags |= CGRP_ROOT_NOPREFIX; 2404 ctx->release_agent = agent; 2405 2406 get_filesystem(&cgroup_fs_type); 2407 put_filesystem(fc->fs_type); 2408 fc->fs_type = &cgroup_fs_type; 2409 2410 return 0; 2411 } 2412 2413 static struct file_system_type cpuset_fs_type = { 2414 .name = "cpuset", 2415 .init_fs_context = cpuset_init_fs_context, 2416 .parameters = cpuset_fs_parameters, 2417 .fs_flags = FS_USERNS_MOUNT, 2418 }; 2419 #endif 2420 2421 int cgroup_path_ns_locked(struct cgroup *cgrp, char *buf, size_t buflen, 2422 struct cgroup_namespace *ns) 2423 { 2424 struct cgroup *root = cset_cgroup_from_root(ns->root_cset, cgrp->root); 2425 2426 return kernfs_path_from_node(cgrp->kn, root->kn, buf, buflen); 2427 } 2428 2429 int cgroup_path_ns(struct cgroup *cgrp, char *buf, size_t buflen, 2430 struct cgroup_namespace *ns) 2431 { 2432 int ret; 2433 2434 cgroup_lock(); 2435 spin_lock_irq(&css_set_lock); 2436 2437 ret = cgroup_path_ns_locked(cgrp, buf, buflen, ns); 2438 2439 spin_unlock_irq(&css_set_lock); 2440 cgroup_unlock(); 2441 2442 return ret; 2443 } 2444 EXPORT_SYMBOL_GPL(cgroup_path_ns); 2445 2446 /** 2447 * cgroup_attach_lock - Lock for ->attach() 2448 * @lock_mode: whether acquire and acquire which rwsem 2449 * @tsk: thread group to lock 2450 * 2451 * cgroup migration sometimes needs to stabilize threadgroups against forks and 2452 * exits by write-locking cgroup_threadgroup_rwsem. However, some ->attach() 2453 * implementations (e.g. cpuset), also need to disable CPU hotplug. 2454 * Unfortunately, letting ->attach() operations acquire cpus_read_lock() can 2455 * lead to deadlocks. 2456 * 2457 * Bringing up a CPU may involve creating and destroying tasks which requires 2458 * read-locking threadgroup_rwsem, so threadgroup_rwsem nests inside 2459 * cpus_read_lock(). If we call an ->attach() which acquires the cpus lock while 2460 * write-locking threadgroup_rwsem, the locking order is reversed and we end up 2461 * waiting for an on-going CPU hotplug operation which in turn is waiting for 2462 * the threadgroup_rwsem to be released to create new tasks. For more details: 2463 * 2464 * http://lkml.kernel.org/r/20220711174629.uehfmqegcwn2lqzu@wubuntu 2465 * 2466 * Resolve the situation by always acquiring cpus_read_lock() before optionally 2467 * write-locking cgroup_threadgroup_rwsem. This allows ->attach() to assume that 2468 * CPU hotplug is disabled on entry. 2469 * 2470 * When favordynmods is enabled, take per threadgroup rwsem to reduce overhead 2471 * on dynamic cgroup modifications. see the comment above 2472 * CGRP_ROOT_FAVOR_DYNMODS definition. 2473 * 2474 * tsk is not NULL only when writing to cgroup.procs. 2475 */ 2476 void cgroup_attach_lock(enum cgroup_attach_lock_mode lock_mode, 2477 struct task_struct *tsk) 2478 { 2479 cpus_read_lock(); 2480 2481 switch (lock_mode) { 2482 case CGRP_ATTACH_LOCK_NONE: 2483 break; 2484 case CGRP_ATTACH_LOCK_GLOBAL: 2485 percpu_down_write(&cgroup_threadgroup_rwsem); 2486 break; 2487 case CGRP_ATTACH_LOCK_PER_THREADGROUP: 2488 down_write(&tsk->signal->cgroup_threadgroup_rwsem); 2489 break; 2490 default: 2491 pr_warn("cgroup: Unexpected attach lock mode."); 2492 break; 2493 } 2494 } 2495 2496 /** 2497 * cgroup_attach_unlock - Undo cgroup_attach_lock() 2498 * @lock_mode: whether release and release which rwsem 2499 * @tsk: thread group to lock 2500 */ 2501 void cgroup_attach_unlock(enum cgroup_attach_lock_mode lock_mode, 2502 struct task_struct *tsk) 2503 { 2504 switch (lock_mode) { 2505 case CGRP_ATTACH_LOCK_NONE: 2506 break; 2507 case CGRP_ATTACH_LOCK_GLOBAL: 2508 percpu_up_write(&cgroup_threadgroup_rwsem); 2509 break; 2510 case CGRP_ATTACH_LOCK_PER_THREADGROUP: 2511 up_write(&tsk->signal->cgroup_threadgroup_rwsem); 2512 break; 2513 default: 2514 pr_warn("cgroup: Unexpected attach lock mode."); 2515 break; 2516 } 2517 2518 cpus_read_unlock(); 2519 } 2520 2521 /** 2522 * cgroup_migrate_add_task - add a migration target task to a migration context 2523 * @task: target task 2524 * @mgctx: target migration context 2525 * 2526 * Add @task, which is a migration target, to @mgctx->tset. This function 2527 * becomes noop if @task doesn't need to be migrated. @task's css_set 2528 * should have been added as a migration source and @task->cg_list will be 2529 * moved from the css_set's tasks list to mg_tasks one. 2530 */ 2531 static void cgroup_migrate_add_task(struct task_struct *task, 2532 struct cgroup_mgctx *mgctx) 2533 { 2534 struct css_set *cset; 2535 2536 lockdep_assert_held(&css_set_lock); 2537 2538 /* @task either already exited or can't exit until the end */ 2539 if (task->flags & PF_EXITING) 2540 return; 2541 2542 /* cgroup_threadgroup_rwsem protects racing against forks */ 2543 WARN_ON_ONCE(list_empty(&task->cg_list)); 2544 2545 cset = task_css_set(task); 2546 if (!cset->mg_src_cgrp) 2547 return; 2548 2549 mgctx->tset.nr_tasks++; 2550 2551 css_set_skip_task_iters(cset, task); 2552 list_move_tail(&task->cg_list, &cset->mg_tasks); 2553 if (list_empty(&cset->mg_node)) 2554 list_add_tail(&cset->mg_node, 2555 &mgctx->tset.src_csets); 2556 if (list_empty(&cset->mg_dst_cset->mg_node)) 2557 list_add_tail(&cset->mg_dst_cset->mg_node, 2558 &mgctx->tset.dst_csets); 2559 } 2560 2561 /** 2562 * cgroup_taskset_first - reset taskset and return the first task 2563 * @tset: taskset of interest 2564 * @dst_cssp: output variable for the destination css 2565 * 2566 * @tset iteration is initialized and the first task is returned. 2567 */ 2568 struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset, 2569 struct cgroup_subsys_state **dst_cssp) 2570 { 2571 tset->cur_cset = list_first_entry(tset->csets, struct css_set, mg_node); 2572 tset->cur_task = NULL; 2573 2574 return cgroup_taskset_next(tset, dst_cssp); 2575 } 2576 2577 /** 2578 * cgroup_taskset_next - iterate to the next task in taskset 2579 * @tset: taskset of interest 2580 * @dst_cssp: output variable for the destination css 2581 * 2582 * Return the next task in @tset. Iteration must have been initialized 2583 * with cgroup_taskset_first(). 2584 */ 2585 struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset, 2586 struct cgroup_subsys_state **dst_cssp) 2587 { 2588 struct css_set *cset = tset->cur_cset; 2589 struct task_struct *task = tset->cur_task; 2590 2591 while (CGROUP_HAS_SUBSYS_CONFIG && &cset->mg_node != tset->csets) { 2592 if (!task) 2593 task = list_first_entry(&cset->mg_tasks, 2594 struct task_struct, cg_list); 2595 else 2596 task = list_next_entry(task, cg_list); 2597 2598 if (&task->cg_list != &cset->mg_tasks) { 2599 tset->cur_cset = cset; 2600 tset->cur_task = task; 2601 2602 /* 2603 * This function may be called both before and 2604 * after cgroup_migrate_execute(). The two cases 2605 * can be distinguished by looking at whether @cset 2606 * has its ->mg_dst_cset set. 2607 */ 2608 if (cset->mg_dst_cset) 2609 *dst_cssp = cset->mg_dst_cset->subsys[tset->ssid]; 2610 else 2611 *dst_cssp = cset->subsys[tset->ssid]; 2612 2613 return task; 2614 } 2615 2616 cset = list_next_entry(cset, mg_node); 2617 task = NULL; 2618 } 2619 2620 return NULL; 2621 } 2622 2623 /** 2624 * cgroup_migrate_execute - migrate a taskset 2625 * @mgctx: migration context 2626 * 2627 * Migrate tasks in @mgctx as setup by migration preparation functions. 2628 * This function fails iff one of the ->can_attach callbacks fails and 2629 * guarantees that either all or none of the tasks in @mgctx are migrated. 2630 * @mgctx is consumed regardless of success. 2631 */ 2632 static int cgroup_migrate_execute(struct cgroup_mgctx *mgctx) 2633 { 2634 struct cgroup_taskset *tset = &mgctx->tset; 2635 struct cgroup_subsys *ss; 2636 struct task_struct *task, *tmp_task; 2637 struct css_set *cset, *tmp_cset; 2638 int ssid, failed_ssid, ret; 2639 2640 /* check that we can legitimately attach to the cgroup */ 2641 if (tset->nr_tasks) { 2642 do_each_subsys_mask(ss, ssid, mgctx->ss_mask) { 2643 if (ss->can_attach) { 2644 tset->ssid = ssid; 2645 ret = ss->can_attach(tset); 2646 if (ret) { 2647 failed_ssid = ssid; 2648 goto out_cancel_attach; 2649 } 2650 } 2651 } while_each_subsys_mask(); 2652 } 2653 2654 /* 2655 * Now that we're guaranteed success, proceed to move all tasks to 2656 * the new cgroup. There are no failure cases after here, so this 2657 * is the commit point. 2658 */ 2659 spin_lock_irq(&css_set_lock); 2660 list_for_each_entry(cset, &tset->src_csets, mg_node) { 2661 list_for_each_entry_safe(task, tmp_task, &cset->mg_tasks, cg_list) { 2662 struct css_set *from_cset = task_css_set(task); 2663 struct css_set *to_cset = cset->mg_dst_cset; 2664 2665 get_css_set(to_cset); 2666 to_cset->nr_tasks++; 2667 css_set_move_task(task, from_cset, to_cset, true); 2668 from_cset->nr_tasks--; 2669 /* 2670 * If the source or destination cgroup is frozen, 2671 * the task might require to change its state. 2672 */ 2673 cgroup_freezer_migrate_task(task, from_cset->dfl_cgrp, 2674 to_cset->dfl_cgrp); 2675 put_css_set_locked(from_cset); 2676 2677 } 2678 } 2679 spin_unlock_irq(&css_set_lock); 2680 2681 /* 2682 * Migration is committed, all target tasks are now on dst_csets. 2683 * Nothing is sensitive to fork() after this point. Notify 2684 * controllers that migration is complete. 2685 */ 2686 tset->csets = &tset->dst_csets; 2687 2688 if (tset->nr_tasks) { 2689 do_each_subsys_mask(ss, ssid, mgctx->ss_mask) { 2690 if (ss->attach) { 2691 tset->ssid = ssid; 2692 ss->attach(tset); 2693 } 2694 } while_each_subsys_mask(); 2695 } 2696 2697 ret = 0; 2698 goto out_release_tset; 2699 2700 out_cancel_attach: 2701 if (tset->nr_tasks) { 2702 do_each_subsys_mask(ss, ssid, mgctx->ss_mask) { 2703 if (ssid == failed_ssid) 2704 break; 2705 if (ss->cancel_attach) { 2706 tset->ssid = ssid; 2707 ss->cancel_attach(tset); 2708 } 2709 } while_each_subsys_mask(); 2710 } 2711 out_release_tset: 2712 spin_lock_irq(&css_set_lock); 2713 list_splice_init(&tset->dst_csets, &tset->src_csets); 2714 list_for_each_entry_safe(cset, tmp_cset, &tset->src_csets, mg_node) { 2715 list_splice_tail_init(&cset->mg_tasks, &cset->tasks); 2716 list_del_init(&cset->mg_node); 2717 } 2718 spin_unlock_irq(&css_set_lock); 2719 2720 /* 2721 * Re-initialize the cgroup_taskset structure in case it is reused 2722 * again in another cgroup_migrate_add_task()/cgroup_migrate_execute() 2723 * iteration. 2724 */ 2725 tset->nr_tasks = 0; 2726 tset->csets = &tset->src_csets; 2727 return ret; 2728 } 2729 2730 /** 2731 * cgroup_migrate_vet_dst - verify whether a cgroup can be migration destination 2732 * @dst_cgrp: destination cgroup to test 2733 * 2734 * On the default hierarchy, except for the mixable, (possible) thread root 2735 * and threaded cgroups, subtree_control must be zero for migration 2736 * destination cgroups with tasks so that child cgroups don't compete 2737 * against tasks. 2738 */ 2739 int cgroup_migrate_vet_dst(struct cgroup *dst_cgrp) 2740 { 2741 /* v1 doesn't have any restriction */ 2742 if (!cgroup_on_dfl(dst_cgrp)) 2743 return 0; 2744 2745 /* verify @dst_cgrp can host resources */ 2746 if (!cgroup_is_valid_domain(dst_cgrp->dom_cgrp)) 2747 return -EOPNOTSUPP; 2748 2749 /* 2750 * If @dst_cgrp is already or can become a thread root or is 2751 * threaded, it doesn't matter. 2752 */ 2753 if (cgroup_can_be_thread_root(dst_cgrp) || cgroup_is_threaded(dst_cgrp)) 2754 return 0; 2755 2756 /* apply no-internal-process constraint */ 2757 if (dst_cgrp->subtree_control) 2758 return -EBUSY; 2759 2760 return 0; 2761 } 2762 2763 /** 2764 * cgroup_migrate_finish - cleanup after attach 2765 * @mgctx: migration context 2766 * 2767 * Undo cgroup_migrate_add_src() and cgroup_migrate_prepare_dst(). See 2768 * those functions for details. 2769 */ 2770 void cgroup_migrate_finish(struct cgroup_mgctx *mgctx) 2771 { 2772 struct css_set *cset, *tmp_cset; 2773 2774 lockdep_assert_held(&cgroup_mutex); 2775 2776 spin_lock_irq(&css_set_lock); 2777 2778 list_for_each_entry_safe(cset, tmp_cset, &mgctx->preloaded_src_csets, 2779 mg_src_preload_node) { 2780 cset->mg_src_cgrp = NULL; 2781 cset->mg_dst_cgrp = NULL; 2782 cset->mg_dst_cset = NULL; 2783 list_del_init(&cset->mg_src_preload_node); 2784 put_css_set_locked(cset); 2785 } 2786 2787 list_for_each_entry_safe(cset, tmp_cset, &mgctx->preloaded_dst_csets, 2788 mg_dst_preload_node) { 2789 cset->mg_src_cgrp = NULL; 2790 cset->mg_dst_cgrp = NULL; 2791 cset->mg_dst_cset = NULL; 2792 list_del_init(&cset->mg_dst_preload_node); 2793 put_css_set_locked(cset); 2794 } 2795 2796 spin_unlock_irq(&css_set_lock); 2797 } 2798 2799 /** 2800 * cgroup_migrate_add_src - add a migration source css_set 2801 * @src_cset: the source css_set to add 2802 * @dst_cgrp: the destination cgroup 2803 * @mgctx: migration context 2804 * 2805 * Tasks belonging to @src_cset are about to be migrated to @dst_cgrp. Pin 2806 * @src_cset and add it to @mgctx->src_csets, which should later be cleaned 2807 * up by cgroup_migrate_finish(). 2808 * 2809 * This function may be called without holding cgroup_threadgroup_rwsem 2810 * even if the target is a process. Threads may be created and destroyed 2811 * but as long as cgroup_mutex is not dropped, no new css_set can be put 2812 * into play and the preloaded css_sets are guaranteed to cover all 2813 * migrations. 2814 */ 2815 void cgroup_migrate_add_src(struct css_set *src_cset, 2816 struct cgroup *dst_cgrp, 2817 struct cgroup_mgctx *mgctx) 2818 { 2819 struct cgroup *src_cgrp; 2820 2821 lockdep_assert_held(&cgroup_mutex); 2822 lockdep_assert_held(&css_set_lock); 2823 2824 /* 2825 * If ->dead, @src_set is associated with one or more dead cgroups 2826 * and doesn't contain any migratable tasks. Ignore it early so 2827 * that the rest of migration path doesn't get confused by it. 2828 */ 2829 if (src_cset->dead) 2830 return; 2831 2832 if (!list_empty(&src_cset->mg_src_preload_node)) 2833 return; 2834 2835 src_cgrp = cset_cgroup_from_root(src_cset, dst_cgrp->root); 2836 2837 WARN_ON(src_cset->mg_src_cgrp); 2838 WARN_ON(src_cset->mg_dst_cgrp); 2839 WARN_ON(!list_empty(&src_cset->mg_tasks)); 2840 WARN_ON(!list_empty(&src_cset->mg_node)); 2841 2842 src_cset->mg_src_cgrp = src_cgrp; 2843 src_cset->mg_dst_cgrp = dst_cgrp; 2844 get_css_set(src_cset); 2845 list_add_tail(&src_cset->mg_src_preload_node, &mgctx->preloaded_src_csets); 2846 } 2847 2848 /** 2849 * cgroup_migrate_prepare_dst - prepare destination css_sets for migration 2850 * @mgctx: migration context 2851 * 2852 * Tasks are about to be moved and all the source css_sets have been 2853 * preloaded to @mgctx->preloaded_src_csets. This function looks up and 2854 * pins all destination css_sets, links each to its source, and append them 2855 * to @mgctx->preloaded_dst_csets. 2856 * 2857 * This function must be called after cgroup_migrate_add_src() has been 2858 * called on each migration source css_set. After migration is performed 2859 * using cgroup_migrate(), cgroup_migrate_finish() must be called on 2860 * @mgctx. 2861 */ 2862 int cgroup_migrate_prepare_dst(struct cgroup_mgctx *mgctx) 2863 { 2864 struct css_set *src_cset, *tmp_cset; 2865 2866 lockdep_assert_held(&cgroup_mutex); 2867 2868 /* look up the dst cset for each src cset and link it to src */ 2869 list_for_each_entry_safe(src_cset, tmp_cset, &mgctx->preloaded_src_csets, 2870 mg_src_preload_node) { 2871 struct css_set *dst_cset; 2872 struct cgroup_subsys *ss; 2873 int ssid; 2874 2875 dst_cset = find_css_set(src_cset, src_cset->mg_dst_cgrp); 2876 if (!dst_cset) 2877 return -ENOMEM; 2878 2879 WARN_ON_ONCE(src_cset->mg_dst_cset || dst_cset->mg_dst_cset); 2880 2881 /* 2882 * If src cset equals dst, it's noop. Drop the src. 2883 * cgroup_migrate() will skip the cset too. Note that we 2884 * can't handle src == dst as some nodes are used by both. 2885 */ 2886 if (src_cset == dst_cset) { 2887 src_cset->mg_src_cgrp = NULL; 2888 src_cset->mg_dst_cgrp = NULL; 2889 list_del_init(&src_cset->mg_src_preload_node); 2890 put_css_set(src_cset); 2891 put_css_set(dst_cset); 2892 continue; 2893 } 2894 2895 src_cset->mg_dst_cset = dst_cset; 2896 2897 if (list_empty(&dst_cset->mg_dst_preload_node)) 2898 list_add_tail(&dst_cset->mg_dst_preload_node, 2899 &mgctx->preloaded_dst_csets); 2900 else 2901 put_css_set(dst_cset); 2902 2903 for_each_subsys(ss, ssid) 2904 if (src_cset->subsys[ssid] != dst_cset->subsys[ssid]) 2905 mgctx->ss_mask |= 1 << ssid; 2906 } 2907 2908 return 0; 2909 } 2910 2911 /** 2912 * cgroup_migrate - migrate a process or task to a cgroup 2913 * @leader: the leader of the process or the task to migrate 2914 * @threadgroup: whether @leader points to the whole process or a single task 2915 * @mgctx: migration context 2916 * 2917 * Migrate a process or task denoted by @leader. If migrating a process, 2918 * the caller must be holding cgroup_threadgroup_rwsem. The caller is also 2919 * responsible for invoking cgroup_migrate_add_src() and 2920 * cgroup_migrate_prepare_dst() on the targets before invoking this 2921 * function and following up with cgroup_migrate_finish(). 2922 * 2923 * As long as a controller's ->can_attach() doesn't fail, this function is 2924 * guaranteed to succeed. This means that, excluding ->can_attach() 2925 * failure, when migrating multiple targets, the success or failure can be 2926 * decided for all targets by invoking group_migrate_prepare_dst() before 2927 * actually starting migrating. 2928 */ 2929 int cgroup_migrate(struct task_struct *leader, bool threadgroup, 2930 struct cgroup_mgctx *mgctx) 2931 { 2932 struct task_struct *task; 2933 2934 /* 2935 * The following thread iteration should be inside an RCU critical 2936 * section to prevent tasks from being freed while taking the snapshot. 2937 * spin_lock_irq() implies RCU critical section here. 2938 */ 2939 spin_lock_irq(&css_set_lock); 2940 task = leader; 2941 do { 2942 cgroup_migrate_add_task(task, mgctx); 2943 if (!threadgroup) 2944 break; 2945 } while_each_thread(leader, task); 2946 spin_unlock_irq(&css_set_lock); 2947 2948 return cgroup_migrate_execute(mgctx); 2949 } 2950 2951 /** 2952 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup 2953 * @dst_cgrp: the cgroup to attach to 2954 * @leader: the task or the leader of the threadgroup to be attached 2955 * @threadgroup: attach the whole threadgroup? 2956 * 2957 * Call holding cgroup_mutex and cgroup_threadgroup_rwsem. 2958 */ 2959 int cgroup_attach_task(struct cgroup *dst_cgrp, struct task_struct *leader, 2960 bool threadgroup) 2961 { 2962 DEFINE_CGROUP_MGCTX(mgctx); 2963 struct task_struct *task; 2964 int ret = 0; 2965 2966 /* look up all src csets */ 2967 spin_lock_irq(&css_set_lock); 2968 task = leader; 2969 do { 2970 cgroup_migrate_add_src(task_css_set(task), dst_cgrp, &mgctx); 2971 if (!threadgroup) 2972 break; 2973 } while_each_thread(leader, task); 2974 spin_unlock_irq(&css_set_lock); 2975 2976 /* prepare dst csets and commit */ 2977 ret = cgroup_migrate_prepare_dst(&mgctx); 2978 if (!ret) 2979 ret = cgroup_migrate(leader, threadgroup, &mgctx); 2980 2981 cgroup_migrate_finish(&mgctx); 2982 2983 if (!ret) 2984 TRACE_CGROUP_PATH(attach_task, dst_cgrp, leader, threadgroup); 2985 2986 return ret; 2987 } 2988 2989 struct task_struct *cgroup_procs_write_start(char *buf, bool threadgroup, 2990 enum cgroup_attach_lock_mode *lock_mode) 2991 { 2992 struct task_struct *tsk; 2993 pid_t pid; 2994 2995 if (kstrtoint(strstrip(buf), 0, &pid) || pid < 0) 2996 return ERR_PTR(-EINVAL); 2997 2998 retry_find_task: 2999 rcu_read_lock(); 3000 if (pid) { 3001 tsk = find_task_by_vpid(pid); 3002 if (!tsk) { 3003 tsk = ERR_PTR(-ESRCH); 3004 goto out_unlock_rcu; 3005 } 3006 } else { 3007 tsk = current; 3008 } 3009 3010 if (threadgroup) 3011 tsk = tsk->group_leader; 3012 3013 /* 3014 * kthreads may acquire PF_NO_SETAFFINITY during initialization. 3015 * If userland migrates such a kthread to a non-root cgroup, it can 3016 * become trapped in a cpuset, or RT kthread may be born in a 3017 * cgroup with no rt_runtime allocated. Just say no. 3018 */ 3019 if (tsk->no_cgroup_migration || (tsk->flags & PF_NO_SETAFFINITY)) { 3020 tsk = ERR_PTR(-EINVAL); 3021 goto out_unlock_rcu; 3022 } 3023 get_task_struct(tsk); 3024 rcu_read_unlock(); 3025 3026 /* 3027 * If we migrate a single thread, we don't care about threadgroup 3028 * stability. If the thread is `current`, it won't exit(2) under our 3029 * hands or change PID through exec(2). We exclude 3030 * cgroup_update_dfl_csses and other cgroup_{proc,thread}s_write callers 3031 * by cgroup_mutex. Therefore, we can skip the global lock. 3032 */ 3033 lockdep_assert_held(&cgroup_mutex); 3034 3035 if (pid || threadgroup) { 3036 if (cgroup_enable_per_threadgroup_rwsem) 3037 *lock_mode = CGRP_ATTACH_LOCK_PER_THREADGROUP; 3038 else 3039 *lock_mode = CGRP_ATTACH_LOCK_GLOBAL; 3040 } else { 3041 *lock_mode = CGRP_ATTACH_LOCK_NONE; 3042 } 3043 3044 cgroup_attach_lock(*lock_mode, tsk); 3045 3046 if (threadgroup) { 3047 if (!thread_group_leader(tsk)) { 3048 /* 3049 * A race with de_thread from another thread's exec() 3050 * may strip us of our leadership. If this happens, 3051 * throw this task away and try again. 3052 */ 3053 cgroup_attach_unlock(*lock_mode, tsk); 3054 put_task_struct(tsk); 3055 goto retry_find_task; 3056 } 3057 } 3058 3059 return tsk; 3060 3061 out_unlock_rcu: 3062 rcu_read_unlock(); 3063 return tsk; 3064 } 3065 3066 void cgroup_procs_write_finish(struct task_struct *task, 3067 enum cgroup_attach_lock_mode lock_mode) 3068 { 3069 cgroup_attach_unlock(lock_mode, task); 3070 3071 /* release reference from cgroup_procs_write_start() */ 3072 put_task_struct(task); 3073 } 3074 3075 static void cgroup_print_ss_mask(struct seq_file *seq, u32 ss_mask) 3076 { 3077 struct cgroup_subsys *ss; 3078 bool printed = false; 3079 int ssid; 3080 3081 do_each_subsys_mask(ss, ssid, ss_mask) { 3082 if (printed) 3083 seq_putc(seq, ' '); 3084 seq_puts(seq, ss->name); 3085 printed = true; 3086 } while_each_subsys_mask(); 3087 if (printed) 3088 seq_putc(seq, '\n'); 3089 } 3090 3091 /* show controllers which are enabled from the parent */ 3092 static int cgroup_controllers_show(struct seq_file *seq, void *v) 3093 { 3094 struct cgroup *cgrp = seq_css(seq)->cgroup; 3095 3096 cgroup_print_ss_mask(seq, cgroup_control(cgrp)); 3097 return 0; 3098 } 3099 3100 /* show controllers which are enabled for a given cgroup's children */ 3101 static int cgroup_subtree_control_show(struct seq_file *seq, void *v) 3102 { 3103 struct cgroup *cgrp = seq_css(seq)->cgroup; 3104 3105 cgroup_print_ss_mask(seq, cgrp->subtree_control); 3106 return 0; 3107 } 3108 3109 /** 3110 * cgroup_update_dfl_csses - update css assoc of a subtree in default hierarchy 3111 * @cgrp: root of the subtree to update csses for 3112 * 3113 * @cgrp's control masks have changed and its subtree's css associations 3114 * need to be updated accordingly. This function looks up all css_sets 3115 * which are attached to the subtree, creates the matching updated css_sets 3116 * and migrates the tasks to the new ones. 3117 */ 3118 static int cgroup_update_dfl_csses(struct cgroup *cgrp) 3119 { 3120 DEFINE_CGROUP_MGCTX(mgctx); 3121 struct cgroup_subsys_state *d_css; 3122 struct cgroup *dsct; 3123 struct css_set *src_cset; 3124 enum cgroup_attach_lock_mode lock_mode; 3125 bool has_tasks; 3126 int ret; 3127 3128 lockdep_assert_held(&cgroup_mutex); 3129 3130 /* look up all csses currently attached to @cgrp's subtree */ 3131 spin_lock_irq(&css_set_lock); 3132 cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) { 3133 struct cgrp_cset_link *link; 3134 3135 /* 3136 * As cgroup_update_dfl_csses() is only called by 3137 * cgroup_apply_control(). The csses associated with the 3138 * given cgrp will not be affected by changes made to 3139 * its subtree_control file. We can skip them. 3140 */ 3141 if (dsct == cgrp) 3142 continue; 3143 3144 list_for_each_entry(link, &dsct->cset_links, cset_link) 3145 cgroup_migrate_add_src(link->cset, dsct, &mgctx); 3146 } 3147 spin_unlock_irq(&css_set_lock); 3148 3149 /* 3150 * We need to write-lock threadgroup_rwsem while migrating tasks. 3151 * However, if there are no source csets for @cgrp, changing its 3152 * controllers isn't gonna produce any task migrations and the 3153 * write-locking can be skipped safely. 3154 */ 3155 has_tasks = !list_empty(&mgctx.preloaded_src_csets); 3156 3157 if (has_tasks) 3158 lock_mode = CGRP_ATTACH_LOCK_GLOBAL; 3159 else 3160 lock_mode = CGRP_ATTACH_LOCK_NONE; 3161 3162 cgroup_attach_lock(lock_mode, NULL); 3163 3164 /* NULL dst indicates self on default hierarchy */ 3165 ret = cgroup_migrate_prepare_dst(&mgctx); 3166 if (ret) 3167 goto out_finish; 3168 3169 spin_lock_irq(&css_set_lock); 3170 list_for_each_entry(src_cset, &mgctx.preloaded_src_csets, 3171 mg_src_preload_node) { 3172 struct task_struct *task, *ntask; 3173 3174 /* all tasks in src_csets need to be migrated */ 3175 list_for_each_entry_safe(task, ntask, &src_cset->tasks, cg_list) 3176 cgroup_migrate_add_task(task, &mgctx); 3177 } 3178 spin_unlock_irq(&css_set_lock); 3179 3180 ret = cgroup_migrate_execute(&mgctx); 3181 out_finish: 3182 cgroup_migrate_finish(&mgctx); 3183 cgroup_attach_unlock(lock_mode, NULL); 3184 return ret; 3185 } 3186 3187 /** 3188 * cgroup_lock_and_drain_offline - lock cgroup_mutex and drain offlined csses 3189 * @cgrp: root of the target subtree 3190 * 3191 * Because css offlining is asynchronous, userland may try to re-enable a 3192 * controller while the previous css is still around. This function grabs 3193 * cgroup_mutex and drains the previous css instances of @cgrp's subtree. 3194 */ 3195 void cgroup_lock_and_drain_offline(struct cgroup *cgrp) 3196 __acquires(&cgroup_mutex) 3197 { 3198 struct cgroup *dsct; 3199 struct cgroup_subsys_state *d_css; 3200 struct cgroup_subsys *ss; 3201 int ssid; 3202 3203 restart: 3204 cgroup_lock(); 3205 3206 cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) { 3207 for_each_subsys(ss, ssid) { 3208 struct cgroup_subsys_state *css = cgroup_css(dsct, ss); 3209 DEFINE_WAIT(wait); 3210 3211 if (!css || !percpu_ref_is_dying(&css->refcnt)) 3212 continue; 3213 3214 cgroup_get_live(dsct); 3215 prepare_to_wait(&dsct->offline_waitq, &wait, 3216 TASK_UNINTERRUPTIBLE); 3217 3218 cgroup_unlock(); 3219 schedule(); 3220 finish_wait(&dsct->offline_waitq, &wait); 3221 3222 cgroup_put(dsct); 3223 goto restart; 3224 } 3225 } 3226 } 3227 3228 /** 3229 * cgroup_save_control - save control masks and dom_cgrp of a subtree 3230 * @cgrp: root of the target subtree 3231 * 3232 * Save ->subtree_control, ->subtree_ss_mask and ->dom_cgrp to the 3233 * respective old_ prefixed fields for @cgrp's subtree including @cgrp 3234 * itself. 3235 */ 3236 static void cgroup_save_control(struct cgroup *cgrp) 3237 { 3238 struct cgroup *dsct; 3239 struct cgroup_subsys_state *d_css; 3240 3241 cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) { 3242 dsct->old_subtree_control = dsct->subtree_control; 3243 dsct->old_subtree_ss_mask = dsct->subtree_ss_mask; 3244 dsct->old_dom_cgrp = dsct->dom_cgrp; 3245 } 3246 } 3247 3248 /** 3249 * cgroup_propagate_control - refresh control masks of a subtree 3250 * @cgrp: root of the target subtree 3251 * 3252 * For @cgrp and its subtree, ensure ->subtree_ss_mask matches 3253 * ->subtree_control and propagate controller availability through the 3254 * subtree so that descendants don't have unavailable controllers enabled. 3255 */ 3256 static void cgroup_propagate_control(struct cgroup *cgrp) 3257 { 3258 struct cgroup *dsct; 3259 struct cgroup_subsys_state *d_css; 3260 3261 cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) { 3262 dsct->subtree_control &= cgroup_control(dsct); 3263 dsct->subtree_ss_mask = 3264 cgroup_calc_subtree_ss_mask(dsct->subtree_control, 3265 cgroup_ss_mask(dsct)); 3266 } 3267 } 3268 3269 /** 3270 * cgroup_restore_control - restore control masks and dom_cgrp of a subtree 3271 * @cgrp: root of the target subtree 3272 * 3273 * Restore ->subtree_control, ->subtree_ss_mask and ->dom_cgrp from the 3274 * respective old_ prefixed fields for @cgrp's subtree including @cgrp 3275 * itself. 3276 */ 3277 static void cgroup_restore_control(struct cgroup *cgrp) 3278 { 3279 struct cgroup *dsct; 3280 struct cgroup_subsys_state *d_css; 3281 3282 cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) { 3283 dsct->subtree_control = dsct->old_subtree_control; 3284 dsct->subtree_ss_mask = dsct->old_subtree_ss_mask; 3285 dsct->dom_cgrp = dsct->old_dom_cgrp; 3286 } 3287 } 3288 3289 static bool css_visible(struct cgroup_subsys_state *css) 3290 { 3291 struct cgroup_subsys *ss = css->ss; 3292 struct cgroup *cgrp = css->cgroup; 3293 3294 if (cgroup_control(cgrp) & (1 << ss->id)) 3295 return true; 3296 if (!(cgroup_ss_mask(cgrp) & (1 << ss->id))) 3297 return false; 3298 return cgroup_on_dfl(cgrp) && ss->implicit_on_dfl; 3299 } 3300 3301 /** 3302 * cgroup_apply_control_enable - enable or show csses according to control 3303 * @cgrp: root of the target subtree 3304 * 3305 * Walk @cgrp's subtree and create new csses or make the existing ones 3306 * visible. A css is created invisible if it's being implicitly enabled 3307 * through dependency. An invisible css is made visible when the userland 3308 * explicitly enables it. 3309 * 3310 * Returns 0 on success, -errno on failure. On failure, csses which have 3311 * been processed already aren't cleaned up. The caller is responsible for 3312 * cleaning up with cgroup_apply_control_disable(). 3313 */ 3314 static int cgroup_apply_control_enable(struct cgroup *cgrp) 3315 { 3316 struct cgroup *dsct; 3317 struct cgroup_subsys_state *d_css; 3318 struct cgroup_subsys *ss; 3319 int ssid, ret; 3320 3321 cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) { 3322 for_each_subsys(ss, ssid) { 3323 struct cgroup_subsys_state *css = cgroup_css(dsct, ss); 3324 3325 if (!(cgroup_ss_mask(dsct) & (1 << ss->id))) 3326 continue; 3327 3328 if (!css) { 3329 css = css_create(dsct, ss); 3330 if (IS_ERR(css)) 3331 return PTR_ERR(css); 3332 } 3333 3334 WARN_ON_ONCE(percpu_ref_is_dying(&css->refcnt)); 3335 3336 if (css_visible(css)) { 3337 ret = css_populate_dir(css); 3338 if (ret) 3339 return ret; 3340 } 3341 } 3342 } 3343 3344 return 0; 3345 } 3346 3347 /** 3348 * cgroup_apply_control_disable - kill or hide csses according to control 3349 * @cgrp: root of the target subtree 3350 * 3351 * Walk @cgrp's subtree and kill and hide csses so that they match 3352 * cgroup_ss_mask() and cgroup_visible_mask(). 3353 * 3354 * A css is hidden when the userland requests it to be disabled while other 3355 * subsystems are still depending on it. The css must not actively control 3356 * resources and be in the vanilla state if it's made visible again later. 3357 * Controllers which may be depended upon should provide ->css_reset() for 3358 * this purpose. 3359 */ 3360 static void cgroup_apply_control_disable(struct cgroup *cgrp) 3361 { 3362 struct cgroup *dsct; 3363 struct cgroup_subsys_state *d_css; 3364 struct cgroup_subsys *ss; 3365 int ssid; 3366 3367 cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) { 3368 for_each_subsys(ss, ssid) { 3369 struct cgroup_subsys_state *css = cgroup_css(dsct, ss); 3370 3371 if (!css) 3372 continue; 3373 3374 WARN_ON_ONCE(percpu_ref_is_dying(&css->refcnt)); 3375 3376 if (css->parent && 3377 !(cgroup_ss_mask(dsct) & (1 << ss->id))) { 3378 kill_css(css); 3379 } else if (!css_visible(css)) { 3380 css_clear_dir(css); 3381 if (ss->css_reset) 3382 ss->css_reset(css); 3383 } 3384 } 3385 } 3386 } 3387 3388 /** 3389 * cgroup_apply_control - apply control mask updates to the subtree 3390 * @cgrp: root of the target subtree 3391 * 3392 * subsystems can be enabled and disabled in a subtree using the following 3393 * steps. 3394 * 3395 * 1. Call cgroup_save_control() to stash the current state. 3396 * 2. Update ->subtree_control masks in the subtree as desired. 3397 * 3. Call cgroup_apply_control() to apply the changes. 3398 * 4. Optionally perform other related operations. 3399 * 5. Call cgroup_finalize_control() to finish up. 3400 * 3401 * This function implements step 3 and propagates the mask changes 3402 * throughout @cgrp's subtree, updates csses accordingly and perform 3403 * process migrations. 3404 */ 3405 static int cgroup_apply_control(struct cgroup *cgrp) 3406 { 3407 int ret; 3408 3409 cgroup_propagate_control(cgrp); 3410 3411 ret = cgroup_apply_control_enable(cgrp); 3412 if (ret) 3413 return ret; 3414 3415 /* 3416 * At this point, cgroup_e_css_by_mask() results reflect the new csses 3417 * making the following cgroup_update_dfl_csses() properly update 3418 * css associations of all tasks in the subtree. 3419 */ 3420 return cgroup_update_dfl_csses(cgrp); 3421 } 3422 3423 /** 3424 * cgroup_finalize_control - finalize control mask update 3425 * @cgrp: root of the target subtree 3426 * @ret: the result of the update 3427 * 3428 * Finalize control mask update. See cgroup_apply_control() for more info. 3429 */ 3430 static void cgroup_finalize_control(struct cgroup *cgrp, int ret) 3431 { 3432 if (ret) { 3433 cgroup_restore_control(cgrp); 3434 cgroup_propagate_control(cgrp); 3435 } 3436 3437 cgroup_apply_control_disable(cgrp); 3438 } 3439 3440 static int cgroup_vet_subtree_control_enable(struct cgroup *cgrp, u32 enable) 3441 { 3442 u32 domain_enable = enable & ~cgrp_dfl_threaded_ss_mask; 3443 3444 /* if nothing is getting enabled, nothing to worry about */ 3445 if (!enable) 3446 return 0; 3447 3448 /* can @cgrp host any resources? */ 3449 if (!cgroup_is_valid_domain(cgrp->dom_cgrp)) 3450 return -EOPNOTSUPP; 3451 3452 /* mixables don't care */ 3453 if (cgroup_is_mixable(cgrp)) 3454 return 0; 3455 3456 if (domain_enable) { 3457 /* can't enable domain controllers inside a thread subtree */ 3458 if (cgroup_is_thread_root(cgrp) || cgroup_is_threaded(cgrp)) 3459 return -EOPNOTSUPP; 3460 } else { 3461 /* 3462 * Threaded controllers can handle internal competitions 3463 * and are always allowed inside a (prospective) thread 3464 * subtree. 3465 */ 3466 if (cgroup_can_be_thread_root(cgrp) || cgroup_is_threaded(cgrp)) 3467 return 0; 3468 } 3469 3470 /* 3471 * Controllers can't be enabled for a cgroup with tasks to avoid 3472 * child cgroups competing against tasks. 3473 */ 3474 if (cgroup_has_tasks(cgrp)) 3475 return -EBUSY; 3476 3477 return 0; 3478 } 3479 3480 /* change the enabled child controllers for a cgroup in the default hierarchy */ 3481 static ssize_t cgroup_subtree_control_write(struct kernfs_open_file *of, 3482 char *buf, size_t nbytes, 3483 loff_t off) 3484 { 3485 u32 enable = 0, disable = 0; 3486 struct cgroup *cgrp, *child; 3487 struct cgroup_subsys *ss; 3488 char *tok; 3489 int ssid, ret; 3490 3491 /* 3492 * Parse input - space separated list of subsystem names prefixed 3493 * with either + or -. 3494 */ 3495 buf = strstrip(buf); 3496 while ((tok = strsep(&buf, " "))) { 3497 if (tok[0] == '\0') 3498 continue; 3499 do_each_subsys_mask(ss, ssid, ~cgrp_dfl_inhibit_ss_mask) { 3500 if (!cgroup_ssid_enabled(ssid) || 3501 strcmp(tok + 1, ss->name)) 3502 continue; 3503 3504 if (*tok == '+') { 3505 enable |= 1 << ssid; 3506 disable &= ~(1 << ssid); 3507 } else if (*tok == '-') { 3508 disable |= 1 << ssid; 3509 enable &= ~(1 << ssid); 3510 } else { 3511 return -EINVAL; 3512 } 3513 break; 3514 } while_each_subsys_mask(); 3515 if (ssid == CGROUP_SUBSYS_COUNT) 3516 return -EINVAL; 3517 } 3518 3519 cgrp = cgroup_kn_lock_live(of->kn, true); 3520 if (!cgrp) 3521 return -ENODEV; 3522 3523 for_each_subsys(ss, ssid) { 3524 if (enable & (1 << ssid)) { 3525 if (cgrp->subtree_control & (1 << ssid)) { 3526 enable &= ~(1 << ssid); 3527 continue; 3528 } 3529 3530 if (!(cgroup_control(cgrp) & (1 << ssid))) { 3531 ret = -ENOENT; 3532 goto out_unlock; 3533 } 3534 } else if (disable & (1 << ssid)) { 3535 if (!(cgrp->subtree_control & (1 << ssid))) { 3536 disable &= ~(1 << ssid); 3537 continue; 3538 } 3539 3540 /* a child has it enabled? */ 3541 cgroup_for_each_live_child(child, cgrp) { 3542 if (child->subtree_control & (1 << ssid)) { 3543 ret = -EBUSY; 3544 goto out_unlock; 3545 } 3546 } 3547 } 3548 } 3549 3550 if (!enable && !disable) { 3551 ret = 0; 3552 goto out_unlock; 3553 } 3554 3555 ret = cgroup_vet_subtree_control_enable(cgrp, enable); 3556 if (ret) 3557 goto out_unlock; 3558 3559 /* save and update control masks and prepare csses */ 3560 cgroup_save_control(cgrp); 3561 3562 cgrp->subtree_control |= enable; 3563 cgrp->subtree_control &= ~disable; 3564 3565 ret = cgroup_apply_control(cgrp); 3566 cgroup_finalize_control(cgrp, ret); 3567 if (ret) 3568 goto out_unlock; 3569 3570 kernfs_activate(cgrp->kn); 3571 out_unlock: 3572 cgroup_kn_unlock(of->kn); 3573 return ret ?: nbytes; 3574 } 3575 3576 /** 3577 * cgroup_enable_threaded - make @cgrp threaded 3578 * @cgrp: the target cgroup 3579 * 3580 * Called when "threaded" is written to the cgroup.type interface file and 3581 * tries to make @cgrp threaded and join the parent's resource domain. 3582 * This function is never called on the root cgroup as cgroup.type doesn't 3583 * exist on it. 3584 */ 3585 static int cgroup_enable_threaded(struct cgroup *cgrp) 3586 { 3587 struct cgroup *parent = cgroup_parent(cgrp); 3588 struct cgroup *dom_cgrp = parent->dom_cgrp; 3589 struct cgroup *dsct; 3590 struct cgroup_subsys_state *d_css; 3591 int ret; 3592 3593 lockdep_assert_held(&cgroup_mutex); 3594 3595 /* noop if already threaded */ 3596 if (cgroup_is_threaded(cgrp)) 3597 return 0; 3598 3599 /* 3600 * If @cgroup is populated or has domain controllers enabled, it 3601 * can't be switched. While the below cgroup_can_be_thread_root() 3602 * test can catch the same conditions, that's only when @parent is 3603 * not mixable, so let's check it explicitly. 3604 */ 3605 if (cgroup_is_populated(cgrp) || 3606 cgrp->subtree_control & ~cgrp_dfl_threaded_ss_mask) 3607 return -EOPNOTSUPP; 3608 3609 /* we're joining the parent's domain, ensure its validity */ 3610 if (!cgroup_is_valid_domain(dom_cgrp) || 3611 !cgroup_can_be_thread_root(dom_cgrp)) 3612 return -EOPNOTSUPP; 3613 3614 /* 3615 * The following shouldn't cause actual migrations and should 3616 * always succeed. 3617 */ 3618 cgroup_save_control(cgrp); 3619 3620 cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) 3621 if (dsct == cgrp || cgroup_is_threaded(dsct)) 3622 dsct->dom_cgrp = dom_cgrp; 3623 3624 ret = cgroup_apply_control(cgrp); 3625 if (!ret) 3626 parent->nr_threaded_children++; 3627 3628 cgroup_finalize_control(cgrp, ret); 3629 return ret; 3630 } 3631 3632 static int cgroup_type_show(struct seq_file *seq, void *v) 3633 { 3634 struct cgroup *cgrp = seq_css(seq)->cgroup; 3635 3636 if (cgroup_is_threaded(cgrp)) 3637 seq_puts(seq, "threaded\n"); 3638 else if (!cgroup_is_valid_domain(cgrp)) 3639 seq_puts(seq, "domain invalid\n"); 3640 else if (cgroup_is_thread_root(cgrp)) 3641 seq_puts(seq, "domain threaded\n"); 3642 else 3643 seq_puts(seq, "domain\n"); 3644 3645 return 0; 3646 } 3647 3648 static ssize_t cgroup_type_write(struct kernfs_open_file *of, char *buf, 3649 size_t nbytes, loff_t off) 3650 { 3651 struct cgroup *cgrp; 3652 int ret; 3653 3654 /* only switching to threaded mode is supported */ 3655 if (strcmp(strstrip(buf), "threaded")) 3656 return -EINVAL; 3657 3658 /* drain dying csses before we re-apply (threaded) subtree control */ 3659 cgrp = cgroup_kn_lock_live(of->kn, true); 3660 if (!cgrp) 3661 return -ENOENT; 3662 3663 /* threaded can only be enabled */ 3664 ret = cgroup_enable_threaded(cgrp); 3665 3666 cgroup_kn_unlock(of->kn); 3667 return ret ?: nbytes; 3668 } 3669 3670 static int cgroup_max_descendants_show(struct seq_file *seq, void *v) 3671 { 3672 struct cgroup *cgrp = seq_css(seq)->cgroup; 3673 int descendants = READ_ONCE(cgrp->max_descendants); 3674 3675 if (descendants == INT_MAX) 3676 seq_puts(seq, "max\n"); 3677 else 3678 seq_printf(seq, "%d\n", descendants); 3679 3680 return 0; 3681 } 3682 3683 static ssize_t cgroup_max_descendants_write(struct kernfs_open_file *of, 3684 char *buf, size_t nbytes, loff_t off) 3685 { 3686 struct cgroup *cgrp; 3687 int descendants; 3688 ssize_t ret; 3689 3690 buf = strstrip(buf); 3691 if (!strcmp(buf, "max")) { 3692 descendants = INT_MAX; 3693 } else { 3694 ret = kstrtoint(buf, 0, &descendants); 3695 if (ret) 3696 return ret; 3697 } 3698 3699 if (descendants < 0) 3700 return -ERANGE; 3701 3702 cgrp = cgroup_kn_lock_live(of->kn, false); 3703 if (!cgrp) 3704 return -ENOENT; 3705 3706 cgrp->max_descendants = descendants; 3707 3708 cgroup_kn_unlock(of->kn); 3709 3710 return nbytes; 3711 } 3712 3713 static int cgroup_max_depth_show(struct seq_file *seq, void *v) 3714 { 3715 struct cgroup *cgrp = seq_css(seq)->cgroup; 3716 int depth = READ_ONCE(cgrp->max_depth); 3717 3718 if (depth == INT_MAX) 3719 seq_puts(seq, "max\n"); 3720 else 3721 seq_printf(seq, "%d\n", depth); 3722 3723 return 0; 3724 } 3725 3726 static ssize_t cgroup_max_depth_write(struct kernfs_open_file *of, 3727 char *buf, size_t nbytes, loff_t off) 3728 { 3729 struct cgroup *cgrp; 3730 ssize_t ret; 3731 int depth; 3732 3733 buf = strstrip(buf); 3734 if (!strcmp(buf, "max")) { 3735 depth = INT_MAX; 3736 } else { 3737 ret = kstrtoint(buf, 0, &depth); 3738 if (ret) 3739 return ret; 3740 } 3741 3742 if (depth < 0) 3743 return -ERANGE; 3744 3745 cgrp = cgroup_kn_lock_live(of->kn, false); 3746 if (!cgrp) 3747 return -ENOENT; 3748 3749 cgrp->max_depth = depth; 3750 3751 cgroup_kn_unlock(of->kn); 3752 3753 return nbytes; 3754 } 3755 3756 static int cgroup_events_show(struct seq_file *seq, void *v) 3757 { 3758 struct cgroup *cgrp = seq_css(seq)->cgroup; 3759 3760 seq_printf(seq, "populated %d\n", cgroup_is_populated(cgrp)); 3761 seq_printf(seq, "frozen %d\n", test_bit(CGRP_FROZEN, &cgrp->flags)); 3762 3763 return 0; 3764 } 3765 3766 static int cgroup_stat_show(struct seq_file *seq, void *v) 3767 { 3768 struct cgroup *cgroup = seq_css(seq)->cgroup; 3769 struct cgroup_subsys_state *css; 3770 int dying_cnt[CGROUP_SUBSYS_COUNT]; 3771 int ssid; 3772 3773 seq_printf(seq, "nr_descendants %d\n", 3774 cgroup->nr_descendants); 3775 3776 /* 3777 * Show the number of live and dying csses associated with each of 3778 * non-inhibited cgroup subsystems that is bound to cgroup v2. 3779 * 3780 * Without proper lock protection, racing is possible. So the 3781 * numbers may not be consistent when that happens. 3782 */ 3783 rcu_read_lock(); 3784 for (ssid = 0; ssid < CGROUP_SUBSYS_COUNT; ssid++) { 3785 dying_cnt[ssid] = -1; 3786 if ((BIT(ssid) & cgrp_dfl_inhibit_ss_mask) || 3787 (cgroup_subsys[ssid]->root != &cgrp_dfl_root)) 3788 continue; 3789 css = rcu_dereference_raw(cgroup->subsys[ssid]); 3790 dying_cnt[ssid] = cgroup->nr_dying_subsys[ssid]; 3791 seq_printf(seq, "nr_subsys_%s %d\n", cgroup_subsys[ssid]->name, 3792 css ? (css->nr_descendants + 1) : 0); 3793 } 3794 3795 seq_printf(seq, "nr_dying_descendants %d\n", 3796 cgroup->nr_dying_descendants); 3797 for (ssid = 0; ssid < CGROUP_SUBSYS_COUNT; ssid++) { 3798 if (dying_cnt[ssid] >= 0) 3799 seq_printf(seq, "nr_dying_subsys_%s %d\n", 3800 cgroup_subsys[ssid]->name, dying_cnt[ssid]); 3801 } 3802 rcu_read_unlock(); 3803 return 0; 3804 } 3805 3806 static int cgroup_core_local_stat_show(struct seq_file *seq, void *v) 3807 { 3808 struct cgroup *cgrp = seq_css(seq)->cgroup; 3809 unsigned int sequence; 3810 u64 freeze_time; 3811 3812 do { 3813 sequence = read_seqcount_begin(&cgrp->freezer.freeze_seq); 3814 freeze_time = cgrp->freezer.frozen_nsec; 3815 /* Add in current freezer interval if the cgroup is freezing. */ 3816 if (test_bit(CGRP_FREEZE, &cgrp->flags)) 3817 freeze_time += (ktime_get_ns() - 3818 cgrp->freezer.freeze_start_nsec); 3819 } while (read_seqcount_retry(&cgrp->freezer.freeze_seq, sequence)); 3820 3821 do_div(freeze_time, NSEC_PER_USEC); 3822 seq_printf(seq, "frozen_usec %llu\n", freeze_time); 3823 3824 return 0; 3825 } 3826 3827 #ifdef CONFIG_CGROUP_SCHED 3828 /** 3829 * cgroup_tryget_css - try to get a cgroup's css for the specified subsystem 3830 * @cgrp: the cgroup of interest 3831 * @ss: the subsystem of interest 3832 * 3833 * Find and get @cgrp's css associated with @ss. If the css doesn't exist 3834 * or is offline, %NULL is returned. 3835 */ 3836 static struct cgroup_subsys_state *cgroup_tryget_css(struct cgroup *cgrp, 3837 struct cgroup_subsys *ss) 3838 { 3839 struct cgroup_subsys_state *css; 3840 3841 rcu_read_lock(); 3842 css = cgroup_css(cgrp, ss); 3843 if (css && !css_tryget_online(css)) 3844 css = NULL; 3845 rcu_read_unlock(); 3846 3847 return css; 3848 } 3849 3850 static int cgroup_extra_stat_show(struct seq_file *seq, int ssid) 3851 { 3852 struct cgroup *cgrp = seq_css(seq)->cgroup; 3853 struct cgroup_subsys *ss = cgroup_subsys[ssid]; 3854 struct cgroup_subsys_state *css; 3855 int ret; 3856 3857 if (!ss->css_extra_stat_show) 3858 return 0; 3859 3860 css = cgroup_tryget_css(cgrp, ss); 3861 if (!css) 3862 return 0; 3863 3864 ret = ss->css_extra_stat_show(seq, css); 3865 css_put(css); 3866 return ret; 3867 } 3868 3869 static int cgroup_local_stat_show(struct seq_file *seq, 3870 struct cgroup *cgrp, int ssid) 3871 { 3872 struct cgroup_subsys *ss = cgroup_subsys[ssid]; 3873 struct cgroup_subsys_state *css; 3874 int ret; 3875 3876 if (!ss->css_local_stat_show) 3877 return 0; 3878 3879 css = cgroup_tryget_css(cgrp, ss); 3880 if (!css) 3881 return 0; 3882 3883 ret = ss->css_local_stat_show(seq, css); 3884 css_put(css); 3885 return ret; 3886 } 3887 #endif 3888 3889 static int cpu_stat_show(struct seq_file *seq, void *v) 3890 { 3891 int ret = 0; 3892 3893 cgroup_base_stat_cputime_show(seq); 3894 #ifdef CONFIG_CGROUP_SCHED 3895 ret = cgroup_extra_stat_show(seq, cpu_cgrp_id); 3896 #endif 3897 return ret; 3898 } 3899 3900 static int cpu_local_stat_show(struct seq_file *seq, void *v) 3901 { 3902 struct cgroup __maybe_unused *cgrp = seq_css(seq)->cgroup; 3903 int ret = 0; 3904 3905 #ifdef CONFIG_CGROUP_SCHED 3906 ret = cgroup_local_stat_show(seq, cgrp, cpu_cgrp_id); 3907 #endif 3908 return ret; 3909 } 3910 3911 #ifdef CONFIG_PSI 3912 static int cgroup_io_pressure_show(struct seq_file *seq, void *v) 3913 { 3914 struct cgroup *cgrp = seq_css(seq)->cgroup; 3915 struct psi_group *psi = cgroup_psi(cgrp); 3916 3917 return psi_show(seq, psi, PSI_IO); 3918 } 3919 static int cgroup_memory_pressure_show(struct seq_file *seq, void *v) 3920 { 3921 struct cgroup *cgrp = seq_css(seq)->cgroup; 3922 struct psi_group *psi = cgroup_psi(cgrp); 3923 3924 return psi_show(seq, psi, PSI_MEM); 3925 } 3926 static int cgroup_cpu_pressure_show(struct seq_file *seq, void *v) 3927 { 3928 struct cgroup *cgrp = seq_css(seq)->cgroup; 3929 struct psi_group *psi = cgroup_psi(cgrp); 3930 3931 return psi_show(seq, psi, PSI_CPU); 3932 } 3933 3934 static ssize_t pressure_write(struct kernfs_open_file *of, char *buf, 3935 size_t nbytes, enum psi_res res) 3936 { 3937 struct cgroup_file_ctx *ctx = of->priv; 3938 struct psi_trigger *new; 3939 struct cgroup *cgrp; 3940 struct psi_group *psi; 3941 3942 cgrp = cgroup_kn_lock_live(of->kn, false); 3943 if (!cgrp) 3944 return -ENODEV; 3945 3946 cgroup_get(cgrp); 3947 cgroup_kn_unlock(of->kn); 3948 3949 /* Allow only one trigger per file descriptor */ 3950 if (ctx->psi.trigger) { 3951 cgroup_put(cgrp); 3952 return -EBUSY; 3953 } 3954 3955 psi = cgroup_psi(cgrp); 3956 new = psi_trigger_create(psi, buf, res, of->file, of); 3957 if (IS_ERR(new)) { 3958 cgroup_put(cgrp); 3959 return PTR_ERR(new); 3960 } 3961 3962 smp_store_release(&ctx->psi.trigger, new); 3963 cgroup_put(cgrp); 3964 3965 return nbytes; 3966 } 3967 3968 static ssize_t cgroup_io_pressure_write(struct kernfs_open_file *of, 3969 char *buf, size_t nbytes, 3970 loff_t off) 3971 { 3972 return pressure_write(of, buf, nbytes, PSI_IO); 3973 } 3974 3975 static ssize_t cgroup_memory_pressure_write(struct kernfs_open_file *of, 3976 char *buf, size_t nbytes, 3977 loff_t off) 3978 { 3979 return pressure_write(of, buf, nbytes, PSI_MEM); 3980 } 3981 3982 static ssize_t cgroup_cpu_pressure_write(struct kernfs_open_file *of, 3983 char *buf, size_t nbytes, 3984 loff_t off) 3985 { 3986 return pressure_write(of, buf, nbytes, PSI_CPU); 3987 } 3988 3989 #ifdef CONFIG_IRQ_TIME_ACCOUNTING 3990 static int cgroup_irq_pressure_show(struct seq_file *seq, void *v) 3991 { 3992 struct cgroup *cgrp = seq_css(seq)->cgroup; 3993 struct psi_group *psi = cgroup_psi(cgrp); 3994 3995 return psi_show(seq, psi, PSI_IRQ); 3996 } 3997 3998 static ssize_t cgroup_irq_pressure_write(struct kernfs_open_file *of, 3999 char *buf, size_t nbytes, 4000 loff_t off) 4001 { 4002 return pressure_write(of, buf, nbytes, PSI_IRQ); 4003 } 4004 #endif 4005 4006 static int cgroup_pressure_show(struct seq_file *seq, void *v) 4007 { 4008 struct cgroup *cgrp = seq_css(seq)->cgroup; 4009 struct psi_group *psi = cgroup_psi(cgrp); 4010 4011 seq_printf(seq, "%d\n", psi->enabled); 4012 4013 return 0; 4014 } 4015 4016 static ssize_t cgroup_pressure_write(struct kernfs_open_file *of, 4017 char *buf, size_t nbytes, 4018 loff_t off) 4019 { 4020 ssize_t ret; 4021 int enable; 4022 struct cgroup *cgrp; 4023 struct psi_group *psi; 4024 4025 ret = kstrtoint(strstrip(buf), 0, &enable); 4026 if (ret) 4027 return ret; 4028 4029 if (enable < 0 || enable > 1) 4030 return -ERANGE; 4031 4032 cgrp = cgroup_kn_lock_live(of->kn, false); 4033 if (!cgrp) 4034 return -ENOENT; 4035 4036 psi = cgroup_psi(cgrp); 4037 if (psi->enabled != enable) { 4038 int i; 4039 4040 /* show or hide {cpu,memory,io,irq}.pressure files */ 4041 for (i = 0; i < NR_PSI_RESOURCES; i++) 4042 cgroup_file_show(&cgrp->psi_files[i], enable); 4043 4044 psi->enabled = enable; 4045 if (enable) 4046 psi_cgroup_restart(psi); 4047 } 4048 4049 cgroup_kn_unlock(of->kn); 4050 4051 return nbytes; 4052 } 4053 4054 static __poll_t cgroup_pressure_poll(struct kernfs_open_file *of, 4055 poll_table *pt) 4056 { 4057 struct cgroup_file_ctx *ctx = of->priv; 4058 4059 return psi_trigger_poll(&ctx->psi.trigger, of->file, pt); 4060 } 4061 4062 static void cgroup_pressure_release(struct kernfs_open_file *of) 4063 { 4064 struct cgroup_file_ctx *ctx = of->priv; 4065 4066 psi_trigger_destroy(ctx->psi.trigger); 4067 } 4068 4069 bool cgroup_psi_enabled(void) 4070 { 4071 if (static_branch_likely(&psi_disabled)) 4072 return false; 4073 4074 return (cgroup_feature_disable_mask & (1 << OPT_FEATURE_PRESSURE)) == 0; 4075 } 4076 4077 #else /* CONFIG_PSI */ 4078 bool cgroup_psi_enabled(void) 4079 { 4080 return false; 4081 } 4082 4083 #endif /* CONFIG_PSI */ 4084 4085 static int cgroup_freeze_show(struct seq_file *seq, void *v) 4086 { 4087 struct cgroup *cgrp = seq_css(seq)->cgroup; 4088 4089 seq_printf(seq, "%d\n", cgrp->freezer.freeze); 4090 4091 return 0; 4092 } 4093 4094 static ssize_t cgroup_freeze_write(struct kernfs_open_file *of, 4095 char *buf, size_t nbytes, loff_t off) 4096 { 4097 struct cgroup *cgrp; 4098 ssize_t ret; 4099 int freeze; 4100 4101 ret = kstrtoint(strstrip(buf), 0, &freeze); 4102 if (ret) 4103 return ret; 4104 4105 if (freeze < 0 || freeze > 1) 4106 return -ERANGE; 4107 4108 cgrp = cgroup_kn_lock_live(of->kn, false); 4109 if (!cgrp) 4110 return -ENOENT; 4111 4112 cgroup_freeze(cgrp, freeze); 4113 4114 cgroup_kn_unlock(of->kn); 4115 4116 return nbytes; 4117 } 4118 4119 static void __cgroup_kill(struct cgroup *cgrp) 4120 { 4121 struct css_task_iter it; 4122 struct task_struct *task; 4123 4124 lockdep_assert_held(&cgroup_mutex); 4125 4126 spin_lock_irq(&css_set_lock); 4127 cgrp->kill_seq++; 4128 spin_unlock_irq(&css_set_lock); 4129 4130 css_task_iter_start(&cgrp->self, CSS_TASK_ITER_PROCS | CSS_TASK_ITER_THREADED, &it); 4131 while ((task = css_task_iter_next(&it))) { 4132 /* Ignore kernel threads here. */ 4133 if (task->flags & PF_KTHREAD) 4134 continue; 4135 4136 /* Skip tasks that are already dying. */ 4137 if (__fatal_signal_pending(task)) 4138 continue; 4139 4140 send_sig(SIGKILL, task, 0); 4141 } 4142 css_task_iter_end(&it); 4143 } 4144 4145 static void cgroup_kill(struct cgroup *cgrp) 4146 { 4147 struct cgroup_subsys_state *css; 4148 struct cgroup *dsct; 4149 4150 lockdep_assert_held(&cgroup_mutex); 4151 4152 cgroup_for_each_live_descendant_pre(dsct, css, cgrp) 4153 __cgroup_kill(dsct); 4154 } 4155 4156 static ssize_t cgroup_kill_write(struct kernfs_open_file *of, char *buf, 4157 size_t nbytes, loff_t off) 4158 { 4159 ssize_t ret = 0; 4160 int kill; 4161 struct cgroup *cgrp; 4162 4163 ret = kstrtoint(strstrip(buf), 0, &kill); 4164 if (ret) 4165 return ret; 4166 4167 if (kill != 1) 4168 return -ERANGE; 4169 4170 cgrp = cgroup_kn_lock_live(of->kn, false); 4171 if (!cgrp) 4172 return -ENOENT; 4173 4174 /* 4175 * Killing is a process directed operation, i.e. the whole thread-group 4176 * is taken down so act like we do for cgroup.procs and only make this 4177 * writable in non-threaded cgroups. 4178 */ 4179 if (cgroup_is_threaded(cgrp)) 4180 ret = -EOPNOTSUPP; 4181 else 4182 cgroup_kill(cgrp); 4183 4184 cgroup_kn_unlock(of->kn); 4185 4186 return ret ?: nbytes; 4187 } 4188 4189 static int cgroup_file_open(struct kernfs_open_file *of) 4190 { 4191 struct cftype *cft = of_cft(of); 4192 struct cgroup_file_ctx *ctx; 4193 int ret; 4194 4195 ctx = kzalloc_obj(*ctx); 4196 if (!ctx) 4197 return -ENOMEM; 4198 4199 ctx->ns = current->nsproxy->cgroup_ns; 4200 get_cgroup_ns(ctx->ns); 4201 of->priv = ctx; 4202 4203 if (!cft->open) 4204 return 0; 4205 4206 ret = cft->open(of); 4207 if (ret) { 4208 put_cgroup_ns(ctx->ns); 4209 kfree(ctx); 4210 } 4211 return ret; 4212 } 4213 4214 static void cgroup_file_release(struct kernfs_open_file *of) 4215 { 4216 struct cftype *cft = of_cft(of); 4217 struct cgroup_file_ctx *ctx = of->priv; 4218 4219 if (cft->release) 4220 cft->release(of); 4221 put_cgroup_ns(ctx->ns); 4222 kfree(ctx); 4223 of->priv = NULL; 4224 } 4225 4226 static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf, 4227 size_t nbytes, loff_t off) 4228 { 4229 struct cgroup_file_ctx *ctx = of->priv; 4230 struct cgroup *cgrp = kn_priv(of->kn); 4231 struct cftype *cft = of_cft(of); 4232 struct cgroup_subsys_state *css; 4233 int ret; 4234 4235 if (!nbytes) 4236 return 0; 4237 4238 /* 4239 * If namespaces are delegation boundaries, disallow writes to 4240 * files in an non-init namespace root from inside the namespace 4241 * except for the files explicitly marked delegatable - 4242 * eg. cgroup.procs, cgroup.threads and cgroup.subtree_control. 4243 */ 4244 if ((cgrp->root->flags & CGRP_ROOT_NS_DELEGATE) && 4245 !(cft->flags & CFTYPE_NS_DELEGATABLE) && 4246 ctx->ns != &init_cgroup_ns && ctx->ns->root_cset->dfl_cgrp == cgrp) 4247 return -EPERM; 4248 4249 if (cft->write) 4250 return cft->write(of, buf, nbytes, off); 4251 4252 /* 4253 * kernfs guarantees that a file isn't deleted with operations in 4254 * flight, which means that the matching css is and stays alive and 4255 * doesn't need to be pinned. The RCU locking is not necessary 4256 * either. It's just for the convenience of using cgroup_css(). 4257 */ 4258 rcu_read_lock(); 4259 css = cgroup_css(cgrp, cft->ss); 4260 rcu_read_unlock(); 4261 4262 if (cft->write_u64) { 4263 unsigned long long v; 4264 ret = kstrtoull(buf, 0, &v); 4265 if (!ret) 4266 ret = cft->write_u64(css, cft, v); 4267 } else if (cft->write_s64) { 4268 long long v; 4269 ret = kstrtoll(buf, 0, &v); 4270 if (!ret) 4271 ret = cft->write_s64(css, cft, v); 4272 } else { 4273 ret = -EINVAL; 4274 } 4275 4276 return ret ?: nbytes; 4277 } 4278 4279 static __poll_t cgroup_file_poll(struct kernfs_open_file *of, poll_table *pt) 4280 { 4281 struct cftype *cft = of_cft(of); 4282 4283 if (cft->poll) 4284 return cft->poll(of, pt); 4285 4286 return kernfs_generic_poll(of, pt); 4287 } 4288 4289 static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos) 4290 { 4291 return seq_cft(seq)->seq_start(seq, ppos); 4292 } 4293 4294 static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos) 4295 { 4296 return seq_cft(seq)->seq_next(seq, v, ppos); 4297 } 4298 4299 static void cgroup_seqfile_stop(struct seq_file *seq, void *v) 4300 { 4301 if (seq_cft(seq)->seq_stop) 4302 seq_cft(seq)->seq_stop(seq, v); 4303 } 4304 4305 static int cgroup_seqfile_show(struct seq_file *m, void *arg) 4306 { 4307 struct cftype *cft = seq_cft(m); 4308 struct cgroup_subsys_state *css = seq_css(m); 4309 4310 if (cft->seq_show) 4311 return cft->seq_show(m, arg); 4312 4313 if (cft->read_u64) 4314 seq_printf(m, "%llu\n", cft->read_u64(css, cft)); 4315 else if (cft->read_s64) 4316 seq_printf(m, "%lld\n", cft->read_s64(css, cft)); 4317 else 4318 return -EINVAL; 4319 return 0; 4320 } 4321 4322 static struct kernfs_ops cgroup_kf_single_ops = { 4323 .atomic_write_len = PAGE_SIZE, 4324 .open = cgroup_file_open, 4325 .release = cgroup_file_release, 4326 .write = cgroup_file_write, 4327 .poll = cgroup_file_poll, 4328 .seq_show = cgroup_seqfile_show, 4329 }; 4330 4331 static struct kernfs_ops cgroup_kf_ops = { 4332 .atomic_write_len = PAGE_SIZE, 4333 .open = cgroup_file_open, 4334 .release = cgroup_file_release, 4335 .write = cgroup_file_write, 4336 .poll = cgroup_file_poll, 4337 .seq_start = cgroup_seqfile_start, 4338 .seq_next = cgroup_seqfile_next, 4339 .seq_stop = cgroup_seqfile_stop, 4340 .seq_show = cgroup_seqfile_show, 4341 }; 4342 4343 static void cgroup_file_notify_timer(struct timer_list *timer) 4344 { 4345 cgroup_file_notify(container_of(timer, struct cgroup_file, 4346 notify_timer)); 4347 } 4348 4349 static int cgroup_add_file(struct cgroup_subsys_state *css, struct cgroup *cgrp, 4350 struct cftype *cft) 4351 { 4352 char name[CGROUP_FILE_NAME_MAX]; 4353 struct kernfs_node *kn; 4354 struct lock_class_key *key = NULL; 4355 4356 #ifdef CONFIG_DEBUG_LOCK_ALLOC 4357 key = &cft->lockdep_key; 4358 #endif 4359 kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name), 4360 cgroup_file_mode(cft), 4361 current_fsuid(), current_fsgid(), 4362 0, cft->kf_ops, cft, 4363 NULL, key); 4364 if (IS_ERR(kn)) 4365 return PTR_ERR(kn); 4366 4367 if (cft->file_offset) { 4368 struct cgroup_file *cfile = (void *)css + cft->file_offset; 4369 4370 timer_setup(&cfile->notify_timer, cgroup_file_notify_timer, 0); 4371 spin_lock_init(&cfile->lock); 4372 cfile->kn = kn; 4373 } 4374 4375 return 0; 4376 } 4377 4378 /** 4379 * cgroup_addrm_files - add or remove files to a cgroup directory 4380 * @css: the target css 4381 * @cgrp: the target cgroup (usually css->cgroup) 4382 * @cfts: array of cftypes to be added 4383 * @is_add: whether to add or remove 4384 * 4385 * Depending on @is_add, add or remove files defined by @cfts on @cgrp. 4386 * For removals, this function never fails. 4387 */ 4388 static int cgroup_addrm_files(struct cgroup_subsys_state *css, 4389 struct cgroup *cgrp, struct cftype cfts[], 4390 bool is_add) 4391 { 4392 struct cftype *cft, *cft_end = NULL; 4393 int ret = 0; 4394 4395 lockdep_assert_held(&cgroup_mutex); 4396 4397 restart: 4398 for (cft = cfts; cft != cft_end && cft->name[0] != '\0'; cft++) { 4399 /* does cft->flags tell us to skip this file on @cgrp? */ 4400 if ((cft->flags & __CFTYPE_ONLY_ON_DFL) && !cgroup_on_dfl(cgrp)) 4401 continue; 4402 if ((cft->flags & __CFTYPE_NOT_ON_DFL) && cgroup_on_dfl(cgrp)) 4403 continue; 4404 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgroup_parent(cgrp)) 4405 continue; 4406 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgroup_parent(cgrp)) 4407 continue; 4408 if ((cft->flags & CFTYPE_DEBUG) && !cgroup_debug) 4409 continue; 4410 if (is_add) { 4411 ret = cgroup_add_file(css, cgrp, cft); 4412 if (ret) { 4413 pr_warn("%s: failed to add %s, err=%d\n", 4414 __func__, cft->name, ret); 4415 cft_end = cft; 4416 is_add = false; 4417 goto restart; 4418 } 4419 } else { 4420 cgroup_rm_file(cgrp, cft); 4421 } 4422 } 4423 return ret; 4424 } 4425 4426 static int cgroup_apply_cftypes(struct cftype *cfts, bool is_add) 4427 { 4428 struct cgroup_subsys *ss = cfts[0].ss; 4429 struct cgroup *root = &ss->root->cgrp; 4430 struct cgroup_subsys_state *css; 4431 int ret = 0; 4432 4433 lockdep_assert_held(&cgroup_mutex); 4434 4435 /* add/rm files for all cgroups created before */ 4436 css_for_each_descendant_pre(css, cgroup_css(root, ss)) { 4437 struct cgroup *cgrp = css->cgroup; 4438 4439 if (!(css->flags & CSS_VISIBLE)) 4440 continue; 4441 4442 ret = cgroup_addrm_files(css, cgrp, cfts, is_add); 4443 if (ret) 4444 break; 4445 } 4446 4447 if (is_add && !ret) 4448 kernfs_activate(root->kn); 4449 return ret; 4450 } 4451 4452 static void cgroup_exit_cftypes(struct cftype *cfts) 4453 { 4454 struct cftype *cft; 4455 4456 for (cft = cfts; cft->name[0] != '\0'; cft++) { 4457 /* free copy for custom atomic_write_len, see init_cftypes() */ 4458 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) 4459 kfree(cft->kf_ops); 4460 cft->kf_ops = NULL; 4461 cft->ss = NULL; 4462 4463 /* revert flags set by cgroup core while adding @cfts */ 4464 cft->flags &= ~(__CFTYPE_ONLY_ON_DFL | __CFTYPE_NOT_ON_DFL | 4465 __CFTYPE_ADDED); 4466 } 4467 } 4468 4469 static int cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) 4470 { 4471 struct cftype *cft; 4472 int ret = 0; 4473 4474 for (cft = cfts; cft->name[0] != '\0'; cft++) { 4475 struct kernfs_ops *kf_ops; 4476 4477 WARN_ON(cft->ss || cft->kf_ops); 4478 4479 if (cft->flags & __CFTYPE_ADDED) { 4480 ret = -EBUSY; 4481 break; 4482 } 4483 4484 if (cft->seq_start) 4485 kf_ops = &cgroup_kf_ops; 4486 else 4487 kf_ops = &cgroup_kf_single_ops; 4488 4489 /* 4490 * Ugh... if @cft wants a custom max_write_len, we need to 4491 * make a copy of kf_ops to set its atomic_write_len. 4492 */ 4493 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) { 4494 kf_ops = kmemdup(kf_ops, sizeof(*kf_ops), GFP_KERNEL); 4495 if (!kf_ops) { 4496 ret = -ENOMEM; 4497 break; 4498 } 4499 kf_ops->atomic_write_len = cft->max_write_len; 4500 } 4501 4502 cft->kf_ops = kf_ops; 4503 cft->ss = ss; 4504 cft->flags |= __CFTYPE_ADDED; 4505 } 4506 4507 if (ret) 4508 cgroup_exit_cftypes(cfts); 4509 return ret; 4510 } 4511 4512 static void cgroup_rm_cftypes_locked(struct cftype *cfts) 4513 { 4514 lockdep_assert_held(&cgroup_mutex); 4515 4516 list_del(&cfts->node); 4517 cgroup_apply_cftypes(cfts, false); 4518 cgroup_exit_cftypes(cfts); 4519 } 4520 4521 /** 4522 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem 4523 * @cfts: zero-length name terminated array of cftypes 4524 * 4525 * Unregister @cfts. Files described by @cfts are removed from all 4526 * existing cgroups and all future cgroups won't have them either. This 4527 * function can be called anytime whether @cfts' subsys is attached or not. 4528 * 4529 * Returns 0 on successful unregistration, -ENOENT if @cfts is not 4530 * registered. 4531 */ 4532 int cgroup_rm_cftypes(struct cftype *cfts) 4533 { 4534 if (!cfts || cfts[0].name[0] == '\0') 4535 return 0; 4536 4537 if (!(cfts[0].flags & __CFTYPE_ADDED)) 4538 return -ENOENT; 4539 4540 cgroup_lock(); 4541 cgroup_rm_cftypes_locked(cfts); 4542 cgroup_unlock(); 4543 return 0; 4544 } 4545 4546 /** 4547 * cgroup_add_cftypes - add an array of cftypes to a subsystem 4548 * @ss: target cgroup subsystem 4549 * @cfts: zero-length name terminated array of cftypes 4550 * 4551 * Register @cfts to @ss. Files described by @cfts are created for all 4552 * existing cgroups to which @ss is attached and all future cgroups will 4553 * have them too. This function can be called anytime whether @ss is 4554 * attached or not. 4555 * 4556 * Returns 0 on successful registration, -errno on failure. Note that this 4557 * function currently returns 0 as long as @cfts registration is successful 4558 * even if some file creation attempts on existing cgroups fail. 4559 */ 4560 int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) 4561 { 4562 int ret; 4563 4564 if (!cgroup_ssid_enabled(ss->id)) 4565 return 0; 4566 4567 if (!cfts || cfts[0].name[0] == '\0') 4568 return 0; 4569 4570 ret = cgroup_init_cftypes(ss, cfts); 4571 if (ret) 4572 return ret; 4573 4574 cgroup_lock(); 4575 4576 list_add_tail(&cfts->node, &ss->cfts); 4577 ret = cgroup_apply_cftypes(cfts, true); 4578 if (ret) 4579 cgroup_rm_cftypes_locked(cfts); 4580 4581 cgroup_unlock(); 4582 return ret; 4583 } 4584 4585 /** 4586 * cgroup_add_dfl_cftypes - add an array of cftypes for default hierarchy 4587 * @ss: target cgroup subsystem 4588 * @cfts: zero-length name terminated array of cftypes 4589 * 4590 * Similar to cgroup_add_cftypes() but the added files are only used for 4591 * the default hierarchy. 4592 */ 4593 int cgroup_add_dfl_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) 4594 { 4595 struct cftype *cft; 4596 4597 for (cft = cfts; cft && cft->name[0] != '\0'; cft++) 4598 cft->flags |= __CFTYPE_ONLY_ON_DFL; 4599 return cgroup_add_cftypes(ss, cfts); 4600 } 4601 4602 /** 4603 * cgroup_add_legacy_cftypes - add an array of cftypes for legacy hierarchies 4604 * @ss: target cgroup subsystem 4605 * @cfts: zero-length name terminated array of cftypes 4606 * 4607 * Similar to cgroup_add_cftypes() but the added files are only used for 4608 * the legacy hierarchies. 4609 */ 4610 int cgroup_add_legacy_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) 4611 { 4612 struct cftype *cft; 4613 4614 for (cft = cfts; cft && cft->name[0] != '\0'; cft++) 4615 cft->flags |= __CFTYPE_NOT_ON_DFL; 4616 return cgroup_add_cftypes(ss, cfts); 4617 } 4618 4619 /** 4620 * cgroup_file_notify - generate a file modified event for a cgroup_file 4621 * @cfile: target cgroup_file 4622 * 4623 * @cfile must have been obtained by setting cftype->file_offset. 4624 */ 4625 void cgroup_file_notify(struct cgroup_file *cfile) 4626 { 4627 unsigned long flags, last, next; 4628 struct kernfs_node *kn = NULL; 4629 4630 if (!READ_ONCE(cfile->kn)) 4631 return; 4632 4633 last = READ_ONCE(cfile->notified_at); 4634 next = last + CGROUP_FILE_NOTIFY_MIN_INTV; 4635 if (time_in_range(jiffies, last, next)) { 4636 timer_reduce(&cfile->notify_timer, next); 4637 if (timer_pending(&cfile->notify_timer)) 4638 return; 4639 } 4640 4641 spin_lock_irqsave(&cfile->lock, flags); 4642 if (cfile->kn) { 4643 kn = cfile->kn; 4644 kernfs_get(kn); 4645 WRITE_ONCE(cfile->notified_at, jiffies); 4646 } 4647 spin_unlock_irqrestore(&cfile->lock, flags); 4648 4649 if (kn) { 4650 kernfs_notify(kn); 4651 kernfs_put(kn); 4652 } 4653 } 4654 EXPORT_SYMBOL_GPL(cgroup_file_notify); 4655 4656 /** 4657 * cgroup_file_show - show or hide a hidden cgroup file 4658 * @cfile: target cgroup_file obtained by setting cftype->file_offset 4659 * @show: whether to show or hide 4660 */ 4661 void cgroup_file_show(struct cgroup_file *cfile, bool show) 4662 { 4663 struct kernfs_node *kn; 4664 4665 spin_lock_irq(&cfile->lock); 4666 kn = cfile->kn; 4667 kernfs_get(kn); 4668 spin_unlock_irq(&cfile->lock); 4669 4670 if (kn) 4671 kernfs_show(kn, show); 4672 4673 kernfs_put(kn); 4674 } 4675 4676 /** 4677 * css_next_child - find the next child of a given css 4678 * @pos: the current position (%NULL to initiate traversal) 4679 * @parent: css whose children to walk 4680 * 4681 * This function returns the next child of @parent and should be called 4682 * under either cgroup_mutex or RCU read lock. The only requirement is 4683 * that @parent and @pos are accessible. The next sibling is guaranteed to 4684 * be returned regardless of their states. 4685 * 4686 * If a subsystem synchronizes ->css_online() and the start of iteration, a 4687 * css which finished ->css_online() is guaranteed to be visible in the 4688 * future iterations and will stay visible until the last reference is put. 4689 * A css which hasn't finished ->css_online() or already finished 4690 * ->css_offline() may show up during traversal. It's each subsystem's 4691 * responsibility to synchronize against on/offlining. 4692 */ 4693 struct cgroup_subsys_state *css_next_child(struct cgroup_subsys_state *pos, 4694 struct cgroup_subsys_state *parent) 4695 { 4696 struct cgroup_subsys_state *next; 4697 4698 cgroup_assert_mutex_or_rcu_locked(); 4699 4700 /* 4701 * @pos could already have been unlinked from the sibling list. 4702 * Once a cgroup is removed, its ->sibling.next is no longer 4703 * updated when its next sibling changes. CSS_RELEASED is set when 4704 * @pos is taken off list, at which time its next pointer is valid, 4705 * and, as releases are serialized, the one pointed to by the next 4706 * pointer is guaranteed to not have started release yet. This 4707 * implies that if we observe !CSS_RELEASED on @pos in this RCU 4708 * critical section, the one pointed to by its next pointer is 4709 * guaranteed to not have finished its RCU grace period even if we 4710 * have dropped rcu_read_lock() in-between iterations. 4711 * 4712 * If @pos has CSS_RELEASED set, its next pointer can't be 4713 * dereferenced; however, as each css is given a monotonically 4714 * increasing unique serial number and always appended to the 4715 * sibling list, the next one can be found by walking the parent's 4716 * children until the first css with higher serial number than 4717 * @pos's. While this path can be slower, it happens iff iteration 4718 * races against release and the race window is very small. 4719 */ 4720 if (!pos) { 4721 next = list_entry_rcu(parent->children.next, struct cgroup_subsys_state, sibling); 4722 } else if (likely(!(pos->flags & CSS_RELEASED))) { 4723 next = list_entry_rcu(pos->sibling.next, struct cgroup_subsys_state, sibling); 4724 } else { 4725 list_for_each_entry_rcu(next, &parent->children, sibling, 4726 lockdep_is_held(&cgroup_mutex)) 4727 if (next->serial_nr > pos->serial_nr) 4728 break; 4729 } 4730 4731 /* 4732 * @next, if not pointing to the head, can be dereferenced and is 4733 * the next sibling. 4734 */ 4735 if (&next->sibling != &parent->children) 4736 return next; 4737 return NULL; 4738 } 4739 4740 /** 4741 * css_next_descendant_pre - find the next descendant for pre-order walk 4742 * @pos: the current position (%NULL to initiate traversal) 4743 * @root: css whose descendants to walk 4744 * 4745 * To be used by css_for_each_descendant_pre(). Find the next descendant 4746 * to visit for pre-order traversal of @root's descendants. @root is 4747 * included in the iteration and the first node to be visited. 4748 * 4749 * While this function requires cgroup_mutex or RCU read locking, it 4750 * doesn't require the whole traversal to be contained in a single critical 4751 * section. Additionally, it isn't necessary to hold onto a reference to @pos. 4752 * This function will return the correct next descendant as long as both @pos 4753 * and @root are accessible and @pos is a descendant of @root. 4754 * 4755 * If a subsystem synchronizes ->css_online() and the start of iteration, a 4756 * css which finished ->css_online() is guaranteed to be visible in the 4757 * future iterations and will stay visible until the last reference is put. 4758 * A css which hasn't finished ->css_online() or already finished 4759 * ->css_offline() may show up during traversal. It's each subsystem's 4760 * responsibility to synchronize against on/offlining. 4761 */ 4762 struct cgroup_subsys_state * 4763 css_next_descendant_pre(struct cgroup_subsys_state *pos, 4764 struct cgroup_subsys_state *root) 4765 { 4766 struct cgroup_subsys_state *next; 4767 4768 cgroup_assert_mutex_or_rcu_locked(); 4769 4770 /* if first iteration, visit @root */ 4771 if (!pos) 4772 return root; 4773 4774 /* visit the first child if exists */ 4775 next = css_next_child(NULL, pos); 4776 if (next) 4777 return next; 4778 4779 /* no child, visit my or the closest ancestor's next sibling */ 4780 while (pos != root) { 4781 next = css_next_child(pos, pos->parent); 4782 if (next) 4783 return next; 4784 pos = pos->parent; 4785 } 4786 4787 return NULL; 4788 } 4789 EXPORT_SYMBOL_GPL(css_next_descendant_pre); 4790 4791 /** 4792 * css_rightmost_descendant - return the rightmost descendant of a css 4793 * @pos: css of interest 4794 * 4795 * Return the rightmost descendant of @pos. If there's no descendant, @pos 4796 * is returned. This can be used during pre-order traversal to skip 4797 * subtree of @pos. 4798 * 4799 * While this function requires cgroup_mutex or RCU read locking, it 4800 * doesn't require the whole traversal to be contained in a single critical 4801 * section. Additionally, it isn't necessary to hold onto a reference to @pos. 4802 * This function will return the correct rightmost descendant as long as @pos 4803 * is accessible. 4804 */ 4805 struct cgroup_subsys_state * 4806 css_rightmost_descendant(struct cgroup_subsys_state *pos) 4807 { 4808 struct cgroup_subsys_state *last, *tmp; 4809 4810 cgroup_assert_mutex_or_rcu_locked(); 4811 4812 do { 4813 last = pos; 4814 /* ->prev isn't RCU safe, walk ->next till the end */ 4815 pos = NULL; 4816 css_for_each_child(tmp, last) 4817 pos = tmp; 4818 } while (pos); 4819 4820 return last; 4821 } 4822 4823 static struct cgroup_subsys_state * 4824 css_leftmost_descendant(struct cgroup_subsys_state *pos) 4825 { 4826 struct cgroup_subsys_state *last; 4827 4828 do { 4829 last = pos; 4830 pos = css_next_child(NULL, pos); 4831 } while (pos); 4832 4833 return last; 4834 } 4835 4836 /** 4837 * css_next_descendant_post - find the next descendant for post-order walk 4838 * @pos: the current position (%NULL to initiate traversal) 4839 * @root: css whose descendants to walk 4840 * 4841 * To be used by css_for_each_descendant_post(). Find the next descendant 4842 * to visit for post-order traversal of @root's descendants. @root is 4843 * included in the iteration and the last node to be visited. 4844 * 4845 * While this function requires cgroup_mutex or RCU read locking, it 4846 * doesn't require the whole traversal to be contained in a single critical 4847 * section. Additionally, it isn't necessary to hold onto a reference to @pos. 4848 * This function will return the correct next descendant as long as both @pos 4849 * and @cgroup are accessible and @pos is a descendant of @cgroup. 4850 * 4851 * If a subsystem synchronizes ->css_online() and the start of iteration, a 4852 * css which finished ->css_online() is guaranteed to be visible in the 4853 * future iterations and will stay visible until the last reference is put. 4854 * A css which hasn't finished ->css_online() or already finished 4855 * ->css_offline() may show up during traversal. It's each subsystem's 4856 * responsibility to synchronize against on/offlining. 4857 */ 4858 struct cgroup_subsys_state * 4859 css_next_descendant_post(struct cgroup_subsys_state *pos, 4860 struct cgroup_subsys_state *root) 4861 { 4862 struct cgroup_subsys_state *next; 4863 4864 cgroup_assert_mutex_or_rcu_locked(); 4865 4866 /* if first iteration, visit leftmost descendant which may be @root */ 4867 if (!pos) 4868 return css_leftmost_descendant(root); 4869 4870 /* if we visited @root, we're done */ 4871 if (pos == root) 4872 return NULL; 4873 4874 /* if there's an unvisited sibling, visit its leftmost descendant */ 4875 next = css_next_child(pos, pos->parent); 4876 if (next) 4877 return css_leftmost_descendant(next); 4878 4879 /* no sibling left, visit parent */ 4880 return pos->parent; 4881 } 4882 4883 /** 4884 * css_has_online_children - does a css have online children 4885 * @css: the target css 4886 * 4887 * Returns %true if @css has any online children; otherwise, %false. This 4888 * function can be called from any context but the caller is responsible 4889 * for synchronizing against on/offlining as necessary. 4890 */ 4891 bool css_has_online_children(struct cgroup_subsys_state *css) 4892 { 4893 struct cgroup_subsys_state *child; 4894 bool ret = false; 4895 4896 rcu_read_lock(); 4897 css_for_each_child(child, css) { 4898 if (css_is_online(child)) { 4899 ret = true; 4900 break; 4901 } 4902 } 4903 rcu_read_unlock(); 4904 return ret; 4905 } 4906 4907 static struct css_set *css_task_iter_next_css_set(struct css_task_iter *it) 4908 { 4909 struct list_head *l; 4910 struct cgrp_cset_link *link; 4911 struct css_set *cset; 4912 4913 lockdep_assert_held(&css_set_lock); 4914 4915 /* find the next threaded cset */ 4916 if (it->tcset_pos) { 4917 l = it->tcset_pos->next; 4918 4919 if (l != it->tcset_head) { 4920 it->tcset_pos = l; 4921 return container_of(l, struct css_set, 4922 threaded_csets_node); 4923 } 4924 4925 it->tcset_pos = NULL; 4926 } 4927 4928 /* find the next cset */ 4929 l = it->cset_pos; 4930 l = l->next; 4931 if (l == it->cset_head) { 4932 it->cset_pos = NULL; 4933 return NULL; 4934 } 4935 4936 if (it->ss) { 4937 cset = container_of(l, struct css_set, e_cset_node[it->ss->id]); 4938 } else { 4939 link = list_entry(l, struct cgrp_cset_link, cset_link); 4940 cset = link->cset; 4941 } 4942 4943 it->cset_pos = l; 4944 4945 /* initialize threaded css_set walking */ 4946 if (it->flags & CSS_TASK_ITER_THREADED) { 4947 if (it->cur_dcset) 4948 put_css_set_locked(it->cur_dcset); 4949 it->cur_dcset = cset; 4950 get_css_set(cset); 4951 4952 it->tcset_head = &cset->threaded_csets; 4953 it->tcset_pos = &cset->threaded_csets; 4954 } 4955 4956 return cset; 4957 } 4958 4959 /** 4960 * css_task_iter_advance_css_set - advance a task iterator to the next css_set 4961 * @it: the iterator to advance 4962 * 4963 * Advance @it to the next css_set to walk. 4964 */ 4965 static void css_task_iter_advance_css_set(struct css_task_iter *it) 4966 { 4967 struct css_set *cset; 4968 4969 lockdep_assert_held(&css_set_lock); 4970 4971 /* Advance to the next non-empty css_set and find first non-empty tasks list*/ 4972 while ((cset = css_task_iter_next_css_set(it))) { 4973 if (!list_empty(&cset->tasks)) { 4974 it->cur_tasks_head = &cset->tasks; 4975 break; 4976 } else if (!list_empty(&cset->mg_tasks)) { 4977 it->cur_tasks_head = &cset->mg_tasks; 4978 break; 4979 } else if (!list_empty(&cset->dying_tasks)) { 4980 it->cur_tasks_head = &cset->dying_tasks; 4981 break; 4982 } 4983 } 4984 if (!cset) { 4985 it->task_pos = NULL; 4986 return; 4987 } 4988 it->task_pos = it->cur_tasks_head->next; 4989 4990 /* 4991 * We don't keep css_sets locked across iteration steps and thus 4992 * need to take steps to ensure that iteration can be resumed after 4993 * the lock is re-acquired. Iteration is performed at two levels - 4994 * css_sets and tasks in them. 4995 * 4996 * Once created, a css_set never leaves its cgroup lists, so a 4997 * pinned css_set is guaranteed to stay put and we can resume 4998 * iteration afterwards. 4999 * 5000 * Tasks may leave @cset across iteration steps. This is resolved 5001 * by registering each iterator with the css_set currently being 5002 * walked and making css_set_move_task() advance iterators whose 5003 * next task is leaving. 5004 */ 5005 if (it->cur_cset) { 5006 list_del(&it->iters_node); 5007 put_css_set_locked(it->cur_cset); 5008 } 5009 get_css_set(cset); 5010 it->cur_cset = cset; 5011 list_add(&it->iters_node, &cset->task_iters); 5012 } 5013 5014 static void css_task_iter_skip(struct css_task_iter *it, 5015 struct task_struct *task) 5016 { 5017 lockdep_assert_held(&css_set_lock); 5018 5019 if (it->task_pos == &task->cg_list) { 5020 it->task_pos = it->task_pos->next; 5021 it->flags |= CSS_TASK_ITER_SKIPPED; 5022 } 5023 } 5024 5025 static void css_task_iter_advance(struct css_task_iter *it) 5026 { 5027 struct task_struct *task; 5028 5029 lockdep_assert_held(&css_set_lock); 5030 repeat: 5031 if (it->task_pos) { 5032 /* 5033 * Advance iterator to find next entry. We go through cset 5034 * tasks, mg_tasks and dying_tasks, when consumed we move onto 5035 * the next cset. 5036 */ 5037 if (it->flags & CSS_TASK_ITER_SKIPPED) 5038 it->flags &= ~CSS_TASK_ITER_SKIPPED; 5039 else 5040 it->task_pos = it->task_pos->next; 5041 5042 if (it->task_pos == &it->cur_cset->tasks) { 5043 it->cur_tasks_head = &it->cur_cset->mg_tasks; 5044 it->task_pos = it->cur_tasks_head->next; 5045 } 5046 if (it->task_pos == &it->cur_cset->mg_tasks) { 5047 it->cur_tasks_head = &it->cur_cset->dying_tasks; 5048 it->task_pos = it->cur_tasks_head->next; 5049 } 5050 if (it->task_pos == &it->cur_cset->dying_tasks) 5051 css_task_iter_advance_css_set(it); 5052 } else { 5053 /* called from start, proceed to the first cset */ 5054 css_task_iter_advance_css_set(it); 5055 } 5056 5057 if (!it->task_pos) 5058 return; 5059 5060 task = list_entry(it->task_pos, struct task_struct, cg_list); 5061 /* 5062 * Hide tasks that are exiting but not yet removed. Keep zombie 5063 * leaders with live threads visible. 5064 */ 5065 if ((task->flags & PF_EXITING) && !atomic_read(&task->signal->live)) 5066 goto repeat; 5067 5068 if (it->flags & CSS_TASK_ITER_PROCS) { 5069 /* if PROCS, skip over tasks which aren't group leaders */ 5070 if (!thread_group_leader(task)) 5071 goto repeat; 5072 5073 /* and dying leaders w/o live member threads */ 5074 if (it->cur_tasks_head == &it->cur_cset->dying_tasks && 5075 !atomic_read(&task->signal->live)) 5076 goto repeat; 5077 } else { 5078 /* skip all dying ones */ 5079 if (it->cur_tasks_head == &it->cur_cset->dying_tasks) 5080 goto repeat; 5081 } 5082 } 5083 5084 /** 5085 * css_task_iter_start - initiate task iteration 5086 * @css: the css to walk tasks of 5087 * @flags: CSS_TASK_ITER_* flags 5088 * @it: the task iterator to use 5089 * 5090 * Initiate iteration through the tasks of @css. The caller can call 5091 * css_task_iter_next() to walk through the tasks until the function 5092 * returns NULL. On completion of iteration, css_task_iter_end() must be 5093 * called. 5094 */ 5095 void css_task_iter_start(struct cgroup_subsys_state *css, unsigned int flags, 5096 struct css_task_iter *it) 5097 { 5098 unsigned long irqflags; 5099 5100 memset(it, 0, sizeof(*it)); 5101 5102 spin_lock_irqsave(&css_set_lock, irqflags); 5103 5104 it->ss = css->ss; 5105 it->flags = flags; 5106 5107 if (CGROUP_HAS_SUBSYS_CONFIG && it->ss) 5108 it->cset_pos = &css->cgroup->e_csets[css->ss->id]; 5109 else 5110 it->cset_pos = &css->cgroup->cset_links; 5111 5112 it->cset_head = it->cset_pos; 5113 5114 css_task_iter_advance(it); 5115 5116 spin_unlock_irqrestore(&css_set_lock, irqflags); 5117 } 5118 5119 /** 5120 * css_task_iter_next - return the next task for the iterator 5121 * @it: the task iterator being iterated 5122 * 5123 * The "next" function for task iteration. @it should have been 5124 * initialized via css_task_iter_start(). Returns NULL when the iteration 5125 * reaches the end. 5126 */ 5127 struct task_struct *css_task_iter_next(struct css_task_iter *it) 5128 { 5129 unsigned long irqflags; 5130 5131 if (it->cur_task) { 5132 put_task_struct(it->cur_task); 5133 it->cur_task = NULL; 5134 } 5135 5136 spin_lock_irqsave(&css_set_lock, irqflags); 5137 5138 /* @it may be half-advanced by skips, finish advancing */ 5139 if (it->flags & CSS_TASK_ITER_SKIPPED) 5140 css_task_iter_advance(it); 5141 5142 if (it->task_pos) { 5143 it->cur_task = list_entry(it->task_pos, struct task_struct, 5144 cg_list); 5145 get_task_struct(it->cur_task); 5146 css_task_iter_advance(it); 5147 } 5148 5149 spin_unlock_irqrestore(&css_set_lock, irqflags); 5150 5151 return it->cur_task; 5152 } 5153 5154 /** 5155 * css_task_iter_end - finish task iteration 5156 * @it: the task iterator to finish 5157 * 5158 * Finish task iteration started by css_task_iter_start(). 5159 */ 5160 void css_task_iter_end(struct css_task_iter *it) 5161 { 5162 unsigned long irqflags; 5163 5164 if (it->cur_cset) { 5165 spin_lock_irqsave(&css_set_lock, irqflags); 5166 list_del(&it->iters_node); 5167 put_css_set_locked(it->cur_cset); 5168 spin_unlock_irqrestore(&css_set_lock, irqflags); 5169 } 5170 5171 if (it->cur_dcset) 5172 put_css_set(it->cur_dcset); 5173 5174 if (it->cur_task) 5175 put_task_struct(it->cur_task); 5176 } 5177 5178 static void cgroup_procs_release(struct kernfs_open_file *of) 5179 { 5180 struct cgroup_file_ctx *ctx = of->priv; 5181 5182 if (ctx->procs.started) 5183 css_task_iter_end(&ctx->procs.iter); 5184 } 5185 5186 static void *cgroup_procs_next(struct seq_file *s, void *v, loff_t *pos) 5187 { 5188 struct kernfs_open_file *of = s->private; 5189 struct cgroup_file_ctx *ctx = of->priv; 5190 5191 if (pos) 5192 (*pos)++; 5193 5194 return css_task_iter_next(&ctx->procs.iter); 5195 } 5196 5197 static void *__cgroup_procs_start(struct seq_file *s, loff_t *pos, 5198 unsigned int iter_flags) 5199 { 5200 struct kernfs_open_file *of = s->private; 5201 struct cgroup *cgrp = seq_css(s)->cgroup; 5202 struct cgroup_file_ctx *ctx = of->priv; 5203 struct css_task_iter *it = &ctx->procs.iter; 5204 5205 /* 5206 * When a seq_file is seeked, it's always traversed sequentially 5207 * from position 0, so we can simply keep iterating on !0 *pos. 5208 */ 5209 if (!ctx->procs.started) { 5210 if (WARN_ON_ONCE((*pos))) 5211 return ERR_PTR(-EINVAL); 5212 css_task_iter_start(&cgrp->self, iter_flags, it); 5213 ctx->procs.started = true; 5214 } else if (!(*pos)) { 5215 css_task_iter_end(it); 5216 css_task_iter_start(&cgrp->self, iter_flags, it); 5217 } else 5218 return it->cur_task; 5219 5220 return cgroup_procs_next(s, NULL, NULL); 5221 } 5222 5223 static void *cgroup_procs_start(struct seq_file *s, loff_t *pos) 5224 { 5225 struct cgroup *cgrp = seq_css(s)->cgroup; 5226 5227 /* 5228 * All processes of a threaded subtree belong to the domain cgroup 5229 * of the subtree. Only threads can be distributed across the 5230 * subtree. Reject reads on cgroup.procs in the subtree proper. 5231 * They're always empty anyway. 5232 */ 5233 if (cgroup_is_threaded(cgrp)) 5234 return ERR_PTR(-EOPNOTSUPP); 5235 5236 return __cgroup_procs_start(s, pos, CSS_TASK_ITER_PROCS | 5237 CSS_TASK_ITER_THREADED); 5238 } 5239 5240 static int cgroup_procs_show(struct seq_file *s, void *v) 5241 { 5242 seq_printf(s, "%d\n", task_pid_vnr(v)); 5243 return 0; 5244 } 5245 5246 static int cgroup_may_write(const struct cgroup *cgrp, struct super_block *sb) 5247 { 5248 int ret; 5249 struct inode *inode; 5250 5251 lockdep_assert_held(&cgroup_mutex); 5252 5253 inode = kernfs_get_inode(sb, cgrp->procs_file.kn); 5254 if (!inode) 5255 return -ENOMEM; 5256 5257 ret = inode_permission(&nop_mnt_idmap, inode, MAY_WRITE); 5258 iput(inode); 5259 return ret; 5260 } 5261 5262 static int cgroup_procs_write_permission(struct cgroup *src_cgrp, 5263 struct cgroup *dst_cgrp, 5264 struct super_block *sb, 5265 struct cgroup_namespace *ns) 5266 { 5267 struct cgroup *com_cgrp = src_cgrp; 5268 int ret; 5269 5270 lockdep_assert_held(&cgroup_mutex); 5271 5272 /* find the common ancestor */ 5273 while (!cgroup_is_descendant(dst_cgrp, com_cgrp)) 5274 com_cgrp = cgroup_parent(com_cgrp); 5275 5276 /* %current should be authorized to migrate to the common ancestor */ 5277 ret = cgroup_may_write(com_cgrp, sb); 5278 if (ret) 5279 return ret; 5280 5281 /* 5282 * If namespaces are delegation boundaries, %current must be able 5283 * to see both source and destination cgroups from its namespace. 5284 */ 5285 if ((cgrp_dfl_root.flags & CGRP_ROOT_NS_DELEGATE) && 5286 (!cgroup_is_descendant(src_cgrp, ns->root_cset->dfl_cgrp) || 5287 !cgroup_is_descendant(dst_cgrp, ns->root_cset->dfl_cgrp))) 5288 return -ENOENT; 5289 5290 return 0; 5291 } 5292 5293 static int cgroup_attach_permissions(struct cgroup *src_cgrp, 5294 struct cgroup *dst_cgrp, 5295 struct super_block *sb, bool threadgroup, 5296 struct cgroup_namespace *ns) 5297 { 5298 int ret = 0; 5299 5300 ret = cgroup_procs_write_permission(src_cgrp, dst_cgrp, sb, ns); 5301 if (ret) 5302 return ret; 5303 5304 ret = cgroup_migrate_vet_dst(dst_cgrp); 5305 if (ret) 5306 return ret; 5307 5308 if (!threadgroup && (src_cgrp->dom_cgrp != dst_cgrp->dom_cgrp)) 5309 ret = -EOPNOTSUPP; 5310 5311 return ret; 5312 } 5313 5314 static ssize_t __cgroup_procs_write(struct kernfs_open_file *of, char *buf, 5315 bool threadgroup) 5316 { 5317 struct cgroup_file_ctx *ctx = of->priv; 5318 struct cgroup *src_cgrp, *dst_cgrp; 5319 struct task_struct *task; 5320 ssize_t ret; 5321 enum cgroup_attach_lock_mode lock_mode; 5322 5323 dst_cgrp = cgroup_kn_lock_live(of->kn, false); 5324 if (!dst_cgrp) 5325 return -ENODEV; 5326 5327 task = cgroup_procs_write_start(buf, threadgroup, &lock_mode); 5328 ret = PTR_ERR_OR_ZERO(task); 5329 if (ret) 5330 goto out_unlock; 5331 5332 /* find the source cgroup */ 5333 spin_lock_irq(&css_set_lock); 5334 src_cgrp = task_cgroup_from_root(task, &cgrp_dfl_root); 5335 spin_unlock_irq(&css_set_lock); 5336 5337 /* 5338 * Process and thread migrations follow same delegation rule. Check 5339 * permissions using the credentials from file open to protect against 5340 * inherited fd attacks. 5341 */ 5342 scoped_with_creds(of->file->f_cred) 5343 ret = cgroup_attach_permissions(src_cgrp, dst_cgrp, 5344 of->file->f_path.dentry->d_sb, 5345 threadgroup, ctx->ns); 5346 if (ret) 5347 goto out_finish; 5348 5349 ret = cgroup_attach_task(dst_cgrp, task, threadgroup); 5350 5351 out_finish: 5352 cgroup_procs_write_finish(task, lock_mode); 5353 out_unlock: 5354 cgroup_kn_unlock(of->kn); 5355 5356 return ret; 5357 } 5358 5359 static ssize_t cgroup_procs_write(struct kernfs_open_file *of, 5360 char *buf, size_t nbytes, loff_t off) 5361 { 5362 return __cgroup_procs_write(of, buf, true) ?: nbytes; 5363 } 5364 5365 static void *cgroup_threads_start(struct seq_file *s, loff_t *pos) 5366 { 5367 return __cgroup_procs_start(s, pos, 0); 5368 } 5369 5370 static ssize_t cgroup_threads_write(struct kernfs_open_file *of, 5371 char *buf, size_t nbytes, loff_t off) 5372 { 5373 return __cgroup_procs_write(of, buf, false) ?: nbytes; 5374 } 5375 5376 /* cgroup core interface files for the default hierarchy */ 5377 static struct cftype cgroup_base_files[] = { 5378 { 5379 .name = "cgroup.type", 5380 .flags = CFTYPE_NOT_ON_ROOT, 5381 .seq_show = cgroup_type_show, 5382 .write = cgroup_type_write, 5383 }, 5384 { 5385 .name = "cgroup.procs", 5386 .flags = CFTYPE_NS_DELEGATABLE, 5387 .file_offset = offsetof(struct cgroup, procs_file), 5388 .release = cgroup_procs_release, 5389 .seq_start = cgroup_procs_start, 5390 .seq_next = cgroup_procs_next, 5391 .seq_show = cgroup_procs_show, 5392 .write = cgroup_procs_write, 5393 }, 5394 { 5395 .name = "cgroup.threads", 5396 .flags = CFTYPE_NS_DELEGATABLE, 5397 .release = cgroup_procs_release, 5398 .seq_start = cgroup_threads_start, 5399 .seq_next = cgroup_procs_next, 5400 .seq_show = cgroup_procs_show, 5401 .write = cgroup_threads_write, 5402 }, 5403 { 5404 .name = "cgroup.controllers", 5405 .seq_show = cgroup_controllers_show, 5406 }, 5407 { 5408 .name = "cgroup.subtree_control", 5409 .flags = CFTYPE_NS_DELEGATABLE, 5410 .seq_show = cgroup_subtree_control_show, 5411 .write = cgroup_subtree_control_write, 5412 }, 5413 { 5414 .name = "cgroup.events", 5415 .flags = CFTYPE_NOT_ON_ROOT, 5416 .file_offset = offsetof(struct cgroup, events_file), 5417 .seq_show = cgroup_events_show, 5418 }, 5419 { 5420 .name = "cgroup.max.descendants", 5421 .seq_show = cgroup_max_descendants_show, 5422 .write = cgroup_max_descendants_write, 5423 }, 5424 { 5425 .name = "cgroup.max.depth", 5426 .seq_show = cgroup_max_depth_show, 5427 .write = cgroup_max_depth_write, 5428 }, 5429 { 5430 .name = "cgroup.stat", 5431 .seq_show = cgroup_stat_show, 5432 }, 5433 { 5434 .name = "cgroup.stat.local", 5435 .flags = CFTYPE_NOT_ON_ROOT, 5436 .seq_show = cgroup_core_local_stat_show, 5437 }, 5438 { 5439 .name = "cgroup.freeze", 5440 .flags = CFTYPE_NOT_ON_ROOT, 5441 .seq_show = cgroup_freeze_show, 5442 .write = cgroup_freeze_write, 5443 }, 5444 { 5445 .name = "cgroup.kill", 5446 .flags = CFTYPE_NOT_ON_ROOT, 5447 .write = cgroup_kill_write, 5448 }, 5449 { 5450 .name = "cpu.stat", 5451 .seq_show = cpu_stat_show, 5452 }, 5453 { 5454 .name = "cpu.stat.local", 5455 .seq_show = cpu_local_stat_show, 5456 }, 5457 { } /* terminate */ 5458 }; 5459 5460 static struct cftype cgroup_psi_files[] = { 5461 #ifdef CONFIG_PSI 5462 { 5463 .name = "io.pressure", 5464 .file_offset = offsetof(struct cgroup, psi_files[PSI_IO]), 5465 .seq_show = cgroup_io_pressure_show, 5466 .write = cgroup_io_pressure_write, 5467 .poll = cgroup_pressure_poll, 5468 .release = cgroup_pressure_release, 5469 }, 5470 { 5471 .name = "memory.pressure", 5472 .file_offset = offsetof(struct cgroup, psi_files[PSI_MEM]), 5473 .seq_show = cgroup_memory_pressure_show, 5474 .write = cgroup_memory_pressure_write, 5475 .poll = cgroup_pressure_poll, 5476 .release = cgroup_pressure_release, 5477 }, 5478 { 5479 .name = "cpu.pressure", 5480 .file_offset = offsetof(struct cgroup, psi_files[PSI_CPU]), 5481 .seq_show = cgroup_cpu_pressure_show, 5482 .write = cgroup_cpu_pressure_write, 5483 .poll = cgroup_pressure_poll, 5484 .release = cgroup_pressure_release, 5485 }, 5486 #ifdef CONFIG_IRQ_TIME_ACCOUNTING 5487 { 5488 .name = "irq.pressure", 5489 .file_offset = offsetof(struct cgroup, psi_files[PSI_IRQ]), 5490 .seq_show = cgroup_irq_pressure_show, 5491 .write = cgroup_irq_pressure_write, 5492 .poll = cgroup_pressure_poll, 5493 .release = cgroup_pressure_release, 5494 }, 5495 #endif 5496 { 5497 .name = "cgroup.pressure", 5498 .seq_show = cgroup_pressure_show, 5499 .write = cgroup_pressure_write, 5500 }, 5501 #endif /* CONFIG_PSI */ 5502 { } /* terminate */ 5503 }; 5504 5505 /* 5506 * css destruction is four-stage process. 5507 * 5508 * 1. Destruction starts. Killing of the percpu_ref is initiated. 5509 * Implemented in kill_css(). 5510 * 5511 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs 5512 * and thus css_tryget_online() is guaranteed to fail, the css can be 5513 * offlined by invoking offline_css(). After offlining, the base ref is 5514 * put. Implemented in css_killed_work_fn(). 5515 * 5516 * 3. When the percpu_ref reaches zero, the only possible remaining 5517 * accessors are inside RCU read sections. css_release() schedules the 5518 * RCU callback. 5519 * 5520 * 4. After the grace period, the css can be freed. Implemented in 5521 * css_free_rwork_fn(). 5522 * 5523 * It is actually hairier because both step 2 and 4 require process context 5524 * and thus involve punting to css->destroy_work adding two additional 5525 * steps to the already complex sequence. 5526 */ 5527 static void css_free_rwork_fn(struct work_struct *work) 5528 { 5529 struct cgroup_subsys_state *css = container_of(to_rcu_work(work), 5530 struct cgroup_subsys_state, destroy_rwork); 5531 struct cgroup_subsys *ss = css->ss; 5532 struct cgroup *cgrp = css->cgroup; 5533 5534 percpu_ref_exit(&css->refcnt); 5535 css_rstat_exit(css); 5536 5537 if (!css_is_self(css)) { 5538 /* css free path */ 5539 struct cgroup_subsys_state *parent = css->parent; 5540 int id = css->id; 5541 5542 ss->css_free(css); 5543 cgroup_idr_remove(&ss->css_idr, id); 5544 cgroup_put(cgrp); 5545 5546 if (parent) 5547 css_put(parent); 5548 } else { 5549 /* cgroup free path */ 5550 atomic_dec(&cgrp->root->nr_cgrps); 5551 if (!cgroup_on_dfl(cgrp)) 5552 cgroup1_pidlist_destroy_all(cgrp); 5553 cancel_work_sync(&cgrp->release_agent_work); 5554 bpf_cgrp_storage_free(cgrp); 5555 5556 if (cgroup_parent(cgrp)) { 5557 /* 5558 * We get a ref to the parent, and put the ref when 5559 * this cgroup is being freed, so it's guaranteed 5560 * that the parent won't be destroyed before its 5561 * children. 5562 */ 5563 cgroup_put(cgroup_parent(cgrp)); 5564 kernfs_put(cgrp->kn); 5565 psi_cgroup_free(cgrp); 5566 kfree(cgrp); 5567 } else { 5568 /* 5569 * This is root cgroup's refcnt reaching zero, 5570 * which indicates that the root should be 5571 * released. 5572 */ 5573 cgroup_destroy_root(cgrp->root); 5574 } 5575 } 5576 } 5577 5578 static void css_release_work_fn(struct work_struct *work) 5579 { 5580 struct cgroup_subsys_state *css = 5581 container_of(work, struct cgroup_subsys_state, destroy_work); 5582 struct cgroup_subsys *ss = css->ss; 5583 struct cgroup *cgrp = css->cgroup; 5584 5585 cgroup_lock(); 5586 5587 css->flags |= CSS_RELEASED; 5588 list_del_rcu(&css->sibling); 5589 5590 if (!css_is_self(css)) { 5591 struct cgroup *parent_cgrp; 5592 5593 css_rstat_flush(css); 5594 5595 cgroup_idr_replace(&ss->css_idr, NULL, css->id); 5596 if (ss->css_released) 5597 ss->css_released(css); 5598 5599 cgrp->nr_dying_subsys[ss->id]--; 5600 /* 5601 * When a css is released and ready to be freed, its 5602 * nr_descendants must be zero. However, the corresponding 5603 * cgrp->nr_dying_subsys[ss->id] may not be 0 if a subsystem 5604 * is activated and deactivated multiple times with one or 5605 * more of its previous activation leaving behind dying csses. 5606 */ 5607 WARN_ON_ONCE(css->nr_descendants); 5608 parent_cgrp = cgroup_parent(cgrp); 5609 while (parent_cgrp) { 5610 parent_cgrp->nr_dying_subsys[ss->id]--; 5611 parent_cgrp = cgroup_parent(parent_cgrp); 5612 } 5613 } else { 5614 struct cgroup *tcgrp; 5615 5616 /* cgroup release path */ 5617 TRACE_CGROUP_PATH(release, cgrp); 5618 5619 css_rstat_flush(&cgrp->self); 5620 5621 spin_lock_irq(&css_set_lock); 5622 for (tcgrp = cgroup_parent(cgrp); tcgrp; 5623 tcgrp = cgroup_parent(tcgrp)) 5624 tcgrp->nr_dying_descendants--; 5625 spin_unlock_irq(&css_set_lock); 5626 5627 /* 5628 * There are two control paths which try to determine 5629 * cgroup from dentry without going through kernfs - 5630 * cgroupstats_build() and css_tryget_online_from_dir(). 5631 * Those are supported by RCU protecting clearing of 5632 * cgrp->kn->priv backpointer. 5633 */ 5634 if (cgrp->kn) 5635 RCU_INIT_POINTER(*(void __rcu __force **)&cgrp->kn->priv, 5636 NULL); 5637 } 5638 5639 cgroup_unlock(); 5640 5641 INIT_RCU_WORK(&css->destroy_rwork, css_free_rwork_fn); 5642 queue_rcu_work(cgroup_free_wq, &css->destroy_rwork); 5643 } 5644 5645 static void css_release(struct percpu_ref *ref) 5646 { 5647 struct cgroup_subsys_state *css = 5648 container_of(ref, struct cgroup_subsys_state, refcnt); 5649 5650 INIT_WORK(&css->destroy_work, css_release_work_fn); 5651 queue_work(cgroup_release_wq, &css->destroy_work); 5652 } 5653 5654 static void init_and_link_css(struct cgroup_subsys_state *css, 5655 struct cgroup_subsys *ss, struct cgroup *cgrp) 5656 { 5657 lockdep_assert_held(&cgroup_mutex); 5658 5659 cgroup_get_live(cgrp); 5660 5661 memset(css, 0, sizeof(*css)); 5662 css->cgroup = cgrp; 5663 css->ss = ss; 5664 css->id = -1; 5665 INIT_LIST_HEAD(&css->sibling); 5666 INIT_LIST_HEAD(&css->children); 5667 css->serial_nr = css_serial_nr_next++; 5668 atomic_set(&css->online_cnt, 0); 5669 5670 if (cgroup_parent(cgrp)) { 5671 css->parent = cgroup_css(cgroup_parent(cgrp), ss); 5672 css_get(css->parent); 5673 } 5674 5675 BUG_ON(cgroup_css(cgrp, ss)); 5676 } 5677 5678 /* invoke ->css_online() on a new CSS and mark it online if successful */ 5679 static int online_css(struct cgroup_subsys_state *css) 5680 { 5681 struct cgroup_subsys *ss = css->ss; 5682 int ret = 0; 5683 5684 lockdep_assert_held(&cgroup_mutex); 5685 5686 if (ss->css_online) 5687 ret = ss->css_online(css); 5688 if (!ret) { 5689 css->flags |= CSS_ONLINE; 5690 rcu_assign_pointer(css->cgroup->subsys[ss->id], css); 5691 5692 atomic_inc(&css->online_cnt); 5693 if (css->parent) { 5694 atomic_inc(&css->parent->online_cnt); 5695 while ((css = css->parent)) 5696 css->nr_descendants++; 5697 } 5698 } 5699 return ret; 5700 } 5701 5702 /* if the CSS is online, invoke ->css_offline() on it and mark it offline */ 5703 static void offline_css(struct cgroup_subsys_state *css) 5704 { 5705 struct cgroup_subsys *ss = css->ss; 5706 5707 lockdep_assert_held(&cgroup_mutex); 5708 5709 if (!css_is_online(css)) 5710 return; 5711 5712 if (ss->css_offline) 5713 ss->css_offline(css); 5714 5715 css->flags &= ~CSS_ONLINE; 5716 RCU_INIT_POINTER(css->cgroup->subsys[ss->id], NULL); 5717 5718 wake_up_all(&css->cgroup->offline_waitq); 5719 5720 css->cgroup->nr_dying_subsys[ss->id]++; 5721 /* 5722 * Parent css and cgroup cannot be freed until after the freeing 5723 * of child css, see css_free_rwork_fn(). 5724 */ 5725 while ((css = css->parent)) { 5726 css->nr_descendants--; 5727 css->cgroup->nr_dying_subsys[ss->id]++; 5728 } 5729 } 5730 5731 /** 5732 * css_create - create a cgroup_subsys_state 5733 * @cgrp: the cgroup new css will be associated with 5734 * @ss: the subsys of new css 5735 * 5736 * Create a new css associated with @cgrp - @ss pair. On success, the new 5737 * css is online and installed in @cgrp. This function doesn't create the 5738 * interface files. Returns 0 on success, -errno on failure. 5739 */ 5740 static struct cgroup_subsys_state *css_create(struct cgroup *cgrp, 5741 struct cgroup_subsys *ss) 5742 { 5743 struct cgroup *parent = cgroup_parent(cgrp); 5744 struct cgroup_subsys_state *parent_css = cgroup_css(parent, ss); 5745 struct cgroup_subsys_state *css; 5746 int err; 5747 5748 lockdep_assert_held(&cgroup_mutex); 5749 5750 css = ss->css_alloc(parent_css); 5751 if (!css) 5752 css = ERR_PTR(-ENOMEM); 5753 if (IS_ERR(css)) 5754 return css; 5755 5756 init_and_link_css(css, ss, cgrp); 5757 5758 err = percpu_ref_init(&css->refcnt, css_release, 0, GFP_KERNEL); 5759 if (err) 5760 goto err_free_css; 5761 5762 err = cgroup_idr_alloc(&ss->css_idr, NULL, 2, 0, GFP_KERNEL); 5763 if (err < 0) 5764 goto err_free_css; 5765 css->id = err; 5766 5767 err = css_rstat_init(css); 5768 if (err) 5769 goto err_free_css; 5770 5771 /* @css is ready to be brought online now, make it visible */ 5772 list_add_tail_rcu(&css->sibling, &parent_css->children); 5773 cgroup_idr_replace(&ss->css_idr, css, css->id); 5774 5775 err = online_css(css); 5776 if (err) 5777 goto err_list_del; 5778 5779 return css; 5780 5781 err_list_del: 5782 list_del_rcu(&css->sibling); 5783 err_free_css: 5784 INIT_RCU_WORK(&css->destroy_rwork, css_free_rwork_fn); 5785 queue_rcu_work(cgroup_free_wq, &css->destroy_rwork); 5786 return ERR_PTR(err); 5787 } 5788 5789 /* 5790 * The returned cgroup is fully initialized including its control mask, but 5791 * it doesn't have the control mask applied. 5792 */ 5793 static struct cgroup *cgroup_create(struct cgroup *parent, const char *name, 5794 umode_t mode) 5795 { 5796 struct cgroup_root *root = parent->root; 5797 struct cgroup *cgrp, *tcgrp; 5798 struct kernfs_node *kn; 5799 int i, level = parent->level + 1; 5800 int ret; 5801 5802 /* allocate the cgroup and its ID, 0 is reserved for the root */ 5803 cgrp = kzalloc_flex(*cgrp, _low_ancestors, level); 5804 if (!cgrp) 5805 return ERR_PTR(-ENOMEM); 5806 5807 ret = percpu_ref_init(&cgrp->self.refcnt, css_release, 0, GFP_KERNEL); 5808 if (ret) 5809 goto out_free_cgrp; 5810 5811 /* create the directory */ 5812 kn = kernfs_create_dir_ns(parent->kn, name, mode, 5813 current_fsuid(), current_fsgid(), 5814 cgrp, NULL); 5815 if (IS_ERR(kn)) { 5816 ret = PTR_ERR(kn); 5817 goto out_cancel_ref; 5818 } 5819 cgrp->kn = kn; 5820 5821 init_cgroup_housekeeping(cgrp); 5822 5823 cgrp->self.parent = &parent->self; 5824 cgrp->root = root; 5825 cgrp->level = level; 5826 5827 /* 5828 * Now that init_cgroup_housekeeping() has been called and cgrp->self 5829 * is setup, it is safe to perform rstat initialization on it. 5830 */ 5831 ret = css_rstat_init(&cgrp->self); 5832 if (ret) 5833 goto out_kernfs_remove; 5834 5835 ret = psi_cgroup_alloc(cgrp); 5836 if (ret) 5837 goto out_stat_exit; 5838 5839 for (tcgrp = cgrp; tcgrp; tcgrp = cgroup_parent(tcgrp)) 5840 cgrp->ancestors[tcgrp->level] = tcgrp; 5841 5842 /* 5843 * New cgroup inherits effective freeze counter, and 5844 * if the parent has to be frozen, the child has too. 5845 */ 5846 cgrp->freezer.e_freeze = parent->freezer.e_freeze; 5847 seqcount_spinlock_init(&cgrp->freezer.freeze_seq, &css_set_lock); 5848 if (cgrp->freezer.e_freeze) { 5849 /* 5850 * Set the CGRP_FREEZE flag, so when a process will be 5851 * attached to the child cgroup, it will become frozen. 5852 * At this point the new cgroup is unpopulated, so we can 5853 * consider it frozen immediately. 5854 */ 5855 set_bit(CGRP_FREEZE, &cgrp->flags); 5856 cgrp->freezer.freeze_start_nsec = ktime_get_ns(); 5857 set_bit(CGRP_FROZEN, &cgrp->flags); 5858 } 5859 5860 if (notify_on_release(parent)) 5861 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); 5862 5863 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags)) 5864 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags); 5865 5866 cgrp->self.serial_nr = css_serial_nr_next++; 5867 5868 ret = blocking_notifier_call_chain_robust(&cgroup_lifetime_notifier, 5869 CGROUP_LIFETIME_ONLINE, 5870 CGROUP_LIFETIME_OFFLINE, cgrp); 5871 ret = notifier_to_errno(ret); 5872 if (ret) 5873 goto out_psi_free; 5874 5875 /* allocation complete, commit to creation */ 5876 spin_lock_irq(&css_set_lock); 5877 for (i = 0; i < level; i++) { 5878 tcgrp = cgrp->ancestors[i]; 5879 tcgrp->nr_descendants++; 5880 5881 /* 5882 * If the new cgroup is frozen, all ancestor cgroups get a new 5883 * frozen descendant, but their state can't change because of 5884 * this. 5885 */ 5886 if (cgrp->freezer.e_freeze) 5887 tcgrp->freezer.nr_frozen_descendants++; 5888 } 5889 spin_unlock_irq(&css_set_lock); 5890 5891 list_add_tail_rcu(&cgrp->self.sibling, &cgroup_parent(cgrp)->self.children); 5892 atomic_inc(&root->nr_cgrps); 5893 cgroup_get_live(parent); 5894 5895 /* 5896 * On the default hierarchy, a child doesn't automatically inherit 5897 * subtree_control from the parent. Each is configured manually. 5898 */ 5899 if (!cgroup_on_dfl(cgrp)) 5900 cgrp->subtree_control = cgroup_control(cgrp); 5901 5902 cgroup_propagate_control(cgrp); 5903 5904 return cgrp; 5905 5906 out_psi_free: 5907 psi_cgroup_free(cgrp); 5908 out_stat_exit: 5909 css_rstat_exit(&cgrp->self); 5910 out_kernfs_remove: 5911 kernfs_remove(cgrp->kn); 5912 out_cancel_ref: 5913 percpu_ref_exit(&cgrp->self.refcnt); 5914 out_free_cgrp: 5915 kfree(cgrp); 5916 return ERR_PTR(ret); 5917 } 5918 5919 static bool cgroup_check_hierarchy_limits(struct cgroup *parent) 5920 { 5921 struct cgroup *cgroup; 5922 int ret = false; 5923 int level = 0; 5924 5925 lockdep_assert_held(&cgroup_mutex); 5926 5927 for (cgroup = parent; cgroup; cgroup = cgroup_parent(cgroup)) { 5928 if (cgroup->nr_descendants >= cgroup->max_descendants) 5929 goto fail; 5930 5931 if (level >= cgroup->max_depth) 5932 goto fail; 5933 5934 level++; 5935 } 5936 5937 ret = true; 5938 fail: 5939 return ret; 5940 } 5941 5942 int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name, umode_t mode) 5943 { 5944 struct cgroup *parent, *cgrp; 5945 int ret; 5946 5947 /* do not accept '\n' to prevent making /proc/<pid>/cgroup unparsable */ 5948 if (strchr(name, '\n')) 5949 return -EINVAL; 5950 5951 parent = cgroup_kn_lock_live(parent_kn, false); 5952 if (!parent) 5953 return -ENODEV; 5954 5955 if (!cgroup_check_hierarchy_limits(parent)) { 5956 ret = -EAGAIN; 5957 goto out_unlock; 5958 } 5959 5960 cgrp = cgroup_create(parent, name, mode); 5961 if (IS_ERR(cgrp)) { 5962 ret = PTR_ERR(cgrp); 5963 goto out_unlock; 5964 } 5965 5966 /* 5967 * This extra ref will be put in css_free_rwork_fn() and guarantees 5968 * that @cgrp->kn is always accessible. 5969 */ 5970 kernfs_get(cgrp->kn); 5971 5972 ret = css_populate_dir(&cgrp->self); 5973 if (ret) 5974 goto out_destroy; 5975 5976 ret = cgroup_apply_control_enable(cgrp); 5977 if (ret) 5978 goto out_destroy; 5979 5980 TRACE_CGROUP_PATH(mkdir, cgrp); 5981 5982 /* let's create and online css's */ 5983 kernfs_activate(cgrp->kn); 5984 5985 ret = 0; 5986 goto out_unlock; 5987 5988 out_destroy: 5989 cgroup_destroy_locked(cgrp); 5990 out_unlock: 5991 cgroup_kn_unlock(parent_kn); 5992 return ret; 5993 } 5994 5995 /* 5996 * This is called when the refcnt of a css is confirmed to be killed. 5997 * css_tryget_online() is now guaranteed to fail. Tell the subsystem to 5998 * initiate destruction and put the css ref from kill_css(). 5999 */ 6000 static void css_killed_work_fn(struct work_struct *work) 6001 { 6002 struct cgroup_subsys_state *css; 6003 6004 css = container_of(to_rcu_work(work), struct cgroup_subsys_state, destroy_rwork); 6005 6006 cgroup_lock(); 6007 6008 do { 6009 offline_css(css); 6010 css_put(css); 6011 /* @css can't go away while we're holding cgroup_mutex */ 6012 css = css->parent; 6013 } while (css && atomic_dec_and_test(&css->online_cnt)); 6014 6015 cgroup_unlock(); 6016 } 6017 6018 /* css kill confirmation processing requires process context, bounce */ 6019 static void css_killed_ref_fn(struct percpu_ref *ref) 6020 { 6021 struct cgroup_subsys_state *css = 6022 container_of(ref, struct cgroup_subsys_state, refcnt); 6023 6024 if (atomic_dec_and_test(&css->online_cnt)) { 6025 INIT_RCU_WORK(&css->destroy_rwork, css_killed_work_fn); 6026 queue_rcu_work(cgroup_offline_wq, &css->destroy_rwork); 6027 } 6028 } 6029 6030 /** 6031 * kill_css - destroy a css 6032 * @css: css to destroy 6033 * 6034 * This function initiates destruction of @css by removing cgroup interface 6035 * files and putting its base reference. ->css_offline() will be invoked 6036 * asynchronously once css_tryget_online() is guaranteed to fail and when 6037 * the reference count reaches zero, @css will be released. 6038 */ 6039 static void kill_css(struct cgroup_subsys_state *css) 6040 { 6041 lockdep_assert_held(&cgroup_mutex); 6042 6043 if (css->flags & CSS_DYING) 6044 return; 6045 6046 /* 6047 * Call css_killed(), if defined, before setting the CSS_DYING flag 6048 */ 6049 if (css->ss->css_killed) 6050 css->ss->css_killed(css); 6051 6052 css->flags |= CSS_DYING; 6053 6054 /* 6055 * This must happen before css is disassociated with its cgroup. 6056 * See seq_css() for details. 6057 */ 6058 css_clear_dir(css); 6059 6060 /* 6061 * Killing would put the base ref, but we need to keep it alive 6062 * until after ->css_offline(). 6063 */ 6064 css_get(css); 6065 6066 /* 6067 * cgroup core guarantees that, by the time ->css_offline() is 6068 * invoked, no new css reference will be given out via 6069 * css_tryget_online(). We can't simply call percpu_ref_kill() and 6070 * proceed to offlining css's because percpu_ref_kill() doesn't 6071 * guarantee that the ref is seen as killed on all CPUs on return. 6072 * 6073 * Use percpu_ref_kill_and_confirm() to get notifications as each 6074 * css is confirmed to be seen as killed on all CPUs. 6075 */ 6076 percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn); 6077 } 6078 6079 /** 6080 * cgroup_destroy_locked - the first stage of cgroup destruction 6081 * @cgrp: cgroup to be destroyed 6082 * 6083 * css's make use of percpu refcnts whose killing latency shouldn't be 6084 * exposed to userland and are RCU protected. Also, cgroup core needs to 6085 * guarantee that css_tryget_online() won't succeed by the time 6086 * ->css_offline() is invoked. To satisfy all the requirements, 6087 * destruction is implemented in the following two steps. 6088 * 6089 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all 6090 * userland visible parts and start killing the percpu refcnts of 6091 * css's. Set up so that the next stage will be kicked off once all 6092 * the percpu refcnts are confirmed to be killed. 6093 * 6094 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the 6095 * rest of destruction. Once all cgroup references are gone, the 6096 * cgroup is RCU-freed. 6097 * 6098 * This function implements s1. After this step, @cgrp is gone as far as 6099 * the userland is concerned and a new cgroup with the same name may be 6100 * created. As cgroup doesn't care about the names internally, this 6101 * doesn't cause any problem. 6102 */ 6103 static int cgroup_destroy_locked(struct cgroup *cgrp) 6104 __releases(&cgroup_mutex) __acquires(&cgroup_mutex) 6105 { 6106 struct cgroup *tcgrp, *parent = cgroup_parent(cgrp); 6107 struct cgroup_subsys_state *css; 6108 struct cgrp_cset_link *link; 6109 int ssid, ret; 6110 6111 lockdep_assert_held(&cgroup_mutex); 6112 6113 /* 6114 * Only migration can raise populated from zero and we're already 6115 * holding cgroup_mutex. 6116 */ 6117 if (cgroup_is_populated(cgrp)) 6118 return -EBUSY; 6119 6120 /* 6121 * Make sure there's no live children. We can't test emptiness of 6122 * ->self.children as dead children linger on it while being 6123 * drained; otherwise, "rmdir parent/child parent" may fail. 6124 */ 6125 if (css_has_online_children(&cgrp->self)) 6126 return -EBUSY; 6127 6128 /* 6129 * Mark @cgrp and the associated csets dead. The former prevents 6130 * further task migration and child creation by disabling 6131 * cgroup_kn_lock_live(). The latter makes the csets ignored by 6132 * the migration path. 6133 */ 6134 cgrp->self.flags &= ~CSS_ONLINE; 6135 6136 spin_lock_irq(&css_set_lock); 6137 list_for_each_entry(link, &cgrp->cset_links, cset_link) 6138 link->cset->dead = true; 6139 spin_unlock_irq(&css_set_lock); 6140 6141 /* initiate massacre of all css's */ 6142 for_each_css(css, ssid, cgrp) 6143 kill_css(css); 6144 6145 /* clear and remove @cgrp dir, @cgrp has an extra ref on its kn */ 6146 css_clear_dir(&cgrp->self); 6147 kernfs_remove(cgrp->kn); 6148 6149 if (cgroup_is_threaded(cgrp)) 6150 parent->nr_threaded_children--; 6151 6152 spin_lock_irq(&css_set_lock); 6153 for (tcgrp = parent; tcgrp; tcgrp = cgroup_parent(tcgrp)) { 6154 tcgrp->nr_descendants--; 6155 tcgrp->nr_dying_descendants++; 6156 /* 6157 * If the dying cgroup is frozen, decrease frozen descendants 6158 * counters of ancestor cgroups. 6159 */ 6160 if (test_bit(CGRP_FROZEN, &cgrp->flags)) 6161 tcgrp->freezer.nr_frozen_descendants--; 6162 } 6163 spin_unlock_irq(&css_set_lock); 6164 6165 cgroup1_check_for_release(parent); 6166 6167 ret = blocking_notifier_call_chain(&cgroup_lifetime_notifier, 6168 CGROUP_LIFETIME_OFFLINE, cgrp); 6169 WARN_ON_ONCE(notifier_to_errno(ret)); 6170 6171 /* put the base reference */ 6172 percpu_ref_kill(&cgrp->self.refcnt); 6173 6174 return 0; 6175 }; 6176 6177 /** 6178 * cgroup_drain_dying - wait for dying tasks to leave before rmdir 6179 * @cgrp: the cgroup being removed 6180 * 6181 * cgroup.procs and cgroup.threads use css_task_iter which filters out 6182 * PF_EXITING tasks so that userspace doesn't see tasks that have already been 6183 * reaped via waitpid(). However, cgroup_has_tasks() - which tests whether the 6184 * cgroup has non-empty css_sets - is only updated when dying tasks pass through 6185 * cgroup_task_dead() in finish_task_switch(). This creates a window where 6186 * cgroup.procs reads empty but cgroup_has_tasks() is still true, making rmdir 6187 * fail with -EBUSY from cgroup_destroy_locked() even though userspace sees no 6188 * tasks. 6189 * 6190 * This function aligns cgroup_has_tasks() with what userspace can observe. If 6191 * cgroup_has_tasks() but the task iterator sees nothing (all remaining tasks are 6192 * PF_EXITING), we wait for cgroup_task_dead() to finish processing them. As the 6193 * window between PF_EXITING and cgroup_task_dead() is short, the wait is brief. 6194 * 6195 * This function only concerns itself with this cgroup's own dying tasks. 6196 * Whether the cgroup has children is cgroup_destroy_locked()'s problem. 6197 * 6198 * Each cgroup_task_dead() kicks the waitqueue via cset->cgrp_links, and we 6199 * retry the full check from scratch. 6200 * 6201 * Must be called with cgroup_mutex held. 6202 */ 6203 static int cgroup_drain_dying(struct cgroup *cgrp) 6204 __releases(&cgroup_mutex) __acquires(&cgroup_mutex) 6205 { 6206 struct css_task_iter it; 6207 struct task_struct *task; 6208 DEFINE_WAIT(wait); 6209 6210 lockdep_assert_held(&cgroup_mutex); 6211 retry: 6212 if (!cgroup_has_tasks(cgrp)) 6213 return 0; 6214 6215 /* Same iterator as cgroup.threads - if any task is visible, it's busy */ 6216 css_task_iter_start(&cgrp->self, 0, &it); 6217 task = css_task_iter_next(&it); 6218 css_task_iter_end(&it); 6219 6220 if (task) 6221 return -EBUSY; 6222 6223 /* 6224 * All remaining tasks are PF_EXITING and will pass through 6225 * cgroup_task_dead() shortly. Wait for a kick and retry. 6226 * 6227 * cgroup_has_tasks() can't transition from false to true while we're 6228 * holding cgroup_mutex, but the true to false transition happens 6229 * under css_set_lock (via cgroup_task_dead()). We must retest and 6230 * prepare_to_wait() under css_set_lock. Otherwise, the transition 6231 * can happen between our first test and prepare_to_wait(), and we 6232 * sleep with no one to wake us. 6233 */ 6234 spin_lock_irq(&css_set_lock); 6235 if (!cgroup_has_tasks(cgrp)) { 6236 spin_unlock_irq(&css_set_lock); 6237 return 0; 6238 } 6239 prepare_to_wait(&cgrp->dying_populated_waitq, &wait, 6240 TASK_UNINTERRUPTIBLE); 6241 spin_unlock_irq(&css_set_lock); 6242 mutex_unlock(&cgroup_mutex); 6243 schedule(); 6244 finish_wait(&cgrp->dying_populated_waitq, &wait); 6245 mutex_lock(&cgroup_mutex); 6246 goto retry; 6247 } 6248 6249 int cgroup_rmdir(struct kernfs_node *kn) 6250 { 6251 struct cgroup *cgrp; 6252 int ret = 0; 6253 6254 cgrp = cgroup_kn_lock_live(kn, false); 6255 if (!cgrp) 6256 return 0; 6257 6258 ret = cgroup_drain_dying(cgrp); 6259 if (!ret) { 6260 ret = cgroup_destroy_locked(cgrp); 6261 if (!ret) 6262 TRACE_CGROUP_PATH(rmdir, cgrp); 6263 } 6264 6265 cgroup_kn_unlock(kn); 6266 return ret; 6267 } 6268 6269 static struct kernfs_syscall_ops cgroup_kf_syscall_ops = { 6270 .show_options = cgroup_show_options, 6271 .mkdir = cgroup_mkdir, 6272 .rmdir = cgroup_rmdir, 6273 .show_path = cgroup_show_path, 6274 }; 6275 6276 static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early) 6277 { 6278 struct cgroup_subsys_state *css; 6279 6280 pr_debug("Initializing cgroup subsys %s\n", ss->name); 6281 6282 cgroup_lock(); 6283 6284 idr_init(&ss->css_idr); 6285 INIT_LIST_HEAD(&ss->cfts); 6286 6287 /* Create the root cgroup state for this subsystem */ 6288 ss->root = &cgrp_dfl_root; 6289 css = ss->css_alloc(NULL); 6290 /* We don't handle early failures gracefully */ 6291 BUG_ON(IS_ERR(css)); 6292 init_and_link_css(css, ss, &cgrp_dfl_root.cgrp); 6293 6294 /* 6295 * Root csses are never destroyed and we can't initialize 6296 * percpu_ref during early init. Disable refcnting. 6297 */ 6298 css->flags |= CSS_NO_REF; 6299 6300 if (early) { 6301 /* allocation can't be done safely during early init */ 6302 css->id = 1; 6303 } else { 6304 css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2, GFP_KERNEL); 6305 BUG_ON(css->id < 0); 6306 6307 BUG_ON(ss_rstat_init(ss)); 6308 BUG_ON(css_rstat_init(css)); 6309 } 6310 6311 /* Update the init_css_set to contain a subsys 6312 * pointer to this state - since the subsystem is 6313 * newly registered, all tasks and hence the 6314 * init_css_set is in the subsystem's root cgroup. */ 6315 init_css_set.subsys[ss->id] = css; 6316 6317 have_fork_callback |= (bool)ss->fork << ss->id; 6318 have_exit_callback |= (bool)ss->exit << ss->id; 6319 have_release_callback |= (bool)ss->release << ss->id; 6320 have_canfork_callback |= (bool)ss->can_fork << ss->id; 6321 6322 /* At system boot, before all subsystems have been 6323 * registered, no tasks have been forked, so we don't 6324 * need to invoke fork callbacks here. */ 6325 BUG_ON(!list_empty(&init_task.tasks)); 6326 6327 BUG_ON(online_css(css)); 6328 6329 cgroup_unlock(); 6330 } 6331 6332 /** 6333 * cgroup_init_early - cgroup initialization at system boot 6334 * 6335 * Initialize cgroups at system boot, and initialize any 6336 * subsystems that request early init. 6337 */ 6338 int __init cgroup_init_early(void) 6339 { 6340 static struct cgroup_fs_context __initdata ctx; 6341 struct cgroup_subsys *ss; 6342 int i; 6343 6344 ctx.root = &cgrp_dfl_root; 6345 init_cgroup_root(&ctx); 6346 cgrp_dfl_root.cgrp.self.flags |= CSS_NO_REF; 6347 6348 RCU_INIT_POINTER(init_task.cgroups, &init_css_set); 6349 6350 for_each_subsys(ss, i) { 6351 WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id, 6352 "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p id:name=%d:%s\n", 6353 i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free, 6354 ss->id, ss->name); 6355 WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN, 6356 "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]); 6357 WARN(ss->early_init && ss->css_rstat_flush, 6358 "cgroup rstat cannot be used with early init subsystem\n"); 6359 6360 ss->id = i; 6361 ss->name = cgroup_subsys_name[i]; 6362 if (!ss->legacy_name) 6363 ss->legacy_name = cgroup_subsys_name[i]; 6364 6365 if (ss->early_init) 6366 cgroup_init_subsys(ss, true); 6367 } 6368 return 0; 6369 } 6370 6371 /** 6372 * cgroup_init - cgroup initialization 6373 * 6374 * Register cgroup filesystem and /proc file, and initialize 6375 * any subsystems that didn't request early init. 6376 */ 6377 int __init cgroup_init(void) 6378 { 6379 struct cgroup_subsys *ss; 6380 int ssid; 6381 6382 BUILD_BUG_ON(CGROUP_SUBSYS_COUNT > 32); 6383 BUG_ON(cgroup_init_cftypes(NULL, cgroup_base_files)); 6384 BUG_ON(cgroup_init_cftypes(NULL, cgroup_psi_files)); 6385 BUG_ON(cgroup_init_cftypes(NULL, cgroup1_base_files)); 6386 6387 BUG_ON(ss_rstat_init(NULL)); 6388 6389 get_user_ns(init_cgroup_ns.user_ns); 6390 cgroup_rt_init(); 6391 6392 cgroup_lock(); 6393 6394 /* 6395 * Add init_css_set to the hash table so that dfl_root can link to 6396 * it during init. 6397 */ 6398 hash_add(css_set_table, &init_css_set.hlist, 6399 css_set_hash(init_css_set.subsys)); 6400 6401 cgroup_bpf_lifetime_notifier_init(); 6402 6403 BUG_ON(cgroup_setup_root(&cgrp_dfl_root, 0)); 6404 6405 cgroup_unlock(); 6406 6407 for_each_subsys(ss, ssid) { 6408 if (ss->early_init) { 6409 struct cgroup_subsys_state *css = 6410 init_css_set.subsys[ss->id]; 6411 6412 css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2, 6413 GFP_KERNEL); 6414 BUG_ON(css->id < 0); 6415 } else { 6416 cgroup_init_subsys(ss, false); 6417 } 6418 6419 list_add_tail(&init_css_set.e_cset_node[ssid], 6420 &cgrp_dfl_root.cgrp.e_csets[ssid]); 6421 6422 /* 6423 * Setting dfl_root subsys_mask needs to consider the 6424 * disabled flag and cftype registration needs kmalloc, 6425 * both of which aren't available during early_init. 6426 */ 6427 if (!cgroup_ssid_enabled(ssid)) 6428 continue; 6429 6430 if (cgroup1_ssid_disabled(ssid)) 6431 pr_info("Disabling %s control group subsystem in v1 mounts\n", 6432 ss->legacy_name); 6433 6434 cgrp_dfl_root.subsys_mask |= 1 << ss->id; 6435 6436 /* implicit controllers must be threaded too */ 6437 WARN_ON(ss->implicit_on_dfl && !ss->threaded); 6438 6439 if (ss->implicit_on_dfl) 6440 cgrp_dfl_implicit_ss_mask |= 1 << ss->id; 6441 else if (!ss->dfl_cftypes) 6442 cgrp_dfl_inhibit_ss_mask |= 1 << ss->id; 6443 6444 if (ss->threaded) 6445 cgrp_dfl_threaded_ss_mask |= 1 << ss->id; 6446 6447 if (ss->dfl_cftypes == ss->legacy_cftypes) { 6448 WARN_ON(cgroup_add_cftypes(ss, ss->dfl_cftypes)); 6449 } else { 6450 WARN_ON(cgroup_add_dfl_cftypes(ss, ss->dfl_cftypes)); 6451 WARN_ON(cgroup_add_legacy_cftypes(ss, ss->legacy_cftypes)); 6452 } 6453 6454 if (ss->bind) 6455 ss->bind(init_css_set.subsys[ssid]); 6456 6457 cgroup_lock(); 6458 css_populate_dir(init_css_set.subsys[ssid]); 6459 cgroup_unlock(); 6460 } 6461 6462 /* init_css_set.subsys[] has been updated, re-hash */ 6463 hash_del(&init_css_set.hlist); 6464 hash_add(css_set_table, &init_css_set.hlist, 6465 css_set_hash(init_css_set.subsys)); 6466 6467 WARN_ON(sysfs_create_mount_point(fs_kobj, "cgroup")); 6468 WARN_ON(register_filesystem(&cgroup_fs_type)); 6469 WARN_ON(register_filesystem(&cgroup2_fs_type)); 6470 WARN_ON(!proc_create_single("cgroups", 0, NULL, proc_cgroupstats_show)); 6471 #ifdef CONFIG_CPUSETS_V1 6472 WARN_ON(register_filesystem(&cpuset_fs_type)); 6473 #endif 6474 6475 ns_tree_add(&init_cgroup_ns); 6476 return 0; 6477 } 6478 6479 static int __init cgroup_wq_init(void) 6480 { 6481 /* 6482 * There isn't much point in executing destruction path in 6483 * parallel. Good chunk is serialized with cgroup_mutex anyway. 6484 * Use 1 for @max_active. 6485 * 6486 * We would prefer to do this in cgroup_init() above, but that 6487 * is called before init_workqueues(): so leave this until after. 6488 */ 6489 cgroup_offline_wq = alloc_workqueue("cgroup_offline", WQ_PERCPU, 1); 6490 BUG_ON(!cgroup_offline_wq); 6491 6492 cgroup_release_wq = alloc_workqueue("cgroup_release", WQ_PERCPU, 1); 6493 BUG_ON(!cgroup_release_wq); 6494 6495 cgroup_free_wq = alloc_workqueue("cgroup_free", WQ_PERCPU, 1); 6496 BUG_ON(!cgroup_free_wq); 6497 return 0; 6498 } 6499 core_initcall(cgroup_wq_init); 6500 6501 void cgroup_path_from_kernfs_id(u64 id, char *buf, size_t buflen) 6502 { 6503 struct kernfs_node *kn; 6504 6505 kn = kernfs_find_and_get_node_by_id(cgrp_dfl_root.kf_root, id); 6506 if (!kn) 6507 return; 6508 kernfs_path(kn, buf, buflen); 6509 kernfs_put(kn); 6510 } 6511 6512 /* 6513 * __cgroup_get_from_id : get the cgroup associated with cgroup id 6514 * @id: cgroup id 6515 * On success return the cgrp or ERR_PTR on failure 6516 * There are no cgroup NS restrictions. 6517 */ 6518 struct cgroup *__cgroup_get_from_id(u64 id) 6519 { 6520 struct kernfs_node *kn; 6521 struct cgroup *cgrp; 6522 6523 kn = kernfs_find_and_get_node_by_id(cgrp_dfl_root.kf_root, id); 6524 if (!kn) 6525 return ERR_PTR(-ENOENT); 6526 6527 if (kernfs_type(kn) != KERNFS_DIR) { 6528 kernfs_put(kn); 6529 return ERR_PTR(-ENOENT); 6530 } 6531 6532 rcu_read_lock(); 6533 6534 cgrp = rcu_dereference(*(void __rcu __force **)&kn->priv); 6535 if (cgrp && !cgroup_tryget(cgrp)) 6536 cgrp = NULL; 6537 6538 rcu_read_unlock(); 6539 kernfs_put(kn); 6540 6541 if (!cgrp) 6542 return ERR_PTR(-ENOENT); 6543 return cgrp; 6544 } 6545 6546 /* 6547 * cgroup_get_from_id : get the cgroup associated with cgroup id 6548 * @id: cgroup id 6549 * On success return the cgrp or ERR_PTR on failure 6550 * Only cgroups within current task's cgroup NS are valid. 6551 */ 6552 struct cgroup *cgroup_get_from_id(u64 id) 6553 { 6554 struct cgroup *cgrp, *root_cgrp; 6555 6556 cgrp = __cgroup_get_from_id(id); 6557 if (IS_ERR(cgrp)) 6558 return cgrp; 6559 6560 root_cgrp = current_cgns_cgroup_dfl(); 6561 if (!cgroup_is_descendant(cgrp, root_cgrp)) { 6562 cgroup_put(cgrp); 6563 return ERR_PTR(-ENOENT); 6564 } 6565 6566 return cgrp; 6567 } 6568 EXPORT_SYMBOL_GPL(cgroup_get_from_id); 6569 6570 /* 6571 * proc_cgroup_show() 6572 * - Print task's cgroup paths into seq_file, one line for each hierarchy 6573 * - Used for /proc/<pid>/cgroup. 6574 */ 6575 int proc_cgroup_show(struct seq_file *m, struct pid_namespace *ns, 6576 struct pid *pid, struct task_struct *tsk) 6577 { 6578 char *buf; 6579 int retval; 6580 struct cgroup_root *root; 6581 6582 retval = -ENOMEM; 6583 buf = kmalloc(PATH_MAX, GFP_KERNEL); 6584 if (!buf) 6585 goto out; 6586 6587 rcu_read_lock(); 6588 spin_lock_irq(&css_set_lock); 6589 6590 for_each_root(root) { 6591 struct cgroup_subsys *ss; 6592 struct cgroup *cgrp; 6593 int ssid, count = 0; 6594 6595 if (root == &cgrp_dfl_root && !READ_ONCE(cgrp_dfl_visible)) 6596 continue; 6597 6598 cgrp = task_cgroup_from_root(tsk, root); 6599 /* The root has already been unmounted. */ 6600 if (!cgrp) 6601 continue; 6602 6603 seq_printf(m, "%d:", root->hierarchy_id); 6604 if (root != &cgrp_dfl_root) 6605 for_each_subsys(ss, ssid) 6606 if (root->subsys_mask & (1 << ssid)) 6607 seq_printf(m, "%s%s", count++ ? "," : "", 6608 ss->legacy_name); 6609 if (strlen(root->name)) 6610 seq_printf(m, "%sname=%s", count ? "," : "", 6611 root->name); 6612 seq_putc(m, ':'); 6613 /* 6614 * On traditional hierarchies, all zombie tasks show up as 6615 * belonging to the root cgroup. On the default hierarchy, 6616 * while a zombie doesn't show up in "cgroup.procs" and 6617 * thus can't be migrated, its /proc/PID/cgroup keeps 6618 * reporting the cgroup it belonged to before exiting. If 6619 * the cgroup is removed before the zombie is reaped, 6620 * " (deleted)" is appended to the cgroup path. 6621 */ 6622 if (cgroup_on_dfl(cgrp) || !(tsk->flags & PF_EXITING)) { 6623 retval = cgroup_path_ns_locked(cgrp, buf, PATH_MAX, 6624 current->nsproxy->cgroup_ns); 6625 if (retval == -E2BIG) 6626 retval = -ENAMETOOLONG; 6627 if (retval < 0) 6628 goto out_unlock; 6629 6630 seq_puts(m, buf); 6631 } else { 6632 seq_puts(m, "/"); 6633 } 6634 6635 if (cgroup_on_dfl(cgrp) && cgroup_is_dead(cgrp)) 6636 seq_puts(m, " (deleted)\n"); 6637 else 6638 seq_putc(m, '\n'); 6639 } 6640 6641 retval = 0; 6642 out_unlock: 6643 spin_unlock_irq(&css_set_lock); 6644 rcu_read_unlock(); 6645 kfree(buf); 6646 out: 6647 return retval; 6648 } 6649 6650 /** 6651 * cgroup_fork - initialize cgroup related fields during copy_process() 6652 * @child: pointer to task_struct of forking parent process. 6653 * 6654 * A task is associated with the init_css_set until cgroup_post_fork() 6655 * attaches it to the target css_set. 6656 */ 6657 void cgroup_fork(struct task_struct *child) 6658 { 6659 RCU_INIT_POINTER(child->cgroups, &init_css_set); 6660 INIT_LIST_HEAD(&child->cg_list); 6661 } 6662 6663 /** 6664 * cgroup_v1v2_get_from_file - get a cgroup pointer from a file pointer 6665 * @f: file corresponding to cgroup_dir 6666 * 6667 * Find the cgroup from a file pointer associated with a cgroup directory. 6668 * Returns a pointer to the cgroup on success. ERR_PTR is returned if the 6669 * cgroup cannot be found. 6670 */ 6671 static struct cgroup *cgroup_v1v2_get_from_file(struct file *f) 6672 { 6673 struct cgroup_subsys_state *css; 6674 6675 css = css_tryget_online_from_dir(f->f_path.dentry, NULL); 6676 if (IS_ERR(css)) 6677 return ERR_CAST(css); 6678 6679 return css->cgroup; 6680 } 6681 6682 /** 6683 * cgroup_get_from_file - same as cgroup_v1v2_get_from_file, but only supports 6684 * cgroup2. 6685 * @f: file corresponding to cgroup2_dir 6686 */ 6687 static struct cgroup *cgroup_get_from_file(struct file *f) 6688 { 6689 struct cgroup *cgrp = cgroup_v1v2_get_from_file(f); 6690 6691 if (IS_ERR(cgrp)) 6692 return ERR_CAST(cgrp); 6693 6694 if (!cgroup_on_dfl(cgrp)) { 6695 cgroup_put(cgrp); 6696 return ERR_PTR(-EBADF); 6697 } 6698 6699 return cgrp; 6700 } 6701 6702 /** 6703 * cgroup_css_set_fork - find or create a css_set for a child process 6704 * @kargs: the arguments passed to create the child process 6705 * 6706 * This functions finds or creates a new css_set which the child 6707 * process will be attached to in cgroup_post_fork(). By default, 6708 * the child process will be given the same css_set as its parent. 6709 * 6710 * If CLONE_INTO_CGROUP is specified this function will try to find an 6711 * existing css_set which includes the requested cgroup and if not create 6712 * a new css_set that the child will be attached to later. If this function 6713 * succeeds it will hold cgroup_threadgroup_rwsem on return. If 6714 * CLONE_INTO_CGROUP is requested this function will grab cgroup mutex 6715 * before grabbing cgroup_threadgroup_rwsem and will hold a reference 6716 * to the target cgroup. 6717 */ 6718 static int cgroup_css_set_fork(struct kernel_clone_args *kargs) 6719 __acquires(&cgroup_mutex) __acquires(&cgroup_threadgroup_rwsem) 6720 { 6721 int ret; 6722 struct cgroup *dst_cgrp = NULL; 6723 struct css_set *cset; 6724 struct super_block *sb; 6725 6726 if (kargs->flags & CLONE_INTO_CGROUP) 6727 cgroup_lock(); 6728 6729 cgroup_threadgroup_change_begin(current); 6730 6731 spin_lock_irq(&css_set_lock); 6732 cset = task_css_set(current); 6733 get_css_set(cset); 6734 if (kargs->cgrp) 6735 kargs->kill_seq = kargs->cgrp->kill_seq; 6736 else 6737 kargs->kill_seq = cset->dfl_cgrp->kill_seq; 6738 spin_unlock_irq(&css_set_lock); 6739 6740 if (!(kargs->flags & CLONE_INTO_CGROUP)) { 6741 kargs->cset = cset; 6742 return 0; 6743 } 6744 6745 CLASS(fd_raw, f)(kargs->cgroup); 6746 if (fd_empty(f)) { 6747 ret = -EBADF; 6748 goto err; 6749 } 6750 sb = fd_file(f)->f_path.dentry->d_sb; 6751 6752 dst_cgrp = cgroup_get_from_file(fd_file(f)); 6753 if (IS_ERR(dst_cgrp)) { 6754 ret = PTR_ERR(dst_cgrp); 6755 dst_cgrp = NULL; 6756 goto err; 6757 } 6758 6759 if (cgroup_is_dead(dst_cgrp)) { 6760 ret = -ENODEV; 6761 goto err; 6762 } 6763 6764 /* 6765 * Verify that we the target cgroup is writable for us. This is 6766 * usually done by the vfs layer but since we're not going through 6767 * the vfs layer here we need to do it "manually". 6768 */ 6769 ret = cgroup_may_write(dst_cgrp, sb); 6770 if (ret) 6771 goto err; 6772 6773 /* 6774 * Spawning a task directly into a cgroup works by passing a file 6775 * descriptor to the target cgroup directory. This can even be an O_PATH 6776 * file descriptor. But it can never be a cgroup.procs file descriptor. 6777 * This was done on purpose so spawning into a cgroup could be 6778 * conceptualized as an atomic 6779 * 6780 * fd = openat(dfd_cgroup, "cgroup.procs", ...); 6781 * write(fd, <child-pid>, ...); 6782 * 6783 * sequence, i.e. it's a shorthand for the caller opening and writing 6784 * cgroup.procs of the cgroup indicated by @dfd_cgroup. This allows us 6785 * to always use the caller's credentials. 6786 */ 6787 ret = cgroup_attach_permissions(cset->dfl_cgrp, dst_cgrp, sb, 6788 !(kargs->flags & CLONE_THREAD), 6789 current->nsproxy->cgroup_ns); 6790 if (ret) 6791 goto err; 6792 6793 kargs->cset = find_css_set(cset, dst_cgrp); 6794 if (!kargs->cset) { 6795 ret = -ENOMEM; 6796 goto err; 6797 } 6798 6799 put_css_set(cset); 6800 kargs->cgrp = dst_cgrp; 6801 return ret; 6802 6803 err: 6804 cgroup_threadgroup_change_end(current); 6805 cgroup_unlock(); 6806 if (dst_cgrp) 6807 cgroup_put(dst_cgrp); 6808 put_css_set(cset); 6809 if (kargs->cset) 6810 put_css_set(kargs->cset); 6811 return ret; 6812 } 6813 6814 /** 6815 * cgroup_css_set_put_fork - drop references we took during fork 6816 * @kargs: the arguments passed to create the child process 6817 * 6818 * Drop references to the prepared css_set and target cgroup if 6819 * CLONE_INTO_CGROUP was requested. 6820 */ 6821 static void cgroup_css_set_put_fork(struct kernel_clone_args *kargs) 6822 __releases(&cgroup_threadgroup_rwsem) __releases(&cgroup_mutex) 6823 { 6824 struct cgroup *cgrp = kargs->cgrp; 6825 struct css_set *cset = kargs->cset; 6826 6827 cgroup_threadgroup_change_end(current); 6828 6829 if (cset) { 6830 put_css_set(cset); 6831 kargs->cset = NULL; 6832 } 6833 6834 if (kargs->flags & CLONE_INTO_CGROUP) { 6835 cgroup_unlock(); 6836 if (cgrp) { 6837 cgroup_put(cgrp); 6838 kargs->cgrp = NULL; 6839 } 6840 } 6841 } 6842 6843 /** 6844 * cgroup_can_fork - called on a new task before the process is exposed 6845 * @child: the child process 6846 * @kargs: the arguments passed to create the child process 6847 * 6848 * This prepares a new css_set for the child process which the child will 6849 * be attached to in cgroup_post_fork(). 6850 * This calls the subsystem can_fork() callbacks. If the cgroup_can_fork() 6851 * callback returns an error, the fork aborts with that error code. This 6852 * allows for a cgroup subsystem to conditionally allow or deny new forks. 6853 */ 6854 int cgroup_can_fork(struct task_struct *child, struct kernel_clone_args *kargs) 6855 { 6856 struct cgroup_subsys *ss; 6857 int i, j, ret; 6858 6859 ret = cgroup_css_set_fork(kargs); 6860 if (ret) 6861 return ret; 6862 6863 do_each_subsys_mask(ss, i, have_canfork_callback) { 6864 ret = ss->can_fork(child, kargs->cset); 6865 if (ret) 6866 goto out_revert; 6867 } while_each_subsys_mask(); 6868 6869 return 0; 6870 6871 out_revert: 6872 for_each_subsys(ss, j) { 6873 if (j >= i) 6874 break; 6875 if (ss->cancel_fork) 6876 ss->cancel_fork(child, kargs->cset); 6877 } 6878 6879 cgroup_css_set_put_fork(kargs); 6880 6881 return ret; 6882 } 6883 6884 /** 6885 * cgroup_cancel_fork - called if a fork failed after cgroup_can_fork() 6886 * @child: the child process 6887 * @kargs: the arguments passed to create the child process 6888 * 6889 * This calls the cancel_fork() callbacks if a fork failed *after* 6890 * cgroup_can_fork() succeeded and cleans up references we took to 6891 * prepare a new css_set for the child process in cgroup_can_fork(). 6892 */ 6893 void cgroup_cancel_fork(struct task_struct *child, 6894 struct kernel_clone_args *kargs) 6895 { 6896 struct cgroup_subsys *ss; 6897 int i; 6898 6899 for_each_subsys(ss, i) 6900 if (ss->cancel_fork) 6901 ss->cancel_fork(child, kargs->cset); 6902 6903 cgroup_css_set_put_fork(kargs); 6904 } 6905 6906 /** 6907 * cgroup_post_fork - finalize cgroup setup for the child process 6908 * @child: the child process 6909 * @kargs: the arguments passed to create the child process 6910 * 6911 * Attach the child process to its css_set calling the subsystem fork() 6912 * callbacks. 6913 */ 6914 void cgroup_post_fork(struct task_struct *child, 6915 struct kernel_clone_args *kargs) 6916 __releases(&cgroup_threadgroup_rwsem) __releases(&cgroup_mutex) 6917 { 6918 unsigned int cgrp_kill_seq = 0; 6919 unsigned long cgrp_flags = 0; 6920 bool kill = false; 6921 struct cgroup_subsys *ss; 6922 struct css_set *cset; 6923 int i; 6924 6925 cset = kargs->cset; 6926 kargs->cset = NULL; 6927 6928 spin_lock_irq(&css_set_lock); 6929 6930 /* init tasks are special, only link regular threads */ 6931 if (likely(child->pid)) { 6932 if (kargs->cgrp) { 6933 cgrp_flags = kargs->cgrp->flags; 6934 cgrp_kill_seq = kargs->cgrp->kill_seq; 6935 } else { 6936 cgrp_flags = cset->dfl_cgrp->flags; 6937 cgrp_kill_seq = cset->dfl_cgrp->kill_seq; 6938 } 6939 6940 WARN_ON_ONCE(!list_empty(&child->cg_list)); 6941 cset->nr_tasks++; 6942 css_set_move_task(child, NULL, cset, false); 6943 } else { 6944 put_css_set(cset); 6945 cset = NULL; 6946 } 6947 6948 if (!(child->flags & PF_KTHREAD)) { 6949 if (unlikely(test_bit(CGRP_FREEZE, &cgrp_flags))) { 6950 /* 6951 * If the cgroup has to be frozen, the new task has 6952 * too. Let's set the JOBCTL_TRAP_FREEZE jobctl bit to 6953 * get the task into the frozen state. 6954 */ 6955 spin_lock(&child->sighand->siglock); 6956 WARN_ON_ONCE(child->frozen); 6957 child->jobctl |= JOBCTL_TRAP_FREEZE; 6958 spin_unlock(&child->sighand->siglock); 6959 6960 /* 6961 * Calling cgroup_update_frozen() isn't required here, 6962 * because it will be called anyway a bit later from 6963 * do_freezer_trap(). So we avoid cgroup's transient 6964 * switch from the frozen state and back. 6965 */ 6966 } 6967 6968 /* 6969 * If the cgroup is to be killed notice it now and take the 6970 * child down right after we finished preparing it for 6971 * userspace. 6972 */ 6973 kill = kargs->kill_seq != cgrp_kill_seq; 6974 } 6975 6976 spin_unlock_irq(&css_set_lock); 6977 6978 /* 6979 * Call ss->fork(). This must happen after @child is linked on 6980 * css_set; otherwise, @child might change state between ->fork() 6981 * and addition to css_set. 6982 */ 6983 do_each_subsys_mask(ss, i, have_fork_callback) { 6984 ss->fork(child); 6985 } while_each_subsys_mask(); 6986 6987 /* Make the new cset the root_cset of the new cgroup namespace. */ 6988 if (kargs->flags & CLONE_NEWCGROUP) { 6989 struct css_set *rcset = child->nsproxy->cgroup_ns->root_cset; 6990 6991 get_css_set(cset); 6992 child->nsproxy->cgroup_ns->root_cset = cset; 6993 put_css_set(rcset); 6994 } 6995 6996 /* Cgroup has to be killed so take down child immediately. */ 6997 if (unlikely(kill)) 6998 do_send_sig_info(SIGKILL, SEND_SIG_NOINFO, child, PIDTYPE_TGID); 6999 7000 cgroup_css_set_put_fork(kargs); 7001 } 7002 7003 /** 7004 * cgroup_task_exit - detach cgroup from exiting task 7005 * @tsk: pointer to task_struct of exiting process 7006 * 7007 * Description: Detach cgroup from @tsk. 7008 * 7009 */ 7010 void cgroup_task_exit(struct task_struct *tsk) 7011 { 7012 struct cgroup_subsys *ss; 7013 int i; 7014 7015 /* see cgroup_post_fork() for details */ 7016 do_each_subsys_mask(ss, i, have_exit_callback) { 7017 ss->exit(tsk); 7018 } while_each_subsys_mask(); 7019 } 7020 7021 static void do_cgroup_task_dead(struct task_struct *tsk) 7022 { 7023 struct cgrp_cset_link *link; 7024 struct css_set *cset; 7025 unsigned long flags; 7026 7027 spin_lock_irqsave(&css_set_lock, flags); 7028 7029 WARN_ON_ONCE(list_empty(&tsk->cg_list)); 7030 cset = task_css_set(tsk); 7031 css_set_move_task(tsk, cset, NULL, false); 7032 cset->nr_tasks--; 7033 /* matches the signal->live check in css_task_iter_advance() */ 7034 if (thread_group_leader(tsk) && atomic_read(&tsk->signal->live)) 7035 list_add_tail(&tsk->cg_list, &cset->dying_tasks); 7036 7037 /* kick cgroup_drain_dying() waiters, see cgroup_rmdir() */ 7038 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) 7039 if (waitqueue_active(&link->cgrp->dying_populated_waitq)) 7040 wake_up(&link->cgrp->dying_populated_waitq); 7041 7042 if (dl_task(tsk)) 7043 dec_dl_tasks_cs(tsk); 7044 7045 WARN_ON_ONCE(cgroup_task_frozen(tsk)); 7046 if (unlikely(!(tsk->flags & PF_KTHREAD) && 7047 test_bit(CGRP_FREEZE, &task_dfl_cgroup(tsk)->flags))) 7048 cgroup_update_frozen(task_dfl_cgroup(tsk)); 7049 7050 spin_unlock_irqrestore(&css_set_lock, flags); 7051 } 7052 7053 #ifdef CONFIG_PREEMPT_RT 7054 /* 7055 * cgroup_task_dead() is called from finish_task_switch() which doesn't allow 7056 * scheduling even in RT. As the task_dead path requires grabbing css_set_lock, 7057 * this lead to sleeping in the invalid context warning bug. css_set_lock is too 7058 * big to become a raw_spinlock. The task_dead path doesn't need to run 7059 * synchronously but can't be delayed indefinitely either as the dead task pins 7060 * the cgroup and task_struct can be pinned indefinitely. Bounce through lazy 7061 * irq_work to allow batching while ensuring timely completion. 7062 */ 7063 static DEFINE_PER_CPU(struct llist_head, cgrp_dead_tasks); 7064 static DEFINE_PER_CPU(struct irq_work, cgrp_dead_tasks_iwork); 7065 7066 static void cgrp_dead_tasks_iwork_fn(struct irq_work *iwork) 7067 { 7068 struct llist_node *lnode; 7069 struct task_struct *task, *next; 7070 7071 lnode = llist_del_all(this_cpu_ptr(&cgrp_dead_tasks)); 7072 llist_for_each_entry_safe(task, next, lnode, cg_dead_lnode) { 7073 do_cgroup_task_dead(task); 7074 put_task_struct(task); 7075 } 7076 } 7077 7078 static void __init cgroup_rt_init(void) 7079 { 7080 int cpu; 7081 7082 for_each_possible_cpu(cpu) { 7083 init_llist_head(per_cpu_ptr(&cgrp_dead_tasks, cpu)); 7084 per_cpu(cgrp_dead_tasks_iwork, cpu) = 7085 IRQ_WORK_INIT_LAZY(cgrp_dead_tasks_iwork_fn); 7086 } 7087 } 7088 7089 void cgroup_task_dead(struct task_struct *task) 7090 { 7091 get_task_struct(task); 7092 llist_add(&task->cg_dead_lnode, this_cpu_ptr(&cgrp_dead_tasks)); 7093 irq_work_queue(this_cpu_ptr(&cgrp_dead_tasks_iwork)); 7094 } 7095 #else /* CONFIG_PREEMPT_RT */ 7096 static void __init cgroup_rt_init(void) {} 7097 7098 void cgroup_task_dead(struct task_struct *task) 7099 { 7100 do_cgroup_task_dead(task); 7101 } 7102 #endif /* CONFIG_PREEMPT_RT */ 7103 7104 void cgroup_task_release(struct task_struct *task) 7105 { 7106 struct cgroup_subsys *ss; 7107 int ssid; 7108 7109 do_each_subsys_mask(ss, ssid, have_release_callback) { 7110 ss->release(task); 7111 } while_each_subsys_mask(); 7112 } 7113 7114 void cgroup_task_free(struct task_struct *task) 7115 { 7116 struct css_set *cset = task_css_set(task); 7117 7118 if (!list_empty(&task->cg_list)) { 7119 spin_lock_irq(&css_set_lock); 7120 css_set_skip_task_iters(task_css_set(task), task); 7121 list_del_init(&task->cg_list); 7122 spin_unlock_irq(&css_set_lock); 7123 } 7124 7125 put_css_set(cset); 7126 } 7127 7128 static int __init cgroup_disable(char *str) 7129 { 7130 struct cgroup_subsys *ss; 7131 char *token; 7132 int i; 7133 7134 while ((token = strsep(&str, ",")) != NULL) { 7135 if (!*token) 7136 continue; 7137 7138 for_each_subsys(ss, i) { 7139 if (strcmp(token, ss->name) && 7140 strcmp(token, ss->legacy_name)) 7141 continue; 7142 7143 static_branch_disable(cgroup_subsys_enabled_key[i]); 7144 pr_info("Disabling %s control group subsystem\n", 7145 ss->name); 7146 } 7147 7148 for (i = 0; i < OPT_FEATURE_COUNT; i++) { 7149 if (strcmp(token, cgroup_opt_feature_names[i])) 7150 continue; 7151 cgroup_feature_disable_mask |= 1 << i; 7152 pr_info("Disabling %s control group feature\n", 7153 cgroup_opt_feature_names[i]); 7154 break; 7155 } 7156 } 7157 return 1; 7158 } 7159 __setup("cgroup_disable=", cgroup_disable); 7160 7161 void __init __weak enable_debug_cgroup(void) { } 7162 7163 static int __init enable_cgroup_debug(char *str) 7164 { 7165 cgroup_debug = true; 7166 enable_debug_cgroup(); 7167 return 1; 7168 } 7169 __setup("cgroup_debug", enable_cgroup_debug); 7170 7171 static int __init cgroup_favordynmods_setup(char *str) 7172 { 7173 return (kstrtobool(str, &have_favordynmods) == 0); 7174 } 7175 __setup("cgroup_favordynmods=", cgroup_favordynmods_setup); 7176 7177 /** 7178 * css_tryget_online_from_dir - get corresponding css from a cgroup dentry 7179 * @dentry: directory dentry of interest 7180 * @ss: subsystem of interest 7181 * 7182 * If @dentry is a directory for a cgroup which has @ss enabled on it, try 7183 * to get the corresponding css and return it. If such css doesn't exist 7184 * or can't be pinned, an ERR_PTR value is returned. 7185 */ 7186 struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry, 7187 struct cgroup_subsys *ss) 7188 { 7189 struct kernfs_node *kn = kernfs_node_from_dentry(dentry); 7190 struct file_system_type *s_type = dentry->d_sb->s_type; 7191 struct cgroup_subsys_state *css = NULL; 7192 struct cgroup *cgrp; 7193 7194 /* is @dentry a cgroup dir? */ 7195 if ((s_type != &cgroup_fs_type && s_type != &cgroup2_fs_type) || 7196 !kn || kernfs_type(kn) != KERNFS_DIR) 7197 return ERR_PTR(-EBADF); 7198 7199 rcu_read_lock(); 7200 7201 /* 7202 * This path doesn't originate from kernfs and @kn could already 7203 * have been or be removed at any point. @kn->priv is RCU 7204 * protected for this access. See css_release_work_fn() for details. 7205 */ 7206 cgrp = rcu_dereference(*(void __rcu __force **)&kn->priv); 7207 if (cgrp) 7208 css = cgroup_css(cgrp, ss); 7209 7210 if (!css || !css_tryget_online(css)) 7211 css = ERR_PTR(-ENOENT); 7212 7213 rcu_read_unlock(); 7214 return css; 7215 } 7216 7217 /** 7218 * css_from_id - lookup css by id 7219 * @id: the cgroup id 7220 * @ss: cgroup subsys to be looked into 7221 * 7222 * Returns the css if there's valid one with @id, otherwise returns NULL. 7223 * Should be called under rcu_read_lock(). 7224 */ 7225 struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss) 7226 { 7227 WARN_ON_ONCE(!rcu_read_lock_held()); 7228 return idr_find(&ss->css_idr, id); 7229 } 7230 7231 /** 7232 * cgroup_get_from_path - lookup and get a cgroup from its default hierarchy path 7233 * @path: path on the default hierarchy 7234 * 7235 * Find the cgroup at @path on the default hierarchy, increment its 7236 * reference count and return it. Returns pointer to the found cgroup on 7237 * success, ERR_PTR(-ENOENT) if @path doesn't exist or if the cgroup has already 7238 * been released and ERR_PTR(-ENOTDIR) if @path points to a non-directory. 7239 */ 7240 struct cgroup *cgroup_get_from_path(const char *path) 7241 { 7242 struct kernfs_node *kn; 7243 struct cgroup *cgrp = ERR_PTR(-ENOENT); 7244 struct cgroup *root_cgrp; 7245 7246 root_cgrp = current_cgns_cgroup_dfl(); 7247 kn = kernfs_walk_and_get(root_cgrp->kn, path); 7248 if (!kn) 7249 goto out; 7250 7251 if (kernfs_type(kn) != KERNFS_DIR) { 7252 cgrp = ERR_PTR(-ENOTDIR); 7253 goto out_kernfs; 7254 } 7255 7256 rcu_read_lock(); 7257 7258 cgrp = rcu_dereference(*(void __rcu __force **)&kn->priv); 7259 if (!cgrp || !cgroup_tryget(cgrp)) 7260 cgrp = ERR_PTR(-ENOENT); 7261 7262 rcu_read_unlock(); 7263 7264 out_kernfs: 7265 kernfs_put(kn); 7266 out: 7267 return cgrp; 7268 } 7269 EXPORT_SYMBOL_GPL(cgroup_get_from_path); 7270 7271 /** 7272 * cgroup_v1v2_get_from_fd - get a cgroup pointer from a fd 7273 * @fd: fd obtained by open(cgroup_dir) 7274 * 7275 * Find the cgroup from a fd which should be obtained 7276 * by opening a cgroup directory. Returns a pointer to the 7277 * cgroup on success. ERR_PTR is returned if the cgroup 7278 * cannot be found. 7279 */ 7280 struct cgroup *cgroup_v1v2_get_from_fd(int fd) 7281 { 7282 CLASS(fd_raw, f)(fd); 7283 if (fd_empty(f)) 7284 return ERR_PTR(-EBADF); 7285 7286 return cgroup_v1v2_get_from_file(fd_file(f)); 7287 } 7288 7289 /** 7290 * cgroup_get_from_fd - same as cgroup_v1v2_get_from_fd, but only supports 7291 * cgroup2. 7292 * @fd: fd obtained by open(cgroup2_dir) 7293 */ 7294 struct cgroup *cgroup_get_from_fd(int fd) 7295 { 7296 struct cgroup *cgrp = cgroup_v1v2_get_from_fd(fd); 7297 7298 if (IS_ERR(cgrp)) 7299 return ERR_CAST(cgrp); 7300 7301 if (!cgroup_on_dfl(cgrp)) { 7302 cgroup_put(cgrp); 7303 return ERR_PTR(-EBADF); 7304 } 7305 return cgrp; 7306 } 7307 EXPORT_SYMBOL_GPL(cgroup_get_from_fd); 7308 7309 static u64 power_of_ten(int power) 7310 { 7311 u64 v = 1; 7312 while (power--) 7313 v *= 10; 7314 return v; 7315 } 7316 7317 /** 7318 * cgroup_parse_float - parse a floating number 7319 * @input: input string 7320 * @dec_shift: number of decimal digits to shift 7321 * @v: output 7322 * 7323 * Parse a decimal floating point number in @input and store the result in 7324 * @v with decimal point right shifted @dec_shift times. For example, if 7325 * @input is "12.3456" and @dec_shift is 3, *@v will be set to 12345. 7326 * Returns 0 on success, -errno otherwise. 7327 * 7328 * There's nothing cgroup specific about this function except that it's 7329 * currently the only user. 7330 */ 7331 int cgroup_parse_float(const char *input, unsigned dec_shift, s64 *v) 7332 { 7333 s64 whole, frac = 0; 7334 int fstart = 0, fend = 0, flen; 7335 7336 if (!sscanf(input, "%lld.%n%lld%n", &whole, &fstart, &frac, &fend)) 7337 return -EINVAL; 7338 if (frac < 0) 7339 return -EINVAL; 7340 7341 flen = fend > fstart ? fend - fstart : 0; 7342 if (flen < dec_shift) 7343 frac *= power_of_ten(dec_shift - flen); 7344 else 7345 frac = DIV_ROUND_CLOSEST_ULL(frac, power_of_ten(flen - dec_shift)); 7346 7347 *v = whole * power_of_ten(dec_shift) + frac; 7348 return 0; 7349 } 7350 7351 /* 7352 * sock->sk_cgrp_data handling. For more info, see sock_cgroup_data 7353 * definition in cgroup-defs.h. 7354 */ 7355 #ifdef CONFIG_SOCK_CGROUP_DATA 7356 7357 void cgroup_sk_alloc(struct sock_cgroup_data *skcd) 7358 { 7359 struct cgroup *cgroup; 7360 7361 rcu_read_lock(); 7362 /* Don't associate the sock with unrelated interrupted task's cgroup. */ 7363 if (in_interrupt()) { 7364 cgroup = &cgrp_dfl_root.cgrp; 7365 cgroup_get(cgroup); 7366 goto out; 7367 } 7368 7369 while (true) { 7370 struct css_set *cset; 7371 7372 cset = task_css_set(current); 7373 if (likely(cgroup_tryget(cset->dfl_cgrp))) { 7374 cgroup = cset->dfl_cgrp; 7375 break; 7376 } 7377 cpu_relax(); 7378 } 7379 out: 7380 skcd->cgroup = cgroup; 7381 cgroup_bpf_get(cgroup); 7382 rcu_read_unlock(); 7383 } 7384 7385 void cgroup_sk_clone(struct sock_cgroup_data *skcd) 7386 { 7387 struct cgroup *cgrp = sock_cgroup_ptr(skcd); 7388 7389 /* 7390 * We might be cloning a socket which is left in an empty 7391 * cgroup and the cgroup might have already been rmdir'd. 7392 * Don't use cgroup_get_live(). 7393 */ 7394 cgroup_get(cgrp); 7395 cgroup_bpf_get(cgrp); 7396 } 7397 7398 void cgroup_sk_free(struct sock_cgroup_data *skcd) 7399 { 7400 struct cgroup *cgrp = sock_cgroup_ptr(skcd); 7401 7402 cgroup_bpf_put(cgrp); 7403 cgroup_put(cgrp); 7404 } 7405 7406 #endif /* CONFIG_SOCK_CGROUP_DATA */ 7407 7408 #ifdef CONFIG_SYSFS 7409 static ssize_t show_delegatable_files(struct cftype *files, char *buf, 7410 ssize_t size, const char *prefix) 7411 { 7412 struct cftype *cft; 7413 ssize_t ret = 0; 7414 7415 for (cft = files; cft && cft->name[0] != '\0'; cft++) { 7416 if (!(cft->flags & CFTYPE_NS_DELEGATABLE)) 7417 continue; 7418 7419 if (prefix) 7420 ret += snprintf(buf + ret, size - ret, "%s.", prefix); 7421 7422 ret += snprintf(buf + ret, size - ret, "%s\n", cft->name); 7423 7424 if (WARN_ON(ret >= size)) 7425 break; 7426 } 7427 7428 return ret; 7429 } 7430 7431 static ssize_t delegate_show(struct kobject *kobj, struct kobj_attribute *attr, 7432 char *buf) 7433 { 7434 struct cgroup_subsys *ss; 7435 int ssid; 7436 ssize_t ret = 0; 7437 7438 ret = show_delegatable_files(cgroup_base_files, buf + ret, 7439 PAGE_SIZE - ret, NULL); 7440 if (cgroup_psi_enabled()) 7441 ret += show_delegatable_files(cgroup_psi_files, buf + ret, 7442 PAGE_SIZE - ret, NULL); 7443 7444 for_each_subsys(ss, ssid) 7445 ret += show_delegatable_files(ss->dfl_cftypes, buf + ret, 7446 PAGE_SIZE - ret, 7447 cgroup_subsys_name[ssid]); 7448 7449 return ret; 7450 } 7451 static struct kobj_attribute cgroup_delegate_attr = __ATTR_RO(delegate); 7452 7453 static ssize_t features_show(struct kobject *kobj, struct kobj_attribute *attr, 7454 char *buf) 7455 { 7456 return snprintf(buf, PAGE_SIZE, 7457 "nsdelegate\n" 7458 "favordynmods\n" 7459 "memory_localevents\n" 7460 "memory_recursiveprot\n" 7461 "memory_hugetlb_accounting\n" 7462 "pids_localevents\n"); 7463 } 7464 static struct kobj_attribute cgroup_features_attr = __ATTR_RO(features); 7465 7466 static struct attribute *cgroup_sysfs_attrs[] = { 7467 &cgroup_delegate_attr.attr, 7468 &cgroup_features_attr.attr, 7469 NULL, 7470 }; 7471 7472 static const struct attribute_group cgroup_sysfs_attr_group = { 7473 .attrs = cgroup_sysfs_attrs, 7474 .name = "cgroup", 7475 }; 7476 7477 static int __init cgroup_sysfs_init(void) 7478 { 7479 return sysfs_create_group(kernel_kobj, &cgroup_sysfs_attr_group); 7480 } 7481 subsys_initcall(cgroup_sysfs_init); 7482 7483 #endif /* CONFIG_SYSFS */ 7484