cgroup: introduce coalesce css_get() and css_put()
[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/cgroupstats.h>
16 #include <linux/prio_heap.h>
17 #include <linux/rwsem.h>
18 #include <linux/idr.h>
19
20 #ifdef CONFIG_CGROUPS
21
22 struct cgroupfs_root;
23 struct cgroup_subsys;
24 struct inode;
25 struct cgroup;
26 struct css_id;
27
28 extern int cgroup_init_early(void);
29 extern int cgroup_init(void);
30 extern void cgroup_lock(void);
31 extern int cgroup_lock_is_held(void);
32 extern bool cgroup_lock_live_group(struct cgroup *cgrp);
33 extern void cgroup_unlock(void);
34 extern void cgroup_fork(struct task_struct *p);
35 extern void cgroup_fork_callbacks(struct task_struct *p);
36 extern void cgroup_post_fork(struct task_struct *p);
37 extern void cgroup_exit(struct task_struct *p, int run_callbacks);
38 extern int cgroupstats_build(struct cgroupstats *stats,
39                                 struct dentry *dentry);
40
41 extern const struct file_operations proc_cgroup_operations;
42
43 /* Define the enumeration of all cgroup subsystems */
44 #define SUBSYS(_x) _x ## _subsys_id,
45 enum cgroup_subsys_id {
46 #include <linux/cgroup_subsys.h>
47         CGROUP_SUBSYS_COUNT
48 };
49 #undef SUBSYS
50
51 /* Per-subsystem/per-cgroup state maintained by the system. */
52 struct cgroup_subsys_state {
53         /*
54          * The cgroup that this subsystem is attached to. Useful
55          * for subsystems that want to know about the cgroup
56          * hierarchy structure
57          */
58         struct cgroup *cgroup;
59
60         /*
61          * State maintained by the cgroup system to allow subsystems
62          * to be "busy". Should be accessed via css_get(),
63          * css_tryget() and and css_put().
64          */
65
66         atomic_t refcnt;
67
68         unsigned long flags;
69         /* ID for this css, if possible */
70         struct css_id *id;
71 };
72
73 /* bits in struct cgroup_subsys_state flags field */
74 enum {
75         CSS_ROOT, /* This CSS is the root of the subsystem */
76         CSS_REMOVED, /* This CSS is dead */
77 };
78
79 /* Caller must verify that the css is not for root cgroup */
80 static inline void __css_get(struct cgroup_subsys_state *css, int count)
81 {
82         atomic_add(count, &css->refcnt);
83 }
84
85 /*
86  * Call css_get() to hold a reference on the css; it can be used
87  * for a reference obtained via:
88  * - an existing ref-counted reference to the css
89  * - task->cgroups for a locked task
90  */
91
92 static inline void css_get(struct cgroup_subsys_state *css)
93 {
94         /* We don't need to reference count the root state */
95         if (!test_bit(CSS_ROOT, &css->flags))
96                 __css_get(css, 1);
97 }
98
99 static inline bool css_is_removed(struct cgroup_subsys_state *css)
100 {
101         return test_bit(CSS_REMOVED, &css->flags);
102 }
103
104 /*
105  * Call css_tryget() to take a reference on a css if your existing
106  * (known-valid) reference isn't already ref-counted. Returns false if
107  * the css has been destroyed.
108  */
109
110 static inline bool css_tryget(struct cgroup_subsys_state *css)
111 {
112         if (test_bit(CSS_ROOT, &css->flags))
113                 return true;
114         while (!atomic_inc_not_zero(&css->refcnt)) {
115                 if (test_bit(CSS_REMOVED, &css->flags))
116                         return false;
117                 cpu_relax();
118         }
119         return true;
120 }
121
122 /*
123  * css_put() should be called to release a reference taken by
124  * css_get() or css_tryget()
125  */
126
127 extern void __css_put(struct cgroup_subsys_state *css, int count);
128 static inline void css_put(struct cgroup_subsys_state *css)
129 {
130         if (!test_bit(CSS_ROOT, &css->flags))
131                 __css_put(css, 1);
132 }
133
134 /* bits in struct cgroup flags field */
135 enum {
136         /* Control Group is dead */
137         CGRP_REMOVED,
138         /*
139          * Control Group has previously had a child cgroup or a task,
140          * but no longer (only if CGRP_NOTIFY_ON_RELEASE is set)
141          */
142         CGRP_RELEASABLE,
143         /* Control Group requires release notifications to userspace */
144         CGRP_NOTIFY_ON_RELEASE,
145         /*
146          * A thread in rmdir() is wating for this cgroup.
147          */
148         CGRP_WAIT_ON_RMDIR,
149 };
150
151 /* which pidlist file are we talking about? */
152 enum cgroup_filetype {
153         CGROUP_FILE_PROCS,
154         CGROUP_FILE_TASKS,
155 };
156
157 /*
158  * A pidlist is a list of pids that virtually represents the contents of one
159  * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
160  * a pair (one each for procs, tasks) for each pid namespace that's relevant
161  * to the cgroup.
162  */
163 struct cgroup_pidlist {
164         /*
165          * used to find which pidlist is wanted. doesn't change as long as
166          * this particular list stays in the list.
167          */
168         struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
169         /* array of xids */
170         pid_t *list;
171         /* how many elements the above list has */
172         int length;
173         /* how many files are using the current array */
174         int use_count;
175         /* each of these stored in a list by its cgroup */
176         struct list_head links;
177         /* pointer to the cgroup we belong to, for list removal purposes */
178         struct cgroup *owner;
179         /* protects the other fields */
180         struct rw_semaphore mutex;
181 };
182
183 struct cgroup {
184         unsigned long flags;            /* "unsigned long" so bitops work */
185
186         /*
187          * count users of this cgroup. >0 means busy, but doesn't
188          * necessarily indicate the number of tasks in the cgroup
189          */
190         atomic_t count;
191
192         /*
193          * We link our 'sibling' struct into our parent's 'children'.
194          * Our children link their 'sibling' into our 'children'.
195          */
196         struct list_head sibling;       /* my parent's children */
197         struct list_head children;      /* my children */
198
199         struct cgroup *parent;          /* my parent */
200         struct dentry *dentry;          /* cgroup fs entry, RCU protected */
201
202         /* Private pointers for each registered subsystem */
203         struct cgroup_subsys_state *subsys[CGROUP_SUBSYS_COUNT];
204
205         struct cgroupfs_root *root;
206         struct cgroup *top_cgroup;
207
208         /*
209          * List of cg_cgroup_links pointing at css_sets with
210          * tasks in this cgroup. Protected by css_set_lock
211          */
212         struct list_head css_sets;
213
214         /*
215          * Linked list running through all cgroups that can
216          * potentially be reaped by the release agent. Protected by
217          * release_list_lock
218          */
219         struct list_head release_list;
220
221         /*
222          * list of pidlists, up to two for each namespace (one for procs, one
223          * for tasks); created on demand.
224          */
225         struct list_head pidlists;
226         struct mutex pidlist_mutex;
227
228         /* For RCU-protected deletion */
229         struct rcu_head rcu_head;
230 };
231
232 /*
233  * A css_set is a structure holding pointers to a set of
234  * cgroup_subsys_state objects. This saves space in the task struct
235  * object and speeds up fork()/exit(), since a single inc/dec and a
236  * list_add()/del() can bump the reference count on the entire cgroup
237  * set for a task.
238  */
239
240 struct css_set {
241
242         /* Reference count */
243         atomic_t refcount;
244
245         /*
246          * List running through all cgroup groups in the same hash
247          * slot. Protected by css_set_lock
248          */
249         struct hlist_node hlist;
250
251         /*
252          * List running through all tasks using this cgroup
253          * group. Protected by css_set_lock
254          */
255         struct list_head tasks;
256
257         /*
258          * List of cg_cgroup_link objects on link chains from
259          * cgroups referenced from this css_set. Protected by
260          * css_set_lock
261          */
262         struct list_head cg_links;
263
264         /*
265          * Set of subsystem states, one for each subsystem. This array
266          * is immutable after creation apart from the init_css_set
267          * during subsystem registration (at boot time).
268          */
269         struct cgroup_subsys_state *subsys[CGROUP_SUBSYS_COUNT];
270
271         /* For RCU-protected deletion */
272         struct rcu_head rcu_head;
273 };
274
275 /*
276  * cgroup_map_cb is an abstract callback API for reporting map-valued
277  * control files
278  */
279
280 struct cgroup_map_cb {
281         int (*fill)(struct cgroup_map_cb *cb, const char *key, u64 value);
282         void *state;
283 };
284
285 /*
286  * struct cftype: handler definitions for cgroup control files
287  *
288  * When reading/writing to a file:
289  *      - the cgroup to use is file->f_dentry->d_parent->d_fsdata
290  *      - the 'cftype' of the file is file->f_dentry->d_fsdata
291  */
292
293 #define MAX_CFTYPE_NAME 64
294 struct cftype {
295         /*
296          * By convention, the name should begin with the name of the
297          * subsystem, followed by a period
298          */
299         char name[MAX_CFTYPE_NAME];
300         int private;
301         /*
302          * If not 0, file mode is set to this value, otherwise it will
303          * be figured out automatically
304          */
305         mode_t mode;
306
307         /*
308          * If non-zero, defines the maximum length of string that can
309          * be passed to write_string; defaults to 64
310          */
311         size_t max_write_len;
312
313         int (*open)(struct inode *inode, struct file *file);
314         ssize_t (*read)(struct cgroup *cgrp, struct cftype *cft,
315                         struct file *file,
316                         char __user *buf, size_t nbytes, loff_t *ppos);
317         /*
318          * read_u64() is a shortcut for the common case of returning a
319          * single integer. Use it in place of read()
320          */
321         u64 (*read_u64)(struct cgroup *cgrp, struct cftype *cft);
322         /*
323          * read_s64() is a signed version of read_u64()
324          */
325         s64 (*read_s64)(struct cgroup *cgrp, struct cftype *cft);
326         /*
327          * read_map() is used for defining a map of key/value
328          * pairs. It should call cb->fill(cb, key, value) for each
329          * entry. The key/value pairs (and their ordering) should not
330          * change between reboots.
331          */
332         int (*read_map)(struct cgroup *cont, struct cftype *cft,
333                         struct cgroup_map_cb *cb);
334         /*
335          * read_seq_string() is used for outputting a simple sequence
336          * using seqfile.
337          */
338         int (*read_seq_string)(struct cgroup *cont, struct cftype *cft,
339                                struct seq_file *m);
340
341         ssize_t (*write)(struct cgroup *cgrp, struct cftype *cft,
342                          struct file *file,
343                          const char __user *buf, size_t nbytes, loff_t *ppos);
344
345         /*
346          * write_u64() is a shortcut for the common case of accepting
347          * a single integer (as parsed by simple_strtoull) from
348          * userspace. Use in place of write(); return 0 or error.
349          */
350         int (*write_u64)(struct cgroup *cgrp, struct cftype *cft, u64 val);
351         /*
352          * write_s64() is a signed version of write_u64()
353          */
354         int (*write_s64)(struct cgroup *cgrp, struct cftype *cft, s64 val);
355
356         /*
357          * write_string() is passed a nul-terminated kernelspace
358          * buffer of maximum length determined by max_write_len.
359          * Returns 0 or -ve error code.
360          */
361         int (*write_string)(struct cgroup *cgrp, struct cftype *cft,
362                             const char *buffer);
363         /*
364          * trigger() callback can be used to get some kick from the
365          * userspace, when the actual string written is not important
366          * at all. The private field can be used to determine the
367          * kick type for multiplexing.
368          */
369         int (*trigger)(struct cgroup *cgrp, unsigned int event);
370
371         int (*release)(struct inode *inode, struct file *file);
372 };
373
374 struct cgroup_scanner {
375         struct cgroup *cg;
376         int (*test_task)(struct task_struct *p, struct cgroup_scanner *scan);
377         void (*process_task)(struct task_struct *p,
378                         struct cgroup_scanner *scan);
379         struct ptr_heap *heap;
380         void *data;
381 };
382
383 /*
384  * Add a new file to the given cgroup directory. Should only be
385  * called by subsystems from within a populate() method
386  */
387 int cgroup_add_file(struct cgroup *cgrp, struct cgroup_subsys *subsys,
388                        const struct cftype *cft);
389
390 /*
391  * Add a set of new files to the given cgroup directory. Should
392  * only be called by subsystems from within a populate() method
393  */
394 int cgroup_add_files(struct cgroup *cgrp,
395                         struct cgroup_subsys *subsys,
396                         const struct cftype cft[],
397                         int count);
398
399 int cgroup_is_removed(const struct cgroup *cgrp);
400
401 int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen);
402
403 int cgroup_task_count(const struct cgroup *cgrp);
404
405 /* Return true if cgrp is a descendant of the task's cgroup */
406 int cgroup_is_descendant(const struct cgroup *cgrp, struct task_struct *task);
407
408 /*
409  * When the subsys has to access css and may add permanent refcnt to css,
410  * it should take care of racy conditions with rmdir(). Following set of
411  * functions, is for stop/restart rmdir if necessary.
412  * Because these will call css_get/put, "css" should be alive css.
413  *
414  *  cgroup_exclude_rmdir();
415  *  ...do some jobs which may access arbitrary empty cgroup
416  *  cgroup_release_and_wakeup_rmdir();
417  *
418  *  When someone removes a cgroup while cgroup_exclude_rmdir() holds it,
419  *  it sleeps and cgroup_release_and_wakeup_rmdir() will wake him up.
420  */
421
422 void cgroup_exclude_rmdir(struct cgroup_subsys_state *css);
423 void cgroup_release_and_wakeup_rmdir(struct cgroup_subsys_state *css);
424
425 /*
426  * Control Group subsystem type.
427  * See Documentation/cgroups/cgroups.txt for details
428  */
429
430 struct cgroup_subsys {
431         struct cgroup_subsys_state *(*create)(struct cgroup_subsys *ss,
432                                                   struct cgroup *cgrp);
433         int (*pre_destroy)(struct cgroup_subsys *ss, struct cgroup *cgrp);
434         void (*destroy)(struct cgroup_subsys *ss, struct cgroup *cgrp);
435         int (*can_attach)(struct cgroup_subsys *ss, struct cgroup *cgrp,
436                           struct task_struct *tsk, bool threadgroup);
437         void (*cancel_attach)(struct cgroup_subsys *ss, struct cgroup *cgrp,
438                           struct task_struct *tsk, bool threadgroup);
439         void (*attach)(struct cgroup_subsys *ss, struct cgroup *cgrp,
440                         struct cgroup *old_cgrp, struct task_struct *tsk,
441                         bool threadgroup);
442         void (*fork)(struct cgroup_subsys *ss, struct task_struct *task);
443         void (*exit)(struct cgroup_subsys *ss, struct task_struct *task);
444         int (*populate)(struct cgroup_subsys *ss,
445                         struct cgroup *cgrp);
446         void (*post_clone)(struct cgroup_subsys *ss, struct cgroup *cgrp);
447         void (*bind)(struct cgroup_subsys *ss, struct cgroup *root);
448
449         int subsys_id;
450         int active;
451         int disabled;
452         int early_init;
453         /*
454          * True if this subsys uses ID. ID is not available before cgroup_init()
455          * (not available in early_init time.)
456          */
457         bool use_id;
458 #define MAX_CGROUP_TYPE_NAMELEN 32
459         const char *name;
460
461         /*
462          * Protects sibling/children links of cgroups in this
463          * hierarchy, plus protects which hierarchy (or none) the
464          * subsystem is a part of (i.e. root/sibling).  To avoid
465          * potential deadlocks, the following operations should not be
466          * undertaken while holding any hierarchy_mutex:
467          *
468          * - allocating memory
469          * - initiating hotplug events
470          */
471         struct mutex hierarchy_mutex;
472         struct lock_class_key subsys_key;
473
474         /*
475          * Link to parent, and list entry in parent's children.
476          * Protected by this->hierarchy_mutex and cgroup_lock()
477          */
478         struct cgroupfs_root *root;
479         struct list_head sibling;
480         /* used when use_id == true */
481         struct idr idr;
482         spinlock_t id_lock;
483 };
484
485 #define SUBSYS(_x) extern struct cgroup_subsys _x ## _subsys;
486 #include <linux/cgroup_subsys.h>
487 #undef SUBSYS
488
489 static inline struct cgroup_subsys_state *cgroup_subsys_state(
490         struct cgroup *cgrp, int subsys_id)
491 {
492         return cgrp->subsys[subsys_id];
493 }
494
495 static inline struct cgroup_subsys_state *task_subsys_state(
496         struct task_struct *task, int subsys_id)
497 {
498         return rcu_dereference_check(task->cgroups->subsys[subsys_id],
499                                      rcu_read_lock_held() ||
500                                      cgroup_lock_is_held());
501 }
502
503 static inline struct cgroup* task_cgroup(struct task_struct *task,
504                                                int subsys_id)
505 {
506         return task_subsys_state(task, subsys_id)->cgroup;
507 }
508
509 int cgroup_clone(struct task_struct *tsk, struct cgroup_subsys *ss,
510                                                         char *nodename);
511
512 /* A cgroup_iter should be treated as an opaque object */
513 struct cgroup_iter {
514         struct list_head *cg_link;
515         struct list_head *task;
516 };
517
518 /*
519  * To iterate across the tasks in a cgroup:
520  *
521  * 1) call cgroup_iter_start to intialize an iterator
522  *
523  * 2) call cgroup_iter_next() to retrieve member tasks until it
524  *    returns NULL or until you want to end the iteration
525  *
526  * 3) call cgroup_iter_end() to destroy the iterator.
527  *
528  * Or, call cgroup_scan_tasks() to iterate through every task in a
529  * cgroup - cgroup_scan_tasks() holds the css_set_lock when calling
530  * the test_task() callback, but not while calling the process_task()
531  * callback.
532  */
533 void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it);
534 struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
535                                         struct cgroup_iter *it);
536 void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it);
537 int cgroup_scan_tasks(struct cgroup_scanner *scan);
538 int cgroup_attach_task(struct cgroup *, struct task_struct *);
539
540 /*
541  * CSS ID is ID for cgroup_subsys_state structs under subsys. This only works
542  * if cgroup_subsys.use_id == true. It can be used for looking up and scanning.
543  * CSS ID is assigned at cgroup allocation (create) automatically
544  * and removed when subsys calls free_css_id() function. This is because
545  * the lifetime of cgroup_subsys_state is subsys's matter.
546  *
547  * Looking up and scanning function should be called under rcu_read_lock().
548  * Taking cgroup_mutex()/hierarchy_mutex() is not necessary for following calls.
549  * But the css returned by this routine can be "not populated yet" or "being
550  * destroyed". The caller should check css and cgroup's status.
551  */
552
553 /*
554  * Typically Called at ->destroy(), or somewhere the subsys frees
555  * cgroup_subsys_state.
556  */
557 void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css);
558
559 /* Find a cgroup_subsys_state which has given ID */
560
561 struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id);
562
563 /*
564  * Get a cgroup whose id is greater than or equal to id under tree of root.
565  * Returning a cgroup_subsys_state or NULL.
566  */
567 struct cgroup_subsys_state *css_get_next(struct cgroup_subsys *ss, int id,
568                 struct cgroup_subsys_state *root, int *foundid);
569
570 /* Returns true if root is ancestor of cg */
571 bool css_is_ancestor(struct cgroup_subsys_state *cg,
572                      const struct cgroup_subsys_state *root);
573
574 /* Get id and depth of css */
575 unsigned short css_id(struct cgroup_subsys_state *css);
576 unsigned short css_depth(struct cgroup_subsys_state *css);
577
578 #else /* !CONFIG_CGROUPS */
579
580 static inline int cgroup_init_early(void) { return 0; }
581 static inline int cgroup_init(void) { return 0; }
582 static inline void cgroup_fork(struct task_struct *p) {}
583 static inline void cgroup_fork_callbacks(struct task_struct *p) {}
584 static inline void cgroup_post_fork(struct task_struct *p) {}
585 static inline void cgroup_exit(struct task_struct *p, int callbacks) {}
586
587 static inline void cgroup_lock(void) {}
588 static inline void cgroup_unlock(void) {}
589 static inline int cgroupstats_build(struct cgroupstats *stats,
590                                         struct dentry *dentry)
591 {
592         return -EINVAL;
593 }
594
595 #endif /* !CONFIG_CGROUPS */
596
597 #endif /* _LINUX_CGROUP_H */