Task Control Groups: shared cgroup subsystem group arrays
[safe/jmp/linux-2.6] / 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/kref.h>
13 #include <linux/cpumask.h>
14 #include <linux/nodemask.h>
15 #include <linux/rcupdate.h>
16
17 #ifdef CONFIG_CGROUPS
18
19 struct cgroupfs_root;
20 struct cgroup_subsys;
21 struct inode;
22
23 extern int cgroup_init_early(void);
24 extern int cgroup_init(void);
25 extern void cgroup_init_smp(void);
26 extern void cgroup_lock(void);
27 extern void cgroup_unlock(void);
28 extern void cgroup_fork(struct task_struct *p);
29 extern void cgroup_fork_callbacks(struct task_struct *p);
30 extern void cgroup_post_fork(struct task_struct *p);
31 extern void cgroup_exit(struct task_struct *p, int run_callbacks);
32
33 extern struct file_operations proc_cgroup_operations;
34
35 /* Define the enumeration of all cgroup subsystems */
36 #define SUBSYS(_x) _x ## _subsys_id,
37 enum cgroup_subsys_id {
38 #include <linux/cgroup_subsys.h>
39         CGROUP_SUBSYS_COUNT
40 };
41 #undef SUBSYS
42
43 /* Per-subsystem/per-cgroup state maintained by the system. */
44 struct cgroup_subsys_state {
45         /* The cgroup that this subsystem is attached to. Useful
46          * for subsystems that want to know about the cgroup
47          * hierarchy structure */
48         struct cgroup *cgroup;
49
50         /* State maintained by the cgroup system to allow
51          * subsystems to be "busy". Should be accessed via css_get()
52          * and css_put() */
53
54         atomic_t refcnt;
55
56         unsigned long flags;
57 };
58
59 /* bits in struct cgroup_subsys_state flags field */
60 enum {
61         CSS_ROOT, /* This CSS is the root of the subsystem */
62 };
63
64 /*
65  * Call css_get() to hold a reference on the cgroup;
66  *
67  */
68
69 static inline void css_get(struct cgroup_subsys_state *css)
70 {
71         /* We don't need to reference count the root state */
72         if (!test_bit(CSS_ROOT, &css->flags))
73                 atomic_inc(&css->refcnt);
74 }
75 /*
76  * css_put() should be called to release a reference taken by
77  * css_get()
78  */
79
80 static inline void css_put(struct cgroup_subsys_state *css)
81 {
82         if (!test_bit(CSS_ROOT, &css->flags))
83                 atomic_dec(&css->refcnt);
84 }
85
86 struct cgroup {
87         unsigned long flags;            /* "unsigned long" so bitops work */
88
89         /* count users of this cgroup. >0 means busy, but doesn't
90          * necessarily indicate the number of tasks in the
91          * cgroup */
92         atomic_t count;
93
94         /*
95          * We link our 'sibling' struct into our parent's 'children'.
96          * Our children link their 'sibling' into our 'children'.
97          */
98         struct list_head sibling;       /* my parent's children */
99         struct list_head children;      /* my children */
100
101         struct cgroup *parent;  /* my parent */
102         struct dentry *dentry;          /* cgroup fs entry */
103
104         /* Private pointers for each registered subsystem */
105         struct cgroup_subsys_state *subsys[CGROUP_SUBSYS_COUNT];
106
107         struct cgroupfs_root *root;
108         struct cgroup *top_cgroup;
109
110         /*
111          * List of cg_cgroup_links pointing at css_sets with
112          * tasks in this cgroup. Protected by css_set_lock
113          */
114         struct list_head css_sets;
115 };
116
117 /* A css_set is a structure holding pointers to a set of
118  * cgroup_subsys_state objects. This saves space in the task struct
119  * object and speeds up fork()/exit(), since a single inc/dec and a
120  * list_add()/del() can bump the reference count on the entire
121  * cgroup set for a task.
122  */
123
124 struct css_set {
125
126         /* Reference count */
127         struct kref ref;
128
129         /*
130          * List running through all cgroup groups. Protected by
131          * css_set_lock
132          */
133         struct list_head list;
134
135         /*
136          * List running through all tasks using this cgroup
137          * group. Protected by css_set_lock
138          */
139         struct list_head tasks;
140
141         /*
142          * List of cg_cgroup_link objects on link chains from
143          * cgroups referenced from this css_set. Protected by
144          * css_set_lock
145          */
146         struct list_head cg_links;
147
148         /*
149          * Set of subsystem states, one for each subsystem. This array
150          * is immutable after creation apart from the init_css_set
151          * during subsystem registration (at boot time).
152          */
153         struct cgroup_subsys_state *subsys[CGROUP_SUBSYS_COUNT];
154
155 };
156
157 /* struct cftype:
158  *
159  * The files in the cgroup filesystem mostly have a very simple read/write
160  * handling, some common function will take care of it. Nevertheless some cases
161  * (read tasks) are special and therefore I define this structure for every
162  * kind of file.
163  *
164  *
165  * When reading/writing to a file:
166  *      - the cgroup to use in file->f_dentry->d_parent->d_fsdata
167  *      - the 'cftype' of the file is file->f_dentry->d_fsdata
168  */
169
170 #define MAX_CFTYPE_NAME 64
171 struct cftype {
172         /* By convention, the name should begin with the name of the
173          * subsystem, followed by a period */
174         char name[MAX_CFTYPE_NAME];
175         int private;
176         int (*open) (struct inode *inode, struct file *file);
177         ssize_t (*read) (struct cgroup *cont, struct cftype *cft,
178                          struct file *file,
179                          char __user *buf, size_t nbytes, loff_t *ppos);
180         /*
181          * read_uint() is a shortcut for the common case of returning a
182          * single integer. Use it in place of read()
183          */
184         u64 (*read_uint) (struct cgroup *cont, struct cftype *cft);
185         ssize_t (*write) (struct cgroup *cont, struct cftype *cft,
186                           struct file *file,
187                           const char __user *buf, size_t nbytes, loff_t *ppos);
188
189         /*
190          * write_uint() is a shortcut for the common case of accepting
191          * a single integer (as parsed by simple_strtoull) from
192          * userspace. Use in place of write(); return 0 or error.
193          */
194         int (*write_uint) (struct cgroup *cont, struct cftype *cft, u64 val);
195
196         int (*release) (struct inode *inode, struct file *file);
197 };
198
199 /* Add a new file to the given cgroup directory. Should only be
200  * called by subsystems from within a populate() method */
201 int cgroup_add_file(struct cgroup *cont, struct cgroup_subsys *subsys,
202                        const struct cftype *cft);
203
204 /* Add a set of new files to the given cgroup directory. Should
205  * only be called by subsystems from within a populate() method */
206 int cgroup_add_files(struct cgroup *cont,
207                         struct cgroup_subsys *subsys,
208                         const struct cftype cft[],
209                         int count);
210
211 int cgroup_is_removed(const struct cgroup *cont);
212
213 int cgroup_path(const struct cgroup *cont, char *buf, int buflen);
214
215 int cgroup_task_count(const struct cgroup *cont);
216
217 /* Return true if the cgroup is a descendant of the current cgroup */
218 int cgroup_is_descendant(const struct cgroup *cont);
219
220 /* Control Group subsystem type. See Documentation/cgroups.txt for details */
221
222 struct cgroup_subsys {
223         struct cgroup_subsys_state *(*create)(struct cgroup_subsys *ss,
224                                                   struct cgroup *cont);
225         void (*destroy)(struct cgroup_subsys *ss, struct cgroup *cont);
226         int (*can_attach)(struct cgroup_subsys *ss,
227                           struct cgroup *cont, struct task_struct *tsk);
228         void (*attach)(struct cgroup_subsys *ss, struct cgroup *cont,
229                         struct cgroup *old_cont, struct task_struct *tsk);
230         void (*fork)(struct cgroup_subsys *ss, struct task_struct *task);
231         void (*exit)(struct cgroup_subsys *ss, struct task_struct *task);
232         int (*populate)(struct cgroup_subsys *ss,
233                         struct cgroup *cont);
234         void (*post_clone)(struct cgroup_subsys *ss, struct cgroup *cont);
235         void (*bind)(struct cgroup_subsys *ss, struct cgroup *root);
236         int subsys_id;
237         int active;
238         int early_init;
239 #define MAX_CGROUP_TYPE_NAMELEN 32
240         const char *name;
241
242         /* Protected by RCU */
243         struct cgroupfs_root *root;
244
245         struct list_head sibling;
246
247         void *private;
248 };
249
250 #define SUBSYS(_x) extern struct cgroup_subsys _x ## _subsys;
251 #include <linux/cgroup_subsys.h>
252 #undef SUBSYS
253
254 static inline struct cgroup_subsys_state *cgroup_subsys_state(
255         struct cgroup *cont, int subsys_id)
256 {
257         return cont->subsys[subsys_id];
258 }
259
260 static inline struct cgroup_subsys_state *task_subsys_state(
261         struct task_struct *task, int subsys_id)
262 {
263         return rcu_dereference(task->cgroups->subsys[subsys_id]);
264 }
265
266 static inline struct cgroup* task_cgroup(struct task_struct *task,
267                                                int subsys_id)
268 {
269         return task_subsys_state(task, subsys_id)->cgroup;
270 }
271
272 int cgroup_path(const struct cgroup *cont, char *buf, int buflen);
273
274 int cgroup_clone(struct task_struct *tsk, struct cgroup_subsys *ss);
275
276 /* A cgroup_iter should be treated as an opaque object */
277 struct cgroup_iter {
278         struct list_head *cg_link;
279         struct list_head *task;
280 };
281
282 /* To iterate across the tasks in a cgroup:
283  *
284  * 1) call cgroup_iter_start to intialize an iterator
285  *
286  * 2) call cgroup_iter_next() to retrieve member tasks until it
287  *    returns NULL or until you want to end the iteration
288  *
289  * 3) call cgroup_iter_end() to destroy the iterator.
290  */
291 void cgroup_iter_start(struct cgroup *cont, struct cgroup_iter *it);
292 struct task_struct *cgroup_iter_next(struct cgroup *cont,
293                                         struct cgroup_iter *it);
294 void cgroup_iter_end(struct cgroup *cont, struct cgroup_iter *it);
295
296
297 #else /* !CONFIG_CGROUPS */
298
299 static inline int cgroup_init_early(void) { return 0; }
300 static inline int cgroup_init(void) { return 0; }
301 static inline void cgroup_init_smp(void) {}
302 static inline void cgroup_fork(struct task_struct *p) {}
303 static inline void cgroup_fork_callbacks(struct task_struct *p) {}
304 static inline void cgroup_post_fork(struct task_struct *p) {}
305 static inline void cgroup_exit(struct task_struct *p, int callbacks) {}
306
307 static inline void cgroup_lock(void) {}
308 static inline void cgroup_unlock(void) {}
309
310 #endif /* !CONFIG_CGROUPS */
311
312 #endif /* _LINUX_CGROUP_H */