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