• 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 struct cgroup *cgroup_v1v2_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 void cgroup_file_show(struct cgroup_file *cfile, bool show);
123 
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 
315 #ifdef CONFIG_DEBUG_CGROUP_REF
316 void css_get(struct cgroup_subsys_state *css);
317 void css_get_many(struct cgroup_subsys_state *css, unsigned int n);
318 bool css_tryget(struct cgroup_subsys_state *css);
319 bool css_tryget_online(struct cgroup_subsys_state *css);
320 void css_put(struct cgroup_subsys_state *css);
321 void css_put_many(struct cgroup_subsys_state *css, unsigned int n);
322 #else
323 #define CGROUP_REF_FN_ATTRS	static inline
324 #define CGROUP_REF_EXPORT(fn)
325 #include <linux/cgroup_refcnt.h>
326 #endif
327 
cgroup_id(const struct cgroup * cgrp)328 static inline u64 cgroup_id(const struct cgroup *cgrp)
329 {
330 	return cgrp->kn->id;
331 }
332 
333 /**
334  * css_is_dying - test whether the specified css is dying
335  * @css: target css
336  *
337  * Test whether @css is in the process of offlining or already offline.  In
338  * most cases, ->css_online() and ->css_offline() callbacks should be
339  * enough; however, the actual offline operations are RCU delayed and this
340  * test returns %true also when @css is scheduled to be offlined.
341  *
342  * This is useful, for example, when the use case requires synchronous
343  * behavior with respect to cgroup removal.  cgroup removal schedules css
344  * offlining but the css can seem alive while the operation is being
345  * delayed.  If the delay affects user visible semantics, this test can be
346  * used to resolve the situation.
347  */
css_is_dying(struct cgroup_subsys_state * css)348 static inline bool css_is_dying(struct cgroup_subsys_state *css)
349 {
350 	return !(css->flags & CSS_NO_REF) && percpu_ref_is_dying(&css->refcnt);
351 }
352 
cgroup_get(struct cgroup * cgrp)353 static inline void cgroup_get(struct cgroup *cgrp)
354 {
355 	css_get(&cgrp->self);
356 }
357 
cgroup_tryget(struct cgroup * cgrp)358 static inline bool cgroup_tryget(struct cgroup *cgrp)
359 {
360 	return css_tryget(&cgrp->self);
361 }
362 
cgroup_put(struct cgroup * cgrp)363 static inline void cgroup_put(struct cgroup *cgrp)
364 {
365 	css_put(&cgrp->self);
366 }
367 
368 extern struct mutex cgroup_mutex;
369 
cgroup_lock(void)370 static inline void cgroup_lock(void)
371 {
372 	mutex_lock(&cgroup_mutex);
373 }
374 
cgroup_unlock(void)375 static inline void cgroup_unlock(void)
376 {
377 	mutex_unlock(&cgroup_mutex);
378 }
379 
380 /**
381  * task_css_set_check - obtain a task's css_set with extra access conditions
382  * @task: the task to obtain css_set for
383  * @__c: extra condition expression to be passed to rcu_dereference_check()
384  *
385  * A task's css_set is RCU protected, initialized and exited while holding
386  * task_lock(), and can only be modified while holding both cgroup_mutex
387  * and task_lock() while the task is alive.  This macro verifies that the
388  * caller is inside proper critical section and returns @task's css_set.
389  *
390  * The caller can also specify additional allowed conditions via @__c, such
391  * as locks used during the cgroup_subsys::attach() methods.
392  */
393 #ifdef CONFIG_PROVE_RCU
394 extern spinlock_t css_set_lock;
395 #define task_css_set_check(task, __c)					\
396 	rcu_dereference_check((task)->cgroups,				\
397 		rcu_read_lock_sched_held() ||				\
398 		lockdep_is_held(&cgroup_mutex) ||			\
399 		lockdep_is_held(&css_set_lock) ||			\
400 		((task)->flags & PF_EXITING) || (__c))
401 #else
402 #define task_css_set_check(task, __c)					\
403 	rcu_dereference((task)->cgroups)
404 #endif
405 
406 /**
407  * task_css_check - obtain css for (task, subsys) w/ extra access conds
408  * @task: the target task
409  * @subsys_id: the target subsystem ID
410  * @__c: extra condition expression to be passed to rcu_dereference_check()
411  *
412  * Return the cgroup_subsys_state for the (@task, @subsys_id) pair.  The
413  * synchronization rules are the same as task_css_set_check().
414  */
415 #define task_css_check(task, subsys_id, __c)				\
416 	task_css_set_check((task), (__c))->subsys[(subsys_id)]
417 
418 /**
419  * task_css_set - obtain a task's css_set
420  * @task: the task to obtain css_set for
421  *
422  * See task_css_set_check().
423  */
task_css_set(struct task_struct * task)424 static inline struct css_set *task_css_set(struct task_struct *task)
425 {
426 	return task_css_set_check(task, false);
427 }
428 
429 /**
430  * task_css - obtain css for (task, subsys)
431  * @task: the target task
432  * @subsys_id: the target subsystem ID
433  *
434  * See task_css_check().
435  */
task_css(struct task_struct * task,int subsys_id)436 static inline struct cgroup_subsys_state *task_css(struct task_struct *task,
437 						   int subsys_id)
438 {
439 	return task_css_check(task, subsys_id, false);
440 }
441 
442 /**
443  * task_get_css - find and get the css for (task, subsys)
444  * @task: the target task
445  * @subsys_id: the target subsystem ID
446  *
447  * Find the css for the (@task, @subsys_id) combination, increment a
448  * reference on and return it.  This function is guaranteed to return a
449  * valid css.  The returned css may already have been offlined.
450  */
451 static inline struct cgroup_subsys_state *
task_get_css(struct task_struct * task,int subsys_id)452 task_get_css(struct task_struct *task, int subsys_id)
453 {
454 	struct cgroup_subsys_state *css;
455 
456 	rcu_read_lock();
457 	while (true) {
458 		css = task_css(task, subsys_id);
459 		/*
460 		 * Can't use css_tryget_online() here.  A task which has
461 		 * PF_EXITING set may stay associated with an offline css.
462 		 * If such task calls this function, css_tryget_online()
463 		 * will keep failing.
464 		 */
465 		if (likely(css_tryget(css)))
466 			break;
467 		cpu_relax();
468 	}
469 	rcu_read_unlock();
470 	return css;
471 }
472 
473 /**
474  * task_css_is_root - test whether a task belongs to the root css
475  * @task: the target task
476  * @subsys_id: the target subsystem ID
477  *
478  * Test whether @task belongs to the root css on the specified subsystem.
479  * May be invoked in any context.
480  */
task_css_is_root(struct task_struct * task,int subsys_id)481 static inline bool task_css_is_root(struct task_struct *task, int subsys_id)
482 {
483 	return task_css_check(task, subsys_id, true) ==
484 		init_css_set.subsys[subsys_id];
485 }
486 
task_cgroup(struct task_struct * task,int subsys_id)487 static inline struct cgroup *task_cgroup(struct task_struct *task,
488 					 int subsys_id)
489 {
490 	return task_css(task, subsys_id)->cgroup;
491 }
492 
task_dfl_cgroup(struct task_struct * task)493 static inline struct cgroup *task_dfl_cgroup(struct task_struct *task)
494 {
495 	return task_css_set(task)->dfl_cgrp;
496 }
497 
cgroup_parent(struct cgroup * cgrp)498 static inline struct cgroup *cgroup_parent(struct cgroup *cgrp)
499 {
500 	struct cgroup_subsys_state *parent_css = cgrp->self.parent;
501 
502 	if (parent_css)
503 		return container_of(parent_css, struct cgroup, self);
504 	return NULL;
505 }
506 
507 /**
508  * cgroup_is_descendant - test ancestry
509  * @cgrp: the cgroup to be tested
510  * @ancestor: possible ancestor of @cgrp
511  *
512  * Test whether @cgrp is a descendant of @ancestor.  It also returns %true
513  * if @cgrp == @ancestor.  This function is safe to call as long as @cgrp
514  * and @ancestor are accessible.
515  */
cgroup_is_descendant(struct cgroup * cgrp,struct cgroup * ancestor)516 static inline bool cgroup_is_descendant(struct cgroup *cgrp,
517 					struct cgroup *ancestor)
518 {
519 	if (cgrp->root != ancestor->root || cgrp->level < ancestor->level)
520 		return false;
521 	return cgrp->ancestors[ancestor->level] == ancestor;
522 }
523 
524 /**
525  * cgroup_ancestor - find ancestor of cgroup
526  * @cgrp: cgroup to find ancestor of
527  * @ancestor_level: level of ancestor to find starting from root
528  *
529  * Find ancestor of cgroup at specified level starting from root if it exists
530  * and return pointer to it. Return NULL if @cgrp doesn't have ancestor at
531  * @ancestor_level.
532  *
533  * This function is safe to call as long as @cgrp is accessible.
534  */
cgroup_ancestor(struct cgroup * cgrp,int ancestor_level)535 static inline struct cgroup *cgroup_ancestor(struct cgroup *cgrp,
536 					     int ancestor_level)
537 {
538 	if (ancestor_level < 0 || ancestor_level > cgrp->level)
539 		return NULL;
540 	return cgrp->ancestors[ancestor_level];
541 }
542 
543 /**
544  * task_under_cgroup_hierarchy - test task's membership of cgroup ancestry
545  * @task: the task to be tested
546  * @ancestor: possible ancestor of @task's cgroup
547  *
548  * Tests whether @task's default cgroup hierarchy is a descendant of @ancestor.
549  * It follows all the same rules as cgroup_is_descendant, and only applies
550  * to the default hierarchy.
551  */
task_under_cgroup_hierarchy(struct task_struct * task,struct cgroup * ancestor)552 static inline bool task_under_cgroup_hierarchy(struct task_struct *task,
553 					       struct cgroup *ancestor)
554 {
555 	struct css_set *cset = task_css_set(task);
556 
557 	return cgroup_is_descendant(cset->dfl_cgrp, ancestor);
558 }
559 
560 /* no synchronization, the result can only be used as a hint */
cgroup_is_populated(struct cgroup * cgrp)561 static inline bool cgroup_is_populated(struct cgroup *cgrp)
562 {
563 	return cgrp->nr_populated_csets + cgrp->nr_populated_domain_children +
564 		cgrp->nr_populated_threaded_children;
565 }
566 
567 /* returns ino associated with a cgroup */
cgroup_ino(struct cgroup * cgrp)568 static inline ino_t cgroup_ino(struct cgroup *cgrp)
569 {
570 	return kernfs_ino(cgrp->kn);
571 }
572 
573 /* cft/css accessors for cftype->write() operation */
of_cft(struct kernfs_open_file * of)574 static inline struct cftype *of_cft(struct kernfs_open_file *of)
575 {
576 	return of->kn->priv;
577 }
578 
579 struct cgroup_subsys_state *of_css(struct kernfs_open_file *of);
580 
581 /* cft/css accessors for cftype->seq_*() operations */
seq_cft(struct seq_file * seq)582 static inline struct cftype *seq_cft(struct seq_file *seq)
583 {
584 	return of_cft(seq->private);
585 }
586 
seq_css(struct seq_file * seq)587 static inline struct cgroup_subsys_state *seq_css(struct seq_file *seq)
588 {
589 	return of_css(seq->private);
590 }
591 
592 /*
593  * Name / path handling functions.  All are thin wrappers around the kernfs
594  * counterparts and can be called under any context.
595  */
596 
cgroup_name(struct cgroup * cgrp,char * buf,size_t buflen)597 static inline int cgroup_name(struct cgroup *cgrp, char *buf, size_t buflen)
598 {
599 	return kernfs_name(cgrp->kn, buf, buflen);
600 }
601 
cgroup_path(struct cgroup * cgrp,char * buf,size_t buflen)602 static inline int cgroup_path(struct cgroup *cgrp, char *buf, size_t buflen)
603 {
604 	return kernfs_path(cgrp->kn, buf, buflen);
605 }
606 
pr_cont_cgroup_name(struct cgroup * cgrp)607 static inline void pr_cont_cgroup_name(struct cgroup *cgrp)
608 {
609 	pr_cont_kernfs_name(cgrp->kn);
610 }
611 
pr_cont_cgroup_path(struct cgroup * cgrp)612 static inline void pr_cont_cgroup_path(struct cgroup *cgrp)
613 {
614 	pr_cont_kernfs_path(cgrp->kn);
615 }
616 
617 bool cgroup_psi_enabled(void);
618 
cgroup_init_kthreadd(void)619 static inline void cgroup_init_kthreadd(void)
620 {
621 	/*
622 	 * kthreadd is inherited by all kthreads, keep it in the root so
623 	 * that the new kthreads are guaranteed to stay in the root until
624 	 * initialization is finished.
625 	 */
626 	current->no_cgroup_migration = 1;
627 }
628 
cgroup_kthread_ready(void)629 static inline void cgroup_kthread_ready(void)
630 {
631 	/*
632 	 * This kthread finished initialization.  The creator should have
633 	 * set PF_NO_SETAFFINITY if this kthread should stay in the root.
634 	 */
635 	current->no_cgroup_migration = 0;
636 }
637 
638 void cgroup_path_from_kernfs_id(u64 id, char *buf, size_t buflen);
639 struct cgroup *cgroup_get_from_id(u64 id);
640 #else /* !CONFIG_CGROUPS */
641 
642 struct cgroup_subsys_state;
643 struct cgroup;
644 
cgroup_id(const struct cgroup * cgrp)645 static inline u64 cgroup_id(const struct cgroup *cgrp) { return 1; }
css_get(struct cgroup_subsys_state * css)646 static inline void css_get(struct cgroup_subsys_state *css) {}
css_put(struct cgroup_subsys_state * css)647 static inline void css_put(struct cgroup_subsys_state *css) {}
cgroup_lock(void)648 static inline void cgroup_lock(void) {}
cgroup_unlock(void)649 static inline void cgroup_unlock(void) {}
cgroup_attach_task_all(struct task_struct * from,struct task_struct * t)650 static inline int cgroup_attach_task_all(struct task_struct *from,
651 					 struct task_struct *t) { return 0; }
cgroupstats_build(struct cgroupstats * stats,struct dentry * dentry)652 static inline int cgroupstats_build(struct cgroupstats *stats,
653 				    struct dentry *dentry) { return -EINVAL; }
654 
cgroup_fork(struct task_struct * p)655 static inline void cgroup_fork(struct task_struct *p) {}
cgroup_can_fork(struct task_struct * p,struct kernel_clone_args * kargs)656 static inline int cgroup_can_fork(struct task_struct *p,
657 				  struct kernel_clone_args *kargs) { return 0; }
cgroup_cancel_fork(struct task_struct * p,struct kernel_clone_args * kargs)658 static inline void cgroup_cancel_fork(struct task_struct *p,
659 				      struct kernel_clone_args *kargs) {}
cgroup_post_fork(struct task_struct * p,struct kernel_clone_args * kargs)660 static inline void cgroup_post_fork(struct task_struct *p,
661 				    struct kernel_clone_args *kargs) {}
cgroup_exit(struct task_struct * p)662 static inline void cgroup_exit(struct task_struct *p) {}
cgroup_release(struct task_struct * p)663 static inline void cgroup_release(struct task_struct *p) {}
cgroup_free(struct task_struct * p)664 static inline void cgroup_free(struct task_struct *p) {}
665 
cgroup_init_early(void)666 static inline int cgroup_init_early(void) { return 0; }
cgroup_init(void)667 static inline int cgroup_init(void) { return 0; }
cgroup_init_kthreadd(void)668 static inline void cgroup_init_kthreadd(void) {}
cgroup_kthread_ready(void)669 static inline void cgroup_kthread_ready(void) {}
670 
cgroup_parent(struct cgroup * cgrp)671 static inline struct cgroup *cgroup_parent(struct cgroup *cgrp)
672 {
673 	return NULL;
674 }
675 
cgroup_psi_enabled(void)676 static inline bool cgroup_psi_enabled(void)
677 {
678 	return false;
679 }
680 
task_under_cgroup_hierarchy(struct task_struct * task,struct cgroup * ancestor)681 static inline bool task_under_cgroup_hierarchy(struct task_struct *task,
682 					       struct cgroup *ancestor)
683 {
684 	return true;
685 }
686 
cgroup_path_from_kernfs_id(u64 id,char * buf,size_t buflen)687 static inline void cgroup_path_from_kernfs_id(u64 id, char *buf, size_t buflen)
688 {}
689 #endif /* !CONFIG_CGROUPS */
690 
691 #ifdef CONFIG_CGROUPS
692 /*
693  * cgroup scalable recursive statistics.
694  */
695 void cgroup_rstat_updated(struct cgroup *cgrp, int cpu);
696 void cgroup_rstat_flush(struct cgroup *cgrp);
697 void cgroup_rstat_flush_hold(struct cgroup *cgrp);
698 void cgroup_rstat_flush_release(void);
699 
700 /*
701  * Basic resource stats.
702  */
703 #ifdef CONFIG_CGROUP_CPUACCT
704 void cpuacct_charge(struct task_struct *tsk, u64 cputime);
705 void cpuacct_account_field(struct task_struct *tsk, int index, u64 val);
706 #else
cpuacct_charge(struct task_struct * tsk,u64 cputime)707 static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime) {}
cpuacct_account_field(struct task_struct * tsk,int index,u64 val)708 static inline void cpuacct_account_field(struct task_struct *tsk, int index,
709 					 u64 val) {}
710 #endif
711 
712 void __cgroup_account_cputime(struct cgroup *cgrp, u64 delta_exec);
713 void __cgroup_account_cputime_field(struct cgroup *cgrp,
714 				    enum cpu_usage_stat index, u64 delta_exec);
715 
cgroup_account_cputime(struct task_struct * task,u64 delta_exec)716 static inline void cgroup_account_cputime(struct task_struct *task,
717 					  u64 delta_exec)
718 {
719 	struct cgroup *cgrp;
720 
721 	cpuacct_charge(task, delta_exec);
722 
723 	cgrp = task_dfl_cgroup(task);
724 	if (cgroup_parent(cgrp))
725 		__cgroup_account_cputime(cgrp, delta_exec);
726 }
727 
cgroup_account_cputime_field(struct task_struct * task,enum cpu_usage_stat index,u64 delta_exec)728 static inline void cgroup_account_cputime_field(struct task_struct *task,
729 						enum cpu_usage_stat index,
730 						u64 delta_exec)
731 {
732 	struct cgroup *cgrp;
733 
734 	cpuacct_account_field(task, index, delta_exec);
735 
736 	cgrp = task_dfl_cgroup(task);
737 	if (cgroup_parent(cgrp))
738 		__cgroup_account_cputime_field(cgrp, index, delta_exec);
739 }
740 
741 #else	/* CONFIG_CGROUPS */
742 
cgroup_account_cputime(struct task_struct * task,u64 delta_exec)743 static inline void cgroup_account_cputime(struct task_struct *task,
744 					  u64 delta_exec) {}
cgroup_account_cputime_field(struct task_struct * task,enum cpu_usage_stat index,u64 delta_exec)745 static inline void cgroup_account_cputime_field(struct task_struct *task,
746 						enum cpu_usage_stat index,
747 						u64 delta_exec) {}
748 
749 #endif	/* CONFIG_CGROUPS */
750 
751 /*
752  * sock->sk_cgrp_data handling.  For more info, see sock_cgroup_data
753  * definition in cgroup-defs.h.
754  */
755 #ifdef CONFIG_SOCK_CGROUP_DATA
756 
757 void cgroup_sk_alloc(struct sock_cgroup_data *skcd);
758 void cgroup_sk_clone(struct sock_cgroup_data *skcd);
759 void cgroup_sk_free(struct sock_cgroup_data *skcd);
760 
sock_cgroup_ptr(struct sock_cgroup_data * skcd)761 static inline struct cgroup *sock_cgroup_ptr(struct sock_cgroup_data *skcd)
762 {
763 	return skcd->cgroup;
764 }
765 
766 #else	/* CONFIG_CGROUP_DATA */
767 
cgroup_sk_alloc(struct sock_cgroup_data * skcd)768 static inline void cgroup_sk_alloc(struct sock_cgroup_data *skcd) {}
cgroup_sk_clone(struct sock_cgroup_data * skcd)769 static inline void cgroup_sk_clone(struct sock_cgroup_data *skcd) {}
cgroup_sk_free(struct sock_cgroup_data * skcd)770 static inline void cgroup_sk_free(struct sock_cgroup_data *skcd) {}
771 
772 #endif	/* CONFIG_CGROUP_DATA */
773 
774 struct cgroup_namespace {
775 	struct ns_common	ns;
776 	struct user_namespace	*user_ns;
777 	struct ucounts		*ucounts;
778 	struct css_set          *root_cset;
779 };
780 
781 extern struct cgroup_namespace init_cgroup_ns;
782 
783 #ifdef CONFIG_CGROUPS
784 
785 void free_cgroup_ns(struct cgroup_namespace *ns);
786 
787 struct cgroup_namespace *copy_cgroup_ns(unsigned long flags,
788 					struct user_namespace *user_ns,
789 					struct cgroup_namespace *old_ns);
790 
791 int cgroup_path_ns(struct cgroup *cgrp, char *buf, size_t buflen,
792 		   struct cgroup_namespace *ns);
793 
794 #else /* !CONFIG_CGROUPS */
795 
free_cgroup_ns(struct cgroup_namespace * ns)796 static inline void free_cgroup_ns(struct cgroup_namespace *ns) { }
797 static inline struct cgroup_namespace *
copy_cgroup_ns(unsigned long flags,struct user_namespace * user_ns,struct cgroup_namespace * old_ns)798 copy_cgroup_ns(unsigned long flags, struct user_namespace *user_ns,
799 	       struct cgroup_namespace *old_ns)
800 {
801 	return old_ns;
802 }
803 
804 #endif /* !CONFIG_CGROUPS */
805 
get_cgroup_ns(struct cgroup_namespace * ns)806 static inline void get_cgroup_ns(struct cgroup_namespace *ns)
807 {
808 	if (ns)
809 		refcount_inc(&ns->ns.count);
810 }
811 
put_cgroup_ns(struct cgroup_namespace * ns)812 static inline void put_cgroup_ns(struct cgroup_namespace *ns)
813 {
814 	if (ns && refcount_dec_and_test(&ns->ns.count))
815 		free_cgroup_ns(ns);
816 }
817 
818 #ifdef CONFIG_CGROUPS
819 
820 void cgroup_enter_frozen(void);
821 void cgroup_leave_frozen(bool always_leave);
822 void cgroup_update_frozen(struct cgroup *cgrp);
823 void cgroup_freeze(struct cgroup *cgrp, bool freeze);
824 void cgroup_freezer_migrate_task(struct task_struct *task, struct cgroup *src,
825 				 struct cgroup *dst);
826 
cgroup_task_frozen(struct task_struct * task)827 static inline bool cgroup_task_frozen(struct task_struct *task)
828 {
829 	return task->frozen;
830 }
831 
832 #else /* !CONFIG_CGROUPS */
833 
cgroup_enter_frozen(void)834 static inline void cgroup_enter_frozen(void) { }
cgroup_leave_frozen(bool always_leave)835 static inline void cgroup_leave_frozen(bool always_leave) { }
cgroup_task_frozen(struct task_struct * task)836 static inline bool cgroup_task_frozen(struct task_struct *task)
837 {
838 	return false;
839 }
840 
841 #endif /* !CONFIG_CGROUPS */
842 
843 #ifdef CONFIG_CGROUP_BPF
cgroup_bpf_get(struct cgroup * cgrp)844 static inline void cgroup_bpf_get(struct cgroup *cgrp)
845 {
846 	percpu_ref_get(&cgrp->bpf.refcnt);
847 }
848 
cgroup_bpf_put(struct cgroup * cgrp)849 static inline void cgroup_bpf_put(struct cgroup *cgrp)
850 {
851 	percpu_ref_put(&cgrp->bpf.refcnt);
852 }
853 
854 #else /* CONFIG_CGROUP_BPF */
855 
cgroup_bpf_get(struct cgroup * cgrp)856 static inline void cgroup_bpf_get(struct cgroup *cgrp) {}
cgroup_bpf_put(struct cgroup * cgrp)857 static inline void cgroup_bpf_put(struct cgroup *cgrp) {}
858 
859 #endif /* CONFIG_CGROUP_BPF */
860 
861 #endif /* _LINUX_CGROUP_H */
862