xref: /linux-6.15/include/linux/cgroup.h (revision fc4fa6e1)
1 #ifndef _LINUX_CGROUP_H
2 #define _LINUX_CGROUP_H
3 /*
4  *  cgroup interface
5  *
6  *  Copyright (C) 2003 BULL SA
7  *  Copyright (C) 2004-2006 Silicon Graphics, Inc.
8  *
9  */
10 
11 #include <linux/sched.h>
12 #include <linux/cpumask.h>
13 #include <linux/nodemask.h>
14 #include <linux/rculist.h>
15 #include <linux/cgroupstats.h>
16 #include <linux/fs.h>
17 #include <linux/seq_file.h>
18 #include <linux/kernfs.h>
19 #include <linux/jump_label.h>
20 
21 #include <linux/cgroup-defs.h>
22 
23 #ifdef CONFIG_CGROUPS
24 
25 /*
26  * All weight knobs on the default hierarhcy should use the following min,
27  * default and max values.  The default value is the logarithmic center of
28  * MIN and MAX and allows 100x to be expressed in both directions.
29  */
30 #define CGROUP_WEIGHT_MIN		1
31 #define CGROUP_WEIGHT_DFL		100
32 #define CGROUP_WEIGHT_MAX		10000
33 
34 /* a css_task_iter should be treated as an opaque object */
35 struct css_task_iter {
36 	struct cgroup_subsys		*ss;
37 
38 	struct list_head		*cset_pos;
39 	struct list_head		*cset_head;
40 
41 	struct list_head		*task_pos;
42 	struct list_head		*tasks_head;
43 	struct list_head		*mg_tasks_head;
44 
45 	struct css_set			*cur_cset;
46 	struct task_struct		*cur_task;
47 	struct list_head		iters_node;	/* css_set->task_iters */
48 };
49 
50 extern struct cgroup_root cgrp_dfl_root;
51 extern struct css_set init_css_set;
52 
53 #define SUBSYS(_x) extern struct cgroup_subsys _x ## _cgrp_subsys;
54 #include <linux/cgroup_subsys.h>
55 #undef SUBSYS
56 
57 #define SUBSYS(_x)								\
58 	extern struct static_key_true _x ## _cgrp_subsys_enabled_key;		\
59 	extern struct static_key_true _x ## _cgrp_subsys_on_dfl_key;
60 #include <linux/cgroup_subsys.h>
61 #undef SUBSYS
62 
63 /**
64  * cgroup_subsys_enabled - fast test on whether a subsys is enabled
65  * @ss: subsystem in question
66  */
67 #define cgroup_subsys_enabled(ss)						\
68 	static_branch_likely(&ss ## _enabled_key)
69 
70 /**
71  * cgroup_subsys_on_dfl - fast test on whether a subsys is on default hierarchy
72  * @ss: subsystem in question
73  */
74 #define cgroup_subsys_on_dfl(ss)						\
75 	static_branch_likely(&ss ## _on_dfl_key)
76 
77 bool css_has_online_children(struct cgroup_subsys_state *css);
78 struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss);
79 struct cgroup_subsys_state *cgroup_get_e_css(struct cgroup *cgroup,
80 					     struct cgroup_subsys *ss);
81 struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry,
82 						       struct cgroup_subsys *ss);
83 
84 bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor);
85 int cgroup_attach_task_all(struct task_struct *from, struct task_struct *);
86 int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from);
87 
88 int cgroup_add_dfl_cftypes(struct cgroup_subsys *ss, struct cftype *cfts);
89 int cgroup_add_legacy_cftypes(struct cgroup_subsys *ss, struct cftype *cfts);
90 int cgroup_rm_cftypes(struct cftype *cfts);
91 
92 char *task_cgroup_path(struct task_struct *task, char *buf, size_t buflen);
93 int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry);
94 int proc_cgroup_show(struct seq_file *m, struct pid_namespace *ns,
95 		     struct pid *pid, struct task_struct *tsk);
96 
97 void cgroup_fork(struct task_struct *p);
98 extern int cgroup_can_fork(struct task_struct *p,
99 			   void *ss_priv[CGROUP_CANFORK_COUNT]);
100 extern void cgroup_cancel_fork(struct task_struct *p,
101 			       void *ss_priv[CGROUP_CANFORK_COUNT]);
102 extern void cgroup_post_fork(struct task_struct *p,
103 			     void *old_ss_priv[CGROUP_CANFORK_COUNT]);
104 void cgroup_exit(struct task_struct *p);
105 void cgroup_free(struct task_struct *p);
106 
107 int cgroup_init_early(void);
108 int cgroup_init(void);
109 
110 /*
111  * Iteration helpers and macros.
112  */
113 
114 struct cgroup_subsys_state *css_next_child(struct cgroup_subsys_state *pos,
115 					   struct cgroup_subsys_state *parent);
116 struct cgroup_subsys_state *css_next_descendant_pre(struct cgroup_subsys_state *pos,
117 						    struct cgroup_subsys_state *css);
118 struct cgroup_subsys_state *css_rightmost_descendant(struct cgroup_subsys_state *pos);
119 struct cgroup_subsys_state *css_next_descendant_post(struct cgroup_subsys_state *pos,
120 						     struct cgroup_subsys_state *css);
121 
122 struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset);
123 struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset);
124 
125 void css_task_iter_start(struct cgroup_subsys_state *css,
126 			 struct css_task_iter *it);
127 struct task_struct *css_task_iter_next(struct css_task_iter *it);
128 void css_task_iter_end(struct css_task_iter *it);
129 
130 /**
131  * css_for_each_child - iterate through children of a css
132  * @pos: the css * to use as the loop cursor
133  * @parent: css whose children to walk
134  *
135  * Walk @parent's children.  Must be called under rcu_read_lock().
136  *
137  * If a subsystem synchronizes ->css_online() and the start of iteration, a
138  * css which finished ->css_online() is guaranteed to be visible in the
139  * future iterations and will stay visible until the last reference is put.
140  * A css which hasn't finished ->css_online() or already finished
141  * ->css_offline() may show up during traversal.  It's each subsystem's
142  * responsibility to synchronize against on/offlining.
143  *
144  * It is allowed to temporarily drop RCU read lock during iteration.  The
145  * caller is responsible for ensuring that @pos remains accessible until
146  * the start of the next iteration by, for example, bumping the css refcnt.
147  */
148 #define css_for_each_child(pos, parent)					\
149 	for ((pos) = css_next_child(NULL, (parent)); (pos);		\
150 	     (pos) = css_next_child((pos), (parent)))
151 
152 /**
153  * css_for_each_descendant_pre - pre-order walk of a css's descendants
154  * @pos: the css * to use as the loop cursor
155  * @root: css whose descendants to walk
156  *
157  * Walk @root's descendants.  @root is included in the iteration and the
158  * first node to be visited.  Must be called under rcu_read_lock().
159  *
160  * If a subsystem synchronizes ->css_online() and the start of iteration, a
161  * css which finished ->css_online() is guaranteed to be visible in the
162  * future iterations and will stay visible until the last reference is put.
163  * A css which hasn't finished ->css_online() or already finished
164  * ->css_offline() may show up during traversal.  It's each subsystem's
165  * responsibility to synchronize against on/offlining.
166  *
167  * For example, the following guarantees that a descendant can't escape
168  * state updates of its ancestors.
169  *
170  * my_online(@css)
171  * {
172  *	Lock @css's parent and @css;
173  *	Inherit state from the parent;
174  *	Unlock both.
175  * }
176  *
177  * my_update_state(@css)
178  * {
179  *	css_for_each_descendant_pre(@pos, @css) {
180  *		Lock @pos;
181  *		if (@pos == @css)
182  *			Update @css's state;
183  *		else
184  *			Verify @pos is alive and inherit state from its parent;
185  *		Unlock @pos;
186  *	}
187  * }
188  *
189  * As long as the inheriting step, including checking the parent state, is
190  * enclosed inside @pos locking, double-locking the parent isn't necessary
191  * while inheriting.  The state update to the parent is guaranteed to be
192  * visible by walking order and, as long as inheriting operations to the
193  * same @pos are atomic to each other, multiple updates racing each other
194  * still result in the correct state.  It's guaranateed that at least one
195  * inheritance happens for any css after the latest update to its parent.
196  *
197  * If checking parent's state requires locking the parent, each inheriting
198  * iteration should lock and unlock both @pos->parent and @pos.
199  *
200  * Alternatively, a subsystem may choose to use a single global lock to
201  * synchronize ->css_online() and ->css_offline() against tree-walking
202  * operations.
203  *
204  * It is allowed to temporarily drop RCU read lock during iteration.  The
205  * caller is responsible for ensuring that @pos remains accessible until
206  * the start of the next iteration by, for example, bumping the css refcnt.
207  */
208 #define css_for_each_descendant_pre(pos, css)				\
209 	for ((pos) = css_next_descendant_pre(NULL, (css)); (pos);	\
210 	     (pos) = css_next_descendant_pre((pos), (css)))
211 
212 /**
213  * css_for_each_descendant_post - post-order walk of a css's descendants
214  * @pos: the css * to use as the loop cursor
215  * @css: css whose descendants to walk
216  *
217  * Similar to css_for_each_descendant_pre() but performs post-order
218  * traversal instead.  @root is included in the iteration and the last
219  * node to be visited.
220  *
221  * If a subsystem synchronizes ->css_online() and the start of iteration, a
222  * css which finished ->css_online() is guaranteed to be visible in the
223  * future iterations and will stay visible until the last reference is put.
224  * A css which hasn't finished ->css_online() or already finished
225  * ->css_offline() may show up during traversal.  It's each subsystem's
226  * responsibility to synchronize against on/offlining.
227  *
228  * Note that the walk visibility guarantee example described in pre-order
229  * walk doesn't apply the same to post-order walks.
230  */
231 #define css_for_each_descendant_post(pos, css)				\
232 	for ((pos) = css_next_descendant_post(NULL, (css)); (pos);	\
233 	     (pos) = css_next_descendant_post((pos), (css)))
234 
235 /**
236  * cgroup_taskset_for_each - iterate cgroup_taskset
237  * @task: the loop cursor
238  * @tset: taskset to iterate
239  *
240  * @tset may contain multiple tasks and they may belong to multiple
241  * processes.  When there are multiple tasks in @tset, if a task of a
242  * process is in @tset, all tasks of the process are in @tset.  Also, all
243  * are guaranteed to share the same source and destination csses.
244  *
245  * Iteration is not in any specific order.
246  */
247 #define cgroup_taskset_for_each(task, tset)				\
248 	for ((task) = cgroup_taskset_first((tset)); (task);		\
249 	     (task) = cgroup_taskset_next((tset)))
250 
251 /**
252  * cgroup_taskset_for_each_leader - iterate group leaders in a cgroup_taskset
253  * @leader: the loop cursor
254  * @tset: takset to iterate
255  *
256  * Iterate threadgroup leaders of @tset.  For single-task migrations, @tset
257  * may not contain any.
258  */
259 #define cgroup_taskset_for_each_leader(leader, tset)			\
260 	for ((leader) = cgroup_taskset_first((tset)); (leader);		\
261 	     (leader) = cgroup_taskset_next((tset)))			\
262 		if ((leader) != (leader)->group_leader)			\
263 			;						\
264 		else
265 
266 /*
267  * Inline functions.
268  */
269 
270 /**
271  * css_get - obtain a reference on the specified css
272  * @css: target css
273  *
274  * The caller must already have a reference.
275  */
276 static inline void css_get(struct cgroup_subsys_state *css)
277 {
278 	if (!(css->flags & CSS_NO_REF))
279 		percpu_ref_get(&css->refcnt);
280 }
281 
282 /**
283  * css_get_many - obtain references on the specified css
284  * @css: target css
285  * @n: number of references to get
286  *
287  * The caller must already have a reference.
288  */
289 static inline void css_get_many(struct cgroup_subsys_state *css, unsigned int n)
290 {
291 	if (!(css->flags & CSS_NO_REF))
292 		percpu_ref_get_many(&css->refcnt, n);
293 }
294 
295 /**
296  * css_tryget - try to obtain a reference on the specified css
297  * @css: target css
298  *
299  * Obtain a reference on @css unless it already has reached zero and is
300  * being released.  This function doesn't care whether @css is on or
301  * offline.  The caller naturally needs to ensure that @css is accessible
302  * but doesn't have to be holding a reference on it - IOW, RCU protected
303  * access is good enough for this function.  Returns %true if a reference
304  * count was successfully obtained; %false otherwise.
305  */
306 static inline bool css_tryget(struct cgroup_subsys_state *css)
307 {
308 	if (!(css->flags & CSS_NO_REF))
309 		return percpu_ref_tryget(&css->refcnt);
310 	return true;
311 }
312 
313 /**
314  * css_tryget_online - try to obtain a reference on the specified css if online
315  * @css: target css
316  *
317  * Obtain a reference on @css if it's online.  The caller naturally needs
318  * to ensure that @css is accessible but doesn't have to be holding a
319  * reference on it - IOW, RCU protected access is good enough for this
320  * function.  Returns %true if a reference count was successfully obtained;
321  * %false otherwise.
322  */
323 static inline bool css_tryget_online(struct cgroup_subsys_state *css)
324 {
325 	if (!(css->flags & CSS_NO_REF))
326 		return percpu_ref_tryget_live(&css->refcnt);
327 	return true;
328 }
329 
330 /**
331  * css_put - put a css reference
332  * @css: target css
333  *
334  * Put a reference obtained via css_get() and css_tryget_online().
335  */
336 static inline void css_put(struct cgroup_subsys_state *css)
337 {
338 	if (!(css->flags & CSS_NO_REF))
339 		percpu_ref_put(&css->refcnt);
340 }
341 
342 /**
343  * css_put_many - put css references
344  * @css: target css
345  * @n: number of references to put
346  *
347  * Put references obtained via css_get() and css_tryget_online().
348  */
349 static inline void css_put_many(struct cgroup_subsys_state *css, unsigned int n)
350 {
351 	if (!(css->flags & CSS_NO_REF))
352 		percpu_ref_put_many(&css->refcnt, n);
353 }
354 
355 /**
356  * task_css_set_check - obtain a task's css_set with extra access conditions
357  * @task: the task to obtain css_set for
358  * @__c: extra condition expression to be passed to rcu_dereference_check()
359  *
360  * A task's css_set is RCU protected, initialized and exited while holding
361  * task_lock(), and can only be modified while holding both cgroup_mutex
362  * and task_lock() while the task is alive.  This macro verifies that the
363  * caller is inside proper critical section and returns @task's css_set.
364  *
365  * The caller can also specify additional allowed conditions via @__c, such
366  * as locks used during the cgroup_subsys::attach() methods.
367  */
368 #ifdef CONFIG_PROVE_RCU
369 extern struct mutex cgroup_mutex;
370 extern spinlock_t css_set_lock;
371 #define task_css_set_check(task, __c)					\
372 	rcu_dereference_check((task)->cgroups,				\
373 		lockdep_is_held(&cgroup_mutex) ||			\
374 		lockdep_is_held(&css_set_lock) ||			\
375 		((task)->flags & PF_EXITING) || (__c))
376 #else
377 #define task_css_set_check(task, __c)					\
378 	rcu_dereference((task)->cgroups)
379 #endif
380 
381 /**
382  * task_css_check - obtain css for (task, subsys) w/ extra access conds
383  * @task: the target task
384  * @subsys_id: the target subsystem ID
385  * @__c: extra condition expression to be passed to rcu_dereference_check()
386  *
387  * Return the cgroup_subsys_state for the (@task, @subsys_id) pair.  The
388  * synchronization rules are the same as task_css_set_check().
389  */
390 #define task_css_check(task, subsys_id, __c)				\
391 	task_css_set_check((task), (__c))->subsys[(subsys_id)]
392 
393 /**
394  * task_css_set - obtain a task's css_set
395  * @task: the task to obtain css_set for
396  *
397  * See task_css_set_check().
398  */
399 static inline struct css_set *task_css_set(struct task_struct *task)
400 {
401 	return task_css_set_check(task, false);
402 }
403 
404 /**
405  * task_css - obtain css for (task, subsys)
406  * @task: the target task
407  * @subsys_id: the target subsystem ID
408  *
409  * See task_css_check().
410  */
411 static inline struct cgroup_subsys_state *task_css(struct task_struct *task,
412 						   int subsys_id)
413 {
414 	return task_css_check(task, subsys_id, false);
415 }
416 
417 /**
418  * task_get_css - find and get the css for (task, subsys)
419  * @task: the target task
420  * @subsys_id: the target subsystem ID
421  *
422  * Find the css for the (@task, @subsys_id) combination, increment a
423  * reference on and return it.  This function is guaranteed to return a
424  * valid css.
425  */
426 static inline struct cgroup_subsys_state *
427 task_get_css(struct task_struct *task, int subsys_id)
428 {
429 	struct cgroup_subsys_state *css;
430 
431 	rcu_read_lock();
432 	while (true) {
433 		css = task_css(task, subsys_id);
434 		if (likely(css_tryget_online(css)))
435 			break;
436 		cpu_relax();
437 	}
438 	rcu_read_unlock();
439 	return css;
440 }
441 
442 /**
443  * task_css_is_root - test whether a task belongs to the root css
444  * @task: the target task
445  * @subsys_id: the target subsystem ID
446  *
447  * Test whether @task belongs to the root css on the specified subsystem.
448  * May be invoked in any context.
449  */
450 static inline bool task_css_is_root(struct task_struct *task, int subsys_id)
451 {
452 	return task_css_check(task, subsys_id, true) ==
453 		init_css_set.subsys[subsys_id];
454 }
455 
456 static inline struct cgroup *task_cgroup(struct task_struct *task,
457 					 int subsys_id)
458 {
459 	return task_css(task, subsys_id)->cgroup;
460 }
461 
462 /* no synchronization, the result can only be used as a hint */
463 static inline bool cgroup_is_populated(struct cgroup *cgrp)
464 {
465 	return cgrp->populated_cnt;
466 }
467 
468 /* returns ino associated with a cgroup */
469 static inline ino_t cgroup_ino(struct cgroup *cgrp)
470 {
471 	return cgrp->kn->ino;
472 }
473 
474 /* cft/css accessors for cftype->write() operation */
475 static inline struct cftype *of_cft(struct kernfs_open_file *of)
476 {
477 	return of->kn->priv;
478 }
479 
480 struct cgroup_subsys_state *of_css(struct kernfs_open_file *of);
481 
482 /* cft/css accessors for cftype->seq_*() operations */
483 static inline struct cftype *seq_cft(struct seq_file *seq)
484 {
485 	return of_cft(seq->private);
486 }
487 
488 static inline struct cgroup_subsys_state *seq_css(struct seq_file *seq)
489 {
490 	return of_css(seq->private);
491 }
492 
493 /*
494  * Name / path handling functions.  All are thin wrappers around the kernfs
495  * counterparts and can be called under any context.
496  */
497 
498 static inline int cgroup_name(struct cgroup *cgrp, char *buf, size_t buflen)
499 {
500 	return kernfs_name(cgrp->kn, buf, buflen);
501 }
502 
503 static inline char * __must_check cgroup_path(struct cgroup *cgrp, char *buf,
504 					      size_t buflen)
505 {
506 	return kernfs_path(cgrp->kn, buf, buflen);
507 }
508 
509 static inline void pr_cont_cgroup_name(struct cgroup *cgrp)
510 {
511 	pr_cont_kernfs_name(cgrp->kn);
512 }
513 
514 static inline void pr_cont_cgroup_path(struct cgroup *cgrp)
515 {
516 	pr_cont_kernfs_path(cgrp->kn);
517 }
518 
519 /**
520  * cgroup_file_notify - generate a file modified event for a cgroup_file
521  * @cfile: target cgroup_file
522  *
523  * @cfile must have been obtained by setting cftype->file_offset.
524  */
525 static inline void cgroup_file_notify(struct cgroup_file *cfile)
526 {
527 	/* might not have been created due to one of the CFTYPE selector flags */
528 	if (cfile->kn)
529 		kernfs_notify(cfile->kn);
530 }
531 
532 #else /* !CONFIG_CGROUPS */
533 
534 struct cgroup_subsys_state;
535 
536 static inline void css_put(struct cgroup_subsys_state *css) {}
537 static inline int cgroup_attach_task_all(struct task_struct *from,
538 					 struct task_struct *t) { return 0; }
539 static inline int cgroupstats_build(struct cgroupstats *stats,
540 				    struct dentry *dentry) { return -EINVAL; }
541 
542 static inline void cgroup_fork(struct task_struct *p) {}
543 static inline int cgroup_can_fork(struct task_struct *p,
544 				  void *ss_priv[CGROUP_CANFORK_COUNT])
545 { return 0; }
546 static inline void cgroup_cancel_fork(struct task_struct *p,
547 				      void *ss_priv[CGROUP_CANFORK_COUNT]) {}
548 static inline void cgroup_post_fork(struct task_struct *p,
549 				    void *ss_priv[CGROUP_CANFORK_COUNT]) {}
550 static inline void cgroup_exit(struct task_struct *p) {}
551 static inline void cgroup_free(struct task_struct *p) {}
552 
553 static inline int cgroup_init_early(void) { return 0; }
554 static inline int cgroup_init(void) { return 0; }
555 
556 #endif /* !CONFIG_CGROUPS */
557 
558 #endif /* _LINUX_CGROUP_H */
559