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 /* sequence number for cgroup.kill, serialized by css_set_lock. */
531 unsigned int kill_seq;
532
533 struct kernfs_node *kn; /* cgroup kernfs entry */
534 struct cgroup_file procs_file; /* handle for "cgroup.procs" */
535 struct cgroup_file events_file; /* handle for "cgroup.events" */
536
537 /* handles for "{cpu,memory,io,irq}.pressure" */
538 struct cgroup_file psi_files[NR_PSI_RESOURCES];
539
540 /*
541 * The bitmask of subsystems enabled on the child cgroups.
542 * ->subtree_control is the one configured through
543 * "cgroup.subtree_control" while ->subtree_ss_mask is the effective
544 * one which may have more subsystems enabled. Controller knobs
545 * are made available iff it's enabled in ->subtree_control.
546 */
547 u32 subtree_control;
548 u32 subtree_ss_mask;
549 u32 old_subtree_control;
550 u32 old_subtree_ss_mask;
551
552 /* Private pointers for each registered subsystem */
553 struct cgroup_subsys_state __rcu *subsys[CGROUP_SUBSYS_COUNT];
554
555 /*
556 * Keep track of total number of dying CSSes at and below this cgroup.
557 * Protected by cgroup_mutex.
558 */
559 int nr_dying_subsys[CGROUP_SUBSYS_COUNT];
560
561 struct cgroup_root *root;
562
563 /*
564 * List of cgrp_cset_links pointing at css_sets with tasks in this
565 * cgroup. Protected by css_set_lock.
566 */
567 struct list_head cset_links;
568
569 /*
570 * On the default hierarchy, a css_set for a cgroup with some
571 * susbsys disabled will point to css's which are associated with
572 * the closest ancestor which has the subsys enabled. The
573 * following lists all css_sets which point to this cgroup's css
574 * for the given subsystem.
575 */
576 struct list_head e_csets[CGROUP_SUBSYS_COUNT];
577
578 /*
579 * If !threaded, self. If threaded, it points to the nearest
580 * domain ancestor. Inside a threaded subtree, cgroups are exempt
581 * from process granularity and no-internal-task constraint.
582 * Domain level resource consumptions which aren't tied to a
583 * specific task are charged to the dom_cgrp.
584 */
585 struct cgroup *dom_cgrp;
586 struct cgroup *old_dom_cgrp; /* used while enabling threaded */
587
588 /*
589 * Depending on the context, this field is initialized via
590 * css_rstat_init() at different places:
591 *
592 * when cgroup is the root cgroup
593 * performed in cgroup_setup_root()
594 * otherwise
595 * performed in cgroup_create()
596 */
597 struct cgroup_rstat_base_cpu __percpu *rstat_base_cpu;
598
599 /*
600 * Add padding to keep the read mostly rstat per-cpu pointer on a
601 * different cacheline than the following *bstat fields which can have
602 * frequent updates.
603 */
604 CACHELINE_PADDING(_pad_);
605
606 /* cgroup basic resource statistics */
607 struct cgroup_base_stat last_bstat;
608 struct cgroup_base_stat bstat;
609 struct prev_cputime prev_cputime; /* for printing out cputime */
610
611 /*
612 * list of pidlists, up to two for each namespace (one for procs, one
613 * for tasks); created on demand.
614 */
615 struct list_head pidlists;
616 struct mutex pidlist_mutex;
617
618 /* used to wait for offlining of csses */
619 wait_queue_head_t offline_waitq;
620
621 /* used to schedule release agent */
622 struct work_struct release_agent_work;
623
624 /* used to track pressure stalls */
625 struct psi_group *psi;
626
627 /* used to store eBPF programs */
628 struct cgroup_bpf bpf;
629
630 /* Used to store internal freezer state */
631 struct cgroup_freezer_state freezer;
632
633 #ifdef CONFIG_BPF_SYSCALL
634 struct bpf_local_storage __rcu *bpf_cgrp_storage;
635 #endif
636 #ifdef CONFIG_EXT_SUB_SCHED
637 struct scx_sched __rcu *scx_sched;
638 #endif
639
640 /* All ancestors including self */
641 union {
642 DECLARE_FLEX_ARRAY(struct cgroup *, ancestors);
643 struct {
644 struct cgroup *_root_ancestor;
645 DECLARE_FLEX_ARRAY(struct cgroup *, _low_ancestors);
646 };
647 };
648 };
649
650 /*
651 * A cgroup_root represents the root of a cgroup hierarchy, and may be
652 * associated with a kernfs_root to form an active hierarchy. This is
653 * internal to cgroup core. Don't access directly from controllers.
654 */
655 struct cgroup_root {
656 struct kernfs_root *kf_root;
657
658 /* The bitmask of subsystems attached to this hierarchy */
659 unsigned int subsys_mask;
660
661 /* Unique id for this hierarchy. */
662 int hierarchy_id;
663
664 /* A list running through the active hierarchies */
665 struct list_head root_list;
666 struct rcu_head rcu; /* Must be near the top */
667
668 /* Number of cgroups in the hierarchy, used only for /proc/cgroups */
669 atomic_t nr_cgrps;
670
671 /* Hierarchy-specific flags */
672 unsigned int flags;
673
674 /* The path to use for release notifications. */
675 char release_agent_path[PATH_MAX];
676
677 /* The name for this hierarchy - may be empty */
678 char name[MAX_CGROUP_ROOT_NAMELEN];
679
680 /*
681 * The root cgroup. The containing cgroup_root will be destroyed on its
682 * release. This must be embedded last due to flexible array at the end
683 * of struct cgroup.
684 */
685 struct cgroup cgrp;
686 };
687
688 /*
689 * struct cftype: handler definitions for cgroup control files
690 *
691 * When reading/writing to a file:
692 * - the cgroup to use is file->f_path.dentry->d_parent->d_fsdata
693 * - the 'cftype' of the file is file->f_path.dentry->d_fsdata
694 */
695 struct cftype {
696 /*
697 * Name of the subsystem is prepended in cgroup_file_name().
698 * Zero length string indicates end of cftype array.
699 */
700 char name[MAX_CFTYPE_NAME];
701 unsigned long private;
702
703 /*
704 * The maximum length of string, excluding trailing nul, that can
705 * be passed to write. If < PAGE_SIZE-1, PAGE_SIZE-1 is assumed.
706 */
707 size_t max_write_len;
708
709 /* CFTYPE_* flags */
710 unsigned int flags;
711
712 /*
713 * If non-zero, should contain the offset from the start of css to
714 * a struct cgroup_file field. cgroup will record the handle of
715 * the created file into it. The recorded handle can be used as
716 * long as the containing css remains accessible.
717 */
718 unsigned int file_offset;
719
720 /*
721 * Fields used for internal bookkeeping. Initialized automatically
722 * during registration.
723 */
724 struct cgroup_subsys *ss; /* NULL for cgroup core files */
725 struct list_head node; /* anchored at ss->cfts */
726 struct kernfs_ops *kf_ops;
727
728 int (*open)(struct kernfs_open_file *of);
729 void (*release)(struct kernfs_open_file *of);
730
731 /*
732 * read_u64() is a shortcut for the common case of returning a
733 * single integer. Use it in place of read()
734 */
735 u64 (*read_u64)(struct cgroup_subsys_state *css, struct cftype *cft);
736 /*
737 * read_s64() is a signed version of read_u64()
738 */
739 s64 (*read_s64)(struct cgroup_subsys_state *css, struct cftype *cft);
740
741 /* generic seq_file read interface */
742 int (*seq_show)(struct seq_file *sf, void *v);
743
744 /* optional ops, implement all or none */
745 void *(*seq_start)(struct seq_file *sf, loff_t *ppos);
746 void *(*seq_next)(struct seq_file *sf, void *v, loff_t *ppos);
747 void (*seq_stop)(struct seq_file *sf, void *v);
748
749 /*
750 * write_u64() is a shortcut for the common case of accepting
751 * a single integer (as parsed by simple_strtoull) from
752 * userspace. Use in place of write(); return 0 or error.
753 */
754 int (*write_u64)(struct cgroup_subsys_state *css, struct cftype *cft,
755 u64 val);
756 /*
757 * write_s64() is a signed version of write_u64()
758 */
759 int (*write_s64)(struct cgroup_subsys_state *css, struct cftype *cft,
760 s64 val);
761
762 /*
763 * write() is the generic write callback which maps directly to
764 * kernfs write operation and overrides all other operations.
765 * Maximum write size is determined by ->max_write_len. Use
766 * of_css/cft() to access the associated css and cft.
767 */
768 ssize_t (*write)(struct kernfs_open_file *of,
769 char *buf, size_t nbytes, loff_t off);
770
771 __poll_t (*poll)(struct kernfs_open_file *of,
772 struct poll_table_struct *pt);
773
774 struct lock_class_key lockdep_key;
775 };
776
777 /*
778 * Control Group subsystem type.
779 * See Documentation/admin-guide/cgroup-v1/cgroups.rst for details
780 */
781 struct cgroup_subsys {
782 struct cgroup_subsys_state *(*css_alloc)(struct cgroup_subsys_state *parent_css);
783 int (*css_online)(struct cgroup_subsys_state *css);
784 void (*css_offline)(struct cgroup_subsys_state *css);
785 void (*css_released)(struct cgroup_subsys_state *css);
786 void (*css_free)(struct cgroup_subsys_state *css);
787 void (*css_reset)(struct cgroup_subsys_state *css);
788 void (*css_killed)(struct cgroup_subsys_state *css);
789 void (*css_rstat_flush)(struct cgroup_subsys_state *css, int cpu);
790 int (*css_extra_stat_show)(struct seq_file *seq,
791 struct cgroup_subsys_state *css);
792 int (*css_local_stat_show)(struct seq_file *seq,
793 struct cgroup_subsys_state *css);
794
795 int (*can_attach)(struct cgroup_taskset *tset);
796 void (*cancel_attach)(struct cgroup_taskset *tset);
797 void (*attach)(struct cgroup_taskset *tset);
798 int (*can_fork)(struct task_struct *task,
799 struct css_set *cset);
800 void (*cancel_fork)(struct task_struct *task, struct css_set *cset);
801 void (*fork)(struct task_struct *task);
802 void (*exit)(struct task_struct *task);
803 void (*release)(struct task_struct *task);
804 void (*bind)(struct cgroup_subsys_state *root_css);
805
806 bool early_init:1;
807
808 /*
809 * If %true, the controller, on the default hierarchy, doesn't show
810 * up in "cgroup.controllers" or "cgroup.subtree_control", is
811 * implicitly enabled on all cgroups on the default hierarchy, and
812 * bypasses the "no internal process" constraint. This is for
813 * utility type controllers which is transparent to userland.
814 *
815 * An implicit controller can be stolen from the default hierarchy
816 * anytime and thus must be okay with offline csses from previous
817 * hierarchies coexisting with csses for the current one.
818 */
819 bool implicit_on_dfl:1;
820
821 /*
822 * If %true, the controller, supports threaded mode on the default
823 * hierarchy. In a threaded subtree, both process granularity and
824 * no-internal-process constraint are ignored and a threaded
825 * controllers should be able to handle that.
826 *
827 * Note that as an implicit controller is automatically enabled on
828 * all cgroups on the default hierarchy, it should also be
829 * threaded. implicit && !threaded is not supported.
830 */
831 bool threaded:1;
832
833 /* the following two fields are initialized automatically during boot */
834 int id;
835 const char *name;
836
837 /* optional, initialized automatically during boot if not set */
838 const char *legacy_name;
839
840 /* link to parent, protected by cgroup_lock() */
841 struct cgroup_root *root;
842
843 /* idr for css->id */
844 struct idr css_idr;
845
846 /*
847 * List of cftypes. Each entry is the first entry of an array
848 * terminated by zero length name.
849 */
850 struct list_head cfts;
851
852 /*
853 * Base cftypes which are automatically registered. The two can
854 * point to the same array.
855 */
856 struct cftype *dfl_cftypes; /* for the default hierarchy */
857 struct cftype *legacy_cftypes; /* for the legacy hierarchies */
858
859 /*
860 * A subsystem may depend on other subsystems. When such subsystem
861 * is enabled on a cgroup, the depended-upon subsystems are enabled
862 * together if available. Subsystems enabled due to dependency are
863 * not visible to userland until explicitly enabled. The following
864 * specifies the mask of subsystems that this one depends on.
865 */
866 unsigned int depends_on;
867
868 spinlock_t rstat_ss_lock;
869 struct llist_head __percpu *lhead; /* lockless update list head */
870 };
871
872 extern struct percpu_rw_semaphore cgroup_threadgroup_rwsem;
873 extern bool cgroup_enable_per_threadgroup_rwsem;
874
875 struct cgroup_of_peak {
876 unsigned long value;
877 struct list_head list;
878 };
879
880 /**
881 * cgroup_threadgroup_change_begin - threadgroup exclusion for cgroups
882 * @tsk: target task
883 *
884 * Allows cgroup operations to synchronize against threadgroup changes
885 * using a global percpu_rw_semaphore and a per threadgroup rw_semaphore when
886 * favordynmods is on. See the comment above CGRP_ROOT_FAVOR_DYNMODS definition.
887 */
cgroup_threadgroup_change_begin(struct task_struct * tsk)888 static inline void cgroup_threadgroup_change_begin(struct task_struct *tsk)
889 {
890 percpu_down_read(&cgroup_threadgroup_rwsem);
891 if (cgroup_enable_per_threadgroup_rwsem)
892 down_read(&tsk->signal->cgroup_threadgroup_rwsem);
893 }
894
895 /**
896 * cgroup_threadgroup_change_end - threadgroup exclusion for cgroups
897 * @tsk: target task
898 *
899 * Counterpart of cgroup_threadgroup_change_begin().
900 */
cgroup_threadgroup_change_end(struct task_struct * tsk)901 static inline void cgroup_threadgroup_change_end(struct task_struct *tsk)
902 {
903 if (cgroup_enable_per_threadgroup_rwsem)
904 up_read(&tsk->signal->cgroup_threadgroup_rwsem);
905 percpu_up_read(&cgroup_threadgroup_rwsem);
906 }
907
908 #else /* CONFIG_CGROUPS */
909
910 #define CGROUP_SUBSYS_COUNT 0
911
cgroup_threadgroup_change_begin(struct task_struct * tsk)912 static inline void cgroup_threadgroup_change_begin(struct task_struct *tsk)
913 {
914 might_sleep();
915 }
916
cgroup_threadgroup_change_end(struct task_struct * tsk)917 static inline void cgroup_threadgroup_change_end(struct task_struct *tsk) {}
918
919 #endif /* CONFIG_CGROUPS */
920
921 #ifdef CONFIG_SOCK_CGROUP_DATA
922
923 /*
924 * sock_cgroup_data is embedded at sock->sk_cgrp_data and contains
925 * per-socket cgroup information except for memcg association.
926 *
927 * On legacy hierarchies, net_prio and net_cls controllers directly
928 * set attributes on each sock which can then be tested by the network
929 * layer. On the default hierarchy, each sock is associated with the
930 * cgroup it was created in and the networking layer can match the
931 * cgroup directly.
932 */
933 struct sock_cgroup_data {
934 struct cgroup *cgroup; /* v2 */
935 #ifdef CONFIG_CGROUP_NET_CLASSID
936 u32 classid; /* v1 */
937 #endif
938 #ifdef CONFIG_CGROUP_NET_PRIO
939 u16 prioidx; /* v1 */
940 #endif
941 };
942
sock_cgroup_prioidx(const struct sock_cgroup_data * skcd)943 static inline u16 sock_cgroup_prioidx(const struct sock_cgroup_data *skcd)
944 {
945 #ifdef CONFIG_CGROUP_NET_PRIO
946 return READ_ONCE(skcd->prioidx);
947 #else
948 return 1;
949 #endif
950 }
951
952 #ifdef CONFIG_CGROUP_NET_CLASSID
sock_cgroup_classid(const struct sock_cgroup_data * skcd)953 static inline u32 sock_cgroup_classid(const struct sock_cgroup_data *skcd)
954 {
955 return READ_ONCE(skcd->classid);
956 }
957 #endif
958
sock_cgroup_set_prioidx(struct sock_cgroup_data * skcd,u16 prioidx)959 static inline void sock_cgroup_set_prioidx(struct sock_cgroup_data *skcd,
960 u16 prioidx)
961 {
962 #ifdef CONFIG_CGROUP_NET_PRIO
963 WRITE_ONCE(skcd->prioidx, prioidx);
964 #endif
965 }
966
967 #ifdef CONFIG_CGROUP_NET_CLASSID
sock_cgroup_set_classid(struct sock_cgroup_data * skcd,u32 classid)968 static inline void sock_cgroup_set_classid(struct sock_cgroup_data *skcd,
969 u32 classid)
970 {
971 WRITE_ONCE(skcd->classid, classid);
972 }
973 #endif
974
975 #else /* CONFIG_SOCK_CGROUP_DATA */
976
977 struct sock_cgroup_data {
978 };
979
980 #endif /* CONFIG_SOCK_CGROUP_DATA */
981
982 #endif /* _LINUX_CGROUP_DEFS_H */
983