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