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