blob: 2efce77f649c25ccd69297b7e34106013905145c [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 *
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08007 * Notifications support
8 * Copyright (C) 2009 Nokia Corporation
9 * Author: Kirill A. Shutemov
10 *
Paul Menageddbcc7e2007-10-18 23:39:30 -070011 * Copyright notices from the original cpuset code:
12 * --------------------------------------------------
13 * Copyright (C) 2003 BULL SA.
14 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
15 *
16 * Portions derived from Patrick Mochel's sysfs code.
17 * sysfs is Copyright (c) 2001-3 Patrick Mochel
18 *
19 * 2003-10-10 Written by Simon Derr.
20 * 2003-10-22 Updates by Stephen Hemminger.
21 * 2004 May-July Rework by Paul Jackson.
22 * ---------------------------------------------------
23 *
24 * This file is subject to the terms and conditions of the GNU General Public
25 * License. See the file COPYING in the main directory of the Linux
26 * distribution for more details.
27 */
28
29#include <linux/cgroup.h>
Paul Menagec6d57f32009-09-23 15:56:19 -070030#include <linux/ctype.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070031#include <linux/errno.h>
32#include <linux/fs.h>
33#include <linux/kernel.h>
34#include <linux/list.h>
35#include <linux/mm.h>
36#include <linux/mutex.h>
37#include <linux/mount.h>
38#include <linux/pagemap.h>
Paul Menagea4243162007-10-18 23:39:35 -070039#include <linux/proc_fs.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070040#include <linux/rcupdate.h>
41#include <linux/sched.h>
Paul Menage817929e2007-10-18 23:39:36 -070042#include <linux/backing-dev.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070043#include <linux/seq_file.h>
44#include <linux/slab.h>
45#include <linux/magic.h>
46#include <linux/spinlock.h>
47#include <linux/string.h>
Paul Menagebbcb81d2007-10-18 23:39:32 -070048#include <linux/sort.h>
Paul Menage81a6a5c2007-10-18 23:39:38 -070049#include <linux/kmod.h>
Ben Blume6a11052010-03-10 15:22:09 -080050#include <linux/module.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070051#include <linux/delayacct.h>
52#include <linux/cgroupstats.h>
Li Zefan472b1052008-04-29 01:00:11 -070053#include <linux/hash.h>
Al Viro3f8206d2008-07-26 03:46:43 -040054#include <linux/namei.h>
Li Zefan096b7fe2009-07-29 15:04:04 -070055#include <linux/pid_namespace.h>
Paul Menage2c6ab6d2009-09-23 15:56:23 -070056#include <linux/idr.h>
Ben Blumd1d9fd32009-09-23 15:56:28 -070057#include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -080058#include <linux/eventfd.h>
59#include <linux/poll.h>
Ben Blumd8466872011-05-26 16:25:21 -070060#include <linux/flex_array.h> /* used in cgroup_attach_proc */
Balbir Singh846c7bb2007-10-18 23:39:44 -070061
Paul Menageddbcc7e2007-10-18 23:39:30 -070062#include <asm/atomic.h>
63
Paul Menage81a6a5c2007-10-18 23:39:38 -070064static DEFINE_MUTEX(cgroup_mutex);
65
Ben Blumaae8aab2010-03-10 15:22:07 -080066/*
67 * Generate an array of cgroup subsystem pointers. At boot time, this is
68 * populated up to CGROUP_BUILTIN_SUBSYS_COUNT, and modular subsystems are
69 * registered after that. The mutable section of this array is protected by
70 * cgroup_mutex.
71 */
Paul Menageddbcc7e2007-10-18 23:39:30 -070072#define SUBSYS(_x) &_x ## _subsys,
Ben Blumaae8aab2010-03-10 15:22:07 -080073static struct cgroup_subsys *subsys[CGROUP_SUBSYS_COUNT] = {
Paul Menageddbcc7e2007-10-18 23:39:30 -070074#include <linux/cgroup_subsys.h>
75};
76
Paul Menagec6d57f32009-09-23 15:56:19 -070077#define MAX_CGROUP_ROOT_NAMELEN 64
78
Paul Menageddbcc7e2007-10-18 23:39:30 -070079/*
80 * A cgroupfs_root represents the root of a cgroup hierarchy,
81 * and may be associated with a superblock to form an active
82 * hierarchy
83 */
84struct cgroupfs_root {
85 struct super_block *sb;
86
87 /*
88 * The bitmask of subsystems intended to be attached to this
89 * hierarchy
90 */
91 unsigned long subsys_bits;
92
Paul Menage2c6ab6d2009-09-23 15:56:23 -070093 /* Unique id for this hierarchy. */
94 int hierarchy_id;
95
Paul Menageddbcc7e2007-10-18 23:39:30 -070096 /* The bitmask of subsystems currently attached to this hierarchy */
97 unsigned long actual_subsys_bits;
98
99 /* A list running through the attached subsystems */
100 struct list_head subsys_list;
101
102 /* The root cgroup for this hierarchy */
103 struct cgroup top_cgroup;
104
105 /* Tracks how many cgroups are currently defined in hierarchy.*/
106 int number_of_cgroups;
107
Li Zefane5f6a862009-01-07 18:07:41 -0800108 /* A list running through the active hierarchies */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700109 struct list_head root_list;
110
111 /* Hierarchy-specific flags */
112 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700113
Paul Menagee788e062008-07-25 01:46:59 -0700114 /* The path to use for release notifications. */
Paul Menage81a6a5c2007-10-18 23:39:38 -0700115 char release_agent_path[PATH_MAX];
Paul Menagec6d57f32009-09-23 15:56:19 -0700116
117 /* The name for this hierarchy - may be empty */
118 char name[MAX_CGROUP_ROOT_NAMELEN];
Paul Menageddbcc7e2007-10-18 23:39:30 -0700119};
120
Paul Menageddbcc7e2007-10-18 23:39:30 -0700121/*
122 * The "rootnode" hierarchy is the "dummy hierarchy", reserved for the
123 * subsystems that are otherwise unattached - it never has more than a
124 * single cgroup, and all tasks are part of that cgroup.
125 */
126static struct cgroupfs_root rootnode;
127
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700128/*
129 * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when
130 * cgroup_subsys->use_id != 0.
131 */
132#define CSS_ID_MAX (65535)
133struct css_id {
134 /*
135 * The css to which this ID points. This pointer is set to valid value
136 * after cgroup is populated. If cgroup is removed, this will be NULL.
137 * This pointer is expected to be RCU-safe because destroy()
138 * is called after synchronize_rcu(). But for safe use, css_is_removed()
139 * css_tryget() should be used for avoiding race.
140 */
Arnd Bergmann2c392b82010-02-24 19:41:39 +0100141 struct cgroup_subsys_state __rcu *css;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700142 /*
143 * ID of this css.
144 */
145 unsigned short id;
146 /*
147 * Depth in hierarchy which this ID belongs to.
148 */
149 unsigned short depth;
150 /*
151 * ID is freed by RCU. (and lookup routine is RCU safe.)
152 */
153 struct rcu_head rcu_head;
154 /*
155 * Hierarchy of CSS ID belongs to.
156 */
157 unsigned short stack[0]; /* Array of Length (depth+1) */
158};
159
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800160/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300161 * cgroup_event represents events which userspace want to receive.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800162 */
163struct cgroup_event {
164 /*
165 * Cgroup which the event belongs to.
166 */
167 struct cgroup *cgrp;
168 /*
169 * Control file which the event associated.
170 */
171 struct cftype *cft;
172 /*
173 * eventfd to signal userspace about the event.
174 */
175 struct eventfd_ctx *eventfd;
176 /*
177 * Each of these stored in a list by the cgroup.
178 */
179 struct list_head list;
180 /*
181 * All fields below needed to unregister event when
182 * userspace closes eventfd.
183 */
184 poll_table pt;
185 wait_queue_head_t *wqh;
186 wait_queue_t wait;
187 struct work_struct remove;
188};
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700189
Paul Menageddbcc7e2007-10-18 23:39:30 -0700190/* The list of hierarchy roots */
191
192static LIST_HEAD(roots);
Paul Menage817929e2007-10-18 23:39:36 -0700193static int root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700194
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700195static DEFINE_IDA(hierarchy_ida);
196static int next_hierarchy_id;
197static DEFINE_SPINLOCK(hierarchy_id_lock);
198
Paul Menageddbcc7e2007-10-18 23:39:30 -0700199/* dummytop is a shorthand for the dummy hierarchy's top cgroup */
200#define dummytop (&rootnode.top_cgroup)
201
202/* This flag indicates whether tasks in the fork and exit paths should
Li Zefana043e3b2008-02-23 15:24:09 -0800203 * check for fork/exit handlers to call. This avoids us having to do
204 * extra work in the fork/exit path if none of the subsystems need to
205 * be called.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700206 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700207static int need_forkexit_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700208
Paul E. McKenneyd11c5632010-02-22 17:04:50 -0800209#ifdef CONFIG_PROVE_LOCKING
210int cgroup_lock_is_held(void)
211{
212 return lockdep_is_held(&cgroup_mutex);
213}
214#else /* #ifdef CONFIG_PROVE_LOCKING */
215int cgroup_lock_is_held(void)
216{
217 return mutex_is_locked(&cgroup_mutex);
218}
219#endif /* #else #ifdef CONFIG_PROVE_LOCKING */
220
221EXPORT_SYMBOL_GPL(cgroup_lock_is_held);
222
Paul Menageddbcc7e2007-10-18 23:39:30 -0700223/* convenient tests for these bits */
Paul Menagebd89aab2007-10-18 23:40:44 -0700224inline int cgroup_is_removed(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700225{
Paul Menagebd89aab2007-10-18 23:40:44 -0700226 return test_bit(CGRP_REMOVED, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700227}
228
229/* bits in struct cgroupfs_root flags field */
230enum {
231 ROOT_NOPREFIX, /* mounted subsystems have no named prefix */
232};
233
Adrian Bunke9685a02008-02-07 00:13:46 -0800234static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700235{
236 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700237 (1 << CGRP_RELEASABLE) |
238 (1 << CGRP_NOTIFY_ON_RELEASE);
239 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700240}
241
Adrian Bunke9685a02008-02-07 00:13:46 -0800242static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700243{
Paul Menagebd89aab2007-10-18 23:40:44 -0700244 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700245}
246
Daniel Lezcano97978e62010-10-27 15:33:35 -0700247static int clone_children(const struct cgroup *cgrp)
248{
249 return test_bit(CGRP_CLONE_CHILDREN, &cgrp->flags);
250}
251
Paul Menageddbcc7e2007-10-18 23:39:30 -0700252/*
253 * for_each_subsys() allows you to iterate on each subsystem attached to
254 * an active hierarchy
255 */
256#define for_each_subsys(_root, _ss) \
257list_for_each_entry(_ss, &_root->subsys_list, sibling)
258
Li Zefane5f6a862009-01-07 18:07:41 -0800259/* for_each_active_root() allows you to iterate across the active hierarchies */
260#define for_each_active_root(_root) \
Paul Menageddbcc7e2007-10-18 23:39:30 -0700261list_for_each_entry(_root, &roots, root_list)
262
Paul Menage81a6a5c2007-10-18 23:39:38 -0700263/* the list of cgroups eligible for automatic release. Protected by
264 * release_list_lock */
265static LIST_HEAD(release_list);
266static DEFINE_SPINLOCK(release_list_lock);
267static void cgroup_release_agent(struct work_struct *work);
268static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700269static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700270
Paul Menage817929e2007-10-18 23:39:36 -0700271/* Link structure for associating css_set objects with cgroups */
272struct cg_cgroup_link {
273 /*
274 * List running through cg_cgroup_links associated with a
275 * cgroup, anchored on cgroup->css_sets
276 */
Paul Menagebd89aab2007-10-18 23:40:44 -0700277 struct list_head cgrp_link_list;
Paul Menage7717f7b2009-09-23 15:56:22 -0700278 struct cgroup *cgrp;
Paul Menage817929e2007-10-18 23:39:36 -0700279 /*
280 * List running through cg_cgroup_links pointing at a
281 * single css_set object, anchored on css_set->cg_links
282 */
283 struct list_head cg_link_list;
284 struct css_set *cg;
285};
286
287/* The default css_set - used by init and its children prior to any
288 * hierarchies being mounted. It contains a pointer to the root state
289 * for each subsystem. Also used to anchor the list of css_sets. Not
290 * reference-counted, to improve performance when child cgroups
291 * haven't been created.
292 */
293
294static struct css_set init_css_set;
295static struct cg_cgroup_link init_css_set_link;
296
Ben Blume6a11052010-03-10 15:22:09 -0800297static int cgroup_init_idr(struct cgroup_subsys *ss,
298 struct cgroup_subsys_state *css);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700299
Paul Menage817929e2007-10-18 23:39:36 -0700300/* css_set_lock protects the list of css_set objects, and the
301 * chain of tasks off each css_set. Nests outside task->alloc_lock
302 * due to cgroup_iter_start() */
303static DEFINE_RWLOCK(css_set_lock);
304static int css_set_count;
305
Paul Menage7717f7b2009-09-23 15:56:22 -0700306/*
307 * hash table for cgroup groups. This improves the performance to find
308 * an existing css_set. This hash doesn't (currently) take into
309 * account cgroups in empty hierarchies.
310 */
Li Zefan472b1052008-04-29 01:00:11 -0700311#define CSS_SET_HASH_BITS 7
312#define CSS_SET_TABLE_SIZE (1 << CSS_SET_HASH_BITS)
313static struct hlist_head css_set_table[CSS_SET_TABLE_SIZE];
314
315static struct hlist_head *css_set_hash(struct cgroup_subsys_state *css[])
316{
317 int i;
318 int index;
319 unsigned long tmp = 0UL;
320
321 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++)
322 tmp += (unsigned long)css[i];
323 tmp = (tmp >> 16) ^ tmp;
324
325 index = hash_long(tmp, CSS_SET_HASH_BITS);
326
327 return &css_set_table[index];
328}
329
Paul Menage817929e2007-10-18 23:39:36 -0700330/* We don't maintain the lists running through each css_set to its
331 * task until after the first call to cgroup_iter_start(). This
332 * reduces the fork()/exit() overhead for people who have cgroups
333 * compiled into their kernel but not actually in use */
Li Zefan8947f9d2008-07-25 01:46:56 -0700334static int use_task_css_set_links __read_mostly;
Paul Menage817929e2007-10-18 23:39:36 -0700335
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700336static void __put_css_set(struct css_set *cg, int taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700337{
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700338 struct cg_cgroup_link *link;
339 struct cg_cgroup_link *saved_link;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700340 /*
341 * Ensure that the refcount doesn't hit zero while any readers
342 * can see it. Similar to atomic_dec_and_lock(), but for an
343 * rwlock
344 */
345 if (atomic_add_unless(&cg->refcount, -1, 1))
346 return;
347 write_lock(&css_set_lock);
348 if (!atomic_dec_and_test(&cg->refcount)) {
349 write_unlock(&css_set_lock);
350 return;
351 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700352
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700353 /* This css_set is dead. unlink it and release cgroup refcounts */
354 hlist_del(&cg->hlist);
355 css_set_count--;
356
357 list_for_each_entry_safe(link, saved_link, &cg->cg_links,
358 cg_link_list) {
359 struct cgroup *cgrp = link->cgrp;
360 list_del(&link->cg_link_list);
361 list_del(&link->cgrp_link_list);
Paul Menagebd89aab2007-10-18 23:40:44 -0700362 if (atomic_dec_and_test(&cgrp->count) &&
363 notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700364 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700365 set_bit(CGRP_RELEASABLE, &cgrp->flags);
366 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700367 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700368
369 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700370 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700371
372 write_unlock(&css_set_lock);
Lai Jiangshan30088ad2011-03-15 17:53:46 +0800373 kfree_rcu(cg, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700374}
375
376/*
377 * refcounted get/put for css_set objects
378 */
379static inline void get_css_set(struct css_set *cg)
380{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700381 atomic_inc(&cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700382}
383
384static inline void put_css_set(struct css_set *cg)
385{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700386 __put_css_set(cg, 0);
Paul Menage817929e2007-10-18 23:39:36 -0700387}
388
Paul Menage81a6a5c2007-10-18 23:39:38 -0700389static inline void put_css_set_taskexit(struct css_set *cg)
390{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700391 __put_css_set(cg, 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700392}
393
Paul Menage817929e2007-10-18 23:39:36 -0700394/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700395 * compare_css_sets - helper function for find_existing_css_set().
396 * @cg: candidate css_set being tested
397 * @old_cg: existing css_set for a task
398 * @new_cgrp: cgroup that's being entered by the task
399 * @template: desired set of css pointers in css_set (pre-calculated)
400 *
401 * Returns true if "cg" matches "old_cg" except for the hierarchy
402 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
403 */
404static bool compare_css_sets(struct css_set *cg,
405 struct css_set *old_cg,
406 struct cgroup *new_cgrp,
407 struct cgroup_subsys_state *template[])
408{
409 struct list_head *l1, *l2;
410
411 if (memcmp(template, cg->subsys, sizeof(cg->subsys))) {
412 /* Not all subsystems matched */
413 return false;
414 }
415
416 /*
417 * Compare cgroup pointers in order to distinguish between
418 * different cgroups in heirarchies with no subsystems. We
419 * could get by with just this check alone (and skip the
420 * memcmp above) but on most setups the memcmp check will
421 * avoid the need for this more expensive check on almost all
422 * candidates.
423 */
424
425 l1 = &cg->cg_links;
426 l2 = &old_cg->cg_links;
427 while (1) {
428 struct cg_cgroup_link *cgl1, *cgl2;
429 struct cgroup *cg1, *cg2;
430
431 l1 = l1->next;
432 l2 = l2->next;
433 /* See if we reached the end - both lists are equal length. */
434 if (l1 == &cg->cg_links) {
435 BUG_ON(l2 != &old_cg->cg_links);
436 break;
437 } else {
438 BUG_ON(l2 == &old_cg->cg_links);
439 }
440 /* Locate the cgroups associated with these links. */
441 cgl1 = list_entry(l1, struct cg_cgroup_link, cg_link_list);
442 cgl2 = list_entry(l2, struct cg_cgroup_link, cg_link_list);
443 cg1 = cgl1->cgrp;
444 cg2 = cgl2->cgrp;
445 /* Hierarchies should be linked in the same order. */
446 BUG_ON(cg1->root != cg2->root);
447
448 /*
449 * If this hierarchy is the hierarchy of the cgroup
450 * that's changing, then we need to check that this
451 * css_set points to the new cgroup; if it's any other
452 * hierarchy, then this css_set should point to the
453 * same cgroup as the old css_set.
454 */
455 if (cg1->root == new_cgrp->root) {
456 if (cg1 != new_cgrp)
457 return false;
458 } else {
459 if (cg1 != cg2)
460 return false;
461 }
462 }
463 return true;
464}
465
466/*
Paul Menage817929e2007-10-18 23:39:36 -0700467 * find_existing_css_set() is a helper for
468 * find_css_set(), and checks to see whether an existing
Li Zefan472b1052008-04-29 01:00:11 -0700469 * css_set is suitable.
Paul Menage817929e2007-10-18 23:39:36 -0700470 *
471 * oldcg: the cgroup group that we're using before the cgroup
472 * transition
473 *
Paul Menagebd89aab2007-10-18 23:40:44 -0700474 * cgrp: the cgroup that we're moving into
Paul Menage817929e2007-10-18 23:39:36 -0700475 *
476 * template: location in which to build the desired set of subsystem
477 * state objects for the new cgroup group
478 */
Paul Menage817929e2007-10-18 23:39:36 -0700479static struct css_set *find_existing_css_set(
480 struct css_set *oldcg,
Paul Menagebd89aab2007-10-18 23:40:44 -0700481 struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -0700482 struct cgroup_subsys_state *template[])
483{
484 int i;
Paul Menagebd89aab2007-10-18 23:40:44 -0700485 struct cgroupfs_root *root = cgrp->root;
Li Zefan472b1052008-04-29 01:00:11 -0700486 struct hlist_head *hhead;
487 struct hlist_node *node;
488 struct css_set *cg;
Paul Menage817929e2007-10-18 23:39:36 -0700489
Ben Blumaae8aab2010-03-10 15:22:07 -0800490 /*
491 * Build the set of subsystem state objects that we want to see in the
492 * new css_set. while subsystems can change globally, the entries here
493 * won't change, so no need for locking.
494 */
Paul Menage817929e2007-10-18 23:39:36 -0700495 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Li Zefan8d53d552008-02-23 15:24:11 -0800496 if (root->subsys_bits & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700497 /* Subsystem is in this hierarchy. So we want
498 * the subsystem state from the new
499 * cgroup */
Paul Menagebd89aab2007-10-18 23:40:44 -0700500 template[i] = cgrp->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700501 } else {
502 /* Subsystem is not in this hierarchy, so we
503 * don't want to change the subsystem state */
504 template[i] = oldcg->subsys[i];
505 }
506 }
507
Li Zefan472b1052008-04-29 01:00:11 -0700508 hhead = css_set_hash(template);
509 hlist_for_each_entry(cg, node, hhead, hlist) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700510 if (!compare_css_sets(cg, oldcg, cgrp, template))
511 continue;
512
513 /* This css_set matches what we need */
514 return cg;
Li Zefan472b1052008-04-29 01:00:11 -0700515 }
Paul Menage817929e2007-10-18 23:39:36 -0700516
517 /* No existing cgroup group matched */
518 return NULL;
519}
520
Paul Menage817929e2007-10-18 23:39:36 -0700521static void free_cg_links(struct list_head *tmp)
522{
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700523 struct cg_cgroup_link *link;
524 struct cg_cgroup_link *saved_link;
525
526 list_for_each_entry_safe(link, saved_link, tmp, cgrp_link_list) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700527 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -0700528 kfree(link);
529 }
530}
531
532/*
Li Zefan36553432008-07-29 22:33:19 -0700533 * allocate_cg_links() allocates "count" cg_cgroup_link structures
534 * and chains them on tmp through their cgrp_link_list fields. Returns 0 on
535 * success or a negative error
536 */
537static int allocate_cg_links(int count, struct list_head *tmp)
538{
539 struct cg_cgroup_link *link;
540 int i;
541 INIT_LIST_HEAD(tmp);
542 for (i = 0; i < count; i++) {
543 link = kmalloc(sizeof(*link), GFP_KERNEL);
544 if (!link) {
545 free_cg_links(tmp);
546 return -ENOMEM;
547 }
548 list_add(&link->cgrp_link_list, tmp);
549 }
550 return 0;
551}
552
Li Zefanc12f65d2009-01-07 18:07:42 -0800553/**
554 * link_css_set - a helper function to link a css_set to a cgroup
555 * @tmp_cg_links: cg_cgroup_link objects allocated by allocate_cg_links()
556 * @cg: the css_set to be linked
557 * @cgrp: the destination cgroup
558 */
559static void link_css_set(struct list_head *tmp_cg_links,
560 struct css_set *cg, struct cgroup *cgrp)
561{
562 struct cg_cgroup_link *link;
563
564 BUG_ON(list_empty(tmp_cg_links));
565 link = list_first_entry(tmp_cg_links, struct cg_cgroup_link,
566 cgrp_link_list);
567 link->cg = cg;
Paul Menage7717f7b2009-09-23 15:56:22 -0700568 link->cgrp = cgrp;
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700569 atomic_inc(&cgrp->count);
Li Zefanc12f65d2009-01-07 18:07:42 -0800570 list_move(&link->cgrp_link_list, &cgrp->css_sets);
Paul Menage7717f7b2009-09-23 15:56:22 -0700571 /*
572 * Always add links to the tail of the list so that the list
573 * is sorted by order of hierarchy creation
574 */
575 list_add_tail(&link->cg_link_list, &cg->cg_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800576}
577
Li Zefan36553432008-07-29 22:33:19 -0700578/*
Paul Menage817929e2007-10-18 23:39:36 -0700579 * find_css_set() takes an existing cgroup group and a
580 * cgroup object, and returns a css_set object that's
581 * equivalent to the old group, but with the given cgroup
582 * substituted into the appropriate hierarchy. Must be called with
583 * cgroup_mutex held
584 */
Paul Menage817929e2007-10-18 23:39:36 -0700585static struct css_set *find_css_set(
Paul Menagebd89aab2007-10-18 23:40:44 -0700586 struct css_set *oldcg, struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700587{
588 struct css_set *res;
589 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT];
Paul Menage817929e2007-10-18 23:39:36 -0700590
591 struct list_head tmp_cg_links;
Paul Menage817929e2007-10-18 23:39:36 -0700592
Li Zefan472b1052008-04-29 01:00:11 -0700593 struct hlist_head *hhead;
Paul Menage7717f7b2009-09-23 15:56:22 -0700594 struct cg_cgroup_link *link;
Li Zefan472b1052008-04-29 01:00:11 -0700595
Paul Menage817929e2007-10-18 23:39:36 -0700596 /* First see if we already have a cgroup group that matches
597 * the desired set */
Li Zefan7e9abd82008-07-25 01:46:54 -0700598 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -0700599 res = find_existing_css_set(oldcg, cgrp, template);
Paul Menage817929e2007-10-18 23:39:36 -0700600 if (res)
601 get_css_set(res);
Li Zefan7e9abd82008-07-25 01:46:54 -0700602 read_unlock(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -0700603
604 if (res)
605 return res;
606
607 res = kmalloc(sizeof(*res), GFP_KERNEL);
608 if (!res)
609 return NULL;
610
611 /* Allocate all the cg_cgroup_link objects that we'll need */
612 if (allocate_cg_links(root_count, &tmp_cg_links) < 0) {
613 kfree(res);
614 return NULL;
615 }
616
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700617 atomic_set(&res->refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -0700618 INIT_LIST_HEAD(&res->cg_links);
619 INIT_LIST_HEAD(&res->tasks);
Li Zefan472b1052008-04-29 01:00:11 -0700620 INIT_HLIST_NODE(&res->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700621
622 /* Copy the set of subsystem state objects generated in
623 * find_existing_css_set() */
624 memcpy(res->subsys, template, sizeof(res->subsys));
625
626 write_lock(&css_set_lock);
627 /* Add reference counts and links from the new css_set. */
Paul Menage7717f7b2009-09-23 15:56:22 -0700628 list_for_each_entry(link, &oldcg->cg_links, cg_link_list) {
629 struct cgroup *c = link->cgrp;
630 if (c->root == cgrp->root)
631 c = cgrp;
632 link_css_set(&tmp_cg_links, res, c);
633 }
Paul Menage817929e2007-10-18 23:39:36 -0700634
635 BUG_ON(!list_empty(&tmp_cg_links));
636
Paul Menage817929e2007-10-18 23:39:36 -0700637 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700638
639 /* Add this cgroup group to the hash table */
640 hhead = css_set_hash(res->subsys);
641 hlist_add_head(&res->hlist, hhead);
642
Paul Menage817929e2007-10-18 23:39:36 -0700643 write_unlock(&css_set_lock);
644
645 return res;
Paul Menageb4f48b62007-10-18 23:39:33 -0700646}
647
Paul Menageddbcc7e2007-10-18 23:39:30 -0700648/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700649 * Return the cgroup for "task" from the given hierarchy. Must be
650 * called with cgroup_mutex held.
651 */
652static struct cgroup *task_cgroup_from_root(struct task_struct *task,
653 struct cgroupfs_root *root)
654{
655 struct css_set *css;
656 struct cgroup *res = NULL;
657
658 BUG_ON(!mutex_is_locked(&cgroup_mutex));
659 read_lock(&css_set_lock);
660 /*
661 * No need to lock the task - since we hold cgroup_mutex the
662 * task can't change groups, so the only thing that can happen
663 * is that it exits and its css is set back to init_css_set.
664 */
665 css = task->cgroups;
666 if (css == &init_css_set) {
667 res = &root->top_cgroup;
668 } else {
669 struct cg_cgroup_link *link;
670 list_for_each_entry(link, &css->cg_links, cg_link_list) {
671 struct cgroup *c = link->cgrp;
672 if (c->root == root) {
673 res = c;
674 break;
675 }
676 }
677 }
678 read_unlock(&css_set_lock);
679 BUG_ON(!res);
680 return res;
681}
682
683/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700684 * There is one global cgroup mutex. We also require taking
685 * task_lock() when dereferencing a task's cgroup subsys pointers.
686 * See "The task_lock() exception", at the end of this comment.
687 *
688 * A task must hold cgroup_mutex to modify cgroups.
689 *
690 * Any task can increment and decrement the count field without lock.
691 * So in general, code holding cgroup_mutex can't rely on the count
692 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800693 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700694 * means that no tasks are currently attached, therefore there is no
695 * way a task attached to that cgroup can fork (the other way to
696 * increment the count). So code holding cgroup_mutex can safely
697 * assume that if the count is zero, it will stay zero. Similarly, if
698 * a task holds cgroup_mutex on a cgroup with zero count, it
699 * knows that the cgroup won't be removed, as cgroup_rmdir()
700 * needs that mutex.
701 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700702 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
703 * (usually) take cgroup_mutex. These are the two most performance
704 * critical pieces of code here. The exception occurs on cgroup_exit(),
705 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
706 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800707 * to the release agent with the name of the cgroup (path relative to
708 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700709 *
710 * A cgroup can only be deleted if both its 'count' of using tasks
711 * is zero, and its list of 'children' cgroups is empty. Since all
712 * tasks in the system use _some_ cgroup, and since there is always at
713 * least one task in the system (init, pid == 1), therefore, top_cgroup
714 * always has either children cgroups and/or using tasks. So we don't
715 * need a special hack to ensure that top_cgroup cannot be deleted.
716 *
717 * The task_lock() exception
718 *
719 * The need for this exception arises from the action of
Cliff Wickman956db3c2008-02-07 00:14:43 -0800720 * cgroup_attach_task(), which overwrites one tasks cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800721 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700722 * several performance critical places that need to reference
723 * task->cgroup without the expense of grabbing a system global
724 * mutex. Therefore except as noted below, when dereferencing or, as
Cliff Wickman956db3c2008-02-07 00:14:43 -0800725 * in cgroup_attach_task(), modifying a task'ss cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700726 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
727 * the task_struct routinely used for such matters.
728 *
729 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800730 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700731 */
732
Paul Menageddbcc7e2007-10-18 23:39:30 -0700733/**
734 * cgroup_lock - lock out any changes to cgroup structures
735 *
736 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700737void cgroup_lock(void)
738{
739 mutex_lock(&cgroup_mutex);
740}
Ben Blum67523c42010-03-10 15:22:11 -0800741EXPORT_SYMBOL_GPL(cgroup_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700742
743/**
744 * cgroup_unlock - release lock on cgroup changes
745 *
746 * Undo the lock taken in a previous cgroup_lock() call.
747 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700748void cgroup_unlock(void)
749{
750 mutex_unlock(&cgroup_mutex);
751}
Ben Blum67523c42010-03-10 15:22:11 -0800752EXPORT_SYMBOL_GPL(cgroup_unlock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700753
754/*
755 * A couple of forward declarations required, due to cyclic reference loop:
756 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
757 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
758 * -> cgroup_mkdir.
759 */
760
761static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode);
Al Viroc72a04e2011-01-14 05:31:45 +0000762static struct dentry *cgroup_lookup(struct inode *, struct dentry *, struct nameidata *);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700763static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -0700764static int cgroup_populate_dir(struct cgroup *cgrp);
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700765static const struct inode_operations cgroup_dir_inode_operations;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700766static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700767
768static struct backing_dev_info cgroup_backing_dev_info = {
Jens Axboed9938312009-06-12 14:45:52 +0200769 .name = "cgroup",
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700770 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Paul Menagea4243162007-10-18 23:39:35 -0700771};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700772
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700773static int alloc_css_id(struct cgroup_subsys *ss,
774 struct cgroup *parent, struct cgroup *child);
775
Paul Menageddbcc7e2007-10-18 23:39:30 -0700776static struct inode *cgroup_new_inode(mode_t mode, struct super_block *sb)
777{
778 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700779
780 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400781 inode->i_ino = get_next_ino();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700782 inode->i_mode = mode;
David Howells76aac0e2008-11-14 10:39:12 +1100783 inode->i_uid = current_fsuid();
784 inode->i_gid = current_fsgid();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700785 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
786 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
787 }
788 return inode;
789}
790
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800791/*
792 * Call subsys's pre_destroy handler.
793 * This is called before css refcnt check.
794 */
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700795static int cgroup_call_pre_destroy(struct cgroup *cgrp)
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800796{
797 struct cgroup_subsys *ss;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700798 int ret = 0;
799
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800800 for_each_subsys(cgrp->root, ss)
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700801 if (ss->pre_destroy) {
802 ret = ss->pre_destroy(ss, cgrp);
803 if (ret)
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -0800804 break;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700805 }
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800806
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700807 return ret;
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800808}
809
Paul Menageddbcc7e2007-10-18 23:39:30 -0700810static void cgroup_diput(struct dentry *dentry, struct inode *inode)
811{
812 /* is dentry a directory ? if so, kfree() associated cgroup */
813 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700814 struct cgroup *cgrp = dentry->d_fsdata;
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800815 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -0700816 BUG_ON(!(cgroup_is_removed(cgrp)));
Paul Menage81a6a5c2007-10-18 23:39:38 -0700817 /* It's possible for external users to be holding css
818 * reference counts on a cgroup; css_put() needs to
819 * be able to access the cgroup after decrementing
820 * the reference count in order to know if it needs to
821 * queue the cgroup to be handled by the release
822 * agent */
823 synchronize_rcu();
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800824
825 mutex_lock(&cgroup_mutex);
826 /*
827 * Release the subsystem state objects.
828 */
Li Zefan75139b82009-01-07 18:07:33 -0800829 for_each_subsys(cgrp->root, ss)
830 ss->destroy(ss, cgrp);
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800831
832 cgrp->root->number_of_cgroups--;
833 mutex_unlock(&cgroup_mutex);
834
Paul Menagea47295e2009-01-07 18:07:44 -0800835 /*
836 * Drop the active superblock reference that we took when we
837 * created the cgroup
838 */
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800839 deactivate_super(cgrp->root->sb);
840
Ben Blum72a8cb32009-09-23 15:56:27 -0700841 /*
842 * if we're getting rid of the cgroup, refcount should ensure
843 * that there are no pidlists left.
844 */
845 BUG_ON(!list_empty(&cgrp->pidlists));
846
Lai Jiangshanf2da1c42011-03-15 17:55:16 +0800847 kfree_rcu(cgrp, rcu_head);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700848 }
849 iput(inode);
850}
851
Al Viroc72a04e2011-01-14 05:31:45 +0000852static int cgroup_delete(const struct dentry *d)
853{
854 return 1;
855}
856
Paul Menageddbcc7e2007-10-18 23:39:30 -0700857static void remove_dir(struct dentry *d)
858{
859 struct dentry *parent = dget(d->d_parent);
860
861 d_delete(d);
862 simple_rmdir(parent->d_inode, d);
863 dput(parent);
864}
865
866static void cgroup_clear_directory(struct dentry *dentry)
867{
868 struct list_head *node;
869
870 BUG_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100871 spin_lock(&dentry->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700872 node = dentry->d_subdirs.next;
873 while (node != &dentry->d_subdirs) {
874 struct dentry *d = list_entry(node, struct dentry, d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100875
876 spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700877 list_del_init(node);
878 if (d->d_inode) {
879 /* This should never be called on a cgroup
880 * directory with child cgroups */
881 BUG_ON(d->d_inode->i_mode & S_IFDIR);
Nick Piggindc0474b2011-01-07 17:49:43 +1100882 dget_dlock(d);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100883 spin_unlock(&d->d_lock);
884 spin_unlock(&dentry->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700885 d_delete(d);
886 simple_unlink(dentry->d_inode, d);
887 dput(d);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100888 spin_lock(&dentry->d_lock);
889 } else
890 spin_unlock(&d->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700891 node = dentry->d_subdirs.next;
892 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100893 spin_unlock(&dentry->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700894}
895
896/*
897 * NOTE : the dentry must have been dget()'ed
898 */
899static void cgroup_d_remove_dir(struct dentry *dentry)
900{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100901 struct dentry *parent;
902
Paul Menageddbcc7e2007-10-18 23:39:30 -0700903 cgroup_clear_directory(dentry);
904
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100905 parent = dentry->d_parent;
906 spin_lock(&parent->d_lock);
Li Zefan3ec762a2011-01-14 11:34:34 +0800907 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700908 list_del_init(&dentry->d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100909 spin_unlock(&dentry->d_lock);
910 spin_unlock(&parent->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700911 remove_dir(dentry);
912}
913
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700914/*
915 * A queue for waiters to do rmdir() cgroup. A tasks will sleep when
916 * cgroup->count == 0 && list_empty(&cgroup->children) && subsys has some
917 * reference to css->refcnt. In general, this refcnt is expected to goes down
918 * to zero, soon.
919 *
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -0700920 * CGRP_WAIT_ON_RMDIR flag is set under cgroup's inode->i_mutex;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700921 */
922DECLARE_WAIT_QUEUE_HEAD(cgroup_rmdir_waitq);
923
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -0700924static void cgroup_wakeup_rmdir_waiter(struct cgroup *cgrp)
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700925{
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -0700926 if (unlikely(test_and_clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags)))
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700927 wake_up_all(&cgroup_rmdir_waitq);
928}
929
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -0700930void cgroup_exclude_rmdir(struct cgroup_subsys_state *css)
931{
932 css_get(css);
933}
934
935void cgroup_release_and_wakeup_rmdir(struct cgroup_subsys_state *css)
936{
937 cgroup_wakeup_rmdir_waiter(css->cgroup);
938 css_put(css);
939}
940
Ben Blumaae8aab2010-03-10 15:22:07 -0800941/*
Ben Blumcf5d5942010-03-10 15:22:09 -0800942 * Call with cgroup_mutex held. Drops reference counts on modules, including
943 * any duplicate ones that parse_cgroupfs_options took. If this function
944 * returns an error, no reference counts are touched.
Ben Blumaae8aab2010-03-10 15:22:07 -0800945 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700946static int rebind_subsystems(struct cgroupfs_root *root,
947 unsigned long final_bits)
948{
949 unsigned long added_bits, removed_bits;
Paul Menagebd89aab2007-10-18 23:40:44 -0700950 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700951 int i;
952
Ben Blumaae8aab2010-03-10 15:22:07 -0800953 BUG_ON(!mutex_is_locked(&cgroup_mutex));
954
Paul Menageddbcc7e2007-10-18 23:39:30 -0700955 removed_bits = root->actual_subsys_bits & ~final_bits;
956 added_bits = final_bits & ~root->actual_subsys_bits;
957 /* Check that any added subsystems are currently free */
958 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Li Zefan8d53d552008-02-23 15:24:11 -0800959 unsigned long bit = 1UL << i;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700960 struct cgroup_subsys *ss = subsys[i];
961 if (!(bit & added_bits))
962 continue;
Ben Blumaae8aab2010-03-10 15:22:07 -0800963 /*
964 * Nobody should tell us to do a subsys that doesn't exist:
965 * parse_cgroupfs_options should catch that case and refcounts
966 * ensure that subsystems won't disappear once selected.
967 */
968 BUG_ON(ss == NULL);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700969 if (ss->root != &rootnode) {
970 /* Subsystem isn't free */
971 return -EBUSY;
972 }
973 }
974
975 /* Currently we don't handle adding/removing subsystems when
976 * any child cgroups exist. This is theoretically supportable
977 * but involves complex error handling, so it's being left until
978 * later */
Paul Menage307257c2008-12-15 13:54:22 -0800979 if (root->number_of_cgroups > 1)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700980 return -EBUSY;
981
982 /* Process each subsystem */
983 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
984 struct cgroup_subsys *ss = subsys[i];
985 unsigned long bit = 1UL << i;
986 if (bit & added_bits) {
987 /* We're binding this subsystem to this hierarchy */
Ben Blumaae8aab2010-03-10 15:22:07 -0800988 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -0700989 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700990 BUG_ON(!dummytop->subsys[i]);
991 BUG_ON(dummytop->subsys[i]->cgroup != dummytop);
Paul Menage999cd8a2009-01-07 18:08:36 -0800992 mutex_lock(&ss->hierarchy_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -0700993 cgrp->subsys[i] = dummytop->subsys[i];
994 cgrp->subsys[i]->cgroup = cgrp;
Li Zefan33a68ac2009-01-07 18:07:42 -0800995 list_move(&ss->sibling, &root->subsys_list);
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -0800996 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700997 if (ss->bind)
Paul Menagebd89aab2007-10-18 23:40:44 -0700998 ss->bind(ss, cgrp);
Paul Menage999cd8a2009-01-07 18:08:36 -0800999 mutex_unlock(&ss->hierarchy_mutex);
Ben Blumcf5d5942010-03-10 15:22:09 -08001000 /* refcount was already taken, and we're keeping it */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001001 } else if (bit & removed_bits) {
1002 /* We're removing this subsystem */
Ben Blumaae8aab2010-03-10 15:22:07 -08001003 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001004 BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]);
1005 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
Paul Menage999cd8a2009-01-07 18:08:36 -08001006 mutex_lock(&ss->hierarchy_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001007 if (ss->bind)
1008 ss->bind(ss, dummytop);
1009 dummytop->subsys[i]->cgroup = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -07001010 cgrp->subsys[i] = NULL;
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001011 subsys[i]->root = &rootnode;
Li Zefan33a68ac2009-01-07 18:07:42 -08001012 list_move(&ss->sibling, &rootnode.subsys_list);
Paul Menage999cd8a2009-01-07 18:08:36 -08001013 mutex_unlock(&ss->hierarchy_mutex);
Ben Blumcf5d5942010-03-10 15:22:09 -08001014 /* subsystem is now free - drop reference on module */
1015 module_put(ss->module);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001016 } else if (bit & final_bits) {
1017 /* Subsystem state should already exist */
Ben Blumaae8aab2010-03-10 15:22:07 -08001018 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001019 BUG_ON(!cgrp->subsys[i]);
Ben Blumcf5d5942010-03-10 15:22:09 -08001020 /*
1021 * a refcount was taken, but we already had one, so
1022 * drop the extra reference.
1023 */
1024 module_put(ss->module);
1025#ifdef CONFIG_MODULE_UNLOAD
1026 BUG_ON(ss->module && !module_refcount(ss->module));
1027#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001028 } else {
1029 /* Subsystem state shouldn't exist */
Paul Menagebd89aab2007-10-18 23:40:44 -07001030 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001031 }
1032 }
1033 root->subsys_bits = root->actual_subsys_bits = final_bits;
1034 synchronize_rcu();
1035
1036 return 0;
1037}
1038
1039static int cgroup_show_options(struct seq_file *seq, struct vfsmount *vfs)
1040{
1041 struct cgroupfs_root *root = vfs->mnt_sb->s_fs_info;
1042 struct cgroup_subsys *ss;
1043
1044 mutex_lock(&cgroup_mutex);
1045 for_each_subsys(root, ss)
1046 seq_printf(seq, ",%s", ss->name);
1047 if (test_bit(ROOT_NOPREFIX, &root->flags))
1048 seq_puts(seq, ",noprefix");
Paul Menage81a6a5c2007-10-18 23:39:38 -07001049 if (strlen(root->release_agent_path))
1050 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Daniel Lezcano97978e62010-10-27 15:33:35 -07001051 if (clone_children(&root->top_cgroup))
1052 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001053 if (strlen(root->name))
1054 seq_printf(seq, ",name=%s", root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001055 mutex_unlock(&cgroup_mutex);
1056 return 0;
1057}
1058
1059struct cgroup_sb_opts {
1060 unsigned long subsys_bits;
1061 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001062 char *release_agent;
Daniel Lezcano97978e62010-10-27 15:33:35 -07001063 bool clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001064 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001065 /* User explicitly requested empty subsystem */
1066 bool none;
Paul Menagec6d57f32009-09-23 15:56:19 -07001067
1068 struct cgroupfs_root *new_root;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001069
Paul Menageddbcc7e2007-10-18 23:39:30 -07001070};
1071
Ben Blumaae8aab2010-03-10 15:22:07 -08001072/*
1073 * Convert a hierarchy specifier into a bitmask of subsystems and flags. Call
Ben Blumcf5d5942010-03-10 15:22:09 -08001074 * with cgroup_mutex held to protect the subsys[] array. This function takes
1075 * refcounts on subsystems to be used, unless it returns error, in which case
1076 * no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001077 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001078static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001079{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001080 char *token, *o = data;
1081 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001082 unsigned long mask = (unsigned long)-1;
Ben Blumcf5d5942010-03-10 15:22:09 -08001083 int i;
1084 bool module_pin_failed = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001085
Ben Blumaae8aab2010-03-10 15:22:07 -08001086 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1087
Li Zefanf9ab5b52009-06-17 16:26:33 -07001088#ifdef CONFIG_CPUSETS
1089 mask = ~(1UL << cpuset_subsys_id);
1090#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001091
Paul Menagec6d57f32009-09-23 15:56:19 -07001092 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001093
1094 while ((token = strsep(&o, ",")) != NULL) {
1095 if (!*token)
1096 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001097 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001098 /* Explicitly have no subsystems */
1099 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001100 continue;
1101 }
1102 if (!strcmp(token, "all")) {
1103 /* Mutually exclusive option 'all' + subsystem name */
1104 if (one_ss)
1105 return -EINVAL;
1106 all_ss = true;
1107 continue;
1108 }
1109 if (!strcmp(token, "noprefix")) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001110 set_bit(ROOT_NOPREFIX, &opts->flags);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001111 continue;
1112 }
1113 if (!strcmp(token, "clone_children")) {
Daniel Lezcano97978e62010-10-27 15:33:35 -07001114 opts->clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001115 continue;
1116 }
1117 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001118 /* Specifying two release agents is forbidden */
1119 if (opts->release_agent)
1120 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001121 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001122 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001123 if (!opts->release_agent)
1124 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001125 continue;
1126 }
1127 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001128 const char *name = token + 5;
1129 /* Can't specify an empty name */
1130 if (!strlen(name))
1131 return -EINVAL;
1132 /* Must match [\w.-]+ */
1133 for (i = 0; i < strlen(name); i++) {
1134 char c = name[i];
1135 if (isalnum(c))
1136 continue;
1137 if ((c == '.') || (c == '-') || (c == '_'))
1138 continue;
1139 return -EINVAL;
1140 }
1141 /* Specifying two names is forbidden */
1142 if (opts->name)
1143 return -EINVAL;
1144 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001145 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001146 GFP_KERNEL);
1147 if (!opts->name)
1148 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001149
1150 continue;
1151 }
1152
1153 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1154 struct cgroup_subsys *ss = subsys[i];
1155 if (ss == NULL)
1156 continue;
1157 if (strcmp(token, ss->name))
1158 continue;
1159 if (ss->disabled)
1160 continue;
1161
1162 /* Mutually exclusive option 'all' + subsystem name */
1163 if (all_ss)
1164 return -EINVAL;
1165 set_bit(i, &opts->subsys_bits);
1166 one_ss = true;
1167
1168 break;
1169 }
1170 if (i == CGROUP_SUBSYS_COUNT)
1171 return -ENOENT;
1172 }
1173
1174 /*
1175 * If the 'all' option was specified select all the subsystems,
Li Zefan2481cbc2011-12-27 14:25:55 +08001176 * otherwise if 'none', 'name=' and a subsystem name options
1177 * were not specified, let's default to 'all'
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001178 */
Li Zefan2481cbc2011-12-27 14:25:55 +08001179 if (all_ss || (!one_ss && !opts->none && !opts->name)) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001180 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1181 struct cgroup_subsys *ss = subsys[i];
1182 if (ss == NULL)
1183 continue;
1184 if (ss->disabled)
1185 continue;
1186 set_bit(i, &opts->subsys_bits);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001187 }
1188 }
1189
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001190 /* Consistency checks */
1191
Li Zefanf9ab5b52009-06-17 16:26:33 -07001192 /*
1193 * Option noprefix was introduced just for backward compatibility
1194 * with the old cpuset, so we allow noprefix only if mounting just
1195 * the cpuset subsystem.
1196 */
1197 if (test_bit(ROOT_NOPREFIX, &opts->flags) &&
1198 (opts->subsys_bits & mask))
1199 return -EINVAL;
1200
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001201
1202 /* Can't specify "none" and some subsystems */
1203 if (opts->subsys_bits && opts->none)
1204 return -EINVAL;
1205
1206 /*
1207 * We either have to specify by name or by subsystems. (So all
1208 * empty hierarchies must have a name).
1209 */
Paul Menagec6d57f32009-09-23 15:56:19 -07001210 if (!opts->subsys_bits && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001211 return -EINVAL;
1212
Ben Blumcf5d5942010-03-10 15:22:09 -08001213 /*
1214 * Grab references on all the modules we'll need, so the subsystems
1215 * don't dance around before rebind_subsystems attaches them. This may
1216 * take duplicate reference counts on a subsystem that's already used,
1217 * but rebind_subsystems handles this case.
1218 */
1219 for (i = CGROUP_BUILTIN_SUBSYS_COUNT; i < CGROUP_SUBSYS_COUNT; i++) {
1220 unsigned long bit = 1UL << i;
1221
1222 if (!(bit & opts->subsys_bits))
1223 continue;
1224 if (!try_module_get(subsys[i]->module)) {
1225 module_pin_failed = true;
1226 break;
1227 }
1228 }
1229 if (module_pin_failed) {
1230 /*
1231 * oops, one of the modules was going away. this means that we
1232 * raced with a module_delete call, and to the user this is
1233 * essentially a "subsystem doesn't exist" case.
1234 */
1235 for (i--; i >= CGROUP_BUILTIN_SUBSYS_COUNT; i--) {
1236 /* drop refcounts only on the ones we took */
1237 unsigned long bit = 1UL << i;
1238
1239 if (!(bit & opts->subsys_bits))
1240 continue;
1241 module_put(subsys[i]->module);
1242 }
1243 return -ENOENT;
1244 }
1245
Paul Menageddbcc7e2007-10-18 23:39:30 -07001246 return 0;
1247}
1248
Ben Blumcf5d5942010-03-10 15:22:09 -08001249static void drop_parsed_module_refcounts(unsigned long subsys_bits)
1250{
1251 int i;
1252 for (i = CGROUP_BUILTIN_SUBSYS_COUNT; i < CGROUP_SUBSYS_COUNT; i++) {
1253 unsigned long bit = 1UL << i;
1254
1255 if (!(bit & subsys_bits))
1256 continue;
1257 module_put(subsys[i]->module);
1258 }
1259}
1260
Paul Menageddbcc7e2007-10-18 23:39:30 -07001261static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1262{
1263 int ret = 0;
1264 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001265 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001266 struct cgroup_sb_opts opts;
1267
Paul Menagebd89aab2007-10-18 23:40:44 -07001268 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001269 mutex_lock(&cgroup_mutex);
1270
1271 /* See what subsystems are wanted */
1272 ret = parse_cgroupfs_options(data, &opts);
1273 if (ret)
1274 goto out_unlock;
1275
Ben Blumcf5d5942010-03-10 15:22:09 -08001276 /* Don't allow flags or name to change at remount */
1277 if (opts.flags != root->flags ||
1278 (opts.name && strcmp(opts.name, root->name))) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001279 ret = -EINVAL;
Ben Blumcf5d5942010-03-10 15:22:09 -08001280 drop_parsed_module_refcounts(opts.subsys_bits);
Paul Menagec6d57f32009-09-23 15:56:19 -07001281 goto out_unlock;
1282 }
1283
Paul Menageddbcc7e2007-10-18 23:39:30 -07001284 ret = rebind_subsystems(root, opts.subsys_bits);
Ben Blumcf5d5942010-03-10 15:22:09 -08001285 if (ret) {
1286 drop_parsed_module_refcounts(opts.subsys_bits);
Li Zefan0670e082009-04-02 16:57:30 -07001287 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001288 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001289
1290 /* (re)populate subsystem files */
Li Zefan0670e082009-04-02 16:57:30 -07001291 cgroup_populate_dir(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001292
Paul Menage81a6a5c2007-10-18 23:39:38 -07001293 if (opts.release_agent)
1294 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001295 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001296 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001297 kfree(opts.name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001298 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07001299 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001300 return ret;
1301}
1302
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07001303static const struct super_operations cgroup_ops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001304 .statfs = simple_statfs,
1305 .drop_inode = generic_delete_inode,
1306 .show_options = cgroup_show_options,
1307 .remount_fs = cgroup_remount,
1308};
1309
Paul Menagecc31edc2008-10-18 20:28:04 -07001310static void init_cgroup_housekeeping(struct cgroup *cgrp)
1311{
1312 INIT_LIST_HEAD(&cgrp->sibling);
1313 INIT_LIST_HEAD(&cgrp->children);
1314 INIT_LIST_HEAD(&cgrp->css_sets);
1315 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001316 INIT_LIST_HEAD(&cgrp->pidlists);
1317 mutex_init(&cgrp->pidlist_mutex);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08001318 INIT_LIST_HEAD(&cgrp->event_list);
1319 spin_lock_init(&cgrp->event_list_lock);
Paul Menagecc31edc2008-10-18 20:28:04 -07001320}
Paul Menagec6d57f32009-09-23 15:56:19 -07001321
Paul Menageddbcc7e2007-10-18 23:39:30 -07001322static void init_cgroup_root(struct cgroupfs_root *root)
1323{
Paul Menagebd89aab2007-10-18 23:40:44 -07001324 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001325 INIT_LIST_HEAD(&root->subsys_list);
1326 INIT_LIST_HEAD(&root->root_list);
1327 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -07001328 cgrp->root = root;
1329 cgrp->top_cgroup = cgrp;
Paul Menagecc31edc2008-10-18 20:28:04 -07001330 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001331}
1332
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001333static bool init_root_id(struct cgroupfs_root *root)
1334{
1335 int ret = 0;
1336
1337 do {
1338 if (!ida_pre_get(&hierarchy_ida, GFP_KERNEL))
1339 return false;
1340 spin_lock(&hierarchy_id_lock);
1341 /* Try to allocate the next unused ID */
1342 ret = ida_get_new_above(&hierarchy_ida, next_hierarchy_id,
1343 &root->hierarchy_id);
1344 if (ret == -ENOSPC)
1345 /* Try again starting from 0 */
1346 ret = ida_get_new(&hierarchy_ida, &root->hierarchy_id);
1347 if (!ret) {
1348 next_hierarchy_id = root->hierarchy_id + 1;
1349 } else if (ret != -EAGAIN) {
1350 /* Can only get here if the 31-bit IDR is full ... */
1351 BUG_ON(ret);
1352 }
1353 spin_unlock(&hierarchy_id_lock);
1354 } while (ret);
1355 return true;
1356}
1357
Paul Menageddbcc7e2007-10-18 23:39:30 -07001358static int cgroup_test_super(struct super_block *sb, void *data)
1359{
Paul Menagec6d57f32009-09-23 15:56:19 -07001360 struct cgroup_sb_opts *opts = data;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001361 struct cgroupfs_root *root = sb->s_fs_info;
1362
Paul Menagec6d57f32009-09-23 15:56:19 -07001363 /* If we asked for a name then it must match */
1364 if (opts->name && strcmp(opts->name, root->name))
1365 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001366
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001367 /*
1368 * If we asked for subsystems (or explicitly for no
1369 * subsystems) then they must match
1370 */
1371 if ((opts->subsys_bits || opts->none)
1372 && (opts->subsys_bits != root->subsys_bits))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001373 return 0;
1374
1375 return 1;
1376}
1377
Paul Menagec6d57f32009-09-23 15:56:19 -07001378static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1379{
1380 struct cgroupfs_root *root;
1381
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001382 if (!opts->subsys_bits && !opts->none)
Paul Menagec6d57f32009-09-23 15:56:19 -07001383 return NULL;
1384
1385 root = kzalloc(sizeof(*root), GFP_KERNEL);
1386 if (!root)
1387 return ERR_PTR(-ENOMEM);
1388
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001389 if (!init_root_id(root)) {
1390 kfree(root);
1391 return ERR_PTR(-ENOMEM);
1392 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001393 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001394
Paul Menagec6d57f32009-09-23 15:56:19 -07001395 root->subsys_bits = opts->subsys_bits;
1396 root->flags = opts->flags;
1397 if (opts->release_agent)
1398 strcpy(root->release_agent_path, opts->release_agent);
1399 if (opts->name)
1400 strcpy(root->name, opts->name);
Daniel Lezcano97978e62010-10-27 15:33:35 -07001401 if (opts->clone_children)
1402 set_bit(CGRP_CLONE_CHILDREN, &root->top_cgroup.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001403 return root;
1404}
1405
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001406static void cgroup_drop_root(struct cgroupfs_root *root)
1407{
1408 if (!root)
1409 return;
1410
1411 BUG_ON(!root->hierarchy_id);
1412 spin_lock(&hierarchy_id_lock);
1413 ida_remove(&hierarchy_ida, root->hierarchy_id);
1414 spin_unlock(&hierarchy_id_lock);
1415 kfree(root);
1416}
1417
Paul Menageddbcc7e2007-10-18 23:39:30 -07001418static int cgroup_set_super(struct super_block *sb, void *data)
1419{
1420 int ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001421 struct cgroup_sb_opts *opts = data;
1422
1423 /* If we don't have a new root, we can't set up a new sb */
1424 if (!opts->new_root)
1425 return -EINVAL;
1426
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001427 BUG_ON(!opts->subsys_bits && !opts->none);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001428
1429 ret = set_anon_super(sb, NULL);
1430 if (ret)
1431 return ret;
1432
Paul Menagec6d57f32009-09-23 15:56:19 -07001433 sb->s_fs_info = opts->new_root;
1434 opts->new_root->sb = sb;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001435
1436 sb->s_blocksize = PAGE_CACHE_SIZE;
1437 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1438 sb->s_magic = CGROUP_SUPER_MAGIC;
1439 sb->s_op = &cgroup_ops;
1440
1441 return 0;
1442}
1443
1444static int cgroup_get_rootdir(struct super_block *sb)
1445{
Al Viro0df6a632010-12-21 13:29:29 -05001446 static const struct dentry_operations cgroup_dops = {
1447 .d_iput = cgroup_diput,
Al Viroc72a04e2011-01-14 05:31:45 +00001448 .d_delete = cgroup_delete,
Al Viro0df6a632010-12-21 13:29:29 -05001449 };
1450
Paul Menageddbcc7e2007-10-18 23:39:30 -07001451 struct inode *inode =
1452 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
1453 struct dentry *dentry;
1454
1455 if (!inode)
1456 return -ENOMEM;
1457
Paul Menageddbcc7e2007-10-18 23:39:30 -07001458 inode->i_fop = &simple_dir_operations;
1459 inode->i_op = &cgroup_dir_inode_operations;
1460 /* directories start off with i_nlink == 2 (for "." entry) */
1461 inc_nlink(inode);
1462 dentry = d_alloc_root(inode);
1463 if (!dentry) {
1464 iput(inode);
1465 return -ENOMEM;
1466 }
1467 sb->s_root = dentry;
Al Viro0df6a632010-12-21 13:29:29 -05001468 /* for everything else we want ->d_op set */
1469 sb->s_d_op = &cgroup_dops;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001470 return 0;
1471}
1472
Al Virof7e83572010-07-26 13:23:11 +04001473static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001474 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001475 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001476{
1477 struct cgroup_sb_opts opts;
Paul Menagec6d57f32009-09-23 15:56:19 -07001478 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001479 int ret = 0;
1480 struct super_block *sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001481 struct cgroupfs_root *new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001482
1483 /* First find the desired set of subsystems */
Ben Blumaae8aab2010-03-10 15:22:07 -08001484 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001485 ret = parse_cgroupfs_options(data, &opts);
Ben Blumaae8aab2010-03-10 15:22:07 -08001486 mutex_unlock(&cgroup_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001487 if (ret)
1488 goto out_err;
1489
1490 /*
1491 * Allocate a new cgroup root. We may not need it if we're
1492 * reusing an existing hierarchy.
1493 */
1494 new_root = cgroup_root_from_opts(&opts);
1495 if (IS_ERR(new_root)) {
1496 ret = PTR_ERR(new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001497 goto drop_modules;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001498 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001499 opts.new_root = new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001500
Paul Menagec6d57f32009-09-23 15:56:19 -07001501 /* Locate an existing or new sb for this hierarchy */
1502 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001503 if (IS_ERR(sb)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001504 ret = PTR_ERR(sb);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001505 cgroup_drop_root(opts.new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001506 goto drop_modules;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001507 }
1508
Paul Menagec6d57f32009-09-23 15:56:19 -07001509 root = sb->s_fs_info;
1510 BUG_ON(!root);
1511 if (root == opts.new_root) {
1512 /* We used the new root structure, so this is a new hierarchy */
1513 struct list_head tmp_cg_links;
Li Zefanc12f65d2009-01-07 18:07:42 -08001514 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menage817929e2007-10-18 23:39:36 -07001515 struct inode *inode;
Paul Menagec6d57f32009-09-23 15:56:19 -07001516 struct cgroupfs_root *existing_root;
Li Zefan28fd5df2008-04-29 01:00:13 -07001517 int i;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001518
1519 BUG_ON(sb->s_root != NULL);
1520
1521 ret = cgroup_get_rootdir(sb);
1522 if (ret)
1523 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001524 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001525
Paul Menage817929e2007-10-18 23:39:36 -07001526 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001527 mutex_lock(&cgroup_mutex);
1528
Paul Menagec6d57f32009-09-23 15:56:19 -07001529 if (strlen(root->name)) {
1530 /* Check for name clashes with existing mounts */
1531 for_each_active_root(existing_root) {
1532 if (!strcmp(existing_root->name, root->name)) {
1533 ret = -EBUSY;
1534 mutex_unlock(&cgroup_mutex);
1535 mutex_unlock(&inode->i_mutex);
1536 goto drop_new_super;
1537 }
1538 }
1539 }
1540
Paul Menage817929e2007-10-18 23:39:36 -07001541 /*
1542 * We're accessing css_set_count without locking
1543 * css_set_lock here, but that's OK - it can only be
1544 * increased by someone holding cgroup_lock, and
1545 * that's us. The worst that can happen is that we
1546 * have some link structures left over
1547 */
1548 ret = allocate_cg_links(css_set_count, &tmp_cg_links);
1549 if (ret) {
1550 mutex_unlock(&cgroup_mutex);
1551 mutex_unlock(&inode->i_mutex);
1552 goto drop_new_super;
1553 }
1554
Paul Menageddbcc7e2007-10-18 23:39:30 -07001555 ret = rebind_subsystems(root, root->subsys_bits);
1556 if (ret == -EBUSY) {
1557 mutex_unlock(&cgroup_mutex);
Paul Menage817929e2007-10-18 23:39:36 -07001558 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001559 free_cg_links(&tmp_cg_links);
1560 goto drop_new_super;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001561 }
Ben Blumcf5d5942010-03-10 15:22:09 -08001562 /*
1563 * There must be no failure case after here, since rebinding
1564 * takes care of subsystems' refcounts, which are explicitly
1565 * dropped in the failure exit path.
1566 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001567
1568 /* EBUSY should be the only error here */
1569 BUG_ON(ret);
1570
1571 list_add(&root->root_list, &roots);
Paul Menage817929e2007-10-18 23:39:36 -07001572 root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001573
Li Zefanc12f65d2009-01-07 18:07:42 -08001574 sb->s_root->d_fsdata = root_cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001575 root->top_cgroup.dentry = sb->s_root;
1576
Paul Menage817929e2007-10-18 23:39:36 -07001577 /* Link the top cgroup in this hierarchy into all
1578 * the css_set objects */
1579 write_lock(&css_set_lock);
Li Zefan28fd5df2008-04-29 01:00:13 -07001580 for (i = 0; i < CSS_SET_TABLE_SIZE; i++) {
1581 struct hlist_head *hhead = &css_set_table[i];
1582 struct hlist_node *node;
Paul Menage817929e2007-10-18 23:39:36 -07001583 struct css_set *cg;
Li Zefan28fd5df2008-04-29 01:00:13 -07001584
Li Zefanc12f65d2009-01-07 18:07:42 -08001585 hlist_for_each_entry(cg, node, hhead, hlist)
1586 link_css_set(&tmp_cg_links, cg, root_cgrp);
Li Zefan28fd5df2008-04-29 01:00:13 -07001587 }
Paul Menage817929e2007-10-18 23:39:36 -07001588 write_unlock(&css_set_lock);
1589
1590 free_cg_links(&tmp_cg_links);
1591
Li Zefanc12f65d2009-01-07 18:07:42 -08001592 BUG_ON(!list_empty(&root_cgrp->sibling));
1593 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001594 BUG_ON(root->number_of_cgroups != 1);
1595
Li Zefanc12f65d2009-01-07 18:07:42 -08001596 cgroup_populate_dir(root_cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001597 mutex_unlock(&cgroup_mutex);
Xiaotian Feng34f77a92009-09-23 15:56:18 -07001598 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001599 } else {
1600 /*
1601 * We re-used an existing hierarchy - the new root (if
1602 * any) is not needed
1603 */
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001604 cgroup_drop_root(opts.new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001605 /* no subsys rebinding, so refcounts don't change */
1606 drop_parsed_module_refcounts(opts.subsys_bits);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001607 }
1608
Paul Menagec6d57f32009-09-23 15:56:19 -07001609 kfree(opts.release_agent);
1610 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001611 return dget(sb->s_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001612
1613 drop_new_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04001614 deactivate_locked_super(sb);
Ben Blumcf5d5942010-03-10 15:22:09 -08001615 drop_modules:
1616 drop_parsed_module_refcounts(opts.subsys_bits);
Paul Menagec6d57f32009-09-23 15:56:19 -07001617 out_err:
1618 kfree(opts.release_agent);
1619 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001620 return ERR_PTR(ret);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001621}
1622
1623static void cgroup_kill_sb(struct super_block *sb) {
1624 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001625 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001626 int ret;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001627 struct cg_cgroup_link *link;
1628 struct cg_cgroup_link *saved_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001629
1630 BUG_ON(!root);
1631
1632 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001633 BUG_ON(!list_empty(&cgrp->children));
1634 BUG_ON(!list_empty(&cgrp->sibling));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001635
1636 mutex_lock(&cgroup_mutex);
1637
1638 /* Rebind all subsystems back to the default hierarchy */
1639 ret = rebind_subsystems(root, 0);
1640 /* Shouldn't be able to fail ... */
1641 BUG_ON(ret);
1642
Paul Menage817929e2007-10-18 23:39:36 -07001643 /*
1644 * Release all the links from css_sets to this hierarchy's
1645 * root cgroup
1646 */
1647 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001648
1649 list_for_each_entry_safe(link, saved_link, &cgrp->css_sets,
1650 cgrp_link_list) {
Paul Menage817929e2007-10-18 23:39:36 -07001651 list_del(&link->cg_link_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07001652 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07001653 kfree(link);
1654 }
1655 write_unlock(&css_set_lock);
1656
Paul Menage839ec542009-01-29 14:25:22 -08001657 if (!list_empty(&root->root_list)) {
1658 list_del(&root->root_list);
1659 root_count--;
1660 }
Li Zefane5f6a862009-01-07 18:07:41 -08001661
Paul Menageddbcc7e2007-10-18 23:39:30 -07001662 mutex_unlock(&cgroup_mutex);
1663
Paul Menageddbcc7e2007-10-18 23:39:30 -07001664 kill_litter_super(sb);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001665 cgroup_drop_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001666}
1667
1668static struct file_system_type cgroup_fs_type = {
1669 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001670 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001671 .kill_sb = cgroup_kill_sb,
1672};
1673
Greg KH676db4a2010-08-05 13:53:35 -07001674static struct kobject *cgroup_kobj;
1675
Paul Menagebd89aab2007-10-18 23:40:44 -07001676static inline struct cgroup *__d_cgrp(struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001677{
1678 return dentry->d_fsdata;
1679}
1680
1681static inline struct cftype *__d_cft(struct dentry *dentry)
1682{
1683 return dentry->d_fsdata;
1684}
1685
Li Zefana043e3b2008-02-23 15:24:09 -08001686/**
1687 * cgroup_path - generate the path of a cgroup
1688 * @cgrp: the cgroup in question
1689 * @buf: the buffer to write the path into
1690 * @buflen: the length of the buffer
1691 *
Paul Menagea47295e2009-01-07 18:07:44 -08001692 * Called with cgroup_mutex held or else with an RCU-protected cgroup
1693 * reference. Writes path of cgroup into buf. Returns 0 on success,
1694 * -errno on error.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001695 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001696int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001697{
1698 char *start;
Li Zefan9a9686b2010-04-22 17:29:24 +08001699 struct dentry *dentry = rcu_dereference_check(cgrp->dentry,
1700 rcu_read_lock_held() ||
1701 cgroup_lock_is_held());
Paul Menageddbcc7e2007-10-18 23:39:30 -07001702
Paul Menagea47295e2009-01-07 18:07:44 -08001703 if (!dentry || cgrp == dummytop) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001704 /*
1705 * Inactive subsystems have no dentry for their root
1706 * cgroup
1707 */
1708 strcpy(buf, "/");
1709 return 0;
1710 }
1711
1712 start = buf + buflen;
1713
1714 *--start = '\0';
1715 for (;;) {
Paul Menagea47295e2009-01-07 18:07:44 -08001716 int len = dentry->d_name.len;
Li Zefan9a9686b2010-04-22 17:29:24 +08001717
Paul Menageddbcc7e2007-10-18 23:39:30 -07001718 if ((start -= len) < buf)
1719 return -ENAMETOOLONG;
Li Zefan9a9686b2010-04-22 17:29:24 +08001720 memcpy(start, dentry->d_name.name, len);
Paul Menagebd89aab2007-10-18 23:40:44 -07001721 cgrp = cgrp->parent;
1722 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001723 break;
Li Zefan9a9686b2010-04-22 17:29:24 +08001724
1725 dentry = rcu_dereference_check(cgrp->dentry,
1726 rcu_read_lock_held() ||
1727 cgroup_lock_is_held());
Paul Menagebd89aab2007-10-18 23:40:44 -07001728 if (!cgrp->parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001729 continue;
1730 if (--start < buf)
1731 return -ENAMETOOLONG;
1732 *start = '/';
1733 }
1734 memmove(buf, start, buf + buflen - start);
1735 return 0;
1736}
Ben Blum67523c42010-03-10 15:22:11 -08001737EXPORT_SYMBOL_GPL(cgroup_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001738
Ben Blum74a11662011-05-26 16:25:20 -07001739/*
1740 * cgroup_task_migrate - move a task from one cgroup to another.
1741 *
1742 * 'guarantee' is set if the caller promises that a new css_set for the task
1743 * will already exist. If not set, this function might sleep, and can fail with
1744 * -ENOMEM. Otherwise, it can only fail with -ESRCH.
1745 */
1746static int cgroup_task_migrate(struct cgroup *cgrp, struct cgroup *oldcgrp,
1747 struct task_struct *tsk, bool guarantee)
1748{
1749 struct css_set *oldcg;
1750 struct css_set *newcg;
1751
1752 /*
1753 * get old css_set. we need to take task_lock and refcount it, because
1754 * an exiting task can change its css_set to init_css_set and drop its
1755 * old one without taking cgroup_mutex.
1756 */
1757 task_lock(tsk);
1758 oldcg = tsk->cgroups;
1759 get_css_set(oldcg);
1760 task_unlock(tsk);
1761
1762 /* locate or allocate a new css_set for this task. */
1763 if (guarantee) {
1764 /* we know the css_set we want already exists. */
1765 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT];
1766 read_lock(&css_set_lock);
1767 newcg = find_existing_css_set(oldcg, cgrp, template);
1768 BUG_ON(!newcg);
1769 get_css_set(newcg);
1770 read_unlock(&css_set_lock);
1771 } else {
1772 might_sleep();
1773 /* find_css_set will give us newcg already referenced. */
1774 newcg = find_css_set(oldcg, cgrp);
1775 if (!newcg) {
1776 put_css_set(oldcg);
1777 return -ENOMEM;
1778 }
1779 }
1780 put_css_set(oldcg);
1781
1782 /* if PF_EXITING is set, the tsk->cgroups pointer is no longer safe. */
1783 task_lock(tsk);
1784 if (tsk->flags & PF_EXITING) {
1785 task_unlock(tsk);
1786 put_css_set(newcg);
1787 return -ESRCH;
1788 }
1789 rcu_assign_pointer(tsk->cgroups, newcg);
1790 task_unlock(tsk);
1791
1792 /* Update the css_set linked lists if we're using them */
1793 write_lock(&css_set_lock);
1794 if (!list_empty(&tsk->cg_list))
1795 list_move(&tsk->cg_list, &newcg->tasks);
1796 write_unlock(&css_set_lock);
1797
1798 /*
1799 * We just gained a reference on oldcg by taking it from the task. As
1800 * trading it for newcg is protected by cgroup_mutex, we're safe to drop
1801 * it here; it will be freed under RCU.
1802 */
1803 put_css_set(oldcg);
1804
1805 set_bit(CGRP_RELEASABLE, &oldcgrp->flags);
1806 return 0;
1807}
1808
Li Zefana043e3b2008-02-23 15:24:09 -08001809/**
1810 * cgroup_attach_task - attach task 'tsk' to cgroup 'cgrp'
1811 * @cgrp: the cgroup the task is attaching to
1812 * @tsk: the task to be attached
Paul Menagebbcb81d2007-10-18 23:39:32 -07001813 *
Li Zefana043e3b2008-02-23 15:24:09 -08001814 * Call holding cgroup_mutex. May take task_lock of
1815 * the task 'tsk' during call.
Paul Menagebbcb81d2007-10-18 23:39:32 -07001816 */
Cliff Wickman956db3c2008-02-07 00:14:43 -08001817int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001818{
Ben Blum74a11662011-05-26 16:25:20 -07001819 int retval;
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001820 struct cgroup_subsys *ss, *failed_ss = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07001821 struct cgroup *oldcgrp;
Paul Menagebd89aab2007-10-18 23:40:44 -07001822 struct cgroupfs_root *root = cgrp->root;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001823
1824 /* Nothing to do if the task is already in that cgroup */
Paul Menage7717f7b2009-09-23 15:56:22 -07001825 oldcgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07001826 if (cgrp == oldcgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001827 return 0;
1828
1829 for_each_subsys(root, ss) {
1830 if (ss->can_attach) {
Ben Blumf780bdb2011-05-26 16:25:19 -07001831 retval = ss->can_attach(ss, cgrp, tsk);
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001832 if (retval) {
1833 /*
1834 * Remember on which subsystem the can_attach()
1835 * failed, so that we only call cancel_attach()
1836 * against the subsystems whose can_attach()
1837 * succeeded. (See below)
1838 */
1839 failed_ss = ss;
1840 goto out;
1841 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07001842 }
Ben Blumf780bdb2011-05-26 16:25:19 -07001843 if (ss->can_attach_task) {
1844 retval = ss->can_attach_task(cgrp, tsk);
1845 if (retval) {
1846 failed_ss = ss;
1847 goto out;
1848 }
1849 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07001850 }
1851
Ben Blum74a11662011-05-26 16:25:20 -07001852 retval = cgroup_task_migrate(cgrp, oldcgrp, tsk, false);
1853 if (retval)
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001854 goto out;
Paul Menage817929e2007-10-18 23:39:36 -07001855
Paul Menagebbcb81d2007-10-18 23:39:32 -07001856 for_each_subsys(root, ss) {
Ben Blumf780bdb2011-05-26 16:25:19 -07001857 if (ss->pre_attach)
1858 ss->pre_attach(cgrp);
1859 if (ss->attach_task)
1860 ss->attach_task(cgrp, tsk);
Paul Jacksone18f6312008-02-07 00:13:44 -08001861 if (ss->attach)
Ben Blumf780bdb2011-05-26 16:25:19 -07001862 ss->attach(ss, cgrp, oldcgrp, tsk);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001863 }
Ben Blum74a11662011-05-26 16:25:20 -07001864
Paul Menagebbcb81d2007-10-18 23:39:32 -07001865 synchronize_rcu();
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07001866
1867 /*
1868 * wake up rmdir() waiter. the rmdir should fail since the cgroup
1869 * is no longer empty.
1870 */
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07001871 cgroup_wakeup_rmdir_waiter(cgrp);
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001872out:
1873 if (retval) {
1874 for_each_subsys(root, ss) {
1875 if (ss == failed_ss)
1876 /*
1877 * This subsystem was the one that failed the
1878 * can_attach() check earlier, so we don't need
1879 * to call cancel_attach() against it or any
1880 * remaining subsystems.
1881 */
1882 break;
1883 if (ss->cancel_attach)
Ben Blumf780bdb2011-05-26 16:25:19 -07001884 ss->cancel_attach(ss, cgrp, tsk);
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001885 }
1886 }
1887 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001888}
1889
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02001890/**
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -07001891 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
1892 * @from: attach to all cgroups of a given task
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02001893 * @tsk: the task to be attached
1894 */
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -07001895int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02001896{
1897 struct cgroupfs_root *root;
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02001898 int retval = 0;
1899
1900 cgroup_lock();
1901 for_each_active_root(root) {
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -07001902 struct cgroup *from_cg = task_cgroup_from_root(from, root);
1903
1904 retval = cgroup_attach_task(from_cg, tsk);
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02001905 if (retval)
1906 break;
1907 }
1908 cgroup_unlock();
1909
1910 return retval;
1911}
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -07001912EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02001913
Paul Menagebbcb81d2007-10-18 23:39:32 -07001914/*
Ben Blum74a11662011-05-26 16:25:20 -07001915 * cgroup_attach_proc works in two stages, the first of which prefetches all
1916 * new css_sets needed (to make sure we have enough memory before committing
1917 * to the move) and stores them in a list of entries of the following type.
1918 * TODO: possible optimization: use css_set->rcu_head for chaining instead
Paul Menagebbcb81d2007-10-18 23:39:32 -07001919 */
Ben Blum74a11662011-05-26 16:25:20 -07001920struct cg_list_entry {
1921 struct css_set *cg;
1922 struct list_head links;
1923};
1924
1925static bool css_set_check_fetched(struct cgroup *cgrp,
1926 struct task_struct *tsk, struct css_set *cg,
1927 struct list_head *newcg_list)
1928{
1929 struct css_set *newcg;
1930 struct cg_list_entry *cg_entry;
1931 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT];
1932
1933 read_lock(&css_set_lock);
1934 newcg = find_existing_css_set(cg, cgrp, template);
1935 if (newcg)
1936 get_css_set(newcg);
1937 read_unlock(&css_set_lock);
1938
1939 /* doesn't exist at all? */
1940 if (!newcg)
1941 return false;
1942 /* see if it's already in the list */
1943 list_for_each_entry(cg_entry, newcg_list, links) {
1944 if (cg_entry->cg == newcg) {
1945 put_css_set(newcg);
1946 return true;
1947 }
1948 }
1949
1950 /* not found */
1951 put_css_set(newcg);
1952 return false;
1953}
1954
1955/*
1956 * Find the new css_set and store it in the list in preparation for moving the
1957 * given task to the given cgroup. Returns 0 or -ENOMEM.
1958 */
1959static int css_set_prefetch(struct cgroup *cgrp, struct css_set *cg,
1960 struct list_head *newcg_list)
1961{
1962 struct css_set *newcg;
1963 struct cg_list_entry *cg_entry;
1964
1965 /* ensure a new css_set will exist for this thread */
1966 newcg = find_css_set(cg, cgrp);
1967 if (!newcg)
1968 return -ENOMEM;
1969 /* add it to the list */
1970 cg_entry = kmalloc(sizeof(struct cg_list_entry), GFP_KERNEL);
1971 if (!cg_entry) {
1972 put_css_set(newcg);
1973 return -ENOMEM;
1974 }
1975 cg_entry->cg = newcg;
1976 list_add(&cg_entry->links, newcg_list);
1977 return 0;
1978}
1979
1980/**
1981 * cgroup_attach_proc - attach all threads in a threadgroup to a cgroup
1982 * @cgrp: the cgroup to attach to
1983 * @leader: the threadgroup leader task_struct of the group to be attached
1984 *
1985 * Call holding cgroup_mutex and the threadgroup_fork_lock of the leader. Will
1986 * take task_lock of each thread in leader's threadgroup individually in turn.
1987 */
1988int cgroup_attach_proc(struct cgroup *cgrp, struct task_struct *leader)
1989{
1990 int retval, i, group_size;
1991 struct cgroup_subsys *ss, *failed_ss = NULL;
1992 bool cancel_failed_ss = false;
1993 /* guaranteed to be initialized later, but the compiler needs this */
1994 struct cgroup *oldcgrp = NULL;
1995 struct css_set *oldcg;
1996 struct cgroupfs_root *root = cgrp->root;
1997 /* threadgroup list cursor and array */
1998 struct task_struct *tsk;
Ben Blumd8466872011-05-26 16:25:21 -07001999 struct flex_array *group;
Ben Blum74a11662011-05-26 16:25:20 -07002000 /*
2001 * we need to make sure we have css_sets for all the tasks we're
2002 * going to move -before- we actually start moving them, so that in
2003 * case we get an ENOMEM we can bail out before making any changes.
2004 */
2005 struct list_head newcg_list;
2006 struct cg_list_entry *cg_entry, *temp_nobe;
2007
2008 /*
2009 * step 0: in order to do expensive, possibly blocking operations for
2010 * every thread, we cannot iterate the thread group list, since it needs
2011 * rcu or tasklist locked. instead, build an array of all threads in the
2012 * group - threadgroup_fork_lock prevents new threads from appearing,
2013 * and if threads exit, this will just be an over-estimate.
2014 */
2015 group_size = get_nr_threads(leader);
Ben Blumd8466872011-05-26 16:25:21 -07002016 /* flex_array supports very large thread-groups better than kmalloc. */
2017 group = flex_array_alloc(sizeof(struct task_struct *), group_size,
2018 GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07002019 if (!group)
2020 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07002021 /* pre-allocate to guarantee space while iterating in rcu read-side. */
2022 retval = flex_array_prealloc(group, 0, group_size - 1, GFP_KERNEL);
2023 if (retval)
2024 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07002025
2026 /* prevent changes to the threadgroup list while we take a snapshot. */
2027 rcu_read_lock();
2028 if (!thread_group_leader(leader)) {
2029 /*
2030 * a race with de_thread from another thread's exec() may strip
2031 * us of our leadership, making while_each_thread unsafe to use
2032 * on this task. if this happens, there is no choice but to
2033 * throw this task away and try again (from cgroup_procs_write);
2034 * this is "double-double-toil-and-trouble-check locking".
2035 */
2036 rcu_read_unlock();
2037 retval = -EAGAIN;
2038 goto out_free_group_list;
2039 }
2040 /* take a reference on each task in the group to go in the array. */
2041 tsk = leader;
2042 i = 0;
2043 do {
2044 /* as per above, nr_threads may decrease, but not increase. */
2045 BUG_ON(i >= group_size);
2046 get_task_struct(tsk);
Ben Blumd8466872011-05-26 16:25:21 -07002047 /*
2048 * saying GFP_ATOMIC has no effect here because we did prealloc
2049 * earlier, but it's good form to communicate our expectations.
2050 */
2051 retval = flex_array_put_ptr(group, i, tsk, GFP_ATOMIC);
2052 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07002053 i++;
2054 } while_each_thread(leader, tsk);
2055 /* remember the number of threads in the array for later. */
2056 group_size = i;
2057 rcu_read_unlock();
2058
2059 /*
2060 * step 1: check that we can legitimately attach to the cgroup.
2061 */
2062 for_each_subsys(root, ss) {
2063 if (ss->can_attach) {
2064 retval = ss->can_attach(ss, cgrp, leader);
2065 if (retval) {
2066 failed_ss = ss;
2067 goto out_cancel_attach;
2068 }
2069 }
2070 /* a callback to be run on every thread in the threadgroup. */
2071 if (ss->can_attach_task) {
2072 /* run on each task in the threadgroup. */
2073 for (i = 0; i < group_size; i++) {
Ben Blumd8466872011-05-26 16:25:21 -07002074 tsk = flex_array_get_ptr(group, i);
2075 retval = ss->can_attach_task(cgrp, tsk);
Ben Blum74a11662011-05-26 16:25:20 -07002076 if (retval) {
2077 failed_ss = ss;
2078 cancel_failed_ss = true;
2079 goto out_cancel_attach;
2080 }
2081 }
2082 }
2083 }
2084
2085 /*
2086 * step 2: make sure css_sets exist for all threads to be migrated.
2087 * we use find_css_set, which allocates a new one if necessary.
2088 */
2089 INIT_LIST_HEAD(&newcg_list);
2090 for (i = 0; i < group_size; i++) {
Ben Blumd8466872011-05-26 16:25:21 -07002091 tsk = flex_array_get_ptr(group, i);
Ben Blum74a11662011-05-26 16:25:20 -07002092 /* nothing to do if this task is already in the cgroup */
2093 oldcgrp = task_cgroup_from_root(tsk, root);
2094 if (cgrp == oldcgrp)
2095 continue;
2096 /* get old css_set pointer */
2097 task_lock(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07002098 oldcg = tsk->cgroups;
2099 get_css_set(oldcg);
2100 task_unlock(tsk);
2101 /* see if the new one for us is already in the list? */
2102 if (css_set_check_fetched(cgrp, tsk, oldcg, &newcg_list)) {
2103 /* was already there, nothing to do. */
2104 put_css_set(oldcg);
2105 } else {
2106 /* we don't already have it. get new one. */
2107 retval = css_set_prefetch(cgrp, oldcg, &newcg_list);
2108 put_css_set(oldcg);
2109 if (retval)
2110 goto out_list_teardown;
2111 }
2112 }
2113
2114 /*
2115 * step 3: now that we're guaranteed success wrt the css_sets, proceed
2116 * to move all tasks to the new cgroup, calling ss->attach_task for each
2117 * one along the way. there are no failure cases after here, so this is
2118 * the commit point.
2119 */
2120 for_each_subsys(root, ss) {
2121 if (ss->pre_attach)
2122 ss->pre_attach(cgrp);
2123 }
2124 for (i = 0; i < group_size; i++) {
Ben Blumd8466872011-05-26 16:25:21 -07002125 tsk = flex_array_get_ptr(group, i);
Ben Blum74a11662011-05-26 16:25:20 -07002126 /* leave current thread as it is if it's already there */
2127 oldcgrp = task_cgroup_from_root(tsk, root);
2128 if (cgrp == oldcgrp)
2129 continue;
2130 /* attach each task to each subsystem */
2131 for_each_subsys(root, ss) {
2132 if (ss->attach_task)
2133 ss->attach_task(cgrp, tsk);
2134 }
2135 /* if the thread is PF_EXITING, it can just get skipped. */
2136 retval = cgroup_task_migrate(cgrp, oldcgrp, tsk, true);
2137 BUG_ON(retval != 0 && retval != -ESRCH);
2138 }
2139 /* nothing is sensitive to fork() after this point. */
2140
2141 /*
2142 * step 4: do expensive, non-thread-specific subsystem callbacks.
2143 * TODO: if ever a subsystem needs to know the oldcgrp for each task
2144 * being moved, this call will need to be reworked to communicate that.
2145 */
2146 for_each_subsys(root, ss) {
2147 if (ss->attach)
2148 ss->attach(ss, cgrp, oldcgrp, leader);
2149 }
2150
2151 /*
2152 * step 5: success! and cleanup
2153 */
2154 synchronize_rcu();
2155 cgroup_wakeup_rmdir_waiter(cgrp);
2156 retval = 0;
2157out_list_teardown:
2158 /* clean up the list of prefetched css_sets. */
2159 list_for_each_entry_safe(cg_entry, temp_nobe, &newcg_list, links) {
2160 list_del(&cg_entry->links);
2161 put_css_set(cg_entry->cg);
2162 kfree(cg_entry);
2163 }
2164out_cancel_attach:
2165 /* same deal as in cgroup_attach_task */
2166 if (retval) {
2167 for_each_subsys(root, ss) {
2168 if (ss == failed_ss) {
2169 if (cancel_failed_ss && ss->cancel_attach)
2170 ss->cancel_attach(ss, cgrp, leader);
2171 break;
2172 }
2173 if (ss->cancel_attach)
2174 ss->cancel_attach(ss, cgrp, leader);
2175 }
2176 }
2177 /* clean up the array of referenced threads in the group. */
Ben Blumd8466872011-05-26 16:25:21 -07002178 for (i = 0; i < group_size; i++) {
2179 tsk = flex_array_get_ptr(group, i);
2180 put_task_struct(tsk);
2181 }
Ben Blum74a11662011-05-26 16:25:20 -07002182out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07002183 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07002184 return retval;
2185}
2186
2187/*
2188 * Find the task_struct of the task to attach by vpid and pass it along to the
2189 * function to attach either it or all tasks in its threadgroup. Will take
2190 * cgroup_mutex; may take task_lock of task.
2191 */
2192static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002193{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002194 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002195 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002196 int ret;
2197
Ben Blum74a11662011-05-26 16:25:20 -07002198 if (!cgroup_lock_live_group(cgrp))
2199 return -ENODEV;
2200
Paul Menagebbcb81d2007-10-18 23:39:32 -07002201 if (pid) {
2202 rcu_read_lock();
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002203 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002204 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002205 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07002206 cgroup_unlock();
2207 return -ESRCH;
2208 }
2209 if (threadgroup) {
2210 /*
2211 * RCU protects this access, since tsk was found in the
2212 * tid map. a race with de_thread may cause group_leader
2213 * to stop being the leader, but cgroup_attach_proc will
2214 * detect it later.
2215 */
2216 tsk = tsk->group_leader;
2217 } else if (tsk->flags & PF_EXITING) {
2218 /* optimization for the single-task-only case */
2219 rcu_read_unlock();
2220 cgroup_unlock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002221 return -ESRCH;
2222 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07002223
Ben Blum74a11662011-05-26 16:25:20 -07002224 /*
2225 * even if we're attaching all tasks in the thread group, we
2226 * only need to check permissions on one of them.
2227 */
David Howellsc69e8d92008-11-14 10:39:19 +11002228 tcred = __task_cred(tsk);
2229 if (cred->euid &&
2230 cred->euid != tcred->uid &&
2231 cred->euid != tcred->suid) {
2232 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07002233 cgroup_unlock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002234 return -EACCES;
2235 }
David Howellsc69e8d92008-11-14 10:39:19 +11002236 get_task_struct(tsk);
2237 rcu_read_unlock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002238 } else {
Ben Blum74a11662011-05-26 16:25:20 -07002239 if (threadgroup)
2240 tsk = current->group_leader;
2241 else
2242 tsk = current;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002243 get_task_struct(tsk);
2244 }
2245
Ben Blum74a11662011-05-26 16:25:20 -07002246 if (threadgroup) {
2247 threadgroup_fork_write_lock(tsk);
2248 ret = cgroup_attach_proc(cgrp, tsk);
2249 threadgroup_fork_write_unlock(tsk);
2250 } else {
2251 ret = cgroup_attach_task(cgrp, tsk);
2252 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07002253 put_task_struct(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07002254 cgroup_unlock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002255 return ret;
2256}
2257
Paul Menageaf351022008-07-25 01:47:01 -07002258static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid)
2259{
Ben Blum74a11662011-05-26 16:25:20 -07002260 return attach_task_by_pid(cgrp, pid, false);
2261}
2262
2263static int cgroup_procs_write(struct cgroup *cgrp, struct cftype *cft, u64 tgid)
2264{
Paul Menageaf351022008-07-25 01:47:01 -07002265 int ret;
Ben Blum74a11662011-05-26 16:25:20 -07002266 do {
2267 /*
2268 * attach_proc fails with -EAGAIN if threadgroup leadership
2269 * changes in the middle of the operation, in which case we need
2270 * to find the task_struct for the new leader and start over.
2271 */
2272 ret = attach_task_by_pid(cgrp, tgid, true);
2273 } while (ret == -EAGAIN);
Paul Menageaf351022008-07-25 01:47:01 -07002274 return ret;
2275}
2276
Paul Menagee788e062008-07-25 01:46:59 -07002277/**
2278 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
2279 * @cgrp: the cgroup to be checked for liveness
2280 *
Paul Menage84eea842008-07-25 01:47:00 -07002281 * On success, returns true; the lock should be later released with
2282 * cgroup_unlock(). On failure returns false with no lock held.
Paul Menagee788e062008-07-25 01:46:59 -07002283 */
Paul Menage84eea842008-07-25 01:47:00 -07002284bool cgroup_lock_live_group(struct cgroup *cgrp)
Paul Menagee788e062008-07-25 01:46:59 -07002285{
2286 mutex_lock(&cgroup_mutex);
2287 if (cgroup_is_removed(cgrp)) {
2288 mutex_unlock(&cgroup_mutex);
2289 return false;
2290 }
2291 return true;
2292}
Ben Blum67523c42010-03-10 15:22:11 -08002293EXPORT_SYMBOL_GPL(cgroup_lock_live_group);
Paul Menagee788e062008-07-25 01:46:59 -07002294
2295static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
2296 const char *buffer)
2297{
2298 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
Evgeny Kuznetsovf4a25892010-10-27 15:33:37 -07002299 if (strlen(buffer) >= PATH_MAX)
2300 return -EINVAL;
Paul Menagee788e062008-07-25 01:46:59 -07002301 if (!cgroup_lock_live_group(cgrp))
2302 return -ENODEV;
2303 strcpy(cgrp->root->release_agent_path, buffer);
Paul Menage84eea842008-07-25 01:47:00 -07002304 cgroup_unlock();
Paul Menagee788e062008-07-25 01:46:59 -07002305 return 0;
2306}
2307
2308static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
2309 struct seq_file *seq)
2310{
2311 if (!cgroup_lock_live_group(cgrp))
2312 return -ENODEV;
2313 seq_puts(seq, cgrp->root->release_agent_path);
2314 seq_putc(seq, '\n');
Paul Menage84eea842008-07-25 01:47:00 -07002315 cgroup_unlock();
Paul Menagee788e062008-07-25 01:46:59 -07002316 return 0;
2317}
2318
Paul Menage84eea842008-07-25 01:47:00 -07002319/* A buffer size big enough for numbers or short strings */
2320#define CGROUP_LOCAL_BUFFER_SIZE 64
2321
Paul Menagee73d2c62008-04-29 01:00:06 -07002322static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
Paul Menagef4c753b2008-04-29 00:59:56 -07002323 struct file *file,
2324 const char __user *userbuf,
2325 size_t nbytes, loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07002326{
Paul Menage84eea842008-07-25 01:47:00 -07002327 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menage355e0c42007-10-18 23:39:33 -07002328 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07002329 char *end;
2330
2331 if (!nbytes)
2332 return -EINVAL;
2333 if (nbytes >= sizeof(buffer))
2334 return -E2BIG;
2335 if (copy_from_user(buffer, userbuf, nbytes))
2336 return -EFAULT;
2337
2338 buffer[nbytes] = 0; /* nul-terminate */
Paul Menagee73d2c62008-04-29 01:00:06 -07002339 if (cft->write_u64) {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002340 u64 val = simple_strtoull(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002341 if (*end)
2342 return -EINVAL;
2343 retval = cft->write_u64(cgrp, cft, val);
2344 } else {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002345 s64 val = simple_strtoll(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002346 if (*end)
2347 return -EINVAL;
2348 retval = cft->write_s64(cgrp, cft, val);
2349 }
Paul Menage355e0c42007-10-18 23:39:33 -07002350 if (!retval)
2351 retval = nbytes;
2352 return retval;
2353}
2354
Paul Menagedb3b1492008-07-25 01:46:58 -07002355static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
2356 struct file *file,
2357 const char __user *userbuf,
2358 size_t nbytes, loff_t *unused_ppos)
2359{
Paul Menage84eea842008-07-25 01:47:00 -07002360 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagedb3b1492008-07-25 01:46:58 -07002361 int retval = 0;
2362 size_t max_bytes = cft->max_write_len;
2363 char *buffer = local_buffer;
2364
2365 if (!max_bytes)
2366 max_bytes = sizeof(local_buffer) - 1;
2367 if (nbytes >= max_bytes)
2368 return -E2BIG;
2369 /* Allocate a dynamic buffer if we need one */
2370 if (nbytes >= sizeof(local_buffer)) {
2371 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
2372 if (buffer == NULL)
2373 return -ENOMEM;
2374 }
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002375 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
2376 retval = -EFAULT;
2377 goto out;
2378 }
Paul Menagedb3b1492008-07-25 01:46:58 -07002379
2380 buffer[nbytes] = 0; /* nul-terminate */
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002381 retval = cft->write_string(cgrp, cft, strstrip(buffer));
Paul Menagedb3b1492008-07-25 01:46:58 -07002382 if (!retval)
2383 retval = nbytes;
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002384out:
Paul Menagedb3b1492008-07-25 01:46:58 -07002385 if (buffer != local_buffer)
2386 kfree(buffer);
2387 return retval;
2388}
2389
Paul Menageddbcc7e2007-10-18 23:39:30 -07002390static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
2391 size_t nbytes, loff_t *ppos)
2392{
2393 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002394 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002395
Li Zefan75139b82009-01-07 18:07:33 -08002396 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002397 return -ENODEV;
Paul Menage355e0c42007-10-18 23:39:33 -07002398 if (cft->write)
Paul Menagebd89aab2007-10-18 23:40:44 -07002399 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002400 if (cft->write_u64 || cft->write_s64)
2401 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagedb3b1492008-07-25 01:46:58 -07002402 if (cft->write_string)
2403 return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002404 if (cft->trigger) {
2405 int ret = cft->trigger(cgrp, (unsigned int)cft->private);
2406 return ret ? ret : nbytes;
2407 }
Paul Menage355e0c42007-10-18 23:39:33 -07002408 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002409}
2410
Paul Menagef4c753b2008-04-29 00:59:56 -07002411static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
2412 struct file *file,
2413 char __user *buf, size_t nbytes,
2414 loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002415{
Paul Menage84eea842008-07-25 01:47:00 -07002416 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagef4c753b2008-04-29 00:59:56 -07002417 u64 val = cft->read_u64(cgrp, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002418 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
2419
2420 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2421}
2422
Paul Menagee73d2c62008-04-29 01:00:06 -07002423static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
2424 struct file *file,
2425 char __user *buf, size_t nbytes,
2426 loff_t *ppos)
2427{
Paul Menage84eea842008-07-25 01:47:00 -07002428 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagee73d2c62008-04-29 01:00:06 -07002429 s64 val = cft->read_s64(cgrp, cft);
2430 int len = sprintf(tmp, "%lld\n", (long long) val);
2431
2432 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2433}
2434
Paul Menageddbcc7e2007-10-18 23:39:30 -07002435static ssize_t cgroup_file_read(struct file *file, char __user *buf,
2436 size_t nbytes, loff_t *ppos)
2437{
2438 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002439 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002440
Li Zefan75139b82009-01-07 18:07:33 -08002441 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002442 return -ENODEV;
2443
2444 if (cft->read)
Paul Menagebd89aab2007-10-18 23:40:44 -07002445 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07002446 if (cft->read_u64)
2447 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002448 if (cft->read_s64)
2449 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002450 return -EINVAL;
2451}
2452
Paul Menage91796562008-04-29 01:00:01 -07002453/*
2454 * seqfile ops/methods for returning structured data. Currently just
2455 * supports string->u64 maps, but can be extended in future.
2456 */
2457
2458struct cgroup_seqfile_state {
2459 struct cftype *cft;
2460 struct cgroup *cgroup;
2461};
2462
2463static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
2464{
2465 struct seq_file *sf = cb->state;
2466 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
2467}
2468
2469static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2470{
2471 struct cgroup_seqfile_state *state = m->private;
2472 struct cftype *cft = state->cft;
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002473 if (cft->read_map) {
2474 struct cgroup_map_cb cb = {
2475 .fill = cgroup_map_add,
2476 .state = m,
2477 };
2478 return cft->read_map(state->cgroup, cft, &cb);
2479 }
2480 return cft->read_seq_string(state->cgroup, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07002481}
2482
Adrian Bunk96930a62008-07-25 19:46:21 -07002483static int cgroup_seqfile_release(struct inode *inode, struct file *file)
Paul Menage91796562008-04-29 01:00:01 -07002484{
2485 struct seq_file *seq = file->private_data;
2486 kfree(seq->private);
2487 return single_release(inode, file);
2488}
2489
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002490static const struct file_operations cgroup_seqfile_operations = {
Paul Menage91796562008-04-29 01:00:01 -07002491 .read = seq_read,
Paul Menagee788e062008-07-25 01:46:59 -07002492 .write = cgroup_file_write,
Paul Menage91796562008-04-29 01:00:01 -07002493 .llseek = seq_lseek,
2494 .release = cgroup_seqfile_release,
2495};
2496
Paul Menageddbcc7e2007-10-18 23:39:30 -07002497static int cgroup_file_open(struct inode *inode, struct file *file)
2498{
2499 int err;
2500 struct cftype *cft;
2501
2502 err = generic_file_open(inode, file);
2503 if (err)
2504 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002505 cft = __d_cft(file->f_dentry);
Li Zefan75139b82009-01-07 18:07:33 -08002506
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002507 if (cft->read_map || cft->read_seq_string) {
Paul Menage91796562008-04-29 01:00:01 -07002508 struct cgroup_seqfile_state *state =
2509 kzalloc(sizeof(*state), GFP_USER);
2510 if (!state)
2511 return -ENOMEM;
2512 state->cft = cft;
2513 state->cgroup = __d_cgrp(file->f_dentry->d_parent);
2514 file->f_op = &cgroup_seqfile_operations;
2515 err = single_open(file, cgroup_seqfile_show, state);
2516 if (err < 0)
2517 kfree(state);
2518 } else if (cft->open)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002519 err = cft->open(inode, file);
2520 else
2521 err = 0;
2522
2523 return err;
2524}
2525
2526static int cgroup_file_release(struct inode *inode, struct file *file)
2527{
2528 struct cftype *cft = __d_cft(file->f_dentry);
2529 if (cft->release)
2530 return cft->release(inode, file);
2531 return 0;
2532}
2533
2534/*
2535 * cgroup_rename - Only allow simple rename of directories in place.
2536 */
2537static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2538 struct inode *new_dir, struct dentry *new_dentry)
2539{
2540 if (!S_ISDIR(old_dentry->d_inode->i_mode))
2541 return -ENOTDIR;
2542 if (new_dentry->d_inode)
2543 return -EEXIST;
2544 if (old_dir != new_dir)
2545 return -EIO;
2546 return simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2547}
2548
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002549static const struct file_operations cgroup_file_operations = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002550 .read = cgroup_file_read,
2551 .write = cgroup_file_write,
2552 .llseek = generic_file_llseek,
2553 .open = cgroup_file_open,
2554 .release = cgroup_file_release,
2555};
2556
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07002557static const struct inode_operations cgroup_dir_inode_operations = {
Al Viroc72a04e2011-01-14 05:31:45 +00002558 .lookup = cgroup_lookup,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002559 .mkdir = cgroup_mkdir,
2560 .rmdir = cgroup_rmdir,
2561 .rename = cgroup_rename,
2562};
2563
Al Viroc72a04e2011-01-14 05:31:45 +00002564static struct dentry *cgroup_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
2565{
2566 if (dentry->d_name.len > NAME_MAX)
2567 return ERR_PTR(-ENAMETOOLONG);
2568 d_add(dentry, NULL);
2569 return NULL;
2570}
2571
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002572/*
2573 * Check if a file is a control file
2574 */
2575static inline struct cftype *__file_cft(struct file *file)
2576{
2577 if (file->f_dentry->d_inode->i_fop != &cgroup_file_operations)
2578 return ERR_PTR(-EINVAL);
2579 return __d_cft(file->f_dentry);
2580}
2581
Nick Piggin5adcee12011-01-07 17:49:20 +11002582static int cgroup_create_file(struct dentry *dentry, mode_t mode,
2583 struct super_block *sb)
2584{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002585 struct inode *inode;
2586
2587 if (!dentry)
2588 return -ENOENT;
2589 if (dentry->d_inode)
2590 return -EEXIST;
2591
2592 inode = cgroup_new_inode(mode, sb);
2593 if (!inode)
2594 return -ENOMEM;
2595
2596 if (S_ISDIR(mode)) {
2597 inode->i_op = &cgroup_dir_inode_operations;
2598 inode->i_fop = &simple_dir_operations;
2599
2600 /* start off with i_nlink == 2 (for "." entry) */
2601 inc_nlink(inode);
2602
2603 /* start with the directory inode held, so that we can
2604 * populate it without racing with another mkdir */
Paul Menage817929e2007-10-18 23:39:36 -07002605 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002606 } else if (S_ISREG(mode)) {
2607 inode->i_size = 0;
2608 inode->i_fop = &cgroup_file_operations;
2609 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002610 d_instantiate(dentry, inode);
2611 dget(dentry); /* Extra count - pin the dentry in core */
2612 return 0;
2613}
2614
2615/*
Li Zefana043e3b2008-02-23 15:24:09 -08002616 * cgroup_create_dir - create a directory for an object.
2617 * @cgrp: the cgroup we create the directory for. It must have a valid
2618 * ->parent field. And we are going to fill its ->dentry field.
2619 * @dentry: dentry of the new cgroup
2620 * @mode: mode to set on new directory.
Paul Menageddbcc7e2007-10-18 23:39:30 -07002621 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002622static int cgroup_create_dir(struct cgroup *cgrp, struct dentry *dentry,
Li Zefan099fca32009-04-02 16:57:29 -07002623 mode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002624{
2625 struct dentry *parent;
2626 int error = 0;
2627
Paul Menagebd89aab2007-10-18 23:40:44 -07002628 parent = cgrp->parent->dentry;
2629 error = cgroup_create_file(dentry, S_IFDIR | mode, cgrp->root->sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002630 if (!error) {
Paul Menagebd89aab2007-10-18 23:40:44 -07002631 dentry->d_fsdata = cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002632 inc_nlink(parent->d_inode);
Paul Menagea47295e2009-01-07 18:07:44 -08002633 rcu_assign_pointer(cgrp->dentry, dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002634 dget(dentry);
2635 }
2636 dput(dentry);
2637
2638 return error;
2639}
2640
Li Zefan099fca32009-04-02 16:57:29 -07002641/**
2642 * cgroup_file_mode - deduce file mode of a control file
2643 * @cft: the control file in question
2644 *
2645 * returns cft->mode if ->mode is not 0
2646 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2647 * returns S_IRUGO if it has only a read handler
2648 * returns S_IWUSR if it has only a write hander
2649 */
2650static mode_t cgroup_file_mode(const struct cftype *cft)
2651{
2652 mode_t mode = 0;
2653
2654 if (cft->mode)
2655 return cft->mode;
2656
2657 if (cft->read || cft->read_u64 || cft->read_s64 ||
2658 cft->read_map || cft->read_seq_string)
2659 mode |= S_IRUGO;
2660
2661 if (cft->write || cft->write_u64 || cft->write_s64 ||
2662 cft->write_string || cft->trigger)
2663 mode |= S_IWUSR;
2664
2665 return mode;
2666}
2667
Paul Menagebd89aab2007-10-18 23:40:44 -07002668int cgroup_add_file(struct cgroup *cgrp,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002669 struct cgroup_subsys *subsys,
2670 const struct cftype *cft)
2671{
Paul Menagebd89aab2007-10-18 23:40:44 -07002672 struct dentry *dir = cgrp->dentry;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002673 struct dentry *dentry;
2674 int error;
Li Zefan099fca32009-04-02 16:57:29 -07002675 mode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002676
2677 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Paul Menagebd89aab2007-10-18 23:40:44 -07002678 if (subsys && !test_bit(ROOT_NOPREFIX, &cgrp->root->flags)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002679 strcpy(name, subsys->name);
2680 strcat(name, ".");
2681 }
2682 strcat(name, cft->name);
2683 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
2684 dentry = lookup_one_len(name, dir, strlen(name));
2685 if (!IS_ERR(dentry)) {
Li Zefan099fca32009-04-02 16:57:29 -07002686 mode = cgroup_file_mode(cft);
2687 error = cgroup_create_file(dentry, mode | S_IFREG,
Paul Menagebd89aab2007-10-18 23:40:44 -07002688 cgrp->root->sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002689 if (!error)
2690 dentry->d_fsdata = (void *)cft;
2691 dput(dentry);
2692 } else
2693 error = PTR_ERR(dentry);
2694 return error;
2695}
Ben Blume6a11052010-03-10 15:22:09 -08002696EXPORT_SYMBOL_GPL(cgroup_add_file);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002697
Paul Menagebd89aab2007-10-18 23:40:44 -07002698int cgroup_add_files(struct cgroup *cgrp,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002699 struct cgroup_subsys *subsys,
2700 const struct cftype cft[],
2701 int count)
2702{
2703 int i, err;
2704 for (i = 0; i < count; i++) {
Paul Menagebd89aab2007-10-18 23:40:44 -07002705 err = cgroup_add_file(cgrp, subsys, &cft[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002706 if (err)
2707 return err;
2708 }
2709 return 0;
2710}
Ben Blume6a11052010-03-10 15:22:09 -08002711EXPORT_SYMBOL_GPL(cgroup_add_files);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002712
Li Zefana043e3b2008-02-23 15:24:09 -08002713/**
2714 * cgroup_task_count - count the number of tasks in a cgroup.
2715 * @cgrp: the cgroup in question
2716 *
2717 * Return the number of tasks in the cgroup.
2718 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002719int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002720{
2721 int count = 0;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07002722 struct cg_cgroup_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002723
Paul Menage817929e2007-10-18 23:39:36 -07002724 read_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07002725 list_for_each_entry(link, &cgrp->css_sets, cgrp_link_list) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07002726 count += atomic_read(&link->cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002727 }
2728 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002729 return count;
2730}
2731
2732/*
Paul Menage817929e2007-10-18 23:39:36 -07002733 * Advance a list_head iterator. The iterator should be positioned at
2734 * the start of a css_set
2735 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002736static void cgroup_advance_iter(struct cgroup *cgrp,
Paul Menage7717f7b2009-09-23 15:56:22 -07002737 struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002738{
2739 struct list_head *l = it->cg_link;
2740 struct cg_cgroup_link *link;
2741 struct css_set *cg;
2742
2743 /* Advance to the next non-empty css_set */
2744 do {
2745 l = l->next;
Paul Menagebd89aab2007-10-18 23:40:44 -07002746 if (l == &cgrp->css_sets) {
Paul Menage817929e2007-10-18 23:39:36 -07002747 it->cg_link = NULL;
2748 return;
2749 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002750 link = list_entry(l, struct cg_cgroup_link, cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07002751 cg = link->cg;
2752 } while (list_empty(&cg->tasks));
2753 it->cg_link = l;
2754 it->task = cg->tasks.next;
2755}
2756
Cliff Wickman31a7df02008-02-07 00:14:42 -08002757/*
2758 * To reduce the fork() overhead for systems that are not actually
2759 * using their cgroups capability, we don't maintain the lists running
2760 * through each css_set to its tasks until we see the list actually
2761 * used - in other words after the first call to cgroup_iter_start().
2762 *
2763 * The tasklist_lock is not held here, as do_each_thread() and
2764 * while_each_thread() are protected by RCU.
2765 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07002766static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002767{
2768 struct task_struct *p, *g;
2769 write_lock(&css_set_lock);
2770 use_task_css_set_links = 1;
2771 do_each_thread(g, p) {
2772 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08002773 /*
2774 * We should check if the process is exiting, otherwise
2775 * it will race with cgroup_exit() in that the list
2776 * entry won't be deleted though the process has exited.
2777 */
2778 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Cliff Wickman31a7df02008-02-07 00:14:42 -08002779 list_add(&p->cg_list, &p->cgroups->tasks);
2780 task_unlock(p);
2781 } while_each_thread(g, p);
2782 write_unlock(&css_set_lock);
2783}
2784
Paul Menagebd89aab2007-10-18 23:40:44 -07002785void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002786{
2787 /*
2788 * The first time anyone tries to iterate across a cgroup,
2789 * we need to enable the list linking each css_set to its
2790 * tasks, and fix up all existing tasks.
2791 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08002792 if (!use_task_css_set_links)
2793 cgroup_enable_task_cg_lists();
2794
Paul Menage817929e2007-10-18 23:39:36 -07002795 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07002796 it->cg_link = &cgrp->css_sets;
2797 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07002798}
2799
Paul Menagebd89aab2007-10-18 23:40:44 -07002800struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -07002801 struct cgroup_iter *it)
2802{
2803 struct task_struct *res;
2804 struct list_head *l = it->task;
Lai Jiangshan2019f632009-01-07 18:07:36 -08002805 struct cg_cgroup_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07002806
2807 /* If the iterator cg is NULL, we have no tasks */
2808 if (!it->cg_link)
2809 return NULL;
2810 res = list_entry(l, struct task_struct, cg_list);
2811 /* Advance iterator to find next entry */
2812 l = l->next;
Lai Jiangshan2019f632009-01-07 18:07:36 -08002813 link = list_entry(it->cg_link, struct cg_cgroup_link, cgrp_link_list);
2814 if (l == &link->cg->tasks) {
Paul Menage817929e2007-10-18 23:39:36 -07002815 /* We reached the end of this task list - move on to
2816 * the next cg_cgroup_link */
Paul Menagebd89aab2007-10-18 23:40:44 -07002817 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07002818 } else {
2819 it->task = l;
2820 }
2821 return res;
2822}
2823
Paul Menagebd89aab2007-10-18 23:40:44 -07002824void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002825{
2826 read_unlock(&css_set_lock);
2827}
2828
Cliff Wickman31a7df02008-02-07 00:14:42 -08002829static inline int started_after_time(struct task_struct *t1,
2830 struct timespec *time,
2831 struct task_struct *t2)
2832{
2833 int start_diff = timespec_compare(&t1->start_time, time);
2834 if (start_diff > 0) {
2835 return 1;
2836 } else if (start_diff < 0) {
2837 return 0;
2838 } else {
2839 /*
2840 * Arbitrarily, if two processes started at the same
2841 * time, we'll say that the lower pointer value
2842 * started first. Note that t2 may have exited by now
2843 * so this may not be a valid pointer any longer, but
2844 * that's fine - it still serves to distinguish
2845 * between two tasks started (effectively) simultaneously.
2846 */
2847 return t1 > t2;
2848 }
2849}
2850
2851/*
2852 * This function is a callback from heap_insert() and is used to order
2853 * the heap.
2854 * In this case we order the heap in descending task start time.
2855 */
2856static inline int started_after(void *p1, void *p2)
2857{
2858 struct task_struct *t1 = p1;
2859 struct task_struct *t2 = p2;
2860 return started_after_time(t1, &t2->start_time, t2);
2861}
2862
2863/**
2864 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
2865 * @scan: struct cgroup_scanner containing arguments for the scan
2866 *
2867 * Arguments include pointers to callback functions test_task() and
2868 * process_task().
2869 * Iterate through all the tasks in a cgroup, calling test_task() for each,
2870 * and if it returns true, call process_task() for it also.
2871 * The test_task pointer may be NULL, meaning always true (select all tasks).
2872 * Effectively duplicates cgroup_iter_{start,next,end}()
2873 * but does not lock css_set_lock for the call to process_task().
2874 * The struct cgroup_scanner may be embedded in any structure of the caller's
2875 * creation.
2876 * It is guaranteed that process_task() will act on every task that
2877 * is a member of the cgroup for the duration of this call. This
2878 * function may or may not call process_task() for tasks that exit
2879 * or move to a different cgroup during the call, or are forked or
2880 * move into the cgroup during the call.
2881 *
2882 * Note that test_task() may be called with locks held, and may in some
2883 * situations be called multiple times for the same task, so it should
2884 * be cheap.
2885 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
2886 * pre-allocated and will be used for heap operations (and its "gt" member will
2887 * be overwritten), else a temporary heap will be used (allocation of which
2888 * may cause this function to fail).
2889 */
2890int cgroup_scan_tasks(struct cgroup_scanner *scan)
2891{
2892 int retval, i;
2893 struct cgroup_iter it;
2894 struct task_struct *p, *dropped;
2895 /* Never dereference latest_task, since it's not refcounted */
2896 struct task_struct *latest_task = NULL;
2897 struct ptr_heap tmp_heap;
2898 struct ptr_heap *heap;
2899 struct timespec latest_time = { 0, 0 };
2900
2901 if (scan->heap) {
2902 /* The caller supplied our heap and pre-allocated its memory */
2903 heap = scan->heap;
2904 heap->gt = &started_after;
2905 } else {
2906 /* We need to allocate our own heap memory */
2907 heap = &tmp_heap;
2908 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
2909 if (retval)
2910 /* cannot allocate the heap */
2911 return retval;
2912 }
2913
2914 again:
2915 /*
2916 * Scan tasks in the cgroup, using the scanner's "test_task" callback
2917 * to determine which are of interest, and using the scanner's
2918 * "process_task" callback to process any of them that need an update.
2919 * Since we don't want to hold any locks during the task updates,
2920 * gather tasks to be processed in a heap structure.
2921 * The heap is sorted by descending task start time.
2922 * If the statically-sized heap fills up, we overflow tasks that
2923 * started later, and in future iterations only consider tasks that
2924 * started after the latest task in the previous pass. This
2925 * guarantees forward progress and that we don't miss any tasks.
2926 */
2927 heap->size = 0;
2928 cgroup_iter_start(scan->cg, &it);
2929 while ((p = cgroup_iter_next(scan->cg, &it))) {
2930 /*
2931 * Only affect tasks that qualify per the caller's callback,
2932 * if he provided one
2933 */
2934 if (scan->test_task && !scan->test_task(p, scan))
2935 continue;
2936 /*
2937 * Only process tasks that started after the last task
2938 * we processed
2939 */
2940 if (!started_after_time(p, &latest_time, latest_task))
2941 continue;
2942 dropped = heap_insert(heap, p);
2943 if (dropped == NULL) {
2944 /*
2945 * The new task was inserted; the heap wasn't
2946 * previously full
2947 */
2948 get_task_struct(p);
2949 } else if (dropped != p) {
2950 /*
2951 * The new task was inserted, and pushed out a
2952 * different task
2953 */
2954 get_task_struct(p);
2955 put_task_struct(dropped);
2956 }
2957 /*
2958 * Else the new task was newer than anything already in
2959 * the heap and wasn't inserted
2960 */
2961 }
2962 cgroup_iter_end(scan->cg, &it);
2963
2964 if (heap->size) {
2965 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07002966 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08002967 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07002968 latest_time = q->start_time;
2969 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08002970 }
2971 /* Process the task per the caller's callback */
Paul Jackson4fe91d52008-04-29 00:59:55 -07002972 scan->process_task(q, scan);
2973 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002974 }
2975 /*
2976 * If we had to process any tasks at all, scan again
2977 * in case some of them were in the middle of forking
2978 * children that didn't get processed.
2979 * Not the most efficient way to do it, but it avoids
2980 * having to take callback_mutex in the fork path
2981 */
2982 goto again;
2983 }
2984 if (heap == &tmp_heap)
2985 heap_free(&tmp_heap);
2986 return 0;
2987}
2988
Paul Menage817929e2007-10-18 23:39:36 -07002989/*
Ben Blum102a7752009-09-23 15:56:26 -07002990 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07002991 *
2992 * Reading this file can return large amounts of data if a cgroup has
2993 * *lots* of attached tasks. So it may need several calls to read(),
2994 * but we cannot guarantee that the information we produce is correct
2995 * unless we produce it entirely atomically.
2996 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07002997 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07002998
2999/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003000 * The following two functions "fix" the issue where there are more pids
3001 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3002 * TODO: replace with a kernel-wide solution to this problem
3003 */
3004#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3005static void *pidlist_allocate(int count)
3006{
3007 if (PIDLIST_TOO_LARGE(count))
3008 return vmalloc(count * sizeof(pid_t));
3009 else
3010 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3011}
3012static void pidlist_free(void *p)
3013{
3014 if (is_vmalloc_addr(p))
3015 vfree(p);
3016 else
3017 kfree(p);
3018}
3019static void *pidlist_resize(void *p, int newcount)
3020{
3021 void *newlist;
3022 /* note: if new alloc fails, old p will still be valid either way */
3023 if (is_vmalloc_addr(p)) {
3024 newlist = vmalloc(newcount * sizeof(pid_t));
3025 if (!newlist)
3026 return NULL;
3027 memcpy(newlist, p, newcount * sizeof(pid_t));
3028 vfree(p);
3029 } else {
3030 newlist = krealloc(p, newcount * sizeof(pid_t), GFP_KERNEL);
3031 }
3032 return newlist;
3033}
3034
3035/*
Ben Blum102a7752009-09-23 15:56:26 -07003036 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
3037 * If the new stripped list is sufficiently smaller and there's enough memory
3038 * to allocate a new buffer, will let go of the unneeded memory. Returns the
3039 * number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003040 */
Ben Blum102a7752009-09-23 15:56:26 -07003041/* is the size difference enough that we should re-allocate the array? */
3042#define PIDLIST_REALLOC_DIFFERENCE(old, new) ((old) - PAGE_SIZE >= (new))
3043static int pidlist_uniq(pid_t **p, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003044{
Ben Blum102a7752009-09-23 15:56:26 -07003045 int src, dest = 1;
3046 pid_t *list = *p;
3047 pid_t *newlist;
3048
3049 /*
3050 * we presume the 0th element is unique, so i starts at 1. trivial
3051 * edge cases first; no work needs to be done for either
3052 */
3053 if (length == 0 || length == 1)
3054 return length;
3055 /* src and dest walk down the list; dest counts unique elements */
3056 for (src = 1; src < length; src++) {
3057 /* find next unique element */
3058 while (list[src] == list[src-1]) {
3059 src++;
3060 if (src == length)
3061 goto after;
3062 }
3063 /* dest always points to where the next unique element goes */
3064 list[dest] = list[src];
3065 dest++;
3066 }
3067after:
3068 /*
3069 * if the length difference is large enough, we want to allocate a
3070 * smaller buffer to save memory. if this fails due to out of memory,
3071 * we'll just stay with what we've got.
3072 */
3073 if (PIDLIST_REALLOC_DIFFERENCE(length, dest)) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003074 newlist = pidlist_resize(list, dest);
Ben Blum102a7752009-09-23 15:56:26 -07003075 if (newlist)
3076 *p = newlist;
3077 }
3078 return dest;
3079}
3080
3081static int cmppid(const void *a, const void *b)
3082{
3083 return *(pid_t *)a - *(pid_t *)b;
3084}
3085
3086/*
Ben Blum72a8cb32009-09-23 15:56:27 -07003087 * find the appropriate pidlist for our purpose (given procs vs tasks)
3088 * returns with the lock on that pidlist already held, and takes care
3089 * of the use count, or returns NULL with no locks held if we're out of
3090 * memory.
3091 */
3092static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3093 enum cgroup_filetype type)
3094{
3095 struct cgroup_pidlist *l;
3096 /* don't need task_nsproxy() if we're looking at ourself */
Li Zefanb70cc5f2010-03-10 15:22:12 -08003097 struct pid_namespace *ns = current->nsproxy->pid_ns;
3098
Ben Blum72a8cb32009-09-23 15:56:27 -07003099 /*
3100 * We can't drop the pidlist_mutex before taking the l->mutex in case
3101 * the last ref-holder is trying to remove l from the list at the same
3102 * time. Holding the pidlist_mutex precludes somebody taking whichever
3103 * list we find out from under us - compare release_pid_array().
3104 */
3105 mutex_lock(&cgrp->pidlist_mutex);
3106 list_for_each_entry(l, &cgrp->pidlists, links) {
3107 if (l->key.type == type && l->key.ns == ns) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003108 /* make sure l doesn't vanish out from under us */
3109 down_write(&l->mutex);
3110 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003111 return l;
3112 }
3113 }
3114 /* entry not found; create a new one */
3115 l = kmalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
3116 if (!l) {
3117 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003118 return l;
3119 }
3120 init_rwsem(&l->mutex);
3121 down_write(&l->mutex);
3122 l->key.type = type;
Li Zefanb70cc5f2010-03-10 15:22:12 -08003123 l->key.ns = get_pid_ns(ns);
Ben Blum72a8cb32009-09-23 15:56:27 -07003124 l->use_count = 0; /* don't increment here */
3125 l->list = NULL;
3126 l->owner = cgrp;
3127 list_add(&l->links, &cgrp->pidlists);
3128 mutex_unlock(&cgrp->pidlist_mutex);
3129 return l;
3130}
3131
3132/*
Ben Blum102a7752009-09-23 15:56:26 -07003133 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3134 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003135static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3136 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003137{
3138 pid_t *array;
3139 int length;
3140 int pid, n = 0; /* used for populating the array */
Paul Menage817929e2007-10-18 23:39:36 -07003141 struct cgroup_iter it;
3142 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003143 struct cgroup_pidlist *l;
3144
3145 /*
3146 * If cgroup gets more users after we read count, we won't have
3147 * enough space - tough. This race is indistinguishable to the
3148 * caller from the case that the additional cgroup users didn't
3149 * show up until sometime later on.
3150 */
3151 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003152 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003153 if (!array)
3154 return -ENOMEM;
3155 /* now, populate the array */
Paul Menagebd89aab2007-10-18 23:40:44 -07003156 cgroup_iter_start(cgrp, &it);
3157 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003158 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003159 break;
Ben Blum102a7752009-09-23 15:56:26 -07003160 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003161 if (type == CGROUP_FILE_PROCS)
3162 pid = task_tgid_vnr(tsk);
3163 else
3164 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003165 if (pid > 0) /* make sure to only use valid results */
3166 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003167 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003168 cgroup_iter_end(cgrp, &it);
Ben Blum102a7752009-09-23 15:56:26 -07003169 length = n;
3170 /* now sort & (if procs) strip out duplicates */
3171 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003172 if (type == CGROUP_FILE_PROCS)
Ben Blum102a7752009-09-23 15:56:26 -07003173 length = pidlist_uniq(&array, length);
Ben Blum72a8cb32009-09-23 15:56:27 -07003174 l = cgroup_pidlist_find(cgrp, type);
3175 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003176 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003177 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003178 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003179 /* store array, freeing old if necessary - lock already held */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003180 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003181 l->list = array;
3182 l->length = length;
3183 l->use_count++;
3184 up_write(&l->mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003185 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003186 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003187}
3188
Balbir Singh846c7bb2007-10-18 23:39:44 -07003189/**
Li Zefana043e3b2008-02-23 15:24:09 -08003190 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003191 * @stats: cgroupstats to fill information into
3192 * @dentry: A dentry entry belonging to the cgroup for which stats have
3193 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003194 *
3195 * Build and fill cgroupstats so that taskstats can export it to user
3196 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003197 */
3198int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3199{
3200 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003201 struct cgroup *cgrp;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003202 struct cgroup_iter it;
3203 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003204
Balbir Singh846c7bb2007-10-18 23:39:44 -07003205 /*
Li Zefan33d283b2008-11-19 15:36:48 -08003206 * Validate dentry by checking the superblock operations,
3207 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003208 */
Li Zefan33d283b2008-11-19 15:36:48 -08003209 if (dentry->d_sb->s_op != &cgroup_ops ||
3210 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07003211 goto err;
3212
3213 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07003214 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003215
Paul Menagebd89aab2007-10-18 23:40:44 -07003216 cgroup_iter_start(cgrp, &it);
3217 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003218 switch (tsk->state) {
3219 case TASK_RUNNING:
3220 stats->nr_running++;
3221 break;
3222 case TASK_INTERRUPTIBLE:
3223 stats->nr_sleeping++;
3224 break;
3225 case TASK_UNINTERRUPTIBLE:
3226 stats->nr_uninterruptible++;
3227 break;
3228 case TASK_STOPPED:
3229 stats->nr_stopped++;
3230 break;
3231 default:
3232 if (delayacct_is_task_waiting_on_io(tsk))
3233 stats->nr_io_wait++;
3234 break;
3235 }
3236 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003237 cgroup_iter_end(cgrp, &it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003238
Balbir Singh846c7bb2007-10-18 23:39:44 -07003239err:
3240 return ret;
3241}
3242
Paul Menage8f3ff202009-09-23 15:56:25 -07003243
Paul Menagecc31edc2008-10-18 20:28:04 -07003244/*
Ben Blum102a7752009-09-23 15:56:26 -07003245 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003246 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003247 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003248 */
3249
Ben Blum102a7752009-09-23 15:56:26 -07003250static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003251{
3252 /*
3253 * Initially we receive a position value that corresponds to
3254 * one more than the last pid shown (or 0 on the first call or
3255 * after a seek to the start). Use a binary-search to find the
3256 * next pid to display, if any
3257 */
Ben Blum102a7752009-09-23 15:56:26 -07003258 struct cgroup_pidlist *l = s->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003259 int index = 0, pid = *pos;
3260 int *iter;
3261
Ben Blum102a7752009-09-23 15:56:26 -07003262 down_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003263 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003264 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003265
Paul Menagecc31edc2008-10-18 20:28:04 -07003266 while (index < end) {
3267 int mid = (index + end) / 2;
Ben Blum102a7752009-09-23 15:56:26 -07003268 if (l->list[mid] == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003269 index = mid;
3270 break;
Ben Blum102a7752009-09-23 15:56:26 -07003271 } else if (l->list[mid] <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003272 index = mid + 1;
3273 else
3274 end = mid;
3275 }
3276 }
3277 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003278 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003279 return NULL;
3280 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003281 iter = l->list + index;
Paul Menagecc31edc2008-10-18 20:28:04 -07003282 *pos = *iter;
3283 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003284}
3285
Ben Blum102a7752009-09-23 15:56:26 -07003286static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003287{
Ben Blum102a7752009-09-23 15:56:26 -07003288 struct cgroup_pidlist *l = s->private;
3289 up_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003290}
3291
Ben Blum102a7752009-09-23 15:56:26 -07003292static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003293{
Ben Blum102a7752009-09-23 15:56:26 -07003294 struct cgroup_pidlist *l = s->private;
3295 pid_t *p = v;
3296 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003297 /*
3298 * Advance to the next pid in the array. If this goes off the
3299 * end, we're done
3300 */
3301 p++;
3302 if (p >= end) {
3303 return NULL;
3304 } else {
3305 *pos = *p;
3306 return p;
3307 }
3308}
3309
Ben Blum102a7752009-09-23 15:56:26 -07003310static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003311{
3312 return seq_printf(s, "%d\n", *(int *)v);
3313}
3314
Ben Blum102a7752009-09-23 15:56:26 -07003315/*
3316 * seq_operations functions for iterating on pidlists through seq_file -
3317 * independent of whether it's tasks or procs
3318 */
3319static const struct seq_operations cgroup_pidlist_seq_operations = {
3320 .start = cgroup_pidlist_start,
3321 .stop = cgroup_pidlist_stop,
3322 .next = cgroup_pidlist_next,
3323 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003324};
3325
Ben Blum102a7752009-09-23 15:56:26 -07003326static void cgroup_release_pid_array(struct cgroup_pidlist *l)
Paul Menagecc31edc2008-10-18 20:28:04 -07003327{
Ben Blum72a8cb32009-09-23 15:56:27 -07003328 /*
3329 * the case where we're the last user of this particular pidlist will
3330 * have us remove it from the cgroup's list, which entails taking the
3331 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3332 * pidlist_mutex, we have to take pidlist_mutex first.
3333 */
3334 mutex_lock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003335 down_write(&l->mutex);
3336 BUG_ON(!l->use_count);
3337 if (!--l->use_count) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003338 /* we're the last user if refcount is 0; remove and free */
3339 list_del(&l->links);
3340 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003341 pidlist_free(l->list);
Ben Blum72a8cb32009-09-23 15:56:27 -07003342 put_pid_ns(l->key.ns);
3343 up_write(&l->mutex);
3344 kfree(l);
3345 return;
Paul Menagecc31edc2008-10-18 20:28:04 -07003346 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003347 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003348 up_write(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003349}
3350
Ben Blum102a7752009-09-23 15:56:26 -07003351static int cgroup_pidlist_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003352{
Ben Blum102a7752009-09-23 15:56:26 -07003353 struct cgroup_pidlist *l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003354 if (!(file->f_mode & FMODE_READ))
3355 return 0;
Ben Blum102a7752009-09-23 15:56:26 -07003356 /*
3357 * the seq_file will only be initialized if the file was opened for
3358 * reading; hence we check if it's not null only in that case.
3359 */
3360 l = ((struct seq_file *)file->private_data)->private;
3361 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003362 return seq_release(inode, file);
3363}
3364
Ben Blum102a7752009-09-23 15:56:26 -07003365static const struct file_operations cgroup_pidlist_operations = {
Paul Menagecc31edc2008-10-18 20:28:04 -07003366 .read = seq_read,
3367 .llseek = seq_lseek,
3368 .write = cgroup_file_write,
Ben Blum102a7752009-09-23 15:56:26 -07003369 .release = cgroup_pidlist_release,
Paul Menagecc31edc2008-10-18 20:28:04 -07003370};
3371
3372/*
Ben Blum102a7752009-09-23 15:56:26 -07003373 * The following functions handle opens on a file that displays a pidlist
3374 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3375 * in the cgroup.
Paul Menagecc31edc2008-10-18 20:28:04 -07003376 */
Ben Blum102a7752009-09-23 15:56:26 -07003377/* helper function for the two below it */
Ben Blum72a8cb32009-09-23 15:56:27 -07003378static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
Paul Menagecc31edc2008-10-18 20:28:04 -07003379{
3380 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Ben Blum72a8cb32009-09-23 15:56:27 -07003381 struct cgroup_pidlist *l;
Paul Menagecc31edc2008-10-18 20:28:04 -07003382 int retval;
3383
3384 /* Nothing to do for write-only files */
3385 if (!(file->f_mode & FMODE_READ))
3386 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003387
Ben Blum102a7752009-09-23 15:56:26 -07003388 /* have the array populated */
Ben Blum72a8cb32009-09-23 15:56:27 -07003389 retval = pidlist_array_load(cgrp, type, &l);
Ben Blum102a7752009-09-23 15:56:26 -07003390 if (retval)
3391 return retval;
3392 /* configure file information */
3393 file->f_op = &cgroup_pidlist_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003394
Ben Blum102a7752009-09-23 15:56:26 -07003395 retval = seq_open(file, &cgroup_pidlist_seq_operations);
Paul Menagecc31edc2008-10-18 20:28:04 -07003396 if (retval) {
Ben Blum102a7752009-09-23 15:56:26 -07003397 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003398 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003399 }
Ben Blum102a7752009-09-23 15:56:26 -07003400 ((struct seq_file *)file->private_data)->private = l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003401 return 0;
3402}
Ben Blum102a7752009-09-23 15:56:26 -07003403static int cgroup_tasks_open(struct inode *unused, struct file *file)
3404{
Ben Blum72a8cb32009-09-23 15:56:27 -07003405 return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
Ben Blum102a7752009-09-23 15:56:26 -07003406}
3407static int cgroup_procs_open(struct inode *unused, struct file *file)
3408{
Ben Blum72a8cb32009-09-23 15:56:27 -07003409 return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
Ben Blum102a7752009-09-23 15:56:26 -07003410}
Paul Menagebbcb81d2007-10-18 23:39:32 -07003411
Paul Menagebd89aab2007-10-18 23:40:44 -07003412static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003413 struct cftype *cft)
3414{
Paul Menagebd89aab2007-10-18 23:40:44 -07003415 return notify_on_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003416}
3417
Paul Menage6379c102008-07-25 01:47:01 -07003418static int cgroup_write_notify_on_release(struct cgroup *cgrp,
3419 struct cftype *cft,
3420 u64 val)
3421{
3422 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
3423 if (val)
3424 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3425 else
3426 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3427 return 0;
3428}
3429
Paul Menagebbcb81d2007-10-18 23:39:32 -07003430/*
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003431 * Unregister event and free resources.
3432 *
3433 * Gets called from workqueue.
3434 */
3435static void cgroup_event_remove(struct work_struct *work)
3436{
3437 struct cgroup_event *event = container_of(work, struct cgroup_event,
3438 remove);
3439 struct cgroup *cgrp = event->cgrp;
3440
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003441 event->cft->unregister_event(cgrp, event->cft, event->eventfd);
3442
3443 eventfd_ctx_put(event->eventfd);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003444 kfree(event);
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08003445 dput(cgrp->dentry);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003446}
3447
3448/*
3449 * Gets called on POLLHUP on eventfd when user closes it.
3450 *
3451 * Called with wqh->lock held and interrupts disabled.
3452 */
3453static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
3454 int sync, void *key)
3455{
3456 struct cgroup_event *event = container_of(wait,
3457 struct cgroup_event, wait);
3458 struct cgroup *cgrp = event->cgrp;
3459 unsigned long flags = (unsigned long)key;
3460
3461 if (flags & POLLHUP) {
Changli Gaoa93d2f12010-05-07 14:33:26 +08003462 __remove_wait_queue(event->wqh, &event->wait);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003463 spin_lock(&cgrp->event_list_lock);
3464 list_del(&event->list);
3465 spin_unlock(&cgrp->event_list_lock);
3466 /*
3467 * We are in atomic context, but cgroup_event_remove() may
3468 * sleep, so we have to call it in workqueue.
3469 */
3470 schedule_work(&event->remove);
3471 }
3472
3473 return 0;
3474}
3475
3476static void cgroup_event_ptable_queue_proc(struct file *file,
3477 wait_queue_head_t *wqh, poll_table *pt)
3478{
3479 struct cgroup_event *event = container_of(pt,
3480 struct cgroup_event, pt);
3481
3482 event->wqh = wqh;
3483 add_wait_queue(wqh, &event->wait);
3484}
3485
3486/*
3487 * Parse input and register new cgroup event handler.
3488 *
3489 * Input must be in format '<event_fd> <control_fd> <args>'.
3490 * Interpretation of args is defined by control file implementation.
3491 */
3492static int cgroup_write_event_control(struct cgroup *cgrp, struct cftype *cft,
3493 const char *buffer)
3494{
3495 struct cgroup_event *event = NULL;
3496 unsigned int efd, cfd;
3497 struct file *efile = NULL;
3498 struct file *cfile = NULL;
3499 char *endp;
3500 int ret;
3501
3502 efd = simple_strtoul(buffer, &endp, 10);
3503 if (*endp != ' ')
3504 return -EINVAL;
3505 buffer = endp + 1;
3506
3507 cfd = simple_strtoul(buffer, &endp, 10);
3508 if ((*endp != ' ') && (*endp != '\0'))
3509 return -EINVAL;
3510 buffer = endp + 1;
3511
3512 event = kzalloc(sizeof(*event), GFP_KERNEL);
3513 if (!event)
3514 return -ENOMEM;
3515 event->cgrp = cgrp;
3516 INIT_LIST_HEAD(&event->list);
3517 init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
3518 init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
3519 INIT_WORK(&event->remove, cgroup_event_remove);
3520
3521 efile = eventfd_fget(efd);
3522 if (IS_ERR(efile)) {
3523 ret = PTR_ERR(efile);
3524 goto fail;
3525 }
3526
3527 event->eventfd = eventfd_ctx_fileget(efile);
3528 if (IS_ERR(event->eventfd)) {
3529 ret = PTR_ERR(event->eventfd);
3530 goto fail;
3531 }
3532
3533 cfile = fget(cfd);
3534 if (!cfile) {
3535 ret = -EBADF;
3536 goto fail;
3537 }
3538
3539 /* the process need read permission on control file */
3540 ret = file_permission(cfile, MAY_READ);
3541 if (ret < 0)
3542 goto fail;
3543
3544 event->cft = __file_cft(cfile);
3545 if (IS_ERR(event->cft)) {
3546 ret = PTR_ERR(event->cft);
3547 goto fail;
3548 }
3549
3550 if (!event->cft->register_event || !event->cft->unregister_event) {
3551 ret = -EINVAL;
3552 goto fail;
3553 }
3554
3555 ret = event->cft->register_event(cgrp, event->cft,
3556 event->eventfd, buffer);
3557 if (ret)
3558 goto fail;
3559
3560 if (efile->f_op->poll(efile, &event->pt) & POLLHUP) {
3561 event->cft->unregister_event(cgrp, event->cft, event->eventfd);
3562 ret = 0;
3563 goto fail;
3564 }
3565
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08003566 /*
3567 * Events should be removed after rmdir of cgroup directory, but before
3568 * destroying subsystem state objects. Let's take reference to cgroup
3569 * directory dentry to do that.
3570 */
3571 dget(cgrp->dentry);
3572
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003573 spin_lock(&cgrp->event_list_lock);
3574 list_add(&event->list, &cgrp->event_list);
3575 spin_unlock(&cgrp->event_list_lock);
3576
3577 fput(cfile);
3578 fput(efile);
3579
3580 return 0;
3581
3582fail:
3583 if (cfile)
3584 fput(cfile);
3585
3586 if (event && event->eventfd && !IS_ERR(event->eventfd))
3587 eventfd_ctx_put(event->eventfd);
3588
3589 if (!IS_ERR_OR_NULL(efile))
3590 fput(efile);
3591
3592 kfree(event);
3593
3594 return ret;
3595}
3596
Daniel Lezcano97978e62010-10-27 15:33:35 -07003597static u64 cgroup_clone_children_read(struct cgroup *cgrp,
3598 struct cftype *cft)
3599{
3600 return clone_children(cgrp);
3601}
3602
3603static int cgroup_clone_children_write(struct cgroup *cgrp,
3604 struct cftype *cft,
3605 u64 val)
3606{
3607 if (val)
3608 set_bit(CGRP_CLONE_CHILDREN, &cgrp->flags);
3609 else
3610 clear_bit(CGRP_CLONE_CHILDREN, &cgrp->flags);
3611 return 0;
3612}
3613
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003614/*
Paul Menagebbcb81d2007-10-18 23:39:32 -07003615 * for the common functions, 'private' gives the type of file
3616 */
Ben Blum102a7752009-09-23 15:56:26 -07003617/* for hysterical raisins, we can't put this on the older files */
3618#define CGROUP_FILE_GENERIC_PREFIX "cgroup."
Paul Menage81a6a5c2007-10-18 23:39:38 -07003619static struct cftype files[] = {
3620 {
3621 .name = "tasks",
3622 .open = cgroup_tasks_open,
Paul Menageaf351022008-07-25 01:47:01 -07003623 .write_u64 = cgroup_tasks_write,
Ben Blum102a7752009-09-23 15:56:26 -07003624 .release = cgroup_pidlist_release,
Li Zefan099fca32009-04-02 16:57:29 -07003625 .mode = S_IRUGO | S_IWUSR,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003626 },
Ben Blum102a7752009-09-23 15:56:26 -07003627 {
3628 .name = CGROUP_FILE_GENERIC_PREFIX "procs",
3629 .open = cgroup_procs_open,
Ben Blum74a11662011-05-26 16:25:20 -07003630 .write_u64 = cgroup_procs_write,
Ben Blum102a7752009-09-23 15:56:26 -07003631 .release = cgroup_pidlist_release,
Ben Blum74a11662011-05-26 16:25:20 -07003632 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07003633 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07003634 {
3635 .name = "notify_on_release",
Paul Menagef4c753b2008-04-29 00:59:56 -07003636 .read_u64 = cgroup_read_notify_on_release,
Paul Menage6379c102008-07-25 01:47:01 -07003637 .write_u64 = cgroup_write_notify_on_release,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003638 },
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003639 {
3640 .name = CGROUP_FILE_GENERIC_PREFIX "event_control",
3641 .write_string = cgroup_write_event_control,
3642 .mode = S_IWUGO,
3643 },
Daniel Lezcano97978e62010-10-27 15:33:35 -07003644 {
3645 .name = "cgroup.clone_children",
3646 .read_u64 = cgroup_clone_children_read,
3647 .write_u64 = cgroup_clone_children_write,
3648 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07003649};
3650
3651static struct cftype cft_release_agent = {
3652 .name = "release_agent",
Paul Menagee788e062008-07-25 01:46:59 -07003653 .read_seq_string = cgroup_release_agent_show,
3654 .write_string = cgroup_release_agent_write,
3655 .max_write_len = PATH_MAX,
Paul Menagebbcb81d2007-10-18 23:39:32 -07003656};
3657
Paul Menagebd89aab2007-10-18 23:40:44 -07003658static int cgroup_populate_dir(struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003659{
3660 int err;
3661 struct cgroup_subsys *ss;
3662
3663 /* First clear out any existing files */
Paul Menagebd89aab2007-10-18 23:40:44 -07003664 cgroup_clear_directory(cgrp->dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003665
Paul Menagebd89aab2007-10-18 23:40:44 -07003666 err = cgroup_add_files(cgrp, NULL, files, ARRAY_SIZE(files));
Paul Menagebbcb81d2007-10-18 23:39:32 -07003667 if (err < 0)
3668 return err;
3669
Paul Menagebd89aab2007-10-18 23:40:44 -07003670 if (cgrp == cgrp->top_cgroup) {
3671 if ((err = cgroup_add_file(cgrp, NULL, &cft_release_agent)) < 0)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003672 return err;
3673 }
3674
Paul Menagebd89aab2007-10-18 23:40:44 -07003675 for_each_subsys(cgrp->root, ss) {
3676 if (ss->populate && (err = ss->populate(ss, cgrp)) < 0)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003677 return err;
3678 }
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07003679 /* This cgroup is ready now */
3680 for_each_subsys(cgrp->root, ss) {
3681 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
3682 /*
3683 * Update id->css pointer and make this css visible from
3684 * CSS ID functions. This pointer will be dereferened
3685 * from RCU-read-side without locks.
3686 */
3687 if (css->id)
3688 rcu_assign_pointer(css->id->css, css);
3689 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003690
3691 return 0;
3692}
3693
3694static void init_cgroup_css(struct cgroup_subsys_state *css,
3695 struct cgroup_subsys *ss,
Paul Menagebd89aab2007-10-18 23:40:44 -07003696 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003697{
Paul Menagebd89aab2007-10-18 23:40:44 -07003698 css->cgroup = cgrp;
Paul Menagee7c5ec92009-01-07 18:08:38 -08003699 atomic_set(&css->refcnt, 1);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003700 css->flags = 0;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07003701 css->id = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003702 if (cgrp == dummytop)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003703 set_bit(CSS_ROOT, &css->flags);
Paul Menagebd89aab2007-10-18 23:40:44 -07003704 BUG_ON(cgrp->subsys[ss->subsys_id]);
3705 cgrp->subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003706}
3707
Paul Menage999cd8a2009-01-07 18:08:36 -08003708static void cgroup_lock_hierarchy(struct cgroupfs_root *root)
3709{
3710 /* We need to take each hierarchy_mutex in a consistent order */
3711 int i;
3712
Ben Blumaae8aab2010-03-10 15:22:07 -08003713 /*
3714 * No worry about a race with rebind_subsystems that might mess up the
3715 * locking order, since both parties are under cgroup_mutex.
3716 */
Paul Menage999cd8a2009-01-07 18:08:36 -08003717 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3718 struct cgroup_subsys *ss = subsys[i];
Ben Blumaae8aab2010-03-10 15:22:07 -08003719 if (ss == NULL)
3720 continue;
Paul Menage999cd8a2009-01-07 18:08:36 -08003721 if (ss->root == root)
Li Zefancfebe562009-02-11 13:04:36 -08003722 mutex_lock(&ss->hierarchy_mutex);
Paul Menage999cd8a2009-01-07 18:08:36 -08003723 }
3724}
3725
3726static void cgroup_unlock_hierarchy(struct cgroupfs_root *root)
3727{
3728 int i;
3729
3730 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3731 struct cgroup_subsys *ss = subsys[i];
Ben Blumaae8aab2010-03-10 15:22:07 -08003732 if (ss == NULL)
3733 continue;
Paul Menage999cd8a2009-01-07 18:08:36 -08003734 if (ss->root == root)
3735 mutex_unlock(&ss->hierarchy_mutex);
3736 }
3737}
3738
Paul Menageddbcc7e2007-10-18 23:39:30 -07003739/*
Li Zefana043e3b2008-02-23 15:24:09 -08003740 * cgroup_create - create a cgroup
3741 * @parent: cgroup that will be parent of the new cgroup
3742 * @dentry: dentry of the new cgroup
3743 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07003744 *
Li Zefana043e3b2008-02-23 15:24:09 -08003745 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07003746 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07003747static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Li Zefan099fca32009-04-02 16:57:29 -07003748 mode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003749{
Paul Menagebd89aab2007-10-18 23:40:44 -07003750 struct cgroup *cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003751 struct cgroupfs_root *root = parent->root;
3752 int err = 0;
3753 struct cgroup_subsys *ss;
3754 struct super_block *sb = root->sb;
3755
Paul Menagebd89aab2007-10-18 23:40:44 -07003756 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
3757 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003758 return -ENOMEM;
3759
3760 /* Grab a reference on the superblock so the hierarchy doesn't
3761 * get deleted on unmount if there are child cgroups. This
3762 * can be done outside cgroup_mutex, since the sb can't
3763 * disappear while someone has an open control file on the
3764 * fs */
3765 atomic_inc(&sb->s_active);
3766
3767 mutex_lock(&cgroup_mutex);
3768
Paul Menagecc31edc2008-10-18 20:28:04 -07003769 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003770
Paul Menagebd89aab2007-10-18 23:40:44 -07003771 cgrp->parent = parent;
3772 cgrp->root = parent->root;
3773 cgrp->top_cgroup = parent->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003774
Li Zefanb6abdb02008-03-04 14:28:19 -08003775 if (notify_on_release(parent))
3776 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3777
Daniel Lezcano97978e62010-10-27 15:33:35 -07003778 if (clone_children(parent))
3779 set_bit(CGRP_CLONE_CHILDREN, &cgrp->flags);
3780
Paul Menageddbcc7e2007-10-18 23:39:30 -07003781 for_each_subsys(root, ss) {
Paul Menagebd89aab2007-10-18 23:40:44 -07003782 struct cgroup_subsys_state *css = ss->create(ss, cgrp);
Li Zefan4528fd02010-02-02 13:44:10 -08003783
Paul Menageddbcc7e2007-10-18 23:39:30 -07003784 if (IS_ERR(css)) {
3785 err = PTR_ERR(css);
3786 goto err_destroy;
3787 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003788 init_cgroup_css(css, ss, cgrp);
Li Zefan4528fd02010-02-02 13:44:10 -08003789 if (ss->use_id) {
3790 err = alloc_css_id(ss, parent, cgrp);
3791 if (err)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07003792 goto err_destroy;
Li Zefan4528fd02010-02-02 13:44:10 -08003793 }
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07003794 /* At error, ->destroy() callback has to free assigned ID. */
Daniel Lezcano97978e62010-10-27 15:33:35 -07003795 if (clone_children(parent) && ss->post_clone)
3796 ss->post_clone(ss, cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003797 }
3798
Paul Menage999cd8a2009-01-07 18:08:36 -08003799 cgroup_lock_hierarchy(root);
Paul Menagebd89aab2007-10-18 23:40:44 -07003800 list_add(&cgrp->sibling, &cgrp->parent->children);
Paul Menage999cd8a2009-01-07 18:08:36 -08003801 cgroup_unlock_hierarchy(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003802 root->number_of_cgroups++;
3803
Paul Menagebd89aab2007-10-18 23:40:44 -07003804 err = cgroup_create_dir(cgrp, dentry, mode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003805 if (err < 0)
3806 goto err_remove;
3807
3808 /* The cgroup directory was pre-locked for us */
Paul Menagebd89aab2007-10-18 23:40:44 -07003809 BUG_ON(!mutex_is_locked(&cgrp->dentry->d_inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07003810
Paul Menagebd89aab2007-10-18 23:40:44 -07003811 err = cgroup_populate_dir(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003812 /* If err < 0, we have a half-filled directory - oh well ;) */
3813
3814 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07003815 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003816
3817 return 0;
3818
3819 err_remove:
3820
KAMEZAWA Hiroyukibaef99a2009-01-29 14:25:10 -08003821 cgroup_lock_hierarchy(root);
Paul Menagebd89aab2007-10-18 23:40:44 -07003822 list_del(&cgrp->sibling);
KAMEZAWA Hiroyukibaef99a2009-01-29 14:25:10 -08003823 cgroup_unlock_hierarchy(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003824 root->number_of_cgroups--;
3825
3826 err_destroy:
3827
3828 for_each_subsys(root, ss) {
Paul Menagebd89aab2007-10-18 23:40:44 -07003829 if (cgrp->subsys[ss->subsys_id])
3830 ss->destroy(ss, cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003831 }
3832
3833 mutex_unlock(&cgroup_mutex);
3834
3835 /* Release the reference count that we took on the superblock */
3836 deactivate_super(sb);
3837
Paul Menagebd89aab2007-10-18 23:40:44 -07003838 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003839 return err;
3840}
3841
3842static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode)
3843{
3844 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
3845
3846 /* the vfs holds inode->i_mutex already */
3847 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
3848}
3849
Li Zefan55b6fd02008-07-29 22:33:20 -07003850static int cgroup_has_css_refs(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003851{
3852 /* Check the reference count on each subsystem. Since we
3853 * already established that there are no tasks in the
Paul Menagee7c5ec92009-01-07 18:08:38 -08003854 * cgroup, if the css refcount is also 1, then there should
Paul Menage81a6a5c2007-10-18 23:39:38 -07003855 * be no outstanding references, so the subsystem is safe to
3856 * destroy. We scan across all subsystems rather than using
3857 * the per-hierarchy linked list of mounted subsystems since
3858 * we can be called via check_for_release() with no
3859 * synchronization other than RCU, and the subsystem linked
3860 * list isn't RCU-safe */
3861 int i;
Ben Blumaae8aab2010-03-10 15:22:07 -08003862 /*
3863 * We won't need to lock the subsys array, because the subsystems
3864 * we're concerned about aren't going anywhere since our cgroup root
3865 * has a reference on them.
3866 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07003867 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3868 struct cgroup_subsys *ss = subsys[i];
3869 struct cgroup_subsys_state *css;
Ben Blumaae8aab2010-03-10 15:22:07 -08003870 /* Skip subsystems not present or not in this hierarchy */
3871 if (ss == NULL || ss->root != cgrp->root)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003872 continue;
Paul Menagebd89aab2007-10-18 23:40:44 -07003873 css = cgrp->subsys[ss->subsys_id];
Paul Menage81a6a5c2007-10-18 23:39:38 -07003874 /* When called from check_for_release() it's possible
3875 * that by this point the cgroup has been removed
3876 * and the css deleted. But a false-positive doesn't
3877 * matter, since it can only happen if the cgroup
3878 * has been deleted and hence no longer needs the
3879 * release agent to be called anyway. */
Paul Menagee7c5ec92009-01-07 18:08:38 -08003880 if (css && (atomic_read(&css->refcnt) > 1))
Paul Menage81a6a5c2007-10-18 23:39:38 -07003881 return 1;
Paul Menage81a6a5c2007-10-18 23:39:38 -07003882 }
3883 return 0;
3884}
3885
Paul Menagee7c5ec92009-01-07 18:08:38 -08003886/*
3887 * Atomically mark all (or else none) of the cgroup's CSS objects as
3888 * CSS_REMOVED. Return true on success, or false if the cgroup has
3889 * busy subsystems. Call with cgroup_mutex held
3890 */
3891
3892static int cgroup_clear_css_refs(struct cgroup *cgrp)
3893{
3894 struct cgroup_subsys *ss;
3895 unsigned long flags;
3896 bool failed = false;
3897 local_irq_save(flags);
3898 for_each_subsys(cgrp->root, ss) {
3899 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
3900 int refcnt;
Paul Menage804b3c22009-01-29 14:25:21 -08003901 while (1) {
Paul Menagee7c5ec92009-01-07 18:08:38 -08003902 /* We can only remove a CSS with a refcnt==1 */
3903 refcnt = atomic_read(&css->refcnt);
3904 if (refcnt > 1) {
3905 failed = true;
3906 goto done;
3907 }
3908 BUG_ON(!refcnt);
3909 /*
3910 * Drop the refcnt to 0 while we check other
3911 * subsystems. This will cause any racing
3912 * css_tryget() to spin until we set the
3913 * CSS_REMOVED bits or abort
3914 */
Paul Menage804b3c22009-01-29 14:25:21 -08003915 if (atomic_cmpxchg(&css->refcnt, refcnt, 0) == refcnt)
3916 break;
3917 cpu_relax();
3918 }
Paul Menagee7c5ec92009-01-07 18:08:38 -08003919 }
3920 done:
3921 for_each_subsys(cgrp->root, ss) {
3922 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
3923 if (failed) {
3924 /*
3925 * Restore old refcnt if we previously managed
3926 * to clear it from 1 to 0
3927 */
3928 if (!atomic_read(&css->refcnt))
3929 atomic_set(&css->refcnt, 1);
3930 } else {
3931 /* Commit the fact that the CSS is removed */
3932 set_bit(CSS_REMOVED, &css->flags);
3933 }
3934 }
3935 local_irq_restore(flags);
3936 return !failed;
3937}
3938
Paul Menageddbcc7e2007-10-18 23:39:30 -07003939static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
3940{
Paul Menagebd89aab2007-10-18 23:40:44 -07003941 struct cgroup *cgrp = dentry->d_fsdata;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003942 struct dentry *d;
3943 struct cgroup *parent;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003944 DEFINE_WAIT(wait);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08003945 struct cgroup_event *event, *tmp;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003946 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003947
3948 /* the vfs holds both inode->i_mutex already */
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003949again:
Paul Menageddbcc7e2007-10-18 23:39:30 -07003950 mutex_lock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07003951 if (atomic_read(&cgrp->count) != 0) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07003952 mutex_unlock(&cgroup_mutex);
3953 return -EBUSY;
3954 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003955 if (!list_empty(&cgrp->children)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07003956 mutex_unlock(&cgroup_mutex);
3957 return -EBUSY;
3958 }
KAMEZAWA Hiroyuki3fa59df2008-11-19 15:36:34 -08003959 mutex_unlock(&cgroup_mutex);
Li Zefana043e3b2008-02-23 15:24:09 -08003960
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -08003961 /*
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003962 * In general, subsystem has no css->refcnt after pre_destroy(). But
3963 * in racy cases, subsystem may have to get css->refcnt after
3964 * pre_destroy() and it makes rmdir return with -EBUSY. This sometimes
3965 * make rmdir return -EBUSY too often. To avoid that, we use waitqueue
3966 * for cgroup's rmdir. CGRP_WAIT_ON_RMDIR is for synchronizing rmdir
3967 * and subsystem's reference count handling. Please see css_get/put
3968 * and css_tryget() and cgroup_wakeup_rmdir_waiter() implementation.
3969 */
3970 set_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
3971
3972 /*
Li Zefana043e3b2008-02-23 15:24:09 -08003973 * Call pre_destroy handlers of subsys. Notify subsystems
3974 * that rmdir() request comes.
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -08003975 */
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003976 ret = cgroup_call_pre_destroy(cgrp);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003977 if (ret) {
3978 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003979 return ret;
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003980 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003981
KAMEZAWA Hiroyuki3fa59df2008-11-19 15:36:34 -08003982 mutex_lock(&cgroup_mutex);
3983 parent = cgrp->parent;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003984 if (atomic_read(&cgrp->count) || !list_empty(&cgrp->children)) {
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003985 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003986 mutex_unlock(&cgroup_mutex);
3987 return -EBUSY;
3988 }
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003989 prepare_to_wait(&cgroup_rmdir_waitq, &wait, TASK_INTERRUPTIBLE);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003990 if (!cgroup_clear_css_refs(cgrp)) {
3991 mutex_unlock(&cgroup_mutex);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003992 /*
3993 * Because someone may call cgroup_wakeup_rmdir_waiter() before
3994 * prepare_to_wait(), we need to check this flag.
3995 */
3996 if (test_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags))
3997 schedule();
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003998 finish_wait(&cgroup_rmdir_waitq, &wait);
3999 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
4000 if (signal_pending(current))
4001 return -EINTR;
4002 goto again;
4003 }
4004 /* NO css_tryget() can success after here. */
4005 finish_wait(&cgroup_rmdir_waitq, &wait);
4006 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004007
Paul Menage81a6a5c2007-10-18 23:39:38 -07004008 spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07004009 set_bit(CGRP_REMOVED, &cgrp->flags);
4010 if (!list_empty(&cgrp->release_list))
Phil Carmody8d258792011-03-22 16:30:13 -07004011 list_del_init(&cgrp->release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004012 spin_unlock(&release_list_lock);
Paul Menage999cd8a2009-01-07 18:08:36 -08004013
4014 cgroup_lock_hierarchy(cgrp->root);
4015 /* delete this cgroup from parent->children */
Phil Carmody8d258792011-03-22 16:30:13 -07004016 list_del_init(&cgrp->sibling);
Paul Menage999cd8a2009-01-07 18:08:36 -08004017 cgroup_unlock_hierarchy(cgrp->root);
4018
Paul Menagebd89aab2007-10-18 23:40:44 -07004019 d = dget(cgrp->dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004020
4021 cgroup_d_remove_dir(d);
4022 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004023
Paul Menagebd89aab2007-10-18 23:40:44 -07004024 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004025 check_for_release(parent);
4026
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004027 /*
4028 * Unregister events and notify userspace.
4029 * Notify userspace about cgroup removing only after rmdir of cgroup
4030 * directory to avoid race between userspace and kernelspace
4031 */
4032 spin_lock(&cgrp->event_list_lock);
4033 list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) {
4034 list_del(&event->list);
4035 remove_wait_queue(event->wqh, &event->wait);
4036 eventfd_signal(event->eventfd, 1);
4037 schedule_work(&event->remove);
4038 }
4039 spin_unlock(&cgrp->event_list_lock);
4040
Paul Menageddbcc7e2007-10-18 23:39:30 -07004041 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004042 return 0;
4043}
4044
Li Zefan06a11922008-04-29 01:00:07 -07004045static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004046{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004047 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004048
4049 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004050
4051 /* Create the top cgroup state for this subsystem */
Li Zefan33a68ac2009-01-07 18:07:42 -08004052 list_add(&ss->sibling, &rootnode.subsys_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004053 ss->root = &rootnode;
4054 css = ss->create(ss, dummytop);
4055 /* We don't handle early failures gracefully */
4056 BUG_ON(IS_ERR(css));
4057 init_cgroup_css(css, ss, dummytop);
4058
Li Zefane8d55fd2008-04-29 01:00:13 -07004059 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004060 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004061 * newly registered, all tasks and hence the
4062 * init_css_set is in the subsystem's top cgroup. */
4063 init_css_set.subsys[ss->subsys_id] = dummytop->subsys[ss->subsys_id];
Paul Menageddbcc7e2007-10-18 23:39:30 -07004064
4065 need_forkexit_callback |= ss->fork || ss->exit;
4066
Li Zefane8d55fd2008-04-29 01:00:13 -07004067 /* At system boot, before all subsystems have been
4068 * registered, no tasks have been forked, so we don't
4069 * need to invoke fork callbacks here. */
4070 BUG_ON(!list_empty(&init_task.tasks));
4071
Paul Menage999cd8a2009-01-07 18:08:36 -08004072 mutex_init(&ss->hierarchy_mutex);
Li Zefancfebe562009-02-11 13:04:36 -08004073 lockdep_set_class(&ss->hierarchy_mutex, &ss->subsys_key);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004074 ss->active = 1;
Ben Blume6a11052010-03-10 15:22:09 -08004075
4076 /* this function shouldn't be used with modular subsystems, since they
4077 * need to register a subsys_id, among other things */
4078 BUG_ON(ss->module);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004079}
4080
4081/**
Ben Blume6a11052010-03-10 15:22:09 -08004082 * cgroup_load_subsys: load and register a modular subsystem at runtime
4083 * @ss: the subsystem to load
4084 *
4085 * This function should be called in a modular subsystem's initcall. If the
Thomas Weber88393162010-03-16 11:47:56 +01004086 * subsystem is built as a module, it will be assigned a new subsys_id and set
Ben Blume6a11052010-03-10 15:22:09 -08004087 * up for use. If the subsystem is built-in anyway, work is delegated to the
4088 * simpler cgroup_init_subsys.
4089 */
4090int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
4091{
4092 int i;
4093 struct cgroup_subsys_state *css;
4094
4095 /* check name and function validity */
4096 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
4097 ss->create == NULL || ss->destroy == NULL)
4098 return -EINVAL;
4099
4100 /*
4101 * we don't support callbacks in modular subsystems. this check is
4102 * before the ss->module check for consistency; a subsystem that could
4103 * be a module should still have no callbacks even if the user isn't
4104 * compiling it as one.
4105 */
4106 if (ss->fork || ss->exit)
4107 return -EINVAL;
4108
4109 /*
4110 * an optionally modular subsystem is built-in: we want to do nothing,
4111 * since cgroup_init_subsys will have already taken care of it.
4112 */
4113 if (ss->module == NULL) {
4114 /* a few sanity checks */
4115 BUG_ON(ss->subsys_id >= CGROUP_BUILTIN_SUBSYS_COUNT);
4116 BUG_ON(subsys[ss->subsys_id] != ss);
4117 return 0;
4118 }
4119
4120 /*
4121 * need to register a subsys id before anything else - for example,
4122 * init_cgroup_css needs it.
4123 */
4124 mutex_lock(&cgroup_mutex);
4125 /* find the first empty slot in the array */
4126 for (i = CGROUP_BUILTIN_SUBSYS_COUNT; i < CGROUP_SUBSYS_COUNT; i++) {
4127 if (subsys[i] == NULL)
4128 break;
4129 }
4130 if (i == CGROUP_SUBSYS_COUNT) {
4131 /* maximum number of subsystems already registered! */
4132 mutex_unlock(&cgroup_mutex);
4133 return -EBUSY;
4134 }
4135 /* assign ourselves the subsys_id */
4136 ss->subsys_id = i;
4137 subsys[i] = ss;
4138
4139 /*
4140 * no ss->create seems to need anything important in the ss struct, so
4141 * this can happen first (i.e. before the rootnode attachment).
4142 */
4143 css = ss->create(ss, dummytop);
4144 if (IS_ERR(css)) {
4145 /* failure case - need to deassign the subsys[] slot. */
4146 subsys[i] = NULL;
4147 mutex_unlock(&cgroup_mutex);
4148 return PTR_ERR(css);
4149 }
4150
4151 list_add(&ss->sibling, &rootnode.subsys_list);
4152 ss->root = &rootnode;
4153
4154 /* our new subsystem will be attached to the dummy hierarchy. */
4155 init_cgroup_css(css, ss, dummytop);
4156 /* init_idr must be after init_cgroup_css because it sets css->id. */
4157 if (ss->use_id) {
4158 int ret = cgroup_init_idr(ss, css);
4159 if (ret) {
4160 dummytop->subsys[ss->subsys_id] = NULL;
4161 ss->destroy(ss, dummytop);
4162 subsys[i] = NULL;
4163 mutex_unlock(&cgroup_mutex);
4164 return ret;
4165 }
4166 }
4167
4168 /*
4169 * Now we need to entangle the css into the existing css_sets. unlike
4170 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4171 * will need a new pointer to it; done by iterating the css_set_table.
4172 * furthermore, modifying the existing css_sets will corrupt the hash
4173 * table state, so each changed css_set will need its hash recomputed.
4174 * this is all done under the css_set_lock.
4175 */
4176 write_lock(&css_set_lock);
4177 for (i = 0; i < CSS_SET_TABLE_SIZE; i++) {
4178 struct css_set *cg;
4179 struct hlist_node *node, *tmp;
4180 struct hlist_head *bucket = &css_set_table[i], *new_bucket;
4181
4182 hlist_for_each_entry_safe(cg, node, tmp, bucket, hlist) {
4183 /* skip entries that we already rehashed */
4184 if (cg->subsys[ss->subsys_id])
4185 continue;
4186 /* remove existing entry */
4187 hlist_del(&cg->hlist);
4188 /* set new value */
4189 cg->subsys[ss->subsys_id] = css;
4190 /* recompute hash and restore entry */
4191 new_bucket = css_set_hash(cg->subsys);
4192 hlist_add_head(&cg->hlist, new_bucket);
4193 }
4194 }
4195 write_unlock(&css_set_lock);
4196
4197 mutex_init(&ss->hierarchy_mutex);
4198 lockdep_set_class(&ss->hierarchy_mutex, &ss->subsys_key);
4199 ss->active = 1;
4200
Ben Blume6a11052010-03-10 15:22:09 -08004201 /* success! */
4202 mutex_unlock(&cgroup_mutex);
4203 return 0;
4204}
4205EXPORT_SYMBOL_GPL(cgroup_load_subsys);
4206
4207/**
Ben Blumcf5d5942010-03-10 15:22:09 -08004208 * cgroup_unload_subsys: unload a modular subsystem
4209 * @ss: the subsystem to unload
4210 *
4211 * This function should be called in a modular subsystem's exitcall. When this
4212 * function is invoked, the refcount on the subsystem's module will be 0, so
4213 * the subsystem will not be attached to any hierarchy.
4214 */
4215void cgroup_unload_subsys(struct cgroup_subsys *ss)
4216{
4217 struct cg_cgroup_link *link;
4218 struct hlist_head *hhead;
4219
4220 BUG_ON(ss->module == NULL);
4221
4222 /*
4223 * we shouldn't be called if the subsystem is in use, and the use of
4224 * try_module_get in parse_cgroupfs_options should ensure that it
4225 * doesn't start being used while we're killing it off.
4226 */
4227 BUG_ON(ss->root != &rootnode);
4228
4229 mutex_lock(&cgroup_mutex);
4230 /* deassign the subsys_id */
4231 BUG_ON(ss->subsys_id < CGROUP_BUILTIN_SUBSYS_COUNT);
4232 subsys[ss->subsys_id] = NULL;
4233
4234 /* remove subsystem from rootnode's list of subsystems */
Phil Carmody8d258792011-03-22 16:30:13 -07004235 list_del_init(&ss->sibling);
Ben Blumcf5d5942010-03-10 15:22:09 -08004236
4237 /*
4238 * disentangle the css from all css_sets attached to the dummytop. as
4239 * in loading, we need to pay our respects to the hashtable gods.
4240 */
4241 write_lock(&css_set_lock);
4242 list_for_each_entry(link, &dummytop->css_sets, cgrp_link_list) {
4243 struct css_set *cg = link->cg;
4244
4245 hlist_del(&cg->hlist);
4246 BUG_ON(!cg->subsys[ss->subsys_id]);
4247 cg->subsys[ss->subsys_id] = NULL;
4248 hhead = css_set_hash(cg->subsys);
4249 hlist_add_head(&cg->hlist, hhead);
4250 }
4251 write_unlock(&css_set_lock);
4252
4253 /*
4254 * remove subsystem's css from the dummytop and free it - need to free
4255 * before marking as null because ss->destroy needs the cgrp->subsys
4256 * pointer to find their state. note that this also takes care of
4257 * freeing the css_id.
4258 */
4259 ss->destroy(ss, dummytop);
4260 dummytop->subsys[ss->subsys_id] = NULL;
4261
4262 mutex_unlock(&cgroup_mutex);
4263}
4264EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
4265
4266/**
Li Zefana043e3b2008-02-23 15:24:09 -08004267 * cgroup_init_early - cgroup initialization at system boot
4268 *
4269 * Initialize cgroups at system boot, and initialize any
4270 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004271 */
4272int __init cgroup_init_early(void)
4273{
4274 int i;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07004275 atomic_set(&init_css_set.refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -07004276 INIT_LIST_HEAD(&init_css_set.cg_links);
4277 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07004278 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07004279 css_set_count = 1;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004280 init_cgroup_root(&rootnode);
Paul Menage817929e2007-10-18 23:39:36 -07004281 root_count = 1;
4282 init_task.cgroups = &init_css_set;
4283
4284 init_css_set_link.cg = &init_css_set;
Paul Menage7717f7b2009-09-23 15:56:22 -07004285 init_css_set_link.cgrp = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -07004286 list_add(&init_css_set_link.cgrp_link_list,
Paul Menage817929e2007-10-18 23:39:36 -07004287 &rootnode.top_cgroup.css_sets);
4288 list_add(&init_css_set_link.cg_link_list,
4289 &init_css_set.cg_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004290
Li Zefan472b1052008-04-29 01:00:11 -07004291 for (i = 0; i < CSS_SET_TABLE_SIZE; i++)
4292 INIT_HLIST_HEAD(&css_set_table[i]);
4293
Ben Blumaae8aab2010-03-10 15:22:07 -08004294 /* at bootup time, we don't worry about modular subsystems */
4295 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004296 struct cgroup_subsys *ss = subsys[i];
4297
4298 BUG_ON(!ss->name);
4299 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
4300 BUG_ON(!ss->create);
4301 BUG_ON(!ss->destroy);
4302 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08004303 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07004304 ss->name, ss->subsys_id);
4305 BUG();
4306 }
4307
4308 if (ss->early_init)
4309 cgroup_init_subsys(ss);
4310 }
4311 return 0;
4312}
4313
4314/**
Li Zefana043e3b2008-02-23 15:24:09 -08004315 * cgroup_init - cgroup initialization
4316 *
4317 * Register cgroup filesystem and /proc file, and initialize
4318 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004319 */
4320int __init cgroup_init(void)
4321{
4322 int err;
4323 int i;
Li Zefan472b1052008-04-29 01:00:11 -07004324 struct hlist_head *hhead;
Paul Menagea4243162007-10-18 23:39:35 -07004325
4326 err = bdi_init(&cgroup_backing_dev_info);
4327 if (err)
4328 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004329
Ben Blumaae8aab2010-03-10 15:22:07 -08004330 /* at bootup time, we don't worry about modular subsystems */
4331 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004332 struct cgroup_subsys *ss = subsys[i];
4333 if (!ss->early_init)
4334 cgroup_init_subsys(ss);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004335 if (ss->use_id)
Ben Blume6a11052010-03-10 15:22:09 -08004336 cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004337 }
4338
Li Zefan472b1052008-04-29 01:00:11 -07004339 /* Add init_css_set to the hash table */
4340 hhead = css_set_hash(init_css_set.subsys);
4341 hlist_add_head(&init_css_set.hlist, hhead);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004342 BUG_ON(!init_root_id(&rootnode));
Greg KH676db4a2010-08-05 13:53:35 -07004343
4344 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
4345 if (!cgroup_kobj) {
4346 err = -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004347 goto out;
Greg KH676db4a2010-08-05 13:53:35 -07004348 }
4349
4350 err = register_filesystem(&cgroup_fs_type);
4351 if (err < 0) {
4352 kobject_put(cgroup_kobj);
4353 goto out;
4354 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004355
Li Zefan46ae2202008-04-29 01:00:08 -07004356 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07004357
Paul Menageddbcc7e2007-10-18 23:39:30 -07004358out:
Paul Menagea4243162007-10-18 23:39:35 -07004359 if (err)
4360 bdi_destroy(&cgroup_backing_dev_info);
4361
Paul Menageddbcc7e2007-10-18 23:39:30 -07004362 return err;
4363}
Paul Menageb4f48b62007-10-18 23:39:33 -07004364
Paul Menagea4243162007-10-18 23:39:35 -07004365/*
4366 * proc_cgroup_show()
4367 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4368 * - Used for /proc/<pid>/cgroup.
4369 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4370 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004371 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07004372 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4373 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4374 * cgroup to top_cgroup.
4375 */
4376
4377/* TODO: Use a proper seq_file iterator */
4378static int proc_cgroup_show(struct seq_file *m, void *v)
4379{
4380 struct pid *pid;
4381 struct task_struct *tsk;
4382 char *buf;
4383 int retval;
4384 struct cgroupfs_root *root;
4385
4386 retval = -ENOMEM;
4387 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4388 if (!buf)
4389 goto out;
4390
4391 retval = -ESRCH;
4392 pid = m->private;
4393 tsk = get_pid_task(pid, PIDTYPE_PID);
4394 if (!tsk)
4395 goto out_free;
4396
4397 retval = 0;
4398
4399 mutex_lock(&cgroup_mutex);
4400
Li Zefane5f6a862009-01-07 18:07:41 -08004401 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004402 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004403 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07004404 int count = 0;
4405
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004406 seq_printf(m, "%d:", root->hierarchy_id);
Paul Menagea4243162007-10-18 23:39:35 -07004407 for_each_subsys(root, ss)
4408 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004409 if (strlen(root->name))
4410 seq_printf(m, "%sname=%s", count ? "," : "",
4411 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004412 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004413 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07004414 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07004415 if (retval < 0)
4416 goto out_unlock;
4417 seq_puts(m, buf);
4418 seq_putc(m, '\n');
4419 }
4420
4421out_unlock:
4422 mutex_unlock(&cgroup_mutex);
4423 put_task_struct(tsk);
4424out_free:
4425 kfree(buf);
4426out:
4427 return retval;
4428}
4429
4430static int cgroup_open(struct inode *inode, struct file *file)
4431{
4432 struct pid *pid = PROC_I(inode)->pid;
4433 return single_open(file, proc_cgroup_show, pid);
4434}
4435
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004436const struct file_operations proc_cgroup_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004437 .open = cgroup_open,
4438 .read = seq_read,
4439 .llseek = seq_lseek,
4440 .release = single_release,
4441};
4442
4443/* Display information about each subsystem and each hierarchy */
4444static int proc_cgroupstats_show(struct seq_file *m, void *v)
4445{
4446 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004447
Paul Menage8bab8dd2008-04-04 14:29:57 -07004448 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08004449 /*
4450 * ideally we don't want subsystems moving around while we do this.
4451 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4452 * subsys/hierarchy state.
4453 */
Paul Menagea4243162007-10-18 23:39:35 -07004454 mutex_lock(&cgroup_mutex);
Paul Menagea4243162007-10-18 23:39:35 -07004455 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
4456 struct cgroup_subsys *ss = subsys[i];
Ben Blumaae8aab2010-03-10 15:22:07 -08004457 if (ss == NULL)
4458 continue;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004459 seq_printf(m, "%s\t%d\t%d\t%d\n",
4460 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07004461 ss->root->number_of_cgroups, !ss->disabled);
Paul Menagea4243162007-10-18 23:39:35 -07004462 }
4463 mutex_unlock(&cgroup_mutex);
4464 return 0;
4465}
4466
4467static int cgroupstats_open(struct inode *inode, struct file *file)
4468{
Al Viro9dce07f12008-03-29 03:07:28 +00004469 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07004470}
4471
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004472static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004473 .open = cgroupstats_open,
4474 .read = seq_read,
4475 .llseek = seq_lseek,
4476 .release = single_release,
4477};
4478
Paul Menageb4f48b62007-10-18 23:39:33 -07004479/**
4480 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08004481 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07004482 *
4483 * Description: A task inherits its parent's cgroup at fork().
4484 *
4485 * A pointer to the shared css_set was automatically copied in
4486 * fork.c by dup_task_struct(). However, we ignore that copy, since
4487 * it was not made under the protection of RCU or cgroup_mutex, so
Cliff Wickman956db3c2008-02-07 00:14:43 -08004488 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
Paul Menage817929e2007-10-18 23:39:36 -07004489 * have already changed current->cgroups, allowing the previously
4490 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07004491 *
4492 * At the point that cgroup_fork() is called, 'current' is the parent
4493 * task, and the passed argument 'child' points to the child task.
4494 */
4495void cgroup_fork(struct task_struct *child)
4496{
Paul Menage817929e2007-10-18 23:39:36 -07004497 task_lock(current);
4498 child->cgroups = current->cgroups;
4499 get_css_set(child->cgroups);
4500 task_unlock(current);
4501 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07004502}
4503
4504/**
Li Zefana043e3b2008-02-23 15:24:09 -08004505 * cgroup_fork_callbacks - run fork callbacks
4506 * @child: the new task
4507 *
4508 * Called on a new task very soon before adding it to the
4509 * tasklist. No need to take any locks since no-one can
4510 * be operating on this task.
Paul Menageb4f48b62007-10-18 23:39:33 -07004511 */
4512void cgroup_fork_callbacks(struct task_struct *child)
4513{
4514 if (need_forkexit_callback) {
4515 int i;
Ben Blumaae8aab2010-03-10 15:22:07 -08004516 /*
4517 * forkexit callbacks are only supported for builtin
4518 * subsystems, and the builtin section of the subsys array is
4519 * immutable, so we don't need to lock the subsys array here.
4520 */
4521 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
Paul Menageb4f48b62007-10-18 23:39:33 -07004522 struct cgroup_subsys *ss = subsys[i];
4523 if (ss->fork)
4524 ss->fork(ss, child);
4525 }
4526 }
4527}
4528
4529/**
Li Zefana043e3b2008-02-23 15:24:09 -08004530 * cgroup_post_fork - called on a new task after adding it to the task list
4531 * @child: the task in question
4532 *
4533 * Adds the task to the list running through its css_set if necessary.
4534 * Has to be after the task is visible on the task list in case we race
4535 * with the first call to cgroup_iter_start() - to guarantee that the
4536 * new task ends up on its list.
4537 */
Paul Menage817929e2007-10-18 23:39:36 -07004538void cgroup_post_fork(struct task_struct *child)
4539{
4540 if (use_task_css_set_links) {
4541 write_lock(&css_set_lock);
Lai Jiangshanb12b5332009-01-07 18:07:36 -08004542 task_lock(child);
Paul Menage817929e2007-10-18 23:39:36 -07004543 if (list_empty(&child->cg_list))
4544 list_add(&child->cg_list, &child->cgroups->tasks);
Lai Jiangshanb12b5332009-01-07 18:07:36 -08004545 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07004546 write_unlock(&css_set_lock);
4547 }
4548}
4549/**
Paul Menageb4f48b62007-10-18 23:39:33 -07004550 * cgroup_exit - detach cgroup from exiting task
4551 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08004552 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07004553 *
4554 * Description: Detach cgroup from @tsk and release it.
4555 *
4556 * Note that cgroups marked notify_on_release force every task in
4557 * them to take the global cgroup_mutex mutex when exiting.
4558 * This could impact scaling on very large systems. Be reluctant to
4559 * use notify_on_release cgroups where very high task exit scaling
4560 * is required on large systems.
4561 *
4562 * the_top_cgroup_hack:
4563 *
4564 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
4565 *
4566 * We call cgroup_exit() while the task is still competent to
4567 * handle notify_on_release(), then leave the task attached to the
4568 * root cgroup in each hierarchy for the remainder of its exit.
4569 *
4570 * To do this properly, we would increment the reference count on
4571 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
4572 * code we would add a second cgroup function call, to drop that
4573 * reference. This would just create an unnecessary hot spot on
4574 * the top_cgroup reference count, to no avail.
4575 *
4576 * Normally, holding a reference to a cgroup without bumping its
4577 * count is unsafe. The cgroup could go away, or someone could
4578 * attach us to a different cgroup, decrementing the count on
4579 * the first cgroup that we never incremented. But in this case,
4580 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004581 * which wards off any cgroup_attach_task() attempts, or task is a failed
4582 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07004583 */
4584void cgroup_exit(struct task_struct *tsk, int run_callbacks)
4585{
Paul Menage817929e2007-10-18 23:39:36 -07004586 struct css_set *cg;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004587 int i;
Paul Menage817929e2007-10-18 23:39:36 -07004588
4589 /*
4590 * Unlink from the css_set task list if necessary.
4591 * Optimistically check cg_list before taking
4592 * css_set_lock
4593 */
4594 if (!list_empty(&tsk->cg_list)) {
4595 write_lock(&css_set_lock);
4596 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07004597 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07004598 write_unlock(&css_set_lock);
4599 }
4600
Paul Menageb4f48b62007-10-18 23:39:33 -07004601 /* Reassign the task to the init_css_set. */
4602 task_lock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07004603 cg = tsk->cgroups;
4604 tsk->cgroups = &init_css_set;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004605
4606 if (run_callbacks && need_forkexit_callback) {
4607 /*
4608 * modular subsystems can't use callbacks, so no need to lock
4609 * the subsys array
4610 */
4611 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
4612 struct cgroup_subsys *ss = subsys[i];
4613 if (ss->exit) {
4614 struct cgroup *old_cgrp =
4615 rcu_dereference_raw(cg->subsys[i])->cgroup;
4616 struct cgroup *cgrp = task_cgroup(tsk, i);
4617 ss->exit(ss, cgrp, old_cgrp, tsk);
4618 }
4619 }
4620 }
Paul Menageb4f48b62007-10-18 23:39:33 -07004621 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004622
Paul Menage817929e2007-10-18 23:39:36 -07004623 if (cg)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004624 put_css_set_taskexit(cg);
Paul Menageb4f48b62007-10-18 23:39:33 -07004625}
Paul Menage697f4162007-10-18 23:39:34 -07004626
4627/**
Grzegorz Nosek313e9242009-04-02 16:57:23 -07004628 * cgroup_is_descendant - see if @cgrp is a descendant of @task's cgrp
Li Zefana043e3b2008-02-23 15:24:09 -08004629 * @cgrp: the cgroup in question
Grzegorz Nosek313e9242009-04-02 16:57:23 -07004630 * @task: the task in question
Li Zefana043e3b2008-02-23 15:24:09 -08004631 *
Grzegorz Nosek313e9242009-04-02 16:57:23 -07004632 * See if @cgrp is a descendant of @task's cgroup in the appropriate
4633 * hierarchy.
Paul Menage697f4162007-10-18 23:39:34 -07004634 *
4635 * If we are sending in dummytop, then presumably we are creating
4636 * the top cgroup in the subsystem.
4637 *
4638 * Called only by the ns (nsproxy) cgroup.
4639 */
Grzegorz Nosek313e9242009-04-02 16:57:23 -07004640int cgroup_is_descendant(const struct cgroup *cgrp, struct task_struct *task)
Paul Menage697f4162007-10-18 23:39:34 -07004641{
4642 int ret;
4643 struct cgroup *target;
Paul Menage697f4162007-10-18 23:39:34 -07004644
Paul Menagebd89aab2007-10-18 23:40:44 -07004645 if (cgrp == dummytop)
Paul Menage697f4162007-10-18 23:39:34 -07004646 return 1;
4647
Paul Menage7717f7b2009-09-23 15:56:22 -07004648 target = task_cgroup_from_root(task, cgrp->root);
Paul Menagebd89aab2007-10-18 23:40:44 -07004649 while (cgrp != target && cgrp!= cgrp->top_cgroup)
4650 cgrp = cgrp->parent;
4651 ret = (cgrp == target);
Paul Menage697f4162007-10-18 23:39:34 -07004652 return ret;
4653}
Paul Menage81a6a5c2007-10-18 23:39:38 -07004654
Paul Menagebd89aab2007-10-18 23:40:44 -07004655static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004656{
4657 /* All of these checks rely on RCU to keep the cgroup
4658 * structure alive */
Paul Menagebd89aab2007-10-18 23:40:44 -07004659 if (cgroup_is_releasable(cgrp) && !atomic_read(&cgrp->count)
4660 && list_empty(&cgrp->children) && !cgroup_has_css_refs(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07004661 /* Control Group is currently removeable. If it's not
4662 * already queued for a userspace notification, queue
4663 * it now */
4664 int need_schedule_work = 0;
4665 spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07004666 if (!cgroup_is_removed(cgrp) &&
4667 list_empty(&cgrp->release_list)) {
4668 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004669 need_schedule_work = 1;
4670 }
4671 spin_unlock(&release_list_lock);
4672 if (need_schedule_work)
4673 schedule_work(&release_agent_work);
4674 }
4675}
4676
Daisuke Nishimurad7b9fff2010-03-10 15:22:05 -08004677/* Caller must verify that the css is not for root cgroup */
4678void __css_put(struct cgroup_subsys_state *css, int count)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004679{
Paul Menagebd89aab2007-10-18 23:40:44 -07004680 struct cgroup *cgrp = css->cgroup;
KAMEZAWA Hiroyuki3dece832009-10-01 15:44:09 -07004681 int val;
Paul Menage81a6a5c2007-10-18 23:39:38 -07004682 rcu_read_lock();
Daisuke Nishimurad7b9fff2010-03-10 15:22:05 -08004683 val = atomic_sub_return(count, &css->refcnt);
KAMEZAWA Hiroyuki3dece832009-10-01 15:44:09 -07004684 if (val == 1) {
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004685 if (notify_on_release(cgrp)) {
4686 set_bit(CGRP_RELEASABLE, &cgrp->flags);
4687 check_for_release(cgrp);
4688 }
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07004689 cgroup_wakeup_rmdir_waiter(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004690 }
4691 rcu_read_unlock();
KAMEZAWA Hiroyuki3dece832009-10-01 15:44:09 -07004692 WARN_ON_ONCE(val < 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004693}
Ben Blum67523c42010-03-10 15:22:11 -08004694EXPORT_SYMBOL_GPL(__css_put);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004695
4696/*
4697 * Notify userspace when a cgroup is released, by running the
4698 * configured release agent with the name of the cgroup (path
4699 * relative to the root of cgroup file system) as the argument.
4700 *
4701 * Most likely, this user command will try to rmdir this cgroup.
4702 *
4703 * This races with the possibility that some other task will be
4704 * attached to this cgroup before it is removed, or that some other
4705 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
4706 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
4707 * unused, and this cgroup will be reprieved from its death sentence,
4708 * to continue to serve a useful existence. Next time it's released,
4709 * we will get notified again, if it still has 'notify_on_release' set.
4710 *
4711 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
4712 * means only wait until the task is successfully execve()'d. The
4713 * separate release agent task is forked by call_usermodehelper(),
4714 * then control in this thread returns here, without waiting for the
4715 * release agent task. We don't bother to wait because the caller of
4716 * this routine has no use for the exit status of the release agent
4717 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07004718 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07004719static void cgroup_release_agent(struct work_struct *work)
4720{
4721 BUG_ON(work != &release_agent_work);
4722 mutex_lock(&cgroup_mutex);
4723 spin_lock(&release_list_lock);
4724 while (!list_empty(&release_list)) {
4725 char *argv[3], *envp[3];
4726 int i;
Paul Menagee788e062008-07-25 01:46:59 -07004727 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07004728 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07004729 struct cgroup,
4730 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07004731 list_del_init(&cgrp->release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004732 spin_unlock(&release_list_lock);
4733 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07004734 if (!pathbuf)
4735 goto continue_free;
4736 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
4737 goto continue_free;
4738 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
4739 if (!agentbuf)
4740 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07004741
4742 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07004743 argv[i++] = agentbuf;
4744 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07004745 argv[i] = NULL;
4746
4747 i = 0;
4748 /* minimal command environment */
4749 envp[i++] = "HOME=/";
4750 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
4751 envp[i] = NULL;
4752
4753 /* Drop the lock while we invoke the usermode helper,
4754 * since the exec could involve hitting disk and hence
4755 * be a slow process */
4756 mutex_unlock(&cgroup_mutex);
4757 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004758 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07004759 continue_free:
4760 kfree(pathbuf);
4761 kfree(agentbuf);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004762 spin_lock(&release_list_lock);
4763 }
4764 spin_unlock(&release_list_lock);
4765 mutex_unlock(&cgroup_mutex);
4766}
Paul Menage8bab8dd2008-04-04 14:29:57 -07004767
4768static int __init cgroup_disable(char *str)
4769{
4770 int i;
4771 char *token;
4772
4773 while ((token = strsep(&str, ",")) != NULL) {
4774 if (!*token)
4775 continue;
Ben Blumaae8aab2010-03-10 15:22:07 -08004776 /*
4777 * cgroup_disable, being at boot time, can't know about module
4778 * subsystems, so we don't worry about them.
4779 */
4780 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07004781 struct cgroup_subsys *ss = subsys[i];
4782
4783 if (!strcmp(token, ss->name)) {
4784 ss->disabled = 1;
4785 printk(KERN_INFO "Disabling %s control group"
4786 " subsystem\n", ss->name);
4787 break;
4788 }
4789 }
4790 }
4791 return 1;
4792}
4793__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004794
4795/*
4796 * Functons for CSS ID.
4797 */
4798
4799/*
4800 *To get ID other than 0, this should be called when !cgroup_is_removed().
4801 */
4802unsigned short css_id(struct cgroup_subsys_state *css)
4803{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07004804 struct css_id *cssid;
4805
4806 /*
4807 * This css_id() can return correct value when somone has refcnt
4808 * on this or this is under rcu_read_lock(). Once css->id is allocated,
4809 * it's unchanged until freed.
4810 */
4811 cssid = rcu_dereference_check(css->id,
4812 rcu_read_lock_held() || atomic_read(&css->refcnt));
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004813
4814 if (cssid)
4815 return cssid->id;
4816 return 0;
4817}
Ben Blum67523c42010-03-10 15:22:11 -08004818EXPORT_SYMBOL_GPL(css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004819
4820unsigned short css_depth(struct cgroup_subsys_state *css)
4821{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07004822 struct css_id *cssid;
4823
4824 cssid = rcu_dereference_check(css->id,
4825 rcu_read_lock_held() || atomic_read(&css->refcnt));
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004826
4827 if (cssid)
4828 return cssid->depth;
4829 return 0;
4830}
Ben Blum67523c42010-03-10 15:22:11 -08004831EXPORT_SYMBOL_GPL(css_depth);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004832
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07004833/**
4834 * css_is_ancestor - test "root" css is an ancestor of "child"
4835 * @child: the css to be tested.
4836 * @root: the css supporsed to be an ancestor of the child.
4837 *
4838 * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
4839 * this function reads css->id, this use rcu_dereference() and rcu_read_lock().
4840 * But, considering usual usage, the csses should be valid objects after test.
4841 * Assuming that the caller will do some action to the child if this returns
4842 * returns true, the caller must take "child";s reference count.
4843 * If "child" is valid object and this returns true, "root" is valid, too.
4844 */
4845
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004846bool css_is_ancestor(struct cgroup_subsys_state *child,
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07004847 const struct cgroup_subsys_state *root)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004848{
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07004849 struct css_id *child_id;
4850 struct css_id *root_id;
4851 bool ret = true;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004852
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07004853 rcu_read_lock();
4854 child_id = rcu_dereference(child->id);
4855 root_id = rcu_dereference(root->id);
4856 if (!child_id
4857 || !root_id
4858 || (child_id->depth < root_id->depth)
4859 || (child_id->stack[root_id->depth] != root_id->id))
4860 ret = false;
4861 rcu_read_unlock();
4862 return ret;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004863}
4864
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004865void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
4866{
4867 struct css_id *id = css->id;
4868 /* When this is called before css_id initialization, id can be NULL */
4869 if (!id)
4870 return;
4871
4872 BUG_ON(!ss->use_id);
4873
4874 rcu_assign_pointer(id->css, NULL);
4875 rcu_assign_pointer(css->id, NULL);
4876 spin_lock(&ss->id_lock);
4877 idr_remove(&ss->idr, id->id);
4878 spin_unlock(&ss->id_lock);
Lai Jiangshan025cea92011-03-15 17:56:10 +08004879 kfree_rcu(id, rcu_head);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004880}
Ben Blum67523c42010-03-10 15:22:11 -08004881EXPORT_SYMBOL_GPL(free_css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004882
4883/*
4884 * This is called by init or create(). Then, calls to this function are
4885 * always serialized (By cgroup_mutex() at create()).
4886 */
4887
4888static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
4889{
4890 struct css_id *newid;
4891 int myid, error, size;
4892
4893 BUG_ON(!ss->use_id);
4894
4895 size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
4896 newid = kzalloc(size, GFP_KERNEL);
4897 if (!newid)
4898 return ERR_PTR(-ENOMEM);
4899 /* get id */
4900 if (unlikely(!idr_pre_get(&ss->idr, GFP_KERNEL))) {
4901 error = -ENOMEM;
4902 goto err_out;
4903 }
4904 spin_lock(&ss->id_lock);
4905 /* Don't use 0. allocates an ID of 1-65535 */
4906 error = idr_get_new_above(&ss->idr, newid, 1, &myid);
4907 spin_unlock(&ss->id_lock);
4908
4909 /* Returns error when there are no free spaces for new ID.*/
4910 if (error) {
4911 error = -ENOSPC;
4912 goto err_out;
4913 }
4914 if (myid > CSS_ID_MAX)
4915 goto remove_idr;
4916
4917 newid->id = myid;
4918 newid->depth = depth;
4919 return newid;
4920remove_idr:
4921 error = -ENOSPC;
4922 spin_lock(&ss->id_lock);
4923 idr_remove(&ss->idr, myid);
4924 spin_unlock(&ss->id_lock);
4925err_out:
4926 kfree(newid);
4927 return ERR_PTR(error);
4928
4929}
4930
Ben Blume6a11052010-03-10 15:22:09 -08004931static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss,
4932 struct cgroup_subsys_state *rootcss)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004933{
4934 struct css_id *newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004935
4936 spin_lock_init(&ss->id_lock);
4937 idr_init(&ss->idr);
4938
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004939 newid = get_new_cssid(ss, 0);
4940 if (IS_ERR(newid))
4941 return PTR_ERR(newid);
4942
4943 newid->stack[0] = newid->id;
4944 newid->css = rootcss;
4945 rootcss->id = newid;
4946 return 0;
4947}
4948
4949static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent,
4950 struct cgroup *child)
4951{
4952 int subsys_id, i, depth = 0;
4953 struct cgroup_subsys_state *parent_css, *child_css;
Li Zefanfae9c792010-04-22 17:30:00 +08004954 struct css_id *child_id, *parent_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004955
4956 subsys_id = ss->subsys_id;
4957 parent_css = parent->subsys[subsys_id];
4958 child_css = child->subsys[subsys_id];
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004959 parent_id = parent_css->id;
Greg Thelen94b3dd02010-06-04 14:15:03 -07004960 depth = parent_id->depth + 1;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004961
4962 child_id = get_new_cssid(ss, depth);
4963 if (IS_ERR(child_id))
4964 return PTR_ERR(child_id);
4965
4966 for (i = 0; i < depth; i++)
4967 child_id->stack[i] = parent_id->stack[i];
4968 child_id->stack[depth] = child_id->id;
4969 /*
4970 * child_id->css pointer will be set after this cgroup is available
4971 * see cgroup_populate_dir()
4972 */
4973 rcu_assign_pointer(child_css->id, child_id);
4974
4975 return 0;
4976}
4977
4978/**
4979 * css_lookup - lookup css by id
4980 * @ss: cgroup subsys to be looked into.
4981 * @id: the id
4982 *
4983 * Returns pointer to cgroup_subsys_state if there is valid one with id.
4984 * NULL if not. Should be called under rcu_read_lock()
4985 */
4986struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
4987{
4988 struct css_id *cssid = NULL;
4989
4990 BUG_ON(!ss->use_id);
4991 cssid = idr_find(&ss->idr, id);
4992
4993 if (unlikely(!cssid))
4994 return NULL;
4995
4996 return rcu_dereference(cssid->css);
4997}
Ben Blum67523c42010-03-10 15:22:11 -08004998EXPORT_SYMBOL_GPL(css_lookup);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004999
5000/**
5001 * css_get_next - lookup next cgroup under specified hierarchy.
5002 * @ss: pointer to subsystem
5003 * @id: current position of iteration.
5004 * @root: pointer to css. search tree under this.
5005 * @foundid: position of found object.
5006 *
5007 * Search next css under the specified hierarchy of rootid. Calling under
5008 * rcu_read_lock() is necessary. Returns NULL if it reaches the end.
5009 */
5010struct cgroup_subsys_state *
5011css_get_next(struct cgroup_subsys *ss, int id,
5012 struct cgroup_subsys_state *root, int *foundid)
5013{
5014 struct cgroup_subsys_state *ret = NULL;
5015 struct css_id *tmp;
5016 int tmpid;
5017 int rootid = css_id(root);
5018 int depth = css_depth(root);
5019
5020 if (!rootid)
5021 return NULL;
5022
5023 BUG_ON(!ss->use_id);
5024 /* fill start point for scan */
5025 tmpid = id;
5026 while (1) {
5027 /*
5028 * scan next entry from bitmap(tree), tmpid is updated after
5029 * idr_get_next().
5030 */
5031 spin_lock(&ss->id_lock);
5032 tmp = idr_get_next(&ss->idr, &tmpid);
5033 spin_unlock(&ss->id_lock);
5034
5035 if (!tmp)
5036 break;
5037 if (tmp->depth >= depth && tmp->stack[depth] == rootid) {
5038 ret = rcu_dereference(tmp->css);
5039 if (ret) {
5040 *foundid = tmpid;
5041 break;
5042 }
5043 }
5044 /* continue to scan from next id */
5045 tmpid = tmpid + 1;
5046 }
5047 return ret;
5048}
5049
Stephane Eraniane5d13672011-02-14 11:20:01 +02005050/*
5051 * get corresponding css from file open on cgroupfs directory
5052 */
5053struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id)
5054{
5055 struct cgroup *cgrp;
5056 struct inode *inode;
5057 struct cgroup_subsys_state *css;
5058
5059 inode = f->f_dentry->d_inode;
5060 /* check in cgroup filesystem dir */
5061 if (inode->i_op != &cgroup_dir_inode_operations)
5062 return ERR_PTR(-EBADF);
5063
5064 if (id < 0 || id >= CGROUP_SUBSYS_COUNT)
5065 return ERR_PTR(-EINVAL);
5066
5067 /* get cgroup */
5068 cgrp = __d_cgrp(f->f_dentry);
5069 css = cgrp->subsys[id];
5070 return css ? css : ERR_PTR(-ENOENT);
5071}
5072
Paul Menagefe693432009-09-23 15:56:20 -07005073#ifdef CONFIG_CGROUP_DEBUG
5074static struct cgroup_subsys_state *debug_create(struct cgroup_subsys *ss,
5075 struct cgroup *cont)
5076{
5077 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5078
5079 if (!css)
5080 return ERR_PTR(-ENOMEM);
5081
5082 return css;
5083}
5084
5085static void debug_destroy(struct cgroup_subsys *ss, struct cgroup *cont)
5086{
5087 kfree(cont->subsys[debug_subsys_id]);
5088}
5089
5090static u64 cgroup_refcount_read(struct cgroup *cont, struct cftype *cft)
5091{
5092 return atomic_read(&cont->count);
5093}
5094
5095static u64 debug_taskcount_read(struct cgroup *cont, struct cftype *cft)
5096{
5097 return cgroup_task_count(cont);
5098}
5099
5100static u64 current_css_set_read(struct cgroup *cont, struct cftype *cft)
5101{
5102 return (u64)(unsigned long)current->cgroups;
5103}
5104
5105static u64 current_css_set_refcount_read(struct cgroup *cont,
5106 struct cftype *cft)
5107{
5108 u64 count;
5109
5110 rcu_read_lock();
5111 count = atomic_read(&current->cgroups->refcount);
5112 rcu_read_unlock();
5113 return count;
5114}
5115
Paul Menage7717f7b2009-09-23 15:56:22 -07005116static int current_css_set_cg_links_read(struct cgroup *cont,
5117 struct cftype *cft,
5118 struct seq_file *seq)
5119{
5120 struct cg_cgroup_link *link;
5121 struct css_set *cg;
5122
5123 read_lock(&css_set_lock);
5124 rcu_read_lock();
5125 cg = rcu_dereference(current->cgroups);
5126 list_for_each_entry(link, &cg->cg_links, cg_link_list) {
5127 struct cgroup *c = link->cgrp;
5128 const char *name;
5129
5130 if (c->dentry)
5131 name = c->dentry->d_name.name;
5132 else
5133 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005134 seq_printf(seq, "Root %d group %s\n",
5135 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07005136 }
5137 rcu_read_unlock();
5138 read_unlock(&css_set_lock);
5139 return 0;
5140}
5141
5142#define MAX_TASKS_SHOWN_PER_CSS 25
5143static int cgroup_css_links_read(struct cgroup *cont,
5144 struct cftype *cft,
5145 struct seq_file *seq)
5146{
5147 struct cg_cgroup_link *link;
5148
5149 read_lock(&css_set_lock);
5150 list_for_each_entry(link, &cont->css_sets, cgrp_link_list) {
5151 struct css_set *cg = link->cg;
5152 struct task_struct *task;
5153 int count = 0;
5154 seq_printf(seq, "css_set %p\n", cg);
5155 list_for_each_entry(task, &cg->tasks, cg_list) {
5156 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5157 seq_puts(seq, " ...\n");
5158 break;
5159 } else {
5160 seq_printf(seq, " task %d\n",
5161 task_pid_vnr(task));
5162 }
5163 }
5164 }
5165 read_unlock(&css_set_lock);
5166 return 0;
5167}
5168
Paul Menagefe693432009-09-23 15:56:20 -07005169static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft)
5170{
5171 return test_bit(CGRP_RELEASABLE, &cgrp->flags);
5172}
5173
5174static struct cftype debug_files[] = {
5175 {
5176 .name = "cgroup_refcount",
5177 .read_u64 = cgroup_refcount_read,
5178 },
5179 {
5180 .name = "taskcount",
5181 .read_u64 = debug_taskcount_read,
5182 },
5183
5184 {
5185 .name = "current_css_set",
5186 .read_u64 = current_css_set_read,
5187 },
5188
5189 {
5190 .name = "current_css_set_refcount",
5191 .read_u64 = current_css_set_refcount_read,
5192 },
5193
5194 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005195 .name = "current_css_set_cg_links",
5196 .read_seq_string = current_css_set_cg_links_read,
5197 },
5198
5199 {
5200 .name = "cgroup_css_links",
5201 .read_seq_string = cgroup_css_links_read,
5202 },
5203
5204 {
Paul Menagefe693432009-09-23 15:56:20 -07005205 .name = "releasable",
5206 .read_u64 = releasable_read,
5207 },
5208};
5209
5210static int debug_populate(struct cgroup_subsys *ss, struct cgroup *cont)
5211{
5212 return cgroup_add_files(cont, ss, debug_files,
5213 ARRAY_SIZE(debug_files));
5214}
5215
5216struct cgroup_subsys debug_subsys = {
5217 .name = "debug",
5218 .create = debug_create,
5219 .destroy = debug_destroy,
5220 .populate = debug_populate,
5221 .subsys_id = debug_subsys_id,
5222};
5223#endif /* CONFIG_CGROUP_DEBUG */