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