blob: cace83ddbcdcfdc2e5ac1c40d12da2482be4ccc5 [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>
Ingo Molnarc50cc752010-02-25 12:02:13 +010026#include <linux/module.h>
Paul Menagec6d57f32009-09-23 15:56:19 -070027#include <linux/ctype.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070028#include <linux/errno.h>
29#include <linux/fs.h>
30#include <linux/kernel.h>
31#include <linux/list.h>
32#include <linux/mm.h>
33#include <linux/mutex.h>
34#include <linux/mount.h>
35#include <linux/pagemap.h>
Paul Menagea4243162007-10-18 23:39:35 -070036#include <linux/proc_fs.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070037#include <linux/rcupdate.h>
38#include <linux/sched.h>
Paul Menage817929e2007-10-18 23:39:36 -070039#include <linux/backing-dev.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070040#include <linux/seq_file.h>
41#include <linux/slab.h>
42#include <linux/magic.h>
43#include <linux/spinlock.h>
44#include <linux/string.h>
Paul Menagebbcb81d2007-10-18 23:39:32 -070045#include <linux/sort.h>
Paul Menage81a6a5c2007-10-18 23:39:38 -070046#include <linux/kmod.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070047#include <linux/delayacct.h>
48#include <linux/cgroupstats.h>
Li Zefan472b1052008-04-29 01:00:11 -070049#include <linux/hash.h>
Al Viro3f8206d2008-07-26 03:46:43 -040050#include <linux/namei.h>
Alessio Igor Bogani337eb002009-05-12 15:10:54 +020051#include <linux/smp_lock.h>
Li Zefan096b7fe2009-07-29 15:04:04 -070052#include <linux/pid_namespace.h>
Paul Menage2c6ab6d2009-09-23 15:56:23 -070053#include <linux/idr.h>
Ben Blumd1d9fd32009-09-23 15:56:28 -070054#include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
Balbir Singh846c7bb2007-10-18 23:39:44 -070055
Paul Menageddbcc7e2007-10-18 23:39:30 -070056#include <asm/atomic.h>
57
Paul Menage81a6a5c2007-10-18 23:39:38 -070058static DEFINE_MUTEX(cgroup_mutex);
59
Paul Menageddbcc7e2007-10-18 23:39:30 -070060/* Generate an array of cgroup subsystem pointers */
61#define SUBSYS(_x) &_x ## _subsys,
62
63static struct cgroup_subsys *subsys[] = {
64#include <linux/cgroup_subsys.h>
65};
66
Paul Menagec6d57f32009-09-23 15:56:19 -070067#define MAX_CGROUP_ROOT_NAMELEN 64
68
Paul Menageddbcc7e2007-10-18 23:39:30 -070069/*
70 * A cgroupfs_root represents the root of a cgroup hierarchy,
71 * and may be associated with a superblock to form an active
72 * hierarchy
73 */
74struct cgroupfs_root {
75 struct super_block *sb;
76
77 /*
78 * The bitmask of subsystems intended to be attached to this
79 * hierarchy
80 */
81 unsigned long subsys_bits;
82
Paul Menage2c6ab6d2009-09-23 15:56:23 -070083 /* Unique id for this hierarchy. */
84 int hierarchy_id;
85
Paul Menageddbcc7e2007-10-18 23:39:30 -070086 /* The bitmask of subsystems currently attached to this hierarchy */
87 unsigned long actual_subsys_bits;
88
89 /* A list running through the attached subsystems */
90 struct list_head subsys_list;
91
92 /* The root cgroup for this hierarchy */
93 struct cgroup top_cgroup;
94
95 /* Tracks how many cgroups are currently defined in hierarchy.*/
96 int number_of_cgroups;
97
Li Zefane5f6a862009-01-07 18:07:41 -080098 /* A list running through the active hierarchies */
Paul Menageddbcc7e2007-10-18 23:39:30 -070099 struct list_head root_list;
100
101 /* Hierarchy-specific flags */
102 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700103
Paul Menagee788e062008-07-25 01:46:59 -0700104 /* The path to use for release notifications. */
Paul Menage81a6a5c2007-10-18 23:39:38 -0700105 char release_agent_path[PATH_MAX];
Paul Menagec6d57f32009-09-23 15:56:19 -0700106
107 /* The name for this hierarchy - may be empty */
108 char name[MAX_CGROUP_ROOT_NAMELEN];
Paul Menageddbcc7e2007-10-18 23:39:30 -0700109};
110
Paul Menageddbcc7e2007-10-18 23:39:30 -0700111/*
112 * The "rootnode" hierarchy is the "dummy hierarchy", reserved for the
113 * subsystems that are otherwise unattached - it never has more than a
114 * single cgroup, and all tasks are part of that cgroup.
115 */
116static struct cgroupfs_root rootnode;
117
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700118/*
119 * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when
120 * cgroup_subsys->use_id != 0.
121 */
122#define CSS_ID_MAX (65535)
123struct css_id {
124 /*
125 * The css to which this ID points. This pointer is set to valid value
126 * after cgroup is populated. If cgroup is removed, this will be NULL.
127 * This pointer is expected to be RCU-safe because destroy()
128 * is called after synchronize_rcu(). But for safe use, css_is_removed()
129 * css_tryget() should be used for avoiding race.
130 */
131 struct cgroup_subsys_state *css;
132 /*
133 * ID of this css.
134 */
135 unsigned short id;
136 /*
137 * Depth in hierarchy which this ID belongs to.
138 */
139 unsigned short depth;
140 /*
141 * ID is freed by RCU. (and lookup routine is RCU safe.)
142 */
143 struct rcu_head rcu_head;
144 /*
145 * Hierarchy of CSS ID belongs to.
146 */
147 unsigned short stack[0]; /* Array of Length (depth+1) */
148};
149
150
Paul Menageddbcc7e2007-10-18 23:39:30 -0700151/* The list of hierarchy roots */
152
153static LIST_HEAD(roots);
Paul Menage817929e2007-10-18 23:39:36 -0700154static int root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700155
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700156static DEFINE_IDA(hierarchy_ida);
157static int next_hierarchy_id;
158static DEFINE_SPINLOCK(hierarchy_id_lock);
159
Paul Menageddbcc7e2007-10-18 23:39:30 -0700160/* dummytop is a shorthand for the dummy hierarchy's top cgroup */
161#define dummytop (&rootnode.top_cgroup)
162
163/* This flag indicates whether tasks in the fork and exit paths should
Li Zefana043e3b2008-02-23 15:24:09 -0800164 * check for fork/exit handlers to call. This avoids us having to do
165 * extra work in the fork/exit path if none of the subsystems need to
166 * be called.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700167 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700168static int need_forkexit_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700169
Paul E. McKenneyd11c5632010-02-22 17:04:50 -0800170#ifdef CONFIG_PROVE_LOCKING
171int cgroup_lock_is_held(void)
172{
173 return lockdep_is_held(&cgroup_mutex);
174}
175#else /* #ifdef CONFIG_PROVE_LOCKING */
176int cgroup_lock_is_held(void)
177{
178 return mutex_is_locked(&cgroup_mutex);
179}
180#endif /* #else #ifdef CONFIG_PROVE_LOCKING */
181
182EXPORT_SYMBOL_GPL(cgroup_lock_is_held);
183
Paul Menageddbcc7e2007-10-18 23:39:30 -0700184/* convenient tests for these bits */
Paul Menagebd89aab2007-10-18 23:40:44 -0700185inline int cgroup_is_removed(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700186{
Paul Menagebd89aab2007-10-18 23:40:44 -0700187 return test_bit(CGRP_REMOVED, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700188}
189
190/* bits in struct cgroupfs_root flags field */
191enum {
192 ROOT_NOPREFIX, /* mounted subsystems have no named prefix */
193};
194
Adrian Bunke9685a02008-02-07 00:13:46 -0800195static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700196{
197 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700198 (1 << CGRP_RELEASABLE) |
199 (1 << CGRP_NOTIFY_ON_RELEASE);
200 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700201}
202
Adrian Bunke9685a02008-02-07 00:13:46 -0800203static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700204{
Paul Menagebd89aab2007-10-18 23:40:44 -0700205 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700206}
207
Paul Menageddbcc7e2007-10-18 23:39:30 -0700208/*
209 * for_each_subsys() allows you to iterate on each subsystem attached to
210 * an active hierarchy
211 */
212#define for_each_subsys(_root, _ss) \
213list_for_each_entry(_ss, &_root->subsys_list, sibling)
214
Li Zefane5f6a862009-01-07 18:07:41 -0800215/* for_each_active_root() allows you to iterate across the active hierarchies */
216#define for_each_active_root(_root) \
Paul Menageddbcc7e2007-10-18 23:39:30 -0700217list_for_each_entry(_root, &roots, root_list)
218
Paul Menage81a6a5c2007-10-18 23:39:38 -0700219/* the list of cgroups eligible for automatic release. Protected by
220 * release_list_lock */
221static LIST_HEAD(release_list);
222static DEFINE_SPINLOCK(release_list_lock);
223static void cgroup_release_agent(struct work_struct *work);
224static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700225static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700226
Paul Menage817929e2007-10-18 23:39:36 -0700227/* Link structure for associating css_set objects with cgroups */
228struct cg_cgroup_link {
229 /*
230 * List running through cg_cgroup_links associated with a
231 * cgroup, anchored on cgroup->css_sets
232 */
Paul Menagebd89aab2007-10-18 23:40:44 -0700233 struct list_head cgrp_link_list;
Paul Menage7717f7b2009-09-23 15:56:22 -0700234 struct cgroup *cgrp;
Paul Menage817929e2007-10-18 23:39:36 -0700235 /*
236 * List running through cg_cgroup_links pointing at a
237 * single css_set object, anchored on css_set->cg_links
238 */
239 struct list_head cg_link_list;
240 struct css_set *cg;
241};
242
243/* The default css_set - used by init and its children prior to any
244 * hierarchies being mounted. It contains a pointer to the root state
245 * for each subsystem. Also used to anchor the list of css_sets. Not
246 * reference-counted, to improve performance when child cgroups
247 * haven't been created.
248 */
249
250static struct css_set init_css_set;
251static struct cg_cgroup_link init_css_set_link;
252
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700253static int cgroup_subsys_init_idr(struct cgroup_subsys *ss);
254
Paul Menage817929e2007-10-18 23:39:36 -0700255/* css_set_lock protects the list of css_set objects, and the
256 * chain of tasks off each css_set. Nests outside task->alloc_lock
257 * due to cgroup_iter_start() */
258static DEFINE_RWLOCK(css_set_lock);
259static int css_set_count;
260
Paul Menage7717f7b2009-09-23 15:56:22 -0700261/*
262 * hash table for cgroup groups. This improves the performance to find
263 * an existing css_set. This hash doesn't (currently) take into
264 * account cgroups in empty hierarchies.
265 */
Li Zefan472b1052008-04-29 01:00:11 -0700266#define CSS_SET_HASH_BITS 7
267#define CSS_SET_TABLE_SIZE (1 << CSS_SET_HASH_BITS)
268static struct hlist_head css_set_table[CSS_SET_TABLE_SIZE];
269
270static struct hlist_head *css_set_hash(struct cgroup_subsys_state *css[])
271{
272 int i;
273 int index;
274 unsigned long tmp = 0UL;
275
276 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++)
277 tmp += (unsigned long)css[i];
278 tmp = (tmp >> 16) ^ tmp;
279
280 index = hash_long(tmp, CSS_SET_HASH_BITS);
281
282 return &css_set_table[index];
283}
284
Ben Blumc3783692009-09-23 15:56:29 -0700285static void free_css_set_rcu(struct rcu_head *obj)
286{
287 struct css_set *cg = container_of(obj, struct css_set, rcu_head);
288 kfree(cg);
289}
290
Paul Menage817929e2007-10-18 23:39:36 -0700291/* We don't maintain the lists running through each css_set to its
292 * task until after the first call to cgroup_iter_start(). This
293 * reduces the fork()/exit() overhead for people who have cgroups
294 * compiled into their kernel but not actually in use */
Li Zefan8947f9d2008-07-25 01:46:56 -0700295static int use_task_css_set_links __read_mostly;
Paul Menage817929e2007-10-18 23:39:36 -0700296
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700297static void __put_css_set(struct css_set *cg, int taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700298{
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700299 struct cg_cgroup_link *link;
300 struct cg_cgroup_link *saved_link;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700301 /*
302 * Ensure that the refcount doesn't hit zero while any readers
303 * can see it. Similar to atomic_dec_and_lock(), but for an
304 * rwlock
305 */
306 if (atomic_add_unless(&cg->refcount, -1, 1))
307 return;
308 write_lock(&css_set_lock);
309 if (!atomic_dec_and_test(&cg->refcount)) {
310 write_unlock(&css_set_lock);
311 return;
312 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700313
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700314 /* This css_set is dead. unlink it and release cgroup refcounts */
315 hlist_del(&cg->hlist);
316 css_set_count--;
317
318 list_for_each_entry_safe(link, saved_link, &cg->cg_links,
319 cg_link_list) {
320 struct cgroup *cgrp = link->cgrp;
321 list_del(&link->cg_link_list);
322 list_del(&link->cgrp_link_list);
Paul Menagebd89aab2007-10-18 23:40:44 -0700323 if (atomic_dec_and_test(&cgrp->count) &&
324 notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700325 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700326 set_bit(CGRP_RELEASABLE, &cgrp->flags);
327 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700328 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700329
330 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700331 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700332
333 write_unlock(&css_set_lock);
Ben Blumc3783692009-09-23 15:56:29 -0700334 call_rcu(&cg->rcu_head, free_css_set_rcu);
Paul Menage817929e2007-10-18 23:39:36 -0700335}
336
337/*
338 * refcounted get/put for css_set objects
339 */
340static inline void get_css_set(struct css_set *cg)
341{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700342 atomic_inc(&cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700343}
344
345static inline void put_css_set(struct css_set *cg)
346{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700347 __put_css_set(cg, 0);
Paul Menage817929e2007-10-18 23:39:36 -0700348}
349
Paul Menage81a6a5c2007-10-18 23:39:38 -0700350static inline void put_css_set_taskexit(struct css_set *cg)
351{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700352 __put_css_set(cg, 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700353}
354
Paul Menage817929e2007-10-18 23:39:36 -0700355/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700356 * compare_css_sets - helper function for find_existing_css_set().
357 * @cg: candidate css_set being tested
358 * @old_cg: existing css_set for a task
359 * @new_cgrp: cgroup that's being entered by the task
360 * @template: desired set of css pointers in css_set (pre-calculated)
361 *
362 * Returns true if "cg" matches "old_cg" except for the hierarchy
363 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
364 */
365static bool compare_css_sets(struct css_set *cg,
366 struct css_set *old_cg,
367 struct cgroup *new_cgrp,
368 struct cgroup_subsys_state *template[])
369{
370 struct list_head *l1, *l2;
371
372 if (memcmp(template, cg->subsys, sizeof(cg->subsys))) {
373 /* Not all subsystems matched */
374 return false;
375 }
376
377 /*
378 * Compare cgroup pointers in order to distinguish between
379 * different cgroups in heirarchies with no subsystems. We
380 * could get by with just this check alone (and skip the
381 * memcmp above) but on most setups the memcmp check will
382 * avoid the need for this more expensive check on almost all
383 * candidates.
384 */
385
386 l1 = &cg->cg_links;
387 l2 = &old_cg->cg_links;
388 while (1) {
389 struct cg_cgroup_link *cgl1, *cgl2;
390 struct cgroup *cg1, *cg2;
391
392 l1 = l1->next;
393 l2 = l2->next;
394 /* See if we reached the end - both lists are equal length. */
395 if (l1 == &cg->cg_links) {
396 BUG_ON(l2 != &old_cg->cg_links);
397 break;
398 } else {
399 BUG_ON(l2 == &old_cg->cg_links);
400 }
401 /* Locate the cgroups associated with these links. */
402 cgl1 = list_entry(l1, struct cg_cgroup_link, cg_link_list);
403 cgl2 = list_entry(l2, struct cg_cgroup_link, cg_link_list);
404 cg1 = cgl1->cgrp;
405 cg2 = cgl2->cgrp;
406 /* Hierarchies should be linked in the same order. */
407 BUG_ON(cg1->root != cg2->root);
408
409 /*
410 * If this hierarchy is the hierarchy of the cgroup
411 * that's changing, then we need to check that this
412 * css_set points to the new cgroup; if it's any other
413 * hierarchy, then this css_set should point to the
414 * same cgroup as the old css_set.
415 */
416 if (cg1->root == new_cgrp->root) {
417 if (cg1 != new_cgrp)
418 return false;
419 } else {
420 if (cg1 != cg2)
421 return false;
422 }
423 }
424 return true;
425}
426
427/*
Paul Menage817929e2007-10-18 23:39:36 -0700428 * find_existing_css_set() is a helper for
429 * find_css_set(), and checks to see whether an existing
Li Zefan472b1052008-04-29 01:00:11 -0700430 * css_set is suitable.
Paul Menage817929e2007-10-18 23:39:36 -0700431 *
432 * oldcg: the cgroup group that we're using before the cgroup
433 * transition
434 *
Paul Menagebd89aab2007-10-18 23:40:44 -0700435 * cgrp: the cgroup that we're moving into
Paul Menage817929e2007-10-18 23:39:36 -0700436 *
437 * template: location in which to build the desired set of subsystem
438 * state objects for the new cgroup group
439 */
Paul Menage817929e2007-10-18 23:39:36 -0700440static struct css_set *find_existing_css_set(
441 struct css_set *oldcg,
Paul Menagebd89aab2007-10-18 23:40:44 -0700442 struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -0700443 struct cgroup_subsys_state *template[])
444{
445 int i;
Paul Menagebd89aab2007-10-18 23:40:44 -0700446 struct cgroupfs_root *root = cgrp->root;
Li Zefan472b1052008-04-29 01:00:11 -0700447 struct hlist_head *hhead;
448 struct hlist_node *node;
449 struct css_set *cg;
Paul Menage817929e2007-10-18 23:39:36 -0700450
451 /* Built the set of subsystem state objects that we want to
452 * see in the new css_set */
453 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Li Zefan8d53d552008-02-23 15:24:11 -0800454 if (root->subsys_bits & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700455 /* Subsystem is in this hierarchy. So we want
456 * the subsystem state from the new
457 * cgroup */
Paul Menagebd89aab2007-10-18 23:40:44 -0700458 template[i] = cgrp->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700459 } else {
460 /* Subsystem is not in this hierarchy, so we
461 * don't want to change the subsystem state */
462 template[i] = oldcg->subsys[i];
463 }
464 }
465
Li Zefan472b1052008-04-29 01:00:11 -0700466 hhead = css_set_hash(template);
467 hlist_for_each_entry(cg, node, hhead, hlist) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700468 if (!compare_css_sets(cg, oldcg, cgrp, template))
469 continue;
470
471 /* This css_set matches what we need */
472 return cg;
Li Zefan472b1052008-04-29 01:00:11 -0700473 }
Paul Menage817929e2007-10-18 23:39:36 -0700474
475 /* No existing cgroup group matched */
476 return NULL;
477}
478
Paul Menage817929e2007-10-18 23:39:36 -0700479static void free_cg_links(struct list_head *tmp)
480{
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700481 struct cg_cgroup_link *link;
482 struct cg_cgroup_link *saved_link;
483
484 list_for_each_entry_safe(link, saved_link, tmp, cgrp_link_list) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700485 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -0700486 kfree(link);
487 }
488}
489
490/*
Li Zefan36553432008-07-29 22:33:19 -0700491 * allocate_cg_links() allocates "count" cg_cgroup_link structures
492 * and chains them on tmp through their cgrp_link_list fields. Returns 0 on
493 * success or a negative error
494 */
495static int allocate_cg_links(int count, struct list_head *tmp)
496{
497 struct cg_cgroup_link *link;
498 int i;
499 INIT_LIST_HEAD(tmp);
500 for (i = 0; i < count; i++) {
501 link = kmalloc(sizeof(*link), GFP_KERNEL);
502 if (!link) {
503 free_cg_links(tmp);
504 return -ENOMEM;
505 }
506 list_add(&link->cgrp_link_list, tmp);
507 }
508 return 0;
509}
510
Li Zefanc12f65d2009-01-07 18:07:42 -0800511/**
512 * link_css_set - a helper function to link a css_set to a cgroup
513 * @tmp_cg_links: cg_cgroup_link objects allocated by allocate_cg_links()
514 * @cg: the css_set to be linked
515 * @cgrp: the destination cgroup
516 */
517static void link_css_set(struct list_head *tmp_cg_links,
518 struct css_set *cg, struct cgroup *cgrp)
519{
520 struct cg_cgroup_link *link;
521
522 BUG_ON(list_empty(tmp_cg_links));
523 link = list_first_entry(tmp_cg_links, struct cg_cgroup_link,
524 cgrp_link_list);
525 link->cg = cg;
Paul Menage7717f7b2009-09-23 15:56:22 -0700526 link->cgrp = cgrp;
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700527 atomic_inc(&cgrp->count);
Li Zefanc12f65d2009-01-07 18:07:42 -0800528 list_move(&link->cgrp_link_list, &cgrp->css_sets);
Paul Menage7717f7b2009-09-23 15:56:22 -0700529 /*
530 * Always add links to the tail of the list so that the list
531 * is sorted by order of hierarchy creation
532 */
533 list_add_tail(&link->cg_link_list, &cg->cg_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800534}
535
Li Zefan36553432008-07-29 22:33:19 -0700536/*
Paul Menage817929e2007-10-18 23:39:36 -0700537 * find_css_set() takes an existing cgroup group and a
538 * cgroup object, and returns a css_set object that's
539 * equivalent to the old group, but with the given cgroup
540 * substituted into the appropriate hierarchy. Must be called with
541 * cgroup_mutex held
542 */
Paul Menage817929e2007-10-18 23:39:36 -0700543static struct css_set *find_css_set(
Paul Menagebd89aab2007-10-18 23:40:44 -0700544 struct css_set *oldcg, struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700545{
546 struct css_set *res;
547 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT];
Paul Menage817929e2007-10-18 23:39:36 -0700548
549 struct list_head tmp_cg_links;
Paul Menage817929e2007-10-18 23:39:36 -0700550
Li Zefan472b1052008-04-29 01:00:11 -0700551 struct hlist_head *hhead;
Paul Menage7717f7b2009-09-23 15:56:22 -0700552 struct cg_cgroup_link *link;
Li Zefan472b1052008-04-29 01:00:11 -0700553
Paul Menage817929e2007-10-18 23:39:36 -0700554 /* First see if we already have a cgroup group that matches
555 * the desired set */
Li Zefan7e9abd82008-07-25 01:46:54 -0700556 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -0700557 res = find_existing_css_set(oldcg, cgrp, template);
Paul Menage817929e2007-10-18 23:39:36 -0700558 if (res)
559 get_css_set(res);
Li Zefan7e9abd82008-07-25 01:46:54 -0700560 read_unlock(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -0700561
562 if (res)
563 return res;
564
565 res = kmalloc(sizeof(*res), GFP_KERNEL);
566 if (!res)
567 return NULL;
568
569 /* Allocate all the cg_cgroup_link objects that we'll need */
570 if (allocate_cg_links(root_count, &tmp_cg_links) < 0) {
571 kfree(res);
572 return NULL;
573 }
574
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700575 atomic_set(&res->refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -0700576 INIT_LIST_HEAD(&res->cg_links);
577 INIT_LIST_HEAD(&res->tasks);
Li Zefan472b1052008-04-29 01:00:11 -0700578 INIT_HLIST_NODE(&res->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700579
580 /* Copy the set of subsystem state objects generated in
581 * find_existing_css_set() */
582 memcpy(res->subsys, template, sizeof(res->subsys));
583
584 write_lock(&css_set_lock);
585 /* Add reference counts and links from the new css_set. */
Paul Menage7717f7b2009-09-23 15:56:22 -0700586 list_for_each_entry(link, &oldcg->cg_links, cg_link_list) {
587 struct cgroup *c = link->cgrp;
588 if (c->root == cgrp->root)
589 c = cgrp;
590 link_css_set(&tmp_cg_links, res, c);
591 }
Paul Menage817929e2007-10-18 23:39:36 -0700592
593 BUG_ON(!list_empty(&tmp_cg_links));
594
Paul Menage817929e2007-10-18 23:39:36 -0700595 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700596
597 /* Add this cgroup group to the hash table */
598 hhead = css_set_hash(res->subsys);
599 hlist_add_head(&res->hlist, hhead);
600
Paul Menage817929e2007-10-18 23:39:36 -0700601 write_unlock(&css_set_lock);
602
603 return res;
Paul Menageb4f48b62007-10-18 23:39:33 -0700604}
605
Paul Menageddbcc7e2007-10-18 23:39:30 -0700606/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700607 * Return the cgroup for "task" from the given hierarchy. Must be
608 * called with cgroup_mutex held.
609 */
610static struct cgroup *task_cgroup_from_root(struct task_struct *task,
611 struct cgroupfs_root *root)
612{
613 struct css_set *css;
614 struct cgroup *res = NULL;
615
616 BUG_ON(!mutex_is_locked(&cgroup_mutex));
617 read_lock(&css_set_lock);
618 /*
619 * No need to lock the task - since we hold cgroup_mutex the
620 * task can't change groups, so the only thing that can happen
621 * is that it exits and its css is set back to init_css_set.
622 */
623 css = task->cgroups;
624 if (css == &init_css_set) {
625 res = &root->top_cgroup;
626 } else {
627 struct cg_cgroup_link *link;
628 list_for_each_entry(link, &css->cg_links, cg_link_list) {
629 struct cgroup *c = link->cgrp;
630 if (c->root == root) {
631 res = c;
632 break;
633 }
634 }
635 }
636 read_unlock(&css_set_lock);
637 BUG_ON(!res);
638 return res;
639}
640
641/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700642 * There is one global cgroup mutex. We also require taking
643 * task_lock() when dereferencing a task's cgroup subsys pointers.
644 * See "The task_lock() exception", at the end of this comment.
645 *
646 * A task must hold cgroup_mutex to modify cgroups.
647 *
648 * Any task can increment and decrement the count field without lock.
649 * So in general, code holding cgroup_mutex can't rely on the count
650 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800651 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700652 * means that no tasks are currently attached, therefore there is no
653 * way a task attached to that cgroup can fork (the other way to
654 * increment the count). So code holding cgroup_mutex can safely
655 * assume that if the count is zero, it will stay zero. Similarly, if
656 * a task holds cgroup_mutex on a cgroup with zero count, it
657 * knows that the cgroup won't be removed, as cgroup_rmdir()
658 * needs that mutex.
659 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700660 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
661 * (usually) take cgroup_mutex. These are the two most performance
662 * critical pieces of code here. The exception occurs on cgroup_exit(),
663 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
664 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800665 * to the release agent with the name of the cgroup (path relative to
666 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700667 *
668 * A cgroup can only be deleted if both its 'count' of using tasks
669 * is zero, and its list of 'children' cgroups is empty. Since all
670 * tasks in the system use _some_ cgroup, and since there is always at
671 * least one task in the system (init, pid == 1), therefore, top_cgroup
672 * always has either children cgroups and/or using tasks. So we don't
673 * need a special hack to ensure that top_cgroup cannot be deleted.
674 *
675 * The task_lock() exception
676 *
677 * The need for this exception arises from the action of
Cliff Wickman956db3c2008-02-07 00:14:43 -0800678 * cgroup_attach_task(), which overwrites one tasks cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800679 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700680 * several performance critical places that need to reference
681 * task->cgroup without the expense of grabbing a system global
682 * mutex. Therefore except as noted below, when dereferencing or, as
Cliff Wickman956db3c2008-02-07 00:14:43 -0800683 * in cgroup_attach_task(), modifying a task'ss cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700684 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
685 * the task_struct routinely used for such matters.
686 *
687 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800688 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700689 */
690
Paul Menageddbcc7e2007-10-18 23:39:30 -0700691/**
692 * cgroup_lock - lock out any changes to cgroup structures
693 *
694 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700695void cgroup_lock(void)
696{
697 mutex_lock(&cgroup_mutex);
698}
699
700/**
701 * cgroup_unlock - release lock on cgroup changes
702 *
703 * Undo the lock taken in a previous cgroup_lock() call.
704 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700705void cgroup_unlock(void)
706{
707 mutex_unlock(&cgroup_mutex);
708}
709
710/*
711 * A couple of forward declarations required, due to cyclic reference loop:
712 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
713 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
714 * -> cgroup_mkdir.
715 */
716
717static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode);
718static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -0700719static int cgroup_populate_dir(struct cgroup *cgrp);
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700720static const struct inode_operations cgroup_dir_inode_operations;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700721static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700722
723static struct backing_dev_info cgroup_backing_dev_info = {
Jens Axboed9938312009-06-12 14:45:52 +0200724 .name = "cgroup",
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700725 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Paul Menagea4243162007-10-18 23:39:35 -0700726};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700727
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700728static int alloc_css_id(struct cgroup_subsys *ss,
729 struct cgroup *parent, struct cgroup *child);
730
Paul Menageddbcc7e2007-10-18 23:39:30 -0700731static struct inode *cgroup_new_inode(mode_t mode, struct super_block *sb)
732{
733 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700734
735 if (inode) {
736 inode->i_mode = mode;
David Howells76aac0e2008-11-14 10:39:12 +1100737 inode->i_uid = current_fsuid();
738 inode->i_gid = current_fsgid();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700739 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
740 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
741 }
742 return inode;
743}
744
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800745/*
746 * Call subsys's pre_destroy handler.
747 * This is called before css refcnt check.
748 */
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700749static int cgroup_call_pre_destroy(struct cgroup *cgrp)
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800750{
751 struct cgroup_subsys *ss;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700752 int ret = 0;
753
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800754 for_each_subsys(cgrp->root, ss)
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700755 if (ss->pre_destroy) {
756 ret = ss->pre_destroy(ss, cgrp);
757 if (ret)
758 break;
759 }
760 return ret;
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800761}
762
Paul Menagea47295e2009-01-07 18:07:44 -0800763static void free_cgroup_rcu(struct rcu_head *obj)
764{
765 struct cgroup *cgrp = container_of(obj, struct cgroup, rcu_head);
766
767 kfree(cgrp);
768}
769
Paul Menageddbcc7e2007-10-18 23:39:30 -0700770static void cgroup_diput(struct dentry *dentry, struct inode *inode)
771{
772 /* is dentry a directory ? if so, kfree() associated cgroup */
773 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700774 struct cgroup *cgrp = dentry->d_fsdata;
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800775 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -0700776 BUG_ON(!(cgroup_is_removed(cgrp)));
Paul Menage81a6a5c2007-10-18 23:39:38 -0700777 /* It's possible for external users to be holding css
778 * reference counts on a cgroup; css_put() needs to
779 * be able to access the cgroup after decrementing
780 * the reference count in order to know if it needs to
781 * queue the cgroup to be handled by the release
782 * agent */
783 synchronize_rcu();
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800784
785 mutex_lock(&cgroup_mutex);
786 /*
787 * Release the subsystem state objects.
788 */
Li Zefan75139b82009-01-07 18:07:33 -0800789 for_each_subsys(cgrp->root, ss)
790 ss->destroy(ss, cgrp);
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800791
792 cgrp->root->number_of_cgroups--;
793 mutex_unlock(&cgroup_mutex);
794
Paul Menagea47295e2009-01-07 18:07:44 -0800795 /*
796 * Drop the active superblock reference that we took when we
797 * created the cgroup
798 */
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800799 deactivate_super(cgrp->root->sb);
800
Ben Blum72a8cb32009-09-23 15:56:27 -0700801 /*
802 * if we're getting rid of the cgroup, refcount should ensure
803 * that there are no pidlists left.
804 */
805 BUG_ON(!list_empty(&cgrp->pidlists));
806
Paul Menagea47295e2009-01-07 18:07:44 -0800807 call_rcu(&cgrp->rcu_head, free_cgroup_rcu);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700808 }
809 iput(inode);
810}
811
812static void remove_dir(struct dentry *d)
813{
814 struct dentry *parent = dget(d->d_parent);
815
816 d_delete(d);
817 simple_rmdir(parent->d_inode, d);
818 dput(parent);
819}
820
821static void cgroup_clear_directory(struct dentry *dentry)
822{
823 struct list_head *node;
824
825 BUG_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
826 spin_lock(&dcache_lock);
827 node = dentry->d_subdirs.next;
828 while (node != &dentry->d_subdirs) {
829 struct dentry *d = list_entry(node, struct dentry, d_u.d_child);
830 list_del_init(node);
831 if (d->d_inode) {
832 /* This should never be called on a cgroup
833 * directory with child cgroups */
834 BUG_ON(d->d_inode->i_mode & S_IFDIR);
835 d = dget_locked(d);
836 spin_unlock(&dcache_lock);
837 d_delete(d);
838 simple_unlink(dentry->d_inode, d);
839 dput(d);
840 spin_lock(&dcache_lock);
841 }
842 node = dentry->d_subdirs.next;
843 }
844 spin_unlock(&dcache_lock);
845}
846
847/*
848 * NOTE : the dentry must have been dget()'ed
849 */
850static void cgroup_d_remove_dir(struct dentry *dentry)
851{
852 cgroup_clear_directory(dentry);
853
854 spin_lock(&dcache_lock);
855 list_del_init(&dentry->d_u.d_child);
856 spin_unlock(&dcache_lock);
857 remove_dir(dentry);
858}
859
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700860/*
861 * A queue for waiters to do rmdir() cgroup. A tasks will sleep when
862 * cgroup->count == 0 && list_empty(&cgroup->children) && subsys has some
863 * reference to css->refcnt. In general, this refcnt is expected to goes down
864 * to zero, soon.
865 *
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -0700866 * CGRP_WAIT_ON_RMDIR flag is set under cgroup's inode->i_mutex;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700867 */
868DECLARE_WAIT_QUEUE_HEAD(cgroup_rmdir_waitq);
869
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -0700870static void cgroup_wakeup_rmdir_waiter(struct cgroup *cgrp)
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700871{
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -0700872 if (unlikely(test_and_clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags)))
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700873 wake_up_all(&cgroup_rmdir_waitq);
874}
875
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -0700876void cgroup_exclude_rmdir(struct cgroup_subsys_state *css)
877{
878 css_get(css);
879}
880
881void cgroup_release_and_wakeup_rmdir(struct cgroup_subsys_state *css)
882{
883 cgroup_wakeup_rmdir_waiter(css->cgroup);
884 css_put(css);
885}
886
887
Paul Menageddbcc7e2007-10-18 23:39:30 -0700888static int rebind_subsystems(struct cgroupfs_root *root,
889 unsigned long final_bits)
890{
891 unsigned long added_bits, removed_bits;
Paul Menagebd89aab2007-10-18 23:40:44 -0700892 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700893 int i;
894
895 removed_bits = root->actual_subsys_bits & ~final_bits;
896 added_bits = final_bits & ~root->actual_subsys_bits;
897 /* Check that any added subsystems are currently free */
898 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Li Zefan8d53d552008-02-23 15:24:11 -0800899 unsigned long bit = 1UL << i;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700900 struct cgroup_subsys *ss = subsys[i];
901 if (!(bit & added_bits))
902 continue;
903 if (ss->root != &rootnode) {
904 /* Subsystem isn't free */
905 return -EBUSY;
906 }
907 }
908
909 /* Currently we don't handle adding/removing subsystems when
910 * any child cgroups exist. This is theoretically supportable
911 * but involves complex error handling, so it's being left until
912 * later */
Paul Menage307257c2008-12-15 13:54:22 -0800913 if (root->number_of_cgroups > 1)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700914 return -EBUSY;
915
916 /* Process each subsystem */
917 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
918 struct cgroup_subsys *ss = subsys[i];
919 unsigned long bit = 1UL << i;
920 if (bit & added_bits) {
921 /* We're binding this subsystem to this hierarchy */
Paul Menagebd89aab2007-10-18 23:40:44 -0700922 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700923 BUG_ON(!dummytop->subsys[i]);
924 BUG_ON(dummytop->subsys[i]->cgroup != dummytop);
Paul Menage999cd8a2009-01-07 18:08:36 -0800925 mutex_lock(&ss->hierarchy_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -0700926 cgrp->subsys[i] = dummytop->subsys[i];
927 cgrp->subsys[i]->cgroup = cgrp;
Li Zefan33a68ac2009-01-07 18:07:42 -0800928 list_move(&ss->sibling, &root->subsys_list);
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -0800929 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700930 if (ss->bind)
Paul Menagebd89aab2007-10-18 23:40:44 -0700931 ss->bind(ss, cgrp);
Paul Menage999cd8a2009-01-07 18:08:36 -0800932 mutex_unlock(&ss->hierarchy_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700933 } else if (bit & removed_bits) {
934 /* We're removing this subsystem */
Paul Menagebd89aab2007-10-18 23:40:44 -0700935 BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]);
936 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
Paul Menage999cd8a2009-01-07 18:08:36 -0800937 mutex_lock(&ss->hierarchy_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700938 if (ss->bind)
939 ss->bind(ss, dummytop);
940 dummytop->subsys[i]->cgroup = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -0700941 cgrp->subsys[i] = NULL;
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -0800942 subsys[i]->root = &rootnode;
Li Zefan33a68ac2009-01-07 18:07:42 -0800943 list_move(&ss->sibling, &rootnode.subsys_list);
Paul Menage999cd8a2009-01-07 18:08:36 -0800944 mutex_unlock(&ss->hierarchy_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700945 } else if (bit & final_bits) {
946 /* Subsystem state should already exist */
Paul Menagebd89aab2007-10-18 23:40:44 -0700947 BUG_ON(!cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700948 } else {
949 /* Subsystem state shouldn't exist */
Paul Menagebd89aab2007-10-18 23:40:44 -0700950 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700951 }
952 }
953 root->subsys_bits = root->actual_subsys_bits = final_bits;
954 synchronize_rcu();
955
956 return 0;
957}
958
959static int cgroup_show_options(struct seq_file *seq, struct vfsmount *vfs)
960{
961 struct cgroupfs_root *root = vfs->mnt_sb->s_fs_info;
962 struct cgroup_subsys *ss;
963
964 mutex_lock(&cgroup_mutex);
965 for_each_subsys(root, ss)
966 seq_printf(seq, ",%s", ss->name);
967 if (test_bit(ROOT_NOPREFIX, &root->flags))
968 seq_puts(seq, ",noprefix");
Paul Menage81a6a5c2007-10-18 23:39:38 -0700969 if (strlen(root->release_agent_path))
970 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Paul Menagec6d57f32009-09-23 15:56:19 -0700971 if (strlen(root->name))
972 seq_printf(seq, ",name=%s", root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700973 mutex_unlock(&cgroup_mutex);
974 return 0;
975}
976
977struct cgroup_sb_opts {
978 unsigned long subsys_bits;
979 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700980 char *release_agent;
Paul Menagec6d57f32009-09-23 15:56:19 -0700981 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700982 /* User explicitly requested empty subsystem */
983 bool none;
Paul Menagec6d57f32009-09-23 15:56:19 -0700984
985 struct cgroupfs_root *new_root;
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700986
Paul Menageddbcc7e2007-10-18 23:39:30 -0700987};
988
989/* Convert a hierarchy specifier into a bitmask of subsystems and
990 * flags. */
991static int parse_cgroupfs_options(char *data,
992 struct cgroup_sb_opts *opts)
993{
994 char *token, *o = data ?: "all";
Li Zefanf9ab5b52009-06-17 16:26:33 -0700995 unsigned long mask = (unsigned long)-1;
996
997#ifdef CONFIG_CPUSETS
998 mask = ~(1UL << cpuset_subsys_id);
999#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001000
Paul Menagec6d57f32009-09-23 15:56:19 -07001001 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001002
1003 while ((token = strsep(&o, ",")) != NULL) {
1004 if (!*token)
1005 return -EINVAL;
1006 if (!strcmp(token, "all")) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07001007 /* Add all non-disabled subsystems */
1008 int i;
1009 opts->subsys_bits = 0;
1010 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1011 struct cgroup_subsys *ss = subsys[i];
1012 if (!ss->disabled)
1013 opts->subsys_bits |= 1ul << i;
1014 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001015 } else if (!strcmp(token, "none")) {
1016 /* Explicitly have no subsystems */
1017 opts->none = true;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001018 } else if (!strcmp(token, "noprefix")) {
1019 set_bit(ROOT_NOPREFIX, &opts->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001020 } else if (!strncmp(token, "release_agent=", 14)) {
1021 /* Specifying two release agents is forbidden */
1022 if (opts->release_agent)
1023 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001024 opts->release_agent =
1025 kstrndup(token + 14, PATH_MAX, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001026 if (!opts->release_agent)
1027 return -ENOMEM;
Paul Menagec6d57f32009-09-23 15:56:19 -07001028 } else if (!strncmp(token, "name=", 5)) {
1029 int i;
1030 const char *name = token + 5;
1031 /* Can't specify an empty name */
1032 if (!strlen(name))
1033 return -EINVAL;
1034 /* Must match [\w.-]+ */
1035 for (i = 0; i < strlen(name); i++) {
1036 char c = name[i];
1037 if (isalnum(c))
1038 continue;
1039 if ((c == '.') || (c == '-') || (c == '_'))
1040 continue;
1041 return -EINVAL;
1042 }
1043 /* Specifying two names is forbidden */
1044 if (opts->name)
1045 return -EINVAL;
1046 opts->name = kstrndup(name,
1047 MAX_CGROUP_ROOT_NAMELEN,
1048 GFP_KERNEL);
1049 if (!opts->name)
1050 return -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001051 } else {
1052 struct cgroup_subsys *ss;
1053 int i;
1054 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1055 ss = subsys[i];
1056 if (!strcmp(token, ss->name)) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07001057 if (!ss->disabled)
1058 set_bit(i, &opts->subsys_bits);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001059 break;
1060 }
1061 }
1062 if (i == CGROUP_SUBSYS_COUNT)
1063 return -ENOENT;
1064 }
1065 }
1066
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001067 /* Consistency checks */
1068
Li Zefanf9ab5b52009-06-17 16:26:33 -07001069 /*
1070 * Option noprefix was introduced just for backward compatibility
1071 * with the old cpuset, so we allow noprefix only if mounting just
1072 * the cpuset subsystem.
1073 */
1074 if (test_bit(ROOT_NOPREFIX, &opts->flags) &&
1075 (opts->subsys_bits & mask))
1076 return -EINVAL;
1077
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001078
1079 /* Can't specify "none" and some subsystems */
1080 if (opts->subsys_bits && opts->none)
1081 return -EINVAL;
1082
1083 /*
1084 * We either have to specify by name or by subsystems. (So all
1085 * empty hierarchies must have a name).
1086 */
Paul Menagec6d57f32009-09-23 15:56:19 -07001087 if (!opts->subsys_bits && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001088 return -EINVAL;
1089
1090 return 0;
1091}
1092
1093static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1094{
1095 int ret = 0;
1096 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001097 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001098 struct cgroup_sb_opts opts;
1099
Alessio Igor Bogani337eb002009-05-12 15:10:54 +02001100 lock_kernel();
Paul Menagebd89aab2007-10-18 23:40:44 -07001101 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001102 mutex_lock(&cgroup_mutex);
1103
1104 /* See what subsystems are wanted */
1105 ret = parse_cgroupfs_options(data, &opts);
1106 if (ret)
1107 goto out_unlock;
1108
1109 /* Don't allow flags to change at remount */
1110 if (opts.flags != root->flags) {
1111 ret = -EINVAL;
1112 goto out_unlock;
1113 }
1114
Paul Menagec6d57f32009-09-23 15:56:19 -07001115 /* Don't allow name to change at remount */
1116 if (opts.name && strcmp(opts.name, root->name)) {
1117 ret = -EINVAL;
1118 goto out_unlock;
1119 }
1120
Paul Menageddbcc7e2007-10-18 23:39:30 -07001121 ret = rebind_subsystems(root, opts.subsys_bits);
Li Zefan0670e082009-04-02 16:57:30 -07001122 if (ret)
1123 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001124
1125 /* (re)populate subsystem files */
Li Zefan0670e082009-04-02 16:57:30 -07001126 cgroup_populate_dir(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001127
Paul Menage81a6a5c2007-10-18 23:39:38 -07001128 if (opts.release_agent)
1129 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001130 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001131 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001132 kfree(opts.name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001133 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07001134 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Alessio Igor Bogani337eb002009-05-12 15:10:54 +02001135 unlock_kernel();
Paul Menageddbcc7e2007-10-18 23:39:30 -07001136 return ret;
1137}
1138
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07001139static const struct super_operations cgroup_ops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001140 .statfs = simple_statfs,
1141 .drop_inode = generic_delete_inode,
1142 .show_options = cgroup_show_options,
1143 .remount_fs = cgroup_remount,
1144};
1145
Paul Menagecc31edc2008-10-18 20:28:04 -07001146static void init_cgroup_housekeeping(struct cgroup *cgrp)
1147{
1148 INIT_LIST_HEAD(&cgrp->sibling);
1149 INIT_LIST_HEAD(&cgrp->children);
1150 INIT_LIST_HEAD(&cgrp->css_sets);
1151 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001152 INIT_LIST_HEAD(&cgrp->pidlists);
1153 mutex_init(&cgrp->pidlist_mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07001154}
Paul Menagec6d57f32009-09-23 15:56:19 -07001155
Paul Menageddbcc7e2007-10-18 23:39:30 -07001156static void init_cgroup_root(struct cgroupfs_root *root)
1157{
Paul Menagebd89aab2007-10-18 23:40:44 -07001158 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001159 INIT_LIST_HEAD(&root->subsys_list);
1160 INIT_LIST_HEAD(&root->root_list);
1161 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -07001162 cgrp->root = root;
1163 cgrp->top_cgroup = cgrp;
Paul Menagecc31edc2008-10-18 20:28:04 -07001164 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001165}
1166
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001167static bool init_root_id(struct cgroupfs_root *root)
1168{
1169 int ret = 0;
1170
1171 do {
1172 if (!ida_pre_get(&hierarchy_ida, GFP_KERNEL))
1173 return false;
1174 spin_lock(&hierarchy_id_lock);
1175 /* Try to allocate the next unused ID */
1176 ret = ida_get_new_above(&hierarchy_ida, next_hierarchy_id,
1177 &root->hierarchy_id);
1178 if (ret == -ENOSPC)
1179 /* Try again starting from 0 */
1180 ret = ida_get_new(&hierarchy_ida, &root->hierarchy_id);
1181 if (!ret) {
1182 next_hierarchy_id = root->hierarchy_id + 1;
1183 } else if (ret != -EAGAIN) {
1184 /* Can only get here if the 31-bit IDR is full ... */
1185 BUG_ON(ret);
1186 }
1187 spin_unlock(&hierarchy_id_lock);
1188 } while (ret);
1189 return true;
1190}
1191
Paul Menageddbcc7e2007-10-18 23:39:30 -07001192static int cgroup_test_super(struct super_block *sb, void *data)
1193{
Paul Menagec6d57f32009-09-23 15:56:19 -07001194 struct cgroup_sb_opts *opts = data;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001195 struct cgroupfs_root *root = sb->s_fs_info;
1196
Paul Menagec6d57f32009-09-23 15:56:19 -07001197 /* If we asked for a name then it must match */
1198 if (opts->name && strcmp(opts->name, root->name))
1199 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001200
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001201 /*
1202 * If we asked for subsystems (or explicitly for no
1203 * subsystems) then they must match
1204 */
1205 if ((opts->subsys_bits || opts->none)
1206 && (opts->subsys_bits != root->subsys_bits))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001207 return 0;
1208
1209 return 1;
1210}
1211
Paul Menagec6d57f32009-09-23 15:56:19 -07001212static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1213{
1214 struct cgroupfs_root *root;
1215
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001216 if (!opts->subsys_bits && !opts->none)
Paul Menagec6d57f32009-09-23 15:56:19 -07001217 return NULL;
1218
1219 root = kzalloc(sizeof(*root), GFP_KERNEL);
1220 if (!root)
1221 return ERR_PTR(-ENOMEM);
1222
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001223 if (!init_root_id(root)) {
1224 kfree(root);
1225 return ERR_PTR(-ENOMEM);
1226 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001227 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001228
Paul Menagec6d57f32009-09-23 15:56:19 -07001229 root->subsys_bits = opts->subsys_bits;
1230 root->flags = opts->flags;
1231 if (opts->release_agent)
1232 strcpy(root->release_agent_path, opts->release_agent);
1233 if (opts->name)
1234 strcpy(root->name, opts->name);
1235 return root;
1236}
1237
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001238static void cgroup_drop_root(struct cgroupfs_root *root)
1239{
1240 if (!root)
1241 return;
1242
1243 BUG_ON(!root->hierarchy_id);
1244 spin_lock(&hierarchy_id_lock);
1245 ida_remove(&hierarchy_ida, root->hierarchy_id);
1246 spin_unlock(&hierarchy_id_lock);
1247 kfree(root);
1248}
1249
Paul Menageddbcc7e2007-10-18 23:39:30 -07001250static int cgroup_set_super(struct super_block *sb, void *data)
1251{
1252 int ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001253 struct cgroup_sb_opts *opts = data;
1254
1255 /* If we don't have a new root, we can't set up a new sb */
1256 if (!opts->new_root)
1257 return -EINVAL;
1258
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001259 BUG_ON(!opts->subsys_bits && !opts->none);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001260
1261 ret = set_anon_super(sb, NULL);
1262 if (ret)
1263 return ret;
1264
Paul Menagec6d57f32009-09-23 15:56:19 -07001265 sb->s_fs_info = opts->new_root;
1266 opts->new_root->sb = sb;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001267
1268 sb->s_blocksize = PAGE_CACHE_SIZE;
1269 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1270 sb->s_magic = CGROUP_SUPER_MAGIC;
1271 sb->s_op = &cgroup_ops;
1272
1273 return 0;
1274}
1275
1276static int cgroup_get_rootdir(struct super_block *sb)
1277{
1278 struct inode *inode =
1279 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
1280 struct dentry *dentry;
1281
1282 if (!inode)
1283 return -ENOMEM;
1284
Paul Menageddbcc7e2007-10-18 23:39:30 -07001285 inode->i_fop = &simple_dir_operations;
1286 inode->i_op = &cgroup_dir_inode_operations;
1287 /* directories start off with i_nlink == 2 (for "." entry) */
1288 inc_nlink(inode);
1289 dentry = d_alloc_root(inode);
1290 if (!dentry) {
1291 iput(inode);
1292 return -ENOMEM;
1293 }
1294 sb->s_root = dentry;
1295 return 0;
1296}
1297
1298static int cgroup_get_sb(struct file_system_type *fs_type,
1299 int flags, const char *unused_dev_name,
1300 void *data, struct vfsmount *mnt)
1301{
1302 struct cgroup_sb_opts opts;
Paul Menagec6d57f32009-09-23 15:56:19 -07001303 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001304 int ret = 0;
1305 struct super_block *sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001306 struct cgroupfs_root *new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001307
1308 /* First find the desired set of subsystems */
1309 ret = parse_cgroupfs_options(data, &opts);
Paul Menagec6d57f32009-09-23 15:56:19 -07001310 if (ret)
1311 goto out_err;
1312
1313 /*
1314 * Allocate a new cgroup root. We may not need it if we're
1315 * reusing an existing hierarchy.
1316 */
1317 new_root = cgroup_root_from_opts(&opts);
1318 if (IS_ERR(new_root)) {
1319 ret = PTR_ERR(new_root);
1320 goto out_err;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001321 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001322 opts.new_root = new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001323
Paul Menagec6d57f32009-09-23 15:56:19 -07001324 /* Locate an existing or new sb for this hierarchy */
1325 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001326 if (IS_ERR(sb)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001327 ret = PTR_ERR(sb);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001328 cgroup_drop_root(opts.new_root);
Paul Menagec6d57f32009-09-23 15:56:19 -07001329 goto out_err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001330 }
1331
Paul Menagec6d57f32009-09-23 15:56:19 -07001332 root = sb->s_fs_info;
1333 BUG_ON(!root);
1334 if (root == opts.new_root) {
1335 /* We used the new root structure, so this is a new hierarchy */
1336 struct list_head tmp_cg_links;
Li Zefanc12f65d2009-01-07 18:07:42 -08001337 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menage817929e2007-10-18 23:39:36 -07001338 struct inode *inode;
Paul Menagec6d57f32009-09-23 15:56:19 -07001339 struct cgroupfs_root *existing_root;
Li Zefan28fd5df2008-04-29 01:00:13 -07001340 int i;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001341
1342 BUG_ON(sb->s_root != NULL);
1343
1344 ret = cgroup_get_rootdir(sb);
1345 if (ret)
1346 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001347 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001348
Paul Menage817929e2007-10-18 23:39:36 -07001349 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001350 mutex_lock(&cgroup_mutex);
1351
Paul Menagec6d57f32009-09-23 15:56:19 -07001352 if (strlen(root->name)) {
1353 /* Check for name clashes with existing mounts */
1354 for_each_active_root(existing_root) {
1355 if (!strcmp(existing_root->name, root->name)) {
1356 ret = -EBUSY;
1357 mutex_unlock(&cgroup_mutex);
1358 mutex_unlock(&inode->i_mutex);
1359 goto drop_new_super;
1360 }
1361 }
1362 }
1363
Paul Menage817929e2007-10-18 23:39:36 -07001364 /*
1365 * We're accessing css_set_count without locking
1366 * css_set_lock here, but that's OK - it can only be
1367 * increased by someone holding cgroup_lock, and
1368 * that's us. The worst that can happen is that we
1369 * have some link structures left over
1370 */
1371 ret = allocate_cg_links(css_set_count, &tmp_cg_links);
1372 if (ret) {
1373 mutex_unlock(&cgroup_mutex);
1374 mutex_unlock(&inode->i_mutex);
1375 goto drop_new_super;
1376 }
1377
Paul Menageddbcc7e2007-10-18 23:39:30 -07001378 ret = rebind_subsystems(root, root->subsys_bits);
1379 if (ret == -EBUSY) {
1380 mutex_unlock(&cgroup_mutex);
Paul Menage817929e2007-10-18 23:39:36 -07001381 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001382 free_cg_links(&tmp_cg_links);
1383 goto drop_new_super;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001384 }
1385
1386 /* EBUSY should be the only error here */
1387 BUG_ON(ret);
1388
1389 list_add(&root->root_list, &roots);
Paul Menage817929e2007-10-18 23:39:36 -07001390 root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001391
Li Zefanc12f65d2009-01-07 18:07:42 -08001392 sb->s_root->d_fsdata = root_cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001393 root->top_cgroup.dentry = sb->s_root;
1394
Paul Menage817929e2007-10-18 23:39:36 -07001395 /* Link the top cgroup in this hierarchy into all
1396 * the css_set objects */
1397 write_lock(&css_set_lock);
Li Zefan28fd5df2008-04-29 01:00:13 -07001398 for (i = 0; i < CSS_SET_TABLE_SIZE; i++) {
1399 struct hlist_head *hhead = &css_set_table[i];
1400 struct hlist_node *node;
Paul Menage817929e2007-10-18 23:39:36 -07001401 struct css_set *cg;
Li Zefan28fd5df2008-04-29 01:00:13 -07001402
Li Zefanc12f65d2009-01-07 18:07:42 -08001403 hlist_for_each_entry(cg, node, hhead, hlist)
1404 link_css_set(&tmp_cg_links, cg, root_cgrp);
Li Zefan28fd5df2008-04-29 01:00:13 -07001405 }
Paul Menage817929e2007-10-18 23:39:36 -07001406 write_unlock(&css_set_lock);
1407
1408 free_cg_links(&tmp_cg_links);
1409
Li Zefanc12f65d2009-01-07 18:07:42 -08001410 BUG_ON(!list_empty(&root_cgrp->sibling));
1411 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001412 BUG_ON(root->number_of_cgroups != 1);
1413
Li Zefanc12f65d2009-01-07 18:07:42 -08001414 cgroup_populate_dir(root_cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001415 mutex_unlock(&cgroup_mutex);
Xiaotian Feng34f77a92009-09-23 15:56:18 -07001416 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001417 } else {
1418 /*
1419 * We re-used an existing hierarchy - the new root (if
1420 * any) is not needed
1421 */
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001422 cgroup_drop_root(opts.new_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001423 }
1424
Sukadev Bhattiprolua3ec9472009-03-04 12:06:34 -08001425 simple_set_mnt(mnt, sb);
Paul Menagec6d57f32009-09-23 15:56:19 -07001426 kfree(opts.release_agent);
1427 kfree(opts.name);
Sukadev Bhattiprolua3ec9472009-03-04 12:06:34 -08001428 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001429
1430 drop_new_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04001431 deactivate_locked_super(sb);
Paul Menagec6d57f32009-09-23 15:56:19 -07001432 out_err:
1433 kfree(opts.release_agent);
1434 kfree(opts.name);
1435
Paul Menageddbcc7e2007-10-18 23:39:30 -07001436 return ret;
1437}
1438
1439static void cgroup_kill_sb(struct super_block *sb) {
1440 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001441 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001442 int ret;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001443 struct cg_cgroup_link *link;
1444 struct cg_cgroup_link *saved_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001445
1446 BUG_ON(!root);
1447
1448 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001449 BUG_ON(!list_empty(&cgrp->children));
1450 BUG_ON(!list_empty(&cgrp->sibling));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001451
1452 mutex_lock(&cgroup_mutex);
1453
1454 /* Rebind all subsystems back to the default hierarchy */
1455 ret = rebind_subsystems(root, 0);
1456 /* Shouldn't be able to fail ... */
1457 BUG_ON(ret);
1458
Paul Menage817929e2007-10-18 23:39:36 -07001459 /*
1460 * Release all the links from css_sets to this hierarchy's
1461 * root cgroup
1462 */
1463 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001464
1465 list_for_each_entry_safe(link, saved_link, &cgrp->css_sets,
1466 cgrp_link_list) {
Paul Menage817929e2007-10-18 23:39:36 -07001467 list_del(&link->cg_link_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07001468 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07001469 kfree(link);
1470 }
1471 write_unlock(&css_set_lock);
1472
Paul Menage839ec542009-01-29 14:25:22 -08001473 if (!list_empty(&root->root_list)) {
1474 list_del(&root->root_list);
1475 root_count--;
1476 }
Li Zefane5f6a862009-01-07 18:07:41 -08001477
Paul Menageddbcc7e2007-10-18 23:39:30 -07001478 mutex_unlock(&cgroup_mutex);
1479
Paul Menageddbcc7e2007-10-18 23:39:30 -07001480 kill_litter_super(sb);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001481 cgroup_drop_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001482}
1483
1484static struct file_system_type cgroup_fs_type = {
1485 .name = "cgroup",
1486 .get_sb = cgroup_get_sb,
1487 .kill_sb = cgroup_kill_sb,
1488};
1489
Paul Menagebd89aab2007-10-18 23:40:44 -07001490static inline struct cgroup *__d_cgrp(struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001491{
1492 return dentry->d_fsdata;
1493}
1494
1495static inline struct cftype *__d_cft(struct dentry *dentry)
1496{
1497 return dentry->d_fsdata;
1498}
1499
Li Zefana043e3b2008-02-23 15:24:09 -08001500/**
1501 * cgroup_path - generate the path of a cgroup
1502 * @cgrp: the cgroup in question
1503 * @buf: the buffer to write the path into
1504 * @buflen: the length of the buffer
1505 *
Paul Menagea47295e2009-01-07 18:07:44 -08001506 * Called with cgroup_mutex held or else with an RCU-protected cgroup
1507 * reference. Writes path of cgroup into buf. Returns 0 on success,
1508 * -errno on error.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001509 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001510int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001511{
1512 char *start;
Paul Menagea47295e2009-01-07 18:07:44 -08001513 struct dentry *dentry = rcu_dereference(cgrp->dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001514
Paul Menagea47295e2009-01-07 18:07:44 -08001515 if (!dentry || cgrp == dummytop) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001516 /*
1517 * Inactive subsystems have no dentry for their root
1518 * cgroup
1519 */
1520 strcpy(buf, "/");
1521 return 0;
1522 }
1523
1524 start = buf + buflen;
1525
1526 *--start = '\0';
1527 for (;;) {
Paul Menagea47295e2009-01-07 18:07:44 -08001528 int len = dentry->d_name.len;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001529 if ((start -= len) < buf)
1530 return -ENAMETOOLONG;
Paul Menagebd89aab2007-10-18 23:40:44 -07001531 memcpy(start, cgrp->dentry->d_name.name, len);
1532 cgrp = cgrp->parent;
1533 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001534 break;
Paul Menagea47295e2009-01-07 18:07:44 -08001535 dentry = rcu_dereference(cgrp->dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07001536 if (!cgrp->parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001537 continue;
1538 if (--start < buf)
1539 return -ENAMETOOLONG;
1540 *start = '/';
1541 }
1542 memmove(buf, start, buf + buflen - start);
1543 return 0;
1544}
1545
Li Zefana043e3b2008-02-23 15:24:09 -08001546/**
1547 * cgroup_attach_task - attach task 'tsk' to cgroup 'cgrp'
1548 * @cgrp: the cgroup the task is attaching to
1549 * @tsk: the task to be attached
Paul Menagebbcb81d2007-10-18 23:39:32 -07001550 *
Li Zefana043e3b2008-02-23 15:24:09 -08001551 * Call holding cgroup_mutex. May take task_lock of
1552 * the task 'tsk' during call.
Paul Menagebbcb81d2007-10-18 23:39:32 -07001553 */
Cliff Wickman956db3c2008-02-07 00:14:43 -08001554int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001555{
1556 int retval = 0;
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001557 struct cgroup_subsys *ss, *failed_ss = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07001558 struct cgroup *oldcgrp;
Lai Jiangshan77efecd2009-01-07 18:07:39 -08001559 struct css_set *cg;
Paul Menage817929e2007-10-18 23:39:36 -07001560 struct css_set *newcg;
Paul Menagebd89aab2007-10-18 23:40:44 -07001561 struct cgroupfs_root *root = cgrp->root;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001562
1563 /* Nothing to do if the task is already in that cgroup */
Paul Menage7717f7b2009-09-23 15:56:22 -07001564 oldcgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07001565 if (cgrp == oldcgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001566 return 0;
1567
1568 for_each_subsys(root, ss) {
1569 if (ss->can_attach) {
Ben Blumbe367d02009-09-23 15:56:31 -07001570 retval = ss->can_attach(ss, cgrp, tsk, false);
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001571 if (retval) {
1572 /*
1573 * Remember on which subsystem the can_attach()
1574 * failed, so that we only call cancel_attach()
1575 * against the subsystems whose can_attach()
1576 * succeeded. (See below)
1577 */
1578 failed_ss = ss;
1579 goto out;
1580 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07001581 }
1582 }
1583
Lai Jiangshan77efecd2009-01-07 18:07:39 -08001584 task_lock(tsk);
1585 cg = tsk->cgroups;
1586 get_css_set(cg);
1587 task_unlock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07001588 /*
1589 * Locate or allocate a new css_set for this task,
1590 * based on its final set of cgroups
1591 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001592 newcg = find_css_set(cg, cgrp);
Lai Jiangshan77efecd2009-01-07 18:07:39 -08001593 put_css_set(cg);
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001594 if (!newcg) {
1595 retval = -ENOMEM;
1596 goto out;
1597 }
Paul Menage817929e2007-10-18 23:39:36 -07001598
Paul Menagebbcb81d2007-10-18 23:39:32 -07001599 task_lock(tsk);
1600 if (tsk->flags & PF_EXITING) {
1601 task_unlock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07001602 put_css_set(newcg);
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001603 retval = -ESRCH;
1604 goto out;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001605 }
Paul Menage817929e2007-10-18 23:39:36 -07001606 rcu_assign_pointer(tsk->cgroups, newcg);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001607 task_unlock(tsk);
1608
Paul Menage817929e2007-10-18 23:39:36 -07001609 /* Update the css_set linked lists if we're using them */
1610 write_lock(&css_set_lock);
1611 if (!list_empty(&tsk->cg_list)) {
1612 list_del(&tsk->cg_list);
1613 list_add(&tsk->cg_list, &newcg->tasks);
1614 }
1615 write_unlock(&css_set_lock);
1616
Paul Menagebbcb81d2007-10-18 23:39:32 -07001617 for_each_subsys(root, ss) {
Paul Jacksone18f6312008-02-07 00:13:44 -08001618 if (ss->attach)
Ben Blumbe367d02009-09-23 15:56:31 -07001619 ss->attach(ss, cgrp, oldcgrp, tsk, false);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001620 }
Paul Menagebd89aab2007-10-18 23:40:44 -07001621 set_bit(CGRP_RELEASABLE, &oldcgrp->flags);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001622 synchronize_rcu();
Paul Menage817929e2007-10-18 23:39:36 -07001623 put_css_set(cg);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07001624
1625 /*
1626 * wake up rmdir() waiter. the rmdir should fail since the cgroup
1627 * is no longer empty.
1628 */
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07001629 cgroup_wakeup_rmdir_waiter(cgrp);
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001630out:
1631 if (retval) {
1632 for_each_subsys(root, ss) {
1633 if (ss == failed_ss)
1634 /*
1635 * This subsystem was the one that failed the
1636 * can_attach() check earlier, so we don't need
1637 * to call cancel_attach() against it or any
1638 * remaining subsystems.
1639 */
1640 break;
1641 if (ss->cancel_attach)
1642 ss->cancel_attach(ss, cgrp, tsk, false);
1643 }
1644 }
1645 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001646}
1647
1648/*
Paul Menageaf351022008-07-25 01:47:01 -07001649 * Attach task with pid 'pid' to cgroup 'cgrp'. Call with cgroup_mutex
1650 * held. May take task_lock of task
Paul Menagebbcb81d2007-10-18 23:39:32 -07001651 */
Paul Menageaf351022008-07-25 01:47:01 -07001652static int attach_task_by_pid(struct cgroup *cgrp, u64 pid)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001653{
Paul Menagebbcb81d2007-10-18 23:39:32 -07001654 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11001655 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001656 int ret;
1657
Paul Menagebbcb81d2007-10-18 23:39:32 -07001658 if (pid) {
1659 rcu_read_lock();
Pavel Emelyanov73507f32008-02-07 00:14:47 -08001660 tsk = find_task_by_vpid(pid);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001661 if (!tsk || tsk->flags & PF_EXITING) {
1662 rcu_read_unlock();
1663 return -ESRCH;
1664 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07001665
David Howellsc69e8d92008-11-14 10:39:19 +11001666 tcred = __task_cred(tsk);
1667 if (cred->euid &&
1668 cred->euid != tcred->uid &&
1669 cred->euid != tcred->suid) {
1670 rcu_read_unlock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07001671 return -EACCES;
1672 }
David Howellsc69e8d92008-11-14 10:39:19 +11001673 get_task_struct(tsk);
1674 rcu_read_unlock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07001675 } else {
1676 tsk = current;
1677 get_task_struct(tsk);
1678 }
1679
Cliff Wickman956db3c2008-02-07 00:14:43 -08001680 ret = cgroup_attach_task(cgrp, tsk);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001681 put_task_struct(tsk);
1682 return ret;
1683}
1684
Paul Menageaf351022008-07-25 01:47:01 -07001685static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid)
1686{
1687 int ret;
1688 if (!cgroup_lock_live_group(cgrp))
1689 return -ENODEV;
1690 ret = attach_task_by_pid(cgrp, pid);
1691 cgroup_unlock();
1692 return ret;
1693}
1694
Paul Menagee788e062008-07-25 01:46:59 -07001695/**
1696 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
1697 * @cgrp: the cgroup to be checked for liveness
1698 *
Paul Menage84eea842008-07-25 01:47:00 -07001699 * On success, returns true; the lock should be later released with
1700 * cgroup_unlock(). On failure returns false with no lock held.
Paul Menagee788e062008-07-25 01:46:59 -07001701 */
Paul Menage84eea842008-07-25 01:47:00 -07001702bool cgroup_lock_live_group(struct cgroup *cgrp)
Paul Menagee788e062008-07-25 01:46:59 -07001703{
1704 mutex_lock(&cgroup_mutex);
1705 if (cgroup_is_removed(cgrp)) {
1706 mutex_unlock(&cgroup_mutex);
1707 return false;
1708 }
1709 return true;
1710}
1711
1712static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
1713 const char *buffer)
1714{
1715 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
1716 if (!cgroup_lock_live_group(cgrp))
1717 return -ENODEV;
1718 strcpy(cgrp->root->release_agent_path, buffer);
Paul Menage84eea842008-07-25 01:47:00 -07001719 cgroup_unlock();
Paul Menagee788e062008-07-25 01:46:59 -07001720 return 0;
1721}
1722
1723static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
1724 struct seq_file *seq)
1725{
1726 if (!cgroup_lock_live_group(cgrp))
1727 return -ENODEV;
1728 seq_puts(seq, cgrp->root->release_agent_path);
1729 seq_putc(seq, '\n');
Paul Menage84eea842008-07-25 01:47:00 -07001730 cgroup_unlock();
Paul Menagee788e062008-07-25 01:46:59 -07001731 return 0;
1732}
1733
Paul Menage84eea842008-07-25 01:47:00 -07001734/* A buffer size big enough for numbers or short strings */
1735#define CGROUP_LOCAL_BUFFER_SIZE 64
1736
Paul Menagee73d2c62008-04-29 01:00:06 -07001737static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
Paul Menagef4c753b2008-04-29 00:59:56 -07001738 struct file *file,
1739 const char __user *userbuf,
1740 size_t nbytes, loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07001741{
Paul Menage84eea842008-07-25 01:47:00 -07001742 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menage355e0c42007-10-18 23:39:33 -07001743 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07001744 char *end;
1745
1746 if (!nbytes)
1747 return -EINVAL;
1748 if (nbytes >= sizeof(buffer))
1749 return -E2BIG;
1750 if (copy_from_user(buffer, userbuf, nbytes))
1751 return -EFAULT;
1752
1753 buffer[nbytes] = 0; /* nul-terminate */
Paul Menagee73d2c62008-04-29 01:00:06 -07001754 if (cft->write_u64) {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07001755 u64 val = simple_strtoull(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07001756 if (*end)
1757 return -EINVAL;
1758 retval = cft->write_u64(cgrp, cft, val);
1759 } else {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07001760 s64 val = simple_strtoll(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07001761 if (*end)
1762 return -EINVAL;
1763 retval = cft->write_s64(cgrp, cft, val);
1764 }
Paul Menage355e0c42007-10-18 23:39:33 -07001765 if (!retval)
1766 retval = nbytes;
1767 return retval;
1768}
1769
Paul Menagedb3b1492008-07-25 01:46:58 -07001770static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
1771 struct file *file,
1772 const char __user *userbuf,
1773 size_t nbytes, loff_t *unused_ppos)
1774{
Paul Menage84eea842008-07-25 01:47:00 -07001775 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagedb3b1492008-07-25 01:46:58 -07001776 int retval = 0;
1777 size_t max_bytes = cft->max_write_len;
1778 char *buffer = local_buffer;
1779
1780 if (!max_bytes)
1781 max_bytes = sizeof(local_buffer) - 1;
1782 if (nbytes >= max_bytes)
1783 return -E2BIG;
1784 /* Allocate a dynamic buffer if we need one */
1785 if (nbytes >= sizeof(local_buffer)) {
1786 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
1787 if (buffer == NULL)
1788 return -ENOMEM;
1789 }
Li Zefan5a3eb9f2008-07-29 22:33:18 -07001790 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
1791 retval = -EFAULT;
1792 goto out;
1793 }
Paul Menagedb3b1492008-07-25 01:46:58 -07001794
1795 buffer[nbytes] = 0; /* nul-terminate */
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07001796 retval = cft->write_string(cgrp, cft, strstrip(buffer));
Paul Menagedb3b1492008-07-25 01:46:58 -07001797 if (!retval)
1798 retval = nbytes;
Li Zefan5a3eb9f2008-07-29 22:33:18 -07001799out:
Paul Menagedb3b1492008-07-25 01:46:58 -07001800 if (buffer != local_buffer)
1801 kfree(buffer);
1802 return retval;
1803}
1804
Paul Menageddbcc7e2007-10-18 23:39:30 -07001805static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
1806 size_t nbytes, loff_t *ppos)
1807{
1808 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07001809 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001810
Li Zefan75139b82009-01-07 18:07:33 -08001811 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001812 return -ENODEV;
Paul Menage355e0c42007-10-18 23:39:33 -07001813 if (cft->write)
Paul Menagebd89aab2007-10-18 23:40:44 -07001814 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07001815 if (cft->write_u64 || cft->write_s64)
1816 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagedb3b1492008-07-25 01:46:58 -07001817 if (cft->write_string)
1818 return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07001819 if (cft->trigger) {
1820 int ret = cft->trigger(cgrp, (unsigned int)cft->private);
1821 return ret ? ret : nbytes;
1822 }
Paul Menage355e0c42007-10-18 23:39:33 -07001823 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001824}
1825
Paul Menagef4c753b2008-04-29 00:59:56 -07001826static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
1827 struct file *file,
1828 char __user *buf, size_t nbytes,
1829 loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001830{
Paul Menage84eea842008-07-25 01:47:00 -07001831 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagef4c753b2008-04-29 00:59:56 -07001832 u64 val = cft->read_u64(cgrp, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001833 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
1834
1835 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
1836}
1837
Paul Menagee73d2c62008-04-29 01:00:06 -07001838static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
1839 struct file *file,
1840 char __user *buf, size_t nbytes,
1841 loff_t *ppos)
1842{
Paul Menage84eea842008-07-25 01:47:00 -07001843 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagee73d2c62008-04-29 01:00:06 -07001844 s64 val = cft->read_s64(cgrp, cft);
1845 int len = sprintf(tmp, "%lld\n", (long long) val);
1846
1847 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
1848}
1849
Paul Menageddbcc7e2007-10-18 23:39:30 -07001850static ssize_t cgroup_file_read(struct file *file, char __user *buf,
1851 size_t nbytes, loff_t *ppos)
1852{
1853 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07001854 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001855
Li Zefan75139b82009-01-07 18:07:33 -08001856 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001857 return -ENODEV;
1858
1859 if (cft->read)
Paul Menagebd89aab2007-10-18 23:40:44 -07001860 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07001861 if (cft->read_u64)
1862 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07001863 if (cft->read_s64)
1864 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001865 return -EINVAL;
1866}
1867
Paul Menage91796562008-04-29 01:00:01 -07001868/*
1869 * seqfile ops/methods for returning structured data. Currently just
1870 * supports string->u64 maps, but can be extended in future.
1871 */
1872
1873struct cgroup_seqfile_state {
1874 struct cftype *cft;
1875 struct cgroup *cgroup;
1876};
1877
1878static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
1879{
1880 struct seq_file *sf = cb->state;
1881 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
1882}
1883
1884static int cgroup_seqfile_show(struct seq_file *m, void *arg)
1885{
1886 struct cgroup_seqfile_state *state = m->private;
1887 struct cftype *cft = state->cft;
Serge E. Hallyn29486df2008-04-29 01:00:14 -07001888 if (cft->read_map) {
1889 struct cgroup_map_cb cb = {
1890 .fill = cgroup_map_add,
1891 .state = m,
1892 };
1893 return cft->read_map(state->cgroup, cft, &cb);
1894 }
1895 return cft->read_seq_string(state->cgroup, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07001896}
1897
Adrian Bunk96930a62008-07-25 19:46:21 -07001898static int cgroup_seqfile_release(struct inode *inode, struct file *file)
Paul Menage91796562008-04-29 01:00:01 -07001899{
1900 struct seq_file *seq = file->private_data;
1901 kfree(seq->private);
1902 return single_release(inode, file);
1903}
1904
Alexey Dobriyan828c0952009-10-01 15:43:56 -07001905static const struct file_operations cgroup_seqfile_operations = {
Paul Menage91796562008-04-29 01:00:01 -07001906 .read = seq_read,
Paul Menagee788e062008-07-25 01:46:59 -07001907 .write = cgroup_file_write,
Paul Menage91796562008-04-29 01:00:01 -07001908 .llseek = seq_lseek,
1909 .release = cgroup_seqfile_release,
1910};
1911
Paul Menageddbcc7e2007-10-18 23:39:30 -07001912static int cgroup_file_open(struct inode *inode, struct file *file)
1913{
1914 int err;
1915 struct cftype *cft;
1916
1917 err = generic_file_open(inode, file);
1918 if (err)
1919 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001920 cft = __d_cft(file->f_dentry);
Li Zefan75139b82009-01-07 18:07:33 -08001921
Serge E. Hallyn29486df2008-04-29 01:00:14 -07001922 if (cft->read_map || cft->read_seq_string) {
Paul Menage91796562008-04-29 01:00:01 -07001923 struct cgroup_seqfile_state *state =
1924 kzalloc(sizeof(*state), GFP_USER);
1925 if (!state)
1926 return -ENOMEM;
1927 state->cft = cft;
1928 state->cgroup = __d_cgrp(file->f_dentry->d_parent);
1929 file->f_op = &cgroup_seqfile_operations;
1930 err = single_open(file, cgroup_seqfile_show, state);
1931 if (err < 0)
1932 kfree(state);
1933 } else if (cft->open)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001934 err = cft->open(inode, file);
1935 else
1936 err = 0;
1937
1938 return err;
1939}
1940
1941static int cgroup_file_release(struct inode *inode, struct file *file)
1942{
1943 struct cftype *cft = __d_cft(file->f_dentry);
1944 if (cft->release)
1945 return cft->release(inode, file);
1946 return 0;
1947}
1948
1949/*
1950 * cgroup_rename - Only allow simple rename of directories in place.
1951 */
1952static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
1953 struct inode *new_dir, struct dentry *new_dentry)
1954{
1955 if (!S_ISDIR(old_dentry->d_inode->i_mode))
1956 return -ENOTDIR;
1957 if (new_dentry->d_inode)
1958 return -EEXIST;
1959 if (old_dir != new_dir)
1960 return -EIO;
1961 return simple_rename(old_dir, old_dentry, new_dir, new_dentry);
1962}
1963
Alexey Dobriyan828c0952009-10-01 15:43:56 -07001964static const struct file_operations cgroup_file_operations = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001965 .read = cgroup_file_read,
1966 .write = cgroup_file_write,
1967 .llseek = generic_file_llseek,
1968 .open = cgroup_file_open,
1969 .release = cgroup_file_release,
1970};
1971
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07001972static const struct inode_operations cgroup_dir_inode_operations = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001973 .lookup = simple_lookup,
1974 .mkdir = cgroup_mkdir,
1975 .rmdir = cgroup_rmdir,
1976 .rename = cgroup_rename,
1977};
1978
Li Zefan099fca32009-04-02 16:57:29 -07001979static int cgroup_create_file(struct dentry *dentry, mode_t mode,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001980 struct super_block *sb)
1981{
Al Viro3ba13d12009-02-20 06:02:22 +00001982 static const struct dentry_operations cgroup_dops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001983 .d_iput = cgroup_diput,
1984 };
1985
1986 struct inode *inode;
1987
1988 if (!dentry)
1989 return -ENOENT;
1990 if (dentry->d_inode)
1991 return -EEXIST;
1992
1993 inode = cgroup_new_inode(mode, sb);
1994 if (!inode)
1995 return -ENOMEM;
1996
1997 if (S_ISDIR(mode)) {
1998 inode->i_op = &cgroup_dir_inode_operations;
1999 inode->i_fop = &simple_dir_operations;
2000
2001 /* start off with i_nlink == 2 (for "." entry) */
2002 inc_nlink(inode);
2003
2004 /* start with the directory inode held, so that we can
2005 * populate it without racing with another mkdir */
Paul Menage817929e2007-10-18 23:39:36 -07002006 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002007 } else if (S_ISREG(mode)) {
2008 inode->i_size = 0;
2009 inode->i_fop = &cgroup_file_operations;
2010 }
2011 dentry->d_op = &cgroup_dops;
2012 d_instantiate(dentry, inode);
2013 dget(dentry); /* Extra count - pin the dentry in core */
2014 return 0;
2015}
2016
2017/*
Li Zefana043e3b2008-02-23 15:24:09 -08002018 * cgroup_create_dir - create a directory for an object.
2019 * @cgrp: the cgroup we create the directory for. It must have a valid
2020 * ->parent field. And we are going to fill its ->dentry field.
2021 * @dentry: dentry of the new cgroup
2022 * @mode: mode to set on new directory.
Paul Menageddbcc7e2007-10-18 23:39:30 -07002023 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002024static int cgroup_create_dir(struct cgroup *cgrp, struct dentry *dentry,
Li Zefan099fca32009-04-02 16:57:29 -07002025 mode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002026{
2027 struct dentry *parent;
2028 int error = 0;
2029
Paul Menagebd89aab2007-10-18 23:40:44 -07002030 parent = cgrp->parent->dentry;
2031 error = cgroup_create_file(dentry, S_IFDIR | mode, cgrp->root->sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002032 if (!error) {
Paul Menagebd89aab2007-10-18 23:40:44 -07002033 dentry->d_fsdata = cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002034 inc_nlink(parent->d_inode);
Paul Menagea47295e2009-01-07 18:07:44 -08002035 rcu_assign_pointer(cgrp->dentry, dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002036 dget(dentry);
2037 }
2038 dput(dentry);
2039
2040 return error;
2041}
2042
Li Zefan099fca32009-04-02 16:57:29 -07002043/**
2044 * cgroup_file_mode - deduce file mode of a control file
2045 * @cft: the control file in question
2046 *
2047 * returns cft->mode if ->mode is not 0
2048 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2049 * returns S_IRUGO if it has only a read handler
2050 * returns S_IWUSR if it has only a write hander
2051 */
2052static mode_t cgroup_file_mode(const struct cftype *cft)
2053{
2054 mode_t mode = 0;
2055
2056 if (cft->mode)
2057 return cft->mode;
2058
2059 if (cft->read || cft->read_u64 || cft->read_s64 ||
2060 cft->read_map || cft->read_seq_string)
2061 mode |= S_IRUGO;
2062
2063 if (cft->write || cft->write_u64 || cft->write_s64 ||
2064 cft->write_string || cft->trigger)
2065 mode |= S_IWUSR;
2066
2067 return mode;
2068}
2069
Paul Menagebd89aab2007-10-18 23:40:44 -07002070int cgroup_add_file(struct cgroup *cgrp,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002071 struct cgroup_subsys *subsys,
2072 const struct cftype *cft)
2073{
Paul Menagebd89aab2007-10-18 23:40:44 -07002074 struct dentry *dir = cgrp->dentry;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002075 struct dentry *dentry;
2076 int error;
Li Zefan099fca32009-04-02 16:57:29 -07002077 mode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002078
2079 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Paul Menagebd89aab2007-10-18 23:40:44 -07002080 if (subsys && !test_bit(ROOT_NOPREFIX, &cgrp->root->flags)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002081 strcpy(name, subsys->name);
2082 strcat(name, ".");
2083 }
2084 strcat(name, cft->name);
2085 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
2086 dentry = lookup_one_len(name, dir, strlen(name));
2087 if (!IS_ERR(dentry)) {
Li Zefan099fca32009-04-02 16:57:29 -07002088 mode = cgroup_file_mode(cft);
2089 error = cgroup_create_file(dentry, mode | S_IFREG,
Paul Menagebd89aab2007-10-18 23:40:44 -07002090 cgrp->root->sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002091 if (!error)
2092 dentry->d_fsdata = (void *)cft;
2093 dput(dentry);
2094 } else
2095 error = PTR_ERR(dentry);
2096 return error;
2097}
2098
Paul Menagebd89aab2007-10-18 23:40:44 -07002099int cgroup_add_files(struct cgroup *cgrp,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002100 struct cgroup_subsys *subsys,
2101 const struct cftype cft[],
2102 int count)
2103{
2104 int i, err;
2105 for (i = 0; i < count; i++) {
Paul Menagebd89aab2007-10-18 23:40:44 -07002106 err = cgroup_add_file(cgrp, subsys, &cft[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002107 if (err)
2108 return err;
2109 }
2110 return 0;
2111}
2112
Li Zefana043e3b2008-02-23 15:24:09 -08002113/**
2114 * cgroup_task_count - count the number of tasks in a cgroup.
2115 * @cgrp: the cgroup in question
2116 *
2117 * Return the number of tasks in the cgroup.
2118 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002119int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002120{
2121 int count = 0;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07002122 struct cg_cgroup_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002123
Paul Menage817929e2007-10-18 23:39:36 -07002124 read_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07002125 list_for_each_entry(link, &cgrp->css_sets, cgrp_link_list) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07002126 count += atomic_read(&link->cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002127 }
2128 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002129 return count;
2130}
2131
2132/*
Paul Menage817929e2007-10-18 23:39:36 -07002133 * Advance a list_head iterator. The iterator should be positioned at
2134 * the start of a css_set
2135 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002136static void cgroup_advance_iter(struct cgroup *cgrp,
Paul Menage7717f7b2009-09-23 15:56:22 -07002137 struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002138{
2139 struct list_head *l = it->cg_link;
2140 struct cg_cgroup_link *link;
2141 struct css_set *cg;
2142
2143 /* Advance to the next non-empty css_set */
2144 do {
2145 l = l->next;
Paul Menagebd89aab2007-10-18 23:40:44 -07002146 if (l == &cgrp->css_sets) {
Paul Menage817929e2007-10-18 23:39:36 -07002147 it->cg_link = NULL;
2148 return;
2149 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002150 link = list_entry(l, struct cg_cgroup_link, cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07002151 cg = link->cg;
2152 } while (list_empty(&cg->tasks));
2153 it->cg_link = l;
2154 it->task = cg->tasks.next;
2155}
2156
Cliff Wickman31a7df02008-02-07 00:14:42 -08002157/*
2158 * To reduce the fork() overhead for systems that are not actually
2159 * using their cgroups capability, we don't maintain the lists running
2160 * through each css_set to its tasks until we see the list actually
2161 * used - in other words after the first call to cgroup_iter_start().
2162 *
2163 * The tasklist_lock is not held here, as do_each_thread() and
2164 * while_each_thread() are protected by RCU.
2165 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07002166static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002167{
2168 struct task_struct *p, *g;
2169 write_lock(&css_set_lock);
2170 use_task_css_set_links = 1;
2171 do_each_thread(g, p) {
2172 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08002173 /*
2174 * We should check if the process is exiting, otherwise
2175 * it will race with cgroup_exit() in that the list
2176 * entry won't be deleted though the process has exited.
2177 */
2178 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Cliff Wickman31a7df02008-02-07 00:14:42 -08002179 list_add(&p->cg_list, &p->cgroups->tasks);
2180 task_unlock(p);
2181 } while_each_thread(g, p);
2182 write_unlock(&css_set_lock);
2183}
2184
Paul Menagebd89aab2007-10-18 23:40:44 -07002185void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002186{
2187 /*
2188 * The first time anyone tries to iterate across a cgroup,
2189 * we need to enable the list linking each css_set to its
2190 * tasks, and fix up all existing tasks.
2191 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08002192 if (!use_task_css_set_links)
2193 cgroup_enable_task_cg_lists();
2194
Paul Menage817929e2007-10-18 23:39:36 -07002195 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07002196 it->cg_link = &cgrp->css_sets;
2197 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07002198}
2199
Paul Menagebd89aab2007-10-18 23:40:44 -07002200struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -07002201 struct cgroup_iter *it)
2202{
2203 struct task_struct *res;
2204 struct list_head *l = it->task;
Lai Jiangshan2019f632009-01-07 18:07:36 -08002205 struct cg_cgroup_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07002206
2207 /* If the iterator cg is NULL, we have no tasks */
2208 if (!it->cg_link)
2209 return NULL;
2210 res = list_entry(l, struct task_struct, cg_list);
2211 /* Advance iterator to find next entry */
2212 l = l->next;
Lai Jiangshan2019f632009-01-07 18:07:36 -08002213 link = list_entry(it->cg_link, struct cg_cgroup_link, cgrp_link_list);
2214 if (l == &link->cg->tasks) {
Paul Menage817929e2007-10-18 23:39:36 -07002215 /* We reached the end of this task list - move on to
2216 * the next cg_cgroup_link */
Paul Menagebd89aab2007-10-18 23:40:44 -07002217 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07002218 } else {
2219 it->task = l;
2220 }
2221 return res;
2222}
2223
Paul Menagebd89aab2007-10-18 23:40:44 -07002224void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002225{
2226 read_unlock(&css_set_lock);
2227}
2228
Cliff Wickman31a7df02008-02-07 00:14:42 -08002229static inline int started_after_time(struct task_struct *t1,
2230 struct timespec *time,
2231 struct task_struct *t2)
2232{
2233 int start_diff = timespec_compare(&t1->start_time, time);
2234 if (start_diff > 0) {
2235 return 1;
2236 } else if (start_diff < 0) {
2237 return 0;
2238 } else {
2239 /*
2240 * Arbitrarily, if two processes started at the same
2241 * time, we'll say that the lower pointer value
2242 * started first. Note that t2 may have exited by now
2243 * so this may not be a valid pointer any longer, but
2244 * that's fine - it still serves to distinguish
2245 * between two tasks started (effectively) simultaneously.
2246 */
2247 return t1 > t2;
2248 }
2249}
2250
2251/*
2252 * This function is a callback from heap_insert() and is used to order
2253 * the heap.
2254 * In this case we order the heap in descending task start time.
2255 */
2256static inline int started_after(void *p1, void *p2)
2257{
2258 struct task_struct *t1 = p1;
2259 struct task_struct *t2 = p2;
2260 return started_after_time(t1, &t2->start_time, t2);
2261}
2262
2263/**
2264 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
2265 * @scan: struct cgroup_scanner containing arguments for the scan
2266 *
2267 * Arguments include pointers to callback functions test_task() and
2268 * process_task().
2269 * Iterate through all the tasks in a cgroup, calling test_task() for each,
2270 * and if it returns true, call process_task() for it also.
2271 * The test_task pointer may be NULL, meaning always true (select all tasks).
2272 * Effectively duplicates cgroup_iter_{start,next,end}()
2273 * but does not lock css_set_lock for the call to process_task().
2274 * The struct cgroup_scanner may be embedded in any structure of the caller's
2275 * creation.
2276 * It is guaranteed that process_task() will act on every task that
2277 * is a member of the cgroup for the duration of this call. This
2278 * function may or may not call process_task() for tasks that exit
2279 * or move to a different cgroup during the call, or are forked or
2280 * move into the cgroup during the call.
2281 *
2282 * Note that test_task() may be called with locks held, and may in some
2283 * situations be called multiple times for the same task, so it should
2284 * be cheap.
2285 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
2286 * pre-allocated and will be used for heap operations (and its "gt" member will
2287 * be overwritten), else a temporary heap will be used (allocation of which
2288 * may cause this function to fail).
2289 */
2290int cgroup_scan_tasks(struct cgroup_scanner *scan)
2291{
2292 int retval, i;
2293 struct cgroup_iter it;
2294 struct task_struct *p, *dropped;
2295 /* Never dereference latest_task, since it's not refcounted */
2296 struct task_struct *latest_task = NULL;
2297 struct ptr_heap tmp_heap;
2298 struct ptr_heap *heap;
2299 struct timespec latest_time = { 0, 0 };
2300
2301 if (scan->heap) {
2302 /* The caller supplied our heap and pre-allocated its memory */
2303 heap = scan->heap;
2304 heap->gt = &started_after;
2305 } else {
2306 /* We need to allocate our own heap memory */
2307 heap = &tmp_heap;
2308 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
2309 if (retval)
2310 /* cannot allocate the heap */
2311 return retval;
2312 }
2313
2314 again:
2315 /*
2316 * Scan tasks in the cgroup, using the scanner's "test_task" callback
2317 * to determine which are of interest, and using the scanner's
2318 * "process_task" callback to process any of them that need an update.
2319 * Since we don't want to hold any locks during the task updates,
2320 * gather tasks to be processed in a heap structure.
2321 * The heap is sorted by descending task start time.
2322 * If the statically-sized heap fills up, we overflow tasks that
2323 * started later, and in future iterations only consider tasks that
2324 * started after the latest task in the previous pass. This
2325 * guarantees forward progress and that we don't miss any tasks.
2326 */
2327 heap->size = 0;
2328 cgroup_iter_start(scan->cg, &it);
2329 while ((p = cgroup_iter_next(scan->cg, &it))) {
2330 /*
2331 * Only affect tasks that qualify per the caller's callback,
2332 * if he provided one
2333 */
2334 if (scan->test_task && !scan->test_task(p, scan))
2335 continue;
2336 /*
2337 * Only process tasks that started after the last task
2338 * we processed
2339 */
2340 if (!started_after_time(p, &latest_time, latest_task))
2341 continue;
2342 dropped = heap_insert(heap, p);
2343 if (dropped == NULL) {
2344 /*
2345 * The new task was inserted; the heap wasn't
2346 * previously full
2347 */
2348 get_task_struct(p);
2349 } else if (dropped != p) {
2350 /*
2351 * The new task was inserted, and pushed out a
2352 * different task
2353 */
2354 get_task_struct(p);
2355 put_task_struct(dropped);
2356 }
2357 /*
2358 * Else the new task was newer than anything already in
2359 * the heap and wasn't inserted
2360 */
2361 }
2362 cgroup_iter_end(scan->cg, &it);
2363
2364 if (heap->size) {
2365 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07002366 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08002367 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07002368 latest_time = q->start_time;
2369 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08002370 }
2371 /* Process the task per the caller's callback */
Paul Jackson4fe91d52008-04-29 00:59:55 -07002372 scan->process_task(q, scan);
2373 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002374 }
2375 /*
2376 * If we had to process any tasks at all, scan again
2377 * in case some of them were in the middle of forking
2378 * children that didn't get processed.
2379 * Not the most efficient way to do it, but it avoids
2380 * having to take callback_mutex in the fork path
2381 */
2382 goto again;
2383 }
2384 if (heap == &tmp_heap)
2385 heap_free(&tmp_heap);
2386 return 0;
2387}
2388
Paul Menage817929e2007-10-18 23:39:36 -07002389/*
Ben Blum102a7752009-09-23 15:56:26 -07002390 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07002391 *
2392 * Reading this file can return large amounts of data if a cgroup has
2393 * *lots* of attached tasks. So it may need several calls to read(),
2394 * but we cannot guarantee that the information we produce is correct
2395 * unless we produce it entirely atomically.
2396 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07002397 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07002398
2399/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07002400 * The following two functions "fix" the issue where there are more pids
2401 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
2402 * TODO: replace with a kernel-wide solution to this problem
2403 */
2404#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
2405static void *pidlist_allocate(int count)
2406{
2407 if (PIDLIST_TOO_LARGE(count))
2408 return vmalloc(count * sizeof(pid_t));
2409 else
2410 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
2411}
2412static void pidlist_free(void *p)
2413{
2414 if (is_vmalloc_addr(p))
2415 vfree(p);
2416 else
2417 kfree(p);
2418}
2419static void *pidlist_resize(void *p, int newcount)
2420{
2421 void *newlist;
2422 /* note: if new alloc fails, old p will still be valid either way */
2423 if (is_vmalloc_addr(p)) {
2424 newlist = vmalloc(newcount * sizeof(pid_t));
2425 if (!newlist)
2426 return NULL;
2427 memcpy(newlist, p, newcount * sizeof(pid_t));
2428 vfree(p);
2429 } else {
2430 newlist = krealloc(p, newcount * sizeof(pid_t), GFP_KERNEL);
2431 }
2432 return newlist;
2433}
2434
2435/*
Ben Blum102a7752009-09-23 15:56:26 -07002436 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
2437 * If the new stripped list is sufficiently smaller and there's enough memory
2438 * to allocate a new buffer, will let go of the unneeded memory. Returns the
2439 * number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07002440 */
Ben Blum102a7752009-09-23 15:56:26 -07002441/* is the size difference enough that we should re-allocate the array? */
2442#define PIDLIST_REALLOC_DIFFERENCE(old, new) ((old) - PAGE_SIZE >= (new))
2443static int pidlist_uniq(pid_t **p, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002444{
Ben Blum102a7752009-09-23 15:56:26 -07002445 int src, dest = 1;
2446 pid_t *list = *p;
2447 pid_t *newlist;
2448
2449 /*
2450 * we presume the 0th element is unique, so i starts at 1. trivial
2451 * edge cases first; no work needs to be done for either
2452 */
2453 if (length == 0 || length == 1)
2454 return length;
2455 /* src and dest walk down the list; dest counts unique elements */
2456 for (src = 1; src < length; src++) {
2457 /* find next unique element */
2458 while (list[src] == list[src-1]) {
2459 src++;
2460 if (src == length)
2461 goto after;
2462 }
2463 /* dest always points to where the next unique element goes */
2464 list[dest] = list[src];
2465 dest++;
2466 }
2467after:
2468 /*
2469 * if the length difference is large enough, we want to allocate a
2470 * smaller buffer to save memory. if this fails due to out of memory,
2471 * we'll just stay with what we've got.
2472 */
2473 if (PIDLIST_REALLOC_DIFFERENCE(length, dest)) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07002474 newlist = pidlist_resize(list, dest);
Ben Blum102a7752009-09-23 15:56:26 -07002475 if (newlist)
2476 *p = newlist;
2477 }
2478 return dest;
2479}
2480
2481static int cmppid(const void *a, const void *b)
2482{
2483 return *(pid_t *)a - *(pid_t *)b;
2484}
2485
2486/*
Ben Blum72a8cb32009-09-23 15:56:27 -07002487 * find the appropriate pidlist for our purpose (given procs vs tasks)
2488 * returns with the lock on that pidlist already held, and takes care
2489 * of the use count, or returns NULL with no locks held if we're out of
2490 * memory.
2491 */
2492static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
2493 enum cgroup_filetype type)
2494{
2495 struct cgroup_pidlist *l;
2496 /* don't need task_nsproxy() if we're looking at ourself */
2497 struct pid_namespace *ns = get_pid_ns(current->nsproxy->pid_ns);
2498 /*
2499 * We can't drop the pidlist_mutex before taking the l->mutex in case
2500 * the last ref-holder is trying to remove l from the list at the same
2501 * time. Holding the pidlist_mutex precludes somebody taking whichever
2502 * list we find out from under us - compare release_pid_array().
2503 */
2504 mutex_lock(&cgrp->pidlist_mutex);
2505 list_for_each_entry(l, &cgrp->pidlists, links) {
2506 if (l->key.type == type && l->key.ns == ns) {
2507 /* found a matching list - drop the extra refcount */
2508 put_pid_ns(ns);
2509 /* make sure l doesn't vanish out from under us */
2510 down_write(&l->mutex);
2511 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07002512 return l;
2513 }
2514 }
2515 /* entry not found; create a new one */
2516 l = kmalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
2517 if (!l) {
2518 mutex_unlock(&cgrp->pidlist_mutex);
2519 put_pid_ns(ns);
2520 return l;
2521 }
2522 init_rwsem(&l->mutex);
2523 down_write(&l->mutex);
2524 l->key.type = type;
2525 l->key.ns = ns;
2526 l->use_count = 0; /* don't increment here */
2527 l->list = NULL;
2528 l->owner = cgrp;
2529 list_add(&l->links, &cgrp->pidlists);
2530 mutex_unlock(&cgrp->pidlist_mutex);
2531 return l;
2532}
2533
2534/*
Ben Blum102a7752009-09-23 15:56:26 -07002535 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
2536 */
Ben Blum72a8cb32009-09-23 15:56:27 -07002537static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
2538 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07002539{
2540 pid_t *array;
2541 int length;
2542 int pid, n = 0; /* used for populating the array */
Paul Menage817929e2007-10-18 23:39:36 -07002543 struct cgroup_iter it;
2544 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07002545 struct cgroup_pidlist *l;
2546
2547 /*
2548 * If cgroup gets more users after we read count, we won't have
2549 * enough space - tough. This race is indistinguishable to the
2550 * caller from the case that the additional cgroup users didn't
2551 * show up until sometime later on.
2552 */
2553 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07002554 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07002555 if (!array)
2556 return -ENOMEM;
2557 /* now, populate the array */
Paul Menagebd89aab2007-10-18 23:40:44 -07002558 cgroup_iter_start(cgrp, &it);
2559 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Ben Blum102a7752009-09-23 15:56:26 -07002560 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07002561 break;
Ben Blum102a7752009-09-23 15:56:26 -07002562 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07002563 if (type == CGROUP_FILE_PROCS)
2564 pid = task_tgid_vnr(tsk);
2565 else
2566 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07002567 if (pid > 0) /* make sure to only use valid results */
2568 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07002569 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002570 cgroup_iter_end(cgrp, &it);
Ben Blum102a7752009-09-23 15:56:26 -07002571 length = n;
2572 /* now sort & (if procs) strip out duplicates */
2573 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07002574 if (type == CGROUP_FILE_PROCS)
Ben Blum102a7752009-09-23 15:56:26 -07002575 length = pidlist_uniq(&array, length);
Ben Blum72a8cb32009-09-23 15:56:27 -07002576 l = cgroup_pidlist_find(cgrp, type);
2577 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07002578 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07002579 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07002580 }
Ben Blum72a8cb32009-09-23 15:56:27 -07002581 /* store array, freeing old if necessary - lock already held */
Ben Blumd1d9fd32009-09-23 15:56:28 -07002582 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07002583 l->list = array;
2584 l->length = length;
2585 l->use_count++;
2586 up_write(&l->mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07002587 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07002588 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002589}
2590
Balbir Singh846c7bb2007-10-18 23:39:44 -07002591/**
Li Zefana043e3b2008-02-23 15:24:09 -08002592 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07002593 * @stats: cgroupstats to fill information into
2594 * @dentry: A dentry entry belonging to the cgroup for which stats have
2595 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08002596 *
2597 * Build and fill cgroupstats so that taskstats can export it to user
2598 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07002599 */
2600int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
2601{
2602 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07002603 struct cgroup *cgrp;
Balbir Singh846c7bb2007-10-18 23:39:44 -07002604 struct cgroup_iter it;
2605 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08002606
Balbir Singh846c7bb2007-10-18 23:39:44 -07002607 /*
Li Zefan33d283b2008-11-19 15:36:48 -08002608 * Validate dentry by checking the superblock operations,
2609 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07002610 */
Li Zefan33d283b2008-11-19 15:36:48 -08002611 if (dentry->d_sb->s_op != &cgroup_ops ||
2612 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07002613 goto err;
2614
2615 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07002616 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07002617
Paul Menagebd89aab2007-10-18 23:40:44 -07002618 cgroup_iter_start(cgrp, &it);
2619 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07002620 switch (tsk->state) {
2621 case TASK_RUNNING:
2622 stats->nr_running++;
2623 break;
2624 case TASK_INTERRUPTIBLE:
2625 stats->nr_sleeping++;
2626 break;
2627 case TASK_UNINTERRUPTIBLE:
2628 stats->nr_uninterruptible++;
2629 break;
2630 case TASK_STOPPED:
2631 stats->nr_stopped++;
2632 break;
2633 default:
2634 if (delayacct_is_task_waiting_on_io(tsk))
2635 stats->nr_io_wait++;
2636 break;
2637 }
2638 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002639 cgroup_iter_end(cgrp, &it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07002640
Balbir Singh846c7bb2007-10-18 23:39:44 -07002641err:
2642 return ret;
2643}
2644
Paul Menage8f3ff202009-09-23 15:56:25 -07002645
Paul Menagecc31edc2008-10-18 20:28:04 -07002646/*
Ben Blum102a7752009-09-23 15:56:26 -07002647 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07002648 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07002649 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07002650 */
2651
Ben Blum102a7752009-09-23 15:56:26 -07002652static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07002653{
2654 /*
2655 * Initially we receive a position value that corresponds to
2656 * one more than the last pid shown (or 0 on the first call or
2657 * after a seek to the start). Use a binary-search to find the
2658 * next pid to display, if any
2659 */
Ben Blum102a7752009-09-23 15:56:26 -07002660 struct cgroup_pidlist *l = s->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07002661 int index = 0, pid = *pos;
2662 int *iter;
2663
Ben Blum102a7752009-09-23 15:56:26 -07002664 down_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07002665 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07002666 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11002667
Paul Menagecc31edc2008-10-18 20:28:04 -07002668 while (index < end) {
2669 int mid = (index + end) / 2;
Ben Blum102a7752009-09-23 15:56:26 -07002670 if (l->list[mid] == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07002671 index = mid;
2672 break;
Ben Blum102a7752009-09-23 15:56:26 -07002673 } else if (l->list[mid] <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07002674 index = mid + 1;
2675 else
2676 end = mid;
2677 }
2678 }
2679 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07002680 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07002681 return NULL;
2682 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07002683 iter = l->list + index;
Paul Menagecc31edc2008-10-18 20:28:04 -07002684 *pos = *iter;
2685 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002686}
2687
Ben Blum102a7752009-09-23 15:56:26 -07002688static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07002689{
Ben Blum102a7752009-09-23 15:56:26 -07002690 struct cgroup_pidlist *l = s->private;
2691 up_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07002692}
2693
Ben Blum102a7752009-09-23 15:56:26 -07002694static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07002695{
Ben Blum102a7752009-09-23 15:56:26 -07002696 struct cgroup_pidlist *l = s->private;
2697 pid_t *p = v;
2698 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07002699 /*
2700 * Advance to the next pid in the array. If this goes off the
2701 * end, we're done
2702 */
2703 p++;
2704 if (p >= end) {
2705 return NULL;
2706 } else {
2707 *pos = *p;
2708 return p;
2709 }
2710}
2711
Ben Blum102a7752009-09-23 15:56:26 -07002712static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07002713{
2714 return seq_printf(s, "%d\n", *(int *)v);
2715}
2716
Ben Blum102a7752009-09-23 15:56:26 -07002717/*
2718 * seq_operations functions for iterating on pidlists through seq_file -
2719 * independent of whether it's tasks or procs
2720 */
2721static const struct seq_operations cgroup_pidlist_seq_operations = {
2722 .start = cgroup_pidlist_start,
2723 .stop = cgroup_pidlist_stop,
2724 .next = cgroup_pidlist_next,
2725 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07002726};
2727
Ben Blum102a7752009-09-23 15:56:26 -07002728static void cgroup_release_pid_array(struct cgroup_pidlist *l)
Paul Menagecc31edc2008-10-18 20:28:04 -07002729{
Ben Blum72a8cb32009-09-23 15:56:27 -07002730 /*
2731 * the case where we're the last user of this particular pidlist will
2732 * have us remove it from the cgroup's list, which entails taking the
2733 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
2734 * pidlist_mutex, we have to take pidlist_mutex first.
2735 */
2736 mutex_lock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07002737 down_write(&l->mutex);
2738 BUG_ON(!l->use_count);
2739 if (!--l->use_count) {
Ben Blum72a8cb32009-09-23 15:56:27 -07002740 /* we're the last user if refcount is 0; remove and free */
2741 list_del(&l->links);
2742 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07002743 pidlist_free(l->list);
Ben Blum72a8cb32009-09-23 15:56:27 -07002744 put_pid_ns(l->key.ns);
2745 up_write(&l->mutex);
2746 kfree(l);
2747 return;
Paul Menagecc31edc2008-10-18 20:28:04 -07002748 }
Ben Blum72a8cb32009-09-23 15:56:27 -07002749 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07002750 up_write(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07002751}
2752
Ben Blum102a7752009-09-23 15:56:26 -07002753static int cgroup_pidlist_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002754{
Ben Blum102a7752009-09-23 15:56:26 -07002755 struct cgroup_pidlist *l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002756 if (!(file->f_mode & FMODE_READ))
2757 return 0;
Ben Blum102a7752009-09-23 15:56:26 -07002758 /*
2759 * the seq_file will only be initialized if the file was opened for
2760 * reading; hence we check if it's not null only in that case.
2761 */
2762 l = ((struct seq_file *)file->private_data)->private;
2763 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07002764 return seq_release(inode, file);
2765}
2766
Ben Blum102a7752009-09-23 15:56:26 -07002767static const struct file_operations cgroup_pidlist_operations = {
Paul Menagecc31edc2008-10-18 20:28:04 -07002768 .read = seq_read,
2769 .llseek = seq_lseek,
2770 .write = cgroup_file_write,
Ben Blum102a7752009-09-23 15:56:26 -07002771 .release = cgroup_pidlist_release,
Paul Menagecc31edc2008-10-18 20:28:04 -07002772};
2773
2774/*
Ben Blum102a7752009-09-23 15:56:26 -07002775 * The following functions handle opens on a file that displays a pidlist
2776 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
2777 * in the cgroup.
Paul Menagecc31edc2008-10-18 20:28:04 -07002778 */
Ben Blum102a7752009-09-23 15:56:26 -07002779/* helper function for the two below it */
Ben Blum72a8cb32009-09-23 15:56:27 -07002780static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
Paul Menagecc31edc2008-10-18 20:28:04 -07002781{
2782 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Ben Blum72a8cb32009-09-23 15:56:27 -07002783 struct cgroup_pidlist *l;
Paul Menagecc31edc2008-10-18 20:28:04 -07002784 int retval;
2785
2786 /* Nothing to do for write-only files */
2787 if (!(file->f_mode & FMODE_READ))
2788 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002789
Ben Blum102a7752009-09-23 15:56:26 -07002790 /* have the array populated */
Ben Blum72a8cb32009-09-23 15:56:27 -07002791 retval = pidlist_array_load(cgrp, type, &l);
Ben Blum102a7752009-09-23 15:56:26 -07002792 if (retval)
2793 return retval;
2794 /* configure file information */
2795 file->f_op = &cgroup_pidlist_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002796
Ben Blum102a7752009-09-23 15:56:26 -07002797 retval = seq_open(file, &cgroup_pidlist_seq_operations);
Paul Menagecc31edc2008-10-18 20:28:04 -07002798 if (retval) {
Ben Blum102a7752009-09-23 15:56:26 -07002799 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07002800 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002801 }
Ben Blum102a7752009-09-23 15:56:26 -07002802 ((struct seq_file *)file->private_data)->private = l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002803 return 0;
2804}
Ben Blum102a7752009-09-23 15:56:26 -07002805static int cgroup_tasks_open(struct inode *unused, struct file *file)
2806{
Ben Blum72a8cb32009-09-23 15:56:27 -07002807 return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
Ben Blum102a7752009-09-23 15:56:26 -07002808}
2809static int cgroup_procs_open(struct inode *unused, struct file *file)
2810{
Ben Blum72a8cb32009-09-23 15:56:27 -07002811 return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
Ben Blum102a7752009-09-23 15:56:26 -07002812}
Paul Menagebbcb81d2007-10-18 23:39:32 -07002813
Paul Menagebd89aab2007-10-18 23:40:44 -07002814static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
Paul Menage81a6a5c2007-10-18 23:39:38 -07002815 struct cftype *cft)
2816{
Paul Menagebd89aab2007-10-18 23:40:44 -07002817 return notify_on_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07002818}
2819
Paul Menage6379c102008-07-25 01:47:01 -07002820static int cgroup_write_notify_on_release(struct cgroup *cgrp,
2821 struct cftype *cft,
2822 u64 val)
2823{
2824 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
2825 if (val)
2826 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
2827 else
2828 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
2829 return 0;
2830}
2831
Paul Menagebbcb81d2007-10-18 23:39:32 -07002832/*
2833 * for the common functions, 'private' gives the type of file
2834 */
Ben Blum102a7752009-09-23 15:56:26 -07002835/* for hysterical raisins, we can't put this on the older files */
2836#define CGROUP_FILE_GENERIC_PREFIX "cgroup."
Paul Menage81a6a5c2007-10-18 23:39:38 -07002837static struct cftype files[] = {
2838 {
2839 .name = "tasks",
2840 .open = cgroup_tasks_open,
Paul Menageaf351022008-07-25 01:47:01 -07002841 .write_u64 = cgroup_tasks_write,
Ben Blum102a7752009-09-23 15:56:26 -07002842 .release = cgroup_pidlist_release,
Li Zefan099fca32009-04-02 16:57:29 -07002843 .mode = S_IRUGO | S_IWUSR,
Paul Menage81a6a5c2007-10-18 23:39:38 -07002844 },
Ben Blum102a7752009-09-23 15:56:26 -07002845 {
2846 .name = CGROUP_FILE_GENERIC_PREFIX "procs",
2847 .open = cgroup_procs_open,
2848 /* .write_u64 = cgroup_procs_write, TODO */
2849 .release = cgroup_pidlist_release,
2850 .mode = S_IRUGO,
2851 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07002852 {
2853 .name = "notify_on_release",
Paul Menagef4c753b2008-04-29 00:59:56 -07002854 .read_u64 = cgroup_read_notify_on_release,
Paul Menage6379c102008-07-25 01:47:01 -07002855 .write_u64 = cgroup_write_notify_on_release,
Paul Menage81a6a5c2007-10-18 23:39:38 -07002856 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07002857};
2858
2859static struct cftype cft_release_agent = {
2860 .name = "release_agent",
Paul Menagee788e062008-07-25 01:46:59 -07002861 .read_seq_string = cgroup_release_agent_show,
2862 .write_string = cgroup_release_agent_write,
2863 .max_write_len = PATH_MAX,
Paul Menagebbcb81d2007-10-18 23:39:32 -07002864};
2865
Paul Menagebd89aab2007-10-18 23:40:44 -07002866static int cgroup_populate_dir(struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002867{
2868 int err;
2869 struct cgroup_subsys *ss;
2870
2871 /* First clear out any existing files */
Paul Menagebd89aab2007-10-18 23:40:44 -07002872 cgroup_clear_directory(cgrp->dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002873
Paul Menagebd89aab2007-10-18 23:40:44 -07002874 err = cgroup_add_files(cgrp, NULL, files, ARRAY_SIZE(files));
Paul Menagebbcb81d2007-10-18 23:39:32 -07002875 if (err < 0)
2876 return err;
2877
Paul Menagebd89aab2007-10-18 23:40:44 -07002878 if (cgrp == cgrp->top_cgroup) {
2879 if ((err = cgroup_add_file(cgrp, NULL, &cft_release_agent)) < 0)
Paul Menage81a6a5c2007-10-18 23:39:38 -07002880 return err;
2881 }
2882
Paul Menagebd89aab2007-10-18 23:40:44 -07002883 for_each_subsys(cgrp->root, ss) {
2884 if (ss->populate && (err = ss->populate(ss, cgrp)) < 0)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002885 return err;
2886 }
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07002887 /* This cgroup is ready now */
2888 for_each_subsys(cgrp->root, ss) {
2889 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
2890 /*
2891 * Update id->css pointer and make this css visible from
2892 * CSS ID functions. This pointer will be dereferened
2893 * from RCU-read-side without locks.
2894 */
2895 if (css->id)
2896 rcu_assign_pointer(css->id->css, css);
2897 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002898
2899 return 0;
2900}
2901
2902static void init_cgroup_css(struct cgroup_subsys_state *css,
2903 struct cgroup_subsys *ss,
Paul Menagebd89aab2007-10-18 23:40:44 -07002904 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002905{
Paul Menagebd89aab2007-10-18 23:40:44 -07002906 css->cgroup = cgrp;
Paul Menagee7c5ec92009-01-07 18:08:38 -08002907 atomic_set(&css->refcnt, 1);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002908 css->flags = 0;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07002909 css->id = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07002910 if (cgrp == dummytop)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002911 set_bit(CSS_ROOT, &css->flags);
Paul Menagebd89aab2007-10-18 23:40:44 -07002912 BUG_ON(cgrp->subsys[ss->subsys_id]);
2913 cgrp->subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002914}
2915
Paul Menage999cd8a2009-01-07 18:08:36 -08002916static void cgroup_lock_hierarchy(struct cgroupfs_root *root)
2917{
2918 /* We need to take each hierarchy_mutex in a consistent order */
2919 int i;
2920
2921 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2922 struct cgroup_subsys *ss = subsys[i];
2923 if (ss->root == root)
Li Zefancfebe562009-02-11 13:04:36 -08002924 mutex_lock(&ss->hierarchy_mutex);
Paul Menage999cd8a2009-01-07 18:08:36 -08002925 }
2926}
2927
2928static void cgroup_unlock_hierarchy(struct cgroupfs_root *root)
2929{
2930 int i;
2931
2932 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2933 struct cgroup_subsys *ss = subsys[i];
2934 if (ss->root == root)
2935 mutex_unlock(&ss->hierarchy_mutex);
2936 }
2937}
2938
Paul Menageddbcc7e2007-10-18 23:39:30 -07002939/*
Li Zefana043e3b2008-02-23 15:24:09 -08002940 * cgroup_create - create a cgroup
2941 * @parent: cgroup that will be parent of the new cgroup
2942 * @dentry: dentry of the new cgroup
2943 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07002944 *
Li Zefana043e3b2008-02-23 15:24:09 -08002945 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07002946 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07002947static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Li Zefan099fca32009-04-02 16:57:29 -07002948 mode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002949{
Paul Menagebd89aab2007-10-18 23:40:44 -07002950 struct cgroup *cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002951 struct cgroupfs_root *root = parent->root;
2952 int err = 0;
2953 struct cgroup_subsys *ss;
2954 struct super_block *sb = root->sb;
2955
Paul Menagebd89aab2007-10-18 23:40:44 -07002956 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
2957 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002958 return -ENOMEM;
2959
2960 /* Grab a reference on the superblock so the hierarchy doesn't
2961 * get deleted on unmount if there are child cgroups. This
2962 * can be done outside cgroup_mutex, since the sb can't
2963 * disappear while someone has an open control file on the
2964 * fs */
2965 atomic_inc(&sb->s_active);
2966
2967 mutex_lock(&cgroup_mutex);
2968
Paul Menagecc31edc2008-10-18 20:28:04 -07002969 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002970
Paul Menagebd89aab2007-10-18 23:40:44 -07002971 cgrp->parent = parent;
2972 cgrp->root = parent->root;
2973 cgrp->top_cgroup = parent->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002974
Li Zefanb6abdb02008-03-04 14:28:19 -08002975 if (notify_on_release(parent))
2976 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
2977
Paul Menageddbcc7e2007-10-18 23:39:30 -07002978 for_each_subsys(root, ss) {
Paul Menagebd89aab2007-10-18 23:40:44 -07002979 struct cgroup_subsys_state *css = ss->create(ss, cgrp);
Li Zefan4528fd02010-02-02 13:44:10 -08002980
Paul Menageddbcc7e2007-10-18 23:39:30 -07002981 if (IS_ERR(css)) {
2982 err = PTR_ERR(css);
2983 goto err_destroy;
2984 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002985 init_cgroup_css(css, ss, cgrp);
Li Zefan4528fd02010-02-02 13:44:10 -08002986 if (ss->use_id) {
2987 err = alloc_css_id(ss, parent, cgrp);
2988 if (err)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07002989 goto err_destroy;
Li Zefan4528fd02010-02-02 13:44:10 -08002990 }
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07002991 /* At error, ->destroy() callback has to free assigned ID. */
Paul Menageddbcc7e2007-10-18 23:39:30 -07002992 }
2993
Paul Menage999cd8a2009-01-07 18:08:36 -08002994 cgroup_lock_hierarchy(root);
Paul Menagebd89aab2007-10-18 23:40:44 -07002995 list_add(&cgrp->sibling, &cgrp->parent->children);
Paul Menage999cd8a2009-01-07 18:08:36 -08002996 cgroup_unlock_hierarchy(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002997 root->number_of_cgroups++;
2998
Paul Menagebd89aab2007-10-18 23:40:44 -07002999 err = cgroup_create_dir(cgrp, dentry, mode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003000 if (err < 0)
3001 goto err_remove;
3002
3003 /* The cgroup directory was pre-locked for us */
Paul Menagebd89aab2007-10-18 23:40:44 -07003004 BUG_ON(!mutex_is_locked(&cgrp->dentry->d_inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07003005
Paul Menagebd89aab2007-10-18 23:40:44 -07003006 err = cgroup_populate_dir(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003007 /* If err < 0, we have a half-filled directory - oh well ;) */
3008
3009 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07003010 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003011
3012 return 0;
3013
3014 err_remove:
3015
KAMEZAWA Hiroyukibaef99a2009-01-29 14:25:10 -08003016 cgroup_lock_hierarchy(root);
Paul Menagebd89aab2007-10-18 23:40:44 -07003017 list_del(&cgrp->sibling);
KAMEZAWA Hiroyukibaef99a2009-01-29 14:25:10 -08003018 cgroup_unlock_hierarchy(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003019 root->number_of_cgroups--;
3020
3021 err_destroy:
3022
3023 for_each_subsys(root, ss) {
Paul Menagebd89aab2007-10-18 23:40:44 -07003024 if (cgrp->subsys[ss->subsys_id])
3025 ss->destroy(ss, cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003026 }
3027
3028 mutex_unlock(&cgroup_mutex);
3029
3030 /* Release the reference count that we took on the superblock */
3031 deactivate_super(sb);
3032
Paul Menagebd89aab2007-10-18 23:40:44 -07003033 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003034 return err;
3035}
3036
3037static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode)
3038{
3039 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
3040
3041 /* the vfs holds inode->i_mutex already */
3042 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
3043}
3044
Li Zefan55b6fd02008-07-29 22:33:20 -07003045static int cgroup_has_css_refs(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003046{
3047 /* Check the reference count on each subsystem. Since we
3048 * already established that there are no tasks in the
Paul Menagee7c5ec92009-01-07 18:08:38 -08003049 * cgroup, if the css refcount is also 1, then there should
Paul Menage81a6a5c2007-10-18 23:39:38 -07003050 * be no outstanding references, so the subsystem is safe to
3051 * destroy. We scan across all subsystems rather than using
3052 * the per-hierarchy linked list of mounted subsystems since
3053 * we can be called via check_for_release() with no
3054 * synchronization other than RCU, and the subsystem linked
3055 * list isn't RCU-safe */
3056 int i;
3057 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3058 struct cgroup_subsys *ss = subsys[i];
3059 struct cgroup_subsys_state *css;
3060 /* Skip subsystems not in this hierarchy */
Paul Menagebd89aab2007-10-18 23:40:44 -07003061 if (ss->root != cgrp->root)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003062 continue;
Paul Menagebd89aab2007-10-18 23:40:44 -07003063 css = cgrp->subsys[ss->subsys_id];
Paul Menage81a6a5c2007-10-18 23:39:38 -07003064 /* When called from check_for_release() it's possible
3065 * that by this point the cgroup has been removed
3066 * and the css deleted. But a false-positive doesn't
3067 * matter, since it can only happen if the cgroup
3068 * has been deleted and hence no longer needs the
3069 * release agent to be called anyway. */
Paul Menagee7c5ec92009-01-07 18:08:38 -08003070 if (css && (atomic_read(&css->refcnt) > 1))
Paul Menage81a6a5c2007-10-18 23:39:38 -07003071 return 1;
Paul Menage81a6a5c2007-10-18 23:39:38 -07003072 }
3073 return 0;
3074}
3075
Paul Menagee7c5ec92009-01-07 18:08:38 -08003076/*
3077 * Atomically mark all (or else none) of the cgroup's CSS objects as
3078 * CSS_REMOVED. Return true on success, or false if the cgroup has
3079 * busy subsystems. Call with cgroup_mutex held
3080 */
3081
3082static int cgroup_clear_css_refs(struct cgroup *cgrp)
3083{
3084 struct cgroup_subsys *ss;
3085 unsigned long flags;
3086 bool failed = false;
3087 local_irq_save(flags);
3088 for_each_subsys(cgrp->root, ss) {
3089 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
3090 int refcnt;
Paul Menage804b3c22009-01-29 14:25:21 -08003091 while (1) {
Paul Menagee7c5ec92009-01-07 18:08:38 -08003092 /* We can only remove a CSS with a refcnt==1 */
3093 refcnt = atomic_read(&css->refcnt);
3094 if (refcnt > 1) {
3095 failed = true;
3096 goto done;
3097 }
3098 BUG_ON(!refcnt);
3099 /*
3100 * Drop the refcnt to 0 while we check other
3101 * subsystems. This will cause any racing
3102 * css_tryget() to spin until we set the
3103 * CSS_REMOVED bits or abort
3104 */
Paul Menage804b3c22009-01-29 14:25:21 -08003105 if (atomic_cmpxchg(&css->refcnt, refcnt, 0) == refcnt)
3106 break;
3107 cpu_relax();
3108 }
Paul Menagee7c5ec92009-01-07 18:08:38 -08003109 }
3110 done:
3111 for_each_subsys(cgrp->root, ss) {
3112 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
3113 if (failed) {
3114 /*
3115 * Restore old refcnt if we previously managed
3116 * to clear it from 1 to 0
3117 */
3118 if (!atomic_read(&css->refcnt))
3119 atomic_set(&css->refcnt, 1);
3120 } else {
3121 /* Commit the fact that the CSS is removed */
3122 set_bit(CSS_REMOVED, &css->flags);
3123 }
3124 }
3125 local_irq_restore(flags);
3126 return !failed;
3127}
3128
Paul Menageddbcc7e2007-10-18 23:39:30 -07003129static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
3130{
Paul Menagebd89aab2007-10-18 23:40:44 -07003131 struct cgroup *cgrp = dentry->d_fsdata;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003132 struct dentry *d;
3133 struct cgroup *parent;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003134 DEFINE_WAIT(wait);
3135 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003136
3137 /* the vfs holds both inode->i_mutex already */
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003138again:
Paul Menageddbcc7e2007-10-18 23:39:30 -07003139 mutex_lock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07003140 if (atomic_read(&cgrp->count) != 0) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07003141 mutex_unlock(&cgroup_mutex);
3142 return -EBUSY;
3143 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003144 if (!list_empty(&cgrp->children)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07003145 mutex_unlock(&cgroup_mutex);
3146 return -EBUSY;
3147 }
KAMEZAWA Hiroyuki3fa59df2008-11-19 15:36:34 -08003148 mutex_unlock(&cgroup_mutex);
Li Zefana043e3b2008-02-23 15:24:09 -08003149
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -08003150 /*
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003151 * In general, subsystem has no css->refcnt after pre_destroy(). But
3152 * in racy cases, subsystem may have to get css->refcnt after
3153 * pre_destroy() and it makes rmdir return with -EBUSY. This sometimes
3154 * make rmdir return -EBUSY too often. To avoid that, we use waitqueue
3155 * for cgroup's rmdir. CGRP_WAIT_ON_RMDIR is for synchronizing rmdir
3156 * and subsystem's reference count handling. Please see css_get/put
3157 * and css_tryget() and cgroup_wakeup_rmdir_waiter() implementation.
3158 */
3159 set_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
3160
3161 /*
Li Zefana043e3b2008-02-23 15:24:09 -08003162 * Call pre_destroy handlers of subsys. Notify subsystems
3163 * that rmdir() request comes.
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -08003164 */
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003165 ret = cgroup_call_pre_destroy(cgrp);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003166 if (ret) {
3167 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003168 return ret;
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003169 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003170
KAMEZAWA Hiroyuki3fa59df2008-11-19 15:36:34 -08003171 mutex_lock(&cgroup_mutex);
3172 parent = cgrp->parent;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003173 if (atomic_read(&cgrp->count) || !list_empty(&cgrp->children)) {
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003174 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003175 mutex_unlock(&cgroup_mutex);
3176 return -EBUSY;
3177 }
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003178 prepare_to_wait(&cgroup_rmdir_waitq, &wait, TASK_INTERRUPTIBLE);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003179 if (!cgroup_clear_css_refs(cgrp)) {
3180 mutex_unlock(&cgroup_mutex);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003181 /*
3182 * Because someone may call cgroup_wakeup_rmdir_waiter() before
3183 * prepare_to_wait(), we need to check this flag.
3184 */
3185 if (test_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags))
3186 schedule();
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003187 finish_wait(&cgroup_rmdir_waitq, &wait);
3188 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
3189 if (signal_pending(current))
3190 return -EINTR;
3191 goto again;
3192 }
3193 /* NO css_tryget() can success after here. */
3194 finish_wait(&cgroup_rmdir_waitq, &wait);
3195 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003196
Paul Menage81a6a5c2007-10-18 23:39:38 -07003197 spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07003198 set_bit(CGRP_REMOVED, &cgrp->flags);
3199 if (!list_empty(&cgrp->release_list))
3200 list_del(&cgrp->release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003201 spin_unlock(&release_list_lock);
Paul Menage999cd8a2009-01-07 18:08:36 -08003202
3203 cgroup_lock_hierarchy(cgrp->root);
3204 /* delete this cgroup from parent->children */
Paul Menagebd89aab2007-10-18 23:40:44 -07003205 list_del(&cgrp->sibling);
Paul Menage999cd8a2009-01-07 18:08:36 -08003206 cgroup_unlock_hierarchy(cgrp->root);
3207
Paul Menagebd89aab2007-10-18 23:40:44 -07003208 spin_lock(&cgrp->dentry->d_lock);
3209 d = dget(cgrp->dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003210 spin_unlock(&d->d_lock);
3211
3212 cgroup_d_remove_dir(d);
3213 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003214
Paul Menagebd89aab2007-10-18 23:40:44 -07003215 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003216 check_for_release(parent);
3217
Paul Menageddbcc7e2007-10-18 23:39:30 -07003218 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003219 return 0;
3220}
3221
Li Zefan06a11922008-04-29 01:00:07 -07003222static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003223{
Paul Menageddbcc7e2007-10-18 23:39:30 -07003224 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08003225
3226 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003227
3228 /* Create the top cgroup state for this subsystem */
Li Zefan33a68ac2009-01-07 18:07:42 -08003229 list_add(&ss->sibling, &rootnode.subsys_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003230 ss->root = &rootnode;
3231 css = ss->create(ss, dummytop);
3232 /* We don't handle early failures gracefully */
3233 BUG_ON(IS_ERR(css));
3234 init_cgroup_css(css, ss, dummytop);
3235
Li Zefane8d55fd2008-04-29 01:00:13 -07003236 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07003237 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07003238 * newly registered, all tasks and hence the
3239 * init_css_set is in the subsystem's top cgroup. */
3240 init_css_set.subsys[ss->subsys_id] = dummytop->subsys[ss->subsys_id];
Paul Menageddbcc7e2007-10-18 23:39:30 -07003241
3242 need_forkexit_callback |= ss->fork || ss->exit;
3243
Li Zefane8d55fd2008-04-29 01:00:13 -07003244 /* At system boot, before all subsystems have been
3245 * registered, no tasks have been forked, so we don't
3246 * need to invoke fork callbacks here. */
3247 BUG_ON(!list_empty(&init_task.tasks));
3248
Paul Menage999cd8a2009-01-07 18:08:36 -08003249 mutex_init(&ss->hierarchy_mutex);
Li Zefancfebe562009-02-11 13:04:36 -08003250 lockdep_set_class(&ss->hierarchy_mutex, &ss->subsys_key);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003251 ss->active = 1;
3252}
3253
3254/**
Li Zefana043e3b2008-02-23 15:24:09 -08003255 * cgroup_init_early - cgroup initialization at system boot
3256 *
3257 * Initialize cgroups at system boot, and initialize any
3258 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07003259 */
3260int __init cgroup_init_early(void)
3261{
3262 int i;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07003263 atomic_set(&init_css_set.refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -07003264 INIT_LIST_HEAD(&init_css_set.cg_links);
3265 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07003266 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07003267 css_set_count = 1;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003268 init_cgroup_root(&rootnode);
Paul Menage817929e2007-10-18 23:39:36 -07003269 root_count = 1;
3270 init_task.cgroups = &init_css_set;
3271
3272 init_css_set_link.cg = &init_css_set;
Paul Menage7717f7b2009-09-23 15:56:22 -07003273 init_css_set_link.cgrp = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -07003274 list_add(&init_css_set_link.cgrp_link_list,
Paul Menage817929e2007-10-18 23:39:36 -07003275 &rootnode.top_cgroup.css_sets);
3276 list_add(&init_css_set_link.cg_link_list,
3277 &init_css_set.cg_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003278
Li Zefan472b1052008-04-29 01:00:11 -07003279 for (i = 0; i < CSS_SET_TABLE_SIZE; i++)
3280 INIT_HLIST_HEAD(&css_set_table[i]);
3281
Paul Menageddbcc7e2007-10-18 23:39:30 -07003282 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3283 struct cgroup_subsys *ss = subsys[i];
3284
3285 BUG_ON(!ss->name);
3286 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
3287 BUG_ON(!ss->create);
3288 BUG_ON(!ss->destroy);
3289 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08003290 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07003291 ss->name, ss->subsys_id);
3292 BUG();
3293 }
3294
3295 if (ss->early_init)
3296 cgroup_init_subsys(ss);
3297 }
3298 return 0;
3299}
3300
3301/**
Li Zefana043e3b2008-02-23 15:24:09 -08003302 * cgroup_init - cgroup initialization
3303 *
3304 * Register cgroup filesystem and /proc file, and initialize
3305 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07003306 */
3307int __init cgroup_init(void)
3308{
3309 int err;
3310 int i;
Li Zefan472b1052008-04-29 01:00:11 -07003311 struct hlist_head *hhead;
Paul Menagea4243162007-10-18 23:39:35 -07003312
3313 err = bdi_init(&cgroup_backing_dev_info);
3314 if (err)
3315 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003316
3317 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3318 struct cgroup_subsys *ss = subsys[i];
3319 if (!ss->early_init)
3320 cgroup_init_subsys(ss);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07003321 if (ss->use_id)
3322 cgroup_subsys_init_idr(ss);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003323 }
3324
Li Zefan472b1052008-04-29 01:00:11 -07003325 /* Add init_css_set to the hash table */
3326 hhead = css_set_hash(init_css_set.subsys);
3327 hlist_add_head(&init_css_set.hlist, hhead);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07003328 BUG_ON(!init_root_id(&rootnode));
Paul Menageddbcc7e2007-10-18 23:39:30 -07003329 err = register_filesystem(&cgroup_fs_type);
3330 if (err < 0)
3331 goto out;
3332
Li Zefan46ae2202008-04-29 01:00:08 -07003333 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07003334
Paul Menageddbcc7e2007-10-18 23:39:30 -07003335out:
Paul Menagea4243162007-10-18 23:39:35 -07003336 if (err)
3337 bdi_destroy(&cgroup_backing_dev_info);
3338
Paul Menageddbcc7e2007-10-18 23:39:30 -07003339 return err;
3340}
Paul Menageb4f48b62007-10-18 23:39:33 -07003341
Paul Menagea4243162007-10-18 23:39:35 -07003342/*
3343 * proc_cgroup_show()
3344 * - Print task's cgroup paths into seq_file, one line for each hierarchy
3345 * - Used for /proc/<pid>/cgroup.
3346 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
3347 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08003348 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07003349 * anyway. No need to check that tsk->cgroup != NULL, thanks to
3350 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
3351 * cgroup to top_cgroup.
3352 */
3353
3354/* TODO: Use a proper seq_file iterator */
3355static int proc_cgroup_show(struct seq_file *m, void *v)
3356{
3357 struct pid *pid;
3358 struct task_struct *tsk;
3359 char *buf;
3360 int retval;
3361 struct cgroupfs_root *root;
3362
3363 retval = -ENOMEM;
3364 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3365 if (!buf)
3366 goto out;
3367
3368 retval = -ESRCH;
3369 pid = m->private;
3370 tsk = get_pid_task(pid, PIDTYPE_PID);
3371 if (!tsk)
3372 goto out_free;
3373
3374 retval = 0;
3375
3376 mutex_lock(&cgroup_mutex);
3377
Li Zefane5f6a862009-01-07 18:07:41 -08003378 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07003379 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07003380 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07003381 int count = 0;
3382
Paul Menage2c6ab6d2009-09-23 15:56:23 -07003383 seq_printf(m, "%d:", root->hierarchy_id);
Paul Menagea4243162007-10-18 23:39:35 -07003384 for_each_subsys(root, ss)
3385 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07003386 if (strlen(root->name))
3387 seq_printf(m, "%sname=%s", count ? "," : "",
3388 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07003389 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07003390 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07003391 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07003392 if (retval < 0)
3393 goto out_unlock;
3394 seq_puts(m, buf);
3395 seq_putc(m, '\n');
3396 }
3397
3398out_unlock:
3399 mutex_unlock(&cgroup_mutex);
3400 put_task_struct(tsk);
3401out_free:
3402 kfree(buf);
3403out:
3404 return retval;
3405}
3406
3407static int cgroup_open(struct inode *inode, struct file *file)
3408{
3409 struct pid *pid = PROC_I(inode)->pid;
3410 return single_open(file, proc_cgroup_show, pid);
3411}
3412
Alexey Dobriyan828c0952009-10-01 15:43:56 -07003413const struct file_operations proc_cgroup_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07003414 .open = cgroup_open,
3415 .read = seq_read,
3416 .llseek = seq_lseek,
3417 .release = single_release,
3418};
3419
3420/* Display information about each subsystem and each hierarchy */
3421static int proc_cgroupstats_show(struct seq_file *m, void *v)
3422{
3423 int i;
Paul Menagea4243162007-10-18 23:39:35 -07003424
Paul Menage8bab8dd2008-04-04 14:29:57 -07003425 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Paul Menagea4243162007-10-18 23:39:35 -07003426 mutex_lock(&cgroup_mutex);
Paul Menagea4243162007-10-18 23:39:35 -07003427 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3428 struct cgroup_subsys *ss = subsys[i];
Paul Menage2c6ab6d2009-09-23 15:56:23 -07003429 seq_printf(m, "%s\t%d\t%d\t%d\n",
3430 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07003431 ss->root->number_of_cgroups, !ss->disabled);
Paul Menagea4243162007-10-18 23:39:35 -07003432 }
3433 mutex_unlock(&cgroup_mutex);
3434 return 0;
3435}
3436
3437static int cgroupstats_open(struct inode *inode, struct file *file)
3438{
Al Viro9dce07f2008-03-29 03:07:28 +00003439 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07003440}
3441
Alexey Dobriyan828c0952009-10-01 15:43:56 -07003442static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07003443 .open = cgroupstats_open,
3444 .read = seq_read,
3445 .llseek = seq_lseek,
3446 .release = single_release,
3447};
3448
Paul Menageb4f48b62007-10-18 23:39:33 -07003449/**
3450 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08003451 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07003452 *
3453 * Description: A task inherits its parent's cgroup at fork().
3454 *
3455 * A pointer to the shared css_set was automatically copied in
3456 * fork.c by dup_task_struct(). However, we ignore that copy, since
3457 * it was not made under the protection of RCU or cgroup_mutex, so
Cliff Wickman956db3c2008-02-07 00:14:43 -08003458 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
Paul Menage817929e2007-10-18 23:39:36 -07003459 * have already changed current->cgroups, allowing the previously
3460 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07003461 *
3462 * At the point that cgroup_fork() is called, 'current' is the parent
3463 * task, and the passed argument 'child' points to the child task.
3464 */
3465void cgroup_fork(struct task_struct *child)
3466{
Paul Menage817929e2007-10-18 23:39:36 -07003467 task_lock(current);
3468 child->cgroups = current->cgroups;
3469 get_css_set(child->cgroups);
3470 task_unlock(current);
3471 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07003472}
3473
3474/**
Li Zefana043e3b2008-02-23 15:24:09 -08003475 * cgroup_fork_callbacks - run fork callbacks
3476 * @child: the new task
3477 *
3478 * Called on a new task very soon before adding it to the
3479 * tasklist. No need to take any locks since no-one can
3480 * be operating on this task.
Paul Menageb4f48b62007-10-18 23:39:33 -07003481 */
3482void cgroup_fork_callbacks(struct task_struct *child)
3483{
3484 if (need_forkexit_callback) {
3485 int i;
3486 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3487 struct cgroup_subsys *ss = subsys[i];
3488 if (ss->fork)
3489 ss->fork(ss, child);
3490 }
3491 }
3492}
3493
3494/**
Li Zefana043e3b2008-02-23 15:24:09 -08003495 * cgroup_post_fork - called on a new task after adding it to the task list
3496 * @child: the task in question
3497 *
3498 * Adds the task to the list running through its css_set if necessary.
3499 * Has to be after the task is visible on the task list in case we race
3500 * with the first call to cgroup_iter_start() - to guarantee that the
3501 * new task ends up on its list.
3502 */
Paul Menage817929e2007-10-18 23:39:36 -07003503void cgroup_post_fork(struct task_struct *child)
3504{
3505 if (use_task_css_set_links) {
3506 write_lock(&css_set_lock);
Lai Jiangshanb12b5332009-01-07 18:07:36 -08003507 task_lock(child);
Paul Menage817929e2007-10-18 23:39:36 -07003508 if (list_empty(&child->cg_list))
3509 list_add(&child->cg_list, &child->cgroups->tasks);
Lai Jiangshanb12b5332009-01-07 18:07:36 -08003510 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07003511 write_unlock(&css_set_lock);
3512 }
3513}
3514/**
Paul Menageb4f48b62007-10-18 23:39:33 -07003515 * cgroup_exit - detach cgroup from exiting task
3516 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08003517 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07003518 *
3519 * Description: Detach cgroup from @tsk and release it.
3520 *
3521 * Note that cgroups marked notify_on_release force every task in
3522 * them to take the global cgroup_mutex mutex when exiting.
3523 * This could impact scaling on very large systems. Be reluctant to
3524 * use notify_on_release cgroups where very high task exit scaling
3525 * is required on large systems.
3526 *
3527 * the_top_cgroup_hack:
3528 *
3529 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
3530 *
3531 * We call cgroup_exit() while the task is still competent to
3532 * handle notify_on_release(), then leave the task attached to the
3533 * root cgroup in each hierarchy for the remainder of its exit.
3534 *
3535 * To do this properly, we would increment the reference count on
3536 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
3537 * code we would add a second cgroup function call, to drop that
3538 * reference. This would just create an unnecessary hot spot on
3539 * the top_cgroup reference count, to no avail.
3540 *
3541 * Normally, holding a reference to a cgroup without bumping its
3542 * count is unsafe. The cgroup could go away, or someone could
3543 * attach us to a different cgroup, decrementing the count on
3544 * the first cgroup that we never incremented. But in this case,
3545 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08003546 * which wards off any cgroup_attach_task() attempts, or task is a failed
3547 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07003548 */
3549void cgroup_exit(struct task_struct *tsk, int run_callbacks)
3550{
3551 int i;
Paul Menage817929e2007-10-18 23:39:36 -07003552 struct css_set *cg;
Paul Menageb4f48b62007-10-18 23:39:33 -07003553
3554 if (run_callbacks && need_forkexit_callback) {
3555 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3556 struct cgroup_subsys *ss = subsys[i];
3557 if (ss->exit)
3558 ss->exit(ss, tsk);
3559 }
3560 }
Paul Menage817929e2007-10-18 23:39:36 -07003561
3562 /*
3563 * Unlink from the css_set task list if necessary.
3564 * Optimistically check cg_list before taking
3565 * css_set_lock
3566 */
3567 if (!list_empty(&tsk->cg_list)) {
3568 write_lock(&css_set_lock);
3569 if (!list_empty(&tsk->cg_list))
3570 list_del(&tsk->cg_list);
3571 write_unlock(&css_set_lock);
3572 }
3573
Paul Menageb4f48b62007-10-18 23:39:33 -07003574 /* Reassign the task to the init_css_set. */
3575 task_lock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07003576 cg = tsk->cgroups;
3577 tsk->cgroups = &init_css_set;
Paul Menageb4f48b62007-10-18 23:39:33 -07003578 task_unlock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07003579 if (cg)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003580 put_css_set_taskexit(cg);
Paul Menageb4f48b62007-10-18 23:39:33 -07003581}
Paul Menage697f4162007-10-18 23:39:34 -07003582
3583/**
Li Zefana043e3b2008-02-23 15:24:09 -08003584 * cgroup_clone - clone the cgroup the given subsystem is attached to
3585 * @tsk: the task to be moved
3586 * @subsys: the given subsystem
Serge E. Hallyne885dcd2008-07-25 01:47:06 -07003587 * @nodename: the name for the new cgroup
Li Zefana043e3b2008-02-23 15:24:09 -08003588 *
3589 * Duplicate the current cgroup in the hierarchy that the given
3590 * subsystem is attached to, and move this task into the new
3591 * child.
Paul Menage697f4162007-10-18 23:39:34 -07003592 */
Serge E. Hallyne885dcd2008-07-25 01:47:06 -07003593int cgroup_clone(struct task_struct *tsk, struct cgroup_subsys *subsys,
3594 char *nodename)
Paul Menage697f4162007-10-18 23:39:34 -07003595{
3596 struct dentry *dentry;
3597 int ret = 0;
Paul Menage697f4162007-10-18 23:39:34 -07003598 struct cgroup *parent, *child;
3599 struct inode *inode;
3600 struct css_set *cg;
3601 struct cgroupfs_root *root;
3602 struct cgroup_subsys *ss;
3603
3604 /* We shouldn't be called by an unregistered subsystem */
3605 BUG_ON(!subsys->active);
3606
3607 /* First figure out what hierarchy and cgroup we're dealing
3608 * with, and pin them so we can drop cgroup_mutex */
3609 mutex_lock(&cgroup_mutex);
3610 again:
3611 root = subsys->root;
3612 if (root == &rootnode) {
Paul Menage697f4162007-10-18 23:39:34 -07003613 mutex_unlock(&cgroup_mutex);
3614 return 0;
3615 }
Paul Menage697f4162007-10-18 23:39:34 -07003616
Paul Menage697f4162007-10-18 23:39:34 -07003617 /* Pin the hierarchy */
Li Zefan1404f062009-01-29 14:25:21 -08003618 if (!atomic_inc_not_zero(&root->sb->s_active)) {
Li Zefan7b574b72009-01-04 12:00:45 -08003619 /* We race with the final deactivate_super() */
3620 mutex_unlock(&cgroup_mutex);
3621 return 0;
3622 }
Paul Menage697f4162007-10-18 23:39:34 -07003623
Paul Menage817929e2007-10-18 23:39:36 -07003624 /* Keep the cgroup alive */
Li Zefan1404f062009-01-29 14:25:21 -08003625 task_lock(tsk);
3626 parent = task_cgroup(tsk, subsys->subsys_id);
3627 cg = tsk->cgroups;
Paul Menage817929e2007-10-18 23:39:36 -07003628 get_css_set(cg);
Lai Jiangshan104cbd52009-01-07 18:07:38 -08003629 task_unlock(tsk);
Li Zefan1404f062009-01-29 14:25:21 -08003630
Paul Menage697f4162007-10-18 23:39:34 -07003631 mutex_unlock(&cgroup_mutex);
3632
3633 /* Now do the VFS work to create a cgroup */
3634 inode = parent->dentry->d_inode;
3635
3636 /* Hold the parent directory mutex across this operation to
3637 * stop anyone else deleting the new cgroup */
3638 mutex_lock(&inode->i_mutex);
3639 dentry = lookup_one_len(nodename, parent->dentry, strlen(nodename));
3640 if (IS_ERR(dentry)) {
3641 printk(KERN_INFO
Diego Callejacfe36bd2007-11-14 16:58:54 -08003642 "cgroup: Couldn't allocate dentry for %s: %ld\n", nodename,
Paul Menage697f4162007-10-18 23:39:34 -07003643 PTR_ERR(dentry));
3644 ret = PTR_ERR(dentry);
3645 goto out_release;
3646 }
3647
3648 /* Create the cgroup directory, which also creates the cgroup */
Li Zefan75139b82009-01-07 18:07:33 -08003649 ret = vfs_mkdir(inode, dentry, 0755);
Paul Menagebd89aab2007-10-18 23:40:44 -07003650 child = __d_cgrp(dentry);
Paul Menage697f4162007-10-18 23:39:34 -07003651 dput(dentry);
3652 if (ret) {
3653 printk(KERN_INFO
3654 "Failed to create cgroup %s: %d\n", nodename,
3655 ret);
3656 goto out_release;
3657 }
3658
Paul Menage697f4162007-10-18 23:39:34 -07003659 /* The cgroup now exists. Retake cgroup_mutex and check
3660 * that we're still in the same state that we thought we
3661 * were. */
3662 mutex_lock(&cgroup_mutex);
3663 if ((root != subsys->root) ||
3664 (parent != task_cgroup(tsk, subsys->subsys_id))) {
3665 /* Aargh, we raced ... */
3666 mutex_unlock(&inode->i_mutex);
Paul Menage817929e2007-10-18 23:39:36 -07003667 put_css_set(cg);
Paul Menage697f4162007-10-18 23:39:34 -07003668
Li Zefan1404f062009-01-29 14:25:21 -08003669 deactivate_super(root->sb);
Paul Menage697f4162007-10-18 23:39:34 -07003670 /* The cgroup is still accessible in the VFS, but
3671 * we're not going to try to rmdir() it at this
3672 * point. */
3673 printk(KERN_INFO
3674 "Race in cgroup_clone() - leaking cgroup %s\n",
3675 nodename);
3676 goto again;
3677 }
3678
3679 /* do any required auto-setup */
3680 for_each_subsys(root, ss) {
3681 if (ss->post_clone)
3682 ss->post_clone(ss, child);
3683 }
3684
3685 /* All seems fine. Finish by moving the task into the new cgroup */
Cliff Wickman956db3c2008-02-07 00:14:43 -08003686 ret = cgroup_attach_task(child, tsk);
Paul Menage697f4162007-10-18 23:39:34 -07003687 mutex_unlock(&cgroup_mutex);
3688
3689 out_release:
3690 mutex_unlock(&inode->i_mutex);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003691
3692 mutex_lock(&cgroup_mutex);
Paul Menage817929e2007-10-18 23:39:36 -07003693 put_css_set(cg);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003694 mutex_unlock(&cgroup_mutex);
Li Zefan1404f062009-01-29 14:25:21 -08003695 deactivate_super(root->sb);
Paul Menage697f4162007-10-18 23:39:34 -07003696 return ret;
3697}
3698
Li Zefana043e3b2008-02-23 15:24:09 -08003699/**
Grzegorz Nosek313e9242009-04-02 16:57:23 -07003700 * cgroup_is_descendant - see if @cgrp is a descendant of @task's cgrp
Li Zefana043e3b2008-02-23 15:24:09 -08003701 * @cgrp: the cgroup in question
Grzegorz Nosek313e9242009-04-02 16:57:23 -07003702 * @task: the task in question
Li Zefana043e3b2008-02-23 15:24:09 -08003703 *
Grzegorz Nosek313e9242009-04-02 16:57:23 -07003704 * See if @cgrp is a descendant of @task's cgroup in the appropriate
3705 * hierarchy.
Paul Menage697f4162007-10-18 23:39:34 -07003706 *
3707 * If we are sending in dummytop, then presumably we are creating
3708 * the top cgroup in the subsystem.
3709 *
3710 * Called only by the ns (nsproxy) cgroup.
3711 */
Grzegorz Nosek313e9242009-04-02 16:57:23 -07003712int cgroup_is_descendant(const struct cgroup *cgrp, struct task_struct *task)
Paul Menage697f4162007-10-18 23:39:34 -07003713{
3714 int ret;
3715 struct cgroup *target;
Paul Menage697f4162007-10-18 23:39:34 -07003716
Paul Menagebd89aab2007-10-18 23:40:44 -07003717 if (cgrp == dummytop)
Paul Menage697f4162007-10-18 23:39:34 -07003718 return 1;
3719
Paul Menage7717f7b2009-09-23 15:56:22 -07003720 target = task_cgroup_from_root(task, cgrp->root);
Paul Menagebd89aab2007-10-18 23:40:44 -07003721 while (cgrp != target && cgrp!= cgrp->top_cgroup)
3722 cgrp = cgrp->parent;
3723 ret = (cgrp == target);
Paul Menage697f4162007-10-18 23:39:34 -07003724 return ret;
3725}
Paul Menage81a6a5c2007-10-18 23:39:38 -07003726
Paul Menagebd89aab2007-10-18 23:40:44 -07003727static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003728{
3729 /* All of these checks rely on RCU to keep the cgroup
3730 * structure alive */
Paul Menagebd89aab2007-10-18 23:40:44 -07003731 if (cgroup_is_releasable(cgrp) && !atomic_read(&cgrp->count)
3732 && list_empty(&cgrp->children) && !cgroup_has_css_refs(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07003733 /* Control Group is currently removeable. If it's not
3734 * already queued for a userspace notification, queue
3735 * it now */
3736 int need_schedule_work = 0;
3737 spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07003738 if (!cgroup_is_removed(cgrp) &&
3739 list_empty(&cgrp->release_list)) {
3740 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003741 need_schedule_work = 1;
3742 }
3743 spin_unlock(&release_list_lock);
3744 if (need_schedule_work)
3745 schedule_work(&release_agent_work);
3746 }
3747}
3748
Daisuke Nishimurad7b9fff2010-03-10 15:22:05 -08003749/* Caller must verify that the css is not for root cgroup */
3750void __css_put(struct cgroup_subsys_state *css, int count)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003751{
Paul Menagebd89aab2007-10-18 23:40:44 -07003752 struct cgroup *cgrp = css->cgroup;
KAMEZAWA Hiroyuki3dece832009-10-01 15:44:09 -07003753 int val;
Paul Menage81a6a5c2007-10-18 23:39:38 -07003754 rcu_read_lock();
Daisuke Nishimurad7b9fff2010-03-10 15:22:05 -08003755 val = atomic_sub_return(count, &css->refcnt);
KAMEZAWA Hiroyuki3dece832009-10-01 15:44:09 -07003756 if (val == 1) {
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003757 if (notify_on_release(cgrp)) {
3758 set_bit(CGRP_RELEASABLE, &cgrp->flags);
3759 check_for_release(cgrp);
3760 }
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003761 cgroup_wakeup_rmdir_waiter(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003762 }
3763 rcu_read_unlock();
KAMEZAWA Hiroyuki3dece832009-10-01 15:44:09 -07003764 WARN_ON_ONCE(val < 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003765}
3766
3767/*
3768 * Notify userspace when a cgroup is released, by running the
3769 * configured release agent with the name of the cgroup (path
3770 * relative to the root of cgroup file system) as the argument.
3771 *
3772 * Most likely, this user command will try to rmdir this cgroup.
3773 *
3774 * This races with the possibility that some other task will be
3775 * attached to this cgroup before it is removed, or that some other
3776 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
3777 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
3778 * unused, and this cgroup will be reprieved from its death sentence,
3779 * to continue to serve a useful existence. Next time it's released,
3780 * we will get notified again, if it still has 'notify_on_release' set.
3781 *
3782 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
3783 * means only wait until the task is successfully execve()'d. The
3784 * separate release agent task is forked by call_usermodehelper(),
3785 * then control in this thread returns here, without waiting for the
3786 * release agent task. We don't bother to wait because the caller of
3787 * this routine has no use for the exit status of the release agent
3788 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07003789 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07003790static void cgroup_release_agent(struct work_struct *work)
3791{
3792 BUG_ON(work != &release_agent_work);
3793 mutex_lock(&cgroup_mutex);
3794 spin_lock(&release_list_lock);
3795 while (!list_empty(&release_list)) {
3796 char *argv[3], *envp[3];
3797 int i;
Paul Menagee788e062008-07-25 01:46:59 -07003798 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003799 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003800 struct cgroup,
3801 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07003802 list_del_init(&cgrp->release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003803 spin_unlock(&release_list_lock);
3804 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07003805 if (!pathbuf)
3806 goto continue_free;
3807 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
3808 goto continue_free;
3809 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
3810 if (!agentbuf)
3811 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07003812
3813 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07003814 argv[i++] = agentbuf;
3815 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07003816 argv[i] = NULL;
3817
3818 i = 0;
3819 /* minimal command environment */
3820 envp[i++] = "HOME=/";
3821 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
3822 envp[i] = NULL;
3823
3824 /* Drop the lock while we invoke the usermode helper,
3825 * since the exec could involve hitting disk and hence
3826 * be a slow process */
3827 mutex_unlock(&cgroup_mutex);
3828 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003829 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07003830 continue_free:
3831 kfree(pathbuf);
3832 kfree(agentbuf);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003833 spin_lock(&release_list_lock);
3834 }
3835 spin_unlock(&release_list_lock);
3836 mutex_unlock(&cgroup_mutex);
3837}
Paul Menage8bab8dd2008-04-04 14:29:57 -07003838
3839static int __init cgroup_disable(char *str)
3840{
3841 int i;
3842 char *token;
3843
3844 while ((token = strsep(&str, ",")) != NULL) {
3845 if (!*token)
3846 continue;
3847
3848 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3849 struct cgroup_subsys *ss = subsys[i];
3850
3851 if (!strcmp(token, ss->name)) {
3852 ss->disabled = 1;
3853 printk(KERN_INFO "Disabling %s control group"
3854 " subsystem\n", ss->name);
3855 break;
3856 }
3857 }
3858 }
3859 return 1;
3860}
3861__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07003862
3863/*
3864 * Functons for CSS ID.
3865 */
3866
3867/*
3868 *To get ID other than 0, this should be called when !cgroup_is_removed().
3869 */
3870unsigned short css_id(struct cgroup_subsys_state *css)
3871{
3872 struct css_id *cssid = rcu_dereference(css->id);
3873
3874 if (cssid)
3875 return cssid->id;
3876 return 0;
3877}
3878
3879unsigned short css_depth(struct cgroup_subsys_state *css)
3880{
3881 struct css_id *cssid = rcu_dereference(css->id);
3882
3883 if (cssid)
3884 return cssid->depth;
3885 return 0;
3886}
3887
3888bool css_is_ancestor(struct cgroup_subsys_state *child,
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07003889 const struct cgroup_subsys_state *root)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07003890{
3891 struct css_id *child_id = rcu_dereference(child->id);
3892 struct css_id *root_id = rcu_dereference(root->id);
3893
3894 if (!child_id || !root_id || (child_id->depth < root_id->depth))
3895 return false;
3896 return child_id->stack[root_id->depth] == root_id->id;
3897}
3898
3899static void __free_css_id_cb(struct rcu_head *head)
3900{
3901 struct css_id *id;
3902
3903 id = container_of(head, struct css_id, rcu_head);
3904 kfree(id);
3905}
3906
3907void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
3908{
3909 struct css_id *id = css->id;
3910 /* When this is called before css_id initialization, id can be NULL */
3911 if (!id)
3912 return;
3913
3914 BUG_ON(!ss->use_id);
3915
3916 rcu_assign_pointer(id->css, NULL);
3917 rcu_assign_pointer(css->id, NULL);
3918 spin_lock(&ss->id_lock);
3919 idr_remove(&ss->idr, id->id);
3920 spin_unlock(&ss->id_lock);
3921 call_rcu(&id->rcu_head, __free_css_id_cb);
3922}
3923
3924/*
3925 * This is called by init or create(). Then, calls to this function are
3926 * always serialized (By cgroup_mutex() at create()).
3927 */
3928
3929static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
3930{
3931 struct css_id *newid;
3932 int myid, error, size;
3933
3934 BUG_ON(!ss->use_id);
3935
3936 size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
3937 newid = kzalloc(size, GFP_KERNEL);
3938 if (!newid)
3939 return ERR_PTR(-ENOMEM);
3940 /* get id */
3941 if (unlikely(!idr_pre_get(&ss->idr, GFP_KERNEL))) {
3942 error = -ENOMEM;
3943 goto err_out;
3944 }
3945 spin_lock(&ss->id_lock);
3946 /* Don't use 0. allocates an ID of 1-65535 */
3947 error = idr_get_new_above(&ss->idr, newid, 1, &myid);
3948 spin_unlock(&ss->id_lock);
3949
3950 /* Returns error when there are no free spaces for new ID.*/
3951 if (error) {
3952 error = -ENOSPC;
3953 goto err_out;
3954 }
3955 if (myid > CSS_ID_MAX)
3956 goto remove_idr;
3957
3958 newid->id = myid;
3959 newid->depth = depth;
3960 return newid;
3961remove_idr:
3962 error = -ENOSPC;
3963 spin_lock(&ss->id_lock);
3964 idr_remove(&ss->idr, myid);
3965 spin_unlock(&ss->id_lock);
3966err_out:
3967 kfree(newid);
3968 return ERR_PTR(error);
3969
3970}
3971
3972static int __init cgroup_subsys_init_idr(struct cgroup_subsys *ss)
3973{
3974 struct css_id *newid;
3975 struct cgroup_subsys_state *rootcss;
3976
3977 spin_lock_init(&ss->id_lock);
3978 idr_init(&ss->idr);
3979
3980 rootcss = init_css_set.subsys[ss->subsys_id];
3981 newid = get_new_cssid(ss, 0);
3982 if (IS_ERR(newid))
3983 return PTR_ERR(newid);
3984
3985 newid->stack[0] = newid->id;
3986 newid->css = rootcss;
3987 rootcss->id = newid;
3988 return 0;
3989}
3990
3991static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent,
3992 struct cgroup *child)
3993{
3994 int subsys_id, i, depth = 0;
3995 struct cgroup_subsys_state *parent_css, *child_css;
3996 struct css_id *child_id, *parent_id = NULL;
3997
3998 subsys_id = ss->subsys_id;
3999 parent_css = parent->subsys[subsys_id];
4000 child_css = child->subsys[subsys_id];
4001 depth = css_depth(parent_css) + 1;
4002 parent_id = parent_css->id;
4003
4004 child_id = get_new_cssid(ss, depth);
4005 if (IS_ERR(child_id))
4006 return PTR_ERR(child_id);
4007
4008 for (i = 0; i < depth; i++)
4009 child_id->stack[i] = parent_id->stack[i];
4010 child_id->stack[depth] = child_id->id;
4011 /*
4012 * child_id->css pointer will be set after this cgroup is available
4013 * see cgroup_populate_dir()
4014 */
4015 rcu_assign_pointer(child_css->id, child_id);
4016
4017 return 0;
4018}
4019
4020/**
4021 * css_lookup - lookup css by id
4022 * @ss: cgroup subsys to be looked into.
4023 * @id: the id
4024 *
4025 * Returns pointer to cgroup_subsys_state if there is valid one with id.
4026 * NULL if not. Should be called under rcu_read_lock()
4027 */
4028struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
4029{
4030 struct css_id *cssid = NULL;
4031
4032 BUG_ON(!ss->use_id);
4033 cssid = idr_find(&ss->idr, id);
4034
4035 if (unlikely(!cssid))
4036 return NULL;
4037
4038 return rcu_dereference(cssid->css);
4039}
4040
4041/**
4042 * css_get_next - lookup next cgroup under specified hierarchy.
4043 * @ss: pointer to subsystem
4044 * @id: current position of iteration.
4045 * @root: pointer to css. search tree under this.
4046 * @foundid: position of found object.
4047 *
4048 * Search next css under the specified hierarchy of rootid. Calling under
4049 * rcu_read_lock() is necessary. Returns NULL if it reaches the end.
4050 */
4051struct cgroup_subsys_state *
4052css_get_next(struct cgroup_subsys *ss, int id,
4053 struct cgroup_subsys_state *root, int *foundid)
4054{
4055 struct cgroup_subsys_state *ret = NULL;
4056 struct css_id *tmp;
4057 int tmpid;
4058 int rootid = css_id(root);
4059 int depth = css_depth(root);
4060
4061 if (!rootid)
4062 return NULL;
4063
4064 BUG_ON(!ss->use_id);
4065 /* fill start point for scan */
4066 tmpid = id;
4067 while (1) {
4068 /*
4069 * scan next entry from bitmap(tree), tmpid is updated after
4070 * idr_get_next().
4071 */
4072 spin_lock(&ss->id_lock);
4073 tmp = idr_get_next(&ss->idr, &tmpid);
4074 spin_unlock(&ss->id_lock);
4075
4076 if (!tmp)
4077 break;
4078 if (tmp->depth >= depth && tmp->stack[depth] == rootid) {
4079 ret = rcu_dereference(tmp->css);
4080 if (ret) {
4081 *foundid = tmpid;
4082 break;
4083 }
4084 }
4085 /* continue to scan from next id */
4086 tmpid = tmpid + 1;
4087 }
4088 return ret;
4089}
4090
Paul Menagefe693432009-09-23 15:56:20 -07004091#ifdef CONFIG_CGROUP_DEBUG
4092static struct cgroup_subsys_state *debug_create(struct cgroup_subsys *ss,
4093 struct cgroup *cont)
4094{
4095 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
4096
4097 if (!css)
4098 return ERR_PTR(-ENOMEM);
4099
4100 return css;
4101}
4102
4103static void debug_destroy(struct cgroup_subsys *ss, struct cgroup *cont)
4104{
4105 kfree(cont->subsys[debug_subsys_id]);
4106}
4107
4108static u64 cgroup_refcount_read(struct cgroup *cont, struct cftype *cft)
4109{
4110 return atomic_read(&cont->count);
4111}
4112
4113static u64 debug_taskcount_read(struct cgroup *cont, struct cftype *cft)
4114{
4115 return cgroup_task_count(cont);
4116}
4117
4118static u64 current_css_set_read(struct cgroup *cont, struct cftype *cft)
4119{
4120 return (u64)(unsigned long)current->cgroups;
4121}
4122
4123static u64 current_css_set_refcount_read(struct cgroup *cont,
4124 struct cftype *cft)
4125{
4126 u64 count;
4127
4128 rcu_read_lock();
4129 count = atomic_read(&current->cgroups->refcount);
4130 rcu_read_unlock();
4131 return count;
4132}
4133
Paul Menage7717f7b2009-09-23 15:56:22 -07004134static int current_css_set_cg_links_read(struct cgroup *cont,
4135 struct cftype *cft,
4136 struct seq_file *seq)
4137{
4138 struct cg_cgroup_link *link;
4139 struct css_set *cg;
4140
4141 read_lock(&css_set_lock);
4142 rcu_read_lock();
4143 cg = rcu_dereference(current->cgroups);
4144 list_for_each_entry(link, &cg->cg_links, cg_link_list) {
4145 struct cgroup *c = link->cgrp;
4146 const char *name;
4147
4148 if (c->dentry)
4149 name = c->dentry->d_name.name;
4150 else
4151 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004152 seq_printf(seq, "Root %d group %s\n",
4153 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07004154 }
4155 rcu_read_unlock();
4156 read_unlock(&css_set_lock);
4157 return 0;
4158}
4159
4160#define MAX_TASKS_SHOWN_PER_CSS 25
4161static int cgroup_css_links_read(struct cgroup *cont,
4162 struct cftype *cft,
4163 struct seq_file *seq)
4164{
4165 struct cg_cgroup_link *link;
4166
4167 read_lock(&css_set_lock);
4168 list_for_each_entry(link, &cont->css_sets, cgrp_link_list) {
4169 struct css_set *cg = link->cg;
4170 struct task_struct *task;
4171 int count = 0;
4172 seq_printf(seq, "css_set %p\n", cg);
4173 list_for_each_entry(task, &cg->tasks, cg_list) {
4174 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
4175 seq_puts(seq, " ...\n");
4176 break;
4177 } else {
4178 seq_printf(seq, " task %d\n",
4179 task_pid_vnr(task));
4180 }
4181 }
4182 }
4183 read_unlock(&css_set_lock);
4184 return 0;
4185}
4186
Paul Menagefe693432009-09-23 15:56:20 -07004187static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft)
4188{
4189 return test_bit(CGRP_RELEASABLE, &cgrp->flags);
4190}
4191
4192static struct cftype debug_files[] = {
4193 {
4194 .name = "cgroup_refcount",
4195 .read_u64 = cgroup_refcount_read,
4196 },
4197 {
4198 .name = "taskcount",
4199 .read_u64 = debug_taskcount_read,
4200 },
4201
4202 {
4203 .name = "current_css_set",
4204 .read_u64 = current_css_set_read,
4205 },
4206
4207 {
4208 .name = "current_css_set_refcount",
4209 .read_u64 = current_css_set_refcount_read,
4210 },
4211
4212 {
Paul Menage7717f7b2009-09-23 15:56:22 -07004213 .name = "current_css_set_cg_links",
4214 .read_seq_string = current_css_set_cg_links_read,
4215 },
4216
4217 {
4218 .name = "cgroup_css_links",
4219 .read_seq_string = cgroup_css_links_read,
4220 },
4221
4222 {
Paul Menagefe693432009-09-23 15:56:20 -07004223 .name = "releasable",
4224 .read_u64 = releasable_read,
4225 },
4226};
4227
4228static int debug_populate(struct cgroup_subsys *ss, struct cgroup *cont)
4229{
4230 return cgroup_add_files(cont, ss, debug_files,
4231 ARRAY_SIZE(debug_files));
4232}
4233
4234struct cgroup_subsys debug_subsys = {
4235 .name = "debug",
4236 .create = debug_create,
4237 .destroy = debug_destroy,
4238 .populate = debug_populate,
4239 .subsys_id = debug_subsys_id,
4240};
4241#endif /* CONFIG_CGROUP_DEBUG */