cgroup: replace cgroup->css_kill_cnt with ->nr_css
[pandora-kernel.git] / 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/prio_heap.h>
18 #include <linux/rwsem.h>
19 #include <linux/idr.h>
20 #include <linux/workqueue.h>
21 #include <linux/xattr.h>
22 #include <linux/fs.h>
23 #include <linux/percpu-refcount.h>
24
25 #ifdef CONFIG_CGROUPS
26
27 struct cgroupfs_root;
28 struct cgroup_subsys;
29 struct inode;
30 struct cgroup;
31 struct css_id;
32 struct eventfd_ctx;
33
34 extern int cgroup_init_early(void);
35 extern int cgroup_init(void);
36 extern void cgroup_fork(struct task_struct *p);
37 extern void cgroup_post_fork(struct task_struct *p);
38 extern void cgroup_exit(struct task_struct *p, int run_callbacks);
39 extern int cgroupstats_build(struct cgroupstats *stats,
40                                 struct dentry *dentry);
41 extern int cgroup_load_subsys(struct cgroup_subsys *ss);
42 extern void cgroup_unload_subsys(struct cgroup_subsys *ss);
43
44 extern int proc_cgroup_show(struct seq_file *, void *);
45
46 /*
47  * Define the enumeration of all cgroup subsystems.
48  *
49  * We define ids for builtin subsystems and then modular ones.
50  */
51 #define SUBSYS(_x) _x ## _subsys_id,
52 enum cgroup_subsys_id {
53 #define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option)
54 #include <linux/cgroup_subsys.h>
55 #undef IS_SUBSYS_ENABLED
56         CGROUP_BUILTIN_SUBSYS_COUNT,
57
58         __CGROUP_SUBSYS_TEMP_PLACEHOLDER = CGROUP_BUILTIN_SUBSYS_COUNT - 1,
59
60 #define IS_SUBSYS_ENABLED(option) IS_MODULE(option)
61 #include <linux/cgroup_subsys.h>
62 #undef IS_SUBSYS_ENABLED
63         CGROUP_SUBSYS_COUNT,
64 };
65 #undef SUBSYS
66
67 /* Per-subsystem/per-cgroup state maintained by the system. */
68 struct cgroup_subsys_state {
69         /* the cgroup that this css is attached to */
70         struct cgroup *cgroup;
71
72         /* the cgroup subsystem that this css is attached to */
73         struct cgroup_subsys *ss;
74
75         /* reference count - access via css_[try]get() and css_put() */
76         struct percpu_ref refcnt;
77
78         /* the parent css */
79         struct cgroup_subsys_state *parent;
80
81         unsigned long flags;
82         /* ID for this css, if possible */
83         struct css_id __rcu *id;
84
85         /* Used to put @cgroup->dentry on the last css_put() */
86         struct work_struct destroy_work;
87 };
88
89 /* bits in struct cgroup_subsys_state flags field */
90 enum {
91         CSS_ROOT        = (1 << 0), /* this CSS is the root of the subsystem */
92         CSS_ONLINE      = (1 << 1), /* between ->css_online() and ->css_offline() */
93 };
94
95 /**
96  * css_get - obtain a reference on the specified css
97  * @css: target css
98  *
99  * The caller must already have a reference.
100  */
101 static inline void css_get(struct cgroup_subsys_state *css)
102 {
103         /* We don't need to reference count the root state */
104         if (!(css->flags & CSS_ROOT))
105                 percpu_ref_get(&css->refcnt);
106 }
107
108 /**
109  * css_tryget - try to obtain a reference on the specified css
110  * @css: target css
111  *
112  * Obtain a reference on @css if it's alive.  The caller naturally needs to
113  * ensure that @css is accessible but doesn't have to be holding a
114  * reference on it - IOW, RCU protected access is good enough for this
115  * function.  Returns %true if a reference count was successfully obtained;
116  * %false otherwise.
117  */
118 static inline bool css_tryget(struct cgroup_subsys_state *css)
119 {
120         if (css->flags & CSS_ROOT)
121                 return true;
122         return percpu_ref_tryget(&css->refcnt);
123 }
124
125 /**
126  * css_put - put a css reference
127  * @css: target css
128  *
129  * Put a reference obtained via css_get() and css_tryget().
130  */
131 static inline void css_put(struct cgroup_subsys_state *css)
132 {
133         if (!(css->flags & CSS_ROOT))
134                 percpu_ref_put(&css->refcnt);
135 }
136
137 /* bits in struct cgroup flags field */
138 enum {
139         /* Control Group is dead */
140         CGRP_DEAD,
141         /*
142          * Control Group has previously had a child cgroup or a task,
143          * but no longer (only if CGRP_NOTIFY_ON_RELEASE is set)
144          */
145         CGRP_RELEASABLE,
146         /* Control Group requires release notifications to userspace */
147         CGRP_NOTIFY_ON_RELEASE,
148         /*
149          * Clone the parent's configuration when creating a new child
150          * cpuset cgroup.  For historical reasons, this option can be
151          * specified at mount time and thus is implemented here.
152          */
153         CGRP_CPUSET_CLONE_CHILDREN,
154         /* see the comment above CGRP_ROOT_SANE_BEHAVIOR for details */
155         CGRP_SANE_BEHAVIOR,
156 };
157
158 struct cgroup_name {
159         struct rcu_head rcu_head;
160         char name[];
161 };
162
163 struct cgroup {
164         unsigned long flags;            /* "unsigned long" so bitops work */
165
166         /*
167          * idr allocated in-hierarchy ID.
168          *
169          * The ID of the root cgroup is always 0, and a new cgroup
170          * will be assigned with a smallest available ID.
171          */
172         int id;
173
174         /* the number of attached css's */
175         int nr_css;
176
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 */
183         struct list_head files;         /* my files */
184
185         struct cgroup *parent;          /* my parent */
186         struct dentry *dentry;          /* cgroup fs entry, RCU protected */
187
188         /*
189          * Monotonically increasing unique serial number which defines a
190          * uniform order among all cgroups.  It's guaranteed that all
191          * ->children lists are in the ascending order of ->serial_nr.
192          * It's used to allow interrupting and resuming iterations.
193          */
194         u64 serial_nr;
195
196         /*
197          * This is a copy of dentry->d_name, and it's needed because
198          * we can't use dentry->d_name in cgroup_path().
199          *
200          * You must acquire rcu_read_lock() to access cgrp->name, and
201          * the only place that can change it is rename(), which is
202          * protected by parent dir's i_mutex.
203          *
204          * Normally you should use cgroup_name() wrapper rather than
205          * access it directly.
206          */
207         struct cgroup_name __rcu *name;
208
209         /* Private pointers for each registered subsystem */
210         struct cgroup_subsys_state __rcu *subsys[CGROUP_SUBSYS_COUNT];
211
212         struct cgroupfs_root *root;
213
214         /*
215          * List of cgrp_cset_links pointing at css_sets with tasks in this
216          * cgroup.  Protected by css_set_lock.
217          */
218         struct list_head cset_links;
219
220         /*
221          * Linked list running through all cgroups that can
222          * potentially be reaped by the release agent. Protected by
223          * release_list_lock
224          */
225         struct list_head release_list;
226
227         /*
228          * list of pidlists, up to two for each namespace (one for procs, one
229          * for tasks); created on demand.
230          */
231         struct list_head pidlists;
232         struct mutex pidlist_mutex;
233
234         /* dummy css with NULL ->ss, points back to this cgroup */
235         struct cgroup_subsys_state dummy_css;
236
237         /* For css percpu_ref killing and RCU-protected deletion */
238         struct rcu_head rcu_head;
239         struct work_struct destroy_work;
240
241         /* List of events which userspace want to receive */
242         struct list_head event_list;
243         spinlock_t event_list_lock;
244
245         /* directory xattrs */
246         struct simple_xattrs xattrs;
247 };
248
249 #define MAX_CGROUP_ROOT_NAMELEN 64
250
251 /* cgroupfs_root->flags */
252 enum {
253         /*
254          * Unfortunately, cgroup core and various controllers are riddled
255          * with idiosyncrasies and pointless options.  The following flag,
256          * when set, will force sane behavior - some options are forced on,
257          * others are disallowed, and some controllers will change their
258          * hierarchical or other behaviors.
259          *
260          * The set of behaviors affected by this flag are still being
261          * determined and developed and the mount option for this flag is
262          * prefixed with __DEVEL__.  The prefix will be dropped once we
263          * reach the point where all behaviors are compatible with the
264          * planned unified hierarchy, which will automatically turn on this
265          * flag.
266          *
267          * The followings are the behaviors currently affected this flag.
268          *
269          * - Mount options "noprefix" and "clone_children" are disallowed.
270          *   Also, cgroupfs file cgroup.clone_children is not created.
271          *
272          * - When mounting an existing superblock, mount options should
273          *   match.
274          *
275          * - Remount is disallowed.
276          *
277          * - rename(2) is disallowed.
278          *
279          * - "tasks" is removed.  Everything should be at process
280          *   granularity.  Use "cgroup.procs" instead.
281          *
282          * - "release_agent" and "notify_on_release" are removed.
283          *   Replacement notification mechanism will be implemented.
284          *
285          * - cpuset: tasks will be kept in empty cpusets when hotplug happens
286          *   and take masks of ancestors with non-empty cpus/mems, instead of
287          *   being moved to an ancestor.
288          *
289          * - cpuset: a task can be moved into an empty cpuset, and again it
290          *   takes masks of ancestors.
291          *
292          * - memcg: use_hierarchy is on by default and the cgroup file for
293          *   the flag is not created.
294          *
295          * - blkcg: blk-throttle becomes properly hierarchical.
296          */
297         CGRP_ROOT_SANE_BEHAVIOR = (1 << 0),
298
299         CGRP_ROOT_NOPREFIX      = (1 << 1), /* mounted subsystems have no named prefix */
300         CGRP_ROOT_XATTR         = (1 << 2), /* supports extended attributes */
301
302         /* mount options live below bit 16 */
303         CGRP_ROOT_OPTION_MASK   = (1 << 16) - 1,
304
305         CGRP_ROOT_SUBSYS_BOUND  = (1 << 16), /* subsystems finished binding */
306 };
307
308 /*
309  * A cgroupfs_root represents the root of a cgroup hierarchy, and may be
310  * associated with a superblock to form an active hierarchy.  This is
311  * internal to cgroup core.  Don't access directly from controllers.
312  */
313 struct cgroupfs_root {
314         struct super_block *sb;
315
316         /* The bitmask of subsystems attached to this hierarchy */
317         unsigned long subsys_mask;
318
319         /* Unique id for this hierarchy. */
320         int hierarchy_id;
321
322         /* A list running through the attached subsystems */
323         struct list_head subsys_list;
324
325         /* The root cgroup for this hierarchy */
326         struct cgroup top_cgroup;
327
328         /* Tracks how many cgroups are currently defined in hierarchy.*/
329         int number_of_cgroups;
330
331         /* A list running through the active hierarchies */
332         struct list_head root_list;
333
334         /* Hierarchy-specific flags */
335         unsigned long flags;
336
337         /* IDs for cgroups in this hierarchy */
338         struct idr cgroup_idr;
339
340         /* The path to use for release notifications. */
341         char release_agent_path[PATH_MAX];
342
343         /* The name for this hierarchy - may be empty */
344         char name[MAX_CGROUP_ROOT_NAMELEN];
345 };
346
347 /*
348  * A css_set is a structure holding pointers to a set of
349  * cgroup_subsys_state objects. This saves space in the task struct
350  * object and speeds up fork()/exit(), since a single inc/dec and a
351  * list_add()/del() can bump the reference count on the entire cgroup
352  * set for a task.
353  */
354
355 struct css_set {
356
357         /* Reference count */
358         atomic_t refcount;
359
360         /*
361          * List running through all cgroup groups in the same hash
362          * slot. Protected by css_set_lock
363          */
364         struct hlist_node hlist;
365
366         /*
367          * List running through all tasks using this cgroup
368          * group. Protected by css_set_lock
369          */
370         struct list_head tasks;
371
372         /*
373          * List of cgrp_cset_links pointing at cgroups referenced from this
374          * css_set.  Protected by css_set_lock.
375          */
376         struct list_head cgrp_links;
377
378         /*
379          * Set of subsystem states, one for each subsystem. This array
380          * is immutable after creation apart from the init_css_set
381          * during subsystem registration (at boot time) and modular subsystem
382          * loading/unloading.
383          */
384         struct cgroup_subsys_state *subsys[CGROUP_SUBSYS_COUNT];
385
386         /* For RCU-protected deletion */
387         struct rcu_head rcu_head;
388 };
389
390 /*
391  * cgroup_map_cb is an abstract callback API for reporting map-valued
392  * control files
393  */
394
395 struct cgroup_map_cb {
396         int (*fill)(struct cgroup_map_cb *cb, const char *key, u64 value);
397         void *state;
398 };
399
400 /*
401  * struct cftype: handler definitions for cgroup control files
402  *
403  * When reading/writing to a file:
404  *      - the cgroup to use is file->f_dentry->d_parent->d_fsdata
405  *      - the 'cftype' of the file is file->f_dentry->d_fsdata
406  */
407
408 /* cftype->flags */
409 enum {
410         CFTYPE_ONLY_ON_ROOT     = (1 << 0),     /* only create on root cgrp */
411         CFTYPE_NOT_ON_ROOT      = (1 << 1),     /* don't create on root cgrp */
412         CFTYPE_INSANE           = (1 << 2),     /* don't create if sane_behavior */
413 };
414
415 #define MAX_CFTYPE_NAME         64
416
417 struct cftype {
418         /*
419          * By convention, the name should begin with the name of the
420          * subsystem, followed by a period.  Zero length string indicates
421          * end of cftype array.
422          */
423         char name[MAX_CFTYPE_NAME];
424         int private;
425         /*
426          * If not 0, file mode is set to this value, otherwise it will
427          * be figured out automatically
428          */
429         umode_t mode;
430
431         /*
432          * If non-zero, defines the maximum length of string that can
433          * be passed to write_string; defaults to 64
434          */
435         size_t max_write_len;
436
437         /* CFTYPE_* flags */
438         unsigned int flags;
439
440         /*
441          * The subsys this file belongs to.  Initialized automatically
442          * during registration.  NULL for cgroup core files.
443          */
444         struct cgroup_subsys *ss;
445
446         int (*open)(struct inode *inode, struct file *file);
447         ssize_t (*read)(struct cgroup_subsys_state *css, struct cftype *cft,
448                         struct file *file,
449                         char __user *buf, size_t nbytes, loff_t *ppos);
450         /*
451          * read_u64() is a shortcut for the common case of returning a
452          * single integer. Use it in place of read()
453          */
454         u64 (*read_u64)(struct cgroup_subsys_state *css, struct cftype *cft);
455         /*
456          * read_s64() is a signed version of read_u64()
457          */
458         s64 (*read_s64)(struct cgroup_subsys_state *css, struct cftype *cft);
459         /*
460          * read_map() is used for defining a map of key/value
461          * pairs. It should call cb->fill(cb, key, value) for each
462          * entry. The key/value pairs (and their ordering) should not
463          * change between reboots.
464          */
465         int (*read_map)(struct cgroup_subsys_state *css, struct cftype *cft,
466                         struct cgroup_map_cb *cb);
467         /*
468          * read_seq_string() is used for outputting a simple sequence
469          * using seqfile.
470          */
471         int (*read_seq_string)(struct cgroup_subsys_state *css,
472                                struct cftype *cft, struct seq_file *m);
473
474         ssize_t (*write)(struct cgroup_subsys_state *css, struct cftype *cft,
475                          struct file *file,
476                          const char __user *buf, size_t nbytes, loff_t *ppos);
477
478         /*
479          * write_u64() is a shortcut for the common case of accepting
480          * a single integer (as parsed by simple_strtoull) from
481          * userspace. Use in place of write(); return 0 or error.
482          */
483         int (*write_u64)(struct cgroup_subsys_state *css, struct cftype *cft,
484                          u64 val);
485         /*
486          * write_s64() is a signed version of write_u64()
487          */
488         int (*write_s64)(struct cgroup_subsys_state *css, struct cftype *cft,
489                          s64 val);
490
491         /*
492          * write_string() is passed a nul-terminated kernelspace
493          * buffer of maximum length determined by max_write_len.
494          * Returns 0 or -ve error code.
495          */
496         int (*write_string)(struct cgroup_subsys_state *css, struct cftype *cft,
497                             const char *buffer);
498         /*
499          * trigger() callback can be used to get some kick from the
500          * userspace, when the actual string written is not important
501          * at all. The private field can be used to determine the
502          * kick type for multiplexing.
503          */
504         int (*trigger)(struct cgroup_subsys_state *css, unsigned int event);
505
506         int (*release)(struct inode *inode, struct file *file);
507
508         /*
509          * register_event() callback will be used to add new userspace
510          * waiter for changes related to the cftype. Implement it if
511          * you want to provide this functionality. Use eventfd_signal()
512          * on eventfd to send notification to userspace.
513          */
514         int (*register_event)(struct cgroup_subsys_state *css,
515                               struct cftype *cft, struct eventfd_ctx *eventfd,
516                               const char *args);
517         /*
518          * unregister_event() callback will be called when userspace
519          * closes the eventfd or on cgroup removing.
520          * This callback must be implemented, if you want provide
521          * notification functionality.
522          */
523         void (*unregister_event)(struct cgroup_subsys_state *css,
524                                  struct cftype *cft,
525                                  struct eventfd_ctx *eventfd);
526 };
527
528 /*
529  * cftype_sets describe cftypes belonging to a subsystem and are chained at
530  * cgroup_subsys->cftsets.  Each cftset points to an array of cftypes
531  * terminated by zero length name.
532  */
533 struct cftype_set {
534         struct list_head                node;   /* chained at subsys->cftsets */
535         struct cftype                   *cfts;
536 };
537
538 /*
539  * See the comment above CGRP_ROOT_SANE_BEHAVIOR for details.  This
540  * function can be called as long as @cgrp is accessible.
541  */
542 static inline bool cgroup_sane_behavior(const struct cgroup *cgrp)
543 {
544         return cgrp->root->flags & CGRP_ROOT_SANE_BEHAVIOR;
545 }
546
547 /* Caller should hold rcu_read_lock() */
548 static inline const char *cgroup_name(const struct cgroup *cgrp)
549 {
550         return rcu_dereference(cgrp->name)->name;
551 }
552
553 int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts);
554 int cgroup_rm_cftypes(struct cftype *cfts);
555
556 bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor);
557
558 int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen);
559 int task_cgroup_path(struct task_struct *task, char *buf, size_t buflen);
560
561 int cgroup_task_count(const struct cgroup *cgrp);
562
563 /*
564  * Control Group taskset, used to pass around set of tasks to cgroup_subsys
565  * methods.
566  */
567 struct cgroup_taskset;
568 struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset);
569 struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset);
570 struct cgroup_subsys_state *cgroup_taskset_cur_css(struct cgroup_taskset *tset,
571                                                    int subsys_id);
572 int cgroup_taskset_size(struct cgroup_taskset *tset);
573
574 /**
575  * cgroup_taskset_for_each - iterate cgroup_taskset
576  * @task: the loop cursor
577  * @skip_css: skip if task's css matches this, %NULL to iterate through all
578  * @tset: taskset to iterate
579  */
580 #define cgroup_taskset_for_each(task, skip_css, tset)                   \
581         for ((task) = cgroup_taskset_first((tset)); (task);             \
582              (task) = cgroup_taskset_next((tset)))                      \
583                 if (!(skip_css) ||                                      \
584                     cgroup_taskset_cur_css((tset),                      \
585                         (skip_css)->ss->subsys_id) != (skip_css))
586
587 /*
588  * Control Group subsystem type.
589  * See Documentation/cgroups/cgroups.txt for details
590  */
591
592 struct cgroup_subsys {
593         struct cgroup_subsys_state *(*css_alloc)(struct cgroup_subsys_state *parent_css);
594         int (*css_online)(struct cgroup_subsys_state *css);
595         void (*css_offline)(struct cgroup_subsys_state *css);
596         void (*css_free)(struct cgroup_subsys_state *css);
597
598         int (*can_attach)(struct cgroup_subsys_state *css,
599                           struct cgroup_taskset *tset);
600         void (*cancel_attach)(struct cgroup_subsys_state *css,
601                               struct cgroup_taskset *tset);
602         void (*attach)(struct cgroup_subsys_state *css,
603                        struct cgroup_taskset *tset);
604         void (*fork)(struct task_struct *task);
605         void (*exit)(struct cgroup_subsys_state *css,
606                      struct cgroup_subsys_state *old_css,
607                      struct task_struct *task);
608         void (*bind)(struct cgroup_subsys_state *root_css);
609
610         int subsys_id;
611         int disabled;
612         int early_init;
613         /*
614          * True if this subsys uses ID. ID is not available before cgroup_init()
615          * (not available in early_init time.)
616          */
617         bool use_id;
618
619         /*
620          * If %false, this subsystem is properly hierarchical -
621          * configuration, resource accounting and restriction on a parent
622          * cgroup cover those of its children.  If %true, hierarchy support
623          * is broken in some ways - some subsystems ignore hierarchy
624          * completely while others are only implemented half-way.
625          *
626          * It's now disallowed to create nested cgroups if the subsystem is
627          * broken and cgroup core will emit a warning message on such
628          * cases.  Eventually, all subsystems will be made properly
629          * hierarchical and this will go away.
630          */
631         bool broken_hierarchy;
632         bool warned_broken_hierarchy;
633
634 #define MAX_CGROUP_TYPE_NAMELEN 32
635         const char *name;
636
637         /*
638          * Link to parent, and list entry in parent's children.
639          * Protected by cgroup_lock()
640          */
641         struct cgroupfs_root *root;
642         struct list_head sibling;
643         /* used when use_id == true */
644         struct idr idr;
645         spinlock_t id_lock;
646
647         /* list of cftype_sets */
648         struct list_head cftsets;
649
650         /* base cftypes, automatically [de]registered with subsys itself */
651         struct cftype *base_cftypes;
652         struct cftype_set base_cftset;
653
654         /* should be defined only by modular subsystems */
655         struct module *module;
656 };
657
658 #define SUBSYS(_x) extern struct cgroup_subsys _x ## _subsys;
659 #define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option)
660 #include <linux/cgroup_subsys.h>
661 #undef IS_SUBSYS_ENABLED
662 #undef SUBSYS
663
664 /**
665  * css_parent - find the parent css
666  * @css: the target cgroup_subsys_state
667  *
668  * Return the parent css of @css.  This function is guaranteed to return
669  * non-NULL parent as long as @css isn't the root.
670  */
671 static inline
672 struct cgroup_subsys_state *css_parent(struct cgroup_subsys_state *css)
673 {
674         return css->parent;
675 }
676
677 /**
678  * task_css_set_check - obtain a task's css_set with extra access conditions
679  * @task: the task to obtain css_set for
680  * @__c: extra condition expression to be passed to rcu_dereference_check()
681  *
682  * A task's css_set is RCU protected, initialized and exited while holding
683  * task_lock(), and can only be modified while holding both cgroup_mutex
684  * and task_lock() while the task is alive.  This macro verifies that the
685  * caller is inside proper critical section and returns @task's css_set.
686  *
687  * The caller can also specify additional allowed conditions via @__c, such
688  * as locks used during the cgroup_subsys::attach() methods.
689  */
690 #ifdef CONFIG_PROVE_RCU
691 extern struct mutex cgroup_mutex;
692 #define task_css_set_check(task, __c)                                   \
693         rcu_dereference_check((task)->cgroups,                          \
694                 lockdep_is_held(&(task)->alloc_lock) ||                 \
695                 lockdep_is_held(&cgroup_mutex) || (__c))
696 #else
697 #define task_css_set_check(task, __c)                                   \
698         rcu_dereference((task)->cgroups)
699 #endif
700
701 /**
702  * task_css_check - obtain css for (task, subsys) w/ extra access conds
703  * @task: the target task
704  * @subsys_id: the target subsystem ID
705  * @__c: extra condition expression to be passed to rcu_dereference_check()
706  *
707  * Return the cgroup_subsys_state for the (@task, @subsys_id) pair.  The
708  * synchronization rules are the same as task_css_set_check().
709  */
710 #define task_css_check(task, subsys_id, __c)                            \
711         task_css_set_check((task), (__c))->subsys[(subsys_id)]
712
713 /**
714  * task_css_set - obtain a task's css_set
715  * @task: the task to obtain css_set for
716  *
717  * See task_css_set_check().
718  */
719 static inline struct css_set *task_css_set(struct task_struct *task)
720 {
721         return task_css_set_check(task, false);
722 }
723
724 /**
725  * task_css - obtain css for (task, subsys)
726  * @task: the target task
727  * @subsys_id: the target subsystem ID
728  *
729  * See task_css_check().
730  */
731 static inline struct cgroup_subsys_state *task_css(struct task_struct *task,
732                                                    int subsys_id)
733 {
734         return task_css_check(task, subsys_id, false);
735 }
736
737 static inline struct cgroup *task_cgroup(struct task_struct *task,
738                                          int subsys_id)
739 {
740         return task_css(task, subsys_id)->cgroup;
741 }
742
743 /**
744  * cgroup_from_id - lookup cgroup by id
745  * @ss: cgroup subsys to be looked into
746  * @id: the cgroup id
747  *
748  * Returns the cgroup if there's valid one with @id, otherwise returns NULL.
749  * Should be called under rcu_read_lock().
750  */
751 static inline struct cgroup *cgroup_from_id(struct cgroup_subsys *ss, int id)
752 {
753 #ifdef CONFIG_PROVE_RCU
754         rcu_lockdep_assert(rcu_read_lock_held() ||
755                            lockdep_is_held(&cgroup_mutex),
756                            "cgroup_from_id() needs proper protection");
757 #endif
758         return idr_find(&ss->root->cgroup_idr, id);
759 }
760
761 struct cgroup_subsys_state *css_next_child(struct cgroup_subsys_state *pos,
762                                            struct cgroup_subsys_state *parent);
763
764 /**
765  * css_for_each_child - iterate through children of a css
766  * @pos: the css * to use as the loop cursor
767  * @parent: css whose children to walk
768  *
769  * Walk @parent's children.  Must be called under rcu_read_lock().  A child
770  * css which hasn't finished ->css_online() or already has finished
771  * ->css_offline() may show up during traversal and it's each subsystem's
772  * responsibility to verify that each @pos is alive.
773  *
774  * If a subsystem synchronizes against the parent in its ->css_online() and
775  * before starting iterating, a css which finished ->css_online() is
776  * guaranteed to be visible in the future iterations.
777  *
778  * It is allowed to temporarily drop RCU read lock during iteration.  The
779  * caller is responsible for ensuring that @pos remains accessible until
780  * the start of the next iteration by, for example, bumping the css refcnt.
781  */
782 #define css_for_each_child(pos, parent)                                 \
783         for ((pos) = css_next_child(NULL, (parent)); (pos);             \
784              (pos) = css_next_child((pos), (parent)))
785
786 struct cgroup_subsys_state *
787 css_next_descendant_pre(struct cgroup_subsys_state *pos,
788                         struct cgroup_subsys_state *css);
789
790 struct cgroup_subsys_state *
791 css_rightmost_descendant(struct cgroup_subsys_state *pos);
792
793 /**
794  * css_for_each_descendant_pre - pre-order walk of a css's descendants
795  * @pos: the css * to use as the loop cursor
796  * @root: css whose descendants to walk
797  *
798  * Walk @root's descendants.  @root is included in the iteration and the
799  * first node to be visited.  Must be called under rcu_read_lock().  A
800  * descendant css which hasn't finished ->css_online() or already has
801  * finished ->css_offline() may show up during traversal and it's each
802  * subsystem's responsibility to verify that each @pos is alive.
803  *
804  * If a subsystem synchronizes against the parent in its ->css_online() and
805  * before starting iterating, and synchronizes against @pos on each
806  * iteration, any descendant css which finished ->css_online() is
807  * guaranteed to be visible in the future iterations.
808  *
809  * In other words, the following guarantees that a descendant can't escape
810  * state updates of its ancestors.
811  *
812  * my_online(@css)
813  * {
814  *      Lock @css's parent and @css;
815  *      Inherit state from the parent;
816  *      Unlock both.
817  * }
818  *
819  * my_update_state(@css)
820  * {
821  *      css_for_each_descendant_pre(@pos, @css) {
822  *              Lock @pos;
823  *              if (@pos == @css)
824  *                      Update @css's state;
825  *              else
826  *                      Verify @pos is alive and inherit state from its parent;
827  *              Unlock @pos;
828  *      }
829  * }
830  *
831  * As long as the inheriting step, including checking the parent state, is
832  * enclosed inside @pos locking, double-locking the parent isn't necessary
833  * while inheriting.  The state update to the parent is guaranteed to be
834  * visible by walking order and, as long as inheriting operations to the
835  * same @pos are atomic to each other, multiple updates racing each other
836  * still result in the correct state.  It's guaranateed that at least one
837  * inheritance happens for any css after the latest update to its parent.
838  *
839  * If checking parent's state requires locking the parent, each inheriting
840  * iteration should lock and unlock both @pos->parent and @pos.
841  *
842  * Alternatively, a subsystem may choose to use a single global lock to
843  * synchronize ->css_online() and ->css_offline() against tree-walking
844  * operations.
845  *
846  * It is allowed to temporarily drop RCU read lock during iteration.  The
847  * caller is responsible for ensuring that @pos remains accessible until
848  * the start of the next iteration by, for example, bumping the css refcnt.
849  */
850 #define css_for_each_descendant_pre(pos, css)                           \
851         for ((pos) = css_next_descendant_pre(NULL, (css)); (pos);       \
852              (pos) = css_next_descendant_pre((pos), (css)))
853
854 struct cgroup_subsys_state *
855 css_next_descendant_post(struct cgroup_subsys_state *pos,
856                          struct cgroup_subsys_state *css);
857
858 /**
859  * css_for_each_descendant_post - post-order walk of a css's descendants
860  * @pos: the css * to use as the loop cursor
861  * @css: css whose descendants to walk
862  *
863  * Similar to css_for_each_descendant_pre() but performs post-order
864  * traversal instead.  @root is included in the iteration and the last
865  * node to be visited.  Note that the walk visibility guarantee described
866  * in pre-order walk doesn't apply the same to post-order walks.
867  */
868 #define css_for_each_descendant_post(pos, css)                          \
869         for ((pos) = css_next_descendant_post(NULL, (css)); (pos);      \
870              (pos) = css_next_descendant_post((pos), (css)))
871
872 /* A css_task_iter should be treated as an opaque object */
873 struct css_task_iter {
874         struct cgroup_subsys_state      *origin_css;
875         struct list_head                *cset_link;
876         struct list_head                *task;
877 };
878
879 void css_task_iter_start(struct cgroup_subsys_state *css,
880                          struct css_task_iter *it);
881 struct task_struct *css_task_iter_next(struct css_task_iter *it);
882 void css_task_iter_end(struct css_task_iter *it);
883
884 int css_scan_tasks(struct cgroup_subsys_state *css,
885                    bool (*test)(struct task_struct *, void *),
886                    void (*process)(struct task_struct *, void *),
887                    void *data, struct ptr_heap *heap);
888
889 int cgroup_attach_task_all(struct task_struct *from, struct task_struct *);
890 int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from);
891
892 /*
893  * CSS ID is ID for cgroup_subsys_state structs under subsys. This only works
894  * if cgroup_subsys.use_id == true. It can be used for looking up and scanning.
895  * CSS ID is assigned at cgroup allocation (create) automatically
896  * and removed when subsys calls free_css_id() function. This is because
897  * the lifetime of cgroup_subsys_state is subsys's matter.
898  *
899  * Looking up and scanning function should be called under rcu_read_lock().
900  * Taking cgroup_mutex is not necessary for following calls.
901  * But the css returned by this routine can be "not populated yet" or "being
902  * destroyed". The caller should check css and cgroup's status.
903  */
904
905 /*
906  * Typically Called at ->destroy(), or somewhere the subsys frees
907  * cgroup_subsys_state.
908  */
909 void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css);
910
911 /* Find a cgroup_subsys_state which has given ID */
912
913 struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id);
914
915 /* Returns true if root is ancestor of cg */
916 bool css_is_ancestor(struct cgroup_subsys_state *cg,
917                      const struct cgroup_subsys_state *root);
918
919 /* Get id and depth of css */
920 unsigned short css_id(struct cgroup_subsys_state *css);
921 struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id);
922
923 #else /* !CONFIG_CGROUPS */
924
925 static inline int cgroup_init_early(void) { return 0; }
926 static inline int cgroup_init(void) { return 0; }
927 static inline void cgroup_fork(struct task_struct *p) {}
928 static inline void cgroup_post_fork(struct task_struct *p) {}
929 static inline void cgroup_exit(struct task_struct *p, int callbacks) {}
930
931 static inline int cgroupstats_build(struct cgroupstats *stats,
932                                         struct dentry *dentry)
933 {
934         return -EINVAL;
935 }
936
937 /* No cgroups - nothing to do */
938 static inline int cgroup_attach_task_all(struct task_struct *from,
939                                          struct task_struct *t)
940 {
941         return 0;
942 }
943
944 #endif /* !CONFIG_CGROUPS */
945
946 #endif /* _LINUX_CGROUP_H */