diff options
Diffstat (limited to 'include/linux/cgroup.h')
-rw-r--r-- | include/linux/cgroup.h | 137 |
1 files changed, 104 insertions, 33 deletions
diff --git a/include/linux/cgroup.h b/include/linux/cgroup.h index 499900d0cee7..4316a546beb5 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,15 @@ 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 | /* Control Group subsystem type. See Documentation/cgroups.txt for details */ |
346 | 369 | ||
347 | struct cgroup_subsys { | 370 | struct cgroup_subsys { |
348 | struct cgroup_subsys_state *(*create)(struct cgroup_subsys *ss, | 371 | struct cgroup_subsys_state *(*create)(struct cgroup_subsys *ss, |
349 | struct cgroup *cgrp); | 372 | struct cgroup *cgrp); |
350 | void (*pre_destroy)(struct cgroup_subsys *ss, struct cgroup *cgrp); | 373 | int (*pre_destroy)(struct cgroup_subsys *ss, struct cgroup *cgrp); |
351 | void (*destroy)(struct cgroup_subsys *ss, struct cgroup *cgrp); | 374 | void (*destroy)(struct cgroup_subsys *ss, struct cgroup *cgrp); |
352 | int (*can_attach)(struct cgroup_subsys *ss, | 375 | int (*can_attach)(struct cgroup_subsys *ss, |
353 | struct cgroup *cgrp, struct task_struct *tsk); | 376 | struct cgroup *cgrp, struct task_struct *tsk); |
@@ -364,6 +387,11 @@ struct cgroup_subsys { | |||
364 | int active; | 387 | int active; |
365 | int disabled; | 388 | int disabled; |
366 | int early_init; | 389 | int early_init; |
390 | /* | ||
391 | * True if this subsys uses ID. ID is not available before cgroup_init() | ||
392 | * (not available in early_init time.) | ||
393 | */ | ||
394 | bool use_id; | ||
367 | #define MAX_CGROUP_TYPE_NAMELEN 32 | 395 | #define MAX_CGROUP_TYPE_NAMELEN 32 |
368 | const char *name; | 396 | const char *name; |
369 | 397 | ||
@@ -386,6 +414,9 @@ struct cgroup_subsys { | |||
386 | */ | 414 | */ |
387 | struct cgroupfs_root *root; | 415 | struct cgroupfs_root *root; |
388 | struct list_head sibling; | 416 | struct list_head sibling; |
417 | /* used when use_id == true */ | ||
418 | struct idr idr; | ||
419 | spinlock_t id_lock; | ||
389 | }; | 420 | }; |
390 | 421 | ||
391 | #define SUBSYS(_x) extern struct cgroup_subsys _x ## _subsys; | 422 | #define SUBSYS(_x) extern struct cgroup_subsys _x ## _subsys; |
@@ -419,7 +450,8 @@ struct cgroup_iter { | |||
419 | struct list_head *task; | 450 | struct list_head *task; |
420 | }; | 451 | }; |
421 | 452 | ||
422 | /* To iterate across the tasks in a cgroup: | 453 | /* |
454 | * To iterate across the tasks in a cgroup: | ||
423 | * | 455 | * |
424 | * 1) call cgroup_iter_start to intialize an iterator | 456 | * 1) call cgroup_iter_start to intialize an iterator |
425 | * | 457 | * |
@@ -428,9 +460,10 @@ struct cgroup_iter { | |||
428 | * | 460 | * |
429 | * 3) call cgroup_iter_end() to destroy the iterator. | 461 | * 3) call cgroup_iter_end() to destroy the iterator. |
430 | * | 462 | * |
431 | * Or, call cgroup_scan_tasks() to iterate through every task in a cpuset. | 463 | * 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() | 464 | * cgroup - cgroup_scan_tasks() holds the css_set_lock when calling |
433 | * callback, but not while calling the process_task() callback. | 465 | * the test_task() callback, but not while calling the process_task() |
466 | * callback. | ||
434 | */ | 467 | */ |
435 | void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it); | 468 | void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it); |
436 | struct task_struct *cgroup_iter_next(struct cgroup *cgrp, | 469 | struct task_struct *cgroup_iter_next(struct cgroup *cgrp, |
@@ -439,6 +472,44 @@ void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it); | |||
439 | int cgroup_scan_tasks(struct cgroup_scanner *scan); | 472 | int cgroup_scan_tasks(struct cgroup_scanner *scan); |
440 | int cgroup_attach_task(struct cgroup *, struct task_struct *); | 473 | int cgroup_attach_task(struct cgroup *, struct task_struct *); |
441 | 474 | ||
475 | /* | ||
476 | * CSS ID is ID for cgroup_subsys_state structs under subsys. This only works | ||
477 | * if cgroup_subsys.use_id == true. It can be used for looking up and scanning. | ||
478 | * CSS ID is assigned at cgroup allocation (create) automatically | ||
479 | * and removed when subsys calls free_css_id() function. This is because | ||
480 | * the lifetime of cgroup_subsys_state is subsys's matter. | ||
481 | * | ||
482 | * Looking up and scanning function should be called under rcu_read_lock(). | ||
483 | * Taking cgroup_mutex()/hierarchy_mutex() is not necessary for following calls. | ||
484 | * But the css returned by this routine can be "not populated yet" or "being | ||
485 | * destroyed". The caller should check css and cgroup's status. | ||
486 | */ | ||
487 | |||
488 | /* | ||
489 | * Typically Called at ->destroy(), or somewhere the subsys frees | ||
490 | * cgroup_subsys_state. | ||
491 | */ | ||
492 | void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css); | ||
493 | |||
494 | /* Find a cgroup_subsys_state which has given ID */ | ||
495 | |||
496 | struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id); | ||
497 | |||
498 | /* | ||
499 | * Get a cgroup whose id is greater than or equal to id under tree of root. | ||
500 | * Returning a cgroup_subsys_state or NULL. | ||
501 | */ | ||
502 | struct cgroup_subsys_state *css_get_next(struct cgroup_subsys *ss, int id, | ||
503 | struct cgroup_subsys_state *root, int *foundid); | ||
504 | |||
505 | /* Returns true if root is ancestor of cg */ | ||
506 | bool css_is_ancestor(struct cgroup_subsys_state *cg, | ||
507 | const struct cgroup_subsys_state *root); | ||
508 | |||
509 | /* Get id and depth of css */ | ||
510 | unsigned short css_id(struct cgroup_subsys_state *css); | ||
511 | unsigned short css_depth(struct cgroup_subsys_state *css); | ||
512 | |||
442 | #else /* !CONFIG_CGROUPS */ | 513 | #else /* !CONFIG_CGROUPS */ |
443 | 514 | ||
444 | static inline int cgroup_init_early(void) { return 0; } | 515 | static inline int cgroup_init_early(void) { return 0; } |