blob: 44129793c7b8be92f92b5ffd35b43dbadb8c1988 [file] [log] [blame]
Paul Menageddbcc7e2007-10-18 23:39:30 -07001#ifndef _LINUX_CGROUP_H
2#define _LINUX_CGROUP_H
3/*
4 * cgroup interface
5 *
6 * Copyright (C) 2003 BULL SA
7 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
8 *
9 */
10
11#include <linux/sched.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070012#include <linux/cpumask.h>
13#include <linux/nodemask.h>
Tejun Heoeb6fd502012-11-09 09:12:29 -080014#include <linux/rculist.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070015#include <linux/cgroupstats.h>
Tejun Heo25a7e682013-04-14 20:15:25 -070016#include <linux/fs.h>
Tejun Heo7da11272013-12-05 12:28:04 -050017#include <linux/seq_file.h>
Tejun Heo2bd59d42014-02-11 11:52:49 -050018#include <linux/kernfs.h>
Tejun Heo49d1dc42015-09-18 11:56:28 -040019#include <linux/jump_label.h>
Aditya Kalia79a9082016-01-29 02:54:06 -060020#include <linux/nsproxy.h>
21#include <linux/types.h>
22#include <linux/ns_common.h>
23#include <linux/nsproxy.h>
24#include <linux/user_namespace.h>
Elena Reshetova387ad962017-02-20 12:19:00 +020025#include <linux/refcount.h>
Tejun Heob4a04ab2015-05-13 15:38:40 -040026
27#include <linux/cgroup-defs.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070028
29#ifdef CONFIG_CGROUPS
30
Tejun Heo6abc8ca2015-08-04 15:20:55 -040031/*
32 * All weight knobs on the default hierarhcy should use the following min,
33 * default and max values. The default value is the logarithmic center of
34 * MIN and MAX and allows 100x to be expressed in both directions.
35 */
36#define CGROUP_WEIGHT_MIN 1
37#define CGROUP_WEIGHT_DFL 100
38#define CGROUP_WEIGHT_MAX 10000
39
Tejun Heoc326aa22015-05-13 16:24:16 -040040/* a css_task_iter should be treated as an opaque object */
41struct css_task_iter {
42 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -070043
Tejun Heoc326aa22015-05-13 16:24:16 -040044 struct list_head *cset_pos;
45 struct list_head *cset_head;
Paul Menageddbcc7e2007-10-18 23:39:30 -070046
Tejun Heoc326aa22015-05-13 16:24:16 -040047 struct list_head *task_pos;
48 struct list_head *tasks_head;
49 struct list_head *mg_tasks_head;
Tejun Heoed27b9f2015-10-15 16:41:52 -040050
51 struct css_set *cur_cset;
52 struct task_struct *cur_task;
53 struct list_head iters_node; /* css_set->task_iters */
Paul Menage817929e2007-10-18 23:39:36 -070054};
Tejun Heoc326aa22015-05-13 16:24:16 -040055
56extern struct cgroup_root cgrp_dfl_root;
57extern struct css_set init_css_set;
58
59#define SUBSYS(_x) extern struct cgroup_subsys _x ## _cgrp_subsys;
60#include <linux/cgroup_subsys.h>
Paul Menage817929e2007-10-18 23:39:36 -070061#undef SUBSYS
62
Tejun Heo49d1dc42015-09-18 11:56:28 -040063#define SUBSYS(_x) \
64 extern struct static_key_true _x ## _cgrp_subsys_enabled_key; \
65 extern struct static_key_true _x ## _cgrp_subsys_on_dfl_key;
66#include <linux/cgroup_subsys.h>
67#undef SUBSYS
68
69/**
70 * cgroup_subsys_enabled - fast test on whether a subsys is enabled
71 * @ss: subsystem in question
72 */
73#define cgroup_subsys_enabled(ss) \
74 static_branch_likely(&ss ## _enabled_key)
75
76/**
77 * cgroup_subsys_on_dfl - fast test on whether a subsys is on default hierarchy
78 * @ss: subsystem in question
79 */
80#define cgroup_subsys_on_dfl(ss) \
81 static_branch_likely(&ss ## _on_dfl_key)
82
Tejun Heoc326aa22015-05-13 16:24:16 -040083bool css_has_online_children(struct cgroup_subsys_state *css);
84struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss);
85struct cgroup_subsys_state *cgroup_get_e_css(struct cgroup *cgroup,
86 struct cgroup_subsys *ss);
87struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry,
88 struct cgroup_subsys *ss);
89
Tejun Heo16af4392015-11-20 15:55:52 -050090struct cgroup *cgroup_get_from_path(const char *path);
Martin KaFai Lau1f3fe7e2016-06-30 10:28:42 -070091struct cgroup *cgroup_get_from_fd(int fd);
Tejun Heo16af4392015-11-20 15:55:52 -050092
Tejun Heoc326aa22015-05-13 16:24:16 -040093int cgroup_attach_task_all(struct task_struct *from, struct task_struct *);
94int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from);
95
96int cgroup_add_dfl_cftypes(struct cgroup_subsys *ss, struct cftype *cfts);
97int cgroup_add_legacy_cftypes(struct cgroup_subsys *ss, struct cftype *cfts);
98int cgroup_rm_cftypes(struct cftype *cfts);
Tejun Heo34c06252015-11-05 00:12:24 -050099void cgroup_file_notify(struct cgroup_file *cfile);
Tejun Heoc326aa22015-05-13 16:24:16 -0400100
Tejun Heo4c737b42016-08-10 11:23:44 -0400101int task_cgroup_path(struct task_struct *task, char *buf, size_t buflen);
Tejun Heoc326aa22015-05-13 16:24:16 -0400102int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry);
103int proc_cgroup_show(struct seq_file *m, struct pid_namespace *ns,
104 struct pid *pid, struct task_struct *tsk);
105
106void cgroup_fork(struct task_struct *p);
Oleg Nesterovb53202e2015-12-03 10:24:08 -0500107extern int cgroup_can_fork(struct task_struct *p);
108extern void cgroup_cancel_fork(struct task_struct *p);
109extern void cgroup_post_fork(struct task_struct *p);
Tejun Heoc326aa22015-05-13 16:24:16 -0400110void cgroup_exit(struct task_struct *p);
Tejun Heo2e91fa72015-10-15 16:41:53 -0400111void cgroup_free(struct task_struct *p);
Tejun Heoc326aa22015-05-13 16:24:16 -0400112
113int cgroup_init_early(void);
114int cgroup_init(void);
115
Tejun Heo5c9d5352014-05-16 13:22:48 -0400116/*
Tejun Heoc326aa22015-05-13 16:24:16 -0400117 * Iteration helpers and macros.
Tejun Heo5c9d5352014-05-16 13:22:48 -0400118 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700119
Tejun Heoc326aa22015-05-13 16:24:16 -0400120struct cgroup_subsys_state *css_next_child(struct cgroup_subsys_state *pos,
121 struct cgroup_subsys_state *parent);
122struct cgroup_subsys_state *css_next_descendant_pre(struct cgroup_subsys_state *pos,
123 struct cgroup_subsys_state *css);
124struct cgroup_subsys_state *css_rightmost_descendant(struct cgroup_subsys_state *pos);
125struct cgroup_subsys_state *css_next_descendant_post(struct cgroup_subsys_state *pos,
126 struct cgroup_subsys_state *css);
Tejun Heo72c97e52013-08-08 20:11:22 -0400127
Tejun Heo1f7dd3e52015-12-03 10:18:21 -0500128struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset,
129 struct cgroup_subsys_state **dst_cssp);
130struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset,
131 struct cgroup_subsys_state **dst_cssp);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700132
Tejun Heoc326aa22015-05-13 16:24:16 -0400133void css_task_iter_start(struct cgroup_subsys_state *css,
134 struct css_task_iter *it);
135struct task_struct *css_task_iter_next(struct css_task_iter *it);
136void css_task_iter_end(struct css_task_iter *it);
Tejun Heo0ae78e02013-08-13 11:01:54 -0400137
Tejun Heoc326aa22015-05-13 16:24:16 -0400138/**
139 * css_for_each_child - iterate through children of a css
140 * @pos: the css * to use as the loop cursor
141 * @parent: css whose children to walk
142 *
143 * Walk @parent's children. Must be called under rcu_read_lock().
144 *
145 * If a subsystem synchronizes ->css_online() and the start of iteration, a
146 * css which finished ->css_online() is guaranteed to be visible in the
147 * future iterations and will stay visible until the last reference is put.
148 * A css which hasn't finished ->css_online() or already finished
149 * ->css_offline() may show up during traversal. It's each subsystem's
150 * responsibility to synchronize against on/offlining.
151 *
152 * It is allowed to temporarily drop RCU read lock during iteration. The
153 * caller is responsible for ensuring that @pos remains accessible until
154 * the start of the next iteration by, for example, bumping the css refcnt.
155 */
156#define css_for_each_child(pos, parent) \
157 for ((pos) = css_next_child(NULL, (parent)); (pos); \
158 (pos) = css_next_child((pos), (parent)))
Tejun Heod5c419b2014-05-16 13:22:48 -0400159
Tejun Heoc326aa22015-05-13 16:24:16 -0400160/**
161 * css_for_each_descendant_pre - pre-order walk of a css's descendants
162 * @pos: the css * to use as the loop cursor
163 * @root: css whose descendants to walk
164 *
165 * Walk @root's descendants. @root is included in the iteration and the
166 * first node to be visited. Must be called under rcu_read_lock().
167 *
168 * If a subsystem synchronizes ->css_online() and the start of iteration, a
169 * css which finished ->css_online() is guaranteed to be visible in the
170 * future iterations and will stay visible until the last reference is put.
171 * A css which hasn't finished ->css_online() or already finished
172 * ->css_offline() may show up during traversal. It's each subsystem's
173 * responsibility to synchronize against on/offlining.
174 *
175 * For example, the following guarantees that a descendant can't escape
176 * state updates of its ancestors.
177 *
178 * my_online(@css)
179 * {
180 * Lock @css's parent and @css;
181 * Inherit state from the parent;
182 * Unlock both.
183 * }
184 *
185 * my_update_state(@css)
186 * {
187 * css_for_each_descendant_pre(@pos, @css) {
188 * Lock @pos;
189 * if (@pos == @css)
190 * Update @css's state;
191 * else
192 * Verify @pos is alive and inherit state from its parent;
193 * Unlock @pos;
194 * }
195 * }
196 *
197 * As long as the inheriting step, including checking the parent state, is
198 * enclosed inside @pos locking, double-locking the parent isn't necessary
199 * while inheriting. The state update to the parent is guaranteed to be
200 * visible by walking order and, as long as inheriting operations to the
201 * same @pos are atomic to each other, multiple updates racing each other
202 * still result in the correct state. It's guaranateed that at least one
203 * inheritance happens for any css after the latest update to its parent.
204 *
205 * If checking parent's state requires locking the parent, each inheriting
206 * iteration should lock and unlock both @pos->parent and @pos.
207 *
208 * Alternatively, a subsystem may choose to use a single global lock to
209 * synchronize ->css_online() and ->css_offline() against tree-walking
210 * operations.
211 *
212 * It is allowed to temporarily drop RCU read lock during iteration. The
213 * caller is responsible for ensuring that @pos remains accessible until
214 * the start of the next iteration by, for example, bumping the css refcnt.
215 */
216#define css_for_each_descendant_pre(pos, css) \
217 for ((pos) = css_next_descendant_pre(NULL, (css)); (pos); \
218 (pos) = css_next_descendant_pre((pos), (css)))
Tejun Heo15a4c832014-05-04 15:09:14 -0400219
Tejun Heoc326aa22015-05-13 16:24:16 -0400220/**
221 * css_for_each_descendant_post - post-order walk of a css's descendants
222 * @pos: the css * to use as the loop cursor
223 * @css: css whose descendants to walk
224 *
225 * Similar to css_for_each_descendant_pre() but performs post-order
226 * traversal instead. @root is included in the iteration and the last
227 * node to be visited.
228 *
229 * If a subsystem synchronizes ->css_online() and the start of iteration, a
230 * css which finished ->css_online() is guaranteed to be visible in the
231 * future iterations and will stay visible until the last reference is put.
232 * A css which hasn't finished ->css_online() or already finished
233 * ->css_offline() may show up during traversal. It's each subsystem's
234 * responsibility to synchronize against on/offlining.
235 *
236 * Note that the walk visibility guarantee example described in pre-order
237 * walk doesn't apply the same to post-order walks.
238 */
239#define css_for_each_descendant_post(pos, css) \
240 for ((pos) = css_next_descendant_post(NULL, (css)); (pos); \
241 (pos) = css_next_descendant_post((pos), (css)))
Tejun Heo48ddbe12012-04-01 12:09:56 -0700242
Tejun Heoc326aa22015-05-13 16:24:16 -0400243/**
244 * cgroup_taskset_for_each - iterate cgroup_taskset
245 * @task: the loop cursor
Tejun Heo1f7dd3e52015-12-03 10:18:21 -0500246 * @dst_css: the destination css
Tejun Heoc326aa22015-05-13 16:24:16 -0400247 * @tset: taskset to iterate
Tejun Heo4530edd2015-09-11 15:00:19 -0400248 *
249 * @tset may contain multiple tasks and they may belong to multiple
Tejun Heo1f7dd3e52015-12-03 10:18:21 -0500250 * processes.
251 *
252 * On the v2 hierarchy, there may be tasks from multiple processes and they
253 * may not share the source or destination csses.
254 *
255 * On traditional hierarchies, when there are multiple tasks in @tset, if a
256 * task of a process is in @tset, all tasks of the process are in @tset.
257 * Also, all are guaranteed to share the same source and destination csses.
Tejun Heo4530edd2015-09-11 15:00:19 -0400258 *
259 * Iteration is not in any specific order.
Tejun Heoc326aa22015-05-13 16:24:16 -0400260 */
Tejun Heo1f7dd3e52015-12-03 10:18:21 -0500261#define cgroup_taskset_for_each(task, dst_css, tset) \
262 for ((task) = cgroup_taskset_first((tset), &(dst_css)); \
263 (task); \
264 (task) = cgroup_taskset_next((tset), &(dst_css)))
Tejun Heo0cb51d72014-05-16 13:22:49 -0400265
Tejun Heo4530edd2015-09-11 15:00:19 -0400266/**
267 * cgroup_taskset_for_each_leader - iterate group leaders in a cgroup_taskset
268 * @leader: the loop cursor
Tejun Heo1f7dd3e52015-12-03 10:18:21 -0500269 * @dst_css: the destination css
Geliang Tang7b4632f2016-12-24 23:28:35 +0800270 * @tset: taskset to iterate
Tejun Heo4530edd2015-09-11 15:00:19 -0400271 *
272 * Iterate threadgroup leaders of @tset. For single-task migrations, @tset
273 * may not contain any.
274 */
Tejun Heo1f7dd3e52015-12-03 10:18:21 -0500275#define cgroup_taskset_for_each_leader(leader, dst_css, tset) \
276 for ((leader) = cgroup_taskset_first((tset), &(dst_css)); \
277 (leader); \
278 (leader) = cgroup_taskset_next((tset), &(dst_css))) \
Tejun Heo4530edd2015-09-11 15:00:19 -0400279 if ((leader) != (leader)->group_leader) \
280 ; \
281 else
282
Tejun Heoc326aa22015-05-13 16:24:16 -0400283/*
284 * Inline functions.
285 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700286
Tejun Heo5de01072013-06-12 21:04:52 -0700287/**
288 * css_get - obtain a reference on the specified css
289 * @css: target css
290 *
291 * The caller must already have a reference.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700292 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700293static inline void css_get(struct cgroup_subsys_state *css)
294{
Tejun Heo3b514d22014-05-16 13:22:47 -0400295 if (!(css->flags & CSS_NO_REF))
296 percpu_ref_get(&css->refcnt);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700297}
Paul Menagee7c5ec92009-01-07 18:08:38 -0800298
Tejun Heo5de01072013-06-12 21:04:52 -0700299/**
Johannes Weinere8ea14c2014-12-10 15:42:42 -0800300 * css_get_many - obtain references on the specified css
301 * @css: target css
302 * @n: number of references to get
303 *
304 * The caller must already have a reference.
305 */
306static inline void css_get_many(struct cgroup_subsys_state *css, unsigned int n)
307{
308 if (!(css->flags & CSS_NO_REF))
309 percpu_ref_get_many(&css->refcnt, n);
310}
311
312/**
Tejun Heo6f4524d2014-05-16 13:22:52 -0400313 * css_tryget - try to obtain a reference on the specified css
314 * @css: target css
315 *
316 * Obtain a reference on @css unless it already has reached zero and is
317 * being released. This function doesn't care whether @css is on or
318 * offline. The caller naturally needs to ensure that @css is accessible
319 * but doesn't have to be holding a reference on it - IOW, RCU protected
320 * access is good enough for this function. Returns %true if a reference
321 * count was successfully obtained; %false otherwise.
322 */
323static inline bool css_tryget(struct cgroup_subsys_state *css)
324{
325 if (!(css->flags & CSS_NO_REF))
326 return percpu_ref_tryget(&css->refcnt);
327 return true;
328}
329
330/**
Tejun Heoec903c02014-05-13 12:11:01 -0400331 * css_tryget_online - try to obtain a reference on the specified css if online
Tejun Heo5de01072013-06-12 21:04:52 -0700332 * @css: target css
333 *
Tejun Heoec903c02014-05-13 12:11:01 -0400334 * Obtain a reference on @css if it's online. The caller naturally needs
335 * to ensure that @css is accessible but doesn't have to be holding a
Tejun Heo5de01072013-06-12 21:04:52 -0700336 * reference on it - IOW, RCU protected access is good enough for this
337 * function. Returns %true if a reference count was successfully obtained;
338 * %false otherwise.
339 */
Tejun Heoec903c02014-05-13 12:11:01 -0400340static inline bool css_tryget_online(struct cgroup_subsys_state *css)
Paul Menagee7c5ec92009-01-07 18:08:38 -0800341{
Tejun Heo3b514d22014-05-16 13:22:47 -0400342 if (!(css->flags & CSS_NO_REF))
343 return percpu_ref_tryget_live(&css->refcnt);
344 return true;
Paul Menagee7c5ec92009-01-07 18:08:38 -0800345}
346
Tejun Heo5de01072013-06-12 21:04:52 -0700347/**
348 * css_put - put a css reference
349 * @css: target css
350 *
Tejun Heoec903c02014-05-13 12:11:01 -0400351 * Put a reference obtained via css_get() and css_tryget_online().
Tejun Heo5de01072013-06-12 21:04:52 -0700352 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700353static inline void css_put(struct cgroup_subsys_state *css)
354{
Tejun Heo3b514d22014-05-16 13:22:47 -0400355 if (!(css->flags & CSS_NO_REF))
356 percpu_ref_put(&css->refcnt);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700357}
358
Johannes Weinere8ea14c2014-12-10 15:42:42 -0800359/**
360 * css_put_many - put css references
361 * @css: target css
362 * @n: number of references to put
363 *
364 * Put references obtained via css_get() and css_tryget_online().
365 */
366static inline void css_put_many(struct cgroup_subsys_state *css, unsigned int n)
367{
368 if (!(css->flags & CSS_NO_REF))
369 percpu_ref_put_many(&css->refcnt, n);
370}
371
Tejun Heo16af4392015-11-20 15:55:52 -0500372static inline void cgroup_put(struct cgroup *cgrp)
373{
374 css_put(&cgrp->self);
375}
376
Tejun Heoc326aa22015-05-13 16:24:16 -0400377/**
378 * task_css_set_check - obtain a task's css_set with extra access conditions
379 * @task: the task to obtain css_set for
380 * @__c: extra condition expression to be passed to rcu_dereference_check()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700381 *
Tejun Heoc326aa22015-05-13 16:24:16 -0400382 * A task's css_set is RCU protected, initialized and exited while holding
383 * task_lock(), and can only be modified while holding both cgroup_mutex
384 * and task_lock() while the task is alive. This macro verifies that the
385 * caller is inside proper critical section and returns @task's css_set.
386 *
387 * The caller can also specify additional allowed conditions via @__c, such
388 * as locks used during the cgroup_subsys::attach() methods.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700389 */
Tejun Heoc326aa22015-05-13 16:24:16 -0400390#ifdef CONFIG_PROVE_RCU
391extern struct mutex cgroup_mutex;
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400392extern spinlock_t css_set_lock;
Tejun Heoc326aa22015-05-13 16:24:16 -0400393#define task_css_set_check(task, __c) \
394 rcu_dereference_check((task)->cgroups, \
395 lockdep_is_held(&cgroup_mutex) || \
Tejun Heof0d9a5f2015-10-15 16:41:53 -0400396 lockdep_is_held(&css_set_lock) || \
Tejun Heoc326aa22015-05-13 16:24:16 -0400397 ((task)->flags & PF_EXITING) || (__c))
398#else
399#define task_css_set_check(task, __c) \
400 rcu_dereference((task)->cgroups)
Tejun Heo2bd59d42014-02-11 11:52:49 -0500401#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -0700402
Tejun Heoc326aa22015-05-13 16:24:16 -0400403/**
404 * task_css_check - obtain css for (task, subsys) w/ extra access conds
405 * @task: the target task
406 * @subsys_id: the target subsystem ID
407 * @__c: extra condition expression to be passed to rcu_dereference_check()
408 *
409 * Return the cgroup_subsys_state for the (@task, @subsys_id) pair. The
410 * synchronization rules are the same as task_css_set_check().
411 */
412#define task_css_check(task, subsys_id, __c) \
413 task_css_set_check((task), (__c))->subsys[(subsys_id)]
414
415/**
416 * task_css_set - obtain a task's css_set
417 * @task: the task to obtain css_set for
418 *
419 * See task_css_set_check().
420 */
421static inline struct css_set *task_css_set(struct task_struct *task)
422{
423 return task_css_set_check(task, false);
424}
425
426/**
427 * task_css - obtain css for (task, subsys)
428 * @task: the target task
429 * @subsys_id: the target subsystem ID
430 *
431 * See task_css_check().
432 */
433static inline struct cgroup_subsys_state *task_css(struct task_struct *task,
434 int subsys_id)
435{
436 return task_css_check(task, subsys_id, false);
437}
438
439/**
Linus Torvaldsbbe179f2015-06-26 19:50:04 -0700440 * task_get_css - find and get the css for (task, subsys)
441 * @task: the target task
442 * @subsys_id: the target subsystem ID
443 *
444 * Find the css for the (@task, @subsys_id) combination, increment a
445 * reference on and return it. This function is guaranteed to return a
446 * valid css.
447 */
448static inline struct cgroup_subsys_state *
449task_get_css(struct task_struct *task, int subsys_id)
450{
451 struct cgroup_subsys_state *css;
452
453 rcu_read_lock();
454 while (true) {
455 css = task_css(task, subsys_id);
456 if (likely(css_tryget_online(css)))
457 break;
458 cpu_relax();
459 }
460 rcu_read_unlock();
461 return css;
462}
463
464/**
Tejun Heoc326aa22015-05-13 16:24:16 -0400465 * task_css_is_root - test whether a task belongs to the root css
466 * @task: the target task
467 * @subsys_id: the target subsystem ID
468 *
469 * Test whether @task belongs to the root css on the specified subsystem.
470 * May be invoked in any context.
471 */
472static inline bool task_css_is_root(struct task_struct *task, int subsys_id)
473{
474 return task_css_check(task, subsys_id, true) ==
475 init_css_set.subsys[subsys_id];
476}
477
478static inline struct cgroup *task_cgroup(struct task_struct *task,
479 int subsys_id)
480{
481 return task_css(task, subsys_id)->cgroup;
482}
Tejun Heoa2dd4242014-03-19 10:23:55 -0400483
Tejun Heob11cfb52015-11-20 15:55:52 -0500484/**
485 * cgroup_is_descendant - test ancestry
486 * @cgrp: the cgroup to be tested
487 * @ancestor: possible ancestor of @cgrp
488 *
489 * Test whether @cgrp is a descendant of @ancestor. It also returns %true
490 * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
491 * and @ancestor are accessible.
492 */
493static inline bool cgroup_is_descendant(struct cgroup *cgrp,
494 struct cgroup *ancestor)
495{
496 if (cgrp->root != ancestor->root || cgrp->level < ancestor->level)
497 return false;
498 return cgrp->ancestor_ids[ancestor->level] == ancestor->id;
499}
500
Sargun Dhillonaed704b2016-08-12 08:56:40 -0700501/**
502 * task_under_cgroup_hierarchy - test task's membership of cgroup ancestry
503 * @task: the task to be tested
504 * @ancestor: possible ancestor of @task's cgroup
505 *
506 * Tests whether @task's default cgroup hierarchy is a descendant of @ancestor.
507 * It follows all the same rules as cgroup_is_descendant, and only applies
508 * to the default hierarchy.
509 */
510static inline bool task_under_cgroup_hierarchy(struct task_struct *task,
511 struct cgroup *ancestor)
512{
513 struct css_set *cset = task_css_set(task);
514
515 return cgroup_is_descendant(cset->dfl_cgrp, ancestor);
516}
517
Tejun Heo07bc3562014-02-13 06:58:39 -0500518/* no synchronization, the result can only be used as a hint */
Tejun Heo27bd4db2015-10-15 16:41:50 -0400519static inline bool cgroup_is_populated(struct cgroup *cgrp)
Tejun Heo07bc3562014-02-13 06:58:39 -0500520{
Tejun Heo27bd4db2015-10-15 16:41:50 -0400521 return cgrp->populated_cnt;
Tejun Heo07bc3562014-02-13 06:58:39 -0500522}
523
Zefan Lif29374b2014-09-19 16:29:31 +0800524/* returns ino associated with a cgroup */
Tejun Heob1664922014-02-11 11:52:49 -0500525static inline ino_t cgroup_ino(struct cgroup *cgrp)
526{
Zefan Lif29374b2014-09-19 16:29:31 +0800527 return cgrp->kn->ino;
Tejun Heob1664922014-02-11 11:52:49 -0500528}
529
Tejun Heob4168642014-05-13 12:16:21 -0400530/* cft/css accessors for cftype->write() operation */
531static inline struct cftype *of_cft(struct kernfs_open_file *of)
Tejun Heo7da11272013-12-05 12:28:04 -0500532{
Tejun Heo2bd59d42014-02-11 11:52:49 -0500533 return of->kn->priv;
Tejun Heo7da11272013-12-05 12:28:04 -0500534}
535
Tejun Heob4168642014-05-13 12:16:21 -0400536struct cgroup_subsys_state *of_css(struct kernfs_open_file *of);
537
538/* cft/css accessors for cftype->seq_*() operations */
539static inline struct cftype *seq_cft(struct seq_file *seq)
540{
541 return of_cft(seq->private);
542}
543
544static inline struct cgroup_subsys_state *seq_css(struct seq_file *seq)
545{
546 return of_css(seq->private);
547}
Tejun Heo59f52962014-02-11 11:52:49 -0500548
Tejun Heoe61734c2014-02-12 09:29:50 -0500549/*
550 * Name / path handling functions. All are thin wrappers around the kernfs
551 * counterparts and can be called under any context.
552 */
553
554static inline int cgroup_name(struct cgroup *cgrp, char *buf, size_t buflen)
555{
Tejun Heofdce6bf2014-03-19 10:23:54 -0400556 return kernfs_name(cgrp->kn, buf, buflen);
Tejun Heoe61734c2014-02-12 09:29:50 -0500557}
558
Tejun Heo4c737b42016-08-10 11:23:44 -0400559static inline int cgroup_path(struct cgroup *cgrp, char *buf, size_t buflen)
Tejun Heoe61734c2014-02-12 09:29:50 -0500560{
Tejun Heofdce6bf2014-03-19 10:23:54 -0400561 return kernfs_path(cgrp->kn, buf, buflen);
Tejun Heoe61734c2014-02-12 09:29:50 -0500562}
563
564static inline void pr_cont_cgroup_name(struct cgroup *cgrp)
565{
Tejun Heofdce6bf2014-03-19 10:23:54 -0400566 pr_cont_kernfs_name(cgrp->kn);
Tejun Heoe61734c2014-02-12 09:29:50 -0500567}
568
569static inline void pr_cont_cgroup_path(struct cgroup *cgrp)
570{
Tejun Heofdce6bf2014-03-19 10:23:54 -0400571 pr_cont_kernfs_path(cgrp->kn);
Tejun Heoe61734c2014-02-12 09:29:50 -0500572}
573
Paul Menageddbcc7e2007-10-18 23:39:30 -0700574#else /* !CONFIG_CGROUPS */
575
Tejun Heof3ba5382015-01-06 12:02:46 -0500576struct cgroup_subsys_state;
Sargun Dhillonaed704b2016-08-12 08:56:40 -0700577struct cgroup;
Tejun Heof3ba5382015-01-06 12:02:46 -0500578
Tejun Heoc326aa22015-05-13 16:24:16 -0400579static inline void css_put(struct cgroup_subsys_state *css) {}
580static inline int cgroup_attach_task_all(struct task_struct *from,
581 struct task_struct *t) { return 0; }
582static inline int cgroupstats_build(struct cgroupstats *stats,
583 struct dentry *dentry) { return -EINVAL; }
584
Paul Menageb4f48b62007-10-18 23:39:33 -0700585static inline void cgroup_fork(struct task_struct *p) {}
Oleg Nesterovb53202e2015-12-03 10:24:08 -0500586static inline int cgroup_can_fork(struct task_struct *p) { return 0; }
587static inline void cgroup_cancel_fork(struct task_struct *p) {}
588static inline void cgroup_post_fork(struct task_struct *p) {}
Li Zefan1ec41832014-03-28 15:22:19 +0800589static inline void cgroup_exit(struct task_struct *p) {}
Tejun Heo2e91fa72015-10-15 16:41:53 -0400590static inline void cgroup_free(struct task_struct *p) {}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700591
Tejun Heoc326aa22015-05-13 16:24:16 -0400592static inline int cgroup_init_early(void) { return 0; }
593static inline int cgroup_init(void) { return 0; }
Sridhar Samudralad7926ee2010-05-30 22:24:39 +0200594
Sargun Dhillonaed704b2016-08-12 08:56:40 -0700595static inline bool task_under_cgroup_hierarchy(struct task_struct *task,
596 struct cgroup *ancestor)
597{
598 return true;
599}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700600#endif /* !CONFIG_CGROUPS */
601
Tejun Heobd1060a2015-12-07 17:38:53 -0500602/*
603 * sock->sk_cgrp_data handling. For more info, see sock_cgroup_data
604 * definition in cgroup-defs.h.
605 */
606#ifdef CONFIG_SOCK_CGROUP_DATA
607
608#if defined(CONFIG_CGROUP_NET_PRIO) || defined(CONFIG_CGROUP_NET_CLASSID)
609extern spinlock_t cgroup_sk_update_lock;
610#endif
611
612void cgroup_sk_alloc_disable(void);
613void cgroup_sk_alloc(struct sock_cgroup_data *skcd);
614void cgroup_sk_free(struct sock_cgroup_data *skcd);
615
616static inline struct cgroup *sock_cgroup_ptr(struct sock_cgroup_data *skcd)
617{
618#if defined(CONFIG_CGROUP_NET_PRIO) || defined(CONFIG_CGROUP_NET_CLASSID)
619 unsigned long v;
620
621 /*
622 * @skcd->val is 64bit but the following is safe on 32bit too as we
623 * just need the lower ulong to be written and read atomically.
624 */
625 v = READ_ONCE(skcd->val);
626
627 if (v & 1)
628 return &cgrp_dfl_root.cgrp;
629
630 return (struct cgroup *)(unsigned long)v ?: &cgrp_dfl_root.cgrp;
631#else
632 return (struct cgroup *)(unsigned long)skcd->val;
633#endif
634}
635
636#else /* CONFIG_CGROUP_DATA */
637
638static inline void cgroup_sk_alloc(struct sock_cgroup_data *skcd) {}
639static inline void cgroup_sk_free(struct sock_cgroup_data *skcd) {}
640
641#endif /* CONFIG_CGROUP_DATA */
642
Aditya Kalia79a9082016-01-29 02:54:06 -0600643struct cgroup_namespace {
Elena Reshetova387ad962017-02-20 12:19:00 +0200644 refcount_t count;
Aditya Kalia79a9082016-01-29 02:54:06 -0600645 struct ns_common ns;
646 struct user_namespace *user_ns;
Eric W. Biedermand08311d2016-08-08 14:25:30 -0500647 struct ucounts *ucounts;
Aditya Kalia79a9082016-01-29 02:54:06 -0600648 struct css_set *root_cset;
649};
650
651extern struct cgroup_namespace init_cgroup_ns;
652
653#ifdef CONFIG_CGROUPS
654
655void free_cgroup_ns(struct cgroup_namespace *ns);
656
657struct cgroup_namespace *copy_cgroup_ns(unsigned long flags,
658 struct user_namespace *user_ns,
659 struct cgroup_namespace *old_ns);
660
Tejun Heo4c737b42016-08-10 11:23:44 -0400661int cgroup_path_ns(struct cgroup *cgrp, char *buf, size_t buflen,
662 struct cgroup_namespace *ns);
Aditya Kalia79a9082016-01-29 02:54:06 -0600663
664#else /* !CONFIG_CGROUPS */
665
666static inline void free_cgroup_ns(struct cgroup_namespace *ns) { }
667static inline struct cgroup_namespace *
668copy_cgroup_ns(unsigned long flags, struct user_namespace *user_ns,
669 struct cgroup_namespace *old_ns)
670{
671 return old_ns;
672}
673
674#endif /* !CONFIG_CGROUPS */
675
676static inline void get_cgroup_ns(struct cgroup_namespace *ns)
677{
678 if (ns)
Elena Reshetova387ad962017-02-20 12:19:00 +0200679 refcount_inc(&ns->count);
Aditya Kalia79a9082016-01-29 02:54:06 -0600680}
681
682static inline void put_cgroup_ns(struct cgroup_namespace *ns)
683{
Elena Reshetova387ad962017-02-20 12:19:00 +0200684 if (ns && refcount_dec_and_test(&ns->count))
Aditya Kalia79a9082016-01-29 02:54:06 -0600685 free_cgroup_ns(ns);
686}
687
Paul Menageddbcc7e2007-10-18 23:39:30 -0700688#endif /* _LINUX_CGROUP_H */