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