bb8feb9feccdedaa3793b98df7d6eefb98eefc14
[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/cpumask.h>
13 #include <linux/nodemask.h>
14 #include <linux/rcupdate.h>
15 #include <linux/cgroupstats.h>
16 #include <linux/prio_heap.h>
17 #include <linux/rwsem.h>
18
19 #ifdef CONFIG_CGROUPS
20
21 struct cgroupfs_root;
22 struct cgroup_subsys;
23 struct inode;
24 struct cgroup;
25
26 extern int cgroup_init_early(void);
27 extern int cgroup_init(void);
28 extern void cgroup_lock(void);
29 extern bool cgroup_lock_live_group(struct cgroup *cgrp);
30 extern void cgroup_unlock(void);
31 extern void cgroup_fork(struct task_struct *p);
32 extern void cgroup_fork_callbacks(struct task_struct *p);
33 extern void cgroup_post_fork(struct task_struct *p);
34 extern void cgroup_exit(struct task_struct *p, int run_callbacks);
35 extern int cgroupstats_build(struct cgroupstats *stats,
36                                 struct dentry *dentry);
37
38 extern struct file_operations proc_cgroup_operations;
39
40 /* Define the enumeration of all cgroup subsystems */
41 #define SUBSYS(_x) _x ## _subsys_id,
42 enum cgroup_subsys_id {
43 #include <linux/cgroup_subsys.h>
44         CGROUP_SUBSYS_COUNT
45 };
46 #undef SUBSYS
47
48 /* Per-subsystem/per-cgroup state maintained by the system. */
49 struct cgroup_subsys_state {
50         /*
51          * The cgroup that this subsystem is attached to. Useful
52          * for subsystems that want to know about the cgroup
53          * hierarchy structure
54          */
55         struct cgroup *cgroup;
56
57         /*
58          * State maintained by the cgroup system to allow subsystems
59          * to be "busy". Should be accessed via css_get(),
60          * css_tryget() and and css_put().
61          */
62
63         atomic_t refcnt;
64
65         unsigned long flags;
66 };
67
68 /* bits in struct cgroup_subsys_state flags field */
69 enum {
70         CSS_ROOT, /* This CSS is the root of the subsystem */
71         CSS_REMOVED, /* This CSS is dead */
72 };
73
74 /*
75  * Call css_get() to hold a reference on the css; it can be used
76  * for a reference obtained via:
77  * - an existing ref-counted reference to the css
78  * - task->cgroups for a locked task
79  */
80
81 static inline void css_get(struct cgroup_subsys_state *css)
82 {
83         /* We don't need to reference count the root state */
84         if (!test_bit(CSS_ROOT, &css->flags))
85                 atomic_inc(&css->refcnt);
86 }
87
88 static inline bool css_is_removed(struct cgroup_subsys_state *css)
89 {
90         return test_bit(CSS_REMOVED, &css->flags);
91 }
92
93 /*
94  * Call css_tryget() to take a reference on a css if your existing
95  * (known-valid) reference isn't already ref-counted. Returns false if
96  * the css has been destroyed.
97  */
98
99 static inline bool css_tryget(struct cgroup_subsys_state *css)
100 {
101         if (test_bit(CSS_ROOT, &css->flags))
102                 return true;
103         while (!atomic_inc_not_zero(&css->refcnt)) {
104                 if (test_bit(CSS_REMOVED, &css->flags))
105                         return false;
106                 cpu_relax();
107         }
108         return true;
109 }
110
111 /*
112  * css_put() should be called to release a reference taken by
113  * css_get() or css_tryget()
114  */
115
116 extern void __css_put(struct cgroup_subsys_state *css);
117 static inline void css_put(struct cgroup_subsys_state *css)
118 {
119         if (!test_bit(CSS_ROOT, &css->flags))
120                 __css_put(css);
121 }
122
123 /* bits in struct cgroup flags field */
124 enum {
125         /* Control Group is dead */
126         CGRP_REMOVED,
127         /*
128          * Control Group has previously had a child cgroup or a task,
129          * but no longer (only if CGRP_NOTIFY_ON_RELEASE is set)
130          */
131         CGRP_RELEASABLE,
132         /* Control Group requires release notifications to userspace */
133         CGRP_NOTIFY_ON_RELEASE,
134 };
135
136 struct cgroup {
137         unsigned long flags;            /* "unsigned long" so bitops work */
138
139         /*
140          * count users of this cgroup. >0 means busy, but doesn't
141          * necessarily indicate the number of tasks in the cgroup
142          */
143         atomic_t count;
144
145         /*
146          * We link our 'sibling' struct into our parent's 'children'.
147          * Our children link their 'sibling' into our 'children'.
148          */
149         struct list_head sibling;       /* my parent's children */
150         struct list_head children;      /* my children */
151
152         struct cgroup *parent;          /* my parent */
153         struct dentry *dentry;          /* cgroup fs entry, RCU protected */
154
155         /* Private pointers for each registered subsystem */
156         struct cgroup_subsys_state *subsys[CGROUP_SUBSYS_COUNT];
157
158         struct cgroupfs_root *root;
159         struct cgroup *top_cgroup;
160
161         /*
162          * List of cg_cgroup_links pointing at css_sets with
163          * tasks in this cgroup. Protected by css_set_lock
164          */
165         struct list_head css_sets;
166
167         /*
168          * Linked list running through all cgroups that can
169          * potentially be reaped by the release agent. Protected by
170          * release_list_lock
171          */
172         struct list_head release_list;
173
174         /* pids_mutex protects the fields below */
175         struct rw_semaphore pids_mutex;
176         /* Array of process ids in the cgroup */
177         pid_t *tasks_pids;
178         /* How many files are using the current tasks_pids array */
179         int pids_use_count;
180         /* Length of the current tasks_pids array */
181         int pids_length;
182
183         /* For RCU-protected deletion */
184         struct rcu_head rcu_head;
185 };
186
187 /*
188  * A css_set is a structure holding pointers to a set of
189  * cgroup_subsys_state objects. This saves space in the task struct
190  * object and speeds up fork()/exit(), since a single inc/dec and a
191  * list_add()/del() can bump the reference count on the entire cgroup
192  * set for a task.
193  */
194
195 struct css_set {
196
197         /* Reference count */
198         atomic_t refcount;
199
200         /*
201          * List running through all cgroup groups in the same hash
202          * slot. Protected by css_set_lock
203          */
204         struct hlist_node hlist;
205
206         /*
207          * List running through all tasks using this cgroup
208          * group. Protected by css_set_lock
209          */
210         struct list_head tasks;
211
212         /*
213          * List of cg_cgroup_link objects on link chains from
214          * cgroups referenced from this css_set. Protected by
215          * css_set_lock
216          */
217         struct list_head cg_links;
218
219         /*
220          * Set of subsystem states, one for each subsystem. This array
221          * is immutable after creation apart from the init_css_set
222          * during subsystem registration (at boot time).
223          */
224         struct cgroup_subsys_state *subsys[CGROUP_SUBSYS_COUNT];
225 };
226
227 /*
228  * cgroup_map_cb is an abstract callback API for reporting map-valued
229  * control files
230  */
231
232 struct cgroup_map_cb {
233         int (*fill)(struct cgroup_map_cb *cb, const char *key, u64 value);
234         void *state;
235 };
236
237 /*
238  * struct cftype: handler definitions for cgroup control files
239  *
240  * When reading/writing to a file:
241  *      - the cgroup to use is file->f_dentry->d_parent->d_fsdata
242  *      - the 'cftype' of the file is file->f_dentry->d_fsdata
243  */
244
245 #define MAX_CFTYPE_NAME 64
246 struct cftype {
247         /*
248          * By convention, the name should begin with the name of the
249          * subsystem, followed by a period
250          */
251         char name[MAX_CFTYPE_NAME];
252         int private;
253
254         /*
255          * If non-zero, defines the maximum length of string that can
256          * be passed to write_string; defaults to 64
257          */
258         size_t max_write_len;
259
260         int (*open)(struct inode *inode, struct file *file);
261         ssize_t (*read)(struct cgroup *cgrp, struct cftype *cft,
262                         struct file *file,
263                         char __user *buf, size_t nbytes, loff_t *ppos);
264         /*
265          * read_u64() is a shortcut for the common case of returning a
266          * single integer. Use it in place of read()
267          */
268         u64 (*read_u64)(struct cgroup *cgrp, struct cftype *cft);
269         /*
270          * read_s64() is a signed version of read_u64()
271          */
272         s64 (*read_s64)(struct cgroup *cgrp, struct cftype *cft);
273         /*
274          * read_map() is used for defining a map of key/value
275          * pairs. It should call cb->fill(cb, key, value) for each
276          * entry. The key/value pairs (and their ordering) should not
277          * change between reboots.
278          */
279         int (*read_map)(struct cgroup *cont, struct cftype *cft,
280                         struct cgroup_map_cb *cb);
281         /*
282          * read_seq_string() is used for outputting a simple sequence
283          * using seqfile.
284          */
285         int (*read_seq_string)(struct cgroup *cont, struct cftype *cft,
286                                struct seq_file *m);
287
288         ssize_t (*write)(struct cgroup *cgrp, struct cftype *cft,
289                          struct file *file,
290                          const char __user *buf, size_t nbytes, loff_t *ppos);
291
292         /*
293          * write_u64() is a shortcut for the common case of accepting
294          * a single integer (as parsed by simple_strtoull) from
295          * userspace. Use in place of write(); return 0 or error.
296          */
297         int (*write_u64)(struct cgroup *cgrp, struct cftype *cft, u64 val);
298         /*
299          * write_s64() is a signed version of write_u64()
300          */
301         int (*write_s64)(struct cgroup *cgrp, struct cftype *cft, s64 val);
302
303         /*
304          * write_string() is passed a nul-terminated kernelspace
305          * buffer of maximum length determined by max_write_len.
306          * Returns 0 or -ve error code.
307          */
308         int (*write_string)(struct cgroup *cgrp, struct cftype *cft,
309                             const char *buffer);
310         /*
311          * trigger() callback can be used to get some kick from the
312          * userspace, when the actual string written is not important
313          * at all. The private field can be used to determine the
314          * kick type for multiplexing.
315          */
316         int (*trigger)(struct cgroup *cgrp, unsigned int event);
317
318         int (*release)(struct inode *inode, struct file *file);
319 };
320
321 struct cgroup_scanner {
322         struct cgroup *cg;
323         int (*test_task)(struct task_struct *p, struct cgroup_scanner *scan);
324         void (*process_task)(struct task_struct *p,
325                         struct cgroup_scanner *scan);
326         struct ptr_heap *heap;
327 };
328
329 /*
330  * Add a new file to the given cgroup directory. Should only be
331  * called by subsystems from within a populate() method
332  */
333 int cgroup_add_file(struct cgroup *cgrp, struct cgroup_subsys *subsys,
334                        const struct cftype *cft);
335
336 /*
337  * Add a set of new files to the given cgroup directory. Should
338  * only be called by subsystems from within a populate() method
339  */
340 int cgroup_add_files(struct cgroup *cgrp,
341                         struct cgroup_subsys *subsys,
342                         const struct cftype cft[],
343                         int count);
344
345 int cgroup_is_removed(const struct cgroup *cgrp);
346
347 int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen);
348
349 int cgroup_task_count(const struct cgroup *cgrp);
350
351 /* Return true if the cgroup is a descendant of the current cgroup */
352 int cgroup_is_descendant(const struct cgroup *cgrp);
353
354 /* Control Group subsystem type. See Documentation/cgroups.txt for details */
355
356 struct cgroup_subsys {
357         struct cgroup_subsys_state *(*create)(struct cgroup_subsys *ss,
358                                                   struct cgroup *cgrp);
359         void (*pre_destroy)(struct cgroup_subsys *ss, struct cgroup *cgrp);
360         void (*destroy)(struct cgroup_subsys *ss, struct cgroup *cgrp);
361         int (*can_attach)(struct cgroup_subsys *ss,
362                           struct cgroup *cgrp, struct task_struct *tsk);
363         void (*attach)(struct cgroup_subsys *ss, struct cgroup *cgrp,
364                         struct cgroup *old_cgrp, struct task_struct *tsk);
365         void (*fork)(struct cgroup_subsys *ss, struct task_struct *task);
366         void (*exit)(struct cgroup_subsys *ss, struct task_struct *task);
367         int (*populate)(struct cgroup_subsys *ss,
368                         struct cgroup *cgrp);
369         void (*post_clone)(struct cgroup_subsys *ss, struct cgroup *cgrp);
370         void (*bind)(struct cgroup_subsys *ss, struct cgroup *root);
371
372         int subsys_id;
373         int active;
374         int disabled;
375         int early_init;
376 #define MAX_CGROUP_TYPE_NAMELEN 32
377         const char *name;
378
379         /*
380          * Protects sibling/children links of cgroups in this
381          * hierarchy, plus protects which hierarchy (or none) the
382          * subsystem is a part of (i.e. root/sibling).  To avoid
383          * potential deadlocks, the following operations should not be
384          * undertaken while holding any hierarchy_mutex:
385          *
386          * - allocating memory
387          * - initiating hotplug events
388          */
389         struct mutex hierarchy_mutex;
390         struct lock_class_key subsys_key;
391
392         /*
393          * Link to parent, and list entry in parent's children.
394          * Protected by this->hierarchy_mutex and cgroup_lock()
395          */
396         struct cgroupfs_root *root;
397         struct list_head sibling;
398 };
399
400 #define SUBSYS(_x) extern struct cgroup_subsys _x ## _subsys;
401 #include <linux/cgroup_subsys.h>
402 #undef SUBSYS
403
404 static inline struct cgroup_subsys_state *cgroup_subsys_state(
405         struct cgroup *cgrp, int subsys_id)
406 {
407         return cgrp->subsys[subsys_id];
408 }
409
410 static inline struct cgroup_subsys_state *task_subsys_state(
411         struct task_struct *task, int subsys_id)
412 {
413         return rcu_dereference(task->cgroups->subsys[subsys_id]);
414 }
415
416 static inline struct cgroup* task_cgroup(struct task_struct *task,
417                                                int subsys_id)
418 {
419         return task_subsys_state(task, subsys_id)->cgroup;
420 }
421
422 int cgroup_clone(struct task_struct *tsk, struct cgroup_subsys *ss,
423                                                         char *nodename);
424
425 /* A cgroup_iter should be treated as an opaque object */
426 struct cgroup_iter {
427         struct list_head *cg_link;
428         struct list_head *task;
429 };
430
431 /*
432  * To iterate across the tasks in a cgroup:
433  *
434  * 1) call cgroup_iter_start to intialize an iterator
435  *
436  * 2) call cgroup_iter_next() to retrieve member tasks until it
437  *    returns NULL or until you want to end the iteration
438  *
439  * 3) call cgroup_iter_end() to destroy the iterator.
440  *
441  * Or, call cgroup_scan_tasks() to iterate through every task in a
442  * cgroup - cgroup_scan_tasks() holds the css_set_lock when calling
443  * the test_task() callback, but not while calling the process_task()
444  * callback.
445  */
446 void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it);
447 struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
448                                         struct cgroup_iter *it);
449 void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it);
450 int cgroup_scan_tasks(struct cgroup_scanner *scan);
451 int cgroup_attach_task(struct cgroup *, struct task_struct *);
452
453 #else /* !CONFIG_CGROUPS */
454
455 static inline int cgroup_init_early(void) { return 0; }
456 static inline int cgroup_init(void) { return 0; }
457 static inline void cgroup_fork(struct task_struct *p) {}
458 static inline void cgroup_fork_callbacks(struct task_struct *p) {}
459 static inline void cgroup_post_fork(struct task_struct *p) {}
460 static inline void cgroup_exit(struct task_struct *p, int callbacks) {}
461
462 static inline void cgroup_lock(void) {}
463 static inline void cgroup_unlock(void) {}
464 static inline int cgroupstats_build(struct cgroupstats *stats,
465                                         struct dentry *dentry)
466 {
467         return -EINVAL;
468 }
469
470 #endif /* !CONFIG_CGROUPS */
471
472 #endif /* _LINUX_CGROUP_H */