blob: d8e92223a79c5bc126235f86ede207a4a8cb9850 [file] [log] [blame]
Paul Menageddbcc7e2007-10-18 23:39:30 -07001#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>
Balbir Singh846c7bb2007-10-18 23:39:44 -070016#include <linux/cgroupstats.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070017
18#ifdef CONFIG_CGROUPS
19
20struct cgroupfs_root;
21struct cgroup_subsys;
22struct inode;
23
24extern int cgroup_init_early(void);
25extern int cgroup_init(void);
26extern void cgroup_init_smp(void);
27extern void cgroup_lock(void);
28extern void cgroup_unlock(void);
Paul Menageb4f48b62007-10-18 23:39:33 -070029extern void cgroup_fork(struct task_struct *p);
30extern void cgroup_fork_callbacks(struct task_struct *p);
Paul Menage817929e2007-10-18 23:39:36 -070031extern void cgroup_post_fork(struct task_struct *p);
Paul Menageb4f48b62007-10-18 23:39:33 -070032extern void cgroup_exit(struct task_struct *p, int run_callbacks);
Balbir Singh846c7bb2007-10-18 23:39:44 -070033extern int cgroupstats_build(struct cgroupstats *stats,
34 struct dentry *dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -070035
Paul Menagea4243162007-10-18 23:39:35 -070036extern struct file_operations proc_cgroup_operations;
37
Paul Menage817929e2007-10-18 23:39:36 -070038/* Define the enumeration of all cgroup subsystems */
39#define SUBSYS(_x) _x ## _subsys_id,
40enum cgroup_subsys_id {
41#include <linux/cgroup_subsys.h>
42 CGROUP_SUBSYS_COUNT
43};
44#undef SUBSYS
45
Paul Menageddbcc7e2007-10-18 23:39:30 -070046/* Per-subsystem/per-cgroup state maintained by the system. */
47struct cgroup_subsys_state {
48 /* The cgroup that this subsystem is attached to. Useful
49 * for subsystems that want to know about the cgroup
50 * hierarchy structure */
51 struct cgroup *cgroup;
52
53 /* State maintained by the cgroup system to allow
54 * subsystems to be "busy". Should be accessed via css_get()
55 * and css_put() */
56
57 atomic_t refcnt;
58
59 unsigned long flags;
60};
61
62/* bits in struct cgroup_subsys_state flags field */
63enum {
64 CSS_ROOT, /* This CSS is the root of the subsystem */
65};
66
67/*
68 * Call css_get() to hold a reference on the cgroup;
69 *
70 */
71
72static inline void css_get(struct cgroup_subsys_state *css)
73{
74 /* We don't need to reference count the root state */
75 if (!test_bit(CSS_ROOT, &css->flags))
76 atomic_inc(&css->refcnt);
77}
78/*
79 * css_put() should be called to release a reference taken by
80 * css_get()
81 */
82
Paul Menage81a6a5c2007-10-18 23:39:38 -070083extern void __css_put(struct cgroup_subsys_state *css);
Paul Menageddbcc7e2007-10-18 23:39:30 -070084static inline void css_put(struct cgroup_subsys_state *css)
85{
86 if (!test_bit(CSS_ROOT, &css->flags))
Paul Menage81a6a5c2007-10-18 23:39:38 -070087 __css_put(css);
Paul Menageddbcc7e2007-10-18 23:39:30 -070088}
89
90struct cgroup {
91 unsigned long flags; /* "unsigned long" so bitops work */
92
93 /* count users of this cgroup. >0 means busy, but doesn't
94 * necessarily indicate the number of tasks in the
95 * cgroup */
96 atomic_t count;
97
98 /*
99 * We link our 'sibling' struct into our parent's 'children'.
100 * Our children link their 'sibling' into our 'children'.
101 */
102 struct list_head sibling; /* my parent's children */
103 struct list_head children; /* my children */
104
105 struct cgroup *parent; /* my parent */
106 struct dentry *dentry; /* cgroup fs entry */
107
108 /* Private pointers for each registered subsystem */
109 struct cgroup_subsys_state *subsys[CGROUP_SUBSYS_COUNT];
110
111 struct cgroupfs_root *root;
112 struct cgroup *top_cgroup;
Paul Menage817929e2007-10-18 23:39:36 -0700113
114 /*
115 * List of cg_cgroup_links pointing at css_sets with
116 * tasks in this cgroup. Protected by css_set_lock
117 */
118 struct list_head css_sets;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700119
120 /*
121 * Linked list running through all cgroups that can
122 * potentially be reaped by the release agent. Protected by
123 * release_list_lock
124 */
125 struct list_head release_list;
Paul Menage817929e2007-10-18 23:39:36 -0700126};
127
128/* A css_set is a structure holding pointers to a set of
129 * cgroup_subsys_state objects. This saves space in the task struct
130 * object and speeds up fork()/exit(), since a single inc/dec and a
131 * list_add()/del() can bump the reference count on the entire
132 * cgroup set for a task.
133 */
134
135struct css_set {
136
137 /* Reference count */
138 struct kref ref;
139
140 /*
141 * List running through all cgroup groups. Protected by
142 * css_set_lock
143 */
144 struct list_head list;
145
146 /*
147 * List running through all tasks using this cgroup
148 * group. Protected by css_set_lock
149 */
150 struct list_head tasks;
151
152 /*
153 * List of cg_cgroup_link objects on link chains from
154 * cgroups referenced from this css_set. Protected by
155 * css_set_lock
156 */
157 struct list_head cg_links;
158
159 /*
160 * Set of subsystem states, one for each subsystem. This array
161 * is immutable after creation apart from the init_css_set
162 * during subsystem registration (at boot time).
163 */
164 struct cgroup_subsys_state *subsys[CGROUP_SUBSYS_COUNT];
165
Paul Menageddbcc7e2007-10-18 23:39:30 -0700166};
167
168/* struct cftype:
169 *
170 * The files in the cgroup filesystem mostly have a very simple read/write
171 * handling, some common function will take care of it. Nevertheless some cases
172 * (read tasks) are special and therefore I define this structure for every
173 * kind of file.
174 *
175 *
176 * When reading/writing to a file:
177 * - the cgroup to use in file->f_dentry->d_parent->d_fsdata
178 * - the 'cftype' of the file is file->f_dentry->d_fsdata
179 */
180
181#define MAX_CFTYPE_NAME 64
182struct cftype {
183 /* By convention, the name should begin with the name of the
184 * subsystem, followed by a period */
185 char name[MAX_CFTYPE_NAME];
186 int private;
187 int (*open) (struct inode *inode, struct file *file);
188 ssize_t (*read) (struct cgroup *cont, struct cftype *cft,
189 struct file *file,
190 char __user *buf, size_t nbytes, loff_t *ppos);
191 /*
192 * read_uint() is a shortcut for the common case of returning a
193 * single integer. Use it in place of read()
194 */
195 u64 (*read_uint) (struct cgroup *cont, struct cftype *cft);
196 ssize_t (*write) (struct cgroup *cont, struct cftype *cft,
197 struct file *file,
198 const char __user *buf, size_t nbytes, loff_t *ppos);
Paul Menage355e0c42007-10-18 23:39:33 -0700199
200 /*
201 * write_uint() is a shortcut for the common case of accepting
202 * a single integer (as parsed by simple_strtoull) from
203 * userspace. Use in place of write(); return 0 or error.
204 */
205 int (*write_uint) (struct cgroup *cont, struct cftype *cft, u64 val);
206
Paul Menageddbcc7e2007-10-18 23:39:30 -0700207 int (*release) (struct inode *inode, struct file *file);
208};
209
210/* Add a new file to the given cgroup directory. Should only be
211 * called by subsystems from within a populate() method */
212int cgroup_add_file(struct cgroup *cont, struct cgroup_subsys *subsys,
213 const struct cftype *cft);
214
215/* Add a set of new files to the given cgroup directory. Should
216 * only be called by subsystems from within a populate() method */
217int cgroup_add_files(struct cgroup *cont,
218 struct cgroup_subsys *subsys,
219 const struct cftype cft[],
220 int count);
221
222int cgroup_is_removed(const struct cgroup *cont);
223
224int cgroup_path(const struct cgroup *cont, char *buf, int buflen);
225
Paul Menage817929e2007-10-18 23:39:36 -0700226int cgroup_task_count(const struct cgroup *cont);
Paul Menagebbcb81d2007-10-18 23:39:32 -0700227
Paul Menageddbcc7e2007-10-18 23:39:30 -0700228/* Return true if the cgroup is a descendant of the current cgroup */
229int cgroup_is_descendant(const struct cgroup *cont);
230
231/* Control Group subsystem type. See Documentation/cgroups.txt for details */
232
233struct cgroup_subsys {
234 struct cgroup_subsys_state *(*create)(struct cgroup_subsys *ss,
235 struct cgroup *cont);
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800236 void (*pre_destroy)(struct cgroup_subsys *ss, struct cgroup *cont);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700237 void (*destroy)(struct cgroup_subsys *ss, struct cgroup *cont);
238 int (*can_attach)(struct cgroup_subsys *ss,
239 struct cgroup *cont, struct task_struct *tsk);
240 void (*attach)(struct cgroup_subsys *ss, struct cgroup *cont,
241 struct cgroup *old_cont, struct task_struct *tsk);
242 void (*fork)(struct cgroup_subsys *ss, struct task_struct *task);
243 void (*exit)(struct cgroup_subsys *ss, struct task_struct *task);
244 int (*populate)(struct cgroup_subsys *ss,
245 struct cgroup *cont);
Paul Menage697f4162007-10-18 23:39:34 -0700246 void (*post_clone)(struct cgroup_subsys *ss, struct cgroup *cont);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700247 void (*bind)(struct cgroup_subsys *ss, struct cgroup *root);
248 int subsys_id;
249 int active;
250 int early_init;
251#define MAX_CGROUP_TYPE_NAMELEN 32
252 const char *name;
253
254 /* Protected by RCU */
255 struct cgroupfs_root *root;
256
257 struct list_head sibling;
258
259 void *private;
260};
261
262#define SUBSYS(_x) extern struct cgroup_subsys _x ## _subsys;
263#include <linux/cgroup_subsys.h>
264#undef SUBSYS
265
266static inline struct cgroup_subsys_state *cgroup_subsys_state(
267 struct cgroup *cont, int subsys_id)
268{
269 return cont->subsys[subsys_id];
270}
271
272static inline struct cgroup_subsys_state *task_subsys_state(
273 struct task_struct *task, int subsys_id)
274{
Paul Menage817929e2007-10-18 23:39:36 -0700275 return rcu_dereference(task->cgroups->subsys[subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700276}
277
278static inline struct cgroup* task_cgroup(struct task_struct *task,
279 int subsys_id)
280{
281 return task_subsys_state(task, subsys_id)->cgroup;
282}
283
284int cgroup_path(const struct cgroup *cont, char *buf, int buflen);
285
Paul Menage697f4162007-10-18 23:39:34 -0700286int cgroup_clone(struct task_struct *tsk, struct cgroup_subsys *ss);
287
Paul Menage817929e2007-10-18 23:39:36 -0700288/* A cgroup_iter should be treated as an opaque object */
289struct cgroup_iter {
290 struct list_head *cg_link;
291 struct list_head *task;
292};
293
294/* To iterate across the tasks in a cgroup:
295 *
296 * 1) call cgroup_iter_start to intialize an iterator
297 *
298 * 2) call cgroup_iter_next() to retrieve member tasks until it
299 * returns NULL or until you want to end the iteration
300 *
301 * 3) call cgroup_iter_end() to destroy the iterator.
302 */
303void cgroup_iter_start(struct cgroup *cont, struct cgroup_iter *it);
304struct task_struct *cgroup_iter_next(struct cgroup *cont,
305 struct cgroup_iter *it);
306void cgroup_iter_end(struct cgroup *cont, struct cgroup_iter *it);
307
Paul Menageddbcc7e2007-10-18 23:39:30 -0700308#else /* !CONFIG_CGROUPS */
309
310static inline int cgroup_init_early(void) { return 0; }
311static inline int cgroup_init(void) { return 0; }
312static inline void cgroup_init_smp(void) {}
Paul Menageb4f48b62007-10-18 23:39:33 -0700313static inline void cgroup_fork(struct task_struct *p) {}
314static inline void cgroup_fork_callbacks(struct task_struct *p) {}
Paul Menage817929e2007-10-18 23:39:36 -0700315static inline void cgroup_post_fork(struct task_struct *p) {}
Paul Menageb4f48b62007-10-18 23:39:33 -0700316static inline void cgroup_exit(struct task_struct *p, int callbacks) {}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700317
318static inline void cgroup_lock(void) {}
319static inline void cgroup_unlock(void) {}
Balbir Singh846c7bb2007-10-18 23:39:44 -0700320static inline int cgroupstats_build(struct cgroupstats *stats,
321 struct dentry *dentry)
322{
323 return -EINVAL;
324}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700325
326#endif /* !CONFIG_CGROUPS */
327
328#endif /* _LINUX_CGROUP_H */