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