blob: 515927eebb37cf44e3afbc0153adf3d86229abee [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>
Tejun Heoeb6fd502012-11-09 09:12:29 -080015#include <linux/rculist.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070016#include <linux/cgroupstats.h>
Cliff Wickman31a7df02008-02-07 00:14:42 -080017#include <linux/prio_heap.h>
Paul Menagecc31edc2008-10-18 20:28:04 -070018#include <linux/rwsem.h>
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -070019#include <linux/idr.h>
Tejun Heo48ddbe12012-04-01 12:09:56 -070020#include <linux/workqueue.h>
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -040021#include <linux/xattr.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070022
23#ifdef CONFIG_CGROUPS
24
25struct cgroupfs_root;
26struct cgroup_subsys;
27struct inode;
Paul Menage84eea842008-07-25 01:47:00 -070028struct cgroup;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -070029struct css_id;
Paul Menageddbcc7e2007-10-18 23:39:30 -070030
31extern int cgroup_init_early(void);
32extern int cgroup_init(void);
Paul Menageb4f48b62007-10-18 23:39:33 -070033extern void cgroup_fork(struct task_struct *p);
Paul Menage817929e2007-10-18 23:39:36 -070034extern void cgroup_post_fork(struct task_struct *p);
Paul Menageb4f48b62007-10-18 23:39:33 -070035extern void cgroup_exit(struct task_struct *p, int run_callbacks);
Balbir Singh846c7bb2007-10-18 23:39:44 -070036extern int cgroupstats_build(struct cgroupstats *stats,
37 struct dentry *dentry);
Ben Blume6a11052010-03-10 15:22:09 -080038extern int cgroup_load_subsys(struct cgroup_subsys *ss);
Ben Blumcf5d5942010-03-10 15:22:09 -080039extern void cgroup_unload_subsys(struct cgroup_subsys *ss);
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
Li Zefan7d8e0bf2013-03-05 10:57:03 +080043/*
44 * Define the enumeration of all cgroup subsystems.
45 *
46 * We define ids for builtin subsystems and then modular ones.
47 */
Paul Menage817929e2007-10-18 23:39:36 -070048#define SUBSYS(_x) _x ## _subsys_id,
49enum cgroup_subsys_id {
Li Zefan7d8e0bf2013-03-05 10:57:03 +080050#define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option)
Paul Menage817929e2007-10-18 23:39:36 -070051#include <linux/cgroup_subsys.h>
Li Zefan7d8e0bf2013-03-05 10:57:03 +080052#undef IS_SUBSYS_ENABLED
53 CGROUP_BUILTIN_SUBSYS_COUNT,
54
55 __CGROUP_SUBSYS_TEMP_PLACEHOLDER = CGROUP_BUILTIN_SUBSYS_COUNT - 1,
56
57#define IS_SUBSYS_ENABLED(option) IS_MODULE(option)
58#include <linux/cgroup_subsys.h>
59#undef IS_SUBSYS_ENABLED
Daniel Wagnera6f00292012-09-12 16:12:08 +020060 CGROUP_SUBSYS_COUNT,
Paul Menage817929e2007-10-18 23:39:36 -070061};
62#undef SUBSYS
63
Paul Menageddbcc7e2007-10-18 23:39:30 -070064/* Per-subsystem/per-cgroup state maintained by the system. */
65struct cgroup_subsys_state {
Paul Menaged20a3902009-04-02 16:57:22 -070066 /*
67 * The cgroup that this subsystem is attached to. Useful
Paul Menageddbcc7e2007-10-18 23:39:30 -070068 * for subsystems that want to know about the cgroup
Paul Menaged20a3902009-04-02 16:57:22 -070069 * hierarchy structure
70 */
Paul Menageddbcc7e2007-10-18 23:39:30 -070071 struct cgroup *cgroup;
72
Paul Menaged20a3902009-04-02 16:57:22 -070073 /*
74 * State maintained by the cgroup system to allow subsystems
Paul Menagee7c5ec92009-01-07 18:08:38 -080075 * to be "busy". Should be accessed via css_get(),
Tao Mad0b2fdd2012-11-20 22:06:18 +080076 * css_tryget() and css_put().
Paul Menaged20a3902009-04-02 16:57:22 -070077 */
Paul Menageddbcc7e2007-10-18 23:39:30 -070078
79 atomic_t refcnt;
80
81 unsigned long flags;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -070082 /* ID for this css, if possible */
Arnd Bergmann2c392b82010-02-24 19:41:39 +010083 struct css_id __rcu *id;
Tejun Heo48ddbe12012-04-01 12:09:56 -070084
85 /* Used to put @cgroup->dentry on the last css_put() */
86 struct work_struct dput_work;
Paul Menageddbcc7e2007-10-18 23:39:30 -070087};
88
89/* bits in struct cgroup_subsys_state flags field */
90enum {
Tejun Heo38b53ab2012-11-19 08:13:36 -080091 CSS_ROOT = (1 << 0), /* this CSS is the root of the subsystem */
Tejun Heo92fb9742012-11-19 08:13:38 -080092 CSS_ONLINE = (1 << 1), /* between ->css_online() and ->css_offline() */
Paul Menageddbcc7e2007-10-18 23:39:30 -070093};
94
Daisuke Nishimurad7b9fff2010-03-10 15:22:05 -080095/* Caller must verify that the css is not for root cgroup */
96static inline void __css_get(struct cgroup_subsys_state *css, int count)
97{
98 atomic_add(count, &css->refcnt);
99}
100
Paul Menageddbcc7e2007-10-18 23:39:30 -0700101/*
Paul Menagee7c5ec92009-01-07 18:08:38 -0800102 * Call css_get() to hold a reference on the css; it can be used
103 * for a reference obtained via:
104 * - an existing ref-counted reference to the css
105 * - task->cgroups for a locked task
Paul Menageddbcc7e2007-10-18 23:39:30 -0700106 */
107
108static inline void css_get(struct cgroup_subsys_state *css)
109{
110 /* We don't need to reference count the root state */
Tejun Heo38b53ab2012-11-19 08:13:36 -0800111 if (!(css->flags & CSS_ROOT))
Daisuke Nishimurad7b9fff2010-03-10 15:22:05 -0800112 __css_get(css, 1);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700113}
Paul Menagee7c5ec92009-01-07 18:08:38 -0800114
Paul Menagee7c5ec92009-01-07 18:08:38 -0800115/*
116 * Call css_tryget() to take a reference on a css if your existing
117 * (known-valid) reference isn't already ref-counted. Returns false if
118 * the css has been destroyed.
119 */
120
Tejun Heo28b4c272012-04-01 12:09:56 -0700121extern bool __css_tryget(struct cgroup_subsys_state *css);
Paul Menagee7c5ec92009-01-07 18:08:38 -0800122static inline bool css_tryget(struct cgroup_subsys_state *css)
123{
Tejun Heo38b53ab2012-11-19 08:13:36 -0800124 if (css->flags & CSS_ROOT)
Paul Menagee7c5ec92009-01-07 18:08:38 -0800125 return true;
Tejun Heo28b4c272012-04-01 12:09:56 -0700126 return __css_tryget(css);
Paul Menagee7c5ec92009-01-07 18:08:38 -0800127}
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
Tejun Heo28b4c272012-04-01 12:09:56 -0700134extern void __css_put(struct cgroup_subsys_state *css);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700135static inline void css_put(struct cgroup_subsys_state *css)
136{
Tejun Heo38b53ab2012-11-19 08:13:36 -0800137 if (!(css->flags & CSS_ROOT))
Tejun Heo28b4c272012-04-01 12:09:56 -0700138 __css_put(css);
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 /*
Tejun Heo2260e7f2012-11-19 08:13:38 -0800153 * Clone the parent's configuration when creating a new child
154 * cpuset cgroup. For historical reasons, this option can be
155 * specified at mount time and thus is implemented here.
Daniel Lezcano97978e62010-10-27 15:33:35 -0700156 */
Tejun Heo2260e7f2012-11-19 08:13:38 -0800157 CGRP_CPUSET_CLONE_CHILDREN,
Paul Menage3116f0e2008-04-29 01:00:04 -0700158};
159
Li Zefan65dff752013-03-01 15:01:56 +0800160struct cgroup_name {
161 struct rcu_head rcu_head;
162 char name[];
163};
164
Paul Menageddbcc7e2007-10-18 23:39:30 -0700165struct cgroup {
166 unsigned long flags; /* "unsigned long" so bitops work */
167
Paul Menaged20a3902009-04-02 16:57:22 -0700168 /*
169 * count users of this cgroup. >0 means busy, but doesn't
170 * necessarily indicate the number of tasks in the cgroup
171 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700172 atomic_t count;
173
Tejun Heo0a950f62012-11-19 09:02:12 -0800174 int id; /* ida allocated in-hierarchy ID */
175
Paul Menageddbcc7e2007-10-18 23:39:30 -0700176 /*
177 * We link our 'sibling' struct into our parent's 'children'.
178 * Our children link their 'sibling' into our 'children'.
179 */
180 struct list_head sibling; /* my parent's children */
181 struct list_head children; /* my children */
Tejun Heo05ef1d72012-04-01 12:09:56 -0700182 struct list_head files; /* my files */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700183
Paul Menaged20a3902009-04-02 16:57:22 -0700184 struct cgroup *parent; /* my parent */
Tejun Heofebfcef2012-11-19 08:13:36 -0800185 struct dentry *dentry; /* cgroup fs entry, RCU protected */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700186
Li Zefan65dff752013-03-01 15:01:56 +0800187 /*
188 * This is a copy of dentry->d_name, and it's needed because
189 * we can't use dentry->d_name in cgroup_path().
190 *
191 * You must acquire rcu_read_lock() to access cgrp->name, and
192 * the only place that can change it is rename(), which is
193 * protected by parent dir's i_mutex.
194 *
195 * Normally you should use cgroup_name() wrapper rather than
196 * access it directly.
197 */
198 struct cgroup_name __rcu *name;
199
Paul Menageddbcc7e2007-10-18 23:39:30 -0700200 /* Private pointers for each registered subsystem */
201 struct cgroup_subsys_state *subsys[CGROUP_SUBSYS_COUNT];
202
203 struct cgroupfs_root *root;
204 struct cgroup *top_cgroup;
Paul Menage817929e2007-10-18 23:39:36 -0700205
206 /*
207 * List of cg_cgroup_links pointing at css_sets with
208 * tasks in this cgroup. Protected by css_set_lock
209 */
210 struct list_head css_sets;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700211
Tejun Heob0ca5a82012-04-01 12:09:54 -0700212 struct list_head allcg_node; /* cgroupfs_root->allcg_list */
Tejun Heo8e3f6542012-04-01 12:09:55 -0700213 struct list_head cft_q_node; /* used during cftype add/rm */
Tejun Heob0ca5a82012-04-01 12:09:54 -0700214
Paul Menage81a6a5c2007-10-18 23:39:38 -0700215 /*
216 * Linked list running through all cgroups that can
217 * potentially be reaped by the release agent. Protected by
218 * release_list_lock
219 */
220 struct list_head release_list;
Paul Menagecc31edc2008-10-18 20:28:04 -0700221
Ben Blum72a8cb32009-09-23 15:56:27 -0700222 /*
223 * list of pidlists, up to two for each namespace (one for procs, one
224 * for tasks); created on demand.
225 */
226 struct list_head pidlists;
227 struct mutex pidlist_mutex;
Paul Menagea47295e2009-01-07 18:07:44 -0800228
229 /* For RCU-protected deletion */
230 struct rcu_head rcu_head;
Li Zefanbe445622013-01-24 14:31:42 +0800231 struct work_struct free_work;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800232
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300233 /* List of events which userspace want to receive */
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800234 struct list_head event_list;
235 spinlock_t event_list_lock;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -0400236
237 /* directory xattrs */
238 struct simple_xattrs xattrs;
Paul Menage817929e2007-10-18 23:39:36 -0700239};
240
Paul Menaged20a3902009-04-02 16:57:22 -0700241/*
242 * A css_set is a structure holding pointers to a set of
Paul Menage817929e2007-10-18 23:39:36 -0700243 * cgroup_subsys_state objects. This saves space in the task struct
244 * object and speeds up fork()/exit(), since a single inc/dec and a
Paul Menaged20a3902009-04-02 16:57:22 -0700245 * list_add()/del() can bump the reference count on the entire cgroup
246 * set for a task.
Paul Menage817929e2007-10-18 23:39:36 -0700247 */
248
249struct css_set {
250
251 /* Reference count */
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700252 atomic_t refcount;
Paul Menage817929e2007-10-18 23:39:36 -0700253
254 /*
Li Zefan472b1052008-04-29 01:00:11 -0700255 * List running through all cgroup groups in the same hash
256 * slot. Protected by css_set_lock
257 */
258 struct hlist_node hlist;
259
260 /*
Paul Menage817929e2007-10-18 23:39:36 -0700261 * List running through all tasks using this cgroup
262 * group. Protected by css_set_lock
263 */
264 struct list_head tasks;
265
266 /*
267 * List of cg_cgroup_link objects on link chains from
268 * cgroups referenced from this css_set. Protected by
269 * css_set_lock
270 */
271 struct list_head cg_links;
272
273 /*
274 * Set of subsystem states, one for each subsystem. This array
275 * is immutable after creation apart from the init_css_set
Ben Blumcf5d5942010-03-10 15:22:09 -0800276 * during subsystem registration (at boot time) and modular subsystem
277 * loading/unloading.
Paul Menage817929e2007-10-18 23:39:36 -0700278 */
279 struct cgroup_subsys_state *subsys[CGROUP_SUBSYS_COUNT];
Ben Blumc3783692009-09-23 15:56:29 -0700280
281 /* For RCU-protected deletion */
282 struct rcu_head rcu_head;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700283};
284
Paul Menage91796562008-04-29 01:00:01 -0700285/*
286 * cgroup_map_cb is an abstract callback API for reporting map-valued
287 * control files
288 */
289
290struct cgroup_map_cb {
291 int (*fill)(struct cgroup_map_cb *cb, const char *key, u64 value);
292 void *state;
293};
294
Paul Menaged20a3902009-04-02 16:57:22 -0700295/*
296 * struct cftype: handler definitions for cgroup control files
Paul Menageddbcc7e2007-10-18 23:39:30 -0700297 *
298 * When reading/writing to a file:
Li Zefana043e3b2008-02-23 15:24:09 -0800299 * - the cgroup to use is file->f_dentry->d_parent->d_fsdata
Paul Menageddbcc7e2007-10-18 23:39:30 -0700300 * - the 'cftype' of the file is file->f_dentry->d_fsdata
301 */
302
Tejun Heo8e3f6542012-04-01 12:09:55 -0700303/* cftype->flags */
304#define CFTYPE_ONLY_ON_ROOT (1U << 0) /* only create on root cg */
Tao Mad0b2fdd2012-11-20 22:06:18 +0800305#define CFTYPE_NOT_ON_ROOT (1U << 1) /* don't create on root cg */
Tejun Heo8e3f6542012-04-01 12:09:55 -0700306
307#define MAX_CFTYPE_NAME 64
308
Paul Menageddbcc7e2007-10-18 23:39:30 -0700309struct cftype {
Paul Menaged20a3902009-04-02 16:57:22 -0700310 /*
311 * By convention, the name should begin with the name of the
Tejun Heo8e3f6542012-04-01 12:09:55 -0700312 * subsystem, followed by a period. Zero length string indicates
313 * end of cftype array.
Paul Menaged20a3902009-04-02 16:57:22 -0700314 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700315 char name[MAX_CFTYPE_NAME];
316 int private;
Li Zefan099fca32009-04-02 16:57:29 -0700317 /*
318 * If not 0, file mode is set to this value, otherwise it will
319 * be figured out automatically
320 */
Al Viroa5e7ed32011-07-26 01:55:55 -0400321 umode_t mode;
Paul Menagedb3b1492008-07-25 01:46:58 -0700322
323 /*
324 * If non-zero, defines the maximum length of string that can
325 * be passed to write_string; defaults to 64
326 */
327 size_t max_write_len;
328
Tejun Heo8e3f6542012-04-01 12:09:55 -0700329 /* CFTYPE_* flags */
330 unsigned int flags;
331
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -0400332 /* file xattrs */
333 struct simple_xattrs xattrs;
334
Paul Menagece16b492008-07-25 01:46:57 -0700335 int (*open)(struct inode *inode, struct file *file);
336 ssize_t (*read)(struct cgroup *cgrp, struct cftype *cft,
337 struct file *file,
338 char __user *buf, size_t nbytes, loff_t *ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700339 /*
Paul Menagef4c753b2008-04-29 00:59:56 -0700340 * read_u64() is a shortcut for the common case of returning a
Paul Menageddbcc7e2007-10-18 23:39:30 -0700341 * single integer. Use it in place of read()
342 */
Paul Menagece16b492008-07-25 01:46:57 -0700343 u64 (*read_u64)(struct cgroup *cgrp, struct cftype *cft);
Paul Menage91796562008-04-29 01:00:01 -0700344 /*
Paul Menagee73d2c62008-04-29 01:00:06 -0700345 * read_s64() is a signed version of read_u64()
346 */
Paul Menagece16b492008-07-25 01:46:57 -0700347 s64 (*read_s64)(struct cgroup *cgrp, struct cftype *cft);
Paul Menagee73d2c62008-04-29 01:00:06 -0700348 /*
Paul Menage91796562008-04-29 01:00:01 -0700349 * read_map() is used for defining a map of key/value
350 * pairs. It should call cb->fill(cb, key, value) for each
351 * entry. The key/value pairs (and their ordering) should not
352 * change between reboots.
353 */
Paul Menagece16b492008-07-25 01:46:57 -0700354 int (*read_map)(struct cgroup *cont, struct cftype *cft,
355 struct cgroup_map_cb *cb);
Serge E. Hallyn29486df2008-04-29 01:00:14 -0700356 /*
357 * read_seq_string() is used for outputting a simple sequence
358 * using seqfile.
359 */
Paul Menagece16b492008-07-25 01:46:57 -0700360 int (*read_seq_string)(struct cgroup *cont, struct cftype *cft,
361 struct seq_file *m);
Paul Menage91796562008-04-29 01:00:01 -0700362
Paul Menagece16b492008-07-25 01:46:57 -0700363 ssize_t (*write)(struct cgroup *cgrp, struct cftype *cft,
364 struct file *file,
365 const char __user *buf, size_t nbytes, loff_t *ppos);
Paul Menage355e0c42007-10-18 23:39:33 -0700366
367 /*
Paul Menagef4c753b2008-04-29 00:59:56 -0700368 * write_u64() is a shortcut for the common case of accepting
Paul Menage355e0c42007-10-18 23:39:33 -0700369 * a single integer (as parsed by simple_strtoull) from
370 * userspace. Use in place of write(); return 0 or error.
371 */
Paul Menagece16b492008-07-25 01:46:57 -0700372 int (*write_u64)(struct cgroup *cgrp, struct cftype *cft, u64 val);
Paul Menagee73d2c62008-04-29 01:00:06 -0700373 /*
374 * write_s64() is a signed version of write_u64()
375 */
Paul Menagece16b492008-07-25 01:46:57 -0700376 int (*write_s64)(struct cgroup *cgrp, struct cftype *cft, s64 val);
Paul Menage355e0c42007-10-18 23:39:33 -0700377
Pavel Emelyanovd447ea22008-04-29 01:00:08 -0700378 /*
Paul Menagedb3b1492008-07-25 01:46:58 -0700379 * write_string() is passed a nul-terminated kernelspace
380 * buffer of maximum length determined by max_write_len.
381 * Returns 0 or -ve error code.
382 */
383 int (*write_string)(struct cgroup *cgrp, struct cftype *cft,
384 const char *buffer);
385 /*
Pavel Emelyanovd447ea22008-04-29 01:00:08 -0700386 * trigger() callback can be used to get some kick from the
387 * userspace, when the actual string written is not important
388 * at all. The private field can be used to determine the
389 * kick type for multiplexing.
390 */
391 int (*trigger)(struct cgroup *cgrp, unsigned int event);
392
Paul Menagece16b492008-07-25 01:46:57 -0700393 int (*release)(struct inode *inode, struct file *file);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800394
395 /*
396 * register_event() callback will be used to add new userspace
397 * waiter for changes related to the cftype. Implement it if
398 * you want to provide this functionality. Use eventfd_signal()
399 * on eventfd to send notification to userspace.
400 */
401 int (*register_event)(struct cgroup *cgrp, struct cftype *cft,
402 struct eventfd_ctx *eventfd, const char *args);
403 /*
404 * unregister_event() callback will be called when userspace
405 * closes the eventfd or on cgroup removing.
406 * This callback must be implemented, if you want provide
407 * notification functionality.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800408 */
Kirill A. Shutemov907860e2010-05-26 14:42:46 -0700409 void (*unregister_event)(struct cgroup *cgrp, struct cftype *cft,
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800410 struct eventfd_ctx *eventfd);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700411};
412
Tejun Heo8e3f6542012-04-01 12:09:55 -0700413/*
414 * cftype_sets describe cftypes belonging to a subsystem and are chained at
415 * cgroup_subsys->cftsets. Each cftset points to an array of cftypes
416 * terminated by zero length name.
417 */
418struct cftype_set {
419 struct list_head node; /* chained at subsys->cftsets */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -0400420 struct cftype *cfts;
Tejun Heo8e3f6542012-04-01 12:09:55 -0700421};
422
Cliff Wickman31a7df02008-02-07 00:14:42 -0800423struct cgroup_scanner {
424 struct cgroup *cg;
425 int (*test_task)(struct task_struct *p, struct cgroup_scanner *scan);
426 void (*process_task)(struct task_struct *p,
427 struct cgroup_scanner *scan);
428 struct ptr_heap *heap;
Li Zefanbd1a8ab2009-04-02 16:57:50 -0700429 void *data;
Cliff Wickman31a7df02008-02-07 00:14:42 -0800430};
431
Li Zefan65dff752013-03-01 15:01:56 +0800432/* Caller should hold rcu_read_lock() */
433static inline const char *cgroup_name(const struct cgroup *cgrp)
434{
435 return rcu_dereference(cgrp->name)->name;
436}
437
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -0400438int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts);
439int cgroup_rm_cftypes(struct cgroup_subsys *ss, struct cftype *cfts);
Tejun Heo8e3f6542012-04-01 12:09:55 -0700440
Li Zefanffd2d882008-02-23 15:24:09 -0800441int cgroup_is_removed(const struct cgroup *cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700442
Li Zefanffd2d882008-02-23 15:24:09 -0800443int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700444
Li Zefanffd2d882008-02-23 15:24:09 -0800445int cgroup_task_count(const struct cgroup *cgrp);
Paul Menagebbcb81d2007-10-18 23:39:32 -0700446
Thadeu Lima de Souza Cascardo21acb9c2009-02-04 10:12:08 +0100447/*
Tejun Heo2f7ee562011-12-12 18:12:21 -0800448 * Control Group taskset, used to pass around set of tasks to cgroup_subsys
449 * methods.
450 */
451struct cgroup_taskset;
452struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset);
453struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset);
454struct cgroup *cgroup_taskset_cur_cgroup(struct cgroup_taskset *tset);
455int cgroup_taskset_size(struct cgroup_taskset *tset);
456
457/**
458 * cgroup_taskset_for_each - iterate cgroup_taskset
459 * @task: the loop cursor
460 * @skip_cgrp: skip if task's cgroup matches this, %NULL to iterate through all
461 * @tset: taskset to iterate
462 */
463#define cgroup_taskset_for_each(task, skip_cgrp, tset) \
464 for ((task) = cgroup_taskset_first((tset)); (task); \
465 (task) = cgroup_taskset_next((tset))) \
466 if (!(skip_cgrp) || \
467 cgroup_taskset_cur_cgroup((tset)) != (skip_cgrp))
468
469/*
Thadeu Lima de Souza Cascardo21acb9c2009-02-04 10:12:08 +0100470 * Control Group subsystem type.
471 * See Documentation/cgroups/cgroups.txt for details
472 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700473
474struct cgroup_subsys {
Tejun Heo92fb9742012-11-19 08:13:38 -0800475 struct cgroup_subsys_state *(*css_alloc)(struct cgroup *cgrp);
476 int (*css_online)(struct cgroup *cgrp);
477 void (*css_offline)(struct cgroup *cgrp);
478 void (*css_free)(struct cgroup *cgrp);
479
Li Zefan761b3ef52012-01-31 13:47:36 +0800480 int (*can_attach)(struct cgroup *cgrp, struct cgroup_taskset *tset);
481 void (*cancel_attach)(struct cgroup *cgrp, struct cgroup_taskset *tset);
482 void (*attach)(struct cgroup *cgrp, struct cgroup_taskset *tset);
483 void (*fork)(struct task_struct *task);
484 void (*exit)(struct cgroup *cgrp, struct cgroup *old_cgrp,
485 struct task_struct *task);
Li Zefan761b3ef52012-01-31 13:47:36 +0800486 void (*bind)(struct cgroup *root);
Hugh Dickinse5991372009-01-06 14:39:22 -0800487
Paul Menageddbcc7e2007-10-18 23:39:30 -0700488 int subsys_id;
489 int active;
Paul Menage8bab8dd2008-04-04 14:29:57 -0700490 int disabled;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700491 int early_init;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700492 /*
493 * True if this subsys uses ID. ID is not available before cgroup_init()
494 * (not available in early_init time.)
495 */
496 bool use_id;
Tejun Heo48ddbe12012-04-01 12:09:56 -0700497
498 /*
Tejun Heo8c7f6ed2012-09-13 12:20:58 -0700499 * If %false, this subsystem is properly hierarchical -
500 * configuration, resource accounting and restriction on a parent
501 * cgroup cover those of its children. If %true, hierarchy support
502 * is broken in some ways - some subsystems ignore hierarchy
503 * completely while others are only implemented half-way.
504 *
505 * It's now disallowed to create nested cgroups if the subsystem is
506 * broken and cgroup core will emit a warning message on such
507 * cases. Eventually, all subsystems will be made properly
508 * hierarchical and this will go away.
509 */
510 bool broken_hierarchy;
511 bool warned_broken_hierarchy;
512
Paul Menageddbcc7e2007-10-18 23:39:30 -0700513#define MAX_CGROUP_TYPE_NAMELEN 32
514 const char *name;
515
Paul Menage999cd8a2009-01-07 18:08:36 -0800516 /*
Paul Menage999cd8a2009-01-07 18:08:36 -0800517 * Link to parent, and list entry in parent's children.
Li Zefan6be96a52012-06-06 19:12:30 -0700518 * Protected by cgroup_lock()
Paul Menage999cd8a2009-01-07 18:08:36 -0800519 */
520 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700521 struct list_head sibling;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700522 /* used when use_id == true */
523 struct idr idr;
Hugh Dickins42aee6c2012-03-21 16:34:21 -0700524 spinlock_t id_lock;
Ben Blume6a11052010-03-10 15:22:09 -0800525
Tejun Heo8e3f6542012-04-01 12:09:55 -0700526 /* list of cftype_sets */
527 struct list_head cftsets;
528
529 /* base cftypes, automatically [de]registered with subsys itself */
530 struct cftype *base_cftypes;
531 struct cftype_set base_cftset;
532
Ben Blume6a11052010-03-10 15:22:09 -0800533 /* should be defined only by modular subsystems */
534 struct module *module;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700535};
536
537#define SUBSYS(_x) extern struct cgroup_subsys _x ## _subsys;
Daniel Wagner5fc0b022012-09-12 16:12:05 +0200538#define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700539#include <linux/cgroup_subsys.h>
Daniel Wagner5fc0b022012-09-12 16:12:05 +0200540#undef IS_SUBSYS_ENABLED
Paul Menageddbcc7e2007-10-18 23:39:30 -0700541#undef SUBSYS
542
543static inline struct cgroup_subsys_state *cgroup_subsys_state(
Li Zefanffd2d882008-02-23 15:24:09 -0800544 struct cgroup *cgrp, int subsys_id)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700545{
Li Zefanffd2d882008-02-23 15:24:09 -0800546 return cgrp->subsys[subsys_id];
Paul Menageddbcc7e2007-10-18 23:39:30 -0700547}
548
Peter Zijlstradc61b1d2010-06-08 11:40:42 +0200549/*
550 * function to get the cgroup_subsys_state which allows for extra
551 * rcu_dereference_check() conditions, such as locks used during the
552 * cgroup_subsys::attach() methods.
553 */
Tejun Heo22194492013-04-07 09:29:51 -0700554#ifdef CONFIG_PROVE_RCU
555extern struct mutex cgroup_mutex;
Peter Zijlstradc61b1d2010-06-08 11:40:42 +0200556#define task_subsys_state_check(task, subsys_id, __c) \
Tejun Heo22194492013-04-07 09:29:51 -0700557 rcu_dereference_check((task)->cgroups->subsys[(subsys_id)], \
558 lockdep_is_held(&(task)->alloc_lock) || \
559 lockdep_is_held(&cgroup_mutex) || (__c))
560#else
561#define task_subsys_state_check(task, subsys_id, __c) \
562 rcu_dereference((task)->cgroups->subsys[(subsys_id)])
563#endif
Peter Zijlstradc61b1d2010-06-08 11:40:42 +0200564
565static inline struct cgroup_subsys_state *
566task_subsys_state(struct task_struct *task, int subsys_id)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700567{
Peter Zijlstradc61b1d2010-06-08 11:40:42 +0200568 return task_subsys_state_check(task, subsys_id, false);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700569}
570
571static inline struct cgroup* task_cgroup(struct task_struct *task,
572 int subsys_id)
573{
574 return task_subsys_state(task, subsys_id)->cgroup;
575}
576
Tejun Heo574bd9f2012-11-09 09:12:29 -0800577/**
578 * cgroup_for_each_child - iterate through children of a cgroup
579 * @pos: the cgroup * to use as the loop cursor
580 * @cgroup: cgroup whose children to walk
581 *
582 * Walk @cgroup's children. Must be called under rcu_read_lock(). A child
Tejun Heo92fb9742012-11-19 08:13:38 -0800583 * cgroup which hasn't finished ->css_online() or already has finished
584 * ->css_offline() may show up during traversal and it's each subsystem's
Tejun Heo574bd9f2012-11-09 09:12:29 -0800585 * responsibility to verify that each @pos is alive.
586 *
Tejun Heo92fb9742012-11-19 08:13:38 -0800587 * If a subsystem synchronizes against the parent in its ->css_online() and
588 * before starting iterating, a cgroup which finished ->css_online() is
589 * guaranteed to be visible in the future iterations.
Tejun Heo574bd9f2012-11-09 09:12:29 -0800590 */
591#define cgroup_for_each_child(pos, cgroup) \
592 list_for_each_entry_rcu(pos, &(cgroup)->children, sibling)
593
594struct cgroup *cgroup_next_descendant_pre(struct cgroup *pos,
595 struct cgroup *cgroup);
Tejun Heo12a9d2f2013-01-07 08:49:33 -0800596struct cgroup *cgroup_rightmost_descendant(struct cgroup *pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -0800597
598/**
599 * cgroup_for_each_descendant_pre - pre-order walk of a cgroup's descendants
600 * @pos: the cgroup * to use as the loop cursor
601 * @cgroup: cgroup whose descendants to walk
602 *
603 * Walk @cgroup's descendants. Must be called under rcu_read_lock(). A
Tejun Heo92fb9742012-11-19 08:13:38 -0800604 * descendant cgroup which hasn't finished ->css_online() or already has
605 * finished ->css_offline() may show up during traversal and it's each
Tejun Heo574bd9f2012-11-09 09:12:29 -0800606 * subsystem's responsibility to verify that each @pos is alive.
607 *
Tejun Heo92fb9742012-11-19 08:13:38 -0800608 * If a subsystem synchronizes against the parent in its ->css_online() and
609 * before starting iterating, and synchronizes against @pos on each
610 * iteration, any descendant cgroup which finished ->css_offline() is
Tejun Heo574bd9f2012-11-09 09:12:29 -0800611 * guaranteed to be visible in the future iterations.
612 *
613 * In other words, the following guarantees that a descendant can't escape
614 * state updates of its ancestors.
615 *
Tejun Heo92fb9742012-11-19 08:13:38 -0800616 * my_online(@cgrp)
Tejun Heo574bd9f2012-11-09 09:12:29 -0800617 * {
618 * Lock @cgrp->parent and @cgrp;
619 * Inherit state from @cgrp->parent;
620 * Unlock both.
621 * }
622 *
623 * my_update_state(@cgrp)
624 * {
625 * Lock @cgrp;
626 * Update @cgrp's state;
627 * Unlock @cgrp;
628 *
629 * cgroup_for_each_descendant_pre(@pos, @cgrp) {
630 * Lock @pos;
631 * Verify @pos is alive and inherit state from @pos->parent;
632 * Unlock @pos;
633 * }
634 * }
635 *
636 * As long as the inheriting step, including checking the parent state, is
637 * enclosed inside @pos locking, double-locking the parent isn't necessary
638 * while inheriting. The state update to the parent is guaranteed to be
639 * visible by walking order and, as long as inheriting operations to the
640 * same @pos are atomic to each other, multiple updates racing each other
641 * still result in the correct state. It's guaranateed that at least one
642 * inheritance happens for any cgroup after the latest update to its
643 * parent.
644 *
645 * If checking parent's state requires locking the parent, each inheriting
646 * iteration should lock and unlock both @pos->parent and @pos.
647 *
648 * Alternatively, a subsystem may choose to use a single global lock to
Tejun Heo92fb9742012-11-19 08:13:38 -0800649 * synchronize ->css_online() and ->css_offline() against tree-walking
Tejun Heo574bd9f2012-11-09 09:12:29 -0800650 * operations.
651 */
652#define cgroup_for_each_descendant_pre(pos, cgroup) \
653 for (pos = cgroup_next_descendant_pre(NULL, (cgroup)); (pos); \
654 pos = cgroup_next_descendant_pre((pos), (cgroup)))
655
656struct cgroup *cgroup_next_descendant_post(struct cgroup *pos,
657 struct cgroup *cgroup);
658
659/**
660 * cgroup_for_each_descendant_post - post-order walk of a cgroup's descendants
661 * @pos: the cgroup * to use as the loop cursor
662 * @cgroup: cgroup whose descendants to walk
663 *
664 * Similar to cgroup_for_each_descendant_pre() but performs post-order
665 * traversal instead. Note that the walk visibility guarantee described in
666 * pre-order walk doesn't apply the same to post-order walks.
667 */
668#define cgroup_for_each_descendant_post(pos, cgroup) \
669 for (pos = cgroup_next_descendant_post(NULL, (cgroup)); (pos); \
670 pos = cgroup_next_descendant_post((pos), (cgroup)))
671
Paul Menage817929e2007-10-18 23:39:36 -0700672/* A cgroup_iter should be treated as an opaque object */
673struct cgroup_iter {
674 struct list_head *cg_link;
675 struct list_head *task;
676};
677
Paul Menaged20a3902009-04-02 16:57:22 -0700678/*
679 * To iterate across the tasks in a cgroup:
Paul Menage817929e2007-10-18 23:39:36 -0700680 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400681 * 1) call cgroup_iter_start to initialize an iterator
Paul Menage817929e2007-10-18 23:39:36 -0700682 *
683 * 2) call cgroup_iter_next() to retrieve member tasks until it
684 * returns NULL or until you want to end the iteration
685 *
686 * 3) call cgroup_iter_end() to destroy the iterator.
Cliff Wickman31a7df02008-02-07 00:14:42 -0800687 *
Paul Menaged20a3902009-04-02 16:57:22 -0700688 * Or, call cgroup_scan_tasks() to iterate through every task in a
689 * cgroup - cgroup_scan_tasks() holds the css_set_lock when calling
690 * the test_task() callback, but not while calling the process_task()
691 * callback.
Paul Menage817929e2007-10-18 23:39:36 -0700692 */
Li Zefanffd2d882008-02-23 15:24:09 -0800693void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it);
694struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -0700695 struct cgroup_iter *it);
Li Zefanffd2d882008-02-23 15:24:09 -0800696void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it);
Cliff Wickman31a7df02008-02-07 00:14:42 -0800697int cgroup_scan_tasks(struct cgroup_scanner *scan);
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -0700698int cgroup_attach_task_all(struct task_struct *from, struct task_struct *);
Tejun Heo8cc99342013-04-07 09:29:50 -0700699int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from);
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -0700700
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700701/*
702 * CSS ID is ID for cgroup_subsys_state structs under subsys. This only works
703 * if cgroup_subsys.use_id == true. It can be used for looking up and scanning.
704 * CSS ID is assigned at cgroup allocation (create) automatically
705 * and removed when subsys calls free_css_id() function. This is because
706 * the lifetime of cgroup_subsys_state is subsys's matter.
707 *
708 * Looking up and scanning function should be called under rcu_read_lock().
Li Zefan6be96a52012-06-06 19:12:30 -0700709 * Taking cgroup_mutex is not necessary for following calls.
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700710 * But the css returned by this routine can be "not populated yet" or "being
711 * destroyed". The caller should check css and cgroup's status.
712 */
713
714/*
715 * Typically Called at ->destroy(), or somewhere the subsys frees
716 * cgroup_subsys_state.
717 */
718void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css);
719
720/* Find a cgroup_subsys_state which has given ID */
721
722struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id);
723
724/*
725 * Get a cgroup whose id is greater than or equal to id under tree of root.
726 * Returning a cgroup_subsys_state or NULL.
727 */
728struct cgroup_subsys_state *css_get_next(struct cgroup_subsys *ss, int id,
729 struct cgroup_subsys_state *root, int *foundid);
730
731/* Returns true if root is ancestor of cg */
732bool css_is_ancestor(struct cgroup_subsys_state *cg,
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700733 const struct cgroup_subsys_state *root);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700734
735/* Get id and depth of css */
736unsigned short css_id(struct cgroup_subsys_state *css);
737unsigned short css_depth(struct cgroup_subsys_state *css);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200738struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700739
Paul Menageddbcc7e2007-10-18 23:39:30 -0700740#else /* !CONFIG_CGROUPS */
741
742static inline int cgroup_init_early(void) { return 0; }
743static inline int cgroup_init(void) { return 0; }
Paul Menageb4f48b62007-10-18 23:39:33 -0700744static inline void cgroup_fork(struct task_struct *p) {}
Paul Menage817929e2007-10-18 23:39:36 -0700745static inline void cgroup_post_fork(struct task_struct *p) {}
Paul Menageb4f48b62007-10-18 23:39:33 -0700746static inline void cgroup_exit(struct task_struct *p, int callbacks) {}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700747
748static inline void cgroup_lock(void) {}
749static inline void cgroup_unlock(void) {}
Balbir Singh846c7bb2007-10-18 23:39:44 -0700750static inline int cgroupstats_build(struct cgroupstats *stats,
751 struct dentry *dentry)
752{
753 return -EINVAL;
754}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700755
Sridhar Samudralad7926ee2010-05-30 22:24:39 +0200756/* No cgroups - nothing to do */
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -0700757static inline int cgroup_attach_task_all(struct task_struct *from,
758 struct task_struct *t)
759{
760 return 0;
761}
Sridhar Samudralad7926ee2010-05-30 22:24:39 +0200762
Paul Menageddbcc7e2007-10-18 23:39:30 -0700763#endif /* !CONFIG_CGROUPS */
764
Paul Menageddbcc7e2007-10-18 23:39:30 -0700765#endif /* _LINUX_CGROUP_H */