]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - include/linux/cgroup.h
Task Control Groups: add fork()/exit() hooks
[linux-2.6-omap-h63xx.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/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_exit(struct task_struct *p, int run_callbacks);
31
32 /* Per-subsystem/per-cgroup state maintained by the system. */
33 struct cgroup_subsys_state {
34         /* The cgroup that this subsystem is attached to. Useful
35          * for subsystems that want to know about the cgroup
36          * hierarchy structure */
37         struct cgroup *cgroup;
38
39         /* State maintained by the cgroup system to allow
40          * subsystems to be "busy". Should be accessed via css_get()
41          * and css_put() */
42
43         atomic_t refcnt;
44
45         unsigned long flags;
46 };
47
48 /* bits in struct cgroup_subsys_state flags field */
49 enum {
50         CSS_ROOT, /* This CSS is the root of the subsystem */
51 };
52
53 /*
54  * Call css_get() to hold a reference on the cgroup;
55  *
56  */
57
58 static inline void css_get(struct cgroup_subsys_state *css)
59 {
60         /* We don't need to reference count the root state */
61         if (!test_bit(CSS_ROOT, &css->flags))
62                 atomic_inc(&css->refcnt);
63 }
64 /*
65  * css_put() should be called to release a reference taken by
66  * css_get()
67  */
68
69 static inline void css_put(struct cgroup_subsys_state *css)
70 {
71         if (!test_bit(CSS_ROOT, &css->flags))
72                 atomic_dec(&css->refcnt);
73 }
74
75 struct cgroup {
76         unsigned long flags;            /* "unsigned long" so bitops work */
77
78         /* count users of this cgroup. >0 means busy, but doesn't
79          * necessarily indicate the number of tasks in the
80          * cgroup */
81         atomic_t count;
82
83         /*
84          * We link our 'sibling' struct into our parent's 'children'.
85          * Our children link their 'sibling' into our 'children'.
86          */
87         struct list_head sibling;       /* my parent's children */
88         struct list_head children;      /* my children */
89
90         struct cgroup *parent;  /* my parent */
91         struct dentry *dentry;          /* cgroup fs entry */
92
93         /* Private pointers for each registered subsystem */
94         struct cgroup_subsys_state *subsys[CGROUP_SUBSYS_COUNT];
95
96         struct cgroupfs_root *root;
97         struct cgroup *top_cgroup;
98 };
99
100 /* struct cftype:
101  *
102  * The files in the cgroup filesystem mostly have a very simple read/write
103  * handling, some common function will take care of it. Nevertheless some cases
104  * (read tasks) are special and therefore I define this structure for every
105  * kind of file.
106  *
107  *
108  * When reading/writing to a file:
109  *      - the cgroup to use in file->f_dentry->d_parent->d_fsdata
110  *      - the 'cftype' of the file is file->f_dentry->d_fsdata
111  */
112
113 #define MAX_CFTYPE_NAME 64
114 struct cftype {
115         /* By convention, the name should begin with the name of the
116          * subsystem, followed by a period */
117         char name[MAX_CFTYPE_NAME];
118         int private;
119         int (*open) (struct inode *inode, struct file *file);
120         ssize_t (*read) (struct cgroup *cont, struct cftype *cft,
121                          struct file *file,
122                          char __user *buf, size_t nbytes, loff_t *ppos);
123         /*
124          * read_uint() is a shortcut for the common case of returning a
125          * single integer. Use it in place of read()
126          */
127         u64 (*read_uint) (struct cgroup *cont, struct cftype *cft);
128         ssize_t (*write) (struct cgroup *cont, struct cftype *cft,
129                           struct file *file,
130                           const char __user *buf, size_t nbytes, loff_t *ppos);
131
132         /*
133          * write_uint() is a shortcut for the common case of accepting
134          * a single integer (as parsed by simple_strtoull) from
135          * userspace. Use in place of write(); return 0 or error.
136          */
137         int (*write_uint) (struct cgroup *cont, struct cftype *cft, u64 val);
138
139         int (*release) (struct inode *inode, struct file *file);
140 };
141
142 /* Add a new file to the given cgroup directory. Should only be
143  * called by subsystems from within a populate() method */
144 int cgroup_add_file(struct cgroup *cont, struct cgroup_subsys *subsys,
145                        const struct cftype *cft);
146
147 /* Add a set of new files to the given cgroup directory. Should
148  * only be called by subsystems from within a populate() method */
149 int cgroup_add_files(struct cgroup *cont,
150                         struct cgroup_subsys *subsys,
151                         const struct cftype cft[],
152                         int count);
153
154 int cgroup_is_removed(const struct cgroup *cont);
155
156 int cgroup_path(const struct cgroup *cont, char *buf, int buflen);
157
158 int __cgroup_task_count(const struct cgroup *cont);
159 static inline int cgroup_task_count(const struct cgroup *cont)
160 {
161         int task_count;
162         rcu_read_lock();
163         task_count = __cgroup_task_count(cont);
164         rcu_read_unlock();
165         return task_count;
166 }
167
168 /* Return true if the cgroup is a descendant of the current cgroup */
169 int cgroup_is_descendant(const struct cgroup *cont);
170
171 /* Control Group subsystem type. See Documentation/cgroups.txt for details */
172
173 struct cgroup_subsys {
174         struct cgroup_subsys_state *(*create)(struct cgroup_subsys *ss,
175                                                   struct cgroup *cont);
176         void (*destroy)(struct cgroup_subsys *ss, struct cgroup *cont);
177         int (*can_attach)(struct cgroup_subsys *ss,
178                           struct cgroup *cont, struct task_struct *tsk);
179         void (*attach)(struct cgroup_subsys *ss, struct cgroup *cont,
180                         struct cgroup *old_cont, struct task_struct *tsk);
181         void (*fork)(struct cgroup_subsys *ss, struct task_struct *task);
182         void (*exit)(struct cgroup_subsys *ss, struct task_struct *task);
183         int (*populate)(struct cgroup_subsys *ss,
184                         struct cgroup *cont);
185         void (*bind)(struct cgroup_subsys *ss, struct cgroup *root);
186         int subsys_id;
187         int active;
188         int early_init;
189 #define MAX_CGROUP_TYPE_NAMELEN 32
190         const char *name;
191
192         /* Protected by RCU */
193         struct cgroupfs_root *root;
194
195         struct list_head sibling;
196
197         void *private;
198 };
199
200 #define SUBSYS(_x) extern struct cgroup_subsys _x ## _subsys;
201 #include <linux/cgroup_subsys.h>
202 #undef SUBSYS
203
204 static inline struct cgroup_subsys_state *cgroup_subsys_state(
205         struct cgroup *cont, int subsys_id)
206 {
207         return cont->subsys[subsys_id];
208 }
209
210 static inline struct cgroup_subsys_state *task_subsys_state(
211         struct task_struct *task, int subsys_id)
212 {
213         return rcu_dereference(task->cgroups.subsys[subsys_id]);
214 }
215
216 static inline struct cgroup* task_cgroup(struct task_struct *task,
217                                                int subsys_id)
218 {
219         return task_subsys_state(task, subsys_id)->cgroup;
220 }
221
222 int cgroup_path(const struct cgroup *cont, char *buf, int buflen);
223
224 #else /* !CONFIG_CGROUPS */
225
226 static inline int cgroup_init_early(void) { return 0; }
227 static inline int cgroup_init(void) { return 0; }
228 static inline void cgroup_init_smp(void) {}
229 static inline void cgroup_fork(struct task_struct *p) {}
230 static inline void cgroup_fork_callbacks(struct task_struct *p) {}
231 static inline void cgroup_exit(struct task_struct *p, int callbacks) {}
232
233 static inline void cgroup_lock(void) {}
234 static inline void cgroup_unlock(void) {}
235
236 #endif /* !CONFIG_CGROUPS */
237
238 #endif /* _LINUX_CGROUP_H */