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