cgroup: kill cgroup_[un]lock()
[linux-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>
14#include <linux/rcupdate.h>
eb6fd504 15#include <linux/rculist.h>
846c7bb0 16#include <linux/cgroupstats.h>
31a7df01 17#include <linux/prio_heap.h>
cc31edce 18#include <linux/rwsem.h>
38460b48 19#include <linux/idr.h>
48ddbe19 20#include <linux/workqueue.h>
03b1cde6 21#include <linux/xattr.h>
ddbcc7e8
PM
22
23#ifdef CONFIG_CGROUPS
24
25struct cgroupfs_root;
26struct cgroup_subsys;
27struct inode;
84eea842 28struct cgroup;
38460b48 29struct css_id;
ddbcc7e8
PM
30
31extern int cgroup_init_early(void);
32extern int cgroup_init(void);
d11c563d 33extern int cgroup_lock_is_held(void);
b4f48b63 34extern void cgroup_fork(struct task_struct *p);
817929ec 35extern void cgroup_post_fork(struct task_struct *p);
b4f48b63 36extern void cgroup_exit(struct task_struct *p, int run_callbacks);
846c7bb0
BS
37extern int cgroupstats_build(struct cgroupstats *stats,
38 struct dentry *dentry);
e6a1105b 39extern int cgroup_load_subsys(struct cgroup_subsys *ss);
cf5d5941 40extern void cgroup_unload_subsys(struct cgroup_subsys *ss);
ddbcc7e8 41
828c0950 42extern const struct file_operations proc_cgroup_operations;
a424316c 43
7d8e0bf5
LZ
44/*
45 * Define the enumeration of all cgroup subsystems.
46 *
47 * We define ids for builtin subsystems and then modular ones.
48 */
817929ec
PM
49#define SUBSYS(_x) _x ## _subsys_id,
50enum cgroup_subsys_id {
7d8e0bf5 51#define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option)
817929ec 52#include <linux/cgroup_subsys.h>
7d8e0bf5
LZ
53#undef IS_SUBSYS_ENABLED
54 CGROUP_BUILTIN_SUBSYS_COUNT,
55
56 __CGROUP_SUBSYS_TEMP_PLACEHOLDER = CGROUP_BUILTIN_SUBSYS_COUNT - 1,
57
58#define IS_SUBSYS_ENABLED(option) IS_MODULE(option)
59#include <linux/cgroup_subsys.h>
60#undef IS_SUBSYS_ENABLED
a6f00298 61 CGROUP_SUBSYS_COUNT,
817929ec
PM
62};
63#undef SUBSYS
64
ddbcc7e8
PM
65/* Per-subsystem/per-cgroup state maintained by the system. */
66struct cgroup_subsys_state {
d20a390a
PM
67 /*
68 * The cgroup that this subsystem is attached to. Useful
ddbcc7e8 69 * for subsystems that want to know about the cgroup
d20a390a
PM
70 * hierarchy structure
71 */
ddbcc7e8
PM
72 struct cgroup *cgroup;
73
d20a390a
PM
74 /*
75 * State maintained by the cgroup system to allow subsystems
e7c5ec91 76 * to be "busy". Should be accessed via css_get(),
d0b2fdd2 77 * css_tryget() and css_put().
d20a390a 78 */
ddbcc7e8
PM
79
80 atomic_t refcnt;
81
82 unsigned long flags;
38460b48 83 /* ID for this css, if possible */
2c392b8c 84 struct css_id __rcu *id;
48ddbe19
TH
85
86 /* Used to put @cgroup->dentry on the last css_put() */
87 struct work_struct dput_work;
ddbcc7e8
PM
88};
89
90/* bits in struct cgroup_subsys_state flags field */
91enum {
38b53aba 92 CSS_ROOT = (1 << 0), /* this CSS is the root of the subsystem */
92fb9748 93 CSS_ONLINE = (1 << 1), /* between ->css_online() and ->css_offline() */
ddbcc7e8
PM
94};
95
d7b9fff7
DN
96/* Caller must verify that the css is not for root cgroup */
97static inline void __css_get(struct cgroup_subsys_state *css, int count)
98{
99 atomic_add(count, &css->refcnt);
100}
101
ddbcc7e8 102/*
e7c5ec91
PM
103 * Call css_get() to hold a reference on the css; it can be used
104 * for a reference obtained via:
105 * - an existing ref-counted reference to the css
106 * - task->cgroups for a locked task
ddbcc7e8
PM
107 */
108
109static inline void css_get(struct cgroup_subsys_state *css)
110{
111 /* We don't need to reference count the root state */
38b53aba 112 if (!(css->flags & CSS_ROOT))
d7b9fff7 113 __css_get(css, 1);
ddbcc7e8 114}
e7c5ec91 115
e7c5ec91
PM
116/*
117 * Call css_tryget() to take a reference on a css if your existing
118 * (known-valid) reference isn't already ref-counted. Returns false if
119 * the css has been destroyed.
120 */
121
28b4c27b 122extern bool __css_tryget(struct cgroup_subsys_state *css);
e7c5ec91
PM
123static inline bool css_tryget(struct cgroup_subsys_state *css)
124{
38b53aba 125 if (css->flags & CSS_ROOT)
e7c5ec91 126 return true;
28b4c27b 127 return __css_tryget(css);
e7c5ec91
PM
128}
129
ddbcc7e8
PM
130/*
131 * css_put() should be called to release a reference taken by
e7c5ec91 132 * css_get() or css_tryget()
ddbcc7e8
PM
133 */
134
28b4c27b 135extern void __css_put(struct cgroup_subsys_state *css);
ddbcc7e8
PM
136static inline void css_put(struct cgroup_subsys_state *css)
137{
38b53aba 138 if (!(css->flags & CSS_ROOT))
28b4c27b 139 __css_put(css);
ddbcc7e8
PM
140}
141
3116f0e3
PM
142/* bits in struct cgroup flags field */
143enum {
144 /* Control Group is dead */
145 CGRP_REMOVED,
d20a390a
PM
146 /*
147 * Control Group has previously had a child cgroup or a task,
148 * but no longer (only if CGRP_NOTIFY_ON_RELEASE is set)
149 */
3116f0e3
PM
150 CGRP_RELEASABLE,
151 /* Control Group requires release notifications to userspace */
152 CGRP_NOTIFY_ON_RELEASE,
97978e6d 153 /*
2260e7fc
TH
154 * Clone the parent's configuration when creating a new child
155 * cpuset cgroup. For historical reasons, this option can be
156 * specified at mount time and thus is implemented here.
97978e6d 157 */
2260e7fc 158 CGRP_CPUSET_CLONE_CHILDREN,
3116f0e3
PM
159};
160
65dff759
LZ
161struct cgroup_name {
162 struct rcu_head rcu_head;
163 char name[];
164};
165
ddbcc7e8
PM
166struct cgroup {
167 unsigned long flags; /* "unsigned long" so bitops work */
168
d20a390a
PM
169 /*
170 * count users of this cgroup. >0 means busy, but doesn't
171 * necessarily indicate the number of tasks in the cgroup
172 */
ddbcc7e8
PM
173 atomic_t count;
174
0a950f65
TH
175 int id; /* ida allocated in-hierarchy ID */
176
ddbcc7e8
PM
177 /*
178 * We link our 'sibling' struct into our parent's 'children'.
179 * Our children link their 'sibling' into our 'children'.
180 */
181 struct list_head sibling; /* my parent's children */
182 struct list_head children; /* my children */
05ef1d7c 183 struct list_head files; /* my files */
ddbcc7e8 184
d20a390a 185 struct cgroup *parent; /* my parent */
febfcef6 186 struct dentry *dentry; /* cgroup fs entry, RCU protected */
ddbcc7e8 187
65dff759
LZ
188 /*
189 * This is a copy of dentry->d_name, and it's needed because
190 * we can't use dentry->d_name in cgroup_path().
191 *
192 * You must acquire rcu_read_lock() to access cgrp->name, and
193 * the only place that can change it is rename(), which is
194 * protected by parent dir's i_mutex.
195 *
196 * Normally you should use cgroup_name() wrapper rather than
197 * access it directly.
198 */
199 struct cgroup_name __rcu *name;
200
ddbcc7e8
PM
201 /* Private pointers for each registered subsystem */
202 struct cgroup_subsys_state *subsys[CGROUP_SUBSYS_COUNT];
203
204 struct cgroupfs_root *root;
205 struct cgroup *top_cgroup;
817929ec
PM
206
207 /*
208 * List of cg_cgroup_links pointing at css_sets with
209 * tasks in this cgroup. Protected by css_set_lock
210 */
211 struct list_head css_sets;
81a6a5cd 212
b0ca5a84 213 struct list_head allcg_node; /* cgroupfs_root->allcg_list */
8e3f6541 214 struct list_head cft_q_node; /* used during cftype add/rm */
b0ca5a84 215
81a6a5cd
PM
216 /*
217 * Linked list running through all cgroups that can
218 * potentially be reaped by the release agent. Protected by
219 * release_list_lock
220 */
221 struct list_head release_list;
cc31edce 222
72a8cb30
BB
223 /*
224 * list of pidlists, up to two for each namespace (one for procs, one
225 * for tasks); created on demand.
226 */
227 struct list_head pidlists;
228 struct mutex pidlist_mutex;
a47295e6
PM
229
230 /* For RCU-protected deletion */
231 struct rcu_head rcu_head;
be445626 232 struct work_struct free_work;
0dea1168 233
25985edc 234 /* List of events which userspace want to receive */
0dea1168
KS
235 struct list_head event_list;
236 spinlock_t event_list_lock;
03b1cde6
AR
237
238 /* directory xattrs */
239 struct simple_xattrs xattrs;
817929ec
PM
240};
241
d20a390a
PM
242/*
243 * A css_set is a structure holding pointers to a set of
817929ec
PM
244 * cgroup_subsys_state objects. This saves space in the task struct
245 * object and speeds up fork()/exit(), since a single inc/dec and a
d20a390a
PM
246 * list_add()/del() can bump the reference count on the entire cgroup
247 * set for a task.
817929ec
PM
248 */
249
250struct css_set {
251
252 /* Reference count */
146aa1bd 253 atomic_t refcount;
817929ec 254
472b1053
LZ
255 /*
256 * List running through all cgroup groups in the same hash
257 * slot. Protected by css_set_lock
258 */
259 struct hlist_node hlist;
260
817929ec
PM
261 /*
262 * List running through all tasks using this cgroup
263 * group. Protected by css_set_lock
264 */
265 struct list_head tasks;
266
267 /*
268 * List of cg_cgroup_link objects on link chains from
269 * cgroups referenced from this css_set. Protected by
270 * css_set_lock
271 */
272 struct list_head cg_links;
273
274 /*
275 * Set of subsystem states, one for each subsystem. This array
276 * is immutable after creation apart from the init_css_set
cf5d5941
BB
277 * during subsystem registration (at boot time) and modular subsystem
278 * loading/unloading.
817929ec
PM
279 */
280 struct cgroup_subsys_state *subsys[CGROUP_SUBSYS_COUNT];
c378369d
BB
281
282 /* For RCU-protected deletion */
283 struct rcu_head rcu_head;
ddbcc7e8
PM
284};
285
91796569
PM
286/*
287 * cgroup_map_cb is an abstract callback API for reporting map-valued
288 * control files
289 */
290
291struct cgroup_map_cb {
292 int (*fill)(struct cgroup_map_cb *cb, const char *key, u64 value);
293 void *state;
294};
295
d20a390a
PM
296/*
297 * struct cftype: handler definitions for cgroup control files
ddbcc7e8
PM
298 *
299 * When reading/writing to a file:
a043e3b2 300 * - the cgroup to use is file->f_dentry->d_parent->d_fsdata
ddbcc7e8
PM
301 * - the 'cftype' of the file is file->f_dentry->d_fsdata
302 */
303
8e3f6541
TH
304/* cftype->flags */
305#define CFTYPE_ONLY_ON_ROOT (1U << 0) /* only create on root cg */
d0b2fdd2 306#define CFTYPE_NOT_ON_ROOT (1U << 1) /* don't create on root cg */
8e3f6541
TH
307
308#define MAX_CFTYPE_NAME 64
309
ddbcc7e8 310struct cftype {
d20a390a
PM
311 /*
312 * By convention, the name should begin with the name of the
8e3f6541
TH
313 * subsystem, followed by a period. Zero length string indicates
314 * end of cftype array.
d20a390a 315 */
ddbcc7e8
PM
316 char name[MAX_CFTYPE_NAME];
317 int private;
099fca32
LZ
318 /*
319 * If not 0, file mode is set to this value, otherwise it will
320 * be figured out automatically
321 */
a5e7ed32 322 umode_t mode;
db3b1497
PM
323
324 /*
325 * If non-zero, defines the maximum length of string that can
326 * be passed to write_string; defaults to 64
327 */
328 size_t max_write_len;
329
8e3f6541
TH
330 /* CFTYPE_* flags */
331 unsigned int flags;
332
03b1cde6
AR
333 /* file xattrs */
334 struct simple_xattrs xattrs;
335
ce16b49d
PM
336 int (*open)(struct inode *inode, struct file *file);
337 ssize_t (*read)(struct cgroup *cgrp, struct cftype *cft,
338 struct file *file,
339 char __user *buf, size_t nbytes, loff_t *ppos);
ddbcc7e8 340 /*
f4c753b7 341 * read_u64() is a shortcut for the common case of returning a
ddbcc7e8
PM
342 * single integer. Use it in place of read()
343 */
ce16b49d 344 u64 (*read_u64)(struct cgroup *cgrp, struct cftype *cft);
e73d2c61
PM
345 /*
346 * read_s64() is a signed version of read_u64()
347 */
ce16b49d 348 s64 (*read_s64)(struct cgroup *cgrp, struct cftype *cft);
91796569
PM
349 /*
350 * read_map() is used for defining a map of key/value
351 * pairs. It should call cb->fill(cb, key, value) for each
352 * entry. The key/value pairs (and their ordering) should not
353 * change between reboots.
354 */
ce16b49d
PM
355 int (*read_map)(struct cgroup *cont, struct cftype *cft,
356 struct cgroup_map_cb *cb);
29486df3
SH
357 /*
358 * read_seq_string() is used for outputting a simple sequence
359 * using seqfile.
360 */
ce16b49d
PM
361 int (*read_seq_string)(struct cgroup *cont, struct cftype *cft,
362 struct seq_file *m);
91796569 363
ce16b49d
PM
364 ssize_t (*write)(struct cgroup *cgrp, struct cftype *cft,
365 struct file *file,
366 const char __user *buf, size_t nbytes, loff_t *ppos);
355e0c48
PM
367
368 /*
f4c753b7 369 * write_u64() is a shortcut for the common case of accepting
355e0c48
PM
370 * a single integer (as parsed by simple_strtoull) from
371 * userspace. Use in place of write(); return 0 or error.
372 */
ce16b49d 373 int (*write_u64)(struct cgroup *cgrp, struct cftype *cft, u64 val);
e73d2c61
PM
374 /*
375 * write_s64() is a signed version of write_u64()
376 */
ce16b49d 377 int (*write_s64)(struct cgroup *cgrp, struct cftype *cft, s64 val);
355e0c48 378
db3b1497
PM
379 /*
380 * write_string() is passed a nul-terminated kernelspace
381 * buffer of maximum length determined by max_write_len.
382 * Returns 0 or -ve error code.
383 */
384 int (*write_string)(struct cgroup *cgrp, struct cftype *cft,
385 const char *buffer);
d447ea2f
PE
386 /*
387 * trigger() callback can be used to get some kick from the
388 * userspace, when the actual string written is not important
389 * at all. The private field can be used to determine the
390 * kick type for multiplexing.
391 */
392 int (*trigger)(struct cgroup *cgrp, unsigned int event);
393
ce16b49d 394 int (*release)(struct inode *inode, struct file *file);
0dea1168
KS
395
396 /*
397 * register_event() callback will be used to add new userspace
398 * waiter for changes related to the cftype. Implement it if
399 * you want to provide this functionality. Use eventfd_signal()
400 * on eventfd to send notification to userspace.
401 */
402 int (*register_event)(struct cgroup *cgrp, struct cftype *cft,
403 struct eventfd_ctx *eventfd, const char *args);
404 /*
405 * unregister_event() callback will be called when userspace
406 * closes the eventfd or on cgroup removing.
407 * This callback must be implemented, if you want provide
408 * notification functionality.
0dea1168 409 */
907860ed 410 void (*unregister_event)(struct cgroup *cgrp, struct cftype *cft,
0dea1168 411 struct eventfd_ctx *eventfd);
ddbcc7e8
PM
412};
413
8e3f6541
TH
414/*
415 * cftype_sets describe cftypes belonging to a subsystem and are chained at
416 * cgroup_subsys->cftsets. Each cftset points to an array of cftypes
417 * terminated by zero length name.
418 */
419struct cftype_set {
420 struct list_head node; /* chained at subsys->cftsets */
03b1cde6 421 struct cftype *cfts;
8e3f6541
TH
422};
423
31a7df01
CW
424struct cgroup_scanner {
425 struct cgroup *cg;
426 int (*test_task)(struct task_struct *p, struct cgroup_scanner *scan);
427 void (*process_task)(struct task_struct *p,
428 struct cgroup_scanner *scan);
429 struct ptr_heap *heap;
bd1a8ab7 430 void *data;
31a7df01
CW
431};
432
65dff759
LZ
433/* Caller should hold rcu_read_lock() */
434static inline const char *cgroup_name(const struct cgroup *cgrp)
435{
436 return rcu_dereference(cgrp->name)->name;
437}
438
03b1cde6
AR
439int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts);
440int cgroup_rm_cftypes(struct cgroup_subsys *ss, struct cftype *cfts);
8e3f6541 441
ffd2d883 442int cgroup_is_removed(const struct cgroup *cgrp);
ddbcc7e8 443
ffd2d883 444int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen);
ddbcc7e8 445
ffd2d883 446int cgroup_task_count(const struct cgroup *cgrp);
bbcb81d0 447
2f7ee569
TH
448/*
449 * Control Group taskset, used to pass around set of tasks to cgroup_subsys
450 * methods.
451 */
452struct cgroup_taskset;
453struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset);
454struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset);
455struct cgroup *cgroup_taskset_cur_cgroup(struct cgroup_taskset *tset);
456int cgroup_taskset_size(struct cgroup_taskset *tset);
457
458/**
459 * cgroup_taskset_for_each - iterate cgroup_taskset
460 * @task: the loop cursor
461 * @skip_cgrp: skip if task's cgroup matches this, %NULL to iterate through all
462 * @tset: taskset to iterate
463 */
464#define cgroup_taskset_for_each(task, skip_cgrp, tset) \
465 for ((task) = cgroup_taskset_first((tset)); (task); \
466 (task) = cgroup_taskset_next((tset))) \
467 if (!(skip_cgrp) || \
468 cgroup_taskset_cur_cgroup((tset)) != (skip_cgrp))
469
21acb9ca
TLSC
470/*
471 * Control Group subsystem type.
472 * See Documentation/cgroups/cgroups.txt for details
473 */
ddbcc7e8
PM
474
475struct cgroup_subsys {
92fb9748
TH
476 struct cgroup_subsys_state *(*css_alloc)(struct cgroup *cgrp);
477 int (*css_online)(struct cgroup *cgrp);
478 void (*css_offline)(struct cgroup *cgrp);
479 void (*css_free)(struct cgroup *cgrp);
480
761b3ef5
LZ
481 int (*can_attach)(struct cgroup *cgrp, struct cgroup_taskset *tset);
482 void (*cancel_attach)(struct cgroup *cgrp, struct cgroup_taskset *tset);
483 void (*attach)(struct cgroup *cgrp, struct cgroup_taskset *tset);
484 void (*fork)(struct task_struct *task);
485 void (*exit)(struct cgroup *cgrp, struct cgroup *old_cgrp,
486 struct task_struct *task);
761b3ef5 487 void (*bind)(struct cgroup *root);
e5991371 488
ddbcc7e8
PM
489 int subsys_id;
490 int active;
8bab8dde 491 int disabled;
ddbcc7e8 492 int early_init;
38460b48
KH
493 /*
494 * True if this subsys uses ID. ID is not available before cgroup_init()
495 * (not available in early_init time.)
496 */
497 bool use_id;
48ddbe19 498
8c7f6edb
TH
499 /*
500 * If %false, this subsystem is properly hierarchical -
501 * configuration, resource accounting and restriction on a parent
502 * cgroup cover those of its children. If %true, hierarchy support
503 * is broken in some ways - some subsystems ignore hierarchy
504 * completely while others are only implemented half-way.
505 *
506 * It's now disallowed to create nested cgroups if the subsystem is
507 * broken and cgroup core will emit a warning message on such
508 * cases. Eventually, all subsystems will be made properly
509 * hierarchical and this will go away.
510 */
511 bool broken_hierarchy;
512 bool warned_broken_hierarchy;
513
ddbcc7e8
PM
514#define MAX_CGROUP_TYPE_NAMELEN 32
515 const char *name;
516
999cd8a4
PM
517 /*
518 * Link to parent, and list entry in parent's children.
6be96a5c 519 * Protected by cgroup_lock()
999cd8a4
PM
520 */
521 struct cgroupfs_root *root;
ddbcc7e8 522 struct list_head sibling;
38460b48
KH
523 /* used when use_id == true */
524 struct idr idr;
42aee6c4 525 spinlock_t id_lock;
e6a1105b 526
8e3f6541
TH
527 /* list of cftype_sets */
528 struct list_head cftsets;
529
530 /* base cftypes, automatically [de]registered with subsys itself */
531 struct cftype *base_cftypes;
532 struct cftype_set base_cftset;
533
e6a1105b
BB
534 /* should be defined only by modular subsystems */
535 struct module *module;
ddbcc7e8
PM
536};
537
538#define SUBSYS(_x) extern struct cgroup_subsys _x ## _subsys;
5fc0b025 539#define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option)
ddbcc7e8 540#include <linux/cgroup_subsys.h>
5fc0b025 541#undef IS_SUBSYS_ENABLED
ddbcc7e8
PM
542#undef SUBSYS
543
544static inline struct cgroup_subsys_state *cgroup_subsys_state(
ffd2d883 545 struct cgroup *cgrp, int subsys_id)
ddbcc7e8 546{
ffd2d883 547 return cgrp->subsys[subsys_id];
ddbcc7e8
PM
548}
549
dc61b1d6
PZ
550/*
551 * function to get the cgroup_subsys_state which allows for extra
552 * rcu_dereference_check() conditions, such as locks used during the
553 * cgroup_subsys::attach() methods.
554 */
555#define task_subsys_state_check(task, subsys_id, __c) \
556 rcu_dereference_check(task->cgroups->subsys[subsys_id], \
dc61b1d6
PZ
557 lockdep_is_held(&task->alloc_lock) || \
558 cgroup_lock_is_held() || (__c))
559
560static inline struct cgroup_subsys_state *
561task_subsys_state(struct task_struct *task, int subsys_id)
ddbcc7e8 562{
dc61b1d6 563 return task_subsys_state_check(task, subsys_id, false);
ddbcc7e8
PM
564}
565
566static inline struct cgroup* task_cgroup(struct task_struct *task,
567 int subsys_id)
568{
569 return task_subsys_state(task, subsys_id)->cgroup;
570}
571
574bd9f7
TH
572/**
573 * cgroup_for_each_child - iterate through children of a cgroup
574 * @pos: the cgroup * to use as the loop cursor
575 * @cgroup: cgroup whose children to walk
576 *
577 * Walk @cgroup's children. Must be called under rcu_read_lock(). A child
92fb9748
TH
578 * cgroup which hasn't finished ->css_online() or already has finished
579 * ->css_offline() may show up during traversal and it's each subsystem's
574bd9f7
TH
580 * responsibility to verify that each @pos is alive.
581 *
92fb9748
TH
582 * If a subsystem synchronizes against the parent in its ->css_online() and
583 * before starting iterating, a cgroup which finished ->css_online() is
584 * guaranteed to be visible in the future iterations.
574bd9f7
TH
585 */
586#define cgroup_for_each_child(pos, cgroup) \
587 list_for_each_entry_rcu(pos, &(cgroup)->children, sibling)
588
589struct cgroup *cgroup_next_descendant_pre(struct cgroup *pos,
590 struct cgroup *cgroup);
12a9d2fe 591struct cgroup *cgroup_rightmost_descendant(struct cgroup *pos);
574bd9f7
TH
592
593/**
594 * cgroup_for_each_descendant_pre - pre-order walk of a cgroup's descendants
595 * @pos: the cgroup * to use as the loop cursor
596 * @cgroup: cgroup whose descendants to walk
597 *
598 * Walk @cgroup's descendants. Must be called under rcu_read_lock(). A
92fb9748
TH
599 * descendant cgroup which hasn't finished ->css_online() or already has
600 * finished ->css_offline() may show up during traversal and it's each
574bd9f7
TH
601 * subsystem's responsibility to verify that each @pos is alive.
602 *
92fb9748
TH
603 * If a subsystem synchronizes against the parent in its ->css_online() and
604 * before starting iterating, and synchronizes against @pos on each
605 * iteration, any descendant cgroup which finished ->css_offline() is
574bd9f7
TH
606 * guaranteed to be visible in the future iterations.
607 *
608 * In other words, the following guarantees that a descendant can't escape
609 * state updates of its ancestors.
610 *
92fb9748 611 * my_online(@cgrp)
574bd9f7
TH
612 * {
613 * Lock @cgrp->parent and @cgrp;
614 * Inherit state from @cgrp->parent;
615 * Unlock both.
616 * }
617 *
618 * my_update_state(@cgrp)
619 * {
620 * Lock @cgrp;
621 * Update @cgrp's state;
622 * Unlock @cgrp;
623 *
624 * cgroup_for_each_descendant_pre(@pos, @cgrp) {
625 * Lock @pos;
626 * Verify @pos is alive and inherit state from @pos->parent;
627 * Unlock @pos;
628 * }
629 * }
630 *
631 * As long as the inheriting step, including checking the parent state, is
632 * enclosed inside @pos locking, double-locking the parent isn't necessary
633 * while inheriting. The state update to the parent is guaranteed to be
634 * visible by walking order and, as long as inheriting operations to the
635 * same @pos are atomic to each other, multiple updates racing each other
636 * still result in the correct state. It's guaranateed that at least one
637 * inheritance happens for any cgroup after the latest update to its
638 * parent.
639 *
640 * If checking parent's state requires locking the parent, each inheriting
641 * iteration should lock and unlock both @pos->parent and @pos.
642 *
643 * Alternatively, a subsystem may choose to use a single global lock to
92fb9748 644 * synchronize ->css_online() and ->css_offline() against tree-walking
574bd9f7
TH
645 * operations.
646 */
647#define cgroup_for_each_descendant_pre(pos, cgroup) \
648 for (pos = cgroup_next_descendant_pre(NULL, (cgroup)); (pos); \
649 pos = cgroup_next_descendant_pre((pos), (cgroup)))
650
651struct cgroup *cgroup_next_descendant_post(struct cgroup *pos,
652 struct cgroup *cgroup);
653
654/**
655 * cgroup_for_each_descendant_post - post-order walk of a cgroup's descendants
656 * @pos: the cgroup * to use as the loop cursor
657 * @cgroup: cgroup whose descendants to walk
658 *
659 * Similar to cgroup_for_each_descendant_pre() but performs post-order
660 * traversal instead. Note that the walk visibility guarantee described in
661 * pre-order walk doesn't apply the same to post-order walks.
662 */
663#define cgroup_for_each_descendant_post(pos, cgroup) \
664 for (pos = cgroup_next_descendant_post(NULL, (cgroup)); (pos); \
665 pos = cgroup_next_descendant_post((pos), (cgroup)))
666
817929ec
PM
667/* A cgroup_iter should be treated as an opaque object */
668struct cgroup_iter {
669 struct list_head *cg_link;
670 struct list_head *task;
671};
672
d20a390a
PM
673/*
674 * To iterate across the tasks in a cgroup:
817929ec 675 *
b595076a 676 * 1) call cgroup_iter_start to initialize an iterator
817929ec
PM
677 *
678 * 2) call cgroup_iter_next() to retrieve member tasks until it
679 * returns NULL or until you want to end the iteration
680 *
681 * 3) call cgroup_iter_end() to destroy the iterator.
31a7df01 682 *
d20a390a
PM
683 * Or, call cgroup_scan_tasks() to iterate through every task in a
684 * cgroup - cgroup_scan_tasks() holds the css_set_lock when calling
685 * the test_task() callback, but not while calling the process_task()
686 * callback.
817929ec 687 */
ffd2d883
LZ
688void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it);
689struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
817929ec 690 struct cgroup_iter *it);
ffd2d883 691void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it);
31a7df01 692int cgroup_scan_tasks(struct cgroup_scanner *scan);
31583bb0 693int cgroup_attach_task_all(struct task_struct *from, struct task_struct *);
8cc99345 694int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from);
31583bb0 695
38460b48
KH
696/*
697 * CSS ID is ID for cgroup_subsys_state structs under subsys. This only works
698 * if cgroup_subsys.use_id == true. It can be used for looking up and scanning.
699 * CSS ID is assigned at cgroup allocation (create) automatically
700 * and removed when subsys calls free_css_id() function. This is because
701 * the lifetime of cgroup_subsys_state is subsys's matter.
702 *
703 * Looking up and scanning function should be called under rcu_read_lock().
6be96a5c 704 * Taking cgroup_mutex is not necessary for following calls.
38460b48
KH
705 * But the css returned by this routine can be "not populated yet" or "being
706 * destroyed". The caller should check css and cgroup's status.
707 */
708
709/*
710 * Typically Called at ->destroy(), or somewhere the subsys frees
711 * cgroup_subsys_state.
712 */
713void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css);
714
715/* Find a cgroup_subsys_state which has given ID */
716
717struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id);
718
719/*
720 * Get a cgroup whose id is greater than or equal to id under tree of root.
721 * Returning a cgroup_subsys_state or NULL.
722 */
723struct cgroup_subsys_state *css_get_next(struct cgroup_subsys *ss, int id,
724 struct cgroup_subsys_state *root, int *foundid);
725
726/* Returns true if root is ancestor of cg */
727bool css_is_ancestor(struct cgroup_subsys_state *cg,
0b7f569e 728 const struct cgroup_subsys_state *root);
38460b48
KH
729
730/* Get id and depth of css */
731unsigned short css_id(struct cgroup_subsys_state *css);
732unsigned short css_depth(struct cgroup_subsys_state *css);
e5d1367f 733struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id);
38460b48 734
ddbcc7e8
PM
735#else /* !CONFIG_CGROUPS */
736
737static inline int cgroup_init_early(void) { return 0; }
738static inline int cgroup_init(void) { return 0; }
b4f48b63 739static inline void cgroup_fork(struct task_struct *p) {}
817929ec 740static inline void cgroup_post_fork(struct task_struct *p) {}
b4f48b63 741static inline void cgroup_exit(struct task_struct *p, int callbacks) {}
ddbcc7e8
PM
742
743static inline void cgroup_lock(void) {}
744static inline void cgroup_unlock(void) {}
846c7bb0
BS
745static inline int cgroupstats_build(struct cgroupstats *stats,
746 struct dentry *dentry)
747{
748 return -EINVAL;
749}
ddbcc7e8 750
d7926ee3 751/* No cgroups - nothing to do */
31583bb0
MT
752static inline int cgroup_attach_task_all(struct task_struct *from,
753 struct task_struct *t)
754{
755 return 0;
756}
d7926ee3 757
ddbcc7e8
PM
758#endif /* !CONFIG_CGROUPS */
759
760#endif /* _LINUX_CGROUP_H */