1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 3 * linux/cgroup-defs.h - basic definitions for cgroup 4 * 5 * This file provides basic type and interface. Include this file directly 6 * only if necessary to avoid cyclic dependencies. 7 */ 8 #ifndef _LINUX_CGROUP_DEFS_H 9 #define _LINUX_CGROUP_DEFS_H 10 11 #include <linux/limits.h> 12 #include <linux/list.h> 13 #include <linux/idr.h> 14 #include <linux/wait.h> 15 #include <linux/mutex.h> 16 #include <linux/rcupdate.h> 17 #include <linux/refcount.h> 18 #include <linux/percpu-refcount.h> 19 #include <linux/percpu-rwsem.h> 20 #include <linux/sched.h> 21 #include <linux/u64_stats_sync.h> 22 #include <linux/workqueue.h> 23 #include <linux/bpf-cgroup-defs.h> 24 #include <linux/psi_types.h> 25 26 #ifdef CONFIG_CGROUPS 27 28 struct cgroup; 29 struct cgroup_root; 30 struct cgroup_subsys; 31 struct cgroup_taskset; 32 struct kernfs_node; 33 struct kernfs_ops; 34 struct kernfs_open_file; 35 struct seq_file; 36 struct poll_table_struct; 37 38 #define MAX_CGROUP_TYPE_NAMELEN 32 39 #define MAX_CGROUP_ROOT_NAMELEN 64 40 #define MAX_CFTYPE_NAME 64 41 42 /* define the enumeration of all cgroup subsystems */ 43 #define SUBSYS(_x) _x ## _cgrp_id, 44 enum cgroup_subsys_id { 45 #include <linux/cgroup_subsys.h> 46 CGROUP_SUBSYS_COUNT, 47 }; 48 #undef SUBSYS 49 50 /* bits in struct cgroup_subsys_state flags field */ 51 enum { 52 CSS_NO_REF = (1 << 0), /* no reference counting for this css */ 53 CSS_ONLINE = (1 << 1), /* between ->css_online() and ->css_offline() */ 54 CSS_RELEASED = (1 << 2), /* refcnt reached zero, released */ 55 CSS_VISIBLE = (1 << 3), /* css is visible to userland */ 56 CSS_DYING = (1 << 4), /* css is dying */ 57 }; 58 59 /* bits in struct cgroup flags field */ 60 enum { 61 /* Control Group requires release notifications to userspace */ 62 CGRP_NOTIFY_ON_RELEASE, 63 /* 64 * Clone the parent's configuration when creating a new child 65 * cpuset cgroup. For historical reasons, this option can be 66 * specified at mount time and thus is implemented here. 67 */ 68 CGRP_CPUSET_CLONE_CHILDREN, 69 70 /* Control group has to be frozen. */ 71 CGRP_FREEZE, 72 73 /* Cgroup is frozen. */ 74 CGRP_FROZEN, 75 }; 76 77 /* cgroup_root->flags */ 78 enum { 79 CGRP_ROOT_NOPREFIX = (1 << 1), /* mounted subsystems have no named prefix */ 80 CGRP_ROOT_XATTR = (1 << 2), /* supports extended attributes */ 81 82 /* 83 * Consider namespaces as delegation boundaries. If this flag is 84 * set, controller specific interface files in a namespace root 85 * aren't writeable from inside the namespace. 86 */ 87 CGRP_ROOT_NS_DELEGATE = (1 << 3), 88 89 /* 90 * Reduce latencies on dynamic cgroup modifications such as task 91 * migrations and controller on/offs by disabling percpu operation on 92 * cgroup_threadgroup_rwsem. This makes hot path operations such as 93 * forks and exits into the slow path and more expensive. 94 * 95 * Alleviate the contention between fork, exec, exit operations and 96 * writing to cgroup.procs by taking a per threadgroup rwsem instead of 97 * the global cgroup_threadgroup_rwsem. Fork and other operations 98 * from threads in different thread groups no longer contend with 99 * writing to cgroup.procs. 100 * 101 * The static usage pattern of creating a cgroup, enabling controllers, 102 * and then seeding it with CLONE_INTO_CGROUP doesn't require write 103 * locking cgroup_threadgroup_rwsem and thus doesn't benefit from 104 * favordynmod. 105 */ 106 CGRP_ROOT_FAVOR_DYNMODS = (1 << 4), 107 108 /* 109 * Enable cpuset controller in v1 cgroup to use v2 behavior. 110 */ 111 CGRP_ROOT_CPUSET_V2_MODE = (1 << 16), 112 113 /* 114 * Enable legacy local memory.events. 115 */ 116 CGRP_ROOT_MEMORY_LOCAL_EVENTS = (1 << 17), 117 118 /* 119 * Enable recursive subtree protection 120 */ 121 CGRP_ROOT_MEMORY_RECURSIVE_PROT = (1 << 18), 122 123 /* 124 * Enable hugetlb accounting for the memory controller. 125 */ 126 CGRP_ROOT_MEMORY_HUGETLB_ACCOUNTING = (1 << 19), 127 128 /* 129 * Enable legacy local pids.events. 130 */ 131 CGRP_ROOT_PIDS_LOCAL_EVENTS = (1 << 20), 132 }; 133 134 /* cftype->flags */ 135 enum { 136 CFTYPE_ONLY_ON_ROOT = (1 << 0), /* only create on root cgrp */ 137 CFTYPE_NOT_ON_ROOT = (1 << 1), /* don't create on root cgrp */ 138 CFTYPE_NS_DELEGATABLE = (1 << 2), /* writeable beyond delegation boundaries */ 139 140 CFTYPE_NO_PREFIX = (1 << 3), /* (DON'T USE FOR NEW FILES) no subsys prefix */ 141 CFTYPE_WORLD_WRITABLE = (1 << 4), /* (DON'T USE FOR NEW FILES) S_IWUGO */ 142 CFTYPE_DEBUG = (1 << 5), /* create when cgroup_debug */ 143 144 /* internal flags, do not use outside cgroup core proper */ 145 __CFTYPE_ONLY_ON_DFL = (1 << 16), /* only on default hierarchy */ 146 __CFTYPE_NOT_ON_DFL = (1 << 17), /* not on default hierarchy */ 147 __CFTYPE_ADDED = (1 << 18), 148 }; 149 150 enum cgroup_attach_lock_mode { 151 /* Default */ 152 CGRP_ATTACH_LOCK_GLOBAL, 153 154 /* When pid=0 && threadgroup=false, see comments in cgroup_procs_write_start */ 155 CGRP_ATTACH_LOCK_NONE, 156 157 /* When favordynmods is on, see comments above CGRP_ROOT_FAVOR_DYNMODS */ 158 CGRP_ATTACH_LOCK_PER_THREADGROUP, 159 }; 160 161 /* 162 * cgroup_file is the handle for a file instance created in a cgroup which 163 * is used, for example, to generate file changed notifications. This can 164 * be obtained by setting cftype->file_offset. 165 */ 166 struct cgroup_file { 167 /* do not access any fields from outside cgroup core */ 168 struct kernfs_node *kn; 169 unsigned long notified_at; 170 struct timer_list notify_timer; 171 spinlock_t lock; 172 }; 173 174 /* 175 * Per-subsystem/per-cgroup state maintained by the system. This is the 176 * fundamental structural building block that controllers deal with. 177 * 178 * Fields marked with "PI:" are public and immutable and may be accessed 179 * directly without synchronization. 180 */ 181 struct cgroup_subsys_state { 182 /* PI: the cgroup that this css is attached to */ 183 struct cgroup *cgroup; 184 185 /* PI: the cgroup subsystem that this css is attached to */ 186 struct cgroup_subsys *ss; 187 188 /* reference count - access via css_[try]get() and css_put() */ 189 struct percpu_ref refcnt; 190 191 /* 192 * Depending on the context, this field is initialized 193 * via css_rstat_init() at different places: 194 * 195 * when css is associated with cgroup::self 196 * when css->cgroup is the root cgroup 197 * performed in cgroup_init() 198 * when css->cgroup is not the root cgroup 199 * performed in cgroup_create() 200 * when css is associated with a subsystem 201 * when css->cgroup is the root cgroup 202 * performed in cgroup_init_subsys() in the non-early path 203 * when css->cgroup is not the root cgroup 204 * performed in css_create() 205 */ 206 struct css_rstat_cpu __percpu *rstat_cpu; 207 208 /* 209 * siblings list anchored at the parent's ->children 210 * 211 * linkage is protected by cgroup_mutex or RCU 212 */ 213 struct list_head sibling; 214 struct list_head children; 215 216 /* 217 * PI: Subsys-unique ID. 0 is unused and root is always 1. The 218 * matching css can be looked up using css_from_id(). 219 */ 220 int id; 221 222 unsigned int flags; 223 224 /* 225 * Monotonically increasing unique serial number which defines a 226 * uniform order among all csses. It's guaranteed that all 227 * ->children lists are in the ascending order of ->serial_nr and 228 * used to allow interrupting and resuming iterations. 229 */ 230 u64 serial_nr; 231 232 /* 233 * Incremented by online self and children. Used to guarantee that 234 * parents are not offlined before their children. 235 */ 236 atomic_t online_cnt; 237 238 /* percpu_ref killing and RCU release */ 239 struct work_struct destroy_work; 240 struct rcu_work destroy_rwork; 241 242 /* 243 * PI: the parent css. Placed here for cache proximity to following 244 * fields of the containing structure. 245 */ 246 struct cgroup_subsys_state *parent; 247 248 /* 249 * Keep track of total numbers of visible descendant CSSes. 250 * The total number of dying CSSes is tracked in 251 * css->cgroup->nr_dying_subsys[ssid]. 252 * Protected by cgroup_mutex. 253 */ 254 int nr_descendants; 255 256 /* 257 * Hierarchical populated state. For cgroup->self, nr_populated_csets 258 * counts populated csets linked via cgrp_cset_link. 259 * nr_populated_children counts immediate-child csses whose own 260 * populated state is nonzero. Protected by css_set_lock. 261 */ 262 int nr_populated_csets; 263 int nr_populated_children; 264 265 /* deferred kill_css_finish() queued by css_update_populated() */ 266 struct work_struct kill_finish_work; 267 268 /* 269 * A singly-linked list of css structures to be rstat flushed. 270 * This is a scratch field to be used exclusively by 271 * css_rstat_flush(). 272 * 273 * Protected by rstat_base_lock when css is cgroup::self. 274 * Protected by css->ss->rstat_ss_lock otherwise. 275 */ 276 struct cgroup_subsys_state *rstat_flush_next; 277 }; 278 279 /* 280 * A css_set is a structure holding pointers to a set of 281 * cgroup_subsys_state objects. This saves space in the task struct 282 * object and speeds up fork()/exit(), since a single inc/dec and a 283 * list_add()/del() can bump the reference count on the entire cgroup 284 * set for a task. 285 */ 286 struct css_set { 287 /* 288 * Set of subsystem states, one for each subsystem. This array is 289 * immutable after creation apart from the init_css_set during 290 * subsystem registration (at boot time). 291 */ 292 struct cgroup_subsys_state *subsys[CGROUP_SUBSYS_COUNT]; 293 294 /* reference count */ 295 refcount_t refcount; 296 297 /* 298 * For a domain cgroup, the following points to self. If threaded, 299 * to the matching cset of the nearest domain ancestor. The 300 * dom_cset provides access to the domain cgroup and its csses to 301 * which domain level resource consumptions should be charged. 302 */ 303 struct css_set *dom_cset; 304 305 /* the default cgroup associated with this css_set */ 306 struct cgroup *dfl_cgrp; 307 308 /* internal task count, protected by css_set_lock */ 309 int nr_tasks; 310 311 /* 312 * Lists running through all tasks using this cgroup group. 313 * mg_tasks lists tasks which belong to this cset but are in the 314 * process of being migrated out or in. Protected by 315 * css_set_lock, but, during migration, once tasks are moved to 316 * mg_tasks, it can be read safely while holding cgroup_mutex. 317 */ 318 struct list_head tasks; 319 struct list_head mg_tasks; 320 struct list_head dying_tasks; 321 322 /* all css_task_iters currently walking this cset */ 323 struct list_head task_iters; 324 325 /* 326 * On the default hierarchy, ->subsys[ssid] may point to a css 327 * attached to an ancestor instead of the cgroup this css_set is 328 * associated with. The following node is anchored at 329 * ->subsys[ssid]->cgroup->e_csets[ssid] and provides a way to 330 * iterate through all css's attached to a given cgroup. 331 */ 332 struct list_head e_cset_node[CGROUP_SUBSYS_COUNT]; 333 334 /* all threaded csets whose ->dom_cset points to this cset */ 335 struct list_head threaded_csets; 336 struct list_head threaded_csets_node; 337 338 /* 339 * List running through all cgroup groups in the same hash 340 * slot. Protected by css_set_lock 341 */ 342 struct hlist_node hlist; 343 344 /* 345 * List of cgrp_cset_links pointing at cgroups referenced from this 346 * css_set. Protected by css_set_lock. 347 */ 348 struct list_head cgrp_links; 349 350 /* 351 * List of csets participating in the on-going migration either as 352 * source or destination. Protected by cgroup_mutex. 353 */ 354 struct list_head mg_src_preload_node; 355 struct list_head mg_dst_preload_node; 356 struct list_head mg_node; 357 358 /* 359 * If this cset is acting as the source of migration the following 360 * two fields are set. mg_src_cgrp and mg_dst_cgrp are 361 * respectively the source and destination cgroups of the on-going 362 * migration. mg_dst_cset is the destination cset the target tasks 363 * on this cset should be migrated to. Protected by cgroup_mutex. 364 */ 365 struct cgroup *mg_src_cgrp; 366 struct cgroup *mg_dst_cgrp; 367 struct css_set *mg_dst_cset; 368 369 /* dead and being drained, ignore for migration */ 370 bool dead; 371 372 /* For RCU-protected deletion */ 373 struct rcu_head rcu_head; 374 }; 375 376 struct cgroup_base_stat { 377 struct task_cputime cputime; 378 379 #ifdef CONFIG_SCHED_CORE 380 u64 forceidle_sum; 381 #endif 382 u64 ntime; 383 }; 384 385 /* 386 * rstat - cgroup scalable recursive statistics. Accounting is done 387 * per-cpu in css_rstat_cpu which is then lazily propagated up the 388 * hierarchy on reads. 389 * 390 * When a stat gets updated, the css_rstat_cpu and its ancestors are 391 * linked into the updated tree. On the following read, propagation only 392 * considers and consumes the updated tree. This makes reading O(the 393 * number of descendants which have been active since last read) instead of 394 * O(the total number of descendants). 395 * 396 * This is important because there can be a lot of (draining) cgroups which 397 * aren't active and stat may be read frequently. The combination can 398 * become very expensive. By propagating selectively, increasing reading 399 * frequency decreases the cost of each read. 400 * 401 * This struct hosts both the fields which implement the above - 402 * updated_children and updated_next. 403 */ 404 struct css_rstat_cpu { 405 /* 406 * Child cgroups with stat updates on this cpu since the last read 407 * are linked on the parent's ->updated_children through 408 * ->updated_next. updated_children is terminated by its container css. 409 */ 410 struct cgroup_subsys_state *updated_children; 411 struct cgroup_subsys_state *updated_next; /* NULL if not on the list */ 412 413 struct llist_node lnode; /* lockless list for update */ 414 struct cgroup_subsys_state *owner; /* back pointer */ 415 }; 416 417 /* 418 * This struct hosts the fields which track basic resource statistics on 419 * top of it - bsync, bstat and last_bstat. 420 */ 421 struct cgroup_rstat_base_cpu { 422 /* 423 * ->bsync protects ->bstat. These are the only fields which get 424 * updated in the hot path. 425 */ 426 struct u64_stats_sync bsync; 427 struct cgroup_base_stat bstat; 428 429 /* 430 * Snapshots at the last reading. These are used to calculate the 431 * deltas to propagate to the global counters. 432 */ 433 struct cgroup_base_stat last_bstat; 434 435 /* 436 * This field is used to record the cumulative per-cpu time of 437 * the cgroup and its descendants. Currently it can be read via 438 * eBPF/drgn etc, and we are still trying to determine how to 439 * expose it in the cgroupfs interface. 440 */ 441 struct cgroup_base_stat subtree_bstat; 442 443 /* 444 * Snapshots at the last reading. These are used to calculate the 445 * deltas to propagate to the per-cpu subtree_bstat. 446 */ 447 struct cgroup_base_stat last_subtree_bstat; 448 }; 449 450 struct cgroup_freezer_state { 451 /* Should the cgroup and its descendants be frozen. */ 452 bool freeze; 453 454 /* Should the cgroup actually be frozen? */ 455 bool e_freeze; 456 457 /* Fields below are protected by css_set_lock */ 458 459 /* Number of frozen descendant cgroups */ 460 int nr_frozen_descendants; 461 462 /* 463 * Number of tasks, which are counted as frozen: 464 * frozen, SIGSTOPped, and PTRACEd. 465 */ 466 int nr_frozen_tasks; 467 468 /* Freeze time data consistency protection */ 469 seqcount_spinlock_t freeze_seq; 470 471 /* 472 * Most recent time the cgroup was requested to freeze. 473 * Accesses guarded by freeze_seq counter. Writes serialized 474 * by css_set_lock. 475 */ 476 u64 freeze_start_nsec; 477 478 /* 479 * Total duration the cgroup has spent freezing. 480 * Accesses guarded by freeze_seq counter. Writes serialized 481 * by css_set_lock. 482 */ 483 u64 frozen_nsec; 484 }; 485 486 struct cgroup { 487 /* self css with NULL ->ss, points back to this cgroup */ 488 struct cgroup_subsys_state self; 489 490 unsigned long flags; /* "unsigned long" so bitops work */ 491 492 /* 493 * The depth this cgroup is at. The root is at depth zero and each 494 * step down the hierarchy increments the level. This along with 495 * ancestors[] can determine whether a given cgroup is a 496 * descendant of another without traversing the hierarchy. 497 */ 498 int level; 499 500 /* Maximum allowed descent tree depth */ 501 int max_depth; 502 503 /* 504 * Keep track of total numbers of visible and dying descent cgroups. 505 * Dying cgroups are cgroups which were deleted by a user, 506 * but are still existing because someone else is holding a reference. 507 * max_descendants is a maximum allowed number of descent cgroups. 508 * 509 * nr_descendants and nr_dying_descendants are protected 510 * by cgroup_mutex and css_set_lock. It's fine to read them holding 511 * any of cgroup_mutex and css_set_lock; for writing both locks 512 * should be held. 513 */ 514 int nr_descendants; 515 int nr_dying_descendants; 516 int max_descendants; 517 518 /* 519 * Domain/threaded split of self.nr_populated_children: each counts 520 * immediate-child cgroups whose subtree is populated and sums to 521 * self.nr_populated_children. Kept as separate fields to allow readers 522 * like cgroup_can_be_thread_root() unlocked access. Protected by 523 * css_set_lock; updated by css_update_populated(). 524 */ 525 int nr_populated_domain_children; 526 int nr_populated_threaded_children; 527 528 int nr_threaded_children; /* # of live threaded child cgroups */ 529 530 /* 531 * Sequence number for cgroup.kill. Incremented with both cgroup_mutex 532 * and css_set_lock held. Readers hold either one. 533 */ 534 unsigned int kill_seq; 535 536 struct kernfs_node *kn; /* cgroup kernfs entry */ 537 struct cgroup_file procs_file; /* handle for "cgroup.procs" */ 538 struct cgroup_file events_file; /* handle for "cgroup.events" */ 539 540 /* handles for "{cpu,memory,io,irq}.pressure" */ 541 struct cgroup_file psi_files[NR_PSI_RESOURCES]; 542 543 /* 544 * The bitmask of subsystems enabled on the child cgroups. 545 * ->subtree_control is the one configured through 546 * "cgroup.subtree_control" while ->subtree_ss_mask is the effective 547 * one which may have more subsystems enabled. Controller knobs 548 * are made available iff it's enabled in ->subtree_control. 549 */ 550 u32 subtree_control; 551 u32 subtree_ss_mask; 552 u32 old_subtree_control; 553 u32 old_subtree_ss_mask; 554 555 /* Private pointers for each registered subsystem */ 556 struct cgroup_subsys_state __rcu *subsys[CGROUP_SUBSYS_COUNT]; 557 558 /* 559 * Effective css for each subsystem: the css of the nearest ancestor, 560 * including this cgroup, that has the subsystem enabled, or the root 561 * css if the subsystem isn't bound to this hierarchy. Updated under 562 * cgroup_mutex and read under RCU. 563 */ 564 struct cgroup_subsys_state __rcu *e_css[CGROUP_SUBSYS_COUNT]; 565 566 /* 567 * Keep track of total number of dying CSSes at and below this cgroup. 568 * Protected by cgroup_mutex. 569 */ 570 int nr_dying_subsys[CGROUP_SUBSYS_COUNT]; 571 572 struct cgroup_root *root; 573 574 /* 575 * List of cgrp_cset_links pointing at css_sets with tasks in this 576 * cgroup. Protected by css_set_lock. 577 */ 578 struct list_head cset_links; 579 580 /* 581 * On the default hierarchy, a css_set for a cgroup with some 582 * susbsys disabled will point to css's which are associated with 583 * the closest ancestor which has the subsys enabled. The 584 * following lists all css_sets which point to this cgroup's css 585 * for the given subsystem. 586 */ 587 struct list_head e_csets[CGROUP_SUBSYS_COUNT]; 588 589 /* 590 * If !threaded, self. If threaded, it points to the nearest 591 * domain ancestor. Inside a threaded subtree, cgroups are exempt 592 * from process granularity and no-internal-task constraint. 593 * Domain level resource consumptions which aren't tied to a 594 * specific task are charged to the dom_cgrp. 595 */ 596 struct cgroup *dom_cgrp; 597 struct cgroup *old_dom_cgrp; /* used while enabling threaded */ 598 599 /* 600 * Depending on the context, this field is initialized via 601 * css_rstat_init() at different places: 602 * 603 * when cgroup is the root cgroup 604 * performed in cgroup_setup_root() 605 * otherwise 606 * performed in cgroup_create() 607 */ 608 struct cgroup_rstat_base_cpu __percpu *rstat_base_cpu; 609 610 /* 611 * Add padding to keep the read mostly rstat per-cpu pointer on a 612 * different cacheline than the following *bstat fields which can have 613 * frequent updates. 614 */ 615 CACHELINE_PADDING(_pad_); 616 617 /* cgroup basic resource statistics */ 618 struct cgroup_base_stat last_bstat; 619 struct cgroup_base_stat bstat; 620 struct prev_cputime prev_cputime; /* for printing out cputime */ 621 622 /* 623 * list of pidlists, up to two for each namespace (one for procs, one 624 * for tasks); created on demand. 625 */ 626 struct list_head pidlists; 627 struct mutex pidlist_mutex; 628 629 /* used to wait for offlining of csses */ 630 wait_queue_head_t offline_waitq; 631 632 /* used to schedule release agent */ 633 struct work_struct release_agent_work; 634 635 /* used to track pressure stalls */ 636 struct psi_group *psi; 637 638 /* used to store eBPF programs */ 639 struct cgroup_bpf bpf; 640 641 /* Used to store internal freezer state */ 642 struct cgroup_freezer_state freezer; 643 644 #ifdef CONFIG_BPF_SYSCALL 645 struct bpf_local_storage __rcu *bpf_cgrp_storage; 646 #endif 647 #ifdef CONFIG_EXT_SUB_SCHED 648 struct scx_sched __rcu *scx_sched; 649 #endif 650 651 /* All ancestors including self */ 652 union { 653 DECLARE_FLEX_ARRAY(struct cgroup *, ancestors); 654 struct { 655 struct cgroup *_root_ancestor; 656 DECLARE_FLEX_ARRAY(struct cgroup *, _low_ancestors); 657 }; 658 }; 659 }; 660 661 /* 662 * A cgroup_root represents the root of a cgroup hierarchy, and may be 663 * associated with a kernfs_root to form an active hierarchy. This is 664 * internal to cgroup core. Don't access directly from controllers. 665 */ 666 struct cgroup_root { 667 struct kernfs_root *kf_root; 668 669 /* The bitmask of subsystems attached to this hierarchy */ 670 unsigned int subsys_mask; 671 672 /* Unique id for this hierarchy. */ 673 int hierarchy_id; 674 675 /* A list running through the active hierarchies */ 676 struct list_head root_list; 677 struct rcu_head rcu; /* Must be near the top */ 678 679 /* Number of cgroups in the hierarchy, used only for /proc/cgroups */ 680 atomic_t nr_cgrps; 681 682 /* Hierarchy-specific flags */ 683 unsigned int flags; 684 685 /* The path to use for release notifications. */ 686 char release_agent_path[PATH_MAX]; 687 688 /* The name for this hierarchy - may be empty */ 689 char name[MAX_CGROUP_ROOT_NAMELEN]; 690 691 /* 692 * The root cgroup. The containing cgroup_root will be destroyed on its 693 * release. This must be embedded last due to flexible array at the end 694 * of struct cgroup. 695 */ 696 struct cgroup cgrp; 697 }; 698 699 /* 700 * struct cftype: handler definitions for cgroup control files 701 * 702 * When reading/writing to a file: 703 * - the cgroup to use is file->f_path.dentry->d_parent->d_fsdata 704 * - the 'cftype' of the file is file->f_path.dentry->d_fsdata 705 */ 706 struct cftype { 707 /* 708 * Name of the subsystem is prepended in cgroup_file_name(). 709 * Zero length string indicates end of cftype array. 710 */ 711 char name[MAX_CFTYPE_NAME]; 712 unsigned long private; 713 714 /* 715 * The maximum length of string, excluding trailing nul, that can 716 * be passed to write. If < PAGE_SIZE-1, PAGE_SIZE-1 is assumed. 717 */ 718 size_t max_write_len; 719 720 /* CFTYPE_* flags */ 721 unsigned int flags; 722 723 /* 724 * If non-zero, should contain the offset from the start of css to 725 * a struct cgroup_file field. cgroup will record the handle of 726 * the created file into it. The recorded handle can be used as 727 * long as the containing css remains accessible. 728 */ 729 unsigned int file_offset; 730 731 /* 732 * Fields used for internal bookkeeping. Initialized automatically 733 * during registration. 734 */ 735 struct cgroup_subsys *ss; /* NULL for cgroup core files */ 736 struct list_head node; /* anchored at ss->cfts */ 737 struct kernfs_ops *kf_ops; 738 739 int (*open)(struct kernfs_open_file *of); 740 void (*release)(struct kernfs_open_file *of); 741 742 /* 743 * read_u64() is a shortcut for the common case of returning a 744 * single integer. Use it in place of read() 745 */ 746 u64 (*read_u64)(struct cgroup_subsys_state *css, struct cftype *cft); 747 /* 748 * read_s64() is a signed version of read_u64() 749 */ 750 s64 (*read_s64)(struct cgroup_subsys_state *css, struct cftype *cft); 751 752 /* generic seq_file read interface */ 753 int (*seq_show)(struct seq_file *sf, void *v); 754 755 /* optional ops, implement all or none */ 756 void *(*seq_start)(struct seq_file *sf, loff_t *ppos); 757 void *(*seq_next)(struct seq_file *sf, void *v, loff_t *ppos); 758 void (*seq_stop)(struct seq_file *sf, void *v); 759 760 /* 761 * write_u64() is a shortcut for the common case of accepting 762 * a single integer (as parsed by simple_strtoull) from 763 * userspace. Use in place of write(); return 0 or error. 764 */ 765 int (*write_u64)(struct cgroup_subsys_state *css, struct cftype *cft, 766 u64 val); 767 /* 768 * write_s64() is a signed version of write_u64() 769 */ 770 int (*write_s64)(struct cgroup_subsys_state *css, struct cftype *cft, 771 s64 val); 772 773 /* 774 * write() is the generic write callback which maps directly to 775 * kernfs write operation and overrides all other operations. 776 * Maximum write size is determined by ->max_write_len. Use 777 * of_css/cft() to access the associated css and cft. 778 */ 779 ssize_t (*write)(struct kernfs_open_file *of, 780 char *buf, size_t nbytes, loff_t off); 781 782 __poll_t (*poll)(struct kernfs_open_file *of, 783 struct poll_table_struct *pt); 784 785 struct lock_class_key lockdep_key; 786 }; 787 788 /* 789 * Control Group subsystem type. 790 * See Documentation/admin-guide/cgroup-v1/cgroups.rst for details 791 */ 792 struct cgroup_subsys { 793 struct cgroup_subsys_state *(*css_alloc)(struct cgroup_subsys_state *parent_css); 794 int (*css_online)(struct cgroup_subsys_state *css); 795 void (*css_offline)(struct cgroup_subsys_state *css); 796 void (*css_released)(struct cgroup_subsys_state *css); 797 void (*css_free)(struct cgroup_subsys_state *css); 798 void (*css_reset)(struct cgroup_subsys_state *css); 799 void (*css_killed)(struct cgroup_subsys_state *css); 800 void (*css_rstat_flush)(struct cgroup_subsys_state *css, int cpu); 801 int (*css_extra_stat_show)(struct seq_file *seq, 802 struct cgroup_subsys_state *css); 803 int (*css_local_stat_show)(struct seq_file *seq, 804 struct cgroup_subsys_state *css); 805 806 int (*can_attach)(struct cgroup_taskset *tset); 807 void (*cancel_attach)(struct cgroup_taskset *tset); 808 void (*attach)(struct cgroup_taskset *tset); 809 int (*can_fork)(struct task_struct *task, 810 struct css_set *cset); 811 void (*cancel_fork)(struct task_struct *task, struct css_set *cset); 812 void (*fork)(struct task_struct *task); 813 void (*exit)(struct task_struct *task); 814 void (*release)(struct task_struct *task); 815 void (*bind)(struct cgroup_subsys_state *root_css); 816 817 bool early_init:1; 818 819 /* 820 * If %true, the controller, on the default hierarchy, doesn't show 821 * up in "cgroup.controllers" or "cgroup.subtree_control", is 822 * implicitly enabled on all cgroups on the default hierarchy, and 823 * bypasses the "no internal process" constraint. This is for 824 * utility type controllers which is transparent to userland. 825 * 826 * An implicit controller can be stolen from the default hierarchy 827 * anytime and thus must be okay with offline csses from previous 828 * hierarchies coexisting with csses for the current one. 829 */ 830 bool implicit_on_dfl:1; 831 832 /* 833 * If %true, the controller, supports threaded mode on the default 834 * hierarchy. In a threaded subtree, both process granularity and 835 * no-internal-process constraint are ignored and a threaded 836 * controllers should be able to handle that. 837 * 838 * Note that as an implicit controller is automatically enabled on 839 * all cgroups on the default hierarchy, it should also be 840 * threaded. implicit && !threaded is not supported. 841 */ 842 bool threaded:1; 843 844 /* the following two fields are initialized automatically during boot */ 845 int id; 846 const char *name; 847 848 /* optional, initialized automatically during boot if not set */ 849 const char *legacy_name; 850 851 /* link to parent, protected by cgroup_lock() */ 852 struct cgroup_root *root; 853 854 /* idr for css->id */ 855 struct idr css_idr; 856 857 /* 858 * List of cftypes. Each entry is the first entry of an array 859 * terminated by zero length name. 860 */ 861 struct list_head cfts; 862 863 /* 864 * Base cftypes which are automatically registered. The two can 865 * point to the same array. 866 */ 867 struct cftype *dfl_cftypes; /* for the default hierarchy */ 868 struct cftype *legacy_cftypes; /* for the legacy hierarchies */ 869 870 /* 871 * A subsystem may depend on other subsystems. When such subsystem 872 * is enabled on a cgroup, the depended-upon subsystems are enabled 873 * together if available. Subsystems enabled due to dependency are 874 * not visible to userland until explicitly enabled. The following 875 * specifies the mask of subsystems that this one depends on. 876 */ 877 unsigned int depends_on; 878 879 spinlock_t rstat_ss_lock; 880 struct llist_head __percpu *lhead; /* lockless update list head */ 881 }; 882 883 extern struct percpu_rw_semaphore cgroup_threadgroup_rwsem; 884 extern bool cgroup_enable_per_threadgroup_rwsem; 885 886 struct cgroup_of_peak { 887 unsigned long value; 888 struct list_head list; 889 }; 890 891 /** 892 * cgroup_threadgroup_change_begin - threadgroup exclusion for cgroups 893 * @tsk: target task 894 * 895 * Allows cgroup operations to synchronize against threadgroup changes 896 * using a global percpu_rw_semaphore and a per threadgroup rw_semaphore when 897 * favordynmods is on. See the comment above CGRP_ROOT_FAVOR_DYNMODS definition. 898 */ 899 static inline void cgroup_threadgroup_change_begin(struct task_struct *tsk) 900 { 901 percpu_down_read(&cgroup_threadgroup_rwsem); 902 if (cgroup_enable_per_threadgroup_rwsem) 903 down_read(&tsk->signal->cgroup_threadgroup_rwsem); 904 } 905 906 /** 907 * cgroup_threadgroup_change_end - threadgroup exclusion for cgroups 908 * @tsk: target task 909 * 910 * Counterpart of cgroup_threadgroup_change_begin(). 911 */ 912 static inline void cgroup_threadgroup_change_end(struct task_struct *tsk) 913 { 914 if (cgroup_enable_per_threadgroup_rwsem) 915 up_read(&tsk->signal->cgroup_threadgroup_rwsem); 916 percpu_up_read(&cgroup_threadgroup_rwsem); 917 } 918 919 #else /* CONFIG_CGROUPS */ 920 921 #define CGROUP_SUBSYS_COUNT 0 922 923 static inline void cgroup_threadgroup_change_begin(struct task_struct *tsk) 924 { 925 might_sleep(); 926 } 927 928 static inline void cgroup_threadgroup_change_end(struct task_struct *tsk) {} 929 930 #endif /* CONFIG_CGROUPS */ 931 932 #ifdef CONFIG_SOCK_CGROUP_DATA 933 934 /* 935 * sock_cgroup_data is embedded at sock->sk_cgrp_data and contains 936 * per-socket cgroup information except for memcg association. 937 * 938 * On legacy hierarchies, net_prio and net_cls controllers directly 939 * set attributes on each sock which can then be tested by the network 940 * layer. On the default hierarchy, each sock is associated with the 941 * cgroup it was created in and the networking layer can match the 942 * cgroup directly. 943 */ 944 struct sock_cgroup_data { 945 struct cgroup *cgroup; /* v2 */ 946 #ifdef CONFIG_CGROUP_NET_CLASSID 947 u32 classid; /* v1 */ 948 #endif 949 #ifdef CONFIG_CGROUP_NET_PRIO 950 u16 prioidx; /* v1 */ 951 #endif 952 }; 953 954 static inline u16 sock_cgroup_prioidx(const struct sock_cgroup_data *skcd) 955 { 956 #ifdef CONFIG_CGROUP_NET_PRIO 957 return READ_ONCE(skcd->prioidx); 958 #else 959 return 1; 960 #endif 961 } 962 963 #ifdef CONFIG_CGROUP_NET_CLASSID 964 static inline u32 sock_cgroup_classid(const struct sock_cgroup_data *skcd) 965 { 966 return READ_ONCE(skcd->classid); 967 } 968 #endif 969 970 static inline void sock_cgroup_set_prioidx(struct sock_cgroup_data *skcd, 971 u16 prioidx) 972 { 973 #ifdef CONFIG_CGROUP_NET_PRIO 974 WRITE_ONCE(skcd->prioidx, prioidx); 975 #endif 976 } 977 978 #ifdef CONFIG_CGROUP_NET_CLASSID 979 static inline void sock_cgroup_set_classid(struct sock_cgroup_data *skcd, 980 u32 classid) 981 { 982 WRITE_ONCE(skcd->classid, classid); 983 } 984 #endif 985 986 #else /* CONFIG_SOCK_CGROUP_DATA */ 987 988 struct sock_cgroup_data { 989 }; 990 991 #endif /* CONFIG_SOCK_CGROUP_DATA */ 992 993 #endif /* _LINUX_CGROUP_DEFS_H */ 994