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/cred.h> 34 #include <linux/errno.h> 35 #include <linux/init_task.h> 36 #include <linux/kernel.h> 37 #include <linux/magic.h> 38 #include <linux/mutex.h> 39 #include <linux/mount.h> 40 #include <linux/pagemap.h> 41 #include <linux/proc_fs.h> 42 #include <linux/rcupdate.h> 43 #include <linux/sched.h> 44 #include <linux/sched/task.h> 45 #include <linux/slab.h> 46 #include <linux/spinlock.h> 47 #include <linux/percpu-rwsem.h> 48 #include <linux/string.h> 49 #include <linux/hashtable.h> 50 #include <linux/idr.h> 51 #include <linux/kthread.h> 52 #include <linux/atomic.h> 53 #include <linux/cpuset.h> 54 #include <linux/proc_ns.h> 55 #include <linux/nsproxy.h> 56 #include <linux/file.h> 57 #include <linux/sched/cputime.h> 58 #include <linux/psi.h> 59 #include <net/sock.h> 60 61 #define CREATE_TRACE_POINTS 62 #include <trace/events/cgroup.h> 63 64 #define CGROUP_FILE_NAME_MAX (MAX_CGROUP_TYPE_NAMELEN + \ 65 MAX_CFTYPE_NAME + 2) 66 /* let's not notify more than 100 times per second */ 67 #define CGROUP_FILE_NOTIFY_MIN_INTV DIV_ROUND_UP(HZ, 100) 68 69 /* 70 * cgroup_mutex is the master lock. Any modification to cgroup or its 71 * hierarchy must be performed while holding it. 72 * 73 * css_set_lock protects task->cgroups pointer, the list of css_set 74 * objects, and the chain of tasks off each css_set. 75 * 76 * These locks are exported if CONFIG_PROVE_RCU so that accessors in 77 * cgroup.h can use them for lockdep annotations. 78 */ 79 DEFINE_MUTEX(cgroup_mutex); 80 DEFINE_SPINLOCK(css_set_lock); 81 82 #ifdef CONFIG_PROVE_RCU 83 EXPORT_SYMBOL_GPL(cgroup_mutex); 84 EXPORT_SYMBOL_GPL(css_set_lock); 85 #endif 86 87 DEFINE_SPINLOCK(trace_cgroup_path_lock); 88 char trace_cgroup_path[TRACE_CGROUP_PATH_LEN]; 89 bool cgroup_debug __read_mostly; 90 91 /* 92 * Protects cgroup_idr and css_idr so that IDs can be released without 93 * grabbing cgroup_mutex. 94 */ 95 static DEFINE_SPINLOCK(cgroup_idr_lock); 96 97 /* 98 * Protects cgroup_file->kn for !self csses. It synchronizes notifications 99 * against file removal/re-creation across css hiding. 100 */ 101 static DEFINE_SPINLOCK(cgroup_file_kn_lock); 102 103 struct percpu_rw_semaphore cgroup_threadgroup_rwsem; 104 105 #define cgroup_assert_mutex_or_rcu_locked() \ 106 RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \ 107 !lockdep_is_held(&cgroup_mutex), \ 108 "cgroup_mutex or RCU read lock required"); 109 110 /* 111 * cgroup destruction makes heavy use of work items and there can be a lot 112 * of concurrent destructions. Use a separate workqueue so that cgroup 113 * destruction work items don't end up filling up max_active of system_wq 114 * which may lead to deadlock. 115 */ 116 static struct workqueue_struct *cgroup_destroy_wq; 117 118 /* generate an array of cgroup subsystem pointers */ 119 #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys, 120 struct cgroup_subsys *cgroup_subsys[] = { 121 #include <linux/cgroup_subsys.h> 122 }; 123 #undef SUBSYS 124 125 /* array of cgroup subsystem names */ 126 #define SUBSYS(_x) [_x ## _cgrp_id] = #_x, 127 static const char *cgroup_subsys_name[] = { 128 #include <linux/cgroup_subsys.h> 129 }; 130 #undef SUBSYS 131 132 /* array of static_keys for cgroup_subsys_enabled() and cgroup_subsys_on_dfl() */ 133 #define SUBSYS(_x) \ 134 DEFINE_STATIC_KEY_TRUE(_x ## _cgrp_subsys_enabled_key); \ 135 DEFINE_STATIC_KEY_TRUE(_x ## _cgrp_subsys_on_dfl_key); \ 136 EXPORT_SYMBOL_GPL(_x ## _cgrp_subsys_enabled_key); \ 137 EXPORT_SYMBOL_GPL(_x ## _cgrp_subsys_on_dfl_key); 138 #include <linux/cgroup_subsys.h> 139 #undef SUBSYS 140 141 #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys_enabled_key, 142 static struct static_key_true *cgroup_subsys_enabled_key[] = { 143 #include <linux/cgroup_subsys.h> 144 }; 145 #undef SUBSYS 146 147 #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys_on_dfl_key, 148 static struct static_key_true *cgroup_subsys_on_dfl_key[] = { 149 #include <linux/cgroup_subsys.h> 150 }; 151 #undef SUBSYS 152 153 static DEFINE_PER_CPU(struct cgroup_rstat_cpu, cgrp_dfl_root_rstat_cpu); 154 155 /* 156 * The default hierarchy, reserved for the subsystems that are otherwise 157 * unattached - it never has more than a single cgroup, and all tasks are 158 * part of that cgroup. 159 */ 160 struct cgroup_root cgrp_dfl_root = { .cgrp.rstat_cpu = &cgrp_dfl_root_rstat_cpu }; 161 EXPORT_SYMBOL_GPL(cgrp_dfl_root); 162 163 /* 164 * The default hierarchy always exists but is hidden until mounted for the 165 * first time. This is for backward compatibility. 166 */ 167 static bool cgrp_dfl_visible; 168 169 /* some controllers are not supported in the default hierarchy */ 170 static u16 cgrp_dfl_inhibit_ss_mask; 171 172 /* some controllers are implicitly enabled on the default hierarchy */ 173 static u16 cgrp_dfl_implicit_ss_mask; 174 175 /* some controllers can be threaded on the default hierarchy */ 176 static u16 cgrp_dfl_threaded_ss_mask; 177 178 /* The list of hierarchy roots */ 179 LIST_HEAD(cgroup_roots); 180 static int cgroup_root_count; 181 182 /* hierarchy ID allocation and mapping, protected by cgroup_mutex */ 183 static DEFINE_IDR(cgroup_hierarchy_idr); 184 185 /* 186 * Assign a monotonically increasing serial number to csses. It guarantees 187 * cgroups with bigger numbers are newer than those with smaller numbers. 188 * Also, as csses are always appended to the parent's ->children list, it 189 * guarantees that sibling csses are always sorted in the ascending serial 190 * number order on the list. Protected by cgroup_mutex. 191 */ 192 static u64 css_serial_nr_next = 1; 193 194 /* 195 * These bitmasks identify subsystems with specific features to avoid 196 * having to do iterative checks repeatedly. 197 */ 198 static u16 have_fork_callback __read_mostly; 199 static u16 have_exit_callback __read_mostly; 200 static u16 have_free_callback __read_mostly; 201 static u16 have_canfork_callback __read_mostly; 202 203 /* cgroup namespace for init task */ 204 struct cgroup_namespace init_cgroup_ns = { 205 .count = REFCOUNT_INIT(2), 206 .user_ns = &init_user_ns, 207 .ns.ops = &cgroupns_operations, 208 .ns.inum = PROC_CGROUP_INIT_INO, 209 .root_cset = &init_css_set, 210 }; 211 212 static struct file_system_type cgroup2_fs_type; 213 static struct cftype cgroup_base_files[]; 214 215 static int cgroup_apply_control(struct cgroup *cgrp); 216 static void cgroup_finalize_control(struct cgroup *cgrp, int ret); 217 static void css_task_iter_advance(struct css_task_iter *it); 218 static int cgroup_destroy_locked(struct cgroup *cgrp); 219 static struct cgroup_subsys_state *css_create(struct cgroup *cgrp, 220 struct cgroup_subsys *ss); 221 static void css_release(struct percpu_ref *ref); 222 static void kill_css(struct cgroup_subsys_state *css); 223 static int cgroup_addrm_files(struct cgroup_subsys_state *css, 224 struct cgroup *cgrp, struct cftype cfts[], 225 bool is_add); 226 227 /** 228 * cgroup_ssid_enabled - cgroup subsys enabled test by subsys ID 229 * @ssid: subsys ID of interest 230 * 231 * cgroup_subsys_enabled() can only be used with literal subsys names which 232 * is fine for individual subsystems but unsuitable for cgroup core. This 233 * is slower static_key_enabled() based test indexed by @ssid. 234 */ 235 bool cgroup_ssid_enabled(int ssid) 236 { 237 if (CGROUP_SUBSYS_COUNT == 0) 238 return false; 239 240 return static_key_enabled(cgroup_subsys_enabled_key[ssid]); 241 } 242 243 /** 244 * cgroup_on_dfl - test whether a cgroup is on the default hierarchy 245 * @cgrp: the cgroup of interest 246 * 247 * The default hierarchy is the v2 interface of cgroup and this function 248 * can be used to test whether a cgroup is on the default hierarchy for 249 * cases where a subsystem should behave differnetly depending on the 250 * interface version. 251 * 252 * The set of behaviors which change on the default hierarchy are still 253 * being determined and the mount option is prefixed with __DEVEL__. 254 * 255 * List of changed behaviors: 256 * 257 * - Mount options "noprefix", "xattr", "clone_children", "release_agent" 258 * and "name" are disallowed. 259 * 260 * - When mounting an existing superblock, mount options should match. 261 * 262 * - Remount is disallowed. 263 * 264 * - rename(2) is disallowed. 265 * 266 * - "tasks" is removed. Everything should be at process granularity. Use 267 * "cgroup.procs" instead. 268 * 269 * - "cgroup.procs" is not sorted. pids will be unique unless they got 270 * recycled inbetween reads. 271 * 272 * - "release_agent" and "notify_on_release" are removed. Replacement 273 * notification mechanism will be implemented. 274 * 275 * - "cgroup.clone_children" is removed. 276 * 277 * - "cgroup.subtree_populated" is available. Its value is 0 if the cgroup 278 * and its descendants contain no task; otherwise, 1. The file also 279 * generates kernfs notification which can be monitored through poll and 280 * [di]notify when the value of the file changes. 281 * 282 * - cpuset: tasks will be kept in empty cpusets when hotplug happens and 283 * take masks of ancestors with non-empty cpus/mems, instead of being 284 * moved to an ancestor. 285 * 286 * - cpuset: a task can be moved into an empty cpuset, and again it takes 287 * masks of ancestors. 288 * 289 * - memcg: use_hierarchy is on by default and the cgroup file for the flag 290 * is not created. 291 * 292 * - blkcg: blk-throttle becomes properly hierarchical. 293 * 294 * - debug: disallowed on the default hierarchy. 295 */ 296 bool cgroup_on_dfl(const struct cgroup *cgrp) 297 { 298 return cgrp->root == &cgrp_dfl_root; 299 } 300 301 /* IDR wrappers which synchronize using cgroup_idr_lock */ 302 static int cgroup_idr_alloc(struct idr *idr, void *ptr, int start, int end, 303 gfp_t gfp_mask) 304 { 305 int ret; 306 307 idr_preload(gfp_mask); 308 spin_lock_bh(&cgroup_idr_lock); 309 ret = idr_alloc(idr, ptr, start, end, gfp_mask & ~__GFP_DIRECT_RECLAIM); 310 spin_unlock_bh(&cgroup_idr_lock); 311 idr_preload_end(); 312 return ret; 313 } 314 315 static void *cgroup_idr_replace(struct idr *idr, void *ptr, int id) 316 { 317 void *ret; 318 319 spin_lock_bh(&cgroup_idr_lock); 320 ret = idr_replace(idr, ptr, id); 321 spin_unlock_bh(&cgroup_idr_lock); 322 return ret; 323 } 324 325 static void cgroup_idr_remove(struct idr *idr, int id) 326 { 327 spin_lock_bh(&cgroup_idr_lock); 328 idr_remove(idr, id); 329 spin_unlock_bh(&cgroup_idr_lock); 330 } 331 332 static bool cgroup_has_tasks(struct cgroup *cgrp) 333 { 334 return cgrp->nr_populated_csets; 335 } 336 337 bool cgroup_is_threaded(struct cgroup *cgrp) 338 { 339 return cgrp->dom_cgrp != cgrp; 340 } 341 342 /* can @cgrp host both domain and threaded children? */ 343 static bool cgroup_is_mixable(struct cgroup *cgrp) 344 { 345 /* 346 * Root isn't under domain level resource control exempting it from 347 * the no-internal-process constraint, so it can serve as a thread 348 * root and a parent of resource domains at the same time. 349 */ 350 return !cgroup_parent(cgrp); 351 } 352 353 /* can @cgrp become a thread root? should always be true for a thread root */ 354 static bool cgroup_can_be_thread_root(struct cgroup *cgrp) 355 { 356 /* mixables don't care */ 357 if (cgroup_is_mixable(cgrp)) 358 return true; 359 360 /* domain roots can't be nested under threaded */ 361 if (cgroup_is_threaded(cgrp)) 362 return false; 363 364 /* can only have either domain or threaded children */ 365 if (cgrp->nr_populated_domain_children) 366 return false; 367 368 /* and no domain controllers can be enabled */ 369 if (cgrp->subtree_control & ~cgrp_dfl_threaded_ss_mask) 370 return false; 371 372 return true; 373 } 374 375 /* is @cgrp root of a threaded subtree? */ 376 bool cgroup_is_thread_root(struct cgroup *cgrp) 377 { 378 /* thread root should be a domain */ 379 if (cgroup_is_threaded(cgrp)) 380 return false; 381 382 /* a domain w/ threaded children is a thread root */ 383 if (cgrp->nr_threaded_children) 384 return true; 385 386 /* 387 * A domain which has tasks and explicit threaded controllers 388 * enabled is a thread root. 389 */ 390 if (cgroup_has_tasks(cgrp) && 391 (cgrp->subtree_control & cgrp_dfl_threaded_ss_mask)) 392 return true; 393 394 return false; 395 } 396 397 /* a domain which isn't connected to the root w/o brekage can't be used */ 398 static bool cgroup_is_valid_domain(struct cgroup *cgrp) 399 { 400 /* the cgroup itself can be a thread root */ 401 if (cgroup_is_threaded(cgrp)) 402 return false; 403 404 /* but the ancestors can't be unless mixable */ 405 while ((cgrp = cgroup_parent(cgrp))) { 406 if (!cgroup_is_mixable(cgrp) && cgroup_is_thread_root(cgrp)) 407 return false; 408 if (cgroup_is_threaded(cgrp)) 409 return false; 410 } 411 412 return true; 413 } 414 415 /* subsystems visibly enabled on a cgroup */ 416 static u16 cgroup_control(struct cgroup *cgrp) 417 { 418 struct cgroup *parent = cgroup_parent(cgrp); 419 u16 root_ss_mask = cgrp->root->subsys_mask; 420 421 if (parent) { 422 u16 ss_mask = parent->subtree_control; 423 424 /* threaded cgroups can only have threaded controllers */ 425 if (cgroup_is_threaded(cgrp)) 426 ss_mask &= cgrp_dfl_threaded_ss_mask; 427 return ss_mask; 428 } 429 430 if (cgroup_on_dfl(cgrp)) 431 root_ss_mask &= ~(cgrp_dfl_inhibit_ss_mask | 432 cgrp_dfl_implicit_ss_mask); 433 return root_ss_mask; 434 } 435 436 /* subsystems enabled on a cgroup */ 437 static u16 cgroup_ss_mask(struct cgroup *cgrp) 438 { 439 struct cgroup *parent = cgroup_parent(cgrp); 440 441 if (parent) { 442 u16 ss_mask = parent->subtree_ss_mask; 443 444 /* threaded cgroups can only have threaded controllers */ 445 if (cgroup_is_threaded(cgrp)) 446 ss_mask &= cgrp_dfl_threaded_ss_mask; 447 return ss_mask; 448 } 449 450 return cgrp->root->subsys_mask; 451 } 452 453 /** 454 * cgroup_css - obtain a cgroup's css for the specified subsystem 455 * @cgrp: the cgroup of interest 456 * @ss: the subsystem of interest (%NULL returns @cgrp->self) 457 * 458 * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This 459 * function must be called either under cgroup_mutex or rcu_read_lock() and 460 * the caller is responsible for pinning the returned css if it wants to 461 * keep accessing it outside the said locks. This function may return 462 * %NULL if @cgrp doesn't have @subsys_id enabled. 463 */ 464 static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp, 465 struct cgroup_subsys *ss) 466 { 467 if (ss) 468 return rcu_dereference_check(cgrp->subsys[ss->id], 469 lockdep_is_held(&cgroup_mutex)); 470 else 471 return &cgrp->self; 472 } 473 474 /** 475 * cgroup_tryget_css - try to get a cgroup's css for the specified subsystem 476 * @cgrp: the cgroup of interest 477 * @ss: the subsystem of interest 478 * 479 * Find and get @cgrp's css assocaited with @ss. If the css doesn't exist 480 * or is offline, %NULL is returned. 481 */ 482 static struct cgroup_subsys_state *cgroup_tryget_css(struct cgroup *cgrp, 483 struct cgroup_subsys *ss) 484 { 485 struct cgroup_subsys_state *css; 486 487 rcu_read_lock(); 488 css = cgroup_css(cgrp, ss); 489 if (!css || !css_tryget_online(css)) 490 css = NULL; 491 rcu_read_unlock(); 492 493 return css; 494 } 495 496 /** 497 * cgroup_e_css_by_mask - obtain a cgroup's effective css for the specified ss 498 * @cgrp: the cgroup of interest 499 * @ss: the subsystem of interest (%NULL returns @cgrp->self) 500 * 501 * Similar to cgroup_css() but returns the effective css, which is defined 502 * as the matching css of the nearest ancestor including self which has @ss 503 * enabled. If @ss is associated with the hierarchy @cgrp is on, this 504 * function is guaranteed to return non-NULL css. 505 */ 506 static struct cgroup_subsys_state *cgroup_e_css_by_mask(struct cgroup *cgrp, 507 struct cgroup_subsys *ss) 508 { 509 lockdep_assert_held(&cgroup_mutex); 510 511 if (!ss) 512 return &cgrp->self; 513 514 /* 515 * This function is used while updating css associations and thus 516 * can't test the csses directly. Test ss_mask. 517 */ 518 while (!(cgroup_ss_mask(cgrp) & (1 << ss->id))) { 519 cgrp = cgroup_parent(cgrp); 520 if (!cgrp) 521 return NULL; 522 } 523 524 return cgroup_css(cgrp, ss); 525 } 526 527 /** 528 * cgroup_e_css - obtain a cgroup's effective css for the specified subsystem 529 * @cgrp: the cgroup of interest 530 * @ss: the subsystem of interest 531 * 532 * Find and get the effective css of @cgrp for @ss. The effective css is 533 * defined as the matching css of the nearest ancestor including self which 534 * has @ss enabled. If @ss is not mounted on the hierarchy @cgrp is on, 535 * the root css is returned, so this function always returns a valid css. 536 * 537 * The returned css is not guaranteed to be online, and therefore it is the 538 * callers responsiblity to tryget a reference for it. 539 */ 540 struct cgroup_subsys_state *cgroup_e_css(struct cgroup *cgrp, 541 struct cgroup_subsys *ss) 542 { 543 struct cgroup_subsys_state *css; 544 545 do { 546 css = cgroup_css(cgrp, ss); 547 548 if (css) 549 return css; 550 cgrp = cgroup_parent(cgrp); 551 } while (cgrp); 552 553 return init_css_set.subsys[ss->id]; 554 } 555 556 /** 557 * cgroup_get_e_css - get a cgroup's effective css for the specified subsystem 558 * @cgrp: the cgroup of interest 559 * @ss: the subsystem of interest 560 * 561 * Find and get the effective css of @cgrp for @ss. The effective css is 562 * defined as the matching css of the nearest ancestor including self which 563 * has @ss enabled. If @ss is not mounted on the hierarchy @cgrp is on, 564 * the root css is returned, so this function always returns a valid css. 565 * The returned css must be put using css_put(). 566 */ 567 struct cgroup_subsys_state *cgroup_get_e_css(struct cgroup *cgrp, 568 struct cgroup_subsys *ss) 569 { 570 struct cgroup_subsys_state *css; 571 572 rcu_read_lock(); 573 574 do { 575 css = cgroup_css(cgrp, ss); 576 577 if (css && css_tryget_online(css)) 578 goto out_unlock; 579 cgrp = cgroup_parent(cgrp); 580 } while (cgrp); 581 582 css = init_css_set.subsys[ss->id]; 583 css_get(css); 584 out_unlock: 585 rcu_read_unlock(); 586 return css; 587 } 588 589 static void cgroup_get_live(struct cgroup *cgrp) 590 { 591 WARN_ON_ONCE(cgroup_is_dead(cgrp)); 592 css_get(&cgrp->self); 593 } 594 595 struct cgroup_subsys_state *of_css(struct kernfs_open_file *of) 596 { 597 struct cgroup *cgrp = of->kn->parent->priv; 598 struct cftype *cft = of_cft(of); 599 600 /* 601 * This is open and unprotected implementation of cgroup_css(). 602 * seq_css() is only called from a kernfs file operation which has 603 * an active reference on the file. Because all the subsystem 604 * files are drained before a css is disassociated with a cgroup, 605 * the matching css from the cgroup's subsys table is guaranteed to 606 * be and stay valid until the enclosing operation is complete. 607 */ 608 if (cft->ss) 609 return rcu_dereference_raw(cgrp->subsys[cft->ss->id]); 610 else 611 return &cgrp->self; 612 } 613 EXPORT_SYMBOL_GPL(of_css); 614 615 /** 616 * for_each_css - iterate all css's of a cgroup 617 * @css: the iteration cursor 618 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end 619 * @cgrp: the target cgroup to iterate css's of 620 * 621 * Should be called under cgroup_[tree_]mutex. 622 */ 623 #define for_each_css(css, ssid, cgrp) \ 624 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \ 625 if (!((css) = rcu_dereference_check( \ 626 (cgrp)->subsys[(ssid)], \ 627 lockdep_is_held(&cgroup_mutex)))) { } \ 628 else 629 630 /** 631 * for_each_e_css - iterate all effective css's of a cgroup 632 * @css: the iteration cursor 633 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end 634 * @cgrp: the target cgroup to iterate css's of 635 * 636 * Should be called under cgroup_[tree_]mutex. 637 */ 638 #define for_each_e_css(css, ssid, cgrp) \ 639 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \ 640 if (!((css) = cgroup_e_css_by_mask(cgrp, \ 641 cgroup_subsys[(ssid)]))) \ 642 ; \ 643 else 644 645 /** 646 * do_each_subsys_mask - filter for_each_subsys with a bitmask 647 * @ss: the iteration cursor 648 * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end 649 * @ss_mask: the bitmask 650 * 651 * The block will only run for cases where the ssid-th bit (1 << ssid) of 652 * @ss_mask is set. 653 */ 654 #define do_each_subsys_mask(ss, ssid, ss_mask) do { \ 655 unsigned long __ss_mask = (ss_mask); \ 656 if (!CGROUP_SUBSYS_COUNT) { /* to avoid spurious gcc warning */ \ 657 (ssid) = 0; \ 658 break; \ 659 } \ 660 for_each_set_bit(ssid, &__ss_mask, CGROUP_SUBSYS_COUNT) { \ 661 (ss) = cgroup_subsys[ssid]; \ 662 { 663 664 #define while_each_subsys_mask() \ 665 } \ 666 } \ 667 } while (false) 668 669 /* iterate over child cgrps, lock should be held throughout iteration */ 670 #define cgroup_for_each_live_child(child, cgrp) \ 671 list_for_each_entry((child), &(cgrp)->self.children, self.sibling) \ 672 if (({ lockdep_assert_held(&cgroup_mutex); \ 673 cgroup_is_dead(child); })) \ 674 ; \ 675 else 676 677 /* walk live descendants in preorder */ 678 #define cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) \ 679 css_for_each_descendant_pre((d_css), cgroup_css((cgrp), NULL)) \ 680 if (({ lockdep_assert_held(&cgroup_mutex); \ 681 (dsct) = (d_css)->cgroup; \ 682 cgroup_is_dead(dsct); })) \ 683 ; \ 684 else 685 686 /* walk live descendants in postorder */ 687 #define cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) \ 688 css_for_each_descendant_post((d_css), cgroup_css((cgrp), NULL)) \ 689 if (({ lockdep_assert_held(&cgroup_mutex); \ 690 (dsct) = (d_css)->cgroup; \ 691 cgroup_is_dead(dsct); })) \ 692 ; \ 693 else 694 695 /* 696 * The default css_set - used by init and its children prior to any 697 * hierarchies being mounted. It contains a pointer to the root state 698 * for each subsystem. Also used to anchor the list of css_sets. Not 699 * reference-counted, to improve performance when child cgroups 700 * haven't been created. 701 */ 702 struct css_set init_css_set = { 703 .refcount = REFCOUNT_INIT(1), 704 .dom_cset = &init_css_set, 705 .tasks = LIST_HEAD_INIT(init_css_set.tasks), 706 .mg_tasks = LIST_HEAD_INIT(init_css_set.mg_tasks), 707 .task_iters = LIST_HEAD_INIT(init_css_set.task_iters), 708 .threaded_csets = LIST_HEAD_INIT(init_css_set.threaded_csets), 709 .cgrp_links = LIST_HEAD_INIT(init_css_set.cgrp_links), 710 .mg_preload_node = LIST_HEAD_INIT(init_css_set.mg_preload_node), 711 .mg_node = LIST_HEAD_INIT(init_css_set.mg_node), 712 713 /* 714 * The following field is re-initialized when this cset gets linked 715 * in cgroup_init(). However, let's initialize the field 716 * statically too so that the default cgroup can be accessed safely 717 * early during boot. 718 */ 719 .dfl_cgrp = &cgrp_dfl_root.cgrp, 720 }; 721 722 static int css_set_count = 1; /* 1 for init_css_set */ 723 724 static bool css_set_threaded(struct css_set *cset) 725 { 726 return cset->dom_cset != cset; 727 } 728 729 /** 730 * css_set_populated - does a css_set contain any tasks? 731 * @cset: target css_set 732 * 733 * css_set_populated() should be the same as !!cset->nr_tasks at steady 734 * state. However, css_set_populated() can be called while a task is being 735 * added to or removed from the linked list before the nr_tasks is 736 * properly updated. Hence, we can't just look at ->nr_tasks here. 737 */ 738 static bool css_set_populated(struct css_set *cset) 739 { 740 lockdep_assert_held(&css_set_lock); 741 742 return !list_empty(&cset->tasks) || !list_empty(&cset->mg_tasks); 743 } 744 745 /** 746 * cgroup_update_populated - update the populated count of a cgroup 747 * @cgrp: the target cgroup 748 * @populated: inc or dec populated count 749 * 750 * One of the css_sets associated with @cgrp is either getting its first 751 * task or losing the last. Update @cgrp->nr_populated_* accordingly. The 752 * count is propagated towards root so that a given cgroup's 753 * nr_populated_children is zero iff none of its descendants contain any 754 * tasks. 755 * 756 * @cgrp's interface file "cgroup.populated" is zero if both 757 * @cgrp->nr_populated_csets and @cgrp->nr_populated_children are zero and 758 * 1 otherwise. When the sum changes from or to zero, userland is notified 759 * that the content of the interface file has changed. This can be used to 760 * detect when @cgrp and its descendants become populated or empty. 761 */ 762 static void cgroup_update_populated(struct cgroup *cgrp, bool populated) 763 { 764 struct cgroup *child = NULL; 765 int adj = populated ? 1 : -1; 766 767 lockdep_assert_held(&css_set_lock); 768 769 do { 770 bool was_populated = cgroup_is_populated(cgrp); 771 772 if (!child) { 773 cgrp->nr_populated_csets += adj; 774 } else { 775 if (cgroup_is_threaded(child)) 776 cgrp->nr_populated_threaded_children += adj; 777 else 778 cgrp->nr_populated_domain_children += adj; 779 } 780 781 if (was_populated == cgroup_is_populated(cgrp)) 782 break; 783 784 cgroup1_check_for_release(cgrp); 785 cgroup_file_notify(&cgrp->events_file); 786 787 child = cgrp; 788 cgrp = cgroup_parent(cgrp); 789 } while (cgrp); 790 } 791 792 /** 793 * css_set_update_populated - update populated state of a css_set 794 * @cset: target css_set 795 * @populated: whether @cset is populated or depopulated 796 * 797 * @cset is either getting the first task or losing the last. Update the 798 * populated counters of all associated cgroups accordingly. 799 */ 800 static void css_set_update_populated(struct css_set *cset, bool populated) 801 { 802 struct cgrp_cset_link *link; 803 804 lockdep_assert_held(&css_set_lock); 805 806 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) 807 cgroup_update_populated(link->cgrp, populated); 808 } 809 810 /** 811 * css_set_move_task - move a task from one css_set to another 812 * @task: task being moved 813 * @from_cset: css_set @task currently belongs to (may be NULL) 814 * @to_cset: new css_set @task is being moved to (may be NULL) 815 * @use_mg_tasks: move to @to_cset->mg_tasks instead of ->tasks 816 * 817 * Move @task from @from_cset to @to_cset. If @task didn't belong to any 818 * css_set, @from_cset can be NULL. If @task is being disassociated 819 * instead of moved, @to_cset can be NULL. 820 * 821 * This function automatically handles populated counter updates and 822 * css_task_iter adjustments but the caller is responsible for managing 823 * @from_cset and @to_cset's reference counts. 824 */ 825 static void css_set_move_task(struct task_struct *task, 826 struct css_set *from_cset, struct css_set *to_cset, 827 bool use_mg_tasks) 828 { 829 lockdep_assert_held(&css_set_lock); 830 831 if (to_cset && !css_set_populated(to_cset)) 832 css_set_update_populated(to_cset, true); 833 834 if (from_cset) { 835 struct css_task_iter *it, *pos; 836 837 WARN_ON_ONCE(list_empty(&task->cg_list)); 838 839 /* 840 * @task is leaving, advance task iterators which are 841 * pointing to it so that they can resume at the next 842 * position. Advancing an iterator might remove it from 843 * the list, use safe walk. See css_task_iter_advance*() 844 * for details. 845 */ 846 list_for_each_entry_safe(it, pos, &from_cset->task_iters, 847 iters_node) 848 if (it->task_pos == &task->cg_list) 849 css_task_iter_advance(it); 850 851 list_del_init(&task->cg_list); 852 if (!css_set_populated(from_cset)) 853 css_set_update_populated(from_cset, false); 854 } else { 855 WARN_ON_ONCE(!list_empty(&task->cg_list)); 856 } 857 858 if (to_cset) { 859 /* 860 * We are synchronized through cgroup_threadgroup_rwsem 861 * against PF_EXITING setting such that we can't race 862 * against cgroup_exit() changing the css_set to 863 * init_css_set and dropping the old one. 864 */ 865 WARN_ON_ONCE(task->flags & PF_EXITING); 866 867 cgroup_move_task(task, to_cset); 868 list_add_tail(&task->cg_list, use_mg_tasks ? &to_cset->mg_tasks : 869 &to_cset->tasks); 870 } 871 } 872 873 /* 874 * hash table for cgroup groups. This improves the performance to find 875 * an existing css_set. This hash doesn't (currently) take into 876 * account cgroups in empty hierarchies. 877 */ 878 #define CSS_SET_HASH_BITS 7 879 static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS); 880 881 static unsigned long css_set_hash(struct cgroup_subsys_state *css[]) 882 { 883 unsigned long key = 0UL; 884 struct cgroup_subsys *ss; 885 int i; 886 887 for_each_subsys(ss, i) 888 key += (unsigned long)css[i]; 889 key = (key >> 16) ^ key; 890 891 return key; 892 } 893 894 void put_css_set_locked(struct css_set *cset) 895 { 896 struct cgrp_cset_link *link, *tmp_link; 897 struct cgroup_subsys *ss; 898 int ssid; 899 900 lockdep_assert_held(&css_set_lock); 901 902 if (!refcount_dec_and_test(&cset->refcount)) 903 return; 904 905 WARN_ON_ONCE(!list_empty(&cset->threaded_csets)); 906 907 /* This css_set is dead. unlink it and release cgroup and css refs */ 908 for_each_subsys(ss, ssid) { 909 list_del(&cset->e_cset_node[ssid]); 910 css_put(cset->subsys[ssid]); 911 } 912 hash_del(&cset->hlist); 913 css_set_count--; 914 915 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) { 916 list_del(&link->cset_link); 917 list_del(&link->cgrp_link); 918 if (cgroup_parent(link->cgrp)) 919 cgroup_put(link->cgrp); 920 kfree(link); 921 } 922 923 if (css_set_threaded(cset)) { 924 list_del(&cset->threaded_csets_node); 925 put_css_set_locked(cset->dom_cset); 926 } 927 928 kfree_rcu(cset, rcu_head); 929 } 930 931 /** 932 * compare_css_sets - helper function for find_existing_css_set(). 933 * @cset: candidate css_set being tested 934 * @old_cset: existing css_set for a task 935 * @new_cgrp: cgroup that's being entered by the task 936 * @template: desired set of css pointers in css_set (pre-calculated) 937 * 938 * Returns true if "cset" matches "old_cset" except for the hierarchy 939 * which "new_cgrp" belongs to, for which it should match "new_cgrp". 940 */ 941 static bool compare_css_sets(struct css_set *cset, 942 struct css_set *old_cset, 943 struct cgroup *new_cgrp, 944 struct cgroup_subsys_state *template[]) 945 { 946 struct cgroup *new_dfl_cgrp; 947 struct list_head *l1, *l2; 948 949 /* 950 * On the default hierarchy, there can be csets which are 951 * associated with the same set of cgroups but different csses. 952 * Let's first ensure that csses match. 953 */ 954 if (memcmp(template, cset->subsys, sizeof(cset->subsys))) 955 return false; 956 957 958 /* @cset's domain should match the default cgroup's */ 959 if (cgroup_on_dfl(new_cgrp)) 960 new_dfl_cgrp = new_cgrp; 961 else 962 new_dfl_cgrp = old_cset->dfl_cgrp; 963 964 if (new_dfl_cgrp->dom_cgrp != cset->dom_cset->dfl_cgrp) 965 return false; 966 967 /* 968 * Compare cgroup pointers in order to distinguish between 969 * different cgroups in hierarchies. As different cgroups may 970 * share the same effective css, this comparison is always 971 * necessary. 972 */ 973 l1 = &cset->cgrp_links; 974 l2 = &old_cset->cgrp_links; 975 while (1) { 976 struct cgrp_cset_link *link1, *link2; 977 struct cgroup *cgrp1, *cgrp2; 978 979 l1 = l1->next; 980 l2 = l2->next; 981 /* See if we reached the end - both lists are equal length. */ 982 if (l1 == &cset->cgrp_links) { 983 BUG_ON(l2 != &old_cset->cgrp_links); 984 break; 985 } else { 986 BUG_ON(l2 == &old_cset->cgrp_links); 987 } 988 /* Locate the cgroups associated with these links. */ 989 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link); 990 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link); 991 cgrp1 = link1->cgrp; 992 cgrp2 = link2->cgrp; 993 /* Hierarchies should be linked in the same order. */ 994 BUG_ON(cgrp1->root != cgrp2->root); 995 996 /* 997 * If this hierarchy is the hierarchy of the cgroup 998 * that's changing, then we need to check that this 999 * css_set points to the new cgroup; if it's any other 1000 * hierarchy, then this css_set should point to the 1001 * same cgroup as the old css_set. 1002 */ 1003 if (cgrp1->root == new_cgrp->root) { 1004 if (cgrp1 != new_cgrp) 1005 return false; 1006 } else { 1007 if (cgrp1 != cgrp2) 1008 return false; 1009 } 1010 } 1011 return true; 1012 } 1013 1014 /** 1015 * find_existing_css_set - init css array and find the matching css_set 1016 * @old_cset: the css_set that we're using before the cgroup transition 1017 * @cgrp: the cgroup that we're moving into 1018 * @template: out param for the new set of csses, should be clear on entry 1019 */ 1020 static struct css_set *find_existing_css_set(struct css_set *old_cset, 1021 struct cgroup *cgrp, 1022 struct cgroup_subsys_state *template[]) 1023 { 1024 struct cgroup_root *root = cgrp->root; 1025 struct cgroup_subsys *ss; 1026 struct css_set *cset; 1027 unsigned long key; 1028 int i; 1029 1030 /* 1031 * Build the set of subsystem state objects that we want to see in the 1032 * new css_set. while subsystems can change globally, the entries here 1033 * won't change, so no need for locking. 1034 */ 1035 for_each_subsys(ss, i) { 1036 if (root->subsys_mask & (1UL << i)) { 1037 /* 1038 * @ss is in this hierarchy, so we want the 1039 * effective css from @cgrp. 1040 */ 1041 template[i] = cgroup_e_css_by_mask(cgrp, ss); 1042 } else { 1043 /* 1044 * @ss is not in this hierarchy, so we don't want 1045 * to change the css. 1046 */ 1047 template[i] = old_cset->subsys[i]; 1048 } 1049 } 1050 1051 key = css_set_hash(template); 1052 hash_for_each_possible(css_set_table, cset, hlist, key) { 1053 if (!compare_css_sets(cset, old_cset, cgrp, template)) 1054 continue; 1055 1056 /* This css_set matches what we need */ 1057 return cset; 1058 } 1059 1060 /* No existing cgroup group matched */ 1061 return NULL; 1062 } 1063 1064 static void free_cgrp_cset_links(struct list_head *links_to_free) 1065 { 1066 struct cgrp_cset_link *link, *tmp_link; 1067 1068 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) { 1069 list_del(&link->cset_link); 1070 kfree(link); 1071 } 1072 } 1073 1074 /** 1075 * allocate_cgrp_cset_links - allocate cgrp_cset_links 1076 * @count: the number of links to allocate 1077 * @tmp_links: list_head the allocated links are put on 1078 * 1079 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links 1080 * through ->cset_link. Returns 0 on success or -errno. 1081 */ 1082 static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links) 1083 { 1084 struct cgrp_cset_link *link; 1085 int i; 1086 1087 INIT_LIST_HEAD(tmp_links); 1088 1089 for (i = 0; i < count; i++) { 1090 link = kzalloc(sizeof(*link), GFP_KERNEL); 1091 if (!link) { 1092 free_cgrp_cset_links(tmp_links); 1093 return -ENOMEM; 1094 } 1095 list_add(&link->cset_link, tmp_links); 1096 } 1097 return 0; 1098 } 1099 1100 /** 1101 * link_css_set - a helper function to link a css_set to a cgroup 1102 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links() 1103 * @cset: the css_set to be linked 1104 * @cgrp: the destination cgroup 1105 */ 1106 static void link_css_set(struct list_head *tmp_links, struct css_set *cset, 1107 struct cgroup *cgrp) 1108 { 1109 struct cgrp_cset_link *link; 1110 1111 BUG_ON(list_empty(tmp_links)); 1112 1113 if (cgroup_on_dfl(cgrp)) 1114 cset->dfl_cgrp = cgrp; 1115 1116 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link); 1117 link->cset = cset; 1118 link->cgrp = cgrp; 1119 1120 /* 1121 * Always add links to the tail of the lists so that the lists are 1122 * in choronological order. 1123 */ 1124 list_move_tail(&link->cset_link, &cgrp->cset_links); 1125 list_add_tail(&link->cgrp_link, &cset->cgrp_links); 1126 1127 if (cgroup_parent(cgrp)) 1128 cgroup_get_live(cgrp); 1129 } 1130 1131 /** 1132 * find_css_set - return a new css_set with one cgroup updated 1133 * @old_cset: the baseline css_set 1134 * @cgrp: the cgroup to be updated 1135 * 1136 * Return a new css_set that's equivalent to @old_cset, but with @cgrp 1137 * substituted into the appropriate hierarchy. 1138 */ 1139 static struct css_set *find_css_set(struct css_set *old_cset, 1140 struct cgroup *cgrp) 1141 { 1142 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { }; 1143 struct css_set *cset; 1144 struct list_head tmp_links; 1145 struct cgrp_cset_link *link; 1146 struct cgroup_subsys *ss; 1147 unsigned long key; 1148 int ssid; 1149 1150 lockdep_assert_held(&cgroup_mutex); 1151 1152 /* First see if we already have a cgroup group that matches 1153 * the desired set */ 1154 spin_lock_irq(&css_set_lock); 1155 cset = find_existing_css_set(old_cset, cgrp, template); 1156 if (cset) 1157 get_css_set(cset); 1158 spin_unlock_irq(&css_set_lock); 1159 1160 if (cset) 1161 return cset; 1162 1163 cset = kzalloc(sizeof(*cset), GFP_KERNEL); 1164 if (!cset) 1165 return NULL; 1166 1167 /* Allocate all the cgrp_cset_link objects that we'll need */ 1168 if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) { 1169 kfree(cset); 1170 return NULL; 1171 } 1172 1173 refcount_set(&cset->refcount, 1); 1174 cset->dom_cset = cset; 1175 INIT_LIST_HEAD(&cset->tasks); 1176 INIT_LIST_HEAD(&cset->mg_tasks); 1177 INIT_LIST_HEAD(&cset->task_iters); 1178 INIT_LIST_HEAD(&cset->threaded_csets); 1179 INIT_HLIST_NODE(&cset->hlist); 1180 INIT_LIST_HEAD(&cset->cgrp_links); 1181 INIT_LIST_HEAD(&cset->mg_preload_node); 1182 INIT_LIST_HEAD(&cset->mg_node); 1183 1184 /* Copy the set of subsystem state objects generated in 1185 * find_existing_css_set() */ 1186 memcpy(cset->subsys, template, sizeof(cset->subsys)); 1187 1188 spin_lock_irq(&css_set_lock); 1189 /* Add reference counts and links from the new css_set. */ 1190 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) { 1191 struct cgroup *c = link->cgrp; 1192 1193 if (c->root == cgrp->root) 1194 c = cgrp; 1195 link_css_set(&tmp_links, cset, c); 1196 } 1197 1198 BUG_ON(!list_empty(&tmp_links)); 1199 1200 css_set_count++; 1201 1202 /* Add @cset to the hash table */ 1203 key = css_set_hash(cset->subsys); 1204 hash_add(css_set_table, &cset->hlist, key); 1205 1206 for_each_subsys(ss, ssid) { 1207 struct cgroup_subsys_state *css = cset->subsys[ssid]; 1208 1209 list_add_tail(&cset->e_cset_node[ssid], 1210 &css->cgroup->e_csets[ssid]); 1211 css_get(css); 1212 } 1213 1214 spin_unlock_irq(&css_set_lock); 1215 1216 /* 1217 * If @cset should be threaded, look up the matching dom_cset and 1218 * link them up. We first fully initialize @cset then look for the 1219 * dom_cset. It's simpler this way and safe as @cset is guaranteed 1220 * to stay empty until we return. 1221 */ 1222 if (cgroup_is_threaded(cset->dfl_cgrp)) { 1223 struct css_set *dcset; 1224 1225 dcset = find_css_set(cset, cset->dfl_cgrp->dom_cgrp); 1226 if (!dcset) { 1227 put_css_set(cset); 1228 return NULL; 1229 } 1230 1231 spin_lock_irq(&css_set_lock); 1232 cset->dom_cset = dcset; 1233 list_add_tail(&cset->threaded_csets_node, 1234 &dcset->threaded_csets); 1235 spin_unlock_irq(&css_set_lock); 1236 } 1237 1238 return cset; 1239 } 1240 1241 struct cgroup_root *cgroup_root_from_kf(struct kernfs_root *kf_root) 1242 { 1243 struct cgroup *root_cgrp = kf_root->kn->priv; 1244 1245 return root_cgrp->root; 1246 } 1247 1248 static int cgroup_init_root_id(struct cgroup_root *root) 1249 { 1250 int id; 1251 1252 lockdep_assert_held(&cgroup_mutex); 1253 1254 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, 0, 0, GFP_KERNEL); 1255 if (id < 0) 1256 return id; 1257 1258 root->hierarchy_id = id; 1259 return 0; 1260 } 1261 1262 static void cgroup_exit_root_id(struct cgroup_root *root) 1263 { 1264 lockdep_assert_held(&cgroup_mutex); 1265 1266 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id); 1267 } 1268 1269 void cgroup_free_root(struct cgroup_root *root) 1270 { 1271 if (root) { 1272 idr_destroy(&root->cgroup_idr); 1273 kfree(root); 1274 } 1275 } 1276 1277 static void cgroup_destroy_root(struct cgroup_root *root) 1278 { 1279 struct cgroup *cgrp = &root->cgrp; 1280 struct cgrp_cset_link *link, *tmp_link; 1281 1282 trace_cgroup_destroy_root(root); 1283 1284 cgroup_lock_and_drain_offline(&cgrp_dfl_root.cgrp); 1285 1286 BUG_ON(atomic_read(&root->nr_cgrps)); 1287 BUG_ON(!list_empty(&cgrp->self.children)); 1288 1289 /* Rebind all subsystems back to the default hierarchy */ 1290 WARN_ON(rebind_subsystems(&cgrp_dfl_root, root->subsys_mask)); 1291 1292 /* 1293 * Release all the links from cset_links to this hierarchy's 1294 * root cgroup 1295 */ 1296 spin_lock_irq(&css_set_lock); 1297 1298 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) { 1299 list_del(&link->cset_link); 1300 list_del(&link->cgrp_link); 1301 kfree(link); 1302 } 1303 1304 spin_unlock_irq(&css_set_lock); 1305 1306 if (!list_empty(&root->root_list)) { 1307 list_del(&root->root_list); 1308 cgroup_root_count--; 1309 } 1310 1311 cgroup_exit_root_id(root); 1312 1313 mutex_unlock(&cgroup_mutex); 1314 1315 kernfs_destroy_root(root->kf_root); 1316 cgroup_free_root(root); 1317 } 1318 1319 /* 1320 * look up cgroup associated with current task's cgroup namespace on the 1321 * specified hierarchy 1322 */ 1323 static struct cgroup * 1324 current_cgns_cgroup_from_root(struct cgroup_root *root) 1325 { 1326 struct cgroup *res = NULL; 1327 struct css_set *cset; 1328 1329 lockdep_assert_held(&css_set_lock); 1330 1331 rcu_read_lock(); 1332 1333 cset = current->nsproxy->cgroup_ns->root_cset; 1334 if (cset == &init_css_set) { 1335 res = &root->cgrp; 1336 } else { 1337 struct cgrp_cset_link *link; 1338 1339 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) { 1340 struct cgroup *c = link->cgrp; 1341 1342 if (c->root == root) { 1343 res = c; 1344 break; 1345 } 1346 } 1347 } 1348 rcu_read_unlock(); 1349 1350 BUG_ON(!res); 1351 return res; 1352 } 1353 1354 /* look up cgroup associated with given css_set on the specified hierarchy */ 1355 static struct cgroup *cset_cgroup_from_root(struct css_set *cset, 1356 struct cgroup_root *root) 1357 { 1358 struct cgroup *res = NULL; 1359 1360 lockdep_assert_held(&cgroup_mutex); 1361 lockdep_assert_held(&css_set_lock); 1362 1363 if (cset == &init_css_set) { 1364 res = &root->cgrp; 1365 } else if (root == &cgrp_dfl_root) { 1366 res = cset->dfl_cgrp; 1367 } else { 1368 struct cgrp_cset_link *link; 1369 1370 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) { 1371 struct cgroup *c = link->cgrp; 1372 1373 if (c->root == root) { 1374 res = c; 1375 break; 1376 } 1377 } 1378 } 1379 1380 BUG_ON(!res); 1381 return res; 1382 } 1383 1384 /* 1385 * Return the cgroup for "task" from the given hierarchy. Must be 1386 * called with cgroup_mutex and css_set_lock held. 1387 */ 1388 struct cgroup *task_cgroup_from_root(struct task_struct *task, 1389 struct cgroup_root *root) 1390 { 1391 /* 1392 * No need to lock the task - since we hold cgroup_mutex the 1393 * task can't change groups, so the only thing that can happen 1394 * is that it exits and its css is set back to init_css_set. 1395 */ 1396 return cset_cgroup_from_root(task_css_set(task), root); 1397 } 1398 1399 /* 1400 * A task must hold cgroup_mutex to modify cgroups. 1401 * 1402 * Any task can increment and decrement the count field without lock. 1403 * So in general, code holding cgroup_mutex can't rely on the count 1404 * field not changing. However, if the count goes to zero, then only 1405 * cgroup_attach_task() can increment it again. Because a count of zero 1406 * means that no tasks are currently attached, therefore there is no 1407 * way a task attached to that cgroup can fork (the other way to 1408 * increment the count). So code holding cgroup_mutex can safely 1409 * assume that if the count is zero, it will stay zero. Similarly, if 1410 * a task holds cgroup_mutex on a cgroup with zero count, it 1411 * knows that the cgroup won't be removed, as cgroup_rmdir() 1412 * needs that mutex. 1413 * 1414 * A cgroup can only be deleted if both its 'count' of using tasks 1415 * is zero, and its list of 'children' cgroups is empty. Since all 1416 * tasks in the system use _some_ cgroup, and since there is always at 1417 * least one task in the system (init, pid == 1), therefore, root cgroup 1418 * always has either children cgroups and/or using tasks. So we don't 1419 * need a special hack to ensure that root cgroup cannot be deleted. 1420 * 1421 * P.S. One more locking exception. RCU is used to guard the 1422 * update of a tasks cgroup pointer by cgroup_attach_task() 1423 */ 1424 1425 static struct kernfs_syscall_ops cgroup_kf_syscall_ops; 1426 1427 static char *cgroup_file_name(struct cgroup *cgrp, const struct cftype *cft, 1428 char *buf) 1429 { 1430 struct cgroup_subsys *ss = cft->ss; 1431 1432 if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) && 1433 !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) { 1434 const char *dbg = (cft->flags & CFTYPE_DEBUG) ? ".__DEBUG__." : ""; 1435 1436 snprintf(buf, CGROUP_FILE_NAME_MAX, "%s%s.%s", 1437 dbg, cgroup_on_dfl(cgrp) ? ss->name : ss->legacy_name, 1438 cft->name); 1439 } else { 1440 strscpy(buf, cft->name, CGROUP_FILE_NAME_MAX); 1441 } 1442 return buf; 1443 } 1444 1445 /** 1446 * cgroup_file_mode - deduce file mode of a control file 1447 * @cft: the control file in question 1448 * 1449 * S_IRUGO for read, S_IWUSR for write. 1450 */ 1451 static umode_t cgroup_file_mode(const struct cftype *cft) 1452 { 1453 umode_t mode = 0; 1454 1455 if (cft->read_u64 || cft->read_s64 || cft->seq_show) 1456 mode |= S_IRUGO; 1457 1458 if (cft->write_u64 || cft->write_s64 || cft->write) { 1459 if (cft->flags & CFTYPE_WORLD_WRITABLE) 1460 mode |= S_IWUGO; 1461 else 1462 mode |= S_IWUSR; 1463 } 1464 1465 return mode; 1466 } 1467 1468 /** 1469 * cgroup_calc_subtree_ss_mask - calculate subtree_ss_mask 1470 * @subtree_control: the new subtree_control mask to consider 1471 * @this_ss_mask: available subsystems 1472 * 1473 * On the default hierarchy, a subsystem may request other subsystems to be 1474 * enabled together through its ->depends_on mask. In such cases, more 1475 * subsystems than specified in "cgroup.subtree_control" may be enabled. 1476 * 1477 * This function calculates which subsystems need to be enabled if 1478 * @subtree_control is to be applied while restricted to @this_ss_mask. 1479 */ 1480 static u16 cgroup_calc_subtree_ss_mask(u16 subtree_control, u16 this_ss_mask) 1481 { 1482 u16 cur_ss_mask = subtree_control; 1483 struct cgroup_subsys *ss; 1484 int ssid; 1485 1486 lockdep_assert_held(&cgroup_mutex); 1487 1488 cur_ss_mask |= cgrp_dfl_implicit_ss_mask; 1489 1490 while (true) { 1491 u16 new_ss_mask = cur_ss_mask; 1492 1493 do_each_subsys_mask(ss, ssid, cur_ss_mask) { 1494 new_ss_mask |= ss->depends_on; 1495 } while_each_subsys_mask(); 1496 1497 /* 1498 * Mask out subsystems which aren't available. This can 1499 * happen only if some depended-upon subsystems were bound 1500 * to non-default hierarchies. 1501 */ 1502 new_ss_mask &= this_ss_mask; 1503 1504 if (new_ss_mask == cur_ss_mask) 1505 break; 1506 cur_ss_mask = new_ss_mask; 1507 } 1508 1509 return cur_ss_mask; 1510 } 1511 1512 /** 1513 * cgroup_kn_unlock - unlocking helper for cgroup kernfs methods 1514 * @kn: the kernfs_node being serviced 1515 * 1516 * This helper undoes cgroup_kn_lock_live() and should be invoked before 1517 * the method finishes if locking succeeded. Note that once this function 1518 * returns the cgroup returned by cgroup_kn_lock_live() may become 1519 * inaccessible any time. If the caller intends to continue to access the 1520 * cgroup, it should pin it before invoking this function. 1521 */ 1522 void cgroup_kn_unlock(struct kernfs_node *kn) 1523 { 1524 struct cgroup *cgrp; 1525 1526 if (kernfs_type(kn) == KERNFS_DIR) 1527 cgrp = kn->priv; 1528 else 1529 cgrp = kn->parent->priv; 1530 1531 mutex_unlock(&cgroup_mutex); 1532 1533 kernfs_unbreak_active_protection(kn); 1534 cgroup_put(cgrp); 1535 } 1536 1537 /** 1538 * cgroup_kn_lock_live - locking helper for cgroup kernfs methods 1539 * @kn: the kernfs_node being serviced 1540 * @drain_offline: perform offline draining on the cgroup 1541 * 1542 * This helper is to be used by a cgroup kernfs method currently servicing 1543 * @kn. It breaks the active protection, performs cgroup locking and 1544 * verifies that the associated cgroup is alive. Returns the cgroup if 1545 * alive; otherwise, %NULL. A successful return should be undone by a 1546 * matching cgroup_kn_unlock() invocation. If @drain_offline is %true, the 1547 * cgroup is drained of offlining csses before return. 1548 * 1549 * Any cgroup kernfs method implementation which requires locking the 1550 * associated cgroup should use this helper. It avoids nesting cgroup 1551 * locking under kernfs active protection and allows all kernfs operations 1552 * including self-removal. 1553 */ 1554 struct cgroup *cgroup_kn_lock_live(struct kernfs_node *kn, bool drain_offline) 1555 { 1556 struct cgroup *cgrp; 1557 1558 if (kernfs_type(kn) == KERNFS_DIR) 1559 cgrp = kn->priv; 1560 else 1561 cgrp = kn->parent->priv; 1562 1563 /* 1564 * We're gonna grab cgroup_mutex which nests outside kernfs 1565 * active_ref. cgroup liveliness check alone provides enough 1566 * protection against removal. Ensure @cgrp stays accessible and 1567 * break the active_ref protection. 1568 */ 1569 if (!cgroup_tryget(cgrp)) 1570 return NULL; 1571 kernfs_break_active_protection(kn); 1572 1573 if (drain_offline) 1574 cgroup_lock_and_drain_offline(cgrp); 1575 else 1576 mutex_lock(&cgroup_mutex); 1577 1578 if (!cgroup_is_dead(cgrp)) 1579 return cgrp; 1580 1581 cgroup_kn_unlock(kn); 1582 return NULL; 1583 } 1584 1585 static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft) 1586 { 1587 char name[CGROUP_FILE_NAME_MAX]; 1588 1589 lockdep_assert_held(&cgroup_mutex); 1590 1591 if (cft->file_offset) { 1592 struct cgroup_subsys_state *css = cgroup_css(cgrp, cft->ss); 1593 struct cgroup_file *cfile = (void *)css + cft->file_offset; 1594 1595 spin_lock_irq(&cgroup_file_kn_lock); 1596 cfile->kn = NULL; 1597 spin_unlock_irq(&cgroup_file_kn_lock); 1598 1599 del_timer_sync(&cfile->notify_timer); 1600 } 1601 1602 kernfs_remove_by_name(cgrp->kn, cgroup_file_name(cgrp, cft, name)); 1603 } 1604 1605 /** 1606 * css_clear_dir - remove subsys files in a cgroup directory 1607 * @css: taget css 1608 */ 1609 static void css_clear_dir(struct cgroup_subsys_state *css) 1610 { 1611 struct cgroup *cgrp = css->cgroup; 1612 struct cftype *cfts; 1613 1614 if (!(css->flags & CSS_VISIBLE)) 1615 return; 1616 1617 css->flags &= ~CSS_VISIBLE; 1618 1619 if (!css->ss) { 1620 if (cgroup_on_dfl(cgrp)) 1621 cfts = cgroup_base_files; 1622 else 1623 cfts = cgroup1_base_files; 1624 1625 cgroup_addrm_files(css, cgrp, cfts, false); 1626 } else { 1627 list_for_each_entry(cfts, &css->ss->cfts, node) 1628 cgroup_addrm_files(css, cgrp, cfts, false); 1629 } 1630 } 1631 1632 /** 1633 * css_populate_dir - create subsys files in a cgroup directory 1634 * @css: target css 1635 * 1636 * On failure, no file is added. 1637 */ 1638 static int css_populate_dir(struct cgroup_subsys_state *css) 1639 { 1640 struct cgroup *cgrp = css->cgroup; 1641 struct cftype *cfts, *failed_cfts; 1642 int ret; 1643 1644 if ((css->flags & CSS_VISIBLE) || !cgrp->kn) 1645 return 0; 1646 1647 if (!css->ss) { 1648 if (cgroup_on_dfl(cgrp)) 1649 cfts = cgroup_base_files; 1650 else 1651 cfts = cgroup1_base_files; 1652 1653 ret = cgroup_addrm_files(&cgrp->self, cgrp, cfts, true); 1654 if (ret < 0) 1655 return ret; 1656 } else { 1657 list_for_each_entry(cfts, &css->ss->cfts, node) { 1658 ret = cgroup_addrm_files(css, cgrp, cfts, true); 1659 if (ret < 0) { 1660 failed_cfts = cfts; 1661 goto err; 1662 } 1663 } 1664 } 1665 1666 css->flags |= CSS_VISIBLE; 1667 1668 return 0; 1669 err: 1670 list_for_each_entry(cfts, &css->ss->cfts, node) { 1671 if (cfts == failed_cfts) 1672 break; 1673 cgroup_addrm_files(css, cgrp, cfts, false); 1674 } 1675 return ret; 1676 } 1677 1678 int rebind_subsystems(struct cgroup_root *dst_root, u16 ss_mask) 1679 { 1680 struct cgroup *dcgrp = &dst_root->cgrp; 1681 struct cgroup_subsys *ss; 1682 int ssid, i, ret; 1683 1684 lockdep_assert_held(&cgroup_mutex); 1685 1686 do_each_subsys_mask(ss, ssid, ss_mask) { 1687 /* 1688 * If @ss has non-root csses attached to it, can't move. 1689 * If @ss is an implicit controller, it is exempt from this 1690 * rule and can be stolen. 1691 */ 1692 if (css_next_child(NULL, cgroup_css(&ss->root->cgrp, ss)) && 1693 !ss->implicit_on_dfl) 1694 return -EBUSY; 1695 1696 /* can't move between two non-dummy roots either */ 1697 if (ss->root != &cgrp_dfl_root && dst_root != &cgrp_dfl_root) 1698 return -EBUSY; 1699 } while_each_subsys_mask(); 1700 1701 do_each_subsys_mask(ss, ssid, ss_mask) { 1702 struct cgroup_root *src_root = ss->root; 1703 struct cgroup *scgrp = &src_root->cgrp; 1704 struct cgroup_subsys_state *css = cgroup_css(scgrp, ss); 1705 struct css_set *cset; 1706 1707 WARN_ON(!css || cgroup_css(dcgrp, ss)); 1708 1709 /* disable from the source */ 1710 src_root->subsys_mask &= ~(1 << ssid); 1711 WARN_ON(cgroup_apply_control(scgrp)); 1712 cgroup_finalize_control(scgrp, 0); 1713 1714 /* rebind */ 1715 RCU_INIT_POINTER(scgrp->subsys[ssid], NULL); 1716 rcu_assign_pointer(dcgrp->subsys[ssid], css); 1717 ss->root = dst_root; 1718 css->cgroup = dcgrp; 1719 1720 spin_lock_irq(&css_set_lock); 1721 hash_for_each(css_set_table, i, cset, hlist) 1722 list_move_tail(&cset->e_cset_node[ss->id], 1723 &dcgrp->e_csets[ss->id]); 1724 spin_unlock_irq(&css_set_lock); 1725 1726 /* default hierarchy doesn't enable controllers by default */ 1727 dst_root->subsys_mask |= 1 << ssid; 1728 if (dst_root == &cgrp_dfl_root) { 1729 static_branch_enable(cgroup_subsys_on_dfl_key[ssid]); 1730 } else { 1731 dcgrp->subtree_control |= 1 << ssid; 1732 static_branch_disable(cgroup_subsys_on_dfl_key[ssid]); 1733 } 1734 1735 ret = cgroup_apply_control(dcgrp); 1736 if (ret) 1737 pr_warn("partial failure to rebind %s controller (err=%d)\n", 1738 ss->name, ret); 1739 1740 if (ss->bind) 1741 ss->bind(css); 1742 } while_each_subsys_mask(); 1743 1744 kernfs_activate(dcgrp->kn); 1745 return 0; 1746 } 1747 1748 int cgroup_show_path(struct seq_file *sf, struct kernfs_node *kf_node, 1749 struct kernfs_root *kf_root) 1750 { 1751 int len = 0; 1752 char *buf = NULL; 1753 struct cgroup_root *kf_cgroot = cgroup_root_from_kf(kf_root); 1754 struct cgroup *ns_cgroup; 1755 1756 buf = kmalloc(PATH_MAX, GFP_KERNEL); 1757 if (!buf) 1758 return -ENOMEM; 1759 1760 spin_lock_irq(&css_set_lock); 1761 ns_cgroup = current_cgns_cgroup_from_root(kf_cgroot); 1762 len = kernfs_path_from_node(kf_node, ns_cgroup->kn, buf, PATH_MAX); 1763 spin_unlock_irq(&css_set_lock); 1764 1765 if (len >= PATH_MAX) 1766 len = -ERANGE; 1767 else if (len > 0) { 1768 seq_escape(sf, buf, " \t\n\\"); 1769 len = 0; 1770 } 1771 kfree(buf); 1772 return len; 1773 } 1774 1775 static int parse_cgroup_root_flags(char *data, unsigned int *root_flags) 1776 { 1777 char *token; 1778 1779 *root_flags = 0; 1780 1781 if (!data || *data == '\0') 1782 return 0; 1783 1784 while ((token = strsep(&data, ",")) != NULL) { 1785 if (!strcmp(token, "nsdelegate")) { 1786 *root_flags |= CGRP_ROOT_NS_DELEGATE; 1787 continue; 1788 } 1789 1790 pr_err("cgroup2: unknown option \"%s\"\n", token); 1791 return -EINVAL; 1792 } 1793 1794 return 0; 1795 } 1796 1797 static void apply_cgroup_root_flags(unsigned int root_flags) 1798 { 1799 if (current->nsproxy->cgroup_ns == &init_cgroup_ns) { 1800 if (root_flags & CGRP_ROOT_NS_DELEGATE) 1801 cgrp_dfl_root.flags |= CGRP_ROOT_NS_DELEGATE; 1802 else 1803 cgrp_dfl_root.flags &= ~CGRP_ROOT_NS_DELEGATE; 1804 } 1805 } 1806 1807 static int cgroup_show_options(struct seq_file *seq, struct kernfs_root *kf_root) 1808 { 1809 if (cgrp_dfl_root.flags & CGRP_ROOT_NS_DELEGATE) 1810 seq_puts(seq, ",nsdelegate"); 1811 return 0; 1812 } 1813 1814 static int cgroup_remount(struct kernfs_root *kf_root, int *flags, char *data) 1815 { 1816 unsigned int root_flags; 1817 int ret; 1818 1819 ret = parse_cgroup_root_flags(data, &root_flags); 1820 if (ret) 1821 return ret; 1822 1823 apply_cgroup_root_flags(root_flags); 1824 return 0; 1825 } 1826 1827 /* 1828 * To reduce the fork() overhead for systems that are not actually using 1829 * their cgroups capability, we don't maintain the lists running through 1830 * each css_set to its tasks until we see the list actually used - in other 1831 * words after the first mount. 1832 */ 1833 static bool use_task_css_set_links __read_mostly; 1834 1835 static void cgroup_enable_task_cg_lists(void) 1836 { 1837 struct task_struct *p, *g; 1838 1839 /* 1840 * We need tasklist_lock because RCU is not safe against 1841 * while_each_thread(). Besides, a forking task that has passed 1842 * cgroup_post_fork() without seeing use_task_css_set_links = 1 1843 * is not guaranteed to have its child immediately visible in the 1844 * tasklist if we walk through it with RCU. 1845 */ 1846 read_lock(&tasklist_lock); 1847 spin_lock_irq(&css_set_lock); 1848 1849 if (use_task_css_set_links) 1850 goto out_unlock; 1851 1852 use_task_css_set_links = true; 1853 1854 do_each_thread(g, p) { 1855 WARN_ON_ONCE(!list_empty(&p->cg_list) || 1856 task_css_set(p) != &init_css_set); 1857 1858 /* 1859 * We should check if the process is exiting, otherwise 1860 * it will race with cgroup_exit() in that the list 1861 * entry won't be deleted though the process has exited. 1862 * Do it while holding siglock so that we don't end up 1863 * racing against cgroup_exit(). 1864 * 1865 * Interrupts were already disabled while acquiring 1866 * the css_set_lock, so we do not need to disable it 1867 * again when acquiring the sighand->siglock here. 1868 */ 1869 spin_lock(&p->sighand->siglock); 1870 if (!(p->flags & PF_EXITING)) { 1871 struct css_set *cset = task_css_set(p); 1872 1873 if (!css_set_populated(cset)) 1874 css_set_update_populated(cset, true); 1875 list_add_tail(&p->cg_list, &cset->tasks); 1876 get_css_set(cset); 1877 cset->nr_tasks++; 1878 } 1879 spin_unlock(&p->sighand->siglock); 1880 } while_each_thread(g, p); 1881 out_unlock: 1882 spin_unlock_irq(&css_set_lock); 1883 read_unlock(&tasklist_lock); 1884 } 1885 1886 static void init_cgroup_housekeeping(struct cgroup *cgrp) 1887 { 1888 struct cgroup_subsys *ss; 1889 int ssid; 1890 1891 INIT_LIST_HEAD(&cgrp->self.sibling); 1892 INIT_LIST_HEAD(&cgrp->self.children); 1893 INIT_LIST_HEAD(&cgrp->cset_links); 1894 INIT_LIST_HEAD(&cgrp->pidlists); 1895 mutex_init(&cgrp->pidlist_mutex); 1896 cgrp->self.cgroup = cgrp; 1897 cgrp->self.flags |= CSS_ONLINE; 1898 cgrp->dom_cgrp = cgrp; 1899 cgrp->max_descendants = INT_MAX; 1900 cgrp->max_depth = INT_MAX; 1901 INIT_LIST_HEAD(&cgrp->rstat_css_list); 1902 prev_cputime_init(&cgrp->prev_cputime); 1903 1904 for_each_subsys(ss, ssid) 1905 INIT_LIST_HEAD(&cgrp->e_csets[ssid]); 1906 1907 init_waitqueue_head(&cgrp->offline_waitq); 1908 INIT_WORK(&cgrp->release_agent_work, cgroup1_release_agent); 1909 } 1910 1911 void init_cgroup_root(struct cgroup_root *root, struct cgroup_sb_opts *opts) 1912 { 1913 struct cgroup *cgrp = &root->cgrp; 1914 1915 INIT_LIST_HEAD(&root->root_list); 1916 atomic_set(&root->nr_cgrps, 1); 1917 cgrp->root = root; 1918 init_cgroup_housekeeping(cgrp); 1919 idr_init(&root->cgroup_idr); 1920 1921 root->flags = opts->flags; 1922 if (opts->release_agent) 1923 strscpy(root->release_agent_path, opts->release_agent, PATH_MAX); 1924 if (opts->name) 1925 strscpy(root->name, opts->name, MAX_CGROUP_ROOT_NAMELEN); 1926 if (opts->cpuset_clone_children) 1927 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->cgrp.flags); 1928 } 1929 1930 int cgroup_setup_root(struct cgroup_root *root, u16 ss_mask) 1931 { 1932 LIST_HEAD(tmp_links); 1933 struct cgroup *root_cgrp = &root->cgrp; 1934 struct kernfs_syscall_ops *kf_sops; 1935 struct css_set *cset; 1936 int i, ret; 1937 1938 lockdep_assert_held(&cgroup_mutex); 1939 1940 ret = cgroup_idr_alloc(&root->cgroup_idr, root_cgrp, 1, 2, GFP_KERNEL); 1941 if (ret < 0) 1942 goto out; 1943 root_cgrp->id = ret; 1944 root_cgrp->ancestor_ids[0] = ret; 1945 1946 ret = percpu_ref_init(&root_cgrp->self.refcnt, css_release, 1947 0, GFP_KERNEL); 1948 if (ret) 1949 goto out; 1950 1951 /* 1952 * We're accessing css_set_count without locking css_set_lock here, 1953 * but that's OK - it can only be increased by someone holding 1954 * cgroup_lock, and that's us. Later rebinding may disable 1955 * controllers on the default hierarchy and thus create new csets, 1956 * which can't be more than the existing ones. Allocate 2x. 1957 */ 1958 ret = allocate_cgrp_cset_links(2 * css_set_count, &tmp_links); 1959 if (ret) 1960 goto cancel_ref; 1961 1962 ret = cgroup_init_root_id(root); 1963 if (ret) 1964 goto cancel_ref; 1965 1966 kf_sops = root == &cgrp_dfl_root ? 1967 &cgroup_kf_syscall_ops : &cgroup1_kf_syscall_ops; 1968 1969 root->kf_root = kernfs_create_root(kf_sops, 1970 KERNFS_ROOT_CREATE_DEACTIVATED | 1971 KERNFS_ROOT_SUPPORT_EXPORTOP, 1972 root_cgrp); 1973 if (IS_ERR(root->kf_root)) { 1974 ret = PTR_ERR(root->kf_root); 1975 goto exit_root_id; 1976 } 1977 root_cgrp->kn = root->kf_root->kn; 1978 1979 ret = css_populate_dir(&root_cgrp->self); 1980 if (ret) 1981 goto destroy_root; 1982 1983 ret = rebind_subsystems(root, ss_mask); 1984 if (ret) 1985 goto destroy_root; 1986 1987 ret = cgroup_bpf_inherit(root_cgrp); 1988 WARN_ON_ONCE(ret); 1989 1990 trace_cgroup_setup_root(root); 1991 1992 /* 1993 * There must be no failure case after here, since rebinding takes 1994 * care of subsystems' refcounts, which are explicitly dropped in 1995 * the failure exit path. 1996 */ 1997 list_add(&root->root_list, &cgroup_roots); 1998 cgroup_root_count++; 1999 2000 /* 2001 * Link the root cgroup in this hierarchy into all the css_set 2002 * objects. 2003 */ 2004 spin_lock_irq(&css_set_lock); 2005 hash_for_each(css_set_table, i, cset, hlist) { 2006 link_css_set(&tmp_links, cset, root_cgrp); 2007 if (css_set_populated(cset)) 2008 cgroup_update_populated(root_cgrp, true); 2009 } 2010 spin_unlock_irq(&css_set_lock); 2011 2012 BUG_ON(!list_empty(&root_cgrp->self.children)); 2013 BUG_ON(atomic_read(&root->nr_cgrps) != 1); 2014 2015 kernfs_activate(root_cgrp->kn); 2016 ret = 0; 2017 goto out; 2018 2019 destroy_root: 2020 kernfs_destroy_root(root->kf_root); 2021 root->kf_root = NULL; 2022 exit_root_id: 2023 cgroup_exit_root_id(root); 2024 cancel_ref: 2025 percpu_ref_exit(&root_cgrp->self.refcnt); 2026 out: 2027 free_cgrp_cset_links(&tmp_links); 2028 return ret; 2029 } 2030 2031 struct dentry *cgroup_do_mount(struct file_system_type *fs_type, int flags, 2032 struct cgroup_root *root, unsigned long magic, 2033 struct cgroup_namespace *ns) 2034 { 2035 struct dentry *dentry; 2036 bool new_sb = false; 2037 2038 dentry = kernfs_mount(fs_type, flags, root->kf_root, magic, &new_sb); 2039 2040 /* 2041 * In non-init cgroup namespace, instead of root cgroup's dentry, 2042 * we return the dentry corresponding to the cgroupns->root_cgrp. 2043 */ 2044 if (!IS_ERR(dentry) && ns != &init_cgroup_ns) { 2045 struct dentry *nsdentry; 2046 struct super_block *sb = dentry->d_sb; 2047 struct cgroup *cgrp; 2048 2049 mutex_lock(&cgroup_mutex); 2050 spin_lock_irq(&css_set_lock); 2051 2052 cgrp = cset_cgroup_from_root(ns->root_cset, root); 2053 2054 spin_unlock_irq(&css_set_lock); 2055 mutex_unlock(&cgroup_mutex); 2056 2057 nsdentry = kernfs_node_dentry(cgrp->kn, sb); 2058 dput(dentry); 2059 if (IS_ERR(nsdentry)) 2060 deactivate_locked_super(sb); 2061 dentry = nsdentry; 2062 } 2063 2064 if (!new_sb) 2065 cgroup_put(&root->cgrp); 2066 2067 return dentry; 2068 } 2069 2070 static struct dentry *cgroup_mount(struct file_system_type *fs_type, 2071 int flags, const char *unused_dev_name, 2072 void *data) 2073 { 2074 struct cgroup_namespace *ns = current->nsproxy->cgroup_ns; 2075 struct dentry *dentry; 2076 int ret; 2077 2078 get_cgroup_ns(ns); 2079 2080 /* Check if the caller has permission to mount. */ 2081 if (!ns_capable(ns->user_ns, CAP_SYS_ADMIN)) { 2082 put_cgroup_ns(ns); 2083 return ERR_PTR(-EPERM); 2084 } 2085 2086 /* 2087 * The first time anyone tries to mount a cgroup, enable the list 2088 * linking each css_set to its tasks and fix up all existing tasks. 2089 */ 2090 if (!use_task_css_set_links) 2091 cgroup_enable_task_cg_lists(); 2092 2093 if (fs_type == &cgroup2_fs_type) { 2094 unsigned int root_flags; 2095 2096 ret = parse_cgroup_root_flags(data, &root_flags); 2097 if (ret) { 2098 put_cgroup_ns(ns); 2099 return ERR_PTR(ret); 2100 } 2101 2102 cgrp_dfl_visible = true; 2103 cgroup_get_live(&cgrp_dfl_root.cgrp); 2104 2105 dentry = cgroup_do_mount(&cgroup2_fs_type, flags, &cgrp_dfl_root, 2106 CGROUP2_SUPER_MAGIC, ns); 2107 if (!IS_ERR(dentry)) 2108 apply_cgroup_root_flags(root_flags); 2109 } else { 2110 dentry = cgroup1_mount(&cgroup_fs_type, flags, data, 2111 CGROUP_SUPER_MAGIC, ns); 2112 } 2113 2114 put_cgroup_ns(ns); 2115 return dentry; 2116 } 2117 2118 static void cgroup_kill_sb(struct super_block *sb) 2119 { 2120 struct kernfs_root *kf_root = kernfs_root_from_sb(sb); 2121 struct cgroup_root *root = cgroup_root_from_kf(kf_root); 2122 2123 /* 2124 * If @root doesn't have any children, start killing it. 2125 * This prevents new mounts by disabling percpu_ref_tryget_live(). 2126 * cgroup_mount() may wait for @root's release. 2127 * 2128 * And don't kill the default root. 2129 */ 2130 if (list_empty(&root->cgrp.self.children) && root != &cgrp_dfl_root && 2131 !percpu_ref_is_dying(&root->cgrp.self.refcnt)) 2132 percpu_ref_kill(&root->cgrp.self.refcnt); 2133 cgroup_put(&root->cgrp); 2134 kernfs_kill_sb(sb); 2135 } 2136 2137 struct file_system_type cgroup_fs_type = { 2138 .name = "cgroup", 2139 .mount = cgroup_mount, 2140 .kill_sb = cgroup_kill_sb, 2141 .fs_flags = FS_USERNS_MOUNT, 2142 }; 2143 2144 static struct file_system_type cgroup2_fs_type = { 2145 .name = "cgroup2", 2146 .mount = cgroup_mount, 2147 .kill_sb = cgroup_kill_sb, 2148 .fs_flags = FS_USERNS_MOUNT, 2149 }; 2150 2151 int cgroup_path_ns_locked(struct cgroup *cgrp, char *buf, size_t buflen, 2152 struct cgroup_namespace *ns) 2153 { 2154 struct cgroup *root = cset_cgroup_from_root(ns->root_cset, cgrp->root); 2155 2156 return kernfs_path_from_node(cgrp->kn, root->kn, buf, buflen); 2157 } 2158 2159 int cgroup_path_ns(struct cgroup *cgrp, char *buf, size_t buflen, 2160 struct cgroup_namespace *ns) 2161 { 2162 int ret; 2163 2164 mutex_lock(&cgroup_mutex); 2165 spin_lock_irq(&css_set_lock); 2166 2167 ret = cgroup_path_ns_locked(cgrp, buf, buflen, ns); 2168 2169 spin_unlock_irq(&css_set_lock); 2170 mutex_unlock(&cgroup_mutex); 2171 2172 return ret; 2173 } 2174 EXPORT_SYMBOL_GPL(cgroup_path_ns); 2175 2176 /** 2177 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy 2178 * @task: target task 2179 * @buf: the buffer to write the path into 2180 * @buflen: the length of the buffer 2181 * 2182 * Determine @task's cgroup on the first (the one with the lowest non-zero 2183 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This 2184 * function grabs cgroup_mutex and shouldn't be used inside locks used by 2185 * cgroup controller callbacks. 2186 * 2187 * Return value is the same as kernfs_path(). 2188 */ 2189 int task_cgroup_path(struct task_struct *task, char *buf, size_t buflen) 2190 { 2191 struct cgroup_root *root; 2192 struct cgroup *cgrp; 2193 int hierarchy_id = 1; 2194 int ret; 2195 2196 mutex_lock(&cgroup_mutex); 2197 spin_lock_irq(&css_set_lock); 2198 2199 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id); 2200 2201 if (root) { 2202 cgrp = task_cgroup_from_root(task, root); 2203 ret = cgroup_path_ns_locked(cgrp, buf, buflen, &init_cgroup_ns); 2204 } else { 2205 /* if no hierarchy exists, everyone is in "/" */ 2206 ret = strlcpy(buf, "/", buflen); 2207 } 2208 2209 spin_unlock_irq(&css_set_lock); 2210 mutex_unlock(&cgroup_mutex); 2211 return ret; 2212 } 2213 EXPORT_SYMBOL_GPL(task_cgroup_path); 2214 2215 /** 2216 * cgroup_migrate_add_task - add a migration target task to a migration context 2217 * @task: target task 2218 * @mgctx: target migration context 2219 * 2220 * Add @task, which is a migration target, to @mgctx->tset. This function 2221 * becomes noop if @task doesn't need to be migrated. @task's css_set 2222 * should have been added as a migration source and @task->cg_list will be 2223 * moved from the css_set's tasks list to mg_tasks one. 2224 */ 2225 static void cgroup_migrate_add_task(struct task_struct *task, 2226 struct cgroup_mgctx *mgctx) 2227 { 2228 struct css_set *cset; 2229 2230 lockdep_assert_held(&css_set_lock); 2231 2232 /* @task either already exited or can't exit until the end */ 2233 if (task->flags & PF_EXITING) 2234 return; 2235 2236 /* leave @task alone if post_fork() hasn't linked it yet */ 2237 if (list_empty(&task->cg_list)) 2238 return; 2239 2240 cset = task_css_set(task); 2241 if (!cset->mg_src_cgrp) 2242 return; 2243 2244 mgctx->tset.nr_tasks++; 2245 2246 list_move_tail(&task->cg_list, &cset->mg_tasks); 2247 if (list_empty(&cset->mg_node)) 2248 list_add_tail(&cset->mg_node, 2249 &mgctx->tset.src_csets); 2250 if (list_empty(&cset->mg_dst_cset->mg_node)) 2251 list_add_tail(&cset->mg_dst_cset->mg_node, 2252 &mgctx->tset.dst_csets); 2253 } 2254 2255 /** 2256 * cgroup_taskset_first - reset taskset and return the first task 2257 * @tset: taskset of interest 2258 * @dst_cssp: output variable for the destination css 2259 * 2260 * @tset iteration is initialized and the first task is returned. 2261 */ 2262 struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset, 2263 struct cgroup_subsys_state **dst_cssp) 2264 { 2265 tset->cur_cset = list_first_entry(tset->csets, struct css_set, mg_node); 2266 tset->cur_task = NULL; 2267 2268 return cgroup_taskset_next(tset, dst_cssp); 2269 } 2270 2271 /** 2272 * cgroup_taskset_next - iterate to the next task in taskset 2273 * @tset: taskset of interest 2274 * @dst_cssp: output variable for the destination css 2275 * 2276 * Return the next task in @tset. Iteration must have been initialized 2277 * with cgroup_taskset_first(). 2278 */ 2279 struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset, 2280 struct cgroup_subsys_state **dst_cssp) 2281 { 2282 struct css_set *cset = tset->cur_cset; 2283 struct task_struct *task = tset->cur_task; 2284 2285 while (&cset->mg_node != tset->csets) { 2286 if (!task) 2287 task = list_first_entry(&cset->mg_tasks, 2288 struct task_struct, cg_list); 2289 else 2290 task = list_next_entry(task, cg_list); 2291 2292 if (&task->cg_list != &cset->mg_tasks) { 2293 tset->cur_cset = cset; 2294 tset->cur_task = task; 2295 2296 /* 2297 * This function may be called both before and 2298 * after cgroup_taskset_migrate(). The two cases 2299 * can be distinguished by looking at whether @cset 2300 * has its ->mg_dst_cset set. 2301 */ 2302 if (cset->mg_dst_cset) 2303 *dst_cssp = cset->mg_dst_cset->subsys[tset->ssid]; 2304 else 2305 *dst_cssp = cset->subsys[tset->ssid]; 2306 2307 return task; 2308 } 2309 2310 cset = list_next_entry(cset, mg_node); 2311 task = NULL; 2312 } 2313 2314 return NULL; 2315 } 2316 2317 /** 2318 * cgroup_taskset_migrate - migrate a taskset 2319 * @mgctx: migration context 2320 * 2321 * Migrate tasks in @mgctx as setup by migration preparation functions. 2322 * This function fails iff one of the ->can_attach callbacks fails and 2323 * guarantees that either all or none of the tasks in @mgctx are migrated. 2324 * @mgctx is consumed regardless of success. 2325 */ 2326 static int cgroup_migrate_execute(struct cgroup_mgctx *mgctx) 2327 { 2328 struct cgroup_taskset *tset = &mgctx->tset; 2329 struct cgroup_subsys *ss; 2330 struct task_struct *task, *tmp_task; 2331 struct css_set *cset, *tmp_cset; 2332 int ssid, failed_ssid, ret; 2333 2334 /* check that we can legitimately attach to the cgroup */ 2335 if (tset->nr_tasks) { 2336 do_each_subsys_mask(ss, ssid, mgctx->ss_mask) { 2337 if (ss->can_attach) { 2338 tset->ssid = ssid; 2339 ret = ss->can_attach(tset); 2340 if (ret) { 2341 failed_ssid = ssid; 2342 goto out_cancel_attach; 2343 } 2344 } 2345 } while_each_subsys_mask(); 2346 } 2347 2348 /* 2349 * Now that we're guaranteed success, proceed to move all tasks to 2350 * the new cgroup. There are no failure cases after here, so this 2351 * is the commit point. 2352 */ 2353 spin_lock_irq(&css_set_lock); 2354 list_for_each_entry(cset, &tset->src_csets, mg_node) { 2355 list_for_each_entry_safe(task, tmp_task, &cset->mg_tasks, cg_list) { 2356 struct css_set *from_cset = task_css_set(task); 2357 struct css_set *to_cset = cset->mg_dst_cset; 2358 2359 get_css_set(to_cset); 2360 to_cset->nr_tasks++; 2361 css_set_move_task(task, from_cset, to_cset, true); 2362 put_css_set_locked(from_cset); 2363 from_cset->nr_tasks--; 2364 } 2365 } 2366 spin_unlock_irq(&css_set_lock); 2367 2368 /* 2369 * Migration is committed, all target tasks are now on dst_csets. 2370 * Nothing is sensitive to fork() after this point. Notify 2371 * controllers that migration is complete. 2372 */ 2373 tset->csets = &tset->dst_csets; 2374 2375 if (tset->nr_tasks) { 2376 do_each_subsys_mask(ss, ssid, mgctx->ss_mask) { 2377 if (ss->attach) { 2378 tset->ssid = ssid; 2379 ss->attach(tset); 2380 } 2381 } while_each_subsys_mask(); 2382 } 2383 2384 ret = 0; 2385 goto out_release_tset; 2386 2387 out_cancel_attach: 2388 if (tset->nr_tasks) { 2389 do_each_subsys_mask(ss, ssid, mgctx->ss_mask) { 2390 if (ssid == failed_ssid) 2391 break; 2392 if (ss->cancel_attach) { 2393 tset->ssid = ssid; 2394 ss->cancel_attach(tset); 2395 } 2396 } while_each_subsys_mask(); 2397 } 2398 out_release_tset: 2399 spin_lock_irq(&css_set_lock); 2400 list_splice_init(&tset->dst_csets, &tset->src_csets); 2401 list_for_each_entry_safe(cset, tmp_cset, &tset->src_csets, mg_node) { 2402 list_splice_tail_init(&cset->mg_tasks, &cset->tasks); 2403 list_del_init(&cset->mg_node); 2404 } 2405 spin_unlock_irq(&css_set_lock); 2406 2407 /* 2408 * Re-initialize the cgroup_taskset structure in case it is reused 2409 * again in another cgroup_migrate_add_task()/cgroup_migrate_execute() 2410 * iteration. 2411 */ 2412 tset->nr_tasks = 0; 2413 tset->csets = &tset->src_csets; 2414 return ret; 2415 } 2416 2417 /** 2418 * cgroup_migrate_vet_dst - verify whether a cgroup can be migration destination 2419 * @dst_cgrp: destination cgroup to test 2420 * 2421 * On the default hierarchy, except for the mixable, (possible) thread root 2422 * and threaded cgroups, subtree_control must be zero for migration 2423 * destination cgroups with tasks so that child cgroups don't compete 2424 * against tasks. 2425 */ 2426 int cgroup_migrate_vet_dst(struct cgroup *dst_cgrp) 2427 { 2428 /* v1 doesn't have any restriction */ 2429 if (!cgroup_on_dfl(dst_cgrp)) 2430 return 0; 2431 2432 /* verify @dst_cgrp can host resources */ 2433 if (!cgroup_is_valid_domain(dst_cgrp->dom_cgrp)) 2434 return -EOPNOTSUPP; 2435 2436 /* mixables don't care */ 2437 if (cgroup_is_mixable(dst_cgrp)) 2438 return 0; 2439 2440 /* 2441 * If @dst_cgrp is already or can become a thread root or is 2442 * threaded, it doesn't matter. 2443 */ 2444 if (cgroup_can_be_thread_root(dst_cgrp) || cgroup_is_threaded(dst_cgrp)) 2445 return 0; 2446 2447 /* apply no-internal-process constraint */ 2448 if (dst_cgrp->subtree_control) 2449 return -EBUSY; 2450 2451 return 0; 2452 } 2453 2454 /** 2455 * cgroup_migrate_finish - cleanup after attach 2456 * @mgctx: migration context 2457 * 2458 * Undo cgroup_migrate_add_src() and cgroup_migrate_prepare_dst(). See 2459 * those functions for details. 2460 */ 2461 void cgroup_migrate_finish(struct cgroup_mgctx *mgctx) 2462 { 2463 LIST_HEAD(preloaded); 2464 struct css_set *cset, *tmp_cset; 2465 2466 lockdep_assert_held(&cgroup_mutex); 2467 2468 spin_lock_irq(&css_set_lock); 2469 2470 list_splice_tail_init(&mgctx->preloaded_src_csets, &preloaded); 2471 list_splice_tail_init(&mgctx->preloaded_dst_csets, &preloaded); 2472 2473 list_for_each_entry_safe(cset, tmp_cset, &preloaded, mg_preload_node) { 2474 cset->mg_src_cgrp = NULL; 2475 cset->mg_dst_cgrp = NULL; 2476 cset->mg_dst_cset = NULL; 2477 list_del_init(&cset->mg_preload_node); 2478 put_css_set_locked(cset); 2479 } 2480 2481 spin_unlock_irq(&css_set_lock); 2482 } 2483 2484 /** 2485 * cgroup_migrate_add_src - add a migration source css_set 2486 * @src_cset: the source css_set to add 2487 * @dst_cgrp: the destination cgroup 2488 * @mgctx: migration context 2489 * 2490 * Tasks belonging to @src_cset are about to be migrated to @dst_cgrp. Pin 2491 * @src_cset and add it to @mgctx->src_csets, which should later be cleaned 2492 * up by cgroup_migrate_finish(). 2493 * 2494 * This function may be called without holding cgroup_threadgroup_rwsem 2495 * even if the target is a process. Threads may be created and destroyed 2496 * but as long as cgroup_mutex is not dropped, no new css_set can be put 2497 * into play and the preloaded css_sets are guaranteed to cover all 2498 * migrations. 2499 */ 2500 void cgroup_migrate_add_src(struct css_set *src_cset, 2501 struct cgroup *dst_cgrp, 2502 struct cgroup_mgctx *mgctx) 2503 { 2504 struct cgroup *src_cgrp; 2505 2506 lockdep_assert_held(&cgroup_mutex); 2507 lockdep_assert_held(&css_set_lock); 2508 2509 /* 2510 * If ->dead, @src_set is associated with one or more dead cgroups 2511 * and doesn't contain any migratable tasks. Ignore it early so 2512 * that the rest of migration path doesn't get confused by it. 2513 */ 2514 if (src_cset->dead) 2515 return; 2516 2517 src_cgrp = cset_cgroup_from_root(src_cset, dst_cgrp->root); 2518 2519 if (!list_empty(&src_cset->mg_preload_node)) 2520 return; 2521 2522 WARN_ON(src_cset->mg_src_cgrp); 2523 WARN_ON(src_cset->mg_dst_cgrp); 2524 WARN_ON(!list_empty(&src_cset->mg_tasks)); 2525 WARN_ON(!list_empty(&src_cset->mg_node)); 2526 2527 src_cset->mg_src_cgrp = src_cgrp; 2528 src_cset->mg_dst_cgrp = dst_cgrp; 2529 get_css_set(src_cset); 2530 list_add_tail(&src_cset->mg_preload_node, &mgctx->preloaded_src_csets); 2531 } 2532 2533 /** 2534 * cgroup_migrate_prepare_dst - prepare destination css_sets for migration 2535 * @mgctx: migration context 2536 * 2537 * Tasks are about to be moved and all the source css_sets have been 2538 * preloaded to @mgctx->preloaded_src_csets. This function looks up and 2539 * pins all destination css_sets, links each to its source, and append them 2540 * to @mgctx->preloaded_dst_csets. 2541 * 2542 * This function must be called after cgroup_migrate_add_src() has been 2543 * called on each migration source css_set. After migration is performed 2544 * using cgroup_migrate(), cgroup_migrate_finish() must be called on 2545 * @mgctx. 2546 */ 2547 int cgroup_migrate_prepare_dst(struct cgroup_mgctx *mgctx) 2548 { 2549 struct css_set *src_cset, *tmp_cset; 2550 2551 lockdep_assert_held(&cgroup_mutex); 2552 2553 /* look up the dst cset for each src cset and link it to src */ 2554 list_for_each_entry_safe(src_cset, tmp_cset, &mgctx->preloaded_src_csets, 2555 mg_preload_node) { 2556 struct css_set *dst_cset; 2557 struct cgroup_subsys *ss; 2558 int ssid; 2559 2560 dst_cset = find_css_set(src_cset, src_cset->mg_dst_cgrp); 2561 if (!dst_cset) 2562 goto err; 2563 2564 WARN_ON_ONCE(src_cset->mg_dst_cset || dst_cset->mg_dst_cset); 2565 2566 /* 2567 * If src cset equals dst, it's noop. Drop the src. 2568 * cgroup_migrate() will skip the cset too. Note that we 2569 * can't handle src == dst as some nodes are used by both. 2570 */ 2571 if (src_cset == dst_cset) { 2572 src_cset->mg_src_cgrp = NULL; 2573 src_cset->mg_dst_cgrp = NULL; 2574 list_del_init(&src_cset->mg_preload_node); 2575 put_css_set(src_cset); 2576 put_css_set(dst_cset); 2577 continue; 2578 } 2579 2580 src_cset->mg_dst_cset = dst_cset; 2581 2582 if (list_empty(&dst_cset->mg_preload_node)) 2583 list_add_tail(&dst_cset->mg_preload_node, 2584 &mgctx->preloaded_dst_csets); 2585 else 2586 put_css_set(dst_cset); 2587 2588 for_each_subsys(ss, ssid) 2589 if (src_cset->subsys[ssid] != dst_cset->subsys[ssid]) 2590 mgctx->ss_mask |= 1 << ssid; 2591 } 2592 2593 return 0; 2594 err: 2595 cgroup_migrate_finish(mgctx); 2596 return -ENOMEM; 2597 } 2598 2599 /** 2600 * cgroup_migrate - migrate a process or task to a cgroup 2601 * @leader: the leader of the process or the task to migrate 2602 * @threadgroup: whether @leader points to the whole process or a single task 2603 * @mgctx: migration context 2604 * 2605 * Migrate a process or task denoted by @leader. If migrating a process, 2606 * the caller must be holding cgroup_threadgroup_rwsem. The caller is also 2607 * responsible for invoking cgroup_migrate_add_src() and 2608 * cgroup_migrate_prepare_dst() on the targets before invoking this 2609 * function and following up with cgroup_migrate_finish(). 2610 * 2611 * As long as a controller's ->can_attach() doesn't fail, this function is 2612 * guaranteed to succeed. This means that, excluding ->can_attach() 2613 * failure, when migrating multiple targets, the success or failure can be 2614 * decided for all targets by invoking group_migrate_prepare_dst() before 2615 * actually starting migrating. 2616 */ 2617 int cgroup_migrate(struct task_struct *leader, bool threadgroup, 2618 struct cgroup_mgctx *mgctx) 2619 { 2620 struct task_struct *task; 2621 2622 /* 2623 * Prevent freeing of tasks while we take a snapshot. Tasks that are 2624 * already PF_EXITING could be freed from underneath us unless we 2625 * take an rcu_read_lock. 2626 */ 2627 spin_lock_irq(&css_set_lock); 2628 rcu_read_lock(); 2629 task = leader; 2630 do { 2631 cgroup_migrate_add_task(task, mgctx); 2632 if (!threadgroup) 2633 break; 2634 } while_each_thread(leader, task); 2635 rcu_read_unlock(); 2636 spin_unlock_irq(&css_set_lock); 2637 2638 return cgroup_migrate_execute(mgctx); 2639 } 2640 2641 /** 2642 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup 2643 * @dst_cgrp: the cgroup to attach to 2644 * @leader: the task or the leader of the threadgroup to be attached 2645 * @threadgroup: attach the whole threadgroup? 2646 * 2647 * Call holding cgroup_mutex and cgroup_threadgroup_rwsem. 2648 */ 2649 int cgroup_attach_task(struct cgroup *dst_cgrp, struct task_struct *leader, 2650 bool threadgroup) 2651 { 2652 DEFINE_CGROUP_MGCTX(mgctx); 2653 struct task_struct *task; 2654 int ret; 2655 2656 ret = cgroup_migrate_vet_dst(dst_cgrp); 2657 if (ret) 2658 return ret; 2659 2660 /* look up all src csets */ 2661 spin_lock_irq(&css_set_lock); 2662 rcu_read_lock(); 2663 task = leader; 2664 do { 2665 cgroup_migrate_add_src(task_css_set(task), dst_cgrp, &mgctx); 2666 if (!threadgroup) 2667 break; 2668 } while_each_thread(leader, task); 2669 rcu_read_unlock(); 2670 spin_unlock_irq(&css_set_lock); 2671 2672 /* prepare dst csets and commit */ 2673 ret = cgroup_migrate_prepare_dst(&mgctx); 2674 if (!ret) 2675 ret = cgroup_migrate(leader, threadgroup, &mgctx); 2676 2677 cgroup_migrate_finish(&mgctx); 2678 2679 if (!ret) 2680 TRACE_CGROUP_PATH(attach_task, dst_cgrp, leader, threadgroup); 2681 2682 return ret; 2683 } 2684 2685 struct task_struct *cgroup_procs_write_start(char *buf, bool threadgroup) 2686 __acquires(&cgroup_threadgroup_rwsem) 2687 { 2688 struct task_struct *tsk; 2689 pid_t pid; 2690 2691 if (kstrtoint(strstrip(buf), 0, &pid) || pid < 0) 2692 return ERR_PTR(-EINVAL); 2693 2694 percpu_down_write(&cgroup_threadgroup_rwsem); 2695 2696 rcu_read_lock(); 2697 if (pid) { 2698 tsk = find_task_by_vpid(pid); 2699 if (!tsk) { 2700 tsk = ERR_PTR(-ESRCH); 2701 goto out_unlock_threadgroup; 2702 } 2703 } else { 2704 tsk = current; 2705 } 2706 2707 if (threadgroup) 2708 tsk = tsk->group_leader; 2709 2710 /* 2711 * kthreads may acquire PF_NO_SETAFFINITY during initialization. 2712 * If userland migrates such a kthread to a non-root cgroup, it can 2713 * become trapped in a cpuset, or RT kthread may be born in a 2714 * cgroup with no rt_runtime allocated. Just say no. 2715 */ 2716 if (tsk->no_cgroup_migration || (tsk->flags & PF_NO_SETAFFINITY)) { 2717 tsk = ERR_PTR(-EINVAL); 2718 goto out_unlock_threadgroup; 2719 } 2720 2721 get_task_struct(tsk); 2722 goto out_unlock_rcu; 2723 2724 out_unlock_threadgroup: 2725 percpu_up_write(&cgroup_threadgroup_rwsem); 2726 out_unlock_rcu: 2727 rcu_read_unlock(); 2728 return tsk; 2729 } 2730 2731 void cgroup_procs_write_finish(struct task_struct *task) 2732 __releases(&cgroup_threadgroup_rwsem) 2733 { 2734 struct cgroup_subsys *ss; 2735 int ssid; 2736 2737 /* release reference from cgroup_procs_write_start() */ 2738 put_task_struct(task); 2739 2740 percpu_up_write(&cgroup_threadgroup_rwsem); 2741 for_each_subsys(ss, ssid) 2742 if (ss->post_attach) 2743 ss->post_attach(); 2744 } 2745 2746 static void cgroup_print_ss_mask(struct seq_file *seq, u16 ss_mask) 2747 { 2748 struct cgroup_subsys *ss; 2749 bool printed = false; 2750 int ssid; 2751 2752 do_each_subsys_mask(ss, ssid, ss_mask) { 2753 if (printed) 2754 seq_putc(seq, ' '); 2755 seq_printf(seq, "%s", ss->name); 2756 printed = true; 2757 } while_each_subsys_mask(); 2758 if (printed) 2759 seq_putc(seq, '\n'); 2760 } 2761 2762 /* show controllers which are enabled from the parent */ 2763 static int cgroup_controllers_show(struct seq_file *seq, void *v) 2764 { 2765 struct cgroup *cgrp = seq_css(seq)->cgroup; 2766 2767 cgroup_print_ss_mask(seq, cgroup_control(cgrp)); 2768 return 0; 2769 } 2770 2771 /* show controllers which are enabled for a given cgroup's children */ 2772 static int cgroup_subtree_control_show(struct seq_file *seq, void *v) 2773 { 2774 struct cgroup *cgrp = seq_css(seq)->cgroup; 2775 2776 cgroup_print_ss_mask(seq, cgrp->subtree_control); 2777 return 0; 2778 } 2779 2780 /** 2781 * cgroup_update_dfl_csses - update css assoc of a subtree in default hierarchy 2782 * @cgrp: root of the subtree to update csses for 2783 * 2784 * @cgrp's control masks have changed and its subtree's css associations 2785 * need to be updated accordingly. This function looks up all css_sets 2786 * which are attached to the subtree, creates the matching updated css_sets 2787 * and migrates the tasks to the new ones. 2788 */ 2789 static int cgroup_update_dfl_csses(struct cgroup *cgrp) 2790 { 2791 DEFINE_CGROUP_MGCTX(mgctx); 2792 struct cgroup_subsys_state *d_css; 2793 struct cgroup *dsct; 2794 struct css_set *src_cset; 2795 int ret; 2796 2797 lockdep_assert_held(&cgroup_mutex); 2798 2799 percpu_down_write(&cgroup_threadgroup_rwsem); 2800 2801 /* look up all csses currently attached to @cgrp's subtree */ 2802 spin_lock_irq(&css_set_lock); 2803 cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) { 2804 struct cgrp_cset_link *link; 2805 2806 list_for_each_entry(link, &dsct->cset_links, cset_link) 2807 cgroup_migrate_add_src(link->cset, dsct, &mgctx); 2808 } 2809 spin_unlock_irq(&css_set_lock); 2810 2811 /* NULL dst indicates self on default hierarchy */ 2812 ret = cgroup_migrate_prepare_dst(&mgctx); 2813 if (ret) 2814 goto out_finish; 2815 2816 spin_lock_irq(&css_set_lock); 2817 list_for_each_entry(src_cset, &mgctx.preloaded_src_csets, mg_preload_node) { 2818 struct task_struct *task, *ntask; 2819 2820 /* all tasks in src_csets need to be migrated */ 2821 list_for_each_entry_safe(task, ntask, &src_cset->tasks, cg_list) 2822 cgroup_migrate_add_task(task, &mgctx); 2823 } 2824 spin_unlock_irq(&css_set_lock); 2825 2826 ret = cgroup_migrate_execute(&mgctx); 2827 out_finish: 2828 cgroup_migrate_finish(&mgctx); 2829 percpu_up_write(&cgroup_threadgroup_rwsem); 2830 return ret; 2831 } 2832 2833 /** 2834 * cgroup_lock_and_drain_offline - lock cgroup_mutex and drain offlined csses 2835 * @cgrp: root of the target subtree 2836 * 2837 * Because css offlining is asynchronous, userland may try to re-enable a 2838 * controller while the previous css is still around. This function grabs 2839 * cgroup_mutex and drains the previous css instances of @cgrp's subtree. 2840 */ 2841 void cgroup_lock_and_drain_offline(struct cgroup *cgrp) 2842 __acquires(&cgroup_mutex) 2843 { 2844 struct cgroup *dsct; 2845 struct cgroup_subsys_state *d_css; 2846 struct cgroup_subsys *ss; 2847 int ssid; 2848 2849 restart: 2850 mutex_lock(&cgroup_mutex); 2851 2852 cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) { 2853 for_each_subsys(ss, ssid) { 2854 struct cgroup_subsys_state *css = cgroup_css(dsct, ss); 2855 DEFINE_WAIT(wait); 2856 2857 if (!css || !percpu_ref_is_dying(&css->refcnt)) 2858 continue; 2859 2860 cgroup_get_live(dsct); 2861 prepare_to_wait(&dsct->offline_waitq, &wait, 2862 TASK_UNINTERRUPTIBLE); 2863 2864 mutex_unlock(&cgroup_mutex); 2865 schedule(); 2866 finish_wait(&dsct->offline_waitq, &wait); 2867 2868 cgroup_put(dsct); 2869 goto restart; 2870 } 2871 } 2872 } 2873 2874 /** 2875 * cgroup_save_control - save control masks and dom_cgrp of a subtree 2876 * @cgrp: root of the target subtree 2877 * 2878 * Save ->subtree_control, ->subtree_ss_mask and ->dom_cgrp to the 2879 * respective old_ prefixed fields for @cgrp's subtree including @cgrp 2880 * itself. 2881 */ 2882 static void cgroup_save_control(struct cgroup *cgrp) 2883 { 2884 struct cgroup *dsct; 2885 struct cgroup_subsys_state *d_css; 2886 2887 cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) { 2888 dsct->old_subtree_control = dsct->subtree_control; 2889 dsct->old_subtree_ss_mask = dsct->subtree_ss_mask; 2890 dsct->old_dom_cgrp = dsct->dom_cgrp; 2891 } 2892 } 2893 2894 /** 2895 * cgroup_propagate_control - refresh control masks of a subtree 2896 * @cgrp: root of the target subtree 2897 * 2898 * For @cgrp and its subtree, ensure ->subtree_ss_mask matches 2899 * ->subtree_control and propagate controller availability through the 2900 * subtree so that descendants don't have unavailable controllers enabled. 2901 */ 2902 static void cgroup_propagate_control(struct cgroup *cgrp) 2903 { 2904 struct cgroup *dsct; 2905 struct cgroup_subsys_state *d_css; 2906 2907 cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) { 2908 dsct->subtree_control &= cgroup_control(dsct); 2909 dsct->subtree_ss_mask = 2910 cgroup_calc_subtree_ss_mask(dsct->subtree_control, 2911 cgroup_ss_mask(dsct)); 2912 } 2913 } 2914 2915 /** 2916 * cgroup_restore_control - restore control masks and dom_cgrp of a subtree 2917 * @cgrp: root of the target subtree 2918 * 2919 * Restore ->subtree_control, ->subtree_ss_mask and ->dom_cgrp from the 2920 * respective old_ prefixed fields for @cgrp's subtree including @cgrp 2921 * itself. 2922 */ 2923 static void cgroup_restore_control(struct cgroup *cgrp) 2924 { 2925 struct cgroup *dsct; 2926 struct cgroup_subsys_state *d_css; 2927 2928 cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) { 2929 dsct->subtree_control = dsct->old_subtree_control; 2930 dsct->subtree_ss_mask = dsct->old_subtree_ss_mask; 2931 dsct->dom_cgrp = dsct->old_dom_cgrp; 2932 } 2933 } 2934 2935 static bool css_visible(struct cgroup_subsys_state *css) 2936 { 2937 struct cgroup_subsys *ss = css->ss; 2938 struct cgroup *cgrp = css->cgroup; 2939 2940 if (cgroup_control(cgrp) & (1 << ss->id)) 2941 return true; 2942 if (!(cgroup_ss_mask(cgrp) & (1 << ss->id))) 2943 return false; 2944 return cgroup_on_dfl(cgrp) && ss->implicit_on_dfl; 2945 } 2946 2947 /** 2948 * cgroup_apply_control_enable - enable or show csses according to control 2949 * @cgrp: root of the target subtree 2950 * 2951 * Walk @cgrp's subtree and create new csses or make the existing ones 2952 * visible. A css is created invisible if it's being implicitly enabled 2953 * through dependency. An invisible css is made visible when the userland 2954 * explicitly enables it. 2955 * 2956 * Returns 0 on success, -errno on failure. On failure, csses which have 2957 * been processed already aren't cleaned up. The caller is responsible for 2958 * cleaning up with cgroup_apply_control_disable(). 2959 */ 2960 static int cgroup_apply_control_enable(struct cgroup *cgrp) 2961 { 2962 struct cgroup *dsct; 2963 struct cgroup_subsys_state *d_css; 2964 struct cgroup_subsys *ss; 2965 int ssid, ret; 2966 2967 cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) { 2968 for_each_subsys(ss, ssid) { 2969 struct cgroup_subsys_state *css = cgroup_css(dsct, ss); 2970 2971 WARN_ON_ONCE(css && percpu_ref_is_dying(&css->refcnt)); 2972 2973 if (!(cgroup_ss_mask(dsct) & (1 << ss->id))) 2974 continue; 2975 2976 if (!css) { 2977 css = css_create(dsct, ss); 2978 if (IS_ERR(css)) 2979 return PTR_ERR(css); 2980 } 2981 2982 if (css_visible(css)) { 2983 ret = css_populate_dir(css); 2984 if (ret) 2985 return ret; 2986 } 2987 } 2988 } 2989 2990 return 0; 2991 } 2992 2993 /** 2994 * cgroup_apply_control_disable - kill or hide csses according to control 2995 * @cgrp: root of the target subtree 2996 * 2997 * Walk @cgrp's subtree and kill and hide csses so that they match 2998 * cgroup_ss_mask() and cgroup_visible_mask(). 2999 * 3000 * A css is hidden when the userland requests it to be disabled while other 3001 * subsystems are still depending on it. The css must not actively control 3002 * resources and be in the vanilla state if it's made visible again later. 3003 * Controllers which may be depended upon should provide ->css_reset() for 3004 * this purpose. 3005 */ 3006 static void cgroup_apply_control_disable(struct cgroup *cgrp) 3007 { 3008 struct cgroup *dsct; 3009 struct cgroup_subsys_state *d_css; 3010 struct cgroup_subsys *ss; 3011 int ssid; 3012 3013 cgroup_for_each_live_descendant_post(dsct, d_css, cgrp) { 3014 for_each_subsys(ss, ssid) { 3015 struct cgroup_subsys_state *css = cgroup_css(dsct, ss); 3016 3017 WARN_ON_ONCE(css && percpu_ref_is_dying(&css->refcnt)); 3018 3019 if (!css) 3020 continue; 3021 3022 if (css->parent && 3023 !(cgroup_ss_mask(dsct) & (1 << ss->id))) { 3024 kill_css(css); 3025 } else if (!css_visible(css)) { 3026 css_clear_dir(css); 3027 if (ss->css_reset) 3028 ss->css_reset(css); 3029 } 3030 } 3031 } 3032 } 3033 3034 /** 3035 * cgroup_apply_control - apply control mask updates to the subtree 3036 * @cgrp: root of the target subtree 3037 * 3038 * subsystems can be enabled and disabled in a subtree using the following 3039 * steps. 3040 * 3041 * 1. Call cgroup_save_control() to stash the current state. 3042 * 2. Update ->subtree_control masks in the subtree as desired. 3043 * 3. Call cgroup_apply_control() to apply the changes. 3044 * 4. Optionally perform other related operations. 3045 * 5. Call cgroup_finalize_control() to finish up. 3046 * 3047 * This function implements step 3 and propagates the mask changes 3048 * throughout @cgrp's subtree, updates csses accordingly and perform 3049 * process migrations. 3050 */ 3051 static int cgroup_apply_control(struct cgroup *cgrp) 3052 { 3053 int ret; 3054 3055 cgroup_propagate_control(cgrp); 3056 3057 ret = cgroup_apply_control_enable(cgrp); 3058 if (ret) 3059 return ret; 3060 3061 /* 3062 * At this point, cgroup_e_css_by_mask() results reflect the new csses 3063 * making the following cgroup_update_dfl_csses() properly update 3064 * css associations of all tasks in the subtree. 3065 */ 3066 ret = cgroup_update_dfl_csses(cgrp); 3067 if (ret) 3068 return ret; 3069 3070 return 0; 3071 } 3072 3073 /** 3074 * cgroup_finalize_control - finalize control mask update 3075 * @cgrp: root of the target subtree 3076 * @ret: the result of the update 3077 * 3078 * Finalize control mask update. See cgroup_apply_control() for more info. 3079 */ 3080 static void cgroup_finalize_control(struct cgroup *cgrp, int ret) 3081 { 3082 if (ret) { 3083 cgroup_restore_control(cgrp); 3084 cgroup_propagate_control(cgrp); 3085 } 3086 3087 cgroup_apply_control_disable(cgrp); 3088 } 3089 3090 static int cgroup_vet_subtree_control_enable(struct cgroup *cgrp, u16 enable) 3091 { 3092 u16 domain_enable = enable & ~cgrp_dfl_threaded_ss_mask; 3093 3094 /* if nothing is getting enabled, nothing to worry about */ 3095 if (!enable) 3096 return 0; 3097 3098 /* can @cgrp host any resources? */ 3099 if (!cgroup_is_valid_domain(cgrp->dom_cgrp)) 3100 return -EOPNOTSUPP; 3101 3102 /* mixables don't care */ 3103 if (cgroup_is_mixable(cgrp)) 3104 return 0; 3105 3106 if (domain_enable) { 3107 /* can't enable domain controllers inside a thread subtree */ 3108 if (cgroup_is_thread_root(cgrp) || cgroup_is_threaded(cgrp)) 3109 return -EOPNOTSUPP; 3110 } else { 3111 /* 3112 * Threaded controllers can handle internal competitions 3113 * and are always allowed inside a (prospective) thread 3114 * subtree. 3115 */ 3116 if (cgroup_can_be_thread_root(cgrp) || cgroup_is_threaded(cgrp)) 3117 return 0; 3118 } 3119 3120 /* 3121 * Controllers can't be enabled for a cgroup with tasks to avoid 3122 * child cgroups competing against tasks. 3123 */ 3124 if (cgroup_has_tasks(cgrp)) 3125 return -EBUSY; 3126 3127 return 0; 3128 } 3129 3130 /* change the enabled child controllers for a cgroup in the default hierarchy */ 3131 static ssize_t cgroup_subtree_control_write(struct kernfs_open_file *of, 3132 char *buf, size_t nbytes, 3133 loff_t off) 3134 { 3135 u16 enable = 0, disable = 0; 3136 struct cgroup *cgrp, *child; 3137 struct cgroup_subsys *ss; 3138 char *tok; 3139 int ssid, ret; 3140 3141 /* 3142 * Parse input - space separated list of subsystem names prefixed 3143 * with either + or -. 3144 */ 3145 buf = strstrip(buf); 3146 while ((tok = strsep(&buf, " "))) { 3147 if (tok[0] == '\0') 3148 continue; 3149 do_each_subsys_mask(ss, ssid, ~cgrp_dfl_inhibit_ss_mask) { 3150 if (!cgroup_ssid_enabled(ssid) || 3151 strcmp(tok + 1, ss->name)) 3152 continue; 3153 3154 if (*tok == '+') { 3155 enable |= 1 << ssid; 3156 disable &= ~(1 << ssid); 3157 } else if (*tok == '-') { 3158 disable |= 1 << ssid; 3159 enable &= ~(1 << ssid); 3160 } else { 3161 return -EINVAL; 3162 } 3163 break; 3164 } while_each_subsys_mask(); 3165 if (ssid == CGROUP_SUBSYS_COUNT) 3166 return -EINVAL; 3167 } 3168 3169 cgrp = cgroup_kn_lock_live(of->kn, true); 3170 if (!cgrp) 3171 return -ENODEV; 3172 3173 for_each_subsys(ss, ssid) { 3174 if (enable & (1 << ssid)) { 3175 if (cgrp->subtree_control & (1 << ssid)) { 3176 enable &= ~(1 << ssid); 3177 continue; 3178 } 3179 3180 if (!(cgroup_control(cgrp) & (1 << ssid))) { 3181 ret = -ENOENT; 3182 goto out_unlock; 3183 } 3184 } else if (disable & (1 << ssid)) { 3185 if (!(cgrp->subtree_control & (1 << ssid))) { 3186 disable &= ~(1 << ssid); 3187 continue; 3188 } 3189 3190 /* a child has it enabled? */ 3191 cgroup_for_each_live_child(child, cgrp) { 3192 if (child->subtree_control & (1 << ssid)) { 3193 ret = -EBUSY; 3194 goto out_unlock; 3195 } 3196 } 3197 } 3198 } 3199 3200 if (!enable && !disable) { 3201 ret = 0; 3202 goto out_unlock; 3203 } 3204 3205 ret = cgroup_vet_subtree_control_enable(cgrp, enable); 3206 if (ret) 3207 goto out_unlock; 3208 3209 /* save and update control masks and prepare csses */ 3210 cgroup_save_control(cgrp); 3211 3212 cgrp->subtree_control |= enable; 3213 cgrp->subtree_control &= ~disable; 3214 3215 ret = cgroup_apply_control(cgrp); 3216 cgroup_finalize_control(cgrp, ret); 3217 if (ret) 3218 goto out_unlock; 3219 3220 kernfs_activate(cgrp->kn); 3221 out_unlock: 3222 cgroup_kn_unlock(of->kn); 3223 return ret ?: nbytes; 3224 } 3225 3226 /** 3227 * cgroup_enable_threaded - make @cgrp threaded 3228 * @cgrp: the target cgroup 3229 * 3230 * Called when "threaded" is written to the cgroup.type interface file and 3231 * tries to make @cgrp threaded and join the parent's resource domain. 3232 * This function is never called on the root cgroup as cgroup.type doesn't 3233 * exist on it. 3234 */ 3235 static int cgroup_enable_threaded(struct cgroup *cgrp) 3236 { 3237 struct cgroup *parent = cgroup_parent(cgrp); 3238 struct cgroup *dom_cgrp = parent->dom_cgrp; 3239 struct cgroup *dsct; 3240 struct cgroup_subsys_state *d_css; 3241 int ret; 3242 3243 lockdep_assert_held(&cgroup_mutex); 3244 3245 /* noop if already threaded */ 3246 if (cgroup_is_threaded(cgrp)) 3247 return 0; 3248 3249 /* 3250 * If @cgroup is populated or has domain controllers enabled, it 3251 * can't be switched. While the below cgroup_can_be_thread_root() 3252 * test can catch the same conditions, that's only when @parent is 3253 * not mixable, so let's check it explicitly. 3254 */ 3255 if (cgroup_is_populated(cgrp) || 3256 cgrp->subtree_control & ~cgrp_dfl_threaded_ss_mask) 3257 return -EOPNOTSUPP; 3258 3259 /* we're joining the parent's domain, ensure its validity */ 3260 if (!cgroup_is_valid_domain(dom_cgrp) || 3261 !cgroup_can_be_thread_root(dom_cgrp)) 3262 return -EOPNOTSUPP; 3263 3264 /* 3265 * The following shouldn't cause actual migrations and should 3266 * always succeed. 3267 */ 3268 cgroup_save_control(cgrp); 3269 3270 cgroup_for_each_live_descendant_pre(dsct, d_css, cgrp) 3271 if (dsct == cgrp || cgroup_is_threaded(dsct)) 3272 dsct->dom_cgrp = dom_cgrp; 3273 3274 ret = cgroup_apply_control(cgrp); 3275 if (!ret) 3276 parent->nr_threaded_children++; 3277 3278 cgroup_finalize_control(cgrp, ret); 3279 return ret; 3280 } 3281 3282 static int cgroup_type_show(struct seq_file *seq, void *v) 3283 { 3284 struct cgroup *cgrp = seq_css(seq)->cgroup; 3285 3286 if (cgroup_is_threaded(cgrp)) 3287 seq_puts(seq, "threaded\n"); 3288 else if (!cgroup_is_valid_domain(cgrp)) 3289 seq_puts(seq, "domain invalid\n"); 3290 else if (cgroup_is_thread_root(cgrp)) 3291 seq_puts(seq, "domain threaded\n"); 3292 else 3293 seq_puts(seq, "domain\n"); 3294 3295 return 0; 3296 } 3297 3298 static ssize_t cgroup_type_write(struct kernfs_open_file *of, char *buf, 3299 size_t nbytes, loff_t off) 3300 { 3301 struct cgroup *cgrp; 3302 int ret; 3303 3304 /* only switching to threaded mode is supported */ 3305 if (strcmp(strstrip(buf), "threaded")) 3306 return -EINVAL; 3307 3308 cgrp = cgroup_kn_lock_live(of->kn, false); 3309 if (!cgrp) 3310 return -ENOENT; 3311 3312 /* threaded can only be enabled */ 3313 ret = cgroup_enable_threaded(cgrp); 3314 3315 cgroup_kn_unlock(of->kn); 3316 return ret ?: nbytes; 3317 } 3318 3319 static int cgroup_max_descendants_show(struct seq_file *seq, void *v) 3320 { 3321 struct cgroup *cgrp = seq_css(seq)->cgroup; 3322 int descendants = READ_ONCE(cgrp->max_descendants); 3323 3324 if (descendants == INT_MAX) 3325 seq_puts(seq, "max\n"); 3326 else 3327 seq_printf(seq, "%d\n", descendants); 3328 3329 return 0; 3330 } 3331 3332 static ssize_t cgroup_max_descendants_write(struct kernfs_open_file *of, 3333 char *buf, size_t nbytes, loff_t off) 3334 { 3335 struct cgroup *cgrp; 3336 int descendants; 3337 ssize_t ret; 3338 3339 buf = strstrip(buf); 3340 if (!strcmp(buf, "max")) { 3341 descendants = INT_MAX; 3342 } else { 3343 ret = kstrtoint(buf, 0, &descendants); 3344 if (ret) 3345 return ret; 3346 } 3347 3348 if (descendants < 0) 3349 return -ERANGE; 3350 3351 cgrp = cgroup_kn_lock_live(of->kn, false); 3352 if (!cgrp) 3353 return -ENOENT; 3354 3355 cgrp->max_descendants = descendants; 3356 3357 cgroup_kn_unlock(of->kn); 3358 3359 return nbytes; 3360 } 3361 3362 static int cgroup_max_depth_show(struct seq_file *seq, void *v) 3363 { 3364 struct cgroup *cgrp = seq_css(seq)->cgroup; 3365 int depth = READ_ONCE(cgrp->max_depth); 3366 3367 if (depth == INT_MAX) 3368 seq_puts(seq, "max\n"); 3369 else 3370 seq_printf(seq, "%d\n", depth); 3371 3372 return 0; 3373 } 3374 3375 static ssize_t cgroup_max_depth_write(struct kernfs_open_file *of, 3376 char *buf, size_t nbytes, loff_t off) 3377 { 3378 struct cgroup *cgrp; 3379 ssize_t ret; 3380 int depth; 3381 3382 buf = strstrip(buf); 3383 if (!strcmp(buf, "max")) { 3384 depth = INT_MAX; 3385 } else { 3386 ret = kstrtoint(buf, 0, &depth); 3387 if (ret) 3388 return ret; 3389 } 3390 3391 if (depth < 0) 3392 return -ERANGE; 3393 3394 cgrp = cgroup_kn_lock_live(of->kn, false); 3395 if (!cgrp) 3396 return -ENOENT; 3397 3398 cgrp->max_depth = depth; 3399 3400 cgroup_kn_unlock(of->kn); 3401 3402 return nbytes; 3403 } 3404 3405 static int cgroup_events_show(struct seq_file *seq, void *v) 3406 { 3407 seq_printf(seq, "populated %d\n", 3408 cgroup_is_populated(seq_css(seq)->cgroup)); 3409 return 0; 3410 } 3411 3412 static int cgroup_stat_show(struct seq_file *seq, void *v) 3413 { 3414 struct cgroup *cgroup = seq_css(seq)->cgroup; 3415 3416 seq_printf(seq, "nr_descendants %d\n", 3417 cgroup->nr_descendants); 3418 seq_printf(seq, "nr_dying_descendants %d\n", 3419 cgroup->nr_dying_descendants); 3420 3421 return 0; 3422 } 3423 3424 static int __maybe_unused cgroup_extra_stat_show(struct seq_file *seq, 3425 struct cgroup *cgrp, int ssid) 3426 { 3427 struct cgroup_subsys *ss = cgroup_subsys[ssid]; 3428 struct cgroup_subsys_state *css; 3429 int ret; 3430 3431 if (!ss->css_extra_stat_show) 3432 return 0; 3433 3434 css = cgroup_tryget_css(cgrp, ss); 3435 if (!css) 3436 return 0; 3437 3438 ret = ss->css_extra_stat_show(seq, css); 3439 css_put(css); 3440 return ret; 3441 } 3442 3443 static int cpu_stat_show(struct seq_file *seq, void *v) 3444 { 3445 struct cgroup __maybe_unused *cgrp = seq_css(seq)->cgroup; 3446 int ret = 0; 3447 3448 cgroup_base_stat_cputime_show(seq); 3449 #ifdef CONFIG_CGROUP_SCHED 3450 ret = cgroup_extra_stat_show(seq, cgrp, cpu_cgrp_id); 3451 #endif 3452 return ret; 3453 } 3454 3455 #ifdef CONFIG_PSI 3456 static int cgroup_io_pressure_show(struct seq_file *seq, void *v) 3457 { 3458 return psi_show(seq, &seq_css(seq)->cgroup->psi, PSI_IO); 3459 } 3460 static int cgroup_memory_pressure_show(struct seq_file *seq, void *v) 3461 { 3462 return psi_show(seq, &seq_css(seq)->cgroup->psi, PSI_MEM); 3463 } 3464 static int cgroup_cpu_pressure_show(struct seq_file *seq, void *v) 3465 { 3466 return psi_show(seq, &seq_css(seq)->cgroup->psi, PSI_CPU); 3467 } 3468 #endif 3469 3470 static int cgroup_file_open(struct kernfs_open_file *of) 3471 { 3472 struct cftype *cft = of->kn->priv; 3473 3474 if (cft->open) 3475 return cft->open(of); 3476 return 0; 3477 } 3478 3479 static void cgroup_file_release(struct kernfs_open_file *of) 3480 { 3481 struct cftype *cft = of->kn->priv; 3482 3483 if (cft->release) 3484 cft->release(of); 3485 } 3486 3487 static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf, 3488 size_t nbytes, loff_t off) 3489 { 3490 struct cgroup_namespace *ns = current->nsproxy->cgroup_ns; 3491 struct cgroup *cgrp = of->kn->parent->priv; 3492 struct cftype *cft = of->kn->priv; 3493 struct cgroup_subsys_state *css; 3494 int ret; 3495 3496 /* 3497 * If namespaces are delegation boundaries, disallow writes to 3498 * files in an non-init namespace root from inside the namespace 3499 * except for the files explicitly marked delegatable - 3500 * cgroup.procs and cgroup.subtree_control. 3501 */ 3502 if ((cgrp->root->flags & CGRP_ROOT_NS_DELEGATE) && 3503 !(cft->flags & CFTYPE_NS_DELEGATABLE) && 3504 ns != &init_cgroup_ns && ns->root_cset->dfl_cgrp == cgrp) 3505 return -EPERM; 3506 3507 if (cft->write) 3508 return cft->write(of, buf, nbytes, off); 3509 3510 /* 3511 * kernfs guarantees that a file isn't deleted with operations in 3512 * flight, which means that the matching css is and stays alive and 3513 * doesn't need to be pinned. The RCU locking is not necessary 3514 * either. It's just for the convenience of using cgroup_css(). 3515 */ 3516 rcu_read_lock(); 3517 css = cgroup_css(cgrp, cft->ss); 3518 rcu_read_unlock(); 3519 3520 if (cft->write_u64) { 3521 unsigned long long v; 3522 ret = kstrtoull(buf, 0, &v); 3523 if (!ret) 3524 ret = cft->write_u64(css, cft, v); 3525 } else if (cft->write_s64) { 3526 long long v; 3527 ret = kstrtoll(buf, 0, &v); 3528 if (!ret) 3529 ret = cft->write_s64(css, cft, v); 3530 } else { 3531 ret = -EINVAL; 3532 } 3533 3534 return ret ?: nbytes; 3535 } 3536 3537 static __poll_t cgroup_file_poll(struct kernfs_open_file *of, poll_table *pt) 3538 { 3539 struct cftype *cft = of->kn->priv; 3540 3541 if (cft->poll) 3542 return cft->poll(of, pt); 3543 3544 return kernfs_generic_poll(of, pt); 3545 } 3546 3547 static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos) 3548 { 3549 return seq_cft(seq)->seq_start(seq, ppos); 3550 } 3551 3552 static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos) 3553 { 3554 return seq_cft(seq)->seq_next(seq, v, ppos); 3555 } 3556 3557 static void cgroup_seqfile_stop(struct seq_file *seq, void *v) 3558 { 3559 if (seq_cft(seq)->seq_stop) 3560 seq_cft(seq)->seq_stop(seq, v); 3561 } 3562 3563 static int cgroup_seqfile_show(struct seq_file *m, void *arg) 3564 { 3565 struct cftype *cft = seq_cft(m); 3566 struct cgroup_subsys_state *css = seq_css(m); 3567 3568 if (cft->seq_show) 3569 return cft->seq_show(m, arg); 3570 3571 if (cft->read_u64) 3572 seq_printf(m, "%llu\n", cft->read_u64(css, cft)); 3573 else if (cft->read_s64) 3574 seq_printf(m, "%lld\n", cft->read_s64(css, cft)); 3575 else 3576 return -EINVAL; 3577 return 0; 3578 } 3579 3580 static struct kernfs_ops cgroup_kf_single_ops = { 3581 .atomic_write_len = PAGE_SIZE, 3582 .open = cgroup_file_open, 3583 .release = cgroup_file_release, 3584 .write = cgroup_file_write, 3585 .poll = cgroup_file_poll, 3586 .seq_show = cgroup_seqfile_show, 3587 }; 3588 3589 static struct kernfs_ops cgroup_kf_ops = { 3590 .atomic_write_len = PAGE_SIZE, 3591 .open = cgroup_file_open, 3592 .release = cgroup_file_release, 3593 .write = cgroup_file_write, 3594 .poll = cgroup_file_poll, 3595 .seq_start = cgroup_seqfile_start, 3596 .seq_next = cgroup_seqfile_next, 3597 .seq_stop = cgroup_seqfile_stop, 3598 .seq_show = cgroup_seqfile_show, 3599 }; 3600 3601 /* set uid and gid of cgroup dirs and files to that of the creator */ 3602 static int cgroup_kn_set_ugid(struct kernfs_node *kn) 3603 { 3604 struct iattr iattr = { .ia_valid = ATTR_UID | ATTR_GID, 3605 .ia_uid = current_fsuid(), 3606 .ia_gid = current_fsgid(), }; 3607 3608 if (uid_eq(iattr.ia_uid, GLOBAL_ROOT_UID) && 3609 gid_eq(iattr.ia_gid, GLOBAL_ROOT_GID)) 3610 return 0; 3611 3612 return kernfs_setattr(kn, &iattr); 3613 } 3614 3615 static void cgroup_file_notify_timer(struct timer_list *timer) 3616 { 3617 cgroup_file_notify(container_of(timer, struct cgroup_file, 3618 notify_timer)); 3619 } 3620 3621 static int cgroup_add_file(struct cgroup_subsys_state *css, struct cgroup *cgrp, 3622 struct cftype *cft) 3623 { 3624 char name[CGROUP_FILE_NAME_MAX]; 3625 struct kernfs_node *kn; 3626 struct lock_class_key *key = NULL; 3627 int ret; 3628 3629 #ifdef CONFIG_DEBUG_LOCK_ALLOC 3630 key = &cft->lockdep_key; 3631 #endif 3632 kn = __kernfs_create_file(cgrp->kn, cgroup_file_name(cgrp, cft, name), 3633 cgroup_file_mode(cft), 3634 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, 3635 0, cft->kf_ops, cft, 3636 NULL, key); 3637 if (IS_ERR(kn)) 3638 return PTR_ERR(kn); 3639 3640 ret = cgroup_kn_set_ugid(kn); 3641 if (ret) { 3642 kernfs_remove(kn); 3643 return ret; 3644 } 3645 3646 if (cft->file_offset) { 3647 struct cgroup_file *cfile = (void *)css + cft->file_offset; 3648 3649 timer_setup(&cfile->notify_timer, cgroup_file_notify_timer, 0); 3650 3651 spin_lock_irq(&cgroup_file_kn_lock); 3652 cfile->kn = kn; 3653 spin_unlock_irq(&cgroup_file_kn_lock); 3654 } 3655 3656 return 0; 3657 } 3658 3659 /** 3660 * cgroup_addrm_files - add or remove files to a cgroup directory 3661 * @css: the target css 3662 * @cgrp: the target cgroup (usually css->cgroup) 3663 * @cfts: array of cftypes to be added 3664 * @is_add: whether to add or remove 3665 * 3666 * Depending on @is_add, add or remove files defined by @cfts on @cgrp. 3667 * For removals, this function never fails. 3668 */ 3669 static int cgroup_addrm_files(struct cgroup_subsys_state *css, 3670 struct cgroup *cgrp, struct cftype cfts[], 3671 bool is_add) 3672 { 3673 struct cftype *cft, *cft_end = NULL; 3674 int ret = 0; 3675 3676 lockdep_assert_held(&cgroup_mutex); 3677 3678 restart: 3679 for (cft = cfts; cft != cft_end && cft->name[0] != '\0'; cft++) { 3680 /* does cft->flags tell us to skip this file on @cgrp? */ 3681 if ((cft->flags & __CFTYPE_ONLY_ON_DFL) && !cgroup_on_dfl(cgrp)) 3682 continue; 3683 if ((cft->flags & __CFTYPE_NOT_ON_DFL) && cgroup_on_dfl(cgrp)) 3684 continue; 3685 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgroup_parent(cgrp)) 3686 continue; 3687 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgroup_parent(cgrp)) 3688 continue; 3689 if ((cft->flags & CFTYPE_DEBUG) && !cgroup_debug) 3690 continue; 3691 if (is_add) { 3692 ret = cgroup_add_file(css, cgrp, cft); 3693 if (ret) { 3694 pr_warn("%s: failed to add %s, err=%d\n", 3695 __func__, cft->name, ret); 3696 cft_end = cft; 3697 is_add = false; 3698 goto restart; 3699 } 3700 } else { 3701 cgroup_rm_file(cgrp, cft); 3702 } 3703 } 3704 return ret; 3705 } 3706 3707 static int cgroup_apply_cftypes(struct cftype *cfts, bool is_add) 3708 { 3709 struct cgroup_subsys *ss = cfts[0].ss; 3710 struct cgroup *root = &ss->root->cgrp; 3711 struct cgroup_subsys_state *css; 3712 int ret = 0; 3713 3714 lockdep_assert_held(&cgroup_mutex); 3715 3716 /* add/rm files for all cgroups created before */ 3717 css_for_each_descendant_pre(css, cgroup_css(root, ss)) { 3718 struct cgroup *cgrp = css->cgroup; 3719 3720 if (!(css->flags & CSS_VISIBLE)) 3721 continue; 3722 3723 ret = cgroup_addrm_files(css, cgrp, cfts, is_add); 3724 if (ret) 3725 break; 3726 } 3727 3728 if (is_add && !ret) 3729 kernfs_activate(root->kn); 3730 return ret; 3731 } 3732 3733 static void cgroup_exit_cftypes(struct cftype *cfts) 3734 { 3735 struct cftype *cft; 3736 3737 for (cft = cfts; cft->name[0] != '\0'; cft++) { 3738 /* free copy for custom atomic_write_len, see init_cftypes() */ 3739 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) 3740 kfree(cft->kf_ops); 3741 cft->kf_ops = NULL; 3742 cft->ss = NULL; 3743 3744 /* revert flags set by cgroup core while adding @cfts */ 3745 cft->flags &= ~(__CFTYPE_ONLY_ON_DFL | __CFTYPE_NOT_ON_DFL); 3746 } 3747 } 3748 3749 static int cgroup_init_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) 3750 { 3751 struct cftype *cft; 3752 3753 for (cft = cfts; cft->name[0] != '\0'; cft++) { 3754 struct kernfs_ops *kf_ops; 3755 3756 WARN_ON(cft->ss || cft->kf_ops); 3757 3758 if (cft->seq_start) 3759 kf_ops = &cgroup_kf_ops; 3760 else 3761 kf_ops = &cgroup_kf_single_ops; 3762 3763 /* 3764 * Ugh... if @cft wants a custom max_write_len, we need to 3765 * make a copy of kf_ops to set its atomic_write_len. 3766 */ 3767 if (cft->max_write_len && cft->max_write_len != PAGE_SIZE) { 3768 kf_ops = kmemdup(kf_ops, sizeof(*kf_ops), GFP_KERNEL); 3769 if (!kf_ops) { 3770 cgroup_exit_cftypes(cfts); 3771 return -ENOMEM; 3772 } 3773 kf_ops->atomic_write_len = cft->max_write_len; 3774 } 3775 3776 cft->kf_ops = kf_ops; 3777 cft->ss = ss; 3778 } 3779 3780 return 0; 3781 } 3782 3783 static int cgroup_rm_cftypes_locked(struct cftype *cfts) 3784 { 3785 lockdep_assert_held(&cgroup_mutex); 3786 3787 if (!cfts || !cfts[0].ss) 3788 return -ENOENT; 3789 3790 list_del(&cfts->node); 3791 cgroup_apply_cftypes(cfts, false); 3792 cgroup_exit_cftypes(cfts); 3793 return 0; 3794 } 3795 3796 /** 3797 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem 3798 * @cfts: zero-length name terminated array of cftypes 3799 * 3800 * Unregister @cfts. Files described by @cfts are removed from all 3801 * existing cgroups and all future cgroups won't have them either. This 3802 * function can be called anytime whether @cfts' subsys is attached or not. 3803 * 3804 * Returns 0 on successful unregistration, -ENOENT if @cfts is not 3805 * registered. 3806 */ 3807 int cgroup_rm_cftypes(struct cftype *cfts) 3808 { 3809 int ret; 3810 3811 mutex_lock(&cgroup_mutex); 3812 ret = cgroup_rm_cftypes_locked(cfts); 3813 mutex_unlock(&cgroup_mutex); 3814 return ret; 3815 } 3816 3817 /** 3818 * cgroup_add_cftypes - add an array of cftypes to a subsystem 3819 * @ss: target cgroup subsystem 3820 * @cfts: zero-length name terminated array of cftypes 3821 * 3822 * Register @cfts to @ss. Files described by @cfts are created for all 3823 * existing cgroups to which @ss is attached and all future cgroups will 3824 * have them too. This function can be called anytime whether @ss is 3825 * attached or not. 3826 * 3827 * Returns 0 on successful registration, -errno on failure. Note that this 3828 * function currently returns 0 as long as @cfts registration is successful 3829 * even if some file creation attempts on existing cgroups fail. 3830 */ 3831 static int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) 3832 { 3833 int ret; 3834 3835 if (!cgroup_ssid_enabled(ss->id)) 3836 return 0; 3837 3838 if (!cfts || cfts[0].name[0] == '\0') 3839 return 0; 3840 3841 ret = cgroup_init_cftypes(ss, cfts); 3842 if (ret) 3843 return ret; 3844 3845 mutex_lock(&cgroup_mutex); 3846 3847 list_add_tail(&cfts->node, &ss->cfts); 3848 ret = cgroup_apply_cftypes(cfts, true); 3849 if (ret) 3850 cgroup_rm_cftypes_locked(cfts); 3851 3852 mutex_unlock(&cgroup_mutex); 3853 return ret; 3854 } 3855 3856 /** 3857 * cgroup_add_dfl_cftypes - add an array of cftypes for default hierarchy 3858 * @ss: target cgroup subsystem 3859 * @cfts: zero-length name terminated array of cftypes 3860 * 3861 * Similar to cgroup_add_cftypes() but the added files are only used for 3862 * the default hierarchy. 3863 */ 3864 int cgroup_add_dfl_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) 3865 { 3866 struct cftype *cft; 3867 3868 for (cft = cfts; cft && cft->name[0] != '\0'; cft++) 3869 cft->flags |= __CFTYPE_ONLY_ON_DFL; 3870 return cgroup_add_cftypes(ss, cfts); 3871 } 3872 3873 /** 3874 * cgroup_add_legacy_cftypes - add an array of cftypes for legacy hierarchies 3875 * @ss: target cgroup subsystem 3876 * @cfts: zero-length name terminated array of cftypes 3877 * 3878 * Similar to cgroup_add_cftypes() but the added files are only used for 3879 * the legacy hierarchies. 3880 */ 3881 int cgroup_add_legacy_cftypes(struct cgroup_subsys *ss, struct cftype *cfts) 3882 { 3883 struct cftype *cft; 3884 3885 for (cft = cfts; cft && cft->name[0] != '\0'; cft++) 3886 cft->flags |= __CFTYPE_NOT_ON_DFL; 3887 return cgroup_add_cftypes(ss, cfts); 3888 } 3889 3890 /** 3891 * cgroup_file_notify - generate a file modified event for a cgroup_file 3892 * @cfile: target cgroup_file 3893 * 3894 * @cfile must have been obtained by setting cftype->file_offset. 3895 */ 3896 void cgroup_file_notify(struct cgroup_file *cfile) 3897 { 3898 unsigned long flags; 3899 3900 spin_lock_irqsave(&cgroup_file_kn_lock, flags); 3901 if (cfile->kn) { 3902 unsigned long last = cfile->notified_at; 3903 unsigned long next = last + CGROUP_FILE_NOTIFY_MIN_INTV; 3904 3905 if (time_in_range(jiffies, last, next)) { 3906 timer_reduce(&cfile->notify_timer, next); 3907 } else { 3908 kernfs_notify(cfile->kn); 3909 cfile->notified_at = jiffies; 3910 } 3911 } 3912 spin_unlock_irqrestore(&cgroup_file_kn_lock, flags); 3913 } 3914 3915 /** 3916 * css_next_child - find the next child of a given css 3917 * @pos: the current position (%NULL to initiate traversal) 3918 * @parent: css whose children to walk 3919 * 3920 * This function returns the next child of @parent and should be called 3921 * under either cgroup_mutex or RCU read lock. The only requirement is 3922 * that @parent and @pos are accessible. The next sibling is guaranteed to 3923 * be returned regardless of their states. 3924 * 3925 * If a subsystem synchronizes ->css_online() and the start of iteration, a 3926 * css which finished ->css_online() is guaranteed to be visible in the 3927 * future iterations and will stay visible until the last reference is put. 3928 * A css which hasn't finished ->css_online() or already finished 3929 * ->css_offline() may show up during traversal. It's each subsystem's 3930 * responsibility to synchronize against on/offlining. 3931 */ 3932 struct cgroup_subsys_state *css_next_child(struct cgroup_subsys_state *pos, 3933 struct cgroup_subsys_state *parent) 3934 { 3935 struct cgroup_subsys_state *next; 3936 3937 cgroup_assert_mutex_or_rcu_locked(); 3938 3939 /* 3940 * @pos could already have been unlinked from the sibling list. 3941 * Once a cgroup is removed, its ->sibling.next is no longer 3942 * updated when its next sibling changes. CSS_RELEASED is set when 3943 * @pos is taken off list, at which time its next pointer is valid, 3944 * and, as releases are serialized, the one pointed to by the next 3945 * pointer is guaranteed to not have started release yet. This 3946 * implies that if we observe !CSS_RELEASED on @pos in this RCU 3947 * critical section, the one pointed to by its next pointer is 3948 * guaranteed to not have finished its RCU grace period even if we 3949 * have dropped rcu_read_lock() inbetween iterations. 3950 * 3951 * If @pos has CSS_RELEASED set, its next pointer can't be 3952 * dereferenced; however, as each css is given a monotonically 3953 * increasing unique serial number and always appended to the 3954 * sibling list, the next one can be found by walking the parent's 3955 * children until the first css with higher serial number than 3956 * @pos's. While this path can be slower, it happens iff iteration 3957 * races against release and the race window is very small. 3958 */ 3959 if (!pos) { 3960 next = list_entry_rcu(parent->children.next, struct cgroup_subsys_state, sibling); 3961 } else if (likely(!(pos->flags & CSS_RELEASED))) { 3962 next = list_entry_rcu(pos->sibling.next, struct cgroup_subsys_state, sibling); 3963 } else { 3964 list_for_each_entry_rcu(next, &parent->children, sibling) 3965 if (next->serial_nr > pos->serial_nr) 3966 break; 3967 } 3968 3969 /* 3970 * @next, if not pointing to the head, can be dereferenced and is 3971 * the next sibling. 3972 */ 3973 if (&next->sibling != &parent->children) 3974 return next; 3975 return NULL; 3976 } 3977 3978 /** 3979 * css_next_descendant_pre - find the next descendant for pre-order walk 3980 * @pos: the current position (%NULL to initiate traversal) 3981 * @root: css whose descendants to walk 3982 * 3983 * To be used by css_for_each_descendant_pre(). Find the next descendant 3984 * to visit for pre-order traversal of @root's descendants. @root is 3985 * included in the iteration and the first node to be visited. 3986 * 3987 * While this function requires cgroup_mutex or RCU read locking, it 3988 * doesn't require the whole traversal to be contained in a single critical 3989 * section. This function will return the correct next descendant as long 3990 * as both @pos and @root are accessible and @pos is a descendant of @root. 3991 * 3992 * If a subsystem synchronizes ->css_online() and the start of iteration, a 3993 * css which finished ->css_online() is guaranteed to be visible in the 3994 * future iterations and will stay visible until the last reference is put. 3995 * A css which hasn't finished ->css_online() or already finished 3996 * ->css_offline() may show up during traversal. It's each subsystem's 3997 * responsibility to synchronize against on/offlining. 3998 */ 3999 struct cgroup_subsys_state * 4000 css_next_descendant_pre(struct cgroup_subsys_state *pos, 4001 struct cgroup_subsys_state *root) 4002 { 4003 struct cgroup_subsys_state *next; 4004 4005 cgroup_assert_mutex_or_rcu_locked(); 4006 4007 /* if first iteration, visit @root */ 4008 if (!pos) 4009 return root; 4010 4011 /* visit the first child if exists */ 4012 next = css_next_child(NULL, pos); 4013 if (next) 4014 return next; 4015 4016 /* no child, visit my or the closest ancestor's next sibling */ 4017 while (pos != root) { 4018 next = css_next_child(pos, pos->parent); 4019 if (next) 4020 return next; 4021 pos = pos->parent; 4022 } 4023 4024 return NULL; 4025 } 4026 4027 /** 4028 * css_rightmost_descendant - return the rightmost descendant of a css 4029 * @pos: css of interest 4030 * 4031 * Return the rightmost descendant of @pos. If there's no descendant, @pos 4032 * is returned. This can be used during pre-order traversal to skip 4033 * subtree of @pos. 4034 * 4035 * While this function requires cgroup_mutex or RCU read locking, it 4036 * doesn't require the whole traversal to be contained in a single critical 4037 * section. This function will return the correct rightmost descendant as 4038 * long as @pos is accessible. 4039 */ 4040 struct cgroup_subsys_state * 4041 css_rightmost_descendant(struct cgroup_subsys_state *pos) 4042 { 4043 struct cgroup_subsys_state *last, *tmp; 4044 4045 cgroup_assert_mutex_or_rcu_locked(); 4046 4047 do { 4048 last = pos; 4049 /* ->prev isn't RCU safe, walk ->next till the end */ 4050 pos = NULL; 4051 css_for_each_child(tmp, last) 4052 pos = tmp; 4053 } while (pos); 4054 4055 return last; 4056 } 4057 4058 static struct cgroup_subsys_state * 4059 css_leftmost_descendant(struct cgroup_subsys_state *pos) 4060 { 4061 struct cgroup_subsys_state *last; 4062 4063 do { 4064 last = pos; 4065 pos = css_next_child(NULL, pos); 4066 } while (pos); 4067 4068 return last; 4069 } 4070 4071 /** 4072 * css_next_descendant_post - find the next descendant for post-order walk 4073 * @pos: the current position (%NULL to initiate traversal) 4074 * @root: css whose descendants to walk 4075 * 4076 * To be used by css_for_each_descendant_post(). Find the next descendant 4077 * to visit for post-order traversal of @root's descendants. @root is 4078 * included in the iteration and the last node to be visited. 4079 * 4080 * While this function requires cgroup_mutex or RCU read locking, it 4081 * doesn't require the whole traversal to be contained in a single critical 4082 * section. This function will return the correct next descendant as long 4083 * as both @pos and @cgroup are accessible and @pos is a descendant of 4084 * @cgroup. 4085 * 4086 * If a subsystem synchronizes ->css_online() and the start of iteration, a 4087 * css which finished ->css_online() is guaranteed to be visible in the 4088 * future iterations and will stay visible until the last reference is put. 4089 * A css which hasn't finished ->css_online() or already finished 4090 * ->css_offline() may show up during traversal. It's each subsystem's 4091 * responsibility to synchronize against on/offlining. 4092 */ 4093 struct cgroup_subsys_state * 4094 css_next_descendant_post(struct cgroup_subsys_state *pos, 4095 struct cgroup_subsys_state *root) 4096 { 4097 struct cgroup_subsys_state *next; 4098 4099 cgroup_assert_mutex_or_rcu_locked(); 4100 4101 /* if first iteration, visit leftmost descendant which may be @root */ 4102 if (!pos) 4103 return css_leftmost_descendant(root); 4104 4105 /* if we visited @root, we're done */ 4106 if (pos == root) 4107 return NULL; 4108 4109 /* if there's an unvisited sibling, visit its leftmost descendant */ 4110 next = css_next_child(pos, pos->parent); 4111 if (next) 4112 return css_leftmost_descendant(next); 4113 4114 /* no sibling left, visit parent */ 4115 return pos->parent; 4116 } 4117 4118 /** 4119 * css_has_online_children - does a css have online children 4120 * @css: the target css 4121 * 4122 * Returns %true if @css has any online children; otherwise, %false. This 4123 * function can be called from any context but the caller is responsible 4124 * for synchronizing against on/offlining as necessary. 4125 */ 4126 bool css_has_online_children(struct cgroup_subsys_state *css) 4127 { 4128 struct cgroup_subsys_state *child; 4129 bool ret = false; 4130 4131 rcu_read_lock(); 4132 css_for_each_child(child, css) { 4133 if (child->flags & CSS_ONLINE) { 4134 ret = true; 4135 break; 4136 } 4137 } 4138 rcu_read_unlock(); 4139 return ret; 4140 } 4141 4142 static struct css_set *css_task_iter_next_css_set(struct css_task_iter *it) 4143 { 4144 struct list_head *l; 4145 struct cgrp_cset_link *link; 4146 struct css_set *cset; 4147 4148 lockdep_assert_held(&css_set_lock); 4149 4150 /* find the next threaded cset */ 4151 if (it->tcset_pos) { 4152 l = it->tcset_pos->next; 4153 4154 if (l != it->tcset_head) { 4155 it->tcset_pos = l; 4156 return container_of(l, struct css_set, 4157 threaded_csets_node); 4158 } 4159 4160 it->tcset_pos = NULL; 4161 } 4162 4163 /* find the next cset */ 4164 l = it->cset_pos; 4165 l = l->next; 4166 if (l == it->cset_head) { 4167 it->cset_pos = NULL; 4168 return NULL; 4169 } 4170 4171 if (it->ss) { 4172 cset = container_of(l, struct css_set, e_cset_node[it->ss->id]); 4173 } else { 4174 link = list_entry(l, struct cgrp_cset_link, cset_link); 4175 cset = link->cset; 4176 } 4177 4178 it->cset_pos = l; 4179 4180 /* initialize threaded css_set walking */ 4181 if (it->flags & CSS_TASK_ITER_THREADED) { 4182 if (it->cur_dcset) 4183 put_css_set_locked(it->cur_dcset); 4184 it->cur_dcset = cset; 4185 get_css_set(cset); 4186 4187 it->tcset_head = &cset->threaded_csets; 4188 it->tcset_pos = &cset->threaded_csets; 4189 } 4190 4191 return cset; 4192 } 4193 4194 /** 4195 * css_task_iter_advance_css_set - advance a task itererator to the next css_set 4196 * @it: the iterator to advance 4197 * 4198 * Advance @it to the next css_set to walk. 4199 */ 4200 static void css_task_iter_advance_css_set(struct css_task_iter *it) 4201 { 4202 struct css_set *cset; 4203 4204 lockdep_assert_held(&css_set_lock); 4205 4206 /* Advance to the next non-empty css_set */ 4207 do { 4208 cset = css_task_iter_next_css_set(it); 4209 if (!cset) { 4210 it->task_pos = NULL; 4211 return; 4212 } 4213 } while (!css_set_populated(cset)); 4214 4215 if (!list_empty(&cset->tasks)) 4216 it->task_pos = cset->tasks.next; 4217 else 4218 it->task_pos = cset->mg_tasks.next; 4219 4220 it->tasks_head = &cset->tasks; 4221 it->mg_tasks_head = &cset->mg_tasks; 4222 4223 /* 4224 * We don't keep css_sets locked across iteration steps and thus 4225 * need to take steps to ensure that iteration can be resumed after 4226 * the lock is re-acquired. Iteration is performed at two levels - 4227 * css_sets and tasks in them. 4228 * 4229 * Once created, a css_set never leaves its cgroup lists, so a 4230 * pinned css_set is guaranteed to stay put and we can resume 4231 * iteration afterwards. 4232 * 4233 * Tasks may leave @cset across iteration steps. This is resolved 4234 * by registering each iterator with the css_set currently being 4235 * walked and making css_set_move_task() advance iterators whose 4236 * next task is leaving. 4237 */ 4238 if (it->cur_cset) { 4239 list_del(&it->iters_node); 4240 put_css_set_locked(it->cur_cset); 4241 } 4242 get_css_set(cset); 4243 it->cur_cset = cset; 4244 list_add(&it->iters_node, &cset->task_iters); 4245 } 4246 4247 static void css_task_iter_advance(struct css_task_iter *it) 4248 { 4249 struct list_head *next; 4250 4251 lockdep_assert_held(&css_set_lock); 4252 repeat: 4253 if (it->task_pos) { 4254 /* 4255 * Advance iterator to find next entry. cset->tasks is 4256 * consumed first and then ->mg_tasks. After ->mg_tasks, 4257 * we move onto the next cset. 4258 */ 4259 next = it->task_pos->next; 4260 4261 if (next == it->tasks_head) 4262 next = it->mg_tasks_head->next; 4263 4264 if (next == it->mg_tasks_head) 4265 css_task_iter_advance_css_set(it); 4266 else 4267 it->task_pos = next; 4268 } else { 4269 /* called from start, proceed to the first cset */ 4270 css_task_iter_advance_css_set(it); 4271 } 4272 4273 /* if PROCS, skip over tasks which aren't group leaders */ 4274 if ((it->flags & CSS_TASK_ITER_PROCS) && it->task_pos && 4275 !thread_group_leader(list_entry(it->task_pos, struct task_struct, 4276 cg_list))) 4277 goto repeat; 4278 } 4279 4280 /** 4281 * css_task_iter_start - initiate task iteration 4282 * @css: the css to walk tasks of 4283 * @flags: CSS_TASK_ITER_* flags 4284 * @it: the task iterator to use 4285 * 4286 * Initiate iteration through the tasks of @css. The caller can call 4287 * css_task_iter_next() to walk through the tasks until the function 4288 * returns NULL. On completion of iteration, css_task_iter_end() must be 4289 * called. 4290 */ 4291 void css_task_iter_start(struct cgroup_subsys_state *css, unsigned int flags, 4292 struct css_task_iter *it) 4293 { 4294 /* no one should try to iterate before mounting cgroups */ 4295 WARN_ON_ONCE(!use_task_css_set_links); 4296 4297 memset(it, 0, sizeof(*it)); 4298 4299 spin_lock_irq(&css_set_lock); 4300 4301 it->ss = css->ss; 4302 it->flags = flags; 4303 4304 if (it->ss) 4305 it->cset_pos = &css->cgroup->e_csets[css->ss->id]; 4306 else 4307 it->cset_pos = &css->cgroup->cset_links; 4308 4309 it->cset_head = it->cset_pos; 4310 4311 css_task_iter_advance(it); 4312 4313 spin_unlock_irq(&css_set_lock); 4314 } 4315 4316 /** 4317 * css_task_iter_next - return the next task for the iterator 4318 * @it: the task iterator being iterated 4319 * 4320 * The "next" function for task iteration. @it should have been 4321 * initialized via css_task_iter_start(). Returns NULL when the iteration 4322 * reaches the end. 4323 */ 4324 struct task_struct *css_task_iter_next(struct css_task_iter *it) 4325 { 4326 if (it->cur_task) { 4327 put_task_struct(it->cur_task); 4328 it->cur_task = NULL; 4329 } 4330 4331 spin_lock_irq(&css_set_lock); 4332 4333 if (it->task_pos) { 4334 it->cur_task = list_entry(it->task_pos, struct task_struct, 4335 cg_list); 4336 get_task_struct(it->cur_task); 4337 css_task_iter_advance(it); 4338 } 4339 4340 spin_unlock_irq(&css_set_lock); 4341 4342 return it->cur_task; 4343 } 4344 4345 /** 4346 * css_task_iter_end - finish task iteration 4347 * @it: the task iterator to finish 4348 * 4349 * Finish task iteration started by css_task_iter_start(). 4350 */ 4351 void css_task_iter_end(struct css_task_iter *it) 4352 { 4353 if (it->cur_cset) { 4354 spin_lock_irq(&css_set_lock); 4355 list_del(&it->iters_node); 4356 put_css_set_locked(it->cur_cset); 4357 spin_unlock_irq(&css_set_lock); 4358 } 4359 4360 if (it->cur_dcset) 4361 put_css_set(it->cur_dcset); 4362 4363 if (it->cur_task) 4364 put_task_struct(it->cur_task); 4365 } 4366 4367 static void cgroup_procs_release(struct kernfs_open_file *of) 4368 { 4369 if (of->priv) { 4370 css_task_iter_end(of->priv); 4371 kfree(of->priv); 4372 } 4373 } 4374 4375 static void *cgroup_procs_next(struct seq_file *s, void *v, loff_t *pos) 4376 { 4377 struct kernfs_open_file *of = s->private; 4378 struct css_task_iter *it = of->priv; 4379 4380 return css_task_iter_next(it); 4381 } 4382 4383 static void *__cgroup_procs_start(struct seq_file *s, loff_t *pos, 4384 unsigned int iter_flags) 4385 { 4386 struct kernfs_open_file *of = s->private; 4387 struct cgroup *cgrp = seq_css(s)->cgroup; 4388 struct css_task_iter *it = of->priv; 4389 4390 /* 4391 * When a seq_file is seeked, it's always traversed sequentially 4392 * from position 0, so we can simply keep iterating on !0 *pos. 4393 */ 4394 if (!it) { 4395 if (WARN_ON_ONCE((*pos)++)) 4396 return ERR_PTR(-EINVAL); 4397 4398 it = kzalloc(sizeof(*it), GFP_KERNEL); 4399 if (!it) 4400 return ERR_PTR(-ENOMEM); 4401 of->priv = it; 4402 css_task_iter_start(&cgrp->self, iter_flags, it); 4403 } else if (!(*pos)++) { 4404 css_task_iter_end(it); 4405 css_task_iter_start(&cgrp->self, iter_flags, it); 4406 } 4407 4408 return cgroup_procs_next(s, NULL, NULL); 4409 } 4410 4411 static void *cgroup_procs_start(struct seq_file *s, loff_t *pos) 4412 { 4413 struct cgroup *cgrp = seq_css(s)->cgroup; 4414 4415 /* 4416 * All processes of a threaded subtree belong to the domain cgroup 4417 * of the subtree. Only threads can be distributed across the 4418 * subtree. Reject reads on cgroup.procs in the subtree proper. 4419 * They're always empty anyway. 4420 */ 4421 if (cgroup_is_threaded(cgrp)) 4422 return ERR_PTR(-EOPNOTSUPP); 4423 4424 return __cgroup_procs_start(s, pos, CSS_TASK_ITER_PROCS | 4425 CSS_TASK_ITER_THREADED); 4426 } 4427 4428 static int cgroup_procs_show(struct seq_file *s, void *v) 4429 { 4430 seq_printf(s, "%d\n", task_pid_vnr(v)); 4431 return 0; 4432 } 4433 4434 static int cgroup_procs_write_permission(struct cgroup *src_cgrp, 4435 struct cgroup *dst_cgrp, 4436 struct super_block *sb) 4437 { 4438 struct cgroup_namespace *ns = current->nsproxy->cgroup_ns; 4439 struct cgroup *com_cgrp = src_cgrp; 4440 struct inode *inode; 4441 int ret; 4442 4443 lockdep_assert_held(&cgroup_mutex); 4444 4445 /* find the common ancestor */ 4446 while (!cgroup_is_descendant(dst_cgrp, com_cgrp)) 4447 com_cgrp = cgroup_parent(com_cgrp); 4448 4449 /* %current should be authorized to migrate to the common ancestor */ 4450 inode = kernfs_get_inode(sb, com_cgrp->procs_file.kn); 4451 if (!inode) 4452 return -ENOMEM; 4453 4454 ret = inode_permission(inode, MAY_WRITE); 4455 iput(inode); 4456 if (ret) 4457 return ret; 4458 4459 /* 4460 * If namespaces are delegation boundaries, %current must be able 4461 * to see both source and destination cgroups from its namespace. 4462 */ 4463 if ((cgrp_dfl_root.flags & CGRP_ROOT_NS_DELEGATE) && 4464 (!cgroup_is_descendant(src_cgrp, ns->root_cset->dfl_cgrp) || 4465 !cgroup_is_descendant(dst_cgrp, ns->root_cset->dfl_cgrp))) 4466 return -ENOENT; 4467 4468 return 0; 4469 } 4470 4471 static ssize_t cgroup_procs_write(struct kernfs_open_file *of, 4472 char *buf, size_t nbytes, loff_t off) 4473 { 4474 struct cgroup *src_cgrp, *dst_cgrp; 4475 struct task_struct *task; 4476 ssize_t ret; 4477 4478 dst_cgrp = cgroup_kn_lock_live(of->kn, false); 4479 if (!dst_cgrp) 4480 return -ENODEV; 4481 4482 task = cgroup_procs_write_start(buf, true); 4483 ret = PTR_ERR_OR_ZERO(task); 4484 if (ret) 4485 goto out_unlock; 4486 4487 /* find the source cgroup */ 4488 spin_lock_irq(&css_set_lock); 4489 src_cgrp = task_cgroup_from_root(task, &cgrp_dfl_root); 4490 spin_unlock_irq(&css_set_lock); 4491 4492 ret = cgroup_procs_write_permission(src_cgrp, dst_cgrp, 4493 of->file->f_path.dentry->d_sb); 4494 if (ret) 4495 goto out_finish; 4496 4497 ret = cgroup_attach_task(dst_cgrp, task, true); 4498 4499 out_finish: 4500 cgroup_procs_write_finish(task); 4501 out_unlock: 4502 cgroup_kn_unlock(of->kn); 4503 4504 return ret ?: nbytes; 4505 } 4506 4507 static void *cgroup_threads_start(struct seq_file *s, loff_t *pos) 4508 { 4509 return __cgroup_procs_start(s, pos, 0); 4510 } 4511 4512 static ssize_t cgroup_threads_write(struct kernfs_open_file *of, 4513 char *buf, size_t nbytes, loff_t off) 4514 { 4515 struct cgroup *src_cgrp, *dst_cgrp; 4516 struct task_struct *task; 4517 ssize_t ret; 4518 4519 buf = strstrip(buf); 4520 4521 dst_cgrp = cgroup_kn_lock_live(of->kn, false); 4522 if (!dst_cgrp) 4523 return -ENODEV; 4524 4525 task = cgroup_procs_write_start(buf, false); 4526 ret = PTR_ERR_OR_ZERO(task); 4527 if (ret) 4528 goto out_unlock; 4529 4530 /* find the source cgroup */ 4531 spin_lock_irq(&css_set_lock); 4532 src_cgrp = task_cgroup_from_root(task, &cgrp_dfl_root); 4533 spin_unlock_irq(&css_set_lock); 4534 4535 /* thread migrations follow the cgroup.procs delegation rule */ 4536 ret = cgroup_procs_write_permission(src_cgrp, dst_cgrp, 4537 of->file->f_path.dentry->d_sb); 4538 if (ret) 4539 goto out_finish; 4540 4541 /* and must be contained in the same domain */ 4542 ret = -EOPNOTSUPP; 4543 if (src_cgrp->dom_cgrp != dst_cgrp->dom_cgrp) 4544 goto out_finish; 4545 4546 ret = cgroup_attach_task(dst_cgrp, task, false); 4547 4548 out_finish: 4549 cgroup_procs_write_finish(task); 4550 out_unlock: 4551 cgroup_kn_unlock(of->kn); 4552 4553 return ret ?: nbytes; 4554 } 4555 4556 /* cgroup core interface files for the default hierarchy */ 4557 static struct cftype cgroup_base_files[] = { 4558 { 4559 .name = "cgroup.type", 4560 .flags = CFTYPE_NOT_ON_ROOT, 4561 .seq_show = cgroup_type_show, 4562 .write = cgroup_type_write, 4563 }, 4564 { 4565 .name = "cgroup.procs", 4566 .flags = CFTYPE_NS_DELEGATABLE, 4567 .file_offset = offsetof(struct cgroup, procs_file), 4568 .release = cgroup_procs_release, 4569 .seq_start = cgroup_procs_start, 4570 .seq_next = cgroup_procs_next, 4571 .seq_show = cgroup_procs_show, 4572 .write = cgroup_procs_write, 4573 }, 4574 { 4575 .name = "cgroup.threads", 4576 .flags = CFTYPE_NS_DELEGATABLE, 4577 .release = cgroup_procs_release, 4578 .seq_start = cgroup_threads_start, 4579 .seq_next = cgroup_procs_next, 4580 .seq_show = cgroup_procs_show, 4581 .write = cgroup_threads_write, 4582 }, 4583 { 4584 .name = "cgroup.controllers", 4585 .seq_show = cgroup_controllers_show, 4586 }, 4587 { 4588 .name = "cgroup.subtree_control", 4589 .flags = CFTYPE_NS_DELEGATABLE, 4590 .seq_show = cgroup_subtree_control_show, 4591 .write = cgroup_subtree_control_write, 4592 }, 4593 { 4594 .name = "cgroup.events", 4595 .flags = CFTYPE_NOT_ON_ROOT, 4596 .file_offset = offsetof(struct cgroup, events_file), 4597 .seq_show = cgroup_events_show, 4598 }, 4599 { 4600 .name = "cgroup.max.descendants", 4601 .seq_show = cgroup_max_descendants_show, 4602 .write = cgroup_max_descendants_write, 4603 }, 4604 { 4605 .name = "cgroup.max.depth", 4606 .seq_show = cgroup_max_depth_show, 4607 .write = cgroup_max_depth_write, 4608 }, 4609 { 4610 .name = "cgroup.stat", 4611 .seq_show = cgroup_stat_show, 4612 }, 4613 { 4614 .name = "cpu.stat", 4615 .flags = CFTYPE_NOT_ON_ROOT, 4616 .seq_show = cpu_stat_show, 4617 }, 4618 #ifdef CONFIG_PSI 4619 { 4620 .name = "io.pressure", 4621 .flags = CFTYPE_NOT_ON_ROOT, 4622 .seq_show = cgroup_io_pressure_show, 4623 }, 4624 { 4625 .name = "memory.pressure", 4626 .flags = CFTYPE_NOT_ON_ROOT, 4627 .seq_show = cgroup_memory_pressure_show, 4628 }, 4629 { 4630 .name = "cpu.pressure", 4631 .flags = CFTYPE_NOT_ON_ROOT, 4632 .seq_show = cgroup_cpu_pressure_show, 4633 }, 4634 #endif 4635 { } /* terminate */ 4636 }; 4637 4638 /* 4639 * css destruction is four-stage process. 4640 * 4641 * 1. Destruction starts. Killing of the percpu_ref is initiated. 4642 * Implemented in kill_css(). 4643 * 4644 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs 4645 * and thus css_tryget_online() is guaranteed to fail, the css can be 4646 * offlined by invoking offline_css(). After offlining, the base ref is 4647 * put. Implemented in css_killed_work_fn(). 4648 * 4649 * 3. When the percpu_ref reaches zero, the only possible remaining 4650 * accessors are inside RCU read sections. css_release() schedules the 4651 * RCU callback. 4652 * 4653 * 4. After the grace period, the css can be freed. Implemented in 4654 * css_free_work_fn(). 4655 * 4656 * It is actually hairier because both step 2 and 4 require process context 4657 * and thus involve punting to css->destroy_work adding two additional 4658 * steps to the already complex sequence. 4659 */ 4660 static void css_free_rwork_fn(struct work_struct *work) 4661 { 4662 struct cgroup_subsys_state *css = container_of(to_rcu_work(work), 4663 struct cgroup_subsys_state, destroy_rwork); 4664 struct cgroup_subsys *ss = css->ss; 4665 struct cgroup *cgrp = css->cgroup; 4666 4667 percpu_ref_exit(&css->refcnt); 4668 4669 if (ss) { 4670 /* css free path */ 4671 struct cgroup_subsys_state *parent = css->parent; 4672 int id = css->id; 4673 4674 ss->css_free(css); 4675 cgroup_idr_remove(&ss->css_idr, id); 4676 cgroup_put(cgrp); 4677 4678 if (parent) 4679 css_put(parent); 4680 } else { 4681 /* cgroup free path */ 4682 atomic_dec(&cgrp->root->nr_cgrps); 4683 cgroup1_pidlist_destroy_all(cgrp); 4684 cancel_work_sync(&cgrp->release_agent_work); 4685 4686 if (cgroup_parent(cgrp)) { 4687 /* 4688 * We get a ref to the parent, and put the ref when 4689 * this cgroup is being freed, so it's guaranteed 4690 * that the parent won't be destroyed before its 4691 * children. 4692 */ 4693 cgroup_put(cgroup_parent(cgrp)); 4694 kernfs_put(cgrp->kn); 4695 psi_cgroup_free(cgrp); 4696 if (cgroup_on_dfl(cgrp)) 4697 cgroup_rstat_exit(cgrp); 4698 kfree(cgrp); 4699 } else { 4700 /* 4701 * This is root cgroup's refcnt reaching zero, 4702 * which indicates that the root should be 4703 * released. 4704 */ 4705 cgroup_destroy_root(cgrp->root); 4706 } 4707 } 4708 } 4709 4710 static void css_release_work_fn(struct work_struct *work) 4711 { 4712 struct cgroup_subsys_state *css = 4713 container_of(work, struct cgroup_subsys_state, destroy_work); 4714 struct cgroup_subsys *ss = css->ss; 4715 struct cgroup *cgrp = css->cgroup; 4716 4717 mutex_lock(&cgroup_mutex); 4718 4719 css->flags |= CSS_RELEASED; 4720 list_del_rcu(&css->sibling); 4721 4722 if (ss) { 4723 /* css release path */ 4724 if (!list_empty(&css->rstat_css_node)) { 4725 cgroup_rstat_flush(cgrp); 4726 list_del_rcu(&css->rstat_css_node); 4727 } 4728 4729 cgroup_idr_replace(&ss->css_idr, NULL, css->id); 4730 if (ss->css_released) 4731 ss->css_released(css); 4732 } else { 4733 struct cgroup *tcgrp; 4734 4735 /* cgroup release path */ 4736 TRACE_CGROUP_PATH(release, cgrp); 4737 4738 if (cgroup_on_dfl(cgrp)) 4739 cgroup_rstat_flush(cgrp); 4740 4741 for (tcgrp = cgroup_parent(cgrp); tcgrp; 4742 tcgrp = cgroup_parent(tcgrp)) 4743 tcgrp->nr_dying_descendants--; 4744 4745 cgroup_idr_remove(&cgrp->root->cgroup_idr, cgrp->id); 4746 cgrp->id = -1; 4747 4748 /* 4749 * There are two control paths which try to determine 4750 * cgroup from dentry without going through kernfs - 4751 * cgroupstats_build() and css_tryget_online_from_dir(). 4752 * Those are supported by RCU protecting clearing of 4753 * cgrp->kn->priv backpointer. 4754 */ 4755 if (cgrp->kn) 4756 RCU_INIT_POINTER(*(void __rcu __force **)&cgrp->kn->priv, 4757 NULL); 4758 4759 cgroup_bpf_put(cgrp); 4760 } 4761 4762 mutex_unlock(&cgroup_mutex); 4763 4764 INIT_RCU_WORK(&css->destroy_rwork, css_free_rwork_fn); 4765 queue_rcu_work(cgroup_destroy_wq, &css->destroy_rwork); 4766 } 4767 4768 static void css_release(struct percpu_ref *ref) 4769 { 4770 struct cgroup_subsys_state *css = 4771 container_of(ref, struct cgroup_subsys_state, refcnt); 4772 4773 INIT_WORK(&css->destroy_work, css_release_work_fn); 4774 queue_work(cgroup_destroy_wq, &css->destroy_work); 4775 } 4776 4777 static void init_and_link_css(struct cgroup_subsys_state *css, 4778 struct cgroup_subsys *ss, struct cgroup *cgrp) 4779 { 4780 lockdep_assert_held(&cgroup_mutex); 4781 4782 cgroup_get_live(cgrp); 4783 4784 memset(css, 0, sizeof(*css)); 4785 css->cgroup = cgrp; 4786 css->ss = ss; 4787 css->id = -1; 4788 INIT_LIST_HEAD(&css->sibling); 4789 INIT_LIST_HEAD(&css->children); 4790 INIT_LIST_HEAD(&css->rstat_css_node); 4791 css->serial_nr = css_serial_nr_next++; 4792 atomic_set(&css->online_cnt, 0); 4793 4794 if (cgroup_parent(cgrp)) { 4795 css->parent = cgroup_css(cgroup_parent(cgrp), ss); 4796 css_get(css->parent); 4797 } 4798 4799 if (cgroup_on_dfl(cgrp) && ss->css_rstat_flush) 4800 list_add_rcu(&css->rstat_css_node, &cgrp->rstat_css_list); 4801 4802 BUG_ON(cgroup_css(cgrp, ss)); 4803 } 4804 4805 /* invoke ->css_online() on a new CSS and mark it online if successful */ 4806 static int online_css(struct cgroup_subsys_state *css) 4807 { 4808 struct cgroup_subsys *ss = css->ss; 4809 int ret = 0; 4810 4811 lockdep_assert_held(&cgroup_mutex); 4812 4813 if (ss->css_online) 4814 ret = ss->css_online(css); 4815 if (!ret) { 4816 css->flags |= CSS_ONLINE; 4817 rcu_assign_pointer(css->cgroup->subsys[ss->id], css); 4818 4819 atomic_inc(&css->online_cnt); 4820 if (css->parent) 4821 atomic_inc(&css->parent->online_cnt); 4822 } 4823 return ret; 4824 } 4825 4826 /* if the CSS is online, invoke ->css_offline() on it and mark it offline */ 4827 static void offline_css(struct cgroup_subsys_state *css) 4828 { 4829 struct cgroup_subsys *ss = css->ss; 4830 4831 lockdep_assert_held(&cgroup_mutex); 4832 4833 if (!(css->flags & CSS_ONLINE)) 4834 return; 4835 4836 if (ss->css_offline) 4837 ss->css_offline(css); 4838 4839 css->flags &= ~CSS_ONLINE; 4840 RCU_INIT_POINTER(css->cgroup->subsys[ss->id], NULL); 4841 4842 wake_up_all(&css->cgroup->offline_waitq); 4843 } 4844 4845 /** 4846 * css_create - create a cgroup_subsys_state 4847 * @cgrp: the cgroup new css will be associated with 4848 * @ss: the subsys of new css 4849 * 4850 * Create a new css associated with @cgrp - @ss pair. On success, the new 4851 * css is online and installed in @cgrp. This function doesn't create the 4852 * interface files. Returns 0 on success, -errno on failure. 4853 */ 4854 static struct cgroup_subsys_state *css_create(struct cgroup *cgrp, 4855 struct cgroup_subsys *ss) 4856 { 4857 struct cgroup *parent = cgroup_parent(cgrp); 4858 struct cgroup_subsys_state *parent_css = cgroup_css(parent, ss); 4859 struct cgroup_subsys_state *css; 4860 int err; 4861 4862 lockdep_assert_held(&cgroup_mutex); 4863 4864 css = ss->css_alloc(parent_css); 4865 if (!css) 4866 css = ERR_PTR(-ENOMEM); 4867 if (IS_ERR(css)) 4868 return css; 4869 4870 init_and_link_css(css, ss, cgrp); 4871 4872 err = percpu_ref_init(&css->refcnt, css_release, 0, GFP_KERNEL); 4873 if (err) 4874 goto err_free_css; 4875 4876 err = cgroup_idr_alloc(&ss->css_idr, NULL, 2, 0, GFP_KERNEL); 4877 if (err < 0) 4878 goto err_free_css; 4879 css->id = err; 4880 4881 /* @css is ready to be brought online now, make it visible */ 4882 list_add_tail_rcu(&css->sibling, &parent_css->children); 4883 cgroup_idr_replace(&ss->css_idr, css, css->id); 4884 4885 err = online_css(css); 4886 if (err) 4887 goto err_list_del; 4888 4889 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy && 4890 cgroup_parent(parent)) { 4891 pr_warn("%s (%d) created nested cgroup for controller \"%s\" which has incomplete hierarchy support. Nested cgroups may change behavior in the future.\n", 4892 current->comm, current->pid, ss->name); 4893 if (!strcmp(ss->name, "memory")) 4894 pr_warn("\"memory\" requires setting use_hierarchy to 1 on the root\n"); 4895 ss->warned_broken_hierarchy = true; 4896 } 4897 4898 return css; 4899 4900 err_list_del: 4901 list_del_rcu(&css->sibling); 4902 err_free_css: 4903 list_del_rcu(&css->rstat_css_node); 4904 INIT_RCU_WORK(&css->destroy_rwork, css_free_rwork_fn); 4905 queue_rcu_work(cgroup_destroy_wq, &css->destroy_rwork); 4906 return ERR_PTR(err); 4907 } 4908 4909 /* 4910 * The returned cgroup is fully initialized including its control mask, but 4911 * it isn't associated with its kernfs_node and doesn't have the control 4912 * mask applied. 4913 */ 4914 static struct cgroup *cgroup_create(struct cgroup *parent) 4915 { 4916 struct cgroup_root *root = parent->root; 4917 struct cgroup *cgrp, *tcgrp; 4918 int level = parent->level + 1; 4919 int ret; 4920 4921 /* allocate the cgroup and its ID, 0 is reserved for the root */ 4922 cgrp = kzalloc(struct_size(cgrp, ancestor_ids, (level + 1)), 4923 GFP_KERNEL); 4924 if (!cgrp) 4925 return ERR_PTR(-ENOMEM); 4926 4927 ret = percpu_ref_init(&cgrp->self.refcnt, css_release, 0, GFP_KERNEL); 4928 if (ret) 4929 goto out_free_cgrp; 4930 4931 if (cgroup_on_dfl(parent)) { 4932 ret = cgroup_rstat_init(cgrp); 4933 if (ret) 4934 goto out_cancel_ref; 4935 } 4936 4937 /* 4938 * Temporarily set the pointer to NULL, so idr_find() won't return 4939 * a half-baked cgroup. 4940 */ 4941 cgrp->id = cgroup_idr_alloc(&root->cgroup_idr, NULL, 2, 0, GFP_KERNEL); 4942 if (cgrp->id < 0) { 4943 ret = -ENOMEM; 4944 goto out_stat_exit; 4945 } 4946 4947 init_cgroup_housekeeping(cgrp); 4948 4949 cgrp->self.parent = &parent->self; 4950 cgrp->root = root; 4951 cgrp->level = level; 4952 4953 ret = psi_cgroup_alloc(cgrp); 4954 if (ret) 4955 goto out_idr_free; 4956 4957 ret = cgroup_bpf_inherit(cgrp); 4958 if (ret) 4959 goto out_psi_free; 4960 4961 for (tcgrp = cgrp; tcgrp; tcgrp = cgroup_parent(tcgrp)) { 4962 cgrp->ancestor_ids[tcgrp->level] = tcgrp->id; 4963 4964 if (tcgrp != cgrp) 4965 tcgrp->nr_descendants++; 4966 } 4967 4968 if (notify_on_release(parent)) 4969 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags); 4970 4971 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags)) 4972 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags); 4973 4974 cgrp->self.serial_nr = css_serial_nr_next++; 4975 4976 /* allocation complete, commit to creation */ 4977 list_add_tail_rcu(&cgrp->self.sibling, &cgroup_parent(cgrp)->self.children); 4978 atomic_inc(&root->nr_cgrps); 4979 cgroup_get_live(parent); 4980 4981 /* 4982 * @cgrp is now fully operational. If something fails after this 4983 * point, it'll be released via the normal destruction path. 4984 */ 4985 cgroup_idr_replace(&root->cgroup_idr, cgrp, cgrp->id); 4986 4987 /* 4988 * On the default hierarchy, a child doesn't automatically inherit 4989 * subtree_control from the parent. Each is configured manually. 4990 */ 4991 if (!cgroup_on_dfl(cgrp)) 4992 cgrp->subtree_control = cgroup_control(cgrp); 4993 4994 cgroup_propagate_control(cgrp); 4995 4996 return cgrp; 4997 4998 out_psi_free: 4999 psi_cgroup_free(cgrp); 5000 out_idr_free: 5001 cgroup_idr_remove(&root->cgroup_idr, cgrp->id); 5002 out_stat_exit: 5003 if (cgroup_on_dfl(parent)) 5004 cgroup_rstat_exit(cgrp); 5005 out_cancel_ref: 5006 percpu_ref_exit(&cgrp->self.refcnt); 5007 out_free_cgrp: 5008 kfree(cgrp); 5009 return ERR_PTR(ret); 5010 } 5011 5012 static bool cgroup_check_hierarchy_limits(struct cgroup *parent) 5013 { 5014 struct cgroup *cgroup; 5015 int ret = false; 5016 int level = 1; 5017 5018 lockdep_assert_held(&cgroup_mutex); 5019 5020 for (cgroup = parent; cgroup; cgroup = cgroup_parent(cgroup)) { 5021 if (cgroup->nr_descendants >= cgroup->max_descendants) 5022 goto fail; 5023 5024 if (level > cgroup->max_depth) 5025 goto fail; 5026 5027 level++; 5028 } 5029 5030 ret = true; 5031 fail: 5032 return ret; 5033 } 5034 5035 int cgroup_mkdir(struct kernfs_node *parent_kn, const char *name, umode_t mode) 5036 { 5037 struct cgroup *parent, *cgrp; 5038 struct kernfs_node *kn; 5039 int ret; 5040 5041 /* do not accept '\n' to prevent making /proc/<pid>/cgroup unparsable */ 5042 if (strchr(name, '\n')) 5043 return -EINVAL; 5044 5045 parent = cgroup_kn_lock_live(parent_kn, false); 5046 if (!parent) 5047 return -ENODEV; 5048 5049 if (!cgroup_check_hierarchy_limits(parent)) { 5050 ret = -EAGAIN; 5051 goto out_unlock; 5052 } 5053 5054 cgrp = cgroup_create(parent); 5055 if (IS_ERR(cgrp)) { 5056 ret = PTR_ERR(cgrp); 5057 goto out_unlock; 5058 } 5059 5060 /* create the directory */ 5061 kn = kernfs_create_dir(parent->kn, name, mode, cgrp); 5062 if (IS_ERR(kn)) { 5063 ret = PTR_ERR(kn); 5064 goto out_destroy; 5065 } 5066 cgrp->kn = kn; 5067 5068 /* 5069 * This extra ref will be put in cgroup_free_fn() and guarantees 5070 * that @cgrp->kn is always accessible. 5071 */ 5072 kernfs_get(kn); 5073 5074 ret = cgroup_kn_set_ugid(kn); 5075 if (ret) 5076 goto out_destroy; 5077 5078 ret = css_populate_dir(&cgrp->self); 5079 if (ret) 5080 goto out_destroy; 5081 5082 ret = cgroup_apply_control_enable(cgrp); 5083 if (ret) 5084 goto out_destroy; 5085 5086 TRACE_CGROUP_PATH(mkdir, cgrp); 5087 5088 /* let's create and online css's */ 5089 kernfs_activate(kn); 5090 5091 ret = 0; 5092 goto out_unlock; 5093 5094 out_destroy: 5095 cgroup_destroy_locked(cgrp); 5096 out_unlock: 5097 cgroup_kn_unlock(parent_kn); 5098 return ret; 5099 } 5100 5101 /* 5102 * This is called when the refcnt of a css is confirmed to be killed. 5103 * css_tryget_online() is now guaranteed to fail. Tell the subsystem to 5104 * initate destruction and put the css ref from kill_css(). 5105 */ 5106 static void css_killed_work_fn(struct work_struct *work) 5107 { 5108 struct cgroup_subsys_state *css = 5109 container_of(work, struct cgroup_subsys_state, destroy_work); 5110 5111 mutex_lock(&cgroup_mutex); 5112 5113 do { 5114 offline_css(css); 5115 css_put(css); 5116 /* @css can't go away while we're holding cgroup_mutex */ 5117 css = css->parent; 5118 } while (css && atomic_dec_and_test(&css->online_cnt)); 5119 5120 mutex_unlock(&cgroup_mutex); 5121 } 5122 5123 /* css kill confirmation processing requires process context, bounce */ 5124 static void css_killed_ref_fn(struct percpu_ref *ref) 5125 { 5126 struct cgroup_subsys_state *css = 5127 container_of(ref, struct cgroup_subsys_state, refcnt); 5128 5129 if (atomic_dec_and_test(&css->online_cnt)) { 5130 INIT_WORK(&css->destroy_work, css_killed_work_fn); 5131 queue_work(cgroup_destroy_wq, &css->destroy_work); 5132 } 5133 } 5134 5135 /** 5136 * kill_css - destroy a css 5137 * @css: css to destroy 5138 * 5139 * This function initiates destruction of @css by removing cgroup interface 5140 * files and putting its base reference. ->css_offline() will be invoked 5141 * asynchronously once css_tryget_online() is guaranteed to fail and when 5142 * the reference count reaches zero, @css will be released. 5143 */ 5144 static void kill_css(struct cgroup_subsys_state *css) 5145 { 5146 lockdep_assert_held(&cgroup_mutex); 5147 5148 if (css->flags & CSS_DYING) 5149 return; 5150 5151 css->flags |= CSS_DYING; 5152 5153 /* 5154 * This must happen before css is disassociated with its cgroup. 5155 * See seq_css() for details. 5156 */ 5157 css_clear_dir(css); 5158 5159 /* 5160 * Killing would put the base ref, but we need to keep it alive 5161 * until after ->css_offline(). 5162 */ 5163 css_get(css); 5164 5165 /* 5166 * cgroup core guarantees that, by the time ->css_offline() is 5167 * invoked, no new css reference will be given out via 5168 * css_tryget_online(). We can't simply call percpu_ref_kill() and 5169 * proceed to offlining css's because percpu_ref_kill() doesn't 5170 * guarantee that the ref is seen as killed on all CPUs on return. 5171 * 5172 * Use percpu_ref_kill_and_confirm() to get notifications as each 5173 * css is confirmed to be seen as killed on all CPUs. 5174 */ 5175 percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn); 5176 } 5177 5178 /** 5179 * cgroup_destroy_locked - the first stage of cgroup destruction 5180 * @cgrp: cgroup to be destroyed 5181 * 5182 * css's make use of percpu refcnts whose killing latency shouldn't be 5183 * exposed to userland and are RCU protected. Also, cgroup core needs to 5184 * guarantee that css_tryget_online() won't succeed by the time 5185 * ->css_offline() is invoked. To satisfy all the requirements, 5186 * destruction is implemented in the following two steps. 5187 * 5188 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all 5189 * userland visible parts and start killing the percpu refcnts of 5190 * css's. Set up so that the next stage will be kicked off once all 5191 * the percpu refcnts are confirmed to be killed. 5192 * 5193 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the 5194 * rest of destruction. Once all cgroup references are gone, the 5195 * cgroup is RCU-freed. 5196 * 5197 * This function implements s1. After this step, @cgrp is gone as far as 5198 * the userland is concerned and a new cgroup with the same name may be 5199 * created. As cgroup doesn't care about the names internally, this 5200 * doesn't cause any problem. 5201 */ 5202 static int cgroup_destroy_locked(struct cgroup *cgrp) 5203 __releases(&cgroup_mutex) __acquires(&cgroup_mutex) 5204 { 5205 struct cgroup *tcgrp, *parent = cgroup_parent(cgrp); 5206 struct cgroup_subsys_state *css; 5207 struct cgrp_cset_link *link; 5208 int ssid; 5209 5210 lockdep_assert_held(&cgroup_mutex); 5211 5212 /* 5213 * Only migration can raise populated from zero and we're already 5214 * holding cgroup_mutex. 5215 */ 5216 if (cgroup_is_populated(cgrp)) 5217 return -EBUSY; 5218 5219 /* 5220 * Make sure there's no live children. We can't test emptiness of 5221 * ->self.children as dead children linger on it while being 5222 * drained; otherwise, "rmdir parent/child parent" may fail. 5223 */ 5224 if (css_has_online_children(&cgrp->self)) 5225 return -EBUSY; 5226 5227 /* 5228 * Mark @cgrp and the associated csets dead. The former prevents 5229 * further task migration and child creation by disabling 5230 * cgroup_lock_live_group(). The latter makes the csets ignored by 5231 * the migration path. 5232 */ 5233 cgrp->self.flags &= ~CSS_ONLINE; 5234 5235 spin_lock_irq(&css_set_lock); 5236 list_for_each_entry(link, &cgrp->cset_links, cset_link) 5237 link->cset->dead = true; 5238 spin_unlock_irq(&css_set_lock); 5239 5240 /* initiate massacre of all css's */ 5241 for_each_css(css, ssid, cgrp) 5242 kill_css(css); 5243 5244 /* clear and remove @cgrp dir, @cgrp has an extra ref on its kn */ 5245 css_clear_dir(&cgrp->self); 5246 kernfs_remove(cgrp->kn); 5247 5248 if (parent && cgroup_is_threaded(cgrp)) 5249 parent->nr_threaded_children--; 5250 5251 for (tcgrp = cgroup_parent(cgrp); tcgrp; tcgrp = cgroup_parent(tcgrp)) { 5252 tcgrp->nr_descendants--; 5253 tcgrp->nr_dying_descendants++; 5254 } 5255 5256 cgroup1_check_for_release(parent); 5257 5258 /* put the base reference */ 5259 percpu_ref_kill(&cgrp->self.refcnt); 5260 5261 return 0; 5262 }; 5263 5264 int cgroup_rmdir(struct kernfs_node *kn) 5265 { 5266 struct cgroup *cgrp; 5267 int ret = 0; 5268 5269 cgrp = cgroup_kn_lock_live(kn, false); 5270 if (!cgrp) 5271 return 0; 5272 5273 ret = cgroup_destroy_locked(cgrp); 5274 if (!ret) 5275 TRACE_CGROUP_PATH(rmdir, cgrp); 5276 5277 cgroup_kn_unlock(kn); 5278 return ret; 5279 } 5280 5281 static struct kernfs_syscall_ops cgroup_kf_syscall_ops = { 5282 .show_options = cgroup_show_options, 5283 .remount_fs = cgroup_remount, 5284 .mkdir = cgroup_mkdir, 5285 .rmdir = cgroup_rmdir, 5286 .show_path = cgroup_show_path, 5287 }; 5288 5289 static void __init cgroup_init_subsys(struct cgroup_subsys *ss, bool early) 5290 { 5291 struct cgroup_subsys_state *css; 5292 5293 pr_debug("Initializing cgroup subsys %s\n", ss->name); 5294 5295 mutex_lock(&cgroup_mutex); 5296 5297 idr_init(&ss->css_idr); 5298 INIT_LIST_HEAD(&ss->cfts); 5299 5300 /* Create the root cgroup state for this subsystem */ 5301 ss->root = &cgrp_dfl_root; 5302 css = ss->css_alloc(cgroup_css(&cgrp_dfl_root.cgrp, ss)); 5303 /* We don't handle early failures gracefully */ 5304 BUG_ON(IS_ERR(css)); 5305 init_and_link_css(css, ss, &cgrp_dfl_root.cgrp); 5306 5307 /* 5308 * Root csses are never destroyed and we can't initialize 5309 * percpu_ref during early init. Disable refcnting. 5310 */ 5311 css->flags |= CSS_NO_REF; 5312 5313 if (early) { 5314 /* allocation can't be done safely during early init */ 5315 css->id = 1; 5316 } else { 5317 css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2, GFP_KERNEL); 5318 BUG_ON(css->id < 0); 5319 } 5320 5321 /* Update the init_css_set to contain a subsys 5322 * pointer to this state - since the subsystem is 5323 * newly registered, all tasks and hence the 5324 * init_css_set is in the subsystem's root cgroup. */ 5325 init_css_set.subsys[ss->id] = css; 5326 5327 have_fork_callback |= (bool)ss->fork << ss->id; 5328 have_exit_callback |= (bool)ss->exit << ss->id; 5329 have_free_callback |= (bool)ss->free << ss->id; 5330 have_canfork_callback |= (bool)ss->can_fork << ss->id; 5331 5332 /* At system boot, before all subsystems have been 5333 * registered, no tasks have been forked, so we don't 5334 * need to invoke fork callbacks here. */ 5335 BUG_ON(!list_empty(&init_task.tasks)); 5336 5337 BUG_ON(online_css(css)); 5338 5339 mutex_unlock(&cgroup_mutex); 5340 } 5341 5342 /** 5343 * cgroup_init_early - cgroup initialization at system boot 5344 * 5345 * Initialize cgroups at system boot, and initialize any 5346 * subsystems that request early init. 5347 */ 5348 int __init cgroup_init_early(void) 5349 { 5350 static struct cgroup_sb_opts __initdata opts; 5351 struct cgroup_subsys *ss; 5352 int i; 5353 5354 init_cgroup_root(&cgrp_dfl_root, &opts); 5355 cgrp_dfl_root.cgrp.self.flags |= CSS_NO_REF; 5356 5357 RCU_INIT_POINTER(init_task.cgroups, &init_css_set); 5358 5359 for_each_subsys(ss, i) { 5360 WARN(!ss->css_alloc || !ss->css_free || ss->name || ss->id, 5361 "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p id:name=%d:%s\n", 5362 i, cgroup_subsys_name[i], ss->css_alloc, ss->css_free, 5363 ss->id, ss->name); 5364 WARN(strlen(cgroup_subsys_name[i]) > MAX_CGROUP_TYPE_NAMELEN, 5365 "cgroup_subsys_name %s too long\n", cgroup_subsys_name[i]); 5366 5367 ss->id = i; 5368 ss->name = cgroup_subsys_name[i]; 5369 if (!ss->legacy_name) 5370 ss->legacy_name = cgroup_subsys_name[i]; 5371 5372 if (ss->early_init) 5373 cgroup_init_subsys(ss, true); 5374 } 5375 return 0; 5376 } 5377 5378 static u16 cgroup_disable_mask __initdata; 5379 5380 /** 5381 * cgroup_init - cgroup initialization 5382 * 5383 * Register cgroup filesystem and /proc file, and initialize 5384 * any subsystems that didn't request early init. 5385 */ 5386 int __init cgroup_init(void) 5387 { 5388 struct cgroup_subsys *ss; 5389 int ssid; 5390 5391 BUILD_BUG_ON(CGROUP_SUBSYS_COUNT > 16); 5392 BUG_ON(percpu_init_rwsem(&cgroup_threadgroup_rwsem)); 5393 BUG_ON(cgroup_init_cftypes(NULL, cgroup_base_files)); 5394 BUG_ON(cgroup_init_cftypes(NULL, cgroup1_base_files)); 5395 5396 cgroup_rstat_boot(); 5397 5398 /* 5399 * The latency of the synchronize_rcu() is too high for cgroups, 5400 * avoid it at the cost of forcing all readers into the slow path. 5401 */ 5402 rcu_sync_enter_start(&cgroup_threadgroup_rwsem.rss); 5403 5404 get_user_ns(init_cgroup_ns.user_ns); 5405 5406 mutex_lock(&cgroup_mutex); 5407 5408 /* 5409 * Add init_css_set to the hash table so that dfl_root can link to 5410 * it during init. 5411 */ 5412 hash_add(css_set_table, &init_css_set.hlist, 5413 css_set_hash(init_css_set.subsys)); 5414 5415 BUG_ON(cgroup_setup_root(&cgrp_dfl_root, 0)); 5416 5417 mutex_unlock(&cgroup_mutex); 5418 5419 for_each_subsys(ss, ssid) { 5420 if (ss->early_init) { 5421 struct cgroup_subsys_state *css = 5422 init_css_set.subsys[ss->id]; 5423 5424 css->id = cgroup_idr_alloc(&ss->css_idr, css, 1, 2, 5425 GFP_KERNEL); 5426 BUG_ON(css->id < 0); 5427 } else { 5428 cgroup_init_subsys(ss, false); 5429 } 5430 5431 list_add_tail(&init_css_set.e_cset_node[ssid], 5432 &cgrp_dfl_root.cgrp.e_csets[ssid]); 5433 5434 /* 5435 * Setting dfl_root subsys_mask needs to consider the 5436 * disabled flag and cftype registration needs kmalloc, 5437 * both of which aren't available during early_init. 5438 */ 5439 if (cgroup_disable_mask & (1 << ssid)) { 5440 static_branch_disable(cgroup_subsys_enabled_key[ssid]); 5441 printk(KERN_INFO "Disabling %s control group subsystem\n", 5442 ss->name); 5443 continue; 5444 } 5445 5446 if (cgroup1_ssid_disabled(ssid)) 5447 printk(KERN_INFO "Disabling %s control group subsystem in v1 mounts\n", 5448 ss->name); 5449 5450 cgrp_dfl_root.subsys_mask |= 1 << ss->id; 5451 5452 /* implicit controllers must be threaded too */ 5453 WARN_ON(ss->implicit_on_dfl && !ss->threaded); 5454 5455 if (ss->implicit_on_dfl) 5456 cgrp_dfl_implicit_ss_mask |= 1 << ss->id; 5457 else if (!ss->dfl_cftypes) 5458 cgrp_dfl_inhibit_ss_mask |= 1 << ss->id; 5459 5460 if (ss->threaded) 5461 cgrp_dfl_threaded_ss_mask |= 1 << ss->id; 5462 5463 if (ss->dfl_cftypes == ss->legacy_cftypes) { 5464 WARN_ON(cgroup_add_cftypes(ss, ss->dfl_cftypes)); 5465 } else { 5466 WARN_ON(cgroup_add_dfl_cftypes(ss, ss->dfl_cftypes)); 5467 WARN_ON(cgroup_add_legacy_cftypes(ss, ss->legacy_cftypes)); 5468 } 5469 5470 if (ss->bind) 5471 ss->bind(init_css_set.subsys[ssid]); 5472 5473 mutex_lock(&cgroup_mutex); 5474 css_populate_dir(init_css_set.subsys[ssid]); 5475 mutex_unlock(&cgroup_mutex); 5476 } 5477 5478 /* init_css_set.subsys[] has been updated, re-hash */ 5479 hash_del(&init_css_set.hlist); 5480 hash_add(css_set_table, &init_css_set.hlist, 5481 css_set_hash(init_css_set.subsys)); 5482 5483 WARN_ON(sysfs_create_mount_point(fs_kobj, "cgroup")); 5484 WARN_ON(register_filesystem(&cgroup_fs_type)); 5485 WARN_ON(register_filesystem(&cgroup2_fs_type)); 5486 WARN_ON(!proc_create_single("cgroups", 0, NULL, proc_cgroupstats_show)); 5487 5488 return 0; 5489 } 5490 5491 static int __init cgroup_wq_init(void) 5492 { 5493 /* 5494 * There isn't much point in executing destruction path in 5495 * parallel. Good chunk is serialized with cgroup_mutex anyway. 5496 * Use 1 for @max_active. 5497 * 5498 * We would prefer to do this in cgroup_init() above, but that 5499 * is called before init_workqueues(): so leave this until after. 5500 */ 5501 cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1); 5502 BUG_ON(!cgroup_destroy_wq); 5503 return 0; 5504 } 5505 core_initcall(cgroup_wq_init); 5506 5507 void cgroup_path_from_kernfs_id(const union kernfs_node_id *id, 5508 char *buf, size_t buflen) 5509 { 5510 struct kernfs_node *kn; 5511 5512 kn = kernfs_get_node_by_id(cgrp_dfl_root.kf_root, id); 5513 if (!kn) 5514 return; 5515 kernfs_path(kn, buf, buflen); 5516 kernfs_put(kn); 5517 } 5518 5519 /* 5520 * proc_cgroup_show() 5521 * - Print task's cgroup paths into seq_file, one line for each hierarchy 5522 * - Used for /proc/<pid>/cgroup. 5523 */ 5524 int proc_cgroup_show(struct seq_file *m, struct pid_namespace *ns, 5525 struct pid *pid, struct task_struct *tsk) 5526 { 5527 char *buf; 5528 int retval; 5529 struct cgroup_root *root; 5530 5531 retval = -ENOMEM; 5532 buf = kmalloc(PATH_MAX, GFP_KERNEL); 5533 if (!buf) 5534 goto out; 5535 5536 mutex_lock(&cgroup_mutex); 5537 spin_lock_irq(&css_set_lock); 5538 5539 for_each_root(root) { 5540 struct cgroup_subsys *ss; 5541 struct cgroup *cgrp; 5542 int ssid, count = 0; 5543 5544 if (root == &cgrp_dfl_root && !cgrp_dfl_visible) 5545 continue; 5546 5547 seq_printf(m, "%d:", root->hierarchy_id); 5548 if (root != &cgrp_dfl_root) 5549 for_each_subsys(ss, ssid) 5550 if (root->subsys_mask & (1 << ssid)) 5551 seq_printf(m, "%s%s", count++ ? "," : "", 5552 ss->legacy_name); 5553 if (strlen(root->name)) 5554 seq_printf(m, "%sname=%s", count ? "," : "", 5555 root->name); 5556 seq_putc(m, ':'); 5557 5558 cgrp = task_cgroup_from_root(tsk, root); 5559 5560 /* 5561 * On traditional hierarchies, all zombie tasks show up as 5562 * belonging to the root cgroup. On the default hierarchy, 5563 * while a zombie doesn't show up in "cgroup.procs" and 5564 * thus can't be migrated, its /proc/PID/cgroup keeps 5565 * reporting the cgroup it belonged to before exiting. If 5566 * the cgroup is removed before the zombie is reaped, 5567 * " (deleted)" is appended to the cgroup path. 5568 */ 5569 if (cgroup_on_dfl(cgrp) || !(tsk->flags & PF_EXITING)) { 5570 retval = cgroup_path_ns_locked(cgrp, buf, PATH_MAX, 5571 current->nsproxy->cgroup_ns); 5572 if (retval >= PATH_MAX) 5573 retval = -ENAMETOOLONG; 5574 if (retval < 0) 5575 goto out_unlock; 5576 5577 seq_puts(m, buf); 5578 } else { 5579 seq_puts(m, "/"); 5580 } 5581 5582 if (cgroup_on_dfl(cgrp) && cgroup_is_dead(cgrp)) 5583 seq_puts(m, " (deleted)\n"); 5584 else 5585 seq_putc(m, '\n'); 5586 } 5587 5588 retval = 0; 5589 out_unlock: 5590 spin_unlock_irq(&css_set_lock); 5591 mutex_unlock(&cgroup_mutex); 5592 kfree(buf); 5593 out: 5594 return retval; 5595 } 5596 5597 /** 5598 * cgroup_fork - initialize cgroup related fields during copy_process() 5599 * @child: pointer to task_struct of forking parent process. 5600 * 5601 * A task is associated with the init_css_set until cgroup_post_fork() 5602 * attaches it to the parent's css_set. Empty cg_list indicates that 5603 * @child isn't holding reference to its css_set. 5604 */ 5605 void cgroup_fork(struct task_struct *child) 5606 { 5607 RCU_INIT_POINTER(child->cgroups, &init_css_set); 5608 INIT_LIST_HEAD(&child->cg_list); 5609 } 5610 5611 /** 5612 * cgroup_can_fork - called on a new task before the process is exposed 5613 * @child: the task in question. 5614 * 5615 * This calls the subsystem can_fork() callbacks. If the can_fork() callback 5616 * returns an error, the fork aborts with that error code. This allows for 5617 * a cgroup subsystem to conditionally allow or deny new forks. 5618 */ 5619 int cgroup_can_fork(struct task_struct *child) 5620 { 5621 struct cgroup_subsys *ss; 5622 int i, j, ret; 5623 5624 do_each_subsys_mask(ss, i, have_canfork_callback) { 5625 ret = ss->can_fork(child); 5626 if (ret) 5627 goto out_revert; 5628 } while_each_subsys_mask(); 5629 5630 return 0; 5631 5632 out_revert: 5633 for_each_subsys(ss, j) { 5634 if (j >= i) 5635 break; 5636 if (ss->cancel_fork) 5637 ss->cancel_fork(child); 5638 } 5639 5640 return ret; 5641 } 5642 5643 /** 5644 * cgroup_cancel_fork - called if a fork failed after cgroup_can_fork() 5645 * @child: the task in question 5646 * 5647 * This calls the cancel_fork() callbacks if a fork failed *after* 5648 * cgroup_can_fork() succeded. 5649 */ 5650 void cgroup_cancel_fork(struct task_struct *child) 5651 { 5652 struct cgroup_subsys *ss; 5653 int i; 5654 5655 for_each_subsys(ss, i) 5656 if (ss->cancel_fork) 5657 ss->cancel_fork(child); 5658 } 5659 5660 /** 5661 * cgroup_post_fork - called on a new task after adding it to the task list 5662 * @child: the task in question 5663 * 5664 * Adds the task to the list running through its css_set if necessary and 5665 * call the subsystem fork() callbacks. Has to be after the task is 5666 * visible on the task list in case we race with the first call to 5667 * cgroup_task_iter_start() - to guarantee that the new task ends up on its 5668 * list. 5669 */ 5670 void cgroup_post_fork(struct task_struct *child) 5671 { 5672 struct cgroup_subsys *ss; 5673 int i; 5674 5675 /* 5676 * This may race against cgroup_enable_task_cg_lists(). As that 5677 * function sets use_task_css_set_links before grabbing 5678 * tasklist_lock and we just went through tasklist_lock to add 5679 * @child, it's guaranteed that either we see the set 5680 * use_task_css_set_links or cgroup_enable_task_cg_lists() sees 5681 * @child during its iteration. 5682 * 5683 * If we won the race, @child is associated with %current's 5684 * css_set. Grabbing css_set_lock guarantees both that the 5685 * association is stable, and, on completion of the parent's 5686 * migration, @child is visible in the source of migration or 5687 * already in the destination cgroup. This guarantee is necessary 5688 * when implementing operations which need to migrate all tasks of 5689 * a cgroup to another. 5690 * 5691 * Note that if we lose to cgroup_enable_task_cg_lists(), @child 5692 * will remain in init_css_set. This is safe because all tasks are 5693 * in the init_css_set before cg_links is enabled and there's no 5694 * operation which transfers all tasks out of init_css_set. 5695 */ 5696 if (use_task_css_set_links) { 5697 struct css_set *cset; 5698 5699 spin_lock_irq(&css_set_lock); 5700 cset = task_css_set(current); 5701 if (list_empty(&child->cg_list)) { 5702 get_css_set(cset); 5703 cset->nr_tasks++; 5704 css_set_move_task(child, NULL, cset, false); 5705 } 5706 spin_unlock_irq(&css_set_lock); 5707 } 5708 5709 /* 5710 * Call ss->fork(). This must happen after @child is linked on 5711 * css_set; otherwise, @child might change state between ->fork() 5712 * and addition to css_set. 5713 */ 5714 do_each_subsys_mask(ss, i, have_fork_callback) { 5715 ss->fork(child); 5716 } while_each_subsys_mask(); 5717 } 5718 5719 /** 5720 * cgroup_exit - detach cgroup from exiting task 5721 * @tsk: pointer to task_struct of exiting process 5722 * 5723 * Description: Detach cgroup from @tsk and release it. 5724 * 5725 * Note that cgroups marked notify_on_release force every task in 5726 * them to take the global cgroup_mutex mutex when exiting. 5727 * This could impact scaling on very large systems. Be reluctant to 5728 * use notify_on_release cgroups where very high task exit scaling 5729 * is required on large systems. 5730 * 5731 * We set the exiting tasks cgroup to the root cgroup (top_cgroup). We 5732 * call cgroup_exit() while the task is still competent to handle 5733 * notify_on_release(), then leave the task attached to the root cgroup in 5734 * each hierarchy for the remainder of its exit. No need to bother with 5735 * init_css_set refcnting. init_css_set never goes away and we can't race 5736 * with migration path - PF_EXITING is visible to migration path. 5737 */ 5738 void cgroup_exit(struct task_struct *tsk) 5739 { 5740 struct cgroup_subsys *ss; 5741 struct css_set *cset; 5742 int i; 5743 5744 /* 5745 * Unlink from @tsk from its css_set. As migration path can't race 5746 * with us, we can check css_set and cg_list without synchronization. 5747 */ 5748 cset = task_css_set(tsk); 5749 5750 if (!list_empty(&tsk->cg_list)) { 5751 spin_lock_irq(&css_set_lock); 5752 css_set_move_task(tsk, cset, NULL, false); 5753 cset->nr_tasks--; 5754 spin_unlock_irq(&css_set_lock); 5755 } else { 5756 get_css_set(cset); 5757 } 5758 5759 /* see cgroup_post_fork() for details */ 5760 do_each_subsys_mask(ss, i, have_exit_callback) { 5761 ss->exit(tsk); 5762 } while_each_subsys_mask(); 5763 } 5764 5765 void cgroup_free(struct task_struct *task) 5766 { 5767 struct css_set *cset = task_css_set(task); 5768 struct cgroup_subsys *ss; 5769 int ssid; 5770 5771 do_each_subsys_mask(ss, ssid, have_free_callback) { 5772 ss->free(task); 5773 } while_each_subsys_mask(); 5774 5775 put_css_set(cset); 5776 } 5777 5778 static int __init cgroup_disable(char *str) 5779 { 5780 struct cgroup_subsys *ss; 5781 char *token; 5782 int i; 5783 5784 while ((token = strsep(&str, ",")) != NULL) { 5785 if (!*token) 5786 continue; 5787 5788 for_each_subsys(ss, i) { 5789 if (strcmp(token, ss->name) && 5790 strcmp(token, ss->legacy_name)) 5791 continue; 5792 cgroup_disable_mask |= 1 << i; 5793 } 5794 } 5795 return 1; 5796 } 5797 __setup("cgroup_disable=", cgroup_disable); 5798 5799 void __init __weak enable_debug_cgroup(void) { } 5800 5801 static int __init enable_cgroup_debug(char *str) 5802 { 5803 cgroup_debug = true; 5804 enable_debug_cgroup(); 5805 return 1; 5806 } 5807 __setup("cgroup_debug", enable_cgroup_debug); 5808 5809 /** 5810 * css_tryget_online_from_dir - get corresponding css from a cgroup dentry 5811 * @dentry: directory dentry of interest 5812 * @ss: subsystem of interest 5813 * 5814 * If @dentry is a directory for a cgroup which has @ss enabled on it, try 5815 * to get the corresponding css and return it. If such css doesn't exist 5816 * or can't be pinned, an ERR_PTR value is returned. 5817 */ 5818 struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry, 5819 struct cgroup_subsys *ss) 5820 { 5821 struct kernfs_node *kn = kernfs_node_from_dentry(dentry); 5822 struct file_system_type *s_type = dentry->d_sb->s_type; 5823 struct cgroup_subsys_state *css = NULL; 5824 struct cgroup *cgrp; 5825 5826 /* is @dentry a cgroup dir? */ 5827 if ((s_type != &cgroup_fs_type && s_type != &cgroup2_fs_type) || 5828 !kn || kernfs_type(kn) != KERNFS_DIR) 5829 return ERR_PTR(-EBADF); 5830 5831 rcu_read_lock(); 5832 5833 /* 5834 * This path doesn't originate from kernfs and @kn could already 5835 * have been or be removed at any point. @kn->priv is RCU 5836 * protected for this access. See css_release_work_fn() for details. 5837 */ 5838 cgrp = rcu_dereference(*(void __rcu __force **)&kn->priv); 5839 if (cgrp) 5840 css = cgroup_css(cgrp, ss); 5841 5842 if (!css || !css_tryget_online(css)) 5843 css = ERR_PTR(-ENOENT); 5844 5845 rcu_read_unlock(); 5846 return css; 5847 } 5848 5849 /** 5850 * css_from_id - lookup css by id 5851 * @id: the cgroup id 5852 * @ss: cgroup subsys to be looked into 5853 * 5854 * Returns the css if there's valid one with @id, otherwise returns NULL. 5855 * Should be called under rcu_read_lock(). 5856 */ 5857 struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss) 5858 { 5859 WARN_ON_ONCE(!rcu_read_lock_held()); 5860 return idr_find(&ss->css_idr, id); 5861 } 5862 5863 /** 5864 * cgroup_get_from_path - lookup and get a cgroup from its default hierarchy path 5865 * @path: path on the default hierarchy 5866 * 5867 * Find the cgroup at @path on the default hierarchy, increment its 5868 * reference count and return it. Returns pointer to the found cgroup on 5869 * success, ERR_PTR(-ENOENT) if @path doens't exist and ERR_PTR(-ENOTDIR) 5870 * if @path points to a non-directory. 5871 */ 5872 struct cgroup *cgroup_get_from_path(const char *path) 5873 { 5874 struct kernfs_node *kn; 5875 struct cgroup *cgrp; 5876 5877 mutex_lock(&cgroup_mutex); 5878 5879 kn = kernfs_walk_and_get(cgrp_dfl_root.cgrp.kn, path); 5880 if (kn) { 5881 if (kernfs_type(kn) == KERNFS_DIR) { 5882 cgrp = kn->priv; 5883 cgroup_get_live(cgrp); 5884 } else { 5885 cgrp = ERR_PTR(-ENOTDIR); 5886 } 5887 kernfs_put(kn); 5888 } else { 5889 cgrp = ERR_PTR(-ENOENT); 5890 } 5891 5892 mutex_unlock(&cgroup_mutex); 5893 return cgrp; 5894 } 5895 EXPORT_SYMBOL_GPL(cgroup_get_from_path); 5896 5897 /** 5898 * cgroup_get_from_fd - get a cgroup pointer from a fd 5899 * @fd: fd obtained by open(cgroup2_dir) 5900 * 5901 * Find the cgroup from a fd which should be obtained 5902 * by opening a cgroup directory. Returns a pointer to the 5903 * cgroup on success. ERR_PTR is returned if the cgroup 5904 * cannot be found. 5905 */ 5906 struct cgroup *cgroup_get_from_fd(int fd) 5907 { 5908 struct cgroup_subsys_state *css; 5909 struct cgroup *cgrp; 5910 struct file *f; 5911 5912 f = fget_raw(fd); 5913 if (!f) 5914 return ERR_PTR(-EBADF); 5915 5916 css = css_tryget_online_from_dir(f->f_path.dentry, NULL); 5917 fput(f); 5918 if (IS_ERR(css)) 5919 return ERR_CAST(css); 5920 5921 cgrp = css->cgroup; 5922 if (!cgroup_on_dfl(cgrp)) { 5923 cgroup_put(cgrp); 5924 return ERR_PTR(-EBADF); 5925 } 5926 5927 return cgrp; 5928 } 5929 EXPORT_SYMBOL_GPL(cgroup_get_from_fd); 5930 5931 /* 5932 * sock->sk_cgrp_data handling. For more info, see sock_cgroup_data 5933 * definition in cgroup-defs.h. 5934 */ 5935 #ifdef CONFIG_SOCK_CGROUP_DATA 5936 5937 #if defined(CONFIG_CGROUP_NET_PRIO) || defined(CONFIG_CGROUP_NET_CLASSID) 5938 5939 DEFINE_SPINLOCK(cgroup_sk_update_lock); 5940 static bool cgroup_sk_alloc_disabled __read_mostly; 5941 5942 void cgroup_sk_alloc_disable(void) 5943 { 5944 if (cgroup_sk_alloc_disabled) 5945 return; 5946 pr_info("cgroup: disabling cgroup2 socket matching due to net_prio or net_cls activation\n"); 5947 cgroup_sk_alloc_disabled = true; 5948 } 5949 5950 #else 5951 5952 #define cgroup_sk_alloc_disabled false 5953 5954 #endif 5955 5956 void cgroup_sk_alloc(struct sock_cgroup_data *skcd) 5957 { 5958 if (cgroup_sk_alloc_disabled) 5959 return; 5960 5961 /* Socket clone path */ 5962 if (skcd->val) { 5963 /* 5964 * We might be cloning a socket which is left in an empty 5965 * cgroup and the cgroup might have already been rmdir'd. 5966 * Don't use cgroup_get_live(). 5967 */ 5968 cgroup_get(sock_cgroup_ptr(skcd)); 5969 return; 5970 } 5971 5972 rcu_read_lock(); 5973 5974 while (true) { 5975 struct css_set *cset; 5976 5977 cset = task_css_set(current); 5978 if (likely(cgroup_tryget(cset->dfl_cgrp))) { 5979 skcd->val = (unsigned long)cset->dfl_cgrp; 5980 break; 5981 } 5982 cpu_relax(); 5983 } 5984 5985 rcu_read_unlock(); 5986 } 5987 5988 void cgroup_sk_free(struct sock_cgroup_data *skcd) 5989 { 5990 cgroup_put(sock_cgroup_ptr(skcd)); 5991 } 5992 5993 #endif /* CONFIG_SOCK_CGROUP_DATA */ 5994 5995 #ifdef CONFIG_CGROUP_BPF 5996 int cgroup_bpf_attach(struct cgroup *cgrp, struct bpf_prog *prog, 5997 enum bpf_attach_type type, u32 flags) 5998 { 5999 int ret; 6000 6001 mutex_lock(&cgroup_mutex); 6002 ret = __cgroup_bpf_attach(cgrp, prog, type, flags); 6003 mutex_unlock(&cgroup_mutex); 6004 return ret; 6005 } 6006 int cgroup_bpf_detach(struct cgroup *cgrp, struct bpf_prog *prog, 6007 enum bpf_attach_type type, u32 flags) 6008 { 6009 int ret; 6010 6011 mutex_lock(&cgroup_mutex); 6012 ret = __cgroup_bpf_detach(cgrp, prog, type); 6013 mutex_unlock(&cgroup_mutex); 6014 return ret; 6015 } 6016 int cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr, 6017 union bpf_attr __user *uattr) 6018 { 6019 int ret; 6020 6021 mutex_lock(&cgroup_mutex); 6022 ret = __cgroup_bpf_query(cgrp, attr, uattr); 6023 mutex_unlock(&cgroup_mutex); 6024 return ret; 6025 } 6026 #endif /* CONFIG_CGROUP_BPF */ 6027 6028 #ifdef CONFIG_SYSFS 6029 static ssize_t show_delegatable_files(struct cftype *files, char *buf, 6030 ssize_t size, const char *prefix) 6031 { 6032 struct cftype *cft; 6033 ssize_t ret = 0; 6034 6035 for (cft = files; cft && cft->name[0] != '\0'; cft++) { 6036 if (!(cft->flags & CFTYPE_NS_DELEGATABLE)) 6037 continue; 6038 6039 if (prefix) 6040 ret += snprintf(buf + ret, size - ret, "%s.", prefix); 6041 6042 ret += snprintf(buf + ret, size - ret, "%s\n", cft->name); 6043 6044 if (WARN_ON(ret >= size)) 6045 break; 6046 } 6047 6048 return ret; 6049 } 6050 6051 static ssize_t delegate_show(struct kobject *kobj, struct kobj_attribute *attr, 6052 char *buf) 6053 { 6054 struct cgroup_subsys *ss; 6055 int ssid; 6056 ssize_t ret = 0; 6057 6058 ret = show_delegatable_files(cgroup_base_files, buf, PAGE_SIZE - ret, 6059 NULL); 6060 6061 for_each_subsys(ss, ssid) 6062 ret += show_delegatable_files(ss->dfl_cftypes, buf + ret, 6063 PAGE_SIZE - ret, 6064 cgroup_subsys_name[ssid]); 6065 6066 return ret; 6067 } 6068 static struct kobj_attribute cgroup_delegate_attr = __ATTR_RO(delegate); 6069 6070 static ssize_t features_show(struct kobject *kobj, struct kobj_attribute *attr, 6071 char *buf) 6072 { 6073 return snprintf(buf, PAGE_SIZE, "nsdelegate\n"); 6074 } 6075 static struct kobj_attribute cgroup_features_attr = __ATTR_RO(features); 6076 6077 static struct attribute *cgroup_sysfs_attrs[] = { 6078 &cgroup_delegate_attr.attr, 6079 &cgroup_features_attr.attr, 6080 NULL, 6081 }; 6082 6083 static const struct attribute_group cgroup_sysfs_attr_group = { 6084 .attrs = cgroup_sysfs_attrs, 6085 .name = "cgroup", 6086 }; 6087 6088 static int __init cgroup_sysfs_init(void) 6089 { 6090 return sysfs_create_group(kernel_kobj, &cgroup_sysfs_attr_group); 6091 } 6092 subsys_initcall(cgroup_sysfs_init); 6093 #endif /* CONFIG_SYSFS */ 6094