blob: 96a2ecd5aa696cbcccb20218a6bb7d5e9d9ca782 [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>
Paul Menagecc31edc2008-10-18 20:28:04 -070016#include <linux/rwsem.h>
Tejun Heo25a7e682013-04-14 20:15:25 -070017#include <linux/fs.h>
Tejun Heo7da11272013-12-05 12:28:04 -050018#include <linux/seq_file.h>
Tejun Heo2bd59d42014-02-11 11:52:49 -050019#include <linux/kernfs.h>
Tejun Heob4a04ab2015-05-13 15:38:40 -040020
21#include <linux/cgroup-defs.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070022
23#ifdef CONFIG_CGROUPS
24
Paul Menageddbcc7e2007-10-18 23:39:30 -070025extern int cgroup_init_early(void);
26extern int cgroup_init(void);
Paul Menageb4f48b62007-10-18 23:39:33 -070027extern void cgroup_fork(struct task_struct *p);
Paul Menage817929e2007-10-18 23:39:36 -070028extern void cgroup_post_fork(struct task_struct *p);
Li Zefan1ec41832014-03-28 15:22:19 +080029extern void cgroup_exit(struct task_struct *p);
Balbir Singh846c7bb2007-10-18 23:39:44 -070030extern int cgroupstats_build(struct cgroupstats *stats,
31 struct dentry *dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -070032
Zefan Li006f4ac2014-09-18 16:03:15 +080033extern int proc_cgroup_show(struct seq_file *m, struct pid_namespace *ns,
34 struct pid *pid, struct task_struct *tsk);
Paul Menagea4243162007-10-18 23:39:35 -070035
Tejun Heo5de01072013-06-12 21:04:52 -070036/**
37 * css_get - obtain a reference on the specified css
38 * @css: target css
39 *
40 * The caller must already have a reference.
Paul Menageddbcc7e2007-10-18 23:39:30 -070041 */
Paul Menageddbcc7e2007-10-18 23:39:30 -070042static inline void css_get(struct cgroup_subsys_state *css)
43{
Tejun Heo3b514d22014-05-16 13:22:47 -040044 if (!(css->flags & CSS_NO_REF))
45 percpu_ref_get(&css->refcnt);
Paul Menageddbcc7e2007-10-18 23:39:30 -070046}
Paul Menagee7c5ec92009-01-07 18:08:38 -080047
Tejun Heo5de01072013-06-12 21:04:52 -070048/**
Johannes Weinere8ea14c2014-12-10 15:42:42 -080049 * css_get_many - obtain references on the specified css
50 * @css: target css
51 * @n: number of references to get
52 *
53 * The caller must already have a reference.
54 */
55static inline void css_get_many(struct cgroup_subsys_state *css, unsigned int n)
56{
57 if (!(css->flags & CSS_NO_REF))
58 percpu_ref_get_many(&css->refcnt, n);
59}
60
61/**
Tejun Heo6f4524d2014-05-16 13:22:52 -040062 * css_tryget - try to obtain a reference on the specified css
63 * @css: target css
64 *
65 * Obtain a reference on @css unless it already has reached zero and is
66 * being released. This function doesn't care whether @css is on or
67 * offline. The caller naturally needs to ensure that @css is accessible
68 * but doesn't have to be holding a reference on it - IOW, RCU protected
69 * access is good enough for this function. Returns %true if a reference
70 * count was successfully obtained; %false otherwise.
71 */
72static inline bool css_tryget(struct cgroup_subsys_state *css)
73{
74 if (!(css->flags & CSS_NO_REF))
75 return percpu_ref_tryget(&css->refcnt);
76 return true;
77}
78
79/**
Tejun Heoec903c02014-05-13 12:11:01 -040080 * css_tryget_online - try to obtain a reference on the specified css if online
Tejun Heo5de01072013-06-12 21:04:52 -070081 * @css: target css
82 *
Tejun Heoec903c02014-05-13 12:11:01 -040083 * Obtain a reference on @css if it's online. The caller naturally needs
84 * to ensure that @css is accessible but doesn't have to be holding a
Tejun Heo5de01072013-06-12 21:04:52 -070085 * reference on it - IOW, RCU protected access is good enough for this
86 * function. Returns %true if a reference count was successfully obtained;
87 * %false otherwise.
88 */
Tejun Heoec903c02014-05-13 12:11:01 -040089static inline bool css_tryget_online(struct cgroup_subsys_state *css)
Paul Menagee7c5ec92009-01-07 18:08:38 -080090{
Tejun Heo3b514d22014-05-16 13:22:47 -040091 if (!(css->flags & CSS_NO_REF))
92 return percpu_ref_tryget_live(&css->refcnt);
93 return true;
Paul Menagee7c5ec92009-01-07 18:08:38 -080094}
95
Tejun Heo5de01072013-06-12 21:04:52 -070096/**
97 * css_put - put a css reference
98 * @css: target css
99 *
Tejun Heoec903c02014-05-13 12:11:01 -0400100 * Put a reference obtained via css_get() and css_tryget_online().
Tejun Heo5de01072013-06-12 21:04:52 -0700101 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700102static inline void css_put(struct cgroup_subsys_state *css)
103{
Tejun Heo3b514d22014-05-16 13:22:47 -0400104 if (!(css->flags & CSS_NO_REF))
105 percpu_ref_put(&css->refcnt);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700106}
107
Johannes Weinere8ea14c2014-12-10 15:42:42 -0800108/**
109 * css_put_many - put css references
110 * @css: target css
111 * @n: number of references to put
112 *
113 * Put references obtained via css_get() and css_tryget_online().
114 */
115static inline void css_put_many(struct cgroup_subsys_state *css, unsigned int n)
116{
117 if (!(css->flags & CSS_NO_REF))
118 percpu_ref_put_many(&css->refcnt, n);
119}
120
Tejun Heoa2dd4242014-03-19 10:23:55 -0400121extern struct cgroup_root cgrp_dfl_root;
Tejun Heo5024ae22014-05-07 21:31:17 -0400122extern struct css_set init_css_set;
Tejun Heoa2dd4242014-03-19 10:23:55 -0400123
Tejun Heoaa6ec292014-07-09 10:08:08 -0400124/**
125 * cgroup_on_dfl - test whether a cgroup is on the default hierarchy
126 * @cgrp: the cgroup of interest
127 *
128 * The default hierarchy is the v2 interface of cgroup and this function
129 * can be used to test whether a cgroup is on the default hierarchy for
130 * cases where a subsystem should behave differnetly depending on the
131 * interface version.
132 *
133 * The set of behaviors which change on the default hierarchy are still
134 * being determined and the mount option is prefixed with __DEVEL__.
135 *
136 * List of changed behaviors:
137 *
138 * - Mount options "noprefix", "xattr", "clone_children", "release_agent"
139 * and "name" are disallowed.
140 *
141 * - When mounting an existing superblock, mount options should match.
142 *
143 * - Remount is disallowed.
144 *
145 * - rename(2) is disallowed.
146 *
147 * - "tasks" is removed. Everything should be at process granularity. Use
148 * "cgroup.procs" instead.
149 *
150 * - "cgroup.procs" is not sorted. pids will be unique unless they got
151 * recycled inbetween reads.
152 *
153 * - "release_agent" and "notify_on_release" are removed. Replacement
154 * notification mechanism will be implemented.
155 *
156 * - "cgroup.clone_children" is removed.
157 *
158 * - "cgroup.subtree_populated" is available. Its value is 0 if the cgroup
159 * and its descendants contain no task; otherwise, 1. The file also
160 * generates kernfs notification which can be monitored through poll and
161 * [di]notify when the value of the file changes.
162 *
163 * - cpuset: tasks will be kept in empty cpusets when hotplug happens and
164 * take masks of ancestors with non-empty cpus/mems, instead of being
165 * moved to an ancestor.
166 *
167 * - cpuset: a task can be moved into an empty cpuset, and again it takes
168 * masks of ancestors.
169 *
170 * - memcg: use_hierarchy is on by default and the cgroup file for the flag
171 * is not created.
172 *
173 * - blkcg: blk-throttle becomes properly hierarchical.
174 *
175 * - debug: disallowed on the default hierarchy.
176 */
Tejun Heoa2dd4242014-03-19 10:23:55 -0400177static inline bool cgroup_on_dfl(const struct cgroup *cgrp)
178{
179 return cgrp->root == &cgrp_dfl_root;
180}
181
Tejun Heo07bc3562014-02-13 06:58:39 -0500182/* no synchronization, the result can only be used as a hint */
183static inline bool cgroup_has_tasks(struct cgroup *cgrp)
184{
185 return !list_empty(&cgrp->cset_links);
186}
187
Zefan Lif29374b2014-09-19 16:29:31 +0800188/* returns ino associated with a cgroup */
Tejun Heob1664922014-02-11 11:52:49 -0500189static inline ino_t cgroup_ino(struct cgroup *cgrp)
190{
Zefan Lif29374b2014-09-19 16:29:31 +0800191 return cgrp->kn->ino;
Tejun Heob1664922014-02-11 11:52:49 -0500192}
193
Tejun Heob4168642014-05-13 12:16:21 -0400194/* cft/css accessors for cftype->write() operation */
195static inline struct cftype *of_cft(struct kernfs_open_file *of)
Tejun Heo7da11272013-12-05 12:28:04 -0500196{
Tejun Heo2bd59d42014-02-11 11:52:49 -0500197 return of->kn->priv;
Tejun Heo7da11272013-12-05 12:28:04 -0500198}
199
Tejun Heob4168642014-05-13 12:16:21 -0400200struct cgroup_subsys_state *of_css(struct kernfs_open_file *of);
201
202/* cft/css accessors for cftype->seq_*() operations */
203static inline struct cftype *seq_cft(struct seq_file *seq)
204{
205 return of_cft(seq->private);
206}
207
208static inline struct cgroup_subsys_state *seq_css(struct seq_file *seq)
209{
210 return of_css(seq->private);
211}
Tejun Heo59f52962014-02-11 11:52:49 -0500212
Tejun Heoe61734c2014-02-12 09:29:50 -0500213/*
214 * Name / path handling functions. All are thin wrappers around the kernfs
215 * counterparts and can be called under any context.
216 */
217
218static inline int cgroup_name(struct cgroup *cgrp, char *buf, size_t buflen)
219{
Tejun Heofdce6bf2014-03-19 10:23:54 -0400220 return kernfs_name(cgrp->kn, buf, buflen);
Tejun Heoe61734c2014-02-12 09:29:50 -0500221}
222
223static inline char * __must_check cgroup_path(struct cgroup *cgrp, char *buf,
224 size_t buflen)
225{
Tejun Heofdce6bf2014-03-19 10:23:54 -0400226 return kernfs_path(cgrp->kn, buf, buflen);
Tejun Heoe61734c2014-02-12 09:29:50 -0500227}
228
229static inline void pr_cont_cgroup_name(struct cgroup *cgrp)
230{
Tejun Heofdce6bf2014-03-19 10:23:54 -0400231 pr_cont_kernfs_name(cgrp->kn);
Tejun Heoe61734c2014-02-12 09:29:50 -0500232}
233
234static inline void pr_cont_cgroup_path(struct cgroup *cgrp)
235{
Tejun Heofdce6bf2014-03-19 10:23:54 -0400236 pr_cont_kernfs_path(cgrp->kn);
Tejun Heoe61734c2014-02-12 09:29:50 -0500237}
238
239char *task_cgroup_path(struct task_struct *task, char *buf, size_t buflen);
240
Tejun Heoa8ddc822014-07-15 11:05:10 -0400241int cgroup_add_dfl_cftypes(struct cgroup_subsys *ss, struct cftype *cfts);
Tejun Heo2cf669a2014-07-15 11:05:09 -0400242int cgroup_add_legacy_cftypes(struct cgroup_subsys *ss, struct cftype *cfts);
Tejun Heo2bb566c2013-08-08 20:11:23 -0400243int cgroup_rm_cftypes(struct cftype *cfts);
Tejun Heo8e3f6542012-04-01 12:09:55 -0700244
Li Zefan78574cf2013-04-08 19:00:38 -0700245bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700246
Tejun Heo2f7ee562011-12-12 18:12:21 -0800247struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset);
248struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset);
Tejun Heo2f7ee562011-12-12 18:12:21 -0800249
250/**
251 * cgroup_taskset_for_each - iterate cgroup_taskset
252 * @task: the loop cursor
Tejun Heo2f7ee562011-12-12 18:12:21 -0800253 * @tset: taskset to iterate
254 */
Tejun Heo924f0d9a2014-02-13 06:58:41 -0500255#define cgroup_taskset_for_each(task, tset) \
Tejun Heo2f7ee562011-12-12 18:12:21 -0800256 for ((task) = cgroup_taskset_first((tset)); (task); \
Tejun Heo924f0d9a2014-02-13 06:58:41 -0500257 (task) = cgroup_taskset_next((tset)))
Tejun Heo2f7ee562011-12-12 18:12:21 -0800258
Tejun Heo073219e2014-02-08 10:36:58 -0500259#define SUBSYS(_x) extern struct cgroup_subsys _x ## _cgrp_subsys;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700260#include <linux/cgroup_subsys.h>
261#undef SUBSYS
262
Tejun Heo8af01f52013-08-08 20:11:22 -0400263/**
Tejun Heo14611e52013-06-25 11:48:32 -0700264 * task_css_set_check - obtain a task's css_set with extra access conditions
265 * @task: the task to obtain css_set for
266 * @__c: extra condition expression to be passed to rcu_dereference_check()
267 *
268 * A task's css_set is RCU protected, initialized and exited while holding
269 * task_lock(), and can only be modified while holding both cgroup_mutex
270 * and task_lock() while the task is alive. This macro verifies that the
271 * caller is inside proper critical section and returns @task's css_set.
272 *
273 * The caller can also specify additional allowed conditions via @__c, such
274 * as locks used during the cgroup_subsys::attach() methods.
Peter Zijlstradc61b1d2010-06-08 11:40:42 +0200275 */
Tejun Heo22194492013-04-07 09:29:51 -0700276#ifdef CONFIG_PROVE_RCU
277extern struct mutex cgroup_mutex;
Tejun Heo0e1d7682014-02-25 10:04:03 -0500278extern struct rw_semaphore css_set_rwsem;
Tejun Heo14611e52013-06-25 11:48:32 -0700279#define task_css_set_check(task, __c) \
280 rcu_dereference_check((task)->cgroups, \
Tejun Heo0e1d7682014-02-25 10:04:03 -0500281 lockdep_is_held(&cgroup_mutex) || \
282 lockdep_is_held(&css_set_rwsem) || \
283 ((task)->flags & PF_EXITING) || (__c))
Tejun Heo22194492013-04-07 09:29:51 -0700284#else
Tejun Heo14611e52013-06-25 11:48:32 -0700285#define task_css_set_check(task, __c) \
286 rcu_dereference((task)->cgroups)
Tejun Heo22194492013-04-07 09:29:51 -0700287#endif
Peter Zijlstradc61b1d2010-06-08 11:40:42 +0200288
Tejun Heo14611e52013-06-25 11:48:32 -0700289/**
Tejun Heo8af01f52013-08-08 20:11:22 -0400290 * task_css_check - obtain css for (task, subsys) w/ extra access conds
Tejun Heo14611e52013-06-25 11:48:32 -0700291 * @task: the target task
292 * @subsys_id: the target subsystem ID
293 * @__c: extra condition expression to be passed to rcu_dereference_check()
294 *
295 * Return the cgroup_subsys_state for the (@task, @subsys_id) pair. The
296 * synchronization rules are the same as task_css_set_check().
297 */
Tejun Heo8af01f52013-08-08 20:11:22 -0400298#define task_css_check(task, subsys_id, __c) \
Tejun Heo14611e52013-06-25 11:48:32 -0700299 task_css_set_check((task), (__c))->subsys[(subsys_id)]
300
301/**
302 * task_css_set - obtain a task's css_set
303 * @task: the task to obtain css_set for
304 *
305 * See task_css_set_check().
306 */
307static inline struct css_set *task_css_set(struct task_struct *task)
308{
309 return task_css_set_check(task, false);
310}
311
312/**
Tejun Heo8af01f52013-08-08 20:11:22 -0400313 * task_css - obtain css for (task, subsys)
Tejun Heo14611e52013-06-25 11:48:32 -0700314 * @task: the target task
315 * @subsys_id: the target subsystem ID
316 *
Tejun Heo8af01f52013-08-08 20:11:22 -0400317 * See task_css_check().
Tejun Heo14611e52013-06-25 11:48:32 -0700318 */
Tejun Heo8af01f52013-08-08 20:11:22 -0400319static inline struct cgroup_subsys_state *task_css(struct task_struct *task,
320 int subsys_id)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700321{
Tejun Heo8af01f52013-08-08 20:11:22 -0400322 return task_css_check(task, subsys_id, false);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700323}
324
Tejun Heo5024ae22014-05-07 21:31:17 -0400325/**
326 * task_css_is_root - test whether a task belongs to the root css
327 * @task: the target task
328 * @subsys_id: the target subsystem ID
329 *
330 * Test whether @task belongs to the root css on the specified subsystem.
331 * May be invoked in any context.
332 */
333static inline bool task_css_is_root(struct task_struct *task, int subsys_id)
334{
335 return task_css_check(task, subsys_id, true) ==
336 init_css_set.subsys[subsys_id];
337}
338
Tejun Heo8af01f52013-08-08 20:11:22 -0400339static inline struct cgroup *task_cgroup(struct task_struct *task,
340 int subsys_id)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700341{
Tejun Heo8af01f52013-08-08 20:11:22 -0400342 return task_css(task, subsys_id)->cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700343}
344
Tejun Heo492eb212013-08-08 20:11:25 -0400345struct cgroup_subsys_state *css_next_child(struct cgroup_subsys_state *pos,
346 struct cgroup_subsys_state *parent);
Tejun Heo53fa5262013-05-24 10:55:38 +0900347
Li Zefan1cb650b2013-08-19 10:05:24 +0800348struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss);
349
Tejun Heo574bd9f2012-11-09 09:12:29 -0800350/**
Tejun Heo492eb212013-08-08 20:11:25 -0400351 * css_for_each_child - iterate through children of a css
352 * @pos: the css * to use as the loop cursor
353 * @parent: css whose children to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -0800354 *
Tejun Heoc2931b72014-05-16 13:22:51 -0400355 * Walk @parent's children. Must be called under rcu_read_lock().
Tejun Heo574bd9f2012-11-09 09:12:29 -0800356 *
Tejun Heoc2931b72014-05-16 13:22:51 -0400357 * If a subsystem synchronizes ->css_online() and the start of iteration, a
358 * css which finished ->css_online() is guaranteed to be visible in the
359 * future iterations and will stay visible until the last reference is put.
360 * A css which hasn't finished ->css_online() or already finished
361 * ->css_offline() may show up during traversal. It's each subsystem's
362 * responsibility to synchronize against on/offlining.
Tejun Heo75501a62013-05-24 10:55:38 +0900363 *
364 * It is allowed to temporarily drop RCU read lock during iteration. The
365 * caller is responsible for ensuring that @pos remains accessible until
366 * the start of the next iteration by, for example, bumping the css refcnt.
Tejun Heo574bd9f2012-11-09 09:12:29 -0800367 */
Tejun Heo492eb212013-08-08 20:11:25 -0400368#define css_for_each_child(pos, parent) \
369 for ((pos) = css_next_child(NULL, (parent)); (pos); \
370 (pos) = css_next_child((pos), (parent)))
Tejun Heo574bd9f2012-11-09 09:12:29 -0800371
Tejun Heo492eb212013-08-08 20:11:25 -0400372struct cgroup_subsys_state *
373css_next_descendant_pre(struct cgroup_subsys_state *pos,
374 struct cgroup_subsys_state *css);
375
376struct cgroup_subsys_state *
377css_rightmost_descendant(struct cgroup_subsys_state *pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -0800378
379/**
Tejun Heo492eb212013-08-08 20:11:25 -0400380 * css_for_each_descendant_pre - pre-order walk of a css's descendants
381 * @pos: the css * to use as the loop cursor
382 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -0800383 *
Tejun Heobd8815a2013-08-08 20:11:27 -0400384 * Walk @root's descendants. @root is included in the iteration and the
Tejun Heoc2931b72014-05-16 13:22:51 -0400385 * first node to be visited. Must be called under rcu_read_lock().
Tejun Heo574bd9f2012-11-09 09:12:29 -0800386 *
Tejun Heoc2931b72014-05-16 13:22:51 -0400387 * If a subsystem synchronizes ->css_online() and the start of iteration, a
388 * css which finished ->css_online() is guaranteed to be visible in the
389 * future iterations and will stay visible until the last reference is put.
390 * A css which hasn't finished ->css_online() or already finished
391 * ->css_offline() may show up during traversal. It's each subsystem's
392 * responsibility to synchronize against on/offlining.
Tejun Heo574bd9f2012-11-09 09:12:29 -0800393 *
Tejun Heoc2931b72014-05-16 13:22:51 -0400394 * For example, the following guarantees that a descendant can't escape
Tejun Heo574bd9f2012-11-09 09:12:29 -0800395 * state updates of its ancestors.
396 *
Tejun Heo492eb212013-08-08 20:11:25 -0400397 * my_online(@css)
Tejun Heo574bd9f2012-11-09 09:12:29 -0800398 * {
Tejun Heo492eb212013-08-08 20:11:25 -0400399 * Lock @css's parent and @css;
400 * Inherit state from the parent;
Tejun Heo574bd9f2012-11-09 09:12:29 -0800401 * Unlock both.
402 * }
403 *
Tejun Heo492eb212013-08-08 20:11:25 -0400404 * my_update_state(@css)
Tejun Heo574bd9f2012-11-09 09:12:29 -0800405 * {
Tejun Heo492eb212013-08-08 20:11:25 -0400406 * css_for_each_descendant_pre(@pos, @css) {
Tejun Heo574bd9f2012-11-09 09:12:29 -0800407 * Lock @pos;
Tejun Heobd8815a2013-08-08 20:11:27 -0400408 * if (@pos == @css)
409 * Update @css's state;
410 * else
411 * Verify @pos is alive and inherit state from its parent;
Tejun Heo574bd9f2012-11-09 09:12:29 -0800412 * Unlock @pos;
413 * }
414 * }
415 *
416 * As long as the inheriting step, including checking the parent state, is
417 * enclosed inside @pos locking, double-locking the parent isn't necessary
418 * while inheriting. The state update to the parent is guaranteed to be
419 * visible by walking order and, as long as inheriting operations to the
420 * same @pos are atomic to each other, multiple updates racing each other
421 * still result in the correct state. It's guaranateed that at least one
Tejun Heo492eb212013-08-08 20:11:25 -0400422 * inheritance happens for any css after the latest update to its parent.
Tejun Heo574bd9f2012-11-09 09:12:29 -0800423 *
424 * If checking parent's state requires locking the parent, each inheriting
425 * iteration should lock and unlock both @pos->parent and @pos.
426 *
427 * Alternatively, a subsystem may choose to use a single global lock to
Tejun Heo92fb9742012-11-19 08:13:38 -0800428 * synchronize ->css_online() and ->css_offline() against tree-walking
Tejun Heo574bd9f2012-11-09 09:12:29 -0800429 * operations.
Tejun Heo75501a62013-05-24 10:55:38 +0900430 *
431 * It is allowed to temporarily drop RCU read lock during iteration. The
432 * caller is responsible for ensuring that @pos remains accessible until
433 * the start of the next iteration by, for example, bumping the css refcnt.
Tejun Heo574bd9f2012-11-09 09:12:29 -0800434 */
Tejun Heo492eb212013-08-08 20:11:25 -0400435#define css_for_each_descendant_pre(pos, css) \
436 for ((pos) = css_next_descendant_pre(NULL, (css)); (pos); \
437 (pos) = css_next_descendant_pre((pos), (css)))
Tejun Heo574bd9f2012-11-09 09:12:29 -0800438
Tejun Heo492eb212013-08-08 20:11:25 -0400439struct cgroup_subsys_state *
440css_next_descendant_post(struct cgroup_subsys_state *pos,
441 struct cgroup_subsys_state *css);
Tejun Heo574bd9f2012-11-09 09:12:29 -0800442
443/**
Tejun Heo492eb212013-08-08 20:11:25 -0400444 * css_for_each_descendant_post - post-order walk of a css's descendants
445 * @pos: the css * to use as the loop cursor
446 * @css: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -0800447 *
Tejun Heo492eb212013-08-08 20:11:25 -0400448 * Similar to css_for_each_descendant_pre() but performs post-order
Tejun Heobd8815a2013-08-08 20:11:27 -0400449 * traversal instead. @root is included in the iteration and the last
Tejun Heoc2931b72014-05-16 13:22:51 -0400450 * node to be visited.
451 *
452 * If a subsystem synchronizes ->css_online() and the start of iteration, a
453 * css which finished ->css_online() is guaranteed to be visible in the
454 * future iterations and will stay visible until the last reference is put.
455 * A css which hasn't finished ->css_online() or already finished
456 * ->css_offline() may show up during traversal. It's each subsystem's
457 * responsibility to synchronize against on/offlining.
458 *
459 * Note that the walk visibility guarantee example described in pre-order
460 * walk doesn't apply the same to post-order walks.
Tejun Heo574bd9f2012-11-09 09:12:29 -0800461 */
Tejun Heo492eb212013-08-08 20:11:25 -0400462#define css_for_each_descendant_post(pos, css) \
463 for ((pos) = css_next_descendant_post(NULL, (css)); (pos); \
464 (pos) = css_next_descendant_post((pos), (css)))
Tejun Heo574bd9f2012-11-09 09:12:29 -0800465
Tejun Heof3d46502014-05-16 13:22:52 -0400466bool css_has_online_children(struct cgroup_subsys_state *css);
467
Tejun Heo72ec7022013-08-08 20:11:26 -0400468/* A css_task_iter should be treated as an opaque object */
469struct css_task_iter {
Tejun Heo3ebb2b62014-04-23 11:13:15 -0400470 struct cgroup_subsys *ss;
471
Tejun Heo0f0a2b42014-04-23 11:13:15 -0400472 struct list_head *cset_pos;
473 struct list_head *cset_head;
474
475 struct list_head *task_pos;
476 struct list_head *tasks_head;
477 struct list_head *mg_tasks_head;
Paul Menage817929e2007-10-18 23:39:36 -0700478};
479
Tejun Heo72ec7022013-08-08 20:11:26 -0400480void css_task_iter_start(struct cgroup_subsys_state *css,
481 struct css_task_iter *it);
482struct task_struct *css_task_iter_next(struct css_task_iter *it);
483void css_task_iter_end(struct css_task_iter *it);
Tejun Heoe5358372013-08-08 20:11:26 -0400484
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -0700485int cgroup_attach_task_all(struct task_struct *from, struct task_struct *);
Tejun Heo8cc99342013-04-07 09:29:50 -0700486int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from);
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -0700487
Tejun Heoeeecbd12014-11-18 02:49:52 -0500488struct cgroup_subsys_state *cgroup_get_e_css(struct cgroup *cgroup,
489 struct cgroup_subsys *ss);
Tejun Heoec903c02014-05-13 12:11:01 -0400490struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry,
491 struct cgroup_subsys *ss);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700492
Paul Menageddbcc7e2007-10-18 23:39:30 -0700493#else /* !CONFIG_CGROUPS */
494
Tejun Heof3ba5382015-01-06 12:02:46 -0500495struct cgroup_subsys_state;
496
Paul Menageddbcc7e2007-10-18 23:39:30 -0700497static inline int cgroup_init_early(void) { return 0; }
498static inline int cgroup_init(void) { return 0; }
Paul Menageb4f48b62007-10-18 23:39:33 -0700499static inline void cgroup_fork(struct task_struct *p) {}
Paul Menage817929e2007-10-18 23:39:36 -0700500static inline void cgroup_post_fork(struct task_struct *p) {}
Li Zefan1ec41832014-03-28 15:22:19 +0800501static inline void cgroup_exit(struct task_struct *p) {}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700502
Balbir Singh846c7bb2007-10-18 23:39:44 -0700503static inline int cgroupstats_build(struct cgroupstats *stats,
504 struct dentry *dentry)
505{
506 return -EINVAL;
507}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700508
Tejun Heof3ba5382015-01-06 12:02:46 -0500509static inline void css_put(struct cgroup_subsys_state *css) {}
510
Sridhar Samudralad7926ee2010-05-30 22:24:39 +0200511/* No cgroups - nothing to do */
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -0700512static inline int cgroup_attach_task_all(struct task_struct *from,
513 struct task_struct *t)
514{
515 return 0;
516}
Sridhar Samudralad7926ee2010-05-30 22:24:39 +0200517
Paul Menageddbcc7e2007-10-18 23:39:30 -0700518#endif /* !CONFIG_CGROUPS */
519
Paul Menageddbcc7e2007-10-18 23:39:30 -0700520#endif /* _LINUX_CGROUP_H */