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