]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - include/linux/cgroup.h
cgroup: use cgroup->self.refcnt for cgroup refcnting
[mirror_ubuntu-bionic-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>
cc31edce 17#include <linux/rwsem.h>
38460b48 18#include <linux/idr.h>
48ddbe19 19#include <linux/workqueue.h>
25a7e684 20#include <linux/fs.h>
d3daf28d 21#include <linux/percpu-refcount.h>
7da11279 22#include <linux/seq_file.h>
2bd59d48 23#include <linux/kernfs.h>
f8f22e53 24#include <linux/wait.h>
ddbcc7e8
PM
25
26#ifdef CONFIG_CGROUPS
27
3dd06ffa 28struct cgroup_root;
ddbcc7e8
PM
29struct cgroup_subsys;
30struct inode;
84eea842 31struct cgroup;
ddbcc7e8
PM
32
33extern int cgroup_init_early(void);
34extern int cgroup_init(void);
b4f48b63 35extern void cgroup_fork(struct task_struct *p);
817929ec 36extern void cgroup_post_fork(struct task_struct *p);
1ec41830 37extern void cgroup_exit(struct task_struct *p);
846c7bb0
BS
38extern int cgroupstats_build(struct cgroupstats *stats,
39 struct dentry *dentry);
ddbcc7e8 40
8d8b97ba 41extern int proc_cgroup_show(struct seq_file *, void *);
a424316c 42
3ed80a62 43/* define the enumeration of all cgroup subsystems */
073219e9 44#define SUBSYS(_x) _x ## _cgrp_id,
817929ec
PM
45enum cgroup_subsys_id {
46#include <linux/cgroup_subsys.h>
a6f00298 47 CGROUP_SUBSYS_COUNT,
817929ec
PM
48};
49#undef SUBSYS
50
ddbcc7e8
PM
51/* Per-subsystem/per-cgroup state maintained by the system. */
52struct cgroup_subsys_state {
72c97e54 53 /* the cgroup that this css is attached to */
ddbcc7e8
PM
54 struct cgroup *cgroup;
55
72c97e54
TH
56 /* the cgroup subsystem that this css is attached to */
57 struct cgroup_subsys *ss;
58
d3daf28d
TH
59 /* reference count - access via css_[try]get() and css_put() */
60 struct percpu_ref refcnt;
ddbcc7e8 61
0ae78e0b
TH
62 /* the parent css */
63 struct cgroup_subsys_state *parent;
64
15a4c835
TH
65 /*
66 * Subsys-unique ID. 0 is unused and root is always 1. The
67 * matching css can be looked up using css_from_id().
68 */
69 int id;
70
69dfa00c 71 unsigned int flags;
48ddbe19 72
0c21ead1
TH
73 /* percpu_ref killing and RCU release */
74 struct rcu_head rcu_head;
35ef10da 75 struct work_struct destroy_work;
ddbcc7e8
PM
76};
77
78/* bits in struct cgroup_subsys_state flags field */
79enum {
92fb9748 80 CSS_ONLINE = (1 << 1), /* between ->css_online() and ->css_offline() */
ddbcc7e8
PM
81};
82
5de0107e
TH
83/**
84 * css_get - obtain a reference on the specified css
85 * @css: target css
86 *
87 * The caller must already have a reference.
ddbcc7e8 88 */
ddbcc7e8
PM
89static inline void css_get(struct cgroup_subsys_state *css)
90{
9395a450 91 percpu_ref_get(&css->refcnt);
ddbcc7e8 92}
e7c5ec91 93
5de0107e 94/**
ec903c0c 95 * css_tryget_online - try to obtain a reference on the specified css if online
5de0107e
TH
96 * @css: target css
97 *
ec903c0c
TH
98 * Obtain a reference on @css if it's online. The caller naturally needs
99 * to ensure that @css is accessible but doesn't have to be holding a
5de0107e
TH
100 * reference on it - IOW, RCU protected access is good enough for this
101 * function. Returns %true if a reference count was successfully obtained;
102 * %false otherwise.
103 */
ec903c0c 104static inline bool css_tryget_online(struct cgroup_subsys_state *css)
e7c5ec91 105{
2070d50e 106 return percpu_ref_tryget_live(&css->refcnt);
e7c5ec91
PM
107}
108
5de0107e
TH
109/**
110 * css_put - put a css reference
111 * @css: target css
112 *
ec903c0c 113 * Put a reference obtained via css_get() and css_tryget_online().
5de0107e 114 */
ddbcc7e8
PM
115static inline void css_put(struct cgroup_subsys_state *css)
116{
9395a450 117 percpu_ref_put(&css->refcnt);
ddbcc7e8
PM
118}
119
3116f0e3
PM
120/* bits in struct cgroup flags field */
121enum {
122 /* Control Group is dead */
54766d4a 123 CGRP_DEAD,
d20a390a
PM
124 /*
125 * Control Group has previously had a child cgroup or a task,
126 * but no longer (only if CGRP_NOTIFY_ON_RELEASE is set)
127 */
3116f0e3
PM
128 CGRP_RELEASABLE,
129 /* Control Group requires release notifications to userspace */
130 CGRP_NOTIFY_ON_RELEASE,
97978e6d 131 /*
2260e7fc
TH
132 * Clone the parent's configuration when creating a new child
133 * cpuset cgroup. For historical reasons, this option can be
134 * specified at mount time and thus is implemented here.
97978e6d 135 */
2260e7fc 136 CGRP_CPUSET_CLONE_CHILDREN,
3116f0e3
PM
137};
138
ddbcc7e8 139struct cgroup {
9d800df1
TH
140 /* self css with NULL ->ss, points back to this cgroup */
141 struct cgroup_subsys_state self;
142
ddbcc7e8
PM
143 unsigned long flags; /* "unsigned long" so bitops work */
144
b414dc09
LZ
145 /*
146 * idr allocated in-hierarchy ID.
147 *
7d699ddb
TH
148 * ID 0 is not used, the ID of the root cgroup is always 1, and a
149 * new cgroup will be assigned with a smallest available ID.
0ab02ca8
LZ
150 *
151 * Allocating/Removing ID must be protected by cgroup_mutex.
b414dc09
LZ
152 */
153 int id;
0a950f65 154
842b597e
TH
155 /*
156 * If this cgroup contains any tasks, it contributes one to
157 * populated_cnt. All children with non-zero popuplated_cnt of
158 * their own contribute one. The count is zero iff there's no task
159 * in this cgroup or its subtree.
160 */
161 int populated_cnt;
162
ddbcc7e8
PM
163 /*
164 * We link our 'sibling' struct into our parent's 'children'.
165 * Our children link their 'sibling' into our 'children'.
166 */
167 struct list_head sibling; /* my parent's children */
168 struct list_head children; /* my children */
169
d20a390a 170 struct cgroup *parent; /* my parent */
2bd59d48 171 struct kernfs_node *kn; /* cgroup kernfs entry */
842b597e 172 struct kernfs_node *populated_kn; /* kn for "cgroup.subtree_populated" */
ddbcc7e8 173
53fa5261
TH
174 /*
175 * Monotonically increasing unique serial number which defines a
176 * uniform order among all cgroups. It's guaranteed that all
177 * ->children lists are in the ascending order of ->serial_nr.
178 * It's used to allow interrupting and resuming iterations.
179 */
180 u64 serial_nr;
181
f392e51c 182 /* the bitmask of subsystems enabled on the child cgroups */
69dfa00c 183 unsigned int child_subsys_mask;
94419627 184
ddbcc7e8 185 /* Private pointers for each registered subsystem */
73e80ed8 186 struct cgroup_subsys_state __rcu *subsys[CGROUP_SUBSYS_COUNT];
ddbcc7e8 187
3dd06ffa 188 struct cgroup_root *root;
817929ec
PM
189
190 /*
69d0206c
TH
191 * List of cgrp_cset_links pointing at css_sets with tasks in this
192 * cgroup. Protected by css_set_lock.
817929ec 193 */
69d0206c 194 struct list_head cset_links;
81a6a5cd 195
2d8f243a
TH
196 /*
197 * On the default hierarchy, a css_set for a cgroup with some
198 * susbsys disabled will point to css's which are associated with
199 * the closest ancestor which has the subsys enabled. The
200 * following lists all css_sets which point to this cgroup's css
201 * for the given subsystem.
202 */
203 struct list_head e_csets[CGROUP_SUBSYS_COUNT];
204
81a6a5cd
PM
205 /*
206 * Linked list running through all cgroups that can
207 * potentially be reaped by the release agent. Protected by
208 * release_list_lock
209 */
210 struct list_head release_list;
cc31edce 211
72a8cb30
BB
212 /*
213 * list of pidlists, up to two for each namespace (one for procs, one
214 * for tasks); created on demand.
215 */
216 struct list_head pidlists;
217 struct mutex pidlist_mutex;
a47295e6 218
f8f22e53
TH
219 /* used to wait for offlining of csses */
220 wait_queue_head_t offline_waitq;
817929ec
PM
221};
222
25a7e684
TH
223#define MAX_CGROUP_ROOT_NAMELEN 64
224
3dd06ffa 225/* cgroup_root->flags */
25a7e684 226enum {
873fe09e
TH
227 /*
228 * Unfortunately, cgroup core and various controllers are riddled
229 * with idiosyncrasies and pointless options. The following flag,
230 * when set, will force sane behavior - some options are forced on,
231 * others are disallowed, and some controllers will change their
232 * hierarchical or other behaviors.
233 *
234 * The set of behaviors affected by this flag are still being
235 * determined and developed and the mount option for this flag is
236 * prefixed with __DEVEL__. The prefix will be dropped once we
237 * reach the point where all behaviors are compatible with the
238 * planned unified hierarchy, which will automatically turn on this
239 * flag.
240 *
241 * The followings are the behaviors currently affected this flag.
242 *
d3ba07c3
TH
243 * - Mount options "noprefix", "xattr", "clone_children",
244 * "release_agent" and "name" are disallowed.
873fe09e
TH
245 *
246 * - When mounting an existing superblock, mount options should
247 * match.
248 *
249 * - Remount is disallowed.
250 *
0b0585c3
LT
251 * - rename(2) is disallowed.
252 *
f63674fd
TH
253 * - "tasks" is removed. Everything should be at process
254 * granularity. Use "cgroup.procs" instead.
f00baae7 255 *
afb2bc14
TH
256 * - "cgroup.procs" is not sorted. pids will be unique unless they
257 * got recycled inbetween reads.
258 *
f63674fd
TH
259 * - "release_agent" and "notify_on_release" are removed.
260 * Replacement notification mechanism will be implemented.
873fe09e 261 *
d3ba07c3 262 * - "cgroup.clone_children" is removed.
86bf4b68 263 *
842b597e
TH
264 * - "cgroup.subtree_populated" is available. Its value is 0 if
265 * the cgroup and its descendants contain no task; otherwise, 1.
266 * The file also generates kernfs notification which can be
267 * monitored through poll and [di]notify when the value of the
268 * file changes.
269 *
a2dd4247
TH
270 * - If mount is requested with sane_behavior but without any
271 * subsystem, the default unified hierarchy is mounted.
272 *
5c5cc623
LZ
273 * - cpuset: tasks will be kept in empty cpusets when hotplug happens
274 * and take masks of ancestors with non-empty cpus/mems, instead of
275 * being moved to an ancestor.
276 *
88fa523b
LZ
277 * - cpuset: a task can be moved into an empty cpuset, and again it
278 * takes masks of ancestors.
6db8e85c 279 *
f63674fd
TH
280 * - memcg: use_hierarchy is on by default and the cgroup file for
281 * the flag is not created.
f00baae7 282 *
9138125b 283 * - blkcg: blk-throttle becomes properly hierarchical.
873fe09e
TH
284 */
285 CGRP_ROOT_SANE_BEHAVIOR = (1 << 0),
286
25a7e684
TH
287 CGRP_ROOT_NOPREFIX = (1 << 1), /* mounted subsystems have no named prefix */
288 CGRP_ROOT_XATTR = (1 << 2), /* supports extended attributes */
0ce6cba3
TH
289
290 /* mount options live below bit 16 */
291 CGRP_ROOT_OPTION_MASK = (1 << 16) - 1,
25a7e684
TH
292};
293
294/*
3dd06ffa 295 * A cgroup_root represents the root of a cgroup hierarchy, and may be
2bd59d48 296 * associated with a kernfs_root to form an active hierarchy. This is
25a7e684
TH
297 * internal to cgroup core. Don't access directly from controllers.
298 */
3dd06ffa 299struct cgroup_root {
2bd59d48 300 struct kernfs_root *kf_root;
25a7e684 301
f392e51c 302 /* The bitmask of subsystems attached to this hierarchy */
69dfa00c 303 unsigned int subsys_mask;
f392e51c 304
25a7e684
TH
305 /* Unique id for this hierarchy. */
306 int hierarchy_id;
307
776f02fa 308 /* The root cgroup. Root is destroyed on its release. */
3dd06ffa 309 struct cgroup cgrp;
25a7e684 310
3c9c825b
TH
311 /* Number of cgroups in the hierarchy, used only for /proc/cgroups */
312 atomic_t nr_cgrps;
25a7e684
TH
313
314 /* A list running through the active hierarchies */
315 struct list_head root_list;
316
25a7e684 317 /* Hierarchy-specific flags */
69dfa00c 318 unsigned int flags;
25a7e684
TH
319
320 /* IDs for cgroups in this hierarchy */
4e96ee8e 321 struct idr cgroup_idr;
25a7e684
TH
322
323 /* The path to use for release notifications. */
324 char release_agent_path[PATH_MAX];
325
326 /* The name for this hierarchy - may be empty */
327 char name[MAX_CGROUP_ROOT_NAMELEN];
328};
329
d20a390a
PM
330/*
331 * A css_set is a structure holding pointers to a set of
817929ec
PM
332 * cgroup_subsys_state objects. This saves space in the task struct
333 * object and speeds up fork()/exit(), since a single inc/dec and a
d20a390a
PM
334 * list_add()/del() can bump the reference count on the entire cgroup
335 * set for a task.
817929ec
PM
336 */
337
338struct css_set {
339
340 /* Reference count */
146aa1bd 341 atomic_t refcount;
817929ec 342
472b1053
LZ
343 /*
344 * List running through all cgroup groups in the same hash
345 * slot. Protected by css_set_lock
346 */
347 struct hlist_node hlist;
348
817929ec 349 /*
c7561128
TH
350 * Lists running through all tasks using this cgroup group.
351 * mg_tasks lists tasks which belong to this cset but are in the
352 * process of being migrated out or in. Protected by
353 * css_set_rwsem, but, during migration, once tasks are moved to
354 * mg_tasks, it can be read safely while holding cgroup_mutex.
817929ec
PM
355 */
356 struct list_head tasks;
c7561128 357 struct list_head mg_tasks;
817929ec
PM
358
359 /*
69d0206c
TH
360 * List of cgrp_cset_links pointing at cgroups referenced from this
361 * css_set. Protected by css_set_lock.
817929ec 362 */
69d0206c 363 struct list_head cgrp_links;
817929ec 364
6803c006
TH
365 /* the default cgroup associated with this css_set */
366 struct cgroup *dfl_cgrp;
367
817929ec 368 /*
3ed80a62
TH
369 * Set of subsystem states, one for each subsystem. This array is
370 * immutable after creation apart from the init_css_set during
371 * subsystem registration (at boot time).
817929ec
PM
372 */
373 struct cgroup_subsys_state *subsys[CGROUP_SUBSYS_COUNT];
c378369d 374
b3dc094e
TH
375 /*
376 * List of csets participating in the on-going migration either as
377 * source or destination. Protected by cgroup_mutex.
378 */
1958d2d5 379 struct list_head mg_preload_node;
b3dc094e
TH
380 struct list_head mg_node;
381
382 /*
383 * If this cset is acting as the source of migration the following
384 * two fields are set. mg_src_cgrp is the source cgroup of the
385 * on-going migration and mg_dst_cset is the destination cset the
386 * target tasks on this cset should be migrated to. Protected by
387 * cgroup_mutex.
388 */
389 struct cgroup *mg_src_cgrp;
390 struct css_set *mg_dst_cset;
391
2d8f243a
TH
392 /*
393 * On the default hierarhcy, ->subsys[ssid] may point to a css
394 * attached to an ancestor instead of the cgroup this css_set is
395 * associated with. The following node is anchored at
396 * ->subsys[ssid]->cgroup->e_csets[ssid] and provides a way to
397 * iterate through all css's attached to a given cgroup.
398 */
399 struct list_head e_cset_node[CGROUP_SUBSYS_COUNT];
400
c378369d
BB
401 /* For RCU-protected deletion */
402 struct rcu_head rcu_head;
ddbcc7e8
PM
403};
404
d20a390a
PM
405/*
406 * struct cftype: handler definitions for cgroup control files
ddbcc7e8
PM
407 *
408 * When reading/writing to a file:
a043e3b2 409 * - the cgroup to use is file->f_dentry->d_parent->d_fsdata
ddbcc7e8
PM
410 * - the 'cftype' of the file is file->f_dentry->d_fsdata
411 */
412
8e3f6541 413/* cftype->flags */
02c402d9 414enum {
6f4b7e63
LZ
415 CFTYPE_ONLY_ON_ROOT = (1 << 0), /* only create on root cgrp */
416 CFTYPE_NOT_ON_ROOT = (1 << 1), /* don't create on root cgrp */
02c402d9 417 CFTYPE_INSANE = (1 << 2), /* don't create if sane_behavior */
9fa4db33 418 CFTYPE_NO_PREFIX = (1 << 3), /* (DON'T USE FOR NEW FILES) no subsys prefix */
8cbbf2c9 419 CFTYPE_ONLY_ON_DFL = (1 << 4), /* only on default hierarchy */
02c402d9 420};
8e3f6541
TH
421
422#define MAX_CFTYPE_NAME 64
423
ddbcc7e8 424struct cftype {
d20a390a
PM
425 /*
426 * By convention, the name should begin with the name of the
8e3f6541
TH
427 * subsystem, followed by a period. Zero length string indicates
428 * end of cftype array.
d20a390a 429 */
ddbcc7e8
PM
430 char name[MAX_CFTYPE_NAME];
431 int private;
099fca32
LZ
432 /*
433 * If not 0, file mode is set to this value, otherwise it will
434 * be figured out automatically
435 */
a5e7ed32 436 umode_t mode;
db3b1497
PM
437
438 /*
5f469907 439 * The maximum length of string, excluding trailing nul, that can
451af504 440 * be passed to write. If < PAGE_SIZE-1, PAGE_SIZE-1 is assumed.
db3b1497
PM
441 */
442 size_t max_write_len;
443
8e3f6541
TH
444 /* CFTYPE_* flags */
445 unsigned int flags;
446
2bb566cb 447 /*
0adb0704
TH
448 * Fields used for internal bookkeeping. Initialized automatically
449 * during registration.
2bb566cb 450 */
0adb0704
TH
451 struct cgroup_subsys *ss; /* NULL for cgroup core files */
452 struct list_head node; /* anchored at ss->cfts */
2bd59d48
TH
453 struct kernfs_ops *kf_ops;
454
ddbcc7e8 455 /*
f4c753b7 456 * read_u64() is a shortcut for the common case of returning a
ddbcc7e8
PM
457 * single integer. Use it in place of read()
458 */
182446d0 459 u64 (*read_u64)(struct cgroup_subsys_state *css, struct cftype *cft);
e73d2c61
PM
460 /*
461 * read_s64() is a signed version of read_u64()
462 */
182446d0 463 s64 (*read_s64)(struct cgroup_subsys_state *css, struct cftype *cft);
2da8ca82
TH
464
465 /* generic seq_file read interface */
466 int (*seq_show)(struct seq_file *sf, void *v);
91796569 467
6612f05b
TH
468 /* optional ops, implement all or none */
469 void *(*seq_start)(struct seq_file *sf, loff_t *ppos);
470 void *(*seq_next)(struct seq_file *sf, void *v, loff_t *ppos);
471 void (*seq_stop)(struct seq_file *sf, void *v);
472
355e0c48 473 /*
f4c753b7 474 * write_u64() is a shortcut for the common case of accepting
355e0c48
PM
475 * a single integer (as parsed by simple_strtoull) from
476 * userspace. Use in place of write(); return 0 or error.
477 */
182446d0
TH
478 int (*write_u64)(struct cgroup_subsys_state *css, struct cftype *cft,
479 u64 val);
e73d2c61
PM
480 /*
481 * write_s64() is a signed version of write_u64()
482 */
182446d0
TH
483 int (*write_s64)(struct cgroup_subsys_state *css, struct cftype *cft,
484 s64 val);
355e0c48 485
b4168640
TH
486 /*
487 * write() is the generic write callback which maps directly to
488 * kernfs write operation and overrides all other operations.
489 * Maximum write size is determined by ->max_write_len. Use
490 * of_css/cft() to access the associated css and cft.
491 */
492 ssize_t (*write)(struct kernfs_open_file *of,
493 char *buf, size_t nbytes, loff_t off);
494
2bd59d48
TH
495#ifdef CONFIG_DEBUG_LOCK_ALLOC
496 struct lock_class_key lockdep_key;
497#endif
ddbcc7e8
PM
498};
499
a2dd4247 500extern struct cgroup_root cgrp_dfl_root;
5024ae29 501extern struct css_set init_css_set;
a2dd4247
TH
502
503static inline bool cgroup_on_dfl(const struct cgroup *cgrp)
504{
505 return cgrp->root == &cgrp_dfl_root;
506}
507
873fe09e
TH
508/*
509 * See the comment above CGRP_ROOT_SANE_BEHAVIOR for details. This
510 * function can be called as long as @cgrp is accessible.
511 */
512static inline bool cgroup_sane_behavior(const struct cgroup *cgrp)
513{
514 return cgrp->root->flags & CGRP_ROOT_SANE_BEHAVIOR;
515}
516
07bc356e
TH
517/* no synchronization, the result can only be used as a hint */
518static inline bool cgroup_has_tasks(struct cgroup *cgrp)
519{
520 return !list_empty(&cgrp->cset_links);
521}
522
b1664924
TH
523/* returns ino associated with a cgroup, 0 indicates unmounted root */
524static inline ino_t cgroup_ino(struct cgroup *cgrp)
525{
2bd59d48
TH
526 if (cgrp->kn)
527 return cgrp->kn->ino;
b1664924
TH
528 else
529 return 0;
530}
531
b4168640
TH
532/* cft/css accessors for cftype->write() operation */
533static inline struct cftype *of_cft(struct kernfs_open_file *of)
7da11279 534{
2bd59d48 535 return of->kn->priv;
7da11279
TH
536}
537
b4168640
TH
538struct cgroup_subsys_state *of_css(struct kernfs_open_file *of);
539
540/* cft/css accessors for cftype->seq_*() operations */
541static inline struct cftype *seq_cft(struct seq_file *seq)
542{
543 return of_cft(seq->private);
544}
545
546static inline struct cgroup_subsys_state *seq_css(struct seq_file *seq)
547{
548 return of_css(seq->private);
549}
59f5296b 550
e61734c5
TH
551/*
552 * Name / path handling functions. All are thin wrappers around the kernfs
553 * counterparts and can be called under any context.
554 */
555
556static inline int cgroup_name(struct cgroup *cgrp, char *buf, size_t buflen)
557{
fdce6bf8 558 return kernfs_name(cgrp->kn, buf, buflen);
e61734c5
TH
559}
560
561static inline char * __must_check cgroup_path(struct cgroup *cgrp, char *buf,
562 size_t buflen)
563{
fdce6bf8 564 return kernfs_path(cgrp->kn, buf, buflen);
e61734c5
TH
565}
566
567static inline void pr_cont_cgroup_name(struct cgroup *cgrp)
568{
fdce6bf8 569 pr_cont_kernfs_name(cgrp->kn);
e61734c5
TH
570}
571
572static inline void pr_cont_cgroup_path(struct cgroup *cgrp)
573{
fdce6bf8 574 pr_cont_kernfs_path(cgrp->kn);
e61734c5
TH
575}
576
577char *task_cgroup_path(struct task_struct *task, char *buf, size_t buflen);
578
03b1cde6 579int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts);
2bb566cb 580int cgroup_rm_cftypes(struct cftype *cfts);
8e3f6541 581
78574cf9 582bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor);
ddbcc7e8 583
2f7ee569
TH
584/*
585 * Control Group taskset, used to pass around set of tasks to cgroup_subsys
586 * methods.
587 */
588struct cgroup_taskset;
589struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset);
590struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset);
2f7ee569
TH
591
592/**
593 * cgroup_taskset_for_each - iterate cgroup_taskset
594 * @task: the loop cursor
2f7ee569
TH
595 * @tset: taskset to iterate
596 */
924f0d9a 597#define cgroup_taskset_for_each(task, tset) \
2f7ee569 598 for ((task) = cgroup_taskset_first((tset)); (task); \
924f0d9a 599 (task) = cgroup_taskset_next((tset)))
2f7ee569 600
21acb9ca
TLSC
601/*
602 * Control Group subsystem type.
603 * See Documentation/cgroups/cgroups.txt for details
604 */
ddbcc7e8
PM
605
606struct cgroup_subsys {
eb95419b
TH
607 struct cgroup_subsys_state *(*css_alloc)(struct cgroup_subsys_state *parent_css);
608 int (*css_online)(struct cgroup_subsys_state *css);
609 void (*css_offline)(struct cgroup_subsys_state *css);
610 void (*css_free)(struct cgroup_subsys_state *css);
611
612 int (*can_attach)(struct cgroup_subsys_state *css,
613 struct cgroup_taskset *tset);
614 void (*cancel_attach)(struct cgroup_subsys_state *css,
615 struct cgroup_taskset *tset);
616 void (*attach)(struct cgroup_subsys_state *css,
617 struct cgroup_taskset *tset);
761b3ef5 618 void (*fork)(struct task_struct *task);
eb95419b
TH
619 void (*exit)(struct cgroup_subsys_state *css,
620 struct cgroup_subsys_state *old_css,
761b3ef5 621 struct task_struct *task);
eb95419b 622 void (*bind)(struct cgroup_subsys_state *root_css);
e5991371 623
8bab8dde 624 int disabled;
ddbcc7e8 625 int early_init;
48ddbe19 626
8c7f6edb
TH
627 /*
628 * If %false, this subsystem is properly hierarchical -
629 * configuration, resource accounting and restriction on a parent
630 * cgroup cover those of its children. If %true, hierarchy support
631 * is broken in some ways - some subsystems ignore hierarchy
632 * completely while others are only implemented half-way.
633 *
634 * It's now disallowed to create nested cgroups if the subsystem is
635 * broken and cgroup core will emit a warning message on such
636 * cases. Eventually, all subsystems will be made properly
637 * hierarchical and this will go away.
638 */
639 bool broken_hierarchy;
640 bool warned_broken_hierarchy;
641
073219e9 642 /* the following two fields are initialized automtically during boot */
aec25020 643 int id;
ddbcc7e8
PM
644#define MAX_CGROUP_TYPE_NAMELEN 32
645 const char *name;
646
b85d2040 647 /* link to parent, protected by cgroup_lock() */
3dd06ffa 648 struct cgroup_root *root;
e6a1105b 649
15a4c835
TH
650 /* idr for css->id */
651 struct idr css_idr;
652
0adb0704
TH
653 /*
654 * List of cftypes. Each entry is the first entry of an array
655 * terminated by zero length name.
656 */
657 struct list_head cfts;
8e3f6541 658
de00ffa5 659 /* base cftypes, automatically registered with subsys itself */
8e3f6541 660 struct cftype *base_cftypes;
ddbcc7e8
PM
661};
662
073219e9 663#define SUBSYS(_x) extern struct cgroup_subsys _x ## _cgrp_subsys;
ddbcc7e8
PM
664#include <linux/cgroup_subsys.h>
665#undef SUBSYS
666
63876986
TH
667/**
668 * css_parent - find the parent css
669 * @css: the target cgroup_subsys_state
670 *
671 * Return the parent css of @css. This function is guaranteed to return
672 * non-NULL parent as long as @css isn't the root.
673 */
674static inline
675struct cgroup_subsys_state *css_parent(struct cgroup_subsys_state *css)
676{
0ae78e0b 677 return css->parent;
63876986
TH
678}
679
14611e51
TH
680/**
681 * task_css_set_check - obtain a task's css_set with extra access conditions
682 * @task: the task to obtain css_set for
683 * @__c: extra condition expression to be passed to rcu_dereference_check()
684 *
685 * A task's css_set is RCU protected, initialized and exited while holding
686 * task_lock(), and can only be modified while holding both cgroup_mutex
687 * and task_lock() while the task is alive. This macro verifies that the
688 * caller is inside proper critical section and returns @task's css_set.
689 *
690 * The caller can also specify additional allowed conditions via @__c, such
691 * as locks used during the cgroup_subsys::attach() methods.
dc61b1d6 692 */
2219449a
TH
693#ifdef CONFIG_PROVE_RCU
694extern struct mutex cgroup_mutex;
0e1d768f 695extern struct rw_semaphore css_set_rwsem;
14611e51
TH
696#define task_css_set_check(task, __c) \
697 rcu_dereference_check((task)->cgroups, \
0e1d768f
TH
698 lockdep_is_held(&cgroup_mutex) || \
699 lockdep_is_held(&css_set_rwsem) || \
700 ((task)->flags & PF_EXITING) || (__c))
2219449a 701#else
14611e51
TH
702#define task_css_set_check(task, __c) \
703 rcu_dereference((task)->cgroups)
2219449a 704#endif
dc61b1d6 705
14611e51 706/**
8af01f56 707 * task_css_check - obtain css for (task, subsys) w/ extra access conds
14611e51
TH
708 * @task: the target task
709 * @subsys_id: the target subsystem ID
710 * @__c: extra condition expression to be passed to rcu_dereference_check()
711 *
712 * Return the cgroup_subsys_state for the (@task, @subsys_id) pair. The
713 * synchronization rules are the same as task_css_set_check().
714 */
8af01f56 715#define task_css_check(task, subsys_id, __c) \
14611e51
TH
716 task_css_set_check((task), (__c))->subsys[(subsys_id)]
717
718/**
719 * task_css_set - obtain a task's css_set
720 * @task: the task to obtain css_set for
721 *
722 * See task_css_set_check().
723 */
724static inline struct css_set *task_css_set(struct task_struct *task)
725{
726 return task_css_set_check(task, false);
727}
728
729/**
8af01f56 730 * task_css - obtain css for (task, subsys)
14611e51
TH
731 * @task: the target task
732 * @subsys_id: the target subsystem ID
733 *
8af01f56 734 * See task_css_check().
14611e51 735 */
8af01f56
TH
736static inline struct cgroup_subsys_state *task_css(struct task_struct *task,
737 int subsys_id)
ddbcc7e8 738{
8af01f56 739 return task_css_check(task, subsys_id, false);
ddbcc7e8
PM
740}
741
5024ae29
TH
742/**
743 * task_css_is_root - test whether a task belongs to the root css
744 * @task: the target task
745 * @subsys_id: the target subsystem ID
746 *
747 * Test whether @task belongs to the root css on the specified subsystem.
748 * May be invoked in any context.
749 */
750static inline bool task_css_is_root(struct task_struct *task, int subsys_id)
751{
752 return task_css_check(task, subsys_id, true) ==
753 init_css_set.subsys[subsys_id];
754}
755
8af01f56
TH
756static inline struct cgroup *task_cgroup(struct task_struct *task,
757 int subsys_id)
ddbcc7e8 758{
8af01f56 759 return task_css(task, subsys_id)->cgroup;
ddbcc7e8
PM
760}
761
492eb21b
TH
762struct cgroup_subsys_state *css_next_child(struct cgroup_subsys_state *pos,
763 struct cgroup_subsys_state *parent);
53fa5261 764
1cb650b9
LZ
765struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss);
766
574bd9f7 767/**
492eb21b
TH
768 * css_for_each_child - iterate through children of a css
769 * @pos: the css * to use as the loop cursor
770 * @parent: css whose children to walk
574bd9f7 771 *
492eb21b
TH
772 * Walk @parent's children. Must be called under rcu_read_lock(). A child
773 * css which hasn't finished ->css_online() or already has finished
92fb9748 774 * ->css_offline() may show up during traversal and it's each subsystem's
574bd9f7
TH
775 * responsibility to verify that each @pos is alive.
776 *
92fb9748 777 * If a subsystem synchronizes against the parent in its ->css_online() and
492eb21b 778 * before starting iterating, a css which finished ->css_online() is
92fb9748 779 * guaranteed to be visible in the future iterations.
75501a6d
TH
780 *
781 * It is allowed to temporarily drop RCU read lock during iteration. The
782 * caller is responsible for ensuring that @pos remains accessible until
783 * the start of the next iteration by, for example, bumping the css refcnt.
574bd9f7 784 */
492eb21b
TH
785#define css_for_each_child(pos, parent) \
786 for ((pos) = css_next_child(NULL, (parent)); (pos); \
787 (pos) = css_next_child((pos), (parent)))
574bd9f7 788
492eb21b
TH
789struct cgroup_subsys_state *
790css_next_descendant_pre(struct cgroup_subsys_state *pos,
791 struct cgroup_subsys_state *css);
792
793struct cgroup_subsys_state *
794css_rightmost_descendant(struct cgroup_subsys_state *pos);
574bd9f7
TH
795
796/**
492eb21b
TH
797 * css_for_each_descendant_pre - pre-order walk of a css's descendants
798 * @pos: the css * to use as the loop cursor
799 * @root: css whose descendants to walk
574bd9f7 800 *
bd8815a6
TH
801 * Walk @root's descendants. @root is included in the iteration and the
802 * first node to be visited. Must be called under rcu_read_lock(). A
492eb21b 803 * descendant css which hasn't finished ->css_online() or already has
92fb9748 804 * finished ->css_offline() may show up during traversal and it's each
574bd9f7
TH
805 * subsystem's responsibility to verify that each @pos is alive.
806 *
92fb9748
TH
807 * If a subsystem synchronizes against the parent in its ->css_online() and
808 * before starting iterating, and synchronizes against @pos on each
492eb21b 809 * iteration, any descendant css which finished ->css_online() is
574bd9f7
TH
810 * guaranteed to be visible in the future iterations.
811 *
812 * In other words, the following guarantees that a descendant can't escape
813 * state updates of its ancestors.
814 *
492eb21b 815 * my_online(@css)
574bd9f7 816 * {
492eb21b
TH
817 * Lock @css's parent and @css;
818 * Inherit state from the parent;
574bd9f7
TH
819 * Unlock both.
820 * }
821 *
492eb21b 822 * my_update_state(@css)
574bd9f7 823 * {
492eb21b 824 * css_for_each_descendant_pre(@pos, @css) {
574bd9f7 825 * Lock @pos;
bd8815a6
TH
826 * if (@pos == @css)
827 * Update @css's state;
828 * else
829 * Verify @pos is alive and inherit state from its parent;
574bd9f7
TH
830 * Unlock @pos;
831 * }
832 * }
833 *
834 * As long as the inheriting step, including checking the parent state, is
835 * enclosed inside @pos locking, double-locking the parent isn't necessary
836 * while inheriting. The state update to the parent is guaranteed to be
837 * visible by walking order and, as long as inheriting operations to the
838 * same @pos are atomic to each other, multiple updates racing each other
839 * still result in the correct state. It's guaranateed that at least one
492eb21b 840 * inheritance happens for any css after the latest update to its parent.
574bd9f7
TH
841 *
842 * If checking parent's state requires locking the parent, each inheriting
843 * iteration should lock and unlock both @pos->parent and @pos.
844 *
845 * Alternatively, a subsystem may choose to use a single global lock to
92fb9748 846 * synchronize ->css_online() and ->css_offline() against tree-walking
574bd9f7 847 * operations.
75501a6d
TH
848 *
849 * It is allowed to temporarily drop RCU read lock during iteration. The
850 * caller is responsible for ensuring that @pos remains accessible until
851 * the start of the next iteration by, for example, bumping the css refcnt.
574bd9f7 852 */
492eb21b
TH
853#define css_for_each_descendant_pre(pos, css) \
854 for ((pos) = css_next_descendant_pre(NULL, (css)); (pos); \
855 (pos) = css_next_descendant_pre((pos), (css)))
574bd9f7 856
492eb21b
TH
857struct cgroup_subsys_state *
858css_next_descendant_post(struct cgroup_subsys_state *pos,
859 struct cgroup_subsys_state *css);
574bd9f7
TH
860
861/**
492eb21b
TH
862 * css_for_each_descendant_post - post-order walk of a css's descendants
863 * @pos: the css * to use as the loop cursor
864 * @css: css whose descendants to walk
574bd9f7 865 *
492eb21b 866 * Similar to css_for_each_descendant_pre() but performs post-order
bd8815a6
TH
867 * traversal instead. @root is included in the iteration and the last
868 * node to be visited. Note that the walk visibility guarantee described
869 * in pre-order walk doesn't apply the same to post-order walks.
574bd9f7 870 */
492eb21b
TH
871#define css_for_each_descendant_post(pos, css) \
872 for ((pos) = css_next_descendant_post(NULL, (css)); (pos); \
873 (pos) = css_next_descendant_post((pos), (css)))
574bd9f7 874
72ec7029
TH
875/* A css_task_iter should be treated as an opaque object */
876struct css_task_iter {
3ebb2b6e
TH
877 struct cgroup_subsys *ss;
878
0f0a2b4f
TH
879 struct list_head *cset_pos;
880 struct list_head *cset_head;
881
882 struct list_head *task_pos;
883 struct list_head *tasks_head;
884 struct list_head *mg_tasks_head;
817929ec
PM
885};
886
72ec7029
TH
887void css_task_iter_start(struct cgroup_subsys_state *css,
888 struct css_task_iter *it);
889struct task_struct *css_task_iter_next(struct css_task_iter *it);
890void css_task_iter_end(struct css_task_iter *it);
e535837b 891
31583bb0 892int cgroup_attach_task_all(struct task_struct *from, struct task_struct *);
8cc99345 893int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from);
31583bb0 894
ec903c0c
TH
895struct cgroup_subsys_state *css_tryget_online_from_dir(struct dentry *dentry,
896 struct cgroup_subsys *ss);
38460b48 897
ddbcc7e8
PM
898#else /* !CONFIG_CGROUPS */
899
900static inline int cgroup_init_early(void) { return 0; }
901static inline int cgroup_init(void) { return 0; }
b4f48b63 902static inline void cgroup_fork(struct task_struct *p) {}
817929ec 903static inline void cgroup_post_fork(struct task_struct *p) {}
1ec41830 904static inline void cgroup_exit(struct task_struct *p) {}
ddbcc7e8 905
846c7bb0
BS
906static inline int cgroupstats_build(struct cgroupstats *stats,
907 struct dentry *dentry)
908{
909 return -EINVAL;
910}
ddbcc7e8 911
d7926ee3 912/* No cgroups - nothing to do */
31583bb0
MT
913static inline int cgroup_attach_task_all(struct task_struct *from,
914 struct task_struct *t)
915{
916 return 0;
917}
d7926ee3 918
ddbcc7e8
PM
919#endif /* !CONFIG_CGROUPS */
920
921#endif /* _LINUX_CGROUP_H */