blob: 1bf4d6db54ab80110beea5c1a65f24499ffe4c79 [file] [log] [blame]
Paul Menageddbcc7e2007-10-18 23:39:30 -07001/*
Paul Menageddbcc7e2007-10-18 23:39:30 -07002 * Generic process-grouping system.
3 *
4 * Based originally on the cpuset system, extracted by Paul Menage
5 * Copyright (C) 2006 Google, Inc
6 *
7 * Copyright notices from the original cpuset code:
8 * --------------------------------------------------
9 * Copyright (C) 2003 BULL SA.
10 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
11 *
12 * Portions derived from Patrick Mochel's sysfs code.
13 * sysfs is Copyright (c) 2001-3 Patrick Mochel
14 *
15 * 2003-10-10 Written by Simon Derr.
16 * 2003-10-22 Updates by Stephen Hemminger.
17 * 2004 May-July Rework by Paul Jackson.
18 * ---------------------------------------------------
19 *
20 * This file is subject to the terms and conditions of the GNU General Public
21 * License. See the file COPYING in the main directory of the Linux
22 * distribution for more details.
23 */
24
25#include <linux/cgroup.h>
Ingo Molnarc50cc752010-02-25 12:02:13 +010026#include <linux/module.h>
Paul Menagec6d57f32009-09-23 15:56:19 -070027#include <linux/ctype.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070028#include <linux/errno.h>
29#include <linux/fs.h>
30#include <linux/kernel.h>
31#include <linux/list.h>
32#include <linux/mm.h>
33#include <linux/mutex.h>
34#include <linux/mount.h>
35#include <linux/pagemap.h>
Paul Menagea4243162007-10-18 23:39:35 -070036#include <linux/proc_fs.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070037#include <linux/rcupdate.h>
38#include <linux/sched.h>
Paul Menage817929e2007-10-18 23:39:36 -070039#include <linux/backing-dev.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070040#include <linux/seq_file.h>
41#include <linux/slab.h>
42#include <linux/magic.h>
43#include <linux/spinlock.h>
44#include <linux/string.h>
Paul Menagebbcb81d2007-10-18 23:39:32 -070045#include <linux/sort.h>
Paul Menage81a6a5c2007-10-18 23:39:38 -070046#include <linux/kmod.h>
Ben Blume6a11052010-03-10 15:22:09 -080047#include <linux/module.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070048#include <linux/delayacct.h>
49#include <linux/cgroupstats.h>
Li Zefan472b1052008-04-29 01:00:11 -070050#include <linux/hash.h>
Al Viro3f8206d2008-07-26 03:46:43 -040051#include <linux/namei.h>
Alessio Igor Bogani337eb002009-05-12 15:10:54 +020052#include <linux/smp_lock.h>
Li Zefan096b7fe2009-07-29 15:04:04 -070053#include <linux/pid_namespace.h>
Paul Menage2c6ab6d2009-09-23 15:56:23 -070054#include <linux/idr.h>
Ben Blumd1d9fd32009-09-23 15:56:28 -070055#include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
Balbir Singh846c7bb2007-10-18 23:39:44 -070056
Paul Menageddbcc7e2007-10-18 23:39:30 -070057#include <asm/atomic.h>
58
Paul Menage81a6a5c2007-10-18 23:39:38 -070059static DEFINE_MUTEX(cgroup_mutex);
60
Ben Blumaae8aab2010-03-10 15:22:07 -080061/*
62 * Generate an array of cgroup subsystem pointers. At boot time, this is
63 * populated up to CGROUP_BUILTIN_SUBSYS_COUNT, and modular subsystems are
64 * registered after that. The mutable section of this array is protected by
65 * cgroup_mutex.
66 */
Paul Menageddbcc7e2007-10-18 23:39:30 -070067#define SUBSYS(_x) &_x ## _subsys,
Ben Blumaae8aab2010-03-10 15:22:07 -080068static struct cgroup_subsys *subsys[CGROUP_SUBSYS_COUNT] = {
Paul Menageddbcc7e2007-10-18 23:39:30 -070069#include <linux/cgroup_subsys.h>
70};
71
Paul Menagec6d57f32009-09-23 15:56:19 -070072#define MAX_CGROUP_ROOT_NAMELEN 64
73
Paul Menageddbcc7e2007-10-18 23:39:30 -070074/*
75 * A cgroupfs_root represents the root of a cgroup hierarchy,
76 * and may be associated with a superblock to form an active
77 * hierarchy
78 */
79struct cgroupfs_root {
80 struct super_block *sb;
81
82 /*
83 * The bitmask of subsystems intended to be attached to this
84 * hierarchy
85 */
86 unsigned long subsys_bits;
87
Paul Menage2c6ab6d2009-09-23 15:56:23 -070088 /* Unique id for this hierarchy. */
89 int hierarchy_id;
90
Paul Menageddbcc7e2007-10-18 23:39:30 -070091 /* The bitmask of subsystems currently attached to this hierarchy */
92 unsigned long actual_subsys_bits;
93
94 /* A list running through the attached subsystems */
95 struct list_head subsys_list;
96
97 /* The root cgroup for this hierarchy */
98 struct cgroup top_cgroup;
99
100 /* Tracks how many cgroups are currently defined in hierarchy.*/
101 int number_of_cgroups;
102
Li Zefane5f6a862009-01-07 18:07:41 -0800103 /* A list running through the active hierarchies */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700104 struct list_head root_list;
105
106 /* Hierarchy-specific flags */
107 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700108
Paul Menagee788e062008-07-25 01:46:59 -0700109 /* The path to use for release notifications. */
Paul Menage81a6a5c2007-10-18 23:39:38 -0700110 char release_agent_path[PATH_MAX];
Paul Menagec6d57f32009-09-23 15:56:19 -0700111
112 /* The name for this hierarchy - may be empty */
113 char name[MAX_CGROUP_ROOT_NAMELEN];
Paul Menageddbcc7e2007-10-18 23:39:30 -0700114};
115
Paul Menageddbcc7e2007-10-18 23:39:30 -0700116/*
117 * The "rootnode" hierarchy is the "dummy hierarchy", reserved for the
118 * subsystems that are otherwise unattached - it never has more than a
119 * single cgroup, and all tasks are part of that cgroup.
120 */
121static struct cgroupfs_root rootnode;
122
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700123/*
124 * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when
125 * cgroup_subsys->use_id != 0.
126 */
127#define CSS_ID_MAX (65535)
128struct css_id {
129 /*
130 * The css to which this ID points. This pointer is set to valid value
131 * after cgroup is populated. If cgroup is removed, this will be NULL.
132 * This pointer is expected to be RCU-safe because destroy()
133 * is called after synchronize_rcu(). But for safe use, css_is_removed()
134 * css_tryget() should be used for avoiding race.
135 */
136 struct cgroup_subsys_state *css;
137 /*
138 * ID of this css.
139 */
140 unsigned short id;
141 /*
142 * Depth in hierarchy which this ID belongs to.
143 */
144 unsigned short depth;
145 /*
146 * ID is freed by RCU. (and lookup routine is RCU safe.)
147 */
148 struct rcu_head rcu_head;
149 /*
150 * Hierarchy of CSS ID belongs to.
151 */
152 unsigned short stack[0]; /* Array of Length (depth+1) */
153};
154
155
Paul Menageddbcc7e2007-10-18 23:39:30 -0700156/* The list of hierarchy roots */
157
158static LIST_HEAD(roots);
Paul Menage817929e2007-10-18 23:39:36 -0700159static int root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700160
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700161static DEFINE_IDA(hierarchy_ida);
162static int next_hierarchy_id;
163static DEFINE_SPINLOCK(hierarchy_id_lock);
164
Paul Menageddbcc7e2007-10-18 23:39:30 -0700165/* dummytop is a shorthand for the dummy hierarchy's top cgroup */
166#define dummytop (&rootnode.top_cgroup)
167
168/* This flag indicates whether tasks in the fork and exit paths should
Li Zefana043e3b2008-02-23 15:24:09 -0800169 * check for fork/exit handlers to call. This avoids us having to do
170 * extra work in the fork/exit path if none of the subsystems need to
171 * be called.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700172 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700173static int need_forkexit_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700174
Paul E. McKenneyd11c5632010-02-22 17:04:50 -0800175#ifdef CONFIG_PROVE_LOCKING
176int cgroup_lock_is_held(void)
177{
178 return lockdep_is_held(&cgroup_mutex);
179}
180#else /* #ifdef CONFIG_PROVE_LOCKING */
181int cgroup_lock_is_held(void)
182{
183 return mutex_is_locked(&cgroup_mutex);
184}
185#endif /* #else #ifdef CONFIG_PROVE_LOCKING */
186
187EXPORT_SYMBOL_GPL(cgroup_lock_is_held);
188
Paul Menageddbcc7e2007-10-18 23:39:30 -0700189/* convenient tests for these bits */
Paul Menagebd89aab2007-10-18 23:40:44 -0700190inline int cgroup_is_removed(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700191{
Paul Menagebd89aab2007-10-18 23:40:44 -0700192 return test_bit(CGRP_REMOVED, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700193}
194
195/* bits in struct cgroupfs_root flags field */
196enum {
197 ROOT_NOPREFIX, /* mounted subsystems have no named prefix */
198};
199
Adrian Bunke9685a02008-02-07 00:13:46 -0800200static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700201{
202 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700203 (1 << CGRP_RELEASABLE) |
204 (1 << CGRP_NOTIFY_ON_RELEASE);
205 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700206}
207
Adrian Bunke9685a02008-02-07 00:13:46 -0800208static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700209{
Paul Menagebd89aab2007-10-18 23:40:44 -0700210 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700211}
212
Paul Menageddbcc7e2007-10-18 23:39:30 -0700213/*
214 * for_each_subsys() allows you to iterate on each subsystem attached to
215 * an active hierarchy
216 */
217#define for_each_subsys(_root, _ss) \
218list_for_each_entry(_ss, &_root->subsys_list, sibling)
219
Li Zefane5f6a862009-01-07 18:07:41 -0800220/* for_each_active_root() allows you to iterate across the active hierarchies */
221#define for_each_active_root(_root) \
Paul Menageddbcc7e2007-10-18 23:39:30 -0700222list_for_each_entry(_root, &roots, root_list)
223
Paul Menage81a6a5c2007-10-18 23:39:38 -0700224/* the list of cgroups eligible for automatic release. Protected by
225 * release_list_lock */
226static LIST_HEAD(release_list);
227static DEFINE_SPINLOCK(release_list_lock);
228static void cgroup_release_agent(struct work_struct *work);
229static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700230static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700231
Paul Menage817929e2007-10-18 23:39:36 -0700232/* Link structure for associating css_set objects with cgroups */
233struct cg_cgroup_link {
234 /*
235 * List running through cg_cgroup_links associated with a
236 * cgroup, anchored on cgroup->css_sets
237 */
Paul Menagebd89aab2007-10-18 23:40:44 -0700238 struct list_head cgrp_link_list;
Paul Menage7717f7b2009-09-23 15:56:22 -0700239 struct cgroup *cgrp;
Paul Menage817929e2007-10-18 23:39:36 -0700240 /*
241 * List running through cg_cgroup_links pointing at a
242 * single css_set object, anchored on css_set->cg_links
243 */
244 struct list_head cg_link_list;
245 struct css_set *cg;
246};
247
248/* The default css_set - used by init and its children prior to any
249 * hierarchies being mounted. It contains a pointer to the root state
250 * for each subsystem. Also used to anchor the list of css_sets. Not
251 * reference-counted, to improve performance when child cgroups
252 * haven't been created.
253 */
254
255static struct css_set init_css_set;
256static struct cg_cgroup_link init_css_set_link;
257
Ben Blume6a11052010-03-10 15:22:09 -0800258static int cgroup_init_idr(struct cgroup_subsys *ss,
259 struct cgroup_subsys_state *css);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700260
Paul Menage817929e2007-10-18 23:39:36 -0700261/* css_set_lock protects the list of css_set objects, and the
262 * chain of tasks off each css_set. Nests outside task->alloc_lock
263 * due to cgroup_iter_start() */
264static DEFINE_RWLOCK(css_set_lock);
265static int css_set_count;
266
Paul Menage7717f7b2009-09-23 15:56:22 -0700267/*
268 * hash table for cgroup groups. This improves the performance to find
269 * an existing css_set. This hash doesn't (currently) take into
270 * account cgroups in empty hierarchies.
271 */
Li Zefan472b1052008-04-29 01:00:11 -0700272#define CSS_SET_HASH_BITS 7
273#define CSS_SET_TABLE_SIZE (1 << CSS_SET_HASH_BITS)
274static struct hlist_head css_set_table[CSS_SET_TABLE_SIZE];
275
276static struct hlist_head *css_set_hash(struct cgroup_subsys_state *css[])
277{
278 int i;
279 int index;
280 unsigned long tmp = 0UL;
281
282 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++)
283 tmp += (unsigned long)css[i];
284 tmp = (tmp >> 16) ^ tmp;
285
286 index = hash_long(tmp, CSS_SET_HASH_BITS);
287
288 return &css_set_table[index];
289}
290
Ben Blumc3783692009-09-23 15:56:29 -0700291static void free_css_set_rcu(struct rcu_head *obj)
292{
293 struct css_set *cg = container_of(obj, struct css_set, rcu_head);
294 kfree(cg);
295}
296
Paul Menage817929e2007-10-18 23:39:36 -0700297/* We don't maintain the lists running through each css_set to its
298 * task until after the first call to cgroup_iter_start(). This
299 * reduces the fork()/exit() overhead for people who have cgroups
300 * compiled into their kernel but not actually in use */
Li Zefan8947f9d2008-07-25 01:46:56 -0700301static int use_task_css_set_links __read_mostly;
Paul Menage817929e2007-10-18 23:39:36 -0700302
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700303static void __put_css_set(struct css_set *cg, int taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700304{
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700305 struct cg_cgroup_link *link;
306 struct cg_cgroup_link *saved_link;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700307 /*
308 * Ensure that the refcount doesn't hit zero while any readers
309 * can see it. Similar to atomic_dec_and_lock(), but for an
310 * rwlock
311 */
312 if (atomic_add_unless(&cg->refcount, -1, 1))
313 return;
314 write_lock(&css_set_lock);
315 if (!atomic_dec_and_test(&cg->refcount)) {
316 write_unlock(&css_set_lock);
317 return;
318 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700319
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700320 /* This css_set is dead. unlink it and release cgroup refcounts */
321 hlist_del(&cg->hlist);
322 css_set_count--;
323
324 list_for_each_entry_safe(link, saved_link, &cg->cg_links,
325 cg_link_list) {
326 struct cgroup *cgrp = link->cgrp;
327 list_del(&link->cg_link_list);
328 list_del(&link->cgrp_link_list);
Paul Menagebd89aab2007-10-18 23:40:44 -0700329 if (atomic_dec_and_test(&cgrp->count) &&
330 notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700331 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700332 set_bit(CGRP_RELEASABLE, &cgrp->flags);
333 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700334 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700335
336 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700337 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700338
339 write_unlock(&css_set_lock);
Ben Blumc3783692009-09-23 15:56:29 -0700340 call_rcu(&cg->rcu_head, free_css_set_rcu);
Paul Menage817929e2007-10-18 23:39:36 -0700341}
342
343/*
344 * refcounted get/put for css_set objects
345 */
346static inline void get_css_set(struct css_set *cg)
347{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700348 atomic_inc(&cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700349}
350
351static inline void put_css_set(struct css_set *cg)
352{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700353 __put_css_set(cg, 0);
Paul Menage817929e2007-10-18 23:39:36 -0700354}
355
Paul Menage81a6a5c2007-10-18 23:39:38 -0700356static inline void put_css_set_taskexit(struct css_set *cg)
357{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700358 __put_css_set(cg, 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700359}
360
Paul Menage817929e2007-10-18 23:39:36 -0700361/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700362 * compare_css_sets - helper function for find_existing_css_set().
363 * @cg: candidate css_set being tested
364 * @old_cg: existing css_set for a task
365 * @new_cgrp: cgroup that's being entered by the task
366 * @template: desired set of css pointers in css_set (pre-calculated)
367 *
368 * Returns true if "cg" matches "old_cg" except for the hierarchy
369 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
370 */
371static bool compare_css_sets(struct css_set *cg,
372 struct css_set *old_cg,
373 struct cgroup *new_cgrp,
374 struct cgroup_subsys_state *template[])
375{
376 struct list_head *l1, *l2;
377
378 if (memcmp(template, cg->subsys, sizeof(cg->subsys))) {
379 /* Not all subsystems matched */
380 return false;
381 }
382
383 /*
384 * Compare cgroup pointers in order to distinguish between
385 * different cgroups in heirarchies with no subsystems. We
386 * could get by with just this check alone (and skip the
387 * memcmp above) but on most setups the memcmp check will
388 * avoid the need for this more expensive check on almost all
389 * candidates.
390 */
391
392 l1 = &cg->cg_links;
393 l2 = &old_cg->cg_links;
394 while (1) {
395 struct cg_cgroup_link *cgl1, *cgl2;
396 struct cgroup *cg1, *cg2;
397
398 l1 = l1->next;
399 l2 = l2->next;
400 /* See if we reached the end - both lists are equal length. */
401 if (l1 == &cg->cg_links) {
402 BUG_ON(l2 != &old_cg->cg_links);
403 break;
404 } else {
405 BUG_ON(l2 == &old_cg->cg_links);
406 }
407 /* Locate the cgroups associated with these links. */
408 cgl1 = list_entry(l1, struct cg_cgroup_link, cg_link_list);
409 cgl2 = list_entry(l2, struct cg_cgroup_link, cg_link_list);
410 cg1 = cgl1->cgrp;
411 cg2 = cgl2->cgrp;
412 /* Hierarchies should be linked in the same order. */
413 BUG_ON(cg1->root != cg2->root);
414
415 /*
416 * If this hierarchy is the hierarchy of the cgroup
417 * that's changing, then we need to check that this
418 * css_set points to the new cgroup; if it's any other
419 * hierarchy, then this css_set should point to the
420 * same cgroup as the old css_set.
421 */
422 if (cg1->root == new_cgrp->root) {
423 if (cg1 != new_cgrp)
424 return false;
425 } else {
426 if (cg1 != cg2)
427 return false;
428 }
429 }
430 return true;
431}
432
433/*
Paul Menage817929e2007-10-18 23:39:36 -0700434 * find_existing_css_set() is a helper for
435 * find_css_set(), and checks to see whether an existing
Li Zefan472b1052008-04-29 01:00:11 -0700436 * css_set is suitable.
Paul Menage817929e2007-10-18 23:39:36 -0700437 *
438 * oldcg: the cgroup group that we're using before the cgroup
439 * transition
440 *
Paul Menagebd89aab2007-10-18 23:40:44 -0700441 * cgrp: the cgroup that we're moving into
Paul Menage817929e2007-10-18 23:39:36 -0700442 *
443 * template: location in which to build the desired set of subsystem
444 * state objects for the new cgroup group
445 */
Paul Menage817929e2007-10-18 23:39:36 -0700446static struct css_set *find_existing_css_set(
447 struct css_set *oldcg,
Paul Menagebd89aab2007-10-18 23:40:44 -0700448 struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -0700449 struct cgroup_subsys_state *template[])
450{
451 int i;
Paul Menagebd89aab2007-10-18 23:40:44 -0700452 struct cgroupfs_root *root = cgrp->root;
Li Zefan472b1052008-04-29 01:00:11 -0700453 struct hlist_head *hhead;
454 struct hlist_node *node;
455 struct css_set *cg;
Paul Menage817929e2007-10-18 23:39:36 -0700456
Ben Blumaae8aab2010-03-10 15:22:07 -0800457 /*
458 * Build the set of subsystem state objects that we want to see in the
459 * new css_set. while subsystems can change globally, the entries here
460 * won't change, so no need for locking.
461 */
Paul Menage817929e2007-10-18 23:39:36 -0700462 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Li Zefan8d53d552008-02-23 15:24:11 -0800463 if (root->subsys_bits & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700464 /* Subsystem is in this hierarchy. So we want
465 * the subsystem state from the new
466 * cgroup */
Paul Menagebd89aab2007-10-18 23:40:44 -0700467 template[i] = cgrp->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700468 } else {
469 /* Subsystem is not in this hierarchy, so we
470 * don't want to change the subsystem state */
471 template[i] = oldcg->subsys[i];
472 }
473 }
474
Li Zefan472b1052008-04-29 01:00:11 -0700475 hhead = css_set_hash(template);
476 hlist_for_each_entry(cg, node, hhead, hlist) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700477 if (!compare_css_sets(cg, oldcg, cgrp, template))
478 continue;
479
480 /* This css_set matches what we need */
481 return cg;
Li Zefan472b1052008-04-29 01:00:11 -0700482 }
Paul Menage817929e2007-10-18 23:39:36 -0700483
484 /* No existing cgroup group matched */
485 return NULL;
486}
487
Paul Menage817929e2007-10-18 23:39:36 -0700488static void free_cg_links(struct list_head *tmp)
489{
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700490 struct cg_cgroup_link *link;
491 struct cg_cgroup_link *saved_link;
492
493 list_for_each_entry_safe(link, saved_link, tmp, cgrp_link_list) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700494 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -0700495 kfree(link);
496 }
497}
498
499/*
Li Zefan36553432008-07-29 22:33:19 -0700500 * allocate_cg_links() allocates "count" cg_cgroup_link structures
501 * and chains them on tmp through their cgrp_link_list fields. Returns 0 on
502 * success or a negative error
503 */
504static int allocate_cg_links(int count, struct list_head *tmp)
505{
506 struct cg_cgroup_link *link;
507 int i;
508 INIT_LIST_HEAD(tmp);
509 for (i = 0; i < count; i++) {
510 link = kmalloc(sizeof(*link), GFP_KERNEL);
511 if (!link) {
512 free_cg_links(tmp);
513 return -ENOMEM;
514 }
515 list_add(&link->cgrp_link_list, tmp);
516 }
517 return 0;
518}
519
Li Zefanc12f65d2009-01-07 18:07:42 -0800520/**
521 * link_css_set - a helper function to link a css_set to a cgroup
522 * @tmp_cg_links: cg_cgroup_link objects allocated by allocate_cg_links()
523 * @cg: the css_set to be linked
524 * @cgrp: the destination cgroup
525 */
526static void link_css_set(struct list_head *tmp_cg_links,
527 struct css_set *cg, struct cgroup *cgrp)
528{
529 struct cg_cgroup_link *link;
530
531 BUG_ON(list_empty(tmp_cg_links));
532 link = list_first_entry(tmp_cg_links, struct cg_cgroup_link,
533 cgrp_link_list);
534 link->cg = cg;
Paul Menage7717f7b2009-09-23 15:56:22 -0700535 link->cgrp = cgrp;
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700536 atomic_inc(&cgrp->count);
Li Zefanc12f65d2009-01-07 18:07:42 -0800537 list_move(&link->cgrp_link_list, &cgrp->css_sets);
Paul Menage7717f7b2009-09-23 15:56:22 -0700538 /*
539 * Always add links to the tail of the list so that the list
540 * is sorted by order of hierarchy creation
541 */
542 list_add_tail(&link->cg_link_list, &cg->cg_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800543}
544
Li Zefan36553432008-07-29 22:33:19 -0700545/*
Paul Menage817929e2007-10-18 23:39:36 -0700546 * find_css_set() takes an existing cgroup group and a
547 * cgroup object, and returns a css_set object that's
548 * equivalent to the old group, but with the given cgroup
549 * substituted into the appropriate hierarchy. Must be called with
550 * cgroup_mutex held
551 */
Paul Menage817929e2007-10-18 23:39:36 -0700552static struct css_set *find_css_set(
Paul Menagebd89aab2007-10-18 23:40:44 -0700553 struct css_set *oldcg, struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700554{
555 struct css_set *res;
556 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT];
Paul Menage817929e2007-10-18 23:39:36 -0700557
558 struct list_head tmp_cg_links;
Paul Menage817929e2007-10-18 23:39:36 -0700559
Li Zefan472b1052008-04-29 01:00:11 -0700560 struct hlist_head *hhead;
Paul Menage7717f7b2009-09-23 15:56:22 -0700561 struct cg_cgroup_link *link;
Li Zefan472b1052008-04-29 01:00:11 -0700562
Paul Menage817929e2007-10-18 23:39:36 -0700563 /* First see if we already have a cgroup group that matches
564 * the desired set */
Li Zefan7e9abd82008-07-25 01:46:54 -0700565 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -0700566 res = find_existing_css_set(oldcg, cgrp, template);
Paul Menage817929e2007-10-18 23:39:36 -0700567 if (res)
568 get_css_set(res);
Li Zefan7e9abd82008-07-25 01:46:54 -0700569 read_unlock(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -0700570
571 if (res)
572 return res;
573
574 res = kmalloc(sizeof(*res), GFP_KERNEL);
575 if (!res)
576 return NULL;
577
578 /* Allocate all the cg_cgroup_link objects that we'll need */
579 if (allocate_cg_links(root_count, &tmp_cg_links) < 0) {
580 kfree(res);
581 return NULL;
582 }
583
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700584 atomic_set(&res->refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -0700585 INIT_LIST_HEAD(&res->cg_links);
586 INIT_LIST_HEAD(&res->tasks);
Li Zefan472b1052008-04-29 01:00:11 -0700587 INIT_HLIST_NODE(&res->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700588
589 /* Copy the set of subsystem state objects generated in
590 * find_existing_css_set() */
591 memcpy(res->subsys, template, sizeof(res->subsys));
592
593 write_lock(&css_set_lock);
594 /* Add reference counts and links from the new css_set. */
Paul Menage7717f7b2009-09-23 15:56:22 -0700595 list_for_each_entry(link, &oldcg->cg_links, cg_link_list) {
596 struct cgroup *c = link->cgrp;
597 if (c->root == cgrp->root)
598 c = cgrp;
599 link_css_set(&tmp_cg_links, res, c);
600 }
Paul Menage817929e2007-10-18 23:39:36 -0700601
602 BUG_ON(!list_empty(&tmp_cg_links));
603
Paul Menage817929e2007-10-18 23:39:36 -0700604 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700605
606 /* Add this cgroup group to the hash table */
607 hhead = css_set_hash(res->subsys);
608 hlist_add_head(&res->hlist, hhead);
609
Paul Menage817929e2007-10-18 23:39:36 -0700610 write_unlock(&css_set_lock);
611
612 return res;
Paul Menageb4f48b62007-10-18 23:39:33 -0700613}
614
Paul Menageddbcc7e2007-10-18 23:39:30 -0700615/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700616 * Return the cgroup for "task" from the given hierarchy. Must be
617 * called with cgroup_mutex held.
618 */
619static struct cgroup *task_cgroup_from_root(struct task_struct *task,
620 struct cgroupfs_root *root)
621{
622 struct css_set *css;
623 struct cgroup *res = NULL;
624
625 BUG_ON(!mutex_is_locked(&cgroup_mutex));
626 read_lock(&css_set_lock);
627 /*
628 * No need to lock the task - since we hold cgroup_mutex the
629 * task can't change groups, so the only thing that can happen
630 * is that it exits and its css is set back to init_css_set.
631 */
632 css = task->cgroups;
633 if (css == &init_css_set) {
634 res = &root->top_cgroup;
635 } else {
636 struct cg_cgroup_link *link;
637 list_for_each_entry(link, &css->cg_links, cg_link_list) {
638 struct cgroup *c = link->cgrp;
639 if (c->root == root) {
640 res = c;
641 break;
642 }
643 }
644 }
645 read_unlock(&css_set_lock);
646 BUG_ON(!res);
647 return res;
648}
649
650/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700651 * There is one global cgroup mutex. We also require taking
652 * task_lock() when dereferencing a task's cgroup subsys pointers.
653 * See "The task_lock() exception", at the end of this comment.
654 *
655 * A task must hold cgroup_mutex to modify cgroups.
656 *
657 * Any task can increment and decrement the count field without lock.
658 * So in general, code holding cgroup_mutex can't rely on the count
659 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800660 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700661 * means that no tasks are currently attached, therefore there is no
662 * way a task attached to that cgroup can fork (the other way to
663 * increment the count). So code holding cgroup_mutex can safely
664 * assume that if the count is zero, it will stay zero. Similarly, if
665 * a task holds cgroup_mutex on a cgroup with zero count, it
666 * knows that the cgroup won't be removed, as cgroup_rmdir()
667 * needs that mutex.
668 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700669 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
670 * (usually) take cgroup_mutex. These are the two most performance
671 * critical pieces of code here. The exception occurs on cgroup_exit(),
672 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
673 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800674 * to the release agent with the name of the cgroup (path relative to
675 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700676 *
677 * A cgroup can only be deleted if both its 'count' of using tasks
678 * is zero, and its list of 'children' cgroups is empty. Since all
679 * tasks in the system use _some_ cgroup, and since there is always at
680 * least one task in the system (init, pid == 1), therefore, top_cgroup
681 * always has either children cgroups and/or using tasks. So we don't
682 * need a special hack to ensure that top_cgroup cannot be deleted.
683 *
684 * The task_lock() exception
685 *
686 * The need for this exception arises from the action of
Cliff Wickman956db3c2008-02-07 00:14:43 -0800687 * cgroup_attach_task(), which overwrites one tasks cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800688 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700689 * several performance critical places that need to reference
690 * task->cgroup without the expense of grabbing a system global
691 * mutex. Therefore except as noted below, when dereferencing or, as
Cliff Wickman956db3c2008-02-07 00:14:43 -0800692 * in cgroup_attach_task(), modifying a task'ss cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700693 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
694 * the task_struct routinely used for such matters.
695 *
696 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800697 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700698 */
699
Paul Menageddbcc7e2007-10-18 23:39:30 -0700700/**
701 * cgroup_lock - lock out any changes to cgroup structures
702 *
703 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700704void cgroup_lock(void)
705{
706 mutex_lock(&cgroup_mutex);
707}
Ben Blum67523c42010-03-10 15:22:11 -0800708EXPORT_SYMBOL_GPL(cgroup_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700709
710/**
711 * cgroup_unlock - release lock on cgroup changes
712 *
713 * Undo the lock taken in a previous cgroup_lock() call.
714 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700715void cgroup_unlock(void)
716{
717 mutex_unlock(&cgroup_mutex);
718}
Ben Blum67523c42010-03-10 15:22:11 -0800719EXPORT_SYMBOL_GPL(cgroup_unlock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700720
721/*
722 * A couple of forward declarations required, due to cyclic reference loop:
723 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
724 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
725 * -> cgroup_mkdir.
726 */
727
728static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode);
729static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -0700730static int cgroup_populate_dir(struct cgroup *cgrp);
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700731static const struct inode_operations cgroup_dir_inode_operations;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700732static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700733
734static struct backing_dev_info cgroup_backing_dev_info = {
Jens Axboed9938312009-06-12 14:45:52 +0200735 .name = "cgroup",
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700736 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Paul Menagea4243162007-10-18 23:39:35 -0700737};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700738
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700739static int alloc_css_id(struct cgroup_subsys *ss,
740 struct cgroup *parent, struct cgroup *child);
741
Paul Menageddbcc7e2007-10-18 23:39:30 -0700742static struct inode *cgroup_new_inode(mode_t mode, struct super_block *sb)
743{
744 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700745
746 if (inode) {
747 inode->i_mode = mode;
David Howells76aac0e2008-11-14 10:39:12 +1100748 inode->i_uid = current_fsuid();
749 inode->i_gid = current_fsgid();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700750 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
751 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
752 }
753 return inode;
754}
755
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800756/*
757 * Call subsys's pre_destroy handler.
758 * This is called before css refcnt check.
759 */
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700760static int cgroup_call_pre_destroy(struct cgroup *cgrp)
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800761{
762 struct cgroup_subsys *ss;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700763 int ret = 0;
764
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800765 for_each_subsys(cgrp->root, ss)
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700766 if (ss->pre_destroy) {
767 ret = ss->pre_destroy(ss, cgrp);
768 if (ret)
769 break;
770 }
771 return ret;
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800772}
773
Paul Menagea47295e2009-01-07 18:07:44 -0800774static void free_cgroup_rcu(struct rcu_head *obj)
775{
776 struct cgroup *cgrp = container_of(obj, struct cgroup, rcu_head);
777
778 kfree(cgrp);
779}
780
Paul Menageddbcc7e2007-10-18 23:39:30 -0700781static void cgroup_diput(struct dentry *dentry, struct inode *inode)
782{
783 /* is dentry a directory ? if so, kfree() associated cgroup */
784 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700785 struct cgroup *cgrp = dentry->d_fsdata;
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800786 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -0700787 BUG_ON(!(cgroup_is_removed(cgrp)));
Paul Menage81a6a5c2007-10-18 23:39:38 -0700788 /* It's possible for external users to be holding css
789 * reference counts on a cgroup; css_put() needs to
790 * be able to access the cgroup after decrementing
791 * the reference count in order to know if it needs to
792 * queue the cgroup to be handled by the release
793 * agent */
794 synchronize_rcu();
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800795
796 mutex_lock(&cgroup_mutex);
797 /*
798 * Release the subsystem state objects.
799 */
Li Zefan75139b82009-01-07 18:07:33 -0800800 for_each_subsys(cgrp->root, ss)
801 ss->destroy(ss, cgrp);
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800802
803 cgrp->root->number_of_cgroups--;
804 mutex_unlock(&cgroup_mutex);
805
Paul Menagea47295e2009-01-07 18:07:44 -0800806 /*
807 * Drop the active superblock reference that we took when we
808 * created the cgroup
809 */
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800810 deactivate_super(cgrp->root->sb);
811
Ben Blum72a8cb32009-09-23 15:56:27 -0700812 /*
813 * if we're getting rid of the cgroup, refcount should ensure
814 * that there are no pidlists left.
815 */
816 BUG_ON(!list_empty(&cgrp->pidlists));
817
Paul Menagea47295e2009-01-07 18:07:44 -0800818 call_rcu(&cgrp->rcu_head, free_cgroup_rcu);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700819 }
820 iput(inode);
821}
822
823static void remove_dir(struct dentry *d)
824{
825 struct dentry *parent = dget(d->d_parent);
826
827 d_delete(d);
828 simple_rmdir(parent->d_inode, d);
829 dput(parent);
830}
831
832static void cgroup_clear_directory(struct dentry *dentry)
833{
834 struct list_head *node;
835
836 BUG_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
837 spin_lock(&dcache_lock);
838 node = dentry->d_subdirs.next;
839 while (node != &dentry->d_subdirs) {
840 struct dentry *d = list_entry(node, struct dentry, d_u.d_child);
841 list_del_init(node);
842 if (d->d_inode) {
843 /* This should never be called on a cgroup
844 * directory with child cgroups */
845 BUG_ON(d->d_inode->i_mode & S_IFDIR);
846 d = dget_locked(d);
847 spin_unlock(&dcache_lock);
848 d_delete(d);
849 simple_unlink(dentry->d_inode, d);
850 dput(d);
851 spin_lock(&dcache_lock);
852 }
853 node = dentry->d_subdirs.next;
854 }
855 spin_unlock(&dcache_lock);
856}
857
858/*
859 * NOTE : the dentry must have been dget()'ed
860 */
861static void cgroup_d_remove_dir(struct dentry *dentry)
862{
863 cgroup_clear_directory(dentry);
864
865 spin_lock(&dcache_lock);
866 list_del_init(&dentry->d_u.d_child);
867 spin_unlock(&dcache_lock);
868 remove_dir(dentry);
869}
870
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700871/*
872 * A queue for waiters to do rmdir() cgroup. A tasks will sleep when
873 * cgroup->count == 0 && list_empty(&cgroup->children) && subsys has some
874 * reference to css->refcnt. In general, this refcnt is expected to goes down
875 * to zero, soon.
876 *
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -0700877 * CGRP_WAIT_ON_RMDIR flag is set under cgroup's inode->i_mutex;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700878 */
879DECLARE_WAIT_QUEUE_HEAD(cgroup_rmdir_waitq);
880
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -0700881static void cgroup_wakeup_rmdir_waiter(struct cgroup *cgrp)
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700882{
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -0700883 if (unlikely(test_and_clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags)))
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700884 wake_up_all(&cgroup_rmdir_waitq);
885}
886
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -0700887void cgroup_exclude_rmdir(struct cgroup_subsys_state *css)
888{
889 css_get(css);
890}
891
892void cgroup_release_and_wakeup_rmdir(struct cgroup_subsys_state *css)
893{
894 cgroup_wakeup_rmdir_waiter(css->cgroup);
895 css_put(css);
896}
897
Ben Blumaae8aab2010-03-10 15:22:07 -0800898/*
Ben Blumcf5d5942010-03-10 15:22:09 -0800899 * Call with cgroup_mutex held. Drops reference counts on modules, including
900 * any duplicate ones that parse_cgroupfs_options took. If this function
901 * returns an error, no reference counts are touched.
Ben Blumaae8aab2010-03-10 15:22:07 -0800902 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700903static int rebind_subsystems(struct cgroupfs_root *root,
904 unsigned long final_bits)
905{
906 unsigned long added_bits, removed_bits;
Paul Menagebd89aab2007-10-18 23:40:44 -0700907 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700908 int i;
909
Ben Blumaae8aab2010-03-10 15:22:07 -0800910 BUG_ON(!mutex_is_locked(&cgroup_mutex));
911
Paul Menageddbcc7e2007-10-18 23:39:30 -0700912 removed_bits = root->actual_subsys_bits & ~final_bits;
913 added_bits = final_bits & ~root->actual_subsys_bits;
914 /* Check that any added subsystems are currently free */
915 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Li Zefan8d53d552008-02-23 15:24:11 -0800916 unsigned long bit = 1UL << i;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700917 struct cgroup_subsys *ss = subsys[i];
918 if (!(bit & added_bits))
919 continue;
Ben Blumaae8aab2010-03-10 15:22:07 -0800920 /*
921 * Nobody should tell us to do a subsys that doesn't exist:
922 * parse_cgroupfs_options should catch that case and refcounts
923 * ensure that subsystems won't disappear once selected.
924 */
925 BUG_ON(ss == NULL);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700926 if (ss->root != &rootnode) {
927 /* Subsystem isn't free */
928 return -EBUSY;
929 }
930 }
931
932 /* Currently we don't handle adding/removing subsystems when
933 * any child cgroups exist. This is theoretically supportable
934 * but involves complex error handling, so it's being left until
935 * later */
Paul Menage307257c2008-12-15 13:54:22 -0800936 if (root->number_of_cgroups > 1)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700937 return -EBUSY;
938
939 /* Process each subsystem */
940 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
941 struct cgroup_subsys *ss = subsys[i];
942 unsigned long bit = 1UL << i;
943 if (bit & added_bits) {
944 /* We're binding this subsystem to this hierarchy */
Ben Blumaae8aab2010-03-10 15:22:07 -0800945 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -0700946 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700947 BUG_ON(!dummytop->subsys[i]);
948 BUG_ON(dummytop->subsys[i]->cgroup != dummytop);
Paul Menage999cd8a2009-01-07 18:08:36 -0800949 mutex_lock(&ss->hierarchy_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -0700950 cgrp->subsys[i] = dummytop->subsys[i];
951 cgrp->subsys[i]->cgroup = cgrp;
Li Zefan33a68ac2009-01-07 18:07:42 -0800952 list_move(&ss->sibling, &root->subsys_list);
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -0800953 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700954 if (ss->bind)
Paul Menagebd89aab2007-10-18 23:40:44 -0700955 ss->bind(ss, cgrp);
Paul Menage999cd8a2009-01-07 18:08:36 -0800956 mutex_unlock(&ss->hierarchy_mutex);
Ben Blumcf5d5942010-03-10 15:22:09 -0800957 /* refcount was already taken, and we're keeping it */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700958 } else if (bit & removed_bits) {
959 /* We're removing this subsystem */
Ben Blumaae8aab2010-03-10 15:22:07 -0800960 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -0700961 BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]);
962 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
Paul Menage999cd8a2009-01-07 18:08:36 -0800963 mutex_lock(&ss->hierarchy_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700964 if (ss->bind)
965 ss->bind(ss, dummytop);
966 dummytop->subsys[i]->cgroup = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -0700967 cgrp->subsys[i] = NULL;
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -0800968 subsys[i]->root = &rootnode;
Li Zefan33a68ac2009-01-07 18:07:42 -0800969 list_move(&ss->sibling, &rootnode.subsys_list);
Paul Menage999cd8a2009-01-07 18:08:36 -0800970 mutex_unlock(&ss->hierarchy_mutex);
Ben Blumcf5d5942010-03-10 15:22:09 -0800971 /* subsystem is now free - drop reference on module */
972 module_put(ss->module);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700973 } else if (bit & final_bits) {
974 /* Subsystem state should already exist */
Ben Blumaae8aab2010-03-10 15:22:07 -0800975 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -0700976 BUG_ON(!cgrp->subsys[i]);
Ben Blumcf5d5942010-03-10 15:22:09 -0800977 /*
978 * a refcount was taken, but we already had one, so
979 * drop the extra reference.
980 */
981 module_put(ss->module);
982#ifdef CONFIG_MODULE_UNLOAD
983 BUG_ON(ss->module && !module_refcount(ss->module));
984#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -0700985 } else {
986 /* Subsystem state shouldn't exist */
Paul Menagebd89aab2007-10-18 23:40:44 -0700987 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700988 }
989 }
990 root->subsys_bits = root->actual_subsys_bits = final_bits;
991 synchronize_rcu();
992
993 return 0;
994}
995
996static int cgroup_show_options(struct seq_file *seq, struct vfsmount *vfs)
997{
998 struct cgroupfs_root *root = vfs->mnt_sb->s_fs_info;
999 struct cgroup_subsys *ss;
1000
1001 mutex_lock(&cgroup_mutex);
1002 for_each_subsys(root, ss)
1003 seq_printf(seq, ",%s", ss->name);
1004 if (test_bit(ROOT_NOPREFIX, &root->flags))
1005 seq_puts(seq, ",noprefix");
Paul Menage81a6a5c2007-10-18 23:39:38 -07001006 if (strlen(root->release_agent_path))
1007 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Paul Menagec6d57f32009-09-23 15:56:19 -07001008 if (strlen(root->name))
1009 seq_printf(seq, ",name=%s", root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001010 mutex_unlock(&cgroup_mutex);
1011 return 0;
1012}
1013
1014struct cgroup_sb_opts {
1015 unsigned long subsys_bits;
1016 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001017 char *release_agent;
Paul Menagec6d57f32009-09-23 15:56:19 -07001018 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001019 /* User explicitly requested empty subsystem */
1020 bool none;
Paul Menagec6d57f32009-09-23 15:56:19 -07001021
1022 struct cgroupfs_root *new_root;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001023
Paul Menageddbcc7e2007-10-18 23:39:30 -07001024};
1025
Ben Blumaae8aab2010-03-10 15:22:07 -08001026/*
1027 * Convert a hierarchy specifier into a bitmask of subsystems and flags. Call
Ben Blumcf5d5942010-03-10 15:22:09 -08001028 * with cgroup_mutex held to protect the subsys[] array. This function takes
1029 * refcounts on subsystems to be used, unless it returns error, in which case
1030 * no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001031 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001032static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001033{
1034 char *token, *o = data ?: "all";
Li Zefanf9ab5b52009-06-17 16:26:33 -07001035 unsigned long mask = (unsigned long)-1;
Ben Blumcf5d5942010-03-10 15:22:09 -08001036 int i;
1037 bool module_pin_failed = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001038
Ben Blumaae8aab2010-03-10 15:22:07 -08001039 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1040
Li Zefanf9ab5b52009-06-17 16:26:33 -07001041#ifdef CONFIG_CPUSETS
1042 mask = ~(1UL << cpuset_subsys_id);
1043#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001044
Paul Menagec6d57f32009-09-23 15:56:19 -07001045 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001046
1047 while ((token = strsep(&o, ",")) != NULL) {
1048 if (!*token)
1049 return -EINVAL;
1050 if (!strcmp(token, "all")) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07001051 /* Add all non-disabled subsystems */
Paul Menage8bab8dd2008-04-04 14:29:57 -07001052 opts->subsys_bits = 0;
1053 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1054 struct cgroup_subsys *ss = subsys[i];
Ben Blumaae8aab2010-03-10 15:22:07 -08001055 if (ss == NULL)
1056 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07001057 if (!ss->disabled)
1058 opts->subsys_bits |= 1ul << i;
1059 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001060 } else if (!strcmp(token, "none")) {
1061 /* Explicitly have no subsystems */
1062 opts->none = true;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001063 } else if (!strcmp(token, "noprefix")) {
1064 set_bit(ROOT_NOPREFIX, &opts->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001065 } else if (!strncmp(token, "release_agent=", 14)) {
1066 /* Specifying two release agents is forbidden */
1067 if (opts->release_agent)
1068 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001069 opts->release_agent =
1070 kstrndup(token + 14, PATH_MAX, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001071 if (!opts->release_agent)
1072 return -ENOMEM;
Paul Menagec6d57f32009-09-23 15:56:19 -07001073 } else if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001074 const char *name = token + 5;
1075 /* Can't specify an empty name */
1076 if (!strlen(name))
1077 return -EINVAL;
1078 /* Must match [\w.-]+ */
1079 for (i = 0; i < strlen(name); i++) {
1080 char c = name[i];
1081 if (isalnum(c))
1082 continue;
1083 if ((c == '.') || (c == '-') || (c == '_'))
1084 continue;
1085 return -EINVAL;
1086 }
1087 /* Specifying two names is forbidden */
1088 if (opts->name)
1089 return -EINVAL;
1090 opts->name = kstrndup(name,
1091 MAX_CGROUP_ROOT_NAMELEN,
1092 GFP_KERNEL);
1093 if (!opts->name)
1094 return -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001095 } else {
1096 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001097 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1098 ss = subsys[i];
Ben Blumaae8aab2010-03-10 15:22:07 -08001099 if (ss == NULL)
1100 continue;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001101 if (!strcmp(token, ss->name)) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07001102 if (!ss->disabled)
1103 set_bit(i, &opts->subsys_bits);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001104 break;
1105 }
1106 }
1107 if (i == CGROUP_SUBSYS_COUNT)
1108 return -ENOENT;
1109 }
1110 }
1111
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001112 /* Consistency checks */
1113
Li Zefanf9ab5b52009-06-17 16:26:33 -07001114 /*
1115 * Option noprefix was introduced just for backward compatibility
1116 * with the old cpuset, so we allow noprefix only if mounting just
1117 * the cpuset subsystem.
1118 */
1119 if (test_bit(ROOT_NOPREFIX, &opts->flags) &&
1120 (opts->subsys_bits & mask))
1121 return -EINVAL;
1122
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001123
1124 /* Can't specify "none" and some subsystems */
1125 if (opts->subsys_bits && opts->none)
1126 return -EINVAL;
1127
1128 /*
1129 * We either have to specify by name or by subsystems. (So all
1130 * empty hierarchies must have a name).
1131 */
Paul Menagec6d57f32009-09-23 15:56:19 -07001132 if (!opts->subsys_bits && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001133 return -EINVAL;
1134
Ben Blumcf5d5942010-03-10 15:22:09 -08001135 /*
1136 * Grab references on all the modules we'll need, so the subsystems
1137 * don't dance around before rebind_subsystems attaches them. This may
1138 * take duplicate reference counts on a subsystem that's already used,
1139 * but rebind_subsystems handles this case.
1140 */
1141 for (i = CGROUP_BUILTIN_SUBSYS_COUNT; i < CGROUP_SUBSYS_COUNT; i++) {
1142 unsigned long bit = 1UL << i;
1143
1144 if (!(bit & opts->subsys_bits))
1145 continue;
1146 if (!try_module_get(subsys[i]->module)) {
1147 module_pin_failed = true;
1148 break;
1149 }
1150 }
1151 if (module_pin_failed) {
1152 /*
1153 * oops, one of the modules was going away. this means that we
1154 * raced with a module_delete call, and to the user this is
1155 * essentially a "subsystem doesn't exist" case.
1156 */
1157 for (i--; i >= CGROUP_BUILTIN_SUBSYS_COUNT; i--) {
1158 /* drop refcounts only on the ones we took */
1159 unsigned long bit = 1UL << i;
1160
1161 if (!(bit & opts->subsys_bits))
1162 continue;
1163 module_put(subsys[i]->module);
1164 }
1165 return -ENOENT;
1166 }
1167
Paul Menageddbcc7e2007-10-18 23:39:30 -07001168 return 0;
1169}
1170
Ben Blumcf5d5942010-03-10 15:22:09 -08001171static void drop_parsed_module_refcounts(unsigned long subsys_bits)
1172{
1173 int i;
1174 for (i = CGROUP_BUILTIN_SUBSYS_COUNT; i < CGROUP_SUBSYS_COUNT; i++) {
1175 unsigned long bit = 1UL << i;
1176
1177 if (!(bit & subsys_bits))
1178 continue;
1179 module_put(subsys[i]->module);
1180 }
1181}
1182
Paul Menageddbcc7e2007-10-18 23:39:30 -07001183static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1184{
1185 int ret = 0;
1186 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001187 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001188 struct cgroup_sb_opts opts;
1189
Alessio Igor Bogani337eb002009-05-12 15:10:54 +02001190 lock_kernel();
Paul Menagebd89aab2007-10-18 23:40:44 -07001191 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001192 mutex_lock(&cgroup_mutex);
1193
1194 /* See what subsystems are wanted */
1195 ret = parse_cgroupfs_options(data, &opts);
1196 if (ret)
1197 goto out_unlock;
1198
Ben Blumcf5d5942010-03-10 15:22:09 -08001199 /* Don't allow flags or name to change at remount */
1200 if (opts.flags != root->flags ||
1201 (opts.name && strcmp(opts.name, root->name))) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001202 ret = -EINVAL;
Ben Blumcf5d5942010-03-10 15:22:09 -08001203 drop_parsed_module_refcounts(opts.subsys_bits);
Paul Menagec6d57f32009-09-23 15:56:19 -07001204 goto out_unlock;
1205 }
1206
Paul Menageddbcc7e2007-10-18 23:39:30 -07001207 ret = rebind_subsystems(root, opts.subsys_bits);
Ben Blumcf5d5942010-03-10 15:22:09 -08001208 if (ret) {
1209 drop_parsed_module_refcounts(opts.subsys_bits);
Li Zefan0670e082009-04-02 16:57:30 -07001210 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001211 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001212
1213 /* (re)populate subsystem files */
Li Zefan0670e082009-04-02 16:57:30 -07001214 cgroup_populate_dir(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001215
Paul Menage81a6a5c2007-10-18 23:39:38 -07001216 if (opts.release_agent)
1217 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001218 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001219 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001220 kfree(opts.name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001221 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07001222 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Alessio Igor Bogani337eb002009-05-12 15:10:54 +02001223 unlock_kernel();
Paul Menageddbcc7e2007-10-18 23:39:30 -07001224 return ret;
1225}
1226
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07001227static const struct super_operations cgroup_ops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001228 .statfs = simple_statfs,
1229 .drop_inode = generic_delete_inode,
1230 .show_options = cgroup_show_options,
1231 .remount_fs = cgroup_remount,
1232};
1233
Paul Menagecc31edc2008-10-18 20:28:04 -07001234static void init_cgroup_housekeeping(struct cgroup *cgrp)
1235{
1236 INIT_LIST_HEAD(&cgrp->sibling);
1237 INIT_LIST_HEAD(&cgrp->children);
1238 INIT_LIST_HEAD(&cgrp->css_sets);
1239 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001240 INIT_LIST_HEAD(&cgrp->pidlists);
1241 mutex_init(&cgrp->pidlist_mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07001242}
Paul Menagec6d57f32009-09-23 15:56:19 -07001243
Paul Menageddbcc7e2007-10-18 23:39:30 -07001244static void init_cgroup_root(struct cgroupfs_root *root)
1245{
Paul Menagebd89aab2007-10-18 23:40:44 -07001246 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001247 INIT_LIST_HEAD(&root->subsys_list);
1248 INIT_LIST_HEAD(&root->root_list);
1249 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -07001250 cgrp->root = root;
1251 cgrp->top_cgroup = cgrp;
Paul Menagecc31edc2008-10-18 20:28:04 -07001252 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001253}
1254
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001255static bool init_root_id(struct cgroupfs_root *root)
1256{
1257 int ret = 0;
1258
1259 do {
1260 if (!ida_pre_get(&hierarchy_ida, GFP_KERNEL))
1261 return false;
1262 spin_lock(&hierarchy_id_lock);
1263 /* Try to allocate the next unused ID */
1264 ret = ida_get_new_above(&hierarchy_ida, next_hierarchy_id,
1265 &root->hierarchy_id);
1266 if (ret == -ENOSPC)
1267 /* Try again starting from 0 */
1268 ret = ida_get_new(&hierarchy_ida, &root->hierarchy_id);
1269 if (!ret) {
1270 next_hierarchy_id = root->hierarchy_id + 1;
1271 } else if (ret != -EAGAIN) {
1272 /* Can only get here if the 31-bit IDR is full ... */
1273 BUG_ON(ret);
1274 }
1275 spin_unlock(&hierarchy_id_lock);
1276 } while (ret);
1277 return true;
1278}
1279
Paul Menageddbcc7e2007-10-18 23:39:30 -07001280static int cgroup_test_super(struct super_block *sb, void *data)
1281{
Paul Menagec6d57f32009-09-23 15:56:19 -07001282 struct cgroup_sb_opts *opts = data;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001283 struct cgroupfs_root *root = sb->s_fs_info;
1284
Paul Menagec6d57f32009-09-23 15:56:19 -07001285 /* If we asked for a name then it must match */
1286 if (opts->name && strcmp(opts->name, root->name))
1287 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001288
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001289 /*
1290 * If we asked for subsystems (or explicitly for no
1291 * subsystems) then they must match
1292 */
1293 if ((opts->subsys_bits || opts->none)
1294 && (opts->subsys_bits != root->subsys_bits))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001295 return 0;
1296
1297 return 1;
1298}
1299
Paul Menagec6d57f32009-09-23 15:56:19 -07001300static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1301{
1302 struct cgroupfs_root *root;
1303
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001304 if (!opts->subsys_bits && !opts->none)
Paul Menagec6d57f32009-09-23 15:56:19 -07001305 return NULL;
1306
1307 root = kzalloc(sizeof(*root), GFP_KERNEL);
1308 if (!root)
1309 return ERR_PTR(-ENOMEM);
1310
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001311 if (!init_root_id(root)) {
1312 kfree(root);
1313 return ERR_PTR(-ENOMEM);
1314 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001315 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001316
Paul Menagec6d57f32009-09-23 15:56:19 -07001317 root->subsys_bits = opts->subsys_bits;
1318 root->flags = opts->flags;
1319 if (opts->release_agent)
1320 strcpy(root->release_agent_path, opts->release_agent);
1321 if (opts->name)
1322 strcpy(root->name, opts->name);
1323 return root;
1324}
1325
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001326static void cgroup_drop_root(struct cgroupfs_root *root)
1327{
1328 if (!root)
1329 return;
1330
1331 BUG_ON(!root->hierarchy_id);
1332 spin_lock(&hierarchy_id_lock);
1333 ida_remove(&hierarchy_ida, root->hierarchy_id);
1334 spin_unlock(&hierarchy_id_lock);
1335 kfree(root);
1336}
1337
Paul Menageddbcc7e2007-10-18 23:39:30 -07001338static int cgroup_set_super(struct super_block *sb, void *data)
1339{
1340 int ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001341 struct cgroup_sb_opts *opts = data;
1342
1343 /* If we don't have a new root, we can't set up a new sb */
1344 if (!opts->new_root)
1345 return -EINVAL;
1346
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001347 BUG_ON(!opts->subsys_bits && !opts->none);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001348
1349 ret = set_anon_super(sb, NULL);
1350 if (ret)
1351 return ret;
1352
Paul Menagec6d57f32009-09-23 15:56:19 -07001353 sb->s_fs_info = opts->new_root;
1354 opts->new_root->sb = sb;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001355
1356 sb->s_blocksize = PAGE_CACHE_SIZE;
1357 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1358 sb->s_magic = CGROUP_SUPER_MAGIC;
1359 sb->s_op = &cgroup_ops;
1360
1361 return 0;
1362}
1363
1364static int cgroup_get_rootdir(struct super_block *sb)
1365{
1366 struct inode *inode =
1367 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
1368 struct dentry *dentry;
1369
1370 if (!inode)
1371 return -ENOMEM;
1372
Paul Menageddbcc7e2007-10-18 23:39:30 -07001373 inode->i_fop = &simple_dir_operations;
1374 inode->i_op = &cgroup_dir_inode_operations;
1375 /* directories start off with i_nlink == 2 (for "." entry) */
1376 inc_nlink(inode);
1377 dentry = d_alloc_root(inode);
1378 if (!dentry) {
1379 iput(inode);
1380 return -ENOMEM;
1381 }
1382 sb->s_root = dentry;
1383 return 0;
1384}
1385
1386static int cgroup_get_sb(struct file_system_type *fs_type,
1387 int flags, const char *unused_dev_name,
1388 void *data, struct vfsmount *mnt)
1389{
1390 struct cgroup_sb_opts opts;
Paul Menagec6d57f32009-09-23 15:56:19 -07001391 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001392 int ret = 0;
1393 struct super_block *sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001394 struct cgroupfs_root *new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001395
1396 /* First find the desired set of subsystems */
Ben Blumaae8aab2010-03-10 15:22:07 -08001397 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001398 ret = parse_cgroupfs_options(data, &opts);
Ben Blumaae8aab2010-03-10 15:22:07 -08001399 mutex_unlock(&cgroup_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001400 if (ret)
1401 goto out_err;
1402
1403 /*
1404 * Allocate a new cgroup root. We may not need it if we're
1405 * reusing an existing hierarchy.
1406 */
1407 new_root = cgroup_root_from_opts(&opts);
1408 if (IS_ERR(new_root)) {
1409 ret = PTR_ERR(new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001410 goto drop_modules;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001411 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001412 opts.new_root = new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001413
Paul Menagec6d57f32009-09-23 15:56:19 -07001414 /* Locate an existing or new sb for this hierarchy */
1415 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001416 if (IS_ERR(sb)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001417 ret = PTR_ERR(sb);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001418 cgroup_drop_root(opts.new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001419 goto drop_modules;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001420 }
1421
Paul Menagec6d57f32009-09-23 15:56:19 -07001422 root = sb->s_fs_info;
1423 BUG_ON(!root);
1424 if (root == opts.new_root) {
1425 /* We used the new root structure, so this is a new hierarchy */
1426 struct list_head tmp_cg_links;
Li Zefanc12f65d2009-01-07 18:07:42 -08001427 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menage817929e2007-10-18 23:39:36 -07001428 struct inode *inode;
Paul Menagec6d57f32009-09-23 15:56:19 -07001429 struct cgroupfs_root *existing_root;
Li Zefan28fd5df2008-04-29 01:00:13 -07001430 int i;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001431
1432 BUG_ON(sb->s_root != NULL);
1433
1434 ret = cgroup_get_rootdir(sb);
1435 if (ret)
1436 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001437 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001438
Paul Menage817929e2007-10-18 23:39:36 -07001439 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001440 mutex_lock(&cgroup_mutex);
1441
Paul Menagec6d57f32009-09-23 15:56:19 -07001442 if (strlen(root->name)) {
1443 /* Check for name clashes with existing mounts */
1444 for_each_active_root(existing_root) {
1445 if (!strcmp(existing_root->name, root->name)) {
1446 ret = -EBUSY;
1447 mutex_unlock(&cgroup_mutex);
1448 mutex_unlock(&inode->i_mutex);
1449 goto drop_new_super;
1450 }
1451 }
1452 }
1453
Paul Menage817929e2007-10-18 23:39:36 -07001454 /*
1455 * We're accessing css_set_count without locking
1456 * css_set_lock here, but that's OK - it can only be
1457 * increased by someone holding cgroup_lock, and
1458 * that's us. The worst that can happen is that we
1459 * have some link structures left over
1460 */
1461 ret = allocate_cg_links(css_set_count, &tmp_cg_links);
1462 if (ret) {
1463 mutex_unlock(&cgroup_mutex);
1464 mutex_unlock(&inode->i_mutex);
1465 goto drop_new_super;
1466 }
1467
Paul Menageddbcc7e2007-10-18 23:39:30 -07001468 ret = rebind_subsystems(root, root->subsys_bits);
1469 if (ret == -EBUSY) {
1470 mutex_unlock(&cgroup_mutex);
Paul Menage817929e2007-10-18 23:39:36 -07001471 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001472 free_cg_links(&tmp_cg_links);
1473 goto drop_new_super;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001474 }
Ben Blumcf5d5942010-03-10 15:22:09 -08001475 /*
1476 * There must be no failure case after here, since rebinding
1477 * takes care of subsystems' refcounts, which are explicitly
1478 * dropped in the failure exit path.
1479 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001480
1481 /* EBUSY should be the only error here */
1482 BUG_ON(ret);
1483
1484 list_add(&root->root_list, &roots);
Paul Menage817929e2007-10-18 23:39:36 -07001485 root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001486
Li Zefanc12f65d2009-01-07 18:07:42 -08001487 sb->s_root->d_fsdata = root_cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001488 root->top_cgroup.dentry = sb->s_root;
1489
Paul Menage817929e2007-10-18 23:39:36 -07001490 /* Link the top cgroup in this hierarchy into all
1491 * the css_set objects */
1492 write_lock(&css_set_lock);
Li Zefan28fd5df2008-04-29 01:00:13 -07001493 for (i = 0; i < CSS_SET_TABLE_SIZE; i++) {
1494 struct hlist_head *hhead = &css_set_table[i];
1495 struct hlist_node *node;
Paul Menage817929e2007-10-18 23:39:36 -07001496 struct css_set *cg;
Li Zefan28fd5df2008-04-29 01:00:13 -07001497
Li Zefanc12f65d2009-01-07 18:07:42 -08001498 hlist_for_each_entry(cg, node, hhead, hlist)
1499 link_css_set(&tmp_cg_links, cg, root_cgrp);
Li Zefan28fd5df2008-04-29 01:00:13 -07001500 }
Paul Menage817929e2007-10-18 23:39:36 -07001501 write_unlock(&css_set_lock);
1502
1503 free_cg_links(&tmp_cg_links);
1504
Li Zefanc12f65d2009-01-07 18:07:42 -08001505 BUG_ON(!list_empty(&root_cgrp->sibling));
1506 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001507 BUG_ON(root->number_of_cgroups != 1);
1508
Li Zefanc12f65d2009-01-07 18:07:42 -08001509 cgroup_populate_dir(root_cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001510 mutex_unlock(&cgroup_mutex);
Xiaotian Feng34f77a92009-09-23 15:56:18 -07001511 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001512 } else {
1513 /*
1514 * We re-used an existing hierarchy - the new root (if
1515 * any) is not needed
1516 */
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001517 cgroup_drop_root(opts.new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001518 /* no subsys rebinding, so refcounts don't change */
1519 drop_parsed_module_refcounts(opts.subsys_bits);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001520 }
1521
Sukadev Bhattiprolua3ec9472009-03-04 12:06:34 -08001522 simple_set_mnt(mnt, sb);
Paul Menagec6d57f32009-09-23 15:56:19 -07001523 kfree(opts.release_agent);
1524 kfree(opts.name);
Sukadev Bhattiprolua3ec9472009-03-04 12:06:34 -08001525 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001526
1527 drop_new_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04001528 deactivate_locked_super(sb);
Ben Blumcf5d5942010-03-10 15:22:09 -08001529 drop_modules:
1530 drop_parsed_module_refcounts(opts.subsys_bits);
Paul Menagec6d57f32009-09-23 15:56:19 -07001531 out_err:
1532 kfree(opts.release_agent);
1533 kfree(opts.name);
1534
Paul Menageddbcc7e2007-10-18 23:39:30 -07001535 return ret;
1536}
1537
1538static void cgroup_kill_sb(struct super_block *sb) {
1539 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001540 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001541 int ret;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001542 struct cg_cgroup_link *link;
1543 struct cg_cgroup_link *saved_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001544
1545 BUG_ON(!root);
1546
1547 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001548 BUG_ON(!list_empty(&cgrp->children));
1549 BUG_ON(!list_empty(&cgrp->sibling));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001550
1551 mutex_lock(&cgroup_mutex);
1552
1553 /* Rebind all subsystems back to the default hierarchy */
1554 ret = rebind_subsystems(root, 0);
1555 /* Shouldn't be able to fail ... */
1556 BUG_ON(ret);
1557
Paul Menage817929e2007-10-18 23:39:36 -07001558 /*
1559 * Release all the links from css_sets to this hierarchy's
1560 * root cgroup
1561 */
1562 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001563
1564 list_for_each_entry_safe(link, saved_link, &cgrp->css_sets,
1565 cgrp_link_list) {
Paul Menage817929e2007-10-18 23:39:36 -07001566 list_del(&link->cg_link_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07001567 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07001568 kfree(link);
1569 }
1570 write_unlock(&css_set_lock);
1571
Paul Menage839ec542009-01-29 14:25:22 -08001572 if (!list_empty(&root->root_list)) {
1573 list_del(&root->root_list);
1574 root_count--;
1575 }
Li Zefane5f6a862009-01-07 18:07:41 -08001576
Paul Menageddbcc7e2007-10-18 23:39:30 -07001577 mutex_unlock(&cgroup_mutex);
1578
Paul Menageddbcc7e2007-10-18 23:39:30 -07001579 kill_litter_super(sb);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001580 cgroup_drop_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001581}
1582
1583static struct file_system_type cgroup_fs_type = {
1584 .name = "cgroup",
1585 .get_sb = cgroup_get_sb,
1586 .kill_sb = cgroup_kill_sb,
1587};
1588
Paul Menagebd89aab2007-10-18 23:40:44 -07001589static inline struct cgroup *__d_cgrp(struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001590{
1591 return dentry->d_fsdata;
1592}
1593
1594static inline struct cftype *__d_cft(struct dentry *dentry)
1595{
1596 return dentry->d_fsdata;
1597}
1598
Li Zefana043e3b2008-02-23 15:24:09 -08001599/**
1600 * cgroup_path - generate the path of a cgroup
1601 * @cgrp: the cgroup in question
1602 * @buf: the buffer to write the path into
1603 * @buflen: the length of the buffer
1604 *
Paul Menagea47295e2009-01-07 18:07:44 -08001605 * Called with cgroup_mutex held or else with an RCU-protected cgroup
1606 * reference. Writes path of cgroup into buf. Returns 0 on success,
1607 * -errno on error.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001608 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001609int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001610{
1611 char *start;
Paul Menagea47295e2009-01-07 18:07:44 -08001612 struct dentry *dentry = rcu_dereference(cgrp->dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001613
Paul Menagea47295e2009-01-07 18:07:44 -08001614 if (!dentry || cgrp == dummytop) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001615 /*
1616 * Inactive subsystems have no dentry for their root
1617 * cgroup
1618 */
1619 strcpy(buf, "/");
1620 return 0;
1621 }
1622
1623 start = buf + buflen;
1624
1625 *--start = '\0';
1626 for (;;) {
Paul Menagea47295e2009-01-07 18:07:44 -08001627 int len = dentry->d_name.len;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001628 if ((start -= len) < buf)
1629 return -ENAMETOOLONG;
Paul Menagebd89aab2007-10-18 23:40:44 -07001630 memcpy(start, cgrp->dentry->d_name.name, len);
1631 cgrp = cgrp->parent;
1632 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001633 break;
Paul Menagea47295e2009-01-07 18:07:44 -08001634 dentry = rcu_dereference(cgrp->dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07001635 if (!cgrp->parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001636 continue;
1637 if (--start < buf)
1638 return -ENAMETOOLONG;
1639 *start = '/';
1640 }
1641 memmove(buf, start, buf + buflen - start);
1642 return 0;
1643}
Ben Blum67523c42010-03-10 15:22:11 -08001644EXPORT_SYMBOL_GPL(cgroup_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001645
Li Zefana043e3b2008-02-23 15:24:09 -08001646/**
1647 * cgroup_attach_task - attach task 'tsk' to cgroup 'cgrp'
1648 * @cgrp: the cgroup the task is attaching to
1649 * @tsk: the task to be attached
Paul Menagebbcb81d2007-10-18 23:39:32 -07001650 *
Li Zefana043e3b2008-02-23 15:24:09 -08001651 * Call holding cgroup_mutex. May take task_lock of
1652 * the task 'tsk' during call.
Paul Menagebbcb81d2007-10-18 23:39:32 -07001653 */
Cliff Wickman956db3c2008-02-07 00:14:43 -08001654int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001655{
1656 int retval = 0;
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001657 struct cgroup_subsys *ss, *failed_ss = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07001658 struct cgroup *oldcgrp;
Lai Jiangshan77efecd2009-01-07 18:07:39 -08001659 struct css_set *cg;
Paul Menage817929e2007-10-18 23:39:36 -07001660 struct css_set *newcg;
Paul Menagebd89aab2007-10-18 23:40:44 -07001661 struct cgroupfs_root *root = cgrp->root;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001662
1663 /* Nothing to do if the task is already in that cgroup */
Paul Menage7717f7b2009-09-23 15:56:22 -07001664 oldcgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07001665 if (cgrp == oldcgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001666 return 0;
1667
1668 for_each_subsys(root, ss) {
1669 if (ss->can_attach) {
Ben Blumbe367d02009-09-23 15:56:31 -07001670 retval = ss->can_attach(ss, cgrp, tsk, false);
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001671 if (retval) {
1672 /*
1673 * Remember on which subsystem the can_attach()
1674 * failed, so that we only call cancel_attach()
1675 * against the subsystems whose can_attach()
1676 * succeeded. (See below)
1677 */
1678 failed_ss = ss;
1679 goto out;
1680 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07001681 }
1682 }
1683
Lai Jiangshan77efecd2009-01-07 18:07:39 -08001684 task_lock(tsk);
1685 cg = tsk->cgroups;
1686 get_css_set(cg);
1687 task_unlock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07001688 /*
1689 * Locate or allocate a new css_set for this task,
1690 * based on its final set of cgroups
1691 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001692 newcg = find_css_set(cg, cgrp);
Lai Jiangshan77efecd2009-01-07 18:07:39 -08001693 put_css_set(cg);
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001694 if (!newcg) {
1695 retval = -ENOMEM;
1696 goto out;
1697 }
Paul Menage817929e2007-10-18 23:39:36 -07001698
Paul Menagebbcb81d2007-10-18 23:39:32 -07001699 task_lock(tsk);
1700 if (tsk->flags & PF_EXITING) {
1701 task_unlock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07001702 put_css_set(newcg);
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001703 retval = -ESRCH;
1704 goto out;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001705 }
Paul Menage817929e2007-10-18 23:39:36 -07001706 rcu_assign_pointer(tsk->cgroups, newcg);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001707 task_unlock(tsk);
1708
Paul Menage817929e2007-10-18 23:39:36 -07001709 /* Update the css_set linked lists if we're using them */
1710 write_lock(&css_set_lock);
1711 if (!list_empty(&tsk->cg_list)) {
1712 list_del(&tsk->cg_list);
1713 list_add(&tsk->cg_list, &newcg->tasks);
1714 }
1715 write_unlock(&css_set_lock);
1716
Paul Menagebbcb81d2007-10-18 23:39:32 -07001717 for_each_subsys(root, ss) {
Paul Jacksone18f6312008-02-07 00:13:44 -08001718 if (ss->attach)
Ben Blumbe367d02009-09-23 15:56:31 -07001719 ss->attach(ss, cgrp, oldcgrp, tsk, false);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001720 }
Paul Menagebd89aab2007-10-18 23:40:44 -07001721 set_bit(CGRP_RELEASABLE, &oldcgrp->flags);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001722 synchronize_rcu();
Paul Menage817929e2007-10-18 23:39:36 -07001723 put_css_set(cg);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07001724
1725 /*
1726 * wake up rmdir() waiter. the rmdir should fail since the cgroup
1727 * is no longer empty.
1728 */
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07001729 cgroup_wakeup_rmdir_waiter(cgrp);
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001730out:
1731 if (retval) {
1732 for_each_subsys(root, ss) {
1733 if (ss == failed_ss)
1734 /*
1735 * This subsystem was the one that failed the
1736 * can_attach() check earlier, so we don't need
1737 * to call cancel_attach() against it or any
1738 * remaining subsystems.
1739 */
1740 break;
1741 if (ss->cancel_attach)
1742 ss->cancel_attach(ss, cgrp, tsk, false);
1743 }
1744 }
1745 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001746}
1747
1748/*
Paul Menageaf351022008-07-25 01:47:01 -07001749 * Attach task with pid 'pid' to cgroup 'cgrp'. Call with cgroup_mutex
1750 * held. May take task_lock of task
Paul Menagebbcb81d2007-10-18 23:39:32 -07001751 */
Paul Menageaf351022008-07-25 01:47:01 -07001752static int attach_task_by_pid(struct cgroup *cgrp, u64 pid)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001753{
Paul Menagebbcb81d2007-10-18 23:39:32 -07001754 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11001755 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001756 int ret;
1757
Paul Menagebbcb81d2007-10-18 23:39:32 -07001758 if (pid) {
1759 rcu_read_lock();
Pavel Emelyanov73507f32008-02-07 00:14:47 -08001760 tsk = find_task_by_vpid(pid);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001761 if (!tsk || tsk->flags & PF_EXITING) {
1762 rcu_read_unlock();
1763 return -ESRCH;
1764 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07001765
David Howellsc69e8d92008-11-14 10:39:19 +11001766 tcred = __task_cred(tsk);
1767 if (cred->euid &&
1768 cred->euid != tcred->uid &&
1769 cred->euid != tcred->suid) {
1770 rcu_read_unlock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07001771 return -EACCES;
1772 }
David Howellsc69e8d92008-11-14 10:39:19 +11001773 get_task_struct(tsk);
1774 rcu_read_unlock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07001775 } else {
1776 tsk = current;
1777 get_task_struct(tsk);
1778 }
1779
Cliff Wickman956db3c2008-02-07 00:14:43 -08001780 ret = cgroup_attach_task(cgrp, tsk);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001781 put_task_struct(tsk);
1782 return ret;
1783}
1784
Paul Menageaf351022008-07-25 01:47:01 -07001785static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid)
1786{
1787 int ret;
1788 if (!cgroup_lock_live_group(cgrp))
1789 return -ENODEV;
1790 ret = attach_task_by_pid(cgrp, pid);
1791 cgroup_unlock();
1792 return ret;
1793}
1794
Paul Menagee788e062008-07-25 01:46:59 -07001795/**
1796 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
1797 * @cgrp: the cgroup to be checked for liveness
1798 *
Paul Menage84eea842008-07-25 01:47:00 -07001799 * On success, returns true; the lock should be later released with
1800 * cgroup_unlock(). On failure returns false with no lock held.
Paul Menagee788e062008-07-25 01:46:59 -07001801 */
Paul Menage84eea842008-07-25 01:47:00 -07001802bool cgroup_lock_live_group(struct cgroup *cgrp)
Paul Menagee788e062008-07-25 01:46:59 -07001803{
1804 mutex_lock(&cgroup_mutex);
1805 if (cgroup_is_removed(cgrp)) {
1806 mutex_unlock(&cgroup_mutex);
1807 return false;
1808 }
1809 return true;
1810}
Ben Blum67523c42010-03-10 15:22:11 -08001811EXPORT_SYMBOL_GPL(cgroup_lock_live_group);
Paul Menagee788e062008-07-25 01:46:59 -07001812
1813static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
1814 const char *buffer)
1815{
1816 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
1817 if (!cgroup_lock_live_group(cgrp))
1818 return -ENODEV;
1819 strcpy(cgrp->root->release_agent_path, buffer);
Paul Menage84eea842008-07-25 01:47:00 -07001820 cgroup_unlock();
Paul Menagee788e062008-07-25 01:46:59 -07001821 return 0;
1822}
1823
1824static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
1825 struct seq_file *seq)
1826{
1827 if (!cgroup_lock_live_group(cgrp))
1828 return -ENODEV;
1829 seq_puts(seq, cgrp->root->release_agent_path);
1830 seq_putc(seq, '\n');
Paul Menage84eea842008-07-25 01:47:00 -07001831 cgroup_unlock();
Paul Menagee788e062008-07-25 01:46:59 -07001832 return 0;
1833}
1834
Paul Menage84eea842008-07-25 01:47:00 -07001835/* A buffer size big enough for numbers or short strings */
1836#define CGROUP_LOCAL_BUFFER_SIZE 64
1837
Paul Menagee73d2c62008-04-29 01:00:06 -07001838static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
Paul Menagef4c753b2008-04-29 00:59:56 -07001839 struct file *file,
1840 const char __user *userbuf,
1841 size_t nbytes, loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07001842{
Paul Menage84eea842008-07-25 01:47:00 -07001843 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menage355e0c42007-10-18 23:39:33 -07001844 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07001845 char *end;
1846
1847 if (!nbytes)
1848 return -EINVAL;
1849 if (nbytes >= sizeof(buffer))
1850 return -E2BIG;
1851 if (copy_from_user(buffer, userbuf, nbytes))
1852 return -EFAULT;
1853
1854 buffer[nbytes] = 0; /* nul-terminate */
Paul Menagee73d2c62008-04-29 01:00:06 -07001855 if (cft->write_u64) {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07001856 u64 val = simple_strtoull(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07001857 if (*end)
1858 return -EINVAL;
1859 retval = cft->write_u64(cgrp, cft, val);
1860 } else {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07001861 s64 val = simple_strtoll(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07001862 if (*end)
1863 return -EINVAL;
1864 retval = cft->write_s64(cgrp, cft, val);
1865 }
Paul Menage355e0c42007-10-18 23:39:33 -07001866 if (!retval)
1867 retval = nbytes;
1868 return retval;
1869}
1870
Paul Menagedb3b1492008-07-25 01:46:58 -07001871static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
1872 struct file *file,
1873 const char __user *userbuf,
1874 size_t nbytes, loff_t *unused_ppos)
1875{
Paul Menage84eea842008-07-25 01:47:00 -07001876 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagedb3b1492008-07-25 01:46:58 -07001877 int retval = 0;
1878 size_t max_bytes = cft->max_write_len;
1879 char *buffer = local_buffer;
1880
1881 if (!max_bytes)
1882 max_bytes = sizeof(local_buffer) - 1;
1883 if (nbytes >= max_bytes)
1884 return -E2BIG;
1885 /* Allocate a dynamic buffer if we need one */
1886 if (nbytes >= sizeof(local_buffer)) {
1887 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
1888 if (buffer == NULL)
1889 return -ENOMEM;
1890 }
Li Zefan5a3eb9f2008-07-29 22:33:18 -07001891 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
1892 retval = -EFAULT;
1893 goto out;
1894 }
Paul Menagedb3b1492008-07-25 01:46:58 -07001895
1896 buffer[nbytes] = 0; /* nul-terminate */
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07001897 retval = cft->write_string(cgrp, cft, strstrip(buffer));
Paul Menagedb3b1492008-07-25 01:46:58 -07001898 if (!retval)
1899 retval = nbytes;
Li Zefan5a3eb9f2008-07-29 22:33:18 -07001900out:
Paul Menagedb3b1492008-07-25 01:46:58 -07001901 if (buffer != local_buffer)
1902 kfree(buffer);
1903 return retval;
1904}
1905
Paul Menageddbcc7e2007-10-18 23:39:30 -07001906static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
1907 size_t nbytes, loff_t *ppos)
1908{
1909 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07001910 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001911
Li Zefan75139b82009-01-07 18:07:33 -08001912 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001913 return -ENODEV;
Paul Menage355e0c42007-10-18 23:39:33 -07001914 if (cft->write)
Paul Menagebd89aab2007-10-18 23:40:44 -07001915 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07001916 if (cft->write_u64 || cft->write_s64)
1917 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagedb3b1492008-07-25 01:46:58 -07001918 if (cft->write_string)
1919 return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07001920 if (cft->trigger) {
1921 int ret = cft->trigger(cgrp, (unsigned int)cft->private);
1922 return ret ? ret : nbytes;
1923 }
Paul Menage355e0c42007-10-18 23:39:33 -07001924 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001925}
1926
Paul Menagef4c753b2008-04-29 00:59:56 -07001927static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
1928 struct file *file,
1929 char __user *buf, size_t nbytes,
1930 loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001931{
Paul Menage84eea842008-07-25 01:47:00 -07001932 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagef4c753b2008-04-29 00:59:56 -07001933 u64 val = cft->read_u64(cgrp, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001934 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
1935
1936 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
1937}
1938
Paul Menagee73d2c62008-04-29 01:00:06 -07001939static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
1940 struct file *file,
1941 char __user *buf, size_t nbytes,
1942 loff_t *ppos)
1943{
Paul Menage84eea842008-07-25 01:47:00 -07001944 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagee73d2c62008-04-29 01:00:06 -07001945 s64 val = cft->read_s64(cgrp, cft);
1946 int len = sprintf(tmp, "%lld\n", (long long) val);
1947
1948 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
1949}
1950
Paul Menageddbcc7e2007-10-18 23:39:30 -07001951static ssize_t cgroup_file_read(struct file *file, char __user *buf,
1952 size_t nbytes, loff_t *ppos)
1953{
1954 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07001955 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001956
Li Zefan75139b82009-01-07 18:07:33 -08001957 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001958 return -ENODEV;
1959
1960 if (cft->read)
Paul Menagebd89aab2007-10-18 23:40:44 -07001961 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07001962 if (cft->read_u64)
1963 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07001964 if (cft->read_s64)
1965 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001966 return -EINVAL;
1967}
1968
Paul Menage91796562008-04-29 01:00:01 -07001969/*
1970 * seqfile ops/methods for returning structured data. Currently just
1971 * supports string->u64 maps, but can be extended in future.
1972 */
1973
1974struct cgroup_seqfile_state {
1975 struct cftype *cft;
1976 struct cgroup *cgroup;
1977};
1978
1979static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
1980{
1981 struct seq_file *sf = cb->state;
1982 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
1983}
1984
1985static int cgroup_seqfile_show(struct seq_file *m, void *arg)
1986{
1987 struct cgroup_seqfile_state *state = m->private;
1988 struct cftype *cft = state->cft;
Serge E. Hallyn29486df2008-04-29 01:00:14 -07001989 if (cft->read_map) {
1990 struct cgroup_map_cb cb = {
1991 .fill = cgroup_map_add,
1992 .state = m,
1993 };
1994 return cft->read_map(state->cgroup, cft, &cb);
1995 }
1996 return cft->read_seq_string(state->cgroup, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07001997}
1998
Adrian Bunk96930a62008-07-25 19:46:21 -07001999static int cgroup_seqfile_release(struct inode *inode, struct file *file)
Paul Menage91796562008-04-29 01:00:01 -07002000{
2001 struct seq_file *seq = file->private_data;
2002 kfree(seq->private);
2003 return single_release(inode, file);
2004}
2005
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002006static const struct file_operations cgroup_seqfile_operations = {
Paul Menage91796562008-04-29 01:00:01 -07002007 .read = seq_read,
Paul Menagee788e062008-07-25 01:46:59 -07002008 .write = cgroup_file_write,
Paul Menage91796562008-04-29 01:00:01 -07002009 .llseek = seq_lseek,
2010 .release = cgroup_seqfile_release,
2011};
2012
Paul Menageddbcc7e2007-10-18 23:39:30 -07002013static int cgroup_file_open(struct inode *inode, struct file *file)
2014{
2015 int err;
2016 struct cftype *cft;
2017
2018 err = generic_file_open(inode, file);
2019 if (err)
2020 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002021 cft = __d_cft(file->f_dentry);
Li Zefan75139b82009-01-07 18:07:33 -08002022
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002023 if (cft->read_map || cft->read_seq_string) {
Paul Menage91796562008-04-29 01:00:01 -07002024 struct cgroup_seqfile_state *state =
2025 kzalloc(sizeof(*state), GFP_USER);
2026 if (!state)
2027 return -ENOMEM;
2028 state->cft = cft;
2029 state->cgroup = __d_cgrp(file->f_dentry->d_parent);
2030 file->f_op = &cgroup_seqfile_operations;
2031 err = single_open(file, cgroup_seqfile_show, state);
2032 if (err < 0)
2033 kfree(state);
2034 } else if (cft->open)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002035 err = cft->open(inode, file);
2036 else
2037 err = 0;
2038
2039 return err;
2040}
2041
2042static int cgroup_file_release(struct inode *inode, struct file *file)
2043{
2044 struct cftype *cft = __d_cft(file->f_dentry);
2045 if (cft->release)
2046 return cft->release(inode, file);
2047 return 0;
2048}
2049
2050/*
2051 * cgroup_rename - Only allow simple rename of directories in place.
2052 */
2053static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2054 struct inode *new_dir, struct dentry *new_dentry)
2055{
2056 if (!S_ISDIR(old_dentry->d_inode->i_mode))
2057 return -ENOTDIR;
2058 if (new_dentry->d_inode)
2059 return -EEXIST;
2060 if (old_dir != new_dir)
2061 return -EIO;
2062 return simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2063}
2064
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002065static const struct file_operations cgroup_file_operations = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002066 .read = cgroup_file_read,
2067 .write = cgroup_file_write,
2068 .llseek = generic_file_llseek,
2069 .open = cgroup_file_open,
2070 .release = cgroup_file_release,
2071};
2072
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07002073static const struct inode_operations cgroup_dir_inode_operations = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002074 .lookup = simple_lookup,
2075 .mkdir = cgroup_mkdir,
2076 .rmdir = cgroup_rmdir,
2077 .rename = cgroup_rename,
2078};
2079
Li Zefan099fca32009-04-02 16:57:29 -07002080static int cgroup_create_file(struct dentry *dentry, mode_t mode,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002081 struct super_block *sb)
2082{
Al Viro3ba13d12009-02-20 06:02:22 +00002083 static const struct dentry_operations cgroup_dops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002084 .d_iput = cgroup_diput,
2085 };
2086
2087 struct inode *inode;
2088
2089 if (!dentry)
2090 return -ENOENT;
2091 if (dentry->d_inode)
2092 return -EEXIST;
2093
2094 inode = cgroup_new_inode(mode, sb);
2095 if (!inode)
2096 return -ENOMEM;
2097
2098 if (S_ISDIR(mode)) {
2099 inode->i_op = &cgroup_dir_inode_operations;
2100 inode->i_fop = &simple_dir_operations;
2101
2102 /* start off with i_nlink == 2 (for "." entry) */
2103 inc_nlink(inode);
2104
2105 /* start with the directory inode held, so that we can
2106 * populate it without racing with another mkdir */
Paul Menage817929e2007-10-18 23:39:36 -07002107 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002108 } else if (S_ISREG(mode)) {
2109 inode->i_size = 0;
2110 inode->i_fop = &cgroup_file_operations;
2111 }
2112 dentry->d_op = &cgroup_dops;
2113 d_instantiate(dentry, inode);
2114 dget(dentry); /* Extra count - pin the dentry in core */
2115 return 0;
2116}
2117
2118/*
Li Zefana043e3b2008-02-23 15:24:09 -08002119 * cgroup_create_dir - create a directory for an object.
2120 * @cgrp: the cgroup we create the directory for. It must have a valid
2121 * ->parent field. And we are going to fill its ->dentry field.
2122 * @dentry: dentry of the new cgroup
2123 * @mode: mode to set on new directory.
Paul Menageddbcc7e2007-10-18 23:39:30 -07002124 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002125static int cgroup_create_dir(struct cgroup *cgrp, struct dentry *dentry,
Li Zefan099fca32009-04-02 16:57:29 -07002126 mode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002127{
2128 struct dentry *parent;
2129 int error = 0;
2130
Paul Menagebd89aab2007-10-18 23:40:44 -07002131 parent = cgrp->parent->dentry;
2132 error = cgroup_create_file(dentry, S_IFDIR | mode, cgrp->root->sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002133 if (!error) {
Paul Menagebd89aab2007-10-18 23:40:44 -07002134 dentry->d_fsdata = cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002135 inc_nlink(parent->d_inode);
Paul Menagea47295e2009-01-07 18:07:44 -08002136 rcu_assign_pointer(cgrp->dentry, dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002137 dget(dentry);
2138 }
2139 dput(dentry);
2140
2141 return error;
2142}
2143
Li Zefan099fca32009-04-02 16:57:29 -07002144/**
2145 * cgroup_file_mode - deduce file mode of a control file
2146 * @cft: the control file in question
2147 *
2148 * returns cft->mode if ->mode is not 0
2149 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2150 * returns S_IRUGO if it has only a read handler
2151 * returns S_IWUSR if it has only a write hander
2152 */
2153static mode_t cgroup_file_mode(const struct cftype *cft)
2154{
2155 mode_t mode = 0;
2156
2157 if (cft->mode)
2158 return cft->mode;
2159
2160 if (cft->read || cft->read_u64 || cft->read_s64 ||
2161 cft->read_map || cft->read_seq_string)
2162 mode |= S_IRUGO;
2163
2164 if (cft->write || cft->write_u64 || cft->write_s64 ||
2165 cft->write_string || cft->trigger)
2166 mode |= S_IWUSR;
2167
2168 return mode;
2169}
2170
Paul Menagebd89aab2007-10-18 23:40:44 -07002171int cgroup_add_file(struct cgroup *cgrp,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002172 struct cgroup_subsys *subsys,
2173 const struct cftype *cft)
2174{
Paul Menagebd89aab2007-10-18 23:40:44 -07002175 struct dentry *dir = cgrp->dentry;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002176 struct dentry *dentry;
2177 int error;
Li Zefan099fca32009-04-02 16:57:29 -07002178 mode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002179
2180 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Paul Menagebd89aab2007-10-18 23:40:44 -07002181 if (subsys && !test_bit(ROOT_NOPREFIX, &cgrp->root->flags)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002182 strcpy(name, subsys->name);
2183 strcat(name, ".");
2184 }
2185 strcat(name, cft->name);
2186 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
2187 dentry = lookup_one_len(name, dir, strlen(name));
2188 if (!IS_ERR(dentry)) {
Li Zefan099fca32009-04-02 16:57:29 -07002189 mode = cgroup_file_mode(cft);
2190 error = cgroup_create_file(dentry, mode | S_IFREG,
Paul Menagebd89aab2007-10-18 23:40:44 -07002191 cgrp->root->sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002192 if (!error)
2193 dentry->d_fsdata = (void *)cft;
2194 dput(dentry);
2195 } else
2196 error = PTR_ERR(dentry);
2197 return error;
2198}
Ben Blume6a11052010-03-10 15:22:09 -08002199EXPORT_SYMBOL_GPL(cgroup_add_file);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002200
Paul Menagebd89aab2007-10-18 23:40:44 -07002201int cgroup_add_files(struct cgroup *cgrp,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002202 struct cgroup_subsys *subsys,
2203 const struct cftype cft[],
2204 int count)
2205{
2206 int i, err;
2207 for (i = 0; i < count; i++) {
Paul Menagebd89aab2007-10-18 23:40:44 -07002208 err = cgroup_add_file(cgrp, subsys, &cft[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002209 if (err)
2210 return err;
2211 }
2212 return 0;
2213}
Ben Blume6a11052010-03-10 15:22:09 -08002214EXPORT_SYMBOL_GPL(cgroup_add_files);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002215
Li Zefana043e3b2008-02-23 15:24:09 -08002216/**
2217 * cgroup_task_count - count the number of tasks in a cgroup.
2218 * @cgrp: the cgroup in question
2219 *
2220 * Return the number of tasks in the cgroup.
2221 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002222int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002223{
2224 int count = 0;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07002225 struct cg_cgroup_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002226
Paul Menage817929e2007-10-18 23:39:36 -07002227 read_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07002228 list_for_each_entry(link, &cgrp->css_sets, cgrp_link_list) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07002229 count += atomic_read(&link->cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002230 }
2231 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002232 return count;
2233}
2234
2235/*
Paul Menage817929e2007-10-18 23:39:36 -07002236 * Advance a list_head iterator. The iterator should be positioned at
2237 * the start of a css_set
2238 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002239static void cgroup_advance_iter(struct cgroup *cgrp,
Paul Menage7717f7b2009-09-23 15:56:22 -07002240 struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002241{
2242 struct list_head *l = it->cg_link;
2243 struct cg_cgroup_link *link;
2244 struct css_set *cg;
2245
2246 /* Advance to the next non-empty css_set */
2247 do {
2248 l = l->next;
Paul Menagebd89aab2007-10-18 23:40:44 -07002249 if (l == &cgrp->css_sets) {
Paul Menage817929e2007-10-18 23:39:36 -07002250 it->cg_link = NULL;
2251 return;
2252 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002253 link = list_entry(l, struct cg_cgroup_link, cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07002254 cg = link->cg;
2255 } while (list_empty(&cg->tasks));
2256 it->cg_link = l;
2257 it->task = cg->tasks.next;
2258}
2259
Cliff Wickman31a7df02008-02-07 00:14:42 -08002260/*
2261 * To reduce the fork() overhead for systems that are not actually
2262 * using their cgroups capability, we don't maintain the lists running
2263 * through each css_set to its tasks until we see the list actually
2264 * used - in other words after the first call to cgroup_iter_start().
2265 *
2266 * The tasklist_lock is not held here, as do_each_thread() and
2267 * while_each_thread() are protected by RCU.
2268 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07002269static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002270{
2271 struct task_struct *p, *g;
2272 write_lock(&css_set_lock);
2273 use_task_css_set_links = 1;
2274 do_each_thread(g, p) {
2275 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08002276 /*
2277 * We should check if the process is exiting, otherwise
2278 * it will race with cgroup_exit() in that the list
2279 * entry won't be deleted though the process has exited.
2280 */
2281 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Cliff Wickman31a7df02008-02-07 00:14:42 -08002282 list_add(&p->cg_list, &p->cgroups->tasks);
2283 task_unlock(p);
2284 } while_each_thread(g, p);
2285 write_unlock(&css_set_lock);
2286}
2287
Paul Menagebd89aab2007-10-18 23:40:44 -07002288void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002289{
2290 /*
2291 * The first time anyone tries to iterate across a cgroup,
2292 * we need to enable the list linking each css_set to its
2293 * tasks, and fix up all existing tasks.
2294 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08002295 if (!use_task_css_set_links)
2296 cgroup_enable_task_cg_lists();
2297
Paul Menage817929e2007-10-18 23:39:36 -07002298 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07002299 it->cg_link = &cgrp->css_sets;
2300 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07002301}
2302
Paul Menagebd89aab2007-10-18 23:40:44 -07002303struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -07002304 struct cgroup_iter *it)
2305{
2306 struct task_struct *res;
2307 struct list_head *l = it->task;
Lai Jiangshan2019f632009-01-07 18:07:36 -08002308 struct cg_cgroup_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07002309
2310 /* If the iterator cg is NULL, we have no tasks */
2311 if (!it->cg_link)
2312 return NULL;
2313 res = list_entry(l, struct task_struct, cg_list);
2314 /* Advance iterator to find next entry */
2315 l = l->next;
Lai Jiangshan2019f632009-01-07 18:07:36 -08002316 link = list_entry(it->cg_link, struct cg_cgroup_link, cgrp_link_list);
2317 if (l == &link->cg->tasks) {
Paul Menage817929e2007-10-18 23:39:36 -07002318 /* We reached the end of this task list - move on to
2319 * the next cg_cgroup_link */
Paul Menagebd89aab2007-10-18 23:40:44 -07002320 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07002321 } else {
2322 it->task = l;
2323 }
2324 return res;
2325}
2326
Paul Menagebd89aab2007-10-18 23:40:44 -07002327void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002328{
2329 read_unlock(&css_set_lock);
2330}
2331
Cliff Wickman31a7df02008-02-07 00:14:42 -08002332static inline int started_after_time(struct task_struct *t1,
2333 struct timespec *time,
2334 struct task_struct *t2)
2335{
2336 int start_diff = timespec_compare(&t1->start_time, time);
2337 if (start_diff > 0) {
2338 return 1;
2339 } else if (start_diff < 0) {
2340 return 0;
2341 } else {
2342 /*
2343 * Arbitrarily, if two processes started at the same
2344 * time, we'll say that the lower pointer value
2345 * started first. Note that t2 may have exited by now
2346 * so this may not be a valid pointer any longer, but
2347 * that's fine - it still serves to distinguish
2348 * between two tasks started (effectively) simultaneously.
2349 */
2350 return t1 > t2;
2351 }
2352}
2353
2354/*
2355 * This function is a callback from heap_insert() and is used to order
2356 * the heap.
2357 * In this case we order the heap in descending task start time.
2358 */
2359static inline int started_after(void *p1, void *p2)
2360{
2361 struct task_struct *t1 = p1;
2362 struct task_struct *t2 = p2;
2363 return started_after_time(t1, &t2->start_time, t2);
2364}
2365
2366/**
2367 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
2368 * @scan: struct cgroup_scanner containing arguments for the scan
2369 *
2370 * Arguments include pointers to callback functions test_task() and
2371 * process_task().
2372 * Iterate through all the tasks in a cgroup, calling test_task() for each,
2373 * and if it returns true, call process_task() for it also.
2374 * The test_task pointer may be NULL, meaning always true (select all tasks).
2375 * Effectively duplicates cgroup_iter_{start,next,end}()
2376 * but does not lock css_set_lock for the call to process_task().
2377 * The struct cgroup_scanner may be embedded in any structure of the caller's
2378 * creation.
2379 * It is guaranteed that process_task() will act on every task that
2380 * is a member of the cgroup for the duration of this call. This
2381 * function may or may not call process_task() for tasks that exit
2382 * or move to a different cgroup during the call, or are forked or
2383 * move into the cgroup during the call.
2384 *
2385 * Note that test_task() may be called with locks held, and may in some
2386 * situations be called multiple times for the same task, so it should
2387 * be cheap.
2388 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
2389 * pre-allocated and will be used for heap operations (and its "gt" member will
2390 * be overwritten), else a temporary heap will be used (allocation of which
2391 * may cause this function to fail).
2392 */
2393int cgroup_scan_tasks(struct cgroup_scanner *scan)
2394{
2395 int retval, i;
2396 struct cgroup_iter it;
2397 struct task_struct *p, *dropped;
2398 /* Never dereference latest_task, since it's not refcounted */
2399 struct task_struct *latest_task = NULL;
2400 struct ptr_heap tmp_heap;
2401 struct ptr_heap *heap;
2402 struct timespec latest_time = { 0, 0 };
2403
2404 if (scan->heap) {
2405 /* The caller supplied our heap and pre-allocated its memory */
2406 heap = scan->heap;
2407 heap->gt = &started_after;
2408 } else {
2409 /* We need to allocate our own heap memory */
2410 heap = &tmp_heap;
2411 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
2412 if (retval)
2413 /* cannot allocate the heap */
2414 return retval;
2415 }
2416
2417 again:
2418 /*
2419 * Scan tasks in the cgroup, using the scanner's "test_task" callback
2420 * to determine which are of interest, and using the scanner's
2421 * "process_task" callback to process any of them that need an update.
2422 * Since we don't want to hold any locks during the task updates,
2423 * gather tasks to be processed in a heap structure.
2424 * The heap is sorted by descending task start time.
2425 * If the statically-sized heap fills up, we overflow tasks that
2426 * started later, and in future iterations only consider tasks that
2427 * started after the latest task in the previous pass. This
2428 * guarantees forward progress and that we don't miss any tasks.
2429 */
2430 heap->size = 0;
2431 cgroup_iter_start(scan->cg, &it);
2432 while ((p = cgroup_iter_next(scan->cg, &it))) {
2433 /*
2434 * Only affect tasks that qualify per the caller's callback,
2435 * if he provided one
2436 */
2437 if (scan->test_task && !scan->test_task(p, scan))
2438 continue;
2439 /*
2440 * Only process tasks that started after the last task
2441 * we processed
2442 */
2443 if (!started_after_time(p, &latest_time, latest_task))
2444 continue;
2445 dropped = heap_insert(heap, p);
2446 if (dropped == NULL) {
2447 /*
2448 * The new task was inserted; the heap wasn't
2449 * previously full
2450 */
2451 get_task_struct(p);
2452 } else if (dropped != p) {
2453 /*
2454 * The new task was inserted, and pushed out a
2455 * different task
2456 */
2457 get_task_struct(p);
2458 put_task_struct(dropped);
2459 }
2460 /*
2461 * Else the new task was newer than anything already in
2462 * the heap and wasn't inserted
2463 */
2464 }
2465 cgroup_iter_end(scan->cg, &it);
2466
2467 if (heap->size) {
2468 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07002469 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08002470 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07002471 latest_time = q->start_time;
2472 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08002473 }
2474 /* Process the task per the caller's callback */
Paul Jackson4fe91d52008-04-29 00:59:55 -07002475 scan->process_task(q, scan);
2476 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002477 }
2478 /*
2479 * If we had to process any tasks at all, scan again
2480 * in case some of them were in the middle of forking
2481 * children that didn't get processed.
2482 * Not the most efficient way to do it, but it avoids
2483 * having to take callback_mutex in the fork path
2484 */
2485 goto again;
2486 }
2487 if (heap == &tmp_heap)
2488 heap_free(&tmp_heap);
2489 return 0;
2490}
2491
Paul Menage817929e2007-10-18 23:39:36 -07002492/*
Ben Blum102a7752009-09-23 15:56:26 -07002493 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07002494 *
2495 * Reading this file can return large amounts of data if a cgroup has
2496 * *lots* of attached tasks. So it may need several calls to read(),
2497 * but we cannot guarantee that the information we produce is correct
2498 * unless we produce it entirely atomically.
2499 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07002500 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07002501
2502/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07002503 * The following two functions "fix" the issue where there are more pids
2504 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
2505 * TODO: replace with a kernel-wide solution to this problem
2506 */
2507#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
2508static void *pidlist_allocate(int count)
2509{
2510 if (PIDLIST_TOO_LARGE(count))
2511 return vmalloc(count * sizeof(pid_t));
2512 else
2513 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
2514}
2515static void pidlist_free(void *p)
2516{
2517 if (is_vmalloc_addr(p))
2518 vfree(p);
2519 else
2520 kfree(p);
2521}
2522static void *pidlist_resize(void *p, int newcount)
2523{
2524 void *newlist;
2525 /* note: if new alloc fails, old p will still be valid either way */
2526 if (is_vmalloc_addr(p)) {
2527 newlist = vmalloc(newcount * sizeof(pid_t));
2528 if (!newlist)
2529 return NULL;
2530 memcpy(newlist, p, newcount * sizeof(pid_t));
2531 vfree(p);
2532 } else {
2533 newlist = krealloc(p, newcount * sizeof(pid_t), GFP_KERNEL);
2534 }
2535 return newlist;
2536}
2537
2538/*
Ben Blum102a7752009-09-23 15:56:26 -07002539 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
2540 * If the new stripped list is sufficiently smaller and there's enough memory
2541 * to allocate a new buffer, will let go of the unneeded memory. Returns the
2542 * number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07002543 */
Ben Blum102a7752009-09-23 15:56:26 -07002544/* is the size difference enough that we should re-allocate the array? */
2545#define PIDLIST_REALLOC_DIFFERENCE(old, new) ((old) - PAGE_SIZE >= (new))
2546static int pidlist_uniq(pid_t **p, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002547{
Ben Blum102a7752009-09-23 15:56:26 -07002548 int src, dest = 1;
2549 pid_t *list = *p;
2550 pid_t *newlist;
2551
2552 /*
2553 * we presume the 0th element is unique, so i starts at 1. trivial
2554 * edge cases first; no work needs to be done for either
2555 */
2556 if (length == 0 || length == 1)
2557 return length;
2558 /* src and dest walk down the list; dest counts unique elements */
2559 for (src = 1; src < length; src++) {
2560 /* find next unique element */
2561 while (list[src] == list[src-1]) {
2562 src++;
2563 if (src == length)
2564 goto after;
2565 }
2566 /* dest always points to where the next unique element goes */
2567 list[dest] = list[src];
2568 dest++;
2569 }
2570after:
2571 /*
2572 * if the length difference is large enough, we want to allocate a
2573 * smaller buffer to save memory. if this fails due to out of memory,
2574 * we'll just stay with what we've got.
2575 */
2576 if (PIDLIST_REALLOC_DIFFERENCE(length, dest)) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07002577 newlist = pidlist_resize(list, dest);
Ben Blum102a7752009-09-23 15:56:26 -07002578 if (newlist)
2579 *p = newlist;
2580 }
2581 return dest;
2582}
2583
2584static int cmppid(const void *a, const void *b)
2585{
2586 return *(pid_t *)a - *(pid_t *)b;
2587}
2588
2589/*
Ben Blum72a8cb32009-09-23 15:56:27 -07002590 * find the appropriate pidlist for our purpose (given procs vs tasks)
2591 * returns with the lock on that pidlist already held, and takes care
2592 * of the use count, or returns NULL with no locks held if we're out of
2593 * memory.
2594 */
2595static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
2596 enum cgroup_filetype type)
2597{
2598 struct cgroup_pidlist *l;
2599 /* don't need task_nsproxy() if we're looking at ourself */
Li Zefanb70cc5f2010-03-10 15:22:12 -08002600 struct pid_namespace *ns = current->nsproxy->pid_ns;
2601
Ben Blum72a8cb32009-09-23 15:56:27 -07002602 /*
2603 * We can't drop the pidlist_mutex before taking the l->mutex in case
2604 * the last ref-holder is trying to remove l from the list at the same
2605 * time. Holding the pidlist_mutex precludes somebody taking whichever
2606 * list we find out from under us - compare release_pid_array().
2607 */
2608 mutex_lock(&cgrp->pidlist_mutex);
2609 list_for_each_entry(l, &cgrp->pidlists, links) {
2610 if (l->key.type == type && l->key.ns == ns) {
Ben Blum72a8cb32009-09-23 15:56:27 -07002611 /* make sure l doesn't vanish out from under us */
2612 down_write(&l->mutex);
2613 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07002614 return l;
2615 }
2616 }
2617 /* entry not found; create a new one */
2618 l = kmalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
2619 if (!l) {
2620 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07002621 return l;
2622 }
2623 init_rwsem(&l->mutex);
2624 down_write(&l->mutex);
2625 l->key.type = type;
Li Zefanb70cc5f2010-03-10 15:22:12 -08002626 l->key.ns = get_pid_ns(ns);
Ben Blum72a8cb32009-09-23 15:56:27 -07002627 l->use_count = 0; /* don't increment here */
2628 l->list = NULL;
2629 l->owner = cgrp;
2630 list_add(&l->links, &cgrp->pidlists);
2631 mutex_unlock(&cgrp->pidlist_mutex);
2632 return l;
2633}
2634
2635/*
Ben Blum102a7752009-09-23 15:56:26 -07002636 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
2637 */
Ben Blum72a8cb32009-09-23 15:56:27 -07002638static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
2639 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07002640{
2641 pid_t *array;
2642 int length;
2643 int pid, n = 0; /* used for populating the array */
Paul Menage817929e2007-10-18 23:39:36 -07002644 struct cgroup_iter it;
2645 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07002646 struct cgroup_pidlist *l;
2647
2648 /*
2649 * If cgroup gets more users after we read count, we won't have
2650 * enough space - tough. This race is indistinguishable to the
2651 * caller from the case that the additional cgroup users didn't
2652 * show up until sometime later on.
2653 */
2654 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07002655 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07002656 if (!array)
2657 return -ENOMEM;
2658 /* now, populate the array */
Paul Menagebd89aab2007-10-18 23:40:44 -07002659 cgroup_iter_start(cgrp, &it);
2660 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Ben Blum102a7752009-09-23 15:56:26 -07002661 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07002662 break;
Ben Blum102a7752009-09-23 15:56:26 -07002663 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07002664 if (type == CGROUP_FILE_PROCS)
2665 pid = task_tgid_vnr(tsk);
2666 else
2667 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07002668 if (pid > 0) /* make sure to only use valid results */
2669 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07002670 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002671 cgroup_iter_end(cgrp, &it);
Ben Blum102a7752009-09-23 15:56:26 -07002672 length = n;
2673 /* now sort & (if procs) strip out duplicates */
2674 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07002675 if (type == CGROUP_FILE_PROCS)
Ben Blum102a7752009-09-23 15:56:26 -07002676 length = pidlist_uniq(&array, length);
Ben Blum72a8cb32009-09-23 15:56:27 -07002677 l = cgroup_pidlist_find(cgrp, type);
2678 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07002679 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07002680 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07002681 }
Ben Blum72a8cb32009-09-23 15:56:27 -07002682 /* store array, freeing old if necessary - lock already held */
Ben Blumd1d9fd32009-09-23 15:56:28 -07002683 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07002684 l->list = array;
2685 l->length = length;
2686 l->use_count++;
2687 up_write(&l->mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07002688 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07002689 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002690}
2691
Balbir Singh846c7bb2007-10-18 23:39:44 -07002692/**
Li Zefana043e3b2008-02-23 15:24:09 -08002693 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07002694 * @stats: cgroupstats to fill information into
2695 * @dentry: A dentry entry belonging to the cgroup for which stats have
2696 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08002697 *
2698 * Build and fill cgroupstats so that taskstats can export it to user
2699 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07002700 */
2701int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
2702{
2703 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07002704 struct cgroup *cgrp;
Balbir Singh846c7bb2007-10-18 23:39:44 -07002705 struct cgroup_iter it;
2706 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08002707
Balbir Singh846c7bb2007-10-18 23:39:44 -07002708 /*
Li Zefan33d283b2008-11-19 15:36:48 -08002709 * Validate dentry by checking the superblock operations,
2710 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07002711 */
Li Zefan33d283b2008-11-19 15:36:48 -08002712 if (dentry->d_sb->s_op != &cgroup_ops ||
2713 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07002714 goto err;
2715
2716 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07002717 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07002718
Paul Menagebd89aab2007-10-18 23:40:44 -07002719 cgroup_iter_start(cgrp, &it);
2720 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07002721 switch (tsk->state) {
2722 case TASK_RUNNING:
2723 stats->nr_running++;
2724 break;
2725 case TASK_INTERRUPTIBLE:
2726 stats->nr_sleeping++;
2727 break;
2728 case TASK_UNINTERRUPTIBLE:
2729 stats->nr_uninterruptible++;
2730 break;
2731 case TASK_STOPPED:
2732 stats->nr_stopped++;
2733 break;
2734 default:
2735 if (delayacct_is_task_waiting_on_io(tsk))
2736 stats->nr_io_wait++;
2737 break;
2738 }
2739 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002740 cgroup_iter_end(cgrp, &it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07002741
Balbir Singh846c7bb2007-10-18 23:39:44 -07002742err:
2743 return ret;
2744}
2745
Paul Menage8f3ff202009-09-23 15:56:25 -07002746
Paul Menagecc31edc2008-10-18 20:28:04 -07002747/*
Ben Blum102a7752009-09-23 15:56:26 -07002748 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07002749 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07002750 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07002751 */
2752
Ben Blum102a7752009-09-23 15:56:26 -07002753static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07002754{
2755 /*
2756 * Initially we receive a position value that corresponds to
2757 * one more than the last pid shown (or 0 on the first call or
2758 * after a seek to the start). Use a binary-search to find the
2759 * next pid to display, if any
2760 */
Ben Blum102a7752009-09-23 15:56:26 -07002761 struct cgroup_pidlist *l = s->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07002762 int index = 0, pid = *pos;
2763 int *iter;
2764
Ben Blum102a7752009-09-23 15:56:26 -07002765 down_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07002766 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07002767 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11002768
Paul Menagecc31edc2008-10-18 20:28:04 -07002769 while (index < end) {
2770 int mid = (index + end) / 2;
Ben Blum102a7752009-09-23 15:56:26 -07002771 if (l->list[mid] == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07002772 index = mid;
2773 break;
Ben Blum102a7752009-09-23 15:56:26 -07002774 } else if (l->list[mid] <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07002775 index = mid + 1;
2776 else
2777 end = mid;
2778 }
2779 }
2780 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07002781 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07002782 return NULL;
2783 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07002784 iter = l->list + index;
Paul Menagecc31edc2008-10-18 20:28:04 -07002785 *pos = *iter;
2786 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002787}
2788
Ben Blum102a7752009-09-23 15:56:26 -07002789static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07002790{
Ben Blum102a7752009-09-23 15:56:26 -07002791 struct cgroup_pidlist *l = s->private;
2792 up_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07002793}
2794
Ben Blum102a7752009-09-23 15:56:26 -07002795static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07002796{
Ben Blum102a7752009-09-23 15:56:26 -07002797 struct cgroup_pidlist *l = s->private;
2798 pid_t *p = v;
2799 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07002800 /*
2801 * Advance to the next pid in the array. If this goes off the
2802 * end, we're done
2803 */
2804 p++;
2805 if (p >= end) {
2806 return NULL;
2807 } else {
2808 *pos = *p;
2809 return p;
2810 }
2811}
2812
Ben Blum102a7752009-09-23 15:56:26 -07002813static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07002814{
2815 return seq_printf(s, "%d\n", *(int *)v);
2816}
2817
Ben Blum102a7752009-09-23 15:56:26 -07002818/*
2819 * seq_operations functions for iterating on pidlists through seq_file -
2820 * independent of whether it's tasks or procs
2821 */
2822static const struct seq_operations cgroup_pidlist_seq_operations = {
2823 .start = cgroup_pidlist_start,
2824 .stop = cgroup_pidlist_stop,
2825 .next = cgroup_pidlist_next,
2826 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07002827};
2828
Ben Blum102a7752009-09-23 15:56:26 -07002829static void cgroup_release_pid_array(struct cgroup_pidlist *l)
Paul Menagecc31edc2008-10-18 20:28:04 -07002830{
Ben Blum72a8cb32009-09-23 15:56:27 -07002831 /*
2832 * the case where we're the last user of this particular pidlist will
2833 * have us remove it from the cgroup's list, which entails taking the
2834 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
2835 * pidlist_mutex, we have to take pidlist_mutex first.
2836 */
2837 mutex_lock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07002838 down_write(&l->mutex);
2839 BUG_ON(!l->use_count);
2840 if (!--l->use_count) {
Ben Blum72a8cb32009-09-23 15:56:27 -07002841 /* we're the last user if refcount is 0; remove and free */
2842 list_del(&l->links);
2843 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07002844 pidlist_free(l->list);
Ben Blum72a8cb32009-09-23 15:56:27 -07002845 put_pid_ns(l->key.ns);
2846 up_write(&l->mutex);
2847 kfree(l);
2848 return;
Paul Menagecc31edc2008-10-18 20:28:04 -07002849 }
Ben Blum72a8cb32009-09-23 15:56:27 -07002850 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07002851 up_write(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07002852}
2853
Ben Blum102a7752009-09-23 15:56:26 -07002854static int cgroup_pidlist_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002855{
Ben Blum102a7752009-09-23 15:56:26 -07002856 struct cgroup_pidlist *l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002857 if (!(file->f_mode & FMODE_READ))
2858 return 0;
Ben Blum102a7752009-09-23 15:56:26 -07002859 /*
2860 * the seq_file will only be initialized if the file was opened for
2861 * reading; hence we check if it's not null only in that case.
2862 */
2863 l = ((struct seq_file *)file->private_data)->private;
2864 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07002865 return seq_release(inode, file);
2866}
2867
Ben Blum102a7752009-09-23 15:56:26 -07002868static const struct file_operations cgroup_pidlist_operations = {
Paul Menagecc31edc2008-10-18 20:28:04 -07002869 .read = seq_read,
2870 .llseek = seq_lseek,
2871 .write = cgroup_file_write,
Ben Blum102a7752009-09-23 15:56:26 -07002872 .release = cgroup_pidlist_release,
Paul Menagecc31edc2008-10-18 20:28:04 -07002873};
2874
2875/*
Ben Blum102a7752009-09-23 15:56:26 -07002876 * The following functions handle opens on a file that displays a pidlist
2877 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
2878 * in the cgroup.
Paul Menagecc31edc2008-10-18 20:28:04 -07002879 */
Ben Blum102a7752009-09-23 15:56:26 -07002880/* helper function for the two below it */
Ben Blum72a8cb32009-09-23 15:56:27 -07002881static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
Paul Menagecc31edc2008-10-18 20:28:04 -07002882{
2883 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Ben Blum72a8cb32009-09-23 15:56:27 -07002884 struct cgroup_pidlist *l;
Paul Menagecc31edc2008-10-18 20:28:04 -07002885 int retval;
2886
2887 /* Nothing to do for write-only files */
2888 if (!(file->f_mode & FMODE_READ))
2889 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002890
Ben Blum102a7752009-09-23 15:56:26 -07002891 /* have the array populated */
Ben Blum72a8cb32009-09-23 15:56:27 -07002892 retval = pidlist_array_load(cgrp, type, &l);
Ben Blum102a7752009-09-23 15:56:26 -07002893 if (retval)
2894 return retval;
2895 /* configure file information */
2896 file->f_op = &cgroup_pidlist_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002897
Ben Blum102a7752009-09-23 15:56:26 -07002898 retval = seq_open(file, &cgroup_pidlist_seq_operations);
Paul Menagecc31edc2008-10-18 20:28:04 -07002899 if (retval) {
Ben Blum102a7752009-09-23 15:56:26 -07002900 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07002901 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002902 }
Ben Blum102a7752009-09-23 15:56:26 -07002903 ((struct seq_file *)file->private_data)->private = l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002904 return 0;
2905}
Ben Blum102a7752009-09-23 15:56:26 -07002906static int cgroup_tasks_open(struct inode *unused, struct file *file)
2907{
Ben Blum72a8cb32009-09-23 15:56:27 -07002908 return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
Ben Blum102a7752009-09-23 15:56:26 -07002909}
2910static int cgroup_procs_open(struct inode *unused, struct file *file)
2911{
Ben Blum72a8cb32009-09-23 15:56:27 -07002912 return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
Ben Blum102a7752009-09-23 15:56:26 -07002913}
Paul Menagebbcb81d2007-10-18 23:39:32 -07002914
Paul Menagebd89aab2007-10-18 23:40:44 -07002915static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
Paul Menage81a6a5c2007-10-18 23:39:38 -07002916 struct cftype *cft)
2917{
Paul Menagebd89aab2007-10-18 23:40:44 -07002918 return notify_on_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07002919}
2920
Paul Menage6379c102008-07-25 01:47:01 -07002921static int cgroup_write_notify_on_release(struct cgroup *cgrp,
2922 struct cftype *cft,
2923 u64 val)
2924{
2925 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
2926 if (val)
2927 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
2928 else
2929 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
2930 return 0;
2931}
2932
Paul Menagebbcb81d2007-10-18 23:39:32 -07002933/*
2934 * for the common functions, 'private' gives the type of file
2935 */
Ben Blum102a7752009-09-23 15:56:26 -07002936/* for hysterical raisins, we can't put this on the older files */
2937#define CGROUP_FILE_GENERIC_PREFIX "cgroup."
Paul Menage81a6a5c2007-10-18 23:39:38 -07002938static struct cftype files[] = {
2939 {
2940 .name = "tasks",
2941 .open = cgroup_tasks_open,
Paul Menageaf351022008-07-25 01:47:01 -07002942 .write_u64 = cgroup_tasks_write,
Ben Blum102a7752009-09-23 15:56:26 -07002943 .release = cgroup_pidlist_release,
Li Zefan099fca32009-04-02 16:57:29 -07002944 .mode = S_IRUGO | S_IWUSR,
Paul Menage81a6a5c2007-10-18 23:39:38 -07002945 },
Ben Blum102a7752009-09-23 15:56:26 -07002946 {
2947 .name = CGROUP_FILE_GENERIC_PREFIX "procs",
2948 .open = cgroup_procs_open,
2949 /* .write_u64 = cgroup_procs_write, TODO */
2950 .release = cgroup_pidlist_release,
2951 .mode = S_IRUGO,
2952 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07002953 {
2954 .name = "notify_on_release",
Paul Menagef4c753b2008-04-29 00:59:56 -07002955 .read_u64 = cgroup_read_notify_on_release,
Paul Menage6379c102008-07-25 01:47:01 -07002956 .write_u64 = cgroup_write_notify_on_release,
Paul Menage81a6a5c2007-10-18 23:39:38 -07002957 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07002958};
2959
2960static struct cftype cft_release_agent = {
2961 .name = "release_agent",
Paul Menagee788e062008-07-25 01:46:59 -07002962 .read_seq_string = cgroup_release_agent_show,
2963 .write_string = cgroup_release_agent_write,
2964 .max_write_len = PATH_MAX,
Paul Menagebbcb81d2007-10-18 23:39:32 -07002965};
2966
Paul Menagebd89aab2007-10-18 23:40:44 -07002967static int cgroup_populate_dir(struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002968{
2969 int err;
2970 struct cgroup_subsys *ss;
2971
2972 /* First clear out any existing files */
Paul Menagebd89aab2007-10-18 23:40:44 -07002973 cgroup_clear_directory(cgrp->dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002974
Paul Menagebd89aab2007-10-18 23:40:44 -07002975 err = cgroup_add_files(cgrp, NULL, files, ARRAY_SIZE(files));
Paul Menagebbcb81d2007-10-18 23:39:32 -07002976 if (err < 0)
2977 return err;
2978
Paul Menagebd89aab2007-10-18 23:40:44 -07002979 if (cgrp == cgrp->top_cgroup) {
2980 if ((err = cgroup_add_file(cgrp, NULL, &cft_release_agent)) < 0)
Paul Menage81a6a5c2007-10-18 23:39:38 -07002981 return err;
2982 }
2983
Paul Menagebd89aab2007-10-18 23:40:44 -07002984 for_each_subsys(cgrp->root, ss) {
2985 if (ss->populate && (err = ss->populate(ss, cgrp)) < 0)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002986 return err;
2987 }
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07002988 /* This cgroup is ready now */
2989 for_each_subsys(cgrp->root, ss) {
2990 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
2991 /*
2992 * Update id->css pointer and make this css visible from
2993 * CSS ID functions. This pointer will be dereferened
2994 * from RCU-read-side without locks.
2995 */
2996 if (css->id)
2997 rcu_assign_pointer(css->id->css, css);
2998 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002999
3000 return 0;
3001}
3002
3003static void init_cgroup_css(struct cgroup_subsys_state *css,
3004 struct cgroup_subsys *ss,
Paul Menagebd89aab2007-10-18 23:40:44 -07003005 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003006{
Paul Menagebd89aab2007-10-18 23:40:44 -07003007 css->cgroup = cgrp;
Paul Menagee7c5ec92009-01-07 18:08:38 -08003008 atomic_set(&css->refcnt, 1);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003009 css->flags = 0;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07003010 css->id = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003011 if (cgrp == dummytop)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003012 set_bit(CSS_ROOT, &css->flags);
Paul Menagebd89aab2007-10-18 23:40:44 -07003013 BUG_ON(cgrp->subsys[ss->subsys_id]);
3014 cgrp->subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003015}
3016
Paul Menage999cd8a2009-01-07 18:08:36 -08003017static void cgroup_lock_hierarchy(struct cgroupfs_root *root)
3018{
3019 /* We need to take each hierarchy_mutex in a consistent order */
3020 int i;
3021
Ben Blumaae8aab2010-03-10 15:22:07 -08003022 /*
3023 * No worry about a race with rebind_subsystems that might mess up the
3024 * locking order, since both parties are under cgroup_mutex.
3025 */
Paul Menage999cd8a2009-01-07 18:08:36 -08003026 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3027 struct cgroup_subsys *ss = subsys[i];
Ben Blumaae8aab2010-03-10 15:22:07 -08003028 if (ss == NULL)
3029 continue;
Paul Menage999cd8a2009-01-07 18:08:36 -08003030 if (ss->root == root)
Li Zefancfebe562009-02-11 13:04:36 -08003031 mutex_lock(&ss->hierarchy_mutex);
Paul Menage999cd8a2009-01-07 18:08:36 -08003032 }
3033}
3034
3035static void cgroup_unlock_hierarchy(struct cgroupfs_root *root)
3036{
3037 int i;
3038
3039 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3040 struct cgroup_subsys *ss = subsys[i];
Ben Blumaae8aab2010-03-10 15:22:07 -08003041 if (ss == NULL)
3042 continue;
Paul Menage999cd8a2009-01-07 18:08:36 -08003043 if (ss->root == root)
3044 mutex_unlock(&ss->hierarchy_mutex);
3045 }
3046}
3047
Paul Menageddbcc7e2007-10-18 23:39:30 -07003048/*
Li Zefana043e3b2008-02-23 15:24:09 -08003049 * cgroup_create - create a cgroup
3050 * @parent: cgroup that will be parent of the new cgroup
3051 * @dentry: dentry of the new cgroup
3052 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07003053 *
Li Zefana043e3b2008-02-23 15:24:09 -08003054 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07003055 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07003056static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Li Zefan099fca32009-04-02 16:57:29 -07003057 mode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003058{
Paul Menagebd89aab2007-10-18 23:40:44 -07003059 struct cgroup *cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003060 struct cgroupfs_root *root = parent->root;
3061 int err = 0;
3062 struct cgroup_subsys *ss;
3063 struct super_block *sb = root->sb;
3064
Paul Menagebd89aab2007-10-18 23:40:44 -07003065 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
3066 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003067 return -ENOMEM;
3068
3069 /* Grab a reference on the superblock so the hierarchy doesn't
3070 * get deleted on unmount if there are child cgroups. This
3071 * can be done outside cgroup_mutex, since the sb can't
3072 * disappear while someone has an open control file on the
3073 * fs */
3074 atomic_inc(&sb->s_active);
3075
3076 mutex_lock(&cgroup_mutex);
3077
Paul Menagecc31edc2008-10-18 20:28:04 -07003078 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003079
Paul Menagebd89aab2007-10-18 23:40:44 -07003080 cgrp->parent = parent;
3081 cgrp->root = parent->root;
3082 cgrp->top_cgroup = parent->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003083
Li Zefanb6abdb02008-03-04 14:28:19 -08003084 if (notify_on_release(parent))
3085 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3086
Paul Menageddbcc7e2007-10-18 23:39:30 -07003087 for_each_subsys(root, ss) {
Paul Menagebd89aab2007-10-18 23:40:44 -07003088 struct cgroup_subsys_state *css = ss->create(ss, cgrp);
Li Zefan4528fd02010-02-02 13:44:10 -08003089
Paul Menageddbcc7e2007-10-18 23:39:30 -07003090 if (IS_ERR(css)) {
3091 err = PTR_ERR(css);
3092 goto err_destroy;
3093 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003094 init_cgroup_css(css, ss, cgrp);
Li Zefan4528fd02010-02-02 13:44:10 -08003095 if (ss->use_id) {
3096 err = alloc_css_id(ss, parent, cgrp);
3097 if (err)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07003098 goto err_destroy;
Li Zefan4528fd02010-02-02 13:44:10 -08003099 }
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07003100 /* At error, ->destroy() callback has to free assigned ID. */
Paul Menageddbcc7e2007-10-18 23:39:30 -07003101 }
3102
Paul Menage999cd8a2009-01-07 18:08:36 -08003103 cgroup_lock_hierarchy(root);
Paul Menagebd89aab2007-10-18 23:40:44 -07003104 list_add(&cgrp->sibling, &cgrp->parent->children);
Paul Menage999cd8a2009-01-07 18:08:36 -08003105 cgroup_unlock_hierarchy(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003106 root->number_of_cgroups++;
3107
Paul Menagebd89aab2007-10-18 23:40:44 -07003108 err = cgroup_create_dir(cgrp, dentry, mode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003109 if (err < 0)
3110 goto err_remove;
3111
3112 /* The cgroup directory was pre-locked for us */
Paul Menagebd89aab2007-10-18 23:40:44 -07003113 BUG_ON(!mutex_is_locked(&cgrp->dentry->d_inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07003114
Paul Menagebd89aab2007-10-18 23:40:44 -07003115 err = cgroup_populate_dir(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003116 /* If err < 0, we have a half-filled directory - oh well ;) */
3117
3118 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07003119 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003120
3121 return 0;
3122
3123 err_remove:
3124
KAMEZAWA Hiroyukibaef99a2009-01-29 14:25:10 -08003125 cgroup_lock_hierarchy(root);
Paul Menagebd89aab2007-10-18 23:40:44 -07003126 list_del(&cgrp->sibling);
KAMEZAWA Hiroyukibaef99a2009-01-29 14:25:10 -08003127 cgroup_unlock_hierarchy(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003128 root->number_of_cgroups--;
3129
3130 err_destroy:
3131
3132 for_each_subsys(root, ss) {
Paul Menagebd89aab2007-10-18 23:40:44 -07003133 if (cgrp->subsys[ss->subsys_id])
3134 ss->destroy(ss, cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003135 }
3136
3137 mutex_unlock(&cgroup_mutex);
3138
3139 /* Release the reference count that we took on the superblock */
3140 deactivate_super(sb);
3141
Paul Menagebd89aab2007-10-18 23:40:44 -07003142 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003143 return err;
3144}
3145
3146static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode)
3147{
3148 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
3149
3150 /* the vfs holds inode->i_mutex already */
3151 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
3152}
3153
Li Zefan55b6fd02008-07-29 22:33:20 -07003154static int cgroup_has_css_refs(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003155{
3156 /* Check the reference count on each subsystem. Since we
3157 * already established that there are no tasks in the
Paul Menagee7c5ec92009-01-07 18:08:38 -08003158 * cgroup, if the css refcount is also 1, then there should
Paul Menage81a6a5c2007-10-18 23:39:38 -07003159 * be no outstanding references, so the subsystem is safe to
3160 * destroy. We scan across all subsystems rather than using
3161 * the per-hierarchy linked list of mounted subsystems since
3162 * we can be called via check_for_release() with no
3163 * synchronization other than RCU, and the subsystem linked
3164 * list isn't RCU-safe */
3165 int i;
Ben Blumaae8aab2010-03-10 15:22:07 -08003166 /*
3167 * We won't need to lock the subsys array, because the subsystems
3168 * we're concerned about aren't going anywhere since our cgroup root
3169 * has a reference on them.
3170 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07003171 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3172 struct cgroup_subsys *ss = subsys[i];
3173 struct cgroup_subsys_state *css;
Ben Blumaae8aab2010-03-10 15:22:07 -08003174 /* Skip subsystems not present or not in this hierarchy */
3175 if (ss == NULL || ss->root != cgrp->root)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003176 continue;
Paul Menagebd89aab2007-10-18 23:40:44 -07003177 css = cgrp->subsys[ss->subsys_id];
Paul Menage81a6a5c2007-10-18 23:39:38 -07003178 /* When called from check_for_release() it's possible
3179 * that by this point the cgroup has been removed
3180 * and the css deleted. But a false-positive doesn't
3181 * matter, since it can only happen if the cgroup
3182 * has been deleted and hence no longer needs the
3183 * release agent to be called anyway. */
Paul Menagee7c5ec92009-01-07 18:08:38 -08003184 if (css && (atomic_read(&css->refcnt) > 1))
Paul Menage81a6a5c2007-10-18 23:39:38 -07003185 return 1;
Paul Menage81a6a5c2007-10-18 23:39:38 -07003186 }
3187 return 0;
3188}
3189
Paul Menagee7c5ec92009-01-07 18:08:38 -08003190/*
3191 * Atomically mark all (or else none) of the cgroup's CSS objects as
3192 * CSS_REMOVED. Return true on success, or false if the cgroup has
3193 * busy subsystems. Call with cgroup_mutex held
3194 */
3195
3196static int cgroup_clear_css_refs(struct cgroup *cgrp)
3197{
3198 struct cgroup_subsys *ss;
3199 unsigned long flags;
3200 bool failed = false;
3201 local_irq_save(flags);
3202 for_each_subsys(cgrp->root, ss) {
3203 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
3204 int refcnt;
Paul Menage804b3c22009-01-29 14:25:21 -08003205 while (1) {
Paul Menagee7c5ec92009-01-07 18:08:38 -08003206 /* We can only remove a CSS with a refcnt==1 */
3207 refcnt = atomic_read(&css->refcnt);
3208 if (refcnt > 1) {
3209 failed = true;
3210 goto done;
3211 }
3212 BUG_ON(!refcnt);
3213 /*
3214 * Drop the refcnt to 0 while we check other
3215 * subsystems. This will cause any racing
3216 * css_tryget() to spin until we set the
3217 * CSS_REMOVED bits or abort
3218 */
Paul Menage804b3c22009-01-29 14:25:21 -08003219 if (atomic_cmpxchg(&css->refcnt, refcnt, 0) == refcnt)
3220 break;
3221 cpu_relax();
3222 }
Paul Menagee7c5ec92009-01-07 18:08:38 -08003223 }
3224 done:
3225 for_each_subsys(cgrp->root, ss) {
3226 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
3227 if (failed) {
3228 /*
3229 * Restore old refcnt if we previously managed
3230 * to clear it from 1 to 0
3231 */
3232 if (!atomic_read(&css->refcnt))
3233 atomic_set(&css->refcnt, 1);
3234 } else {
3235 /* Commit the fact that the CSS is removed */
3236 set_bit(CSS_REMOVED, &css->flags);
3237 }
3238 }
3239 local_irq_restore(flags);
3240 return !failed;
3241}
3242
Paul Menageddbcc7e2007-10-18 23:39:30 -07003243static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
3244{
Paul Menagebd89aab2007-10-18 23:40:44 -07003245 struct cgroup *cgrp = dentry->d_fsdata;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003246 struct dentry *d;
3247 struct cgroup *parent;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003248 DEFINE_WAIT(wait);
3249 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003250
3251 /* the vfs holds both inode->i_mutex already */
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003252again:
Paul Menageddbcc7e2007-10-18 23:39:30 -07003253 mutex_lock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07003254 if (atomic_read(&cgrp->count) != 0) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07003255 mutex_unlock(&cgroup_mutex);
3256 return -EBUSY;
3257 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003258 if (!list_empty(&cgrp->children)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07003259 mutex_unlock(&cgroup_mutex);
3260 return -EBUSY;
3261 }
KAMEZAWA Hiroyuki3fa59df2008-11-19 15:36:34 -08003262 mutex_unlock(&cgroup_mutex);
Li Zefana043e3b2008-02-23 15:24:09 -08003263
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -08003264 /*
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003265 * In general, subsystem has no css->refcnt after pre_destroy(). But
3266 * in racy cases, subsystem may have to get css->refcnt after
3267 * pre_destroy() and it makes rmdir return with -EBUSY. This sometimes
3268 * make rmdir return -EBUSY too often. To avoid that, we use waitqueue
3269 * for cgroup's rmdir. CGRP_WAIT_ON_RMDIR is for synchronizing rmdir
3270 * and subsystem's reference count handling. Please see css_get/put
3271 * and css_tryget() and cgroup_wakeup_rmdir_waiter() implementation.
3272 */
3273 set_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
3274
3275 /*
Li Zefana043e3b2008-02-23 15:24:09 -08003276 * Call pre_destroy handlers of subsys. Notify subsystems
3277 * that rmdir() request comes.
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -08003278 */
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003279 ret = cgroup_call_pre_destroy(cgrp);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003280 if (ret) {
3281 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003282 return ret;
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003283 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003284
KAMEZAWA Hiroyuki3fa59df2008-11-19 15:36:34 -08003285 mutex_lock(&cgroup_mutex);
3286 parent = cgrp->parent;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003287 if (atomic_read(&cgrp->count) || !list_empty(&cgrp->children)) {
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003288 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003289 mutex_unlock(&cgroup_mutex);
3290 return -EBUSY;
3291 }
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003292 prepare_to_wait(&cgroup_rmdir_waitq, &wait, TASK_INTERRUPTIBLE);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003293 if (!cgroup_clear_css_refs(cgrp)) {
3294 mutex_unlock(&cgroup_mutex);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003295 /*
3296 * Because someone may call cgroup_wakeup_rmdir_waiter() before
3297 * prepare_to_wait(), we need to check this flag.
3298 */
3299 if (test_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags))
3300 schedule();
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003301 finish_wait(&cgroup_rmdir_waitq, &wait);
3302 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
3303 if (signal_pending(current))
3304 return -EINTR;
3305 goto again;
3306 }
3307 /* NO css_tryget() can success after here. */
3308 finish_wait(&cgroup_rmdir_waitq, &wait);
3309 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003310
Paul Menage81a6a5c2007-10-18 23:39:38 -07003311 spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07003312 set_bit(CGRP_REMOVED, &cgrp->flags);
3313 if (!list_empty(&cgrp->release_list))
3314 list_del(&cgrp->release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003315 spin_unlock(&release_list_lock);
Paul Menage999cd8a2009-01-07 18:08:36 -08003316
3317 cgroup_lock_hierarchy(cgrp->root);
3318 /* delete this cgroup from parent->children */
Paul Menagebd89aab2007-10-18 23:40:44 -07003319 list_del(&cgrp->sibling);
Paul Menage999cd8a2009-01-07 18:08:36 -08003320 cgroup_unlock_hierarchy(cgrp->root);
3321
Paul Menagebd89aab2007-10-18 23:40:44 -07003322 spin_lock(&cgrp->dentry->d_lock);
3323 d = dget(cgrp->dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003324 spin_unlock(&d->d_lock);
3325
3326 cgroup_d_remove_dir(d);
3327 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003328
Paul Menagebd89aab2007-10-18 23:40:44 -07003329 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003330 check_for_release(parent);
3331
Paul Menageddbcc7e2007-10-18 23:39:30 -07003332 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003333 return 0;
3334}
3335
Li Zefan06a11922008-04-29 01:00:07 -07003336static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003337{
Paul Menageddbcc7e2007-10-18 23:39:30 -07003338 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08003339
3340 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003341
3342 /* Create the top cgroup state for this subsystem */
Li Zefan33a68ac2009-01-07 18:07:42 -08003343 list_add(&ss->sibling, &rootnode.subsys_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003344 ss->root = &rootnode;
3345 css = ss->create(ss, dummytop);
3346 /* We don't handle early failures gracefully */
3347 BUG_ON(IS_ERR(css));
3348 init_cgroup_css(css, ss, dummytop);
3349
Li Zefane8d55fd2008-04-29 01:00:13 -07003350 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07003351 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07003352 * newly registered, all tasks and hence the
3353 * init_css_set is in the subsystem's top cgroup. */
3354 init_css_set.subsys[ss->subsys_id] = dummytop->subsys[ss->subsys_id];
Paul Menageddbcc7e2007-10-18 23:39:30 -07003355
3356 need_forkexit_callback |= ss->fork || ss->exit;
3357
Li Zefane8d55fd2008-04-29 01:00:13 -07003358 /* At system boot, before all subsystems have been
3359 * registered, no tasks have been forked, so we don't
3360 * need to invoke fork callbacks here. */
3361 BUG_ON(!list_empty(&init_task.tasks));
3362
Paul Menage999cd8a2009-01-07 18:08:36 -08003363 mutex_init(&ss->hierarchy_mutex);
Li Zefancfebe562009-02-11 13:04:36 -08003364 lockdep_set_class(&ss->hierarchy_mutex, &ss->subsys_key);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003365 ss->active = 1;
Ben Blume6a11052010-03-10 15:22:09 -08003366
3367 /* this function shouldn't be used with modular subsystems, since they
3368 * need to register a subsys_id, among other things */
3369 BUG_ON(ss->module);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003370}
3371
3372/**
Ben Blume6a11052010-03-10 15:22:09 -08003373 * cgroup_load_subsys: load and register a modular subsystem at runtime
3374 * @ss: the subsystem to load
3375 *
3376 * This function should be called in a modular subsystem's initcall. If the
3377 * subsytem is built as a module, it will be assigned a new subsys_id and set
3378 * up for use. If the subsystem is built-in anyway, work is delegated to the
3379 * simpler cgroup_init_subsys.
3380 */
3381int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
3382{
3383 int i;
3384 struct cgroup_subsys_state *css;
3385
3386 /* check name and function validity */
3387 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
3388 ss->create == NULL || ss->destroy == NULL)
3389 return -EINVAL;
3390
3391 /*
3392 * we don't support callbacks in modular subsystems. this check is
3393 * before the ss->module check for consistency; a subsystem that could
3394 * be a module should still have no callbacks even if the user isn't
3395 * compiling it as one.
3396 */
3397 if (ss->fork || ss->exit)
3398 return -EINVAL;
3399
3400 /*
3401 * an optionally modular subsystem is built-in: we want to do nothing,
3402 * since cgroup_init_subsys will have already taken care of it.
3403 */
3404 if (ss->module == NULL) {
3405 /* a few sanity checks */
3406 BUG_ON(ss->subsys_id >= CGROUP_BUILTIN_SUBSYS_COUNT);
3407 BUG_ON(subsys[ss->subsys_id] != ss);
3408 return 0;
3409 }
3410
3411 /*
3412 * need to register a subsys id before anything else - for example,
3413 * init_cgroup_css needs it.
3414 */
3415 mutex_lock(&cgroup_mutex);
3416 /* find the first empty slot in the array */
3417 for (i = CGROUP_BUILTIN_SUBSYS_COUNT; i < CGROUP_SUBSYS_COUNT; i++) {
3418 if (subsys[i] == NULL)
3419 break;
3420 }
3421 if (i == CGROUP_SUBSYS_COUNT) {
3422 /* maximum number of subsystems already registered! */
3423 mutex_unlock(&cgroup_mutex);
3424 return -EBUSY;
3425 }
3426 /* assign ourselves the subsys_id */
3427 ss->subsys_id = i;
3428 subsys[i] = ss;
3429
3430 /*
3431 * no ss->create seems to need anything important in the ss struct, so
3432 * this can happen first (i.e. before the rootnode attachment).
3433 */
3434 css = ss->create(ss, dummytop);
3435 if (IS_ERR(css)) {
3436 /* failure case - need to deassign the subsys[] slot. */
3437 subsys[i] = NULL;
3438 mutex_unlock(&cgroup_mutex);
3439 return PTR_ERR(css);
3440 }
3441
3442 list_add(&ss->sibling, &rootnode.subsys_list);
3443 ss->root = &rootnode;
3444
3445 /* our new subsystem will be attached to the dummy hierarchy. */
3446 init_cgroup_css(css, ss, dummytop);
3447 /* init_idr must be after init_cgroup_css because it sets css->id. */
3448 if (ss->use_id) {
3449 int ret = cgroup_init_idr(ss, css);
3450 if (ret) {
3451 dummytop->subsys[ss->subsys_id] = NULL;
3452 ss->destroy(ss, dummytop);
3453 subsys[i] = NULL;
3454 mutex_unlock(&cgroup_mutex);
3455 return ret;
3456 }
3457 }
3458
3459 /*
3460 * Now we need to entangle the css into the existing css_sets. unlike
3461 * in cgroup_init_subsys, there are now multiple css_sets, so each one
3462 * will need a new pointer to it; done by iterating the css_set_table.
3463 * furthermore, modifying the existing css_sets will corrupt the hash
3464 * table state, so each changed css_set will need its hash recomputed.
3465 * this is all done under the css_set_lock.
3466 */
3467 write_lock(&css_set_lock);
3468 for (i = 0; i < CSS_SET_TABLE_SIZE; i++) {
3469 struct css_set *cg;
3470 struct hlist_node *node, *tmp;
3471 struct hlist_head *bucket = &css_set_table[i], *new_bucket;
3472
3473 hlist_for_each_entry_safe(cg, node, tmp, bucket, hlist) {
3474 /* skip entries that we already rehashed */
3475 if (cg->subsys[ss->subsys_id])
3476 continue;
3477 /* remove existing entry */
3478 hlist_del(&cg->hlist);
3479 /* set new value */
3480 cg->subsys[ss->subsys_id] = css;
3481 /* recompute hash and restore entry */
3482 new_bucket = css_set_hash(cg->subsys);
3483 hlist_add_head(&cg->hlist, new_bucket);
3484 }
3485 }
3486 write_unlock(&css_set_lock);
3487
3488 mutex_init(&ss->hierarchy_mutex);
3489 lockdep_set_class(&ss->hierarchy_mutex, &ss->subsys_key);
3490 ss->active = 1;
3491
Ben Blume6a11052010-03-10 15:22:09 -08003492 /* success! */
3493 mutex_unlock(&cgroup_mutex);
3494 return 0;
3495}
3496EXPORT_SYMBOL_GPL(cgroup_load_subsys);
3497
3498/**
Ben Blumcf5d5942010-03-10 15:22:09 -08003499 * cgroup_unload_subsys: unload a modular subsystem
3500 * @ss: the subsystem to unload
3501 *
3502 * This function should be called in a modular subsystem's exitcall. When this
3503 * function is invoked, the refcount on the subsystem's module will be 0, so
3504 * the subsystem will not be attached to any hierarchy.
3505 */
3506void cgroup_unload_subsys(struct cgroup_subsys *ss)
3507{
3508 struct cg_cgroup_link *link;
3509 struct hlist_head *hhead;
3510
3511 BUG_ON(ss->module == NULL);
3512
3513 /*
3514 * we shouldn't be called if the subsystem is in use, and the use of
3515 * try_module_get in parse_cgroupfs_options should ensure that it
3516 * doesn't start being used while we're killing it off.
3517 */
3518 BUG_ON(ss->root != &rootnode);
3519
3520 mutex_lock(&cgroup_mutex);
3521 /* deassign the subsys_id */
3522 BUG_ON(ss->subsys_id < CGROUP_BUILTIN_SUBSYS_COUNT);
3523 subsys[ss->subsys_id] = NULL;
3524
3525 /* remove subsystem from rootnode's list of subsystems */
3526 list_del(&ss->sibling);
3527
3528 /*
3529 * disentangle the css from all css_sets attached to the dummytop. as
3530 * in loading, we need to pay our respects to the hashtable gods.
3531 */
3532 write_lock(&css_set_lock);
3533 list_for_each_entry(link, &dummytop->css_sets, cgrp_link_list) {
3534 struct css_set *cg = link->cg;
3535
3536 hlist_del(&cg->hlist);
3537 BUG_ON(!cg->subsys[ss->subsys_id]);
3538 cg->subsys[ss->subsys_id] = NULL;
3539 hhead = css_set_hash(cg->subsys);
3540 hlist_add_head(&cg->hlist, hhead);
3541 }
3542 write_unlock(&css_set_lock);
3543
3544 /*
3545 * remove subsystem's css from the dummytop and free it - need to free
3546 * before marking as null because ss->destroy needs the cgrp->subsys
3547 * pointer to find their state. note that this also takes care of
3548 * freeing the css_id.
3549 */
3550 ss->destroy(ss, dummytop);
3551 dummytop->subsys[ss->subsys_id] = NULL;
3552
3553 mutex_unlock(&cgroup_mutex);
3554}
3555EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
3556
3557/**
Li Zefana043e3b2008-02-23 15:24:09 -08003558 * cgroup_init_early - cgroup initialization at system boot
3559 *
3560 * Initialize cgroups at system boot, and initialize any
3561 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07003562 */
3563int __init cgroup_init_early(void)
3564{
3565 int i;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07003566 atomic_set(&init_css_set.refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -07003567 INIT_LIST_HEAD(&init_css_set.cg_links);
3568 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07003569 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07003570 css_set_count = 1;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003571 init_cgroup_root(&rootnode);
Paul Menage817929e2007-10-18 23:39:36 -07003572 root_count = 1;
3573 init_task.cgroups = &init_css_set;
3574
3575 init_css_set_link.cg = &init_css_set;
Paul Menage7717f7b2009-09-23 15:56:22 -07003576 init_css_set_link.cgrp = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -07003577 list_add(&init_css_set_link.cgrp_link_list,
Paul Menage817929e2007-10-18 23:39:36 -07003578 &rootnode.top_cgroup.css_sets);
3579 list_add(&init_css_set_link.cg_link_list,
3580 &init_css_set.cg_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003581
Li Zefan472b1052008-04-29 01:00:11 -07003582 for (i = 0; i < CSS_SET_TABLE_SIZE; i++)
3583 INIT_HLIST_HEAD(&css_set_table[i]);
3584
Ben Blumaae8aab2010-03-10 15:22:07 -08003585 /* at bootup time, we don't worry about modular subsystems */
3586 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07003587 struct cgroup_subsys *ss = subsys[i];
3588
3589 BUG_ON(!ss->name);
3590 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
3591 BUG_ON(!ss->create);
3592 BUG_ON(!ss->destroy);
3593 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08003594 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07003595 ss->name, ss->subsys_id);
3596 BUG();
3597 }
3598
3599 if (ss->early_init)
3600 cgroup_init_subsys(ss);
3601 }
3602 return 0;
3603}
3604
3605/**
Li Zefana043e3b2008-02-23 15:24:09 -08003606 * cgroup_init - cgroup initialization
3607 *
3608 * Register cgroup filesystem and /proc file, and initialize
3609 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07003610 */
3611int __init cgroup_init(void)
3612{
3613 int err;
3614 int i;
Li Zefan472b1052008-04-29 01:00:11 -07003615 struct hlist_head *hhead;
Paul Menagea4243162007-10-18 23:39:35 -07003616
3617 err = bdi_init(&cgroup_backing_dev_info);
3618 if (err)
3619 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003620
Ben Blumaae8aab2010-03-10 15:22:07 -08003621 /* at bootup time, we don't worry about modular subsystems */
3622 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07003623 struct cgroup_subsys *ss = subsys[i];
3624 if (!ss->early_init)
3625 cgroup_init_subsys(ss);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07003626 if (ss->use_id)
Ben Blume6a11052010-03-10 15:22:09 -08003627 cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003628 }
3629
Li Zefan472b1052008-04-29 01:00:11 -07003630 /* Add init_css_set to the hash table */
3631 hhead = css_set_hash(init_css_set.subsys);
3632 hlist_add_head(&init_css_set.hlist, hhead);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07003633 BUG_ON(!init_root_id(&rootnode));
Paul Menageddbcc7e2007-10-18 23:39:30 -07003634 err = register_filesystem(&cgroup_fs_type);
3635 if (err < 0)
3636 goto out;
3637
Li Zefan46ae2202008-04-29 01:00:08 -07003638 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07003639
Paul Menageddbcc7e2007-10-18 23:39:30 -07003640out:
Paul Menagea4243162007-10-18 23:39:35 -07003641 if (err)
3642 bdi_destroy(&cgroup_backing_dev_info);
3643
Paul Menageddbcc7e2007-10-18 23:39:30 -07003644 return err;
3645}
Paul Menageb4f48b62007-10-18 23:39:33 -07003646
Paul Menagea4243162007-10-18 23:39:35 -07003647/*
3648 * proc_cgroup_show()
3649 * - Print task's cgroup paths into seq_file, one line for each hierarchy
3650 * - Used for /proc/<pid>/cgroup.
3651 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
3652 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08003653 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07003654 * anyway. No need to check that tsk->cgroup != NULL, thanks to
3655 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
3656 * cgroup to top_cgroup.
3657 */
3658
3659/* TODO: Use a proper seq_file iterator */
3660static int proc_cgroup_show(struct seq_file *m, void *v)
3661{
3662 struct pid *pid;
3663 struct task_struct *tsk;
3664 char *buf;
3665 int retval;
3666 struct cgroupfs_root *root;
3667
3668 retval = -ENOMEM;
3669 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3670 if (!buf)
3671 goto out;
3672
3673 retval = -ESRCH;
3674 pid = m->private;
3675 tsk = get_pid_task(pid, PIDTYPE_PID);
3676 if (!tsk)
3677 goto out_free;
3678
3679 retval = 0;
3680
3681 mutex_lock(&cgroup_mutex);
3682
Li Zefane5f6a862009-01-07 18:07:41 -08003683 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07003684 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07003685 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07003686 int count = 0;
3687
Paul Menage2c6ab6d2009-09-23 15:56:23 -07003688 seq_printf(m, "%d:", root->hierarchy_id);
Paul Menagea4243162007-10-18 23:39:35 -07003689 for_each_subsys(root, ss)
3690 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07003691 if (strlen(root->name))
3692 seq_printf(m, "%sname=%s", count ? "," : "",
3693 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07003694 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07003695 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07003696 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07003697 if (retval < 0)
3698 goto out_unlock;
3699 seq_puts(m, buf);
3700 seq_putc(m, '\n');
3701 }
3702
3703out_unlock:
3704 mutex_unlock(&cgroup_mutex);
3705 put_task_struct(tsk);
3706out_free:
3707 kfree(buf);
3708out:
3709 return retval;
3710}
3711
3712static int cgroup_open(struct inode *inode, struct file *file)
3713{
3714 struct pid *pid = PROC_I(inode)->pid;
3715 return single_open(file, proc_cgroup_show, pid);
3716}
3717
Alexey Dobriyan828c0952009-10-01 15:43:56 -07003718const struct file_operations proc_cgroup_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07003719 .open = cgroup_open,
3720 .read = seq_read,
3721 .llseek = seq_lseek,
3722 .release = single_release,
3723};
3724
3725/* Display information about each subsystem and each hierarchy */
3726static int proc_cgroupstats_show(struct seq_file *m, void *v)
3727{
3728 int i;
Paul Menagea4243162007-10-18 23:39:35 -07003729
Paul Menage8bab8dd2008-04-04 14:29:57 -07003730 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08003731 /*
3732 * ideally we don't want subsystems moving around while we do this.
3733 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
3734 * subsys/hierarchy state.
3735 */
Paul Menagea4243162007-10-18 23:39:35 -07003736 mutex_lock(&cgroup_mutex);
Paul Menagea4243162007-10-18 23:39:35 -07003737 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3738 struct cgroup_subsys *ss = subsys[i];
Ben Blumaae8aab2010-03-10 15:22:07 -08003739 if (ss == NULL)
3740 continue;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07003741 seq_printf(m, "%s\t%d\t%d\t%d\n",
3742 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07003743 ss->root->number_of_cgroups, !ss->disabled);
Paul Menagea4243162007-10-18 23:39:35 -07003744 }
3745 mutex_unlock(&cgroup_mutex);
3746 return 0;
3747}
3748
3749static int cgroupstats_open(struct inode *inode, struct file *file)
3750{
Al Viro9dce07f2008-03-29 03:07:28 +00003751 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07003752}
3753
Alexey Dobriyan828c0952009-10-01 15:43:56 -07003754static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07003755 .open = cgroupstats_open,
3756 .read = seq_read,
3757 .llseek = seq_lseek,
3758 .release = single_release,
3759};
3760
Paul Menageb4f48b62007-10-18 23:39:33 -07003761/**
3762 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08003763 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07003764 *
3765 * Description: A task inherits its parent's cgroup at fork().
3766 *
3767 * A pointer to the shared css_set was automatically copied in
3768 * fork.c by dup_task_struct(). However, we ignore that copy, since
3769 * it was not made under the protection of RCU or cgroup_mutex, so
Cliff Wickman956db3c2008-02-07 00:14:43 -08003770 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
Paul Menage817929e2007-10-18 23:39:36 -07003771 * have already changed current->cgroups, allowing the previously
3772 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07003773 *
3774 * At the point that cgroup_fork() is called, 'current' is the parent
3775 * task, and the passed argument 'child' points to the child task.
3776 */
3777void cgroup_fork(struct task_struct *child)
3778{
Paul Menage817929e2007-10-18 23:39:36 -07003779 task_lock(current);
3780 child->cgroups = current->cgroups;
3781 get_css_set(child->cgroups);
3782 task_unlock(current);
3783 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07003784}
3785
3786/**
Li Zefana043e3b2008-02-23 15:24:09 -08003787 * cgroup_fork_callbacks - run fork callbacks
3788 * @child: the new task
3789 *
3790 * Called on a new task very soon before adding it to the
3791 * tasklist. No need to take any locks since no-one can
3792 * be operating on this task.
Paul Menageb4f48b62007-10-18 23:39:33 -07003793 */
3794void cgroup_fork_callbacks(struct task_struct *child)
3795{
3796 if (need_forkexit_callback) {
3797 int i;
Ben Blumaae8aab2010-03-10 15:22:07 -08003798 /*
3799 * forkexit callbacks are only supported for builtin
3800 * subsystems, and the builtin section of the subsys array is
3801 * immutable, so we don't need to lock the subsys array here.
3802 */
3803 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
Paul Menageb4f48b62007-10-18 23:39:33 -07003804 struct cgroup_subsys *ss = subsys[i];
3805 if (ss->fork)
3806 ss->fork(ss, child);
3807 }
3808 }
3809}
3810
3811/**
Li Zefana043e3b2008-02-23 15:24:09 -08003812 * cgroup_post_fork - called on a new task after adding it to the task list
3813 * @child: the task in question
3814 *
3815 * Adds the task to the list running through its css_set if necessary.
3816 * Has to be after the task is visible on the task list in case we race
3817 * with the first call to cgroup_iter_start() - to guarantee that the
3818 * new task ends up on its list.
3819 */
Paul Menage817929e2007-10-18 23:39:36 -07003820void cgroup_post_fork(struct task_struct *child)
3821{
3822 if (use_task_css_set_links) {
3823 write_lock(&css_set_lock);
Lai Jiangshanb12b5332009-01-07 18:07:36 -08003824 task_lock(child);
Paul Menage817929e2007-10-18 23:39:36 -07003825 if (list_empty(&child->cg_list))
3826 list_add(&child->cg_list, &child->cgroups->tasks);
Lai Jiangshanb12b5332009-01-07 18:07:36 -08003827 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07003828 write_unlock(&css_set_lock);
3829 }
3830}
3831/**
Paul Menageb4f48b62007-10-18 23:39:33 -07003832 * cgroup_exit - detach cgroup from exiting task
3833 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08003834 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07003835 *
3836 * Description: Detach cgroup from @tsk and release it.
3837 *
3838 * Note that cgroups marked notify_on_release force every task in
3839 * them to take the global cgroup_mutex mutex when exiting.
3840 * This could impact scaling on very large systems. Be reluctant to
3841 * use notify_on_release cgroups where very high task exit scaling
3842 * is required on large systems.
3843 *
3844 * the_top_cgroup_hack:
3845 *
3846 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
3847 *
3848 * We call cgroup_exit() while the task is still competent to
3849 * handle notify_on_release(), then leave the task attached to the
3850 * root cgroup in each hierarchy for the remainder of its exit.
3851 *
3852 * To do this properly, we would increment the reference count on
3853 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
3854 * code we would add a second cgroup function call, to drop that
3855 * reference. This would just create an unnecessary hot spot on
3856 * the top_cgroup reference count, to no avail.
3857 *
3858 * Normally, holding a reference to a cgroup without bumping its
3859 * count is unsafe. The cgroup could go away, or someone could
3860 * attach us to a different cgroup, decrementing the count on
3861 * the first cgroup that we never incremented. But in this case,
3862 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08003863 * which wards off any cgroup_attach_task() attempts, or task is a failed
3864 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07003865 */
3866void cgroup_exit(struct task_struct *tsk, int run_callbacks)
3867{
3868 int i;
Paul Menage817929e2007-10-18 23:39:36 -07003869 struct css_set *cg;
Paul Menageb4f48b62007-10-18 23:39:33 -07003870
3871 if (run_callbacks && need_forkexit_callback) {
Ben Blumaae8aab2010-03-10 15:22:07 -08003872 /*
3873 * modular subsystems can't use callbacks, so no need to lock
3874 * the subsys array
3875 */
3876 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
Paul Menageb4f48b62007-10-18 23:39:33 -07003877 struct cgroup_subsys *ss = subsys[i];
3878 if (ss->exit)
3879 ss->exit(ss, tsk);
3880 }
3881 }
Paul Menage817929e2007-10-18 23:39:36 -07003882
3883 /*
3884 * Unlink from the css_set task list if necessary.
3885 * Optimistically check cg_list before taking
3886 * css_set_lock
3887 */
3888 if (!list_empty(&tsk->cg_list)) {
3889 write_lock(&css_set_lock);
3890 if (!list_empty(&tsk->cg_list))
3891 list_del(&tsk->cg_list);
3892 write_unlock(&css_set_lock);
3893 }
3894
Paul Menageb4f48b62007-10-18 23:39:33 -07003895 /* Reassign the task to the init_css_set. */
3896 task_lock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07003897 cg = tsk->cgroups;
3898 tsk->cgroups = &init_css_set;
Paul Menageb4f48b62007-10-18 23:39:33 -07003899 task_unlock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07003900 if (cg)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003901 put_css_set_taskexit(cg);
Paul Menageb4f48b62007-10-18 23:39:33 -07003902}
Paul Menage697f4162007-10-18 23:39:34 -07003903
3904/**
Li Zefana043e3b2008-02-23 15:24:09 -08003905 * cgroup_clone - clone the cgroup the given subsystem is attached to
3906 * @tsk: the task to be moved
3907 * @subsys: the given subsystem
Serge E. Hallyne885dcd2008-07-25 01:47:06 -07003908 * @nodename: the name for the new cgroup
Li Zefana043e3b2008-02-23 15:24:09 -08003909 *
3910 * Duplicate the current cgroup in the hierarchy that the given
3911 * subsystem is attached to, and move this task into the new
3912 * child.
Paul Menage697f4162007-10-18 23:39:34 -07003913 */
Serge E. Hallyne885dcd2008-07-25 01:47:06 -07003914int cgroup_clone(struct task_struct *tsk, struct cgroup_subsys *subsys,
3915 char *nodename)
Paul Menage697f4162007-10-18 23:39:34 -07003916{
3917 struct dentry *dentry;
3918 int ret = 0;
Paul Menage697f4162007-10-18 23:39:34 -07003919 struct cgroup *parent, *child;
3920 struct inode *inode;
3921 struct css_set *cg;
3922 struct cgroupfs_root *root;
3923 struct cgroup_subsys *ss;
3924
3925 /* We shouldn't be called by an unregistered subsystem */
3926 BUG_ON(!subsys->active);
3927
3928 /* First figure out what hierarchy and cgroup we're dealing
3929 * with, and pin them so we can drop cgroup_mutex */
3930 mutex_lock(&cgroup_mutex);
3931 again:
3932 root = subsys->root;
3933 if (root == &rootnode) {
Paul Menage697f4162007-10-18 23:39:34 -07003934 mutex_unlock(&cgroup_mutex);
3935 return 0;
3936 }
Paul Menage697f4162007-10-18 23:39:34 -07003937
Paul Menage697f4162007-10-18 23:39:34 -07003938 /* Pin the hierarchy */
Li Zefan1404f062009-01-29 14:25:21 -08003939 if (!atomic_inc_not_zero(&root->sb->s_active)) {
Li Zefan7b574b72009-01-04 12:00:45 -08003940 /* We race with the final deactivate_super() */
3941 mutex_unlock(&cgroup_mutex);
3942 return 0;
3943 }
Paul Menage697f4162007-10-18 23:39:34 -07003944
Paul Menage817929e2007-10-18 23:39:36 -07003945 /* Keep the cgroup alive */
Li Zefan1404f062009-01-29 14:25:21 -08003946 task_lock(tsk);
3947 parent = task_cgroup(tsk, subsys->subsys_id);
3948 cg = tsk->cgroups;
Paul Menage817929e2007-10-18 23:39:36 -07003949 get_css_set(cg);
Lai Jiangshan104cbd52009-01-07 18:07:38 -08003950 task_unlock(tsk);
Li Zefan1404f062009-01-29 14:25:21 -08003951
Paul Menage697f4162007-10-18 23:39:34 -07003952 mutex_unlock(&cgroup_mutex);
3953
3954 /* Now do the VFS work to create a cgroup */
3955 inode = parent->dentry->d_inode;
3956
3957 /* Hold the parent directory mutex across this operation to
3958 * stop anyone else deleting the new cgroup */
3959 mutex_lock(&inode->i_mutex);
3960 dentry = lookup_one_len(nodename, parent->dentry, strlen(nodename));
3961 if (IS_ERR(dentry)) {
3962 printk(KERN_INFO
Diego Callejacfe36bd2007-11-14 16:58:54 -08003963 "cgroup: Couldn't allocate dentry for %s: %ld\n", nodename,
Paul Menage697f4162007-10-18 23:39:34 -07003964 PTR_ERR(dentry));
3965 ret = PTR_ERR(dentry);
3966 goto out_release;
3967 }
3968
3969 /* Create the cgroup directory, which also creates the cgroup */
Li Zefan75139b82009-01-07 18:07:33 -08003970 ret = vfs_mkdir(inode, dentry, 0755);
Paul Menagebd89aab2007-10-18 23:40:44 -07003971 child = __d_cgrp(dentry);
Paul Menage697f4162007-10-18 23:39:34 -07003972 dput(dentry);
3973 if (ret) {
3974 printk(KERN_INFO
3975 "Failed to create cgroup %s: %d\n", nodename,
3976 ret);
3977 goto out_release;
3978 }
3979
Paul Menage697f4162007-10-18 23:39:34 -07003980 /* The cgroup now exists. Retake cgroup_mutex and check
3981 * that we're still in the same state that we thought we
3982 * were. */
3983 mutex_lock(&cgroup_mutex);
3984 if ((root != subsys->root) ||
3985 (parent != task_cgroup(tsk, subsys->subsys_id))) {
3986 /* Aargh, we raced ... */
3987 mutex_unlock(&inode->i_mutex);
Paul Menage817929e2007-10-18 23:39:36 -07003988 put_css_set(cg);
Paul Menage697f4162007-10-18 23:39:34 -07003989
Li Zefan1404f062009-01-29 14:25:21 -08003990 deactivate_super(root->sb);
Paul Menage697f4162007-10-18 23:39:34 -07003991 /* The cgroup is still accessible in the VFS, but
3992 * we're not going to try to rmdir() it at this
3993 * point. */
3994 printk(KERN_INFO
3995 "Race in cgroup_clone() - leaking cgroup %s\n",
3996 nodename);
3997 goto again;
3998 }
3999
4000 /* do any required auto-setup */
4001 for_each_subsys(root, ss) {
4002 if (ss->post_clone)
4003 ss->post_clone(ss, child);
4004 }
4005
4006 /* All seems fine. Finish by moving the task into the new cgroup */
Cliff Wickman956db3c2008-02-07 00:14:43 -08004007 ret = cgroup_attach_task(child, tsk);
Paul Menage697f4162007-10-18 23:39:34 -07004008 mutex_unlock(&cgroup_mutex);
4009
4010 out_release:
4011 mutex_unlock(&inode->i_mutex);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004012
4013 mutex_lock(&cgroup_mutex);
Paul Menage817929e2007-10-18 23:39:36 -07004014 put_css_set(cg);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004015 mutex_unlock(&cgroup_mutex);
Li Zefan1404f062009-01-29 14:25:21 -08004016 deactivate_super(root->sb);
Paul Menage697f4162007-10-18 23:39:34 -07004017 return ret;
4018}
4019
Li Zefana043e3b2008-02-23 15:24:09 -08004020/**
Grzegorz Nosek313e9242009-04-02 16:57:23 -07004021 * cgroup_is_descendant - see if @cgrp is a descendant of @task's cgrp
Li Zefana043e3b2008-02-23 15:24:09 -08004022 * @cgrp: the cgroup in question
Grzegorz Nosek313e9242009-04-02 16:57:23 -07004023 * @task: the task in question
Li Zefana043e3b2008-02-23 15:24:09 -08004024 *
Grzegorz Nosek313e9242009-04-02 16:57:23 -07004025 * See if @cgrp is a descendant of @task's cgroup in the appropriate
4026 * hierarchy.
Paul Menage697f4162007-10-18 23:39:34 -07004027 *
4028 * If we are sending in dummytop, then presumably we are creating
4029 * the top cgroup in the subsystem.
4030 *
4031 * Called only by the ns (nsproxy) cgroup.
4032 */
Grzegorz Nosek313e9242009-04-02 16:57:23 -07004033int cgroup_is_descendant(const struct cgroup *cgrp, struct task_struct *task)
Paul Menage697f4162007-10-18 23:39:34 -07004034{
4035 int ret;
4036 struct cgroup *target;
Paul Menage697f4162007-10-18 23:39:34 -07004037
Paul Menagebd89aab2007-10-18 23:40:44 -07004038 if (cgrp == dummytop)
Paul Menage697f4162007-10-18 23:39:34 -07004039 return 1;
4040
Paul Menage7717f7b2009-09-23 15:56:22 -07004041 target = task_cgroup_from_root(task, cgrp->root);
Paul Menagebd89aab2007-10-18 23:40:44 -07004042 while (cgrp != target && cgrp!= cgrp->top_cgroup)
4043 cgrp = cgrp->parent;
4044 ret = (cgrp == target);
Paul Menage697f4162007-10-18 23:39:34 -07004045 return ret;
4046}
Paul Menage81a6a5c2007-10-18 23:39:38 -07004047
Paul Menagebd89aab2007-10-18 23:40:44 -07004048static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004049{
4050 /* All of these checks rely on RCU to keep the cgroup
4051 * structure alive */
Paul Menagebd89aab2007-10-18 23:40:44 -07004052 if (cgroup_is_releasable(cgrp) && !atomic_read(&cgrp->count)
4053 && list_empty(&cgrp->children) && !cgroup_has_css_refs(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07004054 /* Control Group is currently removeable. If it's not
4055 * already queued for a userspace notification, queue
4056 * it now */
4057 int need_schedule_work = 0;
4058 spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07004059 if (!cgroup_is_removed(cgrp) &&
4060 list_empty(&cgrp->release_list)) {
4061 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004062 need_schedule_work = 1;
4063 }
4064 spin_unlock(&release_list_lock);
4065 if (need_schedule_work)
4066 schedule_work(&release_agent_work);
4067 }
4068}
4069
Daisuke Nishimurad7b9fff2010-03-10 15:22:05 -08004070/* Caller must verify that the css is not for root cgroup */
4071void __css_put(struct cgroup_subsys_state *css, int count)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004072{
Paul Menagebd89aab2007-10-18 23:40:44 -07004073 struct cgroup *cgrp = css->cgroup;
KAMEZAWA Hiroyuki3dece832009-10-01 15:44:09 -07004074 int val;
Paul Menage81a6a5c2007-10-18 23:39:38 -07004075 rcu_read_lock();
Daisuke Nishimurad7b9fff2010-03-10 15:22:05 -08004076 val = atomic_sub_return(count, &css->refcnt);
KAMEZAWA Hiroyuki3dece832009-10-01 15:44:09 -07004077 if (val == 1) {
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004078 if (notify_on_release(cgrp)) {
4079 set_bit(CGRP_RELEASABLE, &cgrp->flags);
4080 check_for_release(cgrp);
4081 }
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07004082 cgroup_wakeup_rmdir_waiter(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004083 }
4084 rcu_read_unlock();
KAMEZAWA Hiroyuki3dece832009-10-01 15:44:09 -07004085 WARN_ON_ONCE(val < 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004086}
Ben Blum67523c42010-03-10 15:22:11 -08004087EXPORT_SYMBOL_GPL(__css_put);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004088
4089/*
4090 * Notify userspace when a cgroup is released, by running the
4091 * configured release agent with the name of the cgroup (path
4092 * relative to the root of cgroup file system) as the argument.
4093 *
4094 * Most likely, this user command will try to rmdir this cgroup.
4095 *
4096 * This races with the possibility that some other task will be
4097 * attached to this cgroup before it is removed, or that some other
4098 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
4099 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
4100 * unused, and this cgroup will be reprieved from its death sentence,
4101 * to continue to serve a useful existence. Next time it's released,
4102 * we will get notified again, if it still has 'notify_on_release' set.
4103 *
4104 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
4105 * means only wait until the task is successfully execve()'d. The
4106 * separate release agent task is forked by call_usermodehelper(),
4107 * then control in this thread returns here, without waiting for the
4108 * release agent task. We don't bother to wait because the caller of
4109 * this routine has no use for the exit status of the release agent
4110 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07004111 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07004112static void cgroup_release_agent(struct work_struct *work)
4113{
4114 BUG_ON(work != &release_agent_work);
4115 mutex_lock(&cgroup_mutex);
4116 spin_lock(&release_list_lock);
4117 while (!list_empty(&release_list)) {
4118 char *argv[3], *envp[3];
4119 int i;
Paul Menagee788e062008-07-25 01:46:59 -07004120 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07004121 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07004122 struct cgroup,
4123 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07004124 list_del_init(&cgrp->release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004125 spin_unlock(&release_list_lock);
4126 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07004127 if (!pathbuf)
4128 goto continue_free;
4129 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
4130 goto continue_free;
4131 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
4132 if (!agentbuf)
4133 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07004134
4135 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07004136 argv[i++] = agentbuf;
4137 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07004138 argv[i] = NULL;
4139
4140 i = 0;
4141 /* minimal command environment */
4142 envp[i++] = "HOME=/";
4143 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
4144 envp[i] = NULL;
4145
4146 /* Drop the lock while we invoke the usermode helper,
4147 * since the exec could involve hitting disk and hence
4148 * be a slow process */
4149 mutex_unlock(&cgroup_mutex);
4150 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004151 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07004152 continue_free:
4153 kfree(pathbuf);
4154 kfree(agentbuf);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004155 spin_lock(&release_list_lock);
4156 }
4157 spin_unlock(&release_list_lock);
4158 mutex_unlock(&cgroup_mutex);
4159}
Paul Menage8bab8dd2008-04-04 14:29:57 -07004160
4161static int __init cgroup_disable(char *str)
4162{
4163 int i;
4164 char *token;
4165
4166 while ((token = strsep(&str, ",")) != NULL) {
4167 if (!*token)
4168 continue;
Ben Blumaae8aab2010-03-10 15:22:07 -08004169 /*
4170 * cgroup_disable, being at boot time, can't know about module
4171 * subsystems, so we don't worry about them.
4172 */
4173 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07004174 struct cgroup_subsys *ss = subsys[i];
4175
4176 if (!strcmp(token, ss->name)) {
4177 ss->disabled = 1;
4178 printk(KERN_INFO "Disabling %s control group"
4179 " subsystem\n", ss->name);
4180 break;
4181 }
4182 }
4183 }
4184 return 1;
4185}
4186__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004187
4188/*
4189 * Functons for CSS ID.
4190 */
4191
4192/*
4193 *To get ID other than 0, this should be called when !cgroup_is_removed().
4194 */
4195unsigned short css_id(struct cgroup_subsys_state *css)
4196{
4197 struct css_id *cssid = rcu_dereference(css->id);
4198
4199 if (cssid)
4200 return cssid->id;
4201 return 0;
4202}
Ben Blum67523c42010-03-10 15:22:11 -08004203EXPORT_SYMBOL_GPL(css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004204
4205unsigned short css_depth(struct cgroup_subsys_state *css)
4206{
4207 struct css_id *cssid = rcu_dereference(css->id);
4208
4209 if (cssid)
4210 return cssid->depth;
4211 return 0;
4212}
Ben Blum67523c42010-03-10 15:22:11 -08004213EXPORT_SYMBOL_GPL(css_depth);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004214
4215bool css_is_ancestor(struct cgroup_subsys_state *child,
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07004216 const struct cgroup_subsys_state *root)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004217{
4218 struct css_id *child_id = rcu_dereference(child->id);
4219 struct css_id *root_id = rcu_dereference(root->id);
4220
4221 if (!child_id || !root_id || (child_id->depth < root_id->depth))
4222 return false;
4223 return child_id->stack[root_id->depth] == root_id->id;
4224}
4225
4226static void __free_css_id_cb(struct rcu_head *head)
4227{
4228 struct css_id *id;
4229
4230 id = container_of(head, struct css_id, rcu_head);
4231 kfree(id);
4232}
4233
4234void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
4235{
4236 struct css_id *id = css->id;
4237 /* When this is called before css_id initialization, id can be NULL */
4238 if (!id)
4239 return;
4240
4241 BUG_ON(!ss->use_id);
4242
4243 rcu_assign_pointer(id->css, NULL);
4244 rcu_assign_pointer(css->id, NULL);
4245 spin_lock(&ss->id_lock);
4246 idr_remove(&ss->idr, id->id);
4247 spin_unlock(&ss->id_lock);
4248 call_rcu(&id->rcu_head, __free_css_id_cb);
4249}
Ben Blum67523c42010-03-10 15:22:11 -08004250EXPORT_SYMBOL_GPL(free_css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004251
4252/*
4253 * This is called by init or create(). Then, calls to this function are
4254 * always serialized (By cgroup_mutex() at create()).
4255 */
4256
4257static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
4258{
4259 struct css_id *newid;
4260 int myid, error, size;
4261
4262 BUG_ON(!ss->use_id);
4263
4264 size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
4265 newid = kzalloc(size, GFP_KERNEL);
4266 if (!newid)
4267 return ERR_PTR(-ENOMEM);
4268 /* get id */
4269 if (unlikely(!idr_pre_get(&ss->idr, GFP_KERNEL))) {
4270 error = -ENOMEM;
4271 goto err_out;
4272 }
4273 spin_lock(&ss->id_lock);
4274 /* Don't use 0. allocates an ID of 1-65535 */
4275 error = idr_get_new_above(&ss->idr, newid, 1, &myid);
4276 spin_unlock(&ss->id_lock);
4277
4278 /* Returns error when there are no free spaces for new ID.*/
4279 if (error) {
4280 error = -ENOSPC;
4281 goto err_out;
4282 }
4283 if (myid > CSS_ID_MAX)
4284 goto remove_idr;
4285
4286 newid->id = myid;
4287 newid->depth = depth;
4288 return newid;
4289remove_idr:
4290 error = -ENOSPC;
4291 spin_lock(&ss->id_lock);
4292 idr_remove(&ss->idr, myid);
4293 spin_unlock(&ss->id_lock);
4294err_out:
4295 kfree(newid);
4296 return ERR_PTR(error);
4297
4298}
4299
Ben Blume6a11052010-03-10 15:22:09 -08004300static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss,
4301 struct cgroup_subsys_state *rootcss)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004302{
4303 struct css_id *newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004304
4305 spin_lock_init(&ss->id_lock);
4306 idr_init(&ss->idr);
4307
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004308 newid = get_new_cssid(ss, 0);
4309 if (IS_ERR(newid))
4310 return PTR_ERR(newid);
4311
4312 newid->stack[0] = newid->id;
4313 newid->css = rootcss;
4314 rootcss->id = newid;
4315 return 0;
4316}
4317
4318static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent,
4319 struct cgroup *child)
4320{
4321 int subsys_id, i, depth = 0;
4322 struct cgroup_subsys_state *parent_css, *child_css;
4323 struct css_id *child_id, *parent_id = NULL;
4324
4325 subsys_id = ss->subsys_id;
4326 parent_css = parent->subsys[subsys_id];
4327 child_css = child->subsys[subsys_id];
4328 depth = css_depth(parent_css) + 1;
4329 parent_id = parent_css->id;
4330
4331 child_id = get_new_cssid(ss, depth);
4332 if (IS_ERR(child_id))
4333 return PTR_ERR(child_id);
4334
4335 for (i = 0; i < depth; i++)
4336 child_id->stack[i] = parent_id->stack[i];
4337 child_id->stack[depth] = child_id->id;
4338 /*
4339 * child_id->css pointer will be set after this cgroup is available
4340 * see cgroup_populate_dir()
4341 */
4342 rcu_assign_pointer(child_css->id, child_id);
4343
4344 return 0;
4345}
4346
4347/**
4348 * css_lookup - lookup css by id
4349 * @ss: cgroup subsys to be looked into.
4350 * @id: the id
4351 *
4352 * Returns pointer to cgroup_subsys_state if there is valid one with id.
4353 * NULL if not. Should be called under rcu_read_lock()
4354 */
4355struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
4356{
4357 struct css_id *cssid = NULL;
4358
4359 BUG_ON(!ss->use_id);
4360 cssid = idr_find(&ss->idr, id);
4361
4362 if (unlikely(!cssid))
4363 return NULL;
4364
4365 return rcu_dereference(cssid->css);
4366}
Ben Blum67523c42010-03-10 15:22:11 -08004367EXPORT_SYMBOL_GPL(css_lookup);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004368
4369/**
4370 * css_get_next - lookup next cgroup under specified hierarchy.
4371 * @ss: pointer to subsystem
4372 * @id: current position of iteration.
4373 * @root: pointer to css. search tree under this.
4374 * @foundid: position of found object.
4375 *
4376 * Search next css under the specified hierarchy of rootid. Calling under
4377 * rcu_read_lock() is necessary. Returns NULL if it reaches the end.
4378 */
4379struct cgroup_subsys_state *
4380css_get_next(struct cgroup_subsys *ss, int id,
4381 struct cgroup_subsys_state *root, int *foundid)
4382{
4383 struct cgroup_subsys_state *ret = NULL;
4384 struct css_id *tmp;
4385 int tmpid;
4386 int rootid = css_id(root);
4387 int depth = css_depth(root);
4388
4389 if (!rootid)
4390 return NULL;
4391
4392 BUG_ON(!ss->use_id);
4393 /* fill start point for scan */
4394 tmpid = id;
4395 while (1) {
4396 /*
4397 * scan next entry from bitmap(tree), tmpid is updated after
4398 * idr_get_next().
4399 */
4400 spin_lock(&ss->id_lock);
4401 tmp = idr_get_next(&ss->idr, &tmpid);
4402 spin_unlock(&ss->id_lock);
4403
4404 if (!tmp)
4405 break;
4406 if (tmp->depth >= depth && tmp->stack[depth] == rootid) {
4407 ret = rcu_dereference(tmp->css);
4408 if (ret) {
4409 *foundid = tmpid;
4410 break;
4411 }
4412 }
4413 /* continue to scan from next id */
4414 tmpid = tmpid + 1;
4415 }
4416 return ret;
4417}
4418
Paul Menagefe693432009-09-23 15:56:20 -07004419#ifdef CONFIG_CGROUP_DEBUG
4420static struct cgroup_subsys_state *debug_create(struct cgroup_subsys *ss,
4421 struct cgroup *cont)
4422{
4423 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
4424
4425 if (!css)
4426 return ERR_PTR(-ENOMEM);
4427
4428 return css;
4429}
4430
4431static void debug_destroy(struct cgroup_subsys *ss, struct cgroup *cont)
4432{
4433 kfree(cont->subsys[debug_subsys_id]);
4434}
4435
4436static u64 cgroup_refcount_read(struct cgroup *cont, struct cftype *cft)
4437{
4438 return atomic_read(&cont->count);
4439}
4440
4441static u64 debug_taskcount_read(struct cgroup *cont, struct cftype *cft)
4442{
4443 return cgroup_task_count(cont);
4444}
4445
4446static u64 current_css_set_read(struct cgroup *cont, struct cftype *cft)
4447{
4448 return (u64)(unsigned long)current->cgroups;
4449}
4450
4451static u64 current_css_set_refcount_read(struct cgroup *cont,
4452 struct cftype *cft)
4453{
4454 u64 count;
4455
4456 rcu_read_lock();
4457 count = atomic_read(&current->cgroups->refcount);
4458 rcu_read_unlock();
4459 return count;
4460}
4461
Paul Menage7717f7b2009-09-23 15:56:22 -07004462static int current_css_set_cg_links_read(struct cgroup *cont,
4463 struct cftype *cft,
4464 struct seq_file *seq)
4465{
4466 struct cg_cgroup_link *link;
4467 struct css_set *cg;
4468
4469 read_lock(&css_set_lock);
4470 rcu_read_lock();
4471 cg = rcu_dereference(current->cgroups);
4472 list_for_each_entry(link, &cg->cg_links, cg_link_list) {
4473 struct cgroup *c = link->cgrp;
4474 const char *name;
4475
4476 if (c->dentry)
4477 name = c->dentry->d_name.name;
4478 else
4479 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004480 seq_printf(seq, "Root %d group %s\n",
4481 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07004482 }
4483 rcu_read_unlock();
4484 read_unlock(&css_set_lock);
4485 return 0;
4486}
4487
4488#define MAX_TASKS_SHOWN_PER_CSS 25
4489static int cgroup_css_links_read(struct cgroup *cont,
4490 struct cftype *cft,
4491 struct seq_file *seq)
4492{
4493 struct cg_cgroup_link *link;
4494
4495 read_lock(&css_set_lock);
4496 list_for_each_entry(link, &cont->css_sets, cgrp_link_list) {
4497 struct css_set *cg = link->cg;
4498 struct task_struct *task;
4499 int count = 0;
4500 seq_printf(seq, "css_set %p\n", cg);
4501 list_for_each_entry(task, &cg->tasks, cg_list) {
4502 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
4503 seq_puts(seq, " ...\n");
4504 break;
4505 } else {
4506 seq_printf(seq, " task %d\n",
4507 task_pid_vnr(task));
4508 }
4509 }
4510 }
4511 read_unlock(&css_set_lock);
4512 return 0;
4513}
4514
Paul Menagefe693432009-09-23 15:56:20 -07004515static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft)
4516{
4517 return test_bit(CGRP_RELEASABLE, &cgrp->flags);
4518}
4519
4520static struct cftype debug_files[] = {
4521 {
4522 .name = "cgroup_refcount",
4523 .read_u64 = cgroup_refcount_read,
4524 },
4525 {
4526 .name = "taskcount",
4527 .read_u64 = debug_taskcount_read,
4528 },
4529
4530 {
4531 .name = "current_css_set",
4532 .read_u64 = current_css_set_read,
4533 },
4534
4535 {
4536 .name = "current_css_set_refcount",
4537 .read_u64 = current_css_set_refcount_read,
4538 },
4539
4540 {
Paul Menage7717f7b2009-09-23 15:56:22 -07004541 .name = "current_css_set_cg_links",
4542 .read_seq_string = current_css_set_cg_links_read,
4543 },
4544
4545 {
4546 .name = "cgroup_css_links",
4547 .read_seq_string = cgroup_css_links_read,
4548 },
4549
4550 {
Paul Menagefe693432009-09-23 15:56:20 -07004551 .name = "releasable",
4552 .read_u64 = releasable_read,
4553 },
4554};
4555
4556static int debug_populate(struct cgroup_subsys *ss, struct cgroup *cont)
4557{
4558 return cgroup_add_files(cont, ss, debug_files,
4559 ARRAY_SIZE(debug_files));
4560}
4561
4562struct cgroup_subsys debug_subsys = {
4563 .name = "debug",
4564 .create = debug_create,
4565 .destroy = debug_destroy,
4566 .populate = debug_populate,
4567 .subsys_id = debug_subsys_id,
4568};
4569#endif /* CONFIG_CGROUP_DEBUG */