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