blob: 145901f5ef99ae554f50f34e222a19012b16afdd [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>
Tejun Heo48ddbe12012-04-01 12:09:56 -070019#include <linux/workqueue.h>
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -040020#include <linux/xattr.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070021
22#ifdef CONFIG_CGROUPS
23
24struct cgroupfs_root;
25struct cgroup_subsys;
26struct inode;
Paul Menage84eea842008-07-25 01:47:00 -070027struct cgroup;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -070028struct css_id;
Paul Menageddbcc7e2007-10-18 23:39:30 -070029
30extern int cgroup_init_early(void);
31extern int cgroup_init(void);
Paul Menageddbcc7e2007-10-18 23:39:30 -070032extern void cgroup_lock(void);
Paul E. McKenneyd11c5632010-02-22 17:04:50 -080033extern int cgroup_lock_is_held(void);
Paul Menage84eea842008-07-25 01:47:00 -070034extern bool cgroup_lock_live_group(struct cgroup *cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -070035extern void cgroup_unlock(void);
Paul Menageb4f48b62007-10-18 23:39:33 -070036extern void cgroup_fork(struct task_struct *p);
37extern void cgroup_fork_callbacks(struct task_struct *p);
Paul Menage817929e2007-10-18 23:39:36 -070038extern void cgroup_post_fork(struct task_struct *p);
Paul Menageb4f48b62007-10-18 23:39:33 -070039extern void cgroup_exit(struct task_struct *p, int run_callbacks);
Balbir Singh846c7bb2007-10-18 23:39:44 -070040extern int cgroupstats_build(struct cgroupstats *stats,
41 struct dentry *dentry);
Ben Blume6a11052010-03-10 15:22:09 -080042extern int cgroup_load_subsys(struct cgroup_subsys *ss);
Ben Blumcf5d5942010-03-10 15:22:09 -080043extern void cgroup_unload_subsys(struct cgroup_subsys *ss);
Paul Menageddbcc7e2007-10-18 23:39:30 -070044
Alexey Dobriyan828c0952009-10-01 15:43:56 -070045extern const struct file_operations proc_cgroup_operations;
Paul Menagea4243162007-10-18 23:39:35 -070046
Ben Blumaae8aab2010-03-10 15:22:07 -080047/* Define the enumeration of all builtin cgroup subsystems */
Paul Menage817929e2007-10-18 23:39:36 -070048#define SUBSYS(_x) _x ## _subsys_id,
49enum cgroup_subsys_id {
50#include <linux/cgroup_subsys.h>
Ben Blumaae8aab2010-03-10 15:22:07 -080051 CGROUP_BUILTIN_SUBSYS_COUNT
Paul Menage817929e2007-10-18 23:39:36 -070052};
53#undef SUBSYS
Ben Blumaae8aab2010-03-10 15:22:07 -080054/*
55 * This define indicates the maximum number of subsystems that can be loaded
56 * at once. We limit to this many since cgroupfs_root has subsys_bits to keep
57 * track of all of them.
58 */
59#define CGROUP_SUBSYS_COUNT (BITS_PER_BYTE*sizeof(unsigned long))
Paul Menage817929e2007-10-18 23:39:36 -070060
Paul Menageddbcc7e2007-10-18 23:39:30 -070061/* Per-subsystem/per-cgroup state maintained by the system. */
62struct cgroup_subsys_state {
Paul Menaged20a3902009-04-02 16:57:22 -070063 /*
64 * The cgroup that this subsystem is attached to. Useful
Paul Menageddbcc7e2007-10-18 23:39:30 -070065 * for subsystems that want to know about the cgroup
Paul Menaged20a3902009-04-02 16:57:22 -070066 * hierarchy structure
67 */
Paul Menageddbcc7e2007-10-18 23:39:30 -070068 struct cgroup *cgroup;
69
Paul Menaged20a3902009-04-02 16:57:22 -070070 /*
71 * State maintained by the cgroup system to allow subsystems
Paul Menagee7c5ec92009-01-07 18:08:38 -080072 * to be "busy". Should be accessed via css_get(),
Paul Menaged20a3902009-04-02 16:57:22 -070073 * css_tryget() and and css_put().
74 */
Paul Menageddbcc7e2007-10-18 23:39:30 -070075
76 atomic_t refcnt;
77
78 unsigned long flags;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -070079 /* ID for this css, if possible */
Arnd Bergmann2c392b82010-02-24 19:41:39 +010080 struct css_id __rcu *id;
Tejun Heo48ddbe12012-04-01 12:09:56 -070081
82 /* Used to put @cgroup->dentry on the last css_put() */
83 struct work_struct dput_work;
Paul Menageddbcc7e2007-10-18 23:39:30 -070084};
85
86/* bits in struct cgroup_subsys_state flags field */
87enum {
88 CSS_ROOT, /* This CSS is the root of the subsystem */
Paul Menagee7c5ec92009-01-07 18:08:38 -080089 CSS_REMOVED, /* This CSS is dead */
Tejun Heo48ddbe12012-04-01 12:09:56 -070090 CSS_CLEAR_CSS_REFS, /* @ss->__DEPRECATED_clear_css_refs */
Paul Menageddbcc7e2007-10-18 23:39:30 -070091};
92
Daisuke Nishimurad7b9fff2010-03-10 15:22:05 -080093/* Caller must verify that the css is not for root cgroup */
94static inline void __css_get(struct cgroup_subsys_state *css, int count)
95{
96 atomic_add(count, &css->refcnt);
97}
98
Paul Menageddbcc7e2007-10-18 23:39:30 -070099/*
Paul Menagee7c5ec92009-01-07 18:08:38 -0800100 * Call css_get() to hold a reference on the css; it can be used
101 * for a reference obtained via:
102 * - an existing ref-counted reference to the css
103 * - task->cgroups for a locked task
Paul Menageddbcc7e2007-10-18 23:39:30 -0700104 */
105
106static inline void css_get(struct cgroup_subsys_state *css)
107{
108 /* We don't need to reference count the root state */
109 if (!test_bit(CSS_ROOT, &css->flags))
Daisuke Nishimurad7b9fff2010-03-10 15:22:05 -0800110 __css_get(css, 1);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700111}
Paul Menagee7c5ec92009-01-07 18:08:38 -0800112
113static inline bool css_is_removed(struct cgroup_subsys_state *css)
114{
115 return test_bit(CSS_REMOVED, &css->flags);
116}
117
118/*
119 * Call css_tryget() to take a reference on a css if your existing
120 * (known-valid) reference isn't already ref-counted. Returns false if
121 * the css has been destroyed.
122 */
123
Tejun Heo28b4c272012-04-01 12:09:56 -0700124extern bool __css_tryget(struct cgroup_subsys_state *css);
Paul Menagee7c5ec92009-01-07 18:08:38 -0800125static inline bool css_tryget(struct cgroup_subsys_state *css)
126{
127 if (test_bit(CSS_ROOT, &css->flags))
128 return true;
Tejun Heo28b4c272012-04-01 12:09:56 -0700129 return __css_tryget(css);
Paul Menagee7c5ec92009-01-07 18:08:38 -0800130}
131
Paul Menageddbcc7e2007-10-18 23:39:30 -0700132/*
133 * css_put() should be called to release a reference taken by
Paul Menagee7c5ec92009-01-07 18:08:38 -0800134 * css_get() or css_tryget()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700135 */
136
Tejun Heo28b4c272012-04-01 12:09:56 -0700137extern void __css_put(struct cgroup_subsys_state *css);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700138static inline void css_put(struct cgroup_subsys_state *css)
139{
140 if (!test_bit(CSS_ROOT, &css->flags))
Tejun Heo28b4c272012-04-01 12:09:56 -0700141 __css_put(css);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700142}
143
Paul Menage3116f0e2008-04-29 01:00:04 -0700144/* bits in struct cgroup flags field */
145enum {
146 /* Control Group is dead */
147 CGRP_REMOVED,
Paul Menaged20a3902009-04-02 16:57:22 -0700148 /*
149 * Control Group has previously had a child cgroup or a task,
150 * but no longer (only if CGRP_NOTIFY_ON_RELEASE is set)
151 */
Paul Menage3116f0e2008-04-29 01:00:04 -0700152 CGRP_RELEASABLE,
153 /* Control Group requires release notifications to userspace */
154 CGRP_NOTIFY_ON_RELEASE,
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700155 /*
156 * A thread in rmdir() is wating for this cgroup.
157 */
158 CGRP_WAIT_ON_RMDIR,
Daniel Lezcano97978e62010-10-27 15:33:35 -0700159 /*
160 * Clone cgroup values when creating a new child cgroup
161 */
162 CGRP_CLONE_CHILDREN,
Paul Menage3116f0e2008-04-29 01:00:04 -0700163};
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
174 /*
175 * We link our 'sibling' struct into our parent's 'children'.
176 * Our children link their 'sibling' into our 'children'.
177 */
178 struct list_head sibling; /* my parent's children */
179 struct list_head children; /* my children */
Tejun Heo05ef1d72012-04-01 12:09:56 -0700180 struct list_head files; /* my files */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700181
Paul Menaged20a3902009-04-02 16:57:22 -0700182 struct cgroup *parent; /* my parent */
Arnd Bergmann2c392b82010-02-24 19:41:39 +0100183 struct dentry __rcu *dentry; /* cgroup fs entry, RCU protected */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700184
185 /* Private pointers for each registered subsystem */
186 struct cgroup_subsys_state *subsys[CGROUP_SUBSYS_COUNT];
187
188 struct cgroupfs_root *root;
189 struct cgroup *top_cgroup;
Paul Menage817929e2007-10-18 23:39:36 -0700190
191 /*
192 * List of cg_cgroup_links pointing at css_sets with
193 * tasks in this cgroup. Protected by css_set_lock
194 */
195 struct list_head css_sets;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700196
Tejun Heob0ca5a82012-04-01 12:09:54 -0700197 struct list_head allcg_node; /* cgroupfs_root->allcg_list */
Tejun Heo8e3f6542012-04-01 12:09:55 -0700198 struct list_head cft_q_node; /* used during cftype add/rm */
Tejun Heob0ca5a82012-04-01 12:09:54 -0700199
Paul Menage81a6a5c2007-10-18 23:39:38 -0700200 /*
201 * Linked list running through all cgroups that can
202 * potentially be reaped by the release agent. Protected by
203 * release_list_lock
204 */
205 struct list_head release_list;
Paul Menagecc31edc2008-10-18 20:28:04 -0700206
Ben Blum72a8cb32009-09-23 15:56:27 -0700207 /*
208 * list of pidlists, up to two for each namespace (one for procs, one
209 * for tasks); created on demand.
210 */
211 struct list_head pidlists;
212 struct mutex pidlist_mutex;
Paul Menagea47295e2009-01-07 18:07:44 -0800213
214 /* For RCU-protected deletion */
215 struct rcu_head rcu_head;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800216
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300217 /* List of events which userspace want to receive */
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800218 struct list_head event_list;
219 spinlock_t event_list_lock;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -0400220
221 /* directory xattrs */
222 struct simple_xattrs xattrs;
Paul Menage817929e2007-10-18 23:39:36 -0700223};
224
Paul Menaged20a3902009-04-02 16:57:22 -0700225/*
226 * A css_set is a structure holding pointers to a set of
Paul Menage817929e2007-10-18 23:39:36 -0700227 * cgroup_subsys_state objects. This saves space in the task struct
228 * object and speeds up fork()/exit(), since a single inc/dec and a
Paul Menaged20a3902009-04-02 16:57:22 -0700229 * list_add()/del() can bump the reference count on the entire cgroup
230 * set for a task.
Paul Menage817929e2007-10-18 23:39:36 -0700231 */
232
233struct css_set {
234
235 /* Reference count */
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700236 atomic_t refcount;
Paul Menage817929e2007-10-18 23:39:36 -0700237
238 /*
Li Zefan472b1052008-04-29 01:00:11 -0700239 * List running through all cgroup groups in the same hash
240 * slot. Protected by css_set_lock
241 */
242 struct hlist_node hlist;
243
244 /*
Paul Menage817929e2007-10-18 23:39:36 -0700245 * List running through all tasks using this cgroup
246 * group. Protected by css_set_lock
247 */
248 struct list_head tasks;
249
250 /*
251 * List of cg_cgroup_link objects on link chains from
252 * cgroups referenced from this css_set. Protected by
253 * css_set_lock
254 */
255 struct list_head cg_links;
256
257 /*
258 * Set of subsystem states, one for each subsystem. This array
259 * is immutable after creation apart from the init_css_set
Ben Blumcf5d5942010-03-10 15:22:09 -0800260 * during subsystem registration (at boot time) and modular subsystem
261 * loading/unloading.
Paul Menage817929e2007-10-18 23:39:36 -0700262 */
263 struct cgroup_subsys_state *subsys[CGROUP_SUBSYS_COUNT];
Ben Blumc3783692009-09-23 15:56:29 -0700264
265 /* For RCU-protected deletion */
266 struct rcu_head rcu_head;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700267};
268
Paul Menage91796562008-04-29 01:00:01 -0700269/*
270 * cgroup_map_cb is an abstract callback API for reporting map-valued
271 * control files
272 */
273
274struct cgroup_map_cb {
275 int (*fill)(struct cgroup_map_cb *cb, const char *key, u64 value);
276 void *state;
277};
278
Paul Menaged20a3902009-04-02 16:57:22 -0700279/*
280 * struct cftype: handler definitions for cgroup control files
Paul Menageddbcc7e2007-10-18 23:39:30 -0700281 *
282 * When reading/writing to a file:
Li Zefana043e3b2008-02-23 15:24:09 -0800283 * - the cgroup to use is file->f_dentry->d_parent->d_fsdata
Paul Menageddbcc7e2007-10-18 23:39:30 -0700284 * - the 'cftype' of the file is file->f_dentry->d_fsdata
285 */
286
Tejun Heo8e3f6542012-04-01 12:09:55 -0700287/* cftype->flags */
288#define CFTYPE_ONLY_ON_ROOT (1U << 0) /* only create on root cg */
289#define CFTYPE_NOT_ON_ROOT (1U << 1) /* don't create onp root cg */
290
291#define MAX_CFTYPE_NAME 64
292
Paul Menageddbcc7e2007-10-18 23:39:30 -0700293struct cftype {
Paul Menaged20a3902009-04-02 16:57:22 -0700294 /*
295 * By convention, the name should begin with the name of the
Tejun Heo8e3f6542012-04-01 12:09:55 -0700296 * subsystem, followed by a period. Zero length string indicates
297 * end of cftype array.
Paul Menaged20a3902009-04-02 16:57:22 -0700298 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700299 char name[MAX_CFTYPE_NAME];
300 int private;
Li Zefan099fca32009-04-02 16:57:29 -0700301 /*
302 * If not 0, file mode is set to this value, otherwise it will
303 * be figured out automatically
304 */
Al Viroa5e7ed32011-07-26 01:55:55 -0400305 umode_t mode;
Paul Menagedb3b1492008-07-25 01:46:58 -0700306
307 /*
308 * If non-zero, defines the maximum length of string that can
309 * be passed to write_string; defaults to 64
310 */
311 size_t max_write_len;
312
Tejun Heo8e3f6542012-04-01 12:09:55 -0700313 /* CFTYPE_* flags */
314 unsigned int flags;
315
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -0400316 /* file xattrs */
317 struct simple_xattrs xattrs;
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);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800378
379 /*
380 * register_event() callback will be used to add new userspace
381 * waiter for changes related to the cftype. Implement it if
382 * you want to provide this functionality. Use eventfd_signal()
383 * on eventfd to send notification to userspace.
384 */
385 int (*register_event)(struct cgroup *cgrp, struct cftype *cft,
386 struct eventfd_ctx *eventfd, const char *args);
387 /*
388 * unregister_event() callback will be called when userspace
389 * closes the eventfd or on cgroup removing.
390 * This callback must be implemented, if you want provide
391 * notification functionality.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800392 */
Kirill A. Shutemov907860e2010-05-26 14:42:46 -0700393 void (*unregister_event)(struct cgroup *cgrp, struct cftype *cft,
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800394 struct eventfd_ctx *eventfd);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700395};
396
Tejun Heo8e3f6542012-04-01 12:09:55 -0700397/*
398 * cftype_sets describe cftypes belonging to a subsystem and are chained at
399 * cgroup_subsys->cftsets. Each cftset points to an array of cftypes
400 * terminated by zero length name.
401 */
402struct cftype_set {
403 struct list_head node; /* chained at subsys->cftsets */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -0400404 struct cftype *cfts;
Tejun Heo8e3f6542012-04-01 12:09:55 -0700405};
406
Cliff Wickman31a7df02008-02-07 00:14:42 -0800407struct cgroup_scanner {
408 struct cgroup *cg;
409 int (*test_task)(struct task_struct *p, struct cgroup_scanner *scan);
410 void (*process_task)(struct task_struct *p,
411 struct cgroup_scanner *scan);
412 struct ptr_heap *heap;
Li Zefanbd1a8ab2009-04-02 16:57:50 -0700413 void *data;
Cliff Wickman31a7df02008-02-07 00:14:42 -0800414};
415
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -0400416int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts);
417int cgroup_rm_cftypes(struct cgroup_subsys *ss, struct cftype *cfts);
Tejun Heo8e3f6542012-04-01 12:09:55 -0700418
Li Zefanffd2d882008-02-23 15:24:09 -0800419int cgroup_is_removed(const struct cgroup *cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700420
Li Zefanffd2d882008-02-23 15:24:09 -0800421int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700422
Li Zefanffd2d882008-02-23 15:24:09 -0800423int cgroup_task_count(const struct cgroup *cgrp);
Paul Menagebbcb81d2007-10-18 23:39:32 -0700424
Grzegorz Nosek313e9242009-04-02 16:57:23 -0700425/* Return true if cgrp is a descendant of the task's cgroup */
426int cgroup_is_descendant(const struct cgroup *cgrp, struct task_struct *task);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700427
Thadeu Lima de Souza Cascardo21acb9c2009-02-04 10:12:08 +0100428/*
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -0700429 * When the subsys has to access css and may add permanent refcnt to css,
430 * it should take care of racy conditions with rmdir(). Following set of
431 * functions, is for stop/restart rmdir if necessary.
432 * Because these will call css_get/put, "css" should be alive css.
433 *
434 * cgroup_exclude_rmdir();
435 * ...do some jobs which may access arbitrary empty cgroup
436 * cgroup_release_and_wakeup_rmdir();
437 *
438 * When someone removes a cgroup while cgroup_exclude_rmdir() holds it,
439 * it sleeps and cgroup_release_and_wakeup_rmdir() will wake him up.
440 */
441
442void cgroup_exclude_rmdir(struct cgroup_subsys_state *css);
443void cgroup_release_and_wakeup_rmdir(struct cgroup_subsys_state *css);
444
445/*
Tejun Heo2f7ee562011-12-12 18:12:21 -0800446 * Control Group taskset, used to pass around set of tasks to cgroup_subsys
447 * methods.
448 */
449struct cgroup_taskset;
450struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset);
451struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset);
452struct cgroup *cgroup_taskset_cur_cgroup(struct cgroup_taskset *tset);
453int cgroup_taskset_size(struct cgroup_taskset *tset);
454
455/**
456 * cgroup_taskset_for_each - iterate cgroup_taskset
457 * @task: the loop cursor
458 * @skip_cgrp: skip if task's cgroup matches this, %NULL to iterate through all
459 * @tset: taskset to iterate
460 */
461#define cgroup_taskset_for_each(task, skip_cgrp, tset) \
462 for ((task) = cgroup_taskset_first((tset)); (task); \
463 (task) = cgroup_taskset_next((tset))) \
464 if (!(skip_cgrp) || \
465 cgroup_taskset_cur_cgroup((tset)) != (skip_cgrp))
466
467/*
Thadeu Lima de Souza Cascardo21acb9c2009-02-04 10:12:08 +0100468 * Control Group subsystem type.
469 * See Documentation/cgroups/cgroups.txt for details
470 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700471
472struct cgroup_subsys {
Li Zefan761b3ef2012-01-31 13:47:36 +0800473 struct cgroup_subsys_state *(*create)(struct cgroup *cgrp);
474 int (*pre_destroy)(struct cgroup *cgrp);
475 void (*destroy)(struct cgroup *cgrp);
476 int (*can_attach)(struct cgroup *cgrp, struct cgroup_taskset *tset);
477 void (*cancel_attach)(struct cgroup *cgrp, struct cgroup_taskset *tset);
478 void (*attach)(struct cgroup *cgrp, struct cgroup_taskset *tset);
479 void (*fork)(struct task_struct *task);
480 void (*exit)(struct cgroup *cgrp, struct cgroup *old_cgrp,
481 struct task_struct *task);
Li Zefan761b3ef2012-01-31 13:47:36 +0800482 void (*post_clone)(struct cgroup *cgrp);
483 void (*bind)(struct cgroup *root);
Hugh Dickinse5991372009-01-06 14:39:22 -0800484
Paul Menageddbcc7e2007-10-18 23:39:30 -0700485 int subsys_id;
486 int active;
Paul Menage8bab8dd2008-04-04 14:29:57 -0700487 int disabled;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700488 int early_init;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700489 /*
490 * True if this subsys uses ID. ID is not available before cgroup_init()
491 * (not available in early_init time.)
492 */
493 bool use_id;
Tejun Heo48ddbe12012-04-01 12:09:56 -0700494
495 /*
496 * If %true, cgroup removal will try to clear css refs by retrying
497 * ss->pre_destroy() until there's no css ref left. This behavior
498 * is strictly for backward compatibility and will be removed as
499 * soon as the current user (memcg) is updated.
500 *
501 * If %false, ss->pre_destroy() can't fail and cgroup removal won't
502 * wait for css refs to drop to zero before proceeding.
503 */
504 bool __DEPRECATED_clear_css_refs;
505
Paul Menageddbcc7e2007-10-18 23:39:30 -0700506#define MAX_CGROUP_TYPE_NAMELEN 32
507 const char *name;
508
Paul Menage999cd8a2009-01-07 18:08:36 -0800509 /*
Paul Menage999cd8a2009-01-07 18:08:36 -0800510 * Link to parent, and list entry in parent's children.
Li Zefan6be96a52012-06-06 19:12:30 -0700511 * Protected by cgroup_lock()
Paul Menage999cd8a2009-01-07 18:08:36 -0800512 */
513 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700514 struct list_head sibling;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700515 /* used when use_id == true */
516 struct idr idr;
Hugh Dickins42aee6c2012-03-21 16:34:21 -0700517 spinlock_t id_lock;
Ben Blume6a11052010-03-10 15:22:09 -0800518
Tejun Heo8e3f6542012-04-01 12:09:55 -0700519 /* list of cftype_sets */
520 struct list_head cftsets;
521
522 /* base cftypes, automatically [de]registered with subsys itself */
523 struct cftype *base_cftypes;
524 struct cftype_set base_cftset;
525
Ben Blume6a11052010-03-10 15:22:09 -0800526 /* should be defined only by modular subsystems */
527 struct module *module;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700528};
529
530#define SUBSYS(_x) extern struct cgroup_subsys _x ## _subsys;
531#include <linux/cgroup_subsys.h>
532#undef SUBSYS
533
534static inline struct cgroup_subsys_state *cgroup_subsys_state(
Li Zefanffd2d882008-02-23 15:24:09 -0800535 struct cgroup *cgrp, int subsys_id)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700536{
Li Zefanffd2d882008-02-23 15:24:09 -0800537 return cgrp->subsys[subsys_id];
Paul Menageddbcc7e2007-10-18 23:39:30 -0700538}
539
Peter Zijlstradc61b1d2010-06-08 11:40:42 +0200540/*
541 * function to get the cgroup_subsys_state which allows for extra
542 * rcu_dereference_check() conditions, such as locks used during the
543 * cgroup_subsys::attach() methods.
544 */
545#define task_subsys_state_check(task, subsys_id, __c) \
546 rcu_dereference_check(task->cgroups->subsys[subsys_id], \
Peter Zijlstradc61b1d2010-06-08 11:40:42 +0200547 lockdep_is_held(&task->alloc_lock) || \
548 cgroup_lock_is_held() || (__c))
549
550static inline struct cgroup_subsys_state *
551task_subsys_state(struct task_struct *task, int subsys_id)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700552{
Peter Zijlstradc61b1d2010-06-08 11:40:42 +0200553 return task_subsys_state_check(task, subsys_id, false);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700554}
555
556static inline struct cgroup* task_cgroup(struct task_struct *task,
557 int subsys_id)
558{
559 return task_subsys_state(task, subsys_id)->cgroup;
560}
561
Paul Menage817929e2007-10-18 23:39:36 -0700562/* A cgroup_iter should be treated as an opaque object */
563struct cgroup_iter {
564 struct list_head *cg_link;
565 struct list_head *task;
566};
567
Paul Menaged20a3902009-04-02 16:57:22 -0700568/*
569 * To iterate across the tasks in a cgroup:
Paul Menage817929e2007-10-18 23:39:36 -0700570 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400571 * 1) call cgroup_iter_start to initialize an iterator
Paul Menage817929e2007-10-18 23:39:36 -0700572 *
573 * 2) call cgroup_iter_next() to retrieve member tasks until it
574 * returns NULL or until you want to end the iteration
575 *
576 * 3) call cgroup_iter_end() to destroy the iterator.
Cliff Wickman31a7df02008-02-07 00:14:42 -0800577 *
Paul Menaged20a3902009-04-02 16:57:22 -0700578 * Or, call cgroup_scan_tasks() to iterate through every task in a
579 * cgroup - cgroup_scan_tasks() holds the css_set_lock when calling
580 * the test_task() callback, but not while calling the process_task()
581 * callback.
Paul Menage817929e2007-10-18 23:39:36 -0700582 */
Li Zefanffd2d882008-02-23 15:24:09 -0800583void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it);
584struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -0700585 struct cgroup_iter *it);
Li Zefanffd2d882008-02-23 15:24:09 -0800586void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it);
Cliff Wickman31a7df02008-02-07 00:14:42 -0800587int cgroup_scan_tasks(struct cgroup_scanner *scan);
Cliff Wickman956db3c2008-02-07 00:14:43 -0800588int cgroup_attach_task(struct cgroup *, struct task_struct *);
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -0700589int cgroup_attach_task_all(struct task_struct *from, struct task_struct *);
590
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700591/*
592 * CSS ID is ID for cgroup_subsys_state structs under subsys. This only works
593 * if cgroup_subsys.use_id == true. It can be used for looking up and scanning.
594 * CSS ID is assigned at cgroup allocation (create) automatically
595 * and removed when subsys calls free_css_id() function. This is because
596 * the lifetime of cgroup_subsys_state is subsys's matter.
597 *
598 * Looking up and scanning function should be called under rcu_read_lock().
Li Zefan6be96a52012-06-06 19:12:30 -0700599 * Taking cgroup_mutex is not necessary for following calls.
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700600 * But the css returned by this routine can be "not populated yet" or "being
601 * destroyed". The caller should check css and cgroup's status.
602 */
603
604/*
605 * Typically Called at ->destroy(), or somewhere the subsys frees
606 * cgroup_subsys_state.
607 */
608void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css);
609
610/* Find a cgroup_subsys_state which has given ID */
611
612struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id);
613
614/*
615 * Get a cgroup whose id is greater than or equal to id under tree of root.
616 * Returning a cgroup_subsys_state or NULL.
617 */
618struct cgroup_subsys_state *css_get_next(struct cgroup_subsys *ss, int id,
619 struct cgroup_subsys_state *root, int *foundid);
620
621/* Returns true if root is ancestor of cg */
622bool css_is_ancestor(struct cgroup_subsys_state *cg,
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700623 const struct cgroup_subsys_state *root);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700624
625/* Get id and depth of css */
626unsigned short css_id(struct cgroup_subsys_state *css);
627unsigned short css_depth(struct cgroup_subsys_state *css);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200628struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700629
Paul Menageddbcc7e2007-10-18 23:39:30 -0700630#else /* !CONFIG_CGROUPS */
631
632static inline int cgroup_init_early(void) { return 0; }
633static inline int cgroup_init(void) { return 0; }
Paul Menageb4f48b62007-10-18 23:39:33 -0700634static inline void cgroup_fork(struct task_struct *p) {}
635static inline void cgroup_fork_callbacks(struct task_struct *p) {}
Paul Menage817929e2007-10-18 23:39:36 -0700636static inline void cgroup_post_fork(struct task_struct *p) {}
Paul Menageb4f48b62007-10-18 23:39:33 -0700637static inline void cgroup_exit(struct task_struct *p, int callbacks) {}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700638
639static inline void cgroup_lock(void) {}
640static inline void cgroup_unlock(void) {}
Balbir Singh846c7bb2007-10-18 23:39:44 -0700641static inline int cgroupstats_build(struct cgroupstats *stats,
642 struct dentry *dentry)
643{
644 return -EINVAL;
645}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700646
Sridhar Samudralad7926ee2010-05-30 22:24:39 +0200647/* No cgroups - nothing to do */
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -0700648static inline int cgroup_attach_task_all(struct task_struct *from,
649 struct task_struct *t)
650{
651 return 0;
652}
Sridhar Samudralad7926ee2010-05-30 22:24:39 +0200653
Paul Menageddbcc7e2007-10-18 23:39:30 -0700654#endif /* !CONFIG_CGROUPS */
655
Paul Menageddbcc7e2007-10-18 23:39:30 -0700656#endif /* _LINUX_CGROUP_H */