Merge branch 'rc-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/mmarek/kbuild
[linux-2.6-block.git] / include / linux / cgroup.h
CommitLineData
ddbcc7e8
PM
1#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>
ddbcc7e8
PM
12#include <linux/cpumask.h>
13#include <linux/nodemask.h>
eb6fd504 14#include <linux/rculist.h>
846c7bb0 15#include <linux/cgroupstats.h>
cc31edce 16#include <linux/rwsem.h>
25a7e684 17#include <linux/fs.h>
7da11279 18#include <linux/seq_file.h>
2bd59d48 19#include <linux/kernfs.h>
ddbcc7e8 20
b4a04ab7 21#include <linux/cgroup-defs.h>
ddbcc7e8
PM
22
23#ifdef CONFIG_CGROUPS
24
c326aa2b
TH
25/* a css_task_iter should be treated as an opaque object */
26struct css_task_iter {
27 struct cgroup_subsys *ss;
28
29 struct list_head *cset_pos;
30 struct list_head *cset_head;
ddbcc7e8 31
c326aa2b
TH
32 struct list_head *task_pos;
33 struct list_head *tasks_head;
34 struct list_head *mg_tasks_head;
35};
ddbcc7e8 36
c326aa2b
TH
37extern struct cgroup_root cgrp_dfl_root;
38extern struct css_set init_css_set;
a424316c 39
c326aa2b 40#define SUBSYS(_x) extern struct cgroup_subsys _x ## _cgrp_subsys;
817929ec 41#include <linux/cgroup_subsys.h>
817929ec
PM
42#undef SUBSYS
43
c326aa2b
TH
44bool css_has_online_children(struct cgroup_subsys_state *css);
45struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss);
46struct cgroup_subsys_state *cgroup_get_e_css(struct cgroup *cgroup,
47 struct cgroup_subsys *ss);
48struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry,
49 struct cgroup_subsys *ss);
50
51bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor);
52int cgroup_attach_task_all(struct task_struct *from, struct task_struct *);
53int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from);
54
55int cgroup_add_dfl_cftypes(struct cgroup_subsys *ss, struct cftype *cfts);
56int cgroup_add_legacy_cftypes(struct cgroup_subsys *ss, struct cftype *cfts);
57int cgroup_rm_cftypes(struct cftype *cfts);
58
59char *task_cgroup_path(struct task_struct *task, char *buf, size_t buflen);
60int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry);
61int proc_cgroup_show(struct seq_file *m, struct pid_namespace *ns,
62 struct pid *pid, struct task_struct *tsk);
63
64void cgroup_fork(struct task_struct *p);
65void cgroup_post_fork(struct task_struct *p);
66void cgroup_exit(struct task_struct *p);
67
68int cgroup_init_early(void);
69int cgroup_init(void);
70
5c9d535b 71/*
c326aa2b
TH
72 * Iteration helpers and macros.
73 */
74
75struct cgroup_subsys_state *css_next_child(struct cgroup_subsys_state *pos,
76 struct cgroup_subsys_state *parent);
77struct cgroup_subsys_state *css_next_descendant_pre(struct cgroup_subsys_state *pos,
78 struct cgroup_subsys_state *css);
79struct cgroup_subsys_state *css_rightmost_descendant(struct cgroup_subsys_state *pos);
80struct cgroup_subsys_state *css_next_descendant_post(struct cgroup_subsys_state *pos,
81 struct cgroup_subsys_state *css);
82
83struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset);
84struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset);
85
86void css_task_iter_start(struct cgroup_subsys_state *css,
87 struct css_task_iter *it);
88struct task_struct *css_task_iter_next(struct css_task_iter *it);
89void css_task_iter_end(struct css_task_iter *it);
90
91/**
92 * css_for_each_child - iterate through children of a css
93 * @pos: the css * to use as the loop cursor
94 * @parent: css whose children to walk
95 *
96 * Walk @parent's children. Must be called under rcu_read_lock().
97 *
98 * If a subsystem synchronizes ->css_online() and the start of iteration, a
99 * css which finished ->css_online() is guaranteed to be visible in the
100 * future iterations and will stay visible until the last reference is put.
101 * A css which hasn't finished ->css_online() or already finished
102 * ->css_offline() may show up during traversal. It's each subsystem's
103 * responsibility to synchronize against on/offlining.
5c9d535b 104 *
c326aa2b
TH
105 * It is allowed to temporarily drop RCU read lock during iteration. The
106 * caller is responsible for ensuring that @pos remains accessible until
107 * the start of the next iteration by, for example, bumping the css refcnt.
5c9d535b 108 */
c326aa2b
TH
109#define css_for_each_child(pos, parent) \
110 for ((pos) = css_next_child(NULL, (parent)); (pos); \
111 (pos) = css_next_child((pos), (parent)))
ddbcc7e8 112
c326aa2b
TH
113/**
114 * css_for_each_descendant_pre - pre-order walk of a css's descendants
115 * @pos: the css * to use as the loop cursor
116 * @root: css whose descendants to walk
117 *
118 * Walk @root's descendants. @root is included in the iteration and the
119 * first node to be visited. Must be called under rcu_read_lock().
120 *
121 * If a subsystem synchronizes ->css_online() and the start of iteration, a
122 * css which finished ->css_online() is guaranteed to be visible in the
123 * future iterations and will stay visible until the last reference is put.
124 * A css which hasn't finished ->css_online() or already finished
125 * ->css_offline() may show up during traversal. It's each subsystem's
126 * responsibility to synchronize against on/offlining.
127 *
128 * For example, the following guarantees that a descendant can't escape
129 * state updates of its ancestors.
130 *
131 * my_online(@css)
132 * {
133 * Lock @css's parent and @css;
134 * Inherit state from the parent;
135 * Unlock both.
136 * }
137 *
138 * my_update_state(@css)
139 * {
140 * css_for_each_descendant_pre(@pos, @css) {
141 * Lock @pos;
142 * if (@pos == @css)
143 * Update @css's state;
144 * else
145 * Verify @pos is alive and inherit state from its parent;
146 * Unlock @pos;
147 * }
148 * }
149 *
150 * As long as the inheriting step, including checking the parent state, is
151 * enclosed inside @pos locking, double-locking the parent isn't necessary
152 * while inheriting. The state update to the parent is guaranteed to be
153 * visible by walking order and, as long as inheriting operations to the
154 * same @pos are atomic to each other, multiple updates racing each other
155 * still result in the correct state. It's guaranateed that at least one
156 * inheritance happens for any css after the latest update to its parent.
157 *
158 * If checking parent's state requires locking the parent, each inheriting
159 * iteration should lock and unlock both @pos->parent and @pos.
160 *
161 * Alternatively, a subsystem may choose to use a single global lock to
162 * synchronize ->css_online() and ->css_offline() against tree-walking
163 * operations.
164 *
165 * It is allowed to temporarily drop RCU read lock during iteration. The
166 * caller is responsible for ensuring that @pos remains accessible until
167 * the start of the next iteration by, for example, bumping the css refcnt.
168 */
169#define css_for_each_descendant_pre(pos, css) \
170 for ((pos) = css_next_descendant_pre(NULL, (css)); (pos); \
171 (pos) = css_next_descendant_pre((pos), (css)))
172
173/**
174 * css_for_each_descendant_post - post-order walk of a css's descendants
175 * @pos: the css * to use as the loop cursor
176 * @css: css whose descendants to walk
177 *
178 * Similar to css_for_each_descendant_pre() but performs post-order
179 * traversal instead. @root is included in the iteration and the last
180 * node to be visited.
181 *
182 * If a subsystem synchronizes ->css_online() and the start of iteration, a
183 * css which finished ->css_online() is guaranteed to be visible in the
184 * future iterations and will stay visible until the last reference is put.
185 * A css which hasn't finished ->css_online() or already finished
186 * ->css_offline() may show up during traversal. It's each subsystem's
187 * responsibility to synchronize against on/offlining.
188 *
189 * Note that the walk visibility guarantee example described in pre-order
190 * walk doesn't apply the same to post-order walks.
191 */
192#define css_for_each_descendant_post(pos, css) \
193 for ((pos) = css_next_descendant_post(NULL, (css)); (pos); \
194 (pos) = css_next_descendant_post((pos), (css)))
195
196/**
197 * cgroup_taskset_for_each - iterate cgroup_taskset
198 * @task: the loop cursor
199 * @tset: taskset to iterate
200 */
201#define cgroup_taskset_for_each(task, tset) \
202 for ((task) = cgroup_taskset_first((tset)); (task); \
203 (task) = cgroup_taskset_next((tset)))
ddbcc7e8 204
c326aa2b
TH
205/*
206 * Inline functions.
207 */
ddbcc7e8 208
5de0107e
TH
209/**
210 * css_get - obtain a reference on the specified css
211 * @css: target css
212 *
213 * The caller must already have a reference.
ddbcc7e8 214 */
ddbcc7e8
PM
215static inline void css_get(struct cgroup_subsys_state *css)
216{
3b514d24
TH
217 if (!(css->flags & CSS_NO_REF))
218 percpu_ref_get(&css->refcnt);
ddbcc7e8 219}
e7c5ec91 220
e8ea14cc
JW
221/**
222 * css_get_many - obtain references on the specified css
223 * @css: target css
224 * @n: number of references to get
225 *
226 * The caller must already have a reference.
227 */
228static inline void css_get_many(struct cgroup_subsys_state *css, unsigned int n)
229{
230 if (!(css->flags & CSS_NO_REF))
231 percpu_ref_get_many(&css->refcnt, n);
232}
233
6f4524d3
TH
234/**
235 * css_tryget - try to obtain a reference on the specified css
236 * @css: target css
237 *
238 * Obtain a reference on @css unless it already has reached zero and is
239 * being released. This function doesn't care whether @css is on or
240 * offline. The caller naturally needs to ensure that @css is accessible
241 * but doesn't have to be holding a reference on it - IOW, RCU protected
242 * access is good enough for this function. Returns %true if a reference
243 * count was successfully obtained; %false otherwise.
244 */
245static inline bool css_tryget(struct cgroup_subsys_state *css)
246{
247 if (!(css->flags & CSS_NO_REF))
248 return percpu_ref_tryget(&css->refcnt);
249 return true;
250}
251
5de0107e 252/**
ec903c0c 253 * css_tryget_online - try to obtain a reference on the specified css if online
5de0107e
TH
254 * @css: target css
255 *
ec903c0c
TH
256 * Obtain a reference on @css if it's online. The caller naturally needs
257 * to ensure that @css is accessible but doesn't have to be holding a
5de0107e
TH
258 * reference on it - IOW, RCU protected access is good enough for this
259 * function. Returns %true if a reference count was successfully obtained;
260 * %false otherwise.
261 */
ec903c0c 262static inline bool css_tryget_online(struct cgroup_subsys_state *css)
e7c5ec91 263{
3b514d24
TH
264 if (!(css->flags & CSS_NO_REF))
265 return percpu_ref_tryget_live(&css->refcnt);
266 return true;
e7c5ec91 267}
2f7ee569
TH
268
269/**
5de0107e
TH
270 * css_put - put a css reference
271 * @css: target css
272 *
ec903c0c 273 * Put a reference obtained via css_get() and css_tryget_online().
2f7ee569 274 */
ddbcc7e8
PM
275static inline void css_put(struct cgroup_subsys_state *css)
276{
3b514d24
TH
277 if (!(css->flags & CSS_NO_REF))
278 percpu_ref_put(&css->refcnt);
ddbcc7e8 279}
2f7ee569 280
e8ea14cc
JW
281/**
282 * css_put_many - put css references
283 * @css: target css
284 * @n: number of references to put
285 *
286 * Put references obtained via css_get() and css_tryget_online().
21acb9ca 287 */
e8ea14cc
JW
288static inline void css_put_many(struct cgroup_subsys_state *css, unsigned int n)
289{
290 if (!(css->flags & CSS_NO_REF))
291 percpu_ref_put_many(&css->refcnt, n);
292}
ddbcc7e8 293
14611e51
TH
294/**
295 * task_css_set_check - obtain a task's css_set with extra access conditions
296 * @task: the task to obtain css_set for
297 * @__c: extra condition expression to be passed to rcu_dereference_check()
298 *
299 * A task's css_set is RCU protected, initialized and exited while holding
300 * task_lock(), and can only be modified while holding both cgroup_mutex
301 * and task_lock() while the task is alive. This macro verifies that the
302 * caller is inside proper critical section and returns @task's css_set.
303 *
304 * The caller can also specify additional allowed conditions via @__c, such
305 * as locks used during the cgroup_subsys::attach() methods.
dc61b1d6 306 */
2219449a
TH
307#ifdef CONFIG_PROVE_RCU
308extern struct mutex cgroup_mutex;
0e1d768f 309extern struct rw_semaphore css_set_rwsem;
14611e51
TH
310#define task_css_set_check(task, __c) \
311 rcu_dereference_check((task)->cgroups, \
0e1d768f
TH
312 lockdep_is_held(&cgroup_mutex) || \
313 lockdep_is_held(&css_set_rwsem) || \
314 ((task)->flags & PF_EXITING) || (__c))
2219449a 315#else
14611e51
TH
316#define task_css_set_check(task, __c) \
317 rcu_dereference((task)->cgroups)
2219449a 318#endif
dc61b1d6 319
14611e51 320/**
8af01f56 321 * task_css_check - obtain css for (task, subsys) w/ extra access conds
14611e51
TH
322 * @task: the target task
323 * @subsys_id: the target subsystem ID
324 * @__c: extra condition expression to be passed to rcu_dereference_check()
325 *
326 * Return the cgroup_subsys_state for the (@task, @subsys_id) pair. The
327 * synchronization rules are the same as task_css_set_check().
328 */
8af01f56 329#define task_css_check(task, subsys_id, __c) \
14611e51
TH
330 task_css_set_check((task), (__c))->subsys[(subsys_id)]
331
332/**
333 * task_css_set - obtain a task's css_set
334 * @task: the task to obtain css_set for
335 *
336 * See task_css_set_check().
337 */
338static inline struct css_set *task_css_set(struct task_struct *task)
339{
340 return task_css_set_check(task, false);
341}
342
343/**
8af01f56 344 * task_css - obtain css for (task, subsys)
14611e51
TH
345 * @task: the target task
346 * @subsys_id: the target subsystem ID
347 *
8af01f56 348 * See task_css_check().
14611e51 349 */
8af01f56
TH
350static inline struct cgroup_subsys_state *task_css(struct task_struct *task,
351 int subsys_id)
ddbcc7e8 352{
8af01f56 353 return task_css_check(task, subsys_id, false);
ddbcc7e8
PM
354}
355
ec438699
TH
356/**
357 * task_get_css - find and get the css for (task, subsys)
358 * @task: the target task
359 * @subsys_id: the target subsystem ID
360 *
361 * Find the css for the (@task, @subsys_id) combination, increment a
362 * reference on and return it. This function is guaranteed to return a
363 * valid css.
364 */
365static inline struct cgroup_subsys_state *
366task_get_css(struct task_struct *task, int subsys_id)
367{
368 struct cgroup_subsys_state *css;
369
370 rcu_read_lock();
371 while (true) {
372 css = task_css(task, subsys_id);
373 if (likely(css_tryget_online(css)))
374 break;
375 cpu_relax();
376 }
377 rcu_read_unlock();
378 return css;
379}
380
5024ae29
TH
381/**
382 * task_css_is_root - test whether a task belongs to the root css
383 * @task: the target task
384 * @subsys_id: the target subsystem ID
385 *
386 * Test whether @task belongs to the root css on the specified subsystem.
387 * May be invoked in any context.
388 */
389static inline bool task_css_is_root(struct task_struct *task, int subsys_id)
390{
391 return task_css_check(task, subsys_id, true) ==
392 init_css_set.subsys[subsys_id];
393}
394
8af01f56
TH
395static inline struct cgroup *task_cgroup(struct task_struct *task,
396 int subsys_id)
ddbcc7e8 397{
8af01f56 398 return task_css(task, subsys_id)->cgroup;
ddbcc7e8
PM
399}
400
574bd9f7 401/**
aa6ec29b
TH
402 * cgroup_on_dfl - test whether a cgroup is on the default hierarchy
403 * @cgrp: the cgroup of interest
574bd9f7 404 *
aa6ec29b
TH
405 * The default hierarchy is the v2 interface of cgroup and this function
406 * can be used to test whether a cgroup is on the default hierarchy for
407 * cases where a subsystem should behave differnetly depending on the
408 * interface version.
574bd9f7 409 *
aa6ec29b
TH
410 * The set of behaviors which change on the default hierarchy are still
411 * being determined and the mount option is prefixed with __DEVEL__.
75501a6d 412 *
aa6ec29b 413 * List of changed behaviors:
574bd9f7 414 *
aa6ec29b
TH
415 * - Mount options "noprefix", "xattr", "clone_children", "release_agent"
416 * and "name" are disallowed.
574bd9f7 417 *
aa6ec29b 418 * - When mounting an existing superblock, mount options should match.
574bd9f7 419 *
aa6ec29b 420 * - Remount is disallowed.
574bd9f7 421 *
aa6ec29b 422 * - rename(2) is disallowed.
574bd9f7 423 *
aa6ec29b
TH
424 * - "tasks" is removed. Everything should be at process granularity. Use
425 * "cgroup.procs" instead.
574bd9f7 426 *
aa6ec29b
TH
427 * - "cgroup.procs" is not sorted. pids will be unique unless they got
428 * recycled inbetween reads.
574bd9f7 429 *
aa6ec29b
TH
430 * - "release_agent" and "notify_on_release" are removed. Replacement
431 * notification mechanism will be implemented.
574bd9f7 432 *
aa6ec29b 433 * - "cgroup.clone_children" is removed.
75501a6d 434 *
aa6ec29b
TH
435 * - "cgroup.subtree_populated" is available. Its value is 0 if the cgroup
436 * and its descendants contain no task; otherwise, 1. The file also
437 * generates kernfs notification which can be monitored through poll and
438 * [di]notify when the value of the file changes.
574bd9f7 439 *
aa6ec29b
TH
440 * - cpuset: tasks will be kept in empty cpusets when hotplug happens and
441 * take masks of ancestors with non-empty cpus/mems, instead of being
442 * moved to an ancestor.
c2931b70 443 *
aa6ec29b
TH
444 * - cpuset: a task can be moved into an empty cpuset, and again it takes
445 * masks of ancestors.
c2931b70 446 *
aa6ec29b
TH
447 * - memcg: use_hierarchy is on by default and the cgroup file for the flag
448 * is not created.
449 *
450 * - blkcg: blk-throttle becomes properly hierarchical.
451 *
452 * - debug: disallowed on the default hierarchy.
574bd9f7 453 */
a2dd4247
TH
454static inline bool cgroup_on_dfl(const struct cgroup *cgrp)
455{
456 return cgrp->root == &cgrp_dfl_root;
457}
574bd9f7 458
07bc356e
TH
459/* no synchronization, the result can only be used as a hint */
460static inline bool cgroup_has_tasks(struct cgroup *cgrp)
461{
462 return !list_empty(&cgrp->cset_links);
463}
f3d46500 464
f29374b1 465/* returns ino associated with a cgroup */
b1664924
TH
466static inline ino_t cgroup_ino(struct cgroup *cgrp)
467{
f29374b1 468 return cgrp->kn->ino;
b1664924 469}
3ebb2b6e 470
b4168640
TH
471/* cft/css accessors for cftype->write() operation */
472static inline struct cftype *of_cft(struct kernfs_open_file *of)
7da11279 473{
2bd59d48 474 return of->kn->priv;
7da11279 475}
0f0a2b4f 476
b4168640 477struct cgroup_subsys_state *of_css(struct kernfs_open_file *of);
817929ec 478
b4168640
TH
479/* cft/css accessors for cftype->seq_*() operations */
480static inline struct cftype *seq_cft(struct seq_file *seq)
481{
482 return of_cft(seq->private);
483}
e535837b 484
b4168640
TH
485static inline struct cgroup_subsys_state *seq_css(struct seq_file *seq)
486{
487 return of_css(seq->private);
488}
31583bb0 489
e61734c5
TH
490/*
491 * Name / path handling functions. All are thin wrappers around the kernfs
492 * counterparts and can be called under any context.
493 */
38460b48 494
e61734c5
TH
495static inline int cgroup_name(struct cgroup *cgrp, char *buf, size_t buflen)
496{
fdce6bf8 497 return kernfs_name(cgrp->kn, buf, buflen);
e61734c5 498}
ddbcc7e8 499
e61734c5
TH
500static inline char * __must_check cgroup_path(struct cgroup *cgrp, char *buf,
501 size_t buflen)
502{
fdce6bf8 503 return kernfs_path(cgrp->kn, buf, buflen);
e61734c5 504}
f3ba5380 505
e61734c5
TH
506static inline void pr_cont_cgroup_name(struct cgroup *cgrp)
507{
fdce6bf8 508 pr_cont_kernfs_name(cgrp->kn);
e61734c5 509}
ddbcc7e8 510
e61734c5 511static inline void pr_cont_cgroup_path(struct cgroup *cgrp)
846c7bb0 512{
fdce6bf8 513 pr_cont_kernfs_path(cgrp->kn);
846c7bb0 514}
ddbcc7e8 515
ddbcc7e8
PM
516#else /* !CONFIG_CGROUPS */
517
f3ba5380 518struct cgroup_subsys_state;
f3ba5380 519
c326aa2b 520static inline void css_put(struct cgroup_subsys_state *css) {}
31583bb0 521static inline int cgroup_attach_task_all(struct task_struct *from,
c326aa2b
TH
522 struct task_struct *t) { return 0; }
523static inline int cgroupstats_build(struct cgroupstats *stats,
524 struct dentry *dentry) { return -EINVAL; }
525
b4f48b63 526static inline void cgroup_fork(struct task_struct *p) {}
817929ec 527static inline void cgroup_post_fork(struct task_struct *p) {}
1ec41830 528static inline void cgroup_exit(struct task_struct *p) {}
ddbcc7e8 529
c326aa2b
TH
530static inline int cgroup_init_early(void) { return 0; }
531static inline int cgroup_init(void) { return 0; }
d7926ee3 532
ddbcc7e8
PM
533#endif /* !CONFIG_CGROUPS */
534
535#endif /* _LINUX_CGROUP_H */