1 #ifndef _LINUX_CGROUP_H
2 #define _LINUX_CGROUP_H
6 * Copyright (C) 2003 BULL SA
7 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
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>
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);
33 extern struct file_operations proc_cgroup_operations;
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>
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;
50 /* State maintained by the cgroup system to allow
51 * subsystems to be "busy". Should be accessed via css_get()
59 /* bits in struct cgroup_subsys_state flags field */
61 CSS_ROOT, /* This CSS is the root of the subsystem */
65 * Call css_get() to hold a reference on the cgroup;
69 static inline void css_get(struct cgroup_subsys_state *css)
71 /* We don't need to reference count the root state */
72 if (!test_bit(CSS_ROOT, &css->flags))
73 atomic_inc(&css->refcnt);
76 * css_put() should be called to release a reference taken by
80 static inline void css_put(struct cgroup_subsys_state *css)
82 if (!test_bit(CSS_ROOT, &css->flags))
83 atomic_dec(&css->refcnt);
87 unsigned long flags; /* "unsigned long" so bitops work */
89 /* count users of this cgroup. >0 means busy, but doesn't
90 * necessarily indicate the number of tasks in the
95 * We link our 'sibling' struct into our parent's 'children'.
96 * Our children link their 'sibling' into our 'children'.
98 struct list_head sibling; /* my parent's children */
99 struct list_head children; /* my children */
101 struct cgroup *parent; /* my parent */
102 struct dentry *dentry; /* cgroup fs entry */
104 /* Private pointers for each registered subsystem */
105 struct cgroup_subsys_state *subsys[CGROUP_SUBSYS_COUNT];
107 struct cgroupfs_root *root;
108 struct cgroup *top_cgroup;
111 * List of cg_cgroup_links pointing at css_sets with
112 * tasks in this cgroup. Protected by css_set_lock
114 struct list_head css_sets;
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.
126 /* Reference count */
130 * List running through all cgroup groups. Protected by
133 struct list_head list;
136 * List running through all tasks using this cgroup
137 * group. Protected by css_set_lock
139 struct list_head tasks;
142 * List of cg_cgroup_link objects on link chains from
143 * cgroups referenced from this css_set. Protected by
146 struct list_head cg_links;
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).
153 struct cgroup_subsys_state *subsys[CGROUP_SUBSYS_COUNT];
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
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
170 #define MAX_CFTYPE_NAME 64
172 /* By convention, the name should begin with the name of the
173 * subsystem, followed by a period */
174 char name[MAX_CFTYPE_NAME];
176 int (*open) (struct inode *inode, struct file *file);
177 ssize_t (*read) (struct cgroup *cont, struct cftype *cft,
179 char __user *buf, size_t nbytes, loff_t *ppos);
181 * read_uint() is a shortcut for the common case of returning a
182 * single integer. Use it in place of read()
184 u64 (*read_uint) (struct cgroup *cont, struct cftype *cft);
185 ssize_t (*write) (struct cgroup *cont, struct cftype *cft,
187 const char __user *buf, size_t nbytes, loff_t *ppos);
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.
194 int (*write_uint) (struct cgroup *cont, struct cftype *cft, u64 val);
196 int (*release) (struct inode *inode, struct file *file);
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);
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[],
211 int cgroup_is_removed(const struct cgroup *cont);
213 int cgroup_path(const struct cgroup *cont, char *buf, int buflen);
215 int cgroup_task_count(const struct cgroup *cont);
217 /* Return true if the cgroup is a descendant of the current cgroup */
218 int cgroup_is_descendant(const struct cgroup *cont);
220 /* Control Group subsystem type. See Documentation/cgroups.txt for details */
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);
239 #define MAX_CGROUP_TYPE_NAMELEN 32
242 /* Protected by RCU */
243 struct cgroupfs_root *root;
245 struct list_head sibling;
250 #define SUBSYS(_x) extern struct cgroup_subsys _x ## _subsys;
251 #include <linux/cgroup_subsys.h>
254 static inline struct cgroup_subsys_state *cgroup_subsys_state(
255 struct cgroup *cont, int subsys_id)
257 return cont->subsys[subsys_id];
260 static inline struct cgroup_subsys_state *task_subsys_state(
261 struct task_struct *task, int subsys_id)
263 return rcu_dereference(task->cgroups->subsys[subsys_id]);
266 static inline struct cgroup* task_cgroup(struct task_struct *task,
269 return task_subsys_state(task, subsys_id)->cgroup;
272 int cgroup_path(const struct cgroup *cont, char *buf, int buflen);
274 int cgroup_clone(struct task_struct *tsk, struct cgroup_subsys *ss);
276 /* A cgroup_iter should be treated as an opaque object */
278 struct list_head *cg_link;
279 struct list_head *task;
282 /* To iterate across the tasks in a cgroup:
284 * 1) call cgroup_iter_start to intialize an iterator
286 * 2) call cgroup_iter_next() to retrieve member tasks until it
287 * returns NULL or until you want to end the iteration
289 * 3) call cgroup_iter_end() to destroy the iterator.
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);
297 #else /* !CONFIG_CGROUPS */
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) {}
307 static inline void cgroup_lock(void) {}
308 static inline void cgroup_unlock(void) {}
310 #endif /* !CONFIG_CGROUPS */
312 #endif /* _LINUX_CGROUP_H */