blob: 14160b5b693f8d2380413b122a8bb32eaf407bd1 [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>
Paul Menageddbcc7e2007-10-18 23:39:30 -070012#include <linux/cpumask.h>
13#include <linux/nodemask.h>
14#include <linux/rcupdate.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070015#include <linux/cgroupstats.h>
Cliff Wickman31a7df02008-02-07 00:14:42 -080016#include <linux/prio_heap.h>
Paul Menagecc31edc2008-10-18 20:28:04 -070017#include <linux/rwsem.h>
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -070018#include <linux/idr.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070019
20#ifdef CONFIG_CGROUPS
21
22struct cgroupfs_root;
23struct cgroup_subsys;
24struct inode;
Paul Menage84eea842008-07-25 01:47:00 -070025struct cgroup;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -070026struct css_id;
Paul Menageddbcc7e2007-10-18 23:39:30 -070027
28extern int cgroup_init_early(void);
29extern int cgroup_init(void);
Paul Menageddbcc7e2007-10-18 23:39:30 -070030extern void cgroup_lock(void);
Paul E. McKenneyd11c5632010-02-22 17:04:50 -080031extern int cgroup_lock_is_held(void);
Paul Menage84eea842008-07-25 01:47:00 -070032extern bool cgroup_lock_live_group(struct cgroup *cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -070033extern void cgroup_unlock(void);
Paul Menageb4f48b62007-10-18 23:39:33 -070034extern void cgroup_fork(struct task_struct *p);
35extern void cgroup_fork_callbacks(struct task_struct *p);
Paul Menage817929e2007-10-18 23:39:36 -070036extern void cgroup_post_fork(struct task_struct *p);
Paul Menageb4f48b62007-10-18 23:39:33 -070037extern void cgroup_exit(struct task_struct *p, int run_callbacks);
Balbir Singh846c7bb2007-10-18 23:39:44 -070038extern int cgroupstats_build(struct cgroupstats *stats,
39 struct dentry *dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -070040
Alexey Dobriyan828c0952009-10-01 15:43:56 -070041extern const struct file_operations proc_cgroup_operations;
Paul Menagea4243162007-10-18 23:39:35 -070042
Paul Menage817929e2007-10-18 23:39:36 -070043/* Define the enumeration of all cgroup subsystems */
44#define SUBSYS(_x) _x ## _subsys_id,
45enum cgroup_subsys_id {
46#include <linux/cgroup_subsys.h>
47 CGROUP_SUBSYS_COUNT
48};
49#undef SUBSYS
50
Paul Menageddbcc7e2007-10-18 23:39:30 -070051/* Per-subsystem/per-cgroup state maintained by the system. */
52struct cgroup_subsys_state {
Paul Menaged20a3902009-04-02 16:57:22 -070053 /*
54 * The cgroup that this subsystem is attached to. Useful
Paul Menageddbcc7e2007-10-18 23:39:30 -070055 * for subsystems that want to know about the cgroup
Paul Menaged20a3902009-04-02 16:57:22 -070056 * hierarchy structure
57 */
Paul Menageddbcc7e2007-10-18 23:39:30 -070058 struct cgroup *cgroup;
59
Paul Menaged20a3902009-04-02 16:57:22 -070060 /*
61 * State maintained by the cgroup system to allow subsystems
Paul Menagee7c5ec92009-01-07 18:08:38 -080062 * to be "busy". Should be accessed via css_get(),
Paul Menaged20a3902009-04-02 16:57:22 -070063 * css_tryget() and and css_put().
64 */
Paul Menageddbcc7e2007-10-18 23:39:30 -070065
66 atomic_t refcnt;
67
68 unsigned long flags;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -070069 /* ID for this css, if possible */
70 struct css_id *id;
Paul Menageddbcc7e2007-10-18 23:39:30 -070071};
72
73/* bits in struct cgroup_subsys_state flags field */
74enum {
75 CSS_ROOT, /* This CSS is the root of the subsystem */
Paul Menagee7c5ec92009-01-07 18:08:38 -080076 CSS_REMOVED, /* This CSS is dead */
Paul Menageddbcc7e2007-10-18 23:39:30 -070077};
78
Daisuke Nishimurad7b9fff2010-03-10 15:22:05 -080079/* Caller must verify that the css is not for root cgroup */
80static inline void __css_get(struct cgroup_subsys_state *css, int count)
81{
82 atomic_add(count, &css->refcnt);
83}
84
Paul Menageddbcc7e2007-10-18 23:39:30 -070085/*
Paul Menagee7c5ec92009-01-07 18:08:38 -080086 * Call css_get() to hold a reference on the css; it can be used
87 * for a reference obtained via:
88 * - an existing ref-counted reference to the css
89 * - task->cgroups for a locked task
Paul Menageddbcc7e2007-10-18 23:39:30 -070090 */
91
92static inline void css_get(struct cgroup_subsys_state *css)
93{
94 /* We don't need to reference count the root state */
95 if (!test_bit(CSS_ROOT, &css->flags))
Daisuke Nishimurad7b9fff2010-03-10 15:22:05 -080096 __css_get(css, 1);
Paul Menageddbcc7e2007-10-18 23:39:30 -070097}
Paul Menagee7c5ec92009-01-07 18:08:38 -080098
99static inline bool css_is_removed(struct cgroup_subsys_state *css)
100{
101 return test_bit(CSS_REMOVED, &css->flags);
102}
103
104/*
105 * Call css_tryget() to take a reference on a css if your existing
106 * (known-valid) reference isn't already ref-counted. Returns false if
107 * the css has been destroyed.
108 */
109
110static inline bool css_tryget(struct cgroup_subsys_state *css)
111{
112 if (test_bit(CSS_ROOT, &css->flags))
113 return true;
114 while (!atomic_inc_not_zero(&css->refcnt)) {
115 if (test_bit(CSS_REMOVED, &css->flags))
116 return false;
Paul Menage804b3c22009-01-29 14:25:21 -0800117 cpu_relax();
Paul Menagee7c5ec92009-01-07 18:08:38 -0800118 }
119 return true;
120}
121
Paul Menageddbcc7e2007-10-18 23:39:30 -0700122/*
123 * css_put() should be called to release a reference taken by
Paul Menagee7c5ec92009-01-07 18:08:38 -0800124 * css_get() or css_tryget()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700125 */
126
Daisuke Nishimurad7b9fff2010-03-10 15:22:05 -0800127extern void __css_put(struct cgroup_subsys_state *css, int count);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700128static inline void css_put(struct cgroup_subsys_state *css)
129{
130 if (!test_bit(CSS_ROOT, &css->flags))
Daisuke Nishimurad7b9fff2010-03-10 15:22:05 -0800131 __css_put(css, 1);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700132}
133
Paul Menage3116f0e2008-04-29 01:00:04 -0700134/* bits in struct cgroup flags field */
135enum {
136 /* Control Group is dead */
137 CGRP_REMOVED,
Paul Menaged20a3902009-04-02 16:57:22 -0700138 /*
139 * Control Group has previously had a child cgroup or a task,
140 * but no longer (only if CGRP_NOTIFY_ON_RELEASE is set)
141 */
Paul Menage3116f0e2008-04-29 01:00:04 -0700142 CGRP_RELEASABLE,
143 /* Control Group requires release notifications to userspace */
144 CGRP_NOTIFY_ON_RELEASE,
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700145 /*
146 * A thread in rmdir() is wating for this cgroup.
147 */
148 CGRP_WAIT_ON_RMDIR,
Paul Menage3116f0e2008-04-29 01:00:04 -0700149};
150
Ben Blum72a8cb32009-09-23 15:56:27 -0700151/* which pidlist file are we talking about? */
152enum cgroup_filetype {
153 CGROUP_FILE_PROCS,
154 CGROUP_FILE_TASKS,
155};
156
157/*
158 * A pidlist is a list of pids that virtually represents the contents of one
159 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
160 * a pair (one each for procs, tasks) for each pid namespace that's relevant
161 * to the cgroup.
162 */
Ben Blum102a7752009-09-23 15:56:26 -0700163struct cgroup_pidlist {
Ben Blum72a8cb32009-09-23 15:56:27 -0700164 /*
165 * used to find which pidlist is wanted. doesn't change as long as
166 * this particular list stays in the list.
167 */
168 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
Ben Blum102a7752009-09-23 15:56:26 -0700169 /* array of xids */
170 pid_t *list;
171 /* how many elements the above list has */
172 int length;
173 /* how many files are using the current array */
174 int use_count;
Ben Blum72a8cb32009-09-23 15:56:27 -0700175 /* each of these stored in a list by its cgroup */
176 struct list_head links;
177 /* pointer to the cgroup we belong to, for list removal purposes */
178 struct cgroup *owner;
179 /* protects the other fields */
180 struct rw_semaphore mutex;
Ben Blum102a7752009-09-23 15:56:26 -0700181};
182
Paul Menageddbcc7e2007-10-18 23:39:30 -0700183struct cgroup {
184 unsigned long flags; /* "unsigned long" so bitops work */
185
Paul Menaged20a3902009-04-02 16:57:22 -0700186 /*
187 * count users of this cgroup. >0 means busy, but doesn't
188 * necessarily indicate the number of tasks in the cgroup
189 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700190 atomic_t count;
191
192 /*
193 * We link our 'sibling' struct into our parent's 'children'.
194 * Our children link their 'sibling' into our 'children'.
195 */
196 struct list_head sibling; /* my parent's children */
197 struct list_head children; /* my children */
198
Paul Menaged20a3902009-04-02 16:57:22 -0700199 struct cgroup *parent; /* my parent */
Paul Menagea47295e2009-01-07 18:07:44 -0800200 struct dentry *dentry; /* cgroup fs entry, RCU protected */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700201
202 /* Private pointers for each registered subsystem */
203 struct cgroup_subsys_state *subsys[CGROUP_SUBSYS_COUNT];
204
205 struct cgroupfs_root *root;
206 struct cgroup *top_cgroup;
Paul Menage817929e2007-10-18 23:39:36 -0700207
208 /*
209 * List of cg_cgroup_links pointing at css_sets with
210 * tasks in this cgroup. Protected by css_set_lock
211 */
212 struct list_head css_sets;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700213
214 /*
215 * Linked list running through all cgroups that can
216 * potentially be reaped by the release agent. Protected by
217 * release_list_lock
218 */
219 struct list_head release_list;
Paul Menagecc31edc2008-10-18 20:28:04 -0700220
Ben Blum72a8cb32009-09-23 15:56:27 -0700221 /*
222 * list of pidlists, up to two for each namespace (one for procs, one
223 * for tasks); created on demand.
224 */
225 struct list_head pidlists;
226 struct mutex pidlist_mutex;
Paul Menagea47295e2009-01-07 18:07:44 -0800227
228 /* For RCU-protected deletion */
229 struct rcu_head rcu_head;
Paul Menage817929e2007-10-18 23:39:36 -0700230};
231
Paul Menaged20a3902009-04-02 16:57:22 -0700232/*
233 * A css_set is a structure holding pointers to a set of
Paul Menage817929e2007-10-18 23:39:36 -0700234 * cgroup_subsys_state objects. This saves space in the task struct
235 * object and speeds up fork()/exit(), since a single inc/dec and a
Paul Menaged20a3902009-04-02 16:57:22 -0700236 * list_add()/del() can bump the reference count on the entire cgroup
237 * set for a task.
Paul Menage817929e2007-10-18 23:39:36 -0700238 */
239
240struct css_set {
241
242 /* Reference count */
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700243 atomic_t refcount;
Paul Menage817929e2007-10-18 23:39:36 -0700244
245 /*
Li Zefan472b1052008-04-29 01:00:11 -0700246 * List running through all cgroup groups in the same hash
247 * slot. Protected by css_set_lock
248 */
249 struct hlist_node hlist;
250
251 /*
Paul Menage817929e2007-10-18 23:39:36 -0700252 * List running through all tasks using this cgroup
253 * group. Protected by css_set_lock
254 */
255 struct list_head tasks;
256
257 /*
258 * List of cg_cgroup_link objects on link chains from
259 * cgroups referenced from this css_set. Protected by
260 * css_set_lock
261 */
262 struct list_head cg_links;
263
264 /*
265 * Set of subsystem states, one for each subsystem. This array
266 * is immutable after creation apart from the init_css_set
267 * during subsystem registration (at boot time).
268 */
269 struct cgroup_subsys_state *subsys[CGROUP_SUBSYS_COUNT];
Ben Blumc3783692009-09-23 15:56:29 -0700270
271 /* For RCU-protected deletion */
272 struct rcu_head rcu_head;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700273};
274
Paul Menage91796562008-04-29 01:00:01 -0700275/*
276 * cgroup_map_cb is an abstract callback API for reporting map-valued
277 * control files
278 */
279
280struct cgroup_map_cb {
281 int (*fill)(struct cgroup_map_cb *cb, const char *key, u64 value);
282 void *state;
283};
284
Paul Menaged20a3902009-04-02 16:57:22 -0700285/*
286 * struct cftype: handler definitions for cgroup control files
Paul Menageddbcc7e2007-10-18 23:39:30 -0700287 *
288 * When reading/writing to a file:
Li Zefana043e3b2008-02-23 15:24:09 -0800289 * - the cgroup to use is file->f_dentry->d_parent->d_fsdata
Paul Menageddbcc7e2007-10-18 23:39:30 -0700290 * - the 'cftype' of the file is file->f_dentry->d_fsdata
291 */
292
293#define MAX_CFTYPE_NAME 64
294struct cftype {
Paul Menaged20a3902009-04-02 16:57:22 -0700295 /*
296 * By convention, the name should begin with the name of the
297 * subsystem, followed by a period
298 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700299 char name[MAX_CFTYPE_NAME];
300 int private;
Li Zefan099fca32009-04-02 16:57:29 -0700301 /*
302 * If not 0, file mode is set to this value, otherwise it will
303 * be figured out automatically
304 */
305 mode_t mode;
Paul Menagedb3b1492008-07-25 01:46:58 -0700306
307 /*
308 * If non-zero, defines the maximum length of string that can
309 * be passed to write_string; defaults to 64
310 */
311 size_t max_write_len;
312
Paul Menagece16b492008-07-25 01:46:57 -0700313 int (*open)(struct inode *inode, struct file *file);
314 ssize_t (*read)(struct cgroup *cgrp, struct cftype *cft,
315 struct file *file,
316 char __user *buf, size_t nbytes, loff_t *ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700317 /*
Paul Menagef4c753b2008-04-29 00:59:56 -0700318 * read_u64() is a shortcut for the common case of returning a
Paul Menageddbcc7e2007-10-18 23:39:30 -0700319 * single integer. Use it in place of read()
320 */
Paul Menagece16b492008-07-25 01:46:57 -0700321 u64 (*read_u64)(struct cgroup *cgrp, struct cftype *cft);
Paul Menage91796562008-04-29 01:00:01 -0700322 /*
Paul Menagee73d2c62008-04-29 01:00:06 -0700323 * read_s64() is a signed version of read_u64()
324 */
Paul Menagece16b492008-07-25 01:46:57 -0700325 s64 (*read_s64)(struct cgroup *cgrp, struct cftype *cft);
Paul Menagee73d2c62008-04-29 01:00:06 -0700326 /*
Paul Menage91796562008-04-29 01:00:01 -0700327 * read_map() is used for defining a map of key/value
328 * pairs. It should call cb->fill(cb, key, value) for each
329 * entry. The key/value pairs (and their ordering) should not
330 * change between reboots.
331 */
Paul Menagece16b492008-07-25 01:46:57 -0700332 int (*read_map)(struct cgroup *cont, struct cftype *cft,
333 struct cgroup_map_cb *cb);
Serge E. Hallyn29486df2008-04-29 01:00:14 -0700334 /*
335 * read_seq_string() is used for outputting a simple sequence
336 * using seqfile.
337 */
Paul Menagece16b492008-07-25 01:46:57 -0700338 int (*read_seq_string)(struct cgroup *cont, struct cftype *cft,
339 struct seq_file *m);
Paul Menage91796562008-04-29 01:00:01 -0700340
Paul Menagece16b492008-07-25 01:46:57 -0700341 ssize_t (*write)(struct cgroup *cgrp, struct cftype *cft,
342 struct file *file,
343 const char __user *buf, size_t nbytes, loff_t *ppos);
Paul Menage355e0c42007-10-18 23:39:33 -0700344
345 /*
Paul Menagef4c753b2008-04-29 00:59:56 -0700346 * write_u64() is a shortcut for the common case of accepting
Paul Menage355e0c42007-10-18 23:39:33 -0700347 * a single integer (as parsed by simple_strtoull) from
348 * userspace. Use in place of write(); return 0 or error.
349 */
Paul Menagece16b492008-07-25 01:46:57 -0700350 int (*write_u64)(struct cgroup *cgrp, struct cftype *cft, u64 val);
Paul Menagee73d2c62008-04-29 01:00:06 -0700351 /*
352 * write_s64() is a signed version of write_u64()
353 */
Paul Menagece16b492008-07-25 01:46:57 -0700354 int (*write_s64)(struct cgroup *cgrp, struct cftype *cft, s64 val);
Paul Menage355e0c42007-10-18 23:39:33 -0700355
Pavel Emelyanovd447ea22008-04-29 01:00:08 -0700356 /*
Paul Menagedb3b1492008-07-25 01:46:58 -0700357 * write_string() is passed a nul-terminated kernelspace
358 * buffer of maximum length determined by max_write_len.
359 * Returns 0 or -ve error code.
360 */
361 int (*write_string)(struct cgroup *cgrp, struct cftype *cft,
362 const char *buffer);
363 /*
Pavel Emelyanovd447ea22008-04-29 01:00:08 -0700364 * trigger() callback can be used to get some kick from the
365 * userspace, when the actual string written is not important
366 * at all. The private field can be used to determine the
367 * kick type for multiplexing.
368 */
369 int (*trigger)(struct cgroup *cgrp, unsigned int event);
370
Paul Menagece16b492008-07-25 01:46:57 -0700371 int (*release)(struct inode *inode, struct file *file);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700372};
373
Cliff Wickman31a7df02008-02-07 00:14:42 -0800374struct cgroup_scanner {
375 struct cgroup *cg;
376 int (*test_task)(struct task_struct *p, struct cgroup_scanner *scan);
377 void (*process_task)(struct task_struct *p,
378 struct cgroup_scanner *scan);
379 struct ptr_heap *heap;
Li Zefanbd1a8ab2009-04-02 16:57:50 -0700380 void *data;
Cliff Wickman31a7df02008-02-07 00:14:42 -0800381};
382
Paul Menaged20a3902009-04-02 16:57:22 -0700383/*
384 * Add a new file to the given cgroup directory. Should only be
385 * called by subsystems from within a populate() method
386 */
Li Zefanffd2d882008-02-23 15:24:09 -0800387int cgroup_add_file(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Paul Menageddbcc7e2007-10-18 23:39:30 -0700388 const struct cftype *cft);
389
Paul Menaged20a3902009-04-02 16:57:22 -0700390/*
391 * Add a set of new files to the given cgroup directory. Should
392 * only be called by subsystems from within a populate() method
393 */
Li Zefanffd2d882008-02-23 15:24:09 -0800394int cgroup_add_files(struct cgroup *cgrp,
Paul Menageddbcc7e2007-10-18 23:39:30 -0700395 struct cgroup_subsys *subsys,
396 const struct cftype cft[],
397 int count);
398
Li Zefanffd2d882008-02-23 15:24:09 -0800399int cgroup_is_removed(const struct cgroup *cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700400
Li Zefanffd2d882008-02-23 15:24:09 -0800401int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700402
Li Zefanffd2d882008-02-23 15:24:09 -0800403int cgroup_task_count(const struct cgroup *cgrp);
Paul Menagebbcb81d2007-10-18 23:39:32 -0700404
Grzegorz Nosek313e9242009-04-02 16:57:23 -0700405/* Return true if cgrp is a descendant of the task's cgroup */
406int cgroup_is_descendant(const struct cgroup *cgrp, struct task_struct *task);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700407
Thadeu Lima de Souza Cascardo21acb9c2009-02-04 10:12:08 +0100408/*
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -0700409 * When the subsys has to access css and may add permanent refcnt to css,
410 * it should take care of racy conditions with rmdir(). Following set of
411 * functions, is for stop/restart rmdir if necessary.
412 * Because these will call css_get/put, "css" should be alive css.
413 *
414 * cgroup_exclude_rmdir();
415 * ...do some jobs which may access arbitrary empty cgroup
416 * cgroup_release_and_wakeup_rmdir();
417 *
418 * When someone removes a cgroup while cgroup_exclude_rmdir() holds it,
419 * it sleeps and cgroup_release_and_wakeup_rmdir() will wake him up.
420 */
421
422void cgroup_exclude_rmdir(struct cgroup_subsys_state *css);
423void cgroup_release_and_wakeup_rmdir(struct cgroup_subsys_state *css);
424
425/*
Thadeu Lima de Souza Cascardo21acb9c2009-02-04 10:12:08 +0100426 * Control Group subsystem type.
427 * See Documentation/cgroups/cgroups.txt for details
428 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700429
430struct cgroup_subsys {
431 struct cgroup_subsys_state *(*create)(struct cgroup_subsys *ss,
Li Zefanffd2d882008-02-23 15:24:09 -0800432 struct cgroup *cgrp);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700433 int (*pre_destroy)(struct cgroup_subsys *ss, struct cgroup *cgrp);
Li Zefanffd2d882008-02-23 15:24:09 -0800434 void (*destroy)(struct cgroup_subsys *ss, struct cgroup *cgrp);
Ben Blumbe367d02009-09-23 15:56:31 -0700435 int (*can_attach)(struct cgroup_subsys *ss, struct cgroup *cgrp,
436 struct task_struct *tsk, bool threadgroup);
Daisuke Nishimura2468c722010-03-10 15:22:03 -0800437 void (*cancel_attach)(struct cgroup_subsys *ss, struct cgroup *cgrp,
438 struct task_struct *tsk, bool threadgroup);
Li Zefanffd2d882008-02-23 15:24:09 -0800439 void (*attach)(struct cgroup_subsys *ss, struct cgroup *cgrp,
Ben Blumbe367d02009-09-23 15:56:31 -0700440 struct cgroup *old_cgrp, struct task_struct *tsk,
441 bool threadgroup);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700442 void (*fork)(struct cgroup_subsys *ss, struct task_struct *task);
443 void (*exit)(struct cgroup_subsys *ss, struct task_struct *task);
444 int (*populate)(struct cgroup_subsys *ss,
Li Zefanffd2d882008-02-23 15:24:09 -0800445 struct cgroup *cgrp);
446 void (*post_clone)(struct cgroup_subsys *ss, struct cgroup *cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700447 void (*bind)(struct cgroup_subsys *ss, struct cgroup *root);
Hugh Dickinse5991372009-01-06 14:39:22 -0800448
Paul Menageddbcc7e2007-10-18 23:39:30 -0700449 int subsys_id;
450 int active;
Paul Menage8bab8dd2008-04-04 14:29:57 -0700451 int disabled;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700452 int early_init;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700453 /*
454 * True if this subsys uses ID. ID is not available before cgroup_init()
455 * (not available in early_init time.)
456 */
457 bool use_id;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700458#define MAX_CGROUP_TYPE_NAMELEN 32
459 const char *name;
460
Paul Menage999cd8a2009-01-07 18:08:36 -0800461 /*
462 * Protects sibling/children links of cgroups in this
463 * hierarchy, plus protects which hierarchy (or none) the
464 * subsystem is a part of (i.e. root/sibling). To avoid
465 * potential deadlocks, the following operations should not be
466 * undertaken while holding any hierarchy_mutex:
467 *
468 * - allocating memory
469 * - initiating hotplug events
470 */
471 struct mutex hierarchy_mutex;
Li Zefancfebe562009-02-11 13:04:36 -0800472 struct lock_class_key subsys_key;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700473
Paul Menage999cd8a2009-01-07 18:08:36 -0800474 /*
475 * Link to parent, and list entry in parent's children.
476 * Protected by this->hierarchy_mutex and cgroup_lock()
477 */
478 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700479 struct list_head sibling;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700480 /* used when use_id == true */
481 struct idr idr;
482 spinlock_t id_lock;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700483};
484
485#define SUBSYS(_x) extern struct cgroup_subsys _x ## _subsys;
486#include <linux/cgroup_subsys.h>
487#undef SUBSYS
488
489static inline struct cgroup_subsys_state *cgroup_subsys_state(
Li Zefanffd2d882008-02-23 15:24:09 -0800490 struct cgroup *cgrp, int subsys_id)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700491{
Li Zefanffd2d882008-02-23 15:24:09 -0800492 return cgrp->subsys[subsys_id];
Paul Menageddbcc7e2007-10-18 23:39:30 -0700493}
494
495static inline struct cgroup_subsys_state *task_subsys_state(
496 struct task_struct *task, int subsys_id)
497{
Paul E. McKenneyd11c5632010-02-22 17:04:50 -0800498 return rcu_dereference_check(task->cgroups->subsys[subsys_id],
499 rcu_read_lock_held() ||
500 cgroup_lock_is_held());
Paul Menageddbcc7e2007-10-18 23:39:30 -0700501}
502
503static inline struct cgroup* task_cgroup(struct task_struct *task,
504 int subsys_id)
505{
506 return task_subsys_state(task, subsys_id)->cgroup;
507}
508
Serge E. Hallyne885dcd2008-07-25 01:47:06 -0700509int cgroup_clone(struct task_struct *tsk, struct cgroup_subsys *ss,
510 char *nodename);
Paul Menage697f4162007-10-18 23:39:34 -0700511
Paul Menage817929e2007-10-18 23:39:36 -0700512/* A cgroup_iter should be treated as an opaque object */
513struct cgroup_iter {
514 struct list_head *cg_link;
515 struct list_head *task;
516};
517
Paul Menaged20a3902009-04-02 16:57:22 -0700518/*
519 * To iterate across the tasks in a cgroup:
Paul Menage817929e2007-10-18 23:39:36 -0700520 *
521 * 1) call cgroup_iter_start to intialize an iterator
522 *
523 * 2) call cgroup_iter_next() to retrieve member tasks until it
524 * returns NULL or until you want to end the iteration
525 *
526 * 3) call cgroup_iter_end() to destroy the iterator.
Cliff Wickman31a7df02008-02-07 00:14:42 -0800527 *
Paul Menaged20a3902009-04-02 16:57:22 -0700528 * Or, call cgroup_scan_tasks() to iterate through every task in a
529 * cgroup - cgroup_scan_tasks() holds the css_set_lock when calling
530 * the test_task() callback, but not while calling the process_task()
531 * callback.
Paul Menage817929e2007-10-18 23:39:36 -0700532 */
Li Zefanffd2d882008-02-23 15:24:09 -0800533void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it);
534struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -0700535 struct cgroup_iter *it);
Li Zefanffd2d882008-02-23 15:24:09 -0800536void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it);
Cliff Wickman31a7df02008-02-07 00:14:42 -0800537int cgroup_scan_tasks(struct cgroup_scanner *scan);
Cliff Wickman956db3c2008-02-07 00:14:43 -0800538int cgroup_attach_task(struct cgroup *, struct task_struct *);
Paul Menage817929e2007-10-18 23:39:36 -0700539
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700540/*
541 * CSS ID is ID for cgroup_subsys_state structs under subsys. This only works
542 * if cgroup_subsys.use_id == true. It can be used for looking up and scanning.
543 * CSS ID is assigned at cgroup allocation (create) automatically
544 * and removed when subsys calls free_css_id() function. This is because
545 * the lifetime of cgroup_subsys_state is subsys's matter.
546 *
547 * Looking up and scanning function should be called under rcu_read_lock().
548 * Taking cgroup_mutex()/hierarchy_mutex() is not necessary for following calls.
549 * But the css returned by this routine can be "not populated yet" or "being
550 * destroyed". The caller should check css and cgroup's status.
551 */
552
553/*
554 * Typically Called at ->destroy(), or somewhere the subsys frees
555 * cgroup_subsys_state.
556 */
557void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css);
558
559/* Find a cgroup_subsys_state which has given ID */
560
561struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id);
562
563/*
564 * Get a cgroup whose id is greater than or equal to id under tree of root.
565 * Returning a cgroup_subsys_state or NULL.
566 */
567struct cgroup_subsys_state *css_get_next(struct cgroup_subsys *ss, int id,
568 struct cgroup_subsys_state *root, int *foundid);
569
570/* Returns true if root is ancestor of cg */
571bool css_is_ancestor(struct cgroup_subsys_state *cg,
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700572 const struct cgroup_subsys_state *root);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700573
574/* Get id and depth of css */
575unsigned short css_id(struct cgroup_subsys_state *css);
576unsigned short css_depth(struct cgroup_subsys_state *css);
577
Paul Menageddbcc7e2007-10-18 23:39:30 -0700578#else /* !CONFIG_CGROUPS */
579
580static inline int cgroup_init_early(void) { return 0; }
581static inline int cgroup_init(void) { return 0; }
Paul Menageb4f48b62007-10-18 23:39:33 -0700582static inline void cgroup_fork(struct task_struct *p) {}
583static inline void cgroup_fork_callbacks(struct task_struct *p) {}
Paul Menage817929e2007-10-18 23:39:36 -0700584static inline void cgroup_post_fork(struct task_struct *p) {}
Paul Menageb4f48b62007-10-18 23:39:33 -0700585static inline void cgroup_exit(struct task_struct *p, int callbacks) {}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700586
587static inline void cgroup_lock(void) {}
588static inline void cgroup_unlock(void) {}
Balbir Singh846c7bb2007-10-18 23:39:44 -0700589static inline int cgroupstats_build(struct cgroupstats *stats,
590 struct dentry *dentry)
591{
592 return -EINVAL;
593}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700594
595#endif /* !CONFIG_CGROUPS */
596
Paul Menageddbcc7e2007-10-18 23:39:30 -0700597#endif /* _LINUX_CGROUP_H */