blob: fc5e4a48582f6473c4d4f3cbc0182d3e56c0e9df [file] [log] [blame]
Paul Menageddbcc7e2007-10-18 23:39:30 -07001/*
Paul Menageddbcc7e2007-10-18 23:39:30 -07002 * Generic process-grouping system.
3 *
4 * Based originally on the cpuset system, extracted by Paul Menage
5 * Copyright (C) 2006 Google, Inc
6 *
7 * Copyright notices from the original cpuset code:
8 * --------------------------------------------------
9 * Copyright (C) 2003 BULL SA.
10 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
11 *
12 * Portions derived from Patrick Mochel's sysfs code.
13 * sysfs is Copyright (c) 2001-3 Patrick Mochel
14 *
15 * 2003-10-10 Written by Simon Derr.
16 * 2003-10-22 Updates by Stephen Hemminger.
17 * 2004 May-July Rework by Paul Jackson.
18 * ---------------------------------------------------
19 *
20 * This file is subject to the terms and conditions of the GNU General Public
21 * License. See the file COPYING in the main directory of the Linux
22 * distribution for more details.
23 */
24
25#include <linux/cgroup.h>
26#include <linux/errno.h>
27#include <linux/fs.h>
28#include <linux/kernel.h>
29#include <linux/list.h>
30#include <linux/mm.h>
31#include <linux/mutex.h>
32#include <linux/mount.h>
33#include <linux/pagemap.h>
Paul Menagea4243162007-10-18 23:39:35 -070034#include <linux/proc_fs.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070035#include <linux/rcupdate.h>
36#include <linux/sched.h>
Paul Menage817929e2007-10-18 23:39:36 -070037#include <linux/backing-dev.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070038#include <linux/seq_file.h>
39#include <linux/slab.h>
40#include <linux/magic.h>
41#include <linux/spinlock.h>
42#include <linux/string.h>
Paul Menagebbcb81d2007-10-18 23:39:32 -070043#include <linux/sort.h>
Paul Menage81a6a5c2007-10-18 23:39:38 -070044#include <linux/kmod.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070045#include <linux/delayacct.h>
46#include <linux/cgroupstats.h>
Li Zefan472b1052008-04-29 01:00:11 -070047#include <linux/hash.h>
Al Viro3f8206d2008-07-26 03:46:43 -040048#include <linux/namei.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070049
Paul Menageddbcc7e2007-10-18 23:39:30 -070050#include <asm/atomic.h>
51
Paul Menage81a6a5c2007-10-18 23:39:38 -070052static DEFINE_MUTEX(cgroup_mutex);
53
Paul Menageddbcc7e2007-10-18 23:39:30 -070054/* Generate an array of cgroup subsystem pointers */
55#define SUBSYS(_x) &_x ## _subsys,
56
57static struct cgroup_subsys *subsys[] = {
58#include <linux/cgroup_subsys.h>
59};
60
61/*
62 * A cgroupfs_root represents the root of a cgroup hierarchy,
63 * and may be associated with a superblock to form an active
64 * hierarchy
65 */
66struct cgroupfs_root {
67 struct super_block *sb;
68
69 /*
70 * The bitmask of subsystems intended to be attached to this
71 * hierarchy
72 */
73 unsigned long subsys_bits;
74
75 /* The bitmask of subsystems currently attached to this hierarchy */
76 unsigned long actual_subsys_bits;
77
78 /* A list running through the attached subsystems */
79 struct list_head subsys_list;
80
81 /* The root cgroup for this hierarchy */
82 struct cgroup top_cgroup;
83
84 /* Tracks how many cgroups are currently defined in hierarchy.*/
85 int number_of_cgroups;
86
Li Zefane5f6a862009-01-07 18:07:41 -080087 /* A list running through the active hierarchies */
Paul Menageddbcc7e2007-10-18 23:39:30 -070088 struct list_head root_list;
89
90 /* Hierarchy-specific flags */
91 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -070092
Paul Menagee788e062008-07-25 01:46:59 -070093 /* The path to use for release notifications. */
Paul Menage81a6a5c2007-10-18 23:39:38 -070094 char release_agent_path[PATH_MAX];
Paul Menageddbcc7e2007-10-18 23:39:30 -070095};
96
Paul Menageddbcc7e2007-10-18 23:39:30 -070097/*
98 * The "rootnode" hierarchy is the "dummy hierarchy", reserved for the
99 * subsystems that are otherwise unattached - it never has more than a
100 * single cgroup, and all tasks are part of that cgroup.
101 */
102static struct cgroupfs_root rootnode;
103
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700104/*
105 * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when
106 * cgroup_subsys->use_id != 0.
107 */
108#define CSS_ID_MAX (65535)
109struct css_id {
110 /*
111 * The css to which this ID points. This pointer is set to valid value
112 * after cgroup is populated. If cgroup is removed, this will be NULL.
113 * This pointer is expected to be RCU-safe because destroy()
114 * is called after synchronize_rcu(). But for safe use, css_is_removed()
115 * css_tryget() should be used for avoiding race.
116 */
117 struct cgroup_subsys_state *css;
118 /*
119 * ID of this css.
120 */
121 unsigned short id;
122 /*
123 * Depth in hierarchy which this ID belongs to.
124 */
125 unsigned short depth;
126 /*
127 * ID is freed by RCU. (and lookup routine is RCU safe.)
128 */
129 struct rcu_head rcu_head;
130 /*
131 * Hierarchy of CSS ID belongs to.
132 */
133 unsigned short stack[0]; /* Array of Length (depth+1) */
134};
135
136
Paul Menageddbcc7e2007-10-18 23:39:30 -0700137/* The list of hierarchy roots */
138
139static LIST_HEAD(roots);
Paul Menage817929e2007-10-18 23:39:36 -0700140static int root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700141
142/* dummytop is a shorthand for the dummy hierarchy's top cgroup */
143#define dummytop (&rootnode.top_cgroup)
144
145/* This flag indicates whether tasks in the fork and exit paths should
Li Zefana043e3b2008-02-23 15:24:09 -0800146 * check for fork/exit handlers to call. This avoids us having to do
147 * extra work in the fork/exit path if none of the subsystems need to
148 * be called.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700149 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700150static int need_forkexit_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700151
Paul Menageddbcc7e2007-10-18 23:39:30 -0700152/* convenient tests for these bits */
Paul Menagebd89aab2007-10-18 23:40:44 -0700153inline int cgroup_is_removed(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700154{
Paul Menagebd89aab2007-10-18 23:40:44 -0700155 return test_bit(CGRP_REMOVED, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700156}
157
158/* bits in struct cgroupfs_root flags field */
159enum {
160 ROOT_NOPREFIX, /* mounted subsystems have no named prefix */
161};
162
Adrian Bunke9685a02008-02-07 00:13:46 -0800163static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700164{
165 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700166 (1 << CGRP_RELEASABLE) |
167 (1 << CGRP_NOTIFY_ON_RELEASE);
168 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700169}
170
Adrian Bunke9685a02008-02-07 00:13:46 -0800171static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700172{
Paul Menagebd89aab2007-10-18 23:40:44 -0700173 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700174}
175
Paul Menageddbcc7e2007-10-18 23:39:30 -0700176/*
177 * for_each_subsys() allows you to iterate on each subsystem attached to
178 * an active hierarchy
179 */
180#define for_each_subsys(_root, _ss) \
181list_for_each_entry(_ss, &_root->subsys_list, sibling)
182
Li Zefane5f6a862009-01-07 18:07:41 -0800183/* for_each_active_root() allows you to iterate across the active hierarchies */
184#define for_each_active_root(_root) \
Paul Menageddbcc7e2007-10-18 23:39:30 -0700185list_for_each_entry(_root, &roots, root_list)
186
Paul Menage81a6a5c2007-10-18 23:39:38 -0700187/* the list of cgroups eligible for automatic release. Protected by
188 * release_list_lock */
189static LIST_HEAD(release_list);
190static DEFINE_SPINLOCK(release_list_lock);
191static void cgroup_release_agent(struct work_struct *work);
192static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700193static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700194
Paul Menage817929e2007-10-18 23:39:36 -0700195/* Link structure for associating css_set objects with cgroups */
196struct cg_cgroup_link {
197 /*
198 * List running through cg_cgroup_links associated with a
199 * cgroup, anchored on cgroup->css_sets
200 */
Paul Menagebd89aab2007-10-18 23:40:44 -0700201 struct list_head cgrp_link_list;
Paul Menage817929e2007-10-18 23:39:36 -0700202 /*
203 * List running through cg_cgroup_links pointing at a
204 * single css_set object, anchored on css_set->cg_links
205 */
206 struct list_head cg_link_list;
207 struct css_set *cg;
208};
209
210/* The default css_set - used by init and its children prior to any
211 * hierarchies being mounted. It contains a pointer to the root state
212 * for each subsystem. Also used to anchor the list of css_sets. Not
213 * reference-counted, to improve performance when child cgroups
214 * haven't been created.
215 */
216
217static struct css_set init_css_set;
218static struct cg_cgroup_link init_css_set_link;
219
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700220static int cgroup_subsys_init_idr(struct cgroup_subsys *ss);
221
Paul Menage817929e2007-10-18 23:39:36 -0700222/* css_set_lock protects the list of css_set objects, and the
223 * chain of tasks off each css_set. Nests outside task->alloc_lock
224 * due to cgroup_iter_start() */
225static DEFINE_RWLOCK(css_set_lock);
226static int css_set_count;
227
Li Zefan472b1052008-04-29 01:00:11 -0700228/* hash table for cgroup groups. This improves the performance to
229 * find an existing css_set */
230#define CSS_SET_HASH_BITS 7
231#define CSS_SET_TABLE_SIZE (1 << CSS_SET_HASH_BITS)
232static struct hlist_head css_set_table[CSS_SET_TABLE_SIZE];
233
234static struct hlist_head *css_set_hash(struct cgroup_subsys_state *css[])
235{
236 int i;
237 int index;
238 unsigned long tmp = 0UL;
239
240 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++)
241 tmp += (unsigned long)css[i];
242 tmp = (tmp >> 16) ^ tmp;
243
244 index = hash_long(tmp, CSS_SET_HASH_BITS);
245
246 return &css_set_table[index];
247}
248
Paul Menage817929e2007-10-18 23:39:36 -0700249/* We don't maintain the lists running through each css_set to its
250 * task until after the first call to cgroup_iter_start(). This
251 * reduces the fork()/exit() overhead for people who have cgroups
252 * compiled into their kernel but not actually in use */
Li Zefan8947f9d2008-07-25 01:46:56 -0700253static int use_task_css_set_links __read_mostly;
Paul Menage817929e2007-10-18 23:39:36 -0700254
255/* When we create or destroy a css_set, the operation simply
256 * takes/releases a reference count on all the cgroups referenced
257 * by subsystems in this css_set. This can end up multiple-counting
258 * some cgroups, but that's OK - the ref-count is just a
259 * busy/not-busy indicator; ensuring that we only count each cgroup
260 * once would require taking a global lock to ensure that no
Paul Menageb4f48b62007-10-18 23:39:33 -0700261 * subsystems moved between hierarchies while we were doing so.
262 *
263 * Possible TODO: decide at boot time based on the number of
264 * registered subsystems and the number of CPUs or NUMA nodes whether
265 * it's better for performance to ref-count every subsystem, or to
266 * take a global lock and only add one ref count to each hierarchy.
267 */
Paul Menageb4f48b62007-10-18 23:39:33 -0700268
Paul Menage817929e2007-10-18 23:39:36 -0700269/*
270 * unlink a css_set from the list and free it
271 */
Paul Menage81a6a5c2007-10-18 23:39:38 -0700272static void unlink_css_set(struct css_set *cg)
Paul Menageb4f48b62007-10-18 23:39:33 -0700273{
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700274 struct cg_cgroup_link *link;
275 struct cg_cgroup_link *saved_link;
276
Li Zefan472b1052008-04-29 01:00:11 -0700277 hlist_del(&cg->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700278 css_set_count--;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700279
280 list_for_each_entry_safe(link, saved_link, &cg->cg_links,
281 cg_link_list) {
Paul Menage817929e2007-10-18 23:39:36 -0700282 list_del(&link->cg_link_list);
Paul Menagebd89aab2007-10-18 23:40:44 -0700283 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -0700284 kfree(link);
285 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700286}
287
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700288static void __put_css_set(struct css_set *cg, int taskexit)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700289{
290 int i;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700291 /*
292 * Ensure that the refcount doesn't hit zero while any readers
293 * can see it. Similar to atomic_dec_and_lock(), but for an
294 * rwlock
295 */
296 if (atomic_add_unless(&cg->refcount, -1, 1))
297 return;
298 write_lock(&css_set_lock);
299 if (!atomic_dec_and_test(&cg->refcount)) {
300 write_unlock(&css_set_lock);
301 return;
302 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700303 unlink_css_set(cg);
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700304 write_unlock(&css_set_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700305
306 rcu_read_lock();
307 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menagea47295e2009-01-07 18:07:44 -0800308 struct cgroup *cgrp = rcu_dereference(cg->subsys[i]->cgroup);
Paul Menagebd89aab2007-10-18 23:40:44 -0700309 if (atomic_dec_and_test(&cgrp->count) &&
310 notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700311 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700312 set_bit(CGRP_RELEASABLE, &cgrp->flags);
313 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700314 }
315 }
316 rcu_read_unlock();
Paul Menage817929e2007-10-18 23:39:36 -0700317 kfree(cg);
318}
319
320/*
321 * refcounted get/put for css_set objects
322 */
323static inline void get_css_set(struct css_set *cg)
324{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700325 atomic_inc(&cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700326}
327
328static inline void put_css_set(struct css_set *cg)
329{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700330 __put_css_set(cg, 0);
Paul Menage817929e2007-10-18 23:39:36 -0700331}
332
Paul Menage81a6a5c2007-10-18 23:39:38 -0700333static inline void put_css_set_taskexit(struct css_set *cg)
334{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700335 __put_css_set(cg, 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700336}
337
Paul Menage817929e2007-10-18 23:39:36 -0700338/*
339 * find_existing_css_set() is a helper for
340 * find_css_set(), and checks to see whether an existing
Li Zefan472b1052008-04-29 01:00:11 -0700341 * css_set is suitable.
Paul Menage817929e2007-10-18 23:39:36 -0700342 *
343 * oldcg: the cgroup group that we're using before the cgroup
344 * transition
345 *
Paul Menagebd89aab2007-10-18 23:40:44 -0700346 * cgrp: the cgroup that we're moving into
Paul Menage817929e2007-10-18 23:39:36 -0700347 *
348 * template: location in which to build the desired set of subsystem
349 * state objects for the new cgroup group
350 */
Paul Menage817929e2007-10-18 23:39:36 -0700351static struct css_set *find_existing_css_set(
352 struct css_set *oldcg,
Paul Menagebd89aab2007-10-18 23:40:44 -0700353 struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -0700354 struct cgroup_subsys_state *template[])
355{
356 int i;
Paul Menagebd89aab2007-10-18 23:40:44 -0700357 struct cgroupfs_root *root = cgrp->root;
Li Zefan472b1052008-04-29 01:00:11 -0700358 struct hlist_head *hhead;
359 struct hlist_node *node;
360 struct css_set *cg;
Paul Menage817929e2007-10-18 23:39:36 -0700361
362 /* Built the set of subsystem state objects that we want to
363 * see in the new css_set */
364 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Li Zefan8d53d552008-02-23 15:24:11 -0800365 if (root->subsys_bits & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700366 /* Subsystem is in this hierarchy. So we want
367 * the subsystem state from the new
368 * cgroup */
Paul Menagebd89aab2007-10-18 23:40:44 -0700369 template[i] = cgrp->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700370 } else {
371 /* Subsystem is not in this hierarchy, so we
372 * don't want to change the subsystem state */
373 template[i] = oldcg->subsys[i];
374 }
375 }
376
Li Zefan472b1052008-04-29 01:00:11 -0700377 hhead = css_set_hash(template);
378 hlist_for_each_entry(cg, node, hhead, hlist) {
Paul Menage817929e2007-10-18 23:39:36 -0700379 if (!memcmp(template, cg->subsys, sizeof(cg->subsys))) {
380 /* All subsystems matched */
381 return cg;
382 }
Li Zefan472b1052008-04-29 01:00:11 -0700383 }
Paul Menage817929e2007-10-18 23:39:36 -0700384
385 /* No existing cgroup group matched */
386 return NULL;
387}
388
Paul Menage817929e2007-10-18 23:39:36 -0700389static void free_cg_links(struct list_head *tmp)
390{
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700391 struct cg_cgroup_link *link;
392 struct cg_cgroup_link *saved_link;
393
394 list_for_each_entry_safe(link, saved_link, tmp, cgrp_link_list) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700395 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -0700396 kfree(link);
397 }
398}
399
400/*
Li Zefan36553432008-07-29 22:33:19 -0700401 * allocate_cg_links() allocates "count" cg_cgroup_link structures
402 * and chains them on tmp through their cgrp_link_list fields. Returns 0 on
403 * success or a negative error
404 */
405static int allocate_cg_links(int count, struct list_head *tmp)
406{
407 struct cg_cgroup_link *link;
408 int i;
409 INIT_LIST_HEAD(tmp);
410 for (i = 0; i < count; i++) {
411 link = kmalloc(sizeof(*link), GFP_KERNEL);
412 if (!link) {
413 free_cg_links(tmp);
414 return -ENOMEM;
415 }
416 list_add(&link->cgrp_link_list, tmp);
417 }
418 return 0;
419}
420
Li Zefanc12f65d2009-01-07 18:07:42 -0800421/**
422 * link_css_set - a helper function to link a css_set to a cgroup
423 * @tmp_cg_links: cg_cgroup_link objects allocated by allocate_cg_links()
424 * @cg: the css_set to be linked
425 * @cgrp: the destination cgroup
426 */
427static void link_css_set(struct list_head *tmp_cg_links,
428 struct css_set *cg, struct cgroup *cgrp)
429{
430 struct cg_cgroup_link *link;
431
432 BUG_ON(list_empty(tmp_cg_links));
433 link = list_first_entry(tmp_cg_links, struct cg_cgroup_link,
434 cgrp_link_list);
435 link->cg = cg;
436 list_move(&link->cgrp_link_list, &cgrp->css_sets);
437 list_add(&link->cg_link_list, &cg->cg_links);
438}
439
Li Zefan36553432008-07-29 22:33:19 -0700440/*
Paul Menage817929e2007-10-18 23:39:36 -0700441 * find_css_set() takes an existing cgroup group and a
442 * cgroup object, and returns a css_set object that's
443 * equivalent to the old group, but with the given cgroup
444 * substituted into the appropriate hierarchy. Must be called with
445 * cgroup_mutex held
446 */
Paul Menage817929e2007-10-18 23:39:36 -0700447static struct css_set *find_css_set(
Paul Menagebd89aab2007-10-18 23:40:44 -0700448 struct css_set *oldcg, struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700449{
450 struct css_set *res;
451 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT];
452 int i;
453
454 struct list_head tmp_cg_links;
Paul Menage817929e2007-10-18 23:39:36 -0700455
Li Zefan472b1052008-04-29 01:00:11 -0700456 struct hlist_head *hhead;
457
Paul Menage817929e2007-10-18 23:39:36 -0700458 /* First see if we already have a cgroup group that matches
459 * the desired set */
Li Zefan7e9abd82008-07-25 01:46:54 -0700460 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -0700461 res = find_existing_css_set(oldcg, cgrp, template);
Paul Menage817929e2007-10-18 23:39:36 -0700462 if (res)
463 get_css_set(res);
Li Zefan7e9abd82008-07-25 01:46:54 -0700464 read_unlock(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -0700465
466 if (res)
467 return res;
468
469 res = kmalloc(sizeof(*res), GFP_KERNEL);
470 if (!res)
471 return NULL;
472
473 /* Allocate all the cg_cgroup_link objects that we'll need */
474 if (allocate_cg_links(root_count, &tmp_cg_links) < 0) {
475 kfree(res);
476 return NULL;
477 }
478
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700479 atomic_set(&res->refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -0700480 INIT_LIST_HEAD(&res->cg_links);
481 INIT_LIST_HEAD(&res->tasks);
Li Zefan472b1052008-04-29 01:00:11 -0700482 INIT_HLIST_NODE(&res->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700483
484 /* Copy the set of subsystem state objects generated in
485 * find_existing_css_set() */
486 memcpy(res->subsys, template, sizeof(res->subsys));
487
488 write_lock(&css_set_lock);
489 /* Add reference counts and links from the new css_set. */
490 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700491 struct cgroup *cgrp = res->subsys[i]->cgroup;
Paul Menage817929e2007-10-18 23:39:36 -0700492 struct cgroup_subsys *ss = subsys[i];
Paul Menagebd89aab2007-10-18 23:40:44 -0700493 atomic_inc(&cgrp->count);
Paul Menage817929e2007-10-18 23:39:36 -0700494 /*
495 * We want to add a link once per cgroup, so we
496 * only do it for the first subsystem in each
497 * hierarchy
498 */
Li Zefanc12f65d2009-01-07 18:07:42 -0800499 if (ss->root->subsys_list.next == &ss->sibling)
500 link_css_set(&tmp_cg_links, res, cgrp);
Paul Menage817929e2007-10-18 23:39:36 -0700501 }
Li Zefanc12f65d2009-01-07 18:07:42 -0800502 if (list_empty(&rootnode.subsys_list))
503 link_css_set(&tmp_cg_links, res, dummytop);
Paul Menage817929e2007-10-18 23:39:36 -0700504
505 BUG_ON(!list_empty(&tmp_cg_links));
506
Paul Menage817929e2007-10-18 23:39:36 -0700507 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700508
509 /* Add this cgroup group to the hash table */
510 hhead = css_set_hash(res->subsys);
511 hlist_add_head(&res->hlist, hhead);
512
Paul Menage817929e2007-10-18 23:39:36 -0700513 write_unlock(&css_set_lock);
514
515 return res;
Paul Menageb4f48b62007-10-18 23:39:33 -0700516}
517
Paul Menageddbcc7e2007-10-18 23:39:30 -0700518/*
519 * There is one global cgroup mutex. We also require taking
520 * task_lock() when dereferencing a task's cgroup subsys pointers.
521 * See "The task_lock() exception", at the end of this comment.
522 *
523 * A task must hold cgroup_mutex to modify cgroups.
524 *
525 * Any task can increment and decrement the count field without lock.
526 * So in general, code holding cgroup_mutex can't rely on the count
527 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800528 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700529 * means that no tasks are currently attached, therefore there is no
530 * way a task attached to that cgroup can fork (the other way to
531 * increment the count). So code holding cgroup_mutex can safely
532 * assume that if the count is zero, it will stay zero. Similarly, if
533 * a task holds cgroup_mutex on a cgroup with zero count, it
534 * knows that the cgroup won't be removed, as cgroup_rmdir()
535 * needs that mutex.
536 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700537 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
538 * (usually) take cgroup_mutex. These are the two most performance
539 * critical pieces of code here. The exception occurs on cgroup_exit(),
540 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
541 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800542 * to the release agent with the name of the cgroup (path relative to
543 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700544 *
545 * A cgroup can only be deleted if both its 'count' of using tasks
546 * is zero, and its list of 'children' cgroups is empty. Since all
547 * tasks in the system use _some_ cgroup, and since there is always at
548 * least one task in the system (init, pid == 1), therefore, top_cgroup
549 * always has either children cgroups and/or using tasks. So we don't
550 * need a special hack to ensure that top_cgroup cannot be deleted.
551 *
552 * The task_lock() exception
553 *
554 * The need for this exception arises from the action of
Cliff Wickman956db3c2008-02-07 00:14:43 -0800555 * cgroup_attach_task(), which overwrites one tasks cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800556 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700557 * several performance critical places that need to reference
558 * task->cgroup without the expense of grabbing a system global
559 * mutex. Therefore except as noted below, when dereferencing or, as
Cliff Wickman956db3c2008-02-07 00:14:43 -0800560 * in cgroup_attach_task(), modifying a task'ss cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700561 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
562 * the task_struct routinely used for such matters.
563 *
564 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800565 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700566 */
567
Paul Menageddbcc7e2007-10-18 23:39:30 -0700568/**
569 * cgroup_lock - lock out any changes to cgroup structures
570 *
571 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700572void cgroup_lock(void)
573{
574 mutex_lock(&cgroup_mutex);
575}
576
577/**
578 * cgroup_unlock - release lock on cgroup changes
579 *
580 * Undo the lock taken in a previous cgroup_lock() call.
581 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700582void cgroup_unlock(void)
583{
584 mutex_unlock(&cgroup_mutex);
585}
586
587/*
588 * A couple of forward declarations required, due to cyclic reference loop:
589 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
590 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
591 * -> cgroup_mkdir.
592 */
593
594static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode);
595static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -0700596static int cgroup_populate_dir(struct cgroup *cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700597static struct inode_operations cgroup_dir_inode_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700598static struct file_operations proc_cgroupstats_operations;
599
600static struct backing_dev_info cgroup_backing_dev_info = {
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700601 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Paul Menagea4243162007-10-18 23:39:35 -0700602};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700603
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700604static int alloc_css_id(struct cgroup_subsys *ss,
605 struct cgroup *parent, struct cgroup *child);
606
Paul Menageddbcc7e2007-10-18 23:39:30 -0700607static struct inode *cgroup_new_inode(mode_t mode, struct super_block *sb)
608{
609 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700610
611 if (inode) {
612 inode->i_mode = mode;
David Howells76aac0e2008-11-14 10:39:12 +1100613 inode->i_uid = current_fsuid();
614 inode->i_gid = current_fsgid();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700615 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
616 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
617 }
618 return inode;
619}
620
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800621/*
622 * Call subsys's pre_destroy handler.
623 * This is called before css refcnt check.
624 */
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700625static int cgroup_call_pre_destroy(struct cgroup *cgrp)
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800626{
627 struct cgroup_subsys *ss;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700628 int ret = 0;
629
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800630 for_each_subsys(cgrp->root, ss)
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700631 if (ss->pre_destroy) {
632 ret = ss->pre_destroy(ss, cgrp);
633 if (ret)
634 break;
635 }
636 return ret;
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800637}
638
Paul Menagea47295e2009-01-07 18:07:44 -0800639static void free_cgroup_rcu(struct rcu_head *obj)
640{
641 struct cgroup *cgrp = container_of(obj, struct cgroup, rcu_head);
642
643 kfree(cgrp);
644}
645
Paul Menageddbcc7e2007-10-18 23:39:30 -0700646static void cgroup_diput(struct dentry *dentry, struct inode *inode)
647{
648 /* is dentry a directory ? if so, kfree() associated cgroup */
649 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700650 struct cgroup *cgrp = dentry->d_fsdata;
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800651 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -0700652 BUG_ON(!(cgroup_is_removed(cgrp)));
Paul Menage81a6a5c2007-10-18 23:39:38 -0700653 /* It's possible for external users to be holding css
654 * reference counts on a cgroup; css_put() needs to
655 * be able to access the cgroup after decrementing
656 * the reference count in order to know if it needs to
657 * queue the cgroup to be handled by the release
658 * agent */
659 synchronize_rcu();
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800660
661 mutex_lock(&cgroup_mutex);
662 /*
663 * Release the subsystem state objects.
664 */
Li Zefan75139b82009-01-07 18:07:33 -0800665 for_each_subsys(cgrp->root, ss)
666 ss->destroy(ss, cgrp);
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800667
668 cgrp->root->number_of_cgroups--;
669 mutex_unlock(&cgroup_mutex);
670
Paul Menagea47295e2009-01-07 18:07:44 -0800671 /*
672 * Drop the active superblock reference that we took when we
673 * created the cgroup
674 */
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800675 deactivate_super(cgrp->root->sb);
676
Paul Menagea47295e2009-01-07 18:07:44 -0800677 call_rcu(&cgrp->rcu_head, free_cgroup_rcu);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700678 }
679 iput(inode);
680}
681
682static void remove_dir(struct dentry *d)
683{
684 struct dentry *parent = dget(d->d_parent);
685
686 d_delete(d);
687 simple_rmdir(parent->d_inode, d);
688 dput(parent);
689}
690
691static void cgroup_clear_directory(struct dentry *dentry)
692{
693 struct list_head *node;
694
695 BUG_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
696 spin_lock(&dcache_lock);
697 node = dentry->d_subdirs.next;
698 while (node != &dentry->d_subdirs) {
699 struct dentry *d = list_entry(node, struct dentry, d_u.d_child);
700 list_del_init(node);
701 if (d->d_inode) {
702 /* This should never be called on a cgroup
703 * directory with child cgroups */
704 BUG_ON(d->d_inode->i_mode & S_IFDIR);
705 d = dget_locked(d);
706 spin_unlock(&dcache_lock);
707 d_delete(d);
708 simple_unlink(dentry->d_inode, d);
709 dput(d);
710 spin_lock(&dcache_lock);
711 }
712 node = dentry->d_subdirs.next;
713 }
714 spin_unlock(&dcache_lock);
715}
716
717/*
718 * NOTE : the dentry must have been dget()'ed
719 */
720static void cgroup_d_remove_dir(struct dentry *dentry)
721{
722 cgroup_clear_directory(dentry);
723
724 spin_lock(&dcache_lock);
725 list_del_init(&dentry->d_u.d_child);
726 spin_unlock(&dcache_lock);
727 remove_dir(dentry);
728}
729
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700730/*
731 * A queue for waiters to do rmdir() cgroup. A tasks will sleep when
732 * cgroup->count == 0 && list_empty(&cgroup->children) && subsys has some
733 * reference to css->refcnt. In general, this refcnt is expected to goes down
734 * to zero, soon.
735 *
736 * CGRP_WAIT_ON_RMDIR flag is modified under cgroup's inode->i_mutex;
737 */
738DECLARE_WAIT_QUEUE_HEAD(cgroup_rmdir_waitq);
739
740static void cgroup_wakeup_rmdir_waiters(const struct cgroup *cgrp)
741{
742 if (unlikely(test_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags)))
743 wake_up_all(&cgroup_rmdir_waitq);
744}
745
Paul Menageddbcc7e2007-10-18 23:39:30 -0700746static int rebind_subsystems(struct cgroupfs_root *root,
747 unsigned long final_bits)
748{
749 unsigned long added_bits, removed_bits;
Paul Menagebd89aab2007-10-18 23:40:44 -0700750 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700751 int i;
752
753 removed_bits = root->actual_subsys_bits & ~final_bits;
754 added_bits = final_bits & ~root->actual_subsys_bits;
755 /* Check that any added subsystems are currently free */
756 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Li Zefan8d53d552008-02-23 15:24:11 -0800757 unsigned long bit = 1UL << i;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700758 struct cgroup_subsys *ss = subsys[i];
759 if (!(bit & added_bits))
760 continue;
761 if (ss->root != &rootnode) {
762 /* Subsystem isn't free */
763 return -EBUSY;
764 }
765 }
766
767 /* Currently we don't handle adding/removing subsystems when
768 * any child cgroups exist. This is theoretically supportable
769 * but involves complex error handling, so it's being left until
770 * later */
Paul Menage307257c2008-12-15 13:54:22 -0800771 if (root->number_of_cgroups > 1)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700772 return -EBUSY;
773
774 /* Process each subsystem */
775 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
776 struct cgroup_subsys *ss = subsys[i];
777 unsigned long bit = 1UL << i;
778 if (bit & added_bits) {
779 /* We're binding this subsystem to this hierarchy */
Paul Menagebd89aab2007-10-18 23:40:44 -0700780 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700781 BUG_ON(!dummytop->subsys[i]);
782 BUG_ON(dummytop->subsys[i]->cgroup != dummytop);
Paul Menage999cd8a2009-01-07 18:08:36 -0800783 mutex_lock(&ss->hierarchy_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -0700784 cgrp->subsys[i] = dummytop->subsys[i];
785 cgrp->subsys[i]->cgroup = cgrp;
Li Zefan33a68ac2009-01-07 18:07:42 -0800786 list_move(&ss->sibling, &root->subsys_list);
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -0800787 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700788 if (ss->bind)
Paul Menagebd89aab2007-10-18 23:40:44 -0700789 ss->bind(ss, cgrp);
Paul Menage999cd8a2009-01-07 18:08:36 -0800790 mutex_unlock(&ss->hierarchy_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700791 } else if (bit & removed_bits) {
792 /* We're removing this subsystem */
Paul Menagebd89aab2007-10-18 23:40:44 -0700793 BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]);
794 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
Paul Menage999cd8a2009-01-07 18:08:36 -0800795 mutex_lock(&ss->hierarchy_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700796 if (ss->bind)
797 ss->bind(ss, dummytop);
798 dummytop->subsys[i]->cgroup = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -0700799 cgrp->subsys[i] = NULL;
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -0800800 subsys[i]->root = &rootnode;
Li Zefan33a68ac2009-01-07 18:07:42 -0800801 list_move(&ss->sibling, &rootnode.subsys_list);
Paul Menage999cd8a2009-01-07 18:08:36 -0800802 mutex_unlock(&ss->hierarchy_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700803 } else if (bit & final_bits) {
804 /* Subsystem state should already exist */
Paul Menagebd89aab2007-10-18 23:40:44 -0700805 BUG_ON(!cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700806 } else {
807 /* Subsystem state shouldn't exist */
Paul Menagebd89aab2007-10-18 23:40:44 -0700808 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700809 }
810 }
811 root->subsys_bits = root->actual_subsys_bits = final_bits;
812 synchronize_rcu();
813
814 return 0;
815}
816
817static int cgroup_show_options(struct seq_file *seq, struct vfsmount *vfs)
818{
819 struct cgroupfs_root *root = vfs->mnt_sb->s_fs_info;
820 struct cgroup_subsys *ss;
821
822 mutex_lock(&cgroup_mutex);
823 for_each_subsys(root, ss)
824 seq_printf(seq, ",%s", ss->name);
825 if (test_bit(ROOT_NOPREFIX, &root->flags))
826 seq_puts(seq, ",noprefix");
Paul Menage81a6a5c2007-10-18 23:39:38 -0700827 if (strlen(root->release_agent_path))
828 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700829 mutex_unlock(&cgroup_mutex);
830 return 0;
831}
832
833struct cgroup_sb_opts {
834 unsigned long subsys_bits;
835 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700836 char *release_agent;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700837};
838
839/* Convert a hierarchy specifier into a bitmask of subsystems and
840 * flags. */
841static int parse_cgroupfs_options(char *data,
842 struct cgroup_sb_opts *opts)
843{
844 char *token, *o = data ?: "all";
845
846 opts->subsys_bits = 0;
847 opts->flags = 0;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700848 opts->release_agent = NULL;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700849
850 while ((token = strsep(&o, ",")) != NULL) {
851 if (!*token)
852 return -EINVAL;
853 if (!strcmp(token, "all")) {
Paul Menage8bab8dd2008-04-04 14:29:57 -0700854 /* Add all non-disabled subsystems */
855 int i;
856 opts->subsys_bits = 0;
857 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
858 struct cgroup_subsys *ss = subsys[i];
859 if (!ss->disabled)
860 opts->subsys_bits |= 1ul << i;
861 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700862 } else if (!strcmp(token, "noprefix")) {
863 set_bit(ROOT_NOPREFIX, &opts->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700864 } else if (!strncmp(token, "release_agent=", 14)) {
865 /* Specifying two release agents is forbidden */
866 if (opts->release_agent)
867 return -EINVAL;
868 opts->release_agent = kzalloc(PATH_MAX, GFP_KERNEL);
869 if (!opts->release_agent)
870 return -ENOMEM;
871 strncpy(opts->release_agent, token + 14, PATH_MAX - 1);
872 opts->release_agent[PATH_MAX - 1] = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700873 } else {
874 struct cgroup_subsys *ss;
875 int i;
876 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
877 ss = subsys[i];
878 if (!strcmp(token, ss->name)) {
Paul Menage8bab8dd2008-04-04 14:29:57 -0700879 if (!ss->disabled)
880 set_bit(i, &opts->subsys_bits);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700881 break;
882 }
883 }
884 if (i == CGROUP_SUBSYS_COUNT)
885 return -ENOENT;
886 }
887 }
888
889 /* We can't have an empty hierarchy */
890 if (!opts->subsys_bits)
891 return -EINVAL;
892
893 return 0;
894}
895
896static int cgroup_remount(struct super_block *sb, int *flags, char *data)
897{
898 int ret = 0;
899 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -0700900 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700901 struct cgroup_sb_opts opts;
902
Paul Menagebd89aab2007-10-18 23:40:44 -0700903 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700904 mutex_lock(&cgroup_mutex);
905
906 /* See what subsystems are wanted */
907 ret = parse_cgroupfs_options(data, &opts);
908 if (ret)
909 goto out_unlock;
910
911 /* Don't allow flags to change at remount */
912 if (opts.flags != root->flags) {
913 ret = -EINVAL;
914 goto out_unlock;
915 }
916
917 ret = rebind_subsystems(root, opts.subsys_bits);
918
919 /* (re)populate subsystem files */
920 if (!ret)
Paul Menagebd89aab2007-10-18 23:40:44 -0700921 cgroup_populate_dir(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700922
Paul Menage81a6a5c2007-10-18 23:39:38 -0700923 if (opts.release_agent)
924 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700925 out_unlock:
Paul Menage81a6a5c2007-10-18 23:39:38 -0700926 if (opts.release_agent)
927 kfree(opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700928 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -0700929 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700930 return ret;
931}
932
933static struct super_operations cgroup_ops = {
934 .statfs = simple_statfs,
935 .drop_inode = generic_delete_inode,
936 .show_options = cgroup_show_options,
937 .remount_fs = cgroup_remount,
938};
939
Paul Menagecc31edc2008-10-18 20:28:04 -0700940static void init_cgroup_housekeeping(struct cgroup *cgrp)
941{
942 INIT_LIST_HEAD(&cgrp->sibling);
943 INIT_LIST_HEAD(&cgrp->children);
944 INIT_LIST_HEAD(&cgrp->css_sets);
945 INIT_LIST_HEAD(&cgrp->release_list);
946 init_rwsem(&cgrp->pids_mutex);
947}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700948static void init_cgroup_root(struct cgroupfs_root *root)
949{
Paul Menagebd89aab2007-10-18 23:40:44 -0700950 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700951 INIT_LIST_HEAD(&root->subsys_list);
952 INIT_LIST_HEAD(&root->root_list);
953 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -0700954 cgrp->root = root;
955 cgrp->top_cgroup = cgrp;
Paul Menagecc31edc2008-10-18 20:28:04 -0700956 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700957}
958
959static int cgroup_test_super(struct super_block *sb, void *data)
960{
961 struct cgroupfs_root *new = data;
962 struct cgroupfs_root *root = sb->s_fs_info;
963
964 /* First check subsystems */
965 if (new->subsys_bits != root->subsys_bits)
966 return 0;
967
968 /* Next check flags */
969 if (new->flags != root->flags)
970 return 0;
971
972 return 1;
973}
974
975static int cgroup_set_super(struct super_block *sb, void *data)
976{
977 int ret;
978 struct cgroupfs_root *root = data;
979
980 ret = set_anon_super(sb, NULL);
981 if (ret)
982 return ret;
983
984 sb->s_fs_info = root;
985 root->sb = sb;
986
987 sb->s_blocksize = PAGE_CACHE_SIZE;
988 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
989 sb->s_magic = CGROUP_SUPER_MAGIC;
990 sb->s_op = &cgroup_ops;
991
992 return 0;
993}
994
995static int cgroup_get_rootdir(struct super_block *sb)
996{
997 struct inode *inode =
998 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
999 struct dentry *dentry;
1000
1001 if (!inode)
1002 return -ENOMEM;
1003
Paul Menageddbcc7e2007-10-18 23:39:30 -07001004 inode->i_fop = &simple_dir_operations;
1005 inode->i_op = &cgroup_dir_inode_operations;
1006 /* directories start off with i_nlink == 2 (for "." entry) */
1007 inc_nlink(inode);
1008 dentry = d_alloc_root(inode);
1009 if (!dentry) {
1010 iput(inode);
1011 return -ENOMEM;
1012 }
1013 sb->s_root = dentry;
1014 return 0;
1015}
1016
1017static int cgroup_get_sb(struct file_system_type *fs_type,
1018 int flags, const char *unused_dev_name,
1019 void *data, struct vfsmount *mnt)
1020{
1021 struct cgroup_sb_opts opts;
1022 int ret = 0;
1023 struct super_block *sb;
1024 struct cgroupfs_root *root;
Li Zefan28fd5df2008-04-29 01:00:13 -07001025 struct list_head tmp_cg_links;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001026
1027 /* First find the desired set of subsystems */
1028 ret = parse_cgroupfs_options(data, &opts);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001029 if (ret) {
1030 if (opts.release_agent)
1031 kfree(opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001032 return ret;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001033 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001034
1035 root = kzalloc(sizeof(*root), GFP_KERNEL);
Li Zefanf7770732008-02-23 15:24:10 -08001036 if (!root) {
1037 if (opts.release_agent)
1038 kfree(opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001039 return -ENOMEM;
Li Zefanf7770732008-02-23 15:24:10 -08001040 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001041
1042 init_cgroup_root(root);
1043 root->subsys_bits = opts.subsys_bits;
1044 root->flags = opts.flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001045 if (opts.release_agent) {
1046 strcpy(root->release_agent_path, opts.release_agent);
1047 kfree(opts.release_agent);
1048 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001049
1050 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, root);
1051
1052 if (IS_ERR(sb)) {
1053 kfree(root);
1054 return PTR_ERR(sb);
1055 }
1056
1057 if (sb->s_fs_info != root) {
1058 /* Reusing an existing superblock */
1059 BUG_ON(sb->s_root == NULL);
1060 kfree(root);
1061 root = NULL;
1062 } else {
1063 /* New superblock */
Li Zefanc12f65d2009-01-07 18:07:42 -08001064 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menage817929e2007-10-18 23:39:36 -07001065 struct inode *inode;
Li Zefan28fd5df2008-04-29 01:00:13 -07001066 int i;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001067
1068 BUG_ON(sb->s_root != NULL);
1069
1070 ret = cgroup_get_rootdir(sb);
1071 if (ret)
1072 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001073 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001074
Paul Menage817929e2007-10-18 23:39:36 -07001075 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001076 mutex_lock(&cgroup_mutex);
1077
Paul Menage817929e2007-10-18 23:39:36 -07001078 /*
1079 * We're accessing css_set_count without locking
1080 * css_set_lock here, but that's OK - it can only be
1081 * increased by someone holding cgroup_lock, and
1082 * that's us. The worst that can happen is that we
1083 * have some link structures left over
1084 */
1085 ret = allocate_cg_links(css_set_count, &tmp_cg_links);
1086 if (ret) {
1087 mutex_unlock(&cgroup_mutex);
1088 mutex_unlock(&inode->i_mutex);
1089 goto drop_new_super;
1090 }
1091
Paul Menageddbcc7e2007-10-18 23:39:30 -07001092 ret = rebind_subsystems(root, root->subsys_bits);
1093 if (ret == -EBUSY) {
1094 mutex_unlock(&cgroup_mutex);
Paul Menage817929e2007-10-18 23:39:36 -07001095 mutex_unlock(&inode->i_mutex);
Li Zefan20ca9b32008-12-23 13:57:14 -08001096 goto free_cg_links;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001097 }
1098
1099 /* EBUSY should be the only error here */
1100 BUG_ON(ret);
1101
1102 list_add(&root->root_list, &roots);
Paul Menage817929e2007-10-18 23:39:36 -07001103 root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001104
Li Zefanc12f65d2009-01-07 18:07:42 -08001105 sb->s_root->d_fsdata = root_cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001106 root->top_cgroup.dentry = sb->s_root;
1107
Paul Menage817929e2007-10-18 23:39:36 -07001108 /* Link the top cgroup in this hierarchy into all
1109 * the css_set objects */
1110 write_lock(&css_set_lock);
Li Zefan28fd5df2008-04-29 01:00:13 -07001111 for (i = 0; i < CSS_SET_TABLE_SIZE; i++) {
1112 struct hlist_head *hhead = &css_set_table[i];
1113 struct hlist_node *node;
Paul Menage817929e2007-10-18 23:39:36 -07001114 struct css_set *cg;
Li Zefan28fd5df2008-04-29 01:00:13 -07001115
Li Zefanc12f65d2009-01-07 18:07:42 -08001116 hlist_for_each_entry(cg, node, hhead, hlist)
1117 link_css_set(&tmp_cg_links, cg, root_cgrp);
Li Zefan28fd5df2008-04-29 01:00:13 -07001118 }
Paul Menage817929e2007-10-18 23:39:36 -07001119 write_unlock(&css_set_lock);
1120
1121 free_cg_links(&tmp_cg_links);
1122
Li Zefanc12f65d2009-01-07 18:07:42 -08001123 BUG_ON(!list_empty(&root_cgrp->sibling));
1124 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001125 BUG_ON(root->number_of_cgroups != 1);
1126
Li Zefanc12f65d2009-01-07 18:07:42 -08001127 cgroup_populate_dir(root_cgrp);
Paul Menage817929e2007-10-18 23:39:36 -07001128 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001129 mutex_unlock(&cgroup_mutex);
1130 }
1131
Sukadev Bhattiprolua3ec9472009-03-04 12:06:34 -08001132 simple_set_mnt(mnt, sb);
1133 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001134
Li Zefan20ca9b32008-12-23 13:57:14 -08001135 free_cg_links:
1136 free_cg_links(&tmp_cg_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001137 drop_new_super:
1138 up_write(&sb->s_umount);
1139 deactivate_super(sb);
1140 return ret;
1141}
1142
1143static void cgroup_kill_sb(struct super_block *sb) {
1144 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001145 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001146 int ret;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001147 struct cg_cgroup_link *link;
1148 struct cg_cgroup_link *saved_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001149
1150 BUG_ON(!root);
1151
1152 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001153 BUG_ON(!list_empty(&cgrp->children));
1154 BUG_ON(!list_empty(&cgrp->sibling));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001155
1156 mutex_lock(&cgroup_mutex);
1157
1158 /* Rebind all subsystems back to the default hierarchy */
1159 ret = rebind_subsystems(root, 0);
1160 /* Shouldn't be able to fail ... */
1161 BUG_ON(ret);
1162
Paul Menage817929e2007-10-18 23:39:36 -07001163 /*
1164 * Release all the links from css_sets to this hierarchy's
1165 * root cgroup
1166 */
1167 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001168
1169 list_for_each_entry_safe(link, saved_link, &cgrp->css_sets,
1170 cgrp_link_list) {
Paul Menage817929e2007-10-18 23:39:36 -07001171 list_del(&link->cg_link_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07001172 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07001173 kfree(link);
1174 }
1175 write_unlock(&css_set_lock);
1176
Paul Menage839ec542009-01-29 14:25:22 -08001177 if (!list_empty(&root->root_list)) {
1178 list_del(&root->root_list);
1179 root_count--;
1180 }
Li Zefane5f6a862009-01-07 18:07:41 -08001181
Paul Menageddbcc7e2007-10-18 23:39:30 -07001182 mutex_unlock(&cgroup_mutex);
1183
Paul Menageddbcc7e2007-10-18 23:39:30 -07001184 kill_litter_super(sb);
Li Zefan67e055d2009-02-18 14:48:20 -08001185 kfree(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001186}
1187
1188static struct file_system_type cgroup_fs_type = {
1189 .name = "cgroup",
1190 .get_sb = cgroup_get_sb,
1191 .kill_sb = cgroup_kill_sb,
1192};
1193
Paul Menagebd89aab2007-10-18 23:40:44 -07001194static inline struct cgroup *__d_cgrp(struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001195{
1196 return dentry->d_fsdata;
1197}
1198
1199static inline struct cftype *__d_cft(struct dentry *dentry)
1200{
1201 return dentry->d_fsdata;
1202}
1203
Li Zefana043e3b2008-02-23 15:24:09 -08001204/**
1205 * cgroup_path - generate the path of a cgroup
1206 * @cgrp: the cgroup in question
1207 * @buf: the buffer to write the path into
1208 * @buflen: the length of the buffer
1209 *
Paul Menagea47295e2009-01-07 18:07:44 -08001210 * Called with cgroup_mutex held or else with an RCU-protected cgroup
1211 * reference. Writes path of cgroup into buf. Returns 0 on success,
1212 * -errno on error.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001213 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001214int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001215{
1216 char *start;
Paul Menagea47295e2009-01-07 18:07:44 -08001217 struct dentry *dentry = rcu_dereference(cgrp->dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001218
Paul Menagea47295e2009-01-07 18:07:44 -08001219 if (!dentry || cgrp == dummytop) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001220 /*
1221 * Inactive subsystems have no dentry for their root
1222 * cgroup
1223 */
1224 strcpy(buf, "/");
1225 return 0;
1226 }
1227
1228 start = buf + buflen;
1229
1230 *--start = '\0';
1231 for (;;) {
Paul Menagea47295e2009-01-07 18:07:44 -08001232 int len = dentry->d_name.len;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001233 if ((start -= len) < buf)
1234 return -ENAMETOOLONG;
Paul Menagebd89aab2007-10-18 23:40:44 -07001235 memcpy(start, cgrp->dentry->d_name.name, len);
1236 cgrp = cgrp->parent;
1237 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001238 break;
Paul Menagea47295e2009-01-07 18:07:44 -08001239 dentry = rcu_dereference(cgrp->dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07001240 if (!cgrp->parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001241 continue;
1242 if (--start < buf)
1243 return -ENAMETOOLONG;
1244 *start = '/';
1245 }
1246 memmove(buf, start, buf + buflen - start);
1247 return 0;
1248}
1249
Paul Menagebbcb81d2007-10-18 23:39:32 -07001250/*
1251 * Return the first subsystem attached to a cgroup's hierarchy, and
1252 * its subsystem id.
1253 */
1254
Paul Menagebd89aab2007-10-18 23:40:44 -07001255static void get_first_subsys(const struct cgroup *cgrp,
Paul Menagebbcb81d2007-10-18 23:39:32 -07001256 struct cgroup_subsys_state **css, int *subsys_id)
1257{
Paul Menagebd89aab2007-10-18 23:40:44 -07001258 const struct cgroupfs_root *root = cgrp->root;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001259 const struct cgroup_subsys *test_ss;
1260 BUG_ON(list_empty(&root->subsys_list));
1261 test_ss = list_entry(root->subsys_list.next,
1262 struct cgroup_subsys, sibling);
1263 if (css) {
Paul Menagebd89aab2007-10-18 23:40:44 -07001264 *css = cgrp->subsys[test_ss->subsys_id];
Paul Menagebbcb81d2007-10-18 23:39:32 -07001265 BUG_ON(!*css);
1266 }
1267 if (subsys_id)
1268 *subsys_id = test_ss->subsys_id;
1269}
1270
Li Zefana043e3b2008-02-23 15:24:09 -08001271/**
1272 * cgroup_attach_task - attach task 'tsk' to cgroup 'cgrp'
1273 * @cgrp: the cgroup the task is attaching to
1274 * @tsk: the task to be attached
Paul Menagebbcb81d2007-10-18 23:39:32 -07001275 *
Li Zefana043e3b2008-02-23 15:24:09 -08001276 * Call holding cgroup_mutex. May take task_lock of
1277 * the task 'tsk' during call.
Paul Menagebbcb81d2007-10-18 23:39:32 -07001278 */
Cliff Wickman956db3c2008-02-07 00:14:43 -08001279int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001280{
1281 int retval = 0;
1282 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07001283 struct cgroup *oldcgrp;
Lai Jiangshan77efecd2009-01-07 18:07:39 -08001284 struct css_set *cg;
Paul Menage817929e2007-10-18 23:39:36 -07001285 struct css_set *newcg;
Paul Menagebd89aab2007-10-18 23:40:44 -07001286 struct cgroupfs_root *root = cgrp->root;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001287 int subsys_id;
1288
Paul Menagebd89aab2007-10-18 23:40:44 -07001289 get_first_subsys(cgrp, NULL, &subsys_id);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001290
1291 /* Nothing to do if the task is already in that cgroup */
Paul Menagebd89aab2007-10-18 23:40:44 -07001292 oldcgrp = task_cgroup(tsk, subsys_id);
1293 if (cgrp == oldcgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001294 return 0;
1295
1296 for_each_subsys(root, ss) {
1297 if (ss->can_attach) {
Paul Menagebd89aab2007-10-18 23:40:44 -07001298 retval = ss->can_attach(ss, cgrp, tsk);
Paul Jacksone18f6312008-02-07 00:13:44 -08001299 if (retval)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001300 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001301 }
1302 }
1303
Lai Jiangshan77efecd2009-01-07 18:07:39 -08001304 task_lock(tsk);
1305 cg = tsk->cgroups;
1306 get_css_set(cg);
1307 task_unlock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07001308 /*
1309 * Locate or allocate a new css_set for this task,
1310 * based on its final set of cgroups
1311 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001312 newcg = find_css_set(cg, cgrp);
Lai Jiangshan77efecd2009-01-07 18:07:39 -08001313 put_css_set(cg);
Paul Jacksone18f6312008-02-07 00:13:44 -08001314 if (!newcg)
Paul Menage817929e2007-10-18 23:39:36 -07001315 return -ENOMEM;
Paul Menage817929e2007-10-18 23:39:36 -07001316
Paul Menagebbcb81d2007-10-18 23:39:32 -07001317 task_lock(tsk);
1318 if (tsk->flags & PF_EXITING) {
1319 task_unlock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07001320 put_css_set(newcg);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001321 return -ESRCH;
1322 }
Paul Menage817929e2007-10-18 23:39:36 -07001323 rcu_assign_pointer(tsk->cgroups, newcg);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001324 task_unlock(tsk);
1325
Paul Menage817929e2007-10-18 23:39:36 -07001326 /* Update the css_set linked lists if we're using them */
1327 write_lock(&css_set_lock);
1328 if (!list_empty(&tsk->cg_list)) {
1329 list_del(&tsk->cg_list);
1330 list_add(&tsk->cg_list, &newcg->tasks);
1331 }
1332 write_unlock(&css_set_lock);
1333
Paul Menagebbcb81d2007-10-18 23:39:32 -07001334 for_each_subsys(root, ss) {
Paul Jacksone18f6312008-02-07 00:13:44 -08001335 if (ss->attach)
Paul Menagebd89aab2007-10-18 23:40:44 -07001336 ss->attach(ss, cgrp, oldcgrp, tsk);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001337 }
Paul Menagebd89aab2007-10-18 23:40:44 -07001338 set_bit(CGRP_RELEASABLE, &oldcgrp->flags);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001339 synchronize_rcu();
Paul Menage817929e2007-10-18 23:39:36 -07001340 put_css_set(cg);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07001341
1342 /*
1343 * wake up rmdir() waiter. the rmdir should fail since the cgroup
1344 * is no longer empty.
1345 */
1346 cgroup_wakeup_rmdir_waiters(cgrp);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001347 return 0;
1348}
1349
1350/*
Paul Menageaf351022008-07-25 01:47:01 -07001351 * Attach task with pid 'pid' to cgroup 'cgrp'. Call with cgroup_mutex
1352 * held. May take task_lock of task
Paul Menagebbcb81d2007-10-18 23:39:32 -07001353 */
Paul Menageaf351022008-07-25 01:47:01 -07001354static int attach_task_by_pid(struct cgroup *cgrp, u64 pid)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001355{
Paul Menagebbcb81d2007-10-18 23:39:32 -07001356 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11001357 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001358 int ret;
1359
Paul Menagebbcb81d2007-10-18 23:39:32 -07001360 if (pid) {
1361 rcu_read_lock();
Pavel Emelyanov73507f32008-02-07 00:14:47 -08001362 tsk = find_task_by_vpid(pid);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001363 if (!tsk || tsk->flags & PF_EXITING) {
1364 rcu_read_unlock();
1365 return -ESRCH;
1366 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07001367
David Howellsc69e8d92008-11-14 10:39:19 +11001368 tcred = __task_cred(tsk);
1369 if (cred->euid &&
1370 cred->euid != tcred->uid &&
1371 cred->euid != tcred->suid) {
1372 rcu_read_unlock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07001373 return -EACCES;
1374 }
David Howellsc69e8d92008-11-14 10:39:19 +11001375 get_task_struct(tsk);
1376 rcu_read_unlock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07001377 } else {
1378 tsk = current;
1379 get_task_struct(tsk);
1380 }
1381
Cliff Wickman956db3c2008-02-07 00:14:43 -08001382 ret = cgroup_attach_task(cgrp, tsk);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001383 put_task_struct(tsk);
1384 return ret;
1385}
1386
Paul Menageaf351022008-07-25 01:47:01 -07001387static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid)
1388{
1389 int ret;
1390 if (!cgroup_lock_live_group(cgrp))
1391 return -ENODEV;
1392 ret = attach_task_by_pid(cgrp, pid);
1393 cgroup_unlock();
1394 return ret;
1395}
1396
Paul Menageddbcc7e2007-10-18 23:39:30 -07001397/* The various types of files and directories in a cgroup file system */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001398enum cgroup_filetype {
1399 FILE_ROOT,
1400 FILE_DIR,
1401 FILE_TASKLIST,
Paul Menage81a6a5c2007-10-18 23:39:38 -07001402 FILE_NOTIFY_ON_RELEASE,
Paul Menage81a6a5c2007-10-18 23:39:38 -07001403 FILE_RELEASE_AGENT,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001404};
1405
Paul Menagee788e062008-07-25 01:46:59 -07001406/**
1407 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
1408 * @cgrp: the cgroup to be checked for liveness
1409 *
Paul Menage84eea842008-07-25 01:47:00 -07001410 * On success, returns true; the lock should be later released with
1411 * cgroup_unlock(). On failure returns false with no lock held.
Paul Menagee788e062008-07-25 01:46:59 -07001412 */
Paul Menage84eea842008-07-25 01:47:00 -07001413bool cgroup_lock_live_group(struct cgroup *cgrp)
Paul Menagee788e062008-07-25 01:46:59 -07001414{
1415 mutex_lock(&cgroup_mutex);
1416 if (cgroup_is_removed(cgrp)) {
1417 mutex_unlock(&cgroup_mutex);
1418 return false;
1419 }
1420 return true;
1421}
1422
1423static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
1424 const char *buffer)
1425{
1426 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
1427 if (!cgroup_lock_live_group(cgrp))
1428 return -ENODEV;
1429 strcpy(cgrp->root->release_agent_path, buffer);
Paul Menage84eea842008-07-25 01:47:00 -07001430 cgroup_unlock();
Paul Menagee788e062008-07-25 01:46:59 -07001431 return 0;
1432}
1433
1434static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
1435 struct seq_file *seq)
1436{
1437 if (!cgroup_lock_live_group(cgrp))
1438 return -ENODEV;
1439 seq_puts(seq, cgrp->root->release_agent_path);
1440 seq_putc(seq, '\n');
Paul Menage84eea842008-07-25 01:47:00 -07001441 cgroup_unlock();
Paul Menagee788e062008-07-25 01:46:59 -07001442 return 0;
1443}
1444
Paul Menage84eea842008-07-25 01:47:00 -07001445/* A buffer size big enough for numbers or short strings */
1446#define CGROUP_LOCAL_BUFFER_SIZE 64
1447
Paul Menagee73d2c62008-04-29 01:00:06 -07001448static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
Paul Menagef4c753b2008-04-29 00:59:56 -07001449 struct file *file,
1450 const char __user *userbuf,
1451 size_t nbytes, loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07001452{
Paul Menage84eea842008-07-25 01:47:00 -07001453 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menage355e0c42007-10-18 23:39:33 -07001454 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07001455 char *end;
1456
1457 if (!nbytes)
1458 return -EINVAL;
1459 if (nbytes >= sizeof(buffer))
1460 return -E2BIG;
1461 if (copy_from_user(buffer, userbuf, nbytes))
1462 return -EFAULT;
1463
1464 buffer[nbytes] = 0; /* nul-terminate */
Paul Menageb7269df2008-04-29 00:59:59 -07001465 strstrip(buffer);
Paul Menagee73d2c62008-04-29 01:00:06 -07001466 if (cft->write_u64) {
1467 u64 val = simple_strtoull(buffer, &end, 0);
1468 if (*end)
1469 return -EINVAL;
1470 retval = cft->write_u64(cgrp, cft, val);
1471 } else {
1472 s64 val = simple_strtoll(buffer, &end, 0);
1473 if (*end)
1474 return -EINVAL;
1475 retval = cft->write_s64(cgrp, cft, val);
1476 }
Paul Menage355e0c42007-10-18 23:39:33 -07001477 if (!retval)
1478 retval = nbytes;
1479 return retval;
1480}
1481
Paul Menagedb3b1492008-07-25 01:46:58 -07001482static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
1483 struct file *file,
1484 const char __user *userbuf,
1485 size_t nbytes, loff_t *unused_ppos)
1486{
Paul Menage84eea842008-07-25 01:47:00 -07001487 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagedb3b1492008-07-25 01:46:58 -07001488 int retval = 0;
1489 size_t max_bytes = cft->max_write_len;
1490 char *buffer = local_buffer;
1491
1492 if (!max_bytes)
1493 max_bytes = sizeof(local_buffer) - 1;
1494 if (nbytes >= max_bytes)
1495 return -E2BIG;
1496 /* Allocate a dynamic buffer if we need one */
1497 if (nbytes >= sizeof(local_buffer)) {
1498 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
1499 if (buffer == NULL)
1500 return -ENOMEM;
1501 }
Li Zefan5a3eb9f2008-07-29 22:33:18 -07001502 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
1503 retval = -EFAULT;
1504 goto out;
1505 }
Paul Menagedb3b1492008-07-25 01:46:58 -07001506
1507 buffer[nbytes] = 0; /* nul-terminate */
1508 strstrip(buffer);
1509 retval = cft->write_string(cgrp, cft, buffer);
1510 if (!retval)
1511 retval = nbytes;
Li Zefan5a3eb9f2008-07-29 22:33:18 -07001512out:
Paul Menagedb3b1492008-07-25 01:46:58 -07001513 if (buffer != local_buffer)
1514 kfree(buffer);
1515 return retval;
1516}
1517
Paul Menageddbcc7e2007-10-18 23:39:30 -07001518static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
1519 size_t nbytes, loff_t *ppos)
1520{
1521 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07001522 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001523
Li Zefan75139b82009-01-07 18:07:33 -08001524 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001525 return -ENODEV;
Paul Menage355e0c42007-10-18 23:39:33 -07001526 if (cft->write)
Paul Menagebd89aab2007-10-18 23:40:44 -07001527 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07001528 if (cft->write_u64 || cft->write_s64)
1529 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagedb3b1492008-07-25 01:46:58 -07001530 if (cft->write_string)
1531 return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07001532 if (cft->trigger) {
1533 int ret = cft->trigger(cgrp, (unsigned int)cft->private);
1534 return ret ? ret : nbytes;
1535 }
Paul Menage355e0c42007-10-18 23:39:33 -07001536 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001537}
1538
Paul Menagef4c753b2008-04-29 00:59:56 -07001539static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
1540 struct file *file,
1541 char __user *buf, size_t nbytes,
1542 loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001543{
Paul Menage84eea842008-07-25 01:47:00 -07001544 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagef4c753b2008-04-29 00:59:56 -07001545 u64 val = cft->read_u64(cgrp, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001546 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
1547
1548 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
1549}
1550
Paul Menagee73d2c62008-04-29 01:00:06 -07001551static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
1552 struct file *file,
1553 char __user *buf, size_t nbytes,
1554 loff_t *ppos)
1555{
Paul Menage84eea842008-07-25 01:47:00 -07001556 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagee73d2c62008-04-29 01:00:06 -07001557 s64 val = cft->read_s64(cgrp, cft);
1558 int len = sprintf(tmp, "%lld\n", (long long) val);
1559
1560 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
1561}
1562
Paul Menageddbcc7e2007-10-18 23:39:30 -07001563static ssize_t cgroup_file_read(struct file *file, char __user *buf,
1564 size_t nbytes, loff_t *ppos)
1565{
1566 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07001567 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001568
Li Zefan75139b82009-01-07 18:07:33 -08001569 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001570 return -ENODEV;
1571
1572 if (cft->read)
Paul Menagebd89aab2007-10-18 23:40:44 -07001573 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07001574 if (cft->read_u64)
1575 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07001576 if (cft->read_s64)
1577 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001578 return -EINVAL;
1579}
1580
Paul Menage91796562008-04-29 01:00:01 -07001581/*
1582 * seqfile ops/methods for returning structured data. Currently just
1583 * supports string->u64 maps, but can be extended in future.
1584 */
1585
1586struct cgroup_seqfile_state {
1587 struct cftype *cft;
1588 struct cgroup *cgroup;
1589};
1590
1591static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
1592{
1593 struct seq_file *sf = cb->state;
1594 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
1595}
1596
1597static int cgroup_seqfile_show(struct seq_file *m, void *arg)
1598{
1599 struct cgroup_seqfile_state *state = m->private;
1600 struct cftype *cft = state->cft;
Serge E. Hallyn29486df2008-04-29 01:00:14 -07001601 if (cft->read_map) {
1602 struct cgroup_map_cb cb = {
1603 .fill = cgroup_map_add,
1604 .state = m,
1605 };
1606 return cft->read_map(state->cgroup, cft, &cb);
1607 }
1608 return cft->read_seq_string(state->cgroup, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07001609}
1610
Adrian Bunk96930a62008-07-25 19:46:21 -07001611static int cgroup_seqfile_release(struct inode *inode, struct file *file)
Paul Menage91796562008-04-29 01:00:01 -07001612{
1613 struct seq_file *seq = file->private_data;
1614 kfree(seq->private);
1615 return single_release(inode, file);
1616}
1617
1618static struct file_operations cgroup_seqfile_operations = {
1619 .read = seq_read,
Paul Menagee788e062008-07-25 01:46:59 -07001620 .write = cgroup_file_write,
Paul Menage91796562008-04-29 01:00:01 -07001621 .llseek = seq_lseek,
1622 .release = cgroup_seqfile_release,
1623};
1624
Paul Menageddbcc7e2007-10-18 23:39:30 -07001625static int cgroup_file_open(struct inode *inode, struct file *file)
1626{
1627 int err;
1628 struct cftype *cft;
1629
1630 err = generic_file_open(inode, file);
1631 if (err)
1632 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001633 cft = __d_cft(file->f_dentry);
Li Zefan75139b82009-01-07 18:07:33 -08001634
Serge E. Hallyn29486df2008-04-29 01:00:14 -07001635 if (cft->read_map || cft->read_seq_string) {
Paul Menage91796562008-04-29 01:00:01 -07001636 struct cgroup_seqfile_state *state =
1637 kzalloc(sizeof(*state), GFP_USER);
1638 if (!state)
1639 return -ENOMEM;
1640 state->cft = cft;
1641 state->cgroup = __d_cgrp(file->f_dentry->d_parent);
1642 file->f_op = &cgroup_seqfile_operations;
1643 err = single_open(file, cgroup_seqfile_show, state);
1644 if (err < 0)
1645 kfree(state);
1646 } else if (cft->open)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001647 err = cft->open(inode, file);
1648 else
1649 err = 0;
1650
1651 return err;
1652}
1653
1654static int cgroup_file_release(struct inode *inode, struct file *file)
1655{
1656 struct cftype *cft = __d_cft(file->f_dentry);
1657 if (cft->release)
1658 return cft->release(inode, file);
1659 return 0;
1660}
1661
1662/*
1663 * cgroup_rename - Only allow simple rename of directories in place.
1664 */
1665static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
1666 struct inode *new_dir, struct dentry *new_dentry)
1667{
1668 if (!S_ISDIR(old_dentry->d_inode->i_mode))
1669 return -ENOTDIR;
1670 if (new_dentry->d_inode)
1671 return -EEXIST;
1672 if (old_dir != new_dir)
1673 return -EIO;
1674 return simple_rename(old_dir, old_dentry, new_dir, new_dentry);
1675}
1676
1677static struct file_operations cgroup_file_operations = {
1678 .read = cgroup_file_read,
1679 .write = cgroup_file_write,
1680 .llseek = generic_file_llseek,
1681 .open = cgroup_file_open,
1682 .release = cgroup_file_release,
1683};
1684
1685static struct inode_operations cgroup_dir_inode_operations = {
1686 .lookup = simple_lookup,
1687 .mkdir = cgroup_mkdir,
1688 .rmdir = cgroup_rmdir,
1689 .rename = cgroup_rename,
1690};
1691
1692static int cgroup_create_file(struct dentry *dentry, int mode,
1693 struct super_block *sb)
1694{
Al Viro3ba13d12009-02-20 06:02:22 +00001695 static const struct dentry_operations cgroup_dops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001696 .d_iput = cgroup_diput,
1697 };
1698
1699 struct inode *inode;
1700
1701 if (!dentry)
1702 return -ENOENT;
1703 if (dentry->d_inode)
1704 return -EEXIST;
1705
1706 inode = cgroup_new_inode(mode, sb);
1707 if (!inode)
1708 return -ENOMEM;
1709
1710 if (S_ISDIR(mode)) {
1711 inode->i_op = &cgroup_dir_inode_operations;
1712 inode->i_fop = &simple_dir_operations;
1713
1714 /* start off with i_nlink == 2 (for "." entry) */
1715 inc_nlink(inode);
1716
1717 /* start with the directory inode held, so that we can
1718 * populate it without racing with another mkdir */
Paul Menage817929e2007-10-18 23:39:36 -07001719 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001720 } else if (S_ISREG(mode)) {
1721 inode->i_size = 0;
1722 inode->i_fop = &cgroup_file_operations;
1723 }
1724 dentry->d_op = &cgroup_dops;
1725 d_instantiate(dentry, inode);
1726 dget(dentry); /* Extra count - pin the dentry in core */
1727 return 0;
1728}
1729
1730/*
Li Zefana043e3b2008-02-23 15:24:09 -08001731 * cgroup_create_dir - create a directory for an object.
1732 * @cgrp: the cgroup we create the directory for. It must have a valid
1733 * ->parent field. And we are going to fill its ->dentry field.
1734 * @dentry: dentry of the new cgroup
1735 * @mode: mode to set on new directory.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001736 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001737static int cgroup_create_dir(struct cgroup *cgrp, struct dentry *dentry,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001738 int mode)
1739{
1740 struct dentry *parent;
1741 int error = 0;
1742
Paul Menagebd89aab2007-10-18 23:40:44 -07001743 parent = cgrp->parent->dentry;
1744 error = cgroup_create_file(dentry, S_IFDIR | mode, cgrp->root->sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001745 if (!error) {
Paul Menagebd89aab2007-10-18 23:40:44 -07001746 dentry->d_fsdata = cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001747 inc_nlink(parent->d_inode);
Paul Menagea47295e2009-01-07 18:07:44 -08001748 rcu_assign_pointer(cgrp->dentry, dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001749 dget(dentry);
1750 }
1751 dput(dentry);
1752
1753 return error;
1754}
1755
Paul Menagebd89aab2007-10-18 23:40:44 -07001756int cgroup_add_file(struct cgroup *cgrp,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001757 struct cgroup_subsys *subsys,
1758 const struct cftype *cft)
1759{
Paul Menagebd89aab2007-10-18 23:40:44 -07001760 struct dentry *dir = cgrp->dentry;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001761 struct dentry *dentry;
1762 int error;
1763
1764 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Paul Menagebd89aab2007-10-18 23:40:44 -07001765 if (subsys && !test_bit(ROOT_NOPREFIX, &cgrp->root->flags)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001766 strcpy(name, subsys->name);
1767 strcat(name, ".");
1768 }
1769 strcat(name, cft->name);
1770 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
1771 dentry = lookup_one_len(name, dir, strlen(name));
1772 if (!IS_ERR(dentry)) {
1773 error = cgroup_create_file(dentry, 0644 | S_IFREG,
Paul Menagebd89aab2007-10-18 23:40:44 -07001774 cgrp->root->sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001775 if (!error)
1776 dentry->d_fsdata = (void *)cft;
1777 dput(dentry);
1778 } else
1779 error = PTR_ERR(dentry);
1780 return error;
1781}
1782
Paul Menagebd89aab2007-10-18 23:40:44 -07001783int cgroup_add_files(struct cgroup *cgrp,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001784 struct cgroup_subsys *subsys,
1785 const struct cftype cft[],
1786 int count)
1787{
1788 int i, err;
1789 for (i = 0; i < count; i++) {
Paul Menagebd89aab2007-10-18 23:40:44 -07001790 err = cgroup_add_file(cgrp, subsys, &cft[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001791 if (err)
1792 return err;
1793 }
1794 return 0;
1795}
1796
Li Zefana043e3b2008-02-23 15:24:09 -08001797/**
1798 * cgroup_task_count - count the number of tasks in a cgroup.
1799 * @cgrp: the cgroup in question
1800 *
1801 * Return the number of tasks in the cgroup.
1802 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001803int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001804{
1805 int count = 0;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001806 struct cg_cgroup_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001807
Paul Menage817929e2007-10-18 23:39:36 -07001808 read_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001809 list_for_each_entry(link, &cgrp->css_sets, cgrp_link_list) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07001810 count += atomic_read(&link->cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07001811 }
1812 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001813 return count;
1814}
1815
1816/*
Paul Menage817929e2007-10-18 23:39:36 -07001817 * Advance a list_head iterator. The iterator should be positioned at
1818 * the start of a css_set
1819 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001820static void cgroup_advance_iter(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -07001821 struct cgroup_iter *it)
1822{
1823 struct list_head *l = it->cg_link;
1824 struct cg_cgroup_link *link;
1825 struct css_set *cg;
1826
1827 /* Advance to the next non-empty css_set */
1828 do {
1829 l = l->next;
Paul Menagebd89aab2007-10-18 23:40:44 -07001830 if (l == &cgrp->css_sets) {
Paul Menage817929e2007-10-18 23:39:36 -07001831 it->cg_link = NULL;
1832 return;
1833 }
Paul Menagebd89aab2007-10-18 23:40:44 -07001834 link = list_entry(l, struct cg_cgroup_link, cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07001835 cg = link->cg;
1836 } while (list_empty(&cg->tasks));
1837 it->cg_link = l;
1838 it->task = cg->tasks.next;
1839}
1840
Cliff Wickman31a7df02008-02-07 00:14:42 -08001841/*
1842 * To reduce the fork() overhead for systems that are not actually
1843 * using their cgroups capability, we don't maintain the lists running
1844 * through each css_set to its tasks until we see the list actually
1845 * used - in other words after the first call to cgroup_iter_start().
1846 *
1847 * The tasklist_lock is not held here, as do_each_thread() and
1848 * while_each_thread() are protected by RCU.
1849 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07001850static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08001851{
1852 struct task_struct *p, *g;
1853 write_lock(&css_set_lock);
1854 use_task_css_set_links = 1;
1855 do_each_thread(g, p) {
1856 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08001857 /*
1858 * We should check if the process is exiting, otherwise
1859 * it will race with cgroup_exit() in that the list
1860 * entry won't be deleted though the process has exited.
1861 */
1862 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Cliff Wickman31a7df02008-02-07 00:14:42 -08001863 list_add(&p->cg_list, &p->cgroups->tasks);
1864 task_unlock(p);
1865 } while_each_thread(g, p);
1866 write_unlock(&css_set_lock);
1867}
1868
Paul Menagebd89aab2007-10-18 23:40:44 -07001869void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07001870{
1871 /*
1872 * The first time anyone tries to iterate across a cgroup,
1873 * we need to enable the list linking each css_set to its
1874 * tasks, and fix up all existing tasks.
1875 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08001876 if (!use_task_css_set_links)
1877 cgroup_enable_task_cg_lists();
1878
Paul Menage817929e2007-10-18 23:39:36 -07001879 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07001880 it->cg_link = &cgrp->css_sets;
1881 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07001882}
1883
Paul Menagebd89aab2007-10-18 23:40:44 -07001884struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -07001885 struct cgroup_iter *it)
1886{
1887 struct task_struct *res;
1888 struct list_head *l = it->task;
Lai Jiangshan2019f632009-01-07 18:07:36 -08001889 struct cg_cgroup_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07001890
1891 /* If the iterator cg is NULL, we have no tasks */
1892 if (!it->cg_link)
1893 return NULL;
1894 res = list_entry(l, struct task_struct, cg_list);
1895 /* Advance iterator to find next entry */
1896 l = l->next;
Lai Jiangshan2019f632009-01-07 18:07:36 -08001897 link = list_entry(it->cg_link, struct cg_cgroup_link, cgrp_link_list);
1898 if (l == &link->cg->tasks) {
Paul Menage817929e2007-10-18 23:39:36 -07001899 /* We reached the end of this task list - move on to
1900 * the next cg_cgroup_link */
Paul Menagebd89aab2007-10-18 23:40:44 -07001901 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07001902 } else {
1903 it->task = l;
1904 }
1905 return res;
1906}
1907
Paul Menagebd89aab2007-10-18 23:40:44 -07001908void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07001909{
1910 read_unlock(&css_set_lock);
1911}
1912
Cliff Wickman31a7df02008-02-07 00:14:42 -08001913static inline int started_after_time(struct task_struct *t1,
1914 struct timespec *time,
1915 struct task_struct *t2)
1916{
1917 int start_diff = timespec_compare(&t1->start_time, time);
1918 if (start_diff > 0) {
1919 return 1;
1920 } else if (start_diff < 0) {
1921 return 0;
1922 } else {
1923 /*
1924 * Arbitrarily, if two processes started at the same
1925 * time, we'll say that the lower pointer value
1926 * started first. Note that t2 may have exited by now
1927 * so this may not be a valid pointer any longer, but
1928 * that's fine - it still serves to distinguish
1929 * between two tasks started (effectively) simultaneously.
1930 */
1931 return t1 > t2;
1932 }
1933}
1934
1935/*
1936 * This function is a callback from heap_insert() and is used to order
1937 * the heap.
1938 * In this case we order the heap in descending task start time.
1939 */
1940static inline int started_after(void *p1, void *p2)
1941{
1942 struct task_struct *t1 = p1;
1943 struct task_struct *t2 = p2;
1944 return started_after_time(t1, &t2->start_time, t2);
1945}
1946
1947/**
1948 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
1949 * @scan: struct cgroup_scanner containing arguments for the scan
1950 *
1951 * Arguments include pointers to callback functions test_task() and
1952 * process_task().
1953 * Iterate through all the tasks in a cgroup, calling test_task() for each,
1954 * and if it returns true, call process_task() for it also.
1955 * The test_task pointer may be NULL, meaning always true (select all tasks).
1956 * Effectively duplicates cgroup_iter_{start,next,end}()
1957 * but does not lock css_set_lock for the call to process_task().
1958 * The struct cgroup_scanner may be embedded in any structure of the caller's
1959 * creation.
1960 * It is guaranteed that process_task() will act on every task that
1961 * is a member of the cgroup for the duration of this call. This
1962 * function may or may not call process_task() for tasks that exit
1963 * or move to a different cgroup during the call, or are forked or
1964 * move into the cgroup during the call.
1965 *
1966 * Note that test_task() may be called with locks held, and may in some
1967 * situations be called multiple times for the same task, so it should
1968 * be cheap.
1969 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
1970 * pre-allocated and will be used for heap operations (and its "gt" member will
1971 * be overwritten), else a temporary heap will be used (allocation of which
1972 * may cause this function to fail).
1973 */
1974int cgroup_scan_tasks(struct cgroup_scanner *scan)
1975{
1976 int retval, i;
1977 struct cgroup_iter it;
1978 struct task_struct *p, *dropped;
1979 /* Never dereference latest_task, since it's not refcounted */
1980 struct task_struct *latest_task = NULL;
1981 struct ptr_heap tmp_heap;
1982 struct ptr_heap *heap;
1983 struct timespec latest_time = { 0, 0 };
1984
1985 if (scan->heap) {
1986 /* The caller supplied our heap and pre-allocated its memory */
1987 heap = scan->heap;
1988 heap->gt = &started_after;
1989 } else {
1990 /* We need to allocate our own heap memory */
1991 heap = &tmp_heap;
1992 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
1993 if (retval)
1994 /* cannot allocate the heap */
1995 return retval;
1996 }
1997
1998 again:
1999 /*
2000 * Scan tasks in the cgroup, using the scanner's "test_task" callback
2001 * to determine which are of interest, and using the scanner's
2002 * "process_task" callback to process any of them that need an update.
2003 * Since we don't want to hold any locks during the task updates,
2004 * gather tasks to be processed in a heap structure.
2005 * The heap is sorted by descending task start time.
2006 * If the statically-sized heap fills up, we overflow tasks that
2007 * started later, and in future iterations only consider tasks that
2008 * started after the latest task in the previous pass. This
2009 * guarantees forward progress and that we don't miss any tasks.
2010 */
2011 heap->size = 0;
2012 cgroup_iter_start(scan->cg, &it);
2013 while ((p = cgroup_iter_next(scan->cg, &it))) {
2014 /*
2015 * Only affect tasks that qualify per the caller's callback,
2016 * if he provided one
2017 */
2018 if (scan->test_task && !scan->test_task(p, scan))
2019 continue;
2020 /*
2021 * Only process tasks that started after the last task
2022 * we processed
2023 */
2024 if (!started_after_time(p, &latest_time, latest_task))
2025 continue;
2026 dropped = heap_insert(heap, p);
2027 if (dropped == NULL) {
2028 /*
2029 * The new task was inserted; the heap wasn't
2030 * previously full
2031 */
2032 get_task_struct(p);
2033 } else if (dropped != p) {
2034 /*
2035 * The new task was inserted, and pushed out a
2036 * different task
2037 */
2038 get_task_struct(p);
2039 put_task_struct(dropped);
2040 }
2041 /*
2042 * Else the new task was newer than anything already in
2043 * the heap and wasn't inserted
2044 */
2045 }
2046 cgroup_iter_end(scan->cg, &it);
2047
2048 if (heap->size) {
2049 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07002050 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08002051 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07002052 latest_time = q->start_time;
2053 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08002054 }
2055 /* Process the task per the caller's callback */
Paul Jackson4fe91d52008-04-29 00:59:55 -07002056 scan->process_task(q, scan);
2057 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002058 }
2059 /*
2060 * If we had to process any tasks at all, scan again
2061 * in case some of them were in the middle of forking
2062 * children that didn't get processed.
2063 * Not the most efficient way to do it, but it avoids
2064 * having to take callback_mutex in the fork path
2065 */
2066 goto again;
2067 }
2068 if (heap == &tmp_heap)
2069 heap_free(&tmp_heap);
2070 return 0;
2071}
2072
Paul Menage817929e2007-10-18 23:39:36 -07002073/*
Paul Menagebbcb81d2007-10-18 23:39:32 -07002074 * Stuff for reading the 'tasks' file.
2075 *
2076 * Reading this file can return large amounts of data if a cgroup has
2077 * *lots* of attached tasks. So it may need several calls to read(),
2078 * but we cannot guarantee that the information we produce is correct
2079 * unless we produce it entirely atomically.
2080 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07002081 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07002082
2083/*
2084 * Load into 'pidarray' up to 'npids' of the tasks using cgroup
Paul Menagebd89aab2007-10-18 23:40:44 -07002085 * 'cgrp'. Return actual number of pids loaded. No need to
Paul Menagebbcb81d2007-10-18 23:39:32 -07002086 * task_lock(p) when reading out p->cgroup, since we're in an RCU
2087 * read section, so the css_set can't go away, and is
2088 * immutable after creation.
2089 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002090static int pid_array_load(pid_t *pidarray, int npids, struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002091{
Gowrishankar Me7b80bb2009-01-07 18:07:43 -08002092 int n = 0, pid;
Paul Menage817929e2007-10-18 23:39:36 -07002093 struct cgroup_iter it;
2094 struct task_struct *tsk;
Paul Menagebd89aab2007-10-18 23:40:44 -07002095 cgroup_iter_start(cgrp, &it);
2096 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Paul Menage817929e2007-10-18 23:39:36 -07002097 if (unlikely(n == npids))
2098 break;
Gowrishankar Me7b80bb2009-01-07 18:07:43 -08002099 pid = task_pid_vnr(tsk);
2100 if (pid > 0)
2101 pidarray[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07002102 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002103 cgroup_iter_end(cgrp, &it);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002104 return n;
2105}
2106
Balbir Singh846c7bb2007-10-18 23:39:44 -07002107/**
Li Zefana043e3b2008-02-23 15:24:09 -08002108 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07002109 * @stats: cgroupstats to fill information into
2110 * @dentry: A dentry entry belonging to the cgroup for which stats have
2111 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08002112 *
2113 * Build and fill cgroupstats so that taskstats can export it to user
2114 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07002115 */
2116int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
2117{
2118 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07002119 struct cgroup *cgrp;
Balbir Singh846c7bb2007-10-18 23:39:44 -07002120 struct cgroup_iter it;
2121 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08002122
Balbir Singh846c7bb2007-10-18 23:39:44 -07002123 /*
Li Zefan33d283b2008-11-19 15:36:48 -08002124 * Validate dentry by checking the superblock operations,
2125 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07002126 */
Li Zefan33d283b2008-11-19 15:36:48 -08002127 if (dentry->d_sb->s_op != &cgroup_ops ||
2128 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07002129 goto err;
2130
2131 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07002132 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07002133
Paul Menagebd89aab2007-10-18 23:40:44 -07002134 cgroup_iter_start(cgrp, &it);
2135 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07002136 switch (tsk->state) {
2137 case TASK_RUNNING:
2138 stats->nr_running++;
2139 break;
2140 case TASK_INTERRUPTIBLE:
2141 stats->nr_sleeping++;
2142 break;
2143 case TASK_UNINTERRUPTIBLE:
2144 stats->nr_uninterruptible++;
2145 break;
2146 case TASK_STOPPED:
2147 stats->nr_stopped++;
2148 break;
2149 default:
2150 if (delayacct_is_task_waiting_on_io(tsk))
2151 stats->nr_io_wait++;
2152 break;
2153 }
2154 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002155 cgroup_iter_end(cgrp, &it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07002156
Balbir Singh846c7bb2007-10-18 23:39:44 -07002157err:
2158 return ret;
2159}
2160
Paul Menagebbcb81d2007-10-18 23:39:32 -07002161static int cmppid(const void *a, const void *b)
2162{
2163 return *(pid_t *)a - *(pid_t *)b;
2164}
2165
Paul Menagebbcb81d2007-10-18 23:39:32 -07002166
Paul Menagecc31edc2008-10-18 20:28:04 -07002167/*
2168 * seq_file methods for the "tasks" file. The seq_file position is the
2169 * next pid to display; the seq_file iterator is a pointer to the pid
2170 * in the cgroup->tasks_pids array.
2171 */
2172
2173static void *cgroup_tasks_start(struct seq_file *s, loff_t *pos)
2174{
2175 /*
2176 * Initially we receive a position value that corresponds to
2177 * one more than the last pid shown (or 0 on the first call or
2178 * after a seek to the start). Use a binary-search to find the
2179 * next pid to display, if any
2180 */
2181 struct cgroup *cgrp = s->private;
2182 int index = 0, pid = *pos;
2183 int *iter;
2184
2185 down_read(&cgrp->pids_mutex);
2186 if (pid) {
2187 int end = cgrp->pids_length;
Stephen Rothwell20777762008-10-21 16:11:20 +11002188
Paul Menagecc31edc2008-10-18 20:28:04 -07002189 while (index < end) {
2190 int mid = (index + end) / 2;
2191 if (cgrp->tasks_pids[mid] == pid) {
2192 index = mid;
2193 break;
2194 } else if (cgrp->tasks_pids[mid] <= pid)
2195 index = mid + 1;
2196 else
2197 end = mid;
2198 }
2199 }
2200 /* If we're off the end of the array, we're done */
2201 if (index >= cgrp->pids_length)
2202 return NULL;
2203 /* Update the abstract position to be the actual pid that we found */
2204 iter = cgrp->tasks_pids + index;
2205 *pos = *iter;
2206 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002207}
2208
Paul Menagecc31edc2008-10-18 20:28:04 -07002209static void cgroup_tasks_stop(struct seq_file *s, void *v)
2210{
2211 struct cgroup *cgrp = s->private;
2212 up_read(&cgrp->pids_mutex);
2213}
2214
2215static void *cgroup_tasks_next(struct seq_file *s, void *v, loff_t *pos)
2216{
2217 struct cgroup *cgrp = s->private;
2218 int *p = v;
2219 int *end = cgrp->tasks_pids + cgrp->pids_length;
2220
2221 /*
2222 * Advance to the next pid in the array. If this goes off the
2223 * end, we're done
2224 */
2225 p++;
2226 if (p >= end) {
2227 return NULL;
2228 } else {
2229 *pos = *p;
2230 return p;
2231 }
2232}
2233
2234static int cgroup_tasks_show(struct seq_file *s, void *v)
2235{
2236 return seq_printf(s, "%d\n", *(int *)v);
2237}
2238
2239static struct seq_operations cgroup_tasks_seq_operations = {
2240 .start = cgroup_tasks_start,
2241 .stop = cgroup_tasks_stop,
2242 .next = cgroup_tasks_next,
2243 .show = cgroup_tasks_show,
2244};
2245
2246static void release_cgroup_pid_array(struct cgroup *cgrp)
2247{
2248 down_write(&cgrp->pids_mutex);
2249 BUG_ON(!cgrp->pids_use_count);
2250 if (!--cgrp->pids_use_count) {
2251 kfree(cgrp->tasks_pids);
2252 cgrp->tasks_pids = NULL;
2253 cgrp->pids_length = 0;
2254 }
2255 up_write(&cgrp->pids_mutex);
2256}
2257
2258static int cgroup_tasks_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002259{
Paul Menagebd89aab2007-10-18 23:40:44 -07002260 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002261
2262 if (!(file->f_mode & FMODE_READ))
2263 return 0;
2264
Paul Menagecc31edc2008-10-18 20:28:04 -07002265 release_cgroup_pid_array(cgrp);
2266 return seq_release(inode, file);
2267}
2268
2269static struct file_operations cgroup_tasks_operations = {
2270 .read = seq_read,
2271 .llseek = seq_lseek,
2272 .write = cgroup_file_write,
2273 .release = cgroup_tasks_release,
2274};
2275
2276/*
2277 * Handle an open on 'tasks' file. Prepare an array containing the
2278 * process id's of tasks currently attached to the cgroup being opened.
2279 */
2280
2281static int cgroup_tasks_open(struct inode *unused, struct file *file)
2282{
2283 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
2284 pid_t *pidarray;
2285 int npids;
2286 int retval;
2287
2288 /* Nothing to do for write-only files */
2289 if (!(file->f_mode & FMODE_READ))
2290 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002291
2292 /*
2293 * If cgroup gets more users after we read count, we won't have
2294 * enough space - tough. This race is indistinguishable to the
2295 * caller from the case that the additional cgroup users didn't
2296 * show up until sometime later on.
2297 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002298 npids = cgroup_task_count(cgrp);
Paul Menagecc31edc2008-10-18 20:28:04 -07002299 pidarray = kmalloc(npids * sizeof(pid_t), GFP_KERNEL);
2300 if (!pidarray)
2301 return -ENOMEM;
2302 npids = pid_array_load(pidarray, npids, cgrp);
2303 sort(pidarray, npids, sizeof(pid_t), cmppid, NULL);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002304
Paul Menagecc31edc2008-10-18 20:28:04 -07002305 /*
2306 * Store the array in the cgroup, freeing the old
2307 * array if necessary
2308 */
2309 down_write(&cgrp->pids_mutex);
2310 kfree(cgrp->tasks_pids);
2311 cgrp->tasks_pids = pidarray;
2312 cgrp->pids_length = npids;
2313 cgrp->pids_use_count++;
2314 up_write(&cgrp->pids_mutex);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002315
Paul Menagecc31edc2008-10-18 20:28:04 -07002316 file->f_op = &cgroup_tasks_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002317
Paul Menagecc31edc2008-10-18 20:28:04 -07002318 retval = seq_open(file, &cgroup_tasks_seq_operations);
2319 if (retval) {
2320 release_cgroup_pid_array(cgrp);
2321 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002322 }
Paul Menagecc31edc2008-10-18 20:28:04 -07002323 ((struct seq_file *)file->private_data)->private = cgrp;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002324 return 0;
2325}
2326
Paul Menagebd89aab2007-10-18 23:40:44 -07002327static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
Paul Menage81a6a5c2007-10-18 23:39:38 -07002328 struct cftype *cft)
2329{
Paul Menagebd89aab2007-10-18 23:40:44 -07002330 return notify_on_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07002331}
2332
Paul Menage6379c102008-07-25 01:47:01 -07002333static int cgroup_write_notify_on_release(struct cgroup *cgrp,
2334 struct cftype *cft,
2335 u64 val)
2336{
2337 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
2338 if (val)
2339 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
2340 else
2341 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
2342 return 0;
2343}
2344
Paul Menagebbcb81d2007-10-18 23:39:32 -07002345/*
2346 * for the common functions, 'private' gives the type of file
2347 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07002348static struct cftype files[] = {
2349 {
2350 .name = "tasks",
2351 .open = cgroup_tasks_open,
Paul Menageaf351022008-07-25 01:47:01 -07002352 .write_u64 = cgroup_tasks_write,
Paul Menage81a6a5c2007-10-18 23:39:38 -07002353 .release = cgroup_tasks_release,
2354 .private = FILE_TASKLIST,
2355 },
2356
2357 {
2358 .name = "notify_on_release",
Paul Menagef4c753b2008-04-29 00:59:56 -07002359 .read_u64 = cgroup_read_notify_on_release,
Paul Menage6379c102008-07-25 01:47:01 -07002360 .write_u64 = cgroup_write_notify_on_release,
Paul Menage81a6a5c2007-10-18 23:39:38 -07002361 .private = FILE_NOTIFY_ON_RELEASE,
2362 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07002363};
2364
2365static struct cftype cft_release_agent = {
2366 .name = "release_agent",
Paul Menagee788e062008-07-25 01:46:59 -07002367 .read_seq_string = cgroup_release_agent_show,
2368 .write_string = cgroup_release_agent_write,
2369 .max_write_len = PATH_MAX,
Paul Menage81a6a5c2007-10-18 23:39:38 -07002370 .private = FILE_RELEASE_AGENT,
Paul Menagebbcb81d2007-10-18 23:39:32 -07002371};
2372
Paul Menagebd89aab2007-10-18 23:40:44 -07002373static int cgroup_populate_dir(struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002374{
2375 int err;
2376 struct cgroup_subsys *ss;
2377
2378 /* First clear out any existing files */
Paul Menagebd89aab2007-10-18 23:40:44 -07002379 cgroup_clear_directory(cgrp->dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002380
Paul Menagebd89aab2007-10-18 23:40:44 -07002381 err = cgroup_add_files(cgrp, NULL, files, ARRAY_SIZE(files));
Paul Menagebbcb81d2007-10-18 23:39:32 -07002382 if (err < 0)
2383 return err;
2384
Paul Menagebd89aab2007-10-18 23:40:44 -07002385 if (cgrp == cgrp->top_cgroup) {
2386 if ((err = cgroup_add_file(cgrp, NULL, &cft_release_agent)) < 0)
Paul Menage81a6a5c2007-10-18 23:39:38 -07002387 return err;
2388 }
2389
Paul Menagebd89aab2007-10-18 23:40:44 -07002390 for_each_subsys(cgrp->root, ss) {
2391 if (ss->populate && (err = ss->populate(ss, cgrp)) < 0)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002392 return err;
2393 }
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07002394 /* This cgroup is ready now */
2395 for_each_subsys(cgrp->root, ss) {
2396 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
2397 /*
2398 * Update id->css pointer and make this css visible from
2399 * CSS ID functions. This pointer will be dereferened
2400 * from RCU-read-side without locks.
2401 */
2402 if (css->id)
2403 rcu_assign_pointer(css->id->css, css);
2404 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002405
2406 return 0;
2407}
2408
2409static void init_cgroup_css(struct cgroup_subsys_state *css,
2410 struct cgroup_subsys *ss,
Paul Menagebd89aab2007-10-18 23:40:44 -07002411 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002412{
Paul Menagebd89aab2007-10-18 23:40:44 -07002413 css->cgroup = cgrp;
Paul Menagee7c5ec92009-01-07 18:08:38 -08002414 atomic_set(&css->refcnt, 1);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002415 css->flags = 0;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07002416 css->id = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07002417 if (cgrp == dummytop)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002418 set_bit(CSS_ROOT, &css->flags);
Paul Menagebd89aab2007-10-18 23:40:44 -07002419 BUG_ON(cgrp->subsys[ss->subsys_id]);
2420 cgrp->subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002421}
2422
Paul Menage999cd8a2009-01-07 18:08:36 -08002423static void cgroup_lock_hierarchy(struct cgroupfs_root *root)
2424{
2425 /* We need to take each hierarchy_mutex in a consistent order */
2426 int i;
2427
2428 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2429 struct cgroup_subsys *ss = subsys[i];
2430 if (ss->root == root)
Li Zefancfebe562009-02-11 13:04:36 -08002431 mutex_lock(&ss->hierarchy_mutex);
Paul Menage999cd8a2009-01-07 18:08:36 -08002432 }
2433}
2434
2435static void cgroup_unlock_hierarchy(struct cgroupfs_root *root)
2436{
2437 int i;
2438
2439 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2440 struct cgroup_subsys *ss = subsys[i];
2441 if (ss->root == root)
2442 mutex_unlock(&ss->hierarchy_mutex);
2443 }
2444}
2445
Paul Menageddbcc7e2007-10-18 23:39:30 -07002446/*
Li Zefana043e3b2008-02-23 15:24:09 -08002447 * cgroup_create - create a cgroup
2448 * @parent: cgroup that will be parent of the new cgroup
2449 * @dentry: dentry of the new cgroup
2450 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07002451 *
Li Zefana043e3b2008-02-23 15:24:09 -08002452 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07002453 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07002454static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
2455 int mode)
2456{
Paul Menagebd89aab2007-10-18 23:40:44 -07002457 struct cgroup *cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002458 struct cgroupfs_root *root = parent->root;
2459 int err = 0;
2460 struct cgroup_subsys *ss;
2461 struct super_block *sb = root->sb;
2462
Paul Menagebd89aab2007-10-18 23:40:44 -07002463 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
2464 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002465 return -ENOMEM;
2466
2467 /* Grab a reference on the superblock so the hierarchy doesn't
2468 * get deleted on unmount if there are child cgroups. This
2469 * can be done outside cgroup_mutex, since the sb can't
2470 * disappear while someone has an open control file on the
2471 * fs */
2472 atomic_inc(&sb->s_active);
2473
2474 mutex_lock(&cgroup_mutex);
2475
Paul Menagecc31edc2008-10-18 20:28:04 -07002476 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002477
Paul Menagebd89aab2007-10-18 23:40:44 -07002478 cgrp->parent = parent;
2479 cgrp->root = parent->root;
2480 cgrp->top_cgroup = parent->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002481
Li Zefanb6abdb02008-03-04 14:28:19 -08002482 if (notify_on_release(parent))
2483 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
2484
Paul Menageddbcc7e2007-10-18 23:39:30 -07002485 for_each_subsys(root, ss) {
Paul Menagebd89aab2007-10-18 23:40:44 -07002486 struct cgroup_subsys_state *css = ss->create(ss, cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002487 if (IS_ERR(css)) {
2488 err = PTR_ERR(css);
2489 goto err_destroy;
2490 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002491 init_cgroup_css(css, ss, cgrp);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07002492 if (ss->use_id)
2493 if (alloc_css_id(ss, parent, cgrp))
2494 goto err_destroy;
2495 /* At error, ->destroy() callback has to free assigned ID. */
Paul Menageddbcc7e2007-10-18 23:39:30 -07002496 }
2497
Paul Menage999cd8a2009-01-07 18:08:36 -08002498 cgroup_lock_hierarchy(root);
Paul Menagebd89aab2007-10-18 23:40:44 -07002499 list_add(&cgrp->sibling, &cgrp->parent->children);
Paul Menage999cd8a2009-01-07 18:08:36 -08002500 cgroup_unlock_hierarchy(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002501 root->number_of_cgroups++;
2502
Paul Menagebd89aab2007-10-18 23:40:44 -07002503 err = cgroup_create_dir(cgrp, dentry, mode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002504 if (err < 0)
2505 goto err_remove;
2506
2507 /* The cgroup directory was pre-locked for us */
Paul Menagebd89aab2007-10-18 23:40:44 -07002508 BUG_ON(!mutex_is_locked(&cgrp->dentry->d_inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002509
Paul Menagebd89aab2007-10-18 23:40:44 -07002510 err = cgroup_populate_dir(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002511 /* If err < 0, we have a half-filled directory - oh well ;) */
2512
2513 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07002514 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002515
2516 return 0;
2517
2518 err_remove:
2519
KAMEZAWA Hiroyukibaef99a2009-01-29 14:25:10 -08002520 cgroup_lock_hierarchy(root);
Paul Menagebd89aab2007-10-18 23:40:44 -07002521 list_del(&cgrp->sibling);
KAMEZAWA Hiroyukibaef99a2009-01-29 14:25:10 -08002522 cgroup_unlock_hierarchy(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002523 root->number_of_cgroups--;
2524
2525 err_destroy:
2526
2527 for_each_subsys(root, ss) {
Paul Menagebd89aab2007-10-18 23:40:44 -07002528 if (cgrp->subsys[ss->subsys_id])
2529 ss->destroy(ss, cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002530 }
2531
2532 mutex_unlock(&cgroup_mutex);
2533
2534 /* Release the reference count that we took on the superblock */
2535 deactivate_super(sb);
2536
Paul Menagebd89aab2007-10-18 23:40:44 -07002537 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002538 return err;
2539}
2540
2541static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2542{
2543 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
2544
2545 /* the vfs holds inode->i_mutex already */
2546 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
2547}
2548
Li Zefan55b6fd02008-07-29 22:33:20 -07002549static int cgroup_has_css_refs(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07002550{
2551 /* Check the reference count on each subsystem. Since we
2552 * already established that there are no tasks in the
Paul Menagee7c5ec92009-01-07 18:08:38 -08002553 * cgroup, if the css refcount is also 1, then there should
Paul Menage81a6a5c2007-10-18 23:39:38 -07002554 * be no outstanding references, so the subsystem is safe to
2555 * destroy. We scan across all subsystems rather than using
2556 * the per-hierarchy linked list of mounted subsystems since
2557 * we can be called via check_for_release() with no
2558 * synchronization other than RCU, and the subsystem linked
2559 * list isn't RCU-safe */
2560 int i;
2561 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2562 struct cgroup_subsys *ss = subsys[i];
2563 struct cgroup_subsys_state *css;
2564 /* Skip subsystems not in this hierarchy */
Paul Menagebd89aab2007-10-18 23:40:44 -07002565 if (ss->root != cgrp->root)
Paul Menage81a6a5c2007-10-18 23:39:38 -07002566 continue;
Paul Menagebd89aab2007-10-18 23:40:44 -07002567 css = cgrp->subsys[ss->subsys_id];
Paul Menage81a6a5c2007-10-18 23:39:38 -07002568 /* When called from check_for_release() it's possible
2569 * that by this point the cgroup has been removed
2570 * and the css deleted. But a false-positive doesn't
2571 * matter, since it can only happen if the cgroup
2572 * has been deleted and hence no longer needs the
2573 * release agent to be called anyway. */
Paul Menagee7c5ec92009-01-07 18:08:38 -08002574 if (css && (atomic_read(&css->refcnt) > 1))
Paul Menage81a6a5c2007-10-18 23:39:38 -07002575 return 1;
Paul Menage81a6a5c2007-10-18 23:39:38 -07002576 }
2577 return 0;
2578}
2579
Paul Menagee7c5ec92009-01-07 18:08:38 -08002580/*
2581 * Atomically mark all (or else none) of the cgroup's CSS objects as
2582 * CSS_REMOVED. Return true on success, or false if the cgroup has
2583 * busy subsystems. Call with cgroup_mutex held
2584 */
2585
2586static int cgroup_clear_css_refs(struct cgroup *cgrp)
2587{
2588 struct cgroup_subsys *ss;
2589 unsigned long flags;
2590 bool failed = false;
2591 local_irq_save(flags);
2592 for_each_subsys(cgrp->root, ss) {
2593 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
2594 int refcnt;
Paul Menage804b3c22009-01-29 14:25:21 -08002595 while (1) {
Paul Menagee7c5ec92009-01-07 18:08:38 -08002596 /* We can only remove a CSS with a refcnt==1 */
2597 refcnt = atomic_read(&css->refcnt);
2598 if (refcnt > 1) {
2599 failed = true;
2600 goto done;
2601 }
2602 BUG_ON(!refcnt);
2603 /*
2604 * Drop the refcnt to 0 while we check other
2605 * subsystems. This will cause any racing
2606 * css_tryget() to spin until we set the
2607 * CSS_REMOVED bits or abort
2608 */
Paul Menage804b3c22009-01-29 14:25:21 -08002609 if (atomic_cmpxchg(&css->refcnt, refcnt, 0) == refcnt)
2610 break;
2611 cpu_relax();
2612 }
Paul Menagee7c5ec92009-01-07 18:08:38 -08002613 }
2614 done:
2615 for_each_subsys(cgrp->root, ss) {
2616 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
2617 if (failed) {
2618 /*
2619 * Restore old refcnt if we previously managed
2620 * to clear it from 1 to 0
2621 */
2622 if (!atomic_read(&css->refcnt))
2623 atomic_set(&css->refcnt, 1);
2624 } else {
2625 /* Commit the fact that the CSS is removed */
2626 set_bit(CSS_REMOVED, &css->flags);
2627 }
2628 }
2629 local_irq_restore(flags);
2630 return !failed;
2631}
2632
Paul Menageddbcc7e2007-10-18 23:39:30 -07002633static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
2634{
Paul Menagebd89aab2007-10-18 23:40:44 -07002635 struct cgroup *cgrp = dentry->d_fsdata;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002636 struct dentry *d;
2637 struct cgroup *parent;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07002638 DEFINE_WAIT(wait);
2639 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002640
2641 /* the vfs holds both inode->i_mutex already */
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07002642again:
Paul Menageddbcc7e2007-10-18 23:39:30 -07002643 mutex_lock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07002644 if (atomic_read(&cgrp->count) != 0) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002645 mutex_unlock(&cgroup_mutex);
2646 return -EBUSY;
2647 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002648 if (!list_empty(&cgrp->children)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002649 mutex_unlock(&cgroup_mutex);
2650 return -EBUSY;
2651 }
KAMEZAWA Hiroyuki3fa59df2008-11-19 15:36:34 -08002652 mutex_unlock(&cgroup_mutex);
Li Zefana043e3b2008-02-23 15:24:09 -08002653
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -08002654 /*
Li Zefana043e3b2008-02-23 15:24:09 -08002655 * Call pre_destroy handlers of subsys. Notify subsystems
2656 * that rmdir() request comes.
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -08002657 */
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07002658 ret = cgroup_call_pre_destroy(cgrp);
2659 if (ret)
2660 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002661
KAMEZAWA Hiroyuki3fa59df2008-11-19 15:36:34 -08002662 mutex_lock(&cgroup_mutex);
2663 parent = cgrp->parent;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07002664 if (atomic_read(&cgrp->count) || !list_empty(&cgrp->children)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002665 mutex_unlock(&cgroup_mutex);
2666 return -EBUSY;
2667 }
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07002668 /*
2669 * css_put/get is provided for subsys to grab refcnt to css. In typical
2670 * case, subsystem has no reference after pre_destroy(). But, under
2671 * hierarchy management, some *temporal* refcnt can be hold.
2672 * To avoid returning -EBUSY to a user, waitqueue is used. If subsys
2673 * is really busy, it should return -EBUSY at pre_destroy(). wake_up
2674 * is called when css_put() is called and refcnt goes down to 0.
2675 */
2676 set_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
2677 prepare_to_wait(&cgroup_rmdir_waitq, &wait, TASK_INTERRUPTIBLE);
2678
2679 if (!cgroup_clear_css_refs(cgrp)) {
2680 mutex_unlock(&cgroup_mutex);
2681 schedule();
2682 finish_wait(&cgroup_rmdir_waitq, &wait);
2683 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
2684 if (signal_pending(current))
2685 return -EINTR;
2686 goto again;
2687 }
2688 /* NO css_tryget() can success after here. */
2689 finish_wait(&cgroup_rmdir_waitq, &wait);
2690 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002691
Paul Menage81a6a5c2007-10-18 23:39:38 -07002692 spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07002693 set_bit(CGRP_REMOVED, &cgrp->flags);
2694 if (!list_empty(&cgrp->release_list))
2695 list_del(&cgrp->release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07002696 spin_unlock(&release_list_lock);
Paul Menage999cd8a2009-01-07 18:08:36 -08002697
2698 cgroup_lock_hierarchy(cgrp->root);
2699 /* delete this cgroup from parent->children */
Paul Menagebd89aab2007-10-18 23:40:44 -07002700 list_del(&cgrp->sibling);
Paul Menage999cd8a2009-01-07 18:08:36 -08002701 cgroup_unlock_hierarchy(cgrp->root);
2702
Paul Menagebd89aab2007-10-18 23:40:44 -07002703 spin_lock(&cgrp->dentry->d_lock);
2704 d = dget(cgrp->dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002705 spin_unlock(&d->d_lock);
2706
2707 cgroup_d_remove_dir(d);
2708 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002709
Paul Menagebd89aab2007-10-18 23:40:44 -07002710 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07002711 check_for_release(parent);
2712
Paul Menageddbcc7e2007-10-18 23:39:30 -07002713 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002714 return 0;
2715}
2716
Li Zefan06a11922008-04-29 01:00:07 -07002717static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002718{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002719 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08002720
2721 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002722
2723 /* Create the top cgroup state for this subsystem */
Li Zefan33a68ac2009-01-07 18:07:42 -08002724 list_add(&ss->sibling, &rootnode.subsys_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002725 ss->root = &rootnode;
2726 css = ss->create(ss, dummytop);
2727 /* We don't handle early failures gracefully */
2728 BUG_ON(IS_ERR(css));
2729 init_cgroup_css(css, ss, dummytop);
2730
Li Zefane8d55fd2008-04-29 01:00:13 -07002731 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07002732 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07002733 * newly registered, all tasks and hence the
2734 * init_css_set is in the subsystem's top cgroup. */
2735 init_css_set.subsys[ss->subsys_id] = dummytop->subsys[ss->subsys_id];
Paul Menageddbcc7e2007-10-18 23:39:30 -07002736
2737 need_forkexit_callback |= ss->fork || ss->exit;
2738
Li Zefane8d55fd2008-04-29 01:00:13 -07002739 /* At system boot, before all subsystems have been
2740 * registered, no tasks have been forked, so we don't
2741 * need to invoke fork callbacks here. */
2742 BUG_ON(!list_empty(&init_task.tasks));
2743
Paul Menage999cd8a2009-01-07 18:08:36 -08002744 mutex_init(&ss->hierarchy_mutex);
Li Zefancfebe562009-02-11 13:04:36 -08002745 lockdep_set_class(&ss->hierarchy_mutex, &ss->subsys_key);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002746 ss->active = 1;
2747}
2748
2749/**
Li Zefana043e3b2008-02-23 15:24:09 -08002750 * cgroup_init_early - cgroup initialization at system boot
2751 *
2752 * Initialize cgroups at system boot, and initialize any
2753 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07002754 */
2755int __init cgroup_init_early(void)
2756{
2757 int i;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07002758 atomic_set(&init_css_set.refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -07002759 INIT_LIST_HEAD(&init_css_set.cg_links);
2760 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07002761 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07002762 css_set_count = 1;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002763 init_cgroup_root(&rootnode);
Paul Menage817929e2007-10-18 23:39:36 -07002764 root_count = 1;
2765 init_task.cgroups = &init_css_set;
2766
2767 init_css_set_link.cg = &init_css_set;
Paul Menagebd89aab2007-10-18 23:40:44 -07002768 list_add(&init_css_set_link.cgrp_link_list,
Paul Menage817929e2007-10-18 23:39:36 -07002769 &rootnode.top_cgroup.css_sets);
2770 list_add(&init_css_set_link.cg_link_list,
2771 &init_css_set.cg_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002772
Li Zefan472b1052008-04-29 01:00:11 -07002773 for (i = 0; i < CSS_SET_TABLE_SIZE; i++)
2774 INIT_HLIST_HEAD(&css_set_table[i]);
2775
Paul Menageddbcc7e2007-10-18 23:39:30 -07002776 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2777 struct cgroup_subsys *ss = subsys[i];
2778
2779 BUG_ON(!ss->name);
2780 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
2781 BUG_ON(!ss->create);
2782 BUG_ON(!ss->destroy);
2783 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08002784 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07002785 ss->name, ss->subsys_id);
2786 BUG();
2787 }
2788
2789 if (ss->early_init)
2790 cgroup_init_subsys(ss);
2791 }
2792 return 0;
2793}
2794
2795/**
Li Zefana043e3b2008-02-23 15:24:09 -08002796 * cgroup_init - cgroup initialization
2797 *
2798 * Register cgroup filesystem and /proc file, and initialize
2799 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07002800 */
2801int __init cgroup_init(void)
2802{
2803 int err;
2804 int i;
Li Zefan472b1052008-04-29 01:00:11 -07002805 struct hlist_head *hhead;
Paul Menagea4243162007-10-18 23:39:35 -07002806
2807 err = bdi_init(&cgroup_backing_dev_info);
2808 if (err)
2809 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002810
2811 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2812 struct cgroup_subsys *ss = subsys[i];
2813 if (!ss->early_init)
2814 cgroup_init_subsys(ss);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07002815 if (ss->use_id)
2816 cgroup_subsys_init_idr(ss);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002817 }
2818
Li Zefan472b1052008-04-29 01:00:11 -07002819 /* Add init_css_set to the hash table */
2820 hhead = css_set_hash(init_css_set.subsys);
2821 hlist_add_head(&init_css_set.hlist, hhead);
2822
Paul Menageddbcc7e2007-10-18 23:39:30 -07002823 err = register_filesystem(&cgroup_fs_type);
2824 if (err < 0)
2825 goto out;
2826
Li Zefan46ae2202008-04-29 01:00:08 -07002827 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07002828
Paul Menageddbcc7e2007-10-18 23:39:30 -07002829out:
Paul Menagea4243162007-10-18 23:39:35 -07002830 if (err)
2831 bdi_destroy(&cgroup_backing_dev_info);
2832
Paul Menageddbcc7e2007-10-18 23:39:30 -07002833 return err;
2834}
Paul Menageb4f48b62007-10-18 23:39:33 -07002835
Paul Menagea4243162007-10-18 23:39:35 -07002836/*
2837 * proc_cgroup_show()
2838 * - Print task's cgroup paths into seq_file, one line for each hierarchy
2839 * - Used for /proc/<pid>/cgroup.
2840 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
2841 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08002842 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07002843 * anyway. No need to check that tsk->cgroup != NULL, thanks to
2844 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
2845 * cgroup to top_cgroup.
2846 */
2847
2848/* TODO: Use a proper seq_file iterator */
2849static int proc_cgroup_show(struct seq_file *m, void *v)
2850{
2851 struct pid *pid;
2852 struct task_struct *tsk;
2853 char *buf;
2854 int retval;
2855 struct cgroupfs_root *root;
2856
2857 retval = -ENOMEM;
2858 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2859 if (!buf)
2860 goto out;
2861
2862 retval = -ESRCH;
2863 pid = m->private;
2864 tsk = get_pid_task(pid, PIDTYPE_PID);
2865 if (!tsk)
2866 goto out_free;
2867
2868 retval = 0;
2869
2870 mutex_lock(&cgroup_mutex);
2871
Li Zefane5f6a862009-01-07 18:07:41 -08002872 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07002873 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07002874 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07002875 int subsys_id;
2876 int count = 0;
2877
Paul Menageb6c30062008-04-10 21:29:16 -07002878 seq_printf(m, "%lu:", root->subsys_bits);
Paul Menagea4243162007-10-18 23:39:35 -07002879 for_each_subsys(root, ss)
2880 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
2881 seq_putc(m, ':');
2882 get_first_subsys(&root->top_cgroup, NULL, &subsys_id);
Paul Menagebd89aab2007-10-18 23:40:44 -07002883 cgrp = task_cgroup(tsk, subsys_id);
2884 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07002885 if (retval < 0)
2886 goto out_unlock;
2887 seq_puts(m, buf);
2888 seq_putc(m, '\n');
2889 }
2890
2891out_unlock:
2892 mutex_unlock(&cgroup_mutex);
2893 put_task_struct(tsk);
2894out_free:
2895 kfree(buf);
2896out:
2897 return retval;
2898}
2899
2900static int cgroup_open(struct inode *inode, struct file *file)
2901{
2902 struct pid *pid = PROC_I(inode)->pid;
2903 return single_open(file, proc_cgroup_show, pid);
2904}
2905
2906struct file_operations proc_cgroup_operations = {
2907 .open = cgroup_open,
2908 .read = seq_read,
2909 .llseek = seq_lseek,
2910 .release = single_release,
2911};
2912
2913/* Display information about each subsystem and each hierarchy */
2914static int proc_cgroupstats_show(struct seq_file *m, void *v)
2915{
2916 int i;
Paul Menagea4243162007-10-18 23:39:35 -07002917
Paul Menage8bab8dd2008-04-04 14:29:57 -07002918 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Paul Menagea4243162007-10-18 23:39:35 -07002919 mutex_lock(&cgroup_mutex);
Paul Menagea4243162007-10-18 23:39:35 -07002920 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2921 struct cgroup_subsys *ss = subsys[i];
Paul Menage8bab8dd2008-04-04 14:29:57 -07002922 seq_printf(m, "%s\t%lu\t%d\t%d\n",
Paul Menage817929e2007-10-18 23:39:36 -07002923 ss->name, ss->root->subsys_bits,
Paul Menage8bab8dd2008-04-04 14:29:57 -07002924 ss->root->number_of_cgroups, !ss->disabled);
Paul Menagea4243162007-10-18 23:39:35 -07002925 }
2926 mutex_unlock(&cgroup_mutex);
2927 return 0;
2928}
2929
2930static int cgroupstats_open(struct inode *inode, struct file *file)
2931{
Al Viro9dce07f2008-03-29 03:07:28 +00002932 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07002933}
2934
2935static struct file_operations proc_cgroupstats_operations = {
2936 .open = cgroupstats_open,
2937 .read = seq_read,
2938 .llseek = seq_lseek,
2939 .release = single_release,
2940};
2941
Paul Menageb4f48b62007-10-18 23:39:33 -07002942/**
2943 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08002944 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07002945 *
2946 * Description: A task inherits its parent's cgroup at fork().
2947 *
2948 * A pointer to the shared css_set was automatically copied in
2949 * fork.c by dup_task_struct(). However, we ignore that copy, since
2950 * it was not made under the protection of RCU or cgroup_mutex, so
Cliff Wickman956db3c2008-02-07 00:14:43 -08002951 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
Paul Menage817929e2007-10-18 23:39:36 -07002952 * have already changed current->cgroups, allowing the previously
2953 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07002954 *
2955 * At the point that cgroup_fork() is called, 'current' is the parent
2956 * task, and the passed argument 'child' points to the child task.
2957 */
2958void cgroup_fork(struct task_struct *child)
2959{
Paul Menage817929e2007-10-18 23:39:36 -07002960 task_lock(current);
2961 child->cgroups = current->cgroups;
2962 get_css_set(child->cgroups);
2963 task_unlock(current);
2964 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07002965}
2966
2967/**
Li Zefana043e3b2008-02-23 15:24:09 -08002968 * cgroup_fork_callbacks - run fork callbacks
2969 * @child: the new task
2970 *
2971 * Called on a new task very soon before adding it to the
2972 * tasklist. No need to take any locks since no-one can
2973 * be operating on this task.
Paul Menageb4f48b62007-10-18 23:39:33 -07002974 */
2975void cgroup_fork_callbacks(struct task_struct *child)
2976{
2977 if (need_forkexit_callback) {
2978 int i;
2979 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2980 struct cgroup_subsys *ss = subsys[i];
2981 if (ss->fork)
2982 ss->fork(ss, child);
2983 }
2984 }
2985}
2986
2987/**
Li Zefana043e3b2008-02-23 15:24:09 -08002988 * cgroup_post_fork - called on a new task after adding it to the task list
2989 * @child: the task in question
2990 *
2991 * Adds the task to the list running through its css_set if necessary.
2992 * Has to be after the task is visible on the task list in case we race
2993 * with the first call to cgroup_iter_start() - to guarantee that the
2994 * new task ends up on its list.
2995 */
Paul Menage817929e2007-10-18 23:39:36 -07002996void cgroup_post_fork(struct task_struct *child)
2997{
2998 if (use_task_css_set_links) {
2999 write_lock(&css_set_lock);
Lai Jiangshanb12b5332009-01-07 18:07:36 -08003000 task_lock(child);
Paul Menage817929e2007-10-18 23:39:36 -07003001 if (list_empty(&child->cg_list))
3002 list_add(&child->cg_list, &child->cgroups->tasks);
Lai Jiangshanb12b5332009-01-07 18:07:36 -08003003 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07003004 write_unlock(&css_set_lock);
3005 }
3006}
3007/**
Paul Menageb4f48b62007-10-18 23:39:33 -07003008 * cgroup_exit - detach cgroup from exiting task
3009 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08003010 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07003011 *
3012 * Description: Detach cgroup from @tsk and release it.
3013 *
3014 * Note that cgroups marked notify_on_release force every task in
3015 * them to take the global cgroup_mutex mutex when exiting.
3016 * This could impact scaling on very large systems. Be reluctant to
3017 * use notify_on_release cgroups where very high task exit scaling
3018 * is required on large systems.
3019 *
3020 * the_top_cgroup_hack:
3021 *
3022 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
3023 *
3024 * We call cgroup_exit() while the task is still competent to
3025 * handle notify_on_release(), then leave the task attached to the
3026 * root cgroup in each hierarchy for the remainder of its exit.
3027 *
3028 * To do this properly, we would increment the reference count on
3029 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
3030 * code we would add a second cgroup function call, to drop that
3031 * reference. This would just create an unnecessary hot spot on
3032 * the top_cgroup reference count, to no avail.
3033 *
3034 * Normally, holding a reference to a cgroup without bumping its
3035 * count is unsafe. The cgroup could go away, or someone could
3036 * attach us to a different cgroup, decrementing the count on
3037 * the first cgroup that we never incremented. But in this case,
3038 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08003039 * which wards off any cgroup_attach_task() attempts, or task is a failed
3040 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07003041 */
3042void cgroup_exit(struct task_struct *tsk, int run_callbacks)
3043{
3044 int i;
Paul Menage817929e2007-10-18 23:39:36 -07003045 struct css_set *cg;
Paul Menageb4f48b62007-10-18 23:39:33 -07003046
3047 if (run_callbacks && need_forkexit_callback) {
3048 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3049 struct cgroup_subsys *ss = subsys[i];
3050 if (ss->exit)
3051 ss->exit(ss, tsk);
3052 }
3053 }
Paul Menage817929e2007-10-18 23:39:36 -07003054
3055 /*
3056 * Unlink from the css_set task list if necessary.
3057 * Optimistically check cg_list before taking
3058 * css_set_lock
3059 */
3060 if (!list_empty(&tsk->cg_list)) {
3061 write_lock(&css_set_lock);
3062 if (!list_empty(&tsk->cg_list))
3063 list_del(&tsk->cg_list);
3064 write_unlock(&css_set_lock);
3065 }
3066
Paul Menageb4f48b62007-10-18 23:39:33 -07003067 /* Reassign the task to the init_css_set. */
3068 task_lock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07003069 cg = tsk->cgroups;
3070 tsk->cgroups = &init_css_set;
Paul Menageb4f48b62007-10-18 23:39:33 -07003071 task_unlock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07003072 if (cg)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003073 put_css_set_taskexit(cg);
Paul Menageb4f48b62007-10-18 23:39:33 -07003074}
Paul Menage697f4162007-10-18 23:39:34 -07003075
3076/**
Li Zefana043e3b2008-02-23 15:24:09 -08003077 * cgroup_clone - clone the cgroup the given subsystem is attached to
3078 * @tsk: the task to be moved
3079 * @subsys: the given subsystem
Serge E. Hallyne885dcd2008-07-25 01:47:06 -07003080 * @nodename: the name for the new cgroup
Li Zefana043e3b2008-02-23 15:24:09 -08003081 *
3082 * Duplicate the current cgroup in the hierarchy that the given
3083 * subsystem is attached to, and move this task into the new
3084 * child.
Paul Menage697f4162007-10-18 23:39:34 -07003085 */
Serge E. Hallyne885dcd2008-07-25 01:47:06 -07003086int cgroup_clone(struct task_struct *tsk, struct cgroup_subsys *subsys,
3087 char *nodename)
Paul Menage697f4162007-10-18 23:39:34 -07003088{
3089 struct dentry *dentry;
3090 int ret = 0;
Paul Menage697f4162007-10-18 23:39:34 -07003091 struct cgroup *parent, *child;
3092 struct inode *inode;
3093 struct css_set *cg;
3094 struct cgroupfs_root *root;
3095 struct cgroup_subsys *ss;
3096
3097 /* We shouldn't be called by an unregistered subsystem */
3098 BUG_ON(!subsys->active);
3099
3100 /* First figure out what hierarchy and cgroup we're dealing
3101 * with, and pin them so we can drop cgroup_mutex */
3102 mutex_lock(&cgroup_mutex);
3103 again:
3104 root = subsys->root;
3105 if (root == &rootnode) {
Paul Menage697f4162007-10-18 23:39:34 -07003106 mutex_unlock(&cgroup_mutex);
3107 return 0;
3108 }
Paul Menage697f4162007-10-18 23:39:34 -07003109
Paul Menage697f4162007-10-18 23:39:34 -07003110 /* Pin the hierarchy */
Li Zefan1404f062009-01-29 14:25:21 -08003111 if (!atomic_inc_not_zero(&root->sb->s_active)) {
Li Zefan7b574b72009-01-04 12:00:45 -08003112 /* We race with the final deactivate_super() */
3113 mutex_unlock(&cgroup_mutex);
3114 return 0;
3115 }
Paul Menage697f4162007-10-18 23:39:34 -07003116
Paul Menage817929e2007-10-18 23:39:36 -07003117 /* Keep the cgroup alive */
Li Zefan1404f062009-01-29 14:25:21 -08003118 task_lock(tsk);
3119 parent = task_cgroup(tsk, subsys->subsys_id);
3120 cg = tsk->cgroups;
Paul Menage817929e2007-10-18 23:39:36 -07003121 get_css_set(cg);
Lai Jiangshan104cbd52009-01-07 18:07:38 -08003122 task_unlock(tsk);
Li Zefan1404f062009-01-29 14:25:21 -08003123
Paul Menage697f4162007-10-18 23:39:34 -07003124 mutex_unlock(&cgroup_mutex);
3125
3126 /* Now do the VFS work to create a cgroup */
3127 inode = parent->dentry->d_inode;
3128
3129 /* Hold the parent directory mutex across this operation to
3130 * stop anyone else deleting the new cgroup */
3131 mutex_lock(&inode->i_mutex);
3132 dentry = lookup_one_len(nodename, parent->dentry, strlen(nodename));
3133 if (IS_ERR(dentry)) {
3134 printk(KERN_INFO
Diego Callejacfe36bd2007-11-14 16:58:54 -08003135 "cgroup: Couldn't allocate dentry for %s: %ld\n", nodename,
Paul Menage697f4162007-10-18 23:39:34 -07003136 PTR_ERR(dentry));
3137 ret = PTR_ERR(dentry);
3138 goto out_release;
3139 }
3140
3141 /* Create the cgroup directory, which also creates the cgroup */
Li Zefan75139b82009-01-07 18:07:33 -08003142 ret = vfs_mkdir(inode, dentry, 0755);
Paul Menagebd89aab2007-10-18 23:40:44 -07003143 child = __d_cgrp(dentry);
Paul Menage697f4162007-10-18 23:39:34 -07003144 dput(dentry);
3145 if (ret) {
3146 printk(KERN_INFO
3147 "Failed to create cgroup %s: %d\n", nodename,
3148 ret);
3149 goto out_release;
3150 }
3151
Paul Menage697f4162007-10-18 23:39:34 -07003152 /* The cgroup now exists. Retake cgroup_mutex and check
3153 * that we're still in the same state that we thought we
3154 * were. */
3155 mutex_lock(&cgroup_mutex);
3156 if ((root != subsys->root) ||
3157 (parent != task_cgroup(tsk, subsys->subsys_id))) {
3158 /* Aargh, we raced ... */
3159 mutex_unlock(&inode->i_mutex);
Paul Menage817929e2007-10-18 23:39:36 -07003160 put_css_set(cg);
Paul Menage697f4162007-10-18 23:39:34 -07003161
Li Zefan1404f062009-01-29 14:25:21 -08003162 deactivate_super(root->sb);
Paul Menage697f4162007-10-18 23:39:34 -07003163 /* The cgroup is still accessible in the VFS, but
3164 * we're not going to try to rmdir() it at this
3165 * point. */
3166 printk(KERN_INFO
3167 "Race in cgroup_clone() - leaking cgroup %s\n",
3168 nodename);
3169 goto again;
3170 }
3171
3172 /* do any required auto-setup */
3173 for_each_subsys(root, ss) {
3174 if (ss->post_clone)
3175 ss->post_clone(ss, child);
3176 }
3177
3178 /* All seems fine. Finish by moving the task into the new cgroup */
Cliff Wickman956db3c2008-02-07 00:14:43 -08003179 ret = cgroup_attach_task(child, tsk);
Paul Menage697f4162007-10-18 23:39:34 -07003180 mutex_unlock(&cgroup_mutex);
3181
3182 out_release:
3183 mutex_unlock(&inode->i_mutex);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003184
3185 mutex_lock(&cgroup_mutex);
Paul Menage817929e2007-10-18 23:39:36 -07003186 put_css_set(cg);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003187 mutex_unlock(&cgroup_mutex);
Li Zefan1404f062009-01-29 14:25:21 -08003188 deactivate_super(root->sb);
Paul Menage697f4162007-10-18 23:39:34 -07003189 return ret;
3190}
3191
Li Zefana043e3b2008-02-23 15:24:09 -08003192/**
Grzegorz Nosek313e9242009-04-02 16:57:23 -07003193 * cgroup_is_descendant - see if @cgrp is a descendant of @task's cgrp
Li Zefana043e3b2008-02-23 15:24:09 -08003194 * @cgrp: the cgroup in question
Grzegorz Nosek313e9242009-04-02 16:57:23 -07003195 * @task: the task in question
Li Zefana043e3b2008-02-23 15:24:09 -08003196 *
Grzegorz Nosek313e9242009-04-02 16:57:23 -07003197 * See if @cgrp is a descendant of @task's cgroup in the appropriate
3198 * hierarchy.
Paul Menage697f4162007-10-18 23:39:34 -07003199 *
3200 * If we are sending in dummytop, then presumably we are creating
3201 * the top cgroup in the subsystem.
3202 *
3203 * Called only by the ns (nsproxy) cgroup.
3204 */
Grzegorz Nosek313e9242009-04-02 16:57:23 -07003205int cgroup_is_descendant(const struct cgroup *cgrp, struct task_struct *task)
Paul Menage697f4162007-10-18 23:39:34 -07003206{
3207 int ret;
3208 struct cgroup *target;
3209 int subsys_id;
3210
Paul Menagebd89aab2007-10-18 23:40:44 -07003211 if (cgrp == dummytop)
Paul Menage697f4162007-10-18 23:39:34 -07003212 return 1;
3213
Paul Menagebd89aab2007-10-18 23:40:44 -07003214 get_first_subsys(cgrp, NULL, &subsys_id);
Grzegorz Nosek313e9242009-04-02 16:57:23 -07003215 target = task_cgroup(task, subsys_id);
Paul Menagebd89aab2007-10-18 23:40:44 -07003216 while (cgrp != target && cgrp!= cgrp->top_cgroup)
3217 cgrp = cgrp->parent;
3218 ret = (cgrp == target);
Paul Menage697f4162007-10-18 23:39:34 -07003219 return ret;
3220}
Paul Menage81a6a5c2007-10-18 23:39:38 -07003221
Paul Menagebd89aab2007-10-18 23:40:44 -07003222static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003223{
3224 /* All of these checks rely on RCU to keep the cgroup
3225 * structure alive */
Paul Menagebd89aab2007-10-18 23:40:44 -07003226 if (cgroup_is_releasable(cgrp) && !atomic_read(&cgrp->count)
3227 && list_empty(&cgrp->children) && !cgroup_has_css_refs(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07003228 /* Control Group is currently removeable. If it's not
3229 * already queued for a userspace notification, queue
3230 * it now */
3231 int need_schedule_work = 0;
3232 spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07003233 if (!cgroup_is_removed(cgrp) &&
3234 list_empty(&cgrp->release_list)) {
3235 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003236 need_schedule_work = 1;
3237 }
3238 spin_unlock(&release_list_lock);
3239 if (need_schedule_work)
3240 schedule_work(&release_agent_work);
3241 }
3242}
3243
3244void __css_put(struct cgroup_subsys_state *css)
3245{
Paul Menagebd89aab2007-10-18 23:40:44 -07003246 struct cgroup *cgrp = css->cgroup;
Paul Menage81a6a5c2007-10-18 23:39:38 -07003247 rcu_read_lock();
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003248 if (atomic_dec_return(&css->refcnt) == 1) {
3249 if (notify_on_release(cgrp)) {
3250 set_bit(CGRP_RELEASABLE, &cgrp->flags);
3251 check_for_release(cgrp);
3252 }
3253 cgroup_wakeup_rmdir_waiters(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003254 }
3255 rcu_read_unlock();
3256}
3257
3258/*
3259 * Notify userspace when a cgroup is released, by running the
3260 * configured release agent with the name of the cgroup (path
3261 * relative to the root of cgroup file system) as the argument.
3262 *
3263 * Most likely, this user command will try to rmdir this cgroup.
3264 *
3265 * This races with the possibility that some other task will be
3266 * attached to this cgroup before it is removed, or that some other
3267 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
3268 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
3269 * unused, and this cgroup will be reprieved from its death sentence,
3270 * to continue to serve a useful existence. Next time it's released,
3271 * we will get notified again, if it still has 'notify_on_release' set.
3272 *
3273 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
3274 * means only wait until the task is successfully execve()'d. The
3275 * separate release agent task is forked by call_usermodehelper(),
3276 * then control in this thread returns here, without waiting for the
3277 * release agent task. We don't bother to wait because the caller of
3278 * this routine has no use for the exit status of the release agent
3279 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07003280 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07003281static void cgroup_release_agent(struct work_struct *work)
3282{
3283 BUG_ON(work != &release_agent_work);
3284 mutex_lock(&cgroup_mutex);
3285 spin_lock(&release_list_lock);
3286 while (!list_empty(&release_list)) {
3287 char *argv[3], *envp[3];
3288 int i;
Paul Menagee788e062008-07-25 01:46:59 -07003289 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003290 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003291 struct cgroup,
3292 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07003293 list_del_init(&cgrp->release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003294 spin_unlock(&release_list_lock);
3295 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07003296 if (!pathbuf)
3297 goto continue_free;
3298 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
3299 goto continue_free;
3300 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
3301 if (!agentbuf)
3302 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07003303
3304 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07003305 argv[i++] = agentbuf;
3306 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07003307 argv[i] = NULL;
3308
3309 i = 0;
3310 /* minimal command environment */
3311 envp[i++] = "HOME=/";
3312 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
3313 envp[i] = NULL;
3314
3315 /* Drop the lock while we invoke the usermode helper,
3316 * since the exec could involve hitting disk and hence
3317 * be a slow process */
3318 mutex_unlock(&cgroup_mutex);
3319 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003320 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07003321 continue_free:
3322 kfree(pathbuf);
3323 kfree(agentbuf);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003324 spin_lock(&release_list_lock);
3325 }
3326 spin_unlock(&release_list_lock);
3327 mutex_unlock(&cgroup_mutex);
3328}
Paul Menage8bab8dd2008-04-04 14:29:57 -07003329
3330static int __init cgroup_disable(char *str)
3331{
3332 int i;
3333 char *token;
3334
3335 while ((token = strsep(&str, ",")) != NULL) {
3336 if (!*token)
3337 continue;
3338
3339 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3340 struct cgroup_subsys *ss = subsys[i];
3341
3342 if (!strcmp(token, ss->name)) {
3343 ss->disabled = 1;
3344 printk(KERN_INFO "Disabling %s control group"
3345 " subsystem\n", ss->name);
3346 break;
3347 }
3348 }
3349 }
3350 return 1;
3351}
3352__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07003353
3354/*
3355 * Functons for CSS ID.
3356 */
3357
3358/*
3359 *To get ID other than 0, this should be called when !cgroup_is_removed().
3360 */
3361unsigned short css_id(struct cgroup_subsys_state *css)
3362{
3363 struct css_id *cssid = rcu_dereference(css->id);
3364
3365 if (cssid)
3366 return cssid->id;
3367 return 0;
3368}
3369
3370unsigned short css_depth(struct cgroup_subsys_state *css)
3371{
3372 struct css_id *cssid = rcu_dereference(css->id);
3373
3374 if (cssid)
3375 return cssid->depth;
3376 return 0;
3377}
3378
3379bool css_is_ancestor(struct cgroup_subsys_state *child,
3380 struct cgroup_subsys_state *root)
3381{
3382 struct css_id *child_id = rcu_dereference(child->id);
3383 struct css_id *root_id = rcu_dereference(root->id);
3384
3385 if (!child_id || !root_id || (child_id->depth < root_id->depth))
3386 return false;
3387 return child_id->stack[root_id->depth] == root_id->id;
3388}
3389
3390static void __free_css_id_cb(struct rcu_head *head)
3391{
3392 struct css_id *id;
3393
3394 id = container_of(head, struct css_id, rcu_head);
3395 kfree(id);
3396}
3397
3398void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
3399{
3400 struct css_id *id = css->id;
3401 /* When this is called before css_id initialization, id can be NULL */
3402 if (!id)
3403 return;
3404
3405 BUG_ON(!ss->use_id);
3406
3407 rcu_assign_pointer(id->css, NULL);
3408 rcu_assign_pointer(css->id, NULL);
3409 spin_lock(&ss->id_lock);
3410 idr_remove(&ss->idr, id->id);
3411 spin_unlock(&ss->id_lock);
3412 call_rcu(&id->rcu_head, __free_css_id_cb);
3413}
3414
3415/*
3416 * This is called by init or create(). Then, calls to this function are
3417 * always serialized (By cgroup_mutex() at create()).
3418 */
3419
3420static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
3421{
3422 struct css_id *newid;
3423 int myid, error, size;
3424
3425 BUG_ON(!ss->use_id);
3426
3427 size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
3428 newid = kzalloc(size, GFP_KERNEL);
3429 if (!newid)
3430 return ERR_PTR(-ENOMEM);
3431 /* get id */
3432 if (unlikely(!idr_pre_get(&ss->idr, GFP_KERNEL))) {
3433 error = -ENOMEM;
3434 goto err_out;
3435 }
3436 spin_lock(&ss->id_lock);
3437 /* Don't use 0. allocates an ID of 1-65535 */
3438 error = idr_get_new_above(&ss->idr, newid, 1, &myid);
3439 spin_unlock(&ss->id_lock);
3440
3441 /* Returns error when there are no free spaces for new ID.*/
3442 if (error) {
3443 error = -ENOSPC;
3444 goto err_out;
3445 }
3446 if (myid > CSS_ID_MAX)
3447 goto remove_idr;
3448
3449 newid->id = myid;
3450 newid->depth = depth;
3451 return newid;
3452remove_idr:
3453 error = -ENOSPC;
3454 spin_lock(&ss->id_lock);
3455 idr_remove(&ss->idr, myid);
3456 spin_unlock(&ss->id_lock);
3457err_out:
3458 kfree(newid);
3459 return ERR_PTR(error);
3460
3461}
3462
3463static int __init cgroup_subsys_init_idr(struct cgroup_subsys *ss)
3464{
3465 struct css_id *newid;
3466 struct cgroup_subsys_state *rootcss;
3467
3468 spin_lock_init(&ss->id_lock);
3469 idr_init(&ss->idr);
3470
3471 rootcss = init_css_set.subsys[ss->subsys_id];
3472 newid = get_new_cssid(ss, 0);
3473 if (IS_ERR(newid))
3474 return PTR_ERR(newid);
3475
3476 newid->stack[0] = newid->id;
3477 newid->css = rootcss;
3478 rootcss->id = newid;
3479 return 0;
3480}
3481
3482static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent,
3483 struct cgroup *child)
3484{
3485 int subsys_id, i, depth = 0;
3486 struct cgroup_subsys_state *parent_css, *child_css;
3487 struct css_id *child_id, *parent_id = NULL;
3488
3489 subsys_id = ss->subsys_id;
3490 parent_css = parent->subsys[subsys_id];
3491 child_css = child->subsys[subsys_id];
3492 depth = css_depth(parent_css) + 1;
3493 parent_id = parent_css->id;
3494
3495 child_id = get_new_cssid(ss, depth);
3496 if (IS_ERR(child_id))
3497 return PTR_ERR(child_id);
3498
3499 for (i = 0; i < depth; i++)
3500 child_id->stack[i] = parent_id->stack[i];
3501 child_id->stack[depth] = child_id->id;
3502 /*
3503 * child_id->css pointer will be set after this cgroup is available
3504 * see cgroup_populate_dir()
3505 */
3506 rcu_assign_pointer(child_css->id, child_id);
3507
3508 return 0;
3509}
3510
3511/**
3512 * css_lookup - lookup css by id
3513 * @ss: cgroup subsys to be looked into.
3514 * @id: the id
3515 *
3516 * Returns pointer to cgroup_subsys_state if there is valid one with id.
3517 * NULL if not. Should be called under rcu_read_lock()
3518 */
3519struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
3520{
3521 struct css_id *cssid = NULL;
3522
3523 BUG_ON(!ss->use_id);
3524 cssid = idr_find(&ss->idr, id);
3525
3526 if (unlikely(!cssid))
3527 return NULL;
3528
3529 return rcu_dereference(cssid->css);
3530}
3531
3532/**
3533 * css_get_next - lookup next cgroup under specified hierarchy.
3534 * @ss: pointer to subsystem
3535 * @id: current position of iteration.
3536 * @root: pointer to css. search tree under this.
3537 * @foundid: position of found object.
3538 *
3539 * Search next css under the specified hierarchy of rootid. Calling under
3540 * rcu_read_lock() is necessary. Returns NULL if it reaches the end.
3541 */
3542struct cgroup_subsys_state *
3543css_get_next(struct cgroup_subsys *ss, int id,
3544 struct cgroup_subsys_state *root, int *foundid)
3545{
3546 struct cgroup_subsys_state *ret = NULL;
3547 struct css_id *tmp;
3548 int tmpid;
3549 int rootid = css_id(root);
3550 int depth = css_depth(root);
3551
3552 if (!rootid)
3553 return NULL;
3554
3555 BUG_ON(!ss->use_id);
3556 /* fill start point for scan */
3557 tmpid = id;
3558 while (1) {
3559 /*
3560 * scan next entry from bitmap(tree), tmpid is updated after
3561 * idr_get_next().
3562 */
3563 spin_lock(&ss->id_lock);
3564 tmp = idr_get_next(&ss->idr, &tmpid);
3565 spin_unlock(&ss->id_lock);
3566
3567 if (!tmp)
3568 break;
3569 if (tmp->depth >= depth && tmp->stack[depth] == rootid) {
3570 ret = rcu_dereference(tmp->css);
3571 if (ret) {
3572 *foundid = tmpid;
3573 break;
3574 }
3575 }
3576 /* continue to scan from next id */
3577 tmpid = tmpid + 1;
3578 }
3579 return ret;
3580}
3581