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