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