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 extern void __css_put(struct cgroup_subsys_state *css);
81 static inline void css_put(struct cgroup_subsys_state *css)
83 if (!test_bit(CSS_ROOT, &css->flags))
88 unsigned long flags; /* "unsigned long" so bitops work */
90 /* count users of this cgroup. >0 means busy, but doesn't
91 * necessarily indicate the number of tasks in the
96 * We link our 'sibling' struct into our parent's 'children'.
97 * Our children link their 'sibling' into our 'children'.
99 struct list_head sibling; /* my parent's children */
100 struct list_head children; /* my children */
102 struct cgroup *parent; /* my parent */
103 struct dentry *dentry; /* cgroup fs entry */
105 /* Private pointers for each registered subsystem */
106 struct cgroup_subsys_state *subsys[CGROUP_SUBSYS_COUNT];
108 struct cgroupfs_root *root;
109 struct cgroup *top_cgroup;
112 * List of cg_cgroup_links pointing at css_sets with
113 * tasks in this cgroup. Protected by css_set_lock
115 struct list_head css_sets;
118 * Linked list running through all cgroups that can
119 * potentially be reaped by the release agent. Protected by
122 struct list_head release_list;
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.
134 /* Reference count */
138 * List running through all cgroup groups. Protected by
141 struct list_head list;
144 * List running through all tasks using this cgroup
145 * group. Protected by css_set_lock
147 struct list_head tasks;
150 * List of cg_cgroup_link objects on link chains from
151 * cgroups referenced from this css_set. Protected by
154 struct list_head cg_links;
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).
161 struct cgroup_subsys_state *subsys[CGROUP_SUBSYS_COUNT];
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
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
178 #define MAX_CFTYPE_NAME 64
180 /* By convention, the name should begin with the name of the
181 * subsystem, followed by a period */
182 char name[MAX_CFTYPE_NAME];
184 int (*open) (struct inode *inode, struct file *file);
185 ssize_t (*read) (struct cgroup *cont, struct cftype *cft,
187 char __user *buf, size_t nbytes, loff_t *ppos);
189 * read_uint() is a shortcut for the common case of returning a
190 * single integer. Use it in place of read()
192 u64 (*read_uint) (struct cgroup *cont, struct cftype *cft);
193 ssize_t (*write) (struct cgroup *cont, struct cftype *cft,
195 const char __user *buf, size_t nbytes, loff_t *ppos);
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.
202 int (*write_uint) (struct cgroup *cont, struct cftype *cft, u64 val);
204 int (*release) (struct inode *inode, struct file *file);
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);
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[],
219 int cgroup_is_removed(const struct cgroup *cont);
221 int cgroup_path(const struct cgroup *cont, char *buf, int buflen);
223 int cgroup_task_count(const struct cgroup *cont);
225 /* Return true if the cgroup is a descendant of the current cgroup */
226 int cgroup_is_descendant(const struct cgroup *cont);
228 /* Control Group subsystem type. See Documentation/cgroups.txt for details */
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);
247 #define MAX_CGROUP_TYPE_NAMELEN 32
250 /* Protected by RCU */
251 struct cgroupfs_root *root;
253 struct list_head sibling;
258 #define SUBSYS(_x) extern struct cgroup_subsys _x ## _subsys;
259 #include <linux/cgroup_subsys.h>
262 static inline struct cgroup_subsys_state *cgroup_subsys_state(
263 struct cgroup *cont, int subsys_id)
265 return cont->subsys[subsys_id];
268 static inline struct cgroup_subsys_state *task_subsys_state(
269 struct task_struct *task, int subsys_id)
271 return rcu_dereference(task->cgroups->subsys[subsys_id]);
274 static inline struct cgroup* task_cgroup(struct task_struct *task,
277 return task_subsys_state(task, subsys_id)->cgroup;
280 int cgroup_path(const struct cgroup *cont, char *buf, int buflen);
282 int cgroup_clone(struct task_struct *tsk, struct cgroup_subsys *ss);
284 /* A cgroup_iter should be treated as an opaque object */
286 struct list_head *cg_link;
287 struct list_head *task;
290 /* To iterate across the tasks in a cgroup:
292 * 1) call cgroup_iter_start to intialize an iterator
294 * 2) call cgroup_iter_next() to retrieve member tasks until it
295 * returns NULL or until you want to end the iteration
297 * 3) call cgroup_iter_end() to destroy the iterator.
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);
304 #else /* !CONFIG_CGROUPS */
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) {}
314 static inline void cgroup_lock(void) {}
315 static inline void cgroup_unlock(void) {}
317 #endif /* !CONFIG_CGROUPS */
319 #endif /* _LINUX_CGROUP_H */