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