diff options
Diffstat (limited to 'include/linux/cgroup.h')
-rw-r--r-- | include/linux/cgroup.h | 142 |
1 files changed, 108 insertions, 34 deletions
diff --git a/include/linux/cgroup.h b/include/linux/cgroup.h index 499900d0cee7..665fa70e4094 100644 --- a/include/linux/cgroup.h +++ b/include/linux/cgroup.h | |||
@@ -15,6 +15,7 @@ | |||
15 | #include <linux/cgroupstats.h> | 15 | #include <linux/cgroupstats.h> |
16 | #include <linux/prio_heap.h> | 16 | #include <linux/prio_heap.h> |
17 | #include <linux/rwsem.h> | 17 | #include <linux/rwsem.h> |
18 | #include <linux/idr.h> | ||
18 | 19 | ||
19 | #ifdef CONFIG_CGROUPS | 20 | #ifdef CONFIG_CGROUPS |
20 | 21 | ||
@@ -22,6 +23,7 @@ struct cgroupfs_root; | |||
22 | struct cgroup_subsys; | 23 | struct cgroup_subsys; |
23 | struct inode; | 24 | struct inode; |
24 | struct cgroup; | 25 | struct cgroup; |
26 | struct css_id; | ||
25 | 27 | ||
26 | extern int cgroup_init_early(void); | 28 | extern int cgroup_init_early(void); |
27 | extern int cgroup_init(void); | 29 | extern int cgroup_init(void); |
@@ -47,18 +49,24 @@ enum cgroup_subsys_id { | |||
47 | 49 | ||
48 | /* Per-subsystem/per-cgroup state maintained by the system. */ | 50 | /* Per-subsystem/per-cgroup state maintained by the system. */ |
49 | struct cgroup_subsys_state { | 51 | struct cgroup_subsys_state { |
50 | /* The cgroup that this subsystem is attached to. Useful | 52 | /* |
53 | * The cgroup that this subsystem is attached to. Useful | ||
51 | * for subsystems that want to know about the cgroup | 54 | * for subsystems that want to know about the cgroup |
52 | * hierarchy structure */ | 55 | * hierarchy structure |
56 | */ | ||
53 | struct cgroup *cgroup; | 57 | struct cgroup *cgroup; |
54 | 58 | ||
55 | /* State maintained by the cgroup system to allow subsystems | 59 | /* |
60 | * State maintained by the cgroup system to allow subsystems | ||
56 | * to be "busy". Should be accessed via css_get(), | 61 | * to be "busy". Should be accessed via css_get(), |
57 | * css_tryget() and and css_put(). */ | 62 | * css_tryget() and and css_put(). |
63 | */ | ||
58 | 64 | ||
59 | atomic_t refcnt; | 65 | atomic_t refcnt; |
60 | 66 | ||
61 | unsigned long flags; | 67 | unsigned long flags; |
68 | /* ID for this css, if possible */ | ||
69 | struct css_id *id; | ||
62 | }; | 70 | }; |
63 | 71 | ||
64 | /* bits in struct cgroup_subsys_state flags field */ | 72 | /* bits in struct cgroup_subsys_state flags field */ |
@@ -120,19 +128,26 @@ static inline void css_put(struct cgroup_subsys_state *css) | |||
120 | enum { | 128 | enum { |
121 | /* Control Group is dead */ | 129 | /* Control Group is dead */ |
122 | CGRP_REMOVED, | 130 | CGRP_REMOVED, |
123 | /* Control Group has previously had a child cgroup or a task, | 131 | /* |
124 | * but no longer (only if CGRP_NOTIFY_ON_RELEASE is set) */ | 132 | * Control Group has previously had a child cgroup or a task, |
133 | * but no longer (only if CGRP_NOTIFY_ON_RELEASE is set) | ||
134 | */ | ||
125 | CGRP_RELEASABLE, | 135 | CGRP_RELEASABLE, |
126 | /* Control Group requires release notifications to userspace */ | 136 | /* Control Group requires release notifications to userspace */ |
127 | CGRP_NOTIFY_ON_RELEASE, | 137 | CGRP_NOTIFY_ON_RELEASE, |
138 | /* | ||
139 | * A thread in rmdir() is wating for this cgroup. | ||
140 | */ | ||
141 | CGRP_WAIT_ON_RMDIR, | ||
128 | }; | 142 | }; |
129 | 143 | ||
130 | struct cgroup { | 144 | struct cgroup { |
131 | unsigned long flags; /* "unsigned long" so bitops work */ | 145 | unsigned long flags; /* "unsigned long" so bitops work */ |
132 | 146 | ||
133 | /* count users of this cgroup. >0 means busy, but doesn't | 147 | /* |
134 | * necessarily indicate the number of tasks in the | 148 | * count users of this cgroup. >0 means busy, but doesn't |
135 | * cgroup */ | 149 | * necessarily indicate the number of tasks in the cgroup |
150 | */ | ||
136 | atomic_t count; | 151 | atomic_t count; |
137 | 152 | ||
138 | /* | 153 | /* |
@@ -142,7 +157,7 @@ struct cgroup { | |||
142 | struct list_head sibling; /* my parent's children */ | 157 | struct list_head sibling; /* my parent's children */ |
143 | struct list_head children; /* my children */ | 158 | struct list_head children; /* my children */ |
144 | 159 | ||
145 | struct cgroup *parent; /* my parent */ | 160 | struct cgroup *parent; /* my parent */ |
146 | struct dentry *dentry; /* cgroup fs entry, RCU protected */ | 161 | struct dentry *dentry; /* cgroup fs entry, RCU protected */ |
147 | 162 | ||
148 | /* Private pointers for each registered subsystem */ | 163 | /* Private pointers for each registered subsystem */ |
@@ -177,11 +192,12 @@ struct cgroup { | |||
177 | struct rcu_head rcu_head; | 192 | struct rcu_head rcu_head; |
178 | }; | 193 | }; |
179 | 194 | ||
180 | /* A css_set is a structure holding pointers to a set of | 195 | /* |
196 | * A css_set is a structure holding pointers to a set of | ||
181 | * cgroup_subsys_state objects. This saves space in the task struct | 197 | * cgroup_subsys_state objects. This saves space in the task struct |
182 | * object and speeds up fork()/exit(), since a single inc/dec and a | 198 | * object and speeds up fork()/exit(), since a single inc/dec and a |
183 | * list_add()/del() can bump the reference count on the entire | 199 | * list_add()/del() can bump the reference count on the entire cgroup |
184 | * cgroup set for a task. | 200 | * set for a task. |
185 | */ | 201 | */ |
186 | 202 | ||
187 | struct css_set { | 203 | struct css_set { |
@@ -226,13 +242,8 @@ struct cgroup_map_cb { | |||
226 | void *state; | 242 | void *state; |
227 | }; | 243 | }; |
228 | 244 | ||
229 | /* struct cftype: | 245 | /* |
230 | * | 246 | * struct cftype: handler definitions for cgroup control files |
231 | * The files in the cgroup filesystem mostly have a very simple read/write | ||
232 | * handling, some common function will take care of it. Nevertheless some cases | ||
233 | * (read tasks) are special and therefore I define this structure for every | ||
234 | * kind of file. | ||
235 | * | ||
236 | * | 247 | * |
237 | * When reading/writing to a file: | 248 | * When reading/writing to a file: |
238 | * - the cgroup to use is file->f_dentry->d_parent->d_fsdata | 249 | * - the cgroup to use is file->f_dentry->d_parent->d_fsdata |
@@ -241,10 +252,17 @@ struct cgroup_map_cb { | |||
241 | 252 | ||
242 | #define MAX_CFTYPE_NAME 64 | 253 | #define MAX_CFTYPE_NAME 64 |
243 | struct cftype { | 254 | struct cftype { |
244 | /* By convention, the name should begin with the name of the | 255 | /* |
245 | * subsystem, followed by a period */ | 256 | * By convention, the name should begin with the name of the |
257 | * subsystem, followed by a period | ||
258 | */ | ||
246 | char name[MAX_CFTYPE_NAME]; | 259 | char name[MAX_CFTYPE_NAME]; |
247 | int private; | 260 | int private; |
261 | /* | ||
262 | * If not 0, file mode is set to this value, otherwise it will | ||
263 | * be figured out automatically | ||
264 | */ | ||
265 | mode_t mode; | ||
248 | 266 | ||
249 | /* | 267 | /* |
250 | * If non-zero, defines the maximum length of string that can | 268 | * If non-zero, defines the maximum length of string that can |
@@ -319,15 +337,20 @@ struct cgroup_scanner { | |||
319 | void (*process_task)(struct task_struct *p, | 337 | void (*process_task)(struct task_struct *p, |
320 | struct cgroup_scanner *scan); | 338 | struct cgroup_scanner *scan); |
321 | struct ptr_heap *heap; | 339 | struct ptr_heap *heap; |
340 | void *data; | ||
322 | }; | 341 | }; |
323 | 342 | ||
324 | /* Add a new file to the given cgroup directory. Should only be | 343 | /* |
325 | * called by subsystems from within a populate() method */ | 344 | * Add a new file to the given cgroup directory. Should only be |
345 | * called by subsystems from within a populate() method | ||
346 | */ | ||
326 | int cgroup_add_file(struct cgroup *cgrp, struct cgroup_subsys *subsys, | 347 | int cgroup_add_file(struct cgroup *cgrp, struct cgroup_subsys *subsys, |
327 | const struct cftype *cft); | 348 | const struct cftype *cft); |
328 | 349 | ||
329 | /* Add a set of new files to the given cgroup directory. Should | 350 | /* |
330 | * only be called by subsystems from within a populate() method */ | 351 | * Add a set of new files to the given cgroup directory. Should |
352 | * only be called by subsystems from within a populate() method | ||
353 | */ | ||
331 | int cgroup_add_files(struct cgroup *cgrp, | 354 | int cgroup_add_files(struct cgroup *cgrp, |
332 | struct cgroup_subsys *subsys, | 355 | struct cgroup_subsys *subsys, |
333 | const struct cftype cft[], | 356 | const struct cftype cft[], |
@@ -339,15 +362,18 @@ int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen); | |||
339 | 362 | ||
340 | int cgroup_task_count(const struct cgroup *cgrp); | 363 | int cgroup_task_count(const struct cgroup *cgrp); |
341 | 364 | ||
342 | /* Return true if the cgroup is a descendant of the current cgroup */ | 365 | /* Return true if cgrp is a descendant of the task's cgroup */ |
343 | int cgroup_is_descendant(const struct cgroup *cgrp); | 366 | int cgroup_is_descendant(const struct cgroup *cgrp, struct task_struct *task); |
344 | 367 | ||
345 | /* Control Group subsystem type. See Documentation/cgroups.txt for details */ | 368 | /* |
369 | * Control Group subsystem type. | ||
370 | * See Documentation/cgroups/cgroups.txt for details | ||
371 | */ | ||
346 | 372 | ||
347 | struct cgroup_subsys { | 373 | struct cgroup_subsys { |
348 | struct cgroup_subsys_state *(*create)(struct cgroup_subsys *ss, | 374 | struct cgroup_subsys_state *(*create)(struct cgroup_subsys *ss, |
349 | struct cgroup *cgrp); | 375 | struct cgroup *cgrp); |
350 | void (*pre_destroy)(struct cgroup_subsys *ss, struct cgroup *cgrp); | 376 | int (*pre_destroy)(struct cgroup_subsys *ss, struct cgroup *cgrp); |
351 | void (*destroy)(struct cgroup_subsys *ss, struct cgroup *cgrp); | 377 | void (*destroy)(struct cgroup_subsys *ss, struct cgroup *cgrp); |
352 | int (*can_attach)(struct cgroup_subsys *ss, | 378 | int (*can_attach)(struct cgroup_subsys *ss, |
353 | struct cgroup *cgrp, struct task_struct *tsk); | 379 | struct cgroup *cgrp, struct task_struct *tsk); |
@@ -364,6 +390,11 @@ struct cgroup_subsys { | |||
364 | int active; | 390 | int active; |
365 | int disabled; | 391 | int disabled; |
366 | int early_init; | 392 | int early_init; |
393 | /* | ||
394 | * True if this subsys uses ID. ID is not available before cgroup_init() | ||
395 | * (not available in early_init time.) | ||
396 | */ | ||
397 | bool use_id; | ||
367 | #define MAX_CGROUP_TYPE_NAMELEN 32 | 398 | #define MAX_CGROUP_TYPE_NAMELEN 32 |
368 | const char *name; | 399 | const char *name; |
369 | 400 | ||
@@ -386,6 +417,9 @@ struct cgroup_subsys { | |||
386 | */ | 417 | */ |
387 | struct cgroupfs_root *root; | 418 | struct cgroupfs_root *root; |
388 | struct list_head sibling; | 419 | struct list_head sibling; |
420 | /* used when use_id == true */ | ||
421 | struct idr idr; | ||
422 | spinlock_t id_lock; | ||
389 | }; | 423 | }; |
390 | 424 | ||
391 | #define SUBSYS(_x) extern struct cgroup_subsys _x ## _subsys; | 425 | #define SUBSYS(_x) extern struct cgroup_subsys _x ## _subsys; |
@@ -419,7 +453,8 @@ struct cgroup_iter { | |||
419 | struct list_head *task; | 453 | struct list_head *task; |
420 | }; | 454 | }; |
421 | 455 | ||
422 | /* To iterate across the tasks in a cgroup: | 456 | /* |
457 | * To iterate across the tasks in a cgroup: | ||
423 | * | 458 | * |
424 | * 1) call cgroup_iter_start to intialize an iterator | 459 | * 1) call cgroup_iter_start to intialize an iterator |
425 | * | 460 | * |
@@ -428,9 +463,10 @@ struct cgroup_iter { | |||
428 | * | 463 | * |
429 | * 3) call cgroup_iter_end() to destroy the iterator. | 464 | * 3) call cgroup_iter_end() to destroy the iterator. |
430 | * | 465 | * |
431 | * Or, call cgroup_scan_tasks() to iterate through every task in a cpuset. | 466 | * Or, call cgroup_scan_tasks() to iterate through every task in a |
432 | * - cgroup_scan_tasks() holds the css_set_lock when calling the test_task() | 467 | * cgroup - cgroup_scan_tasks() holds the css_set_lock when calling |
433 | * callback, but not while calling the process_task() callback. | 468 | * the test_task() callback, but not while calling the process_task() |
469 | * callback. | ||
434 | */ | 470 | */ |
435 | void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it); | 471 | void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it); |
436 | struct task_struct *cgroup_iter_next(struct cgroup *cgrp, | 472 | struct task_struct *cgroup_iter_next(struct cgroup *cgrp, |
@@ -439,6 +475,44 @@ void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it); | |||
439 | int cgroup_scan_tasks(struct cgroup_scanner *scan); | 475 | int cgroup_scan_tasks(struct cgroup_scanner *scan); |
440 | int cgroup_attach_task(struct cgroup *, struct task_struct *); | 476 | int cgroup_attach_task(struct cgroup *, struct task_struct *); |
441 | 477 | ||
478 | /* | ||
479 | * CSS ID is ID for cgroup_subsys_state structs under subsys. This only works | ||
480 | * if cgroup_subsys.use_id == true. It can be used for looking up and scanning. | ||
481 | * CSS ID is assigned at cgroup allocation (create) automatically | ||
482 | * and removed when subsys calls free_css_id() function. This is because | ||
483 | * the lifetime of cgroup_subsys_state is subsys's matter. | ||
484 | * | ||
485 | * Looking up and scanning function should be called under rcu_read_lock(). | ||
486 | * Taking cgroup_mutex()/hierarchy_mutex() is not necessary for following calls. | ||
487 | * But the css returned by this routine can be "not populated yet" or "being | ||
488 | * destroyed". The caller should check css and cgroup's status. | ||
489 | */ | ||
490 | |||
491 | /* | ||
492 | * Typically Called at ->destroy(), or somewhere the subsys frees | ||
493 | * cgroup_subsys_state. | ||
494 | */ | ||
495 | void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css); | ||
496 | |||
497 | /* Find a cgroup_subsys_state which has given ID */ | ||
498 | |||
499 | struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id); | ||
500 | |||
501 | /* | ||
502 | * Get a cgroup whose id is greater than or equal to id under tree of root. | ||
503 | * Returning a cgroup_subsys_state or NULL. | ||
504 | */ | ||
505 | struct cgroup_subsys_state *css_get_next(struct cgroup_subsys *ss, int id, | ||
506 | struct cgroup_subsys_state *root, int *foundid); | ||
507 | |||
508 | /* Returns true if root is ancestor of cg */ | ||
509 | bool css_is_ancestor(struct cgroup_subsys_state *cg, | ||
510 | const struct cgroup_subsys_state *root); | ||
511 | |||
512 | /* Get id and depth of css */ | ||
513 | unsigned short css_id(struct cgroup_subsys_state *css); | ||
514 | unsigned short css_depth(struct cgroup_subsys_state *css); | ||
515 | |||
442 | #else /* !CONFIG_CGROUPS */ | 516 | #else /* !CONFIG_CGROUPS */ |
443 | 517 | ||
444 | static inline int cgroup_init_early(void) { return 0; } | 518 | static inline int cgroup_init_early(void) { return 0; } |