xref: /openbmc/linux/include/linux/cgroup.h (revision d16b3af4)
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/cpumask.h>
14ddbcc7e8SPaul Menage #include <linux/nodemask.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 
32ddbcc7e8SPaul Menage #ifdef CONFIG_CGROUPS
33ddbcc7e8SPaul Menage 
346abc8ca1STejun Heo /*
3508b2b6fdSZhen Lei  * All weight knobs on the default hierarchy should use the following min,
366abc8ca1STejun Heo  * default and max values.  The default value is the logarithmic center of
376abc8ca1STejun Heo  * MIN and MAX and allows 100x to be expressed in both directions.
386abc8ca1STejun Heo  */
396abc8ca1STejun Heo #define CGROUP_WEIGHT_MIN		1
406abc8ca1STejun Heo #define CGROUP_WEIGHT_DFL		100
416abc8ca1STejun Heo #define CGROUP_WEIGHT_MAX		10000
426abc8ca1STejun Heo 
43bc2fb7edSTejun Heo /* walk only threadgroup leaders */
44bc2fb7edSTejun Heo #define CSS_TASK_ITER_PROCS		(1U << 0)
45450ee0c1STejun Heo /* walk all threaded css_sets in the domain */
46450ee0c1STejun Heo #define CSS_TASK_ITER_THREADED		(1U << 1)
47bc2fb7edSTejun Heo 
48b636fd38STejun Heo /* internal flags */
49b636fd38STejun Heo #define CSS_TASK_ITER_SKIPPED		(1U << 16)
50b636fd38STejun Heo 
51c326aa2bSTejun Heo /* a css_task_iter should be treated as an opaque object */
52c326aa2bSTejun Heo struct css_task_iter {
5372c97e54STejun Heo 	struct cgroup_subsys		*ss;
54bc2fb7edSTejun Heo 	unsigned int			flags;
5572c97e54STejun Heo 
56c326aa2bSTejun Heo 	struct list_head		*cset_pos;
57c326aa2bSTejun Heo 	struct list_head		*cset_head;
58ddbcc7e8SPaul Menage 
59450ee0c1STejun Heo 	struct list_head		*tcset_pos;
60450ee0c1STejun Heo 	struct list_head		*tcset_head;
61450ee0c1STejun Heo 
62c326aa2bSTejun Heo 	struct list_head		*task_pos;
63ed27b9f7STejun Heo 
649c974c77SMichal Koutný 	struct list_head		*cur_tasks_head;
65ed27b9f7STejun Heo 	struct css_set			*cur_cset;
66450ee0c1STejun Heo 	struct css_set			*cur_dcset;
67ed27b9f7STejun Heo 	struct task_struct		*cur_task;
68ed27b9f7STejun Heo 	struct list_head		iters_node;	/* css_set->task_iters */
69ddbcc7e8SPaul Menage };
70ddbcc7e8SPaul Menage 
71fbf83212STejun Heo extern struct file_system_type cgroup_fs_type;
72c326aa2bSTejun Heo extern struct cgroup_root cgrp_dfl_root;
73c326aa2bSTejun Heo extern struct css_set init_css_set;
74c326aa2bSTejun Heo 
75c326aa2bSTejun Heo #define SUBSYS(_x) extern struct cgroup_subsys _x ## _cgrp_subsys;
76c326aa2bSTejun Heo #include <linux/cgroup_subsys.h>
77c326aa2bSTejun Heo #undef SUBSYS
78c326aa2bSTejun Heo 
7949d1dc4bSTejun Heo #define SUBSYS(_x)								\
8049d1dc4bSTejun Heo 	extern struct static_key_true _x ## _cgrp_subsys_enabled_key;		\
8149d1dc4bSTejun Heo 	extern struct static_key_true _x ## _cgrp_subsys_on_dfl_key;
8249d1dc4bSTejun Heo #include <linux/cgroup_subsys.h>
8349d1dc4bSTejun Heo #undef SUBSYS
8449d1dc4bSTejun Heo 
8549d1dc4bSTejun Heo /**
8649d1dc4bSTejun Heo  * cgroup_subsys_enabled - fast test on whether a subsys is enabled
8749d1dc4bSTejun Heo  * @ss: subsystem in question
8849d1dc4bSTejun Heo  */
8949d1dc4bSTejun Heo #define cgroup_subsys_enabled(ss)						\
9049d1dc4bSTejun Heo 	static_branch_likely(&ss ## _enabled_key)
9149d1dc4bSTejun Heo 
9249d1dc4bSTejun Heo /**
9349d1dc4bSTejun Heo  * cgroup_subsys_on_dfl - fast test on whether a subsys is on default hierarchy
9449d1dc4bSTejun Heo  * @ss: subsystem in question
9549d1dc4bSTejun Heo  */
9649d1dc4bSTejun Heo #define cgroup_subsys_on_dfl(ss)						\
9749d1dc4bSTejun Heo 	static_branch_likely(&ss ## _on_dfl_key)
9849d1dc4bSTejun Heo 
99c326aa2bSTejun Heo bool css_has_online_children(struct cgroup_subsys_state *css);
100c326aa2bSTejun Heo struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss);
101fc5a828bSDennis Zhou struct cgroup_subsys_state *cgroup_e_css(struct cgroup *cgroup,
102fc5a828bSDennis Zhou 					 struct cgroup_subsys *ss);
103c326aa2bSTejun Heo struct cgroup_subsys_state *cgroup_get_e_css(struct cgroup *cgroup,
104c326aa2bSTejun Heo 					     struct cgroup_subsys *ss);
105c326aa2bSTejun Heo struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry,
106c326aa2bSTejun Heo 						       struct cgroup_subsys *ss);
107c326aa2bSTejun Heo 
10816af4396STejun Heo struct cgroup *cgroup_get_from_path(const char *path);
1091f3fe7ebSMartin KaFai Lau struct cgroup *cgroup_get_from_fd(int fd);
110a6d1ce59SYosry Ahmed struct cgroup *cgroup_v1v2_get_from_fd(int fd);
11116af4396STejun Heo 
112c326aa2bSTejun Heo int cgroup_attach_task_all(struct task_struct *from, struct task_struct *);
113c326aa2bSTejun Heo int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from);
114c326aa2bSTejun Heo 
115c326aa2bSTejun Heo int cgroup_add_dfl_cftypes(struct cgroup_subsys *ss, struct cftype *cfts);
116c326aa2bSTejun Heo int cgroup_add_legacy_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 {
34741c25707STejun Heo 	return !(css->flags & CSS_NO_REF) && percpu_ref_is_dying(&css->refcnt);
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
391f0d9a5f1STejun Heo extern spinlock_t css_set_lock;
392c326aa2bSTejun Heo #define task_css_set_check(task, __c)					\
393c326aa2bSTejun Heo 	rcu_dereference_check((task)->cgroups,				\
394f2aa197eSChengming Zhou 		rcu_read_lock_sched_held() ||				\
395c326aa2bSTejun Heo 		lockdep_is_held(&cgroup_mutex) ||			\
396f0d9a5f1STejun Heo 		lockdep_is_held(&css_set_lock) ||			\
397c326aa2bSTejun Heo 		((task)->flags & PF_EXITING) || (__c))
398c326aa2bSTejun Heo #else
399c326aa2bSTejun Heo #define task_css_set_check(task, __c)					\
400c326aa2bSTejun Heo 	rcu_dereference((task)->cgroups)
4012bd59d48STejun Heo #endif
402ddbcc7e8SPaul Menage 
403c326aa2bSTejun Heo /**
404c326aa2bSTejun Heo  * task_css_check - obtain css for (task, subsys) w/ extra access conds
405c326aa2bSTejun Heo  * @task: the target task
406c326aa2bSTejun Heo  * @subsys_id: the target subsystem ID
407c326aa2bSTejun Heo  * @__c: extra condition expression to be passed to rcu_dereference_check()
408c326aa2bSTejun Heo  *
409c326aa2bSTejun Heo  * Return the cgroup_subsys_state for the (@task, @subsys_id) pair.  The
410c326aa2bSTejun Heo  * synchronization rules are the same as task_css_set_check().
411c326aa2bSTejun Heo  */
412c326aa2bSTejun Heo #define task_css_check(task, subsys_id, __c)				\
413c326aa2bSTejun Heo 	task_css_set_check((task), (__c))->subsys[(subsys_id)]
414c326aa2bSTejun Heo 
415c326aa2bSTejun Heo /**
416c326aa2bSTejun Heo  * task_css_set - obtain a task's css_set
417c326aa2bSTejun Heo  * @task: the task to obtain css_set for
418c326aa2bSTejun Heo  *
419c326aa2bSTejun Heo  * See task_css_set_check().
420c326aa2bSTejun Heo  */
task_css_set(struct task_struct * task)421c326aa2bSTejun Heo static inline struct css_set *task_css_set(struct task_struct *task)
422c326aa2bSTejun Heo {
423c326aa2bSTejun Heo 	return task_css_set_check(task, false);
424c326aa2bSTejun Heo }
425c326aa2bSTejun Heo 
426c326aa2bSTejun Heo /**
427c326aa2bSTejun Heo  * task_css - obtain css for (task, subsys)
428c326aa2bSTejun Heo  * @task: the target task
429c326aa2bSTejun Heo  * @subsys_id: the target subsystem ID
430c326aa2bSTejun Heo  *
431c326aa2bSTejun Heo  * See task_css_check().
432c326aa2bSTejun Heo  */
task_css(struct task_struct * task,int subsys_id)433c326aa2bSTejun Heo static inline struct cgroup_subsys_state *task_css(struct task_struct *task,
434c326aa2bSTejun Heo 						   int subsys_id)
435c326aa2bSTejun Heo {
436c326aa2bSTejun Heo 	return task_css_check(task, subsys_id, false);
437c326aa2bSTejun Heo }
438c326aa2bSTejun Heo 
439c326aa2bSTejun Heo /**
440bbe179f8SLinus Torvalds  * task_get_css - find and get the css for (task, subsys)
441bbe179f8SLinus Torvalds  * @task: the target task
442bbe179f8SLinus Torvalds  * @subsys_id: the target subsystem ID
443bbe179f8SLinus Torvalds  *
444bbe179f8SLinus Torvalds  * Find the css for the (@task, @subsys_id) combination, increment a
445bbe179f8SLinus Torvalds  * reference on and return it.  This function is guaranteed to return a
44618fa84a2STejun Heo  * valid css.  The returned css may already have been offlined.
447bbe179f8SLinus Torvalds  */
448bbe179f8SLinus Torvalds static inline struct cgroup_subsys_state *
task_get_css(struct task_struct * task,int subsys_id)449bbe179f8SLinus Torvalds task_get_css(struct task_struct *task, int subsys_id)
450bbe179f8SLinus Torvalds {
451bbe179f8SLinus Torvalds 	struct cgroup_subsys_state *css;
452bbe179f8SLinus Torvalds 
453bbe179f8SLinus Torvalds 	rcu_read_lock();
454bbe179f8SLinus Torvalds 	while (true) {
455bbe179f8SLinus Torvalds 		css = task_css(task, subsys_id);
45618fa84a2STejun Heo 		/*
45718fa84a2STejun Heo 		 * Can't use css_tryget_online() here.  A task which has
45818fa84a2STejun Heo 		 * PF_EXITING set may stay associated with an offline css.
45918fa84a2STejun Heo 		 * If such task calls this function, css_tryget_online()
46018fa84a2STejun Heo 		 * will keep failing.
46118fa84a2STejun Heo 		 */
46218fa84a2STejun Heo 		if (likely(css_tryget(css)))
463bbe179f8SLinus Torvalds 			break;
464bbe179f8SLinus Torvalds 		cpu_relax();
465bbe179f8SLinus Torvalds 	}
466bbe179f8SLinus Torvalds 	rcu_read_unlock();
467bbe179f8SLinus Torvalds 	return css;
468bbe179f8SLinus Torvalds }
469bbe179f8SLinus Torvalds 
470bbe179f8SLinus Torvalds /**
471c326aa2bSTejun Heo  * task_css_is_root - test whether a task belongs to the root css
472c326aa2bSTejun Heo  * @task: the target task
473c326aa2bSTejun Heo  * @subsys_id: the target subsystem ID
474c326aa2bSTejun Heo  *
475c326aa2bSTejun Heo  * Test whether @task belongs to the root css on the specified subsystem.
476c326aa2bSTejun Heo  * May be invoked in any context.
477c326aa2bSTejun Heo  */
task_css_is_root(struct task_struct * task,int subsys_id)478c326aa2bSTejun Heo static inline bool task_css_is_root(struct task_struct *task, int subsys_id)
479c326aa2bSTejun Heo {
480c326aa2bSTejun Heo 	return task_css_check(task, subsys_id, true) ==
481c326aa2bSTejun Heo 		init_css_set.subsys[subsys_id];
482c326aa2bSTejun Heo }
483c326aa2bSTejun Heo 
task_cgroup(struct task_struct * task,int subsys_id)484c326aa2bSTejun Heo static inline struct cgroup *task_cgroup(struct task_struct *task,
485c326aa2bSTejun Heo 					 int subsys_id)
486c326aa2bSTejun Heo {
487c326aa2bSTejun Heo 	return task_css(task, subsys_id)->cgroup;
488c326aa2bSTejun Heo }
489a2dd4247STejun Heo 
task_dfl_cgroup(struct task_struct * task)4903e48930cSTejun Heo static inline struct cgroup *task_dfl_cgroup(struct task_struct *task)
4913e48930cSTejun Heo {
4923e48930cSTejun Heo 	return task_css_set(task)->dfl_cgrp;
4933e48930cSTejun Heo }
4943e48930cSTejun Heo 
cgroup_parent(struct cgroup * cgrp)4953e48930cSTejun Heo static inline struct cgroup *cgroup_parent(struct cgroup *cgrp)
4963e48930cSTejun Heo {
4973e48930cSTejun Heo 	struct cgroup_subsys_state *parent_css = cgrp->self.parent;
4983e48930cSTejun Heo 
4993e48930cSTejun Heo 	if (parent_css)
5003e48930cSTejun Heo 		return container_of(parent_css, struct cgroup, self);
5013e48930cSTejun Heo 	return NULL;
5023e48930cSTejun Heo }
5033e48930cSTejun Heo 
504b11cfb58STejun Heo /**
505b11cfb58STejun Heo  * cgroup_is_descendant - test ancestry
506b11cfb58STejun Heo  * @cgrp: the cgroup to be tested
507b11cfb58STejun Heo  * @ancestor: possible ancestor of @cgrp
508b11cfb58STejun Heo  *
509b11cfb58STejun Heo  * Test whether @cgrp is a descendant of @ancestor.  It also returns %true
510b11cfb58STejun Heo  * if @cgrp == @ancestor.  This function is safe to call as long as @cgrp
511b11cfb58STejun Heo  * and @ancestor are accessible.
512b11cfb58STejun Heo  */
cgroup_is_descendant(struct cgroup * cgrp,struct cgroup * ancestor)513b11cfb58STejun Heo static inline bool cgroup_is_descendant(struct cgroup *cgrp,
514b11cfb58STejun Heo 					struct cgroup *ancestor)
515b11cfb58STejun Heo {
516b11cfb58STejun Heo 	if (cgrp->root != ancestor->root || cgrp->level < ancestor->level)
517b11cfb58STejun Heo 		return false;
5187f203bc8STejun Heo 	return cgrp->ancestors[ancestor->level] == ancestor;
519b11cfb58STejun Heo }
520b11cfb58STejun Heo 
521aed704b7SSargun Dhillon /**
52277236281SAndrey Ignatov  * cgroup_ancestor - find ancestor of cgroup
52377236281SAndrey Ignatov  * @cgrp: cgroup to find ancestor of
52477236281SAndrey Ignatov  * @ancestor_level: level of ancestor to find starting from root
52577236281SAndrey Ignatov  *
52677236281SAndrey Ignatov  * Find ancestor of cgroup at specified level starting from root if it exists
52777236281SAndrey Ignatov  * and return pointer to it. Return NULL if @cgrp doesn't have ancestor at
52877236281SAndrey Ignatov  * @ancestor_level.
52977236281SAndrey Ignatov  *
53077236281SAndrey Ignatov  * This function is safe to call as long as @cgrp is accessible.
53177236281SAndrey Ignatov  */
cgroup_ancestor(struct cgroup * cgrp,int ancestor_level)53277236281SAndrey Ignatov static inline struct cgroup *cgroup_ancestor(struct cgroup *cgrp,
53377236281SAndrey Ignatov 					     int ancestor_level)
53477236281SAndrey Ignatov {
5357f203bc8STejun Heo 	if (ancestor_level < 0 || ancestor_level > cgrp->level)
53677236281SAndrey Ignatov 		return NULL;
5377f203bc8STejun Heo 	return cgrp->ancestors[ancestor_level];
53877236281SAndrey Ignatov }
53977236281SAndrey Ignatov 
54077236281SAndrey Ignatov /**
541aed704b7SSargun Dhillon  * task_under_cgroup_hierarchy - test task's membership of cgroup ancestry
542aed704b7SSargun Dhillon  * @task: the task to be tested
543aed704b7SSargun Dhillon  * @ancestor: possible ancestor of @task's cgroup
544aed704b7SSargun Dhillon  *
545aed704b7SSargun Dhillon  * Tests whether @task's default cgroup hierarchy is a descendant of @ancestor.
546aed704b7SSargun Dhillon  * It follows all the same rules as cgroup_is_descendant, and only applies
547aed704b7SSargun Dhillon  * to the default hierarchy.
548aed704b7SSargun Dhillon  */
task_under_cgroup_hierarchy(struct task_struct * task,struct cgroup * ancestor)549aed704b7SSargun Dhillon static inline bool task_under_cgroup_hierarchy(struct task_struct *task,
550aed704b7SSargun Dhillon 					       struct cgroup *ancestor)
551aed704b7SSargun Dhillon {
552aed704b7SSargun Dhillon 	struct css_set *cset = task_css_set(task);
553aed704b7SSargun Dhillon 
554aed704b7SSargun Dhillon 	return cgroup_is_descendant(cset->dfl_cgrp, ancestor);
555aed704b7SSargun Dhillon }
556aed704b7SSargun Dhillon 
55707bc356eSTejun Heo /* no synchronization, the result can only be used as a hint */
cgroup_is_populated(struct cgroup * cgrp)55827bd4dbbSTejun Heo static inline bool cgroup_is_populated(struct cgroup *cgrp)
55907bc356eSTejun Heo {
560454000adSTejun Heo 	return cgrp->nr_populated_csets + cgrp->nr_populated_domain_children +
561454000adSTejun Heo 		cgrp->nr_populated_threaded_children;
56207bc356eSTejun Heo }
56307bc356eSTejun Heo 
564f29374b1SZefan Li /* returns ino associated with a cgroup */
cgroup_ino(struct cgroup * cgrp)565b1664924STejun Heo static inline ino_t cgroup_ino(struct cgroup *cgrp)
566b1664924STejun Heo {
56767c0496eSTejun Heo 	return kernfs_ino(cgrp->kn);
568b1664924STejun Heo }
569b1664924STejun Heo 
570b4168640STejun Heo /* cft/css accessors for cftype->write() operation */
of_cft(struct kernfs_open_file * of)571b4168640STejun Heo static inline struct cftype *of_cft(struct kernfs_open_file *of)
5727da11279STejun Heo {
5732bd59d48STejun Heo 	return of->kn->priv;
5747da11279STejun Heo }
5757da11279STejun Heo 
576b4168640STejun Heo struct cgroup_subsys_state *of_css(struct kernfs_open_file *of);
577b4168640STejun Heo 
578b4168640STejun Heo /* cft/css accessors for cftype->seq_*() operations */
seq_cft(struct seq_file * seq)579b4168640STejun Heo static inline struct cftype *seq_cft(struct seq_file *seq)
580b4168640STejun Heo {
581b4168640STejun Heo 	return of_cft(seq->private);
582b4168640STejun Heo }
583b4168640STejun Heo 
seq_css(struct seq_file * seq)584b4168640STejun Heo static inline struct cgroup_subsys_state *seq_css(struct seq_file *seq)
585b4168640STejun Heo {
586b4168640STejun Heo 	return of_css(seq->private);
587b4168640STejun Heo }
58859f5296bSTejun Heo 
589e61734c5STejun Heo /*
590e61734c5STejun Heo  * Name / path handling functions.  All are thin wrappers around the kernfs
591e61734c5STejun Heo  * counterparts and can be called under any context.
592e61734c5STejun Heo  */
593e61734c5STejun Heo 
cgroup_name(struct cgroup * cgrp,char * buf,size_t buflen)594e61734c5STejun Heo static inline int cgroup_name(struct cgroup *cgrp, char *buf, size_t buflen)
595e61734c5STejun Heo {
596e61734c5STejun Heo 	return kernfs_name(cgrp->kn, buf, buflen);
597e61734c5STejun Heo }
598e61734c5STejun Heo 
cgroup_path(struct cgroup * cgrp,char * buf,size_t buflen)5994c737b41STejun Heo static inline int cgroup_path(struct cgroup *cgrp, char *buf, size_t buflen)
600e61734c5STejun Heo {
601e61734c5STejun Heo 	return kernfs_path(cgrp->kn, buf, buflen);
602e61734c5STejun Heo }
603e61734c5STejun Heo 
pr_cont_cgroup_name(struct cgroup * cgrp)604e61734c5STejun Heo static inline void pr_cont_cgroup_name(struct cgroup *cgrp)
605e61734c5STejun Heo {
606e61734c5STejun Heo 	pr_cont_kernfs_name(cgrp->kn);
607e61734c5STejun Heo }
608e61734c5STejun Heo 
pr_cont_cgroup_path(struct cgroup * cgrp)609e61734c5STejun Heo static inline void pr_cont_cgroup_path(struct cgroup *cgrp)
610e61734c5STejun Heo {
611e61734c5STejun Heo 	pr_cont_kernfs_path(cgrp->kn);
612e61734c5STejun Heo }
613e61734c5STejun Heo 
6143958e2d0SSuren Baghdasaryan bool cgroup_psi_enabled(void);
6153958e2d0SSuren Baghdasaryan 
cgroup_init_kthreadd(void)61677f88796STejun Heo static inline void cgroup_init_kthreadd(void)
61777f88796STejun Heo {
61877f88796STejun Heo 	/*
61977f88796STejun Heo 	 * kthreadd is inherited by all kthreads, keep it in the root so
62077f88796STejun Heo 	 * that the new kthreads are guaranteed to stay in the root until
62177f88796STejun Heo 	 * initialization is finished.
62277f88796STejun Heo 	 */
62377f88796STejun Heo 	current->no_cgroup_migration = 1;
62477f88796STejun Heo }
62577f88796STejun Heo 
cgroup_kthread_ready(void)62677f88796STejun Heo static inline void cgroup_kthread_ready(void)
62777f88796STejun Heo {
62877f88796STejun Heo 	/*
62977f88796STejun Heo 	 * This kthread finished initialization.  The creator should have
63077f88796STejun Heo 	 * set PF_NO_SETAFFINITY if this kthread should stay in the root.
63177f88796STejun Heo 	 */
63277f88796STejun Heo 	current->no_cgroup_migration = 0;
63377f88796STejun Heo }
63477f88796STejun Heo 
63567c0496eSTejun Heo void cgroup_path_from_kernfs_id(u64 id, char *buf, size_t buflen);
6366b658c48SMuneendra Kumar struct cgroup *cgroup_get_from_id(u64 id);
637ddbcc7e8SPaul Menage #else /* !CONFIG_CGROUPS */
638ddbcc7e8SPaul Menage 
639f3ba5380STejun Heo struct cgroup_subsys_state;
640aed704b7SSargun Dhillon struct cgroup;
641f3ba5380STejun Heo 
cgroup_id(const struct cgroup * cgrp)642a3b89864SPeter Zijlstra static inline u64 cgroup_id(const struct cgroup *cgrp) { return 1; }
css_get(struct cgroup_subsys_state * css)6439b0eb69bSTejun Heo static inline void css_get(struct cgroup_subsys_state *css) {}
css_put(struct cgroup_subsys_state * css)644c326aa2bSTejun Heo static inline void css_put(struct cgroup_subsys_state *css) {}
cgroup_lock(void)645354ed597SYu Zhao static inline void cgroup_lock(void) {}
cgroup_unlock(void)646354ed597SYu Zhao static inline void cgroup_unlock(void) {}
cgroup_attach_task_all(struct task_struct * from,struct task_struct * t)647c326aa2bSTejun Heo static inline int cgroup_attach_task_all(struct task_struct *from,
648c326aa2bSTejun Heo 					 struct task_struct *t) { return 0; }
cgroupstats_build(struct cgroupstats * stats,struct dentry * dentry)649c326aa2bSTejun Heo static inline int cgroupstats_build(struct cgroupstats *stats,
650c326aa2bSTejun Heo 				    struct dentry *dentry) { return -EINVAL; }
651c326aa2bSTejun Heo 
cgroup_fork(struct task_struct * p)652b4f48b63SPaul Menage static inline void cgroup_fork(struct task_struct *p) {}
cgroup_can_fork(struct task_struct * p,struct kernel_clone_args * kargs)653ef2c41cfSChristian Brauner static inline int cgroup_can_fork(struct task_struct *p,
654ef2c41cfSChristian Brauner 				  struct kernel_clone_args *kargs) { return 0; }
cgroup_cancel_fork(struct task_struct * p,struct kernel_clone_args * kargs)655ef2c41cfSChristian Brauner static inline void cgroup_cancel_fork(struct task_struct *p,
656ef2c41cfSChristian Brauner 				      struct kernel_clone_args *kargs) {}
cgroup_post_fork(struct task_struct * p,struct kernel_clone_args * kargs)657ef2c41cfSChristian Brauner static inline void cgroup_post_fork(struct task_struct *p,
658ef2c41cfSChristian Brauner 				    struct kernel_clone_args *kargs) {}
cgroup_exit(struct task_struct * p)6591ec41830SLi Zefan static inline void cgroup_exit(struct task_struct *p) {}
cgroup_release(struct task_struct * p)66051bee5abSOleg Nesterov static inline void cgroup_release(struct task_struct *p) {}
cgroup_free(struct task_struct * p)6612e91fa7fSTejun Heo static inline void cgroup_free(struct task_struct *p) {}
662ddbcc7e8SPaul Menage 
cgroup_init_early(void)663c326aa2bSTejun Heo static inline int cgroup_init_early(void) { return 0; }
cgroup_init(void)664c326aa2bSTejun Heo static inline int cgroup_init(void) { return 0; }
cgroup_init_kthreadd(void)66577f88796STejun Heo static inline void cgroup_init_kthreadd(void) {}
cgroup_kthread_ready(void)66677f88796STejun Heo static inline void cgroup_kthread_ready(void) {}
667d7926ee3SSridhar Samudrala 
cgroup_parent(struct cgroup * cgrp)6682ce7135aSJohannes Weiner static inline struct cgroup *cgroup_parent(struct cgroup *cgrp)
6692ce7135aSJohannes Weiner {
6702ce7135aSJohannes Weiner 	return NULL;
6712ce7135aSJohannes Weiner }
6722ce7135aSJohannes Weiner 
cgroup_psi_enabled(void)6733958e2d0SSuren Baghdasaryan static inline bool cgroup_psi_enabled(void)
6743958e2d0SSuren Baghdasaryan {
6753958e2d0SSuren Baghdasaryan 	return false;
6763958e2d0SSuren Baghdasaryan }
6773958e2d0SSuren Baghdasaryan 
task_under_cgroup_hierarchy(struct task_struct * task,struct cgroup * ancestor)678aed704b7SSargun Dhillon static inline bool task_under_cgroup_hierarchy(struct task_struct *task,
679aed704b7SSargun Dhillon 					       struct cgroup *ancestor)
680aed704b7SSargun Dhillon {
681aed704b7SSargun Dhillon 	return true;
682aed704b7SSargun Dhillon }
68369fd5c39SShaohua Li 
cgroup_path_from_kernfs_id(u64 id,char * buf,size_t buflen)68467c0496eSTejun Heo static inline void cgroup_path_from_kernfs_id(u64 id, char *buf, size_t buflen)
68567c0496eSTejun Heo {}
686ddbcc7e8SPaul Menage #endif /* !CONFIG_CGROUPS */
687ddbcc7e8SPaul Menage 
6886162cef0STejun Heo #ifdef CONFIG_CGROUPS
6896162cef0STejun Heo /*
6906162cef0STejun Heo  * cgroup scalable recursive statistics.
6916162cef0STejun Heo  */
6926162cef0STejun Heo void cgroup_rstat_updated(struct cgroup *cgrp, int cpu);
6936162cef0STejun Heo void cgroup_rstat_flush(struct cgroup *cgrp);
694*8bff9a04SYosry Ahmed void cgroup_rstat_flush_hold(struct cgroup *cgrp);
6956162cef0STejun Heo void cgroup_rstat_flush_release(void);
6966162cef0STejun Heo 
6976162cef0STejun Heo /*
698bd1060a1STejun Heo  * Basic resource stats.
699d2cc5ed6STejun Heo  */
700d2cc5ed6STejun Heo #ifdef CONFIG_CGROUP_CPUACCT
701d2cc5ed6STejun Heo void cpuacct_charge(struct task_struct *tsk, u64 cputime);
702d2cc5ed6STejun Heo void cpuacct_account_field(struct task_struct *tsk, int index, u64 val);
703d2cc5ed6STejun Heo #else
cpuacct_charge(struct task_struct * tsk,u64 cputime)704d2cc5ed6STejun Heo static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime) {}
cpuacct_account_field(struct task_struct * tsk,int index,u64 val)705d2cc5ed6STejun Heo static inline void cpuacct_account_field(struct task_struct *tsk, int index,
706d2cc5ed6STejun Heo 					 u64 val) {}
707d2cc5ed6STejun Heo #endif
708d2cc5ed6STejun Heo 
709d2cc5ed6STejun Heo void __cgroup_account_cputime(struct cgroup *cgrp, u64 delta_exec);
710041cd640STejun Heo void __cgroup_account_cputime_field(struct cgroup *cgrp,
711041cd640STejun Heo 				    enum cpu_usage_stat index, u64 delta_exec);
712041cd640STejun Heo 
cgroup_account_cputime(struct task_struct * task,u64 delta_exec)713041cd640STejun Heo static inline void cgroup_account_cputime(struct task_struct *task,
714d2cc5ed6STejun Heo 					  u64 delta_exec)
715d2cc5ed6STejun Heo {
716d2cc5ed6STejun Heo 	struct cgroup *cgrp;
717041cd640STejun Heo 
718041cd640STejun Heo 	cpuacct_charge(task, delta_exec);
719d2cc5ed6STejun Heo 
720041cd640STejun Heo 	cgrp = task_dfl_cgroup(task);
721041cd640STejun Heo 	if (cgroup_parent(cgrp))
722041cd640STejun Heo 		__cgroup_account_cputime(cgrp, delta_exec);
723041cd640STejun Heo }
724d2cc5ed6STejun Heo 
cgroup_account_cputime_field(struct task_struct * task,enum cpu_usage_stat index,u64 delta_exec)725d2cc5ed6STejun Heo static inline void cgroup_account_cputime_field(struct task_struct *task,
726d2cc5ed6STejun Heo 						enum cpu_usage_stat index,
727d2cc5ed6STejun Heo 						u64 delta_exec)
728d2cc5ed6STejun Heo {
729d2cc5ed6STejun Heo 	struct cgroup *cgrp;
730041cd640STejun Heo 
731041cd640STejun Heo 	cpuacct_account_field(task, index, delta_exec);
732d2cc5ed6STejun Heo 
733041cd640STejun Heo 	cgrp = task_dfl_cgroup(task);
734041cd640STejun Heo 	if (cgroup_parent(cgrp))
735041cd640STejun Heo 		__cgroup_account_cputime_field(cgrp, index, delta_exec);
736041cd640STejun Heo }
737d2cc5ed6STejun Heo 
738d2cc5ed6STejun Heo #else	/* CONFIG_CGROUPS */
739d2cc5ed6STejun Heo 
cgroup_account_cputime(struct task_struct * task,u64 delta_exec)740d2cc5ed6STejun Heo static inline void cgroup_account_cputime(struct task_struct *task,
741d2cc5ed6STejun Heo 					  u64 delta_exec) {}
cgroup_account_cputime_field(struct task_struct * task,enum cpu_usage_stat index,u64 delta_exec)742d2cc5ed6STejun Heo static inline void cgroup_account_cputime_field(struct task_struct *task,
743d2cc5ed6STejun Heo 						enum cpu_usage_stat index,
744d2cc5ed6STejun Heo 						u64 delta_exec) {}
745d2cc5ed6STejun Heo 
746d2cc5ed6STejun Heo #endif	/* CONFIG_CGROUPS */
747d2cc5ed6STejun Heo 
748d2cc5ed6STejun Heo /*
749d2cc5ed6STejun Heo  * sock->sk_cgrp_data handling.  For more info, see sock_cgroup_data
750bd1060a1STejun Heo  * definition in cgroup-defs.h.
751bd1060a1STejun Heo  */
752bd1060a1STejun Heo #ifdef CONFIG_SOCK_CGROUP_DATA
753bd1060a1STejun Heo 
754bd1060a1STejun Heo void cgroup_sk_alloc(struct sock_cgroup_data *skcd);
755bd1060a1STejun Heo void cgroup_sk_clone(struct sock_cgroup_data *skcd);
756ad0f75e5SCong Wang void cgroup_sk_free(struct sock_cgroup_data *skcd);
757bd1060a1STejun Heo 
sock_cgroup_ptr(struct sock_cgroup_data * skcd)758bd1060a1STejun Heo static inline struct cgroup *sock_cgroup_ptr(struct sock_cgroup_data *skcd)
759bd1060a1STejun Heo {
760bd1060a1STejun Heo 	return skcd->cgroup;
7618520e224SDaniel Borkmann }
762bd1060a1STejun Heo 
763bd1060a1STejun Heo #else	/* CONFIG_CGROUP_DATA */
764bd1060a1STejun Heo 
cgroup_sk_alloc(struct sock_cgroup_data * skcd)765bd1060a1STejun Heo static inline void cgroup_sk_alloc(struct sock_cgroup_data *skcd) {}
cgroup_sk_clone(struct sock_cgroup_data * skcd)766bd1060a1STejun Heo static inline void cgroup_sk_clone(struct sock_cgroup_data *skcd) {}
cgroup_sk_free(struct sock_cgroup_data * skcd)767ad0f75e5SCong Wang static inline void cgroup_sk_free(struct sock_cgroup_data *skcd) {}
768bd1060a1STejun Heo 
769bd1060a1STejun Heo #endif	/* CONFIG_CGROUP_DATA */
770bd1060a1STejun Heo 
771bd1060a1STejun Heo struct cgroup_namespace {
772a79a908fSAditya Kali 	struct ns_common	ns;
773a79a908fSAditya Kali 	struct user_namespace	*user_ns;
774a79a908fSAditya Kali 	struct ucounts		*ucounts;
775d08311ddSEric W. Biederman 	struct css_set          *root_cset;
776a79a908fSAditya Kali };
777a79a908fSAditya Kali 
778a79a908fSAditya Kali extern struct cgroup_namespace init_cgroup_ns;
779a79a908fSAditya Kali 
780a79a908fSAditya Kali #ifdef CONFIG_CGROUPS
781a79a908fSAditya Kali 
782a79a908fSAditya Kali void free_cgroup_ns(struct cgroup_namespace *ns);
783a79a908fSAditya Kali 
784a79a908fSAditya Kali struct cgroup_namespace *copy_cgroup_ns(unsigned long flags,
785a79a908fSAditya Kali 					struct user_namespace *user_ns,
786a79a908fSAditya Kali 					struct cgroup_namespace *old_ns);
787a79a908fSAditya Kali 
788a79a908fSAditya Kali int cgroup_path_ns(struct cgroup *cgrp, char *buf, size_t buflen,
7894c737b41STejun Heo 		   struct cgroup_namespace *ns);
790a79a908fSAditya Kali 
791a79a908fSAditya Kali #else /* !CONFIG_CGROUPS */
792a79a908fSAditya Kali 
free_cgroup_ns(struct cgroup_namespace * ns)793a79a908fSAditya Kali static inline void free_cgroup_ns(struct cgroup_namespace *ns) { }
794a79a908fSAditya Kali static inline struct cgroup_namespace *
copy_cgroup_ns(unsigned long flags,struct user_namespace * user_ns,struct cgroup_namespace * old_ns)795a79a908fSAditya Kali copy_cgroup_ns(unsigned long flags, struct user_namespace *user_ns,
796a79a908fSAditya Kali 	       struct cgroup_namespace *old_ns)
797a79a908fSAditya Kali {
798a79a908fSAditya Kali 	return old_ns;
799a79a908fSAditya Kali }
800a79a908fSAditya Kali 
801a79a908fSAditya Kali #endif /* !CONFIG_CGROUPS */
802a79a908fSAditya Kali 
get_cgroup_ns(struct cgroup_namespace * ns)803a79a908fSAditya Kali static inline void get_cgroup_ns(struct cgroup_namespace *ns)
804a79a908fSAditya Kali {
805a79a908fSAditya Kali 	if (ns)
806a79a908fSAditya Kali 		refcount_inc(&ns->ns.count);
807f387882dSKirill Tkhai }
808a79a908fSAditya Kali 
put_cgroup_ns(struct cgroup_namespace * ns)809a79a908fSAditya Kali static inline void put_cgroup_ns(struct cgroup_namespace *ns)
810a79a908fSAditya Kali {
811a79a908fSAditya Kali 	if (ns && refcount_dec_and_test(&ns->ns.count))
812f387882dSKirill Tkhai 		free_cgroup_ns(ns);
813a79a908fSAditya Kali }
814a79a908fSAditya Kali 
815a79a908fSAditya Kali #ifdef CONFIG_CGROUPS
81676f969e8SRoman Gushchin 
81776f969e8SRoman Gushchin void cgroup_enter_frozen(void);
81876f969e8SRoman Gushchin void cgroup_leave_frozen(bool always_leave);
81976f969e8SRoman Gushchin void cgroup_update_frozen(struct cgroup *cgrp);
82076f969e8SRoman Gushchin void cgroup_freeze(struct cgroup *cgrp, bool freeze);
82176f969e8SRoman Gushchin void cgroup_freezer_migrate_task(struct task_struct *task, struct cgroup *src,
82276f969e8SRoman Gushchin 				 struct cgroup *dst);
82376f969e8SRoman Gushchin 
cgroup_task_frozen(struct task_struct * task)82496b9c592SRoman Gushchin static inline bool cgroup_task_frozen(struct task_struct *task)
82576f969e8SRoman Gushchin {
82676f969e8SRoman Gushchin 	return task->frozen;
82776f969e8SRoman Gushchin }
82876f969e8SRoman Gushchin 
82976f969e8SRoman Gushchin #else /* !CONFIG_CGROUPS */
83076f969e8SRoman Gushchin 
cgroup_enter_frozen(void)83176f969e8SRoman Gushchin static inline void cgroup_enter_frozen(void) { }
cgroup_leave_frozen(bool always_leave)83276f969e8SRoman Gushchin static inline void cgroup_leave_frozen(bool always_leave) { }
cgroup_task_frozen(struct task_struct * task)83376f969e8SRoman Gushchin static inline bool cgroup_task_frozen(struct task_struct *task)
83476f969e8SRoman Gushchin {
83576f969e8SRoman Gushchin 	return false;
83676f969e8SRoman Gushchin }
83776f969e8SRoman Gushchin 
83876f969e8SRoman Gushchin #endif /* !CONFIG_CGROUPS */
83976f969e8SRoman Gushchin 
84076f969e8SRoman Gushchin #ifdef CONFIG_CGROUP_BPF
cgroup_bpf_get(struct cgroup * cgrp)8414bfc0bb2SRoman Gushchin static inline void cgroup_bpf_get(struct cgroup *cgrp)
8424bfc0bb2SRoman Gushchin {
8434bfc0bb2SRoman Gushchin 	percpu_ref_get(&cgrp->bpf.refcnt);
8444bfc0bb2SRoman Gushchin }
8454bfc0bb2SRoman Gushchin 
cgroup_bpf_put(struct cgroup * cgrp)8464bfc0bb2SRoman Gushchin static inline void cgroup_bpf_put(struct cgroup *cgrp)
8474bfc0bb2SRoman Gushchin {
8484bfc0bb2SRoman Gushchin 	percpu_ref_put(&cgrp->bpf.refcnt);
8494bfc0bb2SRoman Gushchin }
8504bfc0bb2SRoman Gushchin 
8514bfc0bb2SRoman Gushchin #else /* CONFIG_CGROUP_BPF */
8524bfc0bb2SRoman Gushchin 
cgroup_bpf_get(struct cgroup * cgrp)8534bfc0bb2SRoman Gushchin static inline void cgroup_bpf_get(struct cgroup *cgrp) {}
cgroup_bpf_put(struct cgroup * cgrp)8544bfc0bb2SRoman Gushchin static inline void cgroup_bpf_put(struct cgroup *cgrp) {}
8554bfc0bb2SRoman Gushchin 
8564bfc0bb2SRoman Gushchin #endif /* CONFIG_CGROUP_BPF */
8574bfc0bb2SRoman Gushchin 
8584bfc0bb2SRoman Gushchin #endif /* _LINUX_CGROUP_H */
859ddbcc7e8SPaul Menage