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_exit(struct task_struct *p, int run_callbacks);
32 extern struct file_operations proc_cgroup_operations;
34 /* Per-subsystem/per-cgroup state maintained by the system. */
35 struct cgroup_subsys_state {
36 /* The cgroup that this subsystem is attached to. Useful
37 * for subsystems that want to know about the cgroup
38 * hierarchy structure */
39 struct cgroup *cgroup;
41 /* State maintained by the cgroup system to allow
42 * subsystems to be "busy". Should be accessed via css_get()
50 /* bits in struct cgroup_subsys_state flags field */
52 CSS_ROOT, /* This CSS is the root of the subsystem */
56 * Call css_get() to hold a reference on the cgroup;
60 static inline void css_get(struct cgroup_subsys_state *css)
62 /* We don't need to reference count the root state */
63 if (!test_bit(CSS_ROOT, &css->flags))
64 atomic_inc(&css->refcnt);
67 * css_put() should be called to release a reference taken by
71 static inline void css_put(struct cgroup_subsys_state *css)
73 if (!test_bit(CSS_ROOT, &css->flags))
74 atomic_dec(&css->refcnt);
78 unsigned long flags; /* "unsigned long" so bitops work */
80 /* count users of this cgroup. >0 means busy, but doesn't
81 * necessarily indicate the number of tasks in the
86 * We link our 'sibling' struct into our parent's 'children'.
87 * Our children link their 'sibling' into our 'children'.
89 struct list_head sibling; /* my parent's children */
90 struct list_head children; /* my children */
92 struct cgroup *parent; /* my parent */
93 struct dentry *dentry; /* cgroup fs entry */
95 /* Private pointers for each registered subsystem */
96 struct cgroup_subsys_state *subsys[CGROUP_SUBSYS_COUNT];
98 struct cgroupfs_root *root;
99 struct cgroup *top_cgroup;
104 * The files in the cgroup filesystem mostly have a very simple read/write
105 * handling, some common function will take care of it. Nevertheless some cases
106 * (read tasks) are special and therefore I define this structure for every
110 * When reading/writing to a file:
111 * - the cgroup to use in file->f_dentry->d_parent->d_fsdata
112 * - the 'cftype' of the file is file->f_dentry->d_fsdata
115 #define MAX_CFTYPE_NAME 64
117 /* By convention, the name should begin with the name of the
118 * subsystem, followed by a period */
119 char name[MAX_CFTYPE_NAME];
121 int (*open) (struct inode *inode, struct file *file);
122 ssize_t (*read) (struct cgroup *cont, struct cftype *cft,
124 char __user *buf, size_t nbytes, loff_t *ppos);
126 * read_uint() is a shortcut for the common case of returning a
127 * single integer. Use it in place of read()
129 u64 (*read_uint) (struct cgroup *cont, struct cftype *cft);
130 ssize_t (*write) (struct cgroup *cont, struct cftype *cft,
132 const char __user *buf, size_t nbytes, loff_t *ppos);
135 * write_uint() is a shortcut for the common case of accepting
136 * a single integer (as parsed by simple_strtoull) from
137 * userspace. Use in place of write(); return 0 or error.
139 int (*write_uint) (struct cgroup *cont, struct cftype *cft, u64 val);
141 int (*release) (struct inode *inode, struct file *file);
144 /* Add a new file to the given cgroup directory. Should only be
145 * called by subsystems from within a populate() method */
146 int cgroup_add_file(struct cgroup *cont, struct cgroup_subsys *subsys,
147 const struct cftype *cft);
149 /* Add a set of new files to the given cgroup directory. Should
150 * only be called by subsystems from within a populate() method */
151 int cgroup_add_files(struct cgroup *cont,
152 struct cgroup_subsys *subsys,
153 const struct cftype cft[],
156 int cgroup_is_removed(const struct cgroup *cont);
158 int cgroup_path(const struct cgroup *cont, char *buf, int buflen);
160 int __cgroup_task_count(const struct cgroup *cont);
161 static inline int cgroup_task_count(const struct cgroup *cont)
165 task_count = __cgroup_task_count(cont);
170 /* Return true if the cgroup is a descendant of the current cgroup */
171 int cgroup_is_descendant(const struct cgroup *cont);
173 /* Control Group subsystem type. See Documentation/cgroups.txt for details */
175 struct cgroup_subsys {
176 struct cgroup_subsys_state *(*create)(struct cgroup_subsys *ss,
177 struct cgroup *cont);
178 void (*destroy)(struct cgroup_subsys *ss, struct cgroup *cont);
179 int (*can_attach)(struct cgroup_subsys *ss,
180 struct cgroup *cont, struct task_struct *tsk);
181 void (*attach)(struct cgroup_subsys *ss, struct cgroup *cont,
182 struct cgroup *old_cont, struct task_struct *tsk);
183 void (*fork)(struct cgroup_subsys *ss, struct task_struct *task);
184 void (*exit)(struct cgroup_subsys *ss, struct task_struct *task);
185 int (*populate)(struct cgroup_subsys *ss,
186 struct cgroup *cont);
187 void (*post_clone)(struct cgroup_subsys *ss, struct cgroup *cont);
188 void (*bind)(struct cgroup_subsys *ss, struct cgroup *root);
192 #define MAX_CGROUP_TYPE_NAMELEN 32
195 /* Protected by RCU */
196 struct cgroupfs_root *root;
198 struct list_head sibling;
203 #define SUBSYS(_x) extern struct cgroup_subsys _x ## _subsys;
204 #include <linux/cgroup_subsys.h>
207 static inline struct cgroup_subsys_state *cgroup_subsys_state(
208 struct cgroup *cont, int subsys_id)
210 return cont->subsys[subsys_id];
213 static inline struct cgroup_subsys_state *task_subsys_state(
214 struct task_struct *task, int subsys_id)
216 return rcu_dereference(task->cgroups.subsys[subsys_id]);
219 static inline struct cgroup* task_cgroup(struct task_struct *task,
222 return task_subsys_state(task, subsys_id)->cgroup;
225 int cgroup_path(const struct cgroup *cont, char *buf, int buflen);
227 int cgroup_clone(struct task_struct *tsk, struct cgroup_subsys *ss);
229 #else /* !CONFIG_CGROUPS */
231 static inline int cgroup_init_early(void) { return 0; }
232 static inline int cgroup_init(void) { return 0; }
233 static inline void cgroup_init_smp(void) {}
234 static inline void cgroup_fork(struct task_struct *p) {}
235 static inline void cgroup_fork_callbacks(struct task_struct *p) {}
236 static inline void cgroup_exit(struct task_struct *p, int callbacks) {}
238 static inline void cgroup_lock(void) {}
239 static inline void cgroup_unlock(void) {}
241 #endif /* !CONFIG_CGROUPS */
243 #endif /* _LINUX_CGROUP_H */