blob: 28319a9fe5696fdf5dce8e8ea63db17438523580 [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
Ben Blumaae8aab2010-03-10 15:22:07 -080043/* Define the enumeration of all builtin cgroup subsystems */
Paul Menage817929e2007-10-18 23:39:36 -070044#define SUBSYS(_x) _x ## _subsys_id,
45enum cgroup_subsys_id {
46#include <linux/cgroup_subsys.h>
Ben Blumaae8aab2010-03-10 15:22:07 -080047 CGROUP_BUILTIN_SUBSYS_COUNT
Paul Menage817929e2007-10-18 23:39:36 -070048};
49#undef SUBSYS
Ben Blumaae8aab2010-03-10 15:22:07 -080050/*
51 * This define indicates the maximum number of subsystems that can be loaded
52 * at once. We limit to this many since cgroupfs_root has subsys_bits to keep
53 * track of all of them.
54 */
55#define CGROUP_SUBSYS_COUNT (BITS_PER_BYTE*sizeof(unsigned long))
Paul Menage817929e2007-10-18 23:39:36 -070056
Paul Menageddbcc7e2007-10-18 23:39:30 -070057/* Per-subsystem/per-cgroup state maintained by the system. */
58struct cgroup_subsys_state {
Paul Menaged20a3902009-04-02 16:57:22 -070059 /*
60 * The cgroup that this subsystem is attached to. Useful
Paul Menageddbcc7e2007-10-18 23:39:30 -070061 * for subsystems that want to know about the cgroup
Paul Menaged20a3902009-04-02 16:57:22 -070062 * hierarchy structure
63 */
Paul Menageddbcc7e2007-10-18 23:39:30 -070064 struct cgroup *cgroup;
65
Paul Menaged20a3902009-04-02 16:57:22 -070066 /*
67 * State maintained by the cgroup system to allow subsystems
Paul Menagee7c5ec92009-01-07 18:08:38 -080068 * to be "busy". Should be accessed via css_get(),
Paul Menaged20a3902009-04-02 16:57:22 -070069 * css_tryget() and and css_put().
70 */
Paul Menageddbcc7e2007-10-18 23:39:30 -070071
72 atomic_t refcnt;
73
74 unsigned long flags;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -070075 /* ID for this css, if possible */
76 struct css_id *id;
Paul Menageddbcc7e2007-10-18 23:39:30 -070077};
78
79/* bits in struct cgroup_subsys_state flags field */
80enum {
81 CSS_ROOT, /* This CSS is the root of the subsystem */
Paul Menagee7c5ec92009-01-07 18:08:38 -080082 CSS_REMOVED, /* This CSS is dead */
Paul Menageddbcc7e2007-10-18 23:39:30 -070083};
84
Daisuke Nishimurad7b9fff2010-03-10 15:22:05 -080085/* Caller must verify that the css is not for root cgroup */
86static inline void __css_get(struct cgroup_subsys_state *css, int count)
87{
88 atomic_add(count, &css->refcnt);
89}
90
Paul Menageddbcc7e2007-10-18 23:39:30 -070091/*
Paul Menagee7c5ec92009-01-07 18:08:38 -080092 * Call css_get() to hold a reference on the css; it can be used
93 * for a reference obtained via:
94 * - an existing ref-counted reference to the css
95 * - task->cgroups for a locked task
Paul Menageddbcc7e2007-10-18 23:39:30 -070096 */
97
98static inline void css_get(struct cgroup_subsys_state *css)
99{
100 /* We don't need to reference count the root state */
101 if (!test_bit(CSS_ROOT, &css->flags))
Daisuke Nishimurad7b9fff2010-03-10 15:22:05 -0800102 __css_get(css, 1);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700103}
Paul Menagee7c5ec92009-01-07 18:08:38 -0800104
105static inline bool css_is_removed(struct cgroup_subsys_state *css)
106{
107 return test_bit(CSS_REMOVED, &css->flags);
108}
109
110/*
111 * Call css_tryget() to take a reference on a css if your existing
112 * (known-valid) reference isn't already ref-counted. Returns false if
113 * the css has been destroyed.
114 */
115
116static inline bool css_tryget(struct cgroup_subsys_state *css)
117{
118 if (test_bit(CSS_ROOT, &css->flags))
119 return true;
120 while (!atomic_inc_not_zero(&css->refcnt)) {
121 if (test_bit(CSS_REMOVED, &css->flags))
122 return false;
Paul Menage804b3c22009-01-29 14:25:21 -0800123 cpu_relax();
Paul Menagee7c5ec92009-01-07 18:08:38 -0800124 }
125 return true;
126}
127
Paul Menageddbcc7e2007-10-18 23:39:30 -0700128/*
129 * css_put() should be called to release a reference taken by
Paul Menagee7c5ec92009-01-07 18:08:38 -0800130 * css_get() or css_tryget()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700131 */
132
Daisuke Nishimurad7b9fff2010-03-10 15:22:05 -0800133extern void __css_put(struct cgroup_subsys_state *css, int count);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700134static inline void css_put(struct cgroup_subsys_state *css)
135{
136 if (!test_bit(CSS_ROOT, &css->flags))
Daisuke Nishimurad7b9fff2010-03-10 15:22:05 -0800137 __css_put(css, 1);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700138}
139
Paul Menage3116f0e2008-04-29 01:00:04 -0700140/* bits in struct cgroup flags field */
141enum {
142 /* Control Group is dead */
143 CGRP_REMOVED,
Paul Menaged20a3902009-04-02 16:57:22 -0700144 /*
145 * Control Group has previously had a child cgroup or a task,
146 * but no longer (only if CGRP_NOTIFY_ON_RELEASE is set)
147 */
Paul Menage3116f0e2008-04-29 01:00:04 -0700148 CGRP_RELEASABLE,
149 /* Control Group requires release notifications to userspace */
150 CGRP_NOTIFY_ON_RELEASE,
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700151 /*
152 * A thread in rmdir() is wating for this cgroup.
153 */
154 CGRP_WAIT_ON_RMDIR,
Paul Menage3116f0e2008-04-29 01:00:04 -0700155};
156
Ben Blum72a8cb32009-09-23 15:56:27 -0700157/* which pidlist file are we talking about? */
158enum cgroup_filetype {
159 CGROUP_FILE_PROCS,
160 CGROUP_FILE_TASKS,
161};
162
163/*
164 * A pidlist is a list of pids that virtually represents the contents of one
165 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
166 * a pair (one each for procs, tasks) for each pid namespace that's relevant
167 * to the cgroup.
168 */
Ben Blum102a7752009-09-23 15:56:26 -0700169struct cgroup_pidlist {
Ben Blum72a8cb32009-09-23 15:56:27 -0700170 /*
171 * used to find which pidlist is wanted. doesn't change as long as
172 * this particular list stays in the list.
173 */
174 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
Ben Blum102a7752009-09-23 15:56:26 -0700175 /* array of xids */
176 pid_t *list;
177 /* how many elements the above list has */
178 int length;
179 /* how many files are using the current array */
180 int use_count;
Ben Blum72a8cb32009-09-23 15:56:27 -0700181 /* each of these stored in a list by its cgroup */
182 struct list_head links;
183 /* pointer to the cgroup we belong to, for list removal purposes */
184 struct cgroup *owner;
185 /* protects the other fields */
186 struct rw_semaphore mutex;
Ben Blum102a7752009-09-23 15:56:26 -0700187};
188
Paul Menageddbcc7e2007-10-18 23:39:30 -0700189struct cgroup {
190 unsigned long flags; /* "unsigned long" so bitops work */
191
Paul Menaged20a3902009-04-02 16:57:22 -0700192 /*
193 * count users of this cgroup. >0 means busy, but doesn't
194 * necessarily indicate the number of tasks in the cgroup
195 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700196 atomic_t count;
197
198 /*
199 * We link our 'sibling' struct into our parent's 'children'.
200 * Our children link their 'sibling' into our 'children'.
201 */
202 struct list_head sibling; /* my parent's children */
203 struct list_head children; /* my children */
204
Paul Menaged20a3902009-04-02 16:57:22 -0700205 struct cgroup *parent; /* my parent */
Paul Menagea47295e2009-01-07 18:07:44 -0800206 struct dentry *dentry; /* cgroup fs entry, RCU protected */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700207
208 /* Private pointers for each registered subsystem */
209 struct cgroup_subsys_state *subsys[CGROUP_SUBSYS_COUNT];
210
211 struct cgroupfs_root *root;
212 struct cgroup *top_cgroup;
Paul Menage817929e2007-10-18 23:39:36 -0700213
214 /*
215 * List of cg_cgroup_links pointing at css_sets with
216 * tasks in this cgroup. Protected by css_set_lock
217 */
218 struct list_head css_sets;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700219
220 /*
221 * Linked list running through all cgroups that can
222 * potentially be reaped by the release agent. Protected by
223 * release_list_lock
224 */
225 struct list_head release_list;
Paul Menagecc31edc2008-10-18 20:28:04 -0700226
Ben Blum72a8cb32009-09-23 15:56:27 -0700227 /*
228 * list of pidlists, up to two for each namespace (one for procs, one
229 * for tasks); created on demand.
230 */
231 struct list_head pidlists;
232 struct mutex pidlist_mutex;
Paul Menagea47295e2009-01-07 18:07:44 -0800233
234 /* For RCU-protected deletion */
235 struct rcu_head rcu_head;
Paul Menage817929e2007-10-18 23:39:36 -0700236};
237
Paul Menaged20a3902009-04-02 16:57:22 -0700238/*
239 * A css_set is a structure holding pointers to a set of
Paul Menage817929e2007-10-18 23:39:36 -0700240 * cgroup_subsys_state objects. This saves space in the task struct
241 * object and speeds up fork()/exit(), since a single inc/dec and a
Paul Menaged20a3902009-04-02 16:57:22 -0700242 * list_add()/del() can bump the reference count on the entire cgroup
243 * set for a task.
Paul Menage817929e2007-10-18 23:39:36 -0700244 */
245
246struct css_set {
247
248 /* Reference count */
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700249 atomic_t refcount;
Paul Menage817929e2007-10-18 23:39:36 -0700250
251 /*
Li Zefan472b1052008-04-29 01:00:11 -0700252 * List running through all cgroup groups in the same hash
253 * slot. Protected by css_set_lock
254 */
255 struct hlist_node hlist;
256
257 /*
Paul Menage817929e2007-10-18 23:39:36 -0700258 * List running through all tasks using this cgroup
259 * group. Protected by css_set_lock
260 */
261 struct list_head tasks;
262
263 /*
264 * List of cg_cgroup_link objects on link chains from
265 * cgroups referenced from this css_set. Protected by
266 * css_set_lock
267 */
268 struct list_head cg_links;
269
270 /*
271 * Set of subsystem states, one for each subsystem. This array
272 * is immutable after creation apart from the init_css_set
273 * during subsystem registration (at boot time).
274 */
275 struct cgroup_subsys_state *subsys[CGROUP_SUBSYS_COUNT];
Ben Blumc3783692009-09-23 15:56:29 -0700276
277 /* For RCU-protected deletion */
278 struct rcu_head rcu_head;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700279};
280
Paul Menage91796562008-04-29 01:00:01 -0700281/*
282 * cgroup_map_cb is an abstract callback API for reporting map-valued
283 * control files
284 */
285
286struct cgroup_map_cb {
287 int (*fill)(struct cgroup_map_cb *cb, const char *key, u64 value);
288 void *state;
289};
290
Paul Menaged20a3902009-04-02 16:57:22 -0700291/*
292 * struct cftype: handler definitions for cgroup control files
Paul Menageddbcc7e2007-10-18 23:39:30 -0700293 *
294 * When reading/writing to a file:
Li Zefana043e3b2008-02-23 15:24:09 -0800295 * - the cgroup to use is file->f_dentry->d_parent->d_fsdata
Paul Menageddbcc7e2007-10-18 23:39:30 -0700296 * - the 'cftype' of the file is file->f_dentry->d_fsdata
297 */
298
299#define MAX_CFTYPE_NAME 64
300struct cftype {
Paul Menaged20a3902009-04-02 16:57:22 -0700301 /*
302 * By convention, the name should begin with the name of the
303 * subsystem, followed by a period
304 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700305 char name[MAX_CFTYPE_NAME];
306 int private;
Li Zefan099fca32009-04-02 16:57:29 -0700307 /*
308 * If not 0, file mode is set to this value, otherwise it will
309 * be figured out automatically
310 */
311 mode_t mode;
Paul Menagedb3b1492008-07-25 01:46:58 -0700312
313 /*
314 * If non-zero, defines the maximum length of string that can
315 * be passed to write_string; defaults to 64
316 */
317 size_t max_write_len;
318
Paul Menagece16b492008-07-25 01:46:57 -0700319 int (*open)(struct inode *inode, struct file *file);
320 ssize_t (*read)(struct cgroup *cgrp, struct cftype *cft,
321 struct file *file,
322 char __user *buf, size_t nbytes, loff_t *ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700323 /*
Paul Menagef4c753b2008-04-29 00:59:56 -0700324 * read_u64() is a shortcut for the common case of returning a
Paul Menageddbcc7e2007-10-18 23:39:30 -0700325 * single integer. Use it in place of read()
326 */
Paul Menagece16b492008-07-25 01:46:57 -0700327 u64 (*read_u64)(struct cgroup *cgrp, struct cftype *cft);
Paul Menage91796562008-04-29 01:00:01 -0700328 /*
Paul Menagee73d2c62008-04-29 01:00:06 -0700329 * read_s64() is a signed version of read_u64()
330 */
Paul Menagece16b492008-07-25 01:46:57 -0700331 s64 (*read_s64)(struct cgroup *cgrp, struct cftype *cft);
Paul Menagee73d2c62008-04-29 01:00:06 -0700332 /*
Paul Menage91796562008-04-29 01:00:01 -0700333 * read_map() is used for defining a map of key/value
334 * pairs. It should call cb->fill(cb, key, value) for each
335 * entry. The key/value pairs (and their ordering) should not
336 * change between reboots.
337 */
Paul Menagece16b492008-07-25 01:46:57 -0700338 int (*read_map)(struct cgroup *cont, struct cftype *cft,
339 struct cgroup_map_cb *cb);
Serge E. Hallyn29486df2008-04-29 01:00:14 -0700340 /*
341 * read_seq_string() is used for outputting a simple sequence
342 * using seqfile.
343 */
Paul Menagece16b492008-07-25 01:46:57 -0700344 int (*read_seq_string)(struct cgroup *cont, struct cftype *cft,
345 struct seq_file *m);
Paul Menage91796562008-04-29 01:00:01 -0700346
Paul Menagece16b492008-07-25 01:46:57 -0700347 ssize_t (*write)(struct cgroup *cgrp, struct cftype *cft,
348 struct file *file,
349 const char __user *buf, size_t nbytes, loff_t *ppos);
Paul Menage355e0c42007-10-18 23:39:33 -0700350
351 /*
Paul Menagef4c753b2008-04-29 00:59:56 -0700352 * write_u64() is a shortcut for the common case of accepting
Paul Menage355e0c42007-10-18 23:39:33 -0700353 * a single integer (as parsed by simple_strtoull) from
354 * userspace. Use in place of write(); return 0 or error.
355 */
Paul Menagece16b492008-07-25 01:46:57 -0700356 int (*write_u64)(struct cgroup *cgrp, struct cftype *cft, u64 val);
Paul Menagee73d2c62008-04-29 01:00:06 -0700357 /*
358 * write_s64() is a signed version of write_u64()
359 */
Paul Menagece16b492008-07-25 01:46:57 -0700360 int (*write_s64)(struct cgroup *cgrp, struct cftype *cft, s64 val);
Paul Menage355e0c42007-10-18 23:39:33 -0700361
Pavel Emelyanovd447ea22008-04-29 01:00:08 -0700362 /*
Paul Menagedb3b1492008-07-25 01:46:58 -0700363 * write_string() is passed a nul-terminated kernelspace
364 * buffer of maximum length determined by max_write_len.
365 * Returns 0 or -ve error code.
366 */
367 int (*write_string)(struct cgroup *cgrp, struct cftype *cft,
368 const char *buffer);
369 /*
Pavel Emelyanovd447ea22008-04-29 01:00:08 -0700370 * trigger() callback can be used to get some kick from the
371 * userspace, when the actual string written is not important
372 * at all. The private field can be used to determine the
373 * kick type for multiplexing.
374 */
375 int (*trigger)(struct cgroup *cgrp, unsigned int event);
376
Paul Menagece16b492008-07-25 01:46:57 -0700377 int (*release)(struct inode *inode, struct file *file);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700378};
379
Cliff Wickman31a7df02008-02-07 00:14:42 -0800380struct cgroup_scanner {
381 struct cgroup *cg;
382 int (*test_task)(struct task_struct *p, struct cgroup_scanner *scan);
383 void (*process_task)(struct task_struct *p,
384 struct cgroup_scanner *scan);
385 struct ptr_heap *heap;
Li Zefanbd1a8ab2009-04-02 16:57:50 -0700386 void *data;
Cliff Wickman31a7df02008-02-07 00:14:42 -0800387};
388
Paul Menaged20a3902009-04-02 16:57:22 -0700389/*
390 * Add a new file to the given cgroup directory. Should only be
391 * called by subsystems from within a populate() method
392 */
Li Zefanffd2d882008-02-23 15:24:09 -0800393int cgroup_add_file(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Paul Menageddbcc7e2007-10-18 23:39:30 -0700394 const struct cftype *cft);
395
Paul Menaged20a3902009-04-02 16:57:22 -0700396/*
397 * Add a set of new files to the given cgroup directory. Should
398 * only be called by subsystems from within a populate() method
399 */
Li Zefanffd2d882008-02-23 15:24:09 -0800400int cgroup_add_files(struct cgroup *cgrp,
Paul Menageddbcc7e2007-10-18 23:39:30 -0700401 struct cgroup_subsys *subsys,
402 const struct cftype cft[],
403 int count);
404
Li Zefanffd2d882008-02-23 15:24:09 -0800405int cgroup_is_removed(const struct cgroup *cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700406
Li Zefanffd2d882008-02-23 15:24:09 -0800407int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700408
Li Zefanffd2d882008-02-23 15:24:09 -0800409int cgroup_task_count(const struct cgroup *cgrp);
Paul Menagebbcb81d2007-10-18 23:39:32 -0700410
Grzegorz Nosek313e9242009-04-02 16:57:23 -0700411/* Return true if cgrp is a descendant of the task's cgroup */
412int cgroup_is_descendant(const struct cgroup *cgrp, struct task_struct *task);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700413
Thadeu Lima de Souza Cascardo21acb9c2009-02-04 10:12:08 +0100414/*
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -0700415 * When the subsys has to access css and may add permanent refcnt to css,
416 * it should take care of racy conditions with rmdir(). Following set of
417 * functions, is for stop/restart rmdir if necessary.
418 * Because these will call css_get/put, "css" should be alive css.
419 *
420 * cgroup_exclude_rmdir();
421 * ...do some jobs which may access arbitrary empty cgroup
422 * cgroup_release_and_wakeup_rmdir();
423 *
424 * When someone removes a cgroup while cgroup_exclude_rmdir() holds it,
425 * it sleeps and cgroup_release_and_wakeup_rmdir() will wake him up.
426 */
427
428void cgroup_exclude_rmdir(struct cgroup_subsys_state *css);
429void cgroup_release_and_wakeup_rmdir(struct cgroup_subsys_state *css);
430
431/*
Thadeu Lima de Souza Cascardo21acb9c2009-02-04 10:12:08 +0100432 * Control Group subsystem type.
433 * See Documentation/cgroups/cgroups.txt for details
434 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700435
436struct cgroup_subsys {
437 struct cgroup_subsys_state *(*create)(struct cgroup_subsys *ss,
Li Zefanffd2d882008-02-23 15:24:09 -0800438 struct cgroup *cgrp);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700439 int (*pre_destroy)(struct cgroup_subsys *ss, struct cgroup *cgrp);
Li Zefanffd2d882008-02-23 15:24:09 -0800440 void (*destroy)(struct cgroup_subsys *ss, struct cgroup *cgrp);
Ben Blumbe367d02009-09-23 15:56:31 -0700441 int (*can_attach)(struct cgroup_subsys *ss, struct cgroup *cgrp,
442 struct task_struct *tsk, bool threadgroup);
Daisuke Nishimura2468c722010-03-10 15:22:03 -0800443 void (*cancel_attach)(struct cgroup_subsys *ss, struct cgroup *cgrp,
444 struct task_struct *tsk, bool threadgroup);
Li Zefanffd2d882008-02-23 15:24:09 -0800445 void (*attach)(struct cgroup_subsys *ss, struct cgroup *cgrp,
Ben Blumbe367d02009-09-23 15:56:31 -0700446 struct cgroup *old_cgrp, struct task_struct *tsk,
447 bool threadgroup);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700448 void (*fork)(struct cgroup_subsys *ss, struct task_struct *task);
449 void (*exit)(struct cgroup_subsys *ss, struct task_struct *task);
450 int (*populate)(struct cgroup_subsys *ss,
Li Zefanffd2d882008-02-23 15:24:09 -0800451 struct cgroup *cgrp);
452 void (*post_clone)(struct cgroup_subsys *ss, struct cgroup *cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700453 void (*bind)(struct cgroup_subsys *ss, struct cgroup *root);
Hugh Dickinse5991372009-01-06 14:39:22 -0800454
Paul Menageddbcc7e2007-10-18 23:39:30 -0700455 int subsys_id;
456 int active;
Paul Menage8bab8dd2008-04-04 14:29:57 -0700457 int disabled;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700458 int early_init;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700459 /*
460 * True if this subsys uses ID. ID is not available before cgroup_init()
461 * (not available in early_init time.)
462 */
463 bool use_id;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700464#define MAX_CGROUP_TYPE_NAMELEN 32
465 const char *name;
466
Paul Menage999cd8a2009-01-07 18:08:36 -0800467 /*
468 * Protects sibling/children links of cgroups in this
469 * hierarchy, plus protects which hierarchy (or none) the
470 * subsystem is a part of (i.e. root/sibling). To avoid
471 * potential deadlocks, the following operations should not be
472 * undertaken while holding any hierarchy_mutex:
473 *
474 * - allocating memory
475 * - initiating hotplug events
476 */
477 struct mutex hierarchy_mutex;
Li Zefancfebe562009-02-11 13:04:36 -0800478 struct lock_class_key subsys_key;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700479
Paul Menage999cd8a2009-01-07 18:08:36 -0800480 /*
481 * Link to parent, and list entry in parent's children.
482 * Protected by this->hierarchy_mutex and cgroup_lock()
483 */
484 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700485 struct list_head sibling;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700486 /* used when use_id == true */
487 struct idr idr;
488 spinlock_t id_lock;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700489};
490
491#define SUBSYS(_x) extern struct cgroup_subsys _x ## _subsys;
492#include <linux/cgroup_subsys.h>
493#undef SUBSYS
494
495static inline struct cgroup_subsys_state *cgroup_subsys_state(
Li Zefanffd2d882008-02-23 15:24:09 -0800496 struct cgroup *cgrp, int subsys_id)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700497{
Li Zefanffd2d882008-02-23 15:24:09 -0800498 return cgrp->subsys[subsys_id];
Paul Menageddbcc7e2007-10-18 23:39:30 -0700499}
500
501static inline struct cgroup_subsys_state *task_subsys_state(
502 struct task_struct *task, int subsys_id)
503{
Paul E. McKenneyd11c5632010-02-22 17:04:50 -0800504 return rcu_dereference_check(task->cgroups->subsys[subsys_id],
505 rcu_read_lock_held() ||
506 cgroup_lock_is_held());
Paul Menageddbcc7e2007-10-18 23:39:30 -0700507}
508
509static inline struct cgroup* task_cgroup(struct task_struct *task,
510 int subsys_id)
511{
512 return task_subsys_state(task, subsys_id)->cgroup;
513}
514
Serge E. Hallyne885dcd2008-07-25 01:47:06 -0700515int cgroup_clone(struct task_struct *tsk, struct cgroup_subsys *ss,
516 char *nodename);
Paul Menage697f4162007-10-18 23:39:34 -0700517
Paul Menage817929e2007-10-18 23:39:36 -0700518/* A cgroup_iter should be treated as an opaque object */
519struct cgroup_iter {
520 struct list_head *cg_link;
521 struct list_head *task;
522};
523
Paul Menaged20a3902009-04-02 16:57:22 -0700524/*
525 * To iterate across the tasks in a cgroup:
Paul Menage817929e2007-10-18 23:39:36 -0700526 *
527 * 1) call cgroup_iter_start to intialize an iterator
528 *
529 * 2) call cgroup_iter_next() to retrieve member tasks until it
530 * returns NULL or until you want to end the iteration
531 *
532 * 3) call cgroup_iter_end() to destroy the iterator.
Cliff Wickman31a7df02008-02-07 00:14:42 -0800533 *
Paul Menaged20a3902009-04-02 16:57:22 -0700534 * Or, call cgroup_scan_tasks() to iterate through every task in a
535 * cgroup - cgroup_scan_tasks() holds the css_set_lock when calling
536 * the test_task() callback, but not while calling the process_task()
537 * callback.
Paul Menage817929e2007-10-18 23:39:36 -0700538 */
Li Zefanffd2d882008-02-23 15:24:09 -0800539void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it);
540struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -0700541 struct cgroup_iter *it);
Li Zefanffd2d882008-02-23 15:24:09 -0800542void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it);
Cliff Wickman31a7df02008-02-07 00:14:42 -0800543int cgroup_scan_tasks(struct cgroup_scanner *scan);
Cliff Wickman956db3c2008-02-07 00:14:43 -0800544int cgroup_attach_task(struct cgroup *, struct task_struct *);
Paul Menage817929e2007-10-18 23:39:36 -0700545
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700546/*
547 * CSS ID is ID for cgroup_subsys_state structs under subsys. This only works
548 * if cgroup_subsys.use_id == true. It can be used for looking up and scanning.
549 * CSS ID is assigned at cgroup allocation (create) automatically
550 * and removed when subsys calls free_css_id() function. This is because
551 * the lifetime of cgroup_subsys_state is subsys's matter.
552 *
553 * Looking up and scanning function should be called under rcu_read_lock().
554 * Taking cgroup_mutex()/hierarchy_mutex() is not necessary for following calls.
555 * But the css returned by this routine can be "not populated yet" or "being
556 * destroyed". The caller should check css and cgroup's status.
557 */
558
559/*
560 * Typically Called at ->destroy(), or somewhere the subsys frees
561 * cgroup_subsys_state.
562 */
563void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css);
564
565/* Find a cgroup_subsys_state which has given ID */
566
567struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id);
568
569/*
570 * Get a cgroup whose id is greater than or equal to id under tree of root.
571 * Returning a cgroup_subsys_state or NULL.
572 */
573struct cgroup_subsys_state *css_get_next(struct cgroup_subsys *ss, int id,
574 struct cgroup_subsys_state *root, int *foundid);
575
576/* Returns true if root is ancestor of cg */
577bool css_is_ancestor(struct cgroup_subsys_state *cg,
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700578 const struct cgroup_subsys_state *root);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700579
580/* Get id and depth of css */
581unsigned short css_id(struct cgroup_subsys_state *css);
582unsigned short css_depth(struct cgroup_subsys_state *css);
583
Paul Menageddbcc7e2007-10-18 23:39:30 -0700584#else /* !CONFIG_CGROUPS */
585
586static inline int cgroup_init_early(void) { return 0; }
587static inline int cgroup_init(void) { return 0; }
Paul Menageb4f48b62007-10-18 23:39:33 -0700588static inline void cgroup_fork(struct task_struct *p) {}
589static inline void cgroup_fork_callbacks(struct task_struct *p) {}
Paul Menage817929e2007-10-18 23:39:36 -0700590static inline void cgroup_post_fork(struct task_struct *p) {}
Paul Menageb4f48b62007-10-18 23:39:33 -0700591static inline void cgroup_exit(struct task_struct *p, int callbacks) {}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700592
593static inline void cgroup_lock(void) {}
594static inline void cgroup_unlock(void) {}
Balbir Singh846c7bb2007-10-18 23:39:44 -0700595static inline int cgroupstats_build(struct cgroupstats *stats,
596 struct dentry *dentry)
597{
598 return -EINVAL;
599}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700600
601#endif /* !CONFIG_CGROUPS */
602
Paul Menageddbcc7e2007-10-18 23:39:30 -0700603#endif /* _LINUX_CGROUP_H */