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