xref: /linux-6.15/include/linux/cgroup.h (revision a22b3d54)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
2ddbcc7e8SPaul Menage #ifndef _LINUX_CGROUP_H
3ddbcc7e8SPaul Menage #define _LINUX_CGROUP_H
4ddbcc7e8SPaul Menage /*
5ddbcc7e8SPaul Menage  *  cgroup interface
6ddbcc7e8SPaul Menage  *
7ddbcc7e8SPaul Menage  *  Copyright (C) 2003 BULL SA
8ddbcc7e8SPaul Menage  *  Copyright (C) 2004-2006 Silicon Graphics, Inc.
9ddbcc7e8SPaul Menage  *
10ddbcc7e8SPaul Menage  */
11ddbcc7e8SPaul Menage 
12ddbcc7e8SPaul Menage #include <linux/sched.h>
13ddbcc7e8SPaul Menage #include <linux/nodemask.h>
14c6f53ed8SDavid Finkel #include <linux/list.h>
15eb6fd504STejun Heo #include <linux/rculist.h>
16846c7bb0SBalbir Singh #include <linux/cgroupstats.h>
1725a7e684STejun Heo #include <linux/fs.h>
187da11279STejun Heo #include <linux/seq_file.h>
192bd59d48STejun Heo #include <linux/kernfs.h>
2049d1dc4bSTejun Heo #include <linux/jump_label.h>
21a79a908fSAditya Kali #include <linux/types.h>
22a79a908fSAditya Kali #include <linux/ns_common.h>
23a79a908fSAditya Kali #include <linux/nsproxy.h>
24a79a908fSAditya Kali #include <linux/user_namespace.h>
25387ad967SElena Reshetova #include <linux/refcount.h>
26d2cc5ed6STejun Heo #include <linux/kernel_stat.h>
27b4a04ab7STejun Heo 
28b4a04ab7STejun Heo #include <linux/cgroup-defs.h>
29ddbcc7e8SPaul Menage 
30ef2c41cfSChristian Brauner struct kernel_clone_args;
31ef2c41cfSChristian Brauner 
326abc8ca1STejun Heo /*
3308b2b6fdSZhen Lei  * All weight knobs on the default hierarchy should use the following min,
346abc8ca1STejun Heo  * default and max values.  The default value is the logarithmic center of
356abc8ca1STejun Heo  * MIN and MAX and allows 100x to be expressed in both directions.
366abc8ca1STejun Heo  */
376abc8ca1STejun Heo #define CGROUP_WEIGHT_MIN		1
386abc8ca1STejun Heo #define CGROUP_WEIGHT_DFL		100
396abc8ca1STejun Heo #define CGROUP_WEIGHT_MAX		10000
406abc8ca1STejun Heo 
414f9c7ca8STejun Heo #ifdef CONFIG_CGROUPS
424f9c7ca8STejun Heo 
436da88306SChuyi Zhou enum {
446da88306SChuyi Zhou 	CSS_TASK_ITER_PROCS    = (1U << 0),  /* walk only threadgroup leaders */
456da88306SChuyi Zhou 	CSS_TASK_ITER_THREADED = (1U << 1),  /* walk all threaded css_sets in the domain */
466da88306SChuyi Zhou 	CSS_TASK_ITER_SKIPPED  = (1U << 16), /* internal flags */
476da88306SChuyi Zhou };
48b636fd38STejun Heo 
49c326aa2bSTejun Heo /* a css_task_iter should be treated as an opaque object */
50c326aa2bSTejun Heo struct css_task_iter {
5172c97e54STejun Heo 	struct cgroup_subsys		*ss;
52bc2fb7edSTejun Heo 	unsigned int			flags;
5372c97e54STejun Heo 
54c326aa2bSTejun Heo 	struct list_head		*cset_pos;
55c326aa2bSTejun Heo 	struct list_head		*cset_head;
56ddbcc7e8SPaul Menage 
57450ee0c1STejun Heo 	struct list_head		*tcset_pos;
58450ee0c1STejun Heo 	struct list_head		*tcset_head;
59450ee0c1STejun Heo 
60c326aa2bSTejun Heo 	struct list_head		*task_pos;
61ed27b9f7STejun Heo 
629c974c77SMichal Koutný 	struct list_head		*cur_tasks_head;
63ed27b9f7STejun Heo 	struct css_set			*cur_cset;
64450ee0c1STejun Heo 	struct css_set			*cur_dcset;
65ed27b9f7STejun Heo 	struct task_struct		*cur_task;
66ed27b9f7STejun Heo 	struct list_head		iters_node;	/* css_set->task_iters */
67ddbcc7e8SPaul Menage };
68ddbcc7e8SPaul Menage 
69fbf83212STejun Heo extern struct file_system_type cgroup_fs_type;
70c326aa2bSTejun Heo extern struct cgroup_root cgrp_dfl_root;
71c326aa2bSTejun Heo extern struct css_set init_css_set;
72aecd408bSYafang Shao extern spinlock_t css_set_lock;
73c326aa2bSTejun Heo 
74c326aa2bSTejun Heo #define SUBSYS(_x) extern struct cgroup_subsys _x ## _cgrp_subsys;
75c326aa2bSTejun Heo #include <linux/cgroup_subsys.h>
76c326aa2bSTejun Heo #undef SUBSYS
77c326aa2bSTejun Heo 
7849d1dc4bSTejun Heo #define SUBSYS(_x)								\
7949d1dc4bSTejun Heo 	extern struct static_key_true _x ## _cgrp_subsys_enabled_key;		\
8049d1dc4bSTejun Heo 	extern struct static_key_true _x ## _cgrp_subsys_on_dfl_key;
8149d1dc4bSTejun Heo #include <linux/cgroup_subsys.h>
8249d1dc4bSTejun Heo #undef SUBSYS
8349d1dc4bSTejun Heo 
8449d1dc4bSTejun Heo /**
8549d1dc4bSTejun Heo  * cgroup_subsys_enabled - fast test on whether a subsys is enabled
8649d1dc4bSTejun Heo  * @ss: subsystem in question
8749d1dc4bSTejun Heo  */
8849d1dc4bSTejun Heo #define cgroup_subsys_enabled(ss)						\
8949d1dc4bSTejun Heo 	static_branch_likely(&ss ## _enabled_key)
9049d1dc4bSTejun Heo 
9149d1dc4bSTejun Heo /**
9249d1dc4bSTejun Heo  * cgroup_subsys_on_dfl - fast test on whether a subsys is on default hierarchy
9349d1dc4bSTejun Heo  * @ss: subsystem in question
9449d1dc4bSTejun Heo  */
9549d1dc4bSTejun Heo #define cgroup_subsys_on_dfl(ss)						\
9649d1dc4bSTejun Heo 	static_branch_likely(&ss ## _on_dfl_key)
9749d1dc4bSTejun Heo 
98c326aa2bSTejun Heo bool css_has_online_children(struct cgroup_subsys_state *css);
99c326aa2bSTejun Heo struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss);
100fc5a828bSDennis Zhou struct cgroup_subsys_state *cgroup_e_css(struct cgroup *cgroup,
101fc5a828bSDennis Zhou 					 struct cgroup_subsys *ss);
102c326aa2bSTejun Heo struct cgroup_subsys_state *cgroup_get_e_css(struct cgroup *cgroup,
103c326aa2bSTejun Heo 					     struct cgroup_subsys *ss);
104c326aa2bSTejun Heo struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry,
105c326aa2bSTejun Heo 						       struct cgroup_subsys *ss);
106c326aa2bSTejun Heo 
10716af4396STejun Heo struct cgroup *cgroup_get_from_path(const char *path);
1081f3fe7ebSMartin KaFai Lau struct cgroup *cgroup_get_from_fd(int fd);
109a6d1ce59SYosry Ahmed struct cgroup *cgroup_v1v2_get_from_fd(int fd);
11016af4396STejun Heo 
111c326aa2bSTejun Heo int cgroup_attach_task_all(struct task_struct *from, struct task_struct *);
112c326aa2bSTejun Heo int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from);
113c326aa2bSTejun Heo 
114c326aa2bSTejun Heo int cgroup_add_dfl_cftypes(struct cgroup_subsys *ss, struct cftype *cfts);
115c326aa2bSTejun Heo int cgroup_add_legacy_cftypes(struct cgroup_subsys *ss, struct cftype *cfts);
1164a893bdcSMichal Koutný int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts);
117c326aa2bSTejun Heo int cgroup_rm_cftypes(struct cftype *cfts);
11834c06254STejun Heo void cgroup_file_notify(struct cgroup_file *cfile);
119e2691f6bSTejun Heo void cgroup_file_show(struct cgroup_file *cfile, bool show);
120c326aa2bSTejun Heo 
121c326aa2bSTejun Heo int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry);
122c326aa2bSTejun Heo int proc_cgroup_show(struct seq_file *m, struct pid_namespace *ns,
123ddbcc7e8SPaul Menage 		     struct pid *pid, struct task_struct *tsk);
124ddbcc7e8SPaul Menage 
125c326aa2bSTejun Heo void cgroup_fork(struct task_struct *p);
126ef2c41cfSChristian Brauner extern int cgroup_can_fork(struct task_struct *p,
127ef2c41cfSChristian Brauner 			   struct kernel_clone_args *kargs);
128ef2c41cfSChristian Brauner extern void cgroup_cancel_fork(struct task_struct *p,
129ef2c41cfSChristian Brauner 			       struct kernel_clone_args *kargs);
130ef2c41cfSChristian Brauner extern void cgroup_post_fork(struct task_struct *p,
131ef2c41cfSChristian Brauner 			     struct kernel_clone_args *kargs);
132c326aa2bSTejun Heo void cgroup_exit(struct task_struct *p);
13351bee5abSOleg Nesterov void cgroup_release(struct task_struct *p);
1342e91fa7fSTejun Heo void cgroup_free(struct task_struct *p);
135c326aa2bSTejun Heo 
136c326aa2bSTejun Heo int cgroup_init_early(void);
137c326aa2bSTejun Heo int cgroup_init(void);
138c326aa2bSTejun Heo 
139a5e112e6STejun Heo int cgroup_parse_float(const char *input, unsigned dec_shift, s64 *v);
140a5e112e6STejun Heo 
141c326aa2bSTejun Heo /*
142c326aa2bSTejun Heo  * Iteration helpers and macros.
143c326aa2bSTejun Heo  */
144c326aa2bSTejun Heo 
145c326aa2bSTejun Heo struct cgroup_subsys_state *css_next_child(struct cgroup_subsys_state *pos,
146c326aa2bSTejun Heo 					   struct cgroup_subsys_state *parent);
147c326aa2bSTejun Heo struct cgroup_subsys_state *css_next_descendant_pre(struct cgroup_subsys_state *pos,
148c326aa2bSTejun Heo 						    struct cgroup_subsys_state *css);
149c326aa2bSTejun Heo struct cgroup_subsys_state *css_rightmost_descendant(struct cgroup_subsys_state *pos);
150c326aa2bSTejun Heo struct cgroup_subsys_state *css_next_descendant_post(struct cgroup_subsys_state *pos,
151c326aa2bSTejun Heo 						     struct cgroup_subsys_state *css);
152c326aa2bSTejun Heo 
1531f7dd3e5STejun Heo struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset,
1541f7dd3e5STejun Heo 					 struct cgroup_subsys_state **dst_cssp);
1551f7dd3e5STejun Heo struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset,
1561f7dd3e5STejun Heo 					struct cgroup_subsys_state **dst_cssp);
157c326aa2bSTejun Heo 
158bc2fb7edSTejun Heo void css_task_iter_start(struct cgroup_subsys_state *css, unsigned int flags,
159c326aa2bSTejun Heo 			 struct css_task_iter *it);
160c326aa2bSTejun Heo struct task_struct *css_task_iter_next(struct css_task_iter *it);
161c326aa2bSTejun Heo void css_task_iter_end(struct css_task_iter *it);
162c326aa2bSTejun Heo 
163c326aa2bSTejun Heo /**
164c326aa2bSTejun Heo  * css_for_each_child - iterate through children of a css
165c326aa2bSTejun Heo  * @pos: the css * to use as the loop cursor
166c326aa2bSTejun Heo  * @parent: css whose children to walk
167c326aa2bSTejun Heo  *
168c326aa2bSTejun Heo  * Walk @parent's children.  Must be called under rcu_read_lock().
169c326aa2bSTejun Heo  *
170c326aa2bSTejun Heo  * If a subsystem synchronizes ->css_online() and the start of iteration, a
171c326aa2bSTejun Heo  * css which finished ->css_online() is guaranteed to be visible in the
172c326aa2bSTejun Heo  * future iterations and will stay visible until the last reference is put.
173c326aa2bSTejun Heo  * A css which hasn't finished ->css_online() or already finished
174c326aa2bSTejun Heo  * ->css_offline() may show up during traversal.  It's each subsystem's
175c326aa2bSTejun Heo  * responsibility to synchronize against on/offlining.
176c326aa2bSTejun Heo  *
177c326aa2bSTejun Heo  * It is allowed to temporarily drop RCU read lock during iteration.  The
178c326aa2bSTejun Heo  * caller is responsible for ensuring that @pos remains accessible until
179c326aa2bSTejun Heo  * the start of the next iteration by, for example, bumping the css refcnt.
180c326aa2bSTejun Heo  */
181c326aa2bSTejun Heo #define css_for_each_child(pos, parent)					\
182c326aa2bSTejun Heo 	for ((pos) = css_next_child(NULL, (parent)); (pos);		\
183c326aa2bSTejun Heo 	     (pos) = css_next_child((pos), (parent)))
184c326aa2bSTejun Heo 
185c326aa2bSTejun Heo /**
186c326aa2bSTejun Heo  * css_for_each_descendant_pre - pre-order walk of a css's descendants
187c326aa2bSTejun Heo  * @pos: the css * to use as the loop cursor
188c326aa2bSTejun Heo  * @root: css whose descendants to walk
189c326aa2bSTejun Heo  *
190c326aa2bSTejun Heo  * Walk @root's descendants.  @root is included in the iteration and the
191c326aa2bSTejun Heo  * first node to be visited.  Must be called under rcu_read_lock().
192c326aa2bSTejun Heo  *
193c326aa2bSTejun Heo  * If a subsystem synchronizes ->css_online() and the start of iteration, a
194c326aa2bSTejun Heo  * css which finished ->css_online() is guaranteed to be visible in the
195c326aa2bSTejun Heo  * future iterations and will stay visible until the last reference is put.
196c326aa2bSTejun Heo  * A css which hasn't finished ->css_online() or already finished
197c326aa2bSTejun Heo  * ->css_offline() may show up during traversal.  It's each subsystem's
198c326aa2bSTejun Heo  * responsibility to synchronize against on/offlining.
199c326aa2bSTejun Heo  *
200c326aa2bSTejun Heo  * For example, the following guarantees that a descendant can't escape
201c326aa2bSTejun Heo  * state updates of its ancestors.
202c326aa2bSTejun Heo  *
203c326aa2bSTejun Heo  * my_online(@css)
204c326aa2bSTejun Heo  * {
205c326aa2bSTejun Heo  *	Lock @css's parent and @css;
206c326aa2bSTejun Heo  *	Inherit state from the parent;
207c326aa2bSTejun Heo  *	Unlock both.
208c326aa2bSTejun Heo  * }
209c326aa2bSTejun Heo  *
210c326aa2bSTejun Heo  * my_update_state(@css)
211c326aa2bSTejun Heo  * {
212c326aa2bSTejun Heo  *	css_for_each_descendant_pre(@pos, @css) {
213c326aa2bSTejun Heo  *		Lock @pos;
214c326aa2bSTejun Heo  *		if (@pos == @css)
215c326aa2bSTejun Heo  *			Update @css's state;
216c326aa2bSTejun Heo  *		else
217c326aa2bSTejun Heo  *			Verify @pos is alive and inherit state from its parent;
218c326aa2bSTejun Heo  *		Unlock @pos;
219c326aa2bSTejun Heo  *	}
220c326aa2bSTejun Heo  * }
221c326aa2bSTejun Heo  *
222c326aa2bSTejun Heo  * As long as the inheriting step, including checking the parent state, is
223c326aa2bSTejun Heo  * enclosed inside @pos locking, double-locking the parent isn't necessary
224c326aa2bSTejun Heo  * while inheriting.  The state update to the parent is guaranteed to be
225c326aa2bSTejun Heo  * visible by walking order and, as long as inheriting operations to the
226c326aa2bSTejun Heo  * same @pos are atomic to each other, multiple updates racing each other
227c326aa2bSTejun Heo  * still result in the correct state.  It's guaranateed that at least one
228c326aa2bSTejun Heo  * inheritance happens for any css after the latest update to its parent.
229c326aa2bSTejun Heo  *
230c326aa2bSTejun Heo  * If checking parent's state requires locking the parent, each inheriting
231c326aa2bSTejun Heo  * iteration should lock and unlock both @pos->parent and @pos.
232c326aa2bSTejun Heo  *
233c326aa2bSTejun Heo  * Alternatively, a subsystem may choose to use a single global lock to
234c326aa2bSTejun Heo  * synchronize ->css_online() and ->css_offline() against tree-walking
235c326aa2bSTejun Heo  * operations.
236c326aa2bSTejun Heo  *
237c326aa2bSTejun Heo  * It is allowed to temporarily drop RCU read lock during iteration.  The
238c326aa2bSTejun Heo  * caller is responsible for ensuring that @pos remains accessible until
239c326aa2bSTejun Heo  * the start of the next iteration by, for example, bumping the css refcnt.
240c326aa2bSTejun Heo  */
241c326aa2bSTejun Heo #define css_for_each_descendant_pre(pos, css)				\
242c326aa2bSTejun Heo 	for ((pos) = css_next_descendant_pre(NULL, (css)); (pos);	\
243c326aa2bSTejun Heo 	     (pos) = css_next_descendant_pre((pos), (css)))
244c326aa2bSTejun Heo 
245c326aa2bSTejun Heo /**
246c326aa2bSTejun Heo  * css_for_each_descendant_post - post-order walk of a css's descendants
247c326aa2bSTejun Heo  * @pos: the css * to use as the loop cursor
248c326aa2bSTejun Heo  * @css: css whose descendants to walk
249c326aa2bSTejun Heo  *
250c326aa2bSTejun Heo  * Similar to css_for_each_descendant_pre() but performs post-order
251c326aa2bSTejun Heo  * traversal instead.  @root is included in the iteration and the last
252c326aa2bSTejun Heo  * node to be visited.
253c326aa2bSTejun Heo  *
254c326aa2bSTejun Heo  * If a subsystem synchronizes ->css_online() and the start of iteration, a
255c326aa2bSTejun Heo  * css which finished ->css_online() is guaranteed to be visible in the
256c326aa2bSTejun Heo  * future iterations and will stay visible until the last reference is put.
257c326aa2bSTejun Heo  * A css which hasn't finished ->css_online() or already finished
258c326aa2bSTejun Heo  * ->css_offline() may show up during traversal.  It's each subsystem's
259c326aa2bSTejun Heo  * responsibility to synchronize against on/offlining.
260c326aa2bSTejun Heo  *
261c326aa2bSTejun Heo  * Note that the walk visibility guarantee example described in pre-order
262c326aa2bSTejun Heo  * walk doesn't apply the same to post-order walks.
263c326aa2bSTejun Heo  */
264c326aa2bSTejun Heo #define css_for_each_descendant_post(pos, css)				\
265c326aa2bSTejun Heo 	for ((pos) = css_next_descendant_post(NULL, (css)); (pos);	\
266c326aa2bSTejun Heo 	     (pos) = css_next_descendant_post((pos), (css)))
267c326aa2bSTejun Heo 
268c326aa2bSTejun Heo /**
269c326aa2bSTejun Heo  * cgroup_taskset_for_each - iterate cgroup_taskset
270c326aa2bSTejun Heo  * @task: the loop cursor
2711f7dd3e5STejun Heo  * @dst_css: the destination css
272c326aa2bSTejun Heo  * @tset: taskset to iterate
2734530eddbSTejun Heo  *
2744530eddbSTejun Heo  * @tset may contain multiple tasks and they may belong to multiple
2751f7dd3e5STejun Heo  * processes.
2761f7dd3e5STejun Heo  *
2771f7dd3e5STejun Heo  * On the v2 hierarchy, there may be tasks from multiple processes and they
2781f7dd3e5STejun Heo  * may not share the source or destination csses.
2791f7dd3e5STejun Heo  *
2801f7dd3e5STejun Heo  * On traditional hierarchies, when there are multiple tasks in @tset, if a
2811f7dd3e5STejun Heo  * task of a process is in @tset, all tasks of the process are in @tset.
2821f7dd3e5STejun Heo  * Also, all are guaranteed to share the same source and destination csses.
2834530eddbSTejun Heo  *
2844530eddbSTejun Heo  * Iteration is not in any specific order.
285c326aa2bSTejun Heo  */
2861f7dd3e5STejun Heo #define cgroup_taskset_for_each(task, dst_css, tset)			\
2871f7dd3e5STejun Heo 	for ((task) = cgroup_taskset_first((tset), &(dst_css));		\
2881f7dd3e5STejun Heo 	     (task);							\
2891f7dd3e5STejun Heo 	     (task) = cgroup_taskset_next((tset), &(dst_css)))
290c326aa2bSTejun Heo 
2914530eddbSTejun Heo /**
2924530eddbSTejun Heo  * cgroup_taskset_for_each_leader - iterate group leaders in a cgroup_taskset
2934530eddbSTejun Heo  * @leader: the loop cursor
2941f7dd3e5STejun Heo  * @dst_css: the destination css
2957b4632f0SGeliang Tang  * @tset: taskset to iterate
2964530eddbSTejun Heo  *
2974530eddbSTejun Heo  * Iterate threadgroup leaders of @tset.  For single-task migrations, @tset
2984530eddbSTejun Heo  * may not contain any.
2994530eddbSTejun Heo  */
3001f7dd3e5STejun Heo #define cgroup_taskset_for_each_leader(leader, dst_css, tset)		\
3011f7dd3e5STejun Heo 	for ((leader) = cgroup_taskset_first((tset), &(dst_css));	\
3021f7dd3e5STejun Heo 	     (leader);							\
3031f7dd3e5STejun Heo 	     (leader) = cgroup_taskset_next((tset), &(dst_css)))	\
3044530eddbSTejun Heo 		if ((leader) != (leader)->group_leader)			\
3054530eddbSTejun Heo 			;						\
3064530eddbSTejun Heo 		else
3074530eddbSTejun Heo 
308c326aa2bSTejun Heo /*
309c326aa2bSTejun Heo  * Inline functions.
310c326aa2bSTejun Heo  */
311ddbcc7e8SPaul Menage 
3126ab42860STejun Heo #ifdef CONFIG_DEBUG_CGROUP_REF
3136ab42860STejun Heo void css_get(struct cgroup_subsys_state *css);
3146ab42860STejun Heo void css_get_many(struct cgroup_subsys_state *css, unsigned int n);
3156ab42860STejun Heo bool css_tryget(struct cgroup_subsys_state *css);
3166ab42860STejun Heo bool css_tryget_online(struct cgroup_subsys_state *css);
3176ab42860STejun Heo void css_put(struct cgroup_subsys_state *css);
3186ab42860STejun Heo void css_put_many(struct cgroup_subsys_state *css, unsigned int n);
3196ab42860STejun Heo #else
3206ab42860STejun Heo #define CGROUP_REF_FN_ATTRS	static inline
32179a7f41fSTejun Heo #define CGROUP_REF_EXPORT(fn)
3226ab42860STejun Heo #include <linux/cgroup_refcnt.h>
3236ab42860STejun Heo #endif
3246ab42860STejun Heo 
cgroup_id(const struct cgroup * cgrp)325a3b89864SPeter Zijlstra static inline u64 cgroup_id(const struct cgroup *cgrp)
32674321038STejun Heo {
32774321038STejun Heo 	return cgrp->kn->id;
32874321038STejun Heo }
32974321038STejun Heo 
3305de0107eSTejun Heo /**
33141c25707STejun Heo  * css_is_dying - test whether the specified css is dying
33241c25707STejun Heo  * @css: target css
33341c25707STejun Heo  *
33441c25707STejun Heo  * Test whether @css is in the process of offlining or already offline.  In
33541c25707STejun Heo  * most cases, ->css_online() and ->css_offline() callbacks should be
33641c25707STejun Heo  * enough; however, the actual offline operations are RCU delayed and this
33741c25707STejun Heo  * test returns %true also when @css is scheduled to be offlined.
33841c25707STejun Heo  *
33941c25707STejun Heo  * This is useful, for example, when the use case requires synchronous
34041c25707STejun Heo  * behavior with respect to cgroup removal.  cgroup removal schedules css
34141c25707STejun Heo  * offlining but the css can seem alive while the operation is being
34241c25707STejun Heo  * delayed.  If the delay affects user visible semantics, this test can be
34341c25707STejun Heo  * used to resolve the situation.
34441c25707STejun Heo  */
css_is_dying(struct cgroup_subsys_state * css)34541c25707STejun Heo static inline bool css_is_dying(struct cgroup_subsys_state *css)
34641c25707STejun Heo {
347*a22b3d54SWaiman Long 	return css->flags & CSS_DYING;
34841c25707STejun Heo }
34941c25707STejun Heo 
cgroup_get(struct cgroup * cgrp)3503e48930cSTejun Heo static inline void cgroup_get(struct cgroup *cgrp)
3513e48930cSTejun Heo {
3523e48930cSTejun Heo 	css_get(&cgrp->self);
3533e48930cSTejun Heo }
3543e48930cSTejun Heo 
cgroup_tryget(struct cgroup * cgrp)3553e48930cSTejun Heo static inline bool cgroup_tryget(struct cgroup *cgrp)
3563e48930cSTejun Heo {
3573e48930cSTejun Heo 	return css_tryget(&cgrp->self);
3583e48930cSTejun Heo }
3593e48930cSTejun Heo 
cgroup_put(struct cgroup * cgrp)36016af4396STejun Heo static inline void cgroup_put(struct cgroup *cgrp)
36116af4396STejun Heo {
36216af4396STejun Heo 	css_put(&cgrp->self);
36316af4396STejun Heo }
36416af4396STejun Heo 
365354ed597SYu Zhao extern struct mutex cgroup_mutex;
366354ed597SYu Zhao 
cgroup_lock(void)367354ed597SYu Zhao static inline void cgroup_lock(void)
368354ed597SYu Zhao {
369354ed597SYu Zhao 	mutex_lock(&cgroup_mutex);
370354ed597SYu Zhao }
371354ed597SYu Zhao 
cgroup_unlock(void)372354ed597SYu Zhao static inline void cgroup_unlock(void)
373354ed597SYu Zhao {
374354ed597SYu Zhao 	mutex_unlock(&cgroup_mutex);
375354ed597SYu Zhao }
376354ed597SYu Zhao 
377c326aa2bSTejun Heo /**
378c326aa2bSTejun Heo  * task_css_set_check - obtain a task's css_set with extra access conditions
379c326aa2bSTejun Heo  * @task: the task to obtain css_set for
380c326aa2bSTejun Heo  * @__c: extra condition expression to be passed to rcu_dereference_check()
381b414dc09SLi Zefan  *
382c326aa2bSTejun Heo  * A task's css_set is RCU protected, initialized and exited while holding
383c326aa2bSTejun Heo  * task_lock(), and can only be modified while holding both cgroup_mutex
384c326aa2bSTejun Heo  * and task_lock() while the task is alive.  This macro verifies that the
385c326aa2bSTejun Heo  * caller is inside proper critical section and returns @task's css_set.
3860ab02ca8SLi Zefan  *
387c326aa2bSTejun Heo  * The caller can also specify additional allowed conditions via @__c, such
388c326aa2bSTejun Heo  * as locks used during the cgroup_subsys::attach() methods.
389b414dc09SLi Zefan  */
390c326aa2bSTejun Heo #ifdef CONFIG_PROVE_RCU
391c326aa2bSTejun Heo #define task_css_set_check(task, __c)					\
392c326aa2bSTejun Heo 	rcu_dereference_check((task)->cgroups,				\
393f2aa197eSChengming Zhou 		rcu_read_lock_sched_held() ||				\
394c326aa2bSTejun Heo 		lockdep_is_held(&cgroup_mutex) ||			\
395f0d9a5f1STejun Heo 		lockdep_is_held(&css_set_lock) ||			\
396c326aa2bSTejun Heo 		((task)->flags & PF_EXITING) || (__c))
397c326aa2bSTejun Heo #else
398c326aa2bSTejun Heo #define task_css_set_check(task, __c)					\
399c326aa2bSTejun Heo 	rcu_dereference((task)->cgroups)
4002bd59d48STejun Heo #endif
401ddbcc7e8SPaul Menage 
402c326aa2bSTejun Heo /**
403c326aa2bSTejun Heo  * task_css_check - obtain css for (task, subsys) w/ extra access conds
404c326aa2bSTejun Heo  * @task: the target task
405c326aa2bSTejun Heo  * @subsys_id: the target subsystem ID
406c326aa2bSTejun Heo  * @__c: extra condition expression to be passed to rcu_dereference_check()
407c326aa2bSTejun Heo  *
408c326aa2bSTejun Heo  * Return the cgroup_subsys_state for the (@task, @subsys_id) pair.  The
409c326aa2bSTejun Heo  * synchronization rules are the same as task_css_set_check().
410c326aa2bSTejun Heo  */
411c326aa2bSTejun Heo #define task_css_check(task, subsys_id, __c)				\
412c326aa2bSTejun Heo 	task_css_set_check((task), (__c))->subsys[(subsys_id)]
413c326aa2bSTejun Heo 
414c326aa2bSTejun Heo /**
415c326aa2bSTejun Heo  * task_css_set - obtain a task's css_set
416c326aa2bSTejun Heo  * @task: the task to obtain css_set for
417c326aa2bSTejun Heo  *
418c326aa2bSTejun Heo  * See task_css_set_check().
419c326aa2bSTejun Heo  */
task_css_set(struct task_struct * task)420c326aa2bSTejun Heo static inline struct css_set *task_css_set(struct task_struct *task)
421c326aa2bSTejun Heo {
422c326aa2bSTejun Heo 	return task_css_set_check(task, false);
423c326aa2bSTejun Heo }
424c326aa2bSTejun Heo 
425c326aa2bSTejun Heo /**
426c326aa2bSTejun Heo  * task_css - obtain css for (task, subsys)
427c326aa2bSTejun Heo  * @task: the target task
428c326aa2bSTejun Heo  * @subsys_id: the target subsystem ID
429c326aa2bSTejun Heo  *
430c326aa2bSTejun Heo  * See task_css_check().
431c326aa2bSTejun Heo  */
task_css(struct task_struct * task,int subsys_id)432c326aa2bSTejun Heo static inline struct cgroup_subsys_state *task_css(struct task_struct *task,
433c326aa2bSTejun Heo 						   int subsys_id)
434c326aa2bSTejun Heo {
435c326aa2bSTejun Heo 	return task_css_check(task, subsys_id, false);
436c326aa2bSTejun Heo }
437c326aa2bSTejun Heo 
438c326aa2bSTejun Heo /**
439bbe179f8SLinus Torvalds  * task_get_css - find and get the css for (task, subsys)
440bbe179f8SLinus Torvalds  * @task: the target task
441bbe179f8SLinus Torvalds  * @subsys_id: the target subsystem ID
442bbe179f8SLinus Torvalds  *
443bbe179f8SLinus Torvalds  * Find the css for the (@task, @subsys_id) combination, increment a
444bbe179f8SLinus Torvalds  * reference on and return it.  This function is guaranteed to return a
44518fa84a2STejun Heo  * valid css.  The returned css may already have been offlined.
446bbe179f8SLinus Torvalds  */
447bbe179f8SLinus Torvalds static inline struct cgroup_subsys_state *
task_get_css(struct task_struct * task,int subsys_id)448bbe179f8SLinus Torvalds task_get_css(struct task_struct *task, int subsys_id)
449bbe179f8SLinus Torvalds {
450bbe179f8SLinus Torvalds 	struct cgroup_subsys_state *css;
451bbe179f8SLinus Torvalds 
452bbe179f8SLinus Torvalds 	rcu_read_lock();
453bbe179f8SLinus Torvalds 	while (true) {
454bbe179f8SLinus Torvalds 		css = task_css(task, subsys_id);
45518fa84a2STejun Heo 		/*
45618fa84a2STejun Heo 		 * Can't use css_tryget_online() here.  A task which has
45718fa84a2STejun Heo 		 * PF_EXITING set may stay associated with an offline css.
45818fa84a2STejun Heo 		 * If such task calls this function, css_tryget_online()
45918fa84a2STejun Heo 		 * will keep failing.
46018fa84a2STejun Heo 		 */
46118fa84a2STejun Heo 		if (likely(css_tryget(css)))
462bbe179f8SLinus Torvalds 			break;
463bbe179f8SLinus Torvalds 		cpu_relax();
464bbe179f8SLinus Torvalds 	}
465bbe179f8SLinus Torvalds 	rcu_read_unlock();
466bbe179f8SLinus Torvalds 	return css;
467bbe179f8SLinus Torvalds }
468bbe179f8SLinus Torvalds 
469bbe179f8SLinus Torvalds /**
470c326aa2bSTejun Heo  * task_css_is_root - test whether a task belongs to the root css
471c326aa2bSTejun Heo  * @task: the target task
472c326aa2bSTejun Heo  * @subsys_id: the target subsystem ID
473c326aa2bSTejun Heo  *
474c326aa2bSTejun Heo  * Test whether @task belongs to the root css on the specified subsystem.
475c326aa2bSTejun Heo  * May be invoked in any context.
476c326aa2bSTejun Heo  */
task_css_is_root(struct task_struct * task,int subsys_id)477c326aa2bSTejun Heo static inline bool task_css_is_root(struct task_struct *task, int subsys_id)
478c326aa2bSTejun Heo {
479c326aa2bSTejun Heo 	return task_css_check(task, subsys_id, true) ==
480c326aa2bSTejun Heo 		init_css_set.subsys[subsys_id];
481c326aa2bSTejun Heo }
482c326aa2bSTejun Heo 
task_cgroup(struct task_struct * task,int subsys_id)483c326aa2bSTejun Heo static inline struct cgroup *task_cgroup(struct task_struct *task,
484c326aa2bSTejun Heo 					 int subsys_id)
485c326aa2bSTejun Heo {
486c326aa2bSTejun Heo 	return task_css(task, subsys_id)->cgroup;
487c326aa2bSTejun Heo }
488a2dd4247STejun Heo 
task_dfl_cgroup(struct task_struct * task)4893e48930cSTejun Heo static inline struct cgroup *task_dfl_cgroup(struct task_struct *task)
4903e48930cSTejun Heo {
4913e48930cSTejun Heo 	return task_css_set(task)->dfl_cgrp;
4923e48930cSTejun Heo }
4933e48930cSTejun Heo 
cgroup_parent(struct cgroup * cgrp)4943e48930cSTejun Heo static inline struct cgroup *cgroup_parent(struct cgroup *cgrp)
4953e48930cSTejun Heo {
4963e48930cSTejun Heo 	struct cgroup_subsys_state *parent_css = cgrp->self.parent;
4973e48930cSTejun Heo 
4983e48930cSTejun Heo 	if (parent_css)
4993e48930cSTejun Heo 		return container_of(parent_css, struct cgroup, self);
5003e48930cSTejun Heo 	return NULL;
5013e48930cSTejun Heo }
5023e48930cSTejun Heo 
503b11cfb58STejun Heo /**
504b11cfb58STejun Heo  * cgroup_is_descendant - test ancestry
505b11cfb58STejun Heo  * @cgrp: the cgroup to be tested
506b11cfb58STejun Heo  * @ancestor: possible ancestor of @cgrp
507b11cfb58STejun Heo  *
508b11cfb58STejun Heo  * Test whether @cgrp is a descendant of @ancestor.  It also returns %true
509b11cfb58STejun Heo  * if @cgrp == @ancestor.  This function is safe to call as long as @cgrp
510b11cfb58STejun Heo  * and @ancestor are accessible.
511b11cfb58STejun Heo  */
cgroup_is_descendant(struct cgroup * cgrp,struct cgroup * ancestor)512b11cfb58STejun Heo static inline bool cgroup_is_descendant(struct cgroup *cgrp,
513b11cfb58STejun Heo 					struct cgroup *ancestor)
514b11cfb58STejun Heo {
515b11cfb58STejun Heo 	if (cgrp->root != ancestor->root || cgrp->level < ancestor->level)
516b11cfb58STejun Heo 		return false;
5177f203bc8STejun Heo 	return cgrp->ancestors[ancestor->level] == ancestor;
518b11cfb58STejun Heo }
519b11cfb58STejun Heo 
520aed704b7SSargun Dhillon /**
52177236281SAndrey Ignatov  * cgroup_ancestor - find ancestor of cgroup
52277236281SAndrey Ignatov  * @cgrp: cgroup to find ancestor of
52377236281SAndrey Ignatov  * @ancestor_level: level of ancestor to find starting from root
52477236281SAndrey Ignatov  *
52577236281SAndrey Ignatov  * Find ancestor of cgroup at specified level starting from root if it exists
52677236281SAndrey Ignatov  * and return pointer to it. Return NULL if @cgrp doesn't have ancestor at
52777236281SAndrey Ignatov  * @ancestor_level.
52877236281SAndrey Ignatov  *
52977236281SAndrey Ignatov  * This function is safe to call as long as @cgrp is accessible.
53077236281SAndrey Ignatov  */
cgroup_ancestor(struct cgroup * cgrp,int ancestor_level)53177236281SAndrey Ignatov static inline struct cgroup *cgroup_ancestor(struct cgroup *cgrp,
53277236281SAndrey Ignatov 					     int ancestor_level)
53377236281SAndrey Ignatov {
5347f203bc8STejun Heo 	if (ancestor_level < 0 || ancestor_level > cgrp->level)
53577236281SAndrey Ignatov 		return NULL;
5367f203bc8STejun Heo 	return cgrp->ancestors[ancestor_level];
53777236281SAndrey Ignatov }
53877236281SAndrey Ignatov 
53977236281SAndrey Ignatov /**
540aed704b7SSargun Dhillon  * task_under_cgroup_hierarchy - test task's membership of cgroup ancestry
541aed704b7SSargun Dhillon  * @task: the task to be tested
542aed704b7SSargun Dhillon  * @ancestor: possible ancestor of @task's cgroup
543aed704b7SSargun Dhillon  *
544aed704b7SSargun Dhillon  * Tests whether @task's default cgroup hierarchy is a descendant of @ancestor.
545aed704b7SSargun Dhillon  * It follows all the same rules as cgroup_is_descendant, and only applies
546aed704b7SSargun Dhillon  * to the default hierarchy.
547aed704b7SSargun Dhillon  */
task_under_cgroup_hierarchy(struct task_struct * task,struct cgroup * ancestor)548aed704b7SSargun Dhillon static inline bool task_under_cgroup_hierarchy(struct task_struct *task,
549aed704b7SSargun Dhillon 					       struct cgroup *ancestor)
550aed704b7SSargun Dhillon {
551aed704b7SSargun Dhillon 	struct css_set *cset = task_css_set(task);
552aed704b7SSargun Dhillon 
553aed704b7SSargun Dhillon 	return cgroup_is_descendant(cset->dfl_cgrp, ancestor);
554aed704b7SSargun Dhillon }
555aed704b7SSargun Dhillon 
55607bc356eSTejun Heo /* no synchronization, the result can only be used as a hint */
cgroup_is_populated(struct cgroup * cgrp)55727bd4dbbSTejun Heo static inline bool cgroup_is_populated(struct cgroup *cgrp)
55807bc356eSTejun Heo {
559454000adSTejun Heo 	return cgrp->nr_populated_csets + cgrp->nr_populated_domain_children +
560454000adSTejun Heo 		cgrp->nr_populated_threaded_children;
56107bc356eSTejun Heo }
56207bc356eSTejun Heo 
563f29374b1SZefan Li /* returns ino associated with a cgroup */
cgroup_ino(struct cgroup * cgrp)564b1664924STejun Heo static inline ino_t cgroup_ino(struct cgroup *cgrp)
565b1664924STejun Heo {
56667c0496eSTejun Heo 	return kernfs_ino(cgrp->kn);
567b1664924STejun Heo }
568b1664924STejun Heo 
569b4168640STejun Heo /* cft/css accessors for cftype->write() operation */
of_cft(struct kernfs_open_file * of)570b4168640STejun Heo static inline struct cftype *of_cft(struct kernfs_open_file *of)
5717da11279STejun Heo {
5722bd59d48STejun Heo 	return of->kn->priv;
5737da11279STejun Heo }
5747da11279STejun Heo 
575b4168640STejun Heo struct cgroup_subsys_state *of_css(struct kernfs_open_file *of);
576b4168640STejun Heo 
577b4168640STejun Heo /* cft/css accessors for cftype->seq_*() operations */
seq_cft(struct seq_file * seq)578b4168640STejun Heo static inline struct cftype *seq_cft(struct seq_file *seq)
579b4168640STejun Heo {
580b4168640STejun Heo 	return of_cft(seq->private);
581b4168640STejun Heo }
582b4168640STejun Heo 
seq_css(struct seq_file * seq)583b4168640STejun Heo static inline struct cgroup_subsys_state *seq_css(struct seq_file *seq)
584b4168640STejun Heo {
585b4168640STejun Heo 	return of_css(seq->private);
586b4168640STejun Heo }
58759f5296bSTejun Heo 
588e61734c5STejun Heo /*
589e61734c5STejun Heo  * Name / path handling functions.  All are thin wrappers around the kernfs
590e61734c5STejun Heo  * counterparts and can be called under any context.
591e61734c5STejun Heo  */
592e61734c5STejun Heo 
cgroup_name(struct cgroup * cgrp,char * buf,size_t buflen)593e61734c5STejun Heo static inline int cgroup_name(struct cgroup *cgrp, char *buf, size_t buflen)
594e61734c5STejun Heo {
595e61734c5STejun Heo 	return kernfs_name(cgrp->kn, buf, buflen);
596e61734c5STejun Heo }
597e61734c5STejun Heo 
cgroup_path(struct cgroup * cgrp,char * buf,size_t buflen)5984c737b41STejun Heo static inline int cgroup_path(struct cgroup *cgrp, char *buf, size_t buflen)
599e61734c5STejun Heo {
600e61734c5STejun Heo 	return kernfs_path(cgrp->kn, buf, buflen);
601e61734c5STejun Heo }
602e61734c5STejun Heo 
pr_cont_cgroup_name(struct cgroup * cgrp)603e61734c5STejun Heo static inline void pr_cont_cgroup_name(struct cgroup *cgrp)
604e61734c5STejun Heo {
605e61734c5STejun Heo 	pr_cont_kernfs_name(cgrp->kn);
606e61734c5STejun Heo }
607e61734c5STejun Heo 
pr_cont_cgroup_path(struct cgroup * cgrp)608e61734c5STejun Heo static inline void pr_cont_cgroup_path(struct cgroup *cgrp)
609e61734c5STejun Heo {
610e61734c5STejun Heo 	pr_cont_kernfs_path(cgrp->kn);
611e61734c5STejun Heo }
612e61734c5STejun Heo 
6133958e2d0SSuren Baghdasaryan bool cgroup_psi_enabled(void);
6143958e2d0SSuren Baghdasaryan 
cgroup_init_kthreadd(void)61577f88796STejun Heo static inline void cgroup_init_kthreadd(void)
61677f88796STejun Heo {
61777f88796STejun Heo 	/*
61877f88796STejun Heo 	 * kthreadd is inherited by all kthreads, keep it in the root so
61977f88796STejun Heo 	 * that the new kthreads are guaranteed to stay in the root until
62077f88796STejun Heo 	 * initialization is finished.
62177f88796STejun Heo 	 */
62277f88796STejun Heo 	current->no_cgroup_migration = 1;
62377f88796STejun Heo }
62477f88796STejun Heo 
cgroup_kthread_ready(void)62577f88796STejun Heo static inline void cgroup_kthread_ready(void)
62677f88796STejun Heo {
62777f88796STejun Heo 	/*
62877f88796STejun Heo 	 * This kthread finished initialization.  The creator should have
62977f88796STejun Heo 	 * set PF_NO_SETAFFINITY if this kthread should stay in the root.
63077f88796STejun Heo 	 */
63177f88796STejun Heo 	current->no_cgroup_migration = 0;
63277f88796STejun Heo }
63377f88796STejun Heo 
63467c0496eSTejun Heo void cgroup_path_from_kernfs_id(u64 id, char *buf, size_t buflen);
6356b658c48SMuneendra Kumar struct cgroup *cgroup_get_from_id(u64 id);
636ddbcc7e8SPaul Menage #else /* !CONFIG_CGROUPS */
637ddbcc7e8SPaul Menage 
638f3ba5380STejun Heo struct cgroup_subsys_state;
639aed704b7SSargun Dhillon struct cgroup;
640f3ba5380STejun Heo 
cgroup_id(const struct cgroup * cgrp)641a3b89864SPeter Zijlstra static inline u64 cgroup_id(const struct cgroup *cgrp) { return 1; }
css_get(struct cgroup_subsys_state * css)6429b0eb69bSTejun Heo static inline void css_get(struct cgroup_subsys_state *css) {}
css_put(struct cgroup_subsys_state * css)643c326aa2bSTejun Heo static inline void css_put(struct cgroup_subsys_state *css) {}
cgroup_lock(void)644354ed597SYu Zhao static inline void cgroup_lock(void) {}
cgroup_unlock(void)645354ed597SYu Zhao static inline void cgroup_unlock(void) {}
cgroup_attach_task_all(struct task_struct * from,struct task_struct * t)646c326aa2bSTejun Heo static inline int cgroup_attach_task_all(struct task_struct *from,
647c326aa2bSTejun Heo 					 struct task_struct *t) { return 0; }
cgroupstats_build(struct cgroupstats * stats,struct dentry * dentry)648c326aa2bSTejun Heo static inline int cgroupstats_build(struct cgroupstats *stats,
649c326aa2bSTejun Heo 				    struct dentry *dentry) { return -EINVAL; }
650c326aa2bSTejun Heo 
cgroup_fork(struct task_struct * p)651b4f48b63SPaul Menage static inline void cgroup_fork(struct task_struct *p) {}
cgroup_can_fork(struct task_struct * p,struct kernel_clone_args * kargs)652ef2c41cfSChristian Brauner static inline int cgroup_can_fork(struct task_struct *p,
653ef2c41cfSChristian Brauner 				  struct kernel_clone_args *kargs) { return 0; }
cgroup_cancel_fork(struct task_struct * p,struct kernel_clone_args * kargs)654ef2c41cfSChristian Brauner static inline void cgroup_cancel_fork(struct task_struct *p,
655ef2c41cfSChristian Brauner 				      struct kernel_clone_args *kargs) {}
cgroup_post_fork(struct task_struct * p,struct kernel_clone_args * kargs)656ef2c41cfSChristian Brauner static inline void cgroup_post_fork(struct task_struct *p,
657ef2c41cfSChristian Brauner 				    struct kernel_clone_args *kargs) {}
cgroup_exit(struct task_struct * p)6581ec41830SLi Zefan static inline void cgroup_exit(struct task_struct *p) {}
cgroup_release(struct task_struct * p)65951bee5abSOleg Nesterov static inline void cgroup_release(struct task_struct *p) {}
cgroup_free(struct task_struct * p)6602e91fa7fSTejun Heo static inline void cgroup_free(struct task_struct *p) {}
661ddbcc7e8SPaul Menage 
cgroup_init_early(void)662c326aa2bSTejun Heo static inline int cgroup_init_early(void) { return 0; }
cgroup_init(void)663c326aa2bSTejun Heo static inline int cgroup_init(void) { return 0; }
cgroup_init_kthreadd(void)66477f88796STejun Heo static inline void cgroup_init_kthreadd(void) {}
cgroup_kthread_ready(void)66577f88796STejun Heo static inline void cgroup_kthread_ready(void) {}
666d7926ee3SSridhar Samudrala 
cgroup_parent(struct cgroup * cgrp)6672ce7135aSJohannes Weiner static inline struct cgroup *cgroup_parent(struct cgroup *cgrp)
6682ce7135aSJohannes Weiner {
6692ce7135aSJohannes Weiner 	return NULL;
6702ce7135aSJohannes Weiner }
6712ce7135aSJohannes Weiner 
cgroup_psi_enabled(void)6723958e2d0SSuren Baghdasaryan static inline bool cgroup_psi_enabled(void)
6733958e2d0SSuren Baghdasaryan {
6743958e2d0SSuren Baghdasaryan 	return false;
6753958e2d0SSuren Baghdasaryan }
6763958e2d0SSuren Baghdasaryan 
task_under_cgroup_hierarchy(struct task_struct * task,struct cgroup * ancestor)677aed704b7SSargun Dhillon static inline bool task_under_cgroup_hierarchy(struct task_struct *task,
678aed704b7SSargun Dhillon 					       struct cgroup *ancestor)
679aed704b7SSargun Dhillon {
680aed704b7SSargun Dhillon 	return true;
681aed704b7SSargun Dhillon }
68269fd5c39SShaohua Li 
cgroup_path_from_kernfs_id(u64 id,char * buf,size_t buflen)68367c0496eSTejun Heo static inline void cgroup_path_from_kernfs_id(u64 id, char *buf, size_t buflen)
68467c0496eSTejun Heo {}
685ddbcc7e8SPaul Menage #endif /* !CONFIG_CGROUPS */
686ddbcc7e8SPaul Menage 
6876162cef0STejun Heo #ifdef CONFIG_CGROUPS
6886162cef0STejun Heo /*
6896162cef0STejun Heo  * cgroup scalable recursive statistics.
6906162cef0STejun Heo  */
6916162cef0STejun Heo void cgroup_rstat_updated(struct cgroup *cgrp, int cpu);
6926162cef0STejun Heo void cgroup_rstat_flush(struct cgroup *cgrp);
6936162cef0STejun Heo 
694bd1060a1STejun Heo /*
695d2cc5ed6STejun Heo  * Basic resource stats.
696d2cc5ed6STejun Heo  */
697d2cc5ed6STejun Heo #ifdef CONFIG_CGROUP_CPUACCT
698d2cc5ed6STejun Heo void cpuacct_charge(struct task_struct *tsk, u64 cputime);
699d2cc5ed6STejun Heo void cpuacct_account_field(struct task_struct *tsk, int index, u64 val);
700d2cc5ed6STejun Heo #else
cpuacct_charge(struct task_struct * tsk,u64 cputime)701d2cc5ed6STejun Heo static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime) {}
cpuacct_account_field(struct task_struct * tsk,int index,u64 val)702d2cc5ed6STejun Heo static inline void cpuacct_account_field(struct task_struct *tsk, int index,
703d2cc5ed6STejun Heo 					 u64 val) {}
704d2cc5ed6STejun Heo #endif
705d2cc5ed6STejun Heo 
706041cd640STejun Heo void __cgroup_account_cputime(struct cgroup *cgrp, u64 delta_exec);
707041cd640STejun Heo void __cgroup_account_cputime_field(struct cgroup *cgrp,
708041cd640STejun Heo 				    enum cpu_usage_stat index, u64 delta_exec);
709041cd640STejun Heo 
cgroup_account_cputime(struct task_struct * task,u64 delta_exec)710d2cc5ed6STejun Heo static inline void cgroup_account_cputime(struct task_struct *task,
711d2cc5ed6STejun Heo 					  u64 delta_exec)
712d2cc5ed6STejun Heo {
713041cd640STejun Heo 	struct cgroup *cgrp;
714041cd640STejun Heo 
715d2cc5ed6STejun Heo 	cpuacct_charge(task, delta_exec);
716041cd640STejun Heo 
717041cd640STejun Heo 	cgrp = task_dfl_cgroup(task);
718041cd640STejun Heo 	if (cgroup_parent(cgrp))
719041cd640STejun Heo 		__cgroup_account_cputime(cgrp, delta_exec);
720d2cc5ed6STejun Heo }
721d2cc5ed6STejun Heo 
cgroup_account_cputime_field(struct task_struct * task,enum cpu_usage_stat index,u64 delta_exec)722d2cc5ed6STejun Heo static inline void cgroup_account_cputime_field(struct task_struct *task,
723d2cc5ed6STejun Heo 						enum cpu_usage_stat index,
724d2cc5ed6STejun Heo 						u64 delta_exec)
725d2cc5ed6STejun Heo {
726041cd640STejun Heo 	struct cgroup *cgrp;
727041cd640STejun Heo 
728d2cc5ed6STejun Heo 	cpuacct_account_field(task, index, delta_exec);
729041cd640STejun Heo 
730041cd640STejun Heo 	cgrp = task_dfl_cgroup(task);
731041cd640STejun Heo 	if (cgroup_parent(cgrp))
732041cd640STejun Heo 		__cgroup_account_cputime_field(cgrp, index, delta_exec);
733d2cc5ed6STejun Heo }
734d2cc5ed6STejun Heo 
735d2cc5ed6STejun Heo #else	/* CONFIG_CGROUPS */
736d2cc5ed6STejun Heo 
cgroup_account_cputime(struct task_struct * task,u64 delta_exec)737d2cc5ed6STejun Heo static inline void cgroup_account_cputime(struct task_struct *task,
738d2cc5ed6STejun Heo 					  u64 delta_exec) {}
cgroup_account_cputime_field(struct task_struct * task,enum cpu_usage_stat index,u64 delta_exec)739d2cc5ed6STejun Heo static inline void cgroup_account_cputime_field(struct task_struct *task,
740d2cc5ed6STejun Heo 						enum cpu_usage_stat index,
741d2cc5ed6STejun Heo 						u64 delta_exec) {}
742d2cc5ed6STejun Heo 
743d2cc5ed6STejun Heo #endif	/* CONFIG_CGROUPS */
744d2cc5ed6STejun Heo 
745d2cc5ed6STejun Heo /*
746bd1060a1STejun Heo  * sock->sk_cgrp_data handling.  For more info, see sock_cgroup_data
747bd1060a1STejun Heo  * definition in cgroup-defs.h.
748bd1060a1STejun Heo  */
749bd1060a1STejun Heo #ifdef CONFIG_SOCK_CGROUP_DATA
750bd1060a1STejun Heo 
751bd1060a1STejun Heo void cgroup_sk_alloc(struct sock_cgroup_data *skcd);
752ad0f75e5SCong Wang void cgroup_sk_clone(struct sock_cgroup_data *skcd);
753bd1060a1STejun Heo void cgroup_sk_free(struct sock_cgroup_data *skcd);
754bd1060a1STejun Heo 
sock_cgroup_ptr(struct sock_cgroup_data * skcd)755bd1060a1STejun Heo static inline struct cgroup *sock_cgroup_ptr(struct sock_cgroup_data *skcd)
756bd1060a1STejun Heo {
7578520e224SDaniel Borkmann 	return skcd->cgroup;
758bd1060a1STejun Heo }
759bd1060a1STejun Heo 
760bd1060a1STejun Heo #else	/* CONFIG_CGROUP_DATA */
761bd1060a1STejun Heo 
cgroup_sk_alloc(struct sock_cgroup_data * skcd)762bd1060a1STejun Heo static inline void cgroup_sk_alloc(struct sock_cgroup_data *skcd) {}
cgroup_sk_clone(struct sock_cgroup_data * skcd)763ad0f75e5SCong Wang static inline void cgroup_sk_clone(struct sock_cgroup_data *skcd) {}
cgroup_sk_free(struct sock_cgroup_data * skcd)764bd1060a1STejun Heo static inline void cgroup_sk_free(struct sock_cgroup_data *skcd) {}
765bd1060a1STejun Heo 
766bd1060a1STejun Heo #endif	/* CONFIG_CGROUP_DATA */
767bd1060a1STejun Heo 
768a79a908fSAditya Kali struct cgroup_namespace {
769a79a908fSAditya Kali 	struct ns_common	ns;
770a79a908fSAditya Kali 	struct user_namespace	*user_ns;
771d08311ddSEric W. Biederman 	struct ucounts		*ucounts;
772a79a908fSAditya Kali 	struct css_set          *root_cset;
773a79a908fSAditya Kali };
774a79a908fSAditya Kali 
775a79a908fSAditya Kali extern struct cgroup_namespace init_cgroup_ns;
776a79a908fSAditya Kali 
777a79a908fSAditya Kali #ifdef CONFIG_CGROUPS
778a79a908fSAditya Kali 
779a79a908fSAditya Kali void free_cgroup_ns(struct cgroup_namespace *ns);
780a79a908fSAditya Kali 
781a79a908fSAditya Kali struct cgroup_namespace *copy_cgroup_ns(unsigned long flags,
782a79a908fSAditya Kali 					struct user_namespace *user_ns,
783a79a908fSAditya Kali 					struct cgroup_namespace *old_ns);
784a79a908fSAditya Kali 
7854c737b41STejun Heo int cgroup_path_ns(struct cgroup *cgrp, char *buf, size_t buflen,
786a79a908fSAditya Kali 		   struct cgroup_namespace *ns);
787a79a908fSAditya Kali 
788a79a908fSAditya Kali #else /* !CONFIG_CGROUPS */
789a79a908fSAditya Kali 
free_cgroup_ns(struct cgroup_namespace * ns)790a79a908fSAditya Kali static inline void free_cgroup_ns(struct cgroup_namespace *ns) { }
791a79a908fSAditya Kali static inline struct cgroup_namespace *
copy_cgroup_ns(unsigned long flags,struct user_namespace * user_ns,struct cgroup_namespace * old_ns)792a79a908fSAditya Kali copy_cgroup_ns(unsigned long flags, struct user_namespace *user_ns,
793a79a908fSAditya Kali 	       struct cgroup_namespace *old_ns)
794a79a908fSAditya Kali {
795a79a908fSAditya Kali 	return old_ns;
796a79a908fSAditya Kali }
797a79a908fSAditya Kali 
798a79a908fSAditya Kali #endif /* !CONFIG_CGROUPS */
799a79a908fSAditya Kali 
get_cgroup_ns(struct cgroup_namespace * ns)800a79a908fSAditya Kali static inline void get_cgroup_ns(struct cgroup_namespace *ns)
801a79a908fSAditya Kali {
802a79a908fSAditya Kali 	if (ns)
803f387882dSKirill Tkhai 		refcount_inc(&ns->ns.count);
804a79a908fSAditya Kali }
805a79a908fSAditya Kali 
put_cgroup_ns(struct cgroup_namespace * ns)806a79a908fSAditya Kali static inline void put_cgroup_ns(struct cgroup_namespace *ns)
807a79a908fSAditya Kali {
808f387882dSKirill Tkhai 	if (ns && refcount_dec_and_test(&ns->ns.count))
809a79a908fSAditya Kali 		free_cgroup_ns(ns);
810a79a908fSAditya Kali }
811a79a908fSAditya Kali 
81276f969e8SRoman Gushchin #ifdef CONFIG_CGROUPS
81376f969e8SRoman Gushchin 
81476f969e8SRoman Gushchin void cgroup_enter_frozen(void);
81576f969e8SRoman Gushchin void cgroup_leave_frozen(bool always_leave);
81676f969e8SRoman Gushchin void cgroup_update_frozen(struct cgroup *cgrp);
81776f969e8SRoman Gushchin void cgroup_freeze(struct cgroup *cgrp, bool freeze);
81876f969e8SRoman Gushchin void cgroup_freezer_migrate_task(struct task_struct *task, struct cgroup *src,
81976f969e8SRoman Gushchin 				 struct cgroup *dst);
82096b9c592SRoman Gushchin 
cgroup_task_frozen(struct task_struct * task)82176f969e8SRoman Gushchin static inline bool cgroup_task_frozen(struct task_struct *task)
82276f969e8SRoman Gushchin {
82376f969e8SRoman Gushchin 	return task->frozen;
82476f969e8SRoman Gushchin }
82576f969e8SRoman Gushchin 
82676f969e8SRoman Gushchin #else /* !CONFIG_CGROUPS */
82776f969e8SRoman Gushchin 
cgroup_enter_frozen(void)82876f969e8SRoman Gushchin static inline void cgroup_enter_frozen(void) { }
cgroup_leave_frozen(bool always_leave)82976f969e8SRoman Gushchin static inline void cgroup_leave_frozen(bool always_leave) { }
cgroup_task_frozen(struct task_struct * task)83076f969e8SRoman Gushchin static inline bool cgroup_task_frozen(struct task_struct *task)
83176f969e8SRoman Gushchin {
83276f969e8SRoman Gushchin 	return false;
83376f969e8SRoman Gushchin }
83476f969e8SRoman Gushchin 
83576f969e8SRoman Gushchin #endif /* !CONFIG_CGROUPS */
83676f969e8SRoman Gushchin 
8374bfc0bb2SRoman Gushchin #ifdef CONFIG_CGROUP_BPF
cgroup_bpf_get(struct cgroup * cgrp)8384bfc0bb2SRoman Gushchin static inline void cgroup_bpf_get(struct cgroup *cgrp)
8394bfc0bb2SRoman Gushchin {
8404bfc0bb2SRoman Gushchin 	percpu_ref_get(&cgrp->bpf.refcnt);
8414bfc0bb2SRoman Gushchin }
8424bfc0bb2SRoman Gushchin 
cgroup_bpf_put(struct cgroup * cgrp)8434bfc0bb2SRoman Gushchin static inline void cgroup_bpf_put(struct cgroup *cgrp)
8444bfc0bb2SRoman Gushchin {
8454bfc0bb2SRoman Gushchin 	percpu_ref_put(&cgrp->bpf.refcnt);
8464bfc0bb2SRoman Gushchin }
8474bfc0bb2SRoman Gushchin 
8484bfc0bb2SRoman Gushchin #else /* CONFIG_CGROUP_BPF */
8494bfc0bb2SRoman Gushchin 
cgroup_bpf_get(struct cgroup * cgrp)8504bfc0bb2SRoman Gushchin static inline void cgroup_bpf_get(struct cgroup *cgrp) {}
cgroup_bpf_put(struct cgroup * cgrp)8514bfc0bb2SRoman Gushchin static inline void cgroup_bpf_put(struct cgroup *cgrp) {}
8524bfc0bb2SRoman Gushchin 
8534bfc0bb2SRoman Gushchin #endif /* CONFIG_CGROUP_BPF */
8544bfc0bb2SRoman Gushchin 
855aecd408bSYafang Shao struct cgroup *task_get_cgroup1(struct task_struct *tsk, int hierarchy_id);
856aecd408bSYafang Shao 
857c6f53ed8SDavid Finkel struct cgroup_of_peak *of_peak(struct kernfs_open_file *of);
858c6f53ed8SDavid Finkel 
859ddbcc7e8SPaul Menage #endif /* _LINUX_CGROUP_H */
860