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