blob: c92fb95493584b9c6c452b783281facb89ce615c [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
Ben Blumaae8aab2010-03-10 15:22:07 -080060/*
61 * Generate an array of cgroup subsystem pointers. At boot time, this is
62 * populated up to CGROUP_BUILTIN_SUBSYS_COUNT, and modular subsystems are
63 * registered after that. The mutable section of this array is protected by
64 * cgroup_mutex.
65 */
Paul Menageddbcc7e2007-10-18 23:39:30 -070066#define SUBSYS(_x) &_x ## _subsys,
Ben Blumaae8aab2010-03-10 15:22:07 -080067static struct cgroup_subsys *subsys[CGROUP_SUBSYS_COUNT] = {
Paul Menageddbcc7e2007-10-18 23:39:30 -070068#include <linux/cgroup_subsys.h>
69};
70
Paul Menagec6d57f32009-09-23 15:56:19 -070071#define MAX_CGROUP_ROOT_NAMELEN 64
72
Paul Menageddbcc7e2007-10-18 23:39:30 -070073/*
74 * A cgroupfs_root represents the root of a cgroup hierarchy,
75 * and may be associated with a superblock to form an active
76 * hierarchy
77 */
78struct cgroupfs_root {
79 struct super_block *sb;
80
81 /*
82 * The bitmask of subsystems intended to be attached to this
83 * hierarchy
84 */
85 unsigned long subsys_bits;
86
Paul Menage2c6ab6d2009-09-23 15:56:23 -070087 /* Unique id for this hierarchy. */
88 int hierarchy_id;
89
Paul Menageddbcc7e2007-10-18 23:39:30 -070090 /* The bitmask of subsystems currently attached to this hierarchy */
91 unsigned long actual_subsys_bits;
92
93 /* A list running through the attached subsystems */
94 struct list_head subsys_list;
95
96 /* The root cgroup for this hierarchy */
97 struct cgroup top_cgroup;
98
99 /* Tracks how many cgroups are currently defined in hierarchy.*/
100 int number_of_cgroups;
101
Li Zefane5f6a862009-01-07 18:07:41 -0800102 /* A list running through the active hierarchies */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700103 struct list_head root_list;
104
105 /* Hierarchy-specific flags */
106 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700107
Paul Menagee788e062008-07-25 01:46:59 -0700108 /* The path to use for release notifications. */
Paul Menage81a6a5c2007-10-18 23:39:38 -0700109 char release_agent_path[PATH_MAX];
Paul Menagec6d57f32009-09-23 15:56:19 -0700110
111 /* The name for this hierarchy - may be empty */
112 char name[MAX_CGROUP_ROOT_NAMELEN];
Paul Menageddbcc7e2007-10-18 23:39:30 -0700113};
114
Paul Menageddbcc7e2007-10-18 23:39:30 -0700115/*
116 * The "rootnode" hierarchy is the "dummy hierarchy", reserved for the
117 * subsystems that are otherwise unattached - it never has more than a
118 * single cgroup, and all tasks are part of that cgroup.
119 */
120static struct cgroupfs_root rootnode;
121
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700122/*
123 * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when
124 * cgroup_subsys->use_id != 0.
125 */
126#define CSS_ID_MAX (65535)
127struct css_id {
128 /*
129 * The css to which this ID points. This pointer is set to valid value
130 * after cgroup is populated. If cgroup is removed, this will be NULL.
131 * This pointer is expected to be RCU-safe because destroy()
132 * is called after synchronize_rcu(). But for safe use, css_is_removed()
133 * css_tryget() should be used for avoiding race.
134 */
135 struct cgroup_subsys_state *css;
136 /*
137 * ID of this css.
138 */
139 unsigned short id;
140 /*
141 * Depth in hierarchy which this ID belongs to.
142 */
143 unsigned short depth;
144 /*
145 * ID is freed by RCU. (and lookup routine is RCU safe.)
146 */
147 struct rcu_head rcu_head;
148 /*
149 * Hierarchy of CSS ID belongs to.
150 */
151 unsigned short stack[0]; /* Array of Length (depth+1) */
152};
153
154
Paul Menageddbcc7e2007-10-18 23:39:30 -0700155/* The list of hierarchy roots */
156
157static LIST_HEAD(roots);
Paul Menage817929e2007-10-18 23:39:36 -0700158static int root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700159
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700160static DEFINE_IDA(hierarchy_ida);
161static int next_hierarchy_id;
162static DEFINE_SPINLOCK(hierarchy_id_lock);
163
Paul Menageddbcc7e2007-10-18 23:39:30 -0700164/* dummytop is a shorthand for the dummy hierarchy's top cgroup */
165#define dummytop (&rootnode.top_cgroup)
166
167/* This flag indicates whether tasks in the fork and exit paths should
Li Zefana043e3b2008-02-23 15:24:09 -0800168 * check for fork/exit handlers to call. This avoids us having to do
169 * extra work in the fork/exit path if none of the subsystems need to
170 * be called.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700171 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700172static int need_forkexit_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700173
Paul E. McKenneyd11c5632010-02-22 17:04:50 -0800174#ifdef CONFIG_PROVE_LOCKING
175int cgroup_lock_is_held(void)
176{
177 return lockdep_is_held(&cgroup_mutex);
178}
179#else /* #ifdef CONFIG_PROVE_LOCKING */
180int cgroup_lock_is_held(void)
181{
182 return mutex_is_locked(&cgroup_mutex);
183}
184#endif /* #else #ifdef CONFIG_PROVE_LOCKING */
185
186EXPORT_SYMBOL_GPL(cgroup_lock_is_held);
187
Paul Menageddbcc7e2007-10-18 23:39:30 -0700188/* convenient tests for these bits */
Paul Menagebd89aab2007-10-18 23:40:44 -0700189inline int cgroup_is_removed(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700190{
Paul Menagebd89aab2007-10-18 23:40:44 -0700191 return test_bit(CGRP_REMOVED, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700192}
193
194/* bits in struct cgroupfs_root flags field */
195enum {
196 ROOT_NOPREFIX, /* mounted subsystems have no named prefix */
197};
198
Adrian Bunke9685a02008-02-07 00:13:46 -0800199static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700200{
201 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700202 (1 << CGRP_RELEASABLE) |
203 (1 << CGRP_NOTIFY_ON_RELEASE);
204 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700205}
206
Adrian Bunke9685a02008-02-07 00:13:46 -0800207static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700208{
Paul Menagebd89aab2007-10-18 23:40:44 -0700209 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700210}
211
Paul Menageddbcc7e2007-10-18 23:39:30 -0700212/*
213 * for_each_subsys() allows you to iterate on each subsystem attached to
214 * an active hierarchy
215 */
216#define for_each_subsys(_root, _ss) \
217list_for_each_entry(_ss, &_root->subsys_list, sibling)
218
Li Zefane5f6a862009-01-07 18:07:41 -0800219/* for_each_active_root() allows you to iterate across the active hierarchies */
220#define for_each_active_root(_root) \
Paul Menageddbcc7e2007-10-18 23:39:30 -0700221list_for_each_entry(_root, &roots, root_list)
222
Paul Menage81a6a5c2007-10-18 23:39:38 -0700223/* the list of cgroups eligible for automatic release. Protected by
224 * release_list_lock */
225static LIST_HEAD(release_list);
226static DEFINE_SPINLOCK(release_list_lock);
227static void cgroup_release_agent(struct work_struct *work);
228static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700229static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700230
Paul Menage817929e2007-10-18 23:39:36 -0700231/* Link structure for associating css_set objects with cgroups */
232struct cg_cgroup_link {
233 /*
234 * List running through cg_cgroup_links associated with a
235 * cgroup, anchored on cgroup->css_sets
236 */
Paul Menagebd89aab2007-10-18 23:40:44 -0700237 struct list_head cgrp_link_list;
Paul Menage7717f7b2009-09-23 15:56:22 -0700238 struct cgroup *cgrp;
Paul Menage817929e2007-10-18 23:39:36 -0700239 /*
240 * List running through cg_cgroup_links pointing at a
241 * single css_set object, anchored on css_set->cg_links
242 */
243 struct list_head cg_link_list;
244 struct css_set *cg;
245};
246
247/* The default css_set - used by init and its children prior to any
248 * hierarchies being mounted. It contains a pointer to the root state
249 * for each subsystem. Also used to anchor the list of css_sets. Not
250 * reference-counted, to improve performance when child cgroups
251 * haven't been created.
252 */
253
254static struct css_set init_css_set;
255static struct cg_cgroup_link init_css_set_link;
256
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700257static int cgroup_subsys_init_idr(struct cgroup_subsys *ss);
258
Paul Menage817929e2007-10-18 23:39:36 -0700259/* css_set_lock protects the list of css_set objects, and the
260 * chain of tasks off each css_set. Nests outside task->alloc_lock
261 * due to cgroup_iter_start() */
262static DEFINE_RWLOCK(css_set_lock);
263static int css_set_count;
264
Paul Menage7717f7b2009-09-23 15:56:22 -0700265/*
266 * hash table for cgroup groups. This improves the performance to find
267 * an existing css_set. This hash doesn't (currently) take into
268 * account cgroups in empty hierarchies.
269 */
Li Zefan472b1052008-04-29 01:00:11 -0700270#define CSS_SET_HASH_BITS 7
271#define CSS_SET_TABLE_SIZE (1 << CSS_SET_HASH_BITS)
272static struct hlist_head css_set_table[CSS_SET_TABLE_SIZE];
273
274static struct hlist_head *css_set_hash(struct cgroup_subsys_state *css[])
275{
276 int i;
277 int index;
278 unsigned long tmp = 0UL;
279
280 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++)
281 tmp += (unsigned long)css[i];
282 tmp = (tmp >> 16) ^ tmp;
283
284 index = hash_long(tmp, CSS_SET_HASH_BITS);
285
286 return &css_set_table[index];
287}
288
Ben Blumc3783692009-09-23 15:56:29 -0700289static void free_css_set_rcu(struct rcu_head *obj)
290{
291 struct css_set *cg = container_of(obj, struct css_set, rcu_head);
292 kfree(cg);
293}
294
Paul Menage817929e2007-10-18 23:39:36 -0700295/* We don't maintain the lists running through each css_set to its
296 * task until after the first call to cgroup_iter_start(). This
297 * reduces the fork()/exit() overhead for people who have cgroups
298 * compiled into their kernel but not actually in use */
Li Zefan8947f9d2008-07-25 01:46:56 -0700299static int use_task_css_set_links __read_mostly;
Paul Menage817929e2007-10-18 23:39:36 -0700300
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700301static void __put_css_set(struct css_set *cg, int taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700302{
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700303 struct cg_cgroup_link *link;
304 struct cg_cgroup_link *saved_link;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700305 /*
306 * Ensure that the refcount doesn't hit zero while any readers
307 * can see it. Similar to atomic_dec_and_lock(), but for an
308 * rwlock
309 */
310 if (atomic_add_unless(&cg->refcount, -1, 1))
311 return;
312 write_lock(&css_set_lock);
313 if (!atomic_dec_and_test(&cg->refcount)) {
314 write_unlock(&css_set_lock);
315 return;
316 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700317
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700318 /* This css_set is dead. unlink it and release cgroup refcounts */
319 hlist_del(&cg->hlist);
320 css_set_count--;
321
322 list_for_each_entry_safe(link, saved_link, &cg->cg_links,
323 cg_link_list) {
324 struct cgroup *cgrp = link->cgrp;
325 list_del(&link->cg_link_list);
326 list_del(&link->cgrp_link_list);
Paul Menagebd89aab2007-10-18 23:40:44 -0700327 if (atomic_dec_and_test(&cgrp->count) &&
328 notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700329 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700330 set_bit(CGRP_RELEASABLE, &cgrp->flags);
331 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700332 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700333
334 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700335 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700336
337 write_unlock(&css_set_lock);
Ben Blumc3783692009-09-23 15:56:29 -0700338 call_rcu(&cg->rcu_head, free_css_set_rcu);
Paul Menage817929e2007-10-18 23:39:36 -0700339}
340
341/*
342 * refcounted get/put for css_set objects
343 */
344static inline void get_css_set(struct css_set *cg)
345{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700346 atomic_inc(&cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700347}
348
349static inline void put_css_set(struct css_set *cg)
350{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700351 __put_css_set(cg, 0);
Paul Menage817929e2007-10-18 23:39:36 -0700352}
353
Paul Menage81a6a5c2007-10-18 23:39:38 -0700354static inline void put_css_set_taskexit(struct css_set *cg)
355{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700356 __put_css_set(cg, 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700357}
358
Paul Menage817929e2007-10-18 23:39:36 -0700359/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700360 * compare_css_sets - helper function for find_existing_css_set().
361 * @cg: candidate css_set being tested
362 * @old_cg: existing css_set for a task
363 * @new_cgrp: cgroup that's being entered by the task
364 * @template: desired set of css pointers in css_set (pre-calculated)
365 *
366 * Returns true if "cg" matches "old_cg" except for the hierarchy
367 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
368 */
369static bool compare_css_sets(struct css_set *cg,
370 struct css_set *old_cg,
371 struct cgroup *new_cgrp,
372 struct cgroup_subsys_state *template[])
373{
374 struct list_head *l1, *l2;
375
376 if (memcmp(template, cg->subsys, sizeof(cg->subsys))) {
377 /* Not all subsystems matched */
378 return false;
379 }
380
381 /*
382 * Compare cgroup pointers in order to distinguish between
383 * different cgroups in heirarchies with no subsystems. We
384 * could get by with just this check alone (and skip the
385 * memcmp above) but on most setups the memcmp check will
386 * avoid the need for this more expensive check on almost all
387 * candidates.
388 */
389
390 l1 = &cg->cg_links;
391 l2 = &old_cg->cg_links;
392 while (1) {
393 struct cg_cgroup_link *cgl1, *cgl2;
394 struct cgroup *cg1, *cg2;
395
396 l1 = l1->next;
397 l2 = l2->next;
398 /* See if we reached the end - both lists are equal length. */
399 if (l1 == &cg->cg_links) {
400 BUG_ON(l2 != &old_cg->cg_links);
401 break;
402 } else {
403 BUG_ON(l2 == &old_cg->cg_links);
404 }
405 /* Locate the cgroups associated with these links. */
406 cgl1 = list_entry(l1, struct cg_cgroup_link, cg_link_list);
407 cgl2 = list_entry(l2, struct cg_cgroup_link, cg_link_list);
408 cg1 = cgl1->cgrp;
409 cg2 = cgl2->cgrp;
410 /* Hierarchies should be linked in the same order. */
411 BUG_ON(cg1->root != cg2->root);
412
413 /*
414 * If this hierarchy is the hierarchy of the cgroup
415 * that's changing, then we need to check that this
416 * css_set points to the new cgroup; if it's any other
417 * hierarchy, then this css_set should point to the
418 * same cgroup as the old css_set.
419 */
420 if (cg1->root == new_cgrp->root) {
421 if (cg1 != new_cgrp)
422 return false;
423 } else {
424 if (cg1 != cg2)
425 return false;
426 }
427 }
428 return true;
429}
430
431/*
Paul Menage817929e2007-10-18 23:39:36 -0700432 * find_existing_css_set() is a helper for
433 * find_css_set(), and checks to see whether an existing
Li Zefan472b1052008-04-29 01:00:11 -0700434 * css_set is suitable.
Paul Menage817929e2007-10-18 23:39:36 -0700435 *
436 * oldcg: the cgroup group that we're using before the cgroup
437 * transition
438 *
Paul Menagebd89aab2007-10-18 23:40:44 -0700439 * cgrp: the cgroup that we're moving into
Paul Menage817929e2007-10-18 23:39:36 -0700440 *
441 * template: location in which to build the desired set of subsystem
442 * state objects for the new cgroup group
443 */
Paul Menage817929e2007-10-18 23:39:36 -0700444static struct css_set *find_existing_css_set(
445 struct css_set *oldcg,
Paul Menagebd89aab2007-10-18 23:40:44 -0700446 struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -0700447 struct cgroup_subsys_state *template[])
448{
449 int i;
Paul Menagebd89aab2007-10-18 23:40:44 -0700450 struct cgroupfs_root *root = cgrp->root;
Li Zefan472b1052008-04-29 01:00:11 -0700451 struct hlist_head *hhead;
452 struct hlist_node *node;
453 struct css_set *cg;
Paul Menage817929e2007-10-18 23:39:36 -0700454
Ben Blumaae8aab2010-03-10 15:22:07 -0800455 /*
456 * Build the set of subsystem state objects that we want to see in the
457 * new css_set. while subsystems can change globally, the entries here
458 * won't change, so no need for locking.
459 */
Paul Menage817929e2007-10-18 23:39:36 -0700460 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Li Zefan8d53d552008-02-23 15:24:11 -0800461 if (root->subsys_bits & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700462 /* Subsystem is in this hierarchy. So we want
463 * the subsystem state from the new
464 * cgroup */
Paul Menagebd89aab2007-10-18 23:40:44 -0700465 template[i] = cgrp->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700466 } else {
467 /* Subsystem is not in this hierarchy, so we
468 * don't want to change the subsystem state */
469 template[i] = oldcg->subsys[i];
470 }
471 }
472
Li Zefan472b1052008-04-29 01:00:11 -0700473 hhead = css_set_hash(template);
474 hlist_for_each_entry(cg, node, hhead, hlist) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700475 if (!compare_css_sets(cg, oldcg, cgrp, template))
476 continue;
477
478 /* This css_set matches what we need */
479 return cg;
Li Zefan472b1052008-04-29 01:00:11 -0700480 }
Paul Menage817929e2007-10-18 23:39:36 -0700481
482 /* No existing cgroup group matched */
483 return NULL;
484}
485
Paul Menage817929e2007-10-18 23:39:36 -0700486static void free_cg_links(struct list_head *tmp)
487{
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700488 struct cg_cgroup_link *link;
489 struct cg_cgroup_link *saved_link;
490
491 list_for_each_entry_safe(link, saved_link, tmp, cgrp_link_list) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700492 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -0700493 kfree(link);
494 }
495}
496
497/*
Li Zefan36553432008-07-29 22:33:19 -0700498 * allocate_cg_links() allocates "count" cg_cgroup_link structures
499 * and chains them on tmp through their cgrp_link_list fields. Returns 0 on
500 * success or a negative error
501 */
502static int allocate_cg_links(int count, struct list_head *tmp)
503{
504 struct cg_cgroup_link *link;
505 int i;
506 INIT_LIST_HEAD(tmp);
507 for (i = 0; i < count; i++) {
508 link = kmalloc(sizeof(*link), GFP_KERNEL);
509 if (!link) {
510 free_cg_links(tmp);
511 return -ENOMEM;
512 }
513 list_add(&link->cgrp_link_list, tmp);
514 }
515 return 0;
516}
517
Li Zefanc12f65d2009-01-07 18:07:42 -0800518/**
519 * link_css_set - a helper function to link a css_set to a cgroup
520 * @tmp_cg_links: cg_cgroup_link objects allocated by allocate_cg_links()
521 * @cg: the css_set to be linked
522 * @cgrp: the destination cgroup
523 */
524static void link_css_set(struct list_head *tmp_cg_links,
525 struct css_set *cg, struct cgroup *cgrp)
526{
527 struct cg_cgroup_link *link;
528
529 BUG_ON(list_empty(tmp_cg_links));
530 link = list_first_entry(tmp_cg_links, struct cg_cgroup_link,
531 cgrp_link_list);
532 link->cg = cg;
Paul Menage7717f7b2009-09-23 15:56:22 -0700533 link->cgrp = cgrp;
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700534 atomic_inc(&cgrp->count);
Li Zefanc12f65d2009-01-07 18:07:42 -0800535 list_move(&link->cgrp_link_list, &cgrp->css_sets);
Paul Menage7717f7b2009-09-23 15:56:22 -0700536 /*
537 * Always add links to the tail of the list so that the list
538 * is sorted by order of hierarchy creation
539 */
540 list_add_tail(&link->cg_link_list, &cg->cg_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800541}
542
Li Zefan36553432008-07-29 22:33:19 -0700543/*
Paul Menage817929e2007-10-18 23:39:36 -0700544 * find_css_set() takes an existing cgroup group and a
545 * cgroup object, and returns a css_set object that's
546 * equivalent to the old group, but with the given cgroup
547 * substituted into the appropriate hierarchy. Must be called with
548 * cgroup_mutex held
549 */
Paul Menage817929e2007-10-18 23:39:36 -0700550static struct css_set *find_css_set(
Paul Menagebd89aab2007-10-18 23:40:44 -0700551 struct css_set *oldcg, struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700552{
553 struct css_set *res;
554 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT];
Paul Menage817929e2007-10-18 23:39:36 -0700555
556 struct list_head tmp_cg_links;
Paul Menage817929e2007-10-18 23:39:36 -0700557
Li Zefan472b1052008-04-29 01:00:11 -0700558 struct hlist_head *hhead;
Paul Menage7717f7b2009-09-23 15:56:22 -0700559 struct cg_cgroup_link *link;
Li Zefan472b1052008-04-29 01:00:11 -0700560
Paul Menage817929e2007-10-18 23:39:36 -0700561 /* First see if we already have a cgroup group that matches
562 * the desired set */
Li Zefan7e9abd82008-07-25 01:46:54 -0700563 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -0700564 res = find_existing_css_set(oldcg, cgrp, template);
Paul Menage817929e2007-10-18 23:39:36 -0700565 if (res)
566 get_css_set(res);
Li Zefan7e9abd82008-07-25 01:46:54 -0700567 read_unlock(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -0700568
569 if (res)
570 return res;
571
572 res = kmalloc(sizeof(*res), GFP_KERNEL);
573 if (!res)
574 return NULL;
575
576 /* Allocate all the cg_cgroup_link objects that we'll need */
577 if (allocate_cg_links(root_count, &tmp_cg_links) < 0) {
578 kfree(res);
579 return NULL;
580 }
581
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700582 atomic_set(&res->refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -0700583 INIT_LIST_HEAD(&res->cg_links);
584 INIT_LIST_HEAD(&res->tasks);
Li Zefan472b1052008-04-29 01:00:11 -0700585 INIT_HLIST_NODE(&res->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700586
587 /* Copy the set of subsystem state objects generated in
588 * find_existing_css_set() */
589 memcpy(res->subsys, template, sizeof(res->subsys));
590
591 write_lock(&css_set_lock);
592 /* Add reference counts and links from the new css_set. */
Paul Menage7717f7b2009-09-23 15:56:22 -0700593 list_for_each_entry(link, &oldcg->cg_links, cg_link_list) {
594 struct cgroup *c = link->cgrp;
595 if (c->root == cgrp->root)
596 c = cgrp;
597 link_css_set(&tmp_cg_links, res, c);
598 }
Paul Menage817929e2007-10-18 23:39:36 -0700599
600 BUG_ON(!list_empty(&tmp_cg_links));
601
Paul Menage817929e2007-10-18 23:39:36 -0700602 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700603
604 /* Add this cgroup group to the hash table */
605 hhead = css_set_hash(res->subsys);
606 hlist_add_head(&res->hlist, hhead);
607
Paul Menage817929e2007-10-18 23:39:36 -0700608 write_unlock(&css_set_lock);
609
610 return res;
Paul Menageb4f48b62007-10-18 23:39:33 -0700611}
612
Paul Menageddbcc7e2007-10-18 23:39:30 -0700613/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700614 * Return the cgroup for "task" from the given hierarchy. Must be
615 * called with cgroup_mutex held.
616 */
617static struct cgroup *task_cgroup_from_root(struct task_struct *task,
618 struct cgroupfs_root *root)
619{
620 struct css_set *css;
621 struct cgroup *res = NULL;
622
623 BUG_ON(!mutex_is_locked(&cgroup_mutex));
624 read_lock(&css_set_lock);
625 /*
626 * No need to lock the task - since we hold cgroup_mutex the
627 * task can't change groups, so the only thing that can happen
628 * is that it exits and its css is set back to init_css_set.
629 */
630 css = task->cgroups;
631 if (css == &init_css_set) {
632 res = &root->top_cgroup;
633 } else {
634 struct cg_cgroup_link *link;
635 list_for_each_entry(link, &css->cg_links, cg_link_list) {
636 struct cgroup *c = link->cgrp;
637 if (c->root == root) {
638 res = c;
639 break;
640 }
641 }
642 }
643 read_unlock(&css_set_lock);
644 BUG_ON(!res);
645 return res;
646}
647
648/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700649 * There is one global cgroup mutex. We also require taking
650 * task_lock() when dereferencing a task's cgroup subsys pointers.
651 * See "The task_lock() exception", at the end of this comment.
652 *
653 * A task must hold cgroup_mutex to modify cgroups.
654 *
655 * Any task can increment and decrement the count field without lock.
656 * So in general, code holding cgroup_mutex can't rely on the count
657 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800658 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700659 * means that no tasks are currently attached, therefore there is no
660 * way a task attached to that cgroup can fork (the other way to
661 * increment the count). So code holding cgroup_mutex can safely
662 * assume that if the count is zero, it will stay zero. Similarly, if
663 * a task holds cgroup_mutex on a cgroup with zero count, it
664 * knows that the cgroup won't be removed, as cgroup_rmdir()
665 * needs that mutex.
666 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700667 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
668 * (usually) take cgroup_mutex. These are the two most performance
669 * critical pieces of code here. The exception occurs on cgroup_exit(),
670 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
671 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800672 * to the release agent with the name of the cgroup (path relative to
673 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700674 *
675 * A cgroup can only be deleted if both its 'count' of using tasks
676 * is zero, and its list of 'children' cgroups is empty. Since all
677 * tasks in the system use _some_ cgroup, and since there is always at
678 * least one task in the system (init, pid == 1), therefore, top_cgroup
679 * always has either children cgroups and/or using tasks. So we don't
680 * need a special hack to ensure that top_cgroup cannot be deleted.
681 *
682 * The task_lock() exception
683 *
684 * The need for this exception arises from the action of
Cliff Wickman956db3c2008-02-07 00:14:43 -0800685 * cgroup_attach_task(), which overwrites one tasks cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800686 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700687 * several performance critical places that need to reference
688 * task->cgroup without the expense of grabbing a system global
689 * mutex. Therefore except as noted below, when dereferencing or, as
Cliff Wickman956db3c2008-02-07 00:14:43 -0800690 * in cgroup_attach_task(), modifying a task'ss cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700691 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
692 * the task_struct routinely used for such matters.
693 *
694 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800695 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700696 */
697
Paul Menageddbcc7e2007-10-18 23:39:30 -0700698/**
699 * cgroup_lock - lock out any changes to cgroup structures
700 *
701 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700702void cgroup_lock(void)
703{
704 mutex_lock(&cgroup_mutex);
705}
706
707/**
708 * cgroup_unlock - release lock on cgroup changes
709 *
710 * Undo the lock taken in a previous cgroup_lock() call.
711 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700712void cgroup_unlock(void)
713{
714 mutex_unlock(&cgroup_mutex);
715}
716
717/*
718 * A couple of forward declarations required, due to cyclic reference loop:
719 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
720 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
721 * -> cgroup_mkdir.
722 */
723
724static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode);
725static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -0700726static int cgroup_populate_dir(struct cgroup *cgrp);
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700727static const struct inode_operations cgroup_dir_inode_operations;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700728static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700729
730static struct backing_dev_info cgroup_backing_dev_info = {
Jens Axboed9938312009-06-12 14:45:52 +0200731 .name = "cgroup",
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700732 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Paul Menagea4243162007-10-18 23:39:35 -0700733};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700734
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700735static int alloc_css_id(struct cgroup_subsys *ss,
736 struct cgroup *parent, struct cgroup *child);
737
Paul Menageddbcc7e2007-10-18 23:39:30 -0700738static struct inode *cgroup_new_inode(mode_t mode, struct super_block *sb)
739{
740 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700741
742 if (inode) {
743 inode->i_mode = mode;
David Howells76aac0e2008-11-14 10:39:12 +1100744 inode->i_uid = current_fsuid();
745 inode->i_gid = current_fsgid();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700746 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
747 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
748 }
749 return inode;
750}
751
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800752/*
753 * Call subsys's pre_destroy handler.
754 * This is called before css refcnt check.
755 */
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700756static int cgroup_call_pre_destroy(struct cgroup *cgrp)
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800757{
758 struct cgroup_subsys *ss;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700759 int ret = 0;
760
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800761 for_each_subsys(cgrp->root, ss)
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700762 if (ss->pre_destroy) {
763 ret = ss->pre_destroy(ss, cgrp);
764 if (ret)
765 break;
766 }
767 return ret;
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800768}
769
Paul Menagea47295e2009-01-07 18:07:44 -0800770static void free_cgroup_rcu(struct rcu_head *obj)
771{
772 struct cgroup *cgrp = container_of(obj, struct cgroup, rcu_head);
773
774 kfree(cgrp);
775}
776
Paul Menageddbcc7e2007-10-18 23:39:30 -0700777static void cgroup_diput(struct dentry *dentry, struct inode *inode)
778{
779 /* is dentry a directory ? if so, kfree() associated cgroup */
780 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700781 struct cgroup *cgrp = dentry->d_fsdata;
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800782 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -0700783 BUG_ON(!(cgroup_is_removed(cgrp)));
Paul Menage81a6a5c2007-10-18 23:39:38 -0700784 /* It's possible for external users to be holding css
785 * reference counts on a cgroup; css_put() needs to
786 * be able to access the cgroup after decrementing
787 * the reference count in order to know if it needs to
788 * queue the cgroup to be handled by the release
789 * agent */
790 synchronize_rcu();
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800791
792 mutex_lock(&cgroup_mutex);
793 /*
794 * Release the subsystem state objects.
795 */
Li Zefan75139b82009-01-07 18:07:33 -0800796 for_each_subsys(cgrp->root, ss)
797 ss->destroy(ss, cgrp);
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800798
799 cgrp->root->number_of_cgroups--;
800 mutex_unlock(&cgroup_mutex);
801
Paul Menagea47295e2009-01-07 18:07:44 -0800802 /*
803 * Drop the active superblock reference that we took when we
804 * created the cgroup
805 */
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800806 deactivate_super(cgrp->root->sb);
807
Ben Blum72a8cb32009-09-23 15:56:27 -0700808 /*
809 * if we're getting rid of the cgroup, refcount should ensure
810 * that there are no pidlists left.
811 */
812 BUG_ON(!list_empty(&cgrp->pidlists));
813
Paul Menagea47295e2009-01-07 18:07:44 -0800814 call_rcu(&cgrp->rcu_head, free_cgroup_rcu);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700815 }
816 iput(inode);
817}
818
819static void remove_dir(struct dentry *d)
820{
821 struct dentry *parent = dget(d->d_parent);
822
823 d_delete(d);
824 simple_rmdir(parent->d_inode, d);
825 dput(parent);
826}
827
828static void cgroup_clear_directory(struct dentry *dentry)
829{
830 struct list_head *node;
831
832 BUG_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
833 spin_lock(&dcache_lock);
834 node = dentry->d_subdirs.next;
835 while (node != &dentry->d_subdirs) {
836 struct dentry *d = list_entry(node, struct dentry, d_u.d_child);
837 list_del_init(node);
838 if (d->d_inode) {
839 /* This should never be called on a cgroup
840 * directory with child cgroups */
841 BUG_ON(d->d_inode->i_mode & S_IFDIR);
842 d = dget_locked(d);
843 spin_unlock(&dcache_lock);
844 d_delete(d);
845 simple_unlink(dentry->d_inode, d);
846 dput(d);
847 spin_lock(&dcache_lock);
848 }
849 node = dentry->d_subdirs.next;
850 }
851 spin_unlock(&dcache_lock);
852}
853
854/*
855 * NOTE : the dentry must have been dget()'ed
856 */
857static void cgroup_d_remove_dir(struct dentry *dentry)
858{
859 cgroup_clear_directory(dentry);
860
861 spin_lock(&dcache_lock);
862 list_del_init(&dentry->d_u.d_child);
863 spin_unlock(&dcache_lock);
864 remove_dir(dentry);
865}
866
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700867/*
868 * A queue for waiters to do rmdir() cgroup. A tasks will sleep when
869 * cgroup->count == 0 && list_empty(&cgroup->children) && subsys has some
870 * reference to css->refcnt. In general, this refcnt is expected to goes down
871 * to zero, soon.
872 *
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -0700873 * CGRP_WAIT_ON_RMDIR flag is set under cgroup's inode->i_mutex;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700874 */
875DECLARE_WAIT_QUEUE_HEAD(cgroup_rmdir_waitq);
876
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -0700877static void cgroup_wakeup_rmdir_waiter(struct cgroup *cgrp)
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700878{
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -0700879 if (unlikely(test_and_clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags)))
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700880 wake_up_all(&cgroup_rmdir_waitq);
881}
882
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -0700883void cgroup_exclude_rmdir(struct cgroup_subsys_state *css)
884{
885 css_get(css);
886}
887
888void cgroup_release_and_wakeup_rmdir(struct cgroup_subsys_state *css)
889{
890 cgroup_wakeup_rmdir_waiter(css->cgroup);
891 css_put(css);
892}
893
Ben Blumaae8aab2010-03-10 15:22:07 -0800894/*
895 * Call with cgroup_mutex held.
896 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700897static int rebind_subsystems(struct cgroupfs_root *root,
898 unsigned long final_bits)
899{
900 unsigned long added_bits, removed_bits;
Paul Menagebd89aab2007-10-18 23:40:44 -0700901 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700902 int i;
903
Ben Blumaae8aab2010-03-10 15:22:07 -0800904 BUG_ON(!mutex_is_locked(&cgroup_mutex));
905
Paul Menageddbcc7e2007-10-18 23:39:30 -0700906 removed_bits = root->actual_subsys_bits & ~final_bits;
907 added_bits = final_bits & ~root->actual_subsys_bits;
908 /* Check that any added subsystems are currently free */
909 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Li Zefan8d53d552008-02-23 15:24:11 -0800910 unsigned long bit = 1UL << i;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700911 struct cgroup_subsys *ss = subsys[i];
912 if (!(bit & added_bits))
913 continue;
Ben Blumaae8aab2010-03-10 15:22:07 -0800914 /*
915 * Nobody should tell us to do a subsys that doesn't exist:
916 * parse_cgroupfs_options should catch that case and refcounts
917 * ensure that subsystems won't disappear once selected.
918 */
919 BUG_ON(ss == NULL);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700920 if (ss->root != &rootnode) {
921 /* Subsystem isn't free */
922 return -EBUSY;
923 }
924 }
925
926 /* Currently we don't handle adding/removing subsystems when
927 * any child cgroups exist. This is theoretically supportable
928 * but involves complex error handling, so it's being left until
929 * later */
Paul Menage307257c2008-12-15 13:54:22 -0800930 if (root->number_of_cgroups > 1)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700931 return -EBUSY;
932
933 /* Process each subsystem */
934 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
935 struct cgroup_subsys *ss = subsys[i];
936 unsigned long bit = 1UL << i;
937 if (bit & added_bits) {
938 /* We're binding this subsystem to this hierarchy */
Ben Blumaae8aab2010-03-10 15:22:07 -0800939 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -0700940 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700941 BUG_ON(!dummytop->subsys[i]);
942 BUG_ON(dummytop->subsys[i]->cgroup != dummytop);
Paul Menage999cd8a2009-01-07 18:08:36 -0800943 mutex_lock(&ss->hierarchy_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -0700944 cgrp->subsys[i] = dummytop->subsys[i];
945 cgrp->subsys[i]->cgroup = cgrp;
Li Zefan33a68ac2009-01-07 18:07:42 -0800946 list_move(&ss->sibling, &root->subsys_list);
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -0800947 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700948 if (ss->bind)
Paul Menagebd89aab2007-10-18 23:40:44 -0700949 ss->bind(ss, cgrp);
Paul Menage999cd8a2009-01-07 18:08:36 -0800950 mutex_unlock(&ss->hierarchy_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700951 } else if (bit & removed_bits) {
952 /* We're removing this subsystem */
Ben Blumaae8aab2010-03-10 15:22:07 -0800953 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -0700954 BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]);
955 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
Paul Menage999cd8a2009-01-07 18:08:36 -0800956 mutex_lock(&ss->hierarchy_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700957 if (ss->bind)
958 ss->bind(ss, dummytop);
959 dummytop->subsys[i]->cgroup = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -0700960 cgrp->subsys[i] = NULL;
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -0800961 subsys[i]->root = &rootnode;
Li Zefan33a68ac2009-01-07 18:07:42 -0800962 list_move(&ss->sibling, &rootnode.subsys_list);
Paul Menage999cd8a2009-01-07 18:08:36 -0800963 mutex_unlock(&ss->hierarchy_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700964 } else if (bit & final_bits) {
965 /* Subsystem state should already exist */
Ben Blumaae8aab2010-03-10 15:22:07 -0800966 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -0700967 BUG_ON(!cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700968 } else {
969 /* Subsystem state shouldn't exist */
Paul Menagebd89aab2007-10-18 23:40:44 -0700970 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700971 }
972 }
973 root->subsys_bits = root->actual_subsys_bits = final_bits;
974 synchronize_rcu();
975
976 return 0;
977}
978
979static int cgroup_show_options(struct seq_file *seq, struct vfsmount *vfs)
980{
981 struct cgroupfs_root *root = vfs->mnt_sb->s_fs_info;
982 struct cgroup_subsys *ss;
983
984 mutex_lock(&cgroup_mutex);
985 for_each_subsys(root, ss)
986 seq_printf(seq, ",%s", ss->name);
987 if (test_bit(ROOT_NOPREFIX, &root->flags))
988 seq_puts(seq, ",noprefix");
Paul Menage81a6a5c2007-10-18 23:39:38 -0700989 if (strlen(root->release_agent_path))
990 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Paul Menagec6d57f32009-09-23 15:56:19 -0700991 if (strlen(root->name))
992 seq_printf(seq, ",name=%s", root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700993 mutex_unlock(&cgroup_mutex);
994 return 0;
995}
996
997struct cgroup_sb_opts {
998 unsigned long subsys_bits;
999 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001000 char *release_agent;
Paul Menagec6d57f32009-09-23 15:56:19 -07001001 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001002 /* User explicitly requested empty subsystem */
1003 bool none;
Paul Menagec6d57f32009-09-23 15:56:19 -07001004
1005 struct cgroupfs_root *new_root;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001006
Paul Menageddbcc7e2007-10-18 23:39:30 -07001007};
1008
Ben Blumaae8aab2010-03-10 15:22:07 -08001009/*
1010 * Convert a hierarchy specifier into a bitmask of subsystems and flags. Call
1011 * with cgroup_mutex held to protect the subsys[] array.
1012 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001013static int parse_cgroupfs_options(char *data,
1014 struct cgroup_sb_opts *opts)
1015{
1016 char *token, *o = data ?: "all";
Li Zefanf9ab5b52009-06-17 16:26:33 -07001017 unsigned long mask = (unsigned long)-1;
1018
Ben Blumaae8aab2010-03-10 15:22:07 -08001019 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1020
Li Zefanf9ab5b52009-06-17 16:26:33 -07001021#ifdef CONFIG_CPUSETS
1022 mask = ~(1UL << cpuset_subsys_id);
1023#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001024
Paul Menagec6d57f32009-09-23 15:56:19 -07001025 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001026
1027 while ((token = strsep(&o, ",")) != NULL) {
1028 if (!*token)
1029 return -EINVAL;
1030 if (!strcmp(token, "all")) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07001031 /* Add all non-disabled subsystems */
1032 int i;
1033 opts->subsys_bits = 0;
1034 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1035 struct cgroup_subsys *ss = subsys[i];
Ben Blumaae8aab2010-03-10 15:22:07 -08001036 if (ss == NULL)
1037 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07001038 if (!ss->disabled)
1039 opts->subsys_bits |= 1ul << i;
1040 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001041 } else if (!strcmp(token, "none")) {
1042 /* Explicitly have no subsystems */
1043 opts->none = true;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001044 } else if (!strcmp(token, "noprefix")) {
1045 set_bit(ROOT_NOPREFIX, &opts->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001046 } else if (!strncmp(token, "release_agent=", 14)) {
1047 /* Specifying two release agents is forbidden */
1048 if (opts->release_agent)
1049 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001050 opts->release_agent =
1051 kstrndup(token + 14, PATH_MAX, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001052 if (!opts->release_agent)
1053 return -ENOMEM;
Paul Menagec6d57f32009-09-23 15:56:19 -07001054 } else if (!strncmp(token, "name=", 5)) {
1055 int i;
1056 const char *name = token + 5;
1057 /* Can't specify an empty name */
1058 if (!strlen(name))
1059 return -EINVAL;
1060 /* Must match [\w.-]+ */
1061 for (i = 0; i < strlen(name); i++) {
1062 char c = name[i];
1063 if (isalnum(c))
1064 continue;
1065 if ((c == '.') || (c == '-') || (c == '_'))
1066 continue;
1067 return -EINVAL;
1068 }
1069 /* Specifying two names is forbidden */
1070 if (opts->name)
1071 return -EINVAL;
1072 opts->name = kstrndup(name,
1073 MAX_CGROUP_ROOT_NAMELEN,
1074 GFP_KERNEL);
1075 if (!opts->name)
1076 return -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001077 } else {
1078 struct cgroup_subsys *ss;
1079 int i;
1080 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1081 ss = subsys[i];
Ben Blumaae8aab2010-03-10 15:22:07 -08001082 if (ss == NULL)
1083 continue;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001084 if (!strcmp(token, ss->name)) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07001085 if (!ss->disabled)
1086 set_bit(i, &opts->subsys_bits);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001087 break;
1088 }
1089 }
1090 if (i == CGROUP_SUBSYS_COUNT)
1091 return -ENOENT;
1092 }
1093 }
1094
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001095 /* Consistency checks */
1096
Li Zefanf9ab5b52009-06-17 16:26:33 -07001097 /*
1098 * Option noprefix was introduced just for backward compatibility
1099 * with the old cpuset, so we allow noprefix only if mounting just
1100 * the cpuset subsystem.
1101 */
1102 if (test_bit(ROOT_NOPREFIX, &opts->flags) &&
1103 (opts->subsys_bits & mask))
1104 return -EINVAL;
1105
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001106
1107 /* Can't specify "none" and some subsystems */
1108 if (opts->subsys_bits && opts->none)
1109 return -EINVAL;
1110
1111 /*
1112 * We either have to specify by name or by subsystems. (So all
1113 * empty hierarchies must have a name).
1114 */
Paul Menagec6d57f32009-09-23 15:56:19 -07001115 if (!opts->subsys_bits && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001116 return -EINVAL;
1117
1118 return 0;
1119}
1120
1121static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1122{
1123 int ret = 0;
1124 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001125 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001126 struct cgroup_sb_opts opts;
1127
Alessio Igor Bogani337eb002009-05-12 15:10:54 +02001128 lock_kernel();
Paul Menagebd89aab2007-10-18 23:40:44 -07001129 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001130 mutex_lock(&cgroup_mutex);
1131
1132 /* See what subsystems are wanted */
1133 ret = parse_cgroupfs_options(data, &opts);
1134 if (ret)
1135 goto out_unlock;
1136
1137 /* Don't allow flags to change at remount */
1138 if (opts.flags != root->flags) {
1139 ret = -EINVAL;
1140 goto out_unlock;
1141 }
1142
Paul Menagec6d57f32009-09-23 15:56:19 -07001143 /* Don't allow name to change at remount */
1144 if (opts.name && strcmp(opts.name, root->name)) {
1145 ret = -EINVAL;
1146 goto out_unlock;
1147 }
1148
Paul Menageddbcc7e2007-10-18 23:39:30 -07001149 ret = rebind_subsystems(root, opts.subsys_bits);
Li Zefan0670e082009-04-02 16:57:30 -07001150 if (ret)
1151 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001152
1153 /* (re)populate subsystem files */
Li Zefan0670e082009-04-02 16:57:30 -07001154 cgroup_populate_dir(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001155
Paul Menage81a6a5c2007-10-18 23:39:38 -07001156 if (opts.release_agent)
1157 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001158 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001159 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001160 kfree(opts.name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001161 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07001162 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Alessio Igor Bogani337eb002009-05-12 15:10:54 +02001163 unlock_kernel();
Paul Menageddbcc7e2007-10-18 23:39:30 -07001164 return ret;
1165}
1166
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07001167static const struct super_operations cgroup_ops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001168 .statfs = simple_statfs,
1169 .drop_inode = generic_delete_inode,
1170 .show_options = cgroup_show_options,
1171 .remount_fs = cgroup_remount,
1172};
1173
Paul Menagecc31edc2008-10-18 20:28:04 -07001174static void init_cgroup_housekeeping(struct cgroup *cgrp)
1175{
1176 INIT_LIST_HEAD(&cgrp->sibling);
1177 INIT_LIST_HEAD(&cgrp->children);
1178 INIT_LIST_HEAD(&cgrp->css_sets);
1179 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001180 INIT_LIST_HEAD(&cgrp->pidlists);
1181 mutex_init(&cgrp->pidlist_mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07001182}
Paul Menagec6d57f32009-09-23 15:56:19 -07001183
Paul Menageddbcc7e2007-10-18 23:39:30 -07001184static void init_cgroup_root(struct cgroupfs_root *root)
1185{
Paul Menagebd89aab2007-10-18 23:40:44 -07001186 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001187 INIT_LIST_HEAD(&root->subsys_list);
1188 INIT_LIST_HEAD(&root->root_list);
1189 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -07001190 cgrp->root = root;
1191 cgrp->top_cgroup = cgrp;
Paul Menagecc31edc2008-10-18 20:28:04 -07001192 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001193}
1194
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001195static bool init_root_id(struct cgroupfs_root *root)
1196{
1197 int ret = 0;
1198
1199 do {
1200 if (!ida_pre_get(&hierarchy_ida, GFP_KERNEL))
1201 return false;
1202 spin_lock(&hierarchy_id_lock);
1203 /* Try to allocate the next unused ID */
1204 ret = ida_get_new_above(&hierarchy_ida, next_hierarchy_id,
1205 &root->hierarchy_id);
1206 if (ret == -ENOSPC)
1207 /* Try again starting from 0 */
1208 ret = ida_get_new(&hierarchy_ida, &root->hierarchy_id);
1209 if (!ret) {
1210 next_hierarchy_id = root->hierarchy_id + 1;
1211 } else if (ret != -EAGAIN) {
1212 /* Can only get here if the 31-bit IDR is full ... */
1213 BUG_ON(ret);
1214 }
1215 spin_unlock(&hierarchy_id_lock);
1216 } while (ret);
1217 return true;
1218}
1219
Paul Menageddbcc7e2007-10-18 23:39:30 -07001220static int cgroup_test_super(struct super_block *sb, void *data)
1221{
Paul Menagec6d57f32009-09-23 15:56:19 -07001222 struct cgroup_sb_opts *opts = data;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001223 struct cgroupfs_root *root = sb->s_fs_info;
1224
Paul Menagec6d57f32009-09-23 15:56:19 -07001225 /* If we asked for a name then it must match */
1226 if (opts->name && strcmp(opts->name, root->name))
1227 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001228
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001229 /*
1230 * If we asked for subsystems (or explicitly for no
1231 * subsystems) then they must match
1232 */
1233 if ((opts->subsys_bits || opts->none)
1234 && (opts->subsys_bits != root->subsys_bits))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001235 return 0;
1236
1237 return 1;
1238}
1239
Paul Menagec6d57f32009-09-23 15:56:19 -07001240static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1241{
1242 struct cgroupfs_root *root;
1243
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001244 if (!opts->subsys_bits && !opts->none)
Paul Menagec6d57f32009-09-23 15:56:19 -07001245 return NULL;
1246
1247 root = kzalloc(sizeof(*root), GFP_KERNEL);
1248 if (!root)
1249 return ERR_PTR(-ENOMEM);
1250
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001251 if (!init_root_id(root)) {
1252 kfree(root);
1253 return ERR_PTR(-ENOMEM);
1254 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001255 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001256
Paul Menagec6d57f32009-09-23 15:56:19 -07001257 root->subsys_bits = opts->subsys_bits;
1258 root->flags = opts->flags;
1259 if (opts->release_agent)
1260 strcpy(root->release_agent_path, opts->release_agent);
1261 if (opts->name)
1262 strcpy(root->name, opts->name);
1263 return root;
1264}
1265
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001266static void cgroup_drop_root(struct cgroupfs_root *root)
1267{
1268 if (!root)
1269 return;
1270
1271 BUG_ON(!root->hierarchy_id);
1272 spin_lock(&hierarchy_id_lock);
1273 ida_remove(&hierarchy_ida, root->hierarchy_id);
1274 spin_unlock(&hierarchy_id_lock);
1275 kfree(root);
1276}
1277
Paul Menageddbcc7e2007-10-18 23:39:30 -07001278static int cgroup_set_super(struct super_block *sb, void *data)
1279{
1280 int ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001281 struct cgroup_sb_opts *opts = data;
1282
1283 /* If we don't have a new root, we can't set up a new sb */
1284 if (!opts->new_root)
1285 return -EINVAL;
1286
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001287 BUG_ON(!opts->subsys_bits && !opts->none);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001288
1289 ret = set_anon_super(sb, NULL);
1290 if (ret)
1291 return ret;
1292
Paul Menagec6d57f32009-09-23 15:56:19 -07001293 sb->s_fs_info = opts->new_root;
1294 opts->new_root->sb = sb;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001295
1296 sb->s_blocksize = PAGE_CACHE_SIZE;
1297 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1298 sb->s_magic = CGROUP_SUPER_MAGIC;
1299 sb->s_op = &cgroup_ops;
1300
1301 return 0;
1302}
1303
1304static int cgroup_get_rootdir(struct super_block *sb)
1305{
1306 struct inode *inode =
1307 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
1308 struct dentry *dentry;
1309
1310 if (!inode)
1311 return -ENOMEM;
1312
Paul Menageddbcc7e2007-10-18 23:39:30 -07001313 inode->i_fop = &simple_dir_operations;
1314 inode->i_op = &cgroup_dir_inode_operations;
1315 /* directories start off with i_nlink == 2 (for "." entry) */
1316 inc_nlink(inode);
1317 dentry = d_alloc_root(inode);
1318 if (!dentry) {
1319 iput(inode);
1320 return -ENOMEM;
1321 }
1322 sb->s_root = dentry;
1323 return 0;
1324}
1325
1326static int cgroup_get_sb(struct file_system_type *fs_type,
1327 int flags, const char *unused_dev_name,
1328 void *data, struct vfsmount *mnt)
1329{
1330 struct cgroup_sb_opts opts;
Paul Menagec6d57f32009-09-23 15:56:19 -07001331 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001332 int ret = 0;
1333 struct super_block *sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001334 struct cgroupfs_root *new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001335
1336 /* First find the desired set of subsystems */
Ben Blumaae8aab2010-03-10 15:22:07 -08001337 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001338 ret = parse_cgroupfs_options(data, &opts);
Ben Blumaae8aab2010-03-10 15:22:07 -08001339 mutex_unlock(&cgroup_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001340 if (ret)
1341 goto out_err;
1342
1343 /*
1344 * Allocate a new cgroup root. We may not need it if we're
1345 * reusing an existing hierarchy.
1346 */
1347 new_root = cgroup_root_from_opts(&opts);
1348 if (IS_ERR(new_root)) {
1349 ret = PTR_ERR(new_root);
1350 goto out_err;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001351 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001352 opts.new_root = new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001353
Paul Menagec6d57f32009-09-23 15:56:19 -07001354 /* Locate an existing or new sb for this hierarchy */
1355 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001356 if (IS_ERR(sb)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001357 ret = PTR_ERR(sb);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001358 cgroup_drop_root(opts.new_root);
Paul Menagec6d57f32009-09-23 15:56:19 -07001359 goto out_err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001360 }
1361
Paul Menagec6d57f32009-09-23 15:56:19 -07001362 root = sb->s_fs_info;
1363 BUG_ON(!root);
1364 if (root == opts.new_root) {
1365 /* We used the new root structure, so this is a new hierarchy */
1366 struct list_head tmp_cg_links;
Li Zefanc12f65d2009-01-07 18:07:42 -08001367 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menage817929e2007-10-18 23:39:36 -07001368 struct inode *inode;
Paul Menagec6d57f32009-09-23 15:56:19 -07001369 struct cgroupfs_root *existing_root;
Li Zefan28fd5df2008-04-29 01:00:13 -07001370 int i;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001371
1372 BUG_ON(sb->s_root != NULL);
1373
1374 ret = cgroup_get_rootdir(sb);
1375 if (ret)
1376 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001377 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001378
Paul Menage817929e2007-10-18 23:39:36 -07001379 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001380 mutex_lock(&cgroup_mutex);
1381
Paul Menagec6d57f32009-09-23 15:56:19 -07001382 if (strlen(root->name)) {
1383 /* Check for name clashes with existing mounts */
1384 for_each_active_root(existing_root) {
1385 if (!strcmp(existing_root->name, root->name)) {
1386 ret = -EBUSY;
1387 mutex_unlock(&cgroup_mutex);
1388 mutex_unlock(&inode->i_mutex);
1389 goto drop_new_super;
1390 }
1391 }
1392 }
1393
Paul Menage817929e2007-10-18 23:39:36 -07001394 /*
1395 * We're accessing css_set_count without locking
1396 * css_set_lock here, but that's OK - it can only be
1397 * increased by someone holding cgroup_lock, and
1398 * that's us. The worst that can happen is that we
1399 * have some link structures left over
1400 */
1401 ret = allocate_cg_links(css_set_count, &tmp_cg_links);
1402 if (ret) {
1403 mutex_unlock(&cgroup_mutex);
1404 mutex_unlock(&inode->i_mutex);
1405 goto drop_new_super;
1406 }
1407
Paul Menageddbcc7e2007-10-18 23:39:30 -07001408 ret = rebind_subsystems(root, root->subsys_bits);
1409 if (ret == -EBUSY) {
1410 mutex_unlock(&cgroup_mutex);
Paul Menage817929e2007-10-18 23:39:36 -07001411 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001412 free_cg_links(&tmp_cg_links);
1413 goto drop_new_super;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001414 }
1415
1416 /* EBUSY should be the only error here */
1417 BUG_ON(ret);
1418
1419 list_add(&root->root_list, &roots);
Paul Menage817929e2007-10-18 23:39:36 -07001420 root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001421
Li Zefanc12f65d2009-01-07 18:07:42 -08001422 sb->s_root->d_fsdata = root_cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001423 root->top_cgroup.dentry = sb->s_root;
1424
Paul Menage817929e2007-10-18 23:39:36 -07001425 /* Link the top cgroup in this hierarchy into all
1426 * the css_set objects */
1427 write_lock(&css_set_lock);
Li Zefan28fd5df2008-04-29 01:00:13 -07001428 for (i = 0; i < CSS_SET_TABLE_SIZE; i++) {
1429 struct hlist_head *hhead = &css_set_table[i];
1430 struct hlist_node *node;
Paul Menage817929e2007-10-18 23:39:36 -07001431 struct css_set *cg;
Li Zefan28fd5df2008-04-29 01:00:13 -07001432
Li Zefanc12f65d2009-01-07 18:07:42 -08001433 hlist_for_each_entry(cg, node, hhead, hlist)
1434 link_css_set(&tmp_cg_links, cg, root_cgrp);
Li Zefan28fd5df2008-04-29 01:00:13 -07001435 }
Paul Menage817929e2007-10-18 23:39:36 -07001436 write_unlock(&css_set_lock);
1437
1438 free_cg_links(&tmp_cg_links);
1439
Li Zefanc12f65d2009-01-07 18:07:42 -08001440 BUG_ON(!list_empty(&root_cgrp->sibling));
1441 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001442 BUG_ON(root->number_of_cgroups != 1);
1443
Li Zefanc12f65d2009-01-07 18:07:42 -08001444 cgroup_populate_dir(root_cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001445 mutex_unlock(&cgroup_mutex);
Xiaotian Feng34f77a92009-09-23 15:56:18 -07001446 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001447 } else {
1448 /*
1449 * We re-used an existing hierarchy - the new root (if
1450 * any) is not needed
1451 */
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001452 cgroup_drop_root(opts.new_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001453 }
1454
Sukadev Bhattiprolua3ec9472009-03-04 12:06:34 -08001455 simple_set_mnt(mnt, sb);
Paul Menagec6d57f32009-09-23 15:56:19 -07001456 kfree(opts.release_agent);
1457 kfree(opts.name);
Sukadev Bhattiprolua3ec9472009-03-04 12:06:34 -08001458 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001459
1460 drop_new_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04001461 deactivate_locked_super(sb);
Paul Menagec6d57f32009-09-23 15:56:19 -07001462 out_err:
1463 kfree(opts.release_agent);
1464 kfree(opts.name);
1465
Paul Menageddbcc7e2007-10-18 23:39:30 -07001466 return ret;
1467}
1468
1469static void cgroup_kill_sb(struct super_block *sb) {
1470 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001471 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001472 int ret;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001473 struct cg_cgroup_link *link;
1474 struct cg_cgroup_link *saved_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001475
1476 BUG_ON(!root);
1477
1478 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001479 BUG_ON(!list_empty(&cgrp->children));
1480 BUG_ON(!list_empty(&cgrp->sibling));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001481
1482 mutex_lock(&cgroup_mutex);
1483
1484 /* Rebind all subsystems back to the default hierarchy */
1485 ret = rebind_subsystems(root, 0);
1486 /* Shouldn't be able to fail ... */
1487 BUG_ON(ret);
1488
Paul Menage817929e2007-10-18 23:39:36 -07001489 /*
1490 * Release all the links from css_sets to this hierarchy's
1491 * root cgroup
1492 */
1493 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001494
1495 list_for_each_entry_safe(link, saved_link, &cgrp->css_sets,
1496 cgrp_link_list) {
Paul Menage817929e2007-10-18 23:39:36 -07001497 list_del(&link->cg_link_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07001498 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07001499 kfree(link);
1500 }
1501 write_unlock(&css_set_lock);
1502
Paul Menage839ec542009-01-29 14:25:22 -08001503 if (!list_empty(&root->root_list)) {
1504 list_del(&root->root_list);
1505 root_count--;
1506 }
Li Zefane5f6a862009-01-07 18:07:41 -08001507
Paul Menageddbcc7e2007-10-18 23:39:30 -07001508 mutex_unlock(&cgroup_mutex);
1509
Paul Menageddbcc7e2007-10-18 23:39:30 -07001510 kill_litter_super(sb);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001511 cgroup_drop_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001512}
1513
1514static struct file_system_type cgroup_fs_type = {
1515 .name = "cgroup",
1516 .get_sb = cgroup_get_sb,
1517 .kill_sb = cgroup_kill_sb,
1518};
1519
Paul Menagebd89aab2007-10-18 23:40:44 -07001520static inline struct cgroup *__d_cgrp(struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001521{
1522 return dentry->d_fsdata;
1523}
1524
1525static inline struct cftype *__d_cft(struct dentry *dentry)
1526{
1527 return dentry->d_fsdata;
1528}
1529
Li Zefana043e3b2008-02-23 15:24:09 -08001530/**
1531 * cgroup_path - generate the path of a cgroup
1532 * @cgrp: the cgroup in question
1533 * @buf: the buffer to write the path into
1534 * @buflen: the length of the buffer
1535 *
Paul Menagea47295e2009-01-07 18:07:44 -08001536 * Called with cgroup_mutex held or else with an RCU-protected cgroup
1537 * reference. Writes path of cgroup into buf. Returns 0 on success,
1538 * -errno on error.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001539 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001540int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001541{
1542 char *start;
Paul Menagea47295e2009-01-07 18:07:44 -08001543 struct dentry *dentry = rcu_dereference(cgrp->dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001544
Paul Menagea47295e2009-01-07 18:07:44 -08001545 if (!dentry || cgrp == dummytop) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001546 /*
1547 * Inactive subsystems have no dentry for their root
1548 * cgroup
1549 */
1550 strcpy(buf, "/");
1551 return 0;
1552 }
1553
1554 start = buf + buflen;
1555
1556 *--start = '\0';
1557 for (;;) {
Paul Menagea47295e2009-01-07 18:07:44 -08001558 int len = dentry->d_name.len;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001559 if ((start -= len) < buf)
1560 return -ENAMETOOLONG;
Paul Menagebd89aab2007-10-18 23:40:44 -07001561 memcpy(start, cgrp->dentry->d_name.name, len);
1562 cgrp = cgrp->parent;
1563 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001564 break;
Paul Menagea47295e2009-01-07 18:07:44 -08001565 dentry = rcu_dereference(cgrp->dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07001566 if (!cgrp->parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001567 continue;
1568 if (--start < buf)
1569 return -ENAMETOOLONG;
1570 *start = '/';
1571 }
1572 memmove(buf, start, buf + buflen - start);
1573 return 0;
1574}
1575
Li Zefana043e3b2008-02-23 15:24:09 -08001576/**
1577 * cgroup_attach_task - attach task 'tsk' to cgroup 'cgrp'
1578 * @cgrp: the cgroup the task is attaching to
1579 * @tsk: the task to be attached
Paul Menagebbcb81d2007-10-18 23:39:32 -07001580 *
Li Zefana043e3b2008-02-23 15:24:09 -08001581 * Call holding cgroup_mutex. May take task_lock of
1582 * the task 'tsk' during call.
Paul Menagebbcb81d2007-10-18 23:39:32 -07001583 */
Cliff Wickman956db3c2008-02-07 00:14:43 -08001584int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001585{
1586 int retval = 0;
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001587 struct cgroup_subsys *ss, *failed_ss = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07001588 struct cgroup *oldcgrp;
Lai Jiangshan77efecd2009-01-07 18:07:39 -08001589 struct css_set *cg;
Paul Menage817929e2007-10-18 23:39:36 -07001590 struct css_set *newcg;
Paul Menagebd89aab2007-10-18 23:40:44 -07001591 struct cgroupfs_root *root = cgrp->root;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001592
1593 /* Nothing to do if the task is already in that cgroup */
Paul Menage7717f7b2009-09-23 15:56:22 -07001594 oldcgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07001595 if (cgrp == oldcgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001596 return 0;
1597
1598 for_each_subsys(root, ss) {
1599 if (ss->can_attach) {
Ben Blumbe367d02009-09-23 15:56:31 -07001600 retval = ss->can_attach(ss, cgrp, tsk, false);
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001601 if (retval) {
1602 /*
1603 * Remember on which subsystem the can_attach()
1604 * failed, so that we only call cancel_attach()
1605 * against the subsystems whose can_attach()
1606 * succeeded. (See below)
1607 */
1608 failed_ss = ss;
1609 goto out;
1610 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07001611 }
1612 }
1613
Lai Jiangshan77efecd2009-01-07 18:07:39 -08001614 task_lock(tsk);
1615 cg = tsk->cgroups;
1616 get_css_set(cg);
1617 task_unlock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07001618 /*
1619 * Locate or allocate a new css_set for this task,
1620 * based on its final set of cgroups
1621 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001622 newcg = find_css_set(cg, cgrp);
Lai Jiangshan77efecd2009-01-07 18:07:39 -08001623 put_css_set(cg);
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001624 if (!newcg) {
1625 retval = -ENOMEM;
1626 goto out;
1627 }
Paul Menage817929e2007-10-18 23:39:36 -07001628
Paul Menagebbcb81d2007-10-18 23:39:32 -07001629 task_lock(tsk);
1630 if (tsk->flags & PF_EXITING) {
1631 task_unlock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07001632 put_css_set(newcg);
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001633 retval = -ESRCH;
1634 goto out;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001635 }
Paul Menage817929e2007-10-18 23:39:36 -07001636 rcu_assign_pointer(tsk->cgroups, newcg);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001637 task_unlock(tsk);
1638
Paul Menage817929e2007-10-18 23:39:36 -07001639 /* Update the css_set linked lists if we're using them */
1640 write_lock(&css_set_lock);
1641 if (!list_empty(&tsk->cg_list)) {
1642 list_del(&tsk->cg_list);
1643 list_add(&tsk->cg_list, &newcg->tasks);
1644 }
1645 write_unlock(&css_set_lock);
1646
Paul Menagebbcb81d2007-10-18 23:39:32 -07001647 for_each_subsys(root, ss) {
Paul Jacksone18f6312008-02-07 00:13:44 -08001648 if (ss->attach)
Ben Blumbe367d02009-09-23 15:56:31 -07001649 ss->attach(ss, cgrp, oldcgrp, tsk, false);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001650 }
Paul Menagebd89aab2007-10-18 23:40:44 -07001651 set_bit(CGRP_RELEASABLE, &oldcgrp->flags);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001652 synchronize_rcu();
Paul Menage817929e2007-10-18 23:39:36 -07001653 put_css_set(cg);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07001654
1655 /*
1656 * wake up rmdir() waiter. the rmdir should fail since the cgroup
1657 * is no longer empty.
1658 */
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07001659 cgroup_wakeup_rmdir_waiter(cgrp);
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001660out:
1661 if (retval) {
1662 for_each_subsys(root, ss) {
1663 if (ss == failed_ss)
1664 /*
1665 * This subsystem was the one that failed the
1666 * can_attach() check earlier, so we don't need
1667 * to call cancel_attach() against it or any
1668 * remaining subsystems.
1669 */
1670 break;
1671 if (ss->cancel_attach)
1672 ss->cancel_attach(ss, cgrp, tsk, false);
1673 }
1674 }
1675 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001676}
1677
1678/*
Paul Menageaf351022008-07-25 01:47:01 -07001679 * Attach task with pid 'pid' to cgroup 'cgrp'. Call with cgroup_mutex
1680 * held. May take task_lock of task
Paul Menagebbcb81d2007-10-18 23:39:32 -07001681 */
Paul Menageaf351022008-07-25 01:47:01 -07001682static int attach_task_by_pid(struct cgroup *cgrp, u64 pid)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001683{
Paul Menagebbcb81d2007-10-18 23:39:32 -07001684 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11001685 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001686 int ret;
1687
Paul Menagebbcb81d2007-10-18 23:39:32 -07001688 if (pid) {
1689 rcu_read_lock();
Pavel Emelyanov73507f32008-02-07 00:14:47 -08001690 tsk = find_task_by_vpid(pid);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001691 if (!tsk || tsk->flags & PF_EXITING) {
1692 rcu_read_unlock();
1693 return -ESRCH;
1694 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07001695
David Howellsc69e8d92008-11-14 10:39:19 +11001696 tcred = __task_cred(tsk);
1697 if (cred->euid &&
1698 cred->euid != tcred->uid &&
1699 cred->euid != tcred->suid) {
1700 rcu_read_unlock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07001701 return -EACCES;
1702 }
David Howellsc69e8d92008-11-14 10:39:19 +11001703 get_task_struct(tsk);
1704 rcu_read_unlock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07001705 } else {
1706 tsk = current;
1707 get_task_struct(tsk);
1708 }
1709
Cliff Wickman956db3c2008-02-07 00:14:43 -08001710 ret = cgroup_attach_task(cgrp, tsk);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001711 put_task_struct(tsk);
1712 return ret;
1713}
1714
Paul Menageaf351022008-07-25 01:47:01 -07001715static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid)
1716{
1717 int ret;
1718 if (!cgroup_lock_live_group(cgrp))
1719 return -ENODEV;
1720 ret = attach_task_by_pid(cgrp, pid);
1721 cgroup_unlock();
1722 return ret;
1723}
1724
Paul Menagee788e062008-07-25 01:46:59 -07001725/**
1726 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
1727 * @cgrp: the cgroup to be checked for liveness
1728 *
Paul Menage84eea842008-07-25 01:47:00 -07001729 * On success, returns true; the lock should be later released with
1730 * cgroup_unlock(). On failure returns false with no lock held.
Paul Menagee788e062008-07-25 01:46:59 -07001731 */
Paul Menage84eea842008-07-25 01:47:00 -07001732bool cgroup_lock_live_group(struct cgroup *cgrp)
Paul Menagee788e062008-07-25 01:46:59 -07001733{
1734 mutex_lock(&cgroup_mutex);
1735 if (cgroup_is_removed(cgrp)) {
1736 mutex_unlock(&cgroup_mutex);
1737 return false;
1738 }
1739 return true;
1740}
1741
1742static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
1743 const char *buffer)
1744{
1745 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
1746 if (!cgroup_lock_live_group(cgrp))
1747 return -ENODEV;
1748 strcpy(cgrp->root->release_agent_path, buffer);
Paul Menage84eea842008-07-25 01:47:00 -07001749 cgroup_unlock();
Paul Menagee788e062008-07-25 01:46:59 -07001750 return 0;
1751}
1752
1753static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
1754 struct seq_file *seq)
1755{
1756 if (!cgroup_lock_live_group(cgrp))
1757 return -ENODEV;
1758 seq_puts(seq, cgrp->root->release_agent_path);
1759 seq_putc(seq, '\n');
Paul Menage84eea842008-07-25 01:47:00 -07001760 cgroup_unlock();
Paul Menagee788e062008-07-25 01:46:59 -07001761 return 0;
1762}
1763
Paul Menage84eea842008-07-25 01:47:00 -07001764/* A buffer size big enough for numbers or short strings */
1765#define CGROUP_LOCAL_BUFFER_SIZE 64
1766
Paul Menagee73d2c62008-04-29 01:00:06 -07001767static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
Paul Menagef4c753b2008-04-29 00:59:56 -07001768 struct file *file,
1769 const char __user *userbuf,
1770 size_t nbytes, loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07001771{
Paul Menage84eea842008-07-25 01:47:00 -07001772 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menage355e0c42007-10-18 23:39:33 -07001773 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07001774 char *end;
1775
1776 if (!nbytes)
1777 return -EINVAL;
1778 if (nbytes >= sizeof(buffer))
1779 return -E2BIG;
1780 if (copy_from_user(buffer, userbuf, nbytes))
1781 return -EFAULT;
1782
1783 buffer[nbytes] = 0; /* nul-terminate */
Paul Menagee73d2c62008-04-29 01:00:06 -07001784 if (cft->write_u64) {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07001785 u64 val = simple_strtoull(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07001786 if (*end)
1787 return -EINVAL;
1788 retval = cft->write_u64(cgrp, cft, val);
1789 } else {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07001790 s64 val = simple_strtoll(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07001791 if (*end)
1792 return -EINVAL;
1793 retval = cft->write_s64(cgrp, cft, val);
1794 }
Paul Menage355e0c42007-10-18 23:39:33 -07001795 if (!retval)
1796 retval = nbytes;
1797 return retval;
1798}
1799
Paul Menagedb3b1492008-07-25 01:46:58 -07001800static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
1801 struct file *file,
1802 const char __user *userbuf,
1803 size_t nbytes, loff_t *unused_ppos)
1804{
Paul Menage84eea842008-07-25 01:47:00 -07001805 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagedb3b1492008-07-25 01:46:58 -07001806 int retval = 0;
1807 size_t max_bytes = cft->max_write_len;
1808 char *buffer = local_buffer;
1809
1810 if (!max_bytes)
1811 max_bytes = sizeof(local_buffer) - 1;
1812 if (nbytes >= max_bytes)
1813 return -E2BIG;
1814 /* Allocate a dynamic buffer if we need one */
1815 if (nbytes >= sizeof(local_buffer)) {
1816 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
1817 if (buffer == NULL)
1818 return -ENOMEM;
1819 }
Li Zefan5a3eb9f2008-07-29 22:33:18 -07001820 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
1821 retval = -EFAULT;
1822 goto out;
1823 }
Paul Menagedb3b1492008-07-25 01:46:58 -07001824
1825 buffer[nbytes] = 0; /* nul-terminate */
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07001826 retval = cft->write_string(cgrp, cft, strstrip(buffer));
Paul Menagedb3b1492008-07-25 01:46:58 -07001827 if (!retval)
1828 retval = nbytes;
Li Zefan5a3eb9f2008-07-29 22:33:18 -07001829out:
Paul Menagedb3b1492008-07-25 01:46:58 -07001830 if (buffer != local_buffer)
1831 kfree(buffer);
1832 return retval;
1833}
1834
Paul Menageddbcc7e2007-10-18 23:39:30 -07001835static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
1836 size_t nbytes, loff_t *ppos)
1837{
1838 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07001839 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001840
Li Zefan75139b82009-01-07 18:07:33 -08001841 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001842 return -ENODEV;
Paul Menage355e0c42007-10-18 23:39:33 -07001843 if (cft->write)
Paul Menagebd89aab2007-10-18 23:40:44 -07001844 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07001845 if (cft->write_u64 || cft->write_s64)
1846 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagedb3b1492008-07-25 01:46:58 -07001847 if (cft->write_string)
1848 return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07001849 if (cft->trigger) {
1850 int ret = cft->trigger(cgrp, (unsigned int)cft->private);
1851 return ret ? ret : nbytes;
1852 }
Paul Menage355e0c42007-10-18 23:39:33 -07001853 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001854}
1855
Paul Menagef4c753b2008-04-29 00:59:56 -07001856static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
1857 struct file *file,
1858 char __user *buf, size_t nbytes,
1859 loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001860{
Paul Menage84eea842008-07-25 01:47:00 -07001861 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagef4c753b2008-04-29 00:59:56 -07001862 u64 val = cft->read_u64(cgrp, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001863 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
1864
1865 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
1866}
1867
Paul Menagee73d2c62008-04-29 01:00:06 -07001868static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
1869 struct file *file,
1870 char __user *buf, size_t nbytes,
1871 loff_t *ppos)
1872{
Paul Menage84eea842008-07-25 01:47:00 -07001873 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagee73d2c62008-04-29 01:00:06 -07001874 s64 val = cft->read_s64(cgrp, cft);
1875 int len = sprintf(tmp, "%lld\n", (long long) val);
1876
1877 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
1878}
1879
Paul Menageddbcc7e2007-10-18 23:39:30 -07001880static ssize_t cgroup_file_read(struct file *file, char __user *buf,
1881 size_t nbytes, loff_t *ppos)
1882{
1883 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07001884 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001885
Li Zefan75139b82009-01-07 18:07:33 -08001886 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001887 return -ENODEV;
1888
1889 if (cft->read)
Paul Menagebd89aab2007-10-18 23:40:44 -07001890 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07001891 if (cft->read_u64)
1892 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07001893 if (cft->read_s64)
1894 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001895 return -EINVAL;
1896}
1897
Paul Menage91796562008-04-29 01:00:01 -07001898/*
1899 * seqfile ops/methods for returning structured data. Currently just
1900 * supports string->u64 maps, but can be extended in future.
1901 */
1902
1903struct cgroup_seqfile_state {
1904 struct cftype *cft;
1905 struct cgroup *cgroup;
1906};
1907
1908static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
1909{
1910 struct seq_file *sf = cb->state;
1911 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
1912}
1913
1914static int cgroup_seqfile_show(struct seq_file *m, void *arg)
1915{
1916 struct cgroup_seqfile_state *state = m->private;
1917 struct cftype *cft = state->cft;
Serge E. Hallyn29486df2008-04-29 01:00:14 -07001918 if (cft->read_map) {
1919 struct cgroup_map_cb cb = {
1920 .fill = cgroup_map_add,
1921 .state = m,
1922 };
1923 return cft->read_map(state->cgroup, cft, &cb);
1924 }
1925 return cft->read_seq_string(state->cgroup, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07001926}
1927
Adrian Bunk96930a62008-07-25 19:46:21 -07001928static int cgroup_seqfile_release(struct inode *inode, struct file *file)
Paul Menage91796562008-04-29 01:00:01 -07001929{
1930 struct seq_file *seq = file->private_data;
1931 kfree(seq->private);
1932 return single_release(inode, file);
1933}
1934
Alexey Dobriyan828c0952009-10-01 15:43:56 -07001935static const struct file_operations cgroup_seqfile_operations = {
Paul Menage91796562008-04-29 01:00:01 -07001936 .read = seq_read,
Paul Menagee788e062008-07-25 01:46:59 -07001937 .write = cgroup_file_write,
Paul Menage91796562008-04-29 01:00:01 -07001938 .llseek = seq_lseek,
1939 .release = cgroup_seqfile_release,
1940};
1941
Paul Menageddbcc7e2007-10-18 23:39:30 -07001942static int cgroup_file_open(struct inode *inode, struct file *file)
1943{
1944 int err;
1945 struct cftype *cft;
1946
1947 err = generic_file_open(inode, file);
1948 if (err)
1949 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001950 cft = __d_cft(file->f_dentry);
Li Zefan75139b82009-01-07 18:07:33 -08001951
Serge E. Hallyn29486df2008-04-29 01:00:14 -07001952 if (cft->read_map || cft->read_seq_string) {
Paul Menage91796562008-04-29 01:00:01 -07001953 struct cgroup_seqfile_state *state =
1954 kzalloc(sizeof(*state), GFP_USER);
1955 if (!state)
1956 return -ENOMEM;
1957 state->cft = cft;
1958 state->cgroup = __d_cgrp(file->f_dentry->d_parent);
1959 file->f_op = &cgroup_seqfile_operations;
1960 err = single_open(file, cgroup_seqfile_show, state);
1961 if (err < 0)
1962 kfree(state);
1963 } else if (cft->open)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001964 err = cft->open(inode, file);
1965 else
1966 err = 0;
1967
1968 return err;
1969}
1970
1971static int cgroup_file_release(struct inode *inode, struct file *file)
1972{
1973 struct cftype *cft = __d_cft(file->f_dentry);
1974 if (cft->release)
1975 return cft->release(inode, file);
1976 return 0;
1977}
1978
1979/*
1980 * cgroup_rename - Only allow simple rename of directories in place.
1981 */
1982static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
1983 struct inode *new_dir, struct dentry *new_dentry)
1984{
1985 if (!S_ISDIR(old_dentry->d_inode->i_mode))
1986 return -ENOTDIR;
1987 if (new_dentry->d_inode)
1988 return -EEXIST;
1989 if (old_dir != new_dir)
1990 return -EIO;
1991 return simple_rename(old_dir, old_dentry, new_dir, new_dentry);
1992}
1993
Alexey Dobriyan828c0952009-10-01 15:43:56 -07001994static const struct file_operations cgroup_file_operations = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001995 .read = cgroup_file_read,
1996 .write = cgroup_file_write,
1997 .llseek = generic_file_llseek,
1998 .open = cgroup_file_open,
1999 .release = cgroup_file_release,
2000};
2001
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07002002static const struct inode_operations cgroup_dir_inode_operations = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002003 .lookup = simple_lookup,
2004 .mkdir = cgroup_mkdir,
2005 .rmdir = cgroup_rmdir,
2006 .rename = cgroup_rename,
2007};
2008
Li Zefan099fca32009-04-02 16:57:29 -07002009static int cgroup_create_file(struct dentry *dentry, mode_t mode,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002010 struct super_block *sb)
2011{
Al Viro3ba13d12009-02-20 06:02:22 +00002012 static const struct dentry_operations cgroup_dops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002013 .d_iput = cgroup_diput,
2014 };
2015
2016 struct inode *inode;
2017
2018 if (!dentry)
2019 return -ENOENT;
2020 if (dentry->d_inode)
2021 return -EEXIST;
2022
2023 inode = cgroup_new_inode(mode, sb);
2024 if (!inode)
2025 return -ENOMEM;
2026
2027 if (S_ISDIR(mode)) {
2028 inode->i_op = &cgroup_dir_inode_operations;
2029 inode->i_fop = &simple_dir_operations;
2030
2031 /* start off with i_nlink == 2 (for "." entry) */
2032 inc_nlink(inode);
2033
2034 /* start with the directory inode held, so that we can
2035 * populate it without racing with another mkdir */
Paul Menage817929e2007-10-18 23:39:36 -07002036 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002037 } else if (S_ISREG(mode)) {
2038 inode->i_size = 0;
2039 inode->i_fop = &cgroup_file_operations;
2040 }
2041 dentry->d_op = &cgroup_dops;
2042 d_instantiate(dentry, inode);
2043 dget(dentry); /* Extra count - pin the dentry in core */
2044 return 0;
2045}
2046
2047/*
Li Zefana043e3b2008-02-23 15:24:09 -08002048 * cgroup_create_dir - create a directory for an object.
2049 * @cgrp: the cgroup we create the directory for. It must have a valid
2050 * ->parent field. And we are going to fill its ->dentry field.
2051 * @dentry: dentry of the new cgroup
2052 * @mode: mode to set on new directory.
Paul Menageddbcc7e2007-10-18 23:39:30 -07002053 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002054static int cgroup_create_dir(struct cgroup *cgrp, struct dentry *dentry,
Li Zefan099fca32009-04-02 16:57:29 -07002055 mode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002056{
2057 struct dentry *parent;
2058 int error = 0;
2059
Paul Menagebd89aab2007-10-18 23:40:44 -07002060 parent = cgrp->parent->dentry;
2061 error = cgroup_create_file(dentry, S_IFDIR | mode, cgrp->root->sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002062 if (!error) {
Paul Menagebd89aab2007-10-18 23:40:44 -07002063 dentry->d_fsdata = cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002064 inc_nlink(parent->d_inode);
Paul Menagea47295e2009-01-07 18:07:44 -08002065 rcu_assign_pointer(cgrp->dentry, dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002066 dget(dentry);
2067 }
2068 dput(dentry);
2069
2070 return error;
2071}
2072
Li Zefan099fca32009-04-02 16:57:29 -07002073/**
2074 * cgroup_file_mode - deduce file mode of a control file
2075 * @cft: the control file in question
2076 *
2077 * returns cft->mode if ->mode is not 0
2078 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2079 * returns S_IRUGO if it has only a read handler
2080 * returns S_IWUSR if it has only a write hander
2081 */
2082static mode_t cgroup_file_mode(const struct cftype *cft)
2083{
2084 mode_t mode = 0;
2085
2086 if (cft->mode)
2087 return cft->mode;
2088
2089 if (cft->read || cft->read_u64 || cft->read_s64 ||
2090 cft->read_map || cft->read_seq_string)
2091 mode |= S_IRUGO;
2092
2093 if (cft->write || cft->write_u64 || cft->write_s64 ||
2094 cft->write_string || cft->trigger)
2095 mode |= S_IWUSR;
2096
2097 return mode;
2098}
2099
Paul Menagebd89aab2007-10-18 23:40:44 -07002100int cgroup_add_file(struct cgroup *cgrp,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002101 struct cgroup_subsys *subsys,
2102 const struct cftype *cft)
2103{
Paul Menagebd89aab2007-10-18 23:40:44 -07002104 struct dentry *dir = cgrp->dentry;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002105 struct dentry *dentry;
2106 int error;
Li Zefan099fca32009-04-02 16:57:29 -07002107 mode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002108
2109 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Paul Menagebd89aab2007-10-18 23:40:44 -07002110 if (subsys && !test_bit(ROOT_NOPREFIX, &cgrp->root->flags)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002111 strcpy(name, subsys->name);
2112 strcat(name, ".");
2113 }
2114 strcat(name, cft->name);
2115 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
2116 dentry = lookup_one_len(name, dir, strlen(name));
2117 if (!IS_ERR(dentry)) {
Li Zefan099fca32009-04-02 16:57:29 -07002118 mode = cgroup_file_mode(cft);
2119 error = cgroup_create_file(dentry, mode | S_IFREG,
Paul Menagebd89aab2007-10-18 23:40:44 -07002120 cgrp->root->sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002121 if (!error)
2122 dentry->d_fsdata = (void *)cft;
2123 dput(dentry);
2124 } else
2125 error = PTR_ERR(dentry);
2126 return error;
2127}
2128
Paul Menagebd89aab2007-10-18 23:40:44 -07002129int cgroup_add_files(struct cgroup *cgrp,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002130 struct cgroup_subsys *subsys,
2131 const struct cftype cft[],
2132 int count)
2133{
2134 int i, err;
2135 for (i = 0; i < count; i++) {
Paul Menagebd89aab2007-10-18 23:40:44 -07002136 err = cgroup_add_file(cgrp, subsys, &cft[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002137 if (err)
2138 return err;
2139 }
2140 return 0;
2141}
2142
Li Zefana043e3b2008-02-23 15:24:09 -08002143/**
2144 * cgroup_task_count - count the number of tasks in a cgroup.
2145 * @cgrp: the cgroup in question
2146 *
2147 * Return the number of tasks in the cgroup.
2148 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002149int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002150{
2151 int count = 0;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07002152 struct cg_cgroup_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002153
Paul Menage817929e2007-10-18 23:39:36 -07002154 read_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07002155 list_for_each_entry(link, &cgrp->css_sets, cgrp_link_list) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07002156 count += atomic_read(&link->cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002157 }
2158 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002159 return count;
2160}
2161
2162/*
Paul Menage817929e2007-10-18 23:39:36 -07002163 * Advance a list_head iterator. The iterator should be positioned at
2164 * the start of a css_set
2165 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002166static void cgroup_advance_iter(struct cgroup *cgrp,
Paul Menage7717f7b2009-09-23 15:56:22 -07002167 struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002168{
2169 struct list_head *l = it->cg_link;
2170 struct cg_cgroup_link *link;
2171 struct css_set *cg;
2172
2173 /* Advance to the next non-empty css_set */
2174 do {
2175 l = l->next;
Paul Menagebd89aab2007-10-18 23:40:44 -07002176 if (l == &cgrp->css_sets) {
Paul Menage817929e2007-10-18 23:39:36 -07002177 it->cg_link = NULL;
2178 return;
2179 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002180 link = list_entry(l, struct cg_cgroup_link, cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07002181 cg = link->cg;
2182 } while (list_empty(&cg->tasks));
2183 it->cg_link = l;
2184 it->task = cg->tasks.next;
2185}
2186
Cliff Wickman31a7df02008-02-07 00:14:42 -08002187/*
2188 * To reduce the fork() overhead for systems that are not actually
2189 * using their cgroups capability, we don't maintain the lists running
2190 * through each css_set to its tasks until we see the list actually
2191 * used - in other words after the first call to cgroup_iter_start().
2192 *
2193 * The tasklist_lock is not held here, as do_each_thread() and
2194 * while_each_thread() are protected by RCU.
2195 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07002196static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002197{
2198 struct task_struct *p, *g;
2199 write_lock(&css_set_lock);
2200 use_task_css_set_links = 1;
2201 do_each_thread(g, p) {
2202 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08002203 /*
2204 * We should check if the process is exiting, otherwise
2205 * it will race with cgroup_exit() in that the list
2206 * entry won't be deleted though the process has exited.
2207 */
2208 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Cliff Wickman31a7df02008-02-07 00:14:42 -08002209 list_add(&p->cg_list, &p->cgroups->tasks);
2210 task_unlock(p);
2211 } while_each_thread(g, p);
2212 write_unlock(&css_set_lock);
2213}
2214
Paul Menagebd89aab2007-10-18 23:40:44 -07002215void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002216{
2217 /*
2218 * The first time anyone tries to iterate across a cgroup,
2219 * we need to enable the list linking each css_set to its
2220 * tasks, and fix up all existing tasks.
2221 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08002222 if (!use_task_css_set_links)
2223 cgroup_enable_task_cg_lists();
2224
Paul Menage817929e2007-10-18 23:39:36 -07002225 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07002226 it->cg_link = &cgrp->css_sets;
2227 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07002228}
2229
Paul Menagebd89aab2007-10-18 23:40:44 -07002230struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -07002231 struct cgroup_iter *it)
2232{
2233 struct task_struct *res;
2234 struct list_head *l = it->task;
Lai Jiangshan2019f632009-01-07 18:07:36 -08002235 struct cg_cgroup_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07002236
2237 /* If the iterator cg is NULL, we have no tasks */
2238 if (!it->cg_link)
2239 return NULL;
2240 res = list_entry(l, struct task_struct, cg_list);
2241 /* Advance iterator to find next entry */
2242 l = l->next;
Lai Jiangshan2019f632009-01-07 18:07:36 -08002243 link = list_entry(it->cg_link, struct cg_cgroup_link, cgrp_link_list);
2244 if (l == &link->cg->tasks) {
Paul Menage817929e2007-10-18 23:39:36 -07002245 /* We reached the end of this task list - move on to
2246 * the next cg_cgroup_link */
Paul Menagebd89aab2007-10-18 23:40:44 -07002247 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07002248 } else {
2249 it->task = l;
2250 }
2251 return res;
2252}
2253
Paul Menagebd89aab2007-10-18 23:40:44 -07002254void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002255{
2256 read_unlock(&css_set_lock);
2257}
2258
Cliff Wickman31a7df02008-02-07 00:14:42 -08002259static inline int started_after_time(struct task_struct *t1,
2260 struct timespec *time,
2261 struct task_struct *t2)
2262{
2263 int start_diff = timespec_compare(&t1->start_time, time);
2264 if (start_diff > 0) {
2265 return 1;
2266 } else if (start_diff < 0) {
2267 return 0;
2268 } else {
2269 /*
2270 * Arbitrarily, if two processes started at the same
2271 * time, we'll say that the lower pointer value
2272 * started first. Note that t2 may have exited by now
2273 * so this may not be a valid pointer any longer, but
2274 * that's fine - it still serves to distinguish
2275 * between two tasks started (effectively) simultaneously.
2276 */
2277 return t1 > t2;
2278 }
2279}
2280
2281/*
2282 * This function is a callback from heap_insert() and is used to order
2283 * the heap.
2284 * In this case we order the heap in descending task start time.
2285 */
2286static inline int started_after(void *p1, void *p2)
2287{
2288 struct task_struct *t1 = p1;
2289 struct task_struct *t2 = p2;
2290 return started_after_time(t1, &t2->start_time, t2);
2291}
2292
2293/**
2294 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
2295 * @scan: struct cgroup_scanner containing arguments for the scan
2296 *
2297 * Arguments include pointers to callback functions test_task() and
2298 * process_task().
2299 * Iterate through all the tasks in a cgroup, calling test_task() for each,
2300 * and if it returns true, call process_task() for it also.
2301 * The test_task pointer may be NULL, meaning always true (select all tasks).
2302 * Effectively duplicates cgroup_iter_{start,next,end}()
2303 * but does not lock css_set_lock for the call to process_task().
2304 * The struct cgroup_scanner may be embedded in any structure of the caller's
2305 * creation.
2306 * It is guaranteed that process_task() will act on every task that
2307 * is a member of the cgroup for the duration of this call. This
2308 * function may or may not call process_task() for tasks that exit
2309 * or move to a different cgroup during the call, or are forked or
2310 * move into the cgroup during the call.
2311 *
2312 * Note that test_task() may be called with locks held, and may in some
2313 * situations be called multiple times for the same task, so it should
2314 * be cheap.
2315 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
2316 * pre-allocated and will be used for heap operations (and its "gt" member will
2317 * be overwritten), else a temporary heap will be used (allocation of which
2318 * may cause this function to fail).
2319 */
2320int cgroup_scan_tasks(struct cgroup_scanner *scan)
2321{
2322 int retval, i;
2323 struct cgroup_iter it;
2324 struct task_struct *p, *dropped;
2325 /* Never dereference latest_task, since it's not refcounted */
2326 struct task_struct *latest_task = NULL;
2327 struct ptr_heap tmp_heap;
2328 struct ptr_heap *heap;
2329 struct timespec latest_time = { 0, 0 };
2330
2331 if (scan->heap) {
2332 /* The caller supplied our heap and pre-allocated its memory */
2333 heap = scan->heap;
2334 heap->gt = &started_after;
2335 } else {
2336 /* We need to allocate our own heap memory */
2337 heap = &tmp_heap;
2338 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
2339 if (retval)
2340 /* cannot allocate the heap */
2341 return retval;
2342 }
2343
2344 again:
2345 /*
2346 * Scan tasks in the cgroup, using the scanner's "test_task" callback
2347 * to determine which are of interest, and using the scanner's
2348 * "process_task" callback to process any of them that need an update.
2349 * Since we don't want to hold any locks during the task updates,
2350 * gather tasks to be processed in a heap structure.
2351 * The heap is sorted by descending task start time.
2352 * If the statically-sized heap fills up, we overflow tasks that
2353 * started later, and in future iterations only consider tasks that
2354 * started after the latest task in the previous pass. This
2355 * guarantees forward progress and that we don't miss any tasks.
2356 */
2357 heap->size = 0;
2358 cgroup_iter_start(scan->cg, &it);
2359 while ((p = cgroup_iter_next(scan->cg, &it))) {
2360 /*
2361 * Only affect tasks that qualify per the caller's callback,
2362 * if he provided one
2363 */
2364 if (scan->test_task && !scan->test_task(p, scan))
2365 continue;
2366 /*
2367 * Only process tasks that started after the last task
2368 * we processed
2369 */
2370 if (!started_after_time(p, &latest_time, latest_task))
2371 continue;
2372 dropped = heap_insert(heap, p);
2373 if (dropped == NULL) {
2374 /*
2375 * The new task was inserted; the heap wasn't
2376 * previously full
2377 */
2378 get_task_struct(p);
2379 } else if (dropped != p) {
2380 /*
2381 * The new task was inserted, and pushed out a
2382 * different task
2383 */
2384 get_task_struct(p);
2385 put_task_struct(dropped);
2386 }
2387 /*
2388 * Else the new task was newer than anything already in
2389 * the heap and wasn't inserted
2390 */
2391 }
2392 cgroup_iter_end(scan->cg, &it);
2393
2394 if (heap->size) {
2395 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07002396 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08002397 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07002398 latest_time = q->start_time;
2399 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08002400 }
2401 /* Process the task per the caller's callback */
Paul Jackson4fe91d52008-04-29 00:59:55 -07002402 scan->process_task(q, scan);
2403 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002404 }
2405 /*
2406 * If we had to process any tasks at all, scan again
2407 * in case some of them were in the middle of forking
2408 * children that didn't get processed.
2409 * Not the most efficient way to do it, but it avoids
2410 * having to take callback_mutex in the fork path
2411 */
2412 goto again;
2413 }
2414 if (heap == &tmp_heap)
2415 heap_free(&tmp_heap);
2416 return 0;
2417}
2418
Paul Menage817929e2007-10-18 23:39:36 -07002419/*
Ben Blum102a7752009-09-23 15:56:26 -07002420 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07002421 *
2422 * Reading this file can return large amounts of data if a cgroup has
2423 * *lots* of attached tasks. So it may need several calls to read(),
2424 * but we cannot guarantee that the information we produce is correct
2425 * unless we produce it entirely atomically.
2426 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07002427 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07002428
2429/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07002430 * The following two functions "fix" the issue where there are more pids
2431 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
2432 * TODO: replace with a kernel-wide solution to this problem
2433 */
2434#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
2435static void *pidlist_allocate(int count)
2436{
2437 if (PIDLIST_TOO_LARGE(count))
2438 return vmalloc(count * sizeof(pid_t));
2439 else
2440 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
2441}
2442static void pidlist_free(void *p)
2443{
2444 if (is_vmalloc_addr(p))
2445 vfree(p);
2446 else
2447 kfree(p);
2448}
2449static void *pidlist_resize(void *p, int newcount)
2450{
2451 void *newlist;
2452 /* note: if new alloc fails, old p will still be valid either way */
2453 if (is_vmalloc_addr(p)) {
2454 newlist = vmalloc(newcount * sizeof(pid_t));
2455 if (!newlist)
2456 return NULL;
2457 memcpy(newlist, p, newcount * sizeof(pid_t));
2458 vfree(p);
2459 } else {
2460 newlist = krealloc(p, newcount * sizeof(pid_t), GFP_KERNEL);
2461 }
2462 return newlist;
2463}
2464
2465/*
Ben Blum102a7752009-09-23 15:56:26 -07002466 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
2467 * If the new stripped list is sufficiently smaller and there's enough memory
2468 * to allocate a new buffer, will let go of the unneeded memory. Returns the
2469 * number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07002470 */
Ben Blum102a7752009-09-23 15:56:26 -07002471/* is the size difference enough that we should re-allocate the array? */
2472#define PIDLIST_REALLOC_DIFFERENCE(old, new) ((old) - PAGE_SIZE >= (new))
2473static int pidlist_uniq(pid_t **p, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002474{
Ben Blum102a7752009-09-23 15:56:26 -07002475 int src, dest = 1;
2476 pid_t *list = *p;
2477 pid_t *newlist;
2478
2479 /*
2480 * we presume the 0th element is unique, so i starts at 1. trivial
2481 * edge cases first; no work needs to be done for either
2482 */
2483 if (length == 0 || length == 1)
2484 return length;
2485 /* src and dest walk down the list; dest counts unique elements */
2486 for (src = 1; src < length; src++) {
2487 /* find next unique element */
2488 while (list[src] == list[src-1]) {
2489 src++;
2490 if (src == length)
2491 goto after;
2492 }
2493 /* dest always points to where the next unique element goes */
2494 list[dest] = list[src];
2495 dest++;
2496 }
2497after:
2498 /*
2499 * if the length difference is large enough, we want to allocate a
2500 * smaller buffer to save memory. if this fails due to out of memory,
2501 * we'll just stay with what we've got.
2502 */
2503 if (PIDLIST_REALLOC_DIFFERENCE(length, dest)) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07002504 newlist = pidlist_resize(list, dest);
Ben Blum102a7752009-09-23 15:56:26 -07002505 if (newlist)
2506 *p = newlist;
2507 }
2508 return dest;
2509}
2510
2511static int cmppid(const void *a, const void *b)
2512{
2513 return *(pid_t *)a - *(pid_t *)b;
2514}
2515
2516/*
Ben Blum72a8cb32009-09-23 15:56:27 -07002517 * find the appropriate pidlist for our purpose (given procs vs tasks)
2518 * returns with the lock on that pidlist already held, and takes care
2519 * of the use count, or returns NULL with no locks held if we're out of
2520 * memory.
2521 */
2522static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
2523 enum cgroup_filetype type)
2524{
2525 struct cgroup_pidlist *l;
2526 /* don't need task_nsproxy() if we're looking at ourself */
2527 struct pid_namespace *ns = get_pid_ns(current->nsproxy->pid_ns);
2528 /*
2529 * We can't drop the pidlist_mutex before taking the l->mutex in case
2530 * the last ref-holder is trying to remove l from the list at the same
2531 * time. Holding the pidlist_mutex precludes somebody taking whichever
2532 * list we find out from under us - compare release_pid_array().
2533 */
2534 mutex_lock(&cgrp->pidlist_mutex);
2535 list_for_each_entry(l, &cgrp->pidlists, links) {
2536 if (l->key.type == type && l->key.ns == ns) {
2537 /* found a matching list - drop the extra refcount */
2538 put_pid_ns(ns);
2539 /* make sure l doesn't vanish out from under us */
2540 down_write(&l->mutex);
2541 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07002542 return l;
2543 }
2544 }
2545 /* entry not found; create a new one */
2546 l = kmalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
2547 if (!l) {
2548 mutex_unlock(&cgrp->pidlist_mutex);
2549 put_pid_ns(ns);
2550 return l;
2551 }
2552 init_rwsem(&l->mutex);
2553 down_write(&l->mutex);
2554 l->key.type = type;
2555 l->key.ns = ns;
2556 l->use_count = 0; /* don't increment here */
2557 l->list = NULL;
2558 l->owner = cgrp;
2559 list_add(&l->links, &cgrp->pidlists);
2560 mutex_unlock(&cgrp->pidlist_mutex);
2561 return l;
2562}
2563
2564/*
Ben Blum102a7752009-09-23 15:56:26 -07002565 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
2566 */
Ben Blum72a8cb32009-09-23 15:56:27 -07002567static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
2568 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07002569{
2570 pid_t *array;
2571 int length;
2572 int pid, n = 0; /* used for populating the array */
Paul Menage817929e2007-10-18 23:39:36 -07002573 struct cgroup_iter it;
2574 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07002575 struct cgroup_pidlist *l;
2576
2577 /*
2578 * If cgroup gets more users after we read count, we won't have
2579 * enough space - tough. This race is indistinguishable to the
2580 * caller from the case that the additional cgroup users didn't
2581 * show up until sometime later on.
2582 */
2583 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07002584 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07002585 if (!array)
2586 return -ENOMEM;
2587 /* now, populate the array */
Paul Menagebd89aab2007-10-18 23:40:44 -07002588 cgroup_iter_start(cgrp, &it);
2589 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Ben Blum102a7752009-09-23 15:56:26 -07002590 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07002591 break;
Ben Blum102a7752009-09-23 15:56:26 -07002592 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07002593 if (type == CGROUP_FILE_PROCS)
2594 pid = task_tgid_vnr(tsk);
2595 else
2596 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07002597 if (pid > 0) /* make sure to only use valid results */
2598 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07002599 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002600 cgroup_iter_end(cgrp, &it);
Ben Blum102a7752009-09-23 15:56:26 -07002601 length = n;
2602 /* now sort & (if procs) strip out duplicates */
2603 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07002604 if (type == CGROUP_FILE_PROCS)
Ben Blum102a7752009-09-23 15:56:26 -07002605 length = pidlist_uniq(&array, length);
Ben Blum72a8cb32009-09-23 15:56:27 -07002606 l = cgroup_pidlist_find(cgrp, type);
2607 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07002608 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07002609 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07002610 }
Ben Blum72a8cb32009-09-23 15:56:27 -07002611 /* store array, freeing old if necessary - lock already held */
Ben Blumd1d9fd32009-09-23 15:56:28 -07002612 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07002613 l->list = array;
2614 l->length = length;
2615 l->use_count++;
2616 up_write(&l->mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07002617 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07002618 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002619}
2620
Balbir Singh846c7bb2007-10-18 23:39:44 -07002621/**
Li Zefana043e3b2008-02-23 15:24:09 -08002622 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07002623 * @stats: cgroupstats to fill information into
2624 * @dentry: A dentry entry belonging to the cgroup for which stats have
2625 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08002626 *
2627 * Build and fill cgroupstats so that taskstats can export it to user
2628 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07002629 */
2630int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
2631{
2632 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07002633 struct cgroup *cgrp;
Balbir Singh846c7bb2007-10-18 23:39:44 -07002634 struct cgroup_iter it;
2635 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08002636
Balbir Singh846c7bb2007-10-18 23:39:44 -07002637 /*
Li Zefan33d283b2008-11-19 15:36:48 -08002638 * Validate dentry by checking the superblock operations,
2639 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07002640 */
Li Zefan33d283b2008-11-19 15:36:48 -08002641 if (dentry->d_sb->s_op != &cgroup_ops ||
2642 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07002643 goto err;
2644
2645 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07002646 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07002647
Paul Menagebd89aab2007-10-18 23:40:44 -07002648 cgroup_iter_start(cgrp, &it);
2649 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07002650 switch (tsk->state) {
2651 case TASK_RUNNING:
2652 stats->nr_running++;
2653 break;
2654 case TASK_INTERRUPTIBLE:
2655 stats->nr_sleeping++;
2656 break;
2657 case TASK_UNINTERRUPTIBLE:
2658 stats->nr_uninterruptible++;
2659 break;
2660 case TASK_STOPPED:
2661 stats->nr_stopped++;
2662 break;
2663 default:
2664 if (delayacct_is_task_waiting_on_io(tsk))
2665 stats->nr_io_wait++;
2666 break;
2667 }
2668 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002669 cgroup_iter_end(cgrp, &it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07002670
Balbir Singh846c7bb2007-10-18 23:39:44 -07002671err:
2672 return ret;
2673}
2674
Paul Menage8f3ff202009-09-23 15:56:25 -07002675
Paul Menagecc31edc2008-10-18 20:28:04 -07002676/*
Ben Blum102a7752009-09-23 15:56:26 -07002677 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07002678 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07002679 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07002680 */
2681
Ben Blum102a7752009-09-23 15:56:26 -07002682static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07002683{
2684 /*
2685 * Initially we receive a position value that corresponds to
2686 * one more than the last pid shown (or 0 on the first call or
2687 * after a seek to the start). Use a binary-search to find the
2688 * next pid to display, if any
2689 */
Ben Blum102a7752009-09-23 15:56:26 -07002690 struct cgroup_pidlist *l = s->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07002691 int index = 0, pid = *pos;
2692 int *iter;
2693
Ben Blum102a7752009-09-23 15:56:26 -07002694 down_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07002695 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07002696 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11002697
Paul Menagecc31edc2008-10-18 20:28:04 -07002698 while (index < end) {
2699 int mid = (index + end) / 2;
Ben Blum102a7752009-09-23 15:56:26 -07002700 if (l->list[mid] == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07002701 index = mid;
2702 break;
Ben Blum102a7752009-09-23 15:56:26 -07002703 } else if (l->list[mid] <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07002704 index = mid + 1;
2705 else
2706 end = mid;
2707 }
2708 }
2709 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07002710 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07002711 return NULL;
2712 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07002713 iter = l->list + index;
Paul Menagecc31edc2008-10-18 20:28:04 -07002714 *pos = *iter;
2715 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002716}
2717
Ben Blum102a7752009-09-23 15:56:26 -07002718static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07002719{
Ben Blum102a7752009-09-23 15:56:26 -07002720 struct cgroup_pidlist *l = s->private;
2721 up_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07002722}
2723
Ben Blum102a7752009-09-23 15:56:26 -07002724static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07002725{
Ben Blum102a7752009-09-23 15:56:26 -07002726 struct cgroup_pidlist *l = s->private;
2727 pid_t *p = v;
2728 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07002729 /*
2730 * Advance to the next pid in the array. If this goes off the
2731 * end, we're done
2732 */
2733 p++;
2734 if (p >= end) {
2735 return NULL;
2736 } else {
2737 *pos = *p;
2738 return p;
2739 }
2740}
2741
Ben Blum102a7752009-09-23 15:56:26 -07002742static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07002743{
2744 return seq_printf(s, "%d\n", *(int *)v);
2745}
2746
Ben Blum102a7752009-09-23 15:56:26 -07002747/*
2748 * seq_operations functions for iterating on pidlists through seq_file -
2749 * independent of whether it's tasks or procs
2750 */
2751static const struct seq_operations cgroup_pidlist_seq_operations = {
2752 .start = cgroup_pidlist_start,
2753 .stop = cgroup_pidlist_stop,
2754 .next = cgroup_pidlist_next,
2755 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07002756};
2757
Ben Blum102a7752009-09-23 15:56:26 -07002758static void cgroup_release_pid_array(struct cgroup_pidlist *l)
Paul Menagecc31edc2008-10-18 20:28:04 -07002759{
Ben Blum72a8cb32009-09-23 15:56:27 -07002760 /*
2761 * the case where we're the last user of this particular pidlist will
2762 * have us remove it from the cgroup's list, which entails taking the
2763 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
2764 * pidlist_mutex, we have to take pidlist_mutex first.
2765 */
2766 mutex_lock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07002767 down_write(&l->mutex);
2768 BUG_ON(!l->use_count);
2769 if (!--l->use_count) {
Ben Blum72a8cb32009-09-23 15:56:27 -07002770 /* we're the last user if refcount is 0; remove and free */
2771 list_del(&l->links);
2772 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07002773 pidlist_free(l->list);
Ben Blum72a8cb32009-09-23 15:56:27 -07002774 put_pid_ns(l->key.ns);
2775 up_write(&l->mutex);
2776 kfree(l);
2777 return;
Paul Menagecc31edc2008-10-18 20:28:04 -07002778 }
Ben Blum72a8cb32009-09-23 15:56:27 -07002779 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07002780 up_write(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07002781}
2782
Ben Blum102a7752009-09-23 15:56:26 -07002783static int cgroup_pidlist_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002784{
Ben Blum102a7752009-09-23 15:56:26 -07002785 struct cgroup_pidlist *l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002786 if (!(file->f_mode & FMODE_READ))
2787 return 0;
Ben Blum102a7752009-09-23 15:56:26 -07002788 /*
2789 * the seq_file will only be initialized if the file was opened for
2790 * reading; hence we check if it's not null only in that case.
2791 */
2792 l = ((struct seq_file *)file->private_data)->private;
2793 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07002794 return seq_release(inode, file);
2795}
2796
Ben Blum102a7752009-09-23 15:56:26 -07002797static const struct file_operations cgroup_pidlist_operations = {
Paul Menagecc31edc2008-10-18 20:28:04 -07002798 .read = seq_read,
2799 .llseek = seq_lseek,
2800 .write = cgroup_file_write,
Ben Blum102a7752009-09-23 15:56:26 -07002801 .release = cgroup_pidlist_release,
Paul Menagecc31edc2008-10-18 20:28:04 -07002802};
2803
2804/*
Ben Blum102a7752009-09-23 15:56:26 -07002805 * The following functions handle opens on a file that displays a pidlist
2806 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
2807 * in the cgroup.
Paul Menagecc31edc2008-10-18 20:28:04 -07002808 */
Ben Blum102a7752009-09-23 15:56:26 -07002809/* helper function for the two below it */
Ben Blum72a8cb32009-09-23 15:56:27 -07002810static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
Paul Menagecc31edc2008-10-18 20:28:04 -07002811{
2812 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Ben Blum72a8cb32009-09-23 15:56:27 -07002813 struct cgroup_pidlist *l;
Paul Menagecc31edc2008-10-18 20:28:04 -07002814 int retval;
2815
2816 /* Nothing to do for write-only files */
2817 if (!(file->f_mode & FMODE_READ))
2818 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002819
Ben Blum102a7752009-09-23 15:56:26 -07002820 /* have the array populated */
Ben Blum72a8cb32009-09-23 15:56:27 -07002821 retval = pidlist_array_load(cgrp, type, &l);
Ben Blum102a7752009-09-23 15:56:26 -07002822 if (retval)
2823 return retval;
2824 /* configure file information */
2825 file->f_op = &cgroup_pidlist_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002826
Ben Blum102a7752009-09-23 15:56:26 -07002827 retval = seq_open(file, &cgroup_pidlist_seq_operations);
Paul Menagecc31edc2008-10-18 20:28:04 -07002828 if (retval) {
Ben Blum102a7752009-09-23 15:56:26 -07002829 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07002830 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002831 }
Ben Blum102a7752009-09-23 15:56:26 -07002832 ((struct seq_file *)file->private_data)->private = l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002833 return 0;
2834}
Ben Blum102a7752009-09-23 15:56:26 -07002835static int cgroup_tasks_open(struct inode *unused, struct file *file)
2836{
Ben Blum72a8cb32009-09-23 15:56:27 -07002837 return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
Ben Blum102a7752009-09-23 15:56:26 -07002838}
2839static int cgroup_procs_open(struct inode *unused, struct file *file)
2840{
Ben Blum72a8cb32009-09-23 15:56:27 -07002841 return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
Ben Blum102a7752009-09-23 15:56:26 -07002842}
Paul Menagebbcb81d2007-10-18 23:39:32 -07002843
Paul Menagebd89aab2007-10-18 23:40:44 -07002844static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
Paul Menage81a6a5c2007-10-18 23:39:38 -07002845 struct cftype *cft)
2846{
Paul Menagebd89aab2007-10-18 23:40:44 -07002847 return notify_on_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07002848}
2849
Paul Menage6379c102008-07-25 01:47:01 -07002850static int cgroup_write_notify_on_release(struct cgroup *cgrp,
2851 struct cftype *cft,
2852 u64 val)
2853{
2854 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
2855 if (val)
2856 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
2857 else
2858 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
2859 return 0;
2860}
2861
Paul Menagebbcb81d2007-10-18 23:39:32 -07002862/*
2863 * for the common functions, 'private' gives the type of file
2864 */
Ben Blum102a7752009-09-23 15:56:26 -07002865/* for hysterical raisins, we can't put this on the older files */
2866#define CGROUP_FILE_GENERIC_PREFIX "cgroup."
Paul Menage81a6a5c2007-10-18 23:39:38 -07002867static struct cftype files[] = {
2868 {
2869 .name = "tasks",
2870 .open = cgroup_tasks_open,
Paul Menageaf351022008-07-25 01:47:01 -07002871 .write_u64 = cgroup_tasks_write,
Ben Blum102a7752009-09-23 15:56:26 -07002872 .release = cgroup_pidlist_release,
Li Zefan099fca32009-04-02 16:57:29 -07002873 .mode = S_IRUGO | S_IWUSR,
Paul Menage81a6a5c2007-10-18 23:39:38 -07002874 },
Ben Blum102a7752009-09-23 15:56:26 -07002875 {
2876 .name = CGROUP_FILE_GENERIC_PREFIX "procs",
2877 .open = cgroup_procs_open,
2878 /* .write_u64 = cgroup_procs_write, TODO */
2879 .release = cgroup_pidlist_release,
2880 .mode = S_IRUGO,
2881 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07002882 {
2883 .name = "notify_on_release",
Paul Menagef4c753b2008-04-29 00:59:56 -07002884 .read_u64 = cgroup_read_notify_on_release,
Paul Menage6379c102008-07-25 01:47:01 -07002885 .write_u64 = cgroup_write_notify_on_release,
Paul Menage81a6a5c2007-10-18 23:39:38 -07002886 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07002887};
2888
2889static struct cftype cft_release_agent = {
2890 .name = "release_agent",
Paul Menagee788e062008-07-25 01:46:59 -07002891 .read_seq_string = cgroup_release_agent_show,
2892 .write_string = cgroup_release_agent_write,
2893 .max_write_len = PATH_MAX,
Paul Menagebbcb81d2007-10-18 23:39:32 -07002894};
2895
Paul Menagebd89aab2007-10-18 23:40:44 -07002896static int cgroup_populate_dir(struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002897{
2898 int err;
2899 struct cgroup_subsys *ss;
2900
2901 /* First clear out any existing files */
Paul Menagebd89aab2007-10-18 23:40:44 -07002902 cgroup_clear_directory(cgrp->dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002903
Paul Menagebd89aab2007-10-18 23:40:44 -07002904 err = cgroup_add_files(cgrp, NULL, files, ARRAY_SIZE(files));
Paul Menagebbcb81d2007-10-18 23:39:32 -07002905 if (err < 0)
2906 return err;
2907
Paul Menagebd89aab2007-10-18 23:40:44 -07002908 if (cgrp == cgrp->top_cgroup) {
2909 if ((err = cgroup_add_file(cgrp, NULL, &cft_release_agent)) < 0)
Paul Menage81a6a5c2007-10-18 23:39:38 -07002910 return err;
2911 }
2912
Paul Menagebd89aab2007-10-18 23:40:44 -07002913 for_each_subsys(cgrp->root, ss) {
2914 if (ss->populate && (err = ss->populate(ss, cgrp)) < 0)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002915 return err;
2916 }
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07002917 /* This cgroup is ready now */
2918 for_each_subsys(cgrp->root, ss) {
2919 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
2920 /*
2921 * Update id->css pointer and make this css visible from
2922 * CSS ID functions. This pointer will be dereferened
2923 * from RCU-read-side without locks.
2924 */
2925 if (css->id)
2926 rcu_assign_pointer(css->id->css, css);
2927 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002928
2929 return 0;
2930}
2931
2932static void init_cgroup_css(struct cgroup_subsys_state *css,
2933 struct cgroup_subsys *ss,
Paul Menagebd89aab2007-10-18 23:40:44 -07002934 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002935{
Paul Menagebd89aab2007-10-18 23:40:44 -07002936 css->cgroup = cgrp;
Paul Menagee7c5ec92009-01-07 18:08:38 -08002937 atomic_set(&css->refcnt, 1);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002938 css->flags = 0;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07002939 css->id = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07002940 if (cgrp == dummytop)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002941 set_bit(CSS_ROOT, &css->flags);
Paul Menagebd89aab2007-10-18 23:40:44 -07002942 BUG_ON(cgrp->subsys[ss->subsys_id]);
2943 cgrp->subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002944}
2945
Paul Menage999cd8a2009-01-07 18:08:36 -08002946static void cgroup_lock_hierarchy(struct cgroupfs_root *root)
2947{
2948 /* We need to take each hierarchy_mutex in a consistent order */
2949 int i;
2950
Ben Blumaae8aab2010-03-10 15:22:07 -08002951 /*
2952 * No worry about a race with rebind_subsystems that might mess up the
2953 * locking order, since both parties are under cgroup_mutex.
2954 */
Paul Menage999cd8a2009-01-07 18:08:36 -08002955 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2956 struct cgroup_subsys *ss = subsys[i];
Ben Blumaae8aab2010-03-10 15:22:07 -08002957 if (ss == NULL)
2958 continue;
Paul Menage999cd8a2009-01-07 18:08:36 -08002959 if (ss->root == root)
Li Zefancfebe562009-02-11 13:04:36 -08002960 mutex_lock(&ss->hierarchy_mutex);
Paul Menage999cd8a2009-01-07 18:08:36 -08002961 }
2962}
2963
2964static void cgroup_unlock_hierarchy(struct cgroupfs_root *root)
2965{
2966 int i;
2967
2968 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2969 struct cgroup_subsys *ss = subsys[i];
Ben Blumaae8aab2010-03-10 15:22:07 -08002970 if (ss == NULL)
2971 continue;
Paul Menage999cd8a2009-01-07 18:08:36 -08002972 if (ss->root == root)
2973 mutex_unlock(&ss->hierarchy_mutex);
2974 }
2975}
2976
Paul Menageddbcc7e2007-10-18 23:39:30 -07002977/*
Li Zefana043e3b2008-02-23 15:24:09 -08002978 * cgroup_create - create a cgroup
2979 * @parent: cgroup that will be parent of the new cgroup
2980 * @dentry: dentry of the new cgroup
2981 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07002982 *
Li Zefana043e3b2008-02-23 15:24:09 -08002983 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07002984 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07002985static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Li Zefan099fca32009-04-02 16:57:29 -07002986 mode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002987{
Paul Menagebd89aab2007-10-18 23:40:44 -07002988 struct cgroup *cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002989 struct cgroupfs_root *root = parent->root;
2990 int err = 0;
2991 struct cgroup_subsys *ss;
2992 struct super_block *sb = root->sb;
2993
Paul Menagebd89aab2007-10-18 23:40:44 -07002994 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
2995 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002996 return -ENOMEM;
2997
2998 /* Grab a reference on the superblock so the hierarchy doesn't
2999 * get deleted on unmount if there are child cgroups. This
3000 * can be done outside cgroup_mutex, since the sb can't
3001 * disappear while someone has an open control file on the
3002 * fs */
3003 atomic_inc(&sb->s_active);
3004
3005 mutex_lock(&cgroup_mutex);
3006
Paul Menagecc31edc2008-10-18 20:28:04 -07003007 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003008
Paul Menagebd89aab2007-10-18 23:40:44 -07003009 cgrp->parent = parent;
3010 cgrp->root = parent->root;
3011 cgrp->top_cgroup = parent->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003012
Li Zefanb6abdb02008-03-04 14:28:19 -08003013 if (notify_on_release(parent))
3014 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3015
Paul Menageddbcc7e2007-10-18 23:39:30 -07003016 for_each_subsys(root, ss) {
Paul Menagebd89aab2007-10-18 23:40:44 -07003017 struct cgroup_subsys_state *css = ss->create(ss, cgrp);
Li Zefan4528fd02010-02-02 13:44:10 -08003018
Paul Menageddbcc7e2007-10-18 23:39:30 -07003019 if (IS_ERR(css)) {
3020 err = PTR_ERR(css);
3021 goto err_destroy;
3022 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003023 init_cgroup_css(css, ss, cgrp);
Li Zefan4528fd02010-02-02 13:44:10 -08003024 if (ss->use_id) {
3025 err = alloc_css_id(ss, parent, cgrp);
3026 if (err)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07003027 goto err_destroy;
Li Zefan4528fd02010-02-02 13:44:10 -08003028 }
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07003029 /* At error, ->destroy() callback has to free assigned ID. */
Paul Menageddbcc7e2007-10-18 23:39:30 -07003030 }
3031
Paul Menage999cd8a2009-01-07 18:08:36 -08003032 cgroup_lock_hierarchy(root);
Paul Menagebd89aab2007-10-18 23:40:44 -07003033 list_add(&cgrp->sibling, &cgrp->parent->children);
Paul Menage999cd8a2009-01-07 18:08:36 -08003034 cgroup_unlock_hierarchy(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003035 root->number_of_cgroups++;
3036
Paul Menagebd89aab2007-10-18 23:40:44 -07003037 err = cgroup_create_dir(cgrp, dentry, mode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003038 if (err < 0)
3039 goto err_remove;
3040
3041 /* The cgroup directory was pre-locked for us */
Paul Menagebd89aab2007-10-18 23:40:44 -07003042 BUG_ON(!mutex_is_locked(&cgrp->dentry->d_inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07003043
Paul Menagebd89aab2007-10-18 23:40:44 -07003044 err = cgroup_populate_dir(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003045 /* If err < 0, we have a half-filled directory - oh well ;) */
3046
3047 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07003048 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003049
3050 return 0;
3051
3052 err_remove:
3053
KAMEZAWA Hiroyukibaef99a2009-01-29 14:25:10 -08003054 cgroup_lock_hierarchy(root);
Paul Menagebd89aab2007-10-18 23:40:44 -07003055 list_del(&cgrp->sibling);
KAMEZAWA Hiroyukibaef99a2009-01-29 14:25:10 -08003056 cgroup_unlock_hierarchy(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003057 root->number_of_cgroups--;
3058
3059 err_destroy:
3060
3061 for_each_subsys(root, ss) {
Paul Menagebd89aab2007-10-18 23:40:44 -07003062 if (cgrp->subsys[ss->subsys_id])
3063 ss->destroy(ss, cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003064 }
3065
3066 mutex_unlock(&cgroup_mutex);
3067
3068 /* Release the reference count that we took on the superblock */
3069 deactivate_super(sb);
3070
Paul Menagebd89aab2007-10-18 23:40:44 -07003071 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003072 return err;
3073}
3074
3075static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode)
3076{
3077 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
3078
3079 /* the vfs holds inode->i_mutex already */
3080 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
3081}
3082
Li Zefan55b6fd02008-07-29 22:33:20 -07003083static int cgroup_has_css_refs(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003084{
3085 /* Check the reference count on each subsystem. Since we
3086 * already established that there are no tasks in the
Paul Menagee7c5ec92009-01-07 18:08:38 -08003087 * cgroup, if the css refcount is also 1, then there should
Paul Menage81a6a5c2007-10-18 23:39:38 -07003088 * be no outstanding references, so the subsystem is safe to
3089 * destroy. We scan across all subsystems rather than using
3090 * the per-hierarchy linked list of mounted subsystems since
3091 * we can be called via check_for_release() with no
3092 * synchronization other than RCU, and the subsystem linked
3093 * list isn't RCU-safe */
3094 int i;
Ben Blumaae8aab2010-03-10 15:22:07 -08003095 /*
3096 * We won't need to lock the subsys array, because the subsystems
3097 * we're concerned about aren't going anywhere since our cgroup root
3098 * has a reference on them.
3099 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07003100 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3101 struct cgroup_subsys *ss = subsys[i];
3102 struct cgroup_subsys_state *css;
Ben Blumaae8aab2010-03-10 15:22:07 -08003103 /* Skip subsystems not present or not in this hierarchy */
3104 if (ss == NULL || ss->root != cgrp->root)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003105 continue;
Paul Menagebd89aab2007-10-18 23:40:44 -07003106 css = cgrp->subsys[ss->subsys_id];
Paul Menage81a6a5c2007-10-18 23:39:38 -07003107 /* When called from check_for_release() it's possible
3108 * that by this point the cgroup has been removed
3109 * and the css deleted. But a false-positive doesn't
3110 * matter, since it can only happen if the cgroup
3111 * has been deleted and hence no longer needs the
3112 * release agent to be called anyway. */
Paul Menagee7c5ec92009-01-07 18:08:38 -08003113 if (css && (atomic_read(&css->refcnt) > 1))
Paul Menage81a6a5c2007-10-18 23:39:38 -07003114 return 1;
Paul Menage81a6a5c2007-10-18 23:39:38 -07003115 }
3116 return 0;
3117}
3118
Paul Menagee7c5ec92009-01-07 18:08:38 -08003119/*
3120 * Atomically mark all (or else none) of the cgroup's CSS objects as
3121 * CSS_REMOVED. Return true on success, or false if the cgroup has
3122 * busy subsystems. Call with cgroup_mutex held
3123 */
3124
3125static int cgroup_clear_css_refs(struct cgroup *cgrp)
3126{
3127 struct cgroup_subsys *ss;
3128 unsigned long flags;
3129 bool failed = false;
3130 local_irq_save(flags);
3131 for_each_subsys(cgrp->root, ss) {
3132 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
3133 int refcnt;
Paul Menage804b3c22009-01-29 14:25:21 -08003134 while (1) {
Paul Menagee7c5ec92009-01-07 18:08:38 -08003135 /* We can only remove a CSS with a refcnt==1 */
3136 refcnt = atomic_read(&css->refcnt);
3137 if (refcnt > 1) {
3138 failed = true;
3139 goto done;
3140 }
3141 BUG_ON(!refcnt);
3142 /*
3143 * Drop the refcnt to 0 while we check other
3144 * subsystems. This will cause any racing
3145 * css_tryget() to spin until we set the
3146 * CSS_REMOVED bits or abort
3147 */
Paul Menage804b3c22009-01-29 14:25:21 -08003148 if (atomic_cmpxchg(&css->refcnt, refcnt, 0) == refcnt)
3149 break;
3150 cpu_relax();
3151 }
Paul Menagee7c5ec92009-01-07 18:08:38 -08003152 }
3153 done:
3154 for_each_subsys(cgrp->root, ss) {
3155 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
3156 if (failed) {
3157 /*
3158 * Restore old refcnt if we previously managed
3159 * to clear it from 1 to 0
3160 */
3161 if (!atomic_read(&css->refcnt))
3162 atomic_set(&css->refcnt, 1);
3163 } else {
3164 /* Commit the fact that the CSS is removed */
3165 set_bit(CSS_REMOVED, &css->flags);
3166 }
3167 }
3168 local_irq_restore(flags);
3169 return !failed;
3170}
3171
Paul Menageddbcc7e2007-10-18 23:39:30 -07003172static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
3173{
Paul Menagebd89aab2007-10-18 23:40:44 -07003174 struct cgroup *cgrp = dentry->d_fsdata;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003175 struct dentry *d;
3176 struct cgroup *parent;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003177 DEFINE_WAIT(wait);
3178 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003179
3180 /* the vfs holds both inode->i_mutex already */
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003181again:
Paul Menageddbcc7e2007-10-18 23:39:30 -07003182 mutex_lock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07003183 if (atomic_read(&cgrp->count) != 0) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07003184 mutex_unlock(&cgroup_mutex);
3185 return -EBUSY;
3186 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003187 if (!list_empty(&cgrp->children)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07003188 mutex_unlock(&cgroup_mutex);
3189 return -EBUSY;
3190 }
KAMEZAWA Hiroyuki3fa59df2008-11-19 15:36:34 -08003191 mutex_unlock(&cgroup_mutex);
Li Zefana043e3b2008-02-23 15:24:09 -08003192
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -08003193 /*
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003194 * In general, subsystem has no css->refcnt after pre_destroy(). But
3195 * in racy cases, subsystem may have to get css->refcnt after
3196 * pre_destroy() and it makes rmdir return with -EBUSY. This sometimes
3197 * make rmdir return -EBUSY too often. To avoid that, we use waitqueue
3198 * for cgroup's rmdir. CGRP_WAIT_ON_RMDIR is for synchronizing rmdir
3199 * and subsystem's reference count handling. Please see css_get/put
3200 * and css_tryget() and cgroup_wakeup_rmdir_waiter() implementation.
3201 */
3202 set_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
3203
3204 /*
Li Zefana043e3b2008-02-23 15:24:09 -08003205 * Call pre_destroy handlers of subsys. Notify subsystems
3206 * that rmdir() request comes.
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -08003207 */
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003208 ret = cgroup_call_pre_destroy(cgrp);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003209 if (ret) {
3210 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003211 return ret;
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003212 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003213
KAMEZAWA Hiroyuki3fa59df2008-11-19 15:36:34 -08003214 mutex_lock(&cgroup_mutex);
3215 parent = cgrp->parent;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003216 if (atomic_read(&cgrp->count) || !list_empty(&cgrp->children)) {
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003217 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003218 mutex_unlock(&cgroup_mutex);
3219 return -EBUSY;
3220 }
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003221 prepare_to_wait(&cgroup_rmdir_waitq, &wait, TASK_INTERRUPTIBLE);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003222 if (!cgroup_clear_css_refs(cgrp)) {
3223 mutex_unlock(&cgroup_mutex);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003224 /*
3225 * Because someone may call cgroup_wakeup_rmdir_waiter() before
3226 * prepare_to_wait(), we need to check this flag.
3227 */
3228 if (test_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags))
3229 schedule();
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003230 finish_wait(&cgroup_rmdir_waitq, &wait);
3231 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
3232 if (signal_pending(current))
3233 return -EINTR;
3234 goto again;
3235 }
3236 /* NO css_tryget() can success after here. */
3237 finish_wait(&cgroup_rmdir_waitq, &wait);
3238 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003239
Paul Menage81a6a5c2007-10-18 23:39:38 -07003240 spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07003241 set_bit(CGRP_REMOVED, &cgrp->flags);
3242 if (!list_empty(&cgrp->release_list))
3243 list_del(&cgrp->release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003244 spin_unlock(&release_list_lock);
Paul Menage999cd8a2009-01-07 18:08:36 -08003245
3246 cgroup_lock_hierarchy(cgrp->root);
3247 /* delete this cgroup from parent->children */
Paul Menagebd89aab2007-10-18 23:40:44 -07003248 list_del(&cgrp->sibling);
Paul Menage999cd8a2009-01-07 18:08:36 -08003249 cgroup_unlock_hierarchy(cgrp->root);
3250
Paul Menagebd89aab2007-10-18 23:40:44 -07003251 spin_lock(&cgrp->dentry->d_lock);
3252 d = dget(cgrp->dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003253 spin_unlock(&d->d_lock);
3254
3255 cgroup_d_remove_dir(d);
3256 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003257
Paul Menagebd89aab2007-10-18 23:40:44 -07003258 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003259 check_for_release(parent);
3260
Paul Menageddbcc7e2007-10-18 23:39:30 -07003261 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003262 return 0;
3263}
3264
Li Zefan06a11922008-04-29 01:00:07 -07003265static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003266{
Paul Menageddbcc7e2007-10-18 23:39:30 -07003267 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08003268
3269 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003270
3271 /* Create the top cgroup state for this subsystem */
Li Zefan33a68ac2009-01-07 18:07:42 -08003272 list_add(&ss->sibling, &rootnode.subsys_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003273 ss->root = &rootnode;
3274 css = ss->create(ss, dummytop);
3275 /* We don't handle early failures gracefully */
3276 BUG_ON(IS_ERR(css));
3277 init_cgroup_css(css, ss, dummytop);
3278
Li Zefane8d55fd2008-04-29 01:00:13 -07003279 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07003280 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07003281 * newly registered, all tasks and hence the
3282 * init_css_set is in the subsystem's top cgroup. */
3283 init_css_set.subsys[ss->subsys_id] = dummytop->subsys[ss->subsys_id];
Paul Menageddbcc7e2007-10-18 23:39:30 -07003284
3285 need_forkexit_callback |= ss->fork || ss->exit;
3286
Li Zefane8d55fd2008-04-29 01:00:13 -07003287 /* At system boot, before all subsystems have been
3288 * registered, no tasks have been forked, so we don't
3289 * need to invoke fork callbacks here. */
3290 BUG_ON(!list_empty(&init_task.tasks));
3291
Paul Menage999cd8a2009-01-07 18:08:36 -08003292 mutex_init(&ss->hierarchy_mutex);
Li Zefancfebe562009-02-11 13:04:36 -08003293 lockdep_set_class(&ss->hierarchy_mutex, &ss->subsys_key);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003294 ss->active = 1;
3295}
3296
3297/**
Li Zefana043e3b2008-02-23 15:24:09 -08003298 * cgroup_init_early - cgroup initialization at system boot
3299 *
3300 * Initialize cgroups at system boot, and initialize any
3301 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07003302 */
3303int __init cgroup_init_early(void)
3304{
3305 int i;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07003306 atomic_set(&init_css_set.refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -07003307 INIT_LIST_HEAD(&init_css_set.cg_links);
3308 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07003309 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07003310 css_set_count = 1;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003311 init_cgroup_root(&rootnode);
Paul Menage817929e2007-10-18 23:39:36 -07003312 root_count = 1;
3313 init_task.cgroups = &init_css_set;
3314
3315 init_css_set_link.cg = &init_css_set;
Paul Menage7717f7b2009-09-23 15:56:22 -07003316 init_css_set_link.cgrp = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -07003317 list_add(&init_css_set_link.cgrp_link_list,
Paul Menage817929e2007-10-18 23:39:36 -07003318 &rootnode.top_cgroup.css_sets);
3319 list_add(&init_css_set_link.cg_link_list,
3320 &init_css_set.cg_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003321
Li Zefan472b1052008-04-29 01:00:11 -07003322 for (i = 0; i < CSS_SET_TABLE_SIZE; i++)
3323 INIT_HLIST_HEAD(&css_set_table[i]);
3324
Ben Blumaae8aab2010-03-10 15:22:07 -08003325 /* at bootup time, we don't worry about modular subsystems */
3326 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07003327 struct cgroup_subsys *ss = subsys[i];
3328
3329 BUG_ON(!ss->name);
3330 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
3331 BUG_ON(!ss->create);
3332 BUG_ON(!ss->destroy);
3333 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08003334 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07003335 ss->name, ss->subsys_id);
3336 BUG();
3337 }
3338
3339 if (ss->early_init)
3340 cgroup_init_subsys(ss);
3341 }
3342 return 0;
3343}
3344
3345/**
Li Zefana043e3b2008-02-23 15:24:09 -08003346 * cgroup_init - cgroup initialization
3347 *
3348 * Register cgroup filesystem and /proc file, and initialize
3349 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07003350 */
3351int __init cgroup_init(void)
3352{
3353 int err;
3354 int i;
Li Zefan472b1052008-04-29 01:00:11 -07003355 struct hlist_head *hhead;
Paul Menagea4243162007-10-18 23:39:35 -07003356
3357 err = bdi_init(&cgroup_backing_dev_info);
3358 if (err)
3359 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003360
Ben Blumaae8aab2010-03-10 15:22:07 -08003361 /* at bootup time, we don't worry about modular subsystems */
3362 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07003363 struct cgroup_subsys *ss = subsys[i];
3364 if (!ss->early_init)
3365 cgroup_init_subsys(ss);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07003366 if (ss->use_id)
3367 cgroup_subsys_init_idr(ss);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003368 }
3369
Li Zefan472b1052008-04-29 01:00:11 -07003370 /* Add init_css_set to the hash table */
3371 hhead = css_set_hash(init_css_set.subsys);
3372 hlist_add_head(&init_css_set.hlist, hhead);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07003373 BUG_ON(!init_root_id(&rootnode));
Paul Menageddbcc7e2007-10-18 23:39:30 -07003374 err = register_filesystem(&cgroup_fs_type);
3375 if (err < 0)
3376 goto out;
3377
Li Zefan46ae2202008-04-29 01:00:08 -07003378 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07003379
Paul Menageddbcc7e2007-10-18 23:39:30 -07003380out:
Paul Menagea4243162007-10-18 23:39:35 -07003381 if (err)
3382 bdi_destroy(&cgroup_backing_dev_info);
3383
Paul Menageddbcc7e2007-10-18 23:39:30 -07003384 return err;
3385}
Paul Menageb4f48b62007-10-18 23:39:33 -07003386
Paul Menagea4243162007-10-18 23:39:35 -07003387/*
3388 * proc_cgroup_show()
3389 * - Print task's cgroup paths into seq_file, one line for each hierarchy
3390 * - Used for /proc/<pid>/cgroup.
3391 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
3392 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08003393 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07003394 * anyway. No need to check that tsk->cgroup != NULL, thanks to
3395 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
3396 * cgroup to top_cgroup.
3397 */
3398
3399/* TODO: Use a proper seq_file iterator */
3400static int proc_cgroup_show(struct seq_file *m, void *v)
3401{
3402 struct pid *pid;
3403 struct task_struct *tsk;
3404 char *buf;
3405 int retval;
3406 struct cgroupfs_root *root;
3407
3408 retval = -ENOMEM;
3409 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3410 if (!buf)
3411 goto out;
3412
3413 retval = -ESRCH;
3414 pid = m->private;
3415 tsk = get_pid_task(pid, PIDTYPE_PID);
3416 if (!tsk)
3417 goto out_free;
3418
3419 retval = 0;
3420
3421 mutex_lock(&cgroup_mutex);
3422
Li Zefane5f6a862009-01-07 18:07:41 -08003423 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07003424 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07003425 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07003426 int count = 0;
3427
Paul Menage2c6ab6d2009-09-23 15:56:23 -07003428 seq_printf(m, "%d:", root->hierarchy_id);
Paul Menagea4243162007-10-18 23:39:35 -07003429 for_each_subsys(root, ss)
3430 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07003431 if (strlen(root->name))
3432 seq_printf(m, "%sname=%s", count ? "," : "",
3433 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07003434 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07003435 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07003436 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07003437 if (retval < 0)
3438 goto out_unlock;
3439 seq_puts(m, buf);
3440 seq_putc(m, '\n');
3441 }
3442
3443out_unlock:
3444 mutex_unlock(&cgroup_mutex);
3445 put_task_struct(tsk);
3446out_free:
3447 kfree(buf);
3448out:
3449 return retval;
3450}
3451
3452static int cgroup_open(struct inode *inode, struct file *file)
3453{
3454 struct pid *pid = PROC_I(inode)->pid;
3455 return single_open(file, proc_cgroup_show, pid);
3456}
3457
Alexey Dobriyan828c0952009-10-01 15:43:56 -07003458const struct file_operations proc_cgroup_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07003459 .open = cgroup_open,
3460 .read = seq_read,
3461 .llseek = seq_lseek,
3462 .release = single_release,
3463};
3464
3465/* Display information about each subsystem and each hierarchy */
3466static int proc_cgroupstats_show(struct seq_file *m, void *v)
3467{
3468 int i;
Paul Menagea4243162007-10-18 23:39:35 -07003469
Paul Menage8bab8dd2008-04-04 14:29:57 -07003470 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08003471 /*
3472 * ideally we don't want subsystems moving around while we do this.
3473 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
3474 * subsys/hierarchy state.
3475 */
Paul Menagea4243162007-10-18 23:39:35 -07003476 mutex_lock(&cgroup_mutex);
Paul Menagea4243162007-10-18 23:39:35 -07003477 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3478 struct cgroup_subsys *ss = subsys[i];
Ben Blumaae8aab2010-03-10 15:22:07 -08003479 if (ss == NULL)
3480 continue;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07003481 seq_printf(m, "%s\t%d\t%d\t%d\n",
3482 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07003483 ss->root->number_of_cgroups, !ss->disabled);
Paul Menagea4243162007-10-18 23:39:35 -07003484 }
3485 mutex_unlock(&cgroup_mutex);
3486 return 0;
3487}
3488
3489static int cgroupstats_open(struct inode *inode, struct file *file)
3490{
Al Viro9dce07f2008-03-29 03:07:28 +00003491 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07003492}
3493
Alexey Dobriyan828c0952009-10-01 15:43:56 -07003494static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07003495 .open = cgroupstats_open,
3496 .read = seq_read,
3497 .llseek = seq_lseek,
3498 .release = single_release,
3499};
3500
Paul Menageb4f48b62007-10-18 23:39:33 -07003501/**
3502 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08003503 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07003504 *
3505 * Description: A task inherits its parent's cgroup at fork().
3506 *
3507 * A pointer to the shared css_set was automatically copied in
3508 * fork.c by dup_task_struct(). However, we ignore that copy, since
3509 * it was not made under the protection of RCU or cgroup_mutex, so
Cliff Wickman956db3c2008-02-07 00:14:43 -08003510 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
Paul Menage817929e2007-10-18 23:39:36 -07003511 * have already changed current->cgroups, allowing the previously
3512 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07003513 *
3514 * At the point that cgroup_fork() is called, 'current' is the parent
3515 * task, and the passed argument 'child' points to the child task.
3516 */
3517void cgroup_fork(struct task_struct *child)
3518{
Paul Menage817929e2007-10-18 23:39:36 -07003519 task_lock(current);
3520 child->cgroups = current->cgroups;
3521 get_css_set(child->cgroups);
3522 task_unlock(current);
3523 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07003524}
3525
3526/**
Li Zefana043e3b2008-02-23 15:24:09 -08003527 * cgroup_fork_callbacks - run fork callbacks
3528 * @child: the new task
3529 *
3530 * Called on a new task very soon before adding it to the
3531 * tasklist. No need to take any locks since no-one can
3532 * be operating on this task.
Paul Menageb4f48b62007-10-18 23:39:33 -07003533 */
3534void cgroup_fork_callbacks(struct task_struct *child)
3535{
3536 if (need_forkexit_callback) {
3537 int i;
Ben Blumaae8aab2010-03-10 15:22:07 -08003538 /*
3539 * forkexit callbacks are only supported for builtin
3540 * subsystems, and the builtin section of the subsys array is
3541 * immutable, so we don't need to lock the subsys array here.
3542 */
3543 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
Paul Menageb4f48b62007-10-18 23:39:33 -07003544 struct cgroup_subsys *ss = subsys[i];
3545 if (ss->fork)
3546 ss->fork(ss, child);
3547 }
3548 }
3549}
3550
3551/**
Li Zefana043e3b2008-02-23 15:24:09 -08003552 * cgroup_post_fork - called on a new task after adding it to the task list
3553 * @child: the task in question
3554 *
3555 * Adds the task to the list running through its css_set if necessary.
3556 * Has to be after the task is visible on the task list in case we race
3557 * with the first call to cgroup_iter_start() - to guarantee that the
3558 * new task ends up on its list.
3559 */
Paul Menage817929e2007-10-18 23:39:36 -07003560void cgroup_post_fork(struct task_struct *child)
3561{
3562 if (use_task_css_set_links) {
3563 write_lock(&css_set_lock);
Lai Jiangshanb12b5332009-01-07 18:07:36 -08003564 task_lock(child);
Paul Menage817929e2007-10-18 23:39:36 -07003565 if (list_empty(&child->cg_list))
3566 list_add(&child->cg_list, &child->cgroups->tasks);
Lai Jiangshanb12b5332009-01-07 18:07:36 -08003567 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07003568 write_unlock(&css_set_lock);
3569 }
3570}
3571/**
Paul Menageb4f48b62007-10-18 23:39:33 -07003572 * cgroup_exit - detach cgroup from exiting task
3573 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08003574 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07003575 *
3576 * Description: Detach cgroup from @tsk and release it.
3577 *
3578 * Note that cgroups marked notify_on_release force every task in
3579 * them to take the global cgroup_mutex mutex when exiting.
3580 * This could impact scaling on very large systems. Be reluctant to
3581 * use notify_on_release cgroups where very high task exit scaling
3582 * is required on large systems.
3583 *
3584 * the_top_cgroup_hack:
3585 *
3586 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
3587 *
3588 * We call cgroup_exit() while the task is still competent to
3589 * handle notify_on_release(), then leave the task attached to the
3590 * root cgroup in each hierarchy for the remainder of its exit.
3591 *
3592 * To do this properly, we would increment the reference count on
3593 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
3594 * code we would add a second cgroup function call, to drop that
3595 * reference. This would just create an unnecessary hot spot on
3596 * the top_cgroup reference count, to no avail.
3597 *
3598 * Normally, holding a reference to a cgroup without bumping its
3599 * count is unsafe. The cgroup could go away, or someone could
3600 * attach us to a different cgroup, decrementing the count on
3601 * the first cgroup that we never incremented. But in this case,
3602 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08003603 * which wards off any cgroup_attach_task() attempts, or task is a failed
3604 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07003605 */
3606void cgroup_exit(struct task_struct *tsk, int run_callbacks)
3607{
3608 int i;
Paul Menage817929e2007-10-18 23:39:36 -07003609 struct css_set *cg;
Paul Menageb4f48b62007-10-18 23:39:33 -07003610
3611 if (run_callbacks && need_forkexit_callback) {
Ben Blumaae8aab2010-03-10 15:22:07 -08003612 /*
3613 * modular subsystems can't use callbacks, so no need to lock
3614 * the subsys array
3615 */
3616 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
Paul Menageb4f48b62007-10-18 23:39:33 -07003617 struct cgroup_subsys *ss = subsys[i];
3618 if (ss->exit)
3619 ss->exit(ss, tsk);
3620 }
3621 }
Paul Menage817929e2007-10-18 23:39:36 -07003622
3623 /*
3624 * Unlink from the css_set task list if necessary.
3625 * Optimistically check cg_list before taking
3626 * css_set_lock
3627 */
3628 if (!list_empty(&tsk->cg_list)) {
3629 write_lock(&css_set_lock);
3630 if (!list_empty(&tsk->cg_list))
3631 list_del(&tsk->cg_list);
3632 write_unlock(&css_set_lock);
3633 }
3634
Paul Menageb4f48b62007-10-18 23:39:33 -07003635 /* Reassign the task to the init_css_set. */
3636 task_lock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07003637 cg = tsk->cgroups;
3638 tsk->cgroups = &init_css_set;
Paul Menageb4f48b62007-10-18 23:39:33 -07003639 task_unlock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07003640 if (cg)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003641 put_css_set_taskexit(cg);
Paul Menageb4f48b62007-10-18 23:39:33 -07003642}
Paul Menage697f4162007-10-18 23:39:34 -07003643
3644/**
Li Zefana043e3b2008-02-23 15:24:09 -08003645 * cgroup_clone - clone the cgroup the given subsystem is attached to
3646 * @tsk: the task to be moved
3647 * @subsys: the given subsystem
Serge E. Hallyne885dcd2008-07-25 01:47:06 -07003648 * @nodename: the name for the new cgroup
Li Zefana043e3b2008-02-23 15:24:09 -08003649 *
3650 * Duplicate the current cgroup in the hierarchy that the given
3651 * subsystem is attached to, and move this task into the new
3652 * child.
Paul Menage697f4162007-10-18 23:39:34 -07003653 */
Serge E. Hallyne885dcd2008-07-25 01:47:06 -07003654int cgroup_clone(struct task_struct *tsk, struct cgroup_subsys *subsys,
3655 char *nodename)
Paul Menage697f4162007-10-18 23:39:34 -07003656{
3657 struct dentry *dentry;
3658 int ret = 0;
Paul Menage697f4162007-10-18 23:39:34 -07003659 struct cgroup *parent, *child;
3660 struct inode *inode;
3661 struct css_set *cg;
3662 struct cgroupfs_root *root;
3663 struct cgroup_subsys *ss;
3664
3665 /* We shouldn't be called by an unregistered subsystem */
3666 BUG_ON(!subsys->active);
3667
3668 /* First figure out what hierarchy and cgroup we're dealing
3669 * with, and pin them so we can drop cgroup_mutex */
3670 mutex_lock(&cgroup_mutex);
3671 again:
3672 root = subsys->root;
3673 if (root == &rootnode) {
Paul Menage697f4162007-10-18 23:39:34 -07003674 mutex_unlock(&cgroup_mutex);
3675 return 0;
3676 }
Paul Menage697f4162007-10-18 23:39:34 -07003677
Paul Menage697f4162007-10-18 23:39:34 -07003678 /* Pin the hierarchy */
Li Zefan1404f062009-01-29 14:25:21 -08003679 if (!atomic_inc_not_zero(&root->sb->s_active)) {
Li Zefan7b574b72009-01-04 12:00:45 -08003680 /* We race with the final deactivate_super() */
3681 mutex_unlock(&cgroup_mutex);
3682 return 0;
3683 }
Paul Menage697f4162007-10-18 23:39:34 -07003684
Paul Menage817929e2007-10-18 23:39:36 -07003685 /* Keep the cgroup alive */
Li Zefan1404f062009-01-29 14:25:21 -08003686 task_lock(tsk);
3687 parent = task_cgroup(tsk, subsys->subsys_id);
3688 cg = tsk->cgroups;
Paul Menage817929e2007-10-18 23:39:36 -07003689 get_css_set(cg);
Lai Jiangshan104cbd52009-01-07 18:07:38 -08003690 task_unlock(tsk);
Li Zefan1404f062009-01-29 14:25:21 -08003691
Paul Menage697f4162007-10-18 23:39:34 -07003692 mutex_unlock(&cgroup_mutex);
3693
3694 /* Now do the VFS work to create a cgroup */
3695 inode = parent->dentry->d_inode;
3696
3697 /* Hold the parent directory mutex across this operation to
3698 * stop anyone else deleting the new cgroup */
3699 mutex_lock(&inode->i_mutex);
3700 dentry = lookup_one_len(nodename, parent->dentry, strlen(nodename));
3701 if (IS_ERR(dentry)) {
3702 printk(KERN_INFO
Diego Callejacfe36bd2007-11-14 16:58:54 -08003703 "cgroup: Couldn't allocate dentry for %s: %ld\n", nodename,
Paul Menage697f4162007-10-18 23:39:34 -07003704 PTR_ERR(dentry));
3705 ret = PTR_ERR(dentry);
3706 goto out_release;
3707 }
3708
3709 /* Create the cgroup directory, which also creates the cgroup */
Li Zefan75139b82009-01-07 18:07:33 -08003710 ret = vfs_mkdir(inode, dentry, 0755);
Paul Menagebd89aab2007-10-18 23:40:44 -07003711 child = __d_cgrp(dentry);
Paul Menage697f4162007-10-18 23:39:34 -07003712 dput(dentry);
3713 if (ret) {
3714 printk(KERN_INFO
3715 "Failed to create cgroup %s: %d\n", nodename,
3716 ret);
3717 goto out_release;
3718 }
3719
Paul Menage697f4162007-10-18 23:39:34 -07003720 /* The cgroup now exists. Retake cgroup_mutex and check
3721 * that we're still in the same state that we thought we
3722 * were. */
3723 mutex_lock(&cgroup_mutex);
3724 if ((root != subsys->root) ||
3725 (parent != task_cgroup(tsk, subsys->subsys_id))) {
3726 /* Aargh, we raced ... */
3727 mutex_unlock(&inode->i_mutex);
Paul Menage817929e2007-10-18 23:39:36 -07003728 put_css_set(cg);
Paul Menage697f4162007-10-18 23:39:34 -07003729
Li Zefan1404f062009-01-29 14:25:21 -08003730 deactivate_super(root->sb);
Paul Menage697f4162007-10-18 23:39:34 -07003731 /* The cgroup is still accessible in the VFS, but
3732 * we're not going to try to rmdir() it at this
3733 * point. */
3734 printk(KERN_INFO
3735 "Race in cgroup_clone() - leaking cgroup %s\n",
3736 nodename);
3737 goto again;
3738 }
3739
3740 /* do any required auto-setup */
3741 for_each_subsys(root, ss) {
3742 if (ss->post_clone)
3743 ss->post_clone(ss, child);
3744 }
3745
3746 /* All seems fine. Finish by moving the task into the new cgroup */
Cliff Wickman956db3c2008-02-07 00:14:43 -08003747 ret = cgroup_attach_task(child, tsk);
Paul Menage697f4162007-10-18 23:39:34 -07003748 mutex_unlock(&cgroup_mutex);
3749
3750 out_release:
3751 mutex_unlock(&inode->i_mutex);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003752
3753 mutex_lock(&cgroup_mutex);
Paul Menage817929e2007-10-18 23:39:36 -07003754 put_css_set(cg);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003755 mutex_unlock(&cgroup_mutex);
Li Zefan1404f062009-01-29 14:25:21 -08003756 deactivate_super(root->sb);
Paul Menage697f4162007-10-18 23:39:34 -07003757 return ret;
3758}
3759
Li Zefana043e3b2008-02-23 15:24:09 -08003760/**
Grzegorz Nosek313e9242009-04-02 16:57:23 -07003761 * cgroup_is_descendant - see if @cgrp is a descendant of @task's cgrp
Li Zefana043e3b2008-02-23 15:24:09 -08003762 * @cgrp: the cgroup in question
Grzegorz Nosek313e9242009-04-02 16:57:23 -07003763 * @task: the task in question
Li Zefana043e3b2008-02-23 15:24:09 -08003764 *
Grzegorz Nosek313e9242009-04-02 16:57:23 -07003765 * See if @cgrp is a descendant of @task's cgroup in the appropriate
3766 * hierarchy.
Paul Menage697f4162007-10-18 23:39:34 -07003767 *
3768 * If we are sending in dummytop, then presumably we are creating
3769 * the top cgroup in the subsystem.
3770 *
3771 * Called only by the ns (nsproxy) cgroup.
3772 */
Grzegorz Nosek313e9242009-04-02 16:57:23 -07003773int cgroup_is_descendant(const struct cgroup *cgrp, struct task_struct *task)
Paul Menage697f4162007-10-18 23:39:34 -07003774{
3775 int ret;
3776 struct cgroup *target;
Paul Menage697f4162007-10-18 23:39:34 -07003777
Paul Menagebd89aab2007-10-18 23:40:44 -07003778 if (cgrp == dummytop)
Paul Menage697f4162007-10-18 23:39:34 -07003779 return 1;
3780
Paul Menage7717f7b2009-09-23 15:56:22 -07003781 target = task_cgroup_from_root(task, cgrp->root);
Paul Menagebd89aab2007-10-18 23:40:44 -07003782 while (cgrp != target && cgrp!= cgrp->top_cgroup)
3783 cgrp = cgrp->parent;
3784 ret = (cgrp == target);
Paul Menage697f4162007-10-18 23:39:34 -07003785 return ret;
3786}
Paul Menage81a6a5c2007-10-18 23:39:38 -07003787
Paul Menagebd89aab2007-10-18 23:40:44 -07003788static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003789{
3790 /* All of these checks rely on RCU to keep the cgroup
3791 * structure alive */
Paul Menagebd89aab2007-10-18 23:40:44 -07003792 if (cgroup_is_releasable(cgrp) && !atomic_read(&cgrp->count)
3793 && list_empty(&cgrp->children) && !cgroup_has_css_refs(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07003794 /* Control Group is currently removeable. If it's not
3795 * already queued for a userspace notification, queue
3796 * it now */
3797 int need_schedule_work = 0;
3798 spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07003799 if (!cgroup_is_removed(cgrp) &&
3800 list_empty(&cgrp->release_list)) {
3801 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003802 need_schedule_work = 1;
3803 }
3804 spin_unlock(&release_list_lock);
3805 if (need_schedule_work)
3806 schedule_work(&release_agent_work);
3807 }
3808}
3809
Daisuke Nishimurad7b9fff2010-03-10 15:22:05 -08003810/* Caller must verify that the css is not for root cgroup */
3811void __css_put(struct cgroup_subsys_state *css, int count)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003812{
Paul Menagebd89aab2007-10-18 23:40:44 -07003813 struct cgroup *cgrp = css->cgroup;
KAMEZAWA Hiroyuki3dece832009-10-01 15:44:09 -07003814 int val;
Paul Menage81a6a5c2007-10-18 23:39:38 -07003815 rcu_read_lock();
Daisuke Nishimurad7b9fff2010-03-10 15:22:05 -08003816 val = atomic_sub_return(count, &css->refcnt);
KAMEZAWA Hiroyuki3dece832009-10-01 15:44:09 -07003817 if (val == 1) {
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003818 if (notify_on_release(cgrp)) {
3819 set_bit(CGRP_RELEASABLE, &cgrp->flags);
3820 check_for_release(cgrp);
3821 }
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003822 cgroup_wakeup_rmdir_waiter(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003823 }
3824 rcu_read_unlock();
KAMEZAWA Hiroyuki3dece832009-10-01 15:44:09 -07003825 WARN_ON_ONCE(val < 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003826}
3827
3828/*
3829 * Notify userspace when a cgroup is released, by running the
3830 * configured release agent with the name of the cgroup (path
3831 * relative to the root of cgroup file system) as the argument.
3832 *
3833 * Most likely, this user command will try to rmdir this cgroup.
3834 *
3835 * This races with the possibility that some other task will be
3836 * attached to this cgroup before it is removed, or that some other
3837 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
3838 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
3839 * unused, and this cgroup will be reprieved from its death sentence,
3840 * to continue to serve a useful existence. Next time it's released,
3841 * we will get notified again, if it still has 'notify_on_release' set.
3842 *
3843 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
3844 * means only wait until the task is successfully execve()'d. The
3845 * separate release agent task is forked by call_usermodehelper(),
3846 * then control in this thread returns here, without waiting for the
3847 * release agent task. We don't bother to wait because the caller of
3848 * this routine has no use for the exit status of the release agent
3849 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07003850 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07003851static void cgroup_release_agent(struct work_struct *work)
3852{
3853 BUG_ON(work != &release_agent_work);
3854 mutex_lock(&cgroup_mutex);
3855 spin_lock(&release_list_lock);
3856 while (!list_empty(&release_list)) {
3857 char *argv[3], *envp[3];
3858 int i;
Paul Menagee788e062008-07-25 01:46:59 -07003859 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003860 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003861 struct cgroup,
3862 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07003863 list_del_init(&cgrp->release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003864 spin_unlock(&release_list_lock);
3865 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07003866 if (!pathbuf)
3867 goto continue_free;
3868 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
3869 goto continue_free;
3870 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
3871 if (!agentbuf)
3872 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07003873
3874 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07003875 argv[i++] = agentbuf;
3876 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07003877 argv[i] = NULL;
3878
3879 i = 0;
3880 /* minimal command environment */
3881 envp[i++] = "HOME=/";
3882 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
3883 envp[i] = NULL;
3884
3885 /* Drop the lock while we invoke the usermode helper,
3886 * since the exec could involve hitting disk and hence
3887 * be a slow process */
3888 mutex_unlock(&cgroup_mutex);
3889 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003890 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07003891 continue_free:
3892 kfree(pathbuf);
3893 kfree(agentbuf);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003894 spin_lock(&release_list_lock);
3895 }
3896 spin_unlock(&release_list_lock);
3897 mutex_unlock(&cgroup_mutex);
3898}
Paul Menage8bab8dd2008-04-04 14:29:57 -07003899
3900static int __init cgroup_disable(char *str)
3901{
3902 int i;
3903 char *token;
3904
3905 while ((token = strsep(&str, ",")) != NULL) {
3906 if (!*token)
3907 continue;
Ben Blumaae8aab2010-03-10 15:22:07 -08003908 /*
3909 * cgroup_disable, being at boot time, can't know about module
3910 * subsystems, so we don't worry about them.
3911 */
3912 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07003913 struct cgroup_subsys *ss = subsys[i];
3914
3915 if (!strcmp(token, ss->name)) {
3916 ss->disabled = 1;
3917 printk(KERN_INFO "Disabling %s control group"
3918 " subsystem\n", ss->name);
3919 break;
3920 }
3921 }
3922 }
3923 return 1;
3924}
3925__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07003926
3927/*
3928 * Functons for CSS ID.
3929 */
3930
3931/*
3932 *To get ID other than 0, this should be called when !cgroup_is_removed().
3933 */
3934unsigned short css_id(struct cgroup_subsys_state *css)
3935{
3936 struct css_id *cssid = rcu_dereference(css->id);
3937
3938 if (cssid)
3939 return cssid->id;
3940 return 0;
3941}
3942
3943unsigned short css_depth(struct cgroup_subsys_state *css)
3944{
3945 struct css_id *cssid = rcu_dereference(css->id);
3946
3947 if (cssid)
3948 return cssid->depth;
3949 return 0;
3950}
3951
3952bool css_is_ancestor(struct cgroup_subsys_state *child,
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07003953 const struct cgroup_subsys_state *root)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07003954{
3955 struct css_id *child_id = rcu_dereference(child->id);
3956 struct css_id *root_id = rcu_dereference(root->id);
3957
3958 if (!child_id || !root_id || (child_id->depth < root_id->depth))
3959 return false;
3960 return child_id->stack[root_id->depth] == root_id->id;
3961}
3962
3963static void __free_css_id_cb(struct rcu_head *head)
3964{
3965 struct css_id *id;
3966
3967 id = container_of(head, struct css_id, rcu_head);
3968 kfree(id);
3969}
3970
3971void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
3972{
3973 struct css_id *id = css->id;
3974 /* When this is called before css_id initialization, id can be NULL */
3975 if (!id)
3976 return;
3977
3978 BUG_ON(!ss->use_id);
3979
3980 rcu_assign_pointer(id->css, NULL);
3981 rcu_assign_pointer(css->id, NULL);
3982 spin_lock(&ss->id_lock);
3983 idr_remove(&ss->idr, id->id);
3984 spin_unlock(&ss->id_lock);
3985 call_rcu(&id->rcu_head, __free_css_id_cb);
3986}
3987
3988/*
3989 * This is called by init or create(). Then, calls to this function are
3990 * always serialized (By cgroup_mutex() at create()).
3991 */
3992
3993static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
3994{
3995 struct css_id *newid;
3996 int myid, error, size;
3997
3998 BUG_ON(!ss->use_id);
3999
4000 size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
4001 newid = kzalloc(size, GFP_KERNEL);
4002 if (!newid)
4003 return ERR_PTR(-ENOMEM);
4004 /* get id */
4005 if (unlikely(!idr_pre_get(&ss->idr, GFP_KERNEL))) {
4006 error = -ENOMEM;
4007 goto err_out;
4008 }
4009 spin_lock(&ss->id_lock);
4010 /* Don't use 0. allocates an ID of 1-65535 */
4011 error = idr_get_new_above(&ss->idr, newid, 1, &myid);
4012 spin_unlock(&ss->id_lock);
4013
4014 /* Returns error when there are no free spaces for new ID.*/
4015 if (error) {
4016 error = -ENOSPC;
4017 goto err_out;
4018 }
4019 if (myid > CSS_ID_MAX)
4020 goto remove_idr;
4021
4022 newid->id = myid;
4023 newid->depth = depth;
4024 return newid;
4025remove_idr:
4026 error = -ENOSPC;
4027 spin_lock(&ss->id_lock);
4028 idr_remove(&ss->idr, myid);
4029 spin_unlock(&ss->id_lock);
4030err_out:
4031 kfree(newid);
4032 return ERR_PTR(error);
4033
4034}
4035
4036static int __init cgroup_subsys_init_idr(struct cgroup_subsys *ss)
4037{
4038 struct css_id *newid;
4039 struct cgroup_subsys_state *rootcss;
4040
4041 spin_lock_init(&ss->id_lock);
4042 idr_init(&ss->idr);
4043
4044 rootcss = init_css_set.subsys[ss->subsys_id];
4045 newid = get_new_cssid(ss, 0);
4046 if (IS_ERR(newid))
4047 return PTR_ERR(newid);
4048
4049 newid->stack[0] = newid->id;
4050 newid->css = rootcss;
4051 rootcss->id = newid;
4052 return 0;
4053}
4054
4055static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent,
4056 struct cgroup *child)
4057{
4058 int subsys_id, i, depth = 0;
4059 struct cgroup_subsys_state *parent_css, *child_css;
4060 struct css_id *child_id, *parent_id = NULL;
4061
4062 subsys_id = ss->subsys_id;
4063 parent_css = parent->subsys[subsys_id];
4064 child_css = child->subsys[subsys_id];
4065 depth = css_depth(parent_css) + 1;
4066 parent_id = parent_css->id;
4067
4068 child_id = get_new_cssid(ss, depth);
4069 if (IS_ERR(child_id))
4070 return PTR_ERR(child_id);
4071
4072 for (i = 0; i < depth; i++)
4073 child_id->stack[i] = parent_id->stack[i];
4074 child_id->stack[depth] = child_id->id;
4075 /*
4076 * child_id->css pointer will be set after this cgroup is available
4077 * see cgroup_populate_dir()
4078 */
4079 rcu_assign_pointer(child_css->id, child_id);
4080
4081 return 0;
4082}
4083
4084/**
4085 * css_lookup - lookup css by id
4086 * @ss: cgroup subsys to be looked into.
4087 * @id: the id
4088 *
4089 * Returns pointer to cgroup_subsys_state if there is valid one with id.
4090 * NULL if not. Should be called under rcu_read_lock()
4091 */
4092struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
4093{
4094 struct css_id *cssid = NULL;
4095
4096 BUG_ON(!ss->use_id);
4097 cssid = idr_find(&ss->idr, id);
4098
4099 if (unlikely(!cssid))
4100 return NULL;
4101
4102 return rcu_dereference(cssid->css);
4103}
4104
4105/**
4106 * css_get_next - lookup next cgroup under specified hierarchy.
4107 * @ss: pointer to subsystem
4108 * @id: current position of iteration.
4109 * @root: pointer to css. search tree under this.
4110 * @foundid: position of found object.
4111 *
4112 * Search next css under the specified hierarchy of rootid. Calling under
4113 * rcu_read_lock() is necessary. Returns NULL if it reaches the end.
4114 */
4115struct cgroup_subsys_state *
4116css_get_next(struct cgroup_subsys *ss, int id,
4117 struct cgroup_subsys_state *root, int *foundid)
4118{
4119 struct cgroup_subsys_state *ret = NULL;
4120 struct css_id *tmp;
4121 int tmpid;
4122 int rootid = css_id(root);
4123 int depth = css_depth(root);
4124
4125 if (!rootid)
4126 return NULL;
4127
4128 BUG_ON(!ss->use_id);
4129 /* fill start point for scan */
4130 tmpid = id;
4131 while (1) {
4132 /*
4133 * scan next entry from bitmap(tree), tmpid is updated after
4134 * idr_get_next().
4135 */
4136 spin_lock(&ss->id_lock);
4137 tmp = idr_get_next(&ss->idr, &tmpid);
4138 spin_unlock(&ss->id_lock);
4139
4140 if (!tmp)
4141 break;
4142 if (tmp->depth >= depth && tmp->stack[depth] == rootid) {
4143 ret = rcu_dereference(tmp->css);
4144 if (ret) {
4145 *foundid = tmpid;
4146 break;
4147 }
4148 }
4149 /* continue to scan from next id */
4150 tmpid = tmpid + 1;
4151 }
4152 return ret;
4153}
4154
Paul Menagefe693432009-09-23 15:56:20 -07004155#ifdef CONFIG_CGROUP_DEBUG
4156static struct cgroup_subsys_state *debug_create(struct cgroup_subsys *ss,
4157 struct cgroup *cont)
4158{
4159 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
4160
4161 if (!css)
4162 return ERR_PTR(-ENOMEM);
4163
4164 return css;
4165}
4166
4167static void debug_destroy(struct cgroup_subsys *ss, struct cgroup *cont)
4168{
4169 kfree(cont->subsys[debug_subsys_id]);
4170}
4171
4172static u64 cgroup_refcount_read(struct cgroup *cont, struct cftype *cft)
4173{
4174 return atomic_read(&cont->count);
4175}
4176
4177static u64 debug_taskcount_read(struct cgroup *cont, struct cftype *cft)
4178{
4179 return cgroup_task_count(cont);
4180}
4181
4182static u64 current_css_set_read(struct cgroup *cont, struct cftype *cft)
4183{
4184 return (u64)(unsigned long)current->cgroups;
4185}
4186
4187static u64 current_css_set_refcount_read(struct cgroup *cont,
4188 struct cftype *cft)
4189{
4190 u64 count;
4191
4192 rcu_read_lock();
4193 count = atomic_read(&current->cgroups->refcount);
4194 rcu_read_unlock();
4195 return count;
4196}
4197
Paul Menage7717f7b2009-09-23 15:56:22 -07004198static int current_css_set_cg_links_read(struct cgroup *cont,
4199 struct cftype *cft,
4200 struct seq_file *seq)
4201{
4202 struct cg_cgroup_link *link;
4203 struct css_set *cg;
4204
4205 read_lock(&css_set_lock);
4206 rcu_read_lock();
4207 cg = rcu_dereference(current->cgroups);
4208 list_for_each_entry(link, &cg->cg_links, cg_link_list) {
4209 struct cgroup *c = link->cgrp;
4210 const char *name;
4211
4212 if (c->dentry)
4213 name = c->dentry->d_name.name;
4214 else
4215 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004216 seq_printf(seq, "Root %d group %s\n",
4217 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07004218 }
4219 rcu_read_unlock();
4220 read_unlock(&css_set_lock);
4221 return 0;
4222}
4223
4224#define MAX_TASKS_SHOWN_PER_CSS 25
4225static int cgroup_css_links_read(struct cgroup *cont,
4226 struct cftype *cft,
4227 struct seq_file *seq)
4228{
4229 struct cg_cgroup_link *link;
4230
4231 read_lock(&css_set_lock);
4232 list_for_each_entry(link, &cont->css_sets, cgrp_link_list) {
4233 struct css_set *cg = link->cg;
4234 struct task_struct *task;
4235 int count = 0;
4236 seq_printf(seq, "css_set %p\n", cg);
4237 list_for_each_entry(task, &cg->tasks, cg_list) {
4238 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
4239 seq_puts(seq, " ...\n");
4240 break;
4241 } else {
4242 seq_printf(seq, " task %d\n",
4243 task_pid_vnr(task));
4244 }
4245 }
4246 }
4247 read_unlock(&css_set_lock);
4248 return 0;
4249}
4250
Paul Menagefe693432009-09-23 15:56:20 -07004251static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft)
4252{
4253 return test_bit(CGRP_RELEASABLE, &cgrp->flags);
4254}
4255
4256static struct cftype debug_files[] = {
4257 {
4258 .name = "cgroup_refcount",
4259 .read_u64 = cgroup_refcount_read,
4260 },
4261 {
4262 .name = "taskcount",
4263 .read_u64 = debug_taskcount_read,
4264 },
4265
4266 {
4267 .name = "current_css_set",
4268 .read_u64 = current_css_set_read,
4269 },
4270
4271 {
4272 .name = "current_css_set_refcount",
4273 .read_u64 = current_css_set_refcount_read,
4274 },
4275
4276 {
Paul Menage7717f7b2009-09-23 15:56:22 -07004277 .name = "current_css_set_cg_links",
4278 .read_seq_string = current_css_set_cg_links_read,
4279 },
4280
4281 {
4282 .name = "cgroup_css_links",
4283 .read_seq_string = cgroup_css_links_read,
4284 },
4285
4286 {
Paul Menagefe693432009-09-23 15:56:20 -07004287 .name = "releasable",
4288 .read_u64 = releasable_read,
4289 },
4290};
4291
4292static int debug_populate(struct cgroup_subsys *ss, struct cgroup *cont)
4293{
4294 return cgroup_add_files(cont, ss, debug_files,
4295 ARRAY_SIZE(debug_files));
4296}
4297
4298struct cgroup_subsys debug_subsys = {
4299 .name = "debug",
4300 .create = debug_create,
4301 .destroy = debug_destroy,
4302 .populate = debug_populate,
4303 .subsys_id = debug_subsys_id,
4304};
4305#endif /* CONFIG_CGROUP_DEBUG */