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