]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - include/linux/cgroup.h
move cgroupfs_root to include/linux/cgroup.h
[mirror_ubuntu-zesty-kernel.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>
25a7e684 22#include <linux/fs.h>
ddbcc7e8
PM
23
24#ifdef CONFIG_CGROUPS
25
26struct cgroupfs_root;
27struct cgroup_subsys;
28struct inode;
84eea842 29struct cgroup;
38460b48 30struct css_id;
ddbcc7e8
PM
31
32extern int cgroup_init_early(void);
33extern int cgroup_init(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
25a7e684
TH
242#define MAX_CGROUP_ROOT_NAMELEN 64
243
244/* cgroupfs_root->flags */
245enum {
246 CGRP_ROOT_NOPREFIX = (1 << 1), /* mounted subsystems have no named prefix */
247 CGRP_ROOT_XATTR = (1 << 2), /* supports extended attributes */
248};
249
250/*
251 * A cgroupfs_root represents the root of a cgroup hierarchy, and may be
252 * associated with a superblock to form an active hierarchy. This is
253 * internal to cgroup core. Don't access directly from controllers.
254 */
255struct cgroupfs_root {
256 struct super_block *sb;
257
258 /*
259 * The bitmask of subsystems intended to be attached to this
260 * hierarchy
261 */
262 unsigned long subsys_mask;
263
264 /* Unique id for this hierarchy. */
265 int hierarchy_id;
266
267 /* The bitmask of subsystems currently attached to this hierarchy */
268 unsigned long actual_subsys_mask;
269
270 /* A list running through the attached subsystems */
271 struct list_head subsys_list;
272
273 /* The root cgroup for this hierarchy */
274 struct cgroup top_cgroup;
275
276 /* Tracks how many cgroups are currently defined in hierarchy.*/
277 int number_of_cgroups;
278
279 /* A list running through the active hierarchies */
280 struct list_head root_list;
281
282 /* All cgroups on this root, cgroup_mutex protected */
283 struct list_head allcg_list;
284
285 /* Hierarchy-specific flags */
286 unsigned long flags;
287
288 /* IDs for cgroups in this hierarchy */
289 struct ida cgroup_ida;
290
291 /* The path to use for release notifications. */
292 char release_agent_path[PATH_MAX];
293
294 /* The name for this hierarchy - may be empty */
295 char name[MAX_CGROUP_ROOT_NAMELEN];
296};
297
d20a390a
PM
298/*
299 * A css_set is a structure holding pointers to a set of
817929ec
PM
300 * cgroup_subsys_state objects. This saves space in the task struct
301 * object and speeds up fork()/exit(), since a single inc/dec and a
d20a390a
PM
302 * list_add()/del() can bump the reference count on the entire cgroup
303 * set for a task.
817929ec
PM
304 */
305
306struct css_set {
307
308 /* Reference count */
146aa1bd 309 atomic_t refcount;
817929ec 310
472b1053
LZ
311 /*
312 * List running through all cgroup groups in the same hash
313 * slot. Protected by css_set_lock
314 */
315 struct hlist_node hlist;
316
817929ec
PM
317 /*
318 * List running through all tasks using this cgroup
319 * group. Protected by css_set_lock
320 */
321 struct list_head tasks;
322
323 /*
324 * List of cg_cgroup_link objects on link chains from
325 * cgroups referenced from this css_set. Protected by
326 * css_set_lock
327 */
328 struct list_head cg_links;
329
330 /*
331 * Set of subsystem states, one for each subsystem. This array
332 * is immutable after creation apart from the init_css_set
cf5d5941
BB
333 * during subsystem registration (at boot time) and modular subsystem
334 * loading/unloading.
817929ec
PM
335 */
336 struct cgroup_subsys_state *subsys[CGROUP_SUBSYS_COUNT];
c378369d
BB
337
338 /* For RCU-protected deletion */
339 struct rcu_head rcu_head;
ddbcc7e8
PM
340};
341
91796569
PM
342/*
343 * cgroup_map_cb is an abstract callback API for reporting map-valued
344 * control files
345 */
346
347struct cgroup_map_cb {
348 int (*fill)(struct cgroup_map_cb *cb, const char *key, u64 value);
349 void *state;
350};
351
d20a390a
PM
352/*
353 * struct cftype: handler definitions for cgroup control files
ddbcc7e8
PM
354 *
355 * When reading/writing to a file:
a043e3b2 356 * - the cgroup to use is file->f_dentry->d_parent->d_fsdata
ddbcc7e8
PM
357 * - the 'cftype' of the file is file->f_dentry->d_fsdata
358 */
359
8e3f6541
TH
360/* cftype->flags */
361#define CFTYPE_ONLY_ON_ROOT (1U << 0) /* only create on root cg */
d0b2fdd2 362#define CFTYPE_NOT_ON_ROOT (1U << 1) /* don't create on root cg */
8e3f6541
TH
363
364#define MAX_CFTYPE_NAME 64
365
ddbcc7e8 366struct cftype {
d20a390a
PM
367 /*
368 * By convention, the name should begin with the name of the
8e3f6541
TH
369 * subsystem, followed by a period. Zero length string indicates
370 * end of cftype array.
d20a390a 371 */
ddbcc7e8
PM
372 char name[MAX_CFTYPE_NAME];
373 int private;
099fca32
LZ
374 /*
375 * If not 0, file mode is set to this value, otherwise it will
376 * be figured out automatically
377 */
a5e7ed32 378 umode_t mode;
db3b1497
PM
379
380 /*
381 * If non-zero, defines the maximum length of string that can
382 * be passed to write_string; defaults to 64
383 */
384 size_t max_write_len;
385
8e3f6541
TH
386 /* CFTYPE_* flags */
387 unsigned int flags;
388
03b1cde6
AR
389 /* file xattrs */
390 struct simple_xattrs xattrs;
391
ce16b49d
PM
392 int (*open)(struct inode *inode, struct file *file);
393 ssize_t (*read)(struct cgroup *cgrp, struct cftype *cft,
394 struct file *file,
395 char __user *buf, size_t nbytes, loff_t *ppos);
ddbcc7e8 396 /*
f4c753b7 397 * read_u64() is a shortcut for the common case of returning a
ddbcc7e8
PM
398 * single integer. Use it in place of read()
399 */
ce16b49d 400 u64 (*read_u64)(struct cgroup *cgrp, struct cftype *cft);
e73d2c61
PM
401 /*
402 * read_s64() is a signed version of read_u64()
403 */
ce16b49d 404 s64 (*read_s64)(struct cgroup *cgrp, struct cftype *cft);
91796569
PM
405 /*
406 * read_map() is used for defining a map of key/value
407 * pairs. It should call cb->fill(cb, key, value) for each
408 * entry. The key/value pairs (and their ordering) should not
409 * change between reboots.
410 */
ce16b49d
PM
411 int (*read_map)(struct cgroup *cont, struct cftype *cft,
412 struct cgroup_map_cb *cb);
29486df3
SH
413 /*
414 * read_seq_string() is used for outputting a simple sequence
415 * using seqfile.
416 */
ce16b49d
PM
417 int (*read_seq_string)(struct cgroup *cont, struct cftype *cft,
418 struct seq_file *m);
91796569 419
ce16b49d
PM
420 ssize_t (*write)(struct cgroup *cgrp, struct cftype *cft,
421 struct file *file,
422 const char __user *buf, size_t nbytes, loff_t *ppos);
355e0c48
PM
423
424 /*
f4c753b7 425 * write_u64() is a shortcut for the common case of accepting
355e0c48
PM
426 * a single integer (as parsed by simple_strtoull) from
427 * userspace. Use in place of write(); return 0 or error.
428 */
ce16b49d 429 int (*write_u64)(struct cgroup *cgrp, struct cftype *cft, u64 val);
e73d2c61
PM
430 /*
431 * write_s64() is a signed version of write_u64()
432 */
ce16b49d 433 int (*write_s64)(struct cgroup *cgrp, struct cftype *cft, s64 val);
355e0c48 434
db3b1497
PM
435 /*
436 * write_string() is passed a nul-terminated kernelspace
437 * buffer of maximum length determined by max_write_len.
438 * Returns 0 or -ve error code.
439 */
440 int (*write_string)(struct cgroup *cgrp, struct cftype *cft,
441 const char *buffer);
d447ea2f
PE
442 /*
443 * trigger() callback can be used to get some kick from the
444 * userspace, when the actual string written is not important
445 * at all. The private field can be used to determine the
446 * kick type for multiplexing.
447 */
448 int (*trigger)(struct cgroup *cgrp, unsigned int event);
449
ce16b49d 450 int (*release)(struct inode *inode, struct file *file);
0dea1168
KS
451
452 /*
453 * register_event() callback will be used to add new userspace
454 * waiter for changes related to the cftype. Implement it if
455 * you want to provide this functionality. Use eventfd_signal()
456 * on eventfd to send notification to userspace.
457 */
458 int (*register_event)(struct cgroup *cgrp, struct cftype *cft,
459 struct eventfd_ctx *eventfd, const char *args);
460 /*
461 * unregister_event() callback will be called when userspace
462 * closes the eventfd or on cgroup removing.
463 * This callback must be implemented, if you want provide
464 * notification functionality.
0dea1168 465 */
907860ed 466 void (*unregister_event)(struct cgroup *cgrp, struct cftype *cft,
0dea1168 467 struct eventfd_ctx *eventfd);
ddbcc7e8
PM
468};
469
8e3f6541
TH
470/*
471 * cftype_sets describe cftypes belonging to a subsystem and are chained at
472 * cgroup_subsys->cftsets. Each cftset points to an array of cftypes
473 * terminated by zero length name.
474 */
475struct cftype_set {
476 struct list_head node; /* chained at subsys->cftsets */
03b1cde6 477 struct cftype *cfts;
8e3f6541
TH
478};
479
31a7df01
CW
480struct cgroup_scanner {
481 struct cgroup *cg;
482 int (*test_task)(struct task_struct *p, struct cgroup_scanner *scan);
483 void (*process_task)(struct task_struct *p,
484 struct cgroup_scanner *scan);
485 struct ptr_heap *heap;
bd1a8ab7 486 void *data;
31a7df01
CW
487};
488
65dff759
LZ
489/* Caller should hold rcu_read_lock() */
490static inline const char *cgroup_name(const struct cgroup *cgrp)
491{
492 return rcu_dereference(cgrp->name)->name;
493}
494
03b1cde6
AR
495int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts);
496int cgroup_rm_cftypes(struct cgroup_subsys *ss, struct cftype *cfts);
8e3f6541 497
ffd2d883 498int cgroup_is_removed(const struct cgroup *cgrp);
78574cf9 499bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor);
ddbcc7e8 500
ffd2d883 501int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen);
ddbcc7e8 502
ffd2d883 503int cgroup_task_count(const struct cgroup *cgrp);
bbcb81d0 504
2f7ee569
TH
505/*
506 * Control Group taskset, used to pass around set of tasks to cgroup_subsys
507 * methods.
508 */
509struct cgroup_taskset;
510struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset);
511struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset);
512struct cgroup *cgroup_taskset_cur_cgroup(struct cgroup_taskset *tset);
513int cgroup_taskset_size(struct cgroup_taskset *tset);
514
515/**
516 * cgroup_taskset_for_each - iterate cgroup_taskset
517 * @task: the loop cursor
518 * @skip_cgrp: skip if task's cgroup matches this, %NULL to iterate through all
519 * @tset: taskset to iterate
520 */
521#define cgroup_taskset_for_each(task, skip_cgrp, tset) \
522 for ((task) = cgroup_taskset_first((tset)); (task); \
523 (task) = cgroup_taskset_next((tset))) \
524 if (!(skip_cgrp) || \
525 cgroup_taskset_cur_cgroup((tset)) != (skip_cgrp))
526
21acb9ca
TLSC
527/*
528 * Control Group subsystem type.
529 * See Documentation/cgroups/cgroups.txt for details
530 */
ddbcc7e8
PM
531
532struct cgroup_subsys {
92fb9748
TH
533 struct cgroup_subsys_state *(*css_alloc)(struct cgroup *cgrp);
534 int (*css_online)(struct cgroup *cgrp);
535 void (*css_offline)(struct cgroup *cgrp);
536 void (*css_free)(struct cgroup *cgrp);
537
761b3ef5
LZ
538 int (*can_attach)(struct cgroup *cgrp, struct cgroup_taskset *tset);
539 void (*cancel_attach)(struct cgroup *cgrp, struct cgroup_taskset *tset);
540 void (*attach)(struct cgroup *cgrp, struct cgroup_taskset *tset);
541 void (*fork)(struct task_struct *task);
542 void (*exit)(struct cgroup *cgrp, struct cgroup *old_cgrp,
543 struct task_struct *task);
26d5bbe5
TH
544 void (*bind)(struct cgroup *root);
545
ddbcc7e8
PM
546 int subsys_id;
547 int active;
8bab8dde 548 int disabled;
ddbcc7e8 549 int early_init;
38460b48
KH
550 /*
551 * True if this subsys uses ID. ID is not available before cgroup_init()
552 * (not available in early_init time.)
553 */
554 bool use_id;
48ddbe19 555
8c7f6edb
TH
556 /*
557 * If %false, this subsystem is properly hierarchical -
558 * configuration, resource accounting and restriction on a parent
559 * cgroup cover those of its children. If %true, hierarchy support
560 * is broken in some ways - some subsystems ignore hierarchy
561 * completely while others are only implemented half-way.
562 *
563 * It's now disallowed to create nested cgroups if the subsystem is
564 * broken and cgroup core will emit a warning message on such
565 * cases. Eventually, all subsystems will be made properly
566 * hierarchical and this will go away.
567 */
568 bool broken_hierarchy;
569 bool warned_broken_hierarchy;
570
ddbcc7e8
PM
571#define MAX_CGROUP_TYPE_NAMELEN 32
572 const char *name;
573
999cd8a4
PM
574 /*
575 * Link to parent, and list entry in parent's children.
6be96a5c 576 * Protected by cgroup_lock()
999cd8a4
PM
577 */
578 struct cgroupfs_root *root;
ddbcc7e8 579 struct list_head sibling;
38460b48
KH
580 /* used when use_id == true */
581 struct idr idr;
42aee6c4 582 spinlock_t id_lock;
e6a1105b 583
8e3f6541
TH
584 /* list of cftype_sets */
585 struct list_head cftsets;
586
587 /* base cftypes, automatically [de]registered with subsys itself */
588 struct cftype *base_cftypes;
589 struct cftype_set base_cftset;
590
e6a1105b
BB
591 /* should be defined only by modular subsystems */
592 struct module *module;
ddbcc7e8
PM
593};
594
595#define SUBSYS(_x) extern struct cgroup_subsys _x ## _subsys;
5fc0b025 596#define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option)
ddbcc7e8 597#include <linux/cgroup_subsys.h>
5fc0b025 598#undef IS_SUBSYS_ENABLED
ddbcc7e8
PM
599#undef SUBSYS
600
601static inline struct cgroup_subsys_state *cgroup_subsys_state(
ffd2d883 602 struct cgroup *cgrp, int subsys_id)
ddbcc7e8 603{
ffd2d883 604 return cgrp->subsys[subsys_id];
ddbcc7e8
PM
605}
606
dc61b1d6
PZ
607/*
608 * function to get the cgroup_subsys_state which allows for extra
609 * rcu_dereference_check() conditions, such as locks used during the
610 * cgroup_subsys::attach() methods.
611 */
2219449a
TH
612#ifdef CONFIG_PROVE_RCU
613extern struct mutex cgroup_mutex;
dc61b1d6 614#define task_subsys_state_check(task, subsys_id, __c) \
2219449a
TH
615 rcu_dereference_check((task)->cgroups->subsys[(subsys_id)], \
616 lockdep_is_held(&(task)->alloc_lock) || \
617 lockdep_is_held(&cgroup_mutex) || (__c))
618#else
619#define task_subsys_state_check(task, subsys_id, __c) \
620 rcu_dereference((task)->cgroups->subsys[(subsys_id)])
621#endif
dc61b1d6
PZ
622
623static inline struct cgroup_subsys_state *
624task_subsys_state(struct task_struct *task, int subsys_id)
ddbcc7e8 625{
dc61b1d6 626 return task_subsys_state_check(task, subsys_id, false);
ddbcc7e8
PM
627}
628
629static inline struct cgroup* task_cgroup(struct task_struct *task,
630 int subsys_id)
631{
632 return task_subsys_state(task, subsys_id)->cgroup;
633}
634
574bd9f7
TH
635/**
636 * cgroup_for_each_child - iterate through children of a cgroup
637 * @pos: the cgroup * to use as the loop cursor
638 * @cgroup: cgroup whose children to walk
639 *
640 * Walk @cgroup's children. Must be called under rcu_read_lock(). A child
92fb9748
TH
641 * cgroup which hasn't finished ->css_online() or already has finished
642 * ->css_offline() may show up during traversal and it's each subsystem's
574bd9f7
TH
643 * responsibility to verify that each @pos is alive.
644 *
92fb9748
TH
645 * If a subsystem synchronizes against the parent in its ->css_online() and
646 * before starting iterating, a cgroup which finished ->css_online() is
647 * guaranteed to be visible in the future iterations.
574bd9f7
TH
648 */
649#define cgroup_for_each_child(pos, cgroup) \
650 list_for_each_entry_rcu(pos, &(cgroup)->children, sibling)
651
652struct cgroup *cgroup_next_descendant_pre(struct cgroup *pos,
653 struct cgroup *cgroup);
12a9d2fe 654struct cgroup *cgroup_rightmost_descendant(struct cgroup *pos);
574bd9f7
TH
655
656/**
657 * cgroup_for_each_descendant_pre - pre-order walk of a cgroup's descendants
658 * @pos: the cgroup * to use as the loop cursor
659 * @cgroup: cgroup whose descendants to walk
660 *
661 * Walk @cgroup's descendants. Must be called under rcu_read_lock(). A
92fb9748
TH
662 * descendant cgroup which hasn't finished ->css_online() or already has
663 * finished ->css_offline() may show up during traversal and it's each
574bd9f7
TH
664 * subsystem's responsibility to verify that each @pos is alive.
665 *
92fb9748
TH
666 * If a subsystem synchronizes against the parent in its ->css_online() and
667 * before starting iterating, and synchronizes against @pos on each
668 * iteration, any descendant cgroup which finished ->css_offline() is
574bd9f7
TH
669 * guaranteed to be visible in the future iterations.
670 *
671 * In other words, the following guarantees that a descendant can't escape
672 * state updates of its ancestors.
673 *
92fb9748 674 * my_online(@cgrp)
574bd9f7
TH
675 * {
676 * Lock @cgrp->parent and @cgrp;
677 * Inherit state from @cgrp->parent;
678 * Unlock both.
679 * }
680 *
681 * my_update_state(@cgrp)
682 * {
683 * Lock @cgrp;
684 * Update @cgrp's state;
685 * Unlock @cgrp;
686 *
687 * cgroup_for_each_descendant_pre(@pos, @cgrp) {
688 * Lock @pos;
689 * Verify @pos is alive and inherit state from @pos->parent;
690 * Unlock @pos;
691 * }
692 * }
693 *
694 * As long as the inheriting step, including checking the parent state, is
695 * enclosed inside @pos locking, double-locking the parent isn't necessary
696 * while inheriting. The state update to the parent is guaranteed to be
697 * visible by walking order and, as long as inheriting operations to the
698 * same @pos are atomic to each other, multiple updates racing each other
699 * still result in the correct state. It's guaranateed that at least one
700 * inheritance happens for any cgroup after the latest update to its
701 * parent.
702 *
703 * If checking parent's state requires locking the parent, each inheriting
704 * iteration should lock and unlock both @pos->parent and @pos.
705 *
706 * Alternatively, a subsystem may choose to use a single global lock to
92fb9748 707 * synchronize ->css_online() and ->css_offline() against tree-walking
574bd9f7
TH
708 * operations.
709 */
710#define cgroup_for_each_descendant_pre(pos, cgroup) \
711 for (pos = cgroup_next_descendant_pre(NULL, (cgroup)); (pos); \
712 pos = cgroup_next_descendant_pre((pos), (cgroup)))
713
714struct cgroup *cgroup_next_descendant_post(struct cgroup *pos,
715 struct cgroup *cgroup);
716
717/**
718 * cgroup_for_each_descendant_post - post-order walk of a cgroup's descendants
719 * @pos: the cgroup * to use as the loop cursor
720 * @cgroup: cgroup whose descendants to walk
721 *
722 * Similar to cgroup_for_each_descendant_pre() but performs post-order
723 * traversal instead. Note that the walk visibility guarantee described in
724 * pre-order walk doesn't apply the same to post-order walks.
725 */
726#define cgroup_for_each_descendant_post(pos, cgroup) \
727 for (pos = cgroup_next_descendant_post(NULL, (cgroup)); (pos); \
728 pos = cgroup_next_descendant_post((pos), (cgroup)))
729
817929ec
PM
730/* A cgroup_iter should be treated as an opaque object */
731struct cgroup_iter {
732 struct list_head *cg_link;
733 struct list_head *task;
734};
735
d20a390a
PM
736/*
737 * To iterate across the tasks in a cgroup:
817929ec 738 *
b595076a 739 * 1) call cgroup_iter_start to initialize an iterator
817929ec
PM
740 *
741 * 2) call cgroup_iter_next() to retrieve member tasks until it
742 * returns NULL or until you want to end the iteration
743 *
744 * 3) call cgroup_iter_end() to destroy the iterator.
31a7df01 745 *
d20a390a
PM
746 * Or, call cgroup_scan_tasks() to iterate through every task in a
747 * cgroup - cgroup_scan_tasks() holds the css_set_lock when calling
748 * the test_task() callback, but not while calling the process_task()
749 * callback.
817929ec 750 */
ffd2d883
LZ
751void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it);
752struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
817929ec 753 struct cgroup_iter *it);
ffd2d883 754void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it);
31a7df01 755int cgroup_scan_tasks(struct cgroup_scanner *scan);
31583bb0 756int cgroup_attach_task_all(struct task_struct *from, struct task_struct *);
8cc99345 757int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from);
31583bb0 758
38460b48
KH
759/*
760 * CSS ID is ID for cgroup_subsys_state structs under subsys. This only works
761 * if cgroup_subsys.use_id == true. It can be used for looking up and scanning.
762 * CSS ID is assigned at cgroup allocation (create) automatically
763 * and removed when subsys calls free_css_id() function. This is because
764 * the lifetime of cgroup_subsys_state is subsys's matter.
765 *
766 * Looking up and scanning function should be called under rcu_read_lock().
6be96a5c 767 * Taking cgroup_mutex is not necessary for following calls.
38460b48
KH
768 * But the css returned by this routine can be "not populated yet" or "being
769 * destroyed". The caller should check css and cgroup's status.
770 */
771
772/*
773 * Typically Called at ->destroy(), or somewhere the subsys frees
774 * cgroup_subsys_state.
775 */
776void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css);
777
778/* Find a cgroup_subsys_state which has given ID */
779
780struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id);
781
782/*
783 * Get a cgroup whose id is greater than or equal to id under tree of root.
784 * Returning a cgroup_subsys_state or NULL.
785 */
786struct cgroup_subsys_state *css_get_next(struct cgroup_subsys *ss, int id,
787 struct cgroup_subsys_state *root, int *foundid);
788
789/* Returns true if root is ancestor of cg */
790bool css_is_ancestor(struct cgroup_subsys_state *cg,
0b7f569e 791 const struct cgroup_subsys_state *root);
38460b48
KH
792
793/* Get id and depth of css */
794unsigned short css_id(struct cgroup_subsys_state *css);
795unsigned short css_depth(struct cgroup_subsys_state *css);
e5d1367f 796struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id);
38460b48 797
ddbcc7e8
PM
798#else /* !CONFIG_CGROUPS */
799
800static inline int cgroup_init_early(void) { return 0; }
801static inline int cgroup_init(void) { return 0; }
b4f48b63 802static inline void cgroup_fork(struct task_struct *p) {}
817929ec 803static inline void cgroup_post_fork(struct task_struct *p) {}
b4f48b63 804static inline void cgroup_exit(struct task_struct *p, int callbacks) {}
ddbcc7e8
PM
805
806static inline void cgroup_lock(void) {}
807static inline void cgroup_unlock(void) {}
846c7bb0
BS
808static inline int cgroupstats_build(struct cgroupstats *stats,
809 struct dentry *dentry)
810{
811 return -EINVAL;
812}
ddbcc7e8 813
d7926ee3 814/* No cgroups - nothing to do */
31583bb0
MT
815static inline int cgroup_attach_task_all(struct task_struct *from,
816 struct task_struct *t)
817{
818 return 0;
819}
d7926ee3 820
ddbcc7e8
PM
821#endif /* !CONFIG_CGROUPS */
822
823#endif /* _LINUX_CGROUP_H */