1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
2b4a04ab7STejun Heo /*
3b4a04ab7STejun Heo * linux/cgroup-defs.h - basic definitions for cgroup
4b4a04ab7STejun Heo *
5b4a04ab7STejun Heo * This file provides basic type and interface. Include this file directly
6b4a04ab7STejun Heo * only if necessary to avoid cyclic dependencies.
7b4a04ab7STejun Heo */
8b4a04ab7STejun Heo #ifndef _LINUX_CGROUP_DEFS_H
9b4a04ab7STejun Heo #define _LINUX_CGROUP_DEFS_H
10b4a04ab7STejun Heo
11b4a04ab7STejun Heo #include <linux/limits.h>
12b4a04ab7STejun Heo #include <linux/list.h>
13b4a04ab7STejun Heo #include <linux/idr.h>
14b4a04ab7STejun Heo #include <linux/wait.h>
15b4a04ab7STejun Heo #include <linux/mutex.h>
16b4a04ab7STejun Heo #include <linux/rcupdate.h>
174b9502e6SElena Reshetova #include <linux/refcount.h>
18b4a04ab7STejun Heo #include <linux/percpu-refcount.h>
197d7efec3STejun Heo #include <linux/percpu-rwsem.h>
20041cd640STejun Heo #include <linux/u64_stats_sync.h>
21b4a04ab7STejun Heo #include <linux/workqueue.h>
22fd1740b6SJakub Kicinski #include <linux/bpf-cgroup-defs.h>
232ce7135aSJohannes Weiner #include <linux/psi_types.h>
24b4a04ab7STejun Heo
25b4a04ab7STejun Heo #ifdef CONFIG_CGROUPS
26b4a04ab7STejun Heo
27b4a04ab7STejun Heo struct cgroup;
28b4a04ab7STejun Heo struct cgroup_root;
29b4a04ab7STejun Heo struct cgroup_subsys;
30b4a04ab7STejun Heo struct cgroup_taskset;
31b4a04ab7STejun Heo struct kernfs_node;
32b4a04ab7STejun Heo struct kernfs_ops;
33b4a04ab7STejun Heo struct kernfs_open_file;
34c80ef9e0SArnd Bergmann struct seq_file;
35dc50537bSJohannes Weiner struct poll_table_struct;
36b4a04ab7STejun Heo
37b4a04ab7STejun Heo #define MAX_CGROUP_TYPE_NAMELEN 32
38b4a04ab7STejun Heo #define MAX_CGROUP_ROOT_NAMELEN 64
39b4a04ab7STejun Heo #define MAX_CFTYPE_NAME 64
40b4a04ab7STejun Heo
41b4a04ab7STejun Heo /* define the enumeration of all cgroup subsystems */
42b4a04ab7STejun Heo #define SUBSYS(_x) _x ## _cgrp_id,
43b4a04ab7STejun Heo enum cgroup_subsys_id {
44b4a04ab7STejun Heo #include <linux/cgroup_subsys.h>
45b4a04ab7STejun Heo CGROUP_SUBSYS_COUNT,
46b4a04ab7STejun Heo };
47b4a04ab7STejun Heo #undef SUBSYS
48b4a04ab7STejun Heo
49b4a04ab7STejun Heo /* bits in struct cgroup_subsys_state flags field */
50b4a04ab7STejun Heo enum {
51b4a04ab7STejun Heo CSS_NO_REF = (1 << 0), /* no reference counting for this css */
52b4a04ab7STejun Heo CSS_ONLINE = (1 << 1), /* between ->css_online() and ->css_offline() */
53b4a04ab7STejun Heo CSS_RELEASED = (1 << 2), /* refcnt reached zero, released */
5488cb04b9STejun Heo CSS_VISIBLE = (1 << 3), /* css is visible to userland */
5533c35aa4SWaiman Long CSS_DYING = (1 << 4), /* css is dying */
56b4a04ab7STejun Heo };
57b4a04ab7STejun Heo
58b4a04ab7STejun Heo /* bits in struct cgroup flags field */
59b4a04ab7STejun Heo enum {
60b4a04ab7STejun Heo /* Control Group requires release notifications to userspace */
61b4a04ab7STejun Heo CGRP_NOTIFY_ON_RELEASE,
62b4a04ab7STejun Heo /*
63b4a04ab7STejun Heo * Clone the parent's configuration when creating a new child
64b4a04ab7STejun Heo * cpuset cgroup. For historical reasons, this option can be
65b4a04ab7STejun Heo * specified at mount time and thus is implemented here.
66b4a04ab7STejun Heo */
67b4a04ab7STejun Heo CGRP_CPUSET_CLONE_CHILDREN,
6876f969e8SRoman Gushchin
6976f969e8SRoman Gushchin /* Control group has to be frozen. */
7076f969e8SRoman Gushchin CGRP_FREEZE,
7176f969e8SRoman Gushchin
7276f969e8SRoman Gushchin /* Cgroup is frozen. */
7376f969e8SRoman Gushchin CGRP_FROZEN,
74b4a04ab7STejun Heo };
75b4a04ab7STejun Heo
76b4a04ab7STejun Heo /* cgroup_root->flags */
77b4a04ab7STejun Heo enum {
78b4a04ab7STejun Heo CGRP_ROOT_NOPREFIX = (1 << 1), /* mounted subsystems have no named prefix */
79b4a04ab7STejun Heo CGRP_ROOT_XATTR = (1 << 2), /* supports extended attributes */
805136f636STejun Heo
815136f636STejun Heo /*
825136f636STejun Heo * Consider namespaces as delegation boundaries. If this flag is
835136f636STejun Heo * set, controller specific interface files in a namespace root
845136f636STejun Heo * aren't writeable from inside the namespace.
855136f636STejun Heo */
865136f636STejun Heo CGRP_ROOT_NS_DELEGATE = (1 << 3),
87e1cba4b8SWaiman Long
88e1cba4b8SWaiman Long /*
896a010a49STejun Heo * Reduce latencies on dynamic cgroup modifications such as task
906a010a49STejun Heo * migrations and controller on/offs by disabling percpu operation on
916a010a49STejun Heo * cgroup_threadgroup_rwsem. This makes hot path operations such as
926a010a49STejun Heo * forks and exits into the slow path and more expensive.
936a010a49STejun Heo *
946a010a49STejun Heo * The static usage pattern of creating a cgroup, enabling controllers,
956a010a49STejun Heo * and then seeding it with CLONE_INTO_CGROUP doesn't require write
966a010a49STejun Heo * locking cgroup_threadgroup_rwsem and thus doesn't benefit from
976a010a49STejun Heo * favordynmod.
986a010a49STejun Heo */
996a010a49STejun Heo CGRP_ROOT_FAVOR_DYNMODS = (1 << 4),
1006a010a49STejun Heo
1016a010a49STejun Heo /*
102e1cba4b8SWaiman Long * Enable cpuset controller in v1 cgroup to use v2 behavior.
103e1cba4b8SWaiman Long */
1046a010a49STejun Heo CGRP_ROOT_CPUSET_V2_MODE = (1 << 16),
1059852ae3fSChris Down
1069852ae3fSChris Down /*
1079852ae3fSChris Down * Enable legacy local memory.events.
1089852ae3fSChris Down */
1096a010a49STejun Heo CGRP_ROOT_MEMORY_LOCAL_EVENTS = (1 << 17),
1108a931f80SJohannes Weiner
1118a931f80SJohannes Weiner /*
1128a931f80SJohannes Weiner * Enable recursive subtree protection
1138a931f80SJohannes Weiner */
1146a010a49STejun Heo CGRP_ROOT_MEMORY_RECURSIVE_PROT = (1 << 18),
115b4a04ab7STejun Heo };
116b4a04ab7STejun Heo
117b4a04ab7STejun Heo /* cftype->flags */
118b4a04ab7STejun Heo enum {
119b4a04ab7STejun Heo CFTYPE_ONLY_ON_ROOT = (1 << 0), /* only create on root cgrp */
120b4a04ab7STejun Heo CFTYPE_NOT_ON_ROOT = (1 << 1), /* don't create on root cgrp */
1215136f636STejun Heo CFTYPE_NS_DELEGATABLE = (1 << 2), /* writeable beyond delegation boundaries */
1225136f636STejun Heo
123b4a04ab7STejun Heo CFTYPE_NO_PREFIX = (1 << 3), /* (DON'T USE FOR NEW FILES) no subsys prefix */
1247dbdb199STejun Heo CFTYPE_WORLD_WRITABLE = (1 << 4), /* (DON'T USE FOR NEW FILES) S_IWUGO */
1255cf8114dSWaiman Long CFTYPE_DEBUG = (1 << 5), /* create when cgroup_debug */
126b4a04ab7STejun Heo
127b4a04ab7STejun Heo /* internal flags, do not use outside cgroup core proper */
128b4a04ab7STejun Heo __CFTYPE_ONLY_ON_DFL = (1 << 16), /* only on default hierarchy */
129b4a04ab7STejun Heo __CFTYPE_NOT_ON_DFL = (1 << 17), /* not on default hierarchy */
1300083d27bSTejun Heo __CFTYPE_ADDED = (1 << 18),
131b4a04ab7STejun Heo };
132b4a04ab7STejun Heo
133b4a04ab7STejun Heo /*
1346f60eadeSTejun Heo * cgroup_file is the handle for a file instance created in a cgroup which
1356f60eadeSTejun Heo * is used, for example, to generate file changed notifications. This can
1366f60eadeSTejun Heo * be obtained by setting cftype->file_offset.
1376f60eadeSTejun Heo */
1386f60eadeSTejun Heo struct cgroup_file {
1396f60eadeSTejun Heo /* do not access any fields from outside cgroup core */
1406f60eadeSTejun Heo struct kernfs_node *kn;
141b12e3583STejun Heo unsigned long notified_at;
142b12e3583STejun Heo struct timer_list notify_timer;
1436f60eadeSTejun Heo };
1446f60eadeSTejun Heo
1456f60eadeSTejun Heo /*
146b4a04ab7STejun Heo * Per-subsystem/per-cgroup state maintained by the system. This is the
147b4a04ab7STejun Heo * fundamental structural building block that controllers deal with.
148b4a04ab7STejun Heo *
149b4a04ab7STejun Heo * Fields marked with "PI:" are public and immutable and may be accessed
150b4a04ab7STejun Heo * directly without synchronization.
151b4a04ab7STejun Heo */
152b4a04ab7STejun Heo struct cgroup_subsys_state {
153b4a04ab7STejun Heo /* PI: the cgroup that this css is attached to */
154b4a04ab7STejun Heo struct cgroup *cgroup;
155b4a04ab7STejun Heo
156b4a04ab7STejun Heo /* PI: the cgroup subsystem that this css is attached to */
157b4a04ab7STejun Heo struct cgroup_subsys *ss;
158b4a04ab7STejun Heo
159b4a04ab7STejun Heo /* reference count - access via css_[try]get() and css_put() */
160b4a04ab7STejun Heo struct percpu_ref refcnt;
161b4a04ab7STejun Heo
162b4a04ab7STejun Heo /* siblings list anchored at the parent's ->children */
163b4a04ab7STejun Heo struct list_head sibling;
164b4a04ab7STejun Heo struct list_head children;
165b4a04ab7STejun Heo
1668f53470bSTejun Heo /* flush target list anchored at cgrp->rstat_css_list */
1678f53470bSTejun Heo struct list_head rstat_css_node;
1688f53470bSTejun Heo
169b4a04ab7STejun Heo /*
170b4a04ab7STejun Heo * PI: Subsys-unique ID. 0 is unused and root is always 1. The
171b4a04ab7STejun Heo * matching css can be looked up using css_from_id().
172b4a04ab7STejun Heo */
173b4a04ab7STejun Heo int id;
174b4a04ab7STejun Heo
175b4a04ab7STejun Heo unsigned int flags;
176b4a04ab7STejun Heo
177b4a04ab7STejun Heo /*
178b4a04ab7STejun Heo * Monotonically increasing unique serial number which defines a
179b4a04ab7STejun Heo * uniform order among all csses. It's guaranteed that all
180b4a04ab7STejun Heo * ->children lists are in the ascending order of ->serial_nr and
181b4a04ab7STejun Heo * used to allow interrupting and resuming iterations.
182b4a04ab7STejun Heo */
183b4a04ab7STejun Heo u64 serial_nr;
184b4a04ab7STejun Heo
185aa226ff4STejun Heo /*
186aa226ff4STejun Heo * Incremented by online self and children. Used to guarantee that
187aa226ff4STejun Heo * parents are not offlined before their children.
188aa226ff4STejun Heo */
189aa226ff4STejun Heo atomic_t online_cnt;
190aa226ff4STejun Heo
191b4a04ab7STejun Heo /* percpu_ref killing and RCU release */
192b4a04ab7STejun Heo struct work_struct destroy_work;
1938f36aaecSTejun Heo struct rcu_work destroy_rwork;
194b8b1a2e5STodd Poynor
195b8b1a2e5STodd Poynor /*
196b8b1a2e5STodd Poynor * PI: the parent css. Placed here for cache proximity to following
197b8b1a2e5STodd Poynor * fields of the containing structure.
198b8b1a2e5STodd Poynor */
199b8b1a2e5STodd Poynor struct cgroup_subsys_state *parent;
200b4a04ab7STejun Heo };
201b4a04ab7STejun Heo
202b4a04ab7STejun Heo /*
203b4a04ab7STejun Heo * A css_set is a structure holding pointers to a set of
204b4a04ab7STejun Heo * cgroup_subsys_state objects. This saves space in the task struct
205b4a04ab7STejun Heo * object and speeds up fork()/exit(), since a single inc/dec and a
206b4a04ab7STejun Heo * list_add()/del() can bump the reference count on the entire cgroup
207b4a04ab7STejun Heo * set for a task.
208b4a04ab7STejun Heo */
209b4a04ab7STejun Heo struct css_set {
2105f617ebbSTejun Heo /*
2115f617ebbSTejun Heo * Set of subsystem states, one for each subsystem. This array is
2125f617ebbSTejun Heo * immutable after creation apart from the init_css_set during
2135f617ebbSTejun Heo * subsystem registration (at boot time).
2145f617ebbSTejun Heo */
2155f617ebbSTejun Heo struct cgroup_subsys_state *subsys[CGROUP_SUBSYS_COUNT];
2165f617ebbSTejun Heo
2175f617ebbSTejun Heo /* reference count */
2184b9502e6SElena Reshetova refcount_t refcount;
219b4a04ab7STejun Heo
220454000adSTejun Heo /*
221454000adSTejun Heo * For a domain cgroup, the following points to self. If threaded,
222454000adSTejun Heo * to the matching cset of the nearest domain ancestor. The
223454000adSTejun Heo * dom_cset provides access to the domain cgroup and its csses to
224454000adSTejun Heo * which domain level resource consumptions should be charged.
225454000adSTejun Heo */
226454000adSTejun Heo struct css_set *dom_cset;
227454000adSTejun Heo
2285f617ebbSTejun Heo /* the default cgroup associated with this css_set */
2295f617ebbSTejun Heo struct cgroup *dfl_cgrp;
230b4a04ab7STejun Heo
23173a7242aSWaiman Long /* internal task count, protected by css_set_lock */
23273a7242aSWaiman Long int nr_tasks;
23373a7242aSWaiman Long
234b4a04ab7STejun Heo /*
235b4a04ab7STejun Heo * Lists running through all tasks using this cgroup group.
236b4a04ab7STejun Heo * mg_tasks lists tasks which belong to this cset but are in the
237b4a04ab7STejun Heo * process of being migrated out or in. Protected by
23813cc9ee8SWaiman Long * css_set_lock, but, during migration, once tasks are moved to
239b4a04ab7STejun Heo * mg_tasks, it can be read safely while holding cgroup_mutex.
240b4a04ab7STejun Heo */
241b4a04ab7STejun Heo struct list_head tasks;
242b4a04ab7STejun Heo struct list_head mg_tasks;
243c03cd773STejun Heo struct list_head dying_tasks;
244b4a04ab7STejun Heo
2455f617ebbSTejun Heo /* all css_task_iters currently walking this cset */
2465f617ebbSTejun Heo struct list_head task_iters;
2475f617ebbSTejun Heo
2485f617ebbSTejun Heo /*
24908b2b6fdSZhen Lei * On the default hierarchy, ->subsys[ssid] may point to a css
2505f617ebbSTejun Heo * attached to an ancestor instead of the cgroup this css_set is
2515f617ebbSTejun Heo * associated with. The following node is anchored at
2525f617ebbSTejun Heo * ->subsys[ssid]->cgroup->e_csets[ssid] and provides a way to
2535f617ebbSTejun Heo * iterate through all css's attached to a given cgroup.
2545f617ebbSTejun Heo */
2555f617ebbSTejun Heo struct list_head e_cset_node[CGROUP_SUBSYS_COUNT];
2565f617ebbSTejun Heo
257454000adSTejun Heo /* all threaded csets whose ->dom_cset points to this cset */
258454000adSTejun Heo struct list_head threaded_csets;
259454000adSTejun Heo struct list_head threaded_csets_node;
260454000adSTejun Heo
2615f617ebbSTejun Heo /*
2625f617ebbSTejun Heo * List running through all cgroup groups in the same hash
2635f617ebbSTejun Heo * slot. Protected by css_set_lock
2645f617ebbSTejun Heo */
2655f617ebbSTejun Heo struct hlist_node hlist;
2665f617ebbSTejun Heo
267b4a04ab7STejun Heo /*
268b4a04ab7STejun Heo * List of cgrp_cset_links pointing at cgroups referenced from this
269b4a04ab7STejun Heo * css_set. Protected by css_set_lock.
270b4a04ab7STejun Heo */
271b4a04ab7STejun Heo struct list_head cgrp_links;
272b4a04ab7STejun Heo
273b4a04ab7STejun Heo /*
274b4a04ab7STejun Heo * List of csets participating in the on-going migration either as
275b4a04ab7STejun Heo * source or destination. Protected by cgroup_mutex.
276b4a04ab7STejun Heo */
27707fd5b6cSTejun Heo struct list_head mg_src_preload_node;
27807fd5b6cSTejun Heo struct list_head mg_dst_preload_node;
279b4a04ab7STejun Heo struct list_head mg_node;
280b4a04ab7STejun Heo
281b4a04ab7STejun Heo /*
282b4a04ab7STejun Heo * If this cset is acting as the source of migration the following
283e4857982STejun Heo * two fields are set. mg_src_cgrp and mg_dst_cgrp are
284e4857982STejun Heo * respectively the source and destination cgroups of the on-going
285e4857982STejun Heo * migration. mg_dst_cset is the destination cset the target tasks
286e4857982STejun Heo * on this cset should be migrated to. Protected by cgroup_mutex.
287b4a04ab7STejun Heo */
288b4a04ab7STejun Heo struct cgroup *mg_src_cgrp;
289e4857982STejun Heo struct cgroup *mg_dst_cgrp;
290b4a04ab7STejun Heo struct css_set *mg_dst_cset;
291b4a04ab7STejun Heo
2922b021cbfSTejun Heo /* dead and being drained, ignore for migration */
2932b021cbfSTejun Heo bool dead;
2942b021cbfSTejun Heo
295b4a04ab7STejun Heo /* For RCU-protected deletion */
296b4a04ab7STejun Heo struct rcu_head rcu_head;
297b4a04ab7STejun Heo };
298b4a04ab7STejun Heo
299d4ff749bSTejun Heo struct cgroup_base_stat {
300d4ff749bSTejun Heo struct task_cputime cputime;
3011fcf54deSJosh Don
3021fcf54deSJosh Don #ifdef CONFIG_SCHED_CORE
3031fcf54deSJosh Don u64 forceidle_sum;
3041fcf54deSJosh Don #endif
305d4ff749bSTejun Heo };
306d4ff749bSTejun Heo
307041cd640STejun Heo /*
308c58632b3STejun Heo * rstat - cgroup scalable recursive statistics. Accounting is done
309c58632b3STejun Heo * per-cpu in cgroup_rstat_cpu which is then lazily propagated up the
310c58632b3STejun Heo * hierarchy on reads.
311041cd640STejun Heo *
312c58632b3STejun Heo * When a stat gets updated, the cgroup_rstat_cpu and its ancestors are
313041cd640STejun Heo * linked into the updated tree. On the following read, propagation only
314041cd640STejun Heo * considers and consumes the updated tree. This makes reading O(the
315041cd640STejun Heo * number of descendants which have been active since last read) instead of
316041cd640STejun Heo * O(the total number of descendants).
317041cd640STejun Heo *
318041cd640STejun Heo * This is important because there can be a lot of (draining) cgroups which
319041cd640STejun Heo * aren't active and stat may be read frequently. The combination can
320041cd640STejun Heo * become very expensive. By propagating selectively, increasing reading
321041cd640STejun Heo * frequency decreases the cost of each read.
322d4ff749bSTejun Heo *
323d4ff749bSTejun Heo * This struct hosts both the fields which implement the above -
324d4ff749bSTejun Heo * updated_children and updated_next - and the fields which track basic
325d4ff749bSTejun Heo * resource statistics on top of it - bsync, bstat and last_bstat.
326041cd640STejun Heo */
327c58632b3STejun Heo struct cgroup_rstat_cpu {
328041cd640STejun Heo /*
329d4ff749bSTejun Heo * ->bsync protects ->bstat. These are the only fields which get
330d4ff749bSTejun Heo * updated in the hot path.
331041cd640STejun Heo */
332d4ff749bSTejun Heo struct u64_stats_sync bsync;
333d4ff749bSTejun Heo struct cgroup_base_stat bstat;
334041cd640STejun Heo
335041cd640STejun Heo /*
336041cd640STejun Heo * Snapshots at the last reading. These are used to calculate the
337041cd640STejun Heo * deltas to propagate to the global counters.
338041cd640STejun Heo */
339d4ff749bSTejun Heo struct cgroup_base_stat last_bstat;
340041cd640STejun Heo
341041cd640STejun Heo /*
3420437719cSHao Jia * This field is used to record the cumulative per-cpu time of
3430437719cSHao Jia * the cgroup and its descendants. Currently it can be read via
3440437719cSHao Jia * eBPF/drgn etc, and we are still trying to determine how to
3450437719cSHao Jia * expose it in the cgroupfs interface.
3460437719cSHao Jia */
3470437719cSHao Jia struct cgroup_base_stat subtree_bstat;
3480437719cSHao Jia
3490437719cSHao Jia /*
3500437719cSHao Jia * Snapshots at the last reading. These are used to calculate the
3510437719cSHao Jia * deltas to propagate to the per-cpu subtree_bstat.
3520437719cSHao Jia */
3530437719cSHao Jia struct cgroup_base_stat last_subtree_bstat;
3540437719cSHao Jia
3550437719cSHao Jia /*
356041cd640STejun Heo * Child cgroups with stat updates on this cpu since the last read
357041cd640STejun Heo * are linked on the parent's ->updated_children through
358041cd640STejun Heo * ->updated_next.
359041cd640STejun Heo *
360041cd640STejun Heo * In addition to being more compact, singly-linked list pointing
361041cd640STejun Heo * to the cgroup makes it unnecessary for each per-cpu struct to
362041cd640STejun Heo * point back to the associated cgroup.
363041cd640STejun Heo *
364c58632b3STejun Heo * Protected by per-cpu cgroup_rstat_cpu_lock.
365041cd640STejun Heo */
366041cd640STejun Heo struct cgroup *updated_children; /* terminated by self cgroup */
367041cd640STejun Heo struct cgroup *updated_next; /* NULL iff not on the list */
368041cd640STejun Heo };
369041cd640STejun Heo
37076f969e8SRoman Gushchin struct cgroup_freezer_state {
37176f969e8SRoman Gushchin /* Should the cgroup and its descendants be frozen. */
37276f969e8SRoman Gushchin bool freeze;
37376f969e8SRoman Gushchin
37476f969e8SRoman Gushchin /* Should the cgroup actually be frozen? */
37576f969e8SRoman Gushchin int e_freeze;
37676f969e8SRoman Gushchin
37776f969e8SRoman Gushchin /* Fields below are protected by css_set_lock */
37876f969e8SRoman Gushchin
37976f969e8SRoman Gushchin /* Number of frozen descendant cgroups */
38076f969e8SRoman Gushchin int nr_frozen_descendants;
38176f969e8SRoman Gushchin
38276f969e8SRoman Gushchin /*
38376f969e8SRoman Gushchin * Number of tasks, which are counted as frozen:
38476f969e8SRoman Gushchin * frozen, SIGSTOPped, and PTRACEd.
38576f969e8SRoman Gushchin */
38676f969e8SRoman Gushchin int nr_frozen_tasks;
38776f969e8SRoman Gushchin };
38876f969e8SRoman Gushchin
389b4a04ab7STejun Heo struct cgroup {
390b4a04ab7STejun Heo /* self css with NULL ->ss, points back to this cgroup */
391b4a04ab7STejun Heo struct cgroup_subsys_state self;
392b4a04ab7STejun Heo
393b4a04ab7STejun Heo unsigned long flags; /* "unsigned long" so bitops work */
394b4a04ab7STejun Heo
395b4a04ab7STejun Heo /*
396b11cfb58STejun Heo * The depth this cgroup is at. The root is at depth zero and each
397b11cfb58STejun Heo * step down the hierarchy increments the level. This along with
3987f203bc8STejun Heo * ancestors[] can determine whether a given cgroup is a
399b11cfb58STejun Heo * descendant of another without traversing the hierarchy.
400b11cfb58STejun Heo */
401b11cfb58STejun Heo int level;
402b11cfb58STejun Heo
4031a926e0bSRoman Gushchin /* Maximum allowed descent tree depth */
4041a926e0bSRoman Gushchin int max_depth;
4051a926e0bSRoman Gushchin
406b11cfb58STejun Heo /*
4070679dee0SRoman Gushchin * Keep track of total numbers of visible and dying descent cgroups.
4080679dee0SRoman Gushchin * Dying cgroups are cgroups which were deleted by a user,
4090679dee0SRoman Gushchin * but are still existing because someone else is holding a reference.
4101a926e0bSRoman Gushchin * max_descendants is a maximum allowed number of descent cgroups.
4114dcabeceSRoman Gushchin *
4124dcabeceSRoman Gushchin * nr_descendants and nr_dying_descendants are protected
4134dcabeceSRoman Gushchin * by cgroup_mutex and css_set_lock. It's fine to read them holding
4144dcabeceSRoman Gushchin * any of cgroup_mutex and css_set_lock; for writing both locks
4154dcabeceSRoman Gushchin * should be held.
4160679dee0SRoman Gushchin */
4170679dee0SRoman Gushchin int nr_descendants;
4180679dee0SRoman Gushchin int nr_dying_descendants;
4191a926e0bSRoman Gushchin int max_descendants;
4200679dee0SRoman Gushchin
4210679dee0SRoman Gushchin /*
4220de0942dSTejun Heo * Each non-empty css_set associated with this cgroup contributes
423788b950cSTejun Heo * one to nr_populated_csets. The counter is zero iff this cgroup
424788b950cSTejun Heo * doesn't have any tasks.
425788b950cSTejun Heo *
426788b950cSTejun Heo * All children which have non-zero nr_populated_csets and/or
427454000adSTejun Heo * nr_populated_children of their own contribute one to either
428454000adSTejun Heo * nr_populated_domain_children or nr_populated_threaded_children
429454000adSTejun Heo * depending on their type. Each counter is zero iff all cgroups
430454000adSTejun Heo * of the type in the subtree proper don't have any tasks.
431b4a04ab7STejun Heo */
432788b950cSTejun Heo int nr_populated_csets;
433454000adSTejun Heo int nr_populated_domain_children;
434454000adSTejun Heo int nr_populated_threaded_children;
435454000adSTejun Heo
436454000adSTejun Heo int nr_threaded_children; /* # of live threaded child cgroups */
437b4a04ab7STejun Heo
438*a00e6071SShakeel Butt /* sequence number for cgroup.kill, serialized by css_set_lock. */
439*a00e6071SShakeel Butt unsigned int kill_seq;
440*a00e6071SShakeel Butt
441b4a04ab7STejun Heo struct kernfs_node *kn; /* cgroup kernfs entry */
4426f60eadeSTejun Heo struct cgroup_file procs_file; /* handle for "cgroup.procs" */
4436f60eadeSTejun Heo struct cgroup_file events_file; /* handle for "cgroup.events" */
444b4a04ab7STejun Heo
44534f26a15SChengming Zhou /* handles for "{cpu,memory,io,irq}.pressure" */
44634f26a15SChengming Zhou struct cgroup_file psi_files[NR_PSI_RESOURCES];
44734f26a15SChengming Zhou
448b4a04ab7STejun Heo /*
449b4a04ab7STejun Heo * The bitmask of subsystems enabled on the child cgroups.
450b4a04ab7STejun Heo * ->subtree_control is the one configured through
451af3bf054SWei Yang * "cgroup.subtree_control" while ->subtree_ss_mask is the effective
4528699b776STejun Heo * one which may have more subsystems enabled. Controller knobs
4538699b776STejun Heo * are made available iff it's enabled in ->subtree_control.
454b4a04ab7STejun Heo */
4556e5c8307STejun Heo u16 subtree_control;
4566e5c8307STejun Heo u16 subtree_ss_mask;
45715a27c36STejun Heo u16 old_subtree_control;
45815a27c36STejun Heo u16 old_subtree_ss_mask;
459b4a04ab7STejun Heo
460b4a04ab7STejun Heo /* Private pointers for each registered subsystem */
461b4a04ab7STejun Heo struct cgroup_subsys_state __rcu *subsys[CGROUP_SUBSYS_COUNT];
462b4a04ab7STejun Heo
463b4a04ab7STejun Heo struct cgroup_root *root;
464b4a04ab7STejun Heo
465b4a04ab7STejun Heo /*
466b4a04ab7STejun Heo * List of cgrp_cset_links pointing at css_sets with tasks in this
467b4a04ab7STejun Heo * cgroup. Protected by css_set_lock.
468b4a04ab7STejun Heo */
469b4a04ab7STejun Heo struct list_head cset_links;
470b4a04ab7STejun Heo
471b4a04ab7STejun Heo /*
472b4a04ab7STejun Heo * On the default hierarchy, a css_set for a cgroup with some
473b4a04ab7STejun Heo * susbsys disabled will point to css's which are associated with
474b4a04ab7STejun Heo * the closest ancestor which has the subsys enabled. The
475b4a04ab7STejun Heo * following lists all css_sets which point to this cgroup's css
476b4a04ab7STejun Heo * for the given subsystem.
477b4a04ab7STejun Heo */
478b4a04ab7STejun Heo struct list_head e_csets[CGROUP_SUBSYS_COUNT];
479b4a04ab7STejun Heo
480b4a04ab7STejun Heo /*
481454000adSTejun Heo * If !threaded, self. If threaded, it points to the nearest
482454000adSTejun Heo * domain ancestor. Inside a threaded subtree, cgroups are exempt
483454000adSTejun Heo * from process granularity and no-internal-task constraint.
484454000adSTejun Heo * Domain level resource consumptions which aren't tied to a
485454000adSTejun Heo * specific task are charged to the dom_cgrp.
486454000adSTejun Heo */
487454000adSTejun Heo struct cgroup *dom_cgrp;
488479adb89STejun Heo struct cgroup *old_dom_cgrp; /* used while enabling threaded */
489454000adSTejun Heo
490c58632b3STejun Heo /* per-cpu recursive resource statistics */
491c58632b3STejun Heo struct cgroup_rstat_cpu __percpu *rstat_cpu;
4928f53470bSTejun Heo struct list_head rstat_css_list;
493c58632b3STejun Heo
494041cd640STejun Heo /* cgroup basic resource statistics */
4951bb5ec2eSTejun Heo struct cgroup_base_stat last_bstat;
496d4ff749bSTejun Heo struct cgroup_base_stat bstat;
497d4ff749bSTejun Heo struct prev_cputime prev_cputime; /* for printing out cputime */
498041cd640STejun Heo
499454000adSTejun Heo /*
500b4a04ab7STejun Heo * list of pidlists, up to two for each namespace (one for procs, one
501b4a04ab7STejun Heo * for tasks); created on demand.
502b4a04ab7STejun Heo */
503b4a04ab7STejun Heo struct list_head pidlists;
504b4a04ab7STejun Heo struct mutex pidlist_mutex;
505b4a04ab7STejun Heo
506b4a04ab7STejun Heo /* used to wait for offlining of csses */
507b4a04ab7STejun Heo wait_queue_head_t offline_waitq;
508b4a04ab7STejun Heo
509b4a04ab7STejun Heo /* used to schedule release agent */
510b4a04ab7STejun Heo struct work_struct release_agent_work;
511b11cfb58STejun Heo
5122ce7135aSJohannes Weiner /* used to track pressure stalls */
5135f69a657SChen Wandun struct psi_group *psi;
5142ce7135aSJohannes Weiner
51530070984SDaniel Mack /* used to store eBPF programs */
51630070984SDaniel Mack struct cgroup_bpf bpf;
51730070984SDaniel Mack
518d09d8df3SJosef Bacik /* If there is block congestion on this cgroup. */
519d09d8df3SJosef Bacik atomic_t congestion_count;
520d09d8df3SJosef Bacik
52176f969e8SRoman Gushchin /* Used to store internal freezer state */
52276f969e8SRoman Gushchin struct cgroup_freezer_state freezer;
52376f969e8SRoman Gushchin
524c4bcfb38SYonghong Song #ifdef CONFIG_BPF_SYSCALL
525c4bcfb38SYonghong Song struct bpf_local_storage __rcu *bpf_cgrp_storage;
526c4bcfb38SYonghong Song #endif
527c4bcfb38SYonghong Song
5287f203bc8STejun Heo /* All ancestors including self */
5297f203bc8STejun Heo struct cgroup *ancestors[];
530b4a04ab7STejun Heo };
531b4a04ab7STejun Heo
532b4a04ab7STejun Heo /*
533b4a04ab7STejun Heo * A cgroup_root represents the root of a cgroup hierarchy, and may be
534b4a04ab7STejun Heo * associated with a kernfs_root to form an active hierarchy. This is
535b4a04ab7STejun Heo * internal to cgroup core. Don't access directly from controllers.
536b4a04ab7STejun Heo */
537b4a04ab7STejun Heo struct cgroup_root {
538b4a04ab7STejun Heo struct kernfs_root *kf_root;
539b4a04ab7STejun Heo
540b4a04ab7STejun Heo /* The bitmask of subsystems attached to this hierarchy */
541b4a04ab7STejun Heo unsigned int subsys_mask;
542b4a04ab7STejun Heo
543b4a04ab7STejun Heo /* Unique id for this hierarchy. */
544b4a04ab7STejun Heo int hierarchy_id;
545b4a04ab7STejun Heo
546f3c60ab6SWaiman Long /* A list running through the active hierarchies */
547f3c60ab6SWaiman Long struct list_head root_list;
548f3c60ab6SWaiman Long struct rcu_head rcu; /* Must be near the top */
549f3c60ab6SWaiman Long
5507f203bc8STejun Heo /*
5517f203bc8STejun Heo * The root cgroup. The containing cgroup_root will be destroyed on its
5527f203bc8STejun Heo * release. cgrp->ancestors[0] will be used overflowing into the
5537f203bc8STejun Heo * following field. cgrp_ancestor_storage must immediately follow.
5547f203bc8STejun Heo */
555b4a04ab7STejun Heo struct cgroup cgrp;
556b4a04ab7STejun Heo
5577f203bc8STejun Heo /* must follow cgrp for cgrp->ancestors[0], see above */
5587f203bc8STejun Heo struct cgroup *cgrp_ancestor_storage;
559b11cfb58STejun Heo
560b4a04ab7STejun Heo /* Number of cgroups in the hierarchy, used only for /proc/cgroups */
561b4a04ab7STejun Heo atomic_t nr_cgrps;
562b4a04ab7STejun Heo
563b4a04ab7STejun Heo /* Hierarchy-specific flags */
564b4a04ab7STejun Heo unsigned int flags;
565b4a04ab7STejun Heo
566b4a04ab7STejun Heo /* The path to use for release notifications. */
567b4a04ab7STejun Heo char release_agent_path[PATH_MAX];
568b4a04ab7STejun Heo
569b4a04ab7STejun Heo /* The name for this hierarchy - may be empty */
570b4a04ab7STejun Heo char name[MAX_CGROUP_ROOT_NAMELEN];
571b4a04ab7STejun Heo };
572b4a04ab7STejun Heo
573b4a04ab7STejun Heo /*
574b4a04ab7STejun Heo * struct cftype: handler definitions for cgroup control files
575b4a04ab7STejun Heo *
576b4a04ab7STejun Heo * When reading/writing to a file:
577b4a04ab7STejun Heo * - the cgroup to use is file->f_path.dentry->d_parent->d_fsdata
578b4a04ab7STejun Heo * - the 'cftype' of the file is file->f_path.dentry->d_fsdata
579b4a04ab7STejun Heo */
580b4a04ab7STejun Heo struct cftype {
581b4a04ab7STejun Heo /*
582b4a04ab7STejun Heo * By convention, the name should begin with the name of the
583b4a04ab7STejun Heo * subsystem, followed by a period. Zero length string indicates
584b4a04ab7STejun Heo * end of cftype array.
585b4a04ab7STejun Heo */
586b4a04ab7STejun Heo char name[MAX_CFTYPE_NAME];
587731a981eSTejun Heo unsigned long private;
588b4a04ab7STejun Heo
589b4a04ab7STejun Heo /*
590b4a04ab7STejun Heo * The maximum length of string, excluding trailing nul, that can
591b4a04ab7STejun Heo * be passed to write. If < PAGE_SIZE-1, PAGE_SIZE-1 is assumed.
592b4a04ab7STejun Heo */
593b4a04ab7STejun Heo size_t max_write_len;
594b4a04ab7STejun Heo
595b4a04ab7STejun Heo /* CFTYPE_* flags */
596b4a04ab7STejun Heo unsigned int flags;
597b4a04ab7STejun Heo
598b4a04ab7STejun Heo /*
5996f60eadeSTejun Heo * If non-zero, should contain the offset from the start of css to
6006f60eadeSTejun Heo * a struct cgroup_file field. cgroup will record the handle of
6016f60eadeSTejun Heo * the created file into it. The recorded handle can be used as
6026f60eadeSTejun Heo * long as the containing css remains accessible.
6036f60eadeSTejun Heo */
6046f60eadeSTejun Heo unsigned int file_offset;
6056f60eadeSTejun Heo
6066f60eadeSTejun Heo /*
607b4a04ab7STejun Heo * Fields used for internal bookkeeping. Initialized automatically
608b4a04ab7STejun Heo * during registration.
609b4a04ab7STejun Heo */
610b4a04ab7STejun Heo struct cgroup_subsys *ss; /* NULL for cgroup core files */
611b4a04ab7STejun Heo struct list_head node; /* anchored at ss->cfts */
612b4a04ab7STejun Heo struct kernfs_ops *kf_ops;
613b4a04ab7STejun Heo
614e90cbebcSTejun Heo int (*open)(struct kernfs_open_file *of);
615e90cbebcSTejun Heo void (*release)(struct kernfs_open_file *of);
616e90cbebcSTejun Heo
617b4a04ab7STejun Heo /*
618b4a04ab7STejun Heo * read_u64() is a shortcut for the common case of returning a
619b4a04ab7STejun Heo * single integer. Use it in place of read()
620b4a04ab7STejun Heo */
621b4a04ab7STejun Heo u64 (*read_u64)(struct cgroup_subsys_state *css, struct cftype *cft);
622b4a04ab7STejun Heo /*
623b4a04ab7STejun Heo * read_s64() is a signed version of read_u64()
624b4a04ab7STejun Heo */
625b4a04ab7STejun Heo s64 (*read_s64)(struct cgroup_subsys_state *css, struct cftype *cft);
626b4a04ab7STejun Heo
627b4a04ab7STejun Heo /* generic seq_file read interface */
628b4a04ab7STejun Heo int (*seq_show)(struct seq_file *sf, void *v);
629b4a04ab7STejun Heo
630b4a04ab7STejun Heo /* optional ops, implement all or none */
631b4a04ab7STejun Heo void *(*seq_start)(struct seq_file *sf, loff_t *ppos);
632b4a04ab7STejun Heo void *(*seq_next)(struct seq_file *sf, void *v, loff_t *ppos);
633b4a04ab7STejun Heo void (*seq_stop)(struct seq_file *sf, void *v);
634b4a04ab7STejun Heo
635b4a04ab7STejun Heo /*
636b4a04ab7STejun Heo * write_u64() is a shortcut for the common case of accepting
637b4a04ab7STejun Heo * a single integer (as parsed by simple_strtoull) from
638b4a04ab7STejun Heo * userspace. Use in place of write(); return 0 or error.
639b4a04ab7STejun Heo */
640b4a04ab7STejun Heo int (*write_u64)(struct cgroup_subsys_state *css, struct cftype *cft,
641b4a04ab7STejun Heo u64 val);
642b4a04ab7STejun Heo /*
643b4a04ab7STejun Heo * write_s64() is a signed version of write_u64()
644b4a04ab7STejun Heo */
645b4a04ab7STejun Heo int (*write_s64)(struct cgroup_subsys_state *css, struct cftype *cft,
646b4a04ab7STejun Heo s64 val);
647b4a04ab7STejun Heo
648b4a04ab7STejun Heo /*
649b4a04ab7STejun Heo * write() is the generic write callback which maps directly to
650b4a04ab7STejun Heo * kernfs write operation and overrides all other operations.
651b4a04ab7STejun Heo * Maximum write size is determined by ->max_write_len. Use
652b4a04ab7STejun Heo * of_css/cft() to access the associated css and cft.
653b4a04ab7STejun Heo */
654b4a04ab7STejun Heo ssize_t (*write)(struct kernfs_open_file *of,
655b4a04ab7STejun Heo char *buf, size_t nbytes, loff_t off);
656b4a04ab7STejun Heo
657dc50537bSJohannes Weiner __poll_t (*poll)(struct kernfs_open_file *of,
658dc50537bSJohannes Weiner struct poll_table_struct *pt);
659dc50537bSJohannes Weiner
660b4a04ab7STejun Heo #ifdef CONFIG_DEBUG_LOCK_ALLOC
661b4a04ab7STejun Heo struct lock_class_key lockdep_key;
662b4a04ab7STejun Heo #endif
663b4a04ab7STejun Heo };
664b4a04ab7STejun Heo
665b4a04ab7STejun Heo /*
666b4a04ab7STejun Heo * Control Group subsystem type.
667da82c92fSMauro Carvalho Chehab * See Documentation/admin-guide/cgroup-v1/cgroups.rst for details
668b4a04ab7STejun Heo */
669b4a04ab7STejun Heo struct cgroup_subsys {
670b4a04ab7STejun Heo struct cgroup_subsys_state *(*css_alloc)(struct cgroup_subsys_state *parent_css);
671b4a04ab7STejun Heo int (*css_online)(struct cgroup_subsys_state *css);
672b4a04ab7STejun Heo void (*css_offline)(struct cgroup_subsys_state *css);
673b4a04ab7STejun Heo void (*css_released)(struct cgroup_subsys_state *css);
674b4a04ab7STejun Heo void (*css_free)(struct cgroup_subsys_state *css);
675b4a04ab7STejun Heo void (*css_reset)(struct cgroup_subsys_state *css);
6768f53470bSTejun Heo void (*css_rstat_flush)(struct cgroup_subsys_state *css, int cpu);
677d41bf8c9STejun Heo int (*css_extra_stat_show)(struct seq_file *seq,
678d41bf8c9STejun Heo struct cgroup_subsys_state *css);
679677ea015SJosh Don int (*css_local_stat_show)(struct seq_file *seq,
680677ea015SJosh Don struct cgroup_subsys_state *css);
681b4a04ab7STejun Heo
6821f7dd3e5STejun Heo int (*can_attach)(struct cgroup_taskset *tset);
6831f7dd3e5STejun Heo void (*cancel_attach)(struct cgroup_taskset *tset);
6841f7dd3e5STejun Heo void (*attach)(struct cgroup_taskset *tset);
6855cf1cacbSTejun Heo void (*post_attach)(void);
686ef2c41cfSChristian Brauner int (*can_fork)(struct task_struct *task,
687ef2c41cfSChristian Brauner struct css_set *cset);
688ef2c41cfSChristian Brauner void (*cancel_fork)(struct task_struct *task, struct css_set *cset);
689b53202e6SOleg Nesterov void (*fork)(struct task_struct *task);
6902e91fa7fSTejun Heo void (*exit)(struct task_struct *task);
69151bee5abSOleg Nesterov void (*release)(struct task_struct *task);
692b4a04ab7STejun Heo void (*bind)(struct cgroup_subsys_state *root_css);
693b4a04ab7STejun Heo
694b38e42e9STejun Heo bool early_init:1;
695b4a04ab7STejun Heo
696b4a04ab7STejun Heo /*
697f6d635adSTejun Heo * If %true, the controller, on the default hierarchy, doesn't show
698f6d635adSTejun Heo * up in "cgroup.controllers" or "cgroup.subtree_control", is
699f6d635adSTejun Heo * implicitly enabled on all cgroups on the default hierarchy, and
700f6d635adSTejun Heo * bypasses the "no internal process" constraint. This is for
701f6d635adSTejun Heo * utility type controllers which is transparent to userland.
702f6d635adSTejun Heo *
703f6d635adSTejun Heo * An implicit controller can be stolen from the default hierarchy
704f6d635adSTejun Heo * anytime and thus must be okay with offline csses from previous
705f6d635adSTejun Heo * hierarchies coexisting with csses for the current one.
706f6d635adSTejun Heo */
707f6d635adSTejun Heo bool implicit_on_dfl:1;
708f6d635adSTejun Heo
709f6d635adSTejun Heo /*
7108cfd8147STejun Heo * If %true, the controller, supports threaded mode on the default
7118cfd8147STejun Heo * hierarchy. In a threaded subtree, both process granularity and
7128cfd8147STejun Heo * no-internal-process constraint are ignored and a threaded
7138cfd8147STejun Heo * controllers should be able to handle that.
7148cfd8147STejun Heo *
7158cfd8147STejun Heo * Note that as an implicit controller is automatically enabled on
7168cfd8147STejun Heo * all cgroups on the default hierarchy, it should also be
7178cfd8147STejun Heo * threaded. implicit && !threaded is not supported.
7188cfd8147STejun Heo */
7198cfd8147STejun Heo bool threaded:1;
7208cfd8147STejun Heo
72108b2b6fdSZhen Lei /* the following two fields are initialized automatically during boot */
722b4a04ab7STejun Heo int id;
723b4a04ab7STejun Heo const char *name;
724b4a04ab7STejun Heo
7253e1d2eedSTejun Heo /* optional, initialized automatically during boot if not set */
7263e1d2eedSTejun Heo const char *legacy_name;
7273e1d2eedSTejun Heo
728b4a04ab7STejun Heo /* link to parent, protected by cgroup_lock() */
729b4a04ab7STejun Heo struct cgroup_root *root;
730b4a04ab7STejun Heo
731b4a04ab7STejun Heo /* idr for css->id */
732b4a04ab7STejun Heo struct idr css_idr;
733b4a04ab7STejun Heo
734b4a04ab7STejun Heo /*
735b4a04ab7STejun Heo * List of cftypes. Each entry is the first entry of an array
736b4a04ab7STejun Heo * terminated by zero length name.
737b4a04ab7STejun Heo */
738b4a04ab7STejun Heo struct list_head cfts;
739b4a04ab7STejun Heo
740b4a04ab7STejun Heo /*
741b4a04ab7STejun Heo * Base cftypes which are automatically registered. The two can
742b4a04ab7STejun Heo * point to the same array.
743b4a04ab7STejun Heo */
744b4a04ab7STejun Heo struct cftype *dfl_cftypes; /* for the default hierarchy */
745b4a04ab7STejun Heo struct cftype *legacy_cftypes; /* for the legacy hierarchies */
746b4a04ab7STejun Heo
747b4a04ab7STejun Heo /*
748b4a04ab7STejun Heo * A subsystem may depend on other subsystems. When such subsystem
749b4a04ab7STejun Heo * is enabled on a cgroup, the depended-upon subsystems are enabled
750b4a04ab7STejun Heo * together if available. Subsystems enabled due to dependency are
751b4a04ab7STejun Heo * not visible to userland until explicitly enabled. The following
752b4a04ab7STejun Heo * specifies the mask of subsystems that this one depends on.
753b4a04ab7STejun Heo */
754b4a04ab7STejun Heo unsigned int depends_on;
755b4a04ab7STejun Heo };
756b4a04ab7STejun Heo
7571ed13287STejun Heo extern struct percpu_rw_semaphore cgroup_threadgroup_rwsem;
7581ed13287STejun Heo
7591ed13287STejun Heo /**
7601ed13287STejun Heo * cgroup_threadgroup_change_begin - threadgroup exclusion for cgroups
7611ed13287STejun Heo * @tsk: target task
7621ed13287STejun Heo *
763780de9ddSIngo Molnar * Allows cgroup operations to synchronize against threadgroup changes
764780de9ddSIngo Molnar * using a percpu_rw_semaphore.
7651ed13287STejun Heo */
cgroup_threadgroup_change_begin(struct task_struct * tsk)7661ed13287STejun Heo static inline void cgroup_threadgroup_change_begin(struct task_struct *tsk)
7671ed13287STejun Heo {
7681ed13287STejun Heo percpu_down_read(&cgroup_threadgroup_rwsem);
7691ed13287STejun Heo }
7701ed13287STejun Heo
7711ed13287STejun Heo /**
7721ed13287STejun Heo * cgroup_threadgroup_change_end - threadgroup exclusion for cgroups
7731ed13287STejun Heo * @tsk: target task
7741ed13287STejun Heo *
775780de9ddSIngo Molnar * Counterpart of cgroup_threadcgroup_change_begin().
7761ed13287STejun Heo */
cgroup_threadgroup_change_end(struct task_struct * tsk)7771ed13287STejun Heo static inline void cgroup_threadgroup_change_end(struct task_struct *tsk)
7781ed13287STejun Heo {
7791ed13287STejun Heo percpu_up_read(&cgroup_threadgroup_rwsem);
7801ed13287STejun Heo }
7817d7efec3STejun Heo
7827d7efec3STejun Heo #else /* CONFIG_CGROUPS */
7837d7efec3STejun Heo
784cb4a3167SAleksa Sarai #define CGROUP_SUBSYS_COUNT 0
785cb4a3167SAleksa Sarai
cgroup_threadgroup_change_begin(struct task_struct * tsk)786780de9ddSIngo Molnar static inline void cgroup_threadgroup_change_begin(struct task_struct *tsk)
787780de9ddSIngo Molnar {
788780de9ddSIngo Molnar might_sleep();
789780de9ddSIngo Molnar }
790780de9ddSIngo Molnar
cgroup_threadgroup_change_end(struct task_struct * tsk)7917d7efec3STejun Heo static inline void cgroup_threadgroup_change_end(struct task_struct *tsk) {}
7927d7efec3STejun Heo
793b4a04ab7STejun Heo #endif /* CONFIG_CGROUPS */
7947d7efec3STejun Heo
7952a56a1feSTejun Heo #ifdef CONFIG_SOCK_CGROUP_DATA
7962a56a1feSTejun Heo
797bd1060a1STejun Heo /*
798bd1060a1STejun Heo * sock_cgroup_data is embedded at sock->sk_cgrp_data and contains
799bd1060a1STejun Heo * per-socket cgroup information except for memcg association.
800bd1060a1STejun Heo *
8018520e224SDaniel Borkmann * On legacy hierarchies, net_prio and net_cls controllers directly
8028520e224SDaniel Borkmann * set attributes on each sock which can then be tested by the network
8038520e224SDaniel Borkmann * layer. On the default hierarchy, each sock is associated with the
8048520e224SDaniel Borkmann * cgroup it was created in and the networking layer can match the
8058520e224SDaniel Borkmann * cgroup directly.
806bd1060a1STejun Heo */
8072a56a1feSTejun Heo struct sock_cgroup_data {
8088520e224SDaniel Borkmann struct cgroup *cgroup; /* v2 */
8098520e224SDaniel Borkmann #ifdef CONFIG_CGROUP_NET_CLASSID
8108520e224SDaniel Borkmann u32 classid; /* v1 */
811bd1060a1STejun Heo #endif
8128520e224SDaniel Borkmann #ifdef CONFIG_CGROUP_NET_PRIO
8138520e224SDaniel Borkmann u16 prioidx; /* v1 */
8148520e224SDaniel Borkmann #endif
8152a56a1feSTejun Heo };
8162a56a1feSTejun Heo
sock_cgroup_prioidx(const struct sock_cgroup_data * skcd)8174dcb31d4SEric Dumazet static inline u16 sock_cgroup_prioidx(const struct sock_cgroup_data *skcd)
8182a56a1feSTejun Heo {
8198520e224SDaniel Borkmann #ifdef CONFIG_CGROUP_NET_PRIO
8208520e224SDaniel Borkmann return READ_ONCE(skcd->prioidx);
8218520e224SDaniel Borkmann #else
8228520e224SDaniel Borkmann return 1;
8238520e224SDaniel Borkmann #endif
8242a56a1feSTejun Heo }
8252a56a1feSTejun Heo
sock_cgroup_classid(const struct sock_cgroup_data * skcd)8264dcb31d4SEric Dumazet static inline u32 sock_cgroup_classid(const struct sock_cgroup_data *skcd)
8272a56a1feSTejun Heo {
8288520e224SDaniel Borkmann #ifdef CONFIG_CGROUP_NET_CLASSID
8298520e224SDaniel Borkmann return READ_ONCE(skcd->classid);
8308520e224SDaniel Borkmann #else
8318520e224SDaniel Borkmann return 0;
8328520e224SDaniel Borkmann #endif
8332a56a1feSTejun Heo }
8342a56a1feSTejun Heo
sock_cgroup_set_prioidx(struct sock_cgroup_data * skcd,u16 prioidx)8352a56a1feSTejun Heo static inline void sock_cgroup_set_prioidx(struct sock_cgroup_data *skcd,
8362a56a1feSTejun Heo u16 prioidx)
8372a56a1feSTejun Heo {
8388520e224SDaniel Borkmann #ifdef CONFIG_CGROUP_NET_PRIO
8398520e224SDaniel Borkmann WRITE_ONCE(skcd->prioidx, prioidx);
8408520e224SDaniel Borkmann #endif
8412a56a1feSTejun Heo }
8422a56a1feSTejun Heo
sock_cgroup_set_classid(struct sock_cgroup_data * skcd,u32 classid)8432a56a1feSTejun Heo static inline void sock_cgroup_set_classid(struct sock_cgroup_data *skcd,
8442a56a1feSTejun Heo u32 classid)
8452a56a1feSTejun Heo {
8468520e224SDaniel Borkmann #ifdef CONFIG_CGROUP_NET_CLASSID
8478520e224SDaniel Borkmann WRITE_ONCE(skcd->classid, classid);
8488520e224SDaniel Borkmann #endif
8492a56a1feSTejun Heo }
8502a56a1feSTejun Heo
8512a56a1feSTejun Heo #else /* CONFIG_SOCK_CGROUP_DATA */
8522a56a1feSTejun Heo
8532a56a1feSTejun Heo struct sock_cgroup_data {
8542a56a1feSTejun Heo };
8552a56a1feSTejun Heo
8562a56a1feSTejun Heo #endif /* CONFIG_SOCK_CGROUP_DATA */
8572a56a1feSTejun Heo
858b4a04ab7STejun Heo #endif /* _LINUX_CGROUP_DEFS_H */
859