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