• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_CGROUP_H
3 #define _LINUX_CGROUP_H
4 /*
5  *  cgroup interface
6  *
7  *  Copyright (C) 2003 BULL SA
8  *  Copyright (C) 2004-2006 Silicon Graphics, Inc.
9  *
10  */
11 
12 #include <linux/sched.h>
13 #include <linux/cpumask.h>
14 #include <linux/nodemask.h>
15 #include <linux/rculist.h>
16 #include <linux/cgroupstats.h>
17 #include <linux/fs.h>
18 #include <linux/seq_file.h>
19 #include <linux/kernfs.h>
20 #include <linux/jump_label.h>
21 #include <linux/types.h>
22 #include <linux/ns_common.h>
23 #include <linux/nsproxy.h>
24 #include <linux/user_namespace.h>
25 #include <linux/refcount.h>
26 #include <linux/kernel_stat.h>
27 #include <linux/android_kabi.h>
28 
29 #include <linux/cgroup-defs.h>
30 
31 struct kernel_clone_args;
32 
33 #ifdef CONFIG_CGROUPS
34 
35 /*
36  * All weight knobs on the default hierarchy should use the following min,
37  * default and max values.  The default value is the logarithmic center of
38  * MIN and MAX and allows 100x to be expressed in both directions.
39  */
40 #define CGROUP_WEIGHT_MIN		1
41 #define CGROUP_WEIGHT_DFL		100
42 #define CGROUP_WEIGHT_MAX		10000
43 
44 /* walk only threadgroup leaders */
45 #define CSS_TASK_ITER_PROCS		(1U << 0)
46 /* walk all threaded css_sets in the domain */
47 #define CSS_TASK_ITER_THREADED		(1U << 1)
48 
49 /* internal flags */
50 #define CSS_TASK_ITER_SKIPPED		(1U << 16)
51 
52 /* a css_task_iter should be treated as an opaque object */
53 struct css_task_iter {
54 	struct cgroup_subsys		*ss;
55 	unsigned int			flags;
56 
57 	struct list_head		*cset_pos;
58 	struct list_head		*cset_head;
59 
60 	struct list_head		*tcset_pos;
61 	struct list_head		*tcset_head;
62 
63 	struct list_head		*task_pos;
64 
65 	struct list_head		*cur_tasks_head;
66 	struct css_set			*cur_cset;
67 	struct css_set			*cur_dcset;
68 	struct task_struct		*cur_task;
69 	struct list_head		iters_node;	/* css_set->task_iters */
70 
71 	ANDROID_KABI_RESERVE(1);
72 };
73 
74 extern struct file_system_type cgroup_fs_type;
75 extern struct cgroup_root cgrp_dfl_root;
76 extern struct css_set init_css_set;
77 
78 #define SUBSYS(_x) extern struct cgroup_subsys _x ## _cgrp_subsys;
79 #include <linux/cgroup_subsys.h>
80 #undef SUBSYS
81 
82 #define SUBSYS(_x)								\
83 	extern struct static_key_true _x ## _cgrp_subsys_enabled_key;		\
84 	extern struct static_key_true _x ## _cgrp_subsys_on_dfl_key;
85 #include <linux/cgroup_subsys.h>
86 #undef SUBSYS
87 
88 /**
89  * cgroup_subsys_enabled - fast test on whether a subsys is enabled
90  * @ss: subsystem in question
91  */
92 #define cgroup_subsys_enabled(ss)						\
93 	static_branch_likely(&ss ## _enabled_key)
94 
95 /**
96  * cgroup_subsys_on_dfl - fast test on whether a subsys is on default hierarchy
97  * @ss: subsystem in question
98  */
99 #define cgroup_subsys_on_dfl(ss)						\
100 	static_branch_likely(&ss ## _on_dfl_key)
101 
102 bool css_has_online_children(struct cgroup_subsys_state *css);
103 struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss);
104 struct cgroup_subsys_state *cgroup_e_css(struct cgroup *cgroup,
105 					 struct cgroup_subsys *ss);
106 struct cgroup_subsys_state *cgroup_get_e_css(struct cgroup *cgroup,
107 					     struct cgroup_subsys *ss);
108 struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry,
109 						       struct cgroup_subsys *ss);
110 
111 struct cgroup *cgroup_get_from_path(const char *path);
112 struct cgroup *cgroup_get_from_fd(int fd);
113 
114 int cgroup_attach_task_all(struct task_struct *from, struct task_struct *);
115 int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from);
116 
117 int cgroup_add_dfl_cftypes(struct cgroup_subsys *ss, struct cftype *cfts);
118 int cgroup_add_legacy_cftypes(struct cgroup_subsys *ss, struct cftype *cfts);
119 int cgroup_rm_cftypes(struct cftype *cfts);
120 void cgroup_file_notify(struct cgroup_file *cfile);
121 
122 int task_cgroup_path(struct task_struct *task, char *buf, size_t buflen);
123 int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry);
124 int proc_cgroup_show(struct seq_file *m, struct pid_namespace *ns,
125 		     struct pid *pid, struct task_struct *tsk);
126 
127 void cgroup_fork(struct task_struct *p);
128 extern int cgroup_can_fork(struct task_struct *p,
129 			   struct kernel_clone_args *kargs);
130 extern void cgroup_cancel_fork(struct task_struct *p,
131 			       struct kernel_clone_args *kargs);
132 extern void cgroup_post_fork(struct task_struct *p,
133 			     struct kernel_clone_args *kargs);
134 void cgroup_exit(struct task_struct *p);
135 void cgroup_release(struct task_struct *p);
136 void cgroup_free(struct task_struct *p);
137 
138 int cgroup_init_early(void);
139 int cgroup_init(void);
140 
141 int cgroup_parse_float(const char *input, unsigned dec_shift, s64 *v);
142 
143 /*
144  * Iteration helpers and macros.
145  */
146 
147 struct cgroup_subsys_state *css_next_child(struct cgroup_subsys_state *pos,
148 					   struct cgroup_subsys_state *parent);
149 struct cgroup_subsys_state *css_next_descendant_pre(struct cgroup_subsys_state *pos,
150 						    struct cgroup_subsys_state *css);
151 struct cgroup_subsys_state *css_rightmost_descendant(struct cgroup_subsys_state *pos);
152 struct cgroup_subsys_state *css_next_descendant_post(struct cgroup_subsys_state *pos,
153 						     struct cgroup_subsys_state *css);
154 
155 struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset,
156 					 struct cgroup_subsys_state **dst_cssp);
157 struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset,
158 					struct cgroup_subsys_state **dst_cssp);
159 
160 void css_task_iter_start(struct cgroup_subsys_state *css, unsigned int flags,
161 			 struct css_task_iter *it);
162 struct task_struct *css_task_iter_next(struct css_task_iter *it);
163 void css_task_iter_end(struct css_task_iter *it);
164 
165 /**
166  * css_for_each_child - iterate through children of a css
167  * @pos: the css * to use as the loop cursor
168  * @parent: css whose children to walk
169  *
170  * Walk @parent's children.  Must be called under rcu_read_lock().
171  *
172  * If a subsystem synchronizes ->css_online() and the start of iteration, a
173  * css which finished ->css_online() is guaranteed to be visible in the
174  * future iterations and will stay visible until the last reference is put.
175  * A css which hasn't finished ->css_online() or already finished
176  * ->css_offline() may show up during traversal.  It's each subsystem's
177  * responsibility to synchronize against on/offlining.
178  *
179  * It is allowed to temporarily drop RCU read lock during iteration.  The
180  * caller is responsible for ensuring that @pos remains accessible until
181  * the start of the next iteration by, for example, bumping the css refcnt.
182  */
183 #define css_for_each_child(pos, parent)					\
184 	for ((pos) = css_next_child(NULL, (parent)); (pos);		\
185 	     (pos) = css_next_child((pos), (parent)))
186 
187 /**
188  * css_for_each_descendant_pre - pre-order walk of a css's descendants
189  * @pos: the css * to use as the loop cursor
190  * @root: css whose descendants to walk
191  *
192  * Walk @root's descendants.  @root is included in the iteration and the
193  * first node to be visited.  Must be called under rcu_read_lock().
194  *
195  * If a subsystem synchronizes ->css_online() and the start of iteration, a
196  * css which finished ->css_online() is guaranteed to be visible in the
197  * future iterations and will stay visible until the last reference is put.
198  * A css which hasn't finished ->css_online() or already finished
199  * ->css_offline() may show up during traversal.  It's each subsystem's
200  * responsibility to synchronize against on/offlining.
201  *
202  * For example, the following guarantees that a descendant can't escape
203  * state updates of its ancestors.
204  *
205  * my_online(@css)
206  * {
207  *	Lock @css's parent and @css;
208  *	Inherit state from the parent;
209  *	Unlock both.
210  * }
211  *
212  * my_update_state(@css)
213  * {
214  *	css_for_each_descendant_pre(@pos, @css) {
215  *		Lock @pos;
216  *		if (@pos == @css)
217  *			Update @css's state;
218  *		else
219  *			Verify @pos is alive and inherit state from its parent;
220  *		Unlock @pos;
221  *	}
222  * }
223  *
224  * As long as the inheriting step, including checking the parent state, is
225  * enclosed inside @pos locking, double-locking the parent isn't necessary
226  * while inheriting.  The state update to the parent is guaranteed to be
227  * visible by walking order and, as long as inheriting operations to the
228  * same @pos are atomic to each other, multiple updates racing each other
229  * still result in the correct state.  It's guaranateed that at least one
230  * inheritance happens for any css after the latest update to its parent.
231  *
232  * If checking parent's state requires locking the parent, each inheriting
233  * iteration should lock and unlock both @pos->parent and @pos.
234  *
235  * Alternatively, a subsystem may choose to use a single global lock to
236  * synchronize ->css_online() and ->css_offline() against tree-walking
237  * operations.
238  *
239  * It is allowed to temporarily drop RCU read lock during iteration.  The
240  * caller is responsible for ensuring that @pos remains accessible until
241  * the start of the next iteration by, for example, bumping the css refcnt.
242  */
243 #define css_for_each_descendant_pre(pos, css)				\
244 	for ((pos) = css_next_descendant_pre(NULL, (css)); (pos);	\
245 	     (pos) = css_next_descendant_pre((pos), (css)))
246 
247 /**
248  * css_for_each_descendant_post - post-order walk of a css's descendants
249  * @pos: the css * to use as the loop cursor
250  * @css: css whose descendants to walk
251  *
252  * Similar to css_for_each_descendant_pre() but performs post-order
253  * traversal instead.  @root is included in the iteration and the last
254  * node to be visited.
255  *
256  * If a subsystem synchronizes ->css_online() and the start of iteration, a
257  * css which finished ->css_online() is guaranteed to be visible in the
258  * future iterations and will stay visible until the last reference is put.
259  * A css which hasn't finished ->css_online() or already finished
260  * ->css_offline() may show up during traversal.  It's each subsystem's
261  * responsibility to synchronize against on/offlining.
262  *
263  * Note that the walk visibility guarantee example described in pre-order
264  * walk doesn't apply the same to post-order walks.
265  */
266 #define css_for_each_descendant_post(pos, css)				\
267 	for ((pos) = css_next_descendant_post(NULL, (css)); (pos);	\
268 	     (pos) = css_next_descendant_post((pos), (css)))
269 
270 /**
271  * cgroup_taskset_for_each - iterate cgroup_taskset
272  * @task: the loop cursor
273  * @dst_css: the destination css
274  * @tset: taskset to iterate
275  *
276  * @tset may contain multiple tasks and they may belong to multiple
277  * processes.
278  *
279  * On the v2 hierarchy, there may be tasks from multiple processes and they
280  * may not share the source or destination csses.
281  *
282  * On traditional hierarchies, when there are multiple tasks in @tset, if a
283  * task of a process is in @tset, all tasks of the process are in @tset.
284  * Also, all are guaranteed to share the same source and destination csses.
285  *
286  * Iteration is not in any specific order.
287  */
288 #define cgroup_taskset_for_each(task, dst_css, tset)			\
289 	for ((task) = cgroup_taskset_first((tset), &(dst_css));		\
290 	     (task);							\
291 	     (task) = cgroup_taskset_next((tset), &(dst_css)))
292 
293 /**
294  * cgroup_taskset_for_each_leader - iterate group leaders in a cgroup_taskset
295  * @leader: the loop cursor
296  * @dst_css: the destination css
297  * @tset: taskset to iterate
298  *
299  * Iterate threadgroup leaders of @tset.  For single-task migrations, @tset
300  * may not contain any.
301  */
302 #define cgroup_taskset_for_each_leader(leader, dst_css, tset)		\
303 	for ((leader) = cgroup_taskset_first((tset), &(dst_css));	\
304 	     (leader);							\
305 	     (leader) = cgroup_taskset_next((tset), &(dst_css)))	\
306 		if ((leader) != (leader)->group_leader)			\
307 			;						\
308 		else
309 
310 /*
311  * Inline functions.
312  */
313 
cgroup_id(const struct cgroup * cgrp)314 static inline u64 cgroup_id(const struct cgroup *cgrp)
315 {
316 	return cgrp->kn->id;
317 }
318 
319 /**
320  * css_get - obtain a reference on the specified css
321  * @css: target css
322  *
323  * The caller must already have a reference.
324  */
css_get(struct cgroup_subsys_state * css)325 static inline void css_get(struct cgroup_subsys_state *css)
326 {
327 	if (!(css->flags & CSS_NO_REF))
328 		percpu_ref_get(&css->refcnt);
329 }
330 
331 /**
332  * css_get_many - obtain references on the specified css
333  * @css: target css
334  * @n: number of references to get
335  *
336  * The caller must already have a reference.
337  */
css_get_many(struct cgroup_subsys_state * css,unsigned int n)338 static inline void css_get_many(struct cgroup_subsys_state *css, unsigned int n)
339 {
340 	if (!(css->flags & CSS_NO_REF))
341 		percpu_ref_get_many(&css->refcnt, n);
342 }
343 
344 /**
345  * css_tryget - try to obtain a reference on the specified css
346  * @css: target css
347  *
348  * Obtain a reference on @css unless it already has reached zero and is
349  * being released.  This function doesn't care whether @css is on or
350  * offline.  The caller naturally needs to ensure that @css is accessible
351  * but doesn't have to be holding a reference on it - IOW, RCU protected
352  * access is good enough for this function.  Returns %true if a reference
353  * count was successfully obtained; %false otherwise.
354  */
css_tryget(struct cgroup_subsys_state * css)355 static inline bool css_tryget(struct cgroup_subsys_state *css)
356 {
357 	if (!(css->flags & CSS_NO_REF))
358 		return percpu_ref_tryget(&css->refcnt);
359 	return true;
360 }
361 
362 /**
363  * css_tryget_online - try to obtain a reference on the specified css if online
364  * @css: target css
365  *
366  * Obtain a reference on @css if it's online.  The caller naturally needs
367  * to ensure that @css is accessible but doesn't have to be holding a
368  * reference on it - IOW, RCU protected access is good enough for this
369  * function.  Returns %true if a reference count was successfully obtained;
370  * %false otherwise.
371  */
css_tryget_online(struct cgroup_subsys_state * css)372 static inline bool css_tryget_online(struct cgroup_subsys_state *css)
373 {
374 	if (!(css->flags & CSS_NO_REF))
375 		return percpu_ref_tryget_live(&css->refcnt);
376 	return true;
377 }
378 
379 /**
380  * css_is_dying - test whether the specified css is dying
381  * @css: target css
382  *
383  * Test whether @css is in the process of offlining or already offline.  In
384  * most cases, ->css_online() and ->css_offline() callbacks should be
385  * enough; however, the actual offline operations are RCU delayed and this
386  * test returns %true also when @css is scheduled to be offlined.
387  *
388  * This is useful, for example, when the use case requires synchronous
389  * behavior with respect to cgroup removal.  cgroup removal schedules css
390  * offlining but the css can seem alive while the operation is being
391  * delayed.  If the delay affects user visible semantics, this test can be
392  * used to resolve the situation.
393  */
css_is_dying(struct cgroup_subsys_state * css)394 static inline bool css_is_dying(struct cgroup_subsys_state *css)
395 {
396 	return !(css->flags & CSS_NO_REF) && percpu_ref_is_dying(&css->refcnt);
397 }
398 
399 /**
400  * css_put - put a css reference
401  * @css: target css
402  *
403  * Put a reference obtained via css_get() and css_tryget_online().
404  */
css_put(struct cgroup_subsys_state * css)405 static inline void css_put(struct cgroup_subsys_state *css)
406 {
407 	if (!(css->flags & CSS_NO_REF))
408 		percpu_ref_put(&css->refcnt);
409 }
410 
411 /**
412  * css_put_many - put css references
413  * @css: target css
414  * @n: number of references to put
415  *
416  * Put references obtained via css_get() and css_tryget_online().
417  */
css_put_many(struct cgroup_subsys_state * css,unsigned int n)418 static inline void css_put_many(struct cgroup_subsys_state *css, unsigned int n)
419 {
420 	if (!(css->flags & CSS_NO_REF))
421 		percpu_ref_put_many(&css->refcnt, n);
422 }
423 
cgroup_get(struct cgroup * cgrp)424 static inline void cgroup_get(struct cgroup *cgrp)
425 {
426 	css_get(&cgrp->self);
427 }
428 
cgroup_tryget(struct cgroup * cgrp)429 static inline bool cgroup_tryget(struct cgroup *cgrp)
430 {
431 	return css_tryget(&cgrp->self);
432 }
433 
cgroup_put(struct cgroup * cgrp)434 static inline void cgroup_put(struct cgroup *cgrp)
435 {
436 	css_put(&cgrp->self);
437 }
438 
439 extern struct mutex cgroup_mutex;
440 
cgroup_lock(void)441 static inline void cgroup_lock(void)
442 {
443 	mutex_lock(&cgroup_mutex);
444 }
445 
cgroup_unlock(void)446 static inline void cgroup_unlock(void)
447 {
448 	mutex_unlock(&cgroup_mutex);
449 }
450 
451 /**
452  * task_css_set_check - obtain a task's css_set with extra access conditions
453  * @task: the task to obtain css_set for
454  * @__c: extra condition expression to be passed to rcu_dereference_check()
455  *
456  * A task's css_set is RCU protected, initialized and exited while holding
457  * task_lock(), and can only be modified while holding both cgroup_mutex
458  * and task_lock() while the task is alive.  This macro verifies that the
459  * caller is inside proper critical section and returns @task's css_set.
460  *
461  * The caller can also specify additional allowed conditions via @__c, such
462  * as locks used during the cgroup_subsys::attach() methods.
463  */
464 #ifdef CONFIG_PROVE_RCU
465 extern spinlock_t css_set_lock;
466 #define task_css_set_check(task, __c)					\
467 	rcu_dereference_check((task)->cgroups,				\
468 		rcu_read_lock_sched_held() ||				\
469 		lockdep_is_held(&cgroup_mutex) ||			\
470 		lockdep_is_held(&css_set_lock) ||			\
471 		((task)->flags & PF_EXITING) || (__c))
472 #else
473 #define task_css_set_check(task, __c)					\
474 	rcu_dereference((task)->cgroups)
475 #endif
476 
477 /**
478  * task_css_check - obtain css for (task, subsys) w/ extra access conds
479  * @task: the target task
480  * @subsys_id: the target subsystem ID
481  * @__c: extra condition expression to be passed to rcu_dereference_check()
482  *
483  * Return the cgroup_subsys_state for the (@task, @subsys_id) pair.  The
484  * synchronization rules are the same as task_css_set_check().
485  */
486 #define task_css_check(task, subsys_id, __c)				\
487 	task_css_set_check((task), (__c))->subsys[(subsys_id)]
488 
489 /**
490  * task_css_set - obtain a task's css_set
491  * @task: the task to obtain css_set for
492  *
493  * See task_css_set_check().
494  */
task_css_set(struct task_struct * task)495 static inline struct css_set *task_css_set(struct task_struct *task)
496 {
497 	return task_css_set_check(task, false);
498 }
499 
500 /**
501  * task_css - obtain css for (task, subsys)
502  * @task: the target task
503  * @subsys_id: the target subsystem ID
504  *
505  * See task_css_check().
506  */
task_css(struct task_struct * task,int subsys_id)507 static inline struct cgroup_subsys_state *task_css(struct task_struct *task,
508 						   int subsys_id)
509 {
510 	return task_css_check(task, subsys_id, false);
511 }
512 
513 /**
514  * task_get_css - find and get the css for (task, subsys)
515  * @task: the target task
516  * @subsys_id: the target subsystem ID
517  *
518  * Find the css for the (@task, @subsys_id) combination, increment a
519  * reference on and return it.  This function is guaranteed to return a
520  * valid css.  The returned css may already have been offlined.
521  */
522 static inline struct cgroup_subsys_state *
task_get_css(struct task_struct * task,int subsys_id)523 task_get_css(struct task_struct *task, int subsys_id)
524 {
525 	struct cgroup_subsys_state *css;
526 
527 	rcu_read_lock();
528 	while (true) {
529 		css = task_css(task, subsys_id);
530 		/*
531 		 * Can't use css_tryget_online() here.  A task which has
532 		 * PF_EXITING set may stay associated with an offline css.
533 		 * If such task calls this function, css_tryget_online()
534 		 * will keep failing.
535 		 */
536 		if (likely(css_tryget(css)))
537 			break;
538 		cpu_relax();
539 	}
540 	rcu_read_unlock();
541 	return css;
542 }
543 
544 /**
545  * task_css_is_root - test whether a task belongs to the root css
546  * @task: the target task
547  * @subsys_id: the target subsystem ID
548  *
549  * Test whether @task belongs to the root css on the specified subsystem.
550  * May be invoked in any context.
551  */
task_css_is_root(struct task_struct * task,int subsys_id)552 static inline bool task_css_is_root(struct task_struct *task, int subsys_id)
553 {
554 	return task_css_check(task, subsys_id, true) ==
555 		init_css_set.subsys[subsys_id];
556 }
557 
task_cgroup(struct task_struct * task,int subsys_id)558 static inline struct cgroup *task_cgroup(struct task_struct *task,
559 					 int subsys_id)
560 {
561 	return task_css(task, subsys_id)->cgroup;
562 }
563 
task_dfl_cgroup(struct task_struct * task)564 static inline struct cgroup *task_dfl_cgroup(struct task_struct *task)
565 {
566 	return task_css_set(task)->dfl_cgrp;
567 }
568 
cgroup_parent(struct cgroup * cgrp)569 static inline struct cgroup *cgroup_parent(struct cgroup *cgrp)
570 {
571 	struct cgroup_subsys_state *parent_css = cgrp->self.parent;
572 
573 	if (parent_css)
574 		return container_of(parent_css, struct cgroup, self);
575 	return NULL;
576 }
577 
578 /**
579  * cgroup_is_descendant - test ancestry
580  * @cgrp: the cgroup to be tested
581  * @ancestor: possible ancestor of @cgrp
582  *
583  * Test whether @cgrp is a descendant of @ancestor.  It also returns %true
584  * if @cgrp == @ancestor.  This function is safe to call as long as @cgrp
585  * and @ancestor are accessible.
586  */
cgroup_is_descendant(struct cgroup * cgrp,struct cgroup * ancestor)587 static inline bool cgroup_is_descendant(struct cgroup *cgrp,
588 					struct cgroup *ancestor)
589 {
590 	if (cgrp->root != ancestor->root || cgrp->level < ancestor->level)
591 		return false;
592 	return cgrp->ancestor_ids[ancestor->level] == cgroup_id(ancestor);
593 }
594 
595 /**
596  * cgroup_ancestor - find ancestor of cgroup
597  * @cgrp: cgroup to find ancestor of
598  * @ancestor_level: level of ancestor to find starting from root
599  *
600  * Find ancestor of cgroup at specified level starting from root if it exists
601  * and return pointer to it. Return NULL if @cgrp doesn't have ancestor at
602  * @ancestor_level.
603  *
604  * This function is safe to call as long as @cgrp is accessible.
605  */
cgroup_ancestor(struct cgroup * cgrp,int ancestor_level)606 static inline struct cgroup *cgroup_ancestor(struct cgroup *cgrp,
607 					     int ancestor_level)
608 {
609 	if (cgrp->level < ancestor_level)
610 		return NULL;
611 	while (cgrp && cgrp->level > ancestor_level)
612 		cgrp = cgroup_parent(cgrp);
613 	return cgrp;
614 }
615 
616 /**
617  * task_under_cgroup_hierarchy - test task's membership of cgroup ancestry
618  * @task: the task to be tested
619  * @ancestor: possible ancestor of @task's cgroup
620  *
621  * Tests whether @task's default cgroup hierarchy is a descendant of @ancestor.
622  * It follows all the same rules as cgroup_is_descendant, and only applies
623  * to the default hierarchy.
624  */
task_under_cgroup_hierarchy(struct task_struct * task,struct cgroup * ancestor)625 static inline bool task_under_cgroup_hierarchy(struct task_struct *task,
626 					       struct cgroup *ancestor)
627 {
628 	struct css_set *cset = task_css_set(task);
629 
630 	return cgroup_is_descendant(cset->dfl_cgrp, ancestor);
631 }
632 
633 /* no synchronization, the result can only be used as a hint */
cgroup_is_populated(struct cgroup * cgrp)634 static inline bool cgroup_is_populated(struct cgroup *cgrp)
635 {
636 	return cgrp->nr_populated_csets + cgrp->nr_populated_domain_children +
637 		cgrp->nr_populated_threaded_children;
638 }
639 
640 /* returns ino associated with a cgroup */
cgroup_ino(struct cgroup * cgrp)641 static inline ino_t cgroup_ino(struct cgroup *cgrp)
642 {
643 	return kernfs_ino(cgrp->kn);
644 }
645 
646 /* cft/css accessors for cftype->write() operation */
of_cft(struct kernfs_open_file * of)647 static inline struct cftype *of_cft(struct kernfs_open_file *of)
648 {
649 	return of->kn->priv;
650 }
651 
652 struct cgroup_subsys_state *of_css(struct kernfs_open_file *of);
653 
654 /* cft/css accessors for cftype->seq_*() operations */
seq_cft(struct seq_file * seq)655 static inline struct cftype *seq_cft(struct seq_file *seq)
656 {
657 	return of_cft(seq->private);
658 }
659 
seq_css(struct seq_file * seq)660 static inline struct cgroup_subsys_state *seq_css(struct seq_file *seq)
661 {
662 	return of_css(seq->private);
663 }
664 
665 /*
666  * Name / path handling functions.  All are thin wrappers around the kernfs
667  * counterparts and can be called under any context.
668  */
669 
cgroup_name(struct cgroup * cgrp,char * buf,size_t buflen)670 static inline int cgroup_name(struct cgroup *cgrp, char *buf, size_t buflen)
671 {
672 	return kernfs_name(cgrp->kn, buf, buflen);
673 }
674 
cgroup_path(struct cgroup * cgrp,char * buf,size_t buflen)675 static inline int cgroup_path(struct cgroup *cgrp, char *buf, size_t buflen)
676 {
677 	return kernfs_path(cgrp->kn, buf, buflen);
678 }
679 
pr_cont_cgroup_name(struct cgroup * cgrp)680 static inline void pr_cont_cgroup_name(struct cgroup *cgrp)
681 {
682 	pr_cont_kernfs_name(cgrp->kn);
683 }
684 
pr_cont_cgroup_path(struct cgroup * cgrp)685 static inline void pr_cont_cgroup_path(struct cgroup *cgrp)
686 {
687 	pr_cont_kernfs_path(cgrp->kn);
688 }
689 
cgroup_psi(struct cgroup * cgrp)690 static inline struct psi_group *cgroup_psi(struct cgroup *cgrp)
691 {
692 	return &cgrp->psi;
693 }
694 
695 bool cgroup_psi_enabled(void);
696 
cgroup_init_kthreadd(void)697 static inline void cgroup_init_kthreadd(void)
698 {
699 	/*
700 	 * kthreadd is inherited by all kthreads, keep it in the root so
701 	 * that the new kthreads are guaranteed to stay in the root until
702 	 * initialization is finished.
703 	 */
704 	current->no_cgroup_migration = 1;
705 }
706 
cgroup_kthread_ready(void)707 static inline void cgroup_kthread_ready(void)
708 {
709 	/*
710 	 * This kthread finished initialization.  The creator should have
711 	 * set PF_NO_SETAFFINITY if this kthread should stay in the root.
712 	 */
713 	current->no_cgroup_migration = 0;
714 }
715 
716 void cgroup_path_from_kernfs_id(u64 id, char *buf, size_t buflen);
717 struct cgroup *cgroup_get_from_id(u64 id);
718 #else /* !CONFIG_CGROUPS */
719 
720 struct cgroup_subsys_state;
721 struct cgroup;
722 
cgroup_id(const struct cgroup * cgrp)723 static inline u64 cgroup_id(const struct cgroup *cgrp) { return 1; }
css_get(struct cgroup_subsys_state * css)724 static inline void css_get(struct cgroup_subsys_state *css) {}
css_put(struct cgroup_subsys_state * css)725 static inline void css_put(struct cgroup_subsys_state *css) {}
cgroup_lock(void)726 static inline void cgroup_lock(void) {}
cgroup_unlock(void)727 static inline void cgroup_unlock(void) {}
cgroup_attach_task_all(struct task_struct * from,struct task_struct * t)728 static inline int cgroup_attach_task_all(struct task_struct *from,
729 					 struct task_struct *t) { return 0; }
cgroupstats_build(struct cgroupstats * stats,struct dentry * dentry)730 static inline int cgroupstats_build(struct cgroupstats *stats,
731 				    struct dentry *dentry) { return -EINVAL; }
732 
cgroup_fork(struct task_struct * p)733 static inline void cgroup_fork(struct task_struct *p) {}
cgroup_can_fork(struct task_struct * p,struct kernel_clone_args * kargs)734 static inline int cgroup_can_fork(struct task_struct *p,
735 				  struct kernel_clone_args *kargs) { return 0; }
cgroup_cancel_fork(struct task_struct * p,struct kernel_clone_args * kargs)736 static inline void cgroup_cancel_fork(struct task_struct *p,
737 				      struct kernel_clone_args *kargs) {}
cgroup_post_fork(struct task_struct * p,struct kernel_clone_args * kargs)738 static inline void cgroup_post_fork(struct task_struct *p,
739 				    struct kernel_clone_args *kargs) {}
cgroup_exit(struct task_struct * p)740 static inline void cgroup_exit(struct task_struct *p) {}
cgroup_release(struct task_struct * p)741 static inline void cgroup_release(struct task_struct *p) {}
cgroup_free(struct task_struct * p)742 static inline void cgroup_free(struct task_struct *p) {}
743 
cgroup_init_early(void)744 static inline int cgroup_init_early(void) { return 0; }
cgroup_init(void)745 static inline int cgroup_init(void) { return 0; }
cgroup_init_kthreadd(void)746 static inline void cgroup_init_kthreadd(void) {}
cgroup_kthread_ready(void)747 static inline void cgroup_kthread_ready(void) {}
748 
cgroup_parent(struct cgroup * cgrp)749 static inline struct cgroup *cgroup_parent(struct cgroup *cgrp)
750 {
751 	return NULL;
752 }
753 
cgroup_psi(struct cgroup * cgrp)754 static inline struct psi_group *cgroup_psi(struct cgroup *cgrp)
755 {
756 	return NULL;
757 }
758 
cgroup_psi_enabled(void)759 static inline bool cgroup_psi_enabled(void)
760 {
761 	return false;
762 }
763 
task_under_cgroup_hierarchy(struct task_struct * task,struct cgroup * ancestor)764 static inline bool task_under_cgroup_hierarchy(struct task_struct *task,
765 					       struct cgroup *ancestor)
766 {
767 	return true;
768 }
769 
cgroup_path_from_kernfs_id(u64 id,char * buf,size_t buflen)770 static inline void cgroup_path_from_kernfs_id(u64 id, char *buf, size_t buflen)
771 {}
772 
cgroup_get_from_id(u64 id)773 static inline struct cgroup *cgroup_get_from_id(u64 id)
774 {
775 	return NULL;
776 }
777 #endif /* !CONFIG_CGROUPS */
778 
779 #ifdef CONFIG_CGROUPS
780 /*
781  * cgroup scalable recursive statistics.
782  */
783 void cgroup_rstat_updated(struct cgroup *cgrp, int cpu);
784 void cgroup_rstat_flush(struct cgroup *cgrp);
785 void cgroup_rstat_flush_irqsafe(struct cgroup *cgrp);
786 void cgroup_rstat_flush_hold(struct cgroup *cgrp);
787 void cgroup_rstat_flush_release(void);
788 
789 /*
790  * Basic resource stats.
791  */
792 #ifdef CONFIG_CGROUP_CPUACCT
793 void cpuacct_charge(struct task_struct *tsk, u64 cputime);
794 void cpuacct_account_field(struct task_struct *tsk, int index, u64 val);
795 #else
cpuacct_charge(struct task_struct * tsk,u64 cputime)796 static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime) {}
cpuacct_account_field(struct task_struct * tsk,int index,u64 val)797 static inline void cpuacct_account_field(struct task_struct *tsk, int index,
798 					 u64 val) {}
799 #endif
800 
801 void __cgroup_account_cputime(struct cgroup *cgrp, u64 delta_exec);
802 void __cgroup_account_cputime_field(struct cgroup *cgrp,
803 				    enum cpu_usage_stat index, u64 delta_exec);
804 
cgroup_account_cputime(struct task_struct * task,u64 delta_exec)805 static inline void cgroup_account_cputime(struct task_struct *task,
806 					  u64 delta_exec)
807 {
808 	struct cgroup *cgrp;
809 
810 	cpuacct_charge(task, delta_exec);
811 
812 	cgrp = task_dfl_cgroup(task);
813 	if (cgroup_parent(cgrp))
814 		__cgroup_account_cputime(cgrp, delta_exec);
815 }
816 
cgroup_account_cputime_field(struct task_struct * task,enum cpu_usage_stat index,u64 delta_exec)817 static inline void cgroup_account_cputime_field(struct task_struct *task,
818 						enum cpu_usage_stat index,
819 						u64 delta_exec)
820 {
821 	struct cgroup *cgrp;
822 
823 	cpuacct_account_field(task, index, delta_exec);
824 
825 	rcu_read_lock();
826 	cgrp = task_dfl_cgroup(task);
827 	if (cgroup_parent(cgrp))
828 		__cgroup_account_cputime_field(cgrp, index, delta_exec);
829 	rcu_read_unlock();
830 }
831 
832 #else	/* CONFIG_CGROUPS */
833 
cgroup_account_cputime(struct task_struct * task,u64 delta_exec)834 static inline void cgroup_account_cputime(struct task_struct *task,
835 					  u64 delta_exec) {}
cgroup_account_cputime_field(struct task_struct * task,enum cpu_usage_stat index,u64 delta_exec)836 static inline void cgroup_account_cputime_field(struct task_struct *task,
837 						enum cpu_usage_stat index,
838 						u64 delta_exec) {}
839 
840 #endif	/* CONFIG_CGROUPS */
841 
842 /*
843  * sock->sk_cgrp_data handling.  For more info, see sock_cgroup_data
844  * definition in cgroup-defs.h.
845  */
846 #ifdef CONFIG_SOCK_CGROUP_DATA
847 
848 void cgroup_sk_alloc(struct sock_cgroup_data *skcd);
849 void cgroup_sk_clone(struct sock_cgroup_data *skcd);
850 void cgroup_sk_free(struct sock_cgroup_data *skcd);
851 
sock_cgroup_ptr(struct sock_cgroup_data * skcd)852 static inline struct cgroup *sock_cgroup_ptr(struct sock_cgroup_data *skcd)
853 {
854 	return skcd->cgroup;
855 }
856 
857 #else	/* CONFIG_CGROUP_DATA */
858 
cgroup_sk_alloc(struct sock_cgroup_data * skcd)859 static inline void cgroup_sk_alloc(struct sock_cgroup_data *skcd) {}
cgroup_sk_clone(struct sock_cgroup_data * skcd)860 static inline void cgroup_sk_clone(struct sock_cgroup_data *skcd) {}
cgroup_sk_free(struct sock_cgroup_data * skcd)861 static inline void cgroup_sk_free(struct sock_cgroup_data *skcd) {}
862 
863 #endif	/* CONFIG_CGROUP_DATA */
864 
865 struct cgroup_namespace {
866 	struct ns_common	ns;
867 	struct user_namespace	*user_ns;
868 	struct ucounts		*ucounts;
869 	struct css_set          *root_cset;
870 };
871 
872 extern struct cgroup_namespace init_cgroup_ns;
873 
874 #ifdef CONFIG_CGROUPS
875 
876 void free_cgroup_ns(struct cgroup_namespace *ns);
877 
878 struct cgroup_namespace *copy_cgroup_ns(unsigned long flags,
879 					struct user_namespace *user_ns,
880 					struct cgroup_namespace *old_ns);
881 
882 int cgroup_path_ns(struct cgroup *cgrp, char *buf, size_t buflen,
883 		   struct cgroup_namespace *ns);
884 
885 #else /* !CONFIG_CGROUPS */
886 
free_cgroup_ns(struct cgroup_namespace * ns)887 static inline void free_cgroup_ns(struct cgroup_namespace *ns) { }
888 static inline struct cgroup_namespace *
copy_cgroup_ns(unsigned long flags,struct user_namespace * user_ns,struct cgroup_namespace * old_ns)889 copy_cgroup_ns(unsigned long flags, struct user_namespace *user_ns,
890 	       struct cgroup_namespace *old_ns)
891 {
892 	return old_ns;
893 }
894 
895 #endif /* !CONFIG_CGROUPS */
896 
get_cgroup_ns(struct cgroup_namespace * ns)897 static inline void get_cgroup_ns(struct cgroup_namespace *ns)
898 {
899 	if (ns)
900 		refcount_inc(&ns->ns.count);
901 }
902 
put_cgroup_ns(struct cgroup_namespace * ns)903 static inline void put_cgroup_ns(struct cgroup_namespace *ns)
904 {
905 	if (ns && refcount_dec_and_test(&ns->ns.count))
906 		free_cgroup_ns(ns);
907 }
908 
909 #ifdef CONFIG_CGROUPS
910 
911 void cgroup_enter_frozen(void);
912 void cgroup_leave_frozen(bool always_leave);
913 void cgroup_update_frozen(struct cgroup *cgrp);
914 void cgroup_freeze(struct cgroup *cgrp, bool freeze);
915 void cgroup_freezer_migrate_task(struct task_struct *task, struct cgroup *src,
916 				 struct cgroup *dst);
917 
cgroup_task_frozen(struct task_struct * task)918 static inline bool cgroup_task_frozen(struct task_struct *task)
919 {
920 	return task->frozen;
921 }
922 
923 #else /* !CONFIG_CGROUPS */
924 
cgroup_enter_frozen(void)925 static inline void cgroup_enter_frozen(void) { }
cgroup_leave_frozen(bool always_leave)926 static inline void cgroup_leave_frozen(bool always_leave) { }
cgroup_task_frozen(struct task_struct * task)927 static inline bool cgroup_task_frozen(struct task_struct *task)
928 {
929 	return false;
930 }
931 
932 #endif /* !CONFIG_CGROUPS */
933 
934 #ifdef CONFIG_CGROUP_BPF
cgroup_bpf_get(struct cgroup * cgrp)935 static inline void cgroup_bpf_get(struct cgroup *cgrp)
936 {
937 	percpu_ref_get(&cgrp->bpf.refcnt);
938 }
939 
cgroup_bpf_put(struct cgroup * cgrp)940 static inline void cgroup_bpf_put(struct cgroup *cgrp)
941 {
942 	percpu_ref_put(&cgrp->bpf.refcnt);
943 }
944 
945 #else /* CONFIG_CGROUP_BPF */
946 
cgroup_bpf_get(struct cgroup * cgrp)947 static inline void cgroup_bpf_get(struct cgroup *cgrp) {}
cgroup_bpf_put(struct cgroup * cgrp)948 static inline void cgroup_bpf_put(struct cgroup *cgrp) {}
949 
950 #endif /* CONFIG_CGROUP_BPF */
951 
952 #endif /* _LINUX_CGROUP_H */
953