blob: be71efc36bbae281f1baa995755a19abf12d3e57 [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);
Paul Menage817929e2007-10-18 23:39:36 -070035extern void cgroup_post_fork(struct task_struct *p);
Paul Menageb4f48b62007-10-18 23:39:33 -070036extern void cgroup_exit(struct task_struct *p, int run_callbacks);
Balbir Singh846c7bb2007-10-18 23:39:44 -070037extern int cgroupstats_build(struct cgroupstats *stats,
38 struct dentry *dentry);
Ben Blume6a11052010-03-10 15:22:09 -080039extern int cgroup_load_subsys(struct cgroup_subsys *ss);
Ben Blumcf5d5942010-03-10 15:22:09 -080040extern void cgroup_unload_subsys(struct cgroup_subsys *ss);
Paul Menageddbcc7e2007-10-18 23:39:30 -070041
Alexey Dobriyan828c0952009-10-01 15:43:56 -070042extern const struct file_operations proc_cgroup_operations;
Paul Menagea4243162007-10-18 23:39:35 -070043
Ben Blumaae8aab2010-03-10 15:22:07 -080044/* Define the enumeration of all builtin cgroup subsystems */
Paul Menage817929e2007-10-18 23:39:36 -070045#define SUBSYS(_x) _x ## _subsys_id,
46enum cgroup_subsys_id {
47#include <linux/cgroup_subsys.h>
Ben Blumaae8aab2010-03-10 15:22:07 -080048 CGROUP_BUILTIN_SUBSYS_COUNT
Paul Menage817929e2007-10-18 23:39:36 -070049};
50#undef SUBSYS
Ben Blumaae8aab2010-03-10 15:22:07 -080051/*
52 * This define indicates the maximum number of subsystems that can be loaded
53 * at once. We limit to this many since cgroupfs_root has subsys_bits to keep
54 * track of all of them.
55 */
56#define CGROUP_SUBSYS_COUNT (BITS_PER_BYTE*sizeof(unsigned long))
Paul Menage817929e2007-10-18 23:39:36 -070057
Paul Menageddbcc7e2007-10-18 23:39:30 -070058/* Per-subsystem/per-cgroup state maintained by the system. */
59struct cgroup_subsys_state {
Paul Menaged20a3902009-04-02 16:57:22 -070060 /*
61 * The cgroup that this subsystem is attached to. Useful
Paul Menageddbcc7e2007-10-18 23:39:30 -070062 * for subsystems that want to know about the cgroup
Paul Menaged20a3902009-04-02 16:57:22 -070063 * hierarchy structure
64 */
Paul Menageddbcc7e2007-10-18 23:39:30 -070065 struct cgroup *cgroup;
66
Paul Menaged20a3902009-04-02 16:57:22 -070067 /*
68 * State maintained by the cgroup system to allow subsystems
Paul Menagee7c5ec92009-01-07 18:08:38 -080069 * to be "busy". Should be accessed via css_get(),
Paul Menaged20a3902009-04-02 16:57:22 -070070 * css_tryget() and and css_put().
71 */
Paul Menageddbcc7e2007-10-18 23:39:30 -070072
73 atomic_t refcnt;
74
75 unsigned long flags;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -070076 /* ID for this css, if possible */
Arnd Bergmann2c392b82010-02-24 19:41:39 +010077 struct css_id __rcu *id;
Paul Menageddbcc7e2007-10-18 23:39:30 -070078};
79
80/* bits in struct cgroup_subsys_state flags field */
81enum {
82 CSS_ROOT, /* This CSS is the root of the subsystem */
Paul Menagee7c5ec92009-01-07 18:08:38 -080083 CSS_REMOVED, /* This CSS is dead */
Paul Menageddbcc7e2007-10-18 23:39:30 -070084};
85
Daisuke Nishimurad7b9fff2010-03-10 15:22:05 -080086/* Caller must verify that the css is not for root cgroup */
87static inline void __css_get(struct cgroup_subsys_state *css, int count)
88{
89 atomic_add(count, &css->refcnt);
90}
91
Paul Menageddbcc7e2007-10-18 23:39:30 -070092/*
Paul Menagee7c5ec92009-01-07 18:08:38 -080093 * Call css_get() to hold a reference on the css; it can be used
94 * for a reference obtained via:
95 * - an existing ref-counted reference to the css
96 * - task->cgroups for a locked task
Paul Menageddbcc7e2007-10-18 23:39:30 -070097 */
98
99static inline void css_get(struct cgroup_subsys_state *css)
100{
101 /* We don't need to reference count the root state */
102 if (!test_bit(CSS_ROOT, &css->flags))
Daisuke Nishimurad7b9fff2010-03-10 15:22:05 -0800103 __css_get(css, 1);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700104}
Paul Menagee7c5ec92009-01-07 18:08:38 -0800105
106static inline bool css_is_removed(struct cgroup_subsys_state *css)
107{
108 return test_bit(CSS_REMOVED, &css->flags);
109}
110
111/*
112 * Call css_tryget() to take a reference on a css if your existing
113 * (known-valid) reference isn't already ref-counted. Returns false if
114 * the css has been destroyed.
115 */
116
117static inline bool css_tryget(struct cgroup_subsys_state *css)
118{
119 if (test_bit(CSS_ROOT, &css->flags))
120 return true;
121 while (!atomic_inc_not_zero(&css->refcnt)) {
122 if (test_bit(CSS_REMOVED, &css->flags))
123 return false;
Paul Menage804b3c22009-01-29 14:25:21 -0800124 cpu_relax();
Paul Menagee7c5ec92009-01-07 18:08:38 -0800125 }
126 return true;
127}
128
Paul Menageddbcc7e2007-10-18 23:39:30 -0700129/*
130 * css_put() should be called to release a reference taken by
Paul Menagee7c5ec92009-01-07 18:08:38 -0800131 * css_get() or css_tryget()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700132 */
133
Daisuke Nishimurad7b9fff2010-03-10 15:22:05 -0800134extern void __css_put(struct cgroup_subsys_state *css, int count);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700135static inline void css_put(struct cgroup_subsys_state *css)
136{
137 if (!test_bit(CSS_ROOT, &css->flags))
Daisuke Nishimurad7b9fff2010-03-10 15:22:05 -0800138 __css_put(css, 1);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700139}
140
Paul Menage3116f0e2008-04-29 01:00:04 -0700141/* bits in struct cgroup flags field */
142enum {
143 /* Control Group is dead */
144 CGRP_REMOVED,
Paul Menaged20a3902009-04-02 16:57:22 -0700145 /*
146 * Control Group has previously had a child cgroup or a task,
147 * but no longer (only if CGRP_NOTIFY_ON_RELEASE is set)
148 */
Paul Menage3116f0e2008-04-29 01:00:04 -0700149 CGRP_RELEASABLE,
150 /* Control Group requires release notifications to userspace */
151 CGRP_NOTIFY_ON_RELEASE,
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700152 /*
153 * A thread in rmdir() is wating for this cgroup.
154 */
155 CGRP_WAIT_ON_RMDIR,
Daniel Lezcano97978e62010-10-27 15:33:35 -0700156 /*
157 * Clone cgroup values when creating a new child cgroup
158 */
159 CGRP_CLONE_CHILDREN,
Paul Menage3116f0e2008-04-29 01:00:04 -0700160};
161
Paul Menageddbcc7e2007-10-18 23:39:30 -0700162struct cgroup {
163 unsigned long flags; /* "unsigned long" so bitops work */
164
Paul Menaged20a3902009-04-02 16:57:22 -0700165 /*
166 * count users of this cgroup. >0 means busy, but doesn't
167 * necessarily indicate the number of tasks in the cgroup
168 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700169 atomic_t count;
170
171 /*
172 * We link our 'sibling' struct into our parent's 'children'.
173 * Our children link their 'sibling' into our 'children'.
174 */
175 struct list_head sibling; /* my parent's children */
176 struct list_head children; /* my children */
177
Paul Menaged20a3902009-04-02 16:57:22 -0700178 struct cgroup *parent; /* my parent */
Arnd Bergmann2c392b82010-02-24 19:41:39 +0100179 struct dentry __rcu *dentry; /* cgroup fs entry, RCU protected */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700180
181 /* Private pointers for each registered subsystem */
182 struct cgroup_subsys_state *subsys[CGROUP_SUBSYS_COUNT];
183
184 struct cgroupfs_root *root;
185 struct cgroup *top_cgroup;
Paul Menage817929e2007-10-18 23:39:36 -0700186
187 /*
188 * List of cg_cgroup_links pointing at css_sets with
189 * tasks in this cgroup. Protected by css_set_lock
190 */
191 struct list_head css_sets;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700192
193 /*
194 * Linked list running through all cgroups that can
195 * potentially be reaped by the release agent. Protected by
196 * release_list_lock
197 */
198 struct list_head release_list;
Paul Menagecc31edc2008-10-18 20:28:04 -0700199
Ben Blum72a8cb32009-09-23 15:56:27 -0700200 /*
201 * list of pidlists, up to two for each namespace (one for procs, one
202 * for tasks); created on demand.
203 */
204 struct list_head pidlists;
205 struct mutex pidlist_mutex;
Paul Menagea47295e2009-01-07 18:07:44 -0800206
207 /* For RCU-protected deletion */
208 struct rcu_head rcu_head;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800209
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300210 /* List of events which userspace want to receive */
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800211 struct list_head event_list;
212 spinlock_t event_list_lock;
Paul Menage817929e2007-10-18 23:39:36 -0700213};
214
Paul Menaged20a3902009-04-02 16:57:22 -0700215/*
216 * A css_set is a structure holding pointers to a set of
Paul Menage817929e2007-10-18 23:39:36 -0700217 * cgroup_subsys_state objects. This saves space in the task struct
218 * object and speeds up fork()/exit(), since a single inc/dec and a
Paul Menaged20a3902009-04-02 16:57:22 -0700219 * list_add()/del() can bump the reference count on the entire cgroup
220 * set for a task.
Paul Menage817929e2007-10-18 23:39:36 -0700221 */
222
223struct css_set {
224
225 /* Reference count */
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700226 atomic_t refcount;
Paul Menage817929e2007-10-18 23:39:36 -0700227
228 /*
Li Zefan472b1052008-04-29 01:00:11 -0700229 * List running through all cgroup groups in the same hash
230 * slot. Protected by css_set_lock
231 */
232 struct hlist_node hlist;
233
234 /*
Paul Menage817929e2007-10-18 23:39:36 -0700235 * List running through all tasks using this cgroup
236 * group. Protected by css_set_lock
237 */
238 struct list_head tasks;
239
240 /*
241 * List of cg_cgroup_link objects on link chains from
242 * cgroups referenced from this css_set. Protected by
243 * css_set_lock
244 */
245 struct list_head cg_links;
246
247 /*
248 * Set of subsystem states, one for each subsystem. This array
249 * is immutable after creation apart from the init_css_set
Ben Blumcf5d5942010-03-10 15:22:09 -0800250 * during subsystem registration (at boot time) and modular subsystem
251 * loading/unloading.
Paul Menage817929e2007-10-18 23:39:36 -0700252 */
253 struct cgroup_subsys_state *subsys[CGROUP_SUBSYS_COUNT];
Ben Blumc3783692009-09-23 15:56:29 -0700254
255 /* For RCU-protected deletion */
256 struct rcu_head rcu_head;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700257};
258
Paul Menage91796562008-04-29 01:00:01 -0700259/*
260 * cgroup_map_cb is an abstract callback API for reporting map-valued
261 * control files
262 */
263
264struct cgroup_map_cb {
265 int (*fill)(struct cgroup_map_cb *cb, const char *key, u64 value);
266 void *state;
267};
268
Paul Menaged20a3902009-04-02 16:57:22 -0700269/*
270 * struct cftype: handler definitions for cgroup control files
Paul Menageddbcc7e2007-10-18 23:39:30 -0700271 *
272 * When reading/writing to a file:
Li Zefana043e3b2008-02-23 15:24:09 -0800273 * - the cgroup to use is file->f_dentry->d_parent->d_fsdata
Paul Menageddbcc7e2007-10-18 23:39:30 -0700274 * - the 'cftype' of the file is file->f_dentry->d_fsdata
275 */
276
277#define MAX_CFTYPE_NAME 64
278struct cftype {
Paul Menaged20a3902009-04-02 16:57:22 -0700279 /*
280 * By convention, the name should begin with the name of the
281 * subsystem, followed by a period
282 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700283 char name[MAX_CFTYPE_NAME];
284 int private;
Li Zefan099fca32009-04-02 16:57:29 -0700285 /*
286 * If not 0, file mode is set to this value, otherwise it will
287 * be figured out automatically
288 */
Al Viroa5e7ed32011-07-26 01:55:55 -0400289 umode_t mode;
Paul Menagedb3b1492008-07-25 01:46:58 -0700290
291 /*
292 * If non-zero, defines the maximum length of string that can
293 * be passed to write_string; defaults to 64
294 */
295 size_t max_write_len;
296
Paul Menagece16b492008-07-25 01:46:57 -0700297 int (*open)(struct inode *inode, struct file *file);
298 ssize_t (*read)(struct cgroup *cgrp, struct cftype *cft,
299 struct file *file,
300 char __user *buf, size_t nbytes, loff_t *ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700301 /*
Paul Menagef4c753b2008-04-29 00:59:56 -0700302 * read_u64() is a shortcut for the common case of returning a
Paul Menageddbcc7e2007-10-18 23:39:30 -0700303 * single integer. Use it in place of read()
304 */
Paul Menagece16b492008-07-25 01:46:57 -0700305 u64 (*read_u64)(struct cgroup *cgrp, struct cftype *cft);
Paul Menage91796562008-04-29 01:00:01 -0700306 /*
Paul Menagee73d2c62008-04-29 01:00:06 -0700307 * read_s64() is a signed version of read_u64()
308 */
Paul Menagece16b492008-07-25 01:46:57 -0700309 s64 (*read_s64)(struct cgroup *cgrp, struct cftype *cft);
Paul Menagee73d2c62008-04-29 01:00:06 -0700310 /*
Paul Menage91796562008-04-29 01:00:01 -0700311 * read_map() is used for defining a map of key/value
312 * pairs. It should call cb->fill(cb, key, value) for each
313 * entry. The key/value pairs (and their ordering) should not
314 * change between reboots.
315 */
Paul Menagece16b492008-07-25 01:46:57 -0700316 int (*read_map)(struct cgroup *cont, struct cftype *cft,
317 struct cgroup_map_cb *cb);
Serge E. Hallyn29486df2008-04-29 01:00:14 -0700318 /*
319 * read_seq_string() is used for outputting a simple sequence
320 * using seqfile.
321 */
Paul Menagece16b492008-07-25 01:46:57 -0700322 int (*read_seq_string)(struct cgroup *cont, struct cftype *cft,
323 struct seq_file *m);
Paul Menage91796562008-04-29 01:00:01 -0700324
Paul Menagece16b492008-07-25 01:46:57 -0700325 ssize_t (*write)(struct cgroup *cgrp, struct cftype *cft,
326 struct file *file,
327 const char __user *buf, size_t nbytes, loff_t *ppos);
Paul Menage355e0c42007-10-18 23:39:33 -0700328
329 /*
Paul Menagef4c753b2008-04-29 00:59:56 -0700330 * write_u64() is a shortcut for the common case of accepting
Paul Menage355e0c42007-10-18 23:39:33 -0700331 * a single integer (as parsed by simple_strtoull) from
332 * userspace. Use in place of write(); return 0 or error.
333 */
Paul Menagece16b492008-07-25 01:46:57 -0700334 int (*write_u64)(struct cgroup *cgrp, struct cftype *cft, u64 val);
Paul Menagee73d2c62008-04-29 01:00:06 -0700335 /*
336 * write_s64() is a signed version of write_u64()
337 */
Paul Menagece16b492008-07-25 01:46:57 -0700338 int (*write_s64)(struct cgroup *cgrp, struct cftype *cft, s64 val);
Paul Menage355e0c42007-10-18 23:39:33 -0700339
Pavel Emelyanovd447ea22008-04-29 01:00:08 -0700340 /*
Paul Menagedb3b1492008-07-25 01:46:58 -0700341 * write_string() is passed a nul-terminated kernelspace
342 * buffer of maximum length determined by max_write_len.
343 * Returns 0 or -ve error code.
344 */
345 int (*write_string)(struct cgroup *cgrp, struct cftype *cft,
346 const char *buffer);
347 /*
Pavel Emelyanovd447ea22008-04-29 01:00:08 -0700348 * trigger() callback can be used to get some kick from the
349 * userspace, when the actual string written is not important
350 * at all. The private field can be used to determine the
351 * kick type for multiplexing.
352 */
353 int (*trigger)(struct cgroup *cgrp, unsigned int event);
354
Paul Menagece16b492008-07-25 01:46:57 -0700355 int (*release)(struct inode *inode, struct file *file);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800356
357 /*
358 * register_event() callback will be used to add new userspace
359 * waiter for changes related to the cftype. Implement it if
360 * you want to provide this functionality. Use eventfd_signal()
361 * on eventfd to send notification to userspace.
362 */
363 int (*register_event)(struct cgroup *cgrp, struct cftype *cft,
364 struct eventfd_ctx *eventfd, const char *args);
365 /*
366 * unregister_event() callback will be called when userspace
367 * closes the eventfd or on cgroup removing.
368 * This callback must be implemented, if you want provide
369 * notification functionality.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800370 */
Kirill A. Shutemov907860e2010-05-26 14:42:46 -0700371 void (*unregister_event)(struct cgroup *cgrp, struct cftype *cft,
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800372 struct eventfd_ctx *eventfd);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700373};
374
Cliff Wickman31a7df02008-02-07 00:14:42 -0800375struct cgroup_scanner {
376 struct cgroup *cg;
377 int (*test_task)(struct task_struct *p, struct cgroup_scanner *scan);
378 void (*process_task)(struct task_struct *p,
379 struct cgroup_scanner *scan);
380 struct ptr_heap *heap;
Li Zefanbd1a8ab2009-04-02 16:57:50 -0700381 void *data;
Cliff Wickman31a7df02008-02-07 00:14:42 -0800382};
383
Paul Menaged20a3902009-04-02 16:57:22 -0700384/*
385 * Add a new file to the given cgroup directory. Should only be
386 * called by subsystems from within a populate() method
387 */
Li Zefanffd2d882008-02-23 15:24:09 -0800388int cgroup_add_file(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Paul Menageddbcc7e2007-10-18 23:39:30 -0700389 const struct cftype *cft);
390
Paul Menaged20a3902009-04-02 16:57:22 -0700391/*
392 * Add a set of new files to the given cgroup directory. Should
393 * only be called by subsystems from within a populate() method
394 */
Li Zefanffd2d882008-02-23 15:24:09 -0800395int cgroup_add_files(struct cgroup *cgrp,
Paul Menageddbcc7e2007-10-18 23:39:30 -0700396 struct cgroup_subsys *subsys,
397 const struct cftype cft[],
398 int count);
399
Li Zefanffd2d882008-02-23 15:24:09 -0800400int cgroup_is_removed(const struct cgroup *cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700401
Li Zefanffd2d882008-02-23 15:24:09 -0800402int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700403
Li Zefanffd2d882008-02-23 15:24:09 -0800404int cgroup_task_count(const struct cgroup *cgrp);
Paul Menagebbcb81d2007-10-18 23:39:32 -0700405
Grzegorz Nosek313e9242009-04-02 16:57:23 -0700406/* Return true if cgrp is a descendant of the task's cgroup */
407int cgroup_is_descendant(const struct cgroup *cgrp, struct task_struct *task);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700408
Thadeu Lima de Souza Cascardo21acb9c2009-02-04 10:12:08 +0100409/*
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -0700410 * When the subsys has to access css and may add permanent refcnt to css,
411 * it should take care of racy conditions with rmdir(). Following set of
412 * functions, is for stop/restart rmdir if necessary.
413 * Because these will call css_get/put, "css" should be alive css.
414 *
415 * cgroup_exclude_rmdir();
416 * ...do some jobs which may access arbitrary empty cgroup
417 * cgroup_release_and_wakeup_rmdir();
418 *
419 * When someone removes a cgroup while cgroup_exclude_rmdir() holds it,
420 * it sleeps and cgroup_release_and_wakeup_rmdir() will wake him up.
421 */
422
423void cgroup_exclude_rmdir(struct cgroup_subsys_state *css);
424void cgroup_release_and_wakeup_rmdir(struct cgroup_subsys_state *css);
425
426/*
Tejun Heo2f7ee562011-12-12 18:12:21 -0800427 * Control Group taskset, used to pass around set of tasks to cgroup_subsys
428 * methods.
429 */
430struct cgroup_taskset;
431struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset);
432struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset);
433struct cgroup *cgroup_taskset_cur_cgroup(struct cgroup_taskset *tset);
434int cgroup_taskset_size(struct cgroup_taskset *tset);
435
436/**
437 * cgroup_taskset_for_each - iterate cgroup_taskset
438 * @task: the loop cursor
439 * @skip_cgrp: skip if task's cgroup matches this, %NULL to iterate through all
440 * @tset: taskset to iterate
441 */
442#define cgroup_taskset_for_each(task, skip_cgrp, tset) \
443 for ((task) = cgroup_taskset_first((tset)); (task); \
444 (task) = cgroup_taskset_next((tset))) \
445 if (!(skip_cgrp) || \
446 cgroup_taskset_cur_cgroup((tset)) != (skip_cgrp))
447
448/*
Thadeu Lima de Souza Cascardo21acb9c2009-02-04 10:12:08 +0100449 * Control Group subsystem type.
450 * See Documentation/cgroups/cgroups.txt for details
451 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700452
453struct cgroup_subsys {
Li Zefan761b3ef2012-01-31 13:47:36 +0800454 struct cgroup_subsys_state *(*create)(struct cgroup *cgrp);
455 int (*pre_destroy)(struct cgroup *cgrp);
456 void (*destroy)(struct cgroup *cgrp);
457 int (*can_attach)(struct cgroup *cgrp, struct cgroup_taskset *tset);
458 void (*cancel_attach)(struct cgroup *cgrp, struct cgroup_taskset *tset);
459 void (*attach)(struct cgroup *cgrp, struct cgroup_taskset *tset);
460 void (*fork)(struct task_struct *task);
461 void (*exit)(struct cgroup *cgrp, struct cgroup *old_cgrp,
462 struct task_struct *task);
463 int (*populate)(struct cgroup_subsys *ss, struct cgroup *cgrp);
464 void (*post_clone)(struct cgroup *cgrp);
465 void (*bind)(struct cgroup *root);
Hugh Dickinse5991372009-01-06 14:39:22 -0800466
Paul Menageddbcc7e2007-10-18 23:39:30 -0700467 int subsys_id;
468 int active;
Paul Menage8bab8dd2008-04-04 14:29:57 -0700469 int disabled;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700470 int early_init;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700471 /*
472 * True if this subsys uses ID. ID is not available before cgroup_init()
473 * (not available in early_init time.)
474 */
475 bool use_id;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700476#define MAX_CGROUP_TYPE_NAMELEN 32
477 const char *name;
478
Paul Menage999cd8a2009-01-07 18:08:36 -0800479 /*
480 * Protects sibling/children links of cgroups in this
481 * hierarchy, plus protects which hierarchy (or none) the
482 * subsystem is a part of (i.e. root/sibling). To avoid
483 * potential deadlocks, the following operations should not be
484 * undertaken while holding any hierarchy_mutex:
485 *
486 * - allocating memory
487 * - initiating hotplug events
488 */
489 struct mutex hierarchy_mutex;
Li Zefancfebe562009-02-11 13:04:36 -0800490 struct lock_class_key subsys_key;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700491
Paul Menage999cd8a2009-01-07 18:08:36 -0800492 /*
493 * Link to parent, and list entry in parent's children.
494 * Protected by this->hierarchy_mutex and cgroup_lock()
495 */
496 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700497 struct list_head sibling;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700498 /* used when use_id == true */
499 struct idr idr;
Hugh Dickins42aee6c2012-03-21 16:34:21 -0700500 spinlock_t id_lock;
Ben Blume6a11052010-03-10 15:22:09 -0800501
502 /* should be defined only by modular subsystems */
503 struct module *module;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700504};
505
506#define SUBSYS(_x) extern struct cgroup_subsys _x ## _subsys;
507#include <linux/cgroup_subsys.h>
508#undef SUBSYS
509
510static inline struct cgroup_subsys_state *cgroup_subsys_state(
Li Zefanffd2d882008-02-23 15:24:09 -0800511 struct cgroup *cgrp, int subsys_id)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700512{
Li Zefanffd2d882008-02-23 15:24:09 -0800513 return cgrp->subsys[subsys_id];
Paul Menageddbcc7e2007-10-18 23:39:30 -0700514}
515
Tejun Heo71a20682013-06-25 11:48:32 -0700516/**
517 * task_css_set_check - obtain a task's css_set with extra access conditions
518 * @task: the task to obtain css_set for
519 * @__c: extra condition expression to be passed to rcu_dereference_check()
520 *
521 * A task's css_set is RCU protected, initialized and exited while holding
522 * task_lock(), and can only be modified while holding both cgroup_mutex
523 * and task_lock() while the task is alive. This macro verifies that the
524 * caller is inside proper critical section and returns @task's css_set.
525 *
526 * The caller can also specify additional allowed conditions via @__c, such
527 * as locks used during the cgroup_subsys::attach() methods.
528 */
529#define task_css_set_check(task, __c) \
530 rcu_dereference_check((task)->cgroups, \
531 lockdep_is_held(&(task)->alloc_lock) || \
532 cgroup_lock_is_held() || (__c))
533
534/**
535 * task_subsys_state_check - obtain css for (task, subsys) w/ extra access conds
536 * @task: the target task
537 * @subsys_id: the target subsystem ID
538 * @__c: extra condition expression to be passed to rcu_dereference_check()
539 *
540 * Return the cgroup_subsys_state for the (@task, @subsys_id) pair. The
541 * synchronization rules are the same as task_css_set_check().
Peter Zijlstradc61b1d2010-06-08 11:40:42 +0200542 */
543#define task_subsys_state_check(task, subsys_id, __c) \
Tejun Heo71a20682013-06-25 11:48:32 -0700544 task_css_set_check((task), (__c))->subsys[(subsys_id)]
Peter Zijlstradc61b1d2010-06-08 11:40:42 +0200545
Tejun Heo71a20682013-06-25 11:48:32 -0700546/**
547 * task_css_set - obtain a task's css_set
548 * @task: the task to obtain css_set for
549 *
550 * See task_css_set_check().
551 */
552static inline struct css_set *task_css_set(struct task_struct *task)
553{
554 return task_css_set_check(task, false);
555}
556
557/**
558 * task_subsys_state - obtain css for (task, subsys)
559 * @task: the target task
560 * @subsys_id: the target subsystem ID
561 *
562 * See task_subsys_state_check().
563 */
Peter Zijlstradc61b1d2010-06-08 11:40:42 +0200564static inline struct cgroup_subsys_state *
565task_subsys_state(struct task_struct *task, int subsys_id)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700566{
Peter Zijlstradc61b1d2010-06-08 11:40:42 +0200567 return task_subsys_state_check(task, subsys_id, false);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700568}
569
570static inline struct cgroup* task_cgroup(struct task_struct *task,
571 int subsys_id)
572{
573 return task_subsys_state(task, subsys_id)->cgroup;
574}
575
Paul Menage817929e2007-10-18 23:39:36 -0700576/* A cgroup_iter should be treated as an opaque object */
577struct cgroup_iter {
578 struct list_head *cg_link;
579 struct list_head *task;
580};
581
Paul Menaged20a3902009-04-02 16:57:22 -0700582/*
583 * To iterate across the tasks in a cgroup:
Paul Menage817929e2007-10-18 23:39:36 -0700584 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400585 * 1) call cgroup_iter_start to initialize an iterator
Paul Menage817929e2007-10-18 23:39:36 -0700586 *
587 * 2) call cgroup_iter_next() to retrieve member tasks until it
588 * returns NULL or until you want to end the iteration
589 *
590 * 3) call cgroup_iter_end() to destroy the iterator.
Cliff Wickman31a7df02008-02-07 00:14:42 -0800591 *
Paul Menaged20a3902009-04-02 16:57:22 -0700592 * Or, call cgroup_scan_tasks() to iterate through every task in a
593 * cgroup - cgroup_scan_tasks() holds the css_set_lock when calling
594 * the test_task() callback, but not while calling the process_task()
595 * callback.
Paul Menage817929e2007-10-18 23:39:36 -0700596 */
Li Zefanffd2d882008-02-23 15:24:09 -0800597void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it);
598struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -0700599 struct cgroup_iter *it);
Li Zefanffd2d882008-02-23 15:24:09 -0800600void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it);
Cliff Wickman31a7df02008-02-07 00:14:42 -0800601int cgroup_scan_tasks(struct cgroup_scanner *scan);
Cliff Wickman956db3c2008-02-07 00:14:43 -0800602int cgroup_attach_task(struct cgroup *, struct task_struct *);
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -0700603int cgroup_attach_task_all(struct task_struct *from, struct task_struct *);
604
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700605/*
606 * CSS ID is ID for cgroup_subsys_state structs under subsys. This only works
607 * if cgroup_subsys.use_id == true. It can be used for looking up and scanning.
608 * CSS ID is assigned at cgroup allocation (create) automatically
609 * and removed when subsys calls free_css_id() function. This is because
610 * the lifetime of cgroup_subsys_state is subsys's matter.
611 *
612 * Looking up and scanning function should be called under rcu_read_lock().
613 * Taking cgroup_mutex()/hierarchy_mutex() is not necessary for following calls.
614 * But the css returned by this routine can be "not populated yet" or "being
615 * destroyed". The caller should check css and cgroup's status.
616 */
617
618/*
619 * Typically Called at ->destroy(), or somewhere the subsys frees
620 * cgroup_subsys_state.
621 */
622void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css);
623
624/* Find a cgroup_subsys_state which has given ID */
625
626struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id);
627
628/*
629 * Get a cgroup whose id is greater than or equal to id under tree of root.
630 * Returning a cgroup_subsys_state or NULL.
631 */
632struct cgroup_subsys_state *css_get_next(struct cgroup_subsys *ss, int id,
633 struct cgroup_subsys_state *root, int *foundid);
634
635/* Returns true if root is ancestor of cg */
636bool css_is_ancestor(struct cgroup_subsys_state *cg,
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700637 const struct cgroup_subsys_state *root);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700638
639/* Get id and depth of css */
640unsigned short css_id(struct cgroup_subsys_state *css);
641unsigned short css_depth(struct cgroup_subsys_state *css);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200642struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700643
Paul Menageddbcc7e2007-10-18 23:39:30 -0700644#else /* !CONFIG_CGROUPS */
645
646static inline int cgroup_init_early(void) { return 0; }
647static inline int cgroup_init(void) { return 0; }
Paul Menageb4f48b62007-10-18 23:39:33 -0700648static inline void cgroup_fork(struct task_struct *p) {}
649static inline void cgroup_fork_callbacks(struct task_struct *p) {}
Paul Menage817929e2007-10-18 23:39:36 -0700650static inline void cgroup_post_fork(struct task_struct *p) {}
Paul Menageb4f48b62007-10-18 23:39:33 -0700651static inline void cgroup_exit(struct task_struct *p, int callbacks) {}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700652
653static inline void cgroup_lock(void) {}
654static inline void cgroup_unlock(void) {}
Balbir Singh846c7bb2007-10-18 23:39:44 -0700655static inline int cgroupstats_build(struct cgroupstats *stats,
656 struct dentry *dentry)
657{
658 return -EINVAL;
659}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700660
Sridhar Samudralad7926ee2010-05-30 22:24:39 +0200661/* No cgroups - nothing to do */
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -0700662static inline int cgroup_attach_task_all(struct task_struct *from,
663 struct task_struct *t)
664{
665 return 0;
666}
Sridhar Samudralad7926ee2010-05-30 22:24:39 +0200667
Paul Menageddbcc7e2007-10-18 23:39:30 -0700668#endif /* !CONFIG_CGROUPS */
669
Paul Menageddbcc7e2007-10-18 23:39:30 -0700670#endif /* _LINUX_CGROUP_H */