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