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