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