Commit | Line | Data |
---|---|---|
ddbcc7e8 PM |
1 | #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> | |
ddbcc7e8 PM |
12 | #include <linux/cpumask.h> |
13 | #include <linux/nodemask.h> | |
14 | #include <linux/rcupdate.h> | |
846c7bb0 | 15 | #include <linux/cgroupstats.h> |
31a7df01 | 16 | #include <linux/prio_heap.h> |
cc31edce | 17 | #include <linux/rwsem.h> |
ddbcc7e8 PM |
18 | |
19 | #ifdef CONFIG_CGROUPS | |
20 | ||
21 | struct cgroupfs_root; | |
22 | struct cgroup_subsys; | |
23 | struct inode; | |
84eea842 | 24 | struct cgroup; |
ddbcc7e8 PM |
25 | |
26 | extern int cgroup_init_early(void); | |
27 | extern int cgroup_init(void); | |
ddbcc7e8 | 28 | extern void cgroup_lock(void); |
84eea842 | 29 | extern bool cgroup_lock_live_group(struct cgroup *cgrp); |
ddbcc7e8 | 30 | extern void cgroup_unlock(void); |
b4f48b63 PM |
31 | extern void cgroup_fork(struct task_struct *p); |
32 | extern void cgroup_fork_callbacks(struct task_struct *p); | |
817929ec | 33 | extern void cgroup_post_fork(struct task_struct *p); |
b4f48b63 | 34 | extern void cgroup_exit(struct task_struct *p, int run_callbacks); |
846c7bb0 BS |
35 | extern int cgroupstats_build(struct cgroupstats *stats, |
36 | struct dentry *dentry); | |
ddbcc7e8 | 37 | |
a424316c PM |
38 | extern struct file_operations proc_cgroup_operations; |
39 | ||
817929ec PM |
40 | /* Define the enumeration of all cgroup subsystems */ |
41 | #define SUBSYS(_x) _x ## _subsys_id, | |
42 | enum cgroup_subsys_id { | |
43 | #include <linux/cgroup_subsys.h> | |
44 | CGROUP_SUBSYS_COUNT | |
45 | }; | |
46 | #undef SUBSYS | |
47 | ||
ddbcc7e8 PM |
48 | /* Per-subsystem/per-cgroup state maintained by the system. */ |
49 | struct cgroup_subsys_state { | |
d20a390a PM |
50 | /* |
51 | * The cgroup that this subsystem is attached to. Useful | |
ddbcc7e8 | 52 | * for subsystems that want to know about the cgroup |
d20a390a PM |
53 | * hierarchy structure |
54 | */ | |
ddbcc7e8 PM |
55 | struct cgroup *cgroup; |
56 | ||
d20a390a PM |
57 | /* |
58 | * State maintained by the cgroup system to allow subsystems | |
e7c5ec91 | 59 | * to be "busy". Should be accessed via css_get(), |
d20a390a PM |
60 | * css_tryget() and and css_put(). |
61 | */ | |
ddbcc7e8 PM |
62 | |
63 | atomic_t refcnt; | |
64 | ||
65 | unsigned long flags; | |
66 | }; | |
67 | ||
68 | /* bits in struct cgroup_subsys_state flags field */ | |
69 | enum { | |
70 | CSS_ROOT, /* This CSS is the root of the subsystem */ | |
e7c5ec91 | 71 | CSS_REMOVED, /* This CSS is dead */ |
ddbcc7e8 PM |
72 | }; |
73 | ||
74 | /* | |
e7c5ec91 PM |
75 | * Call css_get() to hold a reference on the css; it can be used |
76 | * for a reference obtained via: | |
77 | * - an existing ref-counted reference to the css | |
78 | * - task->cgroups for a locked task | |
ddbcc7e8 PM |
79 | */ |
80 | ||
81 | static inline void css_get(struct cgroup_subsys_state *css) | |
82 | { | |
83 | /* We don't need to reference count the root state */ | |
84 | if (!test_bit(CSS_ROOT, &css->flags)) | |
85 | atomic_inc(&css->refcnt); | |
86 | } | |
e7c5ec91 PM |
87 | |
88 | static inline bool css_is_removed(struct cgroup_subsys_state *css) | |
89 | { | |
90 | return test_bit(CSS_REMOVED, &css->flags); | |
91 | } | |
92 | ||
93 | /* | |
94 | * Call css_tryget() to take a reference on a css if your existing | |
95 | * (known-valid) reference isn't already ref-counted. Returns false if | |
96 | * the css has been destroyed. | |
97 | */ | |
98 | ||
99 | static inline bool css_tryget(struct cgroup_subsys_state *css) | |
100 | { | |
101 | if (test_bit(CSS_ROOT, &css->flags)) | |
102 | return true; | |
103 | while (!atomic_inc_not_zero(&css->refcnt)) { | |
104 | if (test_bit(CSS_REMOVED, &css->flags)) | |
105 | return false; | |
804b3c28 | 106 | cpu_relax(); |
e7c5ec91 PM |
107 | } |
108 | return true; | |
109 | } | |
110 | ||
ddbcc7e8 PM |
111 | /* |
112 | * css_put() should be called to release a reference taken by | |
e7c5ec91 | 113 | * css_get() or css_tryget() |
ddbcc7e8 PM |
114 | */ |
115 | ||
81a6a5cd | 116 | extern void __css_put(struct cgroup_subsys_state *css); |
ddbcc7e8 PM |
117 | static inline void css_put(struct cgroup_subsys_state *css) |
118 | { | |
119 | if (!test_bit(CSS_ROOT, &css->flags)) | |
81a6a5cd | 120 | __css_put(css); |
ddbcc7e8 PM |
121 | } |
122 | ||
3116f0e3 PM |
123 | /* bits in struct cgroup flags field */ |
124 | enum { | |
125 | /* Control Group is dead */ | |
126 | CGRP_REMOVED, | |
d20a390a PM |
127 | /* |
128 | * Control Group has previously had a child cgroup or a task, | |
129 | * but no longer (only if CGRP_NOTIFY_ON_RELEASE is set) | |
130 | */ | |
3116f0e3 PM |
131 | CGRP_RELEASABLE, |
132 | /* Control Group requires release notifications to userspace */ | |
133 | CGRP_NOTIFY_ON_RELEASE, | |
134 | }; | |
135 | ||
ddbcc7e8 PM |
136 | struct cgroup { |
137 | unsigned long flags; /* "unsigned long" so bitops work */ | |
138 | ||
d20a390a PM |
139 | /* |
140 | * count users of this cgroup. >0 means busy, but doesn't | |
141 | * necessarily indicate the number of tasks in the cgroup | |
142 | */ | |
ddbcc7e8 PM |
143 | atomic_t count; |
144 | ||
145 | /* | |
146 | * We link our 'sibling' struct into our parent's 'children'. | |
147 | * Our children link their 'sibling' into our 'children'. | |
148 | */ | |
149 | struct list_head sibling; /* my parent's children */ | |
150 | struct list_head children; /* my children */ | |
151 | ||
d20a390a | 152 | struct cgroup *parent; /* my parent */ |
a47295e6 | 153 | struct dentry *dentry; /* cgroup fs entry, RCU protected */ |
ddbcc7e8 PM |
154 | |
155 | /* Private pointers for each registered subsystem */ | |
156 | struct cgroup_subsys_state *subsys[CGROUP_SUBSYS_COUNT]; | |
157 | ||
158 | struct cgroupfs_root *root; | |
159 | struct cgroup *top_cgroup; | |
817929ec PM |
160 | |
161 | /* | |
162 | * List of cg_cgroup_links pointing at css_sets with | |
163 | * tasks in this cgroup. Protected by css_set_lock | |
164 | */ | |
165 | struct list_head css_sets; | |
81a6a5cd PM |
166 | |
167 | /* | |
168 | * Linked list running through all cgroups that can | |
169 | * potentially be reaped by the release agent. Protected by | |
170 | * release_list_lock | |
171 | */ | |
172 | struct list_head release_list; | |
cc31edce PM |
173 | |
174 | /* pids_mutex protects the fields below */ | |
175 | struct rw_semaphore pids_mutex; | |
176 | /* Array of process ids in the cgroup */ | |
177 | pid_t *tasks_pids; | |
178 | /* How many files are using the current tasks_pids array */ | |
179 | int pids_use_count; | |
180 | /* Length of the current tasks_pids array */ | |
181 | int pids_length; | |
a47295e6 PM |
182 | |
183 | /* For RCU-protected deletion */ | |
184 | struct rcu_head rcu_head; | |
817929ec PM |
185 | }; |
186 | ||
d20a390a PM |
187 | /* |
188 | * A css_set is a structure holding pointers to a set of | |
817929ec PM |
189 | * cgroup_subsys_state objects. This saves space in the task struct |
190 | * object and speeds up fork()/exit(), since a single inc/dec and a | |
d20a390a PM |
191 | * list_add()/del() can bump the reference count on the entire cgroup |
192 | * set for a task. | |
817929ec PM |
193 | */ |
194 | ||
195 | struct css_set { | |
196 | ||
197 | /* Reference count */ | |
146aa1bd | 198 | atomic_t refcount; |
817929ec | 199 | |
472b1053 LZ |
200 | /* |
201 | * List running through all cgroup groups in the same hash | |
202 | * slot. Protected by css_set_lock | |
203 | */ | |
204 | struct hlist_node hlist; | |
205 | ||
817929ec PM |
206 | /* |
207 | * List running through all tasks using this cgroup | |
208 | * group. Protected by css_set_lock | |
209 | */ | |
210 | struct list_head tasks; | |
211 | ||
212 | /* | |
213 | * List of cg_cgroup_link objects on link chains from | |
214 | * cgroups referenced from this css_set. Protected by | |
215 | * css_set_lock | |
216 | */ | |
217 | struct list_head cg_links; | |
218 | ||
219 | /* | |
220 | * Set of subsystem states, one for each subsystem. This array | |
221 | * is immutable after creation apart from the init_css_set | |
222 | * during subsystem registration (at boot time). | |
223 | */ | |
224 | struct cgroup_subsys_state *subsys[CGROUP_SUBSYS_COUNT]; | |
ddbcc7e8 PM |
225 | }; |
226 | ||
91796569 PM |
227 | /* |
228 | * cgroup_map_cb is an abstract callback API for reporting map-valued | |
229 | * control files | |
230 | */ | |
231 | ||
232 | struct cgroup_map_cb { | |
233 | int (*fill)(struct cgroup_map_cb *cb, const char *key, u64 value); | |
234 | void *state; | |
235 | }; | |
236 | ||
d20a390a PM |
237 | /* |
238 | * struct cftype: handler definitions for cgroup control files | |
ddbcc7e8 PM |
239 | * |
240 | * When reading/writing to a file: | |
a043e3b2 | 241 | * - the cgroup to use is file->f_dentry->d_parent->d_fsdata |
ddbcc7e8 PM |
242 | * - the 'cftype' of the file is file->f_dentry->d_fsdata |
243 | */ | |
244 | ||
245 | #define MAX_CFTYPE_NAME 64 | |
246 | struct cftype { | |
d20a390a PM |
247 | /* |
248 | * By convention, the name should begin with the name of the | |
249 | * subsystem, followed by a period | |
250 | */ | |
ddbcc7e8 PM |
251 | char name[MAX_CFTYPE_NAME]; |
252 | int private; | |
db3b1497 PM |
253 | |
254 | /* | |
255 | * If non-zero, defines the maximum length of string that can | |
256 | * be passed to write_string; defaults to 64 | |
257 | */ | |
258 | size_t max_write_len; | |
259 | ||
ce16b49d PM |
260 | int (*open)(struct inode *inode, struct file *file); |
261 | ssize_t (*read)(struct cgroup *cgrp, struct cftype *cft, | |
262 | struct file *file, | |
263 | char __user *buf, size_t nbytes, loff_t *ppos); | |
ddbcc7e8 | 264 | /* |
f4c753b7 | 265 | * read_u64() is a shortcut for the common case of returning a |
ddbcc7e8 PM |
266 | * single integer. Use it in place of read() |
267 | */ | |
ce16b49d | 268 | u64 (*read_u64)(struct cgroup *cgrp, struct cftype *cft); |
e73d2c61 PM |
269 | /* |
270 | * read_s64() is a signed version of read_u64() | |
271 | */ | |
ce16b49d | 272 | s64 (*read_s64)(struct cgroup *cgrp, struct cftype *cft); |
91796569 PM |
273 | /* |
274 | * read_map() is used for defining a map of key/value | |
275 | * pairs. It should call cb->fill(cb, key, value) for each | |
276 | * entry. The key/value pairs (and their ordering) should not | |
277 | * change between reboots. | |
278 | */ | |
ce16b49d PM |
279 | int (*read_map)(struct cgroup *cont, struct cftype *cft, |
280 | struct cgroup_map_cb *cb); | |
29486df3 SH |
281 | /* |
282 | * read_seq_string() is used for outputting a simple sequence | |
283 | * using seqfile. | |
284 | */ | |
ce16b49d PM |
285 | int (*read_seq_string)(struct cgroup *cont, struct cftype *cft, |
286 | struct seq_file *m); | |
91796569 | 287 | |
ce16b49d PM |
288 | ssize_t (*write)(struct cgroup *cgrp, struct cftype *cft, |
289 | struct file *file, | |
290 | const char __user *buf, size_t nbytes, loff_t *ppos); | |
355e0c48 PM |
291 | |
292 | /* | |
f4c753b7 | 293 | * write_u64() is a shortcut for the common case of accepting |
355e0c48 PM |
294 | * a single integer (as parsed by simple_strtoull) from |
295 | * userspace. Use in place of write(); return 0 or error. | |
296 | */ | |
ce16b49d | 297 | int (*write_u64)(struct cgroup *cgrp, struct cftype *cft, u64 val); |
e73d2c61 PM |
298 | /* |
299 | * write_s64() is a signed version of write_u64() | |
300 | */ | |
ce16b49d | 301 | int (*write_s64)(struct cgroup *cgrp, struct cftype *cft, s64 val); |
355e0c48 | 302 | |
db3b1497 PM |
303 | /* |
304 | * write_string() is passed a nul-terminated kernelspace | |
305 | * buffer of maximum length determined by max_write_len. | |
306 | * Returns 0 or -ve error code. | |
307 | */ | |
308 | int (*write_string)(struct cgroup *cgrp, struct cftype *cft, | |
309 | const char *buffer); | |
d447ea2f PE |
310 | /* |
311 | * trigger() callback can be used to get some kick from the | |
312 | * userspace, when the actual string written is not important | |
313 | * at all. The private field can be used to determine the | |
314 | * kick type for multiplexing. | |
315 | */ | |
316 | int (*trigger)(struct cgroup *cgrp, unsigned int event); | |
317 | ||
ce16b49d | 318 | int (*release)(struct inode *inode, struct file *file); |
ddbcc7e8 PM |
319 | }; |
320 | ||
31a7df01 CW |
321 | struct cgroup_scanner { |
322 | struct cgroup *cg; | |
323 | int (*test_task)(struct task_struct *p, struct cgroup_scanner *scan); | |
324 | void (*process_task)(struct task_struct *p, | |
325 | struct cgroup_scanner *scan); | |
326 | struct ptr_heap *heap; | |
327 | }; | |
328 | ||
d20a390a PM |
329 | /* |
330 | * Add a new file to the given cgroup directory. Should only be | |
331 | * called by subsystems from within a populate() method | |
332 | */ | |
ffd2d883 | 333 | int cgroup_add_file(struct cgroup *cgrp, struct cgroup_subsys *subsys, |
ddbcc7e8 PM |
334 | const struct cftype *cft); |
335 | ||
d20a390a PM |
336 | /* |
337 | * Add a set of new files to the given cgroup directory. Should | |
338 | * only be called by subsystems from within a populate() method | |
339 | */ | |
ffd2d883 | 340 | int cgroup_add_files(struct cgroup *cgrp, |
ddbcc7e8 PM |
341 | struct cgroup_subsys *subsys, |
342 | const struct cftype cft[], | |
343 | int count); | |
344 | ||
ffd2d883 | 345 | int cgroup_is_removed(const struct cgroup *cgrp); |
ddbcc7e8 | 346 | |
ffd2d883 | 347 | int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen); |
ddbcc7e8 | 348 | |
ffd2d883 | 349 | int cgroup_task_count(const struct cgroup *cgrp); |
bbcb81d0 | 350 | |
313e924c GN |
351 | /* Return true if cgrp is a descendant of the task's cgroup */ |
352 | int cgroup_is_descendant(const struct cgroup *cgrp, struct task_struct *task); | |
ddbcc7e8 PM |
353 | |
354 | /* Control Group subsystem type. See Documentation/cgroups.txt for details */ | |
355 | ||
356 | struct cgroup_subsys { | |
357 | struct cgroup_subsys_state *(*create)(struct cgroup_subsys *ss, | |
ffd2d883 LZ |
358 | struct cgroup *cgrp); |
359 | void (*pre_destroy)(struct cgroup_subsys *ss, struct cgroup *cgrp); | |
360 | void (*destroy)(struct cgroup_subsys *ss, struct cgroup *cgrp); | |
ddbcc7e8 | 361 | int (*can_attach)(struct cgroup_subsys *ss, |
ffd2d883 LZ |
362 | struct cgroup *cgrp, struct task_struct *tsk); |
363 | void (*attach)(struct cgroup_subsys *ss, struct cgroup *cgrp, | |
364 | struct cgroup *old_cgrp, struct task_struct *tsk); | |
ddbcc7e8 PM |
365 | void (*fork)(struct cgroup_subsys *ss, struct task_struct *task); |
366 | void (*exit)(struct cgroup_subsys *ss, struct task_struct *task); | |
367 | int (*populate)(struct cgroup_subsys *ss, | |
ffd2d883 LZ |
368 | struct cgroup *cgrp); |
369 | void (*post_clone)(struct cgroup_subsys *ss, struct cgroup *cgrp); | |
ddbcc7e8 | 370 | void (*bind)(struct cgroup_subsys *ss, struct cgroup *root); |
e5991371 | 371 | |
ddbcc7e8 PM |
372 | int subsys_id; |
373 | int active; | |
8bab8dde | 374 | int disabled; |
ddbcc7e8 PM |
375 | int early_init; |
376 | #define MAX_CGROUP_TYPE_NAMELEN 32 | |
377 | const char *name; | |
378 | ||
999cd8a4 PM |
379 | /* |
380 | * Protects sibling/children links of cgroups in this | |
381 | * hierarchy, plus protects which hierarchy (or none) the | |
382 | * subsystem is a part of (i.e. root/sibling). To avoid | |
383 | * potential deadlocks, the following operations should not be | |
384 | * undertaken while holding any hierarchy_mutex: | |
385 | * | |
386 | * - allocating memory | |
387 | * - initiating hotplug events | |
388 | */ | |
389 | struct mutex hierarchy_mutex; | |
cfebe563 | 390 | struct lock_class_key subsys_key; |
ddbcc7e8 | 391 | |
999cd8a4 PM |
392 | /* |
393 | * Link to parent, and list entry in parent's children. | |
394 | * Protected by this->hierarchy_mutex and cgroup_lock() | |
395 | */ | |
396 | struct cgroupfs_root *root; | |
ddbcc7e8 | 397 | struct list_head sibling; |
ddbcc7e8 PM |
398 | }; |
399 | ||
400 | #define SUBSYS(_x) extern struct cgroup_subsys _x ## _subsys; | |
401 | #include <linux/cgroup_subsys.h> | |
402 | #undef SUBSYS | |
403 | ||
404 | static inline struct cgroup_subsys_state *cgroup_subsys_state( | |
ffd2d883 | 405 | struct cgroup *cgrp, int subsys_id) |
ddbcc7e8 | 406 | { |
ffd2d883 | 407 | return cgrp->subsys[subsys_id]; |
ddbcc7e8 PM |
408 | } |
409 | ||
410 | static inline struct cgroup_subsys_state *task_subsys_state( | |
411 | struct task_struct *task, int subsys_id) | |
412 | { | |
817929ec | 413 | return rcu_dereference(task->cgroups->subsys[subsys_id]); |
ddbcc7e8 PM |
414 | } |
415 | ||
416 | static inline struct cgroup* task_cgroup(struct task_struct *task, | |
417 | int subsys_id) | |
418 | { | |
419 | return task_subsys_state(task, subsys_id)->cgroup; | |
420 | } | |
421 | ||
e885dcde SH |
422 | int cgroup_clone(struct task_struct *tsk, struct cgroup_subsys *ss, |
423 | char *nodename); | |
697f4161 | 424 | |
817929ec PM |
425 | /* A cgroup_iter should be treated as an opaque object */ |
426 | struct cgroup_iter { | |
427 | struct list_head *cg_link; | |
428 | struct list_head *task; | |
429 | }; | |
430 | ||
d20a390a PM |
431 | /* |
432 | * To iterate across the tasks in a cgroup: | |
817929ec PM |
433 | * |
434 | * 1) call cgroup_iter_start to intialize an iterator | |
435 | * | |
436 | * 2) call cgroup_iter_next() to retrieve member tasks until it | |
437 | * returns NULL or until you want to end the iteration | |
438 | * | |
439 | * 3) call cgroup_iter_end() to destroy the iterator. | |
31a7df01 | 440 | * |
d20a390a PM |
441 | * Or, call cgroup_scan_tasks() to iterate through every task in a |
442 | * cgroup - cgroup_scan_tasks() holds the css_set_lock when calling | |
443 | * the test_task() callback, but not while calling the process_task() | |
444 | * callback. | |
817929ec | 445 | */ |
ffd2d883 LZ |
446 | void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it); |
447 | struct task_struct *cgroup_iter_next(struct cgroup *cgrp, | |
817929ec | 448 | struct cgroup_iter *it); |
ffd2d883 | 449 | void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it); |
31a7df01 | 450 | int cgroup_scan_tasks(struct cgroup_scanner *scan); |
956db3ca | 451 | int cgroup_attach_task(struct cgroup *, struct task_struct *); |
817929ec | 452 | |
ddbcc7e8 PM |
453 | #else /* !CONFIG_CGROUPS */ |
454 | ||
455 | static inline int cgroup_init_early(void) { return 0; } | |
456 | static inline int cgroup_init(void) { return 0; } | |
b4f48b63 PM |
457 | static inline void cgroup_fork(struct task_struct *p) {} |
458 | static inline void cgroup_fork_callbacks(struct task_struct *p) {} | |
817929ec | 459 | static inline void cgroup_post_fork(struct task_struct *p) {} |
b4f48b63 | 460 | static inline void cgroup_exit(struct task_struct *p, int callbacks) {} |
ddbcc7e8 PM |
461 | |
462 | static inline void cgroup_lock(void) {} | |
463 | static inline void cgroup_unlock(void) {} | |
846c7bb0 BS |
464 | static inline int cgroupstats_build(struct cgroupstats *stats, |
465 | struct dentry *dentry) | |
466 | { | |
467 | return -EINVAL; | |
468 | } | |
ddbcc7e8 PM |
469 | |
470 | #endif /* !CONFIG_CGROUPS */ | |
471 | ||
472 | #endif /* _LINUX_CGROUP_H */ |