blob: b1a0f5a528fe6563badc393f96db9e4cfa2d9255 [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>
Paul Menagec6d57f32009-09-23 15:56:19 -070026#include <linux/ctype.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070027#include <linux/errno.h>
28#include <linux/fs.h>
29#include <linux/kernel.h>
30#include <linux/list.h>
31#include <linux/mm.h>
32#include <linux/mutex.h>
33#include <linux/mount.h>
34#include <linux/pagemap.h>
Paul Menagea4243162007-10-18 23:39:35 -070035#include <linux/proc_fs.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070036#include <linux/rcupdate.h>
37#include <linux/sched.h>
Paul Menage817929e2007-10-18 23:39:36 -070038#include <linux/backing-dev.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070039#include <linux/seq_file.h>
40#include <linux/slab.h>
41#include <linux/magic.h>
42#include <linux/spinlock.h>
43#include <linux/string.h>
Paul Menagebbcb81d2007-10-18 23:39:32 -070044#include <linux/sort.h>
Paul Menage81a6a5c2007-10-18 23:39:38 -070045#include <linux/kmod.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070046#include <linux/delayacct.h>
47#include <linux/cgroupstats.h>
Li Zefan472b1052008-04-29 01:00:11 -070048#include <linux/hash.h>
Al Viro3f8206d2008-07-26 03:46:43 -040049#include <linux/namei.h>
Alessio Igor Bogani337eb002009-05-12 15:10:54 +020050#include <linux/smp_lock.h>
Li Zefan096b7fe2009-07-29 15:04:04 -070051#include <linux/pid_namespace.h>
Paul Menage2c6ab6d2009-09-23 15:56:23 -070052#include <linux/idr.h>
Ben Blumd1d9fd32009-09-23 15:56:28 -070053#include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
Balbir Singh846c7bb2007-10-18 23:39:44 -070054
Paul Menageddbcc7e2007-10-18 23:39:30 -070055#include <asm/atomic.h>
56
Paul Menage81a6a5c2007-10-18 23:39:38 -070057static DEFINE_MUTEX(cgroup_mutex);
58
Paul Menageddbcc7e2007-10-18 23:39:30 -070059/* Generate an array of cgroup subsystem pointers */
60#define SUBSYS(_x) &_x ## _subsys,
61
62static struct cgroup_subsys *subsys[] = {
63#include <linux/cgroup_subsys.h>
64};
65
Paul Menagec6d57f32009-09-23 15:56:19 -070066#define MAX_CGROUP_ROOT_NAMELEN 64
67
Paul Menageddbcc7e2007-10-18 23:39:30 -070068/*
69 * A cgroupfs_root represents the root of a cgroup hierarchy,
70 * and may be associated with a superblock to form an active
71 * hierarchy
72 */
73struct cgroupfs_root {
74 struct super_block *sb;
75
76 /*
77 * The bitmask of subsystems intended to be attached to this
78 * hierarchy
79 */
80 unsigned long subsys_bits;
81
Paul Menage2c6ab6d2009-09-23 15:56:23 -070082 /* Unique id for this hierarchy. */
83 int hierarchy_id;
84
Paul Menageddbcc7e2007-10-18 23:39:30 -070085 /* The bitmask of subsystems currently attached to this hierarchy */
86 unsigned long actual_subsys_bits;
87
88 /* A list running through the attached subsystems */
89 struct list_head subsys_list;
90
91 /* The root cgroup for this hierarchy */
92 struct cgroup top_cgroup;
93
94 /* Tracks how many cgroups are currently defined in hierarchy.*/
95 int number_of_cgroups;
96
Li Zefane5f6a862009-01-07 18:07:41 -080097 /* A list running through the active hierarchies */
Paul Menageddbcc7e2007-10-18 23:39:30 -070098 struct list_head root_list;
99
100 /* Hierarchy-specific flags */
101 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700102
Paul Menagee788e0662008-07-25 01:46:59 -0700103 /* The path to use for release notifications. */
Paul Menage81a6a5c2007-10-18 23:39:38 -0700104 char release_agent_path[PATH_MAX];
Paul Menagec6d57f32009-09-23 15:56:19 -0700105
106 /* The name for this hierarchy - may be empty */
107 char name[MAX_CGROUP_ROOT_NAMELEN];
Paul Menageddbcc7e2007-10-18 23:39:30 -0700108};
109
Paul Menageddbcc7e2007-10-18 23:39:30 -0700110/*
111 * The "rootnode" hierarchy is the "dummy hierarchy", reserved for the
112 * subsystems that are otherwise unattached - it never has more than a
113 * single cgroup, and all tasks are part of that cgroup.
114 */
115static struct cgroupfs_root rootnode;
116
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700117/*
118 * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when
119 * cgroup_subsys->use_id != 0.
120 */
121#define CSS_ID_MAX (65535)
122struct css_id {
123 /*
124 * The css to which this ID points. This pointer is set to valid value
125 * after cgroup is populated. If cgroup is removed, this will be NULL.
126 * This pointer is expected to be RCU-safe because destroy()
127 * is called after synchronize_rcu(). But for safe use, css_is_removed()
128 * css_tryget() should be used for avoiding race.
129 */
130 struct cgroup_subsys_state *css;
131 /*
132 * ID of this css.
133 */
134 unsigned short id;
135 /*
136 * Depth in hierarchy which this ID belongs to.
137 */
138 unsigned short depth;
139 /*
140 * ID is freed by RCU. (and lookup routine is RCU safe.)
141 */
142 struct rcu_head rcu_head;
143 /*
144 * Hierarchy of CSS ID belongs to.
145 */
146 unsigned short stack[0]; /* Array of Length (depth+1) */
147};
148
149
Paul Menageddbcc7e2007-10-18 23:39:30 -0700150/* The list of hierarchy roots */
151
152static LIST_HEAD(roots);
Paul Menage817929e2007-10-18 23:39:36 -0700153static int root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700154
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700155static DEFINE_IDA(hierarchy_ida);
156static int next_hierarchy_id;
157static DEFINE_SPINLOCK(hierarchy_id_lock);
158
Paul Menageddbcc7e2007-10-18 23:39:30 -0700159/* dummytop is a shorthand for the dummy hierarchy's top cgroup */
160#define dummytop (&rootnode.top_cgroup)
161
162/* This flag indicates whether tasks in the fork and exit paths should
Li Zefana043e3b2008-02-23 15:24:09 -0800163 * check for fork/exit handlers to call. This avoids us having to do
164 * extra work in the fork/exit path if none of the subsystems need to
165 * be called.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700166 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700167static int need_forkexit_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700168
Paul E. McKenneyd11c5632010-02-22 17:04:50 -0800169#ifdef CONFIG_PROVE_LOCKING
170int cgroup_lock_is_held(void)
171{
172 return lockdep_is_held(&cgroup_mutex);
173}
174#else /* #ifdef CONFIG_PROVE_LOCKING */
175int cgroup_lock_is_held(void)
176{
177 return mutex_is_locked(&cgroup_mutex);
178}
179#endif /* #else #ifdef CONFIG_PROVE_LOCKING */
180
181EXPORT_SYMBOL_GPL(cgroup_lock_is_held);
182
Paul Menageddbcc7e2007-10-18 23:39:30 -0700183/* convenient tests for these bits */
Paul Menagebd89aab2007-10-18 23:40:44 -0700184inline int cgroup_is_removed(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700185{
Paul Menagebd89aab2007-10-18 23:40:44 -0700186 return test_bit(CGRP_REMOVED, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700187}
188
189/* bits in struct cgroupfs_root flags field */
190enum {
191 ROOT_NOPREFIX, /* mounted subsystems have no named prefix */
192};
193
Adrian Bunke9685a02008-02-07 00:13:46 -0800194static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700195{
196 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700197 (1 << CGRP_RELEASABLE) |
198 (1 << CGRP_NOTIFY_ON_RELEASE);
199 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700200}
201
Adrian Bunke9685a02008-02-07 00:13:46 -0800202static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700203{
Paul Menagebd89aab2007-10-18 23:40:44 -0700204 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700205}
206
Paul Menageddbcc7e2007-10-18 23:39:30 -0700207/*
208 * for_each_subsys() allows you to iterate on each subsystem attached to
209 * an active hierarchy
210 */
211#define for_each_subsys(_root, _ss) \
212list_for_each_entry(_ss, &_root->subsys_list, sibling)
213
Li Zefane5f6a862009-01-07 18:07:41 -0800214/* for_each_active_root() allows you to iterate across the active hierarchies */
215#define for_each_active_root(_root) \
Paul Menageddbcc7e2007-10-18 23:39:30 -0700216list_for_each_entry(_root, &roots, root_list)
217
Paul Menage81a6a5c2007-10-18 23:39:38 -0700218/* the list of cgroups eligible for automatic release. Protected by
219 * release_list_lock */
220static LIST_HEAD(release_list);
221static DEFINE_SPINLOCK(release_list_lock);
222static void cgroup_release_agent(struct work_struct *work);
223static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700224static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700225
Paul Menage817929e2007-10-18 23:39:36 -0700226/* Link structure for associating css_set objects with cgroups */
227struct cg_cgroup_link {
228 /*
229 * List running through cg_cgroup_links associated with a
230 * cgroup, anchored on cgroup->css_sets
231 */
Paul Menagebd89aab2007-10-18 23:40:44 -0700232 struct list_head cgrp_link_list;
Paul Menage7717f7b2009-09-23 15:56:22 -0700233 struct cgroup *cgrp;
Paul Menage817929e2007-10-18 23:39:36 -0700234 /*
235 * List running through cg_cgroup_links pointing at a
236 * single css_set object, anchored on css_set->cg_links
237 */
238 struct list_head cg_link_list;
239 struct css_set *cg;
240};
241
242/* The default css_set - used by init and its children prior to any
243 * hierarchies being mounted. It contains a pointer to the root state
244 * for each subsystem. Also used to anchor the list of css_sets. Not
245 * reference-counted, to improve performance when child cgroups
246 * haven't been created.
247 */
248
249static struct css_set init_css_set;
250static struct cg_cgroup_link init_css_set_link;
251
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700252static int cgroup_subsys_init_idr(struct cgroup_subsys *ss);
253
Paul Menage817929e2007-10-18 23:39:36 -0700254/* css_set_lock protects the list of css_set objects, and the
255 * chain of tasks off each css_set. Nests outside task->alloc_lock
256 * due to cgroup_iter_start() */
257static DEFINE_RWLOCK(css_set_lock);
258static int css_set_count;
259
Paul Menage7717f7b2009-09-23 15:56:22 -0700260/*
261 * hash table for cgroup groups. This improves the performance to find
262 * an existing css_set. This hash doesn't (currently) take into
263 * account cgroups in empty hierarchies.
264 */
Li Zefan472b1052008-04-29 01:00:11 -0700265#define CSS_SET_HASH_BITS 7
266#define CSS_SET_TABLE_SIZE (1 << CSS_SET_HASH_BITS)
267static struct hlist_head css_set_table[CSS_SET_TABLE_SIZE];
268
269static struct hlist_head *css_set_hash(struct cgroup_subsys_state *css[])
270{
271 int i;
272 int index;
273 unsigned long tmp = 0UL;
274
275 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++)
276 tmp += (unsigned long)css[i];
277 tmp = (tmp >> 16) ^ tmp;
278
279 index = hash_long(tmp, CSS_SET_HASH_BITS);
280
281 return &css_set_table[index];
282}
283
Ben Blumc3783692009-09-23 15:56:29 -0700284static void free_css_set_rcu(struct rcu_head *obj)
285{
286 struct css_set *cg = container_of(obj, struct css_set, rcu_head);
287 kfree(cg);
288}
289
Paul Menage817929e2007-10-18 23:39:36 -0700290/* We don't maintain the lists running through each css_set to its
291 * task until after the first call to cgroup_iter_start(). This
292 * reduces the fork()/exit() overhead for people who have cgroups
293 * compiled into their kernel but not actually in use */
Li Zefan8947f9d2008-07-25 01:46:56 -0700294static int use_task_css_set_links __read_mostly;
Paul Menage817929e2007-10-18 23:39:36 -0700295
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700296static void __put_css_set(struct css_set *cg, int taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700297{
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700298 struct cg_cgroup_link *link;
299 struct cg_cgroup_link *saved_link;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700300 /*
301 * Ensure that the refcount doesn't hit zero while any readers
302 * can see it. Similar to atomic_dec_and_lock(), but for an
303 * rwlock
304 */
305 if (atomic_add_unless(&cg->refcount, -1, 1))
306 return;
307 write_lock(&css_set_lock);
308 if (!atomic_dec_and_test(&cg->refcount)) {
309 write_unlock(&css_set_lock);
310 return;
311 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700312
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700313 /* This css_set is dead. unlink it and release cgroup refcounts */
314 hlist_del(&cg->hlist);
315 css_set_count--;
316
317 list_for_each_entry_safe(link, saved_link, &cg->cg_links,
318 cg_link_list) {
319 struct cgroup *cgrp = link->cgrp;
320 list_del(&link->cg_link_list);
321 list_del(&link->cgrp_link_list);
Paul Menagebd89aab2007-10-18 23:40:44 -0700322 if (atomic_dec_and_test(&cgrp->count) &&
323 notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700324 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700325 set_bit(CGRP_RELEASABLE, &cgrp->flags);
326 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700327 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700328
329 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700330 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700331
332 write_unlock(&css_set_lock);
Ben Blumc3783692009-09-23 15:56:29 -0700333 call_rcu(&cg->rcu_head, free_css_set_rcu);
Paul Menage817929e2007-10-18 23:39:36 -0700334}
335
336/*
337 * refcounted get/put for css_set objects
338 */
339static inline void get_css_set(struct css_set *cg)
340{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700341 atomic_inc(&cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700342}
343
344static inline void put_css_set(struct css_set *cg)
345{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700346 __put_css_set(cg, 0);
Paul Menage817929e2007-10-18 23:39:36 -0700347}
348
Paul Menage81a6a5c2007-10-18 23:39:38 -0700349static inline void put_css_set_taskexit(struct css_set *cg)
350{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700351 __put_css_set(cg, 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700352}
353
Paul Menage817929e2007-10-18 23:39:36 -0700354/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700355 * compare_css_sets - helper function for find_existing_css_set().
356 * @cg: candidate css_set being tested
357 * @old_cg: existing css_set for a task
358 * @new_cgrp: cgroup that's being entered by the task
359 * @template: desired set of css pointers in css_set (pre-calculated)
360 *
361 * Returns true if "cg" matches "old_cg" except for the hierarchy
362 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
363 */
364static bool compare_css_sets(struct css_set *cg,
365 struct css_set *old_cg,
366 struct cgroup *new_cgrp,
367 struct cgroup_subsys_state *template[])
368{
369 struct list_head *l1, *l2;
370
371 if (memcmp(template, cg->subsys, sizeof(cg->subsys))) {
372 /* Not all subsystems matched */
373 return false;
374 }
375
376 /*
377 * Compare cgroup pointers in order to distinguish between
378 * different cgroups in heirarchies with no subsystems. We
379 * could get by with just this check alone (and skip the
380 * memcmp above) but on most setups the memcmp check will
381 * avoid the need for this more expensive check on almost all
382 * candidates.
383 */
384
385 l1 = &cg->cg_links;
386 l2 = &old_cg->cg_links;
387 while (1) {
388 struct cg_cgroup_link *cgl1, *cgl2;
389 struct cgroup *cg1, *cg2;
390
391 l1 = l1->next;
392 l2 = l2->next;
393 /* See if we reached the end - both lists are equal length. */
394 if (l1 == &cg->cg_links) {
395 BUG_ON(l2 != &old_cg->cg_links);
396 break;
397 } else {
398 BUG_ON(l2 == &old_cg->cg_links);
399 }
400 /* Locate the cgroups associated with these links. */
401 cgl1 = list_entry(l1, struct cg_cgroup_link, cg_link_list);
402 cgl2 = list_entry(l2, struct cg_cgroup_link, cg_link_list);
403 cg1 = cgl1->cgrp;
404 cg2 = cgl2->cgrp;
405 /* Hierarchies should be linked in the same order. */
406 BUG_ON(cg1->root != cg2->root);
407
408 /*
409 * If this hierarchy is the hierarchy of the cgroup
410 * that's changing, then we need to check that this
411 * css_set points to the new cgroup; if it's any other
412 * hierarchy, then this css_set should point to the
413 * same cgroup as the old css_set.
414 */
415 if (cg1->root == new_cgrp->root) {
416 if (cg1 != new_cgrp)
417 return false;
418 } else {
419 if (cg1 != cg2)
420 return false;
421 }
422 }
423 return true;
424}
425
426/*
Paul Menage817929e2007-10-18 23:39:36 -0700427 * find_existing_css_set() is a helper for
428 * find_css_set(), and checks to see whether an existing
Li Zefan472b1052008-04-29 01:00:11 -0700429 * css_set is suitable.
Paul Menage817929e2007-10-18 23:39:36 -0700430 *
431 * oldcg: the cgroup group that we're using before the cgroup
432 * transition
433 *
Paul Menagebd89aab2007-10-18 23:40:44 -0700434 * cgrp: the cgroup that we're moving into
Paul Menage817929e2007-10-18 23:39:36 -0700435 *
436 * template: location in which to build the desired set of subsystem
437 * state objects for the new cgroup group
438 */
Paul Menage817929e2007-10-18 23:39:36 -0700439static struct css_set *find_existing_css_set(
440 struct css_set *oldcg,
Paul Menagebd89aab2007-10-18 23:40:44 -0700441 struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -0700442 struct cgroup_subsys_state *template[])
443{
444 int i;
Paul Menagebd89aab2007-10-18 23:40:44 -0700445 struct cgroupfs_root *root = cgrp->root;
Li Zefan472b1052008-04-29 01:00:11 -0700446 struct hlist_head *hhead;
447 struct hlist_node *node;
448 struct css_set *cg;
Paul Menage817929e2007-10-18 23:39:36 -0700449
450 /* Built the set of subsystem state objects that we want to
451 * see in the new css_set */
452 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Li Zefan8d53d552008-02-23 15:24:11 -0800453 if (root->subsys_bits & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700454 /* Subsystem is in this hierarchy. So we want
455 * the subsystem state from the new
456 * cgroup */
Paul Menagebd89aab2007-10-18 23:40:44 -0700457 template[i] = cgrp->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700458 } else {
459 /* Subsystem is not in this hierarchy, so we
460 * don't want to change the subsystem state */
461 template[i] = oldcg->subsys[i];
462 }
463 }
464
Li Zefan472b1052008-04-29 01:00:11 -0700465 hhead = css_set_hash(template);
466 hlist_for_each_entry(cg, node, hhead, hlist) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700467 if (!compare_css_sets(cg, oldcg, cgrp, template))
468 continue;
469
470 /* This css_set matches what we need */
471 return cg;
Li Zefan472b1052008-04-29 01:00:11 -0700472 }
Paul Menage817929e2007-10-18 23:39:36 -0700473
474 /* No existing cgroup group matched */
475 return NULL;
476}
477
Paul Menage817929e2007-10-18 23:39:36 -0700478static void free_cg_links(struct list_head *tmp)
479{
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700480 struct cg_cgroup_link *link;
481 struct cg_cgroup_link *saved_link;
482
483 list_for_each_entry_safe(link, saved_link, tmp, cgrp_link_list) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700484 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -0700485 kfree(link);
486 }
487}
488
489/*
Li Zefan36553432008-07-29 22:33:19 -0700490 * allocate_cg_links() allocates "count" cg_cgroup_link structures
491 * and chains them on tmp through their cgrp_link_list fields. Returns 0 on
492 * success or a negative error
493 */
494static int allocate_cg_links(int count, struct list_head *tmp)
495{
496 struct cg_cgroup_link *link;
497 int i;
498 INIT_LIST_HEAD(tmp);
499 for (i = 0; i < count; i++) {
500 link = kmalloc(sizeof(*link), GFP_KERNEL);
501 if (!link) {
502 free_cg_links(tmp);
503 return -ENOMEM;
504 }
505 list_add(&link->cgrp_link_list, tmp);
506 }
507 return 0;
508}
509
Li Zefanc12f65d2009-01-07 18:07:42 -0800510/**
511 * link_css_set - a helper function to link a css_set to a cgroup
512 * @tmp_cg_links: cg_cgroup_link objects allocated by allocate_cg_links()
513 * @cg: the css_set to be linked
514 * @cgrp: the destination cgroup
515 */
516static void link_css_set(struct list_head *tmp_cg_links,
517 struct css_set *cg, struct cgroup *cgrp)
518{
519 struct cg_cgroup_link *link;
520
521 BUG_ON(list_empty(tmp_cg_links));
522 link = list_first_entry(tmp_cg_links, struct cg_cgroup_link,
523 cgrp_link_list);
524 link->cg = cg;
Paul Menage7717f7b2009-09-23 15:56:22 -0700525 link->cgrp = cgrp;
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700526 atomic_inc(&cgrp->count);
Li Zefanc12f65d2009-01-07 18:07:42 -0800527 list_move(&link->cgrp_link_list, &cgrp->css_sets);
Paul Menage7717f7b2009-09-23 15:56:22 -0700528 /*
529 * Always add links to the tail of the list so that the list
530 * is sorted by order of hierarchy creation
531 */
532 list_add_tail(&link->cg_link_list, &cg->cg_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800533}
534
Li Zefan36553432008-07-29 22:33:19 -0700535/*
Paul Menage817929e2007-10-18 23:39:36 -0700536 * find_css_set() takes an existing cgroup group and a
537 * cgroup object, and returns a css_set object that's
538 * equivalent to the old group, but with the given cgroup
539 * substituted into the appropriate hierarchy. Must be called with
540 * cgroup_mutex held
541 */
Paul Menage817929e2007-10-18 23:39:36 -0700542static struct css_set *find_css_set(
Paul Menagebd89aab2007-10-18 23:40:44 -0700543 struct css_set *oldcg, struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700544{
545 struct css_set *res;
546 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT];
Paul Menage817929e2007-10-18 23:39:36 -0700547
548 struct list_head tmp_cg_links;
Paul Menage817929e2007-10-18 23:39:36 -0700549
Li Zefan472b1052008-04-29 01:00:11 -0700550 struct hlist_head *hhead;
Paul Menage7717f7b2009-09-23 15:56:22 -0700551 struct cg_cgroup_link *link;
Li Zefan472b1052008-04-29 01:00:11 -0700552
Paul Menage817929e2007-10-18 23:39:36 -0700553 /* First see if we already have a cgroup group that matches
554 * the desired set */
Li Zefan7e9abd82008-07-25 01:46:54 -0700555 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -0700556 res = find_existing_css_set(oldcg, cgrp, template);
Paul Menage817929e2007-10-18 23:39:36 -0700557 if (res)
558 get_css_set(res);
Li Zefan7e9abd82008-07-25 01:46:54 -0700559 read_unlock(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -0700560
561 if (res)
562 return res;
563
564 res = kmalloc(sizeof(*res), GFP_KERNEL);
565 if (!res)
566 return NULL;
567
568 /* Allocate all the cg_cgroup_link objects that we'll need */
569 if (allocate_cg_links(root_count, &tmp_cg_links) < 0) {
570 kfree(res);
571 return NULL;
572 }
573
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700574 atomic_set(&res->refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -0700575 INIT_LIST_HEAD(&res->cg_links);
576 INIT_LIST_HEAD(&res->tasks);
Li Zefan472b1052008-04-29 01:00:11 -0700577 INIT_HLIST_NODE(&res->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700578
579 /* Copy the set of subsystem state objects generated in
580 * find_existing_css_set() */
581 memcpy(res->subsys, template, sizeof(res->subsys));
582
583 write_lock(&css_set_lock);
584 /* Add reference counts and links from the new css_set. */
Paul Menage7717f7b2009-09-23 15:56:22 -0700585 list_for_each_entry(link, &oldcg->cg_links, cg_link_list) {
586 struct cgroup *c = link->cgrp;
587 if (c->root == cgrp->root)
588 c = cgrp;
589 link_css_set(&tmp_cg_links, res, c);
590 }
Paul Menage817929e2007-10-18 23:39:36 -0700591
592 BUG_ON(!list_empty(&tmp_cg_links));
593
Paul Menage817929e2007-10-18 23:39:36 -0700594 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700595
596 /* Add this cgroup group to the hash table */
597 hhead = css_set_hash(res->subsys);
598 hlist_add_head(&res->hlist, hhead);
599
Paul Menage817929e2007-10-18 23:39:36 -0700600 write_unlock(&css_set_lock);
601
602 return res;
Paul Menageb4f48b62007-10-18 23:39:33 -0700603}
604
Paul Menageddbcc7e2007-10-18 23:39:30 -0700605/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700606 * Return the cgroup for "task" from the given hierarchy. Must be
607 * called with cgroup_mutex held.
608 */
609static struct cgroup *task_cgroup_from_root(struct task_struct *task,
610 struct cgroupfs_root *root)
611{
612 struct css_set *css;
613 struct cgroup *res = NULL;
614
615 BUG_ON(!mutex_is_locked(&cgroup_mutex));
616 read_lock(&css_set_lock);
617 /*
618 * No need to lock the task - since we hold cgroup_mutex the
619 * task can't change groups, so the only thing that can happen
620 * is that it exits and its css is set back to init_css_set.
621 */
622 css = task->cgroups;
623 if (css == &init_css_set) {
624 res = &root->top_cgroup;
625 } else {
626 struct cg_cgroup_link *link;
627 list_for_each_entry(link, &css->cg_links, cg_link_list) {
628 struct cgroup *c = link->cgrp;
629 if (c->root == root) {
630 res = c;
631 break;
632 }
633 }
634 }
635 read_unlock(&css_set_lock);
636 BUG_ON(!res);
637 return res;
638}
639
640/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700641 * There is one global cgroup mutex. We also require taking
642 * task_lock() when dereferencing a task's cgroup subsys pointers.
643 * See "The task_lock() exception", at the end of this comment.
644 *
645 * A task must hold cgroup_mutex to modify cgroups.
646 *
647 * Any task can increment and decrement the count field without lock.
648 * So in general, code holding cgroup_mutex can't rely on the count
649 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800650 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700651 * means that no tasks are currently attached, therefore there is no
652 * way a task attached to that cgroup can fork (the other way to
653 * increment the count). So code holding cgroup_mutex can safely
654 * assume that if the count is zero, it will stay zero. Similarly, if
655 * a task holds cgroup_mutex on a cgroup with zero count, it
656 * knows that the cgroup won't be removed, as cgroup_rmdir()
657 * needs that mutex.
658 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700659 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
660 * (usually) take cgroup_mutex. These are the two most performance
661 * critical pieces of code here. The exception occurs on cgroup_exit(),
662 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
663 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800664 * to the release agent with the name of the cgroup (path relative to
665 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700666 *
667 * A cgroup can only be deleted if both its 'count' of using tasks
668 * is zero, and its list of 'children' cgroups is empty. Since all
669 * tasks in the system use _some_ cgroup, and since there is always at
670 * least one task in the system (init, pid == 1), therefore, top_cgroup
671 * always has either children cgroups and/or using tasks. So we don't
672 * need a special hack to ensure that top_cgroup cannot be deleted.
673 *
674 * The task_lock() exception
675 *
676 * The need for this exception arises from the action of
Cliff Wickman956db3c2008-02-07 00:14:43 -0800677 * cgroup_attach_task(), which overwrites one tasks cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800678 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700679 * several performance critical places that need to reference
680 * task->cgroup without the expense of grabbing a system global
681 * mutex. Therefore except as noted below, when dereferencing or, as
Cliff Wickman956db3c2008-02-07 00:14:43 -0800682 * in cgroup_attach_task(), modifying a task'ss cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700683 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
684 * the task_struct routinely used for such matters.
685 *
686 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800687 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700688 */
689
Paul Menageddbcc7e2007-10-18 23:39:30 -0700690/**
691 * cgroup_lock - lock out any changes to cgroup structures
692 *
693 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700694void cgroup_lock(void)
695{
696 mutex_lock(&cgroup_mutex);
697}
698
699/**
700 * cgroup_unlock - release lock on cgroup changes
701 *
702 * Undo the lock taken in a previous cgroup_lock() call.
703 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700704void cgroup_unlock(void)
705{
706 mutex_unlock(&cgroup_mutex);
707}
708
709/*
710 * A couple of forward declarations required, due to cyclic reference loop:
711 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
712 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
713 * -> cgroup_mkdir.
714 */
715
716static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode);
717static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -0700718static int cgroup_populate_dir(struct cgroup *cgrp);
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700719static const struct inode_operations cgroup_dir_inode_operations;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700720static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700721
722static struct backing_dev_info cgroup_backing_dev_info = {
Jens Axboed9938312009-06-12 14:45:52 +0200723 .name = "cgroup",
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700724 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Paul Menagea4243162007-10-18 23:39:35 -0700725};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700726
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700727static int alloc_css_id(struct cgroup_subsys *ss,
728 struct cgroup *parent, struct cgroup *child);
729
Paul Menageddbcc7e2007-10-18 23:39:30 -0700730static struct inode *cgroup_new_inode(mode_t mode, struct super_block *sb)
731{
732 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700733
734 if (inode) {
735 inode->i_mode = mode;
David Howells76aac0e2008-11-14 10:39:12 +1100736 inode->i_uid = current_fsuid();
737 inode->i_gid = current_fsgid();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700738 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
739 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
740 }
741 return inode;
742}
743
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800744/*
745 * Call subsys's pre_destroy handler.
746 * This is called before css refcnt check.
747 */
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700748static int cgroup_call_pre_destroy(struct cgroup *cgrp)
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800749{
750 struct cgroup_subsys *ss;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700751 int ret = 0;
752
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800753 for_each_subsys(cgrp->root, ss)
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700754 if (ss->pre_destroy) {
755 ret = ss->pre_destroy(ss, cgrp);
756 if (ret)
757 break;
758 }
759 return ret;
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800760}
761
Paul Menagea47295e2009-01-07 18:07:44 -0800762static void free_cgroup_rcu(struct rcu_head *obj)
763{
764 struct cgroup *cgrp = container_of(obj, struct cgroup, rcu_head);
765
766 kfree(cgrp);
767}
768
Paul Menageddbcc7e2007-10-18 23:39:30 -0700769static void cgroup_diput(struct dentry *dentry, struct inode *inode)
770{
771 /* is dentry a directory ? if so, kfree() associated cgroup */
772 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700773 struct cgroup *cgrp = dentry->d_fsdata;
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800774 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -0700775 BUG_ON(!(cgroup_is_removed(cgrp)));
Paul Menage81a6a5c2007-10-18 23:39:38 -0700776 /* It's possible for external users to be holding css
777 * reference counts on a cgroup; css_put() needs to
778 * be able to access the cgroup after decrementing
779 * the reference count in order to know if it needs to
780 * queue the cgroup to be handled by the release
781 * agent */
782 synchronize_rcu();
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800783
784 mutex_lock(&cgroup_mutex);
785 /*
786 * Release the subsystem state objects.
787 */
Li Zefan75139b82009-01-07 18:07:33 -0800788 for_each_subsys(cgrp->root, ss)
789 ss->destroy(ss, cgrp);
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800790
791 cgrp->root->number_of_cgroups--;
792 mutex_unlock(&cgroup_mutex);
793
Paul Menagea47295e2009-01-07 18:07:44 -0800794 /*
795 * Drop the active superblock reference that we took when we
796 * created the cgroup
797 */
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800798 deactivate_super(cgrp->root->sb);
799
Ben Blum72a8cb32009-09-23 15:56:27 -0700800 /*
801 * if we're getting rid of the cgroup, refcount should ensure
802 * that there are no pidlists left.
803 */
804 BUG_ON(!list_empty(&cgrp->pidlists));
805
Paul Menagea47295e2009-01-07 18:07:44 -0800806 call_rcu(&cgrp->rcu_head, free_cgroup_rcu);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700807 }
808 iput(inode);
809}
810
811static void remove_dir(struct dentry *d)
812{
813 struct dentry *parent = dget(d->d_parent);
814
815 d_delete(d);
816 simple_rmdir(parent->d_inode, d);
817 dput(parent);
818}
819
820static void cgroup_clear_directory(struct dentry *dentry)
821{
822 struct list_head *node;
823
824 BUG_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
825 spin_lock(&dcache_lock);
826 node = dentry->d_subdirs.next;
827 while (node != &dentry->d_subdirs) {
828 struct dentry *d = list_entry(node, struct dentry, d_u.d_child);
829 list_del_init(node);
830 if (d->d_inode) {
831 /* This should never be called on a cgroup
832 * directory with child cgroups */
833 BUG_ON(d->d_inode->i_mode & S_IFDIR);
834 d = dget_locked(d);
835 spin_unlock(&dcache_lock);
836 d_delete(d);
837 simple_unlink(dentry->d_inode, d);
838 dput(d);
839 spin_lock(&dcache_lock);
840 }
841 node = dentry->d_subdirs.next;
842 }
843 spin_unlock(&dcache_lock);
844}
845
846/*
847 * NOTE : the dentry must have been dget()'ed
848 */
849static void cgroup_d_remove_dir(struct dentry *dentry)
850{
851 cgroup_clear_directory(dentry);
852
853 spin_lock(&dcache_lock);
854 list_del_init(&dentry->d_u.d_child);
855 spin_unlock(&dcache_lock);
856 remove_dir(dentry);
857}
858
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700859/*
860 * A queue for waiters to do rmdir() cgroup. A tasks will sleep when
861 * cgroup->count == 0 && list_empty(&cgroup->children) && subsys has some
862 * reference to css->refcnt. In general, this refcnt is expected to goes down
863 * to zero, soon.
864 *
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -0700865 * CGRP_WAIT_ON_RMDIR flag is set under cgroup's inode->i_mutex;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700866 */
867DECLARE_WAIT_QUEUE_HEAD(cgroup_rmdir_waitq);
868
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -0700869static void cgroup_wakeup_rmdir_waiter(struct cgroup *cgrp)
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700870{
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -0700871 if (unlikely(test_and_clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags)))
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700872 wake_up_all(&cgroup_rmdir_waitq);
873}
874
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -0700875void cgroup_exclude_rmdir(struct cgroup_subsys_state *css)
876{
877 css_get(css);
878}
879
880void cgroup_release_and_wakeup_rmdir(struct cgroup_subsys_state *css)
881{
882 cgroup_wakeup_rmdir_waiter(css->cgroup);
883 css_put(css);
884}
885
886
Paul Menageddbcc7e2007-10-18 23:39:30 -0700887static int rebind_subsystems(struct cgroupfs_root *root,
888 unsigned long final_bits)
889{
890 unsigned long added_bits, removed_bits;
Paul Menagebd89aab2007-10-18 23:40:44 -0700891 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700892 int i;
893
894 removed_bits = root->actual_subsys_bits & ~final_bits;
895 added_bits = final_bits & ~root->actual_subsys_bits;
896 /* Check that any added subsystems are currently free */
897 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Li Zefan8d53d552008-02-23 15:24:11 -0800898 unsigned long bit = 1UL << i;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700899 struct cgroup_subsys *ss = subsys[i];
900 if (!(bit & added_bits))
901 continue;
902 if (ss->root != &rootnode) {
903 /* Subsystem isn't free */
904 return -EBUSY;
905 }
906 }
907
908 /* Currently we don't handle adding/removing subsystems when
909 * any child cgroups exist. This is theoretically supportable
910 * but involves complex error handling, so it's being left until
911 * later */
Paul Menage307257c2008-12-15 13:54:22 -0800912 if (root->number_of_cgroups > 1)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700913 return -EBUSY;
914
915 /* Process each subsystem */
916 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
917 struct cgroup_subsys *ss = subsys[i];
918 unsigned long bit = 1UL << i;
919 if (bit & added_bits) {
920 /* We're binding this subsystem to this hierarchy */
Paul Menagebd89aab2007-10-18 23:40:44 -0700921 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700922 BUG_ON(!dummytop->subsys[i]);
923 BUG_ON(dummytop->subsys[i]->cgroup != dummytop);
Paul Menage999cd8a2009-01-07 18:08:36 -0800924 mutex_lock(&ss->hierarchy_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -0700925 cgrp->subsys[i] = dummytop->subsys[i];
926 cgrp->subsys[i]->cgroup = cgrp;
Li Zefan33a68ac2009-01-07 18:07:42 -0800927 list_move(&ss->sibling, &root->subsys_list);
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -0800928 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700929 if (ss->bind)
Paul Menagebd89aab2007-10-18 23:40:44 -0700930 ss->bind(ss, cgrp);
Paul Menage999cd8a2009-01-07 18:08:36 -0800931 mutex_unlock(&ss->hierarchy_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700932 } else if (bit & removed_bits) {
933 /* We're removing this subsystem */
Paul Menagebd89aab2007-10-18 23:40:44 -0700934 BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]);
935 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
Paul Menage999cd8a2009-01-07 18:08:36 -0800936 mutex_lock(&ss->hierarchy_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700937 if (ss->bind)
938 ss->bind(ss, dummytop);
939 dummytop->subsys[i]->cgroup = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -0700940 cgrp->subsys[i] = NULL;
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -0800941 subsys[i]->root = &rootnode;
Li Zefan33a68ac2009-01-07 18:07:42 -0800942 list_move(&ss->sibling, &rootnode.subsys_list);
Paul Menage999cd8a2009-01-07 18:08:36 -0800943 mutex_unlock(&ss->hierarchy_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700944 } else if (bit & final_bits) {
945 /* Subsystem state should already exist */
Paul Menagebd89aab2007-10-18 23:40:44 -0700946 BUG_ON(!cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700947 } else {
948 /* Subsystem state shouldn't exist */
Paul Menagebd89aab2007-10-18 23:40:44 -0700949 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700950 }
951 }
952 root->subsys_bits = root->actual_subsys_bits = final_bits;
953 synchronize_rcu();
954
955 return 0;
956}
957
958static int cgroup_show_options(struct seq_file *seq, struct vfsmount *vfs)
959{
960 struct cgroupfs_root *root = vfs->mnt_sb->s_fs_info;
961 struct cgroup_subsys *ss;
962
963 mutex_lock(&cgroup_mutex);
964 for_each_subsys(root, ss)
965 seq_printf(seq, ",%s", ss->name);
966 if (test_bit(ROOT_NOPREFIX, &root->flags))
967 seq_puts(seq, ",noprefix");
Paul Menage81a6a5c2007-10-18 23:39:38 -0700968 if (strlen(root->release_agent_path))
969 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Paul Menagec6d57f32009-09-23 15:56:19 -0700970 if (strlen(root->name))
971 seq_printf(seq, ",name=%s", root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700972 mutex_unlock(&cgroup_mutex);
973 return 0;
974}
975
976struct cgroup_sb_opts {
977 unsigned long subsys_bits;
978 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700979 char *release_agent;
Paul Menagec6d57f32009-09-23 15:56:19 -0700980 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700981 /* User explicitly requested empty subsystem */
982 bool none;
Paul Menagec6d57f32009-09-23 15:56:19 -0700983
984 struct cgroupfs_root *new_root;
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700985
Paul Menageddbcc7e2007-10-18 23:39:30 -0700986};
987
988/* Convert a hierarchy specifier into a bitmask of subsystems and
989 * flags. */
990static int parse_cgroupfs_options(char *data,
991 struct cgroup_sb_opts *opts)
992{
993 char *token, *o = data ?: "all";
Li Zefanf9ab5b52009-06-17 16:26:33 -0700994 unsigned long mask = (unsigned long)-1;
995
996#ifdef CONFIG_CPUSETS
997 mask = ~(1UL << cpuset_subsys_id);
998#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -0700999
Paul Menagec6d57f32009-09-23 15:56:19 -07001000 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001001
1002 while ((token = strsep(&o, ",")) != NULL) {
1003 if (!*token)
1004 return -EINVAL;
1005 if (!strcmp(token, "all")) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07001006 /* Add all non-disabled subsystems */
1007 int i;
1008 opts->subsys_bits = 0;
1009 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1010 struct cgroup_subsys *ss = subsys[i];
1011 if (!ss->disabled)
1012 opts->subsys_bits |= 1ul << i;
1013 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001014 } else if (!strcmp(token, "none")) {
1015 /* Explicitly have no subsystems */
1016 opts->none = true;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001017 } else if (!strcmp(token, "noprefix")) {
1018 set_bit(ROOT_NOPREFIX, &opts->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001019 } else if (!strncmp(token, "release_agent=", 14)) {
1020 /* Specifying two release agents is forbidden */
1021 if (opts->release_agent)
1022 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001023 opts->release_agent =
1024 kstrndup(token + 14, PATH_MAX, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001025 if (!opts->release_agent)
1026 return -ENOMEM;
Paul Menagec6d57f32009-09-23 15:56:19 -07001027 } else if (!strncmp(token, "name=", 5)) {
1028 int i;
1029 const char *name = token + 5;
1030 /* Can't specify an empty name */
1031 if (!strlen(name))
1032 return -EINVAL;
1033 /* Must match [\w.-]+ */
1034 for (i = 0; i < strlen(name); i++) {
1035 char c = name[i];
1036 if (isalnum(c))
1037 continue;
1038 if ((c == '.') || (c == '-') || (c == '_'))
1039 continue;
1040 return -EINVAL;
1041 }
1042 /* Specifying two names is forbidden */
1043 if (opts->name)
1044 return -EINVAL;
1045 opts->name = kstrndup(name,
1046 MAX_CGROUP_ROOT_NAMELEN,
1047 GFP_KERNEL);
1048 if (!opts->name)
1049 return -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001050 } else {
1051 struct cgroup_subsys *ss;
1052 int i;
1053 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1054 ss = subsys[i];
1055 if (!strcmp(token, ss->name)) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07001056 if (!ss->disabled)
1057 set_bit(i, &opts->subsys_bits);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001058 break;
1059 }
1060 }
1061 if (i == CGROUP_SUBSYS_COUNT)
1062 return -ENOENT;
1063 }
1064 }
1065
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001066 /* Consistency checks */
1067
Li Zefanf9ab5b52009-06-17 16:26:33 -07001068 /*
1069 * Option noprefix was introduced just for backward compatibility
1070 * with the old cpuset, so we allow noprefix only if mounting just
1071 * the cpuset subsystem.
1072 */
1073 if (test_bit(ROOT_NOPREFIX, &opts->flags) &&
1074 (opts->subsys_bits & mask))
1075 return -EINVAL;
1076
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001077
1078 /* Can't specify "none" and some subsystems */
1079 if (opts->subsys_bits && opts->none)
1080 return -EINVAL;
1081
1082 /*
1083 * We either have to specify by name or by subsystems. (So all
1084 * empty hierarchies must have a name).
1085 */
Paul Menagec6d57f32009-09-23 15:56:19 -07001086 if (!opts->subsys_bits && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001087 return -EINVAL;
1088
1089 return 0;
1090}
1091
1092static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1093{
1094 int ret = 0;
1095 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001096 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001097 struct cgroup_sb_opts opts;
1098
Alessio Igor Bogani337eb002009-05-12 15:10:54 +02001099 lock_kernel();
Paul Menagebd89aab2007-10-18 23:40:44 -07001100 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001101 mutex_lock(&cgroup_mutex);
1102
1103 /* See what subsystems are wanted */
1104 ret = parse_cgroupfs_options(data, &opts);
1105 if (ret)
1106 goto out_unlock;
1107
1108 /* Don't allow flags to change at remount */
1109 if (opts.flags != root->flags) {
1110 ret = -EINVAL;
1111 goto out_unlock;
1112 }
1113
Paul Menagec6d57f32009-09-23 15:56:19 -07001114 /* Don't allow name to change at remount */
1115 if (opts.name && strcmp(opts.name, root->name)) {
1116 ret = -EINVAL;
1117 goto out_unlock;
1118 }
1119
Paul Menageddbcc7e2007-10-18 23:39:30 -07001120 ret = rebind_subsystems(root, opts.subsys_bits);
Li Zefan0670e082009-04-02 16:57:30 -07001121 if (ret)
1122 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001123
1124 /* (re)populate subsystem files */
Li Zefan0670e082009-04-02 16:57:30 -07001125 cgroup_populate_dir(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001126
Paul Menage81a6a5c2007-10-18 23:39:38 -07001127 if (opts.release_agent)
1128 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001129 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001130 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001131 kfree(opts.name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001132 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07001133 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Alessio Igor Bogani337eb002009-05-12 15:10:54 +02001134 unlock_kernel();
Paul Menageddbcc7e2007-10-18 23:39:30 -07001135 return ret;
1136}
1137
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07001138static const struct super_operations cgroup_ops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001139 .statfs = simple_statfs,
1140 .drop_inode = generic_delete_inode,
1141 .show_options = cgroup_show_options,
1142 .remount_fs = cgroup_remount,
1143};
1144
Paul Menagecc31edc2008-10-18 20:28:04 -07001145static void init_cgroup_housekeeping(struct cgroup *cgrp)
1146{
1147 INIT_LIST_HEAD(&cgrp->sibling);
1148 INIT_LIST_HEAD(&cgrp->children);
1149 INIT_LIST_HEAD(&cgrp->css_sets);
1150 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001151 INIT_LIST_HEAD(&cgrp->pidlists);
1152 mutex_init(&cgrp->pidlist_mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07001153}
Paul Menagec6d57f32009-09-23 15:56:19 -07001154
Paul Menageddbcc7e2007-10-18 23:39:30 -07001155static void init_cgroup_root(struct cgroupfs_root *root)
1156{
Paul Menagebd89aab2007-10-18 23:40:44 -07001157 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001158 INIT_LIST_HEAD(&root->subsys_list);
1159 INIT_LIST_HEAD(&root->root_list);
1160 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -07001161 cgrp->root = root;
1162 cgrp->top_cgroup = cgrp;
Paul Menagecc31edc2008-10-18 20:28:04 -07001163 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001164}
1165
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001166static bool init_root_id(struct cgroupfs_root *root)
1167{
1168 int ret = 0;
1169
1170 do {
1171 if (!ida_pre_get(&hierarchy_ida, GFP_KERNEL))
1172 return false;
1173 spin_lock(&hierarchy_id_lock);
1174 /* Try to allocate the next unused ID */
1175 ret = ida_get_new_above(&hierarchy_ida, next_hierarchy_id,
1176 &root->hierarchy_id);
1177 if (ret == -ENOSPC)
1178 /* Try again starting from 0 */
1179 ret = ida_get_new(&hierarchy_ida, &root->hierarchy_id);
1180 if (!ret) {
1181 next_hierarchy_id = root->hierarchy_id + 1;
1182 } else if (ret != -EAGAIN) {
1183 /* Can only get here if the 31-bit IDR is full ... */
1184 BUG_ON(ret);
1185 }
1186 spin_unlock(&hierarchy_id_lock);
1187 } while (ret);
1188 return true;
1189}
1190
Paul Menageddbcc7e2007-10-18 23:39:30 -07001191static int cgroup_test_super(struct super_block *sb, void *data)
1192{
Paul Menagec6d57f32009-09-23 15:56:19 -07001193 struct cgroup_sb_opts *opts = data;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001194 struct cgroupfs_root *root = sb->s_fs_info;
1195
Paul Menagec6d57f32009-09-23 15:56:19 -07001196 /* If we asked for a name then it must match */
1197 if (opts->name && strcmp(opts->name, root->name))
1198 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001199
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001200 /*
1201 * If we asked for subsystems (or explicitly for no
1202 * subsystems) then they must match
1203 */
1204 if ((opts->subsys_bits || opts->none)
1205 && (opts->subsys_bits != root->subsys_bits))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001206 return 0;
1207
1208 return 1;
1209}
1210
Paul Menagec6d57f32009-09-23 15:56:19 -07001211static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1212{
1213 struct cgroupfs_root *root;
1214
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001215 if (!opts->subsys_bits && !opts->none)
Paul Menagec6d57f32009-09-23 15:56:19 -07001216 return NULL;
1217
1218 root = kzalloc(sizeof(*root), GFP_KERNEL);
1219 if (!root)
1220 return ERR_PTR(-ENOMEM);
1221
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001222 if (!init_root_id(root)) {
1223 kfree(root);
1224 return ERR_PTR(-ENOMEM);
1225 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001226 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001227
Paul Menagec6d57f32009-09-23 15:56:19 -07001228 root->subsys_bits = opts->subsys_bits;
1229 root->flags = opts->flags;
1230 if (opts->release_agent)
1231 strcpy(root->release_agent_path, opts->release_agent);
1232 if (opts->name)
1233 strcpy(root->name, opts->name);
1234 return root;
1235}
1236
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001237static void cgroup_drop_root(struct cgroupfs_root *root)
1238{
1239 if (!root)
1240 return;
1241
1242 BUG_ON(!root->hierarchy_id);
1243 spin_lock(&hierarchy_id_lock);
1244 ida_remove(&hierarchy_ida, root->hierarchy_id);
1245 spin_unlock(&hierarchy_id_lock);
1246 kfree(root);
1247}
1248
Paul Menageddbcc7e2007-10-18 23:39:30 -07001249static int cgroup_set_super(struct super_block *sb, void *data)
1250{
1251 int ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001252 struct cgroup_sb_opts *opts = data;
1253
1254 /* If we don't have a new root, we can't set up a new sb */
1255 if (!opts->new_root)
1256 return -EINVAL;
1257
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001258 BUG_ON(!opts->subsys_bits && !opts->none);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001259
1260 ret = set_anon_super(sb, NULL);
1261 if (ret)
1262 return ret;
1263
Paul Menagec6d57f32009-09-23 15:56:19 -07001264 sb->s_fs_info = opts->new_root;
1265 opts->new_root->sb = sb;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001266
1267 sb->s_blocksize = PAGE_CACHE_SIZE;
1268 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1269 sb->s_magic = CGROUP_SUPER_MAGIC;
1270 sb->s_op = &cgroup_ops;
1271
1272 return 0;
1273}
1274
1275static int cgroup_get_rootdir(struct super_block *sb)
1276{
1277 struct inode *inode =
1278 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
1279 struct dentry *dentry;
1280
1281 if (!inode)
1282 return -ENOMEM;
1283
Paul Menageddbcc7e2007-10-18 23:39:30 -07001284 inode->i_fop = &simple_dir_operations;
1285 inode->i_op = &cgroup_dir_inode_operations;
1286 /* directories start off with i_nlink == 2 (for "." entry) */
1287 inc_nlink(inode);
1288 dentry = d_alloc_root(inode);
1289 if (!dentry) {
1290 iput(inode);
1291 return -ENOMEM;
1292 }
1293 sb->s_root = dentry;
1294 return 0;
1295}
1296
1297static int cgroup_get_sb(struct file_system_type *fs_type,
1298 int flags, const char *unused_dev_name,
1299 void *data, struct vfsmount *mnt)
1300{
1301 struct cgroup_sb_opts opts;
Paul Menagec6d57f32009-09-23 15:56:19 -07001302 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001303 int ret = 0;
1304 struct super_block *sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001305 struct cgroupfs_root *new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001306
1307 /* First find the desired set of subsystems */
1308 ret = parse_cgroupfs_options(data, &opts);
Paul Menagec6d57f32009-09-23 15:56:19 -07001309 if (ret)
1310 goto out_err;
1311
1312 /*
1313 * Allocate a new cgroup root. We may not need it if we're
1314 * reusing an existing hierarchy.
1315 */
1316 new_root = cgroup_root_from_opts(&opts);
1317 if (IS_ERR(new_root)) {
1318 ret = PTR_ERR(new_root);
1319 goto out_err;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001320 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001321 opts.new_root = new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001322
Paul Menagec6d57f32009-09-23 15:56:19 -07001323 /* Locate an existing or new sb for this hierarchy */
1324 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001325 if (IS_ERR(sb)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001326 ret = PTR_ERR(sb);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001327 cgroup_drop_root(opts.new_root);
Paul Menagec6d57f32009-09-23 15:56:19 -07001328 goto out_err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001329 }
1330
Paul Menagec6d57f32009-09-23 15:56:19 -07001331 root = sb->s_fs_info;
1332 BUG_ON(!root);
1333 if (root == opts.new_root) {
1334 /* We used the new root structure, so this is a new hierarchy */
1335 struct list_head tmp_cg_links;
Li Zefanc12f65d2009-01-07 18:07:42 -08001336 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menage817929e2007-10-18 23:39:36 -07001337 struct inode *inode;
Paul Menagec6d57f32009-09-23 15:56:19 -07001338 struct cgroupfs_root *existing_root;
Li Zefan28fd5df2008-04-29 01:00:13 -07001339 int i;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001340
1341 BUG_ON(sb->s_root != NULL);
1342
1343 ret = cgroup_get_rootdir(sb);
1344 if (ret)
1345 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001346 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001347
Paul Menage817929e2007-10-18 23:39:36 -07001348 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001349 mutex_lock(&cgroup_mutex);
1350
Paul Menagec6d57f32009-09-23 15:56:19 -07001351 if (strlen(root->name)) {
1352 /* Check for name clashes with existing mounts */
1353 for_each_active_root(existing_root) {
1354 if (!strcmp(existing_root->name, root->name)) {
1355 ret = -EBUSY;
1356 mutex_unlock(&cgroup_mutex);
1357 mutex_unlock(&inode->i_mutex);
1358 goto drop_new_super;
1359 }
1360 }
1361 }
1362
Paul Menage817929e2007-10-18 23:39:36 -07001363 /*
1364 * We're accessing css_set_count without locking
1365 * css_set_lock here, but that's OK - it can only be
1366 * increased by someone holding cgroup_lock, and
1367 * that's us. The worst that can happen is that we
1368 * have some link structures left over
1369 */
1370 ret = allocate_cg_links(css_set_count, &tmp_cg_links);
1371 if (ret) {
1372 mutex_unlock(&cgroup_mutex);
1373 mutex_unlock(&inode->i_mutex);
1374 goto drop_new_super;
1375 }
1376
Paul Menageddbcc7e2007-10-18 23:39:30 -07001377 ret = rebind_subsystems(root, root->subsys_bits);
1378 if (ret == -EBUSY) {
1379 mutex_unlock(&cgroup_mutex);
Paul Menage817929e2007-10-18 23:39:36 -07001380 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001381 free_cg_links(&tmp_cg_links);
1382 goto drop_new_super;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001383 }
1384
1385 /* EBUSY should be the only error here */
1386 BUG_ON(ret);
1387
1388 list_add(&root->root_list, &roots);
Paul Menage817929e2007-10-18 23:39:36 -07001389 root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001390
Li Zefanc12f65d2009-01-07 18:07:42 -08001391 sb->s_root->d_fsdata = root_cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001392 root->top_cgroup.dentry = sb->s_root;
1393
Paul Menage817929e2007-10-18 23:39:36 -07001394 /* Link the top cgroup in this hierarchy into all
1395 * the css_set objects */
1396 write_lock(&css_set_lock);
Li Zefan28fd5df2008-04-29 01:00:13 -07001397 for (i = 0; i < CSS_SET_TABLE_SIZE; i++) {
1398 struct hlist_head *hhead = &css_set_table[i];
1399 struct hlist_node *node;
Paul Menage817929e2007-10-18 23:39:36 -07001400 struct css_set *cg;
Li Zefan28fd5df2008-04-29 01:00:13 -07001401
Li Zefanc12f65d2009-01-07 18:07:42 -08001402 hlist_for_each_entry(cg, node, hhead, hlist)
1403 link_css_set(&tmp_cg_links, cg, root_cgrp);
Li Zefan28fd5df2008-04-29 01:00:13 -07001404 }
Paul Menage817929e2007-10-18 23:39:36 -07001405 write_unlock(&css_set_lock);
1406
1407 free_cg_links(&tmp_cg_links);
1408
Li Zefanc12f65d2009-01-07 18:07:42 -08001409 BUG_ON(!list_empty(&root_cgrp->sibling));
1410 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001411 BUG_ON(root->number_of_cgroups != 1);
1412
Li Zefanc12f65d2009-01-07 18:07:42 -08001413 cgroup_populate_dir(root_cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001414 mutex_unlock(&cgroup_mutex);
Xiaotian Feng34f77a92009-09-23 15:56:18 -07001415 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001416 } else {
1417 /*
1418 * We re-used an existing hierarchy - the new root (if
1419 * any) is not needed
1420 */
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001421 cgroup_drop_root(opts.new_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001422 }
1423
Sukadev Bhattiprolua3ec9472009-03-04 12:06:34 -08001424 simple_set_mnt(mnt, sb);
Paul Menagec6d57f32009-09-23 15:56:19 -07001425 kfree(opts.release_agent);
1426 kfree(opts.name);
Sukadev Bhattiprolua3ec9472009-03-04 12:06:34 -08001427 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001428
1429 drop_new_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04001430 deactivate_locked_super(sb);
Paul Menagec6d57f32009-09-23 15:56:19 -07001431 out_err:
1432 kfree(opts.release_agent);
1433 kfree(opts.name);
1434
Paul Menageddbcc7e2007-10-18 23:39:30 -07001435 return ret;
1436}
1437
1438static void cgroup_kill_sb(struct super_block *sb) {
1439 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001440 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001441 int ret;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001442 struct cg_cgroup_link *link;
1443 struct cg_cgroup_link *saved_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001444
1445 BUG_ON(!root);
1446
1447 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001448 BUG_ON(!list_empty(&cgrp->children));
1449 BUG_ON(!list_empty(&cgrp->sibling));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001450
1451 mutex_lock(&cgroup_mutex);
1452
1453 /* Rebind all subsystems back to the default hierarchy */
1454 ret = rebind_subsystems(root, 0);
1455 /* Shouldn't be able to fail ... */
1456 BUG_ON(ret);
1457
Paul Menage817929e2007-10-18 23:39:36 -07001458 /*
1459 * Release all the links from css_sets to this hierarchy's
1460 * root cgroup
1461 */
1462 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001463
1464 list_for_each_entry_safe(link, saved_link, &cgrp->css_sets,
1465 cgrp_link_list) {
Paul Menage817929e2007-10-18 23:39:36 -07001466 list_del(&link->cg_link_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07001467 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07001468 kfree(link);
1469 }
1470 write_unlock(&css_set_lock);
1471
Paul Menage839ec542009-01-29 14:25:22 -08001472 if (!list_empty(&root->root_list)) {
1473 list_del(&root->root_list);
1474 root_count--;
1475 }
Li Zefane5f6a862009-01-07 18:07:41 -08001476
Paul Menageddbcc7e2007-10-18 23:39:30 -07001477 mutex_unlock(&cgroup_mutex);
1478
Paul Menageddbcc7e2007-10-18 23:39:30 -07001479 kill_litter_super(sb);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001480 cgroup_drop_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001481}
1482
1483static struct file_system_type cgroup_fs_type = {
1484 .name = "cgroup",
1485 .get_sb = cgroup_get_sb,
1486 .kill_sb = cgroup_kill_sb,
1487};
1488
Paul Menagebd89aab2007-10-18 23:40:44 -07001489static inline struct cgroup *__d_cgrp(struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001490{
1491 return dentry->d_fsdata;
1492}
1493
1494static inline struct cftype *__d_cft(struct dentry *dentry)
1495{
1496 return dentry->d_fsdata;
1497}
1498
Li Zefana043e3b2008-02-23 15:24:09 -08001499/**
1500 * cgroup_path - generate the path of a cgroup
1501 * @cgrp: the cgroup in question
1502 * @buf: the buffer to write the path into
1503 * @buflen: the length of the buffer
1504 *
Paul Menagea47295e2009-01-07 18:07:44 -08001505 * Called with cgroup_mutex held or else with an RCU-protected cgroup
1506 * reference. Writes path of cgroup into buf. Returns 0 on success,
1507 * -errno on error.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001508 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001509int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001510{
1511 char *start;
Paul Menagea47295e2009-01-07 18:07:44 -08001512 struct dentry *dentry = rcu_dereference(cgrp->dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001513
Paul Menagea47295e2009-01-07 18:07:44 -08001514 if (!dentry || cgrp == dummytop) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001515 /*
1516 * Inactive subsystems have no dentry for their root
1517 * cgroup
1518 */
1519 strcpy(buf, "/");
1520 return 0;
1521 }
1522
1523 start = buf + buflen;
1524
1525 *--start = '\0';
1526 for (;;) {
Paul Menagea47295e2009-01-07 18:07:44 -08001527 int len = dentry->d_name.len;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001528 if ((start -= len) < buf)
1529 return -ENAMETOOLONG;
Paul Menagebd89aab2007-10-18 23:40:44 -07001530 memcpy(start, cgrp->dentry->d_name.name, len);
1531 cgrp = cgrp->parent;
1532 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001533 break;
Paul Menagea47295e2009-01-07 18:07:44 -08001534 dentry = rcu_dereference(cgrp->dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07001535 if (!cgrp->parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001536 continue;
1537 if (--start < buf)
1538 return -ENAMETOOLONG;
1539 *start = '/';
1540 }
1541 memmove(buf, start, buf + buflen - start);
1542 return 0;
1543}
1544
Li Zefana043e3b2008-02-23 15:24:09 -08001545/**
1546 * cgroup_attach_task - attach task 'tsk' to cgroup 'cgrp'
1547 * @cgrp: the cgroup the task is attaching to
1548 * @tsk: the task to be attached
Paul Menagebbcb81d2007-10-18 23:39:32 -07001549 *
Li Zefana043e3b2008-02-23 15:24:09 -08001550 * Call holding cgroup_mutex. May take task_lock of
1551 * the task 'tsk' during call.
Paul Menagebbcb81d2007-10-18 23:39:32 -07001552 */
Cliff Wickman956db3c2008-02-07 00:14:43 -08001553int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001554{
1555 int retval = 0;
1556 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07001557 struct cgroup *oldcgrp;
Lai Jiangshan77efecd2009-01-07 18:07:39 -08001558 struct css_set *cg;
Paul Menage817929e2007-10-18 23:39:36 -07001559 struct css_set *newcg;
Paul Menagebd89aab2007-10-18 23:40:44 -07001560 struct cgroupfs_root *root = cgrp->root;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001561
1562 /* Nothing to do if the task is already in that cgroup */
Paul Menage7717f7b2009-09-23 15:56:22 -07001563 oldcgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07001564 if (cgrp == oldcgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001565 return 0;
1566
1567 for_each_subsys(root, ss) {
1568 if (ss->can_attach) {
Ben Blumbe367d02009-09-23 15:56:31 -07001569 retval = ss->can_attach(ss, cgrp, tsk, false);
Paul Jacksone18f6312008-02-07 00:13:44 -08001570 if (retval)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001571 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001572 }
1573 }
1574
Lai Jiangshan77efecd2009-01-07 18:07:39 -08001575 task_lock(tsk);
1576 cg = tsk->cgroups;
1577 get_css_set(cg);
1578 task_unlock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07001579 /*
1580 * Locate or allocate a new css_set for this task,
1581 * based on its final set of cgroups
1582 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001583 newcg = find_css_set(cg, cgrp);
Lai Jiangshan77efecd2009-01-07 18:07:39 -08001584 put_css_set(cg);
Paul Jacksone18f6312008-02-07 00:13:44 -08001585 if (!newcg)
Paul Menage817929e2007-10-18 23:39:36 -07001586 return -ENOMEM;
Paul Menage817929e2007-10-18 23:39:36 -07001587
Paul Menagebbcb81d2007-10-18 23:39:32 -07001588 task_lock(tsk);
1589 if (tsk->flags & PF_EXITING) {
1590 task_unlock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07001591 put_css_set(newcg);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001592 return -ESRCH;
1593 }
Paul Menage817929e2007-10-18 23:39:36 -07001594 rcu_assign_pointer(tsk->cgroups, newcg);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001595 task_unlock(tsk);
1596
Paul Menage817929e2007-10-18 23:39:36 -07001597 /* Update the css_set linked lists if we're using them */
1598 write_lock(&css_set_lock);
1599 if (!list_empty(&tsk->cg_list)) {
1600 list_del(&tsk->cg_list);
1601 list_add(&tsk->cg_list, &newcg->tasks);
1602 }
1603 write_unlock(&css_set_lock);
1604
Paul Menagebbcb81d2007-10-18 23:39:32 -07001605 for_each_subsys(root, ss) {
Paul Jacksone18f6312008-02-07 00:13:44 -08001606 if (ss->attach)
Ben Blumbe367d02009-09-23 15:56:31 -07001607 ss->attach(ss, cgrp, oldcgrp, tsk, false);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001608 }
Paul Menagebd89aab2007-10-18 23:40:44 -07001609 set_bit(CGRP_RELEASABLE, &oldcgrp->flags);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001610 synchronize_rcu();
Paul Menage817929e2007-10-18 23:39:36 -07001611 put_css_set(cg);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07001612
1613 /*
1614 * wake up rmdir() waiter. the rmdir should fail since the cgroup
1615 * is no longer empty.
1616 */
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07001617 cgroup_wakeup_rmdir_waiter(cgrp);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001618 return 0;
1619}
1620
1621/*
Paul Menageaf351022008-07-25 01:47:01 -07001622 * Attach task with pid 'pid' to cgroup 'cgrp'. Call with cgroup_mutex
1623 * held. May take task_lock of task
Paul Menagebbcb81d2007-10-18 23:39:32 -07001624 */
Paul Menageaf351022008-07-25 01:47:01 -07001625static int attach_task_by_pid(struct cgroup *cgrp, u64 pid)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001626{
Paul Menagebbcb81d2007-10-18 23:39:32 -07001627 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11001628 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001629 int ret;
1630
Paul Menagebbcb81d2007-10-18 23:39:32 -07001631 if (pid) {
1632 rcu_read_lock();
Pavel Emelyanov73507f32008-02-07 00:14:47 -08001633 tsk = find_task_by_vpid(pid);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001634 if (!tsk || tsk->flags & PF_EXITING) {
1635 rcu_read_unlock();
1636 return -ESRCH;
1637 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07001638
David Howellsc69e8d92008-11-14 10:39:19 +11001639 tcred = __task_cred(tsk);
1640 if (cred->euid &&
1641 cred->euid != tcred->uid &&
1642 cred->euid != tcred->suid) {
1643 rcu_read_unlock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07001644 return -EACCES;
1645 }
David Howellsc69e8d92008-11-14 10:39:19 +11001646 get_task_struct(tsk);
1647 rcu_read_unlock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07001648 } else {
1649 tsk = current;
1650 get_task_struct(tsk);
1651 }
1652
Cliff Wickman956db3c2008-02-07 00:14:43 -08001653 ret = cgroup_attach_task(cgrp, tsk);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001654 put_task_struct(tsk);
1655 return ret;
1656}
1657
Paul Menageaf351022008-07-25 01:47:01 -07001658static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid)
1659{
1660 int ret;
1661 if (!cgroup_lock_live_group(cgrp))
1662 return -ENODEV;
1663 ret = attach_task_by_pid(cgrp, pid);
1664 cgroup_unlock();
1665 return ret;
1666}
1667
Paul Menagee788e0662008-07-25 01:46:59 -07001668/**
1669 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
1670 * @cgrp: the cgroup to be checked for liveness
1671 *
Paul Menage84eea842008-07-25 01:47:00 -07001672 * On success, returns true; the lock should be later released with
1673 * cgroup_unlock(). On failure returns false with no lock held.
Paul Menagee788e0662008-07-25 01:46:59 -07001674 */
Paul Menage84eea842008-07-25 01:47:00 -07001675bool cgroup_lock_live_group(struct cgroup *cgrp)
Paul Menagee788e0662008-07-25 01:46:59 -07001676{
1677 mutex_lock(&cgroup_mutex);
1678 if (cgroup_is_removed(cgrp)) {
1679 mutex_unlock(&cgroup_mutex);
1680 return false;
1681 }
1682 return true;
1683}
1684
1685static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
1686 const char *buffer)
1687{
1688 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
1689 if (!cgroup_lock_live_group(cgrp))
1690 return -ENODEV;
1691 strcpy(cgrp->root->release_agent_path, buffer);
Paul Menage84eea842008-07-25 01:47:00 -07001692 cgroup_unlock();
Paul Menagee788e0662008-07-25 01:46:59 -07001693 return 0;
1694}
1695
1696static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
1697 struct seq_file *seq)
1698{
1699 if (!cgroup_lock_live_group(cgrp))
1700 return -ENODEV;
1701 seq_puts(seq, cgrp->root->release_agent_path);
1702 seq_putc(seq, '\n');
Paul Menage84eea842008-07-25 01:47:00 -07001703 cgroup_unlock();
Paul Menagee788e0662008-07-25 01:46:59 -07001704 return 0;
1705}
1706
Paul Menage84eea842008-07-25 01:47:00 -07001707/* A buffer size big enough for numbers or short strings */
1708#define CGROUP_LOCAL_BUFFER_SIZE 64
1709
Paul Menagee73d2c62008-04-29 01:00:06 -07001710static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
Paul Menagef4c753b2008-04-29 00:59:56 -07001711 struct file *file,
1712 const char __user *userbuf,
1713 size_t nbytes, loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07001714{
Paul Menage84eea842008-07-25 01:47:00 -07001715 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menage355e0c42007-10-18 23:39:33 -07001716 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07001717 char *end;
1718
1719 if (!nbytes)
1720 return -EINVAL;
1721 if (nbytes >= sizeof(buffer))
1722 return -E2BIG;
1723 if (copy_from_user(buffer, userbuf, nbytes))
1724 return -EFAULT;
1725
1726 buffer[nbytes] = 0; /* nul-terminate */
Paul Menagee73d2c62008-04-29 01:00:06 -07001727 if (cft->write_u64) {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07001728 u64 val = simple_strtoull(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07001729 if (*end)
1730 return -EINVAL;
1731 retval = cft->write_u64(cgrp, cft, val);
1732 } else {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07001733 s64 val = simple_strtoll(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07001734 if (*end)
1735 return -EINVAL;
1736 retval = cft->write_s64(cgrp, cft, val);
1737 }
Paul Menage355e0c42007-10-18 23:39:33 -07001738 if (!retval)
1739 retval = nbytes;
1740 return retval;
1741}
1742
Paul Menagedb3b1492008-07-25 01:46:58 -07001743static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
1744 struct file *file,
1745 const char __user *userbuf,
1746 size_t nbytes, loff_t *unused_ppos)
1747{
Paul Menage84eea842008-07-25 01:47:00 -07001748 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagedb3b1492008-07-25 01:46:58 -07001749 int retval = 0;
1750 size_t max_bytes = cft->max_write_len;
1751 char *buffer = local_buffer;
1752
1753 if (!max_bytes)
1754 max_bytes = sizeof(local_buffer) - 1;
1755 if (nbytes >= max_bytes)
1756 return -E2BIG;
1757 /* Allocate a dynamic buffer if we need one */
1758 if (nbytes >= sizeof(local_buffer)) {
1759 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
1760 if (buffer == NULL)
1761 return -ENOMEM;
1762 }
Li Zefan5a3eb9f2008-07-29 22:33:18 -07001763 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
1764 retval = -EFAULT;
1765 goto out;
1766 }
Paul Menagedb3b1492008-07-25 01:46:58 -07001767
1768 buffer[nbytes] = 0; /* nul-terminate */
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07001769 retval = cft->write_string(cgrp, cft, strstrip(buffer));
Paul Menagedb3b1492008-07-25 01:46:58 -07001770 if (!retval)
1771 retval = nbytes;
Li Zefan5a3eb9f2008-07-29 22:33:18 -07001772out:
Paul Menagedb3b1492008-07-25 01:46:58 -07001773 if (buffer != local_buffer)
1774 kfree(buffer);
1775 return retval;
1776}
1777
Paul Menageddbcc7e2007-10-18 23:39:30 -07001778static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
1779 size_t nbytes, loff_t *ppos)
1780{
1781 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07001782 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001783
Li Zefan75139b82009-01-07 18:07:33 -08001784 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001785 return -ENODEV;
Paul Menage355e0c42007-10-18 23:39:33 -07001786 if (cft->write)
Paul Menagebd89aab2007-10-18 23:40:44 -07001787 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07001788 if (cft->write_u64 || cft->write_s64)
1789 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagedb3b1492008-07-25 01:46:58 -07001790 if (cft->write_string)
1791 return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07001792 if (cft->trigger) {
1793 int ret = cft->trigger(cgrp, (unsigned int)cft->private);
1794 return ret ? ret : nbytes;
1795 }
Paul Menage355e0c42007-10-18 23:39:33 -07001796 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001797}
1798
Paul Menagef4c753b2008-04-29 00:59:56 -07001799static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
1800 struct file *file,
1801 char __user *buf, size_t nbytes,
1802 loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001803{
Paul Menage84eea842008-07-25 01:47:00 -07001804 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagef4c753b2008-04-29 00:59:56 -07001805 u64 val = cft->read_u64(cgrp, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001806 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
1807
1808 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
1809}
1810
Paul Menagee73d2c62008-04-29 01:00:06 -07001811static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
1812 struct file *file,
1813 char __user *buf, size_t nbytes,
1814 loff_t *ppos)
1815{
Paul Menage84eea842008-07-25 01:47:00 -07001816 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagee73d2c62008-04-29 01:00:06 -07001817 s64 val = cft->read_s64(cgrp, cft);
1818 int len = sprintf(tmp, "%lld\n", (long long) val);
1819
1820 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
1821}
1822
Paul Menageddbcc7e2007-10-18 23:39:30 -07001823static ssize_t cgroup_file_read(struct file *file, char __user *buf,
1824 size_t nbytes, loff_t *ppos)
1825{
1826 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07001827 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001828
Li Zefan75139b82009-01-07 18:07:33 -08001829 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001830 return -ENODEV;
1831
1832 if (cft->read)
Paul Menagebd89aab2007-10-18 23:40:44 -07001833 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07001834 if (cft->read_u64)
1835 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07001836 if (cft->read_s64)
1837 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001838 return -EINVAL;
1839}
1840
Paul Menage91796562008-04-29 01:00:01 -07001841/*
1842 * seqfile ops/methods for returning structured data. Currently just
1843 * supports string->u64 maps, but can be extended in future.
1844 */
1845
1846struct cgroup_seqfile_state {
1847 struct cftype *cft;
1848 struct cgroup *cgroup;
1849};
1850
1851static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
1852{
1853 struct seq_file *sf = cb->state;
1854 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
1855}
1856
1857static int cgroup_seqfile_show(struct seq_file *m, void *arg)
1858{
1859 struct cgroup_seqfile_state *state = m->private;
1860 struct cftype *cft = state->cft;
Serge E. Hallyn29486df2008-04-29 01:00:14 -07001861 if (cft->read_map) {
1862 struct cgroup_map_cb cb = {
1863 .fill = cgroup_map_add,
1864 .state = m,
1865 };
1866 return cft->read_map(state->cgroup, cft, &cb);
1867 }
1868 return cft->read_seq_string(state->cgroup, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07001869}
1870
Adrian Bunk96930a62008-07-25 19:46:21 -07001871static int cgroup_seqfile_release(struct inode *inode, struct file *file)
Paul Menage91796562008-04-29 01:00:01 -07001872{
1873 struct seq_file *seq = file->private_data;
1874 kfree(seq->private);
1875 return single_release(inode, file);
1876}
1877
Alexey Dobriyan828c0952009-10-01 15:43:56 -07001878static const struct file_operations cgroup_seqfile_operations = {
Paul Menage91796562008-04-29 01:00:01 -07001879 .read = seq_read,
Paul Menagee788e0662008-07-25 01:46:59 -07001880 .write = cgroup_file_write,
Paul Menage91796562008-04-29 01:00:01 -07001881 .llseek = seq_lseek,
1882 .release = cgroup_seqfile_release,
1883};
1884
Paul Menageddbcc7e2007-10-18 23:39:30 -07001885static int cgroup_file_open(struct inode *inode, struct file *file)
1886{
1887 int err;
1888 struct cftype *cft;
1889
1890 err = generic_file_open(inode, file);
1891 if (err)
1892 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001893 cft = __d_cft(file->f_dentry);
Li Zefan75139b82009-01-07 18:07:33 -08001894
Serge E. Hallyn29486df2008-04-29 01:00:14 -07001895 if (cft->read_map || cft->read_seq_string) {
Paul Menage91796562008-04-29 01:00:01 -07001896 struct cgroup_seqfile_state *state =
1897 kzalloc(sizeof(*state), GFP_USER);
1898 if (!state)
1899 return -ENOMEM;
1900 state->cft = cft;
1901 state->cgroup = __d_cgrp(file->f_dentry->d_parent);
1902 file->f_op = &cgroup_seqfile_operations;
1903 err = single_open(file, cgroup_seqfile_show, state);
1904 if (err < 0)
1905 kfree(state);
1906 } else if (cft->open)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001907 err = cft->open(inode, file);
1908 else
1909 err = 0;
1910
1911 return err;
1912}
1913
1914static int cgroup_file_release(struct inode *inode, struct file *file)
1915{
1916 struct cftype *cft = __d_cft(file->f_dentry);
1917 if (cft->release)
1918 return cft->release(inode, file);
1919 return 0;
1920}
1921
1922/*
1923 * cgroup_rename - Only allow simple rename of directories in place.
1924 */
1925static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
1926 struct inode *new_dir, struct dentry *new_dentry)
1927{
1928 if (!S_ISDIR(old_dentry->d_inode->i_mode))
1929 return -ENOTDIR;
1930 if (new_dentry->d_inode)
1931 return -EEXIST;
1932 if (old_dir != new_dir)
1933 return -EIO;
1934 return simple_rename(old_dir, old_dentry, new_dir, new_dentry);
1935}
1936
Alexey Dobriyan828c0952009-10-01 15:43:56 -07001937static const struct file_operations cgroup_file_operations = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001938 .read = cgroup_file_read,
1939 .write = cgroup_file_write,
1940 .llseek = generic_file_llseek,
1941 .open = cgroup_file_open,
1942 .release = cgroup_file_release,
1943};
1944
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07001945static const struct inode_operations cgroup_dir_inode_operations = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001946 .lookup = simple_lookup,
1947 .mkdir = cgroup_mkdir,
1948 .rmdir = cgroup_rmdir,
1949 .rename = cgroup_rename,
1950};
1951
Li Zefan099fca32009-04-02 16:57:29 -07001952static int cgroup_create_file(struct dentry *dentry, mode_t mode,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001953 struct super_block *sb)
1954{
Al Viro3ba13d12009-02-20 06:02:22 +00001955 static const struct dentry_operations cgroup_dops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001956 .d_iput = cgroup_diput,
1957 };
1958
1959 struct inode *inode;
1960
1961 if (!dentry)
1962 return -ENOENT;
1963 if (dentry->d_inode)
1964 return -EEXIST;
1965
1966 inode = cgroup_new_inode(mode, sb);
1967 if (!inode)
1968 return -ENOMEM;
1969
1970 if (S_ISDIR(mode)) {
1971 inode->i_op = &cgroup_dir_inode_operations;
1972 inode->i_fop = &simple_dir_operations;
1973
1974 /* start off with i_nlink == 2 (for "." entry) */
1975 inc_nlink(inode);
1976
1977 /* start with the directory inode held, so that we can
1978 * populate it without racing with another mkdir */
Paul Menage817929e2007-10-18 23:39:36 -07001979 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001980 } else if (S_ISREG(mode)) {
1981 inode->i_size = 0;
1982 inode->i_fop = &cgroup_file_operations;
1983 }
1984 dentry->d_op = &cgroup_dops;
1985 d_instantiate(dentry, inode);
1986 dget(dentry); /* Extra count - pin the dentry in core */
1987 return 0;
1988}
1989
1990/*
Li Zefana043e3b2008-02-23 15:24:09 -08001991 * cgroup_create_dir - create a directory for an object.
1992 * @cgrp: the cgroup we create the directory for. It must have a valid
1993 * ->parent field. And we are going to fill its ->dentry field.
1994 * @dentry: dentry of the new cgroup
1995 * @mode: mode to set on new directory.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001996 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001997static int cgroup_create_dir(struct cgroup *cgrp, struct dentry *dentry,
Li Zefan099fca32009-04-02 16:57:29 -07001998 mode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001999{
2000 struct dentry *parent;
2001 int error = 0;
2002
Paul Menagebd89aab2007-10-18 23:40:44 -07002003 parent = cgrp->parent->dentry;
2004 error = cgroup_create_file(dentry, S_IFDIR | mode, cgrp->root->sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002005 if (!error) {
Paul Menagebd89aab2007-10-18 23:40:44 -07002006 dentry->d_fsdata = cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002007 inc_nlink(parent->d_inode);
Paul Menagea47295e2009-01-07 18:07:44 -08002008 rcu_assign_pointer(cgrp->dentry, dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002009 dget(dentry);
2010 }
2011 dput(dentry);
2012
2013 return error;
2014}
2015
Li Zefan099fca32009-04-02 16:57:29 -07002016/**
2017 * cgroup_file_mode - deduce file mode of a control file
2018 * @cft: the control file in question
2019 *
2020 * returns cft->mode if ->mode is not 0
2021 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2022 * returns S_IRUGO if it has only a read handler
2023 * returns S_IWUSR if it has only a write hander
2024 */
2025static mode_t cgroup_file_mode(const struct cftype *cft)
2026{
2027 mode_t mode = 0;
2028
2029 if (cft->mode)
2030 return cft->mode;
2031
2032 if (cft->read || cft->read_u64 || cft->read_s64 ||
2033 cft->read_map || cft->read_seq_string)
2034 mode |= S_IRUGO;
2035
2036 if (cft->write || cft->write_u64 || cft->write_s64 ||
2037 cft->write_string || cft->trigger)
2038 mode |= S_IWUSR;
2039
2040 return mode;
2041}
2042
Paul Menagebd89aab2007-10-18 23:40:44 -07002043int cgroup_add_file(struct cgroup *cgrp,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002044 struct cgroup_subsys *subsys,
2045 const struct cftype *cft)
2046{
Paul Menagebd89aab2007-10-18 23:40:44 -07002047 struct dentry *dir = cgrp->dentry;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002048 struct dentry *dentry;
2049 int error;
Li Zefan099fca32009-04-02 16:57:29 -07002050 mode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002051
2052 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Paul Menagebd89aab2007-10-18 23:40:44 -07002053 if (subsys && !test_bit(ROOT_NOPREFIX, &cgrp->root->flags)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002054 strcpy(name, subsys->name);
2055 strcat(name, ".");
2056 }
2057 strcat(name, cft->name);
2058 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
2059 dentry = lookup_one_len(name, dir, strlen(name));
2060 if (!IS_ERR(dentry)) {
Li Zefan099fca32009-04-02 16:57:29 -07002061 mode = cgroup_file_mode(cft);
2062 error = cgroup_create_file(dentry, mode | S_IFREG,
Paul Menagebd89aab2007-10-18 23:40:44 -07002063 cgrp->root->sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002064 if (!error)
2065 dentry->d_fsdata = (void *)cft;
2066 dput(dentry);
2067 } else
2068 error = PTR_ERR(dentry);
2069 return error;
2070}
2071
Paul Menagebd89aab2007-10-18 23:40:44 -07002072int cgroup_add_files(struct cgroup *cgrp,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002073 struct cgroup_subsys *subsys,
2074 const struct cftype cft[],
2075 int count)
2076{
2077 int i, err;
2078 for (i = 0; i < count; i++) {
Paul Menagebd89aab2007-10-18 23:40:44 -07002079 err = cgroup_add_file(cgrp, subsys, &cft[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002080 if (err)
2081 return err;
2082 }
2083 return 0;
2084}
2085
Li Zefana043e3b2008-02-23 15:24:09 -08002086/**
2087 * cgroup_task_count - count the number of tasks in a cgroup.
2088 * @cgrp: the cgroup in question
2089 *
2090 * Return the number of tasks in the cgroup.
2091 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002092int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002093{
2094 int count = 0;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07002095 struct cg_cgroup_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002096
Paul Menage817929e2007-10-18 23:39:36 -07002097 read_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07002098 list_for_each_entry(link, &cgrp->css_sets, cgrp_link_list) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07002099 count += atomic_read(&link->cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002100 }
2101 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002102 return count;
2103}
2104
2105/*
Paul Menage817929e2007-10-18 23:39:36 -07002106 * Advance a list_head iterator. The iterator should be positioned at
2107 * the start of a css_set
2108 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002109static void cgroup_advance_iter(struct cgroup *cgrp,
Paul Menage7717f7b2009-09-23 15:56:22 -07002110 struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002111{
2112 struct list_head *l = it->cg_link;
2113 struct cg_cgroup_link *link;
2114 struct css_set *cg;
2115
2116 /* Advance to the next non-empty css_set */
2117 do {
2118 l = l->next;
Paul Menagebd89aab2007-10-18 23:40:44 -07002119 if (l == &cgrp->css_sets) {
Paul Menage817929e2007-10-18 23:39:36 -07002120 it->cg_link = NULL;
2121 return;
2122 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002123 link = list_entry(l, struct cg_cgroup_link, cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07002124 cg = link->cg;
2125 } while (list_empty(&cg->tasks));
2126 it->cg_link = l;
2127 it->task = cg->tasks.next;
2128}
2129
Cliff Wickman31a7df02008-02-07 00:14:42 -08002130/*
2131 * To reduce the fork() overhead for systems that are not actually
2132 * using their cgroups capability, we don't maintain the lists running
2133 * through each css_set to its tasks until we see the list actually
2134 * used - in other words after the first call to cgroup_iter_start().
2135 *
2136 * The tasklist_lock is not held here, as do_each_thread() and
2137 * while_each_thread() are protected by RCU.
2138 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07002139static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002140{
2141 struct task_struct *p, *g;
2142 write_lock(&css_set_lock);
2143 use_task_css_set_links = 1;
2144 do_each_thread(g, p) {
2145 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08002146 /*
2147 * We should check if the process is exiting, otherwise
2148 * it will race with cgroup_exit() in that the list
2149 * entry won't be deleted though the process has exited.
2150 */
2151 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Cliff Wickman31a7df02008-02-07 00:14:42 -08002152 list_add(&p->cg_list, &p->cgroups->tasks);
2153 task_unlock(p);
2154 } while_each_thread(g, p);
2155 write_unlock(&css_set_lock);
2156}
2157
Paul Menagebd89aab2007-10-18 23:40:44 -07002158void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002159{
2160 /*
2161 * The first time anyone tries to iterate across a cgroup,
2162 * we need to enable the list linking each css_set to its
2163 * tasks, and fix up all existing tasks.
2164 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08002165 if (!use_task_css_set_links)
2166 cgroup_enable_task_cg_lists();
2167
Paul Menage817929e2007-10-18 23:39:36 -07002168 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07002169 it->cg_link = &cgrp->css_sets;
2170 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07002171}
2172
Paul Menagebd89aab2007-10-18 23:40:44 -07002173struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -07002174 struct cgroup_iter *it)
2175{
2176 struct task_struct *res;
2177 struct list_head *l = it->task;
Lai Jiangshan2019f632009-01-07 18:07:36 -08002178 struct cg_cgroup_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07002179
2180 /* If the iterator cg is NULL, we have no tasks */
2181 if (!it->cg_link)
2182 return NULL;
2183 res = list_entry(l, struct task_struct, cg_list);
2184 /* Advance iterator to find next entry */
2185 l = l->next;
Lai Jiangshan2019f632009-01-07 18:07:36 -08002186 link = list_entry(it->cg_link, struct cg_cgroup_link, cgrp_link_list);
2187 if (l == &link->cg->tasks) {
Paul Menage817929e2007-10-18 23:39:36 -07002188 /* We reached the end of this task list - move on to
2189 * the next cg_cgroup_link */
Paul Menagebd89aab2007-10-18 23:40:44 -07002190 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07002191 } else {
2192 it->task = l;
2193 }
2194 return res;
2195}
2196
Paul Menagebd89aab2007-10-18 23:40:44 -07002197void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002198{
2199 read_unlock(&css_set_lock);
2200}
2201
Cliff Wickman31a7df02008-02-07 00:14:42 -08002202static inline int started_after_time(struct task_struct *t1,
2203 struct timespec *time,
2204 struct task_struct *t2)
2205{
2206 int start_diff = timespec_compare(&t1->start_time, time);
2207 if (start_diff > 0) {
2208 return 1;
2209 } else if (start_diff < 0) {
2210 return 0;
2211 } else {
2212 /*
2213 * Arbitrarily, if two processes started at the same
2214 * time, we'll say that the lower pointer value
2215 * started first. Note that t2 may have exited by now
2216 * so this may not be a valid pointer any longer, but
2217 * that's fine - it still serves to distinguish
2218 * between two tasks started (effectively) simultaneously.
2219 */
2220 return t1 > t2;
2221 }
2222}
2223
2224/*
2225 * This function is a callback from heap_insert() and is used to order
2226 * the heap.
2227 * In this case we order the heap in descending task start time.
2228 */
2229static inline int started_after(void *p1, void *p2)
2230{
2231 struct task_struct *t1 = p1;
2232 struct task_struct *t2 = p2;
2233 return started_after_time(t1, &t2->start_time, t2);
2234}
2235
2236/**
2237 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
2238 * @scan: struct cgroup_scanner containing arguments for the scan
2239 *
2240 * Arguments include pointers to callback functions test_task() and
2241 * process_task().
2242 * Iterate through all the tasks in a cgroup, calling test_task() for each,
2243 * and if it returns true, call process_task() for it also.
2244 * The test_task pointer may be NULL, meaning always true (select all tasks).
2245 * Effectively duplicates cgroup_iter_{start,next,end}()
2246 * but does not lock css_set_lock for the call to process_task().
2247 * The struct cgroup_scanner may be embedded in any structure of the caller's
2248 * creation.
2249 * It is guaranteed that process_task() will act on every task that
2250 * is a member of the cgroup for the duration of this call. This
2251 * function may or may not call process_task() for tasks that exit
2252 * or move to a different cgroup during the call, or are forked or
2253 * move into the cgroup during the call.
2254 *
2255 * Note that test_task() may be called with locks held, and may in some
2256 * situations be called multiple times for the same task, so it should
2257 * be cheap.
2258 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
2259 * pre-allocated and will be used for heap operations (and its "gt" member will
2260 * be overwritten), else a temporary heap will be used (allocation of which
2261 * may cause this function to fail).
2262 */
2263int cgroup_scan_tasks(struct cgroup_scanner *scan)
2264{
2265 int retval, i;
2266 struct cgroup_iter it;
2267 struct task_struct *p, *dropped;
2268 /* Never dereference latest_task, since it's not refcounted */
2269 struct task_struct *latest_task = NULL;
2270 struct ptr_heap tmp_heap;
2271 struct ptr_heap *heap;
2272 struct timespec latest_time = { 0, 0 };
2273
2274 if (scan->heap) {
2275 /* The caller supplied our heap and pre-allocated its memory */
2276 heap = scan->heap;
2277 heap->gt = &started_after;
2278 } else {
2279 /* We need to allocate our own heap memory */
2280 heap = &tmp_heap;
2281 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
2282 if (retval)
2283 /* cannot allocate the heap */
2284 return retval;
2285 }
2286
2287 again:
2288 /*
2289 * Scan tasks in the cgroup, using the scanner's "test_task" callback
2290 * to determine which are of interest, and using the scanner's
2291 * "process_task" callback to process any of them that need an update.
2292 * Since we don't want to hold any locks during the task updates,
2293 * gather tasks to be processed in a heap structure.
2294 * The heap is sorted by descending task start time.
2295 * If the statically-sized heap fills up, we overflow tasks that
2296 * started later, and in future iterations only consider tasks that
2297 * started after the latest task in the previous pass. This
2298 * guarantees forward progress and that we don't miss any tasks.
2299 */
2300 heap->size = 0;
2301 cgroup_iter_start(scan->cg, &it);
2302 while ((p = cgroup_iter_next(scan->cg, &it))) {
2303 /*
2304 * Only affect tasks that qualify per the caller's callback,
2305 * if he provided one
2306 */
2307 if (scan->test_task && !scan->test_task(p, scan))
2308 continue;
2309 /*
2310 * Only process tasks that started after the last task
2311 * we processed
2312 */
2313 if (!started_after_time(p, &latest_time, latest_task))
2314 continue;
2315 dropped = heap_insert(heap, p);
2316 if (dropped == NULL) {
2317 /*
2318 * The new task was inserted; the heap wasn't
2319 * previously full
2320 */
2321 get_task_struct(p);
2322 } else if (dropped != p) {
2323 /*
2324 * The new task was inserted, and pushed out a
2325 * different task
2326 */
2327 get_task_struct(p);
2328 put_task_struct(dropped);
2329 }
2330 /*
2331 * Else the new task was newer than anything already in
2332 * the heap and wasn't inserted
2333 */
2334 }
2335 cgroup_iter_end(scan->cg, &it);
2336
2337 if (heap->size) {
2338 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07002339 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08002340 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07002341 latest_time = q->start_time;
2342 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08002343 }
2344 /* Process the task per the caller's callback */
Paul Jackson4fe91d52008-04-29 00:59:55 -07002345 scan->process_task(q, scan);
2346 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002347 }
2348 /*
2349 * If we had to process any tasks at all, scan again
2350 * in case some of them were in the middle of forking
2351 * children that didn't get processed.
2352 * Not the most efficient way to do it, but it avoids
2353 * having to take callback_mutex in the fork path
2354 */
2355 goto again;
2356 }
2357 if (heap == &tmp_heap)
2358 heap_free(&tmp_heap);
2359 return 0;
2360}
2361
Paul Menage817929e2007-10-18 23:39:36 -07002362/*
Ben Blum102a7752009-09-23 15:56:26 -07002363 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07002364 *
2365 * Reading this file can return large amounts of data if a cgroup has
2366 * *lots* of attached tasks. So it may need several calls to read(),
2367 * but we cannot guarantee that the information we produce is correct
2368 * unless we produce it entirely atomically.
2369 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07002370 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07002371
2372/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07002373 * The following two functions "fix" the issue where there are more pids
2374 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
2375 * TODO: replace with a kernel-wide solution to this problem
2376 */
2377#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
2378static void *pidlist_allocate(int count)
2379{
2380 if (PIDLIST_TOO_LARGE(count))
2381 return vmalloc(count * sizeof(pid_t));
2382 else
2383 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
2384}
2385static void pidlist_free(void *p)
2386{
2387 if (is_vmalloc_addr(p))
2388 vfree(p);
2389 else
2390 kfree(p);
2391}
2392static void *pidlist_resize(void *p, int newcount)
2393{
2394 void *newlist;
2395 /* note: if new alloc fails, old p will still be valid either way */
2396 if (is_vmalloc_addr(p)) {
2397 newlist = vmalloc(newcount * sizeof(pid_t));
2398 if (!newlist)
2399 return NULL;
2400 memcpy(newlist, p, newcount * sizeof(pid_t));
2401 vfree(p);
2402 } else {
2403 newlist = krealloc(p, newcount * sizeof(pid_t), GFP_KERNEL);
2404 }
2405 return newlist;
2406}
2407
2408/*
Ben Blum102a7752009-09-23 15:56:26 -07002409 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
2410 * If the new stripped list is sufficiently smaller and there's enough memory
2411 * to allocate a new buffer, will let go of the unneeded memory. Returns the
2412 * number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07002413 */
Ben Blum102a7752009-09-23 15:56:26 -07002414/* is the size difference enough that we should re-allocate the array? */
2415#define PIDLIST_REALLOC_DIFFERENCE(old, new) ((old) - PAGE_SIZE >= (new))
2416static int pidlist_uniq(pid_t **p, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002417{
Ben Blum102a7752009-09-23 15:56:26 -07002418 int src, dest = 1;
2419 pid_t *list = *p;
2420 pid_t *newlist;
2421
2422 /*
2423 * we presume the 0th element is unique, so i starts at 1. trivial
2424 * edge cases first; no work needs to be done for either
2425 */
2426 if (length == 0 || length == 1)
2427 return length;
2428 /* src and dest walk down the list; dest counts unique elements */
2429 for (src = 1; src < length; src++) {
2430 /* find next unique element */
2431 while (list[src] == list[src-1]) {
2432 src++;
2433 if (src == length)
2434 goto after;
2435 }
2436 /* dest always points to where the next unique element goes */
2437 list[dest] = list[src];
2438 dest++;
2439 }
2440after:
2441 /*
2442 * if the length difference is large enough, we want to allocate a
2443 * smaller buffer to save memory. if this fails due to out of memory,
2444 * we'll just stay with what we've got.
2445 */
2446 if (PIDLIST_REALLOC_DIFFERENCE(length, dest)) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07002447 newlist = pidlist_resize(list, dest);
Ben Blum102a7752009-09-23 15:56:26 -07002448 if (newlist)
2449 *p = newlist;
2450 }
2451 return dest;
2452}
2453
2454static int cmppid(const void *a, const void *b)
2455{
2456 return *(pid_t *)a - *(pid_t *)b;
2457}
2458
2459/*
Ben Blum72a8cb32009-09-23 15:56:27 -07002460 * find the appropriate pidlist for our purpose (given procs vs tasks)
2461 * returns with the lock on that pidlist already held, and takes care
2462 * of the use count, or returns NULL with no locks held if we're out of
2463 * memory.
2464 */
2465static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
2466 enum cgroup_filetype type)
2467{
2468 struct cgroup_pidlist *l;
2469 /* don't need task_nsproxy() if we're looking at ourself */
2470 struct pid_namespace *ns = get_pid_ns(current->nsproxy->pid_ns);
2471 /*
2472 * We can't drop the pidlist_mutex before taking the l->mutex in case
2473 * the last ref-holder is trying to remove l from the list at the same
2474 * time. Holding the pidlist_mutex precludes somebody taking whichever
2475 * list we find out from under us - compare release_pid_array().
2476 */
2477 mutex_lock(&cgrp->pidlist_mutex);
2478 list_for_each_entry(l, &cgrp->pidlists, links) {
2479 if (l->key.type == type && l->key.ns == ns) {
2480 /* found a matching list - drop the extra refcount */
2481 put_pid_ns(ns);
2482 /* make sure l doesn't vanish out from under us */
2483 down_write(&l->mutex);
2484 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07002485 return l;
2486 }
2487 }
2488 /* entry not found; create a new one */
2489 l = kmalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
2490 if (!l) {
2491 mutex_unlock(&cgrp->pidlist_mutex);
2492 put_pid_ns(ns);
2493 return l;
2494 }
2495 init_rwsem(&l->mutex);
2496 down_write(&l->mutex);
2497 l->key.type = type;
2498 l->key.ns = ns;
2499 l->use_count = 0; /* don't increment here */
2500 l->list = NULL;
2501 l->owner = cgrp;
2502 list_add(&l->links, &cgrp->pidlists);
2503 mutex_unlock(&cgrp->pidlist_mutex);
2504 return l;
2505}
2506
2507/*
Ben Blum102a7752009-09-23 15:56:26 -07002508 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
2509 */
Ben Blum72a8cb32009-09-23 15:56:27 -07002510static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
2511 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07002512{
2513 pid_t *array;
2514 int length;
2515 int pid, n = 0; /* used for populating the array */
Paul Menage817929e2007-10-18 23:39:36 -07002516 struct cgroup_iter it;
2517 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07002518 struct cgroup_pidlist *l;
2519
2520 /*
2521 * If cgroup gets more users after we read count, we won't have
2522 * enough space - tough. This race is indistinguishable to the
2523 * caller from the case that the additional cgroup users didn't
2524 * show up until sometime later on.
2525 */
2526 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07002527 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07002528 if (!array)
2529 return -ENOMEM;
2530 /* now, populate the array */
Paul Menagebd89aab2007-10-18 23:40:44 -07002531 cgroup_iter_start(cgrp, &it);
2532 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Ben Blum102a7752009-09-23 15:56:26 -07002533 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07002534 break;
Ben Blum102a7752009-09-23 15:56:26 -07002535 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07002536 if (type == CGROUP_FILE_PROCS)
2537 pid = task_tgid_vnr(tsk);
2538 else
2539 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07002540 if (pid > 0) /* make sure to only use valid results */
2541 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07002542 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002543 cgroup_iter_end(cgrp, &it);
Ben Blum102a7752009-09-23 15:56:26 -07002544 length = n;
2545 /* now sort & (if procs) strip out duplicates */
2546 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07002547 if (type == CGROUP_FILE_PROCS)
Ben Blum102a7752009-09-23 15:56:26 -07002548 length = pidlist_uniq(&array, length);
Ben Blum72a8cb32009-09-23 15:56:27 -07002549 l = cgroup_pidlist_find(cgrp, type);
2550 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07002551 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07002552 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07002553 }
Ben Blum72a8cb32009-09-23 15:56:27 -07002554 /* store array, freeing old if necessary - lock already held */
Ben Blumd1d9fd32009-09-23 15:56:28 -07002555 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07002556 l->list = array;
2557 l->length = length;
2558 l->use_count++;
2559 up_write(&l->mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07002560 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07002561 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002562}
2563
Balbir Singh846c7bb2007-10-18 23:39:44 -07002564/**
Li Zefana043e3b2008-02-23 15:24:09 -08002565 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07002566 * @stats: cgroupstats to fill information into
2567 * @dentry: A dentry entry belonging to the cgroup for which stats have
2568 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08002569 *
2570 * Build and fill cgroupstats so that taskstats can export it to user
2571 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07002572 */
2573int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
2574{
2575 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07002576 struct cgroup *cgrp;
Balbir Singh846c7bb2007-10-18 23:39:44 -07002577 struct cgroup_iter it;
2578 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08002579
Balbir Singh846c7bb2007-10-18 23:39:44 -07002580 /*
Li Zefan33d283b2008-11-19 15:36:48 -08002581 * Validate dentry by checking the superblock operations,
2582 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07002583 */
Li Zefan33d283b2008-11-19 15:36:48 -08002584 if (dentry->d_sb->s_op != &cgroup_ops ||
2585 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07002586 goto err;
2587
2588 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07002589 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07002590
Paul Menagebd89aab2007-10-18 23:40:44 -07002591 cgroup_iter_start(cgrp, &it);
2592 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07002593 switch (tsk->state) {
2594 case TASK_RUNNING:
2595 stats->nr_running++;
2596 break;
2597 case TASK_INTERRUPTIBLE:
2598 stats->nr_sleeping++;
2599 break;
2600 case TASK_UNINTERRUPTIBLE:
2601 stats->nr_uninterruptible++;
2602 break;
2603 case TASK_STOPPED:
2604 stats->nr_stopped++;
2605 break;
2606 default:
2607 if (delayacct_is_task_waiting_on_io(tsk))
2608 stats->nr_io_wait++;
2609 break;
2610 }
2611 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002612 cgroup_iter_end(cgrp, &it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07002613
Balbir Singh846c7bb2007-10-18 23:39:44 -07002614err:
2615 return ret;
2616}
2617
Paul Menage8f3ff202009-09-23 15:56:25 -07002618
Paul Menagecc31edc2008-10-18 20:28:04 -07002619/*
Ben Blum102a7752009-09-23 15:56:26 -07002620 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07002621 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07002622 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07002623 */
2624
Ben Blum102a7752009-09-23 15:56:26 -07002625static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07002626{
2627 /*
2628 * Initially we receive a position value that corresponds to
2629 * one more than the last pid shown (or 0 on the first call or
2630 * after a seek to the start). Use a binary-search to find the
2631 * next pid to display, if any
2632 */
Ben Blum102a7752009-09-23 15:56:26 -07002633 struct cgroup_pidlist *l = s->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07002634 int index = 0, pid = *pos;
2635 int *iter;
2636
Ben Blum102a7752009-09-23 15:56:26 -07002637 down_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07002638 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07002639 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11002640
Paul Menagecc31edc2008-10-18 20:28:04 -07002641 while (index < end) {
2642 int mid = (index + end) / 2;
Ben Blum102a7752009-09-23 15:56:26 -07002643 if (l->list[mid] == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07002644 index = mid;
2645 break;
Ben Blum102a7752009-09-23 15:56:26 -07002646 } else if (l->list[mid] <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07002647 index = mid + 1;
2648 else
2649 end = mid;
2650 }
2651 }
2652 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07002653 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07002654 return NULL;
2655 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07002656 iter = l->list + index;
Paul Menagecc31edc2008-10-18 20:28:04 -07002657 *pos = *iter;
2658 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002659}
2660
Ben Blum102a7752009-09-23 15:56:26 -07002661static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07002662{
Ben Blum102a7752009-09-23 15:56:26 -07002663 struct cgroup_pidlist *l = s->private;
2664 up_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07002665}
2666
Ben Blum102a7752009-09-23 15:56:26 -07002667static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07002668{
Ben Blum102a7752009-09-23 15:56:26 -07002669 struct cgroup_pidlist *l = s->private;
2670 pid_t *p = v;
2671 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07002672 /*
2673 * Advance to the next pid in the array. If this goes off the
2674 * end, we're done
2675 */
2676 p++;
2677 if (p >= end) {
2678 return NULL;
2679 } else {
2680 *pos = *p;
2681 return p;
2682 }
2683}
2684
Ben Blum102a7752009-09-23 15:56:26 -07002685static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07002686{
2687 return seq_printf(s, "%d\n", *(int *)v);
2688}
2689
Ben Blum102a7752009-09-23 15:56:26 -07002690/*
2691 * seq_operations functions for iterating on pidlists through seq_file -
2692 * independent of whether it's tasks or procs
2693 */
2694static const struct seq_operations cgroup_pidlist_seq_operations = {
2695 .start = cgroup_pidlist_start,
2696 .stop = cgroup_pidlist_stop,
2697 .next = cgroup_pidlist_next,
2698 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07002699};
2700
Ben Blum102a7752009-09-23 15:56:26 -07002701static void cgroup_release_pid_array(struct cgroup_pidlist *l)
Paul Menagecc31edc2008-10-18 20:28:04 -07002702{
Ben Blum72a8cb32009-09-23 15:56:27 -07002703 /*
2704 * the case where we're the last user of this particular pidlist will
2705 * have us remove it from the cgroup's list, which entails taking the
2706 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
2707 * pidlist_mutex, we have to take pidlist_mutex first.
2708 */
2709 mutex_lock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07002710 down_write(&l->mutex);
2711 BUG_ON(!l->use_count);
2712 if (!--l->use_count) {
Ben Blum72a8cb32009-09-23 15:56:27 -07002713 /* we're the last user if refcount is 0; remove and free */
2714 list_del(&l->links);
2715 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07002716 pidlist_free(l->list);
Ben Blum72a8cb32009-09-23 15:56:27 -07002717 put_pid_ns(l->key.ns);
2718 up_write(&l->mutex);
2719 kfree(l);
2720 return;
Paul Menagecc31edc2008-10-18 20:28:04 -07002721 }
Ben Blum72a8cb32009-09-23 15:56:27 -07002722 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07002723 up_write(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07002724}
2725
Ben Blum102a7752009-09-23 15:56:26 -07002726static int cgroup_pidlist_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002727{
Ben Blum102a7752009-09-23 15:56:26 -07002728 struct cgroup_pidlist *l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002729 if (!(file->f_mode & FMODE_READ))
2730 return 0;
Ben Blum102a7752009-09-23 15:56:26 -07002731 /*
2732 * the seq_file will only be initialized if the file was opened for
2733 * reading; hence we check if it's not null only in that case.
2734 */
2735 l = ((struct seq_file *)file->private_data)->private;
2736 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07002737 return seq_release(inode, file);
2738}
2739
Ben Blum102a7752009-09-23 15:56:26 -07002740static const struct file_operations cgroup_pidlist_operations = {
Paul Menagecc31edc2008-10-18 20:28:04 -07002741 .read = seq_read,
2742 .llseek = seq_lseek,
2743 .write = cgroup_file_write,
Ben Blum102a7752009-09-23 15:56:26 -07002744 .release = cgroup_pidlist_release,
Paul Menagecc31edc2008-10-18 20:28:04 -07002745};
2746
2747/*
Ben Blum102a7752009-09-23 15:56:26 -07002748 * The following functions handle opens on a file that displays a pidlist
2749 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
2750 * in the cgroup.
Paul Menagecc31edc2008-10-18 20:28:04 -07002751 */
Ben Blum102a7752009-09-23 15:56:26 -07002752/* helper function for the two below it */
Ben Blum72a8cb32009-09-23 15:56:27 -07002753static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
Paul Menagecc31edc2008-10-18 20:28:04 -07002754{
2755 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Ben Blum72a8cb32009-09-23 15:56:27 -07002756 struct cgroup_pidlist *l;
Paul Menagecc31edc2008-10-18 20:28:04 -07002757 int retval;
2758
2759 /* Nothing to do for write-only files */
2760 if (!(file->f_mode & FMODE_READ))
2761 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002762
Ben Blum102a7752009-09-23 15:56:26 -07002763 /* have the array populated */
Ben Blum72a8cb32009-09-23 15:56:27 -07002764 retval = pidlist_array_load(cgrp, type, &l);
Ben Blum102a7752009-09-23 15:56:26 -07002765 if (retval)
2766 return retval;
2767 /* configure file information */
2768 file->f_op = &cgroup_pidlist_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002769
Ben Blum102a7752009-09-23 15:56:26 -07002770 retval = seq_open(file, &cgroup_pidlist_seq_operations);
Paul Menagecc31edc2008-10-18 20:28:04 -07002771 if (retval) {
Ben Blum102a7752009-09-23 15:56:26 -07002772 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07002773 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002774 }
Ben Blum102a7752009-09-23 15:56:26 -07002775 ((struct seq_file *)file->private_data)->private = l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002776 return 0;
2777}
Ben Blum102a7752009-09-23 15:56:26 -07002778static int cgroup_tasks_open(struct inode *unused, struct file *file)
2779{
Ben Blum72a8cb32009-09-23 15:56:27 -07002780 return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
Ben Blum102a7752009-09-23 15:56:26 -07002781}
2782static int cgroup_procs_open(struct inode *unused, struct file *file)
2783{
Ben Blum72a8cb32009-09-23 15:56:27 -07002784 return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
Ben Blum102a7752009-09-23 15:56:26 -07002785}
Paul Menagebbcb81d2007-10-18 23:39:32 -07002786
Paul Menagebd89aab2007-10-18 23:40:44 -07002787static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
Paul Menage81a6a5c2007-10-18 23:39:38 -07002788 struct cftype *cft)
2789{
Paul Menagebd89aab2007-10-18 23:40:44 -07002790 return notify_on_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07002791}
2792
Paul Menage6379c102008-07-25 01:47:01 -07002793static int cgroup_write_notify_on_release(struct cgroup *cgrp,
2794 struct cftype *cft,
2795 u64 val)
2796{
2797 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
2798 if (val)
2799 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
2800 else
2801 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
2802 return 0;
2803}
2804
Paul Menagebbcb81d2007-10-18 23:39:32 -07002805/*
2806 * for the common functions, 'private' gives the type of file
2807 */
Ben Blum102a7752009-09-23 15:56:26 -07002808/* for hysterical raisins, we can't put this on the older files */
2809#define CGROUP_FILE_GENERIC_PREFIX "cgroup."
Paul Menage81a6a5c2007-10-18 23:39:38 -07002810static struct cftype files[] = {
2811 {
2812 .name = "tasks",
2813 .open = cgroup_tasks_open,
Paul Menageaf351022008-07-25 01:47:01 -07002814 .write_u64 = cgroup_tasks_write,
Ben Blum102a7752009-09-23 15:56:26 -07002815 .release = cgroup_pidlist_release,
Li Zefan099fca32009-04-02 16:57:29 -07002816 .mode = S_IRUGO | S_IWUSR,
Paul Menage81a6a5c2007-10-18 23:39:38 -07002817 },
Ben Blum102a7752009-09-23 15:56:26 -07002818 {
2819 .name = CGROUP_FILE_GENERIC_PREFIX "procs",
2820 .open = cgroup_procs_open,
2821 /* .write_u64 = cgroup_procs_write, TODO */
2822 .release = cgroup_pidlist_release,
2823 .mode = S_IRUGO,
2824 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07002825 {
2826 .name = "notify_on_release",
Paul Menagef4c753b2008-04-29 00:59:56 -07002827 .read_u64 = cgroup_read_notify_on_release,
Paul Menage6379c102008-07-25 01:47:01 -07002828 .write_u64 = cgroup_write_notify_on_release,
Paul Menage81a6a5c2007-10-18 23:39:38 -07002829 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07002830};
2831
2832static struct cftype cft_release_agent = {
2833 .name = "release_agent",
Paul Menagee788e0662008-07-25 01:46:59 -07002834 .read_seq_string = cgroup_release_agent_show,
2835 .write_string = cgroup_release_agent_write,
2836 .max_write_len = PATH_MAX,
Paul Menagebbcb81d2007-10-18 23:39:32 -07002837};
2838
Paul Menagebd89aab2007-10-18 23:40:44 -07002839static int cgroup_populate_dir(struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002840{
2841 int err;
2842 struct cgroup_subsys *ss;
2843
2844 /* First clear out any existing files */
Paul Menagebd89aab2007-10-18 23:40:44 -07002845 cgroup_clear_directory(cgrp->dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002846
Paul Menagebd89aab2007-10-18 23:40:44 -07002847 err = cgroup_add_files(cgrp, NULL, files, ARRAY_SIZE(files));
Paul Menagebbcb81d2007-10-18 23:39:32 -07002848 if (err < 0)
2849 return err;
2850
Paul Menagebd89aab2007-10-18 23:40:44 -07002851 if (cgrp == cgrp->top_cgroup) {
2852 if ((err = cgroup_add_file(cgrp, NULL, &cft_release_agent)) < 0)
Paul Menage81a6a5c2007-10-18 23:39:38 -07002853 return err;
2854 }
2855
Paul Menagebd89aab2007-10-18 23:40:44 -07002856 for_each_subsys(cgrp->root, ss) {
2857 if (ss->populate && (err = ss->populate(ss, cgrp)) < 0)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002858 return err;
2859 }
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07002860 /* This cgroup is ready now */
2861 for_each_subsys(cgrp->root, ss) {
2862 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
2863 /*
2864 * Update id->css pointer and make this css visible from
2865 * CSS ID functions. This pointer will be dereferened
2866 * from RCU-read-side without locks.
2867 */
2868 if (css->id)
2869 rcu_assign_pointer(css->id->css, css);
2870 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002871
2872 return 0;
2873}
2874
2875static void init_cgroup_css(struct cgroup_subsys_state *css,
2876 struct cgroup_subsys *ss,
Paul Menagebd89aab2007-10-18 23:40:44 -07002877 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002878{
Paul Menagebd89aab2007-10-18 23:40:44 -07002879 css->cgroup = cgrp;
Paul Menagee7c5ec92009-01-07 18:08:38 -08002880 atomic_set(&css->refcnt, 1);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002881 css->flags = 0;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07002882 css->id = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07002883 if (cgrp == dummytop)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002884 set_bit(CSS_ROOT, &css->flags);
Paul Menagebd89aab2007-10-18 23:40:44 -07002885 BUG_ON(cgrp->subsys[ss->subsys_id]);
2886 cgrp->subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002887}
2888
Paul Menage999cd8a2009-01-07 18:08:36 -08002889static void cgroup_lock_hierarchy(struct cgroupfs_root *root)
2890{
2891 /* We need to take each hierarchy_mutex in a consistent order */
2892 int i;
2893
2894 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2895 struct cgroup_subsys *ss = subsys[i];
2896 if (ss->root == root)
Li Zefancfebe562009-02-11 13:04:36 -08002897 mutex_lock(&ss->hierarchy_mutex);
Paul Menage999cd8a2009-01-07 18:08:36 -08002898 }
2899}
2900
2901static void cgroup_unlock_hierarchy(struct cgroupfs_root *root)
2902{
2903 int i;
2904
2905 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2906 struct cgroup_subsys *ss = subsys[i];
2907 if (ss->root == root)
2908 mutex_unlock(&ss->hierarchy_mutex);
2909 }
2910}
2911
Paul Menageddbcc7e2007-10-18 23:39:30 -07002912/*
Li Zefana043e3b2008-02-23 15:24:09 -08002913 * cgroup_create - create a cgroup
2914 * @parent: cgroup that will be parent of the new cgroup
2915 * @dentry: dentry of the new cgroup
2916 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07002917 *
Li Zefana043e3b2008-02-23 15:24:09 -08002918 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07002919 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07002920static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Li Zefan099fca32009-04-02 16:57:29 -07002921 mode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002922{
Paul Menagebd89aab2007-10-18 23:40:44 -07002923 struct cgroup *cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002924 struct cgroupfs_root *root = parent->root;
2925 int err = 0;
2926 struct cgroup_subsys *ss;
2927 struct super_block *sb = root->sb;
2928
Paul Menagebd89aab2007-10-18 23:40:44 -07002929 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
2930 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002931 return -ENOMEM;
2932
2933 /* Grab a reference on the superblock so the hierarchy doesn't
2934 * get deleted on unmount if there are child cgroups. This
2935 * can be done outside cgroup_mutex, since the sb can't
2936 * disappear while someone has an open control file on the
2937 * fs */
2938 atomic_inc(&sb->s_active);
2939
2940 mutex_lock(&cgroup_mutex);
2941
Paul Menagecc31edc2008-10-18 20:28:04 -07002942 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002943
Paul Menagebd89aab2007-10-18 23:40:44 -07002944 cgrp->parent = parent;
2945 cgrp->root = parent->root;
2946 cgrp->top_cgroup = parent->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002947
Li Zefanb6abdb02008-03-04 14:28:19 -08002948 if (notify_on_release(parent))
2949 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
2950
Paul Menageddbcc7e2007-10-18 23:39:30 -07002951 for_each_subsys(root, ss) {
Paul Menagebd89aab2007-10-18 23:40:44 -07002952 struct cgroup_subsys_state *css = ss->create(ss, cgrp);
Li Zefan4528fd02010-02-02 13:44:10 -08002953
Paul Menageddbcc7e2007-10-18 23:39:30 -07002954 if (IS_ERR(css)) {
2955 err = PTR_ERR(css);
2956 goto err_destroy;
2957 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002958 init_cgroup_css(css, ss, cgrp);
Li Zefan4528fd02010-02-02 13:44:10 -08002959 if (ss->use_id) {
2960 err = alloc_css_id(ss, parent, cgrp);
2961 if (err)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07002962 goto err_destroy;
Li Zefan4528fd02010-02-02 13:44:10 -08002963 }
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07002964 /* At error, ->destroy() callback has to free assigned ID. */
Paul Menageddbcc7e2007-10-18 23:39:30 -07002965 }
2966
Paul Menage999cd8a2009-01-07 18:08:36 -08002967 cgroup_lock_hierarchy(root);
Paul Menagebd89aab2007-10-18 23:40:44 -07002968 list_add(&cgrp->sibling, &cgrp->parent->children);
Paul Menage999cd8a2009-01-07 18:08:36 -08002969 cgroup_unlock_hierarchy(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002970 root->number_of_cgroups++;
2971
Paul Menagebd89aab2007-10-18 23:40:44 -07002972 err = cgroup_create_dir(cgrp, dentry, mode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002973 if (err < 0)
2974 goto err_remove;
2975
2976 /* The cgroup directory was pre-locked for us */
Paul Menagebd89aab2007-10-18 23:40:44 -07002977 BUG_ON(!mutex_is_locked(&cgrp->dentry->d_inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002978
Paul Menagebd89aab2007-10-18 23:40:44 -07002979 err = cgroup_populate_dir(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002980 /* If err < 0, we have a half-filled directory - oh well ;) */
2981
2982 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07002983 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002984
2985 return 0;
2986
2987 err_remove:
2988
KAMEZAWA Hiroyukibaef99a2009-01-29 14:25:10 -08002989 cgroup_lock_hierarchy(root);
Paul Menagebd89aab2007-10-18 23:40:44 -07002990 list_del(&cgrp->sibling);
KAMEZAWA Hiroyukibaef99a2009-01-29 14:25:10 -08002991 cgroup_unlock_hierarchy(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002992 root->number_of_cgroups--;
2993
2994 err_destroy:
2995
2996 for_each_subsys(root, ss) {
Paul Menagebd89aab2007-10-18 23:40:44 -07002997 if (cgrp->subsys[ss->subsys_id])
2998 ss->destroy(ss, cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002999 }
3000
3001 mutex_unlock(&cgroup_mutex);
3002
3003 /* Release the reference count that we took on the superblock */
3004 deactivate_super(sb);
3005
Paul Menagebd89aab2007-10-18 23:40:44 -07003006 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003007 return err;
3008}
3009
3010static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode)
3011{
3012 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
3013
3014 /* the vfs holds inode->i_mutex already */
3015 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
3016}
3017
Li Zefan55b6fd02008-07-29 22:33:20 -07003018static int cgroup_has_css_refs(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003019{
3020 /* Check the reference count on each subsystem. Since we
3021 * already established that there are no tasks in the
Paul Menagee7c5ec92009-01-07 18:08:38 -08003022 * cgroup, if the css refcount is also 1, then there should
Paul Menage81a6a5c2007-10-18 23:39:38 -07003023 * be no outstanding references, so the subsystem is safe to
3024 * destroy. We scan across all subsystems rather than using
3025 * the per-hierarchy linked list of mounted subsystems since
3026 * we can be called via check_for_release() with no
3027 * synchronization other than RCU, and the subsystem linked
3028 * list isn't RCU-safe */
3029 int i;
3030 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3031 struct cgroup_subsys *ss = subsys[i];
3032 struct cgroup_subsys_state *css;
3033 /* Skip subsystems not in this hierarchy */
Paul Menagebd89aab2007-10-18 23:40:44 -07003034 if (ss->root != cgrp->root)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003035 continue;
Paul Menagebd89aab2007-10-18 23:40:44 -07003036 css = cgrp->subsys[ss->subsys_id];
Paul Menage81a6a5c2007-10-18 23:39:38 -07003037 /* When called from check_for_release() it's possible
3038 * that by this point the cgroup has been removed
3039 * and the css deleted. But a false-positive doesn't
3040 * matter, since it can only happen if the cgroup
3041 * has been deleted and hence no longer needs the
3042 * release agent to be called anyway. */
Paul Menagee7c5ec92009-01-07 18:08:38 -08003043 if (css && (atomic_read(&css->refcnt) > 1))
Paul Menage81a6a5c2007-10-18 23:39:38 -07003044 return 1;
Paul Menage81a6a5c2007-10-18 23:39:38 -07003045 }
3046 return 0;
3047}
3048
Paul Menagee7c5ec92009-01-07 18:08:38 -08003049/*
3050 * Atomically mark all (or else none) of the cgroup's CSS objects as
3051 * CSS_REMOVED. Return true on success, or false if the cgroup has
3052 * busy subsystems. Call with cgroup_mutex held
3053 */
3054
3055static int cgroup_clear_css_refs(struct cgroup *cgrp)
3056{
3057 struct cgroup_subsys *ss;
3058 unsigned long flags;
3059 bool failed = false;
3060 local_irq_save(flags);
3061 for_each_subsys(cgrp->root, ss) {
3062 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
3063 int refcnt;
Paul Menage804b3c22009-01-29 14:25:21 -08003064 while (1) {
Paul Menagee7c5ec92009-01-07 18:08:38 -08003065 /* We can only remove a CSS with a refcnt==1 */
3066 refcnt = atomic_read(&css->refcnt);
3067 if (refcnt > 1) {
3068 failed = true;
3069 goto done;
3070 }
3071 BUG_ON(!refcnt);
3072 /*
3073 * Drop the refcnt to 0 while we check other
3074 * subsystems. This will cause any racing
3075 * css_tryget() to spin until we set the
3076 * CSS_REMOVED bits or abort
3077 */
Paul Menage804b3c22009-01-29 14:25:21 -08003078 if (atomic_cmpxchg(&css->refcnt, refcnt, 0) == refcnt)
3079 break;
3080 cpu_relax();
3081 }
Paul Menagee7c5ec92009-01-07 18:08:38 -08003082 }
3083 done:
3084 for_each_subsys(cgrp->root, ss) {
3085 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
3086 if (failed) {
3087 /*
3088 * Restore old refcnt if we previously managed
3089 * to clear it from 1 to 0
3090 */
3091 if (!atomic_read(&css->refcnt))
3092 atomic_set(&css->refcnt, 1);
3093 } else {
3094 /* Commit the fact that the CSS is removed */
3095 set_bit(CSS_REMOVED, &css->flags);
3096 }
3097 }
3098 local_irq_restore(flags);
3099 return !failed;
3100}
3101
Paul Menageddbcc7e2007-10-18 23:39:30 -07003102static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
3103{
Paul Menagebd89aab2007-10-18 23:40:44 -07003104 struct cgroup *cgrp = dentry->d_fsdata;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003105 struct dentry *d;
3106 struct cgroup *parent;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003107 DEFINE_WAIT(wait);
3108 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003109
3110 /* the vfs holds both inode->i_mutex already */
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003111again:
Paul Menageddbcc7e2007-10-18 23:39:30 -07003112 mutex_lock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07003113 if (atomic_read(&cgrp->count) != 0) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07003114 mutex_unlock(&cgroup_mutex);
3115 return -EBUSY;
3116 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003117 if (!list_empty(&cgrp->children)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07003118 mutex_unlock(&cgroup_mutex);
3119 return -EBUSY;
3120 }
KAMEZAWA Hiroyuki3fa59df2008-11-19 15:36:34 -08003121 mutex_unlock(&cgroup_mutex);
Li Zefana043e3b2008-02-23 15:24:09 -08003122
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -08003123 /*
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003124 * In general, subsystem has no css->refcnt after pre_destroy(). But
3125 * in racy cases, subsystem may have to get css->refcnt after
3126 * pre_destroy() and it makes rmdir return with -EBUSY. This sometimes
3127 * make rmdir return -EBUSY too often. To avoid that, we use waitqueue
3128 * for cgroup's rmdir. CGRP_WAIT_ON_RMDIR is for synchronizing rmdir
3129 * and subsystem's reference count handling. Please see css_get/put
3130 * and css_tryget() and cgroup_wakeup_rmdir_waiter() implementation.
3131 */
3132 set_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
3133
3134 /*
Li Zefana043e3b2008-02-23 15:24:09 -08003135 * Call pre_destroy handlers of subsys. Notify subsystems
3136 * that rmdir() request comes.
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -08003137 */
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003138 ret = cgroup_call_pre_destroy(cgrp);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003139 if (ret) {
3140 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003141 return ret;
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003142 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003143
KAMEZAWA Hiroyuki3fa59df2008-11-19 15:36:34 -08003144 mutex_lock(&cgroup_mutex);
3145 parent = cgrp->parent;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003146 if (atomic_read(&cgrp->count) || !list_empty(&cgrp->children)) {
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003147 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003148 mutex_unlock(&cgroup_mutex);
3149 return -EBUSY;
3150 }
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003151 prepare_to_wait(&cgroup_rmdir_waitq, &wait, TASK_INTERRUPTIBLE);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003152 if (!cgroup_clear_css_refs(cgrp)) {
3153 mutex_unlock(&cgroup_mutex);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003154 /*
3155 * Because someone may call cgroup_wakeup_rmdir_waiter() before
3156 * prepare_to_wait(), we need to check this flag.
3157 */
3158 if (test_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags))
3159 schedule();
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003160 finish_wait(&cgroup_rmdir_waitq, &wait);
3161 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
3162 if (signal_pending(current))
3163 return -EINTR;
3164 goto again;
3165 }
3166 /* NO css_tryget() can success after here. */
3167 finish_wait(&cgroup_rmdir_waitq, &wait);
3168 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003169
Paul Menage81a6a5c2007-10-18 23:39:38 -07003170 spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07003171 set_bit(CGRP_REMOVED, &cgrp->flags);
3172 if (!list_empty(&cgrp->release_list))
3173 list_del(&cgrp->release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003174 spin_unlock(&release_list_lock);
Paul Menage999cd8a2009-01-07 18:08:36 -08003175
3176 cgroup_lock_hierarchy(cgrp->root);
3177 /* delete this cgroup from parent->children */
Paul Menagebd89aab2007-10-18 23:40:44 -07003178 list_del(&cgrp->sibling);
Paul Menage999cd8a2009-01-07 18:08:36 -08003179 cgroup_unlock_hierarchy(cgrp->root);
3180
Paul Menagebd89aab2007-10-18 23:40:44 -07003181 spin_lock(&cgrp->dentry->d_lock);
3182 d = dget(cgrp->dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003183 spin_unlock(&d->d_lock);
3184
3185 cgroup_d_remove_dir(d);
3186 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003187
Paul Menagebd89aab2007-10-18 23:40:44 -07003188 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003189 check_for_release(parent);
3190
Paul Menageddbcc7e2007-10-18 23:39:30 -07003191 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003192 return 0;
3193}
3194
Li Zefan06a11922008-04-29 01:00:07 -07003195static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003196{
Paul Menageddbcc7e2007-10-18 23:39:30 -07003197 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08003198
3199 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003200
3201 /* Create the top cgroup state for this subsystem */
Li Zefan33a68ac2009-01-07 18:07:42 -08003202 list_add(&ss->sibling, &rootnode.subsys_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003203 ss->root = &rootnode;
3204 css = ss->create(ss, dummytop);
3205 /* We don't handle early failures gracefully */
3206 BUG_ON(IS_ERR(css));
3207 init_cgroup_css(css, ss, dummytop);
3208
Li Zefane8d55fd2008-04-29 01:00:13 -07003209 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07003210 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07003211 * newly registered, all tasks and hence the
3212 * init_css_set is in the subsystem's top cgroup. */
3213 init_css_set.subsys[ss->subsys_id] = dummytop->subsys[ss->subsys_id];
Paul Menageddbcc7e2007-10-18 23:39:30 -07003214
3215 need_forkexit_callback |= ss->fork || ss->exit;
3216
Li Zefane8d55fd2008-04-29 01:00:13 -07003217 /* At system boot, before all subsystems have been
3218 * registered, no tasks have been forked, so we don't
3219 * need to invoke fork callbacks here. */
3220 BUG_ON(!list_empty(&init_task.tasks));
3221
Paul Menage999cd8a2009-01-07 18:08:36 -08003222 mutex_init(&ss->hierarchy_mutex);
Li Zefancfebe562009-02-11 13:04:36 -08003223 lockdep_set_class(&ss->hierarchy_mutex, &ss->subsys_key);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003224 ss->active = 1;
3225}
3226
3227/**
Li Zefana043e3b2008-02-23 15:24:09 -08003228 * cgroup_init_early - cgroup initialization at system boot
3229 *
3230 * Initialize cgroups at system boot, and initialize any
3231 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07003232 */
3233int __init cgroup_init_early(void)
3234{
3235 int i;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07003236 atomic_set(&init_css_set.refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -07003237 INIT_LIST_HEAD(&init_css_set.cg_links);
3238 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07003239 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07003240 css_set_count = 1;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003241 init_cgroup_root(&rootnode);
Paul Menage817929e2007-10-18 23:39:36 -07003242 root_count = 1;
3243 init_task.cgroups = &init_css_set;
3244
3245 init_css_set_link.cg = &init_css_set;
Paul Menage7717f7b2009-09-23 15:56:22 -07003246 init_css_set_link.cgrp = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -07003247 list_add(&init_css_set_link.cgrp_link_list,
Paul Menage817929e2007-10-18 23:39:36 -07003248 &rootnode.top_cgroup.css_sets);
3249 list_add(&init_css_set_link.cg_link_list,
3250 &init_css_set.cg_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003251
Li Zefan472b1052008-04-29 01:00:11 -07003252 for (i = 0; i < CSS_SET_TABLE_SIZE; i++)
3253 INIT_HLIST_HEAD(&css_set_table[i]);
3254
Paul Menageddbcc7e2007-10-18 23:39:30 -07003255 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3256 struct cgroup_subsys *ss = subsys[i];
3257
3258 BUG_ON(!ss->name);
3259 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
3260 BUG_ON(!ss->create);
3261 BUG_ON(!ss->destroy);
3262 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08003263 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07003264 ss->name, ss->subsys_id);
3265 BUG();
3266 }
3267
3268 if (ss->early_init)
3269 cgroup_init_subsys(ss);
3270 }
3271 return 0;
3272}
3273
3274/**
Li Zefana043e3b2008-02-23 15:24:09 -08003275 * cgroup_init - cgroup initialization
3276 *
3277 * Register cgroup filesystem and /proc file, and initialize
3278 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07003279 */
3280int __init cgroup_init(void)
3281{
3282 int err;
3283 int i;
Li Zefan472b1052008-04-29 01:00:11 -07003284 struct hlist_head *hhead;
Paul Menagea4243162007-10-18 23:39:35 -07003285
3286 err = bdi_init(&cgroup_backing_dev_info);
3287 if (err)
3288 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003289
3290 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3291 struct cgroup_subsys *ss = subsys[i];
3292 if (!ss->early_init)
3293 cgroup_init_subsys(ss);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07003294 if (ss->use_id)
3295 cgroup_subsys_init_idr(ss);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003296 }
3297
Li Zefan472b1052008-04-29 01:00:11 -07003298 /* Add init_css_set to the hash table */
3299 hhead = css_set_hash(init_css_set.subsys);
3300 hlist_add_head(&init_css_set.hlist, hhead);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07003301 BUG_ON(!init_root_id(&rootnode));
Paul Menageddbcc7e2007-10-18 23:39:30 -07003302 err = register_filesystem(&cgroup_fs_type);
3303 if (err < 0)
3304 goto out;
3305
Li Zefan46ae2202008-04-29 01:00:08 -07003306 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07003307
Paul Menageddbcc7e2007-10-18 23:39:30 -07003308out:
Paul Menagea4243162007-10-18 23:39:35 -07003309 if (err)
3310 bdi_destroy(&cgroup_backing_dev_info);
3311
Paul Menageddbcc7e2007-10-18 23:39:30 -07003312 return err;
3313}
Paul Menageb4f48b62007-10-18 23:39:33 -07003314
Paul Menagea4243162007-10-18 23:39:35 -07003315/*
3316 * proc_cgroup_show()
3317 * - Print task's cgroup paths into seq_file, one line for each hierarchy
3318 * - Used for /proc/<pid>/cgroup.
3319 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
3320 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08003321 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07003322 * anyway. No need to check that tsk->cgroup != NULL, thanks to
3323 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
3324 * cgroup to top_cgroup.
3325 */
3326
3327/* TODO: Use a proper seq_file iterator */
3328static int proc_cgroup_show(struct seq_file *m, void *v)
3329{
3330 struct pid *pid;
3331 struct task_struct *tsk;
3332 char *buf;
3333 int retval;
3334 struct cgroupfs_root *root;
3335
3336 retval = -ENOMEM;
3337 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3338 if (!buf)
3339 goto out;
3340
3341 retval = -ESRCH;
3342 pid = m->private;
3343 tsk = get_pid_task(pid, PIDTYPE_PID);
3344 if (!tsk)
3345 goto out_free;
3346
3347 retval = 0;
3348
3349 mutex_lock(&cgroup_mutex);
3350
Li Zefane5f6a862009-01-07 18:07:41 -08003351 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07003352 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07003353 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07003354 int count = 0;
3355
Paul Menage2c6ab6d2009-09-23 15:56:23 -07003356 seq_printf(m, "%d:", root->hierarchy_id);
Paul Menagea4243162007-10-18 23:39:35 -07003357 for_each_subsys(root, ss)
3358 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07003359 if (strlen(root->name))
3360 seq_printf(m, "%sname=%s", count ? "," : "",
3361 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07003362 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07003363 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07003364 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07003365 if (retval < 0)
3366 goto out_unlock;
3367 seq_puts(m, buf);
3368 seq_putc(m, '\n');
3369 }
3370
3371out_unlock:
3372 mutex_unlock(&cgroup_mutex);
3373 put_task_struct(tsk);
3374out_free:
3375 kfree(buf);
3376out:
3377 return retval;
3378}
3379
3380static int cgroup_open(struct inode *inode, struct file *file)
3381{
3382 struct pid *pid = PROC_I(inode)->pid;
3383 return single_open(file, proc_cgroup_show, pid);
3384}
3385
Alexey Dobriyan828c0952009-10-01 15:43:56 -07003386const struct file_operations proc_cgroup_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07003387 .open = cgroup_open,
3388 .read = seq_read,
3389 .llseek = seq_lseek,
3390 .release = single_release,
3391};
3392
3393/* Display information about each subsystem and each hierarchy */
3394static int proc_cgroupstats_show(struct seq_file *m, void *v)
3395{
3396 int i;
Paul Menagea4243162007-10-18 23:39:35 -07003397
Paul Menage8bab8dd2008-04-04 14:29:57 -07003398 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Paul Menagea4243162007-10-18 23:39:35 -07003399 mutex_lock(&cgroup_mutex);
Paul Menagea4243162007-10-18 23:39:35 -07003400 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3401 struct cgroup_subsys *ss = subsys[i];
Paul Menage2c6ab6d2009-09-23 15:56:23 -07003402 seq_printf(m, "%s\t%d\t%d\t%d\n",
3403 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07003404 ss->root->number_of_cgroups, !ss->disabled);
Paul Menagea4243162007-10-18 23:39:35 -07003405 }
3406 mutex_unlock(&cgroup_mutex);
3407 return 0;
3408}
3409
3410static int cgroupstats_open(struct inode *inode, struct file *file)
3411{
Al Viro9dce07f2008-03-29 03:07:28 +00003412 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07003413}
3414
Alexey Dobriyan828c0952009-10-01 15:43:56 -07003415static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07003416 .open = cgroupstats_open,
3417 .read = seq_read,
3418 .llseek = seq_lseek,
3419 .release = single_release,
3420};
3421
Paul Menageb4f48b62007-10-18 23:39:33 -07003422/**
3423 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08003424 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07003425 *
3426 * Description: A task inherits its parent's cgroup at fork().
3427 *
3428 * A pointer to the shared css_set was automatically copied in
3429 * fork.c by dup_task_struct(). However, we ignore that copy, since
3430 * it was not made under the protection of RCU or cgroup_mutex, so
Cliff Wickman956db3c2008-02-07 00:14:43 -08003431 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
Paul Menage817929e2007-10-18 23:39:36 -07003432 * have already changed current->cgroups, allowing the previously
3433 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07003434 *
3435 * At the point that cgroup_fork() is called, 'current' is the parent
3436 * task, and the passed argument 'child' points to the child task.
3437 */
3438void cgroup_fork(struct task_struct *child)
3439{
Paul Menage817929e2007-10-18 23:39:36 -07003440 task_lock(current);
3441 child->cgroups = current->cgroups;
3442 get_css_set(child->cgroups);
3443 task_unlock(current);
3444 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07003445}
3446
3447/**
Li Zefana043e3b2008-02-23 15:24:09 -08003448 * cgroup_fork_callbacks - run fork callbacks
3449 * @child: the new task
3450 *
3451 * Called on a new task very soon before adding it to the
3452 * tasklist. No need to take any locks since no-one can
3453 * be operating on this task.
Paul Menageb4f48b62007-10-18 23:39:33 -07003454 */
3455void cgroup_fork_callbacks(struct task_struct *child)
3456{
3457 if (need_forkexit_callback) {
3458 int i;
3459 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3460 struct cgroup_subsys *ss = subsys[i];
3461 if (ss->fork)
3462 ss->fork(ss, child);
3463 }
3464 }
3465}
3466
3467/**
Li Zefana043e3b2008-02-23 15:24:09 -08003468 * cgroup_post_fork - called on a new task after adding it to the task list
3469 * @child: the task in question
3470 *
3471 * Adds the task to the list running through its css_set if necessary.
3472 * Has to be after the task is visible on the task list in case we race
3473 * with the first call to cgroup_iter_start() - to guarantee that the
3474 * new task ends up on its list.
3475 */
Paul Menage817929e2007-10-18 23:39:36 -07003476void cgroup_post_fork(struct task_struct *child)
3477{
3478 if (use_task_css_set_links) {
3479 write_lock(&css_set_lock);
Lai Jiangshanb12b5332009-01-07 18:07:36 -08003480 task_lock(child);
Paul Menage817929e2007-10-18 23:39:36 -07003481 if (list_empty(&child->cg_list))
3482 list_add(&child->cg_list, &child->cgroups->tasks);
Lai Jiangshanb12b5332009-01-07 18:07:36 -08003483 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07003484 write_unlock(&css_set_lock);
3485 }
3486}
3487/**
Paul Menageb4f48b62007-10-18 23:39:33 -07003488 * cgroup_exit - detach cgroup from exiting task
3489 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08003490 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07003491 *
3492 * Description: Detach cgroup from @tsk and release it.
3493 *
3494 * Note that cgroups marked notify_on_release force every task in
3495 * them to take the global cgroup_mutex mutex when exiting.
3496 * This could impact scaling on very large systems. Be reluctant to
3497 * use notify_on_release cgroups where very high task exit scaling
3498 * is required on large systems.
3499 *
3500 * the_top_cgroup_hack:
3501 *
3502 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
3503 *
3504 * We call cgroup_exit() while the task is still competent to
3505 * handle notify_on_release(), then leave the task attached to the
3506 * root cgroup in each hierarchy for the remainder of its exit.
3507 *
3508 * To do this properly, we would increment the reference count on
3509 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
3510 * code we would add a second cgroup function call, to drop that
3511 * reference. This would just create an unnecessary hot spot on
3512 * the top_cgroup reference count, to no avail.
3513 *
3514 * Normally, holding a reference to a cgroup without bumping its
3515 * count is unsafe. The cgroup could go away, or someone could
3516 * attach us to a different cgroup, decrementing the count on
3517 * the first cgroup that we never incremented. But in this case,
3518 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08003519 * which wards off any cgroup_attach_task() attempts, or task is a failed
3520 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07003521 */
3522void cgroup_exit(struct task_struct *tsk, int run_callbacks)
3523{
3524 int i;
Paul Menage817929e2007-10-18 23:39:36 -07003525 struct css_set *cg;
Paul Menageb4f48b62007-10-18 23:39:33 -07003526
3527 if (run_callbacks && need_forkexit_callback) {
3528 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3529 struct cgroup_subsys *ss = subsys[i];
3530 if (ss->exit)
3531 ss->exit(ss, tsk);
3532 }
3533 }
Paul Menage817929e2007-10-18 23:39:36 -07003534
3535 /*
3536 * Unlink from the css_set task list if necessary.
3537 * Optimistically check cg_list before taking
3538 * css_set_lock
3539 */
3540 if (!list_empty(&tsk->cg_list)) {
3541 write_lock(&css_set_lock);
3542 if (!list_empty(&tsk->cg_list))
3543 list_del(&tsk->cg_list);
3544 write_unlock(&css_set_lock);
3545 }
3546
Paul Menageb4f48b62007-10-18 23:39:33 -07003547 /* Reassign the task to the init_css_set. */
3548 task_lock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07003549 cg = tsk->cgroups;
3550 tsk->cgroups = &init_css_set;
Paul Menageb4f48b62007-10-18 23:39:33 -07003551 task_unlock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07003552 if (cg)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003553 put_css_set_taskexit(cg);
Paul Menageb4f48b62007-10-18 23:39:33 -07003554}
Paul Menage697f4162007-10-18 23:39:34 -07003555
3556/**
Li Zefana043e3b2008-02-23 15:24:09 -08003557 * cgroup_clone - clone the cgroup the given subsystem is attached to
3558 * @tsk: the task to be moved
3559 * @subsys: the given subsystem
Serge E. Hallyne885dcd2008-07-25 01:47:06 -07003560 * @nodename: the name for the new cgroup
Li Zefana043e3b2008-02-23 15:24:09 -08003561 *
3562 * Duplicate the current cgroup in the hierarchy that the given
3563 * subsystem is attached to, and move this task into the new
3564 * child.
Paul Menage697f4162007-10-18 23:39:34 -07003565 */
Serge E. Hallyne885dcd2008-07-25 01:47:06 -07003566int cgroup_clone(struct task_struct *tsk, struct cgroup_subsys *subsys,
3567 char *nodename)
Paul Menage697f4162007-10-18 23:39:34 -07003568{
3569 struct dentry *dentry;
3570 int ret = 0;
Paul Menage697f4162007-10-18 23:39:34 -07003571 struct cgroup *parent, *child;
3572 struct inode *inode;
3573 struct css_set *cg;
3574 struct cgroupfs_root *root;
3575 struct cgroup_subsys *ss;
3576
3577 /* We shouldn't be called by an unregistered subsystem */
3578 BUG_ON(!subsys->active);
3579
3580 /* First figure out what hierarchy and cgroup we're dealing
3581 * with, and pin them so we can drop cgroup_mutex */
3582 mutex_lock(&cgroup_mutex);
3583 again:
3584 root = subsys->root;
3585 if (root == &rootnode) {
Paul Menage697f4162007-10-18 23:39:34 -07003586 mutex_unlock(&cgroup_mutex);
3587 return 0;
3588 }
Paul Menage697f4162007-10-18 23:39:34 -07003589
Paul Menage697f4162007-10-18 23:39:34 -07003590 /* Pin the hierarchy */
Li Zefan1404f062009-01-29 14:25:21 -08003591 if (!atomic_inc_not_zero(&root->sb->s_active)) {
Li Zefan7b574b72009-01-04 12:00:45 -08003592 /* We race with the final deactivate_super() */
3593 mutex_unlock(&cgroup_mutex);
3594 return 0;
3595 }
Paul Menage697f4162007-10-18 23:39:34 -07003596
Paul Menage817929e2007-10-18 23:39:36 -07003597 /* Keep the cgroup alive */
Li Zefan1404f062009-01-29 14:25:21 -08003598 task_lock(tsk);
3599 parent = task_cgroup(tsk, subsys->subsys_id);
3600 cg = tsk->cgroups;
Paul Menage817929e2007-10-18 23:39:36 -07003601 get_css_set(cg);
Lai Jiangshan104cbd52009-01-07 18:07:38 -08003602 task_unlock(tsk);
Li Zefan1404f062009-01-29 14:25:21 -08003603
Paul Menage697f4162007-10-18 23:39:34 -07003604 mutex_unlock(&cgroup_mutex);
3605
3606 /* Now do the VFS work to create a cgroup */
3607 inode = parent->dentry->d_inode;
3608
3609 /* Hold the parent directory mutex across this operation to
3610 * stop anyone else deleting the new cgroup */
3611 mutex_lock(&inode->i_mutex);
3612 dentry = lookup_one_len(nodename, parent->dentry, strlen(nodename));
3613 if (IS_ERR(dentry)) {
3614 printk(KERN_INFO
Diego Callejacfe36bd2007-11-14 16:58:54 -08003615 "cgroup: Couldn't allocate dentry for %s: %ld\n", nodename,
Paul Menage697f4162007-10-18 23:39:34 -07003616 PTR_ERR(dentry));
3617 ret = PTR_ERR(dentry);
3618 goto out_release;
3619 }
3620
3621 /* Create the cgroup directory, which also creates the cgroup */
Li Zefan75139b82009-01-07 18:07:33 -08003622 ret = vfs_mkdir(inode, dentry, 0755);
Paul Menagebd89aab2007-10-18 23:40:44 -07003623 child = __d_cgrp(dentry);
Paul Menage697f4162007-10-18 23:39:34 -07003624 dput(dentry);
3625 if (ret) {
3626 printk(KERN_INFO
3627 "Failed to create cgroup %s: %d\n", nodename,
3628 ret);
3629 goto out_release;
3630 }
3631
Paul Menage697f4162007-10-18 23:39:34 -07003632 /* The cgroup now exists. Retake cgroup_mutex and check
3633 * that we're still in the same state that we thought we
3634 * were. */
3635 mutex_lock(&cgroup_mutex);
3636 if ((root != subsys->root) ||
3637 (parent != task_cgroup(tsk, subsys->subsys_id))) {
3638 /* Aargh, we raced ... */
3639 mutex_unlock(&inode->i_mutex);
Paul Menage817929e2007-10-18 23:39:36 -07003640 put_css_set(cg);
Paul Menage697f4162007-10-18 23:39:34 -07003641
Li Zefan1404f062009-01-29 14:25:21 -08003642 deactivate_super(root->sb);
Paul Menage697f4162007-10-18 23:39:34 -07003643 /* The cgroup is still accessible in the VFS, but
3644 * we're not going to try to rmdir() it at this
3645 * point. */
3646 printk(KERN_INFO
3647 "Race in cgroup_clone() - leaking cgroup %s\n",
3648 nodename);
3649 goto again;
3650 }
3651
3652 /* do any required auto-setup */
3653 for_each_subsys(root, ss) {
3654 if (ss->post_clone)
3655 ss->post_clone(ss, child);
3656 }
3657
3658 /* All seems fine. Finish by moving the task into the new cgroup */
Cliff Wickman956db3c2008-02-07 00:14:43 -08003659 ret = cgroup_attach_task(child, tsk);
Paul Menage697f4162007-10-18 23:39:34 -07003660 mutex_unlock(&cgroup_mutex);
3661
3662 out_release:
3663 mutex_unlock(&inode->i_mutex);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003664
3665 mutex_lock(&cgroup_mutex);
Paul Menage817929e2007-10-18 23:39:36 -07003666 put_css_set(cg);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003667 mutex_unlock(&cgroup_mutex);
Li Zefan1404f062009-01-29 14:25:21 -08003668 deactivate_super(root->sb);
Paul Menage697f4162007-10-18 23:39:34 -07003669 return ret;
3670}
3671
Li Zefana043e3b2008-02-23 15:24:09 -08003672/**
Grzegorz Nosek313e9242009-04-02 16:57:23 -07003673 * cgroup_is_descendant - see if @cgrp is a descendant of @task's cgrp
Li Zefana043e3b2008-02-23 15:24:09 -08003674 * @cgrp: the cgroup in question
Grzegorz Nosek313e9242009-04-02 16:57:23 -07003675 * @task: the task in question
Li Zefana043e3b2008-02-23 15:24:09 -08003676 *
Grzegorz Nosek313e9242009-04-02 16:57:23 -07003677 * See if @cgrp is a descendant of @task's cgroup in the appropriate
3678 * hierarchy.
Paul Menage697f4162007-10-18 23:39:34 -07003679 *
3680 * If we are sending in dummytop, then presumably we are creating
3681 * the top cgroup in the subsystem.
3682 *
3683 * Called only by the ns (nsproxy) cgroup.
3684 */
Grzegorz Nosek313e9242009-04-02 16:57:23 -07003685int cgroup_is_descendant(const struct cgroup *cgrp, struct task_struct *task)
Paul Menage697f4162007-10-18 23:39:34 -07003686{
3687 int ret;
3688 struct cgroup *target;
Paul Menage697f4162007-10-18 23:39:34 -07003689
Paul Menagebd89aab2007-10-18 23:40:44 -07003690 if (cgrp == dummytop)
Paul Menage697f4162007-10-18 23:39:34 -07003691 return 1;
3692
Paul Menage7717f7b2009-09-23 15:56:22 -07003693 target = task_cgroup_from_root(task, cgrp->root);
Paul Menagebd89aab2007-10-18 23:40:44 -07003694 while (cgrp != target && cgrp!= cgrp->top_cgroup)
3695 cgrp = cgrp->parent;
3696 ret = (cgrp == target);
Paul Menage697f4162007-10-18 23:39:34 -07003697 return ret;
3698}
Paul Menage81a6a5c2007-10-18 23:39:38 -07003699
Paul Menagebd89aab2007-10-18 23:40:44 -07003700static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003701{
3702 /* All of these checks rely on RCU to keep the cgroup
3703 * structure alive */
Paul Menagebd89aab2007-10-18 23:40:44 -07003704 if (cgroup_is_releasable(cgrp) && !atomic_read(&cgrp->count)
3705 && list_empty(&cgrp->children) && !cgroup_has_css_refs(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07003706 /* Control Group is currently removeable. If it's not
3707 * already queued for a userspace notification, queue
3708 * it now */
3709 int need_schedule_work = 0;
3710 spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07003711 if (!cgroup_is_removed(cgrp) &&
3712 list_empty(&cgrp->release_list)) {
3713 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003714 need_schedule_work = 1;
3715 }
3716 spin_unlock(&release_list_lock);
3717 if (need_schedule_work)
3718 schedule_work(&release_agent_work);
3719 }
3720}
3721
3722void __css_put(struct cgroup_subsys_state *css)
3723{
Paul Menagebd89aab2007-10-18 23:40:44 -07003724 struct cgroup *cgrp = css->cgroup;
KAMEZAWA Hiroyuki3dece832009-10-01 15:44:09 -07003725 int val;
Paul Menage81a6a5c2007-10-18 23:39:38 -07003726 rcu_read_lock();
KAMEZAWA Hiroyuki3dece832009-10-01 15:44:09 -07003727 val = atomic_dec_return(&css->refcnt);
3728 if (val == 1) {
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003729 if (notify_on_release(cgrp)) {
3730 set_bit(CGRP_RELEASABLE, &cgrp->flags);
3731 check_for_release(cgrp);
3732 }
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003733 cgroup_wakeup_rmdir_waiter(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003734 }
3735 rcu_read_unlock();
KAMEZAWA Hiroyuki3dece832009-10-01 15:44:09 -07003736 WARN_ON_ONCE(val < 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003737}
3738
3739/*
3740 * Notify userspace when a cgroup is released, by running the
3741 * configured release agent with the name of the cgroup (path
3742 * relative to the root of cgroup file system) as the argument.
3743 *
3744 * Most likely, this user command will try to rmdir this cgroup.
3745 *
3746 * This races with the possibility that some other task will be
3747 * attached to this cgroup before it is removed, or that some other
3748 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
3749 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
3750 * unused, and this cgroup will be reprieved from its death sentence,
3751 * to continue to serve a useful existence. Next time it's released,
3752 * we will get notified again, if it still has 'notify_on_release' set.
3753 *
3754 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
3755 * means only wait until the task is successfully execve()'d. The
3756 * separate release agent task is forked by call_usermodehelper(),
3757 * then control in this thread returns here, without waiting for the
3758 * release agent task. We don't bother to wait because the caller of
3759 * this routine has no use for the exit status of the release agent
3760 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07003761 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07003762static void cgroup_release_agent(struct work_struct *work)
3763{
3764 BUG_ON(work != &release_agent_work);
3765 mutex_lock(&cgroup_mutex);
3766 spin_lock(&release_list_lock);
3767 while (!list_empty(&release_list)) {
3768 char *argv[3], *envp[3];
3769 int i;
Paul Menagee788e0662008-07-25 01:46:59 -07003770 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003771 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003772 struct cgroup,
3773 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07003774 list_del_init(&cgrp->release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003775 spin_unlock(&release_list_lock);
3776 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e0662008-07-25 01:46:59 -07003777 if (!pathbuf)
3778 goto continue_free;
3779 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
3780 goto continue_free;
3781 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
3782 if (!agentbuf)
3783 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07003784
3785 i = 0;
Paul Menagee788e0662008-07-25 01:46:59 -07003786 argv[i++] = agentbuf;
3787 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07003788 argv[i] = NULL;
3789
3790 i = 0;
3791 /* minimal command environment */
3792 envp[i++] = "HOME=/";
3793 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
3794 envp[i] = NULL;
3795
3796 /* Drop the lock while we invoke the usermode helper,
3797 * since the exec could involve hitting disk and hence
3798 * be a slow process */
3799 mutex_unlock(&cgroup_mutex);
3800 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003801 mutex_lock(&cgroup_mutex);
Paul Menagee788e0662008-07-25 01:46:59 -07003802 continue_free:
3803 kfree(pathbuf);
3804 kfree(agentbuf);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003805 spin_lock(&release_list_lock);
3806 }
3807 spin_unlock(&release_list_lock);
3808 mutex_unlock(&cgroup_mutex);
3809}
Paul Menage8bab8dd2008-04-04 14:29:57 -07003810
3811static int __init cgroup_disable(char *str)
3812{
3813 int i;
3814 char *token;
3815
3816 while ((token = strsep(&str, ",")) != NULL) {
3817 if (!*token)
3818 continue;
3819
3820 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3821 struct cgroup_subsys *ss = subsys[i];
3822
3823 if (!strcmp(token, ss->name)) {
3824 ss->disabled = 1;
3825 printk(KERN_INFO "Disabling %s control group"
3826 " subsystem\n", ss->name);
3827 break;
3828 }
3829 }
3830 }
3831 return 1;
3832}
3833__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07003834
3835/*
3836 * Functons for CSS ID.
3837 */
3838
3839/*
3840 *To get ID other than 0, this should be called when !cgroup_is_removed().
3841 */
3842unsigned short css_id(struct cgroup_subsys_state *css)
3843{
3844 struct css_id *cssid = rcu_dereference(css->id);
3845
3846 if (cssid)
3847 return cssid->id;
3848 return 0;
3849}
3850
3851unsigned short css_depth(struct cgroup_subsys_state *css)
3852{
3853 struct css_id *cssid = rcu_dereference(css->id);
3854
3855 if (cssid)
3856 return cssid->depth;
3857 return 0;
3858}
3859
3860bool css_is_ancestor(struct cgroup_subsys_state *child,
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07003861 const struct cgroup_subsys_state *root)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07003862{
3863 struct css_id *child_id = rcu_dereference(child->id);
3864 struct css_id *root_id = rcu_dereference(root->id);
3865
3866 if (!child_id || !root_id || (child_id->depth < root_id->depth))
3867 return false;
3868 return child_id->stack[root_id->depth] == root_id->id;
3869}
3870
3871static void __free_css_id_cb(struct rcu_head *head)
3872{
3873 struct css_id *id;
3874
3875 id = container_of(head, struct css_id, rcu_head);
3876 kfree(id);
3877}
3878
3879void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
3880{
3881 struct css_id *id = css->id;
3882 /* When this is called before css_id initialization, id can be NULL */
3883 if (!id)
3884 return;
3885
3886 BUG_ON(!ss->use_id);
3887
3888 rcu_assign_pointer(id->css, NULL);
3889 rcu_assign_pointer(css->id, NULL);
3890 spin_lock(&ss->id_lock);
3891 idr_remove(&ss->idr, id->id);
3892 spin_unlock(&ss->id_lock);
3893 call_rcu(&id->rcu_head, __free_css_id_cb);
3894}
3895
3896/*
3897 * This is called by init or create(). Then, calls to this function are
3898 * always serialized (By cgroup_mutex() at create()).
3899 */
3900
3901static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
3902{
3903 struct css_id *newid;
3904 int myid, error, size;
3905
3906 BUG_ON(!ss->use_id);
3907
3908 size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
3909 newid = kzalloc(size, GFP_KERNEL);
3910 if (!newid)
3911 return ERR_PTR(-ENOMEM);
3912 /* get id */
3913 if (unlikely(!idr_pre_get(&ss->idr, GFP_KERNEL))) {
3914 error = -ENOMEM;
3915 goto err_out;
3916 }
3917 spin_lock(&ss->id_lock);
3918 /* Don't use 0. allocates an ID of 1-65535 */
3919 error = idr_get_new_above(&ss->idr, newid, 1, &myid);
3920 spin_unlock(&ss->id_lock);
3921
3922 /* Returns error when there are no free spaces for new ID.*/
3923 if (error) {
3924 error = -ENOSPC;
3925 goto err_out;
3926 }
3927 if (myid > CSS_ID_MAX)
3928 goto remove_idr;
3929
3930 newid->id = myid;
3931 newid->depth = depth;
3932 return newid;
3933remove_idr:
3934 error = -ENOSPC;
3935 spin_lock(&ss->id_lock);
3936 idr_remove(&ss->idr, myid);
3937 spin_unlock(&ss->id_lock);
3938err_out:
3939 kfree(newid);
3940 return ERR_PTR(error);
3941
3942}
3943
3944static int __init cgroup_subsys_init_idr(struct cgroup_subsys *ss)
3945{
3946 struct css_id *newid;
3947 struct cgroup_subsys_state *rootcss;
3948
3949 spin_lock_init(&ss->id_lock);
3950 idr_init(&ss->idr);
3951
3952 rootcss = init_css_set.subsys[ss->subsys_id];
3953 newid = get_new_cssid(ss, 0);
3954 if (IS_ERR(newid))
3955 return PTR_ERR(newid);
3956
3957 newid->stack[0] = newid->id;
3958 newid->css = rootcss;
3959 rootcss->id = newid;
3960 return 0;
3961}
3962
3963static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent,
3964 struct cgroup *child)
3965{
3966 int subsys_id, i, depth = 0;
3967 struct cgroup_subsys_state *parent_css, *child_css;
3968 struct css_id *child_id, *parent_id = NULL;
3969
3970 subsys_id = ss->subsys_id;
3971 parent_css = parent->subsys[subsys_id];
3972 child_css = child->subsys[subsys_id];
3973 depth = css_depth(parent_css) + 1;
3974 parent_id = parent_css->id;
3975
3976 child_id = get_new_cssid(ss, depth);
3977 if (IS_ERR(child_id))
3978 return PTR_ERR(child_id);
3979
3980 for (i = 0; i < depth; i++)
3981 child_id->stack[i] = parent_id->stack[i];
3982 child_id->stack[depth] = child_id->id;
3983 /*
3984 * child_id->css pointer will be set after this cgroup is available
3985 * see cgroup_populate_dir()
3986 */
3987 rcu_assign_pointer(child_css->id, child_id);
3988
3989 return 0;
3990}
3991
3992/**
3993 * css_lookup - lookup css by id
3994 * @ss: cgroup subsys to be looked into.
3995 * @id: the id
3996 *
3997 * Returns pointer to cgroup_subsys_state if there is valid one with id.
3998 * NULL if not. Should be called under rcu_read_lock()
3999 */
4000struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
4001{
4002 struct css_id *cssid = NULL;
4003
4004 BUG_ON(!ss->use_id);
4005 cssid = idr_find(&ss->idr, id);
4006
4007 if (unlikely(!cssid))
4008 return NULL;
4009
4010 return rcu_dereference(cssid->css);
4011}
4012
4013/**
4014 * css_get_next - lookup next cgroup under specified hierarchy.
4015 * @ss: pointer to subsystem
4016 * @id: current position of iteration.
4017 * @root: pointer to css. search tree under this.
4018 * @foundid: position of found object.
4019 *
4020 * Search next css under the specified hierarchy of rootid. Calling under
4021 * rcu_read_lock() is necessary. Returns NULL if it reaches the end.
4022 */
4023struct cgroup_subsys_state *
4024css_get_next(struct cgroup_subsys *ss, int id,
4025 struct cgroup_subsys_state *root, int *foundid)
4026{
4027 struct cgroup_subsys_state *ret = NULL;
4028 struct css_id *tmp;
4029 int tmpid;
4030 int rootid = css_id(root);
4031 int depth = css_depth(root);
4032
4033 if (!rootid)
4034 return NULL;
4035
4036 BUG_ON(!ss->use_id);
4037 /* fill start point for scan */
4038 tmpid = id;
4039 while (1) {
4040 /*
4041 * scan next entry from bitmap(tree), tmpid is updated after
4042 * idr_get_next().
4043 */
4044 spin_lock(&ss->id_lock);
4045 tmp = idr_get_next(&ss->idr, &tmpid);
4046 spin_unlock(&ss->id_lock);
4047
4048 if (!tmp)
4049 break;
4050 if (tmp->depth >= depth && tmp->stack[depth] == rootid) {
4051 ret = rcu_dereference(tmp->css);
4052 if (ret) {
4053 *foundid = tmpid;
4054 break;
4055 }
4056 }
4057 /* continue to scan from next id */
4058 tmpid = tmpid + 1;
4059 }
4060 return ret;
4061}
4062
Paul Menagefe693432009-09-23 15:56:20 -07004063#ifdef CONFIG_CGROUP_DEBUG
4064static struct cgroup_subsys_state *debug_create(struct cgroup_subsys *ss,
4065 struct cgroup *cont)
4066{
4067 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
4068
4069 if (!css)
4070 return ERR_PTR(-ENOMEM);
4071
4072 return css;
4073}
4074
4075static void debug_destroy(struct cgroup_subsys *ss, struct cgroup *cont)
4076{
4077 kfree(cont->subsys[debug_subsys_id]);
4078}
4079
4080static u64 cgroup_refcount_read(struct cgroup *cont, struct cftype *cft)
4081{
4082 return atomic_read(&cont->count);
4083}
4084
4085static u64 debug_taskcount_read(struct cgroup *cont, struct cftype *cft)
4086{
4087 return cgroup_task_count(cont);
4088}
4089
4090static u64 current_css_set_read(struct cgroup *cont, struct cftype *cft)
4091{
4092 return (u64)(unsigned long)current->cgroups;
4093}
4094
4095static u64 current_css_set_refcount_read(struct cgroup *cont,
4096 struct cftype *cft)
4097{
4098 u64 count;
4099
4100 rcu_read_lock();
4101 count = atomic_read(&current->cgroups->refcount);
4102 rcu_read_unlock();
4103 return count;
4104}
4105
Paul Menage7717f7b2009-09-23 15:56:22 -07004106static int current_css_set_cg_links_read(struct cgroup *cont,
4107 struct cftype *cft,
4108 struct seq_file *seq)
4109{
4110 struct cg_cgroup_link *link;
4111 struct css_set *cg;
4112
4113 read_lock(&css_set_lock);
4114 rcu_read_lock();
4115 cg = rcu_dereference(current->cgroups);
4116 list_for_each_entry(link, &cg->cg_links, cg_link_list) {
4117 struct cgroup *c = link->cgrp;
4118 const char *name;
4119
4120 if (c->dentry)
4121 name = c->dentry->d_name.name;
4122 else
4123 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004124 seq_printf(seq, "Root %d group %s\n",
4125 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07004126 }
4127 rcu_read_unlock();
4128 read_unlock(&css_set_lock);
4129 return 0;
4130}
4131
4132#define MAX_TASKS_SHOWN_PER_CSS 25
4133static int cgroup_css_links_read(struct cgroup *cont,
4134 struct cftype *cft,
4135 struct seq_file *seq)
4136{
4137 struct cg_cgroup_link *link;
4138
4139 read_lock(&css_set_lock);
4140 list_for_each_entry(link, &cont->css_sets, cgrp_link_list) {
4141 struct css_set *cg = link->cg;
4142 struct task_struct *task;
4143 int count = 0;
4144 seq_printf(seq, "css_set %p\n", cg);
4145 list_for_each_entry(task, &cg->tasks, cg_list) {
4146 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
4147 seq_puts(seq, " ...\n");
4148 break;
4149 } else {
4150 seq_printf(seq, " task %d\n",
4151 task_pid_vnr(task));
4152 }
4153 }
4154 }
4155 read_unlock(&css_set_lock);
4156 return 0;
4157}
4158
Paul Menagefe693432009-09-23 15:56:20 -07004159static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft)
4160{
4161 return test_bit(CGRP_RELEASABLE, &cgrp->flags);
4162}
4163
4164static struct cftype debug_files[] = {
4165 {
4166 .name = "cgroup_refcount",
4167 .read_u64 = cgroup_refcount_read,
4168 },
4169 {
4170 .name = "taskcount",
4171 .read_u64 = debug_taskcount_read,
4172 },
4173
4174 {
4175 .name = "current_css_set",
4176 .read_u64 = current_css_set_read,
4177 },
4178
4179 {
4180 .name = "current_css_set_refcount",
4181 .read_u64 = current_css_set_refcount_read,
4182 },
4183
4184 {
Paul Menage7717f7b2009-09-23 15:56:22 -07004185 .name = "current_css_set_cg_links",
4186 .read_seq_string = current_css_set_cg_links_read,
4187 },
4188
4189 {
4190 .name = "cgroup_css_links",
4191 .read_seq_string = cgroup_css_links_read,
4192 },
4193
4194 {
Paul Menagefe693432009-09-23 15:56:20 -07004195 .name = "releasable",
4196 .read_u64 = releasable_read,
4197 },
4198};
4199
4200static int debug_populate(struct cgroup_subsys *ss, struct cgroup *cont)
4201{
4202 return cgroup_add_files(cont, ss, debug_files,
4203 ARRAY_SIZE(debug_files));
4204}
4205
4206struct cgroup_subsys debug_subsys = {
4207 .name = "debug",
4208 .create = debug_create,
4209 .destroy = debug_destroy,
4210 .populate = debug_populate,
4211 .subsys_id = debug_subsys_id,
4212};
4213#endif /* CONFIG_CGROUP_DEBUG */