blob: 129b47f1df5b20d96f1eedd14eb51cf33eb8778e [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 */
San Mehat1d38bc72009-05-21 14:10:06 -070061#include <linux/capability.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070062
Paul Menageddbcc7e2007-10-18 23:39:30 -070063#include <asm/atomic.h>
64
Paul Menage81a6a5c2007-10-18 23:39:38 -070065static DEFINE_MUTEX(cgroup_mutex);
66
Ben Blumaae8aab2010-03-10 15:22:07 -080067/*
68 * Generate an array of cgroup subsystem pointers. At boot time, this is
69 * populated up to CGROUP_BUILTIN_SUBSYS_COUNT, and modular subsystems are
70 * registered after that. The mutable section of this array is protected by
71 * cgroup_mutex.
72 */
Paul Menageddbcc7e2007-10-18 23:39:30 -070073#define SUBSYS(_x) &_x ## _subsys,
Ben Blumaae8aab2010-03-10 15:22:07 -080074static struct cgroup_subsys *subsys[CGROUP_SUBSYS_COUNT] = {
Paul Menageddbcc7e2007-10-18 23:39:30 -070075#include <linux/cgroup_subsys.h>
76};
77
Paul Menagec6d57f32009-09-23 15:56:19 -070078#define MAX_CGROUP_ROOT_NAMELEN 64
79
Paul Menageddbcc7e2007-10-18 23:39:30 -070080/*
81 * A cgroupfs_root represents the root of a cgroup hierarchy,
82 * and may be associated with a superblock to form an active
83 * hierarchy
84 */
85struct cgroupfs_root {
86 struct super_block *sb;
87
88 /*
89 * The bitmask of subsystems intended to be attached to this
90 * hierarchy
91 */
92 unsigned long subsys_bits;
93
Paul Menage2c6ab6d2009-09-23 15:56:23 -070094 /* Unique id for this hierarchy. */
95 int hierarchy_id;
96
Paul Menageddbcc7e2007-10-18 23:39:30 -070097 /* The bitmask of subsystems currently attached to this hierarchy */
98 unsigned long actual_subsys_bits;
99
100 /* A list running through the attached subsystems */
101 struct list_head subsys_list;
102
103 /* The root cgroup for this hierarchy */
104 struct cgroup top_cgroup;
105
106 /* Tracks how many cgroups are currently defined in hierarchy.*/
107 int number_of_cgroups;
108
Li Zefane5f6a862009-01-07 18:07:41 -0800109 /* A list running through the active hierarchies */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700110 struct list_head root_list;
111
112 /* Hierarchy-specific flags */
113 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700114
Paul Menagee788e062008-07-25 01:46:59 -0700115 /* The path to use for release notifications. */
Paul Menage81a6a5c2007-10-18 23:39:38 -0700116 char release_agent_path[PATH_MAX];
Paul Menagec6d57f32009-09-23 15:56:19 -0700117
118 /* The name for this hierarchy - may be empty */
119 char name[MAX_CGROUP_ROOT_NAMELEN];
Paul Menageddbcc7e2007-10-18 23:39:30 -0700120};
121
Paul Menageddbcc7e2007-10-18 23:39:30 -0700122/*
123 * The "rootnode" hierarchy is the "dummy hierarchy", reserved for the
124 * subsystems that are otherwise unattached - it never has more than a
125 * single cgroup, and all tasks are part of that cgroup.
126 */
127static struct cgroupfs_root rootnode;
128
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700129/*
130 * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when
131 * cgroup_subsys->use_id != 0.
132 */
133#define CSS_ID_MAX (65535)
134struct css_id {
135 /*
136 * The css to which this ID points. This pointer is set to valid value
137 * after cgroup is populated. If cgroup is removed, this will be NULL.
138 * This pointer is expected to be RCU-safe because destroy()
139 * is called after synchronize_rcu(). But for safe use, css_is_removed()
140 * css_tryget() should be used for avoiding race.
141 */
Arnd Bergmann2c392b82010-02-24 19:41:39 +0100142 struct cgroup_subsys_state __rcu *css;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700143 /*
144 * ID of this css.
145 */
146 unsigned short id;
147 /*
148 * Depth in hierarchy which this ID belongs to.
149 */
150 unsigned short depth;
151 /*
152 * ID is freed by RCU. (and lookup routine is RCU safe.)
153 */
154 struct rcu_head rcu_head;
155 /*
156 * Hierarchy of CSS ID belongs to.
157 */
158 unsigned short stack[0]; /* Array of Length (depth+1) */
159};
160
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800161/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300162 * cgroup_event represents events which userspace want to receive.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800163 */
164struct cgroup_event {
165 /*
166 * Cgroup which the event belongs to.
167 */
168 struct cgroup *cgrp;
169 /*
170 * Control file which the event associated.
171 */
172 struct cftype *cft;
173 /*
174 * eventfd to signal userspace about the event.
175 */
176 struct eventfd_ctx *eventfd;
177 /*
178 * Each of these stored in a list by the cgroup.
179 */
180 struct list_head list;
181 /*
182 * All fields below needed to unregister event when
183 * userspace closes eventfd.
184 */
185 poll_table pt;
186 wait_queue_head_t *wqh;
187 wait_queue_t wait;
188 struct work_struct remove;
189};
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700190
Paul Menageddbcc7e2007-10-18 23:39:30 -0700191/* The list of hierarchy roots */
192
193static LIST_HEAD(roots);
Paul Menage817929e2007-10-18 23:39:36 -0700194static int root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700195
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700196static DEFINE_IDA(hierarchy_ida);
197static int next_hierarchy_id;
198static DEFINE_SPINLOCK(hierarchy_id_lock);
199
Paul Menageddbcc7e2007-10-18 23:39:30 -0700200/* dummytop is a shorthand for the dummy hierarchy's top cgroup */
201#define dummytop (&rootnode.top_cgroup)
202
203/* This flag indicates whether tasks in the fork and exit paths should
Li Zefana043e3b2008-02-23 15:24:09 -0800204 * check for fork/exit handlers to call. This avoids us having to do
205 * extra work in the fork/exit path if none of the subsystems need to
206 * be called.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700207 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700208static int need_forkexit_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700209
Paul E. McKenneyd11c5632010-02-22 17:04:50 -0800210#ifdef CONFIG_PROVE_LOCKING
211int cgroup_lock_is_held(void)
212{
213 return lockdep_is_held(&cgroup_mutex);
214}
215#else /* #ifdef CONFIG_PROVE_LOCKING */
216int cgroup_lock_is_held(void)
217{
218 return mutex_is_locked(&cgroup_mutex);
219}
220#endif /* #else #ifdef CONFIG_PROVE_LOCKING */
221
222EXPORT_SYMBOL_GPL(cgroup_lock_is_held);
223
Paul Menageddbcc7e2007-10-18 23:39:30 -0700224/* convenient tests for these bits */
Paul Menagebd89aab2007-10-18 23:40:44 -0700225inline int cgroup_is_removed(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700226{
Paul Menagebd89aab2007-10-18 23:40:44 -0700227 return test_bit(CGRP_REMOVED, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700228}
229
230/* bits in struct cgroupfs_root flags field */
231enum {
232 ROOT_NOPREFIX, /* mounted subsystems have no named prefix */
233};
234
Adrian Bunke9685a02008-02-07 00:13:46 -0800235static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700236{
237 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700238 (1 << CGRP_RELEASABLE) |
239 (1 << CGRP_NOTIFY_ON_RELEASE);
240 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700241}
242
Adrian Bunke9685a02008-02-07 00:13:46 -0800243static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700244{
Paul Menagebd89aab2007-10-18 23:40:44 -0700245 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700246}
247
Daniel Lezcano97978e62010-10-27 15:33:35 -0700248static int clone_children(const struct cgroup *cgrp)
249{
250 return test_bit(CGRP_CLONE_CHILDREN, &cgrp->flags);
251}
252
Paul Menageddbcc7e2007-10-18 23:39:30 -0700253/*
254 * for_each_subsys() allows you to iterate on each subsystem attached to
255 * an active hierarchy
256 */
257#define for_each_subsys(_root, _ss) \
258list_for_each_entry(_ss, &_root->subsys_list, sibling)
259
Li Zefane5f6a862009-01-07 18:07:41 -0800260/* for_each_active_root() allows you to iterate across the active hierarchies */
261#define for_each_active_root(_root) \
Paul Menageddbcc7e2007-10-18 23:39:30 -0700262list_for_each_entry(_root, &roots, root_list)
263
Paul Menage81a6a5c2007-10-18 23:39:38 -0700264/* the list of cgroups eligible for automatic release. Protected by
265 * release_list_lock */
266static LIST_HEAD(release_list);
267static DEFINE_SPINLOCK(release_list_lock);
268static void cgroup_release_agent(struct work_struct *work);
269static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700270static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700271
Paul Menage817929e2007-10-18 23:39:36 -0700272/* Link structure for associating css_set objects with cgroups */
273struct cg_cgroup_link {
274 /*
275 * List running through cg_cgroup_links associated with a
276 * cgroup, anchored on cgroup->css_sets
277 */
Paul Menagebd89aab2007-10-18 23:40:44 -0700278 struct list_head cgrp_link_list;
Paul Menage7717f7b2009-09-23 15:56:22 -0700279 struct cgroup *cgrp;
Paul Menage817929e2007-10-18 23:39:36 -0700280 /*
281 * List running through cg_cgroup_links pointing at a
282 * single css_set object, anchored on css_set->cg_links
283 */
284 struct list_head cg_link_list;
285 struct css_set *cg;
286};
287
288/* The default css_set - used by init and its children prior to any
289 * hierarchies being mounted. It contains a pointer to the root state
290 * for each subsystem. Also used to anchor the list of css_sets. Not
291 * reference-counted, to improve performance when child cgroups
292 * haven't been created.
293 */
294
295static struct css_set init_css_set;
296static struct cg_cgroup_link init_css_set_link;
297
Ben Blume6a11052010-03-10 15:22:09 -0800298static int cgroup_init_idr(struct cgroup_subsys *ss,
299 struct cgroup_subsys_state *css);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700300
Paul Menage817929e2007-10-18 23:39:36 -0700301/* css_set_lock protects the list of css_set objects, and the
302 * chain of tasks off each css_set. Nests outside task->alloc_lock
303 * due to cgroup_iter_start() */
304static DEFINE_RWLOCK(css_set_lock);
305static int css_set_count;
306
Paul Menage7717f7b2009-09-23 15:56:22 -0700307/*
308 * hash table for cgroup groups. This improves the performance to find
309 * an existing css_set. This hash doesn't (currently) take into
310 * account cgroups in empty hierarchies.
311 */
Li Zefan472b1052008-04-29 01:00:11 -0700312#define CSS_SET_HASH_BITS 7
313#define CSS_SET_TABLE_SIZE (1 << CSS_SET_HASH_BITS)
314static struct hlist_head css_set_table[CSS_SET_TABLE_SIZE];
315
316static struct hlist_head *css_set_hash(struct cgroup_subsys_state *css[])
317{
318 int i;
319 int index;
320 unsigned long tmp = 0UL;
321
322 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++)
323 tmp += (unsigned long)css[i];
324 tmp = (tmp >> 16) ^ tmp;
325
326 index = hash_long(tmp, CSS_SET_HASH_BITS);
327
328 return &css_set_table[index];
329}
330
Paul Menage817929e2007-10-18 23:39:36 -0700331/* We don't maintain the lists running through each css_set to its
332 * task until after the first call to cgroup_iter_start(). This
333 * reduces the fork()/exit() overhead for people who have cgroups
334 * compiled into their kernel but not actually in use */
Li Zefan8947f9d2008-07-25 01:46:56 -0700335static int use_task_css_set_links __read_mostly;
Paul Menage817929e2007-10-18 23:39:36 -0700336
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700337static void __put_css_set(struct css_set *cg, int taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700338{
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700339 struct cg_cgroup_link *link;
340 struct cg_cgroup_link *saved_link;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700341 /*
342 * Ensure that the refcount doesn't hit zero while any readers
343 * can see it. Similar to atomic_dec_and_lock(), but for an
344 * rwlock
345 */
346 if (atomic_add_unless(&cg->refcount, -1, 1))
347 return;
348 write_lock(&css_set_lock);
349 if (!atomic_dec_and_test(&cg->refcount)) {
350 write_unlock(&css_set_lock);
351 return;
352 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700353
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700354 /* This css_set is dead. unlink it and release cgroup refcounts */
355 hlist_del(&cg->hlist);
356 css_set_count--;
357
358 list_for_each_entry_safe(link, saved_link, &cg->cg_links,
359 cg_link_list) {
360 struct cgroup *cgrp = link->cgrp;
361 list_del(&link->cg_link_list);
362 list_del(&link->cgrp_link_list);
Paul Menagebd89aab2007-10-18 23:40:44 -0700363 if (atomic_dec_and_test(&cgrp->count) &&
364 notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700365 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700366 set_bit(CGRP_RELEASABLE, &cgrp->flags);
367 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700368 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700369
370 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700371 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700372
373 write_unlock(&css_set_lock);
Lai Jiangshan30088ad2011-03-15 17:53:46 +0800374 kfree_rcu(cg, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700375}
376
377/*
378 * refcounted get/put for css_set objects
379 */
380static inline void get_css_set(struct css_set *cg)
381{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700382 atomic_inc(&cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700383}
384
385static inline void put_css_set(struct css_set *cg)
386{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700387 __put_css_set(cg, 0);
Paul Menage817929e2007-10-18 23:39:36 -0700388}
389
Paul Menage81a6a5c2007-10-18 23:39:38 -0700390static inline void put_css_set_taskexit(struct css_set *cg)
391{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700392 __put_css_set(cg, 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700393}
394
Paul Menage817929e2007-10-18 23:39:36 -0700395/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700396 * compare_css_sets - helper function for find_existing_css_set().
397 * @cg: candidate css_set being tested
398 * @old_cg: existing css_set for a task
399 * @new_cgrp: cgroup that's being entered by the task
400 * @template: desired set of css pointers in css_set (pre-calculated)
401 *
402 * Returns true if "cg" matches "old_cg" except for the hierarchy
403 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
404 */
405static bool compare_css_sets(struct css_set *cg,
406 struct css_set *old_cg,
407 struct cgroup *new_cgrp,
408 struct cgroup_subsys_state *template[])
409{
410 struct list_head *l1, *l2;
411
412 if (memcmp(template, cg->subsys, sizeof(cg->subsys))) {
413 /* Not all subsystems matched */
414 return false;
415 }
416
417 /*
418 * Compare cgroup pointers in order to distinguish between
419 * different cgroups in heirarchies with no subsystems. We
420 * could get by with just this check alone (and skip the
421 * memcmp above) but on most setups the memcmp check will
422 * avoid the need for this more expensive check on almost all
423 * candidates.
424 */
425
426 l1 = &cg->cg_links;
427 l2 = &old_cg->cg_links;
428 while (1) {
429 struct cg_cgroup_link *cgl1, *cgl2;
430 struct cgroup *cg1, *cg2;
431
432 l1 = l1->next;
433 l2 = l2->next;
434 /* See if we reached the end - both lists are equal length. */
435 if (l1 == &cg->cg_links) {
436 BUG_ON(l2 != &old_cg->cg_links);
437 break;
438 } else {
439 BUG_ON(l2 == &old_cg->cg_links);
440 }
441 /* Locate the cgroups associated with these links. */
442 cgl1 = list_entry(l1, struct cg_cgroup_link, cg_link_list);
443 cgl2 = list_entry(l2, struct cg_cgroup_link, cg_link_list);
444 cg1 = cgl1->cgrp;
445 cg2 = cgl2->cgrp;
446 /* Hierarchies should be linked in the same order. */
447 BUG_ON(cg1->root != cg2->root);
448
449 /*
450 * If this hierarchy is the hierarchy of the cgroup
451 * that's changing, then we need to check that this
452 * css_set points to the new cgroup; if it's any other
453 * hierarchy, then this css_set should point to the
454 * same cgroup as the old css_set.
455 */
456 if (cg1->root == new_cgrp->root) {
457 if (cg1 != new_cgrp)
458 return false;
459 } else {
460 if (cg1 != cg2)
461 return false;
462 }
463 }
464 return true;
465}
466
467/*
Paul Menage817929e2007-10-18 23:39:36 -0700468 * find_existing_css_set() is a helper for
469 * find_css_set(), and checks to see whether an existing
Li Zefan472b1052008-04-29 01:00:11 -0700470 * css_set is suitable.
Paul Menage817929e2007-10-18 23:39:36 -0700471 *
472 * oldcg: the cgroup group that we're using before the cgroup
473 * transition
474 *
Paul Menagebd89aab2007-10-18 23:40:44 -0700475 * cgrp: the cgroup that we're moving into
Paul Menage817929e2007-10-18 23:39:36 -0700476 *
477 * template: location in which to build the desired set of subsystem
478 * state objects for the new cgroup group
479 */
Paul Menage817929e2007-10-18 23:39:36 -0700480static struct css_set *find_existing_css_set(
481 struct css_set *oldcg,
Paul Menagebd89aab2007-10-18 23:40:44 -0700482 struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -0700483 struct cgroup_subsys_state *template[])
484{
485 int i;
Paul Menagebd89aab2007-10-18 23:40:44 -0700486 struct cgroupfs_root *root = cgrp->root;
Li Zefan472b1052008-04-29 01:00:11 -0700487 struct hlist_head *hhead;
488 struct hlist_node *node;
489 struct css_set *cg;
Paul Menage817929e2007-10-18 23:39:36 -0700490
Ben Blumaae8aab2010-03-10 15:22:07 -0800491 /*
492 * Build the set of subsystem state objects that we want to see in the
493 * new css_set. while subsystems can change globally, the entries here
494 * won't change, so no need for locking.
495 */
Paul Menage817929e2007-10-18 23:39:36 -0700496 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Li Zefan8d53d552008-02-23 15:24:11 -0800497 if (root->subsys_bits & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700498 /* Subsystem is in this hierarchy. So we want
499 * the subsystem state from the new
500 * cgroup */
Paul Menagebd89aab2007-10-18 23:40:44 -0700501 template[i] = cgrp->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700502 } else {
503 /* Subsystem is not in this hierarchy, so we
504 * don't want to change the subsystem state */
505 template[i] = oldcg->subsys[i];
506 }
507 }
508
Li Zefan472b1052008-04-29 01:00:11 -0700509 hhead = css_set_hash(template);
510 hlist_for_each_entry(cg, node, hhead, hlist) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700511 if (!compare_css_sets(cg, oldcg, cgrp, template))
512 continue;
513
514 /* This css_set matches what we need */
515 return cg;
Li Zefan472b1052008-04-29 01:00:11 -0700516 }
Paul Menage817929e2007-10-18 23:39:36 -0700517
518 /* No existing cgroup group matched */
519 return NULL;
520}
521
Paul Menage817929e2007-10-18 23:39:36 -0700522static void free_cg_links(struct list_head *tmp)
523{
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700524 struct cg_cgroup_link *link;
525 struct cg_cgroup_link *saved_link;
526
527 list_for_each_entry_safe(link, saved_link, tmp, cgrp_link_list) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700528 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -0700529 kfree(link);
530 }
531}
532
533/*
Li Zefan36553432008-07-29 22:33:19 -0700534 * allocate_cg_links() allocates "count" cg_cgroup_link structures
535 * and chains them on tmp through their cgrp_link_list fields. Returns 0 on
536 * success or a negative error
537 */
538static int allocate_cg_links(int count, struct list_head *tmp)
539{
540 struct cg_cgroup_link *link;
541 int i;
542 INIT_LIST_HEAD(tmp);
543 for (i = 0; i < count; i++) {
544 link = kmalloc(sizeof(*link), GFP_KERNEL);
545 if (!link) {
546 free_cg_links(tmp);
547 return -ENOMEM;
548 }
549 list_add(&link->cgrp_link_list, tmp);
550 }
551 return 0;
552}
553
Li Zefanc12f65d2009-01-07 18:07:42 -0800554/**
555 * link_css_set - a helper function to link a css_set to a cgroup
556 * @tmp_cg_links: cg_cgroup_link objects allocated by allocate_cg_links()
557 * @cg: the css_set to be linked
558 * @cgrp: the destination cgroup
559 */
560static void link_css_set(struct list_head *tmp_cg_links,
561 struct css_set *cg, struct cgroup *cgrp)
562{
563 struct cg_cgroup_link *link;
564
565 BUG_ON(list_empty(tmp_cg_links));
566 link = list_first_entry(tmp_cg_links, struct cg_cgroup_link,
567 cgrp_link_list);
568 link->cg = cg;
Paul Menage7717f7b2009-09-23 15:56:22 -0700569 link->cgrp = cgrp;
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700570 atomic_inc(&cgrp->count);
Li Zefanc12f65d2009-01-07 18:07:42 -0800571 list_move(&link->cgrp_link_list, &cgrp->css_sets);
Paul Menage7717f7b2009-09-23 15:56:22 -0700572 /*
573 * Always add links to the tail of the list so that the list
574 * is sorted by order of hierarchy creation
575 */
576 list_add_tail(&link->cg_link_list, &cg->cg_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800577}
578
Li Zefan36553432008-07-29 22:33:19 -0700579/*
Paul Menage817929e2007-10-18 23:39:36 -0700580 * find_css_set() takes an existing cgroup group and a
581 * cgroup object, and returns a css_set object that's
582 * equivalent to the old group, but with the given cgroup
583 * substituted into the appropriate hierarchy. Must be called with
584 * cgroup_mutex held
585 */
Paul Menage817929e2007-10-18 23:39:36 -0700586static struct css_set *find_css_set(
Paul Menagebd89aab2007-10-18 23:40:44 -0700587 struct css_set *oldcg, struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700588{
589 struct css_set *res;
590 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT];
Paul Menage817929e2007-10-18 23:39:36 -0700591
592 struct list_head tmp_cg_links;
Paul Menage817929e2007-10-18 23:39:36 -0700593
Li Zefan472b1052008-04-29 01:00:11 -0700594 struct hlist_head *hhead;
Paul Menage7717f7b2009-09-23 15:56:22 -0700595 struct cg_cgroup_link *link;
Li Zefan472b1052008-04-29 01:00:11 -0700596
Paul Menage817929e2007-10-18 23:39:36 -0700597 /* First see if we already have a cgroup group that matches
598 * the desired set */
Li Zefan7e9abd82008-07-25 01:46:54 -0700599 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -0700600 res = find_existing_css_set(oldcg, cgrp, template);
Paul Menage817929e2007-10-18 23:39:36 -0700601 if (res)
602 get_css_set(res);
Li Zefan7e9abd82008-07-25 01:46:54 -0700603 read_unlock(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -0700604
605 if (res)
606 return res;
607
608 res = kmalloc(sizeof(*res), GFP_KERNEL);
609 if (!res)
610 return NULL;
611
612 /* Allocate all the cg_cgroup_link objects that we'll need */
613 if (allocate_cg_links(root_count, &tmp_cg_links) < 0) {
614 kfree(res);
615 return NULL;
616 }
617
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700618 atomic_set(&res->refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -0700619 INIT_LIST_HEAD(&res->cg_links);
620 INIT_LIST_HEAD(&res->tasks);
Li Zefan472b1052008-04-29 01:00:11 -0700621 INIT_HLIST_NODE(&res->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700622
623 /* Copy the set of subsystem state objects generated in
624 * find_existing_css_set() */
625 memcpy(res->subsys, template, sizeof(res->subsys));
626
627 write_lock(&css_set_lock);
628 /* Add reference counts and links from the new css_set. */
Paul Menage7717f7b2009-09-23 15:56:22 -0700629 list_for_each_entry(link, &oldcg->cg_links, cg_link_list) {
630 struct cgroup *c = link->cgrp;
631 if (c->root == cgrp->root)
632 c = cgrp;
633 link_css_set(&tmp_cg_links, res, c);
634 }
Paul Menage817929e2007-10-18 23:39:36 -0700635
636 BUG_ON(!list_empty(&tmp_cg_links));
637
Paul Menage817929e2007-10-18 23:39:36 -0700638 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700639
640 /* Add this cgroup group to the hash table */
641 hhead = css_set_hash(res->subsys);
642 hlist_add_head(&res->hlist, hhead);
643
Paul Menage817929e2007-10-18 23:39:36 -0700644 write_unlock(&css_set_lock);
645
646 return res;
Paul Menageb4f48b62007-10-18 23:39:33 -0700647}
648
Paul Menageddbcc7e2007-10-18 23:39:30 -0700649/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700650 * Return the cgroup for "task" from the given hierarchy. Must be
651 * called with cgroup_mutex held.
652 */
653static struct cgroup *task_cgroup_from_root(struct task_struct *task,
654 struct cgroupfs_root *root)
655{
656 struct css_set *css;
657 struct cgroup *res = NULL;
658
659 BUG_ON(!mutex_is_locked(&cgroup_mutex));
660 read_lock(&css_set_lock);
661 /*
662 * No need to lock the task - since we hold cgroup_mutex the
663 * task can't change groups, so the only thing that can happen
664 * is that it exits and its css is set back to init_css_set.
665 */
666 css = task->cgroups;
667 if (css == &init_css_set) {
668 res = &root->top_cgroup;
669 } else {
670 struct cg_cgroup_link *link;
671 list_for_each_entry(link, &css->cg_links, cg_link_list) {
672 struct cgroup *c = link->cgrp;
673 if (c->root == root) {
674 res = c;
675 break;
676 }
677 }
678 }
679 read_unlock(&css_set_lock);
680 BUG_ON(!res);
681 return res;
682}
683
684/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700685 * There is one global cgroup mutex. We also require taking
686 * task_lock() when dereferencing a task's cgroup subsys pointers.
687 * See "The task_lock() exception", at the end of this comment.
688 *
689 * A task must hold cgroup_mutex to modify cgroups.
690 *
691 * Any task can increment and decrement the count field without lock.
692 * So in general, code holding cgroup_mutex can't rely on the count
693 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800694 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700695 * means that no tasks are currently attached, therefore there is no
696 * way a task attached to that cgroup can fork (the other way to
697 * increment the count). So code holding cgroup_mutex can safely
698 * assume that if the count is zero, it will stay zero. Similarly, if
699 * a task holds cgroup_mutex on a cgroup with zero count, it
700 * knows that the cgroup won't be removed, as cgroup_rmdir()
701 * needs that mutex.
702 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700703 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
704 * (usually) take cgroup_mutex. These are the two most performance
705 * critical pieces of code here. The exception occurs on cgroup_exit(),
706 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
707 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800708 * to the release agent with the name of the cgroup (path relative to
709 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700710 *
711 * A cgroup can only be deleted if both its 'count' of using tasks
712 * is zero, and its list of 'children' cgroups is empty. Since all
713 * tasks in the system use _some_ cgroup, and since there is always at
714 * least one task in the system (init, pid == 1), therefore, top_cgroup
715 * always has either children cgroups and/or using tasks. So we don't
716 * need a special hack to ensure that top_cgroup cannot be deleted.
717 *
718 * The task_lock() exception
719 *
720 * The need for this exception arises from the action of
Cliff Wickman956db3c2008-02-07 00:14:43 -0800721 * cgroup_attach_task(), which overwrites one tasks cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800722 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700723 * several performance critical places that need to reference
724 * task->cgroup without the expense of grabbing a system global
725 * mutex. Therefore except as noted below, when dereferencing or, as
Cliff Wickman956db3c2008-02-07 00:14:43 -0800726 * in cgroup_attach_task(), modifying a task'ss cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700727 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
728 * the task_struct routinely used for such matters.
729 *
730 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800731 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700732 */
733
Paul Menageddbcc7e2007-10-18 23:39:30 -0700734/**
735 * cgroup_lock - lock out any changes to cgroup structures
736 *
737 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700738void cgroup_lock(void)
739{
740 mutex_lock(&cgroup_mutex);
741}
Ben Blum67523c42010-03-10 15:22:11 -0800742EXPORT_SYMBOL_GPL(cgroup_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700743
744/**
745 * cgroup_unlock - release lock on cgroup changes
746 *
747 * Undo the lock taken in a previous cgroup_lock() call.
748 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700749void cgroup_unlock(void)
750{
751 mutex_unlock(&cgroup_mutex);
752}
Ben Blum67523c42010-03-10 15:22:11 -0800753EXPORT_SYMBOL_GPL(cgroup_unlock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700754
755/*
756 * A couple of forward declarations required, due to cyclic reference loop:
757 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
758 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
759 * -> cgroup_mkdir.
760 */
761
762static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode);
Al Viroc72a04e2011-01-14 05:31:45 +0000763static struct dentry *cgroup_lookup(struct inode *, struct dentry *, struct nameidata *);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700764static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -0700765static int cgroup_populate_dir(struct cgroup *cgrp);
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700766static const struct inode_operations cgroup_dir_inode_operations;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700767static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700768
769static struct backing_dev_info cgroup_backing_dev_info = {
Jens Axboed9938312009-06-12 14:45:52 +0200770 .name = "cgroup",
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700771 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Paul Menagea4243162007-10-18 23:39:35 -0700772};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700773
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700774static int alloc_css_id(struct cgroup_subsys *ss,
775 struct cgroup *parent, struct cgroup *child);
776
Paul Menageddbcc7e2007-10-18 23:39:30 -0700777static struct inode *cgroup_new_inode(mode_t mode, struct super_block *sb)
778{
779 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700780
781 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400782 inode->i_ino = get_next_ino();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700783 inode->i_mode = mode;
David Howells76aac0e2008-11-14 10:39:12 +1100784 inode->i_uid = current_fsuid();
785 inode->i_gid = current_fsgid();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700786 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
787 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
788 }
789 return inode;
790}
791
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800792/*
793 * Call subsys's pre_destroy handler.
794 * This is called before css refcnt check.
795 */
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700796static int cgroup_call_pre_destroy(struct cgroup *cgrp)
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800797{
798 struct cgroup_subsys *ss;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700799 int ret = 0;
800
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800801 for_each_subsys(cgrp->root, ss)
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700802 if (ss->pre_destroy) {
803 ret = ss->pre_destroy(ss, cgrp);
804 if (ret)
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -0800805 break;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700806 }
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800807
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700808 return ret;
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -0800809}
810
Paul Menageddbcc7e2007-10-18 23:39:30 -0700811static void cgroup_diput(struct dentry *dentry, struct inode *inode)
812{
813 /* is dentry a directory ? if so, kfree() associated cgroup */
814 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700815 struct cgroup *cgrp = dentry->d_fsdata;
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800816 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -0700817 BUG_ON(!(cgroup_is_removed(cgrp)));
Paul Menage81a6a5c2007-10-18 23:39:38 -0700818 /* It's possible for external users to be holding css
819 * reference counts on a cgroup; css_put() needs to
820 * be able to access the cgroup after decrementing
821 * the reference count in order to know if it needs to
822 * queue the cgroup to be handled by the release
823 * agent */
824 synchronize_rcu();
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800825
826 mutex_lock(&cgroup_mutex);
827 /*
828 * Release the subsystem state objects.
829 */
Li Zefan75139b82009-01-07 18:07:33 -0800830 for_each_subsys(cgrp->root, ss)
831 ss->destroy(ss, cgrp);
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800832
833 cgrp->root->number_of_cgroups--;
834 mutex_unlock(&cgroup_mutex);
835
Paul Menagea47295e2009-01-07 18:07:44 -0800836 /*
837 * Drop the active superblock reference that we took when we
838 * created the cgroup
839 */
Paul Menage8dc4f3e2008-02-07 00:13:45 -0800840 deactivate_super(cgrp->root->sb);
841
Ben Blum72a8cb32009-09-23 15:56:27 -0700842 /*
843 * if we're getting rid of the cgroup, refcount should ensure
844 * that there are no pidlists left.
845 */
846 BUG_ON(!list_empty(&cgrp->pidlists));
847
Lai Jiangshanf2da1c42011-03-15 17:55:16 +0800848 kfree_rcu(cgrp, rcu_head);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700849 }
850 iput(inode);
851}
852
Al Viroc72a04e2011-01-14 05:31:45 +0000853static int cgroup_delete(const struct dentry *d)
854{
855 return 1;
856}
857
Paul Menageddbcc7e2007-10-18 23:39:30 -0700858static void remove_dir(struct dentry *d)
859{
860 struct dentry *parent = dget(d->d_parent);
861
862 d_delete(d);
863 simple_rmdir(parent->d_inode, d);
864 dput(parent);
865}
866
867static void cgroup_clear_directory(struct dentry *dentry)
868{
869 struct list_head *node;
870
871 BUG_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100872 spin_lock(&dentry->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700873 node = dentry->d_subdirs.next;
874 while (node != &dentry->d_subdirs) {
875 struct dentry *d = list_entry(node, struct dentry, d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100876
877 spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700878 list_del_init(node);
879 if (d->d_inode) {
880 /* This should never be called on a cgroup
881 * directory with child cgroups */
882 BUG_ON(d->d_inode->i_mode & S_IFDIR);
Nick Piggindc0474b2011-01-07 17:49:43 +1100883 dget_dlock(d);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100884 spin_unlock(&d->d_lock);
885 spin_unlock(&dentry->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700886 d_delete(d);
887 simple_unlink(dentry->d_inode, d);
888 dput(d);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100889 spin_lock(&dentry->d_lock);
890 } else
891 spin_unlock(&d->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700892 node = dentry->d_subdirs.next;
893 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100894 spin_unlock(&dentry->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700895}
896
897/*
898 * NOTE : the dentry must have been dget()'ed
899 */
900static void cgroup_d_remove_dir(struct dentry *dentry)
901{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100902 struct dentry *parent;
903
Paul Menageddbcc7e2007-10-18 23:39:30 -0700904 cgroup_clear_directory(dentry);
905
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100906 parent = dentry->d_parent;
907 spin_lock(&parent->d_lock);
Li Zefan3ec762a2011-01-14 11:34:34 +0800908 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700909 list_del_init(&dentry->d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100910 spin_unlock(&dentry->d_lock);
911 spin_unlock(&parent->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700912 remove_dir(dentry);
913}
914
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700915/*
916 * A queue for waiters to do rmdir() cgroup. A tasks will sleep when
917 * cgroup->count == 0 && list_empty(&cgroup->children) && subsys has some
918 * reference to css->refcnt. In general, this refcnt is expected to goes down
919 * to zero, soon.
920 *
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -0700921 * CGRP_WAIT_ON_RMDIR flag is set under cgroup's inode->i_mutex;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700922 */
923DECLARE_WAIT_QUEUE_HEAD(cgroup_rmdir_waitq);
924
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -0700925static void cgroup_wakeup_rmdir_waiter(struct cgroup *cgrp)
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700926{
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -0700927 if (unlikely(test_and_clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags)))
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700928 wake_up_all(&cgroup_rmdir_waitq);
929}
930
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -0700931void cgroup_exclude_rmdir(struct cgroup_subsys_state *css)
932{
933 css_get(css);
934}
935
936void cgroup_release_and_wakeup_rmdir(struct cgroup_subsys_state *css)
937{
938 cgroup_wakeup_rmdir_waiter(css->cgroup);
939 css_put(css);
940}
941
Ben Blumaae8aab2010-03-10 15:22:07 -0800942/*
Ben Blumcf5d5942010-03-10 15:22:09 -0800943 * Call with cgroup_mutex held. Drops reference counts on modules, including
944 * any duplicate ones that parse_cgroupfs_options took. If this function
945 * returns an error, no reference counts are touched.
Ben Blumaae8aab2010-03-10 15:22:07 -0800946 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700947static int rebind_subsystems(struct cgroupfs_root *root,
948 unsigned long final_bits)
949{
950 unsigned long added_bits, removed_bits;
Paul Menagebd89aab2007-10-18 23:40:44 -0700951 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700952 int i;
953
Ben Blumaae8aab2010-03-10 15:22:07 -0800954 BUG_ON(!mutex_is_locked(&cgroup_mutex));
955
Paul Menageddbcc7e2007-10-18 23:39:30 -0700956 removed_bits = root->actual_subsys_bits & ~final_bits;
957 added_bits = final_bits & ~root->actual_subsys_bits;
958 /* Check that any added subsystems are currently free */
959 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Li Zefan8d53d552008-02-23 15:24:11 -0800960 unsigned long bit = 1UL << i;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700961 struct cgroup_subsys *ss = subsys[i];
962 if (!(bit & added_bits))
963 continue;
Ben Blumaae8aab2010-03-10 15:22:07 -0800964 /*
965 * Nobody should tell us to do a subsys that doesn't exist:
966 * parse_cgroupfs_options should catch that case and refcounts
967 * ensure that subsystems won't disappear once selected.
968 */
969 BUG_ON(ss == NULL);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700970 if (ss->root != &rootnode) {
971 /* Subsystem isn't free */
972 return -EBUSY;
973 }
974 }
975
976 /* Currently we don't handle adding/removing subsystems when
977 * any child cgroups exist. This is theoretically supportable
978 * but involves complex error handling, so it's being left until
979 * later */
Paul Menage307257c2008-12-15 13:54:22 -0800980 if (root->number_of_cgroups > 1)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700981 return -EBUSY;
982
983 /* Process each subsystem */
984 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
985 struct cgroup_subsys *ss = subsys[i];
986 unsigned long bit = 1UL << i;
987 if (bit & added_bits) {
988 /* We're binding this subsystem to this hierarchy */
Ben Blumaae8aab2010-03-10 15:22:07 -0800989 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -0700990 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700991 BUG_ON(!dummytop->subsys[i]);
992 BUG_ON(dummytop->subsys[i]->cgroup != dummytop);
Paul Menage999cd8a2009-01-07 18:08:36 -0800993 mutex_lock(&ss->hierarchy_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -0700994 cgrp->subsys[i] = dummytop->subsys[i];
995 cgrp->subsys[i]->cgroup = cgrp;
Li Zefan33a68ac2009-01-07 18:07:42 -0800996 list_move(&ss->sibling, &root->subsys_list);
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -0800997 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700998 if (ss->bind)
Paul Menagebd89aab2007-10-18 23:40:44 -0700999 ss->bind(ss, cgrp);
Paul Menage999cd8a2009-01-07 18:08:36 -08001000 mutex_unlock(&ss->hierarchy_mutex);
Ben Blumcf5d5942010-03-10 15:22:09 -08001001 /* refcount was already taken, and we're keeping it */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001002 } else if (bit & removed_bits) {
1003 /* We're removing this subsystem */
Ben Blumaae8aab2010-03-10 15:22:07 -08001004 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001005 BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]);
1006 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
Paul Menage999cd8a2009-01-07 18:08:36 -08001007 mutex_lock(&ss->hierarchy_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001008 if (ss->bind)
1009 ss->bind(ss, dummytop);
1010 dummytop->subsys[i]->cgroup = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -07001011 cgrp->subsys[i] = NULL;
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001012 subsys[i]->root = &rootnode;
Li Zefan33a68ac2009-01-07 18:07:42 -08001013 list_move(&ss->sibling, &rootnode.subsys_list);
Paul Menage999cd8a2009-01-07 18:08:36 -08001014 mutex_unlock(&ss->hierarchy_mutex);
Ben Blumcf5d5942010-03-10 15:22:09 -08001015 /* subsystem is now free - drop reference on module */
1016 module_put(ss->module);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001017 } else if (bit & final_bits) {
1018 /* Subsystem state should already exist */
Ben Blumaae8aab2010-03-10 15:22:07 -08001019 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001020 BUG_ON(!cgrp->subsys[i]);
Ben Blumcf5d5942010-03-10 15:22:09 -08001021 /*
1022 * a refcount was taken, but we already had one, so
1023 * drop the extra reference.
1024 */
1025 module_put(ss->module);
1026#ifdef CONFIG_MODULE_UNLOAD
1027 BUG_ON(ss->module && !module_refcount(ss->module));
1028#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001029 } else {
1030 /* Subsystem state shouldn't exist */
Paul Menagebd89aab2007-10-18 23:40:44 -07001031 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001032 }
1033 }
1034 root->subsys_bits = root->actual_subsys_bits = final_bits;
1035 synchronize_rcu();
1036
1037 return 0;
1038}
1039
1040static int cgroup_show_options(struct seq_file *seq, struct vfsmount *vfs)
1041{
1042 struct cgroupfs_root *root = vfs->mnt_sb->s_fs_info;
1043 struct cgroup_subsys *ss;
1044
1045 mutex_lock(&cgroup_mutex);
1046 for_each_subsys(root, ss)
1047 seq_printf(seq, ",%s", ss->name);
1048 if (test_bit(ROOT_NOPREFIX, &root->flags))
1049 seq_puts(seq, ",noprefix");
Paul Menage81a6a5c2007-10-18 23:39:38 -07001050 if (strlen(root->release_agent_path))
1051 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Daniel Lezcano97978e62010-10-27 15:33:35 -07001052 if (clone_children(&root->top_cgroup))
1053 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001054 if (strlen(root->name))
1055 seq_printf(seq, ",name=%s", root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001056 mutex_unlock(&cgroup_mutex);
1057 return 0;
1058}
1059
1060struct cgroup_sb_opts {
1061 unsigned long subsys_bits;
1062 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001063 char *release_agent;
Daniel Lezcano97978e62010-10-27 15:33:35 -07001064 bool clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001065 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001066 /* User explicitly requested empty subsystem */
1067 bool none;
Paul Menagec6d57f32009-09-23 15:56:19 -07001068
1069 struct cgroupfs_root *new_root;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001070
Paul Menageddbcc7e2007-10-18 23:39:30 -07001071};
1072
Ben Blumaae8aab2010-03-10 15:22:07 -08001073/*
1074 * Convert a hierarchy specifier into a bitmask of subsystems and flags. Call
Ben Blumcf5d5942010-03-10 15:22:09 -08001075 * with cgroup_mutex held to protect the subsys[] array. This function takes
1076 * refcounts on subsystems to be used, unless it returns error, in which case
1077 * no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001078 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001079static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001080{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001081 char *token, *o = data;
1082 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001083 unsigned long mask = (unsigned long)-1;
Ben Blumcf5d5942010-03-10 15:22:09 -08001084 int i;
1085 bool module_pin_failed = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001086
Ben Blumaae8aab2010-03-10 15:22:07 -08001087 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1088
Li Zefanf9ab5b52009-06-17 16:26:33 -07001089#ifdef CONFIG_CPUSETS
1090 mask = ~(1UL << cpuset_subsys_id);
1091#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001092
Paul Menagec6d57f32009-09-23 15:56:19 -07001093 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001094
1095 while ((token = strsep(&o, ",")) != NULL) {
1096 if (!*token)
1097 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001098 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001099 /* Explicitly have no subsystems */
1100 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001101 continue;
1102 }
1103 if (!strcmp(token, "all")) {
1104 /* Mutually exclusive option 'all' + subsystem name */
1105 if (one_ss)
1106 return -EINVAL;
1107 all_ss = true;
1108 continue;
1109 }
1110 if (!strcmp(token, "noprefix")) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001111 set_bit(ROOT_NOPREFIX, &opts->flags);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001112 continue;
1113 }
1114 if (!strcmp(token, "clone_children")) {
Daniel Lezcano97978e62010-10-27 15:33:35 -07001115 opts->clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001116 continue;
1117 }
1118 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001119 /* Specifying two release agents is forbidden */
1120 if (opts->release_agent)
1121 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001122 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001123 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001124 if (!opts->release_agent)
1125 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001126 continue;
1127 }
1128 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001129 const char *name = token + 5;
1130 /* Can't specify an empty name */
1131 if (!strlen(name))
1132 return -EINVAL;
1133 /* Must match [\w.-]+ */
1134 for (i = 0; i < strlen(name); i++) {
1135 char c = name[i];
1136 if (isalnum(c))
1137 continue;
1138 if ((c == '.') || (c == '-') || (c == '_'))
1139 continue;
1140 return -EINVAL;
1141 }
1142 /* Specifying two names is forbidden */
1143 if (opts->name)
1144 return -EINVAL;
1145 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001146 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001147 GFP_KERNEL);
1148 if (!opts->name)
1149 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001150
1151 continue;
1152 }
1153
1154 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1155 struct cgroup_subsys *ss = subsys[i];
1156 if (ss == NULL)
1157 continue;
1158 if (strcmp(token, ss->name))
1159 continue;
1160 if (ss->disabled)
1161 continue;
1162
1163 /* Mutually exclusive option 'all' + subsystem name */
1164 if (all_ss)
1165 return -EINVAL;
1166 set_bit(i, &opts->subsys_bits);
1167 one_ss = true;
1168
1169 break;
1170 }
1171 if (i == CGROUP_SUBSYS_COUNT)
1172 return -ENOENT;
1173 }
1174
1175 /*
1176 * If the 'all' option was specified select all the subsystems,
1177 * otherwise 'all, 'none' and a subsystem name options were not
1178 * specified, let's default to 'all'
1179 */
1180 if (all_ss || (!all_ss && !one_ss && !opts->none)) {
1181 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1182 struct cgroup_subsys *ss = subsys[i];
1183 if (ss == NULL)
1184 continue;
1185 if (ss->disabled)
1186 continue;
1187 set_bit(i, &opts->subsys_bits);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001188 }
1189 }
1190
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001191 /* Consistency checks */
1192
Li Zefanf9ab5b52009-06-17 16:26:33 -07001193 /*
1194 * Option noprefix was introduced just for backward compatibility
1195 * with the old cpuset, so we allow noprefix only if mounting just
1196 * the cpuset subsystem.
1197 */
1198 if (test_bit(ROOT_NOPREFIX, &opts->flags) &&
1199 (opts->subsys_bits & mask))
1200 return -EINVAL;
1201
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001202
1203 /* Can't specify "none" and some subsystems */
1204 if (opts->subsys_bits && opts->none)
1205 return -EINVAL;
1206
1207 /*
1208 * We either have to specify by name or by subsystems. (So all
1209 * empty hierarchies must have a name).
1210 */
Paul Menagec6d57f32009-09-23 15:56:19 -07001211 if (!opts->subsys_bits && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001212 return -EINVAL;
1213
Ben Blumcf5d5942010-03-10 15:22:09 -08001214 /*
1215 * Grab references on all the modules we'll need, so the subsystems
1216 * don't dance around before rebind_subsystems attaches them. This may
1217 * take duplicate reference counts on a subsystem that's already used,
1218 * but rebind_subsystems handles this case.
1219 */
1220 for (i = CGROUP_BUILTIN_SUBSYS_COUNT; i < CGROUP_SUBSYS_COUNT; i++) {
1221 unsigned long bit = 1UL << i;
1222
1223 if (!(bit & opts->subsys_bits))
1224 continue;
1225 if (!try_module_get(subsys[i]->module)) {
1226 module_pin_failed = true;
1227 break;
1228 }
1229 }
1230 if (module_pin_failed) {
1231 /*
1232 * oops, one of the modules was going away. this means that we
1233 * raced with a module_delete call, and to the user this is
1234 * essentially a "subsystem doesn't exist" case.
1235 */
1236 for (i--; i >= CGROUP_BUILTIN_SUBSYS_COUNT; i--) {
1237 /* drop refcounts only on the ones we took */
1238 unsigned long bit = 1UL << i;
1239
1240 if (!(bit & opts->subsys_bits))
1241 continue;
1242 module_put(subsys[i]->module);
1243 }
1244 return -ENOENT;
1245 }
1246
Paul Menageddbcc7e2007-10-18 23:39:30 -07001247 return 0;
1248}
1249
Ben Blumcf5d5942010-03-10 15:22:09 -08001250static void drop_parsed_module_refcounts(unsigned long subsys_bits)
1251{
1252 int i;
1253 for (i = CGROUP_BUILTIN_SUBSYS_COUNT; i < CGROUP_SUBSYS_COUNT; i++) {
1254 unsigned long bit = 1UL << i;
1255
1256 if (!(bit & subsys_bits))
1257 continue;
1258 module_put(subsys[i]->module);
1259 }
1260}
1261
Paul Menageddbcc7e2007-10-18 23:39:30 -07001262static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1263{
1264 int ret = 0;
1265 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001266 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001267 struct cgroup_sb_opts opts;
1268
Paul Menagebd89aab2007-10-18 23:40:44 -07001269 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001270 mutex_lock(&cgroup_mutex);
1271
1272 /* See what subsystems are wanted */
1273 ret = parse_cgroupfs_options(data, &opts);
1274 if (ret)
1275 goto out_unlock;
1276
Ben Blumcf5d5942010-03-10 15:22:09 -08001277 /* Don't allow flags or name to change at remount */
1278 if (opts.flags != root->flags ||
1279 (opts.name && strcmp(opts.name, root->name))) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001280 ret = -EINVAL;
Ben Blumcf5d5942010-03-10 15:22:09 -08001281 drop_parsed_module_refcounts(opts.subsys_bits);
Paul Menagec6d57f32009-09-23 15:56:19 -07001282 goto out_unlock;
1283 }
1284
Paul Menageddbcc7e2007-10-18 23:39:30 -07001285 ret = rebind_subsystems(root, opts.subsys_bits);
Ben Blumcf5d5942010-03-10 15:22:09 -08001286 if (ret) {
1287 drop_parsed_module_refcounts(opts.subsys_bits);
Li Zefan0670e082009-04-02 16:57:30 -07001288 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001289 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001290
1291 /* (re)populate subsystem files */
Li Zefan0670e082009-04-02 16:57:30 -07001292 cgroup_populate_dir(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001293
Paul Menage81a6a5c2007-10-18 23:39:38 -07001294 if (opts.release_agent)
1295 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001296 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001297 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001298 kfree(opts.name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001299 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07001300 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001301 return ret;
1302}
1303
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07001304static const struct super_operations cgroup_ops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001305 .statfs = simple_statfs,
1306 .drop_inode = generic_delete_inode,
1307 .show_options = cgroup_show_options,
1308 .remount_fs = cgroup_remount,
1309};
1310
Paul Menagecc31edc2008-10-18 20:28:04 -07001311static void init_cgroup_housekeeping(struct cgroup *cgrp)
1312{
1313 INIT_LIST_HEAD(&cgrp->sibling);
1314 INIT_LIST_HEAD(&cgrp->children);
1315 INIT_LIST_HEAD(&cgrp->css_sets);
1316 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001317 INIT_LIST_HEAD(&cgrp->pidlists);
1318 mutex_init(&cgrp->pidlist_mutex);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08001319 INIT_LIST_HEAD(&cgrp->event_list);
1320 spin_lock_init(&cgrp->event_list_lock);
Paul Menagecc31edc2008-10-18 20:28:04 -07001321}
Paul Menagec6d57f32009-09-23 15:56:19 -07001322
Paul Menageddbcc7e2007-10-18 23:39:30 -07001323static void init_cgroup_root(struct cgroupfs_root *root)
1324{
Paul Menagebd89aab2007-10-18 23:40:44 -07001325 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001326 INIT_LIST_HEAD(&root->subsys_list);
1327 INIT_LIST_HEAD(&root->root_list);
1328 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -07001329 cgrp->root = root;
1330 cgrp->top_cgroup = cgrp;
Paul Menagecc31edc2008-10-18 20:28:04 -07001331 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001332}
1333
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001334static bool init_root_id(struct cgroupfs_root *root)
1335{
1336 int ret = 0;
1337
1338 do {
1339 if (!ida_pre_get(&hierarchy_ida, GFP_KERNEL))
1340 return false;
1341 spin_lock(&hierarchy_id_lock);
1342 /* Try to allocate the next unused ID */
1343 ret = ida_get_new_above(&hierarchy_ida, next_hierarchy_id,
1344 &root->hierarchy_id);
1345 if (ret == -ENOSPC)
1346 /* Try again starting from 0 */
1347 ret = ida_get_new(&hierarchy_ida, &root->hierarchy_id);
1348 if (!ret) {
1349 next_hierarchy_id = root->hierarchy_id + 1;
1350 } else if (ret != -EAGAIN) {
1351 /* Can only get here if the 31-bit IDR is full ... */
1352 BUG_ON(ret);
1353 }
1354 spin_unlock(&hierarchy_id_lock);
1355 } while (ret);
1356 return true;
1357}
1358
Paul Menageddbcc7e2007-10-18 23:39:30 -07001359static int cgroup_test_super(struct super_block *sb, void *data)
1360{
Paul Menagec6d57f32009-09-23 15:56:19 -07001361 struct cgroup_sb_opts *opts = data;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001362 struct cgroupfs_root *root = sb->s_fs_info;
1363
Paul Menagec6d57f32009-09-23 15:56:19 -07001364 /* If we asked for a name then it must match */
1365 if (opts->name && strcmp(opts->name, root->name))
1366 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001367
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001368 /*
1369 * If we asked for subsystems (or explicitly for no
1370 * subsystems) then they must match
1371 */
1372 if ((opts->subsys_bits || opts->none)
1373 && (opts->subsys_bits != root->subsys_bits))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001374 return 0;
1375
1376 return 1;
1377}
1378
Paul Menagec6d57f32009-09-23 15:56:19 -07001379static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1380{
1381 struct cgroupfs_root *root;
1382
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001383 if (!opts->subsys_bits && !opts->none)
Paul Menagec6d57f32009-09-23 15:56:19 -07001384 return NULL;
1385
1386 root = kzalloc(sizeof(*root), GFP_KERNEL);
1387 if (!root)
1388 return ERR_PTR(-ENOMEM);
1389
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001390 if (!init_root_id(root)) {
1391 kfree(root);
1392 return ERR_PTR(-ENOMEM);
1393 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001394 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001395
Paul Menagec6d57f32009-09-23 15:56:19 -07001396 root->subsys_bits = opts->subsys_bits;
1397 root->flags = opts->flags;
1398 if (opts->release_agent)
1399 strcpy(root->release_agent_path, opts->release_agent);
1400 if (opts->name)
1401 strcpy(root->name, opts->name);
Daniel Lezcano97978e62010-10-27 15:33:35 -07001402 if (opts->clone_children)
1403 set_bit(CGRP_CLONE_CHILDREN, &root->top_cgroup.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001404 return root;
1405}
1406
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001407static void cgroup_drop_root(struct cgroupfs_root *root)
1408{
1409 if (!root)
1410 return;
1411
1412 BUG_ON(!root->hierarchy_id);
1413 spin_lock(&hierarchy_id_lock);
1414 ida_remove(&hierarchy_ida, root->hierarchy_id);
1415 spin_unlock(&hierarchy_id_lock);
1416 kfree(root);
1417}
1418
Paul Menageddbcc7e2007-10-18 23:39:30 -07001419static int cgroup_set_super(struct super_block *sb, void *data)
1420{
1421 int ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001422 struct cgroup_sb_opts *opts = data;
1423
1424 /* If we don't have a new root, we can't set up a new sb */
1425 if (!opts->new_root)
1426 return -EINVAL;
1427
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001428 BUG_ON(!opts->subsys_bits && !opts->none);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001429
1430 ret = set_anon_super(sb, NULL);
1431 if (ret)
1432 return ret;
1433
Paul Menagec6d57f32009-09-23 15:56:19 -07001434 sb->s_fs_info = opts->new_root;
1435 opts->new_root->sb = sb;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001436
1437 sb->s_blocksize = PAGE_CACHE_SIZE;
1438 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1439 sb->s_magic = CGROUP_SUPER_MAGIC;
1440 sb->s_op = &cgroup_ops;
1441
1442 return 0;
1443}
1444
1445static int cgroup_get_rootdir(struct super_block *sb)
1446{
Al Viro0df6a632010-12-21 13:29:29 -05001447 static const struct dentry_operations cgroup_dops = {
1448 .d_iput = cgroup_diput,
Al Viroc72a04e2011-01-14 05:31:45 +00001449 .d_delete = cgroup_delete,
Al Viro0df6a632010-12-21 13:29:29 -05001450 };
1451
Paul Menageddbcc7e2007-10-18 23:39:30 -07001452 struct inode *inode =
1453 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
1454 struct dentry *dentry;
1455
1456 if (!inode)
1457 return -ENOMEM;
1458
Paul Menageddbcc7e2007-10-18 23:39:30 -07001459 inode->i_fop = &simple_dir_operations;
1460 inode->i_op = &cgroup_dir_inode_operations;
1461 /* directories start off with i_nlink == 2 (for "." entry) */
1462 inc_nlink(inode);
1463 dentry = d_alloc_root(inode);
1464 if (!dentry) {
1465 iput(inode);
1466 return -ENOMEM;
1467 }
1468 sb->s_root = dentry;
Al Viro0df6a632010-12-21 13:29:29 -05001469 /* for everything else we want ->d_op set */
1470 sb->s_d_op = &cgroup_dops;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001471 return 0;
1472}
1473
Al Virof7e83572010-07-26 13:23:11 +04001474static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001475 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001476 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001477{
1478 struct cgroup_sb_opts opts;
Paul Menagec6d57f32009-09-23 15:56:19 -07001479 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001480 int ret = 0;
1481 struct super_block *sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001482 struct cgroupfs_root *new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001483
1484 /* First find the desired set of subsystems */
Ben Blumaae8aab2010-03-10 15:22:07 -08001485 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001486 ret = parse_cgroupfs_options(data, &opts);
Ben Blumaae8aab2010-03-10 15:22:07 -08001487 mutex_unlock(&cgroup_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001488 if (ret)
1489 goto out_err;
1490
1491 /*
1492 * Allocate a new cgroup root. We may not need it if we're
1493 * reusing an existing hierarchy.
1494 */
1495 new_root = cgroup_root_from_opts(&opts);
1496 if (IS_ERR(new_root)) {
1497 ret = PTR_ERR(new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001498 goto drop_modules;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001499 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001500 opts.new_root = new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001501
Paul Menagec6d57f32009-09-23 15:56:19 -07001502 /* Locate an existing or new sb for this hierarchy */
1503 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001504 if (IS_ERR(sb)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001505 ret = PTR_ERR(sb);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001506 cgroup_drop_root(opts.new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001507 goto drop_modules;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001508 }
1509
Paul Menagec6d57f32009-09-23 15:56:19 -07001510 root = sb->s_fs_info;
1511 BUG_ON(!root);
1512 if (root == opts.new_root) {
1513 /* We used the new root structure, so this is a new hierarchy */
1514 struct list_head tmp_cg_links;
Li Zefanc12f65d2009-01-07 18:07:42 -08001515 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menage817929e2007-10-18 23:39:36 -07001516 struct inode *inode;
Paul Menagec6d57f32009-09-23 15:56:19 -07001517 struct cgroupfs_root *existing_root;
Li Zefan28fd5df2008-04-29 01:00:13 -07001518 int i;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001519
1520 BUG_ON(sb->s_root != NULL);
1521
1522 ret = cgroup_get_rootdir(sb);
1523 if (ret)
1524 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001525 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001526
Paul Menage817929e2007-10-18 23:39:36 -07001527 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001528 mutex_lock(&cgroup_mutex);
1529
Paul Menagec6d57f32009-09-23 15:56:19 -07001530 if (strlen(root->name)) {
1531 /* Check for name clashes with existing mounts */
1532 for_each_active_root(existing_root) {
1533 if (!strcmp(existing_root->name, root->name)) {
1534 ret = -EBUSY;
1535 mutex_unlock(&cgroup_mutex);
1536 mutex_unlock(&inode->i_mutex);
1537 goto drop_new_super;
1538 }
1539 }
1540 }
1541
Paul Menage817929e2007-10-18 23:39:36 -07001542 /*
1543 * We're accessing css_set_count without locking
1544 * css_set_lock here, but that's OK - it can only be
1545 * increased by someone holding cgroup_lock, and
1546 * that's us. The worst that can happen is that we
1547 * have some link structures left over
1548 */
1549 ret = allocate_cg_links(css_set_count, &tmp_cg_links);
1550 if (ret) {
1551 mutex_unlock(&cgroup_mutex);
1552 mutex_unlock(&inode->i_mutex);
1553 goto drop_new_super;
1554 }
1555
Paul Menageddbcc7e2007-10-18 23:39:30 -07001556 ret = rebind_subsystems(root, root->subsys_bits);
1557 if (ret == -EBUSY) {
1558 mutex_unlock(&cgroup_mutex);
Paul Menage817929e2007-10-18 23:39:36 -07001559 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001560 free_cg_links(&tmp_cg_links);
1561 goto drop_new_super;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001562 }
Ben Blumcf5d5942010-03-10 15:22:09 -08001563 /*
1564 * There must be no failure case after here, since rebinding
1565 * takes care of subsystems' refcounts, which are explicitly
1566 * dropped in the failure exit path.
1567 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001568
1569 /* EBUSY should be the only error here */
1570 BUG_ON(ret);
1571
1572 list_add(&root->root_list, &roots);
Paul Menage817929e2007-10-18 23:39:36 -07001573 root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001574
Li Zefanc12f65d2009-01-07 18:07:42 -08001575 sb->s_root->d_fsdata = root_cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001576 root->top_cgroup.dentry = sb->s_root;
1577
Paul Menage817929e2007-10-18 23:39:36 -07001578 /* Link the top cgroup in this hierarchy into all
1579 * the css_set objects */
1580 write_lock(&css_set_lock);
Li Zefan28fd5df2008-04-29 01:00:13 -07001581 for (i = 0; i < CSS_SET_TABLE_SIZE; i++) {
1582 struct hlist_head *hhead = &css_set_table[i];
1583 struct hlist_node *node;
Paul Menage817929e2007-10-18 23:39:36 -07001584 struct css_set *cg;
Li Zefan28fd5df2008-04-29 01:00:13 -07001585
Li Zefanc12f65d2009-01-07 18:07:42 -08001586 hlist_for_each_entry(cg, node, hhead, hlist)
1587 link_css_set(&tmp_cg_links, cg, root_cgrp);
Li Zefan28fd5df2008-04-29 01:00:13 -07001588 }
Paul Menage817929e2007-10-18 23:39:36 -07001589 write_unlock(&css_set_lock);
1590
1591 free_cg_links(&tmp_cg_links);
1592
Li Zefanc12f65d2009-01-07 18:07:42 -08001593 BUG_ON(!list_empty(&root_cgrp->sibling));
1594 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001595 BUG_ON(root->number_of_cgroups != 1);
1596
Li Zefanc12f65d2009-01-07 18:07:42 -08001597 cgroup_populate_dir(root_cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001598 mutex_unlock(&cgroup_mutex);
Xiaotian Feng34f77a92009-09-23 15:56:18 -07001599 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001600 } else {
1601 /*
1602 * We re-used an existing hierarchy - the new root (if
1603 * any) is not needed
1604 */
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001605 cgroup_drop_root(opts.new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001606 /* no subsys rebinding, so refcounts don't change */
1607 drop_parsed_module_refcounts(opts.subsys_bits);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001608 }
1609
Paul Menagec6d57f32009-09-23 15:56:19 -07001610 kfree(opts.release_agent);
1611 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001612 return dget(sb->s_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001613
1614 drop_new_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04001615 deactivate_locked_super(sb);
Ben Blumcf5d5942010-03-10 15:22:09 -08001616 drop_modules:
1617 drop_parsed_module_refcounts(opts.subsys_bits);
Paul Menagec6d57f32009-09-23 15:56:19 -07001618 out_err:
1619 kfree(opts.release_agent);
1620 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001621 return ERR_PTR(ret);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001622}
1623
1624static void cgroup_kill_sb(struct super_block *sb) {
1625 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001626 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001627 int ret;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001628 struct cg_cgroup_link *link;
1629 struct cg_cgroup_link *saved_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001630
1631 BUG_ON(!root);
1632
1633 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001634 BUG_ON(!list_empty(&cgrp->children));
1635 BUG_ON(!list_empty(&cgrp->sibling));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001636
1637 mutex_lock(&cgroup_mutex);
1638
1639 /* Rebind all subsystems back to the default hierarchy */
1640 ret = rebind_subsystems(root, 0);
1641 /* Shouldn't be able to fail ... */
1642 BUG_ON(ret);
1643
Paul Menage817929e2007-10-18 23:39:36 -07001644 /*
1645 * Release all the links from css_sets to this hierarchy's
1646 * root cgroup
1647 */
1648 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001649
1650 list_for_each_entry_safe(link, saved_link, &cgrp->css_sets,
1651 cgrp_link_list) {
Paul Menage817929e2007-10-18 23:39:36 -07001652 list_del(&link->cg_link_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07001653 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07001654 kfree(link);
1655 }
1656 write_unlock(&css_set_lock);
1657
Paul Menage839ec542009-01-29 14:25:22 -08001658 if (!list_empty(&root->root_list)) {
1659 list_del(&root->root_list);
1660 root_count--;
1661 }
Li Zefane5f6a862009-01-07 18:07:41 -08001662
Paul Menageddbcc7e2007-10-18 23:39:30 -07001663 mutex_unlock(&cgroup_mutex);
1664
Paul Menageddbcc7e2007-10-18 23:39:30 -07001665 kill_litter_super(sb);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001666 cgroup_drop_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001667}
1668
1669static struct file_system_type cgroup_fs_type = {
1670 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001671 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001672 .kill_sb = cgroup_kill_sb,
1673};
1674
Greg KH676db4a2010-08-05 13:53:35 -07001675static struct kobject *cgroup_kobj;
1676
Paul Menagebd89aab2007-10-18 23:40:44 -07001677static inline struct cgroup *__d_cgrp(struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001678{
1679 return dentry->d_fsdata;
1680}
1681
1682static inline struct cftype *__d_cft(struct dentry *dentry)
1683{
1684 return dentry->d_fsdata;
1685}
1686
Li Zefana043e3b2008-02-23 15:24:09 -08001687/**
1688 * cgroup_path - generate the path of a cgroup
1689 * @cgrp: the cgroup in question
1690 * @buf: the buffer to write the path into
1691 * @buflen: the length of the buffer
1692 *
Paul Menagea47295e2009-01-07 18:07:44 -08001693 * Called with cgroup_mutex held or else with an RCU-protected cgroup
1694 * reference. Writes path of cgroup into buf. Returns 0 on success,
1695 * -errno on error.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001696 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001697int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001698{
1699 char *start;
Li Zefan9a9686b2010-04-22 17:29:24 +08001700 struct dentry *dentry = rcu_dereference_check(cgrp->dentry,
1701 rcu_read_lock_held() ||
1702 cgroup_lock_is_held());
Paul Menageddbcc7e2007-10-18 23:39:30 -07001703
Paul Menagea47295e2009-01-07 18:07:44 -08001704 if (!dentry || cgrp == dummytop) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001705 /*
1706 * Inactive subsystems have no dentry for their root
1707 * cgroup
1708 */
1709 strcpy(buf, "/");
1710 return 0;
1711 }
1712
1713 start = buf + buflen;
1714
1715 *--start = '\0';
1716 for (;;) {
Paul Menagea47295e2009-01-07 18:07:44 -08001717 int len = dentry->d_name.len;
Li Zefan9a9686b2010-04-22 17:29:24 +08001718
Paul Menageddbcc7e2007-10-18 23:39:30 -07001719 if ((start -= len) < buf)
1720 return -ENAMETOOLONG;
Li Zefan9a9686b2010-04-22 17:29:24 +08001721 memcpy(start, dentry->d_name.name, len);
Paul Menagebd89aab2007-10-18 23:40:44 -07001722 cgrp = cgrp->parent;
1723 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001724 break;
Li Zefan9a9686b2010-04-22 17:29:24 +08001725
1726 dentry = rcu_dereference_check(cgrp->dentry,
1727 rcu_read_lock_held() ||
1728 cgroup_lock_is_held());
Paul Menagebd89aab2007-10-18 23:40:44 -07001729 if (!cgrp->parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001730 continue;
1731 if (--start < buf)
1732 return -ENAMETOOLONG;
1733 *start = '/';
1734 }
1735 memmove(buf, start, buf + buflen - start);
1736 return 0;
1737}
Ben Blum67523c42010-03-10 15:22:11 -08001738EXPORT_SYMBOL_GPL(cgroup_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001739
Ben Blum74a11662011-05-26 16:25:20 -07001740/*
1741 * cgroup_task_migrate - move a task from one cgroup to another.
1742 *
1743 * 'guarantee' is set if the caller promises that a new css_set for the task
1744 * will already exist. If not set, this function might sleep, and can fail with
1745 * -ENOMEM. Otherwise, it can only fail with -ESRCH.
1746 */
1747static int cgroup_task_migrate(struct cgroup *cgrp, struct cgroup *oldcgrp,
1748 struct task_struct *tsk, bool guarantee)
1749{
1750 struct css_set *oldcg;
1751 struct css_set *newcg;
1752
1753 /*
1754 * get old css_set. we need to take task_lock and refcount it, because
1755 * an exiting task can change its css_set to init_css_set and drop its
1756 * old one without taking cgroup_mutex.
1757 */
1758 task_lock(tsk);
1759 oldcg = tsk->cgroups;
1760 get_css_set(oldcg);
1761 task_unlock(tsk);
1762
1763 /* locate or allocate a new css_set for this task. */
1764 if (guarantee) {
1765 /* we know the css_set we want already exists. */
1766 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT];
1767 read_lock(&css_set_lock);
1768 newcg = find_existing_css_set(oldcg, cgrp, template);
1769 BUG_ON(!newcg);
1770 get_css_set(newcg);
1771 read_unlock(&css_set_lock);
1772 } else {
1773 might_sleep();
1774 /* find_css_set will give us newcg already referenced. */
1775 newcg = find_css_set(oldcg, cgrp);
1776 if (!newcg) {
1777 put_css_set(oldcg);
1778 return -ENOMEM;
1779 }
1780 }
1781 put_css_set(oldcg);
1782
1783 /* if PF_EXITING is set, the tsk->cgroups pointer is no longer safe. */
1784 task_lock(tsk);
1785 if (tsk->flags & PF_EXITING) {
1786 task_unlock(tsk);
1787 put_css_set(newcg);
1788 return -ESRCH;
1789 }
1790 rcu_assign_pointer(tsk->cgroups, newcg);
1791 task_unlock(tsk);
1792
1793 /* Update the css_set linked lists if we're using them */
1794 write_lock(&css_set_lock);
1795 if (!list_empty(&tsk->cg_list))
1796 list_move(&tsk->cg_list, &newcg->tasks);
1797 write_unlock(&css_set_lock);
1798
1799 /*
1800 * We just gained a reference on oldcg by taking it from the task. As
1801 * trading it for newcg is protected by cgroup_mutex, we're safe to drop
1802 * it here; it will be freed under RCU.
1803 */
1804 put_css_set(oldcg);
1805
1806 set_bit(CGRP_RELEASABLE, &oldcgrp->flags);
1807 return 0;
1808}
1809
Li Zefana043e3b2008-02-23 15:24:09 -08001810/**
1811 * cgroup_attach_task - attach task 'tsk' to cgroup 'cgrp'
1812 * @cgrp: the cgroup the task is attaching to
1813 * @tsk: the task to be attached
Paul Menagebbcb81d2007-10-18 23:39:32 -07001814 *
Li Zefana043e3b2008-02-23 15:24:09 -08001815 * Call holding cgroup_mutex. May take task_lock of
1816 * the task 'tsk' during call.
Paul Menagebbcb81d2007-10-18 23:39:32 -07001817 */
Cliff Wickman956db3c2008-02-07 00:14:43 -08001818int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001819{
Ben Blum74a11662011-05-26 16:25:20 -07001820 int retval;
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001821 struct cgroup_subsys *ss, *failed_ss = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07001822 struct cgroup *oldcgrp;
Paul Menagebd89aab2007-10-18 23:40:44 -07001823 struct cgroupfs_root *root = cgrp->root;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001824
1825 /* Nothing to do if the task is already in that cgroup */
Paul Menage7717f7b2009-09-23 15:56:22 -07001826 oldcgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07001827 if (cgrp == oldcgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001828 return 0;
1829
1830 for_each_subsys(root, ss) {
1831 if (ss->can_attach) {
Ben Blumf780bdb2011-05-26 16:25:19 -07001832 retval = ss->can_attach(ss, cgrp, tsk);
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001833 if (retval) {
1834 /*
1835 * Remember on which subsystem the can_attach()
1836 * failed, so that we only call cancel_attach()
1837 * against the subsystems whose can_attach()
1838 * succeeded. (See below)
1839 */
1840 failed_ss = ss;
1841 goto out;
1842 }
San Mehat1d38bc72009-05-21 14:10:06 -07001843 } else if (!capable(CAP_SYS_ADMIN)) {
1844 const struct cred *cred = current_cred(), *tcred;
1845
1846 /* No can_attach() - check perms generically */
1847 tcred = __task_cred(tsk);
1848 if (cred->euid != tcred->uid &&
1849 cred->euid != tcred->suid) {
1850 return -EACCES;
1851 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07001852 }
Ben Blumf780bdb2011-05-26 16:25:19 -07001853 if (ss->can_attach_task) {
1854 retval = ss->can_attach_task(cgrp, tsk);
1855 if (retval) {
1856 failed_ss = ss;
1857 goto out;
1858 }
1859 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07001860 }
1861
Ben Blum74a11662011-05-26 16:25:20 -07001862 retval = cgroup_task_migrate(cgrp, oldcgrp, tsk, false);
1863 if (retval)
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001864 goto out;
Paul Menage817929e2007-10-18 23:39:36 -07001865
Paul Menagebbcb81d2007-10-18 23:39:32 -07001866 for_each_subsys(root, ss) {
Ben Blumf780bdb2011-05-26 16:25:19 -07001867 if (ss->pre_attach)
1868 ss->pre_attach(cgrp);
1869 if (ss->attach_task)
1870 ss->attach_task(cgrp, tsk);
Paul Jacksone18f6312008-02-07 00:13:44 -08001871 if (ss->attach)
Ben Blumf780bdb2011-05-26 16:25:19 -07001872 ss->attach(ss, cgrp, oldcgrp, tsk);
Paul Menagebbcb81d2007-10-18 23:39:32 -07001873 }
Ben Blum74a11662011-05-26 16:25:20 -07001874
Paul Menagebbcb81d2007-10-18 23:39:32 -07001875 synchronize_rcu();
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07001876
1877 /*
1878 * wake up rmdir() waiter. the rmdir should fail since the cgroup
1879 * is no longer empty.
1880 */
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07001881 cgroup_wakeup_rmdir_waiter(cgrp);
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001882out:
1883 if (retval) {
1884 for_each_subsys(root, ss) {
1885 if (ss == failed_ss)
1886 /*
1887 * This subsystem was the one that failed the
1888 * can_attach() check earlier, so we don't need
1889 * to call cancel_attach() against it or any
1890 * remaining subsystems.
1891 */
1892 break;
1893 if (ss->cancel_attach)
Ben Blumf780bdb2011-05-26 16:25:19 -07001894 ss->cancel_attach(ss, cgrp, tsk);
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001895 }
1896 }
1897 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001898}
1899
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02001900/**
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -07001901 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
1902 * @from: attach to all cgroups of a given task
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02001903 * @tsk: the task to be attached
1904 */
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -07001905int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02001906{
1907 struct cgroupfs_root *root;
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02001908 int retval = 0;
1909
1910 cgroup_lock();
1911 for_each_active_root(root) {
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -07001912 struct cgroup *from_cg = task_cgroup_from_root(from, root);
1913
1914 retval = cgroup_attach_task(from_cg, tsk);
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02001915 if (retval)
1916 break;
1917 }
1918 cgroup_unlock();
1919
1920 return retval;
1921}
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -07001922EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02001923
Paul Menagebbcb81d2007-10-18 23:39:32 -07001924/*
Ben Blum74a11662011-05-26 16:25:20 -07001925 * cgroup_attach_proc works in two stages, the first of which prefetches all
1926 * new css_sets needed (to make sure we have enough memory before committing
1927 * to the move) and stores them in a list of entries of the following type.
1928 * TODO: possible optimization: use css_set->rcu_head for chaining instead
Paul Menagebbcb81d2007-10-18 23:39:32 -07001929 */
Ben Blum74a11662011-05-26 16:25:20 -07001930struct cg_list_entry {
1931 struct css_set *cg;
1932 struct list_head links;
1933};
1934
1935static bool css_set_check_fetched(struct cgroup *cgrp,
1936 struct task_struct *tsk, struct css_set *cg,
1937 struct list_head *newcg_list)
1938{
1939 struct css_set *newcg;
1940 struct cg_list_entry *cg_entry;
1941 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT];
1942
1943 read_lock(&css_set_lock);
1944 newcg = find_existing_css_set(cg, cgrp, template);
1945 if (newcg)
1946 get_css_set(newcg);
1947 read_unlock(&css_set_lock);
1948
1949 /* doesn't exist at all? */
1950 if (!newcg)
1951 return false;
1952 /* see if it's already in the list */
1953 list_for_each_entry(cg_entry, newcg_list, links) {
1954 if (cg_entry->cg == newcg) {
1955 put_css_set(newcg);
1956 return true;
1957 }
1958 }
1959
1960 /* not found */
1961 put_css_set(newcg);
1962 return false;
1963}
1964
1965/*
1966 * Find the new css_set and store it in the list in preparation for moving the
1967 * given task to the given cgroup. Returns 0 or -ENOMEM.
1968 */
1969static int css_set_prefetch(struct cgroup *cgrp, struct css_set *cg,
1970 struct list_head *newcg_list)
1971{
1972 struct css_set *newcg;
1973 struct cg_list_entry *cg_entry;
1974
1975 /* ensure a new css_set will exist for this thread */
1976 newcg = find_css_set(cg, cgrp);
1977 if (!newcg)
1978 return -ENOMEM;
1979 /* add it to the list */
1980 cg_entry = kmalloc(sizeof(struct cg_list_entry), GFP_KERNEL);
1981 if (!cg_entry) {
1982 put_css_set(newcg);
1983 return -ENOMEM;
1984 }
1985 cg_entry->cg = newcg;
1986 list_add(&cg_entry->links, newcg_list);
1987 return 0;
1988}
1989
1990/**
1991 * cgroup_attach_proc - attach all threads in a threadgroup to a cgroup
1992 * @cgrp: the cgroup to attach to
1993 * @leader: the threadgroup leader task_struct of the group to be attached
1994 *
1995 * Call holding cgroup_mutex and the threadgroup_fork_lock of the leader. Will
1996 * take task_lock of each thread in leader's threadgroup individually in turn.
1997 */
1998int cgroup_attach_proc(struct cgroup *cgrp, struct task_struct *leader)
1999{
2000 int retval, i, group_size;
2001 struct cgroup_subsys *ss, *failed_ss = NULL;
2002 bool cancel_failed_ss = false;
2003 /* guaranteed to be initialized later, but the compiler needs this */
2004 struct cgroup *oldcgrp = NULL;
2005 struct css_set *oldcg;
2006 struct cgroupfs_root *root = cgrp->root;
2007 /* threadgroup list cursor and array */
2008 struct task_struct *tsk;
Ben Blumd8466872011-05-26 16:25:21 -07002009 struct flex_array *group;
Ben Blum74a11662011-05-26 16:25:20 -07002010 /*
2011 * we need to make sure we have css_sets for all the tasks we're
2012 * going to move -before- we actually start moving them, so that in
2013 * case we get an ENOMEM we can bail out before making any changes.
2014 */
2015 struct list_head newcg_list;
2016 struct cg_list_entry *cg_entry, *temp_nobe;
2017
2018 /*
2019 * step 0: in order to do expensive, possibly blocking operations for
2020 * every thread, we cannot iterate the thread group list, since it needs
2021 * rcu or tasklist locked. instead, build an array of all threads in the
2022 * group - threadgroup_fork_lock prevents new threads from appearing,
2023 * and if threads exit, this will just be an over-estimate.
2024 */
2025 group_size = get_nr_threads(leader);
Ben Blumd8466872011-05-26 16:25:21 -07002026 /* flex_array supports very large thread-groups better than kmalloc. */
2027 group = flex_array_alloc(sizeof(struct task_struct *), group_size,
2028 GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07002029 if (!group)
2030 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07002031 /* pre-allocate to guarantee space while iterating in rcu read-side. */
2032 retval = flex_array_prealloc(group, 0, group_size - 1, GFP_KERNEL);
2033 if (retval)
2034 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07002035
2036 /* prevent changes to the threadgroup list while we take a snapshot. */
2037 rcu_read_lock();
2038 if (!thread_group_leader(leader)) {
2039 /*
2040 * a race with de_thread from another thread's exec() may strip
2041 * us of our leadership, making while_each_thread unsafe to use
2042 * on this task. if this happens, there is no choice but to
2043 * throw this task away and try again (from cgroup_procs_write);
2044 * this is "double-double-toil-and-trouble-check locking".
2045 */
2046 rcu_read_unlock();
2047 retval = -EAGAIN;
2048 goto out_free_group_list;
2049 }
2050 /* take a reference on each task in the group to go in the array. */
2051 tsk = leader;
2052 i = 0;
2053 do {
2054 /* as per above, nr_threads may decrease, but not increase. */
2055 BUG_ON(i >= group_size);
2056 get_task_struct(tsk);
Ben Blumd8466872011-05-26 16:25:21 -07002057 /*
2058 * saying GFP_ATOMIC has no effect here because we did prealloc
2059 * earlier, but it's good form to communicate our expectations.
2060 */
2061 retval = flex_array_put_ptr(group, i, tsk, GFP_ATOMIC);
2062 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07002063 i++;
2064 } while_each_thread(leader, tsk);
2065 /* remember the number of threads in the array for later. */
2066 group_size = i;
2067 rcu_read_unlock();
2068
2069 /*
2070 * step 1: check that we can legitimately attach to the cgroup.
2071 */
2072 for_each_subsys(root, ss) {
2073 if (ss->can_attach) {
2074 retval = ss->can_attach(ss, cgrp, leader);
2075 if (retval) {
2076 failed_ss = ss;
2077 goto out_cancel_attach;
2078 }
2079 }
2080 /* a callback to be run on every thread in the threadgroup. */
2081 if (ss->can_attach_task) {
2082 /* run on each task in the threadgroup. */
2083 for (i = 0; i < group_size; i++) {
Ben Blumd8466872011-05-26 16:25:21 -07002084 tsk = flex_array_get_ptr(group, i);
2085 retval = ss->can_attach_task(cgrp, tsk);
Ben Blum74a11662011-05-26 16:25:20 -07002086 if (retval) {
2087 failed_ss = ss;
2088 cancel_failed_ss = true;
2089 goto out_cancel_attach;
2090 }
2091 }
2092 }
2093 }
2094
2095 /*
2096 * step 2: make sure css_sets exist for all threads to be migrated.
2097 * we use find_css_set, which allocates a new one if necessary.
2098 */
2099 INIT_LIST_HEAD(&newcg_list);
2100 for (i = 0; i < group_size; i++) {
Ben Blumd8466872011-05-26 16:25:21 -07002101 tsk = flex_array_get_ptr(group, i);
Ben Blum74a11662011-05-26 16:25:20 -07002102 /* nothing to do if this task is already in the cgroup */
2103 oldcgrp = task_cgroup_from_root(tsk, root);
2104 if (cgrp == oldcgrp)
2105 continue;
2106 /* get old css_set pointer */
2107 task_lock(tsk);
2108 if (tsk->flags & PF_EXITING) {
2109 /* ignore this task if it's going away */
2110 task_unlock(tsk);
2111 continue;
2112 }
2113 oldcg = tsk->cgroups;
2114 get_css_set(oldcg);
2115 task_unlock(tsk);
2116 /* see if the new one for us is already in the list? */
2117 if (css_set_check_fetched(cgrp, tsk, oldcg, &newcg_list)) {
2118 /* was already there, nothing to do. */
2119 put_css_set(oldcg);
2120 } else {
2121 /* we don't already have it. get new one. */
2122 retval = css_set_prefetch(cgrp, oldcg, &newcg_list);
2123 put_css_set(oldcg);
2124 if (retval)
2125 goto out_list_teardown;
2126 }
2127 }
2128
2129 /*
2130 * step 3: now that we're guaranteed success wrt the css_sets, proceed
2131 * to move all tasks to the new cgroup, calling ss->attach_task for each
2132 * one along the way. there are no failure cases after here, so this is
2133 * the commit point.
2134 */
2135 for_each_subsys(root, ss) {
2136 if (ss->pre_attach)
2137 ss->pre_attach(cgrp);
2138 }
2139 for (i = 0; i < group_size; i++) {
Ben Blumd8466872011-05-26 16:25:21 -07002140 tsk = flex_array_get_ptr(group, i);
Ben Blum74a11662011-05-26 16:25:20 -07002141 /* leave current thread as it is if it's already there */
2142 oldcgrp = task_cgroup_from_root(tsk, root);
2143 if (cgrp == oldcgrp)
2144 continue;
2145 /* attach each task to each subsystem */
2146 for_each_subsys(root, ss) {
2147 if (ss->attach_task)
2148 ss->attach_task(cgrp, tsk);
2149 }
2150 /* if the thread is PF_EXITING, it can just get skipped. */
2151 retval = cgroup_task_migrate(cgrp, oldcgrp, tsk, true);
2152 BUG_ON(retval != 0 && retval != -ESRCH);
2153 }
2154 /* nothing is sensitive to fork() after this point. */
2155
2156 /*
2157 * step 4: do expensive, non-thread-specific subsystem callbacks.
2158 * TODO: if ever a subsystem needs to know the oldcgrp for each task
2159 * being moved, this call will need to be reworked to communicate that.
2160 */
2161 for_each_subsys(root, ss) {
2162 if (ss->attach)
2163 ss->attach(ss, cgrp, oldcgrp, leader);
2164 }
2165
2166 /*
2167 * step 5: success! and cleanup
2168 */
2169 synchronize_rcu();
2170 cgroup_wakeup_rmdir_waiter(cgrp);
2171 retval = 0;
2172out_list_teardown:
2173 /* clean up the list of prefetched css_sets. */
2174 list_for_each_entry_safe(cg_entry, temp_nobe, &newcg_list, links) {
2175 list_del(&cg_entry->links);
2176 put_css_set(cg_entry->cg);
2177 kfree(cg_entry);
2178 }
2179out_cancel_attach:
2180 /* same deal as in cgroup_attach_task */
2181 if (retval) {
2182 for_each_subsys(root, ss) {
2183 if (ss == failed_ss) {
2184 if (cancel_failed_ss && ss->cancel_attach)
2185 ss->cancel_attach(ss, cgrp, leader);
2186 break;
2187 }
2188 if (ss->cancel_attach)
2189 ss->cancel_attach(ss, cgrp, leader);
2190 }
2191 }
2192 /* clean up the array of referenced threads in the group. */
Ben Blumd8466872011-05-26 16:25:21 -07002193 for (i = 0; i < group_size; i++) {
2194 tsk = flex_array_get_ptr(group, i);
2195 put_task_struct(tsk);
2196 }
Ben Blum74a11662011-05-26 16:25:20 -07002197out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07002198 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07002199 return retval;
2200}
2201
2202/*
2203 * Find the task_struct of the task to attach by vpid and pass it along to the
2204 * function to attach either it or all tasks in its threadgroup. Will take
2205 * cgroup_mutex; may take task_lock of task.
2206 */
2207static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002208{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002209 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002210 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002211 int ret;
2212
Ben Blum74a11662011-05-26 16:25:20 -07002213 if (!cgroup_lock_live_group(cgrp))
2214 return -ENODEV;
2215
Paul Menagebbcb81d2007-10-18 23:39:32 -07002216 if (pid) {
2217 rcu_read_lock();
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002218 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002219 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002220 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07002221 cgroup_unlock();
2222 return -ESRCH;
2223 }
2224 if (threadgroup) {
2225 /*
2226 * RCU protects this access, since tsk was found in the
2227 * tid map. a race with de_thread may cause group_leader
2228 * to stop being the leader, but cgroup_attach_proc will
2229 * detect it later.
2230 */
2231 tsk = tsk->group_leader;
2232 } else if (tsk->flags & PF_EXITING) {
2233 /* optimization for the single-task-only case */
2234 rcu_read_unlock();
2235 cgroup_unlock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002236 return -ESRCH;
2237 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07002238
Ben Blum74a11662011-05-26 16:25:20 -07002239 /*
2240 * even if we're attaching all tasks in the thread group, we
2241 * only need to check permissions on one of them.
2242 */
David Howellsc69e8d92008-11-14 10:39:19 +11002243 tcred = __task_cred(tsk);
2244 if (cred->euid &&
2245 cred->euid != tcred->uid &&
2246 cred->euid != tcred->suid) {
2247 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07002248 cgroup_unlock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002249 return -EACCES;
2250 }
David Howellsc69e8d92008-11-14 10:39:19 +11002251 get_task_struct(tsk);
2252 rcu_read_unlock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002253 } else {
Ben Blum74a11662011-05-26 16:25:20 -07002254 if (threadgroup)
2255 tsk = current->group_leader;
2256 else
2257 tsk = current;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002258 get_task_struct(tsk);
2259 }
2260
Ben Blum74a11662011-05-26 16:25:20 -07002261 if (threadgroup) {
2262 threadgroup_fork_write_lock(tsk);
2263 ret = cgroup_attach_proc(cgrp, tsk);
2264 threadgroup_fork_write_unlock(tsk);
2265 } else {
2266 ret = cgroup_attach_task(cgrp, tsk);
2267 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07002268 put_task_struct(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07002269 cgroup_unlock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002270 return ret;
2271}
2272
Paul Menageaf351022008-07-25 01:47:01 -07002273static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid)
2274{
Ben Blum74a11662011-05-26 16:25:20 -07002275 return attach_task_by_pid(cgrp, pid, false);
2276}
2277
2278static int cgroup_procs_write(struct cgroup *cgrp, struct cftype *cft, u64 tgid)
2279{
Paul Menageaf351022008-07-25 01:47:01 -07002280 int ret;
Ben Blum74a11662011-05-26 16:25:20 -07002281 do {
2282 /*
2283 * attach_proc fails with -EAGAIN if threadgroup leadership
2284 * changes in the middle of the operation, in which case we need
2285 * to find the task_struct for the new leader and start over.
2286 */
2287 ret = attach_task_by_pid(cgrp, tgid, true);
2288 } while (ret == -EAGAIN);
Paul Menageaf351022008-07-25 01:47:01 -07002289 return ret;
2290}
2291
Paul Menagee788e062008-07-25 01:46:59 -07002292/**
2293 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
2294 * @cgrp: the cgroup to be checked for liveness
2295 *
Paul Menage84eea842008-07-25 01:47:00 -07002296 * On success, returns true; the lock should be later released with
2297 * cgroup_unlock(). On failure returns false with no lock held.
Paul Menagee788e062008-07-25 01:46:59 -07002298 */
Paul Menage84eea842008-07-25 01:47:00 -07002299bool cgroup_lock_live_group(struct cgroup *cgrp)
Paul Menagee788e062008-07-25 01:46:59 -07002300{
2301 mutex_lock(&cgroup_mutex);
2302 if (cgroup_is_removed(cgrp)) {
2303 mutex_unlock(&cgroup_mutex);
2304 return false;
2305 }
2306 return true;
2307}
Ben Blum67523c42010-03-10 15:22:11 -08002308EXPORT_SYMBOL_GPL(cgroup_lock_live_group);
Paul Menagee788e062008-07-25 01:46:59 -07002309
2310static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
2311 const char *buffer)
2312{
2313 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
Evgeny Kuznetsovf4a25892010-10-27 15:33:37 -07002314 if (strlen(buffer) >= PATH_MAX)
2315 return -EINVAL;
Paul Menagee788e062008-07-25 01:46:59 -07002316 if (!cgroup_lock_live_group(cgrp))
2317 return -ENODEV;
2318 strcpy(cgrp->root->release_agent_path, buffer);
Paul Menage84eea842008-07-25 01:47:00 -07002319 cgroup_unlock();
Paul Menagee788e062008-07-25 01:46:59 -07002320 return 0;
2321}
2322
2323static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
2324 struct seq_file *seq)
2325{
2326 if (!cgroup_lock_live_group(cgrp))
2327 return -ENODEV;
2328 seq_puts(seq, cgrp->root->release_agent_path);
2329 seq_putc(seq, '\n');
Paul Menage84eea842008-07-25 01:47:00 -07002330 cgroup_unlock();
Paul Menagee788e062008-07-25 01:46:59 -07002331 return 0;
2332}
2333
Paul Menage84eea842008-07-25 01:47:00 -07002334/* A buffer size big enough for numbers or short strings */
2335#define CGROUP_LOCAL_BUFFER_SIZE 64
2336
Paul Menagee73d2c62008-04-29 01:00:06 -07002337static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
Paul Menagef4c753b2008-04-29 00:59:56 -07002338 struct file *file,
2339 const char __user *userbuf,
2340 size_t nbytes, loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07002341{
Paul Menage84eea842008-07-25 01:47:00 -07002342 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menage355e0c42007-10-18 23:39:33 -07002343 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07002344 char *end;
2345
2346 if (!nbytes)
2347 return -EINVAL;
2348 if (nbytes >= sizeof(buffer))
2349 return -E2BIG;
2350 if (copy_from_user(buffer, userbuf, nbytes))
2351 return -EFAULT;
2352
2353 buffer[nbytes] = 0; /* nul-terminate */
Paul Menagee73d2c62008-04-29 01:00:06 -07002354 if (cft->write_u64) {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002355 u64 val = simple_strtoull(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002356 if (*end)
2357 return -EINVAL;
2358 retval = cft->write_u64(cgrp, cft, val);
2359 } else {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002360 s64 val = simple_strtoll(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002361 if (*end)
2362 return -EINVAL;
2363 retval = cft->write_s64(cgrp, cft, val);
2364 }
Paul Menage355e0c42007-10-18 23:39:33 -07002365 if (!retval)
2366 retval = nbytes;
2367 return retval;
2368}
2369
Paul Menagedb3b1492008-07-25 01:46:58 -07002370static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
2371 struct file *file,
2372 const char __user *userbuf,
2373 size_t nbytes, loff_t *unused_ppos)
2374{
Paul Menage84eea842008-07-25 01:47:00 -07002375 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagedb3b1492008-07-25 01:46:58 -07002376 int retval = 0;
2377 size_t max_bytes = cft->max_write_len;
2378 char *buffer = local_buffer;
2379
2380 if (!max_bytes)
2381 max_bytes = sizeof(local_buffer) - 1;
2382 if (nbytes >= max_bytes)
2383 return -E2BIG;
2384 /* Allocate a dynamic buffer if we need one */
2385 if (nbytes >= sizeof(local_buffer)) {
2386 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
2387 if (buffer == NULL)
2388 return -ENOMEM;
2389 }
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002390 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
2391 retval = -EFAULT;
2392 goto out;
2393 }
Paul Menagedb3b1492008-07-25 01:46:58 -07002394
2395 buffer[nbytes] = 0; /* nul-terminate */
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002396 retval = cft->write_string(cgrp, cft, strstrip(buffer));
Paul Menagedb3b1492008-07-25 01:46:58 -07002397 if (!retval)
2398 retval = nbytes;
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002399out:
Paul Menagedb3b1492008-07-25 01:46:58 -07002400 if (buffer != local_buffer)
2401 kfree(buffer);
2402 return retval;
2403}
2404
Paul Menageddbcc7e2007-10-18 23:39:30 -07002405static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
2406 size_t nbytes, loff_t *ppos)
2407{
2408 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002409 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002410
Li Zefan75139b82009-01-07 18:07:33 -08002411 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002412 return -ENODEV;
Paul Menage355e0c42007-10-18 23:39:33 -07002413 if (cft->write)
Paul Menagebd89aab2007-10-18 23:40:44 -07002414 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002415 if (cft->write_u64 || cft->write_s64)
2416 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagedb3b1492008-07-25 01:46:58 -07002417 if (cft->write_string)
2418 return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002419 if (cft->trigger) {
2420 int ret = cft->trigger(cgrp, (unsigned int)cft->private);
2421 return ret ? ret : nbytes;
2422 }
Paul Menage355e0c42007-10-18 23:39:33 -07002423 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002424}
2425
Paul Menagef4c753b2008-04-29 00:59:56 -07002426static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
2427 struct file *file,
2428 char __user *buf, size_t nbytes,
2429 loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002430{
Paul Menage84eea842008-07-25 01:47:00 -07002431 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagef4c753b2008-04-29 00:59:56 -07002432 u64 val = cft->read_u64(cgrp, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002433 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
2434
2435 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2436}
2437
Paul Menagee73d2c62008-04-29 01:00:06 -07002438static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
2439 struct file *file,
2440 char __user *buf, size_t nbytes,
2441 loff_t *ppos)
2442{
Paul Menage84eea842008-07-25 01:47:00 -07002443 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagee73d2c62008-04-29 01:00:06 -07002444 s64 val = cft->read_s64(cgrp, cft);
2445 int len = sprintf(tmp, "%lld\n", (long long) val);
2446
2447 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2448}
2449
Paul Menageddbcc7e2007-10-18 23:39:30 -07002450static ssize_t cgroup_file_read(struct file *file, char __user *buf,
2451 size_t nbytes, loff_t *ppos)
2452{
2453 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002454 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002455
Li Zefan75139b82009-01-07 18:07:33 -08002456 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002457 return -ENODEV;
2458
2459 if (cft->read)
Paul Menagebd89aab2007-10-18 23:40:44 -07002460 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07002461 if (cft->read_u64)
2462 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002463 if (cft->read_s64)
2464 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002465 return -EINVAL;
2466}
2467
Paul Menage91796562008-04-29 01:00:01 -07002468/*
2469 * seqfile ops/methods for returning structured data. Currently just
2470 * supports string->u64 maps, but can be extended in future.
2471 */
2472
2473struct cgroup_seqfile_state {
2474 struct cftype *cft;
2475 struct cgroup *cgroup;
2476};
2477
2478static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
2479{
2480 struct seq_file *sf = cb->state;
2481 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
2482}
2483
2484static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2485{
2486 struct cgroup_seqfile_state *state = m->private;
2487 struct cftype *cft = state->cft;
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002488 if (cft->read_map) {
2489 struct cgroup_map_cb cb = {
2490 .fill = cgroup_map_add,
2491 .state = m,
2492 };
2493 return cft->read_map(state->cgroup, cft, &cb);
2494 }
2495 return cft->read_seq_string(state->cgroup, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07002496}
2497
Adrian Bunk96930a62008-07-25 19:46:21 -07002498static int cgroup_seqfile_release(struct inode *inode, struct file *file)
Paul Menage91796562008-04-29 01:00:01 -07002499{
2500 struct seq_file *seq = file->private_data;
2501 kfree(seq->private);
2502 return single_release(inode, file);
2503}
2504
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002505static const struct file_operations cgroup_seqfile_operations = {
Paul Menage91796562008-04-29 01:00:01 -07002506 .read = seq_read,
Paul Menagee788e062008-07-25 01:46:59 -07002507 .write = cgroup_file_write,
Paul Menage91796562008-04-29 01:00:01 -07002508 .llseek = seq_lseek,
2509 .release = cgroup_seqfile_release,
2510};
2511
Paul Menageddbcc7e2007-10-18 23:39:30 -07002512static int cgroup_file_open(struct inode *inode, struct file *file)
2513{
2514 int err;
2515 struct cftype *cft;
2516
2517 err = generic_file_open(inode, file);
2518 if (err)
2519 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002520 cft = __d_cft(file->f_dentry);
Li Zefan75139b82009-01-07 18:07:33 -08002521
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002522 if (cft->read_map || cft->read_seq_string) {
Paul Menage91796562008-04-29 01:00:01 -07002523 struct cgroup_seqfile_state *state =
2524 kzalloc(sizeof(*state), GFP_USER);
2525 if (!state)
2526 return -ENOMEM;
2527 state->cft = cft;
2528 state->cgroup = __d_cgrp(file->f_dentry->d_parent);
2529 file->f_op = &cgroup_seqfile_operations;
2530 err = single_open(file, cgroup_seqfile_show, state);
2531 if (err < 0)
2532 kfree(state);
2533 } else if (cft->open)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002534 err = cft->open(inode, file);
2535 else
2536 err = 0;
2537
2538 return err;
2539}
2540
2541static int cgroup_file_release(struct inode *inode, struct file *file)
2542{
2543 struct cftype *cft = __d_cft(file->f_dentry);
2544 if (cft->release)
2545 return cft->release(inode, file);
2546 return 0;
2547}
2548
2549/*
2550 * cgroup_rename - Only allow simple rename of directories in place.
2551 */
2552static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2553 struct inode *new_dir, struct dentry *new_dentry)
2554{
2555 if (!S_ISDIR(old_dentry->d_inode->i_mode))
2556 return -ENOTDIR;
2557 if (new_dentry->d_inode)
2558 return -EEXIST;
2559 if (old_dir != new_dir)
2560 return -EIO;
2561 return simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2562}
2563
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002564static const struct file_operations cgroup_file_operations = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002565 .read = cgroup_file_read,
2566 .write = cgroup_file_write,
2567 .llseek = generic_file_llseek,
2568 .open = cgroup_file_open,
2569 .release = cgroup_file_release,
2570};
2571
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07002572static const struct inode_operations cgroup_dir_inode_operations = {
Al Viroc72a04e2011-01-14 05:31:45 +00002573 .lookup = cgroup_lookup,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002574 .mkdir = cgroup_mkdir,
2575 .rmdir = cgroup_rmdir,
2576 .rename = cgroup_rename,
2577};
2578
Al Viroc72a04e2011-01-14 05:31:45 +00002579static struct dentry *cgroup_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
2580{
2581 if (dentry->d_name.len > NAME_MAX)
2582 return ERR_PTR(-ENAMETOOLONG);
2583 d_add(dentry, NULL);
2584 return NULL;
2585}
2586
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002587/*
2588 * Check if a file is a control file
2589 */
2590static inline struct cftype *__file_cft(struct file *file)
2591{
2592 if (file->f_dentry->d_inode->i_fop != &cgroup_file_operations)
2593 return ERR_PTR(-EINVAL);
2594 return __d_cft(file->f_dentry);
2595}
2596
Nick Piggin5adcee12011-01-07 17:49:20 +11002597static int cgroup_create_file(struct dentry *dentry, mode_t mode,
2598 struct super_block *sb)
2599{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002600 struct inode *inode;
2601
2602 if (!dentry)
2603 return -ENOENT;
2604 if (dentry->d_inode)
2605 return -EEXIST;
2606
2607 inode = cgroup_new_inode(mode, sb);
2608 if (!inode)
2609 return -ENOMEM;
2610
2611 if (S_ISDIR(mode)) {
2612 inode->i_op = &cgroup_dir_inode_operations;
2613 inode->i_fop = &simple_dir_operations;
2614
2615 /* start off with i_nlink == 2 (for "." entry) */
2616 inc_nlink(inode);
2617
2618 /* start with the directory inode held, so that we can
2619 * populate it without racing with another mkdir */
Paul Menage817929e2007-10-18 23:39:36 -07002620 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002621 } else if (S_ISREG(mode)) {
2622 inode->i_size = 0;
2623 inode->i_fop = &cgroup_file_operations;
2624 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002625 d_instantiate(dentry, inode);
2626 dget(dentry); /* Extra count - pin the dentry in core */
2627 return 0;
2628}
2629
2630/*
Li Zefana043e3b2008-02-23 15:24:09 -08002631 * cgroup_create_dir - create a directory for an object.
2632 * @cgrp: the cgroup we create the directory for. It must have a valid
2633 * ->parent field. And we are going to fill its ->dentry field.
2634 * @dentry: dentry of the new cgroup
2635 * @mode: mode to set on new directory.
Paul Menageddbcc7e2007-10-18 23:39:30 -07002636 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002637static int cgroup_create_dir(struct cgroup *cgrp, struct dentry *dentry,
Li Zefan099fca32009-04-02 16:57:29 -07002638 mode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002639{
2640 struct dentry *parent;
2641 int error = 0;
2642
Paul Menagebd89aab2007-10-18 23:40:44 -07002643 parent = cgrp->parent->dentry;
2644 error = cgroup_create_file(dentry, S_IFDIR | mode, cgrp->root->sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002645 if (!error) {
Paul Menagebd89aab2007-10-18 23:40:44 -07002646 dentry->d_fsdata = cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002647 inc_nlink(parent->d_inode);
Paul Menagea47295e2009-01-07 18:07:44 -08002648 rcu_assign_pointer(cgrp->dentry, dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002649 dget(dentry);
2650 }
2651 dput(dentry);
2652
2653 return error;
2654}
2655
Li Zefan099fca32009-04-02 16:57:29 -07002656/**
2657 * cgroup_file_mode - deduce file mode of a control file
2658 * @cft: the control file in question
2659 *
2660 * returns cft->mode if ->mode is not 0
2661 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2662 * returns S_IRUGO if it has only a read handler
2663 * returns S_IWUSR if it has only a write hander
2664 */
2665static mode_t cgroup_file_mode(const struct cftype *cft)
2666{
2667 mode_t mode = 0;
2668
2669 if (cft->mode)
2670 return cft->mode;
2671
2672 if (cft->read || cft->read_u64 || cft->read_s64 ||
2673 cft->read_map || cft->read_seq_string)
2674 mode |= S_IRUGO;
2675
2676 if (cft->write || cft->write_u64 || cft->write_s64 ||
2677 cft->write_string || cft->trigger)
2678 mode |= S_IWUSR;
2679
2680 return mode;
2681}
2682
Paul Menagebd89aab2007-10-18 23:40:44 -07002683int cgroup_add_file(struct cgroup *cgrp,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002684 struct cgroup_subsys *subsys,
2685 const struct cftype *cft)
2686{
Paul Menagebd89aab2007-10-18 23:40:44 -07002687 struct dentry *dir = cgrp->dentry;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002688 struct dentry *dentry;
2689 int error;
Li Zefan099fca32009-04-02 16:57:29 -07002690 mode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002691
2692 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Paul Menagebd89aab2007-10-18 23:40:44 -07002693 if (subsys && !test_bit(ROOT_NOPREFIX, &cgrp->root->flags)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002694 strcpy(name, subsys->name);
2695 strcat(name, ".");
2696 }
2697 strcat(name, cft->name);
2698 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
2699 dentry = lookup_one_len(name, dir, strlen(name));
2700 if (!IS_ERR(dentry)) {
Li Zefan099fca32009-04-02 16:57:29 -07002701 mode = cgroup_file_mode(cft);
2702 error = cgroup_create_file(dentry, mode | S_IFREG,
Paul Menagebd89aab2007-10-18 23:40:44 -07002703 cgrp->root->sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002704 if (!error)
2705 dentry->d_fsdata = (void *)cft;
2706 dput(dentry);
2707 } else
2708 error = PTR_ERR(dentry);
2709 return error;
2710}
Ben Blume6a11052010-03-10 15:22:09 -08002711EXPORT_SYMBOL_GPL(cgroup_add_file);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002712
Paul Menagebd89aab2007-10-18 23:40:44 -07002713int cgroup_add_files(struct cgroup *cgrp,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002714 struct cgroup_subsys *subsys,
2715 const struct cftype cft[],
2716 int count)
2717{
2718 int i, err;
2719 for (i = 0; i < count; i++) {
Paul Menagebd89aab2007-10-18 23:40:44 -07002720 err = cgroup_add_file(cgrp, subsys, &cft[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002721 if (err)
2722 return err;
2723 }
2724 return 0;
2725}
Ben Blume6a11052010-03-10 15:22:09 -08002726EXPORT_SYMBOL_GPL(cgroup_add_files);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002727
Li Zefana043e3b2008-02-23 15:24:09 -08002728/**
2729 * cgroup_task_count - count the number of tasks in a cgroup.
2730 * @cgrp: the cgroup in question
2731 *
2732 * Return the number of tasks in the cgroup.
2733 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002734int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002735{
2736 int count = 0;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07002737 struct cg_cgroup_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002738
Paul Menage817929e2007-10-18 23:39:36 -07002739 read_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07002740 list_for_each_entry(link, &cgrp->css_sets, cgrp_link_list) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07002741 count += atomic_read(&link->cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002742 }
2743 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002744 return count;
2745}
2746
2747/*
Paul Menage817929e2007-10-18 23:39:36 -07002748 * Advance a list_head iterator. The iterator should be positioned at
2749 * the start of a css_set
2750 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002751static void cgroup_advance_iter(struct cgroup *cgrp,
Paul Menage7717f7b2009-09-23 15:56:22 -07002752 struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002753{
2754 struct list_head *l = it->cg_link;
2755 struct cg_cgroup_link *link;
2756 struct css_set *cg;
2757
2758 /* Advance to the next non-empty css_set */
2759 do {
2760 l = l->next;
Paul Menagebd89aab2007-10-18 23:40:44 -07002761 if (l == &cgrp->css_sets) {
Paul Menage817929e2007-10-18 23:39:36 -07002762 it->cg_link = NULL;
2763 return;
2764 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002765 link = list_entry(l, struct cg_cgroup_link, cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07002766 cg = link->cg;
2767 } while (list_empty(&cg->tasks));
2768 it->cg_link = l;
2769 it->task = cg->tasks.next;
2770}
2771
Cliff Wickman31a7df02008-02-07 00:14:42 -08002772/*
2773 * To reduce the fork() overhead for systems that are not actually
2774 * using their cgroups capability, we don't maintain the lists running
2775 * through each css_set to its tasks until we see the list actually
2776 * used - in other words after the first call to cgroup_iter_start().
2777 *
2778 * The tasklist_lock is not held here, as do_each_thread() and
2779 * while_each_thread() are protected by RCU.
2780 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07002781static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002782{
2783 struct task_struct *p, *g;
2784 write_lock(&css_set_lock);
2785 use_task_css_set_links = 1;
2786 do_each_thread(g, p) {
2787 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08002788 /*
2789 * We should check if the process is exiting, otherwise
2790 * it will race with cgroup_exit() in that the list
2791 * entry won't be deleted though the process has exited.
2792 */
2793 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Cliff Wickman31a7df02008-02-07 00:14:42 -08002794 list_add(&p->cg_list, &p->cgroups->tasks);
2795 task_unlock(p);
2796 } while_each_thread(g, p);
2797 write_unlock(&css_set_lock);
2798}
2799
Paul Menagebd89aab2007-10-18 23:40:44 -07002800void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002801{
2802 /*
2803 * The first time anyone tries to iterate across a cgroup,
2804 * we need to enable the list linking each css_set to its
2805 * tasks, and fix up all existing tasks.
2806 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08002807 if (!use_task_css_set_links)
2808 cgroup_enable_task_cg_lists();
2809
Paul Menage817929e2007-10-18 23:39:36 -07002810 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07002811 it->cg_link = &cgrp->css_sets;
2812 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07002813}
2814
Paul Menagebd89aab2007-10-18 23:40:44 -07002815struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -07002816 struct cgroup_iter *it)
2817{
2818 struct task_struct *res;
2819 struct list_head *l = it->task;
Lai Jiangshan2019f632009-01-07 18:07:36 -08002820 struct cg_cgroup_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07002821
2822 /* If the iterator cg is NULL, we have no tasks */
2823 if (!it->cg_link)
2824 return NULL;
2825 res = list_entry(l, struct task_struct, cg_list);
2826 /* Advance iterator to find next entry */
2827 l = l->next;
Lai Jiangshan2019f632009-01-07 18:07:36 -08002828 link = list_entry(it->cg_link, struct cg_cgroup_link, cgrp_link_list);
2829 if (l == &link->cg->tasks) {
Paul Menage817929e2007-10-18 23:39:36 -07002830 /* We reached the end of this task list - move on to
2831 * the next cg_cgroup_link */
Paul Menagebd89aab2007-10-18 23:40:44 -07002832 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07002833 } else {
2834 it->task = l;
2835 }
2836 return res;
2837}
2838
Paul Menagebd89aab2007-10-18 23:40:44 -07002839void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002840{
2841 read_unlock(&css_set_lock);
2842}
2843
Cliff Wickman31a7df02008-02-07 00:14:42 -08002844static inline int started_after_time(struct task_struct *t1,
2845 struct timespec *time,
2846 struct task_struct *t2)
2847{
2848 int start_diff = timespec_compare(&t1->start_time, time);
2849 if (start_diff > 0) {
2850 return 1;
2851 } else if (start_diff < 0) {
2852 return 0;
2853 } else {
2854 /*
2855 * Arbitrarily, if two processes started at the same
2856 * time, we'll say that the lower pointer value
2857 * started first. Note that t2 may have exited by now
2858 * so this may not be a valid pointer any longer, but
2859 * that's fine - it still serves to distinguish
2860 * between two tasks started (effectively) simultaneously.
2861 */
2862 return t1 > t2;
2863 }
2864}
2865
2866/*
2867 * This function is a callback from heap_insert() and is used to order
2868 * the heap.
2869 * In this case we order the heap in descending task start time.
2870 */
2871static inline int started_after(void *p1, void *p2)
2872{
2873 struct task_struct *t1 = p1;
2874 struct task_struct *t2 = p2;
2875 return started_after_time(t1, &t2->start_time, t2);
2876}
2877
2878/**
2879 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
2880 * @scan: struct cgroup_scanner containing arguments for the scan
2881 *
2882 * Arguments include pointers to callback functions test_task() and
2883 * process_task().
2884 * Iterate through all the tasks in a cgroup, calling test_task() for each,
2885 * and if it returns true, call process_task() for it also.
2886 * The test_task pointer may be NULL, meaning always true (select all tasks).
2887 * Effectively duplicates cgroup_iter_{start,next,end}()
2888 * but does not lock css_set_lock for the call to process_task().
2889 * The struct cgroup_scanner may be embedded in any structure of the caller's
2890 * creation.
2891 * It is guaranteed that process_task() will act on every task that
2892 * is a member of the cgroup for the duration of this call. This
2893 * function may or may not call process_task() for tasks that exit
2894 * or move to a different cgroup during the call, or are forked or
2895 * move into the cgroup during the call.
2896 *
2897 * Note that test_task() may be called with locks held, and may in some
2898 * situations be called multiple times for the same task, so it should
2899 * be cheap.
2900 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
2901 * pre-allocated and will be used for heap operations (and its "gt" member will
2902 * be overwritten), else a temporary heap will be used (allocation of which
2903 * may cause this function to fail).
2904 */
2905int cgroup_scan_tasks(struct cgroup_scanner *scan)
2906{
2907 int retval, i;
2908 struct cgroup_iter it;
2909 struct task_struct *p, *dropped;
2910 /* Never dereference latest_task, since it's not refcounted */
2911 struct task_struct *latest_task = NULL;
2912 struct ptr_heap tmp_heap;
2913 struct ptr_heap *heap;
2914 struct timespec latest_time = { 0, 0 };
2915
2916 if (scan->heap) {
2917 /* The caller supplied our heap and pre-allocated its memory */
2918 heap = scan->heap;
2919 heap->gt = &started_after;
2920 } else {
2921 /* We need to allocate our own heap memory */
2922 heap = &tmp_heap;
2923 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
2924 if (retval)
2925 /* cannot allocate the heap */
2926 return retval;
2927 }
2928
2929 again:
2930 /*
2931 * Scan tasks in the cgroup, using the scanner's "test_task" callback
2932 * to determine which are of interest, and using the scanner's
2933 * "process_task" callback to process any of them that need an update.
2934 * Since we don't want to hold any locks during the task updates,
2935 * gather tasks to be processed in a heap structure.
2936 * The heap is sorted by descending task start time.
2937 * If the statically-sized heap fills up, we overflow tasks that
2938 * started later, and in future iterations only consider tasks that
2939 * started after the latest task in the previous pass. This
2940 * guarantees forward progress and that we don't miss any tasks.
2941 */
2942 heap->size = 0;
2943 cgroup_iter_start(scan->cg, &it);
2944 while ((p = cgroup_iter_next(scan->cg, &it))) {
2945 /*
2946 * Only affect tasks that qualify per the caller's callback,
2947 * if he provided one
2948 */
2949 if (scan->test_task && !scan->test_task(p, scan))
2950 continue;
2951 /*
2952 * Only process tasks that started after the last task
2953 * we processed
2954 */
2955 if (!started_after_time(p, &latest_time, latest_task))
2956 continue;
2957 dropped = heap_insert(heap, p);
2958 if (dropped == NULL) {
2959 /*
2960 * The new task was inserted; the heap wasn't
2961 * previously full
2962 */
2963 get_task_struct(p);
2964 } else if (dropped != p) {
2965 /*
2966 * The new task was inserted, and pushed out a
2967 * different task
2968 */
2969 get_task_struct(p);
2970 put_task_struct(dropped);
2971 }
2972 /*
2973 * Else the new task was newer than anything already in
2974 * the heap and wasn't inserted
2975 */
2976 }
2977 cgroup_iter_end(scan->cg, &it);
2978
2979 if (heap->size) {
2980 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07002981 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08002982 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07002983 latest_time = q->start_time;
2984 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08002985 }
2986 /* Process the task per the caller's callback */
Paul Jackson4fe91d52008-04-29 00:59:55 -07002987 scan->process_task(q, scan);
2988 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002989 }
2990 /*
2991 * If we had to process any tasks at all, scan again
2992 * in case some of them were in the middle of forking
2993 * children that didn't get processed.
2994 * Not the most efficient way to do it, but it avoids
2995 * having to take callback_mutex in the fork path
2996 */
2997 goto again;
2998 }
2999 if (heap == &tmp_heap)
3000 heap_free(&tmp_heap);
3001 return 0;
3002}
3003
Paul Menage817929e2007-10-18 23:39:36 -07003004/*
Ben Blum102a7752009-09-23 15:56:26 -07003005 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003006 *
3007 * Reading this file can return large amounts of data if a cgroup has
3008 * *lots* of attached tasks. So it may need several calls to read(),
3009 * but we cannot guarantee that the information we produce is correct
3010 * unless we produce it entirely atomically.
3011 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003012 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003013
3014/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003015 * The following two functions "fix" the issue where there are more pids
3016 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3017 * TODO: replace with a kernel-wide solution to this problem
3018 */
3019#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3020static void *pidlist_allocate(int count)
3021{
3022 if (PIDLIST_TOO_LARGE(count))
3023 return vmalloc(count * sizeof(pid_t));
3024 else
3025 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3026}
3027static void pidlist_free(void *p)
3028{
3029 if (is_vmalloc_addr(p))
3030 vfree(p);
3031 else
3032 kfree(p);
3033}
3034static void *pidlist_resize(void *p, int newcount)
3035{
3036 void *newlist;
3037 /* note: if new alloc fails, old p will still be valid either way */
3038 if (is_vmalloc_addr(p)) {
3039 newlist = vmalloc(newcount * sizeof(pid_t));
3040 if (!newlist)
3041 return NULL;
3042 memcpy(newlist, p, newcount * sizeof(pid_t));
3043 vfree(p);
3044 } else {
3045 newlist = krealloc(p, newcount * sizeof(pid_t), GFP_KERNEL);
3046 }
3047 return newlist;
3048}
3049
3050/*
Ben Blum102a7752009-09-23 15:56:26 -07003051 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
3052 * If the new stripped list is sufficiently smaller and there's enough memory
3053 * to allocate a new buffer, will let go of the unneeded memory. Returns the
3054 * number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003055 */
Ben Blum102a7752009-09-23 15:56:26 -07003056/* is the size difference enough that we should re-allocate the array? */
3057#define PIDLIST_REALLOC_DIFFERENCE(old, new) ((old) - PAGE_SIZE >= (new))
3058static int pidlist_uniq(pid_t **p, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003059{
Ben Blum102a7752009-09-23 15:56:26 -07003060 int src, dest = 1;
3061 pid_t *list = *p;
3062 pid_t *newlist;
3063
3064 /*
3065 * we presume the 0th element is unique, so i starts at 1. trivial
3066 * edge cases first; no work needs to be done for either
3067 */
3068 if (length == 0 || length == 1)
3069 return length;
3070 /* src and dest walk down the list; dest counts unique elements */
3071 for (src = 1; src < length; src++) {
3072 /* find next unique element */
3073 while (list[src] == list[src-1]) {
3074 src++;
3075 if (src == length)
3076 goto after;
3077 }
3078 /* dest always points to where the next unique element goes */
3079 list[dest] = list[src];
3080 dest++;
3081 }
3082after:
3083 /*
3084 * if the length difference is large enough, we want to allocate a
3085 * smaller buffer to save memory. if this fails due to out of memory,
3086 * we'll just stay with what we've got.
3087 */
3088 if (PIDLIST_REALLOC_DIFFERENCE(length, dest)) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003089 newlist = pidlist_resize(list, dest);
Ben Blum102a7752009-09-23 15:56:26 -07003090 if (newlist)
3091 *p = newlist;
3092 }
3093 return dest;
3094}
3095
3096static int cmppid(const void *a, const void *b)
3097{
3098 return *(pid_t *)a - *(pid_t *)b;
3099}
3100
3101/*
Ben Blum72a8cb32009-09-23 15:56:27 -07003102 * find the appropriate pidlist for our purpose (given procs vs tasks)
3103 * returns with the lock on that pidlist already held, and takes care
3104 * of the use count, or returns NULL with no locks held if we're out of
3105 * memory.
3106 */
3107static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3108 enum cgroup_filetype type)
3109{
3110 struct cgroup_pidlist *l;
3111 /* don't need task_nsproxy() if we're looking at ourself */
Li Zefanb70cc5f2010-03-10 15:22:12 -08003112 struct pid_namespace *ns = current->nsproxy->pid_ns;
3113
Ben Blum72a8cb32009-09-23 15:56:27 -07003114 /*
3115 * We can't drop the pidlist_mutex before taking the l->mutex in case
3116 * the last ref-holder is trying to remove l from the list at the same
3117 * time. Holding the pidlist_mutex precludes somebody taking whichever
3118 * list we find out from under us - compare release_pid_array().
3119 */
3120 mutex_lock(&cgrp->pidlist_mutex);
3121 list_for_each_entry(l, &cgrp->pidlists, links) {
3122 if (l->key.type == type && l->key.ns == ns) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003123 /* make sure l doesn't vanish out from under us */
3124 down_write(&l->mutex);
3125 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003126 return l;
3127 }
3128 }
3129 /* entry not found; create a new one */
3130 l = kmalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
3131 if (!l) {
3132 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003133 return l;
3134 }
3135 init_rwsem(&l->mutex);
3136 down_write(&l->mutex);
3137 l->key.type = type;
Li Zefanb70cc5f2010-03-10 15:22:12 -08003138 l->key.ns = get_pid_ns(ns);
Ben Blum72a8cb32009-09-23 15:56:27 -07003139 l->use_count = 0; /* don't increment here */
3140 l->list = NULL;
3141 l->owner = cgrp;
3142 list_add(&l->links, &cgrp->pidlists);
3143 mutex_unlock(&cgrp->pidlist_mutex);
3144 return l;
3145}
3146
3147/*
Ben Blum102a7752009-09-23 15:56:26 -07003148 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3149 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003150static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3151 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003152{
3153 pid_t *array;
3154 int length;
3155 int pid, n = 0; /* used for populating the array */
Paul Menage817929e2007-10-18 23:39:36 -07003156 struct cgroup_iter it;
3157 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003158 struct cgroup_pidlist *l;
3159
3160 /*
3161 * If cgroup gets more users after we read count, we won't have
3162 * enough space - tough. This race is indistinguishable to the
3163 * caller from the case that the additional cgroup users didn't
3164 * show up until sometime later on.
3165 */
3166 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003167 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003168 if (!array)
3169 return -ENOMEM;
3170 /* now, populate the array */
Paul Menagebd89aab2007-10-18 23:40:44 -07003171 cgroup_iter_start(cgrp, &it);
3172 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003173 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003174 break;
Ben Blum102a7752009-09-23 15:56:26 -07003175 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003176 if (type == CGROUP_FILE_PROCS)
3177 pid = task_tgid_vnr(tsk);
3178 else
3179 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003180 if (pid > 0) /* make sure to only use valid results */
3181 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003182 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003183 cgroup_iter_end(cgrp, &it);
Ben Blum102a7752009-09-23 15:56:26 -07003184 length = n;
3185 /* now sort & (if procs) strip out duplicates */
3186 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003187 if (type == CGROUP_FILE_PROCS)
Ben Blum102a7752009-09-23 15:56:26 -07003188 length = pidlist_uniq(&array, length);
Ben Blum72a8cb32009-09-23 15:56:27 -07003189 l = cgroup_pidlist_find(cgrp, type);
3190 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003191 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003192 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003193 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003194 /* store array, freeing old if necessary - lock already held */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003195 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003196 l->list = array;
3197 l->length = length;
3198 l->use_count++;
3199 up_write(&l->mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003200 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003201 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003202}
3203
Balbir Singh846c7bb2007-10-18 23:39:44 -07003204/**
Li Zefana043e3b2008-02-23 15:24:09 -08003205 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003206 * @stats: cgroupstats to fill information into
3207 * @dentry: A dentry entry belonging to the cgroup for which stats have
3208 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003209 *
3210 * Build and fill cgroupstats so that taskstats can export it to user
3211 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003212 */
3213int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3214{
3215 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003216 struct cgroup *cgrp;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003217 struct cgroup_iter it;
3218 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003219
Balbir Singh846c7bb2007-10-18 23:39:44 -07003220 /*
Li Zefan33d283b2008-11-19 15:36:48 -08003221 * Validate dentry by checking the superblock operations,
3222 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003223 */
Li Zefan33d283b2008-11-19 15:36:48 -08003224 if (dentry->d_sb->s_op != &cgroup_ops ||
3225 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07003226 goto err;
3227
3228 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07003229 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003230
Paul Menagebd89aab2007-10-18 23:40:44 -07003231 cgroup_iter_start(cgrp, &it);
3232 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003233 switch (tsk->state) {
3234 case TASK_RUNNING:
3235 stats->nr_running++;
3236 break;
3237 case TASK_INTERRUPTIBLE:
3238 stats->nr_sleeping++;
3239 break;
3240 case TASK_UNINTERRUPTIBLE:
3241 stats->nr_uninterruptible++;
3242 break;
3243 case TASK_STOPPED:
3244 stats->nr_stopped++;
3245 break;
3246 default:
3247 if (delayacct_is_task_waiting_on_io(tsk))
3248 stats->nr_io_wait++;
3249 break;
3250 }
3251 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003252 cgroup_iter_end(cgrp, &it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003253
Balbir Singh846c7bb2007-10-18 23:39:44 -07003254err:
3255 return ret;
3256}
3257
Paul Menage8f3ff202009-09-23 15:56:25 -07003258
Paul Menagecc31edc2008-10-18 20:28:04 -07003259/*
Ben Blum102a7752009-09-23 15:56:26 -07003260 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003261 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003262 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003263 */
3264
Ben Blum102a7752009-09-23 15:56:26 -07003265static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003266{
3267 /*
3268 * Initially we receive a position value that corresponds to
3269 * one more than the last pid shown (or 0 on the first call or
3270 * after a seek to the start). Use a binary-search to find the
3271 * next pid to display, if any
3272 */
Ben Blum102a7752009-09-23 15:56:26 -07003273 struct cgroup_pidlist *l = s->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003274 int index = 0, pid = *pos;
3275 int *iter;
3276
Ben Blum102a7752009-09-23 15:56:26 -07003277 down_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003278 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003279 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003280
Paul Menagecc31edc2008-10-18 20:28:04 -07003281 while (index < end) {
3282 int mid = (index + end) / 2;
Ben Blum102a7752009-09-23 15:56:26 -07003283 if (l->list[mid] == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003284 index = mid;
3285 break;
Ben Blum102a7752009-09-23 15:56:26 -07003286 } else if (l->list[mid] <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003287 index = mid + 1;
3288 else
3289 end = mid;
3290 }
3291 }
3292 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003293 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003294 return NULL;
3295 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003296 iter = l->list + index;
Paul Menagecc31edc2008-10-18 20:28:04 -07003297 *pos = *iter;
3298 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003299}
3300
Ben Blum102a7752009-09-23 15:56:26 -07003301static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003302{
Ben Blum102a7752009-09-23 15:56:26 -07003303 struct cgroup_pidlist *l = s->private;
3304 up_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003305}
3306
Ben Blum102a7752009-09-23 15:56:26 -07003307static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003308{
Ben Blum102a7752009-09-23 15:56:26 -07003309 struct cgroup_pidlist *l = s->private;
3310 pid_t *p = v;
3311 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003312 /*
3313 * Advance to the next pid in the array. If this goes off the
3314 * end, we're done
3315 */
3316 p++;
3317 if (p >= end) {
3318 return NULL;
3319 } else {
3320 *pos = *p;
3321 return p;
3322 }
3323}
3324
Ben Blum102a7752009-09-23 15:56:26 -07003325static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003326{
3327 return seq_printf(s, "%d\n", *(int *)v);
3328}
3329
Ben Blum102a7752009-09-23 15:56:26 -07003330/*
3331 * seq_operations functions for iterating on pidlists through seq_file -
3332 * independent of whether it's tasks or procs
3333 */
3334static const struct seq_operations cgroup_pidlist_seq_operations = {
3335 .start = cgroup_pidlist_start,
3336 .stop = cgroup_pidlist_stop,
3337 .next = cgroup_pidlist_next,
3338 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003339};
3340
Ben Blum102a7752009-09-23 15:56:26 -07003341static void cgroup_release_pid_array(struct cgroup_pidlist *l)
Paul Menagecc31edc2008-10-18 20:28:04 -07003342{
Ben Blum72a8cb32009-09-23 15:56:27 -07003343 /*
3344 * the case where we're the last user of this particular pidlist will
3345 * have us remove it from the cgroup's list, which entails taking the
3346 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3347 * pidlist_mutex, we have to take pidlist_mutex first.
3348 */
3349 mutex_lock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003350 down_write(&l->mutex);
3351 BUG_ON(!l->use_count);
3352 if (!--l->use_count) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003353 /* we're the last user if refcount is 0; remove and free */
3354 list_del(&l->links);
3355 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003356 pidlist_free(l->list);
Ben Blum72a8cb32009-09-23 15:56:27 -07003357 put_pid_ns(l->key.ns);
3358 up_write(&l->mutex);
3359 kfree(l);
3360 return;
Paul Menagecc31edc2008-10-18 20:28:04 -07003361 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003362 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003363 up_write(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003364}
3365
Ben Blum102a7752009-09-23 15:56:26 -07003366static int cgroup_pidlist_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003367{
Ben Blum102a7752009-09-23 15:56:26 -07003368 struct cgroup_pidlist *l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003369 if (!(file->f_mode & FMODE_READ))
3370 return 0;
Ben Blum102a7752009-09-23 15:56:26 -07003371 /*
3372 * the seq_file will only be initialized if the file was opened for
3373 * reading; hence we check if it's not null only in that case.
3374 */
3375 l = ((struct seq_file *)file->private_data)->private;
3376 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003377 return seq_release(inode, file);
3378}
3379
Ben Blum102a7752009-09-23 15:56:26 -07003380static const struct file_operations cgroup_pidlist_operations = {
Paul Menagecc31edc2008-10-18 20:28:04 -07003381 .read = seq_read,
3382 .llseek = seq_lseek,
3383 .write = cgroup_file_write,
Ben Blum102a7752009-09-23 15:56:26 -07003384 .release = cgroup_pidlist_release,
Paul Menagecc31edc2008-10-18 20:28:04 -07003385};
3386
3387/*
Ben Blum102a7752009-09-23 15:56:26 -07003388 * The following functions handle opens on a file that displays a pidlist
3389 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3390 * in the cgroup.
Paul Menagecc31edc2008-10-18 20:28:04 -07003391 */
Ben Blum102a7752009-09-23 15:56:26 -07003392/* helper function for the two below it */
Ben Blum72a8cb32009-09-23 15:56:27 -07003393static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
Paul Menagecc31edc2008-10-18 20:28:04 -07003394{
3395 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Ben Blum72a8cb32009-09-23 15:56:27 -07003396 struct cgroup_pidlist *l;
Paul Menagecc31edc2008-10-18 20:28:04 -07003397 int retval;
3398
3399 /* Nothing to do for write-only files */
3400 if (!(file->f_mode & FMODE_READ))
3401 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003402
Ben Blum102a7752009-09-23 15:56:26 -07003403 /* have the array populated */
Ben Blum72a8cb32009-09-23 15:56:27 -07003404 retval = pidlist_array_load(cgrp, type, &l);
Ben Blum102a7752009-09-23 15:56:26 -07003405 if (retval)
3406 return retval;
3407 /* configure file information */
3408 file->f_op = &cgroup_pidlist_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003409
Ben Blum102a7752009-09-23 15:56:26 -07003410 retval = seq_open(file, &cgroup_pidlist_seq_operations);
Paul Menagecc31edc2008-10-18 20:28:04 -07003411 if (retval) {
Ben Blum102a7752009-09-23 15:56:26 -07003412 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003413 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003414 }
Ben Blum102a7752009-09-23 15:56:26 -07003415 ((struct seq_file *)file->private_data)->private = l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003416 return 0;
3417}
Ben Blum102a7752009-09-23 15:56:26 -07003418static int cgroup_tasks_open(struct inode *unused, struct file *file)
3419{
Ben Blum72a8cb32009-09-23 15:56:27 -07003420 return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
Ben Blum102a7752009-09-23 15:56:26 -07003421}
3422static int cgroup_procs_open(struct inode *unused, struct file *file)
3423{
Ben Blum72a8cb32009-09-23 15:56:27 -07003424 return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
Ben Blum102a7752009-09-23 15:56:26 -07003425}
Paul Menagebbcb81d2007-10-18 23:39:32 -07003426
Paul Menagebd89aab2007-10-18 23:40:44 -07003427static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003428 struct cftype *cft)
3429{
Paul Menagebd89aab2007-10-18 23:40:44 -07003430 return notify_on_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003431}
3432
Paul Menage6379c102008-07-25 01:47:01 -07003433static int cgroup_write_notify_on_release(struct cgroup *cgrp,
3434 struct cftype *cft,
3435 u64 val)
3436{
3437 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
3438 if (val)
3439 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3440 else
3441 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3442 return 0;
3443}
3444
Paul Menagebbcb81d2007-10-18 23:39:32 -07003445/*
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003446 * Unregister event and free resources.
3447 *
3448 * Gets called from workqueue.
3449 */
3450static void cgroup_event_remove(struct work_struct *work)
3451{
3452 struct cgroup_event *event = container_of(work, struct cgroup_event,
3453 remove);
3454 struct cgroup *cgrp = event->cgrp;
3455
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003456 event->cft->unregister_event(cgrp, event->cft, event->eventfd);
3457
3458 eventfd_ctx_put(event->eventfd);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003459 kfree(event);
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08003460 dput(cgrp->dentry);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003461}
3462
3463/*
3464 * Gets called on POLLHUP on eventfd when user closes it.
3465 *
3466 * Called with wqh->lock held and interrupts disabled.
3467 */
3468static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
3469 int sync, void *key)
3470{
3471 struct cgroup_event *event = container_of(wait,
3472 struct cgroup_event, wait);
3473 struct cgroup *cgrp = event->cgrp;
3474 unsigned long flags = (unsigned long)key;
3475
3476 if (flags & POLLHUP) {
Changli Gaoa93d2f12010-05-07 14:33:26 +08003477 __remove_wait_queue(event->wqh, &event->wait);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003478 spin_lock(&cgrp->event_list_lock);
3479 list_del(&event->list);
3480 spin_unlock(&cgrp->event_list_lock);
3481 /*
3482 * We are in atomic context, but cgroup_event_remove() may
3483 * sleep, so we have to call it in workqueue.
3484 */
3485 schedule_work(&event->remove);
3486 }
3487
3488 return 0;
3489}
3490
3491static void cgroup_event_ptable_queue_proc(struct file *file,
3492 wait_queue_head_t *wqh, poll_table *pt)
3493{
3494 struct cgroup_event *event = container_of(pt,
3495 struct cgroup_event, pt);
3496
3497 event->wqh = wqh;
3498 add_wait_queue(wqh, &event->wait);
3499}
3500
3501/*
3502 * Parse input and register new cgroup event handler.
3503 *
3504 * Input must be in format '<event_fd> <control_fd> <args>'.
3505 * Interpretation of args is defined by control file implementation.
3506 */
3507static int cgroup_write_event_control(struct cgroup *cgrp, struct cftype *cft,
3508 const char *buffer)
3509{
3510 struct cgroup_event *event = NULL;
3511 unsigned int efd, cfd;
3512 struct file *efile = NULL;
3513 struct file *cfile = NULL;
3514 char *endp;
3515 int ret;
3516
3517 efd = simple_strtoul(buffer, &endp, 10);
3518 if (*endp != ' ')
3519 return -EINVAL;
3520 buffer = endp + 1;
3521
3522 cfd = simple_strtoul(buffer, &endp, 10);
3523 if ((*endp != ' ') && (*endp != '\0'))
3524 return -EINVAL;
3525 buffer = endp + 1;
3526
3527 event = kzalloc(sizeof(*event), GFP_KERNEL);
3528 if (!event)
3529 return -ENOMEM;
3530 event->cgrp = cgrp;
3531 INIT_LIST_HEAD(&event->list);
3532 init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
3533 init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
3534 INIT_WORK(&event->remove, cgroup_event_remove);
3535
3536 efile = eventfd_fget(efd);
3537 if (IS_ERR(efile)) {
3538 ret = PTR_ERR(efile);
3539 goto fail;
3540 }
3541
3542 event->eventfd = eventfd_ctx_fileget(efile);
3543 if (IS_ERR(event->eventfd)) {
3544 ret = PTR_ERR(event->eventfd);
3545 goto fail;
3546 }
3547
3548 cfile = fget(cfd);
3549 if (!cfile) {
3550 ret = -EBADF;
3551 goto fail;
3552 }
3553
3554 /* the process need read permission on control file */
3555 ret = file_permission(cfile, MAY_READ);
3556 if (ret < 0)
3557 goto fail;
3558
3559 event->cft = __file_cft(cfile);
3560 if (IS_ERR(event->cft)) {
3561 ret = PTR_ERR(event->cft);
3562 goto fail;
3563 }
3564
3565 if (!event->cft->register_event || !event->cft->unregister_event) {
3566 ret = -EINVAL;
3567 goto fail;
3568 }
3569
3570 ret = event->cft->register_event(cgrp, event->cft,
3571 event->eventfd, buffer);
3572 if (ret)
3573 goto fail;
3574
3575 if (efile->f_op->poll(efile, &event->pt) & POLLHUP) {
3576 event->cft->unregister_event(cgrp, event->cft, event->eventfd);
3577 ret = 0;
3578 goto fail;
3579 }
3580
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08003581 /*
3582 * Events should be removed after rmdir of cgroup directory, but before
3583 * destroying subsystem state objects. Let's take reference to cgroup
3584 * directory dentry to do that.
3585 */
3586 dget(cgrp->dentry);
3587
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003588 spin_lock(&cgrp->event_list_lock);
3589 list_add(&event->list, &cgrp->event_list);
3590 spin_unlock(&cgrp->event_list_lock);
3591
3592 fput(cfile);
3593 fput(efile);
3594
3595 return 0;
3596
3597fail:
3598 if (cfile)
3599 fput(cfile);
3600
3601 if (event && event->eventfd && !IS_ERR(event->eventfd))
3602 eventfd_ctx_put(event->eventfd);
3603
3604 if (!IS_ERR_OR_NULL(efile))
3605 fput(efile);
3606
3607 kfree(event);
3608
3609 return ret;
3610}
3611
Daniel Lezcano97978e62010-10-27 15:33:35 -07003612static u64 cgroup_clone_children_read(struct cgroup *cgrp,
3613 struct cftype *cft)
3614{
3615 return clone_children(cgrp);
3616}
3617
3618static int cgroup_clone_children_write(struct cgroup *cgrp,
3619 struct cftype *cft,
3620 u64 val)
3621{
3622 if (val)
3623 set_bit(CGRP_CLONE_CHILDREN, &cgrp->flags);
3624 else
3625 clear_bit(CGRP_CLONE_CHILDREN, &cgrp->flags);
3626 return 0;
3627}
3628
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003629/*
Paul Menagebbcb81d2007-10-18 23:39:32 -07003630 * for the common functions, 'private' gives the type of file
3631 */
Ben Blum102a7752009-09-23 15:56:26 -07003632/* for hysterical raisins, we can't put this on the older files */
3633#define CGROUP_FILE_GENERIC_PREFIX "cgroup."
Paul Menage81a6a5c2007-10-18 23:39:38 -07003634static struct cftype files[] = {
3635 {
3636 .name = "tasks",
3637 .open = cgroup_tasks_open,
Paul Menageaf351022008-07-25 01:47:01 -07003638 .write_u64 = cgroup_tasks_write,
Ben Blum102a7752009-09-23 15:56:26 -07003639 .release = cgroup_pidlist_release,
Li Zefan099fca32009-04-02 16:57:29 -07003640 .mode = S_IRUGO | S_IWUSR,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003641 },
Ben Blum102a7752009-09-23 15:56:26 -07003642 {
3643 .name = CGROUP_FILE_GENERIC_PREFIX "procs",
3644 .open = cgroup_procs_open,
Ben Blum74a11662011-05-26 16:25:20 -07003645 .write_u64 = cgroup_procs_write,
Ben Blum102a7752009-09-23 15:56:26 -07003646 .release = cgroup_pidlist_release,
Ben Blum74a11662011-05-26 16:25:20 -07003647 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07003648 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07003649 {
3650 .name = "notify_on_release",
Paul Menagef4c753b2008-04-29 00:59:56 -07003651 .read_u64 = cgroup_read_notify_on_release,
Paul Menage6379c102008-07-25 01:47:01 -07003652 .write_u64 = cgroup_write_notify_on_release,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003653 },
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003654 {
3655 .name = CGROUP_FILE_GENERIC_PREFIX "event_control",
3656 .write_string = cgroup_write_event_control,
3657 .mode = S_IWUGO,
3658 },
Daniel Lezcano97978e62010-10-27 15:33:35 -07003659 {
3660 .name = "cgroup.clone_children",
3661 .read_u64 = cgroup_clone_children_read,
3662 .write_u64 = cgroup_clone_children_write,
3663 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07003664};
3665
3666static struct cftype cft_release_agent = {
3667 .name = "release_agent",
Paul Menagee788e062008-07-25 01:46:59 -07003668 .read_seq_string = cgroup_release_agent_show,
3669 .write_string = cgroup_release_agent_write,
3670 .max_write_len = PATH_MAX,
Paul Menagebbcb81d2007-10-18 23:39:32 -07003671};
3672
Paul Menagebd89aab2007-10-18 23:40:44 -07003673static int cgroup_populate_dir(struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003674{
3675 int err;
3676 struct cgroup_subsys *ss;
3677
3678 /* First clear out any existing files */
Paul Menagebd89aab2007-10-18 23:40:44 -07003679 cgroup_clear_directory(cgrp->dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003680
Paul Menagebd89aab2007-10-18 23:40:44 -07003681 err = cgroup_add_files(cgrp, NULL, files, ARRAY_SIZE(files));
Paul Menagebbcb81d2007-10-18 23:39:32 -07003682 if (err < 0)
3683 return err;
3684
Paul Menagebd89aab2007-10-18 23:40:44 -07003685 if (cgrp == cgrp->top_cgroup) {
3686 if ((err = cgroup_add_file(cgrp, NULL, &cft_release_agent)) < 0)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003687 return err;
3688 }
3689
Paul Menagebd89aab2007-10-18 23:40:44 -07003690 for_each_subsys(cgrp->root, ss) {
3691 if (ss->populate && (err = ss->populate(ss, cgrp)) < 0)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003692 return err;
3693 }
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07003694 /* This cgroup is ready now */
3695 for_each_subsys(cgrp->root, ss) {
3696 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
3697 /*
3698 * Update id->css pointer and make this css visible from
3699 * CSS ID functions. This pointer will be dereferened
3700 * from RCU-read-side without locks.
3701 */
3702 if (css->id)
3703 rcu_assign_pointer(css->id->css, css);
3704 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003705
3706 return 0;
3707}
3708
3709static void init_cgroup_css(struct cgroup_subsys_state *css,
3710 struct cgroup_subsys *ss,
Paul Menagebd89aab2007-10-18 23:40:44 -07003711 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003712{
Paul Menagebd89aab2007-10-18 23:40:44 -07003713 css->cgroup = cgrp;
Paul Menagee7c5ec92009-01-07 18:08:38 -08003714 atomic_set(&css->refcnt, 1);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003715 css->flags = 0;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07003716 css->id = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003717 if (cgrp == dummytop)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003718 set_bit(CSS_ROOT, &css->flags);
Paul Menagebd89aab2007-10-18 23:40:44 -07003719 BUG_ON(cgrp->subsys[ss->subsys_id]);
3720 cgrp->subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003721}
3722
Paul Menage999cd8a2009-01-07 18:08:36 -08003723static void cgroup_lock_hierarchy(struct cgroupfs_root *root)
3724{
3725 /* We need to take each hierarchy_mutex in a consistent order */
3726 int i;
3727
Ben Blumaae8aab2010-03-10 15:22:07 -08003728 /*
3729 * No worry about a race with rebind_subsystems that might mess up the
3730 * locking order, since both parties are under cgroup_mutex.
3731 */
Paul Menage999cd8a2009-01-07 18:08:36 -08003732 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3733 struct cgroup_subsys *ss = subsys[i];
Ben Blumaae8aab2010-03-10 15:22:07 -08003734 if (ss == NULL)
3735 continue;
Paul Menage999cd8a2009-01-07 18:08:36 -08003736 if (ss->root == root)
Li Zefancfebe562009-02-11 13:04:36 -08003737 mutex_lock(&ss->hierarchy_mutex);
Paul Menage999cd8a2009-01-07 18:08:36 -08003738 }
3739}
3740
3741static void cgroup_unlock_hierarchy(struct cgroupfs_root *root)
3742{
3743 int i;
3744
3745 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3746 struct cgroup_subsys *ss = subsys[i];
Ben Blumaae8aab2010-03-10 15:22:07 -08003747 if (ss == NULL)
3748 continue;
Paul Menage999cd8a2009-01-07 18:08:36 -08003749 if (ss->root == root)
3750 mutex_unlock(&ss->hierarchy_mutex);
3751 }
3752}
3753
Paul Menageddbcc7e2007-10-18 23:39:30 -07003754/*
Li Zefana043e3b2008-02-23 15:24:09 -08003755 * cgroup_create - create a cgroup
3756 * @parent: cgroup that will be parent of the new cgroup
3757 * @dentry: dentry of the new cgroup
3758 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07003759 *
Li Zefana043e3b2008-02-23 15:24:09 -08003760 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07003761 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07003762static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Li Zefan099fca32009-04-02 16:57:29 -07003763 mode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003764{
Paul Menagebd89aab2007-10-18 23:40:44 -07003765 struct cgroup *cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003766 struct cgroupfs_root *root = parent->root;
3767 int err = 0;
3768 struct cgroup_subsys *ss;
3769 struct super_block *sb = root->sb;
3770
Paul Menagebd89aab2007-10-18 23:40:44 -07003771 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
3772 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003773 return -ENOMEM;
3774
3775 /* Grab a reference on the superblock so the hierarchy doesn't
3776 * get deleted on unmount if there are child cgroups. This
3777 * can be done outside cgroup_mutex, since the sb can't
3778 * disappear while someone has an open control file on the
3779 * fs */
3780 atomic_inc(&sb->s_active);
3781
3782 mutex_lock(&cgroup_mutex);
3783
Paul Menagecc31edc2008-10-18 20:28:04 -07003784 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003785
Paul Menagebd89aab2007-10-18 23:40:44 -07003786 cgrp->parent = parent;
3787 cgrp->root = parent->root;
3788 cgrp->top_cgroup = parent->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003789
Li Zefanb6abdb02008-03-04 14:28:19 -08003790 if (notify_on_release(parent))
3791 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3792
Daniel Lezcano97978e62010-10-27 15:33:35 -07003793 if (clone_children(parent))
3794 set_bit(CGRP_CLONE_CHILDREN, &cgrp->flags);
3795
Paul Menageddbcc7e2007-10-18 23:39:30 -07003796 for_each_subsys(root, ss) {
Paul Menagebd89aab2007-10-18 23:40:44 -07003797 struct cgroup_subsys_state *css = ss->create(ss, cgrp);
Li Zefan4528fd02010-02-02 13:44:10 -08003798
Paul Menageddbcc7e2007-10-18 23:39:30 -07003799 if (IS_ERR(css)) {
3800 err = PTR_ERR(css);
3801 goto err_destroy;
3802 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003803 init_cgroup_css(css, ss, cgrp);
Li Zefan4528fd02010-02-02 13:44:10 -08003804 if (ss->use_id) {
3805 err = alloc_css_id(ss, parent, cgrp);
3806 if (err)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07003807 goto err_destroy;
Li Zefan4528fd02010-02-02 13:44:10 -08003808 }
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07003809 /* At error, ->destroy() callback has to free assigned ID. */
Daniel Lezcano97978e62010-10-27 15:33:35 -07003810 if (clone_children(parent) && ss->post_clone)
3811 ss->post_clone(ss, cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003812 }
3813
Paul Menage999cd8a2009-01-07 18:08:36 -08003814 cgroup_lock_hierarchy(root);
Paul Menagebd89aab2007-10-18 23:40:44 -07003815 list_add(&cgrp->sibling, &cgrp->parent->children);
Paul Menage999cd8a2009-01-07 18:08:36 -08003816 cgroup_unlock_hierarchy(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003817 root->number_of_cgroups++;
3818
Paul Menagebd89aab2007-10-18 23:40:44 -07003819 err = cgroup_create_dir(cgrp, dentry, mode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003820 if (err < 0)
3821 goto err_remove;
3822
3823 /* The cgroup directory was pre-locked for us */
Paul Menagebd89aab2007-10-18 23:40:44 -07003824 BUG_ON(!mutex_is_locked(&cgrp->dentry->d_inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07003825
Paul Menagebd89aab2007-10-18 23:40:44 -07003826 err = cgroup_populate_dir(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003827 /* If err < 0, we have a half-filled directory - oh well ;) */
3828
3829 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07003830 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003831
3832 return 0;
3833
3834 err_remove:
3835
KAMEZAWA Hiroyukibaef99a2009-01-29 14:25:10 -08003836 cgroup_lock_hierarchy(root);
Paul Menagebd89aab2007-10-18 23:40:44 -07003837 list_del(&cgrp->sibling);
KAMEZAWA Hiroyukibaef99a2009-01-29 14:25:10 -08003838 cgroup_unlock_hierarchy(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003839 root->number_of_cgroups--;
3840
3841 err_destroy:
3842
3843 for_each_subsys(root, ss) {
Paul Menagebd89aab2007-10-18 23:40:44 -07003844 if (cgrp->subsys[ss->subsys_id])
3845 ss->destroy(ss, cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003846 }
3847
3848 mutex_unlock(&cgroup_mutex);
3849
3850 /* Release the reference count that we took on the superblock */
3851 deactivate_super(sb);
3852
Paul Menagebd89aab2007-10-18 23:40:44 -07003853 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003854 return err;
3855}
3856
3857static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode)
3858{
3859 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
3860
3861 /* the vfs holds inode->i_mutex already */
3862 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
3863}
3864
Li Zefan55b6fd02008-07-29 22:33:20 -07003865static int cgroup_has_css_refs(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003866{
3867 /* Check the reference count on each subsystem. Since we
3868 * already established that there are no tasks in the
Paul Menagee7c5ec92009-01-07 18:08:38 -08003869 * cgroup, if the css refcount is also 1, then there should
Paul Menage81a6a5c2007-10-18 23:39:38 -07003870 * be no outstanding references, so the subsystem is safe to
3871 * destroy. We scan across all subsystems rather than using
3872 * the per-hierarchy linked list of mounted subsystems since
3873 * we can be called via check_for_release() with no
3874 * synchronization other than RCU, and the subsystem linked
3875 * list isn't RCU-safe */
3876 int i;
Ben Blumaae8aab2010-03-10 15:22:07 -08003877 /*
3878 * We won't need to lock the subsys array, because the subsystems
3879 * we're concerned about aren't going anywhere since our cgroup root
3880 * has a reference on them.
3881 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07003882 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3883 struct cgroup_subsys *ss = subsys[i];
3884 struct cgroup_subsys_state *css;
Ben Blumaae8aab2010-03-10 15:22:07 -08003885 /* Skip subsystems not present or not in this hierarchy */
3886 if (ss == NULL || ss->root != cgrp->root)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003887 continue;
Paul Menagebd89aab2007-10-18 23:40:44 -07003888 css = cgrp->subsys[ss->subsys_id];
Paul Menage81a6a5c2007-10-18 23:39:38 -07003889 /* When called from check_for_release() it's possible
3890 * that by this point the cgroup has been removed
3891 * and the css deleted. But a false-positive doesn't
3892 * matter, since it can only happen if the cgroup
3893 * has been deleted and hence no longer needs the
3894 * release agent to be called anyway. */
Paul Menagee7c5ec92009-01-07 18:08:38 -08003895 if (css && (atomic_read(&css->refcnt) > 1))
Paul Menage81a6a5c2007-10-18 23:39:38 -07003896 return 1;
Paul Menage81a6a5c2007-10-18 23:39:38 -07003897 }
3898 return 0;
3899}
3900
Paul Menagee7c5ec92009-01-07 18:08:38 -08003901/*
3902 * Atomically mark all (or else none) of the cgroup's CSS objects as
3903 * CSS_REMOVED. Return true on success, or false if the cgroup has
3904 * busy subsystems. Call with cgroup_mutex held
3905 */
3906
3907static int cgroup_clear_css_refs(struct cgroup *cgrp)
3908{
3909 struct cgroup_subsys *ss;
3910 unsigned long flags;
3911 bool failed = false;
3912 local_irq_save(flags);
3913 for_each_subsys(cgrp->root, ss) {
3914 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
3915 int refcnt;
Paul Menage804b3c22009-01-29 14:25:21 -08003916 while (1) {
Paul Menagee7c5ec92009-01-07 18:08:38 -08003917 /* We can only remove a CSS with a refcnt==1 */
3918 refcnt = atomic_read(&css->refcnt);
3919 if (refcnt > 1) {
3920 failed = true;
3921 goto done;
3922 }
3923 BUG_ON(!refcnt);
3924 /*
3925 * Drop the refcnt to 0 while we check other
3926 * subsystems. This will cause any racing
3927 * css_tryget() to spin until we set the
3928 * CSS_REMOVED bits or abort
3929 */
Paul Menage804b3c22009-01-29 14:25:21 -08003930 if (atomic_cmpxchg(&css->refcnt, refcnt, 0) == refcnt)
3931 break;
3932 cpu_relax();
3933 }
Paul Menagee7c5ec92009-01-07 18:08:38 -08003934 }
3935 done:
3936 for_each_subsys(cgrp->root, ss) {
3937 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
3938 if (failed) {
3939 /*
3940 * Restore old refcnt if we previously managed
3941 * to clear it from 1 to 0
3942 */
3943 if (!atomic_read(&css->refcnt))
3944 atomic_set(&css->refcnt, 1);
3945 } else {
3946 /* Commit the fact that the CSS is removed */
3947 set_bit(CSS_REMOVED, &css->flags);
3948 }
3949 }
3950 local_irq_restore(flags);
3951 return !failed;
3952}
3953
Paul Menageddbcc7e2007-10-18 23:39:30 -07003954static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
3955{
Paul Menagebd89aab2007-10-18 23:40:44 -07003956 struct cgroup *cgrp = dentry->d_fsdata;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003957 struct dentry *d;
3958 struct cgroup *parent;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003959 DEFINE_WAIT(wait);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08003960 struct cgroup_event *event, *tmp;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003961 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003962
3963 /* the vfs holds both inode->i_mutex already */
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003964again:
Paul Menageddbcc7e2007-10-18 23:39:30 -07003965 mutex_lock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07003966 if (atomic_read(&cgrp->count) != 0) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07003967 mutex_unlock(&cgroup_mutex);
3968 return -EBUSY;
3969 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003970 if (!list_empty(&cgrp->children)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07003971 mutex_unlock(&cgroup_mutex);
3972 return -EBUSY;
3973 }
KAMEZAWA Hiroyuki3fa59df2008-11-19 15:36:34 -08003974 mutex_unlock(&cgroup_mutex);
Li Zefana043e3b2008-02-23 15:24:09 -08003975
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -08003976 /*
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003977 * In general, subsystem has no css->refcnt after pre_destroy(). But
3978 * in racy cases, subsystem may have to get css->refcnt after
3979 * pre_destroy() and it makes rmdir return with -EBUSY. This sometimes
3980 * make rmdir return -EBUSY too often. To avoid that, we use waitqueue
3981 * for cgroup's rmdir. CGRP_WAIT_ON_RMDIR is for synchronizing rmdir
3982 * and subsystem's reference count handling. Please see css_get/put
3983 * and css_tryget() and cgroup_wakeup_rmdir_waiter() implementation.
3984 */
3985 set_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
3986
3987 /*
Li Zefana043e3b2008-02-23 15:24:09 -08003988 * Call pre_destroy handlers of subsys. Notify subsystems
3989 * that rmdir() request comes.
KAMEZAWA Hiroyuki4fca88c2008-02-07 00:14:27 -08003990 */
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003991 ret = cgroup_call_pre_destroy(cgrp);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003992 if (ret) {
3993 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003994 return ret;
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07003995 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003996
KAMEZAWA Hiroyuki3fa59df2008-11-19 15:36:34 -08003997 mutex_lock(&cgroup_mutex);
3998 parent = cgrp->parent;
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003999 if (atomic_read(&cgrp->count) || !list_empty(&cgrp->children)) {
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07004000 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004001 mutex_unlock(&cgroup_mutex);
4002 return -EBUSY;
4003 }
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004004 prepare_to_wait(&cgroup_rmdir_waitq, &wait, TASK_INTERRUPTIBLE);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004005 if (!cgroup_clear_css_refs(cgrp)) {
4006 mutex_unlock(&cgroup_mutex);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07004007 /*
4008 * Because someone may call cgroup_wakeup_rmdir_waiter() before
4009 * prepare_to_wait(), we need to check this flag.
4010 */
4011 if (test_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags))
4012 schedule();
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004013 finish_wait(&cgroup_rmdir_waitq, &wait);
4014 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
4015 if (signal_pending(current))
4016 return -EINTR;
4017 goto again;
4018 }
4019 /* NO css_tryget() can success after here. */
4020 finish_wait(&cgroup_rmdir_waitq, &wait);
4021 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004022
Paul Menage81a6a5c2007-10-18 23:39:38 -07004023 spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07004024 set_bit(CGRP_REMOVED, &cgrp->flags);
4025 if (!list_empty(&cgrp->release_list))
Phil Carmody8d258792011-03-22 16:30:13 -07004026 list_del_init(&cgrp->release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004027 spin_unlock(&release_list_lock);
Paul Menage999cd8a2009-01-07 18:08:36 -08004028
4029 cgroup_lock_hierarchy(cgrp->root);
4030 /* delete this cgroup from parent->children */
Phil Carmody8d258792011-03-22 16:30:13 -07004031 list_del_init(&cgrp->sibling);
Paul Menage999cd8a2009-01-07 18:08:36 -08004032 cgroup_unlock_hierarchy(cgrp->root);
4033
Paul Menagebd89aab2007-10-18 23:40:44 -07004034 d = dget(cgrp->dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004035
4036 cgroup_d_remove_dir(d);
4037 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004038
Paul Menagebd89aab2007-10-18 23:40:44 -07004039 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004040 check_for_release(parent);
4041
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004042 /*
4043 * Unregister events and notify userspace.
4044 * Notify userspace about cgroup removing only after rmdir of cgroup
4045 * directory to avoid race between userspace and kernelspace
4046 */
4047 spin_lock(&cgrp->event_list_lock);
4048 list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) {
4049 list_del(&event->list);
4050 remove_wait_queue(event->wqh, &event->wait);
4051 eventfd_signal(event->eventfd, 1);
4052 schedule_work(&event->remove);
4053 }
4054 spin_unlock(&cgrp->event_list_lock);
4055
Paul Menageddbcc7e2007-10-18 23:39:30 -07004056 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004057 return 0;
4058}
4059
Li Zefan06a11922008-04-29 01:00:07 -07004060static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004061{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004062 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004063
4064 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004065
4066 /* Create the top cgroup state for this subsystem */
Li Zefan33a68ac2009-01-07 18:07:42 -08004067 list_add(&ss->sibling, &rootnode.subsys_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004068 ss->root = &rootnode;
4069 css = ss->create(ss, dummytop);
4070 /* We don't handle early failures gracefully */
4071 BUG_ON(IS_ERR(css));
4072 init_cgroup_css(css, ss, dummytop);
4073
Li Zefane8d55fd2008-04-29 01:00:13 -07004074 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004075 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004076 * newly registered, all tasks and hence the
4077 * init_css_set is in the subsystem's top cgroup. */
4078 init_css_set.subsys[ss->subsys_id] = dummytop->subsys[ss->subsys_id];
Paul Menageddbcc7e2007-10-18 23:39:30 -07004079
4080 need_forkexit_callback |= ss->fork || ss->exit;
4081
Li Zefane8d55fd2008-04-29 01:00:13 -07004082 /* At system boot, before all subsystems have been
4083 * registered, no tasks have been forked, so we don't
4084 * need to invoke fork callbacks here. */
4085 BUG_ON(!list_empty(&init_task.tasks));
4086
Paul Menage999cd8a2009-01-07 18:08:36 -08004087 mutex_init(&ss->hierarchy_mutex);
Li Zefancfebe562009-02-11 13:04:36 -08004088 lockdep_set_class(&ss->hierarchy_mutex, &ss->subsys_key);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004089 ss->active = 1;
Ben Blume6a11052010-03-10 15:22:09 -08004090
4091 /* this function shouldn't be used with modular subsystems, since they
4092 * need to register a subsys_id, among other things */
4093 BUG_ON(ss->module);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004094}
4095
4096/**
Ben Blume6a11052010-03-10 15:22:09 -08004097 * cgroup_load_subsys: load and register a modular subsystem at runtime
4098 * @ss: the subsystem to load
4099 *
4100 * This function should be called in a modular subsystem's initcall. If the
Thomas Weber88393162010-03-16 11:47:56 +01004101 * subsystem is built as a module, it will be assigned a new subsys_id and set
Ben Blume6a11052010-03-10 15:22:09 -08004102 * up for use. If the subsystem is built-in anyway, work is delegated to the
4103 * simpler cgroup_init_subsys.
4104 */
4105int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
4106{
4107 int i;
4108 struct cgroup_subsys_state *css;
4109
4110 /* check name and function validity */
4111 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
4112 ss->create == NULL || ss->destroy == NULL)
4113 return -EINVAL;
4114
4115 /*
4116 * we don't support callbacks in modular subsystems. this check is
4117 * before the ss->module check for consistency; a subsystem that could
4118 * be a module should still have no callbacks even if the user isn't
4119 * compiling it as one.
4120 */
4121 if (ss->fork || ss->exit)
4122 return -EINVAL;
4123
4124 /*
4125 * an optionally modular subsystem is built-in: we want to do nothing,
4126 * since cgroup_init_subsys will have already taken care of it.
4127 */
4128 if (ss->module == NULL) {
4129 /* a few sanity checks */
4130 BUG_ON(ss->subsys_id >= CGROUP_BUILTIN_SUBSYS_COUNT);
4131 BUG_ON(subsys[ss->subsys_id] != ss);
4132 return 0;
4133 }
4134
4135 /*
4136 * need to register a subsys id before anything else - for example,
4137 * init_cgroup_css needs it.
4138 */
4139 mutex_lock(&cgroup_mutex);
4140 /* find the first empty slot in the array */
4141 for (i = CGROUP_BUILTIN_SUBSYS_COUNT; i < CGROUP_SUBSYS_COUNT; i++) {
4142 if (subsys[i] == NULL)
4143 break;
4144 }
4145 if (i == CGROUP_SUBSYS_COUNT) {
4146 /* maximum number of subsystems already registered! */
4147 mutex_unlock(&cgroup_mutex);
4148 return -EBUSY;
4149 }
4150 /* assign ourselves the subsys_id */
4151 ss->subsys_id = i;
4152 subsys[i] = ss;
4153
4154 /*
4155 * no ss->create seems to need anything important in the ss struct, so
4156 * this can happen first (i.e. before the rootnode attachment).
4157 */
4158 css = ss->create(ss, dummytop);
4159 if (IS_ERR(css)) {
4160 /* failure case - need to deassign the subsys[] slot. */
4161 subsys[i] = NULL;
4162 mutex_unlock(&cgroup_mutex);
4163 return PTR_ERR(css);
4164 }
4165
4166 list_add(&ss->sibling, &rootnode.subsys_list);
4167 ss->root = &rootnode;
4168
4169 /* our new subsystem will be attached to the dummy hierarchy. */
4170 init_cgroup_css(css, ss, dummytop);
4171 /* init_idr must be after init_cgroup_css because it sets css->id. */
4172 if (ss->use_id) {
4173 int ret = cgroup_init_idr(ss, css);
4174 if (ret) {
4175 dummytop->subsys[ss->subsys_id] = NULL;
4176 ss->destroy(ss, dummytop);
4177 subsys[i] = NULL;
4178 mutex_unlock(&cgroup_mutex);
4179 return ret;
4180 }
4181 }
4182
4183 /*
4184 * Now we need to entangle the css into the existing css_sets. unlike
4185 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4186 * will need a new pointer to it; done by iterating the css_set_table.
4187 * furthermore, modifying the existing css_sets will corrupt the hash
4188 * table state, so each changed css_set will need its hash recomputed.
4189 * this is all done under the css_set_lock.
4190 */
4191 write_lock(&css_set_lock);
4192 for (i = 0; i < CSS_SET_TABLE_SIZE; i++) {
4193 struct css_set *cg;
4194 struct hlist_node *node, *tmp;
4195 struct hlist_head *bucket = &css_set_table[i], *new_bucket;
4196
4197 hlist_for_each_entry_safe(cg, node, tmp, bucket, hlist) {
4198 /* skip entries that we already rehashed */
4199 if (cg->subsys[ss->subsys_id])
4200 continue;
4201 /* remove existing entry */
4202 hlist_del(&cg->hlist);
4203 /* set new value */
4204 cg->subsys[ss->subsys_id] = css;
4205 /* recompute hash and restore entry */
4206 new_bucket = css_set_hash(cg->subsys);
4207 hlist_add_head(&cg->hlist, new_bucket);
4208 }
4209 }
4210 write_unlock(&css_set_lock);
4211
4212 mutex_init(&ss->hierarchy_mutex);
4213 lockdep_set_class(&ss->hierarchy_mutex, &ss->subsys_key);
4214 ss->active = 1;
4215
Ben Blume6a11052010-03-10 15:22:09 -08004216 /* success! */
4217 mutex_unlock(&cgroup_mutex);
4218 return 0;
4219}
4220EXPORT_SYMBOL_GPL(cgroup_load_subsys);
4221
4222/**
Ben Blumcf5d5942010-03-10 15:22:09 -08004223 * cgroup_unload_subsys: unload a modular subsystem
4224 * @ss: the subsystem to unload
4225 *
4226 * This function should be called in a modular subsystem's exitcall. When this
4227 * function is invoked, the refcount on the subsystem's module will be 0, so
4228 * the subsystem will not be attached to any hierarchy.
4229 */
4230void cgroup_unload_subsys(struct cgroup_subsys *ss)
4231{
4232 struct cg_cgroup_link *link;
4233 struct hlist_head *hhead;
4234
4235 BUG_ON(ss->module == NULL);
4236
4237 /*
4238 * we shouldn't be called if the subsystem is in use, and the use of
4239 * try_module_get in parse_cgroupfs_options should ensure that it
4240 * doesn't start being used while we're killing it off.
4241 */
4242 BUG_ON(ss->root != &rootnode);
4243
4244 mutex_lock(&cgroup_mutex);
4245 /* deassign the subsys_id */
4246 BUG_ON(ss->subsys_id < CGROUP_BUILTIN_SUBSYS_COUNT);
4247 subsys[ss->subsys_id] = NULL;
4248
4249 /* remove subsystem from rootnode's list of subsystems */
Phil Carmody8d258792011-03-22 16:30:13 -07004250 list_del_init(&ss->sibling);
Ben Blumcf5d5942010-03-10 15:22:09 -08004251
4252 /*
4253 * disentangle the css from all css_sets attached to the dummytop. as
4254 * in loading, we need to pay our respects to the hashtable gods.
4255 */
4256 write_lock(&css_set_lock);
4257 list_for_each_entry(link, &dummytop->css_sets, cgrp_link_list) {
4258 struct css_set *cg = link->cg;
4259
4260 hlist_del(&cg->hlist);
4261 BUG_ON(!cg->subsys[ss->subsys_id]);
4262 cg->subsys[ss->subsys_id] = NULL;
4263 hhead = css_set_hash(cg->subsys);
4264 hlist_add_head(&cg->hlist, hhead);
4265 }
4266 write_unlock(&css_set_lock);
4267
4268 /*
4269 * remove subsystem's css from the dummytop and free it - need to free
4270 * before marking as null because ss->destroy needs the cgrp->subsys
4271 * pointer to find their state. note that this also takes care of
4272 * freeing the css_id.
4273 */
4274 ss->destroy(ss, dummytop);
4275 dummytop->subsys[ss->subsys_id] = NULL;
4276
4277 mutex_unlock(&cgroup_mutex);
4278}
4279EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
4280
4281/**
Li Zefana043e3b2008-02-23 15:24:09 -08004282 * cgroup_init_early - cgroup initialization at system boot
4283 *
4284 * Initialize cgroups at system boot, and initialize any
4285 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004286 */
4287int __init cgroup_init_early(void)
4288{
4289 int i;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07004290 atomic_set(&init_css_set.refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -07004291 INIT_LIST_HEAD(&init_css_set.cg_links);
4292 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07004293 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07004294 css_set_count = 1;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004295 init_cgroup_root(&rootnode);
Paul Menage817929e2007-10-18 23:39:36 -07004296 root_count = 1;
4297 init_task.cgroups = &init_css_set;
4298
4299 init_css_set_link.cg = &init_css_set;
Paul Menage7717f7b2009-09-23 15:56:22 -07004300 init_css_set_link.cgrp = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -07004301 list_add(&init_css_set_link.cgrp_link_list,
Paul Menage817929e2007-10-18 23:39:36 -07004302 &rootnode.top_cgroup.css_sets);
4303 list_add(&init_css_set_link.cg_link_list,
4304 &init_css_set.cg_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004305
Li Zefan472b1052008-04-29 01:00:11 -07004306 for (i = 0; i < CSS_SET_TABLE_SIZE; i++)
4307 INIT_HLIST_HEAD(&css_set_table[i]);
4308
Ben Blumaae8aab2010-03-10 15:22:07 -08004309 /* at bootup time, we don't worry about modular subsystems */
4310 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004311 struct cgroup_subsys *ss = subsys[i];
4312
4313 BUG_ON(!ss->name);
4314 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
4315 BUG_ON(!ss->create);
4316 BUG_ON(!ss->destroy);
4317 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08004318 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07004319 ss->name, ss->subsys_id);
4320 BUG();
4321 }
4322
4323 if (ss->early_init)
4324 cgroup_init_subsys(ss);
4325 }
4326 return 0;
4327}
4328
4329/**
Li Zefana043e3b2008-02-23 15:24:09 -08004330 * cgroup_init - cgroup initialization
4331 *
4332 * Register cgroup filesystem and /proc file, and initialize
4333 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004334 */
4335int __init cgroup_init(void)
4336{
4337 int err;
4338 int i;
Li Zefan472b1052008-04-29 01:00:11 -07004339 struct hlist_head *hhead;
Paul Menagea4243162007-10-18 23:39:35 -07004340
4341 err = bdi_init(&cgroup_backing_dev_info);
4342 if (err)
4343 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004344
Ben Blumaae8aab2010-03-10 15:22:07 -08004345 /* at bootup time, we don't worry about modular subsystems */
4346 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004347 struct cgroup_subsys *ss = subsys[i];
4348 if (!ss->early_init)
4349 cgroup_init_subsys(ss);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004350 if (ss->use_id)
Ben Blume6a11052010-03-10 15:22:09 -08004351 cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004352 }
4353
Li Zefan472b1052008-04-29 01:00:11 -07004354 /* Add init_css_set to the hash table */
4355 hhead = css_set_hash(init_css_set.subsys);
4356 hlist_add_head(&init_css_set.hlist, hhead);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004357 BUG_ON(!init_root_id(&rootnode));
Greg KH676db4a2010-08-05 13:53:35 -07004358
4359 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
4360 if (!cgroup_kobj) {
4361 err = -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004362 goto out;
Greg KH676db4a2010-08-05 13:53:35 -07004363 }
4364
4365 err = register_filesystem(&cgroup_fs_type);
4366 if (err < 0) {
4367 kobject_put(cgroup_kobj);
4368 goto out;
4369 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004370
Li Zefan46ae2202008-04-29 01:00:08 -07004371 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07004372
Paul Menageddbcc7e2007-10-18 23:39:30 -07004373out:
Paul Menagea4243162007-10-18 23:39:35 -07004374 if (err)
4375 bdi_destroy(&cgroup_backing_dev_info);
4376
Paul Menageddbcc7e2007-10-18 23:39:30 -07004377 return err;
4378}
Paul Menageb4f48b62007-10-18 23:39:33 -07004379
Paul Menagea4243162007-10-18 23:39:35 -07004380/*
4381 * proc_cgroup_show()
4382 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4383 * - Used for /proc/<pid>/cgroup.
4384 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4385 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004386 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07004387 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4388 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4389 * cgroup to top_cgroup.
4390 */
4391
4392/* TODO: Use a proper seq_file iterator */
4393static int proc_cgroup_show(struct seq_file *m, void *v)
4394{
4395 struct pid *pid;
4396 struct task_struct *tsk;
4397 char *buf;
4398 int retval;
4399 struct cgroupfs_root *root;
4400
4401 retval = -ENOMEM;
4402 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4403 if (!buf)
4404 goto out;
4405
4406 retval = -ESRCH;
4407 pid = m->private;
4408 tsk = get_pid_task(pid, PIDTYPE_PID);
4409 if (!tsk)
4410 goto out_free;
4411
4412 retval = 0;
4413
4414 mutex_lock(&cgroup_mutex);
4415
Li Zefane5f6a862009-01-07 18:07:41 -08004416 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004417 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004418 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07004419 int count = 0;
4420
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004421 seq_printf(m, "%d:", root->hierarchy_id);
Paul Menagea4243162007-10-18 23:39:35 -07004422 for_each_subsys(root, ss)
4423 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004424 if (strlen(root->name))
4425 seq_printf(m, "%sname=%s", count ? "," : "",
4426 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004427 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004428 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07004429 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07004430 if (retval < 0)
4431 goto out_unlock;
4432 seq_puts(m, buf);
4433 seq_putc(m, '\n');
4434 }
4435
4436out_unlock:
4437 mutex_unlock(&cgroup_mutex);
4438 put_task_struct(tsk);
4439out_free:
4440 kfree(buf);
4441out:
4442 return retval;
4443}
4444
4445static int cgroup_open(struct inode *inode, struct file *file)
4446{
4447 struct pid *pid = PROC_I(inode)->pid;
4448 return single_open(file, proc_cgroup_show, pid);
4449}
4450
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004451const struct file_operations proc_cgroup_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004452 .open = cgroup_open,
4453 .read = seq_read,
4454 .llseek = seq_lseek,
4455 .release = single_release,
4456};
4457
4458/* Display information about each subsystem and each hierarchy */
4459static int proc_cgroupstats_show(struct seq_file *m, void *v)
4460{
4461 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004462
Paul Menage8bab8dd2008-04-04 14:29:57 -07004463 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08004464 /*
4465 * ideally we don't want subsystems moving around while we do this.
4466 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4467 * subsys/hierarchy state.
4468 */
Paul Menagea4243162007-10-18 23:39:35 -07004469 mutex_lock(&cgroup_mutex);
Paul Menagea4243162007-10-18 23:39:35 -07004470 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
4471 struct cgroup_subsys *ss = subsys[i];
Ben Blumaae8aab2010-03-10 15:22:07 -08004472 if (ss == NULL)
4473 continue;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004474 seq_printf(m, "%s\t%d\t%d\t%d\n",
4475 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07004476 ss->root->number_of_cgroups, !ss->disabled);
Paul Menagea4243162007-10-18 23:39:35 -07004477 }
4478 mutex_unlock(&cgroup_mutex);
4479 return 0;
4480}
4481
4482static int cgroupstats_open(struct inode *inode, struct file *file)
4483{
Al Viro9dce07f12008-03-29 03:07:28 +00004484 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07004485}
4486
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004487static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004488 .open = cgroupstats_open,
4489 .read = seq_read,
4490 .llseek = seq_lseek,
4491 .release = single_release,
4492};
4493
Paul Menageb4f48b62007-10-18 23:39:33 -07004494/**
4495 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08004496 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07004497 *
4498 * Description: A task inherits its parent's cgroup at fork().
4499 *
4500 * A pointer to the shared css_set was automatically copied in
4501 * fork.c by dup_task_struct(). However, we ignore that copy, since
4502 * it was not made under the protection of RCU or cgroup_mutex, so
Cliff Wickman956db3c2008-02-07 00:14:43 -08004503 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
Paul Menage817929e2007-10-18 23:39:36 -07004504 * have already changed current->cgroups, allowing the previously
4505 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07004506 *
4507 * At the point that cgroup_fork() is called, 'current' is the parent
4508 * task, and the passed argument 'child' points to the child task.
4509 */
4510void cgroup_fork(struct task_struct *child)
4511{
Paul Menage817929e2007-10-18 23:39:36 -07004512 task_lock(current);
4513 child->cgroups = current->cgroups;
4514 get_css_set(child->cgroups);
4515 task_unlock(current);
4516 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07004517}
4518
4519/**
Li Zefana043e3b2008-02-23 15:24:09 -08004520 * cgroup_fork_callbacks - run fork callbacks
4521 * @child: the new task
4522 *
4523 * Called on a new task very soon before adding it to the
4524 * tasklist. No need to take any locks since no-one can
4525 * be operating on this task.
Paul Menageb4f48b62007-10-18 23:39:33 -07004526 */
4527void cgroup_fork_callbacks(struct task_struct *child)
4528{
4529 if (need_forkexit_callback) {
4530 int i;
Ben Blumaae8aab2010-03-10 15:22:07 -08004531 /*
4532 * forkexit callbacks are only supported for builtin
4533 * subsystems, and the builtin section of the subsys array is
4534 * immutable, so we don't need to lock the subsys array here.
4535 */
4536 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
Paul Menageb4f48b62007-10-18 23:39:33 -07004537 struct cgroup_subsys *ss = subsys[i];
4538 if (ss->fork)
4539 ss->fork(ss, child);
4540 }
4541 }
4542}
4543
4544/**
Li Zefana043e3b2008-02-23 15:24:09 -08004545 * cgroup_post_fork - called on a new task after adding it to the task list
4546 * @child: the task in question
4547 *
4548 * Adds the task to the list running through its css_set if necessary.
4549 * Has to be after the task is visible on the task list in case we race
4550 * with the first call to cgroup_iter_start() - to guarantee that the
4551 * new task ends up on its list.
4552 */
Paul Menage817929e2007-10-18 23:39:36 -07004553void cgroup_post_fork(struct task_struct *child)
4554{
4555 if (use_task_css_set_links) {
4556 write_lock(&css_set_lock);
Lai Jiangshanb12b5332009-01-07 18:07:36 -08004557 task_lock(child);
Paul Menage817929e2007-10-18 23:39:36 -07004558 if (list_empty(&child->cg_list))
4559 list_add(&child->cg_list, &child->cgroups->tasks);
Lai Jiangshanb12b5332009-01-07 18:07:36 -08004560 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07004561 write_unlock(&css_set_lock);
4562 }
4563}
4564/**
Paul Menageb4f48b62007-10-18 23:39:33 -07004565 * cgroup_exit - detach cgroup from exiting task
4566 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08004567 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07004568 *
4569 * Description: Detach cgroup from @tsk and release it.
4570 *
4571 * Note that cgroups marked notify_on_release force every task in
4572 * them to take the global cgroup_mutex mutex when exiting.
4573 * This could impact scaling on very large systems. Be reluctant to
4574 * use notify_on_release cgroups where very high task exit scaling
4575 * is required on large systems.
4576 *
4577 * the_top_cgroup_hack:
4578 *
4579 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
4580 *
4581 * We call cgroup_exit() while the task is still competent to
4582 * handle notify_on_release(), then leave the task attached to the
4583 * root cgroup in each hierarchy for the remainder of its exit.
4584 *
4585 * To do this properly, we would increment the reference count on
4586 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
4587 * code we would add a second cgroup function call, to drop that
4588 * reference. This would just create an unnecessary hot spot on
4589 * the top_cgroup reference count, to no avail.
4590 *
4591 * Normally, holding a reference to a cgroup without bumping its
4592 * count is unsafe. The cgroup could go away, or someone could
4593 * attach us to a different cgroup, decrementing the count on
4594 * the first cgroup that we never incremented. But in this case,
4595 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004596 * which wards off any cgroup_attach_task() attempts, or task is a failed
4597 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07004598 */
4599void cgroup_exit(struct task_struct *tsk, int run_callbacks)
4600{
Paul Menage817929e2007-10-18 23:39:36 -07004601 struct css_set *cg;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004602 int i;
Paul Menage817929e2007-10-18 23:39:36 -07004603
4604 /*
4605 * Unlink from the css_set task list if necessary.
4606 * Optimistically check cg_list before taking
4607 * css_set_lock
4608 */
4609 if (!list_empty(&tsk->cg_list)) {
4610 write_lock(&css_set_lock);
4611 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07004612 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07004613 write_unlock(&css_set_lock);
4614 }
4615
Paul Menageb4f48b62007-10-18 23:39:33 -07004616 /* Reassign the task to the init_css_set. */
4617 task_lock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07004618 cg = tsk->cgroups;
4619 tsk->cgroups = &init_css_set;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004620
4621 if (run_callbacks && need_forkexit_callback) {
4622 /*
4623 * modular subsystems can't use callbacks, so no need to lock
4624 * the subsys array
4625 */
4626 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
4627 struct cgroup_subsys *ss = subsys[i];
4628 if (ss->exit) {
4629 struct cgroup *old_cgrp =
4630 rcu_dereference_raw(cg->subsys[i])->cgroup;
4631 struct cgroup *cgrp = task_cgroup(tsk, i);
4632 ss->exit(ss, cgrp, old_cgrp, tsk);
4633 }
4634 }
4635 }
Paul Menageb4f48b62007-10-18 23:39:33 -07004636 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004637
Paul Menage817929e2007-10-18 23:39:36 -07004638 if (cg)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004639 put_css_set_taskexit(cg);
Paul Menageb4f48b62007-10-18 23:39:33 -07004640}
Paul Menage697f4162007-10-18 23:39:34 -07004641
4642/**
Grzegorz Nosek313e9242009-04-02 16:57:23 -07004643 * cgroup_is_descendant - see if @cgrp is a descendant of @task's cgrp
Li Zefana043e3b2008-02-23 15:24:09 -08004644 * @cgrp: the cgroup in question
Grzegorz Nosek313e9242009-04-02 16:57:23 -07004645 * @task: the task in question
Li Zefana043e3b2008-02-23 15:24:09 -08004646 *
Grzegorz Nosek313e9242009-04-02 16:57:23 -07004647 * See if @cgrp is a descendant of @task's cgroup in the appropriate
4648 * hierarchy.
Paul Menage697f4162007-10-18 23:39:34 -07004649 *
4650 * If we are sending in dummytop, then presumably we are creating
4651 * the top cgroup in the subsystem.
4652 *
4653 * Called only by the ns (nsproxy) cgroup.
4654 */
Grzegorz Nosek313e9242009-04-02 16:57:23 -07004655int cgroup_is_descendant(const struct cgroup *cgrp, struct task_struct *task)
Paul Menage697f4162007-10-18 23:39:34 -07004656{
4657 int ret;
4658 struct cgroup *target;
Paul Menage697f4162007-10-18 23:39:34 -07004659
Paul Menagebd89aab2007-10-18 23:40:44 -07004660 if (cgrp == dummytop)
Paul Menage697f4162007-10-18 23:39:34 -07004661 return 1;
4662
Paul Menage7717f7b2009-09-23 15:56:22 -07004663 target = task_cgroup_from_root(task, cgrp->root);
Paul Menagebd89aab2007-10-18 23:40:44 -07004664 while (cgrp != target && cgrp!= cgrp->top_cgroup)
4665 cgrp = cgrp->parent;
4666 ret = (cgrp == target);
Paul Menage697f4162007-10-18 23:39:34 -07004667 return ret;
4668}
Paul Menage81a6a5c2007-10-18 23:39:38 -07004669
Paul Menagebd89aab2007-10-18 23:40:44 -07004670static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004671{
4672 /* All of these checks rely on RCU to keep the cgroup
4673 * structure alive */
Paul Menagebd89aab2007-10-18 23:40:44 -07004674 if (cgroup_is_releasable(cgrp) && !atomic_read(&cgrp->count)
4675 && list_empty(&cgrp->children) && !cgroup_has_css_refs(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07004676 /* Control Group is currently removeable. If it's not
4677 * already queued for a userspace notification, queue
4678 * it now */
4679 int need_schedule_work = 0;
4680 spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07004681 if (!cgroup_is_removed(cgrp) &&
4682 list_empty(&cgrp->release_list)) {
4683 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004684 need_schedule_work = 1;
4685 }
4686 spin_unlock(&release_list_lock);
4687 if (need_schedule_work)
4688 schedule_work(&release_agent_work);
4689 }
4690}
4691
Daisuke Nishimurad7b9fff2010-03-10 15:22:05 -08004692/* Caller must verify that the css is not for root cgroup */
4693void __css_put(struct cgroup_subsys_state *css, int count)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004694{
Paul Menagebd89aab2007-10-18 23:40:44 -07004695 struct cgroup *cgrp = css->cgroup;
KAMEZAWA Hiroyuki3dece832009-10-01 15:44:09 -07004696 int val;
Paul Menage81a6a5c2007-10-18 23:39:38 -07004697 rcu_read_lock();
Daisuke Nishimurad7b9fff2010-03-10 15:22:05 -08004698 val = atomic_sub_return(count, &css->refcnt);
KAMEZAWA Hiroyuki3dece832009-10-01 15:44:09 -07004699 if (val == 1) {
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004700 if (notify_on_release(cgrp)) {
4701 set_bit(CGRP_RELEASABLE, &cgrp->flags);
4702 check_for_release(cgrp);
4703 }
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07004704 cgroup_wakeup_rmdir_waiter(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004705 }
4706 rcu_read_unlock();
KAMEZAWA Hiroyuki3dece832009-10-01 15:44:09 -07004707 WARN_ON_ONCE(val < 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004708}
Ben Blum67523c42010-03-10 15:22:11 -08004709EXPORT_SYMBOL_GPL(__css_put);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004710
4711/*
4712 * Notify userspace when a cgroup is released, by running the
4713 * configured release agent with the name of the cgroup (path
4714 * relative to the root of cgroup file system) as the argument.
4715 *
4716 * Most likely, this user command will try to rmdir this cgroup.
4717 *
4718 * This races with the possibility that some other task will be
4719 * attached to this cgroup before it is removed, or that some other
4720 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
4721 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
4722 * unused, and this cgroup will be reprieved from its death sentence,
4723 * to continue to serve a useful existence. Next time it's released,
4724 * we will get notified again, if it still has 'notify_on_release' set.
4725 *
4726 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
4727 * means only wait until the task is successfully execve()'d. The
4728 * separate release agent task is forked by call_usermodehelper(),
4729 * then control in this thread returns here, without waiting for the
4730 * release agent task. We don't bother to wait because the caller of
4731 * this routine has no use for the exit status of the release agent
4732 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07004733 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07004734static void cgroup_release_agent(struct work_struct *work)
4735{
4736 BUG_ON(work != &release_agent_work);
4737 mutex_lock(&cgroup_mutex);
4738 spin_lock(&release_list_lock);
4739 while (!list_empty(&release_list)) {
4740 char *argv[3], *envp[3];
4741 int i;
Paul Menagee788e062008-07-25 01:46:59 -07004742 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07004743 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07004744 struct cgroup,
4745 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07004746 list_del_init(&cgrp->release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004747 spin_unlock(&release_list_lock);
4748 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07004749 if (!pathbuf)
4750 goto continue_free;
4751 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
4752 goto continue_free;
4753 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
4754 if (!agentbuf)
4755 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07004756
4757 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07004758 argv[i++] = agentbuf;
4759 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07004760 argv[i] = NULL;
4761
4762 i = 0;
4763 /* minimal command environment */
4764 envp[i++] = "HOME=/";
4765 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
4766 envp[i] = NULL;
4767
4768 /* Drop the lock while we invoke the usermode helper,
4769 * since the exec could involve hitting disk and hence
4770 * be a slow process */
4771 mutex_unlock(&cgroup_mutex);
4772 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004773 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07004774 continue_free:
4775 kfree(pathbuf);
4776 kfree(agentbuf);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004777 spin_lock(&release_list_lock);
4778 }
4779 spin_unlock(&release_list_lock);
4780 mutex_unlock(&cgroup_mutex);
4781}
Paul Menage8bab8dd2008-04-04 14:29:57 -07004782
4783static int __init cgroup_disable(char *str)
4784{
4785 int i;
4786 char *token;
4787
4788 while ((token = strsep(&str, ",")) != NULL) {
4789 if (!*token)
4790 continue;
Ben Blumaae8aab2010-03-10 15:22:07 -08004791 /*
4792 * cgroup_disable, being at boot time, can't know about module
4793 * subsystems, so we don't worry about them.
4794 */
4795 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07004796 struct cgroup_subsys *ss = subsys[i];
4797
4798 if (!strcmp(token, ss->name)) {
4799 ss->disabled = 1;
4800 printk(KERN_INFO "Disabling %s control group"
4801 " subsystem\n", ss->name);
4802 break;
4803 }
4804 }
4805 }
4806 return 1;
4807}
4808__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004809
4810/*
4811 * Functons for CSS ID.
4812 */
4813
4814/*
4815 *To get ID other than 0, this should be called when !cgroup_is_removed().
4816 */
4817unsigned short css_id(struct cgroup_subsys_state *css)
4818{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07004819 struct css_id *cssid;
4820
4821 /*
4822 * This css_id() can return correct value when somone has refcnt
4823 * on this or this is under rcu_read_lock(). Once css->id is allocated,
4824 * it's unchanged until freed.
4825 */
4826 cssid = rcu_dereference_check(css->id,
4827 rcu_read_lock_held() || atomic_read(&css->refcnt));
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004828
4829 if (cssid)
4830 return cssid->id;
4831 return 0;
4832}
Ben Blum67523c42010-03-10 15:22:11 -08004833EXPORT_SYMBOL_GPL(css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004834
4835unsigned short css_depth(struct cgroup_subsys_state *css)
4836{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07004837 struct css_id *cssid;
4838
4839 cssid = rcu_dereference_check(css->id,
4840 rcu_read_lock_held() || atomic_read(&css->refcnt));
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004841
4842 if (cssid)
4843 return cssid->depth;
4844 return 0;
4845}
Ben Blum67523c42010-03-10 15:22:11 -08004846EXPORT_SYMBOL_GPL(css_depth);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004847
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07004848/**
4849 * css_is_ancestor - test "root" css is an ancestor of "child"
4850 * @child: the css to be tested.
4851 * @root: the css supporsed to be an ancestor of the child.
4852 *
4853 * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
4854 * this function reads css->id, this use rcu_dereference() and rcu_read_lock().
4855 * But, considering usual usage, the csses should be valid objects after test.
4856 * Assuming that the caller will do some action to the child if this returns
4857 * returns true, the caller must take "child";s reference count.
4858 * If "child" is valid object and this returns true, "root" is valid, too.
4859 */
4860
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004861bool css_is_ancestor(struct cgroup_subsys_state *child,
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07004862 const struct cgroup_subsys_state *root)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004863{
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07004864 struct css_id *child_id;
4865 struct css_id *root_id;
4866 bool ret = true;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004867
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07004868 rcu_read_lock();
4869 child_id = rcu_dereference(child->id);
4870 root_id = rcu_dereference(root->id);
4871 if (!child_id
4872 || !root_id
4873 || (child_id->depth < root_id->depth)
4874 || (child_id->stack[root_id->depth] != root_id->id))
4875 ret = false;
4876 rcu_read_unlock();
4877 return ret;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004878}
4879
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004880void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
4881{
4882 struct css_id *id = css->id;
4883 /* When this is called before css_id initialization, id can be NULL */
4884 if (!id)
4885 return;
4886
4887 BUG_ON(!ss->use_id);
4888
4889 rcu_assign_pointer(id->css, NULL);
4890 rcu_assign_pointer(css->id, NULL);
4891 spin_lock(&ss->id_lock);
4892 idr_remove(&ss->idr, id->id);
4893 spin_unlock(&ss->id_lock);
Lai Jiangshan025cea92011-03-15 17:56:10 +08004894 kfree_rcu(id, rcu_head);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004895}
Ben Blum67523c42010-03-10 15:22:11 -08004896EXPORT_SYMBOL_GPL(free_css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004897
4898/*
4899 * This is called by init or create(). Then, calls to this function are
4900 * always serialized (By cgroup_mutex() at create()).
4901 */
4902
4903static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
4904{
4905 struct css_id *newid;
4906 int myid, error, size;
4907
4908 BUG_ON(!ss->use_id);
4909
4910 size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
4911 newid = kzalloc(size, GFP_KERNEL);
4912 if (!newid)
4913 return ERR_PTR(-ENOMEM);
4914 /* get id */
4915 if (unlikely(!idr_pre_get(&ss->idr, GFP_KERNEL))) {
4916 error = -ENOMEM;
4917 goto err_out;
4918 }
4919 spin_lock(&ss->id_lock);
4920 /* Don't use 0. allocates an ID of 1-65535 */
4921 error = idr_get_new_above(&ss->idr, newid, 1, &myid);
4922 spin_unlock(&ss->id_lock);
4923
4924 /* Returns error when there are no free spaces for new ID.*/
4925 if (error) {
4926 error = -ENOSPC;
4927 goto err_out;
4928 }
4929 if (myid > CSS_ID_MAX)
4930 goto remove_idr;
4931
4932 newid->id = myid;
4933 newid->depth = depth;
4934 return newid;
4935remove_idr:
4936 error = -ENOSPC;
4937 spin_lock(&ss->id_lock);
4938 idr_remove(&ss->idr, myid);
4939 spin_unlock(&ss->id_lock);
4940err_out:
4941 kfree(newid);
4942 return ERR_PTR(error);
4943
4944}
4945
Ben Blume6a11052010-03-10 15:22:09 -08004946static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss,
4947 struct cgroup_subsys_state *rootcss)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004948{
4949 struct css_id *newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004950
4951 spin_lock_init(&ss->id_lock);
4952 idr_init(&ss->idr);
4953
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004954 newid = get_new_cssid(ss, 0);
4955 if (IS_ERR(newid))
4956 return PTR_ERR(newid);
4957
4958 newid->stack[0] = newid->id;
4959 newid->css = rootcss;
4960 rootcss->id = newid;
4961 return 0;
4962}
4963
4964static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent,
4965 struct cgroup *child)
4966{
4967 int subsys_id, i, depth = 0;
4968 struct cgroup_subsys_state *parent_css, *child_css;
Li Zefanfae9c792010-04-22 17:30:00 +08004969 struct css_id *child_id, *parent_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004970
4971 subsys_id = ss->subsys_id;
4972 parent_css = parent->subsys[subsys_id];
4973 child_css = child->subsys[subsys_id];
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004974 parent_id = parent_css->id;
Greg Thelen94b3dd02010-06-04 14:15:03 -07004975 depth = parent_id->depth + 1;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004976
4977 child_id = get_new_cssid(ss, depth);
4978 if (IS_ERR(child_id))
4979 return PTR_ERR(child_id);
4980
4981 for (i = 0; i < depth; i++)
4982 child_id->stack[i] = parent_id->stack[i];
4983 child_id->stack[depth] = child_id->id;
4984 /*
4985 * child_id->css pointer will be set after this cgroup is available
4986 * see cgroup_populate_dir()
4987 */
4988 rcu_assign_pointer(child_css->id, child_id);
4989
4990 return 0;
4991}
4992
4993/**
4994 * css_lookup - lookup css by id
4995 * @ss: cgroup subsys to be looked into.
4996 * @id: the id
4997 *
4998 * Returns pointer to cgroup_subsys_state if there is valid one with id.
4999 * NULL if not. Should be called under rcu_read_lock()
5000 */
5001struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
5002{
5003 struct css_id *cssid = NULL;
5004
5005 BUG_ON(!ss->use_id);
5006 cssid = idr_find(&ss->idr, id);
5007
5008 if (unlikely(!cssid))
5009 return NULL;
5010
5011 return rcu_dereference(cssid->css);
5012}
Ben Blum67523c42010-03-10 15:22:11 -08005013EXPORT_SYMBOL_GPL(css_lookup);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005014
5015/**
5016 * css_get_next - lookup next cgroup under specified hierarchy.
5017 * @ss: pointer to subsystem
5018 * @id: current position of iteration.
5019 * @root: pointer to css. search tree under this.
5020 * @foundid: position of found object.
5021 *
5022 * Search next css under the specified hierarchy of rootid. Calling under
5023 * rcu_read_lock() is necessary. Returns NULL if it reaches the end.
5024 */
5025struct cgroup_subsys_state *
5026css_get_next(struct cgroup_subsys *ss, int id,
5027 struct cgroup_subsys_state *root, int *foundid)
5028{
5029 struct cgroup_subsys_state *ret = NULL;
5030 struct css_id *tmp;
5031 int tmpid;
5032 int rootid = css_id(root);
5033 int depth = css_depth(root);
5034
5035 if (!rootid)
5036 return NULL;
5037
5038 BUG_ON(!ss->use_id);
5039 /* fill start point for scan */
5040 tmpid = id;
5041 while (1) {
5042 /*
5043 * scan next entry from bitmap(tree), tmpid is updated after
5044 * idr_get_next().
5045 */
5046 spin_lock(&ss->id_lock);
5047 tmp = idr_get_next(&ss->idr, &tmpid);
5048 spin_unlock(&ss->id_lock);
5049
5050 if (!tmp)
5051 break;
5052 if (tmp->depth >= depth && tmp->stack[depth] == rootid) {
5053 ret = rcu_dereference(tmp->css);
5054 if (ret) {
5055 *foundid = tmpid;
5056 break;
5057 }
5058 }
5059 /* continue to scan from next id */
5060 tmpid = tmpid + 1;
5061 }
5062 return ret;
5063}
5064
Stephane Eraniane5d13672011-02-14 11:20:01 +02005065/*
5066 * get corresponding css from file open on cgroupfs directory
5067 */
5068struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id)
5069{
5070 struct cgroup *cgrp;
5071 struct inode *inode;
5072 struct cgroup_subsys_state *css;
5073
5074 inode = f->f_dentry->d_inode;
5075 /* check in cgroup filesystem dir */
5076 if (inode->i_op != &cgroup_dir_inode_operations)
5077 return ERR_PTR(-EBADF);
5078
5079 if (id < 0 || id >= CGROUP_SUBSYS_COUNT)
5080 return ERR_PTR(-EINVAL);
5081
5082 /* get cgroup */
5083 cgrp = __d_cgrp(f->f_dentry);
5084 css = cgrp->subsys[id];
5085 return css ? css : ERR_PTR(-ENOENT);
5086}
5087
Paul Menagefe693432009-09-23 15:56:20 -07005088#ifdef CONFIG_CGROUP_DEBUG
5089static struct cgroup_subsys_state *debug_create(struct cgroup_subsys *ss,
5090 struct cgroup *cont)
5091{
5092 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5093
5094 if (!css)
5095 return ERR_PTR(-ENOMEM);
5096
5097 return css;
5098}
5099
5100static void debug_destroy(struct cgroup_subsys *ss, struct cgroup *cont)
5101{
5102 kfree(cont->subsys[debug_subsys_id]);
5103}
5104
5105static u64 cgroup_refcount_read(struct cgroup *cont, struct cftype *cft)
5106{
5107 return atomic_read(&cont->count);
5108}
5109
5110static u64 debug_taskcount_read(struct cgroup *cont, struct cftype *cft)
5111{
5112 return cgroup_task_count(cont);
5113}
5114
5115static u64 current_css_set_read(struct cgroup *cont, struct cftype *cft)
5116{
5117 return (u64)(unsigned long)current->cgroups;
5118}
5119
5120static u64 current_css_set_refcount_read(struct cgroup *cont,
5121 struct cftype *cft)
5122{
5123 u64 count;
5124
5125 rcu_read_lock();
5126 count = atomic_read(&current->cgroups->refcount);
5127 rcu_read_unlock();
5128 return count;
5129}
5130
Paul Menage7717f7b2009-09-23 15:56:22 -07005131static int current_css_set_cg_links_read(struct cgroup *cont,
5132 struct cftype *cft,
5133 struct seq_file *seq)
5134{
5135 struct cg_cgroup_link *link;
5136 struct css_set *cg;
5137
5138 read_lock(&css_set_lock);
5139 rcu_read_lock();
5140 cg = rcu_dereference(current->cgroups);
5141 list_for_each_entry(link, &cg->cg_links, cg_link_list) {
5142 struct cgroup *c = link->cgrp;
5143 const char *name;
5144
5145 if (c->dentry)
5146 name = c->dentry->d_name.name;
5147 else
5148 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005149 seq_printf(seq, "Root %d group %s\n",
5150 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07005151 }
5152 rcu_read_unlock();
5153 read_unlock(&css_set_lock);
5154 return 0;
5155}
5156
5157#define MAX_TASKS_SHOWN_PER_CSS 25
5158static int cgroup_css_links_read(struct cgroup *cont,
5159 struct cftype *cft,
5160 struct seq_file *seq)
5161{
5162 struct cg_cgroup_link *link;
5163
5164 read_lock(&css_set_lock);
5165 list_for_each_entry(link, &cont->css_sets, cgrp_link_list) {
5166 struct css_set *cg = link->cg;
5167 struct task_struct *task;
5168 int count = 0;
5169 seq_printf(seq, "css_set %p\n", cg);
5170 list_for_each_entry(task, &cg->tasks, cg_list) {
5171 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5172 seq_puts(seq, " ...\n");
5173 break;
5174 } else {
5175 seq_printf(seq, " task %d\n",
5176 task_pid_vnr(task));
5177 }
5178 }
5179 }
5180 read_unlock(&css_set_lock);
5181 return 0;
5182}
5183
Paul Menagefe693432009-09-23 15:56:20 -07005184static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft)
5185{
5186 return test_bit(CGRP_RELEASABLE, &cgrp->flags);
5187}
5188
5189static struct cftype debug_files[] = {
5190 {
5191 .name = "cgroup_refcount",
5192 .read_u64 = cgroup_refcount_read,
5193 },
5194 {
5195 .name = "taskcount",
5196 .read_u64 = debug_taskcount_read,
5197 },
5198
5199 {
5200 .name = "current_css_set",
5201 .read_u64 = current_css_set_read,
5202 },
5203
5204 {
5205 .name = "current_css_set_refcount",
5206 .read_u64 = current_css_set_refcount_read,
5207 },
5208
5209 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005210 .name = "current_css_set_cg_links",
5211 .read_seq_string = current_css_set_cg_links_read,
5212 },
5213
5214 {
5215 .name = "cgroup_css_links",
5216 .read_seq_string = cgroup_css_links_read,
5217 },
5218
5219 {
Paul Menagefe693432009-09-23 15:56:20 -07005220 .name = "releasable",
5221 .read_u64 = releasable_read,
5222 },
5223};
5224
5225static int debug_populate(struct cgroup_subsys *ss, struct cgroup *cont)
5226{
5227 return cgroup_add_files(cont, ss, debug_files,
5228 ARRAY_SIZE(debug_files));
5229}
5230
5231struct cgroup_subsys debug_subsys = {
5232 .name = "debug",
5233 .create = debug_create,
5234 .destroy = debug_destroy,
5235 .populate = debug_populate,
5236 .subsys_id = debug_subsys_id,
5237};
5238#endif /* CONFIG_CGROUP_DEBUG */