cgroup files: move notify_on_release file to separate write handler
[linux-2.6-block.git] / kernel / cgroup.c
CommitLineData
ddbcc7e8 1/*
ddbcc7e8
PM
2 * 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>
26#include <linux/errno.h>
27#include <linux/fs.h>
28#include <linux/kernel.h>
29#include <linux/list.h>
30#include <linux/mm.h>
31#include <linux/mutex.h>
32#include <linux/mount.h>
33#include <linux/pagemap.h>
a424316c 34#include <linux/proc_fs.h>
ddbcc7e8
PM
35#include <linux/rcupdate.h>
36#include <linux/sched.h>
817929ec 37#include <linux/backing-dev.h>
ddbcc7e8
PM
38#include <linux/seq_file.h>
39#include <linux/slab.h>
40#include <linux/magic.h>
41#include <linux/spinlock.h>
42#include <linux/string.h>
bbcb81d0 43#include <linux/sort.h>
81a6a5cd 44#include <linux/kmod.h>
846c7bb0
BS
45#include <linux/delayacct.h>
46#include <linux/cgroupstats.h>
472b1053 47#include <linux/hash.h>
846c7bb0 48
ddbcc7e8
PM
49#include <asm/atomic.h>
50
81a6a5cd
PM
51static DEFINE_MUTEX(cgroup_mutex);
52
ddbcc7e8
PM
53/* Generate an array of cgroup subsystem pointers */
54#define SUBSYS(_x) &_x ## _subsys,
55
56static struct cgroup_subsys *subsys[] = {
57#include <linux/cgroup_subsys.h>
58};
59
60/*
61 * A cgroupfs_root represents the root of a cgroup hierarchy,
62 * and may be associated with a superblock to form an active
63 * hierarchy
64 */
65struct cgroupfs_root {
66 struct super_block *sb;
67
68 /*
69 * The bitmask of subsystems intended to be attached to this
70 * hierarchy
71 */
72 unsigned long subsys_bits;
73
74 /* The bitmask of subsystems currently attached to this hierarchy */
75 unsigned long actual_subsys_bits;
76
77 /* A list running through the attached subsystems */
78 struct list_head subsys_list;
79
80 /* The root cgroup for this hierarchy */
81 struct cgroup top_cgroup;
82
83 /* Tracks how many cgroups are currently defined in hierarchy.*/
84 int number_of_cgroups;
85
86 /* A list running through the mounted hierarchies */
87 struct list_head root_list;
88
89 /* Hierarchy-specific flags */
90 unsigned long flags;
81a6a5cd 91
e788e066 92 /* The path to use for release notifications. */
81a6a5cd 93 char release_agent_path[PATH_MAX];
ddbcc7e8
PM
94};
95
96
97/*
98 * The "rootnode" hierarchy is the "dummy hierarchy", reserved for the
99 * subsystems that are otherwise unattached - it never has more than a
100 * single cgroup, and all tasks are part of that cgroup.
101 */
102static struct cgroupfs_root rootnode;
103
104/* The list of hierarchy roots */
105
106static LIST_HEAD(roots);
817929ec 107static int root_count;
ddbcc7e8
PM
108
109/* dummytop is a shorthand for the dummy hierarchy's top cgroup */
110#define dummytop (&rootnode.top_cgroup)
111
112/* This flag indicates whether tasks in the fork and exit paths should
a043e3b2
LZ
113 * check for fork/exit handlers to call. This avoids us having to do
114 * extra work in the fork/exit path if none of the subsystems need to
115 * be called.
ddbcc7e8 116 */
8947f9d5 117static int need_forkexit_callback __read_mostly;
cf475ad2 118static int need_mm_owner_callback __read_mostly;
ddbcc7e8 119
ddbcc7e8 120/* convenient tests for these bits */
bd89aabc 121inline int cgroup_is_removed(const struct cgroup *cgrp)
ddbcc7e8 122{
bd89aabc 123 return test_bit(CGRP_REMOVED, &cgrp->flags);
ddbcc7e8
PM
124}
125
126/* bits in struct cgroupfs_root flags field */
127enum {
128 ROOT_NOPREFIX, /* mounted subsystems have no named prefix */
129};
130
e9685a03 131static int cgroup_is_releasable(const struct cgroup *cgrp)
81a6a5cd
PM
132{
133 const int bits =
bd89aabc
PM
134 (1 << CGRP_RELEASABLE) |
135 (1 << CGRP_NOTIFY_ON_RELEASE);
136 return (cgrp->flags & bits) == bits;
81a6a5cd
PM
137}
138
e9685a03 139static int notify_on_release(const struct cgroup *cgrp)
81a6a5cd 140{
bd89aabc 141 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
81a6a5cd
PM
142}
143
ddbcc7e8
PM
144/*
145 * for_each_subsys() allows you to iterate on each subsystem attached to
146 * an active hierarchy
147 */
148#define for_each_subsys(_root, _ss) \
149list_for_each_entry(_ss, &_root->subsys_list, sibling)
150
151/* for_each_root() allows you to iterate across the active hierarchies */
152#define for_each_root(_root) \
153list_for_each_entry(_root, &roots, root_list)
154
81a6a5cd
PM
155/* the list of cgroups eligible for automatic release. Protected by
156 * release_list_lock */
157static LIST_HEAD(release_list);
158static DEFINE_SPINLOCK(release_list_lock);
159static void cgroup_release_agent(struct work_struct *work);
160static DECLARE_WORK(release_agent_work, cgroup_release_agent);
bd89aabc 161static void check_for_release(struct cgroup *cgrp);
81a6a5cd 162
817929ec
PM
163/* Link structure for associating css_set objects with cgroups */
164struct cg_cgroup_link {
165 /*
166 * List running through cg_cgroup_links associated with a
167 * cgroup, anchored on cgroup->css_sets
168 */
bd89aabc 169 struct list_head cgrp_link_list;
817929ec
PM
170 /*
171 * List running through cg_cgroup_links pointing at a
172 * single css_set object, anchored on css_set->cg_links
173 */
174 struct list_head cg_link_list;
175 struct css_set *cg;
176};
177
178/* The default css_set - used by init and its children prior to any
179 * hierarchies being mounted. It contains a pointer to the root state
180 * for each subsystem. Also used to anchor the list of css_sets. Not
181 * reference-counted, to improve performance when child cgroups
182 * haven't been created.
183 */
184
185static struct css_set init_css_set;
186static struct cg_cgroup_link init_css_set_link;
187
188/* css_set_lock protects the list of css_set objects, and the
189 * chain of tasks off each css_set. Nests outside task->alloc_lock
190 * due to cgroup_iter_start() */
191static DEFINE_RWLOCK(css_set_lock);
192static int css_set_count;
193
472b1053
LZ
194/* hash table for cgroup groups. This improves the performance to
195 * find an existing css_set */
196#define CSS_SET_HASH_BITS 7
197#define CSS_SET_TABLE_SIZE (1 << CSS_SET_HASH_BITS)
198static struct hlist_head css_set_table[CSS_SET_TABLE_SIZE];
199
200static struct hlist_head *css_set_hash(struct cgroup_subsys_state *css[])
201{
202 int i;
203 int index;
204 unsigned long tmp = 0UL;
205
206 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++)
207 tmp += (unsigned long)css[i];
208 tmp = (tmp >> 16) ^ tmp;
209
210 index = hash_long(tmp, CSS_SET_HASH_BITS);
211
212 return &css_set_table[index];
213}
214
817929ec
PM
215/* We don't maintain the lists running through each css_set to its
216 * task until after the first call to cgroup_iter_start(). This
217 * reduces the fork()/exit() overhead for people who have cgroups
218 * compiled into their kernel but not actually in use */
8947f9d5 219static int use_task_css_set_links __read_mostly;
817929ec
PM
220
221/* When we create or destroy a css_set, the operation simply
222 * takes/releases a reference count on all the cgroups referenced
223 * by subsystems in this css_set. This can end up multiple-counting
224 * some cgroups, but that's OK - the ref-count is just a
225 * busy/not-busy indicator; ensuring that we only count each cgroup
226 * once would require taking a global lock to ensure that no
b4f48b63
PM
227 * subsystems moved between hierarchies while we were doing so.
228 *
229 * Possible TODO: decide at boot time based on the number of
230 * registered subsystems and the number of CPUs or NUMA nodes whether
231 * it's better for performance to ref-count every subsystem, or to
232 * take a global lock and only add one ref count to each hierarchy.
233 */
817929ec
PM
234
235/*
236 * unlink a css_set from the list and free it
237 */
81a6a5cd 238static void unlink_css_set(struct css_set *cg)
b4f48b63 239{
71cbb949
KM
240 struct cg_cgroup_link *link;
241 struct cg_cgroup_link *saved_link;
242
817929ec 243 write_lock(&css_set_lock);
472b1053 244 hlist_del(&cg->hlist);
817929ec 245 css_set_count--;
71cbb949
KM
246
247 list_for_each_entry_safe(link, saved_link, &cg->cg_links,
248 cg_link_list) {
817929ec 249 list_del(&link->cg_link_list);
bd89aabc 250 list_del(&link->cgrp_link_list);
817929ec
PM
251 kfree(link);
252 }
71cbb949 253
817929ec 254 write_unlock(&css_set_lock);
81a6a5cd
PM
255}
256
257static void __release_css_set(struct kref *k, int taskexit)
258{
259 int i;
260 struct css_set *cg = container_of(k, struct css_set, ref);
261
262 unlink_css_set(cg);
263
264 rcu_read_lock();
265 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
bd89aabc
PM
266 struct cgroup *cgrp = cg->subsys[i]->cgroup;
267 if (atomic_dec_and_test(&cgrp->count) &&
268 notify_on_release(cgrp)) {
81a6a5cd 269 if (taskexit)
bd89aabc
PM
270 set_bit(CGRP_RELEASABLE, &cgrp->flags);
271 check_for_release(cgrp);
81a6a5cd
PM
272 }
273 }
274 rcu_read_unlock();
817929ec 275 kfree(cg);
b4f48b63
PM
276}
277
81a6a5cd
PM
278static void release_css_set(struct kref *k)
279{
280 __release_css_set(k, 0);
281}
282
283static void release_css_set_taskexit(struct kref *k)
284{
285 __release_css_set(k, 1);
286}
287
817929ec
PM
288/*
289 * refcounted get/put for css_set objects
290 */
291static inline void get_css_set(struct css_set *cg)
292{
293 kref_get(&cg->ref);
294}
295
296static inline void put_css_set(struct css_set *cg)
297{
298 kref_put(&cg->ref, release_css_set);
299}
300
81a6a5cd
PM
301static inline void put_css_set_taskexit(struct css_set *cg)
302{
303 kref_put(&cg->ref, release_css_set_taskexit);
304}
305
817929ec
PM
306/*
307 * find_existing_css_set() is a helper for
308 * find_css_set(), and checks to see whether an existing
472b1053 309 * css_set is suitable.
817929ec
PM
310 *
311 * oldcg: the cgroup group that we're using before the cgroup
312 * transition
313 *
bd89aabc 314 * cgrp: the cgroup that we're moving into
817929ec
PM
315 *
316 * template: location in which to build the desired set of subsystem
317 * state objects for the new cgroup group
318 */
817929ec
PM
319static struct css_set *find_existing_css_set(
320 struct css_set *oldcg,
bd89aabc 321 struct cgroup *cgrp,
817929ec 322 struct cgroup_subsys_state *template[])
b4f48b63
PM
323{
324 int i;
bd89aabc 325 struct cgroupfs_root *root = cgrp->root;
472b1053
LZ
326 struct hlist_head *hhead;
327 struct hlist_node *node;
328 struct css_set *cg;
817929ec
PM
329
330 /* Built the set of subsystem state objects that we want to
331 * see in the new css_set */
332 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
8d53d55d 333 if (root->subsys_bits & (1UL << i)) {
817929ec
PM
334 /* Subsystem is in this hierarchy. So we want
335 * the subsystem state from the new
336 * cgroup */
bd89aabc 337 template[i] = cgrp->subsys[i];
817929ec
PM
338 } else {
339 /* Subsystem is not in this hierarchy, so we
340 * don't want to change the subsystem state */
341 template[i] = oldcg->subsys[i];
342 }
343 }
344
472b1053
LZ
345 hhead = css_set_hash(template);
346 hlist_for_each_entry(cg, node, hhead, hlist) {
817929ec
PM
347 if (!memcmp(template, cg->subsys, sizeof(cg->subsys))) {
348 /* All subsystems matched */
349 return cg;
350 }
472b1053 351 }
817929ec
PM
352
353 /* No existing cgroup group matched */
354 return NULL;
355}
356
357/*
358 * allocate_cg_links() allocates "count" cg_cgroup_link structures
bd89aabc 359 * and chains them on tmp through their cgrp_link_list fields. Returns 0 on
817929ec
PM
360 * success or a negative error
361 */
817929ec
PM
362static int allocate_cg_links(int count, struct list_head *tmp)
363{
364 struct cg_cgroup_link *link;
71cbb949 365 struct cg_cgroup_link *saved_link;
817929ec
PM
366 int i;
367 INIT_LIST_HEAD(tmp);
368 for (i = 0; i < count; i++) {
369 link = kmalloc(sizeof(*link), GFP_KERNEL);
370 if (!link) {
71cbb949
KM
371 list_for_each_entry_safe(link, saved_link, tmp,
372 cgrp_link_list) {
bd89aabc 373 list_del(&link->cgrp_link_list);
817929ec
PM
374 kfree(link);
375 }
376 return -ENOMEM;
377 }
bd89aabc 378 list_add(&link->cgrp_link_list, tmp);
817929ec
PM
379 }
380 return 0;
381}
382
383static void free_cg_links(struct list_head *tmp)
384{
71cbb949
KM
385 struct cg_cgroup_link *link;
386 struct cg_cgroup_link *saved_link;
387
388 list_for_each_entry_safe(link, saved_link, tmp, cgrp_link_list) {
bd89aabc 389 list_del(&link->cgrp_link_list);
817929ec
PM
390 kfree(link);
391 }
392}
393
394/*
395 * find_css_set() takes an existing cgroup group and a
396 * cgroup object, and returns a css_set object that's
397 * equivalent to the old group, but with the given cgroup
398 * substituted into the appropriate hierarchy. Must be called with
399 * cgroup_mutex held
400 */
817929ec 401static struct css_set *find_css_set(
bd89aabc 402 struct css_set *oldcg, struct cgroup *cgrp)
817929ec
PM
403{
404 struct css_set *res;
405 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT];
406 int i;
407
408 struct list_head tmp_cg_links;
409 struct cg_cgroup_link *link;
410
472b1053
LZ
411 struct hlist_head *hhead;
412
817929ec
PM
413 /* First see if we already have a cgroup group that matches
414 * the desired set */
7e9abd89 415 read_lock(&css_set_lock);
bd89aabc 416 res = find_existing_css_set(oldcg, cgrp, template);
817929ec
PM
417 if (res)
418 get_css_set(res);
7e9abd89 419 read_unlock(&css_set_lock);
817929ec
PM
420
421 if (res)
422 return res;
423
424 res = kmalloc(sizeof(*res), GFP_KERNEL);
425 if (!res)
426 return NULL;
427
428 /* Allocate all the cg_cgroup_link objects that we'll need */
429 if (allocate_cg_links(root_count, &tmp_cg_links) < 0) {
430 kfree(res);
431 return NULL;
432 }
433
434 kref_init(&res->ref);
435 INIT_LIST_HEAD(&res->cg_links);
436 INIT_LIST_HEAD(&res->tasks);
472b1053 437 INIT_HLIST_NODE(&res->hlist);
817929ec
PM
438
439 /* Copy the set of subsystem state objects generated in
440 * find_existing_css_set() */
441 memcpy(res->subsys, template, sizeof(res->subsys));
442
443 write_lock(&css_set_lock);
444 /* Add reference counts and links from the new css_set. */
445 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
bd89aabc 446 struct cgroup *cgrp = res->subsys[i]->cgroup;
817929ec 447 struct cgroup_subsys *ss = subsys[i];
bd89aabc 448 atomic_inc(&cgrp->count);
817929ec
PM
449 /*
450 * We want to add a link once per cgroup, so we
451 * only do it for the first subsystem in each
452 * hierarchy
453 */
454 if (ss->root->subsys_list.next == &ss->sibling) {
455 BUG_ON(list_empty(&tmp_cg_links));
456 link = list_entry(tmp_cg_links.next,
457 struct cg_cgroup_link,
bd89aabc
PM
458 cgrp_link_list);
459 list_del(&link->cgrp_link_list);
460 list_add(&link->cgrp_link_list, &cgrp->css_sets);
817929ec
PM
461 link->cg = res;
462 list_add(&link->cg_link_list, &res->cg_links);
463 }
464 }
465 if (list_empty(&rootnode.subsys_list)) {
466 link = list_entry(tmp_cg_links.next,
467 struct cg_cgroup_link,
bd89aabc
PM
468 cgrp_link_list);
469 list_del(&link->cgrp_link_list);
470 list_add(&link->cgrp_link_list, &dummytop->css_sets);
817929ec
PM
471 link->cg = res;
472 list_add(&link->cg_link_list, &res->cg_links);
473 }
474
475 BUG_ON(!list_empty(&tmp_cg_links));
476
817929ec 477 css_set_count++;
472b1053
LZ
478
479 /* Add this cgroup group to the hash table */
480 hhead = css_set_hash(res->subsys);
481 hlist_add_head(&res->hlist, hhead);
482
817929ec
PM
483 write_unlock(&css_set_lock);
484
485 return res;
b4f48b63
PM
486}
487
ddbcc7e8
PM
488/*
489 * There is one global cgroup mutex. We also require taking
490 * task_lock() when dereferencing a task's cgroup subsys pointers.
491 * See "The task_lock() exception", at the end of this comment.
492 *
493 * A task must hold cgroup_mutex to modify cgroups.
494 *
495 * Any task can increment and decrement the count field without lock.
496 * So in general, code holding cgroup_mutex can't rely on the count
497 * field not changing. However, if the count goes to zero, then only
956db3ca 498 * cgroup_attach_task() can increment it again. Because a count of zero
ddbcc7e8
PM
499 * means that no tasks are currently attached, therefore there is no
500 * way a task attached to that cgroup can fork (the other way to
501 * increment the count). So code holding cgroup_mutex can safely
502 * assume that if the count is zero, it will stay zero. Similarly, if
503 * a task holds cgroup_mutex on a cgroup with zero count, it
504 * knows that the cgroup won't be removed, as cgroup_rmdir()
505 * needs that mutex.
506 *
507 * The cgroup_common_file_write handler for operations that modify
508 * the cgroup hierarchy holds cgroup_mutex across the entire operation,
509 * single threading all such cgroup modifications across the system.
510 *
511 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
512 * (usually) take cgroup_mutex. These are the two most performance
513 * critical pieces of code here. The exception occurs on cgroup_exit(),
514 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
515 * is taken, and if the cgroup count is zero, a usermode call made
a043e3b2
LZ
516 * to the release agent with the name of the cgroup (path relative to
517 * the root of cgroup file system) as the argument.
ddbcc7e8
PM
518 *
519 * A cgroup can only be deleted if both its 'count' of using tasks
520 * is zero, and its list of 'children' cgroups is empty. Since all
521 * tasks in the system use _some_ cgroup, and since there is always at
522 * least one task in the system (init, pid == 1), therefore, top_cgroup
523 * always has either children cgroups and/or using tasks. So we don't
524 * need a special hack to ensure that top_cgroup cannot be deleted.
525 *
526 * The task_lock() exception
527 *
528 * The need for this exception arises from the action of
956db3ca 529 * cgroup_attach_task(), which overwrites one tasks cgroup pointer with
a043e3b2 530 * another. It does so using cgroup_mutex, however there are
ddbcc7e8
PM
531 * several performance critical places that need to reference
532 * task->cgroup without the expense of grabbing a system global
533 * mutex. Therefore except as noted below, when dereferencing or, as
956db3ca 534 * in cgroup_attach_task(), modifying a task'ss cgroup pointer we use
ddbcc7e8
PM
535 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
536 * the task_struct routinely used for such matters.
537 *
538 * P.S. One more locking exception. RCU is used to guard the
956db3ca 539 * update of a tasks cgroup pointer by cgroup_attach_task()
ddbcc7e8
PM
540 */
541
ddbcc7e8
PM
542/**
543 * cgroup_lock - lock out any changes to cgroup structures
544 *
545 */
ddbcc7e8
PM
546void cgroup_lock(void)
547{
548 mutex_lock(&cgroup_mutex);
549}
550
551/**
552 * cgroup_unlock - release lock on cgroup changes
553 *
554 * Undo the lock taken in a previous cgroup_lock() call.
555 */
ddbcc7e8
PM
556void cgroup_unlock(void)
557{
558 mutex_unlock(&cgroup_mutex);
559}
560
561/*
562 * A couple of forward declarations required, due to cyclic reference loop:
563 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
564 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
565 * -> cgroup_mkdir.
566 */
567
568static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode);
569static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
bd89aabc 570static int cgroup_populate_dir(struct cgroup *cgrp);
ddbcc7e8 571static struct inode_operations cgroup_dir_inode_operations;
a424316c
PM
572static struct file_operations proc_cgroupstats_operations;
573
574static struct backing_dev_info cgroup_backing_dev_info = {
e4ad08fe 575 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
a424316c 576};
ddbcc7e8
PM
577
578static struct inode *cgroup_new_inode(mode_t mode, struct super_block *sb)
579{
580 struct inode *inode = new_inode(sb);
ddbcc7e8
PM
581
582 if (inode) {
583 inode->i_mode = mode;
584 inode->i_uid = current->fsuid;
585 inode->i_gid = current->fsgid;
586 inode->i_blocks = 0;
587 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
588 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
589 }
590 return inode;
591}
592
4fca88c8
KH
593/*
594 * Call subsys's pre_destroy handler.
595 * This is called before css refcnt check.
596 */
4fca88c8
KH
597static void cgroup_call_pre_destroy(struct cgroup *cgrp)
598{
599 struct cgroup_subsys *ss;
600 for_each_subsys(cgrp->root, ss)
601 if (ss->pre_destroy && cgrp->subsys[ss->subsys_id])
602 ss->pre_destroy(ss, cgrp);
603 return;
604}
605
ddbcc7e8
PM
606static void cgroup_diput(struct dentry *dentry, struct inode *inode)
607{
608 /* is dentry a directory ? if so, kfree() associated cgroup */
609 if (S_ISDIR(inode->i_mode)) {
bd89aabc 610 struct cgroup *cgrp = dentry->d_fsdata;
8dc4f3e1 611 struct cgroup_subsys *ss;
bd89aabc 612 BUG_ON(!(cgroup_is_removed(cgrp)));
81a6a5cd
PM
613 /* It's possible for external users to be holding css
614 * reference counts on a cgroup; css_put() needs to
615 * be able to access the cgroup after decrementing
616 * the reference count in order to know if it needs to
617 * queue the cgroup to be handled by the release
618 * agent */
619 synchronize_rcu();
8dc4f3e1
PM
620
621 mutex_lock(&cgroup_mutex);
622 /*
623 * Release the subsystem state objects.
624 */
625 for_each_subsys(cgrp->root, ss) {
626 if (cgrp->subsys[ss->subsys_id])
627 ss->destroy(ss, cgrp);
628 }
629
630 cgrp->root->number_of_cgroups--;
631 mutex_unlock(&cgroup_mutex);
632
633 /* Drop the active superblock reference that we took when we
634 * created the cgroup */
635 deactivate_super(cgrp->root->sb);
636
bd89aabc 637 kfree(cgrp);
ddbcc7e8
PM
638 }
639 iput(inode);
640}
641
642static void remove_dir(struct dentry *d)
643{
644 struct dentry *parent = dget(d->d_parent);
645
646 d_delete(d);
647 simple_rmdir(parent->d_inode, d);
648 dput(parent);
649}
650
651static void cgroup_clear_directory(struct dentry *dentry)
652{
653 struct list_head *node;
654
655 BUG_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
656 spin_lock(&dcache_lock);
657 node = dentry->d_subdirs.next;
658 while (node != &dentry->d_subdirs) {
659 struct dentry *d = list_entry(node, struct dentry, d_u.d_child);
660 list_del_init(node);
661 if (d->d_inode) {
662 /* This should never be called on a cgroup
663 * directory with child cgroups */
664 BUG_ON(d->d_inode->i_mode & S_IFDIR);
665 d = dget_locked(d);
666 spin_unlock(&dcache_lock);
667 d_delete(d);
668 simple_unlink(dentry->d_inode, d);
669 dput(d);
670 spin_lock(&dcache_lock);
671 }
672 node = dentry->d_subdirs.next;
673 }
674 spin_unlock(&dcache_lock);
675}
676
677/*
678 * NOTE : the dentry must have been dget()'ed
679 */
680static void cgroup_d_remove_dir(struct dentry *dentry)
681{
682 cgroup_clear_directory(dentry);
683
684 spin_lock(&dcache_lock);
685 list_del_init(&dentry->d_u.d_child);
686 spin_unlock(&dcache_lock);
687 remove_dir(dentry);
688}
689
690static int rebind_subsystems(struct cgroupfs_root *root,
691 unsigned long final_bits)
692{
693 unsigned long added_bits, removed_bits;
bd89aabc 694 struct cgroup *cgrp = &root->top_cgroup;
ddbcc7e8
PM
695 int i;
696
697 removed_bits = root->actual_subsys_bits & ~final_bits;
698 added_bits = final_bits & ~root->actual_subsys_bits;
699 /* Check that any added subsystems are currently free */
700 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
8d53d55d 701 unsigned long bit = 1UL << i;
ddbcc7e8
PM
702 struct cgroup_subsys *ss = subsys[i];
703 if (!(bit & added_bits))
704 continue;
705 if (ss->root != &rootnode) {
706 /* Subsystem isn't free */
707 return -EBUSY;
708 }
709 }
710
711 /* Currently we don't handle adding/removing subsystems when
712 * any child cgroups exist. This is theoretically supportable
713 * but involves complex error handling, so it's being left until
714 * later */
bd89aabc 715 if (!list_empty(&cgrp->children))
ddbcc7e8
PM
716 return -EBUSY;
717
718 /* Process each subsystem */
719 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
720 struct cgroup_subsys *ss = subsys[i];
721 unsigned long bit = 1UL << i;
722 if (bit & added_bits) {
723 /* We're binding this subsystem to this hierarchy */
bd89aabc 724 BUG_ON(cgrp->subsys[i]);
ddbcc7e8
PM
725 BUG_ON(!dummytop->subsys[i]);
726 BUG_ON(dummytop->subsys[i]->cgroup != dummytop);
bd89aabc
PM
727 cgrp->subsys[i] = dummytop->subsys[i];
728 cgrp->subsys[i]->cgroup = cgrp;
ddbcc7e8
PM
729 list_add(&ss->sibling, &root->subsys_list);
730 rcu_assign_pointer(ss->root, root);
731 if (ss->bind)
bd89aabc 732 ss->bind(ss, cgrp);
ddbcc7e8
PM
733
734 } else if (bit & removed_bits) {
735 /* We're removing this subsystem */
bd89aabc
PM
736 BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]);
737 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
ddbcc7e8
PM
738 if (ss->bind)
739 ss->bind(ss, dummytop);
740 dummytop->subsys[i]->cgroup = dummytop;
bd89aabc 741 cgrp->subsys[i] = NULL;
ddbcc7e8
PM
742 rcu_assign_pointer(subsys[i]->root, &rootnode);
743 list_del(&ss->sibling);
744 } else if (bit & final_bits) {
745 /* Subsystem state should already exist */
bd89aabc 746 BUG_ON(!cgrp->subsys[i]);
ddbcc7e8
PM
747 } else {
748 /* Subsystem state shouldn't exist */
bd89aabc 749 BUG_ON(cgrp->subsys[i]);
ddbcc7e8
PM
750 }
751 }
752 root->subsys_bits = root->actual_subsys_bits = final_bits;
753 synchronize_rcu();
754
755 return 0;
756}
757
758static int cgroup_show_options(struct seq_file *seq, struct vfsmount *vfs)
759{
760 struct cgroupfs_root *root = vfs->mnt_sb->s_fs_info;
761 struct cgroup_subsys *ss;
762
763 mutex_lock(&cgroup_mutex);
764 for_each_subsys(root, ss)
765 seq_printf(seq, ",%s", ss->name);
766 if (test_bit(ROOT_NOPREFIX, &root->flags))
767 seq_puts(seq, ",noprefix");
81a6a5cd
PM
768 if (strlen(root->release_agent_path))
769 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
ddbcc7e8
PM
770 mutex_unlock(&cgroup_mutex);
771 return 0;
772}
773
774struct cgroup_sb_opts {
775 unsigned long subsys_bits;
776 unsigned long flags;
81a6a5cd 777 char *release_agent;
ddbcc7e8
PM
778};
779
780/* Convert a hierarchy specifier into a bitmask of subsystems and
781 * flags. */
782static int parse_cgroupfs_options(char *data,
783 struct cgroup_sb_opts *opts)
784{
785 char *token, *o = data ?: "all";
786
787 opts->subsys_bits = 0;
788 opts->flags = 0;
81a6a5cd 789 opts->release_agent = NULL;
ddbcc7e8
PM
790
791 while ((token = strsep(&o, ",")) != NULL) {
792 if (!*token)
793 return -EINVAL;
794 if (!strcmp(token, "all")) {
8bab8dde
PM
795 /* Add all non-disabled subsystems */
796 int i;
797 opts->subsys_bits = 0;
798 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
799 struct cgroup_subsys *ss = subsys[i];
800 if (!ss->disabled)
801 opts->subsys_bits |= 1ul << i;
802 }
ddbcc7e8
PM
803 } else if (!strcmp(token, "noprefix")) {
804 set_bit(ROOT_NOPREFIX, &opts->flags);
81a6a5cd
PM
805 } else if (!strncmp(token, "release_agent=", 14)) {
806 /* Specifying two release agents is forbidden */
807 if (opts->release_agent)
808 return -EINVAL;
809 opts->release_agent = kzalloc(PATH_MAX, GFP_KERNEL);
810 if (!opts->release_agent)
811 return -ENOMEM;
812 strncpy(opts->release_agent, token + 14, PATH_MAX - 1);
813 opts->release_agent[PATH_MAX - 1] = 0;
ddbcc7e8
PM
814 } else {
815 struct cgroup_subsys *ss;
816 int i;
817 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
818 ss = subsys[i];
819 if (!strcmp(token, ss->name)) {
8bab8dde
PM
820 if (!ss->disabled)
821 set_bit(i, &opts->subsys_bits);
ddbcc7e8
PM
822 break;
823 }
824 }
825 if (i == CGROUP_SUBSYS_COUNT)
826 return -ENOENT;
827 }
828 }
829
830 /* We can't have an empty hierarchy */
831 if (!opts->subsys_bits)
832 return -EINVAL;
833
834 return 0;
835}
836
837static int cgroup_remount(struct super_block *sb, int *flags, char *data)
838{
839 int ret = 0;
840 struct cgroupfs_root *root = sb->s_fs_info;
bd89aabc 841 struct cgroup *cgrp = &root->top_cgroup;
ddbcc7e8
PM
842 struct cgroup_sb_opts opts;
843
bd89aabc 844 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
ddbcc7e8
PM
845 mutex_lock(&cgroup_mutex);
846
847 /* See what subsystems are wanted */
848 ret = parse_cgroupfs_options(data, &opts);
849 if (ret)
850 goto out_unlock;
851
852 /* Don't allow flags to change at remount */
853 if (opts.flags != root->flags) {
854 ret = -EINVAL;
855 goto out_unlock;
856 }
857
858 ret = rebind_subsystems(root, opts.subsys_bits);
859
860 /* (re)populate subsystem files */
861 if (!ret)
bd89aabc 862 cgroup_populate_dir(cgrp);
ddbcc7e8 863
81a6a5cd
PM
864 if (opts.release_agent)
865 strcpy(root->release_agent_path, opts.release_agent);
ddbcc7e8 866 out_unlock:
81a6a5cd
PM
867 if (opts.release_agent)
868 kfree(opts.release_agent);
ddbcc7e8 869 mutex_unlock(&cgroup_mutex);
bd89aabc 870 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
ddbcc7e8
PM
871 return ret;
872}
873
874static struct super_operations cgroup_ops = {
875 .statfs = simple_statfs,
876 .drop_inode = generic_delete_inode,
877 .show_options = cgroup_show_options,
878 .remount_fs = cgroup_remount,
879};
880
881static void init_cgroup_root(struct cgroupfs_root *root)
882{
bd89aabc 883 struct cgroup *cgrp = &root->top_cgroup;
ddbcc7e8
PM
884 INIT_LIST_HEAD(&root->subsys_list);
885 INIT_LIST_HEAD(&root->root_list);
886 root->number_of_cgroups = 1;
bd89aabc
PM
887 cgrp->root = root;
888 cgrp->top_cgroup = cgrp;
889 INIT_LIST_HEAD(&cgrp->sibling);
890 INIT_LIST_HEAD(&cgrp->children);
891 INIT_LIST_HEAD(&cgrp->css_sets);
892 INIT_LIST_HEAD(&cgrp->release_list);
ddbcc7e8
PM
893}
894
895static int cgroup_test_super(struct super_block *sb, void *data)
896{
897 struct cgroupfs_root *new = data;
898 struct cgroupfs_root *root = sb->s_fs_info;
899
900 /* First check subsystems */
901 if (new->subsys_bits != root->subsys_bits)
902 return 0;
903
904 /* Next check flags */
905 if (new->flags != root->flags)
906 return 0;
907
908 return 1;
909}
910
911static int cgroup_set_super(struct super_block *sb, void *data)
912{
913 int ret;
914 struct cgroupfs_root *root = data;
915
916 ret = set_anon_super(sb, NULL);
917 if (ret)
918 return ret;
919
920 sb->s_fs_info = root;
921 root->sb = sb;
922
923 sb->s_blocksize = PAGE_CACHE_SIZE;
924 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
925 sb->s_magic = CGROUP_SUPER_MAGIC;
926 sb->s_op = &cgroup_ops;
927
928 return 0;
929}
930
931static int cgroup_get_rootdir(struct super_block *sb)
932{
933 struct inode *inode =
934 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
935 struct dentry *dentry;
936
937 if (!inode)
938 return -ENOMEM;
939
ddbcc7e8
PM
940 inode->i_fop = &simple_dir_operations;
941 inode->i_op = &cgroup_dir_inode_operations;
942 /* directories start off with i_nlink == 2 (for "." entry) */
943 inc_nlink(inode);
944 dentry = d_alloc_root(inode);
945 if (!dentry) {
946 iput(inode);
947 return -ENOMEM;
948 }
949 sb->s_root = dentry;
950 return 0;
951}
952
953static int cgroup_get_sb(struct file_system_type *fs_type,
954 int flags, const char *unused_dev_name,
955 void *data, struct vfsmount *mnt)
956{
957 struct cgroup_sb_opts opts;
958 int ret = 0;
959 struct super_block *sb;
960 struct cgroupfs_root *root;
28fd5dfc 961 struct list_head tmp_cg_links;
817929ec 962 INIT_LIST_HEAD(&tmp_cg_links);
ddbcc7e8
PM
963
964 /* First find the desired set of subsystems */
965 ret = parse_cgroupfs_options(data, &opts);
81a6a5cd
PM
966 if (ret) {
967 if (opts.release_agent)
968 kfree(opts.release_agent);
ddbcc7e8 969 return ret;
81a6a5cd 970 }
ddbcc7e8
PM
971
972 root = kzalloc(sizeof(*root), GFP_KERNEL);
f7770738
LZ
973 if (!root) {
974 if (opts.release_agent)
975 kfree(opts.release_agent);
ddbcc7e8 976 return -ENOMEM;
f7770738 977 }
ddbcc7e8
PM
978
979 init_cgroup_root(root);
980 root->subsys_bits = opts.subsys_bits;
981 root->flags = opts.flags;
81a6a5cd
PM
982 if (opts.release_agent) {
983 strcpy(root->release_agent_path, opts.release_agent);
984 kfree(opts.release_agent);
985 }
ddbcc7e8
PM
986
987 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, root);
988
989 if (IS_ERR(sb)) {
990 kfree(root);
991 return PTR_ERR(sb);
992 }
993
994 if (sb->s_fs_info != root) {
995 /* Reusing an existing superblock */
996 BUG_ON(sb->s_root == NULL);
997 kfree(root);
998 root = NULL;
999 } else {
1000 /* New superblock */
bd89aabc 1001 struct cgroup *cgrp = &root->top_cgroup;
817929ec 1002 struct inode *inode;
28fd5dfc 1003 int i;
ddbcc7e8
PM
1004
1005 BUG_ON(sb->s_root != NULL);
1006
1007 ret = cgroup_get_rootdir(sb);
1008 if (ret)
1009 goto drop_new_super;
817929ec 1010 inode = sb->s_root->d_inode;
ddbcc7e8 1011
817929ec 1012 mutex_lock(&inode->i_mutex);
ddbcc7e8
PM
1013 mutex_lock(&cgroup_mutex);
1014
817929ec
PM
1015 /*
1016 * We're accessing css_set_count without locking
1017 * css_set_lock here, but that's OK - it can only be
1018 * increased by someone holding cgroup_lock, and
1019 * that's us. The worst that can happen is that we
1020 * have some link structures left over
1021 */
1022 ret = allocate_cg_links(css_set_count, &tmp_cg_links);
1023 if (ret) {
1024 mutex_unlock(&cgroup_mutex);
1025 mutex_unlock(&inode->i_mutex);
1026 goto drop_new_super;
1027 }
1028
ddbcc7e8
PM
1029 ret = rebind_subsystems(root, root->subsys_bits);
1030 if (ret == -EBUSY) {
1031 mutex_unlock(&cgroup_mutex);
817929ec 1032 mutex_unlock(&inode->i_mutex);
ddbcc7e8
PM
1033 goto drop_new_super;
1034 }
1035
1036 /* EBUSY should be the only error here */
1037 BUG_ON(ret);
1038
1039 list_add(&root->root_list, &roots);
817929ec 1040 root_count++;
ddbcc7e8
PM
1041
1042 sb->s_root->d_fsdata = &root->top_cgroup;
1043 root->top_cgroup.dentry = sb->s_root;
1044
817929ec
PM
1045 /* Link the top cgroup in this hierarchy into all
1046 * the css_set objects */
1047 write_lock(&css_set_lock);
28fd5dfc
LZ
1048 for (i = 0; i < CSS_SET_TABLE_SIZE; i++) {
1049 struct hlist_head *hhead = &css_set_table[i];
1050 struct hlist_node *node;
817929ec 1051 struct css_set *cg;
28fd5dfc
LZ
1052
1053 hlist_for_each_entry(cg, node, hhead, hlist) {
1054 struct cg_cgroup_link *link;
1055
1056 BUG_ON(list_empty(&tmp_cg_links));
1057 link = list_entry(tmp_cg_links.next,
1058 struct cg_cgroup_link,
1059 cgrp_link_list);
1060 list_del(&link->cgrp_link_list);
1061 link->cg = cg;
1062 list_add(&link->cgrp_link_list,
1063 &root->top_cgroup.css_sets);
1064 list_add(&link->cg_link_list, &cg->cg_links);
1065 }
1066 }
817929ec
PM
1067 write_unlock(&css_set_lock);
1068
1069 free_cg_links(&tmp_cg_links);
1070
bd89aabc
PM
1071 BUG_ON(!list_empty(&cgrp->sibling));
1072 BUG_ON(!list_empty(&cgrp->children));
ddbcc7e8
PM
1073 BUG_ON(root->number_of_cgroups != 1);
1074
bd89aabc 1075 cgroup_populate_dir(cgrp);
817929ec 1076 mutex_unlock(&inode->i_mutex);
ddbcc7e8
PM
1077 mutex_unlock(&cgroup_mutex);
1078 }
1079
1080 return simple_set_mnt(mnt, sb);
1081
1082 drop_new_super:
1083 up_write(&sb->s_umount);
1084 deactivate_super(sb);
817929ec 1085 free_cg_links(&tmp_cg_links);
ddbcc7e8
PM
1086 return ret;
1087}
1088
1089static void cgroup_kill_sb(struct super_block *sb) {
1090 struct cgroupfs_root *root = sb->s_fs_info;
bd89aabc 1091 struct cgroup *cgrp = &root->top_cgroup;
ddbcc7e8 1092 int ret;
71cbb949
KM
1093 struct cg_cgroup_link *link;
1094 struct cg_cgroup_link *saved_link;
ddbcc7e8
PM
1095
1096 BUG_ON(!root);
1097
1098 BUG_ON(root->number_of_cgroups != 1);
bd89aabc
PM
1099 BUG_ON(!list_empty(&cgrp->children));
1100 BUG_ON(!list_empty(&cgrp->sibling));
ddbcc7e8
PM
1101
1102 mutex_lock(&cgroup_mutex);
1103
1104 /* Rebind all subsystems back to the default hierarchy */
1105 ret = rebind_subsystems(root, 0);
1106 /* Shouldn't be able to fail ... */
1107 BUG_ON(ret);
1108
817929ec
PM
1109 /*
1110 * Release all the links from css_sets to this hierarchy's
1111 * root cgroup
1112 */
1113 write_lock(&css_set_lock);
71cbb949
KM
1114
1115 list_for_each_entry_safe(link, saved_link, &cgrp->css_sets,
1116 cgrp_link_list) {
817929ec 1117 list_del(&link->cg_link_list);
bd89aabc 1118 list_del(&link->cgrp_link_list);
817929ec
PM
1119 kfree(link);
1120 }
1121 write_unlock(&css_set_lock);
1122
1123 if (!list_empty(&root->root_list)) {
ddbcc7e8 1124 list_del(&root->root_list);
817929ec
PM
1125 root_count--;
1126 }
ddbcc7e8
PM
1127 mutex_unlock(&cgroup_mutex);
1128
1129 kfree(root);
1130 kill_litter_super(sb);
1131}
1132
1133static struct file_system_type cgroup_fs_type = {
1134 .name = "cgroup",
1135 .get_sb = cgroup_get_sb,
1136 .kill_sb = cgroup_kill_sb,
1137};
1138
bd89aabc 1139static inline struct cgroup *__d_cgrp(struct dentry *dentry)
ddbcc7e8
PM
1140{
1141 return dentry->d_fsdata;
1142}
1143
1144static inline struct cftype *__d_cft(struct dentry *dentry)
1145{
1146 return dentry->d_fsdata;
1147}
1148
a043e3b2
LZ
1149/**
1150 * cgroup_path - generate the path of a cgroup
1151 * @cgrp: the cgroup in question
1152 * @buf: the buffer to write the path into
1153 * @buflen: the length of the buffer
1154 *
1155 * Called with cgroup_mutex held. Writes path of cgroup into buf.
ddbcc7e8
PM
1156 * Returns 0 on success, -errno on error.
1157 */
bd89aabc 1158int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
ddbcc7e8
PM
1159{
1160 char *start;
1161
bd89aabc 1162 if (cgrp == dummytop) {
ddbcc7e8
PM
1163 /*
1164 * Inactive subsystems have no dentry for their root
1165 * cgroup
1166 */
1167 strcpy(buf, "/");
1168 return 0;
1169 }
1170
1171 start = buf + buflen;
1172
1173 *--start = '\0';
1174 for (;;) {
bd89aabc 1175 int len = cgrp->dentry->d_name.len;
ddbcc7e8
PM
1176 if ((start -= len) < buf)
1177 return -ENAMETOOLONG;
bd89aabc
PM
1178 memcpy(start, cgrp->dentry->d_name.name, len);
1179 cgrp = cgrp->parent;
1180 if (!cgrp)
ddbcc7e8 1181 break;
bd89aabc 1182 if (!cgrp->parent)
ddbcc7e8
PM
1183 continue;
1184 if (--start < buf)
1185 return -ENAMETOOLONG;
1186 *start = '/';
1187 }
1188 memmove(buf, start, buf + buflen - start);
1189 return 0;
1190}
1191
bbcb81d0
PM
1192/*
1193 * Return the first subsystem attached to a cgroup's hierarchy, and
1194 * its subsystem id.
1195 */
1196
bd89aabc 1197static void get_first_subsys(const struct cgroup *cgrp,
bbcb81d0
PM
1198 struct cgroup_subsys_state **css, int *subsys_id)
1199{
bd89aabc 1200 const struct cgroupfs_root *root = cgrp->root;
bbcb81d0
PM
1201 const struct cgroup_subsys *test_ss;
1202 BUG_ON(list_empty(&root->subsys_list));
1203 test_ss = list_entry(root->subsys_list.next,
1204 struct cgroup_subsys, sibling);
1205 if (css) {
bd89aabc 1206 *css = cgrp->subsys[test_ss->subsys_id];
bbcb81d0
PM
1207 BUG_ON(!*css);
1208 }
1209 if (subsys_id)
1210 *subsys_id = test_ss->subsys_id;
1211}
1212
a043e3b2
LZ
1213/**
1214 * cgroup_attach_task - attach task 'tsk' to cgroup 'cgrp'
1215 * @cgrp: the cgroup the task is attaching to
1216 * @tsk: the task to be attached
bbcb81d0 1217 *
a043e3b2
LZ
1218 * Call holding cgroup_mutex. May take task_lock of
1219 * the task 'tsk' during call.
bbcb81d0 1220 */
956db3ca 1221int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
bbcb81d0
PM
1222{
1223 int retval = 0;
1224 struct cgroup_subsys *ss;
bd89aabc 1225 struct cgroup *oldcgrp;
817929ec
PM
1226 struct css_set *cg = tsk->cgroups;
1227 struct css_set *newcg;
bd89aabc 1228 struct cgroupfs_root *root = cgrp->root;
bbcb81d0
PM
1229 int subsys_id;
1230
bd89aabc 1231 get_first_subsys(cgrp, NULL, &subsys_id);
bbcb81d0
PM
1232
1233 /* Nothing to do if the task is already in that cgroup */
bd89aabc
PM
1234 oldcgrp = task_cgroup(tsk, subsys_id);
1235 if (cgrp == oldcgrp)
bbcb81d0
PM
1236 return 0;
1237
1238 for_each_subsys(root, ss) {
1239 if (ss->can_attach) {
bd89aabc 1240 retval = ss->can_attach(ss, cgrp, tsk);
e18f6318 1241 if (retval)
bbcb81d0 1242 return retval;
bbcb81d0
PM
1243 }
1244 }
1245
817929ec
PM
1246 /*
1247 * Locate or allocate a new css_set for this task,
1248 * based on its final set of cgroups
1249 */
bd89aabc 1250 newcg = find_css_set(cg, cgrp);
e18f6318 1251 if (!newcg)
817929ec 1252 return -ENOMEM;
817929ec 1253
bbcb81d0
PM
1254 task_lock(tsk);
1255 if (tsk->flags & PF_EXITING) {
1256 task_unlock(tsk);
817929ec 1257 put_css_set(newcg);
bbcb81d0
PM
1258 return -ESRCH;
1259 }
817929ec 1260 rcu_assign_pointer(tsk->cgroups, newcg);
bbcb81d0
PM
1261 task_unlock(tsk);
1262
817929ec
PM
1263 /* Update the css_set linked lists if we're using them */
1264 write_lock(&css_set_lock);
1265 if (!list_empty(&tsk->cg_list)) {
1266 list_del(&tsk->cg_list);
1267 list_add(&tsk->cg_list, &newcg->tasks);
1268 }
1269 write_unlock(&css_set_lock);
1270
bbcb81d0 1271 for_each_subsys(root, ss) {
e18f6318 1272 if (ss->attach)
bd89aabc 1273 ss->attach(ss, cgrp, oldcgrp, tsk);
bbcb81d0 1274 }
bd89aabc 1275 set_bit(CGRP_RELEASABLE, &oldcgrp->flags);
bbcb81d0 1276 synchronize_rcu();
817929ec 1277 put_css_set(cg);
bbcb81d0
PM
1278 return 0;
1279}
1280
1281/*
bd89aabc 1282 * Attach task with pid 'pid' to cgroup 'cgrp'. Call with
bbcb81d0
PM
1283 * cgroup_mutex, may take task_lock of task
1284 */
bd89aabc 1285static int attach_task_by_pid(struct cgroup *cgrp, char *pidbuf)
bbcb81d0
PM
1286{
1287 pid_t pid;
1288 struct task_struct *tsk;
1289 int ret;
1290
1291 if (sscanf(pidbuf, "%d", &pid) != 1)
1292 return -EIO;
1293
1294 if (pid) {
1295 rcu_read_lock();
73507f33 1296 tsk = find_task_by_vpid(pid);
bbcb81d0
PM
1297 if (!tsk || tsk->flags & PF_EXITING) {
1298 rcu_read_unlock();
1299 return -ESRCH;
1300 }
1301 get_task_struct(tsk);
1302 rcu_read_unlock();
1303
1304 if ((current->euid) && (current->euid != tsk->uid)
1305 && (current->euid != tsk->suid)) {
1306 put_task_struct(tsk);
1307 return -EACCES;
1308 }
1309 } else {
1310 tsk = current;
1311 get_task_struct(tsk);
1312 }
1313
956db3ca 1314 ret = cgroup_attach_task(cgrp, tsk);
bbcb81d0
PM
1315 put_task_struct(tsk);
1316 return ret;
1317}
1318
ddbcc7e8 1319/* The various types of files and directories in a cgroup file system */
ddbcc7e8
PM
1320enum cgroup_filetype {
1321 FILE_ROOT,
1322 FILE_DIR,
1323 FILE_TASKLIST,
81a6a5cd 1324 FILE_NOTIFY_ON_RELEASE,
81a6a5cd 1325 FILE_RELEASE_AGENT,
ddbcc7e8
PM
1326};
1327
e788e066
PM
1328/**
1329 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
1330 * @cgrp: the cgroup to be checked for liveness
1331 *
84eea842
PM
1332 * On success, returns true; the lock should be later released with
1333 * cgroup_unlock(). On failure returns false with no lock held.
e788e066 1334 */
84eea842 1335bool cgroup_lock_live_group(struct cgroup *cgrp)
e788e066
PM
1336{
1337 mutex_lock(&cgroup_mutex);
1338 if (cgroup_is_removed(cgrp)) {
1339 mutex_unlock(&cgroup_mutex);
1340 return false;
1341 }
1342 return true;
1343}
1344
1345static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
1346 const char *buffer)
1347{
1348 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
1349 if (!cgroup_lock_live_group(cgrp))
1350 return -ENODEV;
1351 strcpy(cgrp->root->release_agent_path, buffer);
84eea842 1352 cgroup_unlock();
e788e066
PM
1353 return 0;
1354}
1355
1356static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
1357 struct seq_file *seq)
1358{
1359 if (!cgroup_lock_live_group(cgrp))
1360 return -ENODEV;
1361 seq_puts(seq, cgrp->root->release_agent_path);
1362 seq_putc(seq, '\n');
84eea842 1363 cgroup_unlock();
e788e066
PM
1364 return 0;
1365}
1366
84eea842
PM
1367/* A buffer size big enough for numbers or short strings */
1368#define CGROUP_LOCAL_BUFFER_SIZE 64
1369
e73d2c61 1370static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
f4c753b7
PM
1371 struct file *file,
1372 const char __user *userbuf,
1373 size_t nbytes, loff_t *unused_ppos)
355e0c48 1374{
84eea842 1375 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
355e0c48 1376 int retval = 0;
355e0c48
PM
1377 char *end;
1378
1379 if (!nbytes)
1380 return -EINVAL;
1381 if (nbytes >= sizeof(buffer))
1382 return -E2BIG;
1383 if (copy_from_user(buffer, userbuf, nbytes))
1384 return -EFAULT;
1385
1386 buffer[nbytes] = 0; /* nul-terminate */
b7269dfc 1387 strstrip(buffer);
e73d2c61
PM
1388 if (cft->write_u64) {
1389 u64 val = simple_strtoull(buffer, &end, 0);
1390 if (*end)
1391 return -EINVAL;
1392 retval = cft->write_u64(cgrp, cft, val);
1393 } else {
1394 s64 val = simple_strtoll(buffer, &end, 0);
1395 if (*end)
1396 return -EINVAL;
1397 retval = cft->write_s64(cgrp, cft, val);
1398 }
355e0c48
PM
1399 if (!retval)
1400 retval = nbytes;
1401 return retval;
1402}
1403
db3b1497
PM
1404static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
1405 struct file *file,
1406 const char __user *userbuf,
1407 size_t nbytes, loff_t *unused_ppos)
1408{
84eea842 1409 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
db3b1497
PM
1410 int retval = 0;
1411 size_t max_bytes = cft->max_write_len;
1412 char *buffer = local_buffer;
1413
1414 if (!max_bytes)
1415 max_bytes = sizeof(local_buffer) - 1;
1416 if (nbytes >= max_bytes)
1417 return -E2BIG;
1418 /* Allocate a dynamic buffer if we need one */
1419 if (nbytes >= sizeof(local_buffer)) {
1420 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
1421 if (buffer == NULL)
1422 return -ENOMEM;
1423 }
1424 if (nbytes && copy_from_user(buffer, userbuf, nbytes))
1425 return -EFAULT;
1426
1427 buffer[nbytes] = 0; /* nul-terminate */
1428 strstrip(buffer);
1429 retval = cft->write_string(cgrp, cft, buffer);
1430 if (!retval)
1431 retval = nbytes;
1432 if (buffer != local_buffer)
1433 kfree(buffer);
1434 return retval;
1435}
1436
bd89aabc 1437static ssize_t cgroup_common_file_write(struct cgroup *cgrp,
bbcb81d0
PM
1438 struct cftype *cft,
1439 struct file *file,
1440 const char __user *userbuf,
1441 size_t nbytes, loff_t *unused_ppos)
1442{
1443 enum cgroup_filetype type = cft->private;
1444 char *buffer;
1445 int retval = 0;
1446
1447 if (nbytes >= PATH_MAX)
1448 return -E2BIG;
1449
1450 /* +1 for nul-terminator */
1451 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
1452 if (buffer == NULL)
1453 return -ENOMEM;
1454
1455 if (copy_from_user(buffer, userbuf, nbytes)) {
1456 retval = -EFAULT;
1457 goto out1;
1458 }
1459 buffer[nbytes] = 0; /* nul-terminate */
622d42ca 1460 strstrip(buffer); /* strip -just- trailing whitespace */
bbcb81d0
PM
1461
1462 mutex_lock(&cgroup_mutex);
1463
8dc4f3e1
PM
1464 /*
1465 * This was already checked for in cgroup_file_write(), but
1466 * check again now we're holding cgroup_mutex.
1467 */
bd89aabc 1468 if (cgroup_is_removed(cgrp)) {
bbcb81d0
PM
1469 retval = -ENODEV;
1470 goto out2;
1471 }
1472
1473 switch (type) {
1474 case FILE_TASKLIST:
bd89aabc 1475 retval = attach_task_by_pid(cgrp, buffer);
bbcb81d0
PM
1476 break;
1477 default:
1478 retval = -EINVAL;
1479 goto out2;
1480 }
1481
1482 if (retval == 0)
1483 retval = nbytes;
1484out2:
1485 mutex_unlock(&cgroup_mutex);
1486out1:
1487 kfree(buffer);
1488 return retval;
1489}
1490
ddbcc7e8
PM
1491static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
1492 size_t nbytes, loff_t *ppos)
1493{
1494 struct cftype *cft = __d_cft(file->f_dentry);
bd89aabc 1495 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
ddbcc7e8 1496
8dc4f3e1 1497 if (!cft || cgroup_is_removed(cgrp))
ddbcc7e8 1498 return -ENODEV;
355e0c48 1499 if (cft->write)
bd89aabc 1500 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
e73d2c61
PM
1501 if (cft->write_u64 || cft->write_s64)
1502 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
db3b1497
PM
1503 if (cft->write_string)
1504 return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
d447ea2f
PE
1505 if (cft->trigger) {
1506 int ret = cft->trigger(cgrp, (unsigned int)cft->private);
1507 return ret ? ret : nbytes;
1508 }
355e0c48 1509 return -EINVAL;
ddbcc7e8
PM
1510}
1511
f4c753b7
PM
1512static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
1513 struct file *file,
1514 char __user *buf, size_t nbytes,
1515 loff_t *ppos)
ddbcc7e8 1516{
84eea842 1517 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
f4c753b7 1518 u64 val = cft->read_u64(cgrp, cft);
ddbcc7e8
PM
1519 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
1520
1521 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
1522}
1523
e73d2c61
PM
1524static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
1525 struct file *file,
1526 char __user *buf, size_t nbytes,
1527 loff_t *ppos)
1528{
84eea842 1529 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
e73d2c61
PM
1530 s64 val = cft->read_s64(cgrp, cft);
1531 int len = sprintf(tmp, "%lld\n", (long long) val);
1532
1533 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
1534}
1535
ddbcc7e8
PM
1536static ssize_t cgroup_file_read(struct file *file, char __user *buf,
1537 size_t nbytes, loff_t *ppos)
1538{
1539 struct cftype *cft = __d_cft(file->f_dentry);
bd89aabc 1540 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
ddbcc7e8 1541
8dc4f3e1 1542 if (!cft || cgroup_is_removed(cgrp))
ddbcc7e8
PM
1543 return -ENODEV;
1544
1545 if (cft->read)
bd89aabc 1546 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
f4c753b7
PM
1547 if (cft->read_u64)
1548 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
e73d2c61
PM
1549 if (cft->read_s64)
1550 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
ddbcc7e8
PM
1551 return -EINVAL;
1552}
1553
91796569
PM
1554/*
1555 * seqfile ops/methods for returning structured data. Currently just
1556 * supports string->u64 maps, but can be extended in future.
1557 */
1558
1559struct cgroup_seqfile_state {
1560 struct cftype *cft;
1561 struct cgroup *cgroup;
1562};
1563
1564static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
1565{
1566 struct seq_file *sf = cb->state;
1567 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
1568}
1569
1570static int cgroup_seqfile_show(struct seq_file *m, void *arg)
1571{
1572 struct cgroup_seqfile_state *state = m->private;
1573 struct cftype *cft = state->cft;
29486df3
SH
1574 if (cft->read_map) {
1575 struct cgroup_map_cb cb = {
1576 .fill = cgroup_map_add,
1577 .state = m,
1578 };
1579 return cft->read_map(state->cgroup, cft, &cb);
1580 }
1581 return cft->read_seq_string(state->cgroup, cft, m);
91796569
PM
1582}
1583
1584int cgroup_seqfile_release(struct inode *inode, struct file *file)
1585{
1586 struct seq_file *seq = file->private_data;
1587 kfree(seq->private);
1588 return single_release(inode, file);
1589}
1590
1591static struct file_operations cgroup_seqfile_operations = {
1592 .read = seq_read,
e788e066 1593 .write = cgroup_file_write,
91796569
PM
1594 .llseek = seq_lseek,
1595 .release = cgroup_seqfile_release,
1596};
1597
ddbcc7e8
PM
1598static int cgroup_file_open(struct inode *inode, struct file *file)
1599{
1600 int err;
1601 struct cftype *cft;
1602
1603 err = generic_file_open(inode, file);
1604 if (err)
1605 return err;
1606
1607 cft = __d_cft(file->f_dentry);
1608 if (!cft)
1609 return -ENODEV;
29486df3 1610 if (cft->read_map || cft->read_seq_string) {
91796569
PM
1611 struct cgroup_seqfile_state *state =
1612 kzalloc(sizeof(*state), GFP_USER);
1613 if (!state)
1614 return -ENOMEM;
1615 state->cft = cft;
1616 state->cgroup = __d_cgrp(file->f_dentry->d_parent);
1617 file->f_op = &cgroup_seqfile_operations;
1618 err = single_open(file, cgroup_seqfile_show, state);
1619 if (err < 0)
1620 kfree(state);
1621 } else if (cft->open)
ddbcc7e8
PM
1622 err = cft->open(inode, file);
1623 else
1624 err = 0;
1625
1626 return err;
1627}
1628
1629static int cgroup_file_release(struct inode *inode, struct file *file)
1630{
1631 struct cftype *cft = __d_cft(file->f_dentry);
1632 if (cft->release)
1633 return cft->release(inode, file);
1634 return 0;
1635}
1636
1637/*
1638 * cgroup_rename - Only allow simple rename of directories in place.
1639 */
1640static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
1641 struct inode *new_dir, struct dentry *new_dentry)
1642{
1643 if (!S_ISDIR(old_dentry->d_inode->i_mode))
1644 return -ENOTDIR;
1645 if (new_dentry->d_inode)
1646 return -EEXIST;
1647 if (old_dir != new_dir)
1648 return -EIO;
1649 return simple_rename(old_dir, old_dentry, new_dir, new_dentry);
1650}
1651
1652static struct file_operations cgroup_file_operations = {
1653 .read = cgroup_file_read,
1654 .write = cgroup_file_write,
1655 .llseek = generic_file_llseek,
1656 .open = cgroup_file_open,
1657 .release = cgroup_file_release,
1658};
1659
1660static struct inode_operations cgroup_dir_inode_operations = {
1661 .lookup = simple_lookup,
1662 .mkdir = cgroup_mkdir,
1663 .rmdir = cgroup_rmdir,
1664 .rename = cgroup_rename,
1665};
1666
1667static int cgroup_create_file(struct dentry *dentry, int mode,
1668 struct super_block *sb)
1669{
1670 static struct dentry_operations cgroup_dops = {
1671 .d_iput = cgroup_diput,
1672 };
1673
1674 struct inode *inode;
1675
1676 if (!dentry)
1677 return -ENOENT;
1678 if (dentry->d_inode)
1679 return -EEXIST;
1680
1681 inode = cgroup_new_inode(mode, sb);
1682 if (!inode)
1683 return -ENOMEM;
1684
1685 if (S_ISDIR(mode)) {
1686 inode->i_op = &cgroup_dir_inode_operations;
1687 inode->i_fop = &simple_dir_operations;
1688
1689 /* start off with i_nlink == 2 (for "." entry) */
1690 inc_nlink(inode);
1691
1692 /* start with the directory inode held, so that we can
1693 * populate it without racing with another mkdir */
817929ec 1694 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
ddbcc7e8
PM
1695 } else if (S_ISREG(mode)) {
1696 inode->i_size = 0;
1697 inode->i_fop = &cgroup_file_operations;
1698 }
1699 dentry->d_op = &cgroup_dops;
1700 d_instantiate(dentry, inode);
1701 dget(dentry); /* Extra count - pin the dentry in core */
1702 return 0;
1703}
1704
1705/*
a043e3b2
LZ
1706 * cgroup_create_dir - create a directory for an object.
1707 * @cgrp: the cgroup we create the directory for. It must have a valid
1708 * ->parent field. And we are going to fill its ->dentry field.
1709 * @dentry: dentry of the new cgroup
1710 * @mode: mode to set on new directory.
ddbcc7e8 1711 */
bd89aabc 1712static int cgroup_create_dir(struct cgroup *cgrp, struct dentry *dentry,
ddbcc7e8
PM
1713 int mode)
1714{
1715 struct dentry *parent;
1716 int error = 0;
1717
bd89aabc
PM
1718 parent = cgrp->parent->dentry;
1719 error = cgroup_create_file(dentry, S_IFDIR | mode, cgrp->root->sb);
ddbcc7e8 1720 if (!error) {
bd89aabc 1721 dentry->d_fsdata = cgrp;
ddbcc7e8 1722 inc_nlink(parent->d_inode);
bd89aabc 1723 cgrp->dentry = dentry;
ddbcc7e8
PM
1724 dget(dentry);
1725 }
1726 dput(dentry);
1727
1728 return error;
1729}
1730
bd89aabc 1731int cgroup_add_file(struct cgroup *cgrp,
ddbcc7e8
PM
1732 struct cgroup_subsys *subsys,
1733 const struct cftype *cft)
1734{
bd89aabc 1735 struct dentry *dir = cgrp->dentry;
ddbcc7e8
PM
1736 struct dentry *dentry;
1737 int error;
1738
1739 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
bd89aabc 1740 if (subsys && !test_bit(ROOT_NOPREFIX, &cgrp->root->flags)) {
ddbcc7e8
PM
1741 strcpy(name, subsys->name);
1742 strcat(name, ".");
1743 }
1744 strcat(name, cft->name);
1745 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
1746 dentry = lookup_one_len(name, dir, strlen(name));
1747 if (!IS_ERR(dentry)) {
1748 error = cgroup_create_file(dentry, 0644 | S_IFREG,
bd89aabc 1749 cgrp->root->sb);
ddbcc7e8
PM
1750 if (!error)
1751 dentry->d_fsdata = (void *)cft;
1752 dput(dentry);
1753 } else
1754 error = PTR_ERR(dentry);
1755 return error;
1756}
1757
bd89aabc 1758int cgroup_add_files(struct cgroup *cgrp,
ddbcc7e8
PM
1759 struct cgroup_subsys *subsys,
1760 const struct cftype cft[],
1761 int count)
1762{
1763 int i, err;
1764 for (i = 0; i < count; i++) {
bd89aabc 1765 err = cgroup_add_file(cgrp, subsys, &cft[i]);
ddbcc7e8
PM
1766 if (err)
1767 return err;
1768 }
1769 return 0;
1770}
1771
a043e3b2
LZ
1772/**
1773 * cgroup_task_count - count the number of tasks in a cgroup.
1774 * @cgrp: the cgroup in question
1775 *
1776 * Return the number of tasks in the cgroup.
1777 */
bd89aabc 1778int cgroup_task_count(const struct cgroup *cgrp)
bbcb81d0
PM
1779{
1780 int count = 0;
71cbb949 1781 struct cg_cgroup_link *link;
817929ec
PM
1782
1783 read_lock(&css_set_lock);
71cbb949 1784 list_for_each_entry(link, &cgrp->css_sets, cgrp_link_list) {
817929ec 1785 count += atomic_read(&link->cg->ref.refcount);
817929ec
PM
1786 }
1787 read_unlock(&css_set_lock);
bbcb81d0
PM
1788 return count;
1789}
1790
817929ec
PM
1791/*
1792 * Advance a list_head iterator. The iterator should be positioned at
1793 * the start of a css_set
1794 */
bd89aabc 1795static void cgroup_advance_iter(struct cgroup *cgrp,
817929ec
PM
1796 struct cgroup_iter *it)
1797{
1798 struct list_head *l = it->cg_link;
1799 struct cg_cgroup_link *link;
1800 struct css_set *cg;
1801
1802 /* Advance to the next non-empty css_set */
1803 do {
1804 l = l->next;
bd89aabc 1805 if (l == &cgrp->css_sets) {
817929ec
PM
1806 it->cg_link = NULL;
1807 return;
1808 }
bd89aabc 1809 link = list_entry(l, struct cg_cgroup_link, cgrp_link_list);
817929ec
PM
1810 cg = link->cg;
1811 } while (list_empty(&cg->tasks));
1812 it->cg_link = l;
1813 it->task = cg->tasks.next;
1814}
1815
31a7df01
CW
1816/*
1817 * To reduce the fork() overhead for systems that are not actually
1818 * using their cgroups capability, we don't maintain the lists running
1819 * through each css_set to its tasks until we see the list actually
1820 * used - in other words after the first call to cgroup_iter_start().
1821 *
1822 * The tasklist_lock is not held here, as do_each_thread() and
1823 * while_each_thread() are protected by RCU.
1824 */
3df91fe3 1825static void cgroup_enable_task_cg_lists(void)
31a7df01
CW
1826{
1827 struct task_struct *p, *g;
1828 write_lock(&css_set_lock);
1829 use_task_css_set_links = 1;
1830 do_each_thread(g, p) {
1831 task_lock(p);
0e04388f
LZ
1832 /*
1833 * We should check if the process is exiting, otherwise
1834 * it will race with cgroup_exit() in that the list
1835 * entry won't be deleted though the process has exited.
1836 */
1837 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
31a7df01
CW
1838 list_add(&p->cg_list, &p->cgroups->tasks);
1839 task_unlock(p);
1840 } while_each_thread(g, p);
1841 write_unlock(&css_set_lock);
1842}
1843
bd89aabc 1844void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
817929ec
PM
1845{
1846 /*
1847 * The first time anyone tries to iterate across a cgroup,
1848 * we need to enable the list linking each css_set to its
1849 * tasks, and fix up all existing tasks.
1850 */
31a7df01
CW
1851 if (!use_task_css_set_links)
1852 cgroup_enable_task_cg_lists();
1853
817929ec 1854 read_lock(&css_set_lock);
bd89aabc
PM
1855 it->cg_link = &cgrp->css_sets;
1856 cgroup_advance_iter(cgrp, it);
817929ec
PM
1857}
1858
bd89aabc 1859struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
817929ec
PM
1860 struct cgroup_iter *it)
1861{
1862 struct task_struct *res;
1863 struct list_head *l = it->task;
1864
1865 /* If the iterator cg is NULL, we have no tasks */
1866 if (!it->cg_link)
1867 return NULL;
1868 res = list_entry(l, struct task_struct, cg_list);
1869 /* Advance iterator to find next entry */
1870 l = l->next;
1871 if (l == &res->cgroups->tasks) {
1872 /* We reached the end of this task list - move on to
1873 * the next cg_cgroup_link */
bd89aabc 1874 cgroup_advance_iter(cgrp, it);
817929ec
PM
1875 } else {
1876 it->task = l;
1877 }
1878 return res;
1879}
1880
bd89aabc 1881void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
817929ec
PM
1882{
1883 read_unlock(&css_set_lock);
1884}
1885
31a7df01
CW
1886static inline int started_after_time(struct task_struct *t1,
1887 struct timespec *time,
1888 struct task_struct *t2)
1889{
1890 int start_diff = timespec_compare(&t1->start_time, time);
1891 if (start_diff > 0) {
1892 return 1;
1893 } else if (start_diff < 0) {
1894 return 0;
1895 } else {
1896 /*
1897 * Arbitrarily, if two processes started at the same
1898 * time, we'll say that the lower pointer value
1899 * started first. Note that t2 may have exited by now
1900 * so this may not be a valid pointer any longer, but
1901 * that's fine - it still serves to distinguish
1902 * between two tasks started (effectively) simultaneously.
1903 */
1904 return t1 > t2;
1905 }
1906}
1907
1908/*
1909 * This function is a callback from heap_insert() and is used to order
1910 * the heap.
1911 * In this case we order the heap in descending task start time.
1912 */
1913static inline int started_after(void *p1, void *p2)
1914{
1915 struct task_struct *t1 = p1;
1916 struct task_struct *t2 = p2;
1917 return started_after_time(t1, &t2->start_time, t2);
1918}
1919
1920/**
1921 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
1922 * @scan: struct cgroup_scanner containing arguments for the scan
1923 *
1924 * Arguments include pointers to callback functions test_task() and
1925 * process_task().
1926 * Iterate through all the tasks in a cgroup, calling test_task() for each,
1927 * and if it returns true, call process_task() for it also.
1928 * The test_task pointer may be NULL, meaning always true (select all tasks).
1929 * Effectively duplicates cgroup_iter_{start,next,end}()
1930 * but does not lock css_set_lock for the call to process_task().
1931 * The struct cgroup_scanner may be embedded in any structure of the caller's
1932 * creation.
1933 * It is guaranteed that process_task() will act on every task that
1934 * is a member of the cgroup for the duration of this call. This
1935 * function may or may not call process_task() for tasks that exit
1936 * or move to a different cgroup during the call, or are forked or
1937 * move into the cgroup during the call.
1938 *
1939 * Note that test_task() may be called with locks held, and may in some
1940 * situations be called multiple times for the same task, so it should
1941 * be cheap.
1942 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
1943 * pre-allocated and will be used for heap operations (and its "gt" member will
1944 * be overwritten), else a temporary heap will be used (allocation of which
1945 * may cause this function to fail).
1946 */
1947int cgroup_scan_tasks(struct cgroup_scanner *scan)
1948{
1949 int retval, i;
1950 struct cgroup_iter it;
1951 struct task_struct *p, *dropped;
1952 /* Never dereference latest_task, since it's not refcounted */
1953 struct task_struct *latest_task = NULL;
1954 struct ptr_heap tmp_heap;
1955 struct ptr_heap *heap;
1956 struct timespec latest_time = { 0, 0 };
1957
1958 if (scan->heap) {
1959 /* The caller supplied our heap and pre-allocated its memory */
1960 heap = scan->heap;
1961 heap->gt = &started_after;
1962 } else {
1963 /* We need to allocate our own heap memory */
1964 heap = &tmp_heap;
1965 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
1966 if (retval)
1967 /* cannot allocate the heap */
1968 return retval;
1969 }
1970
1971 again:
1972 /*
1973 * Scan tasks in the cgroup, using the scanner's "test_task" callback
1974 * to determine which are of interest, and using the scanner's
1975 * "process_task" callback to process any of them that need an update.
1976 * Since we don't want to hold any locks during the task updates,
1977 * gather tasks to be processed in a heap structure.
1978 * The heap is sorted by descending task start time.
1979 * If the statically-sized heap fills up, we overflow tasks that
1980 * started later, and in future iterations only consider tasks that
1981 * started after the latest task in the previous pass. This
1982 * guarantees forward progress and that we don't miss any tasks.
1983 */
1984 heap->size = 0;
1985 cgroup_iter_start(scan->cg, &it);
1986 while ((p = cgroup_iter_next(scan->cg, &it))) {
1987 /*
1988 * Only affect tasks that qualify per the caller's callback,
1989 * if he provided one
1990 */
1991 if (scan->test_task && !scan->test_task(p, scan))
1992 continue;
1993 /*
1994 * Only process tasks that started after the last task
1995 * we processed
1996 */
1997 if (!started_after_time(p, &latest_time, latest_task))
1998 continue;
1999 dropped = heap_insert(heap, p);
2000 if (dropped == NULL) {
2001 /*
2002 * The new task was inserted; the heap wasn't
2003 * previously full
2004 */
2005 get_task_struct(p);
2006 } else if (dropped != p) {
2007 /*
2008 * The new task was inserted, and pushed out a
2009 * different task
2010 */
2011 get_task_struct(p);
2012 put_task_struct(dropped);
2013 }
2014 /*
2015 * Else the new task was newer than anything already in
2016 * the heap and wasn't inserted
2017 */
2018 }
2019 cgroup_iter_end(scan->cg, &it);
2020
2021 if (heap->size) {
2022 for (i = 0; i < heap->size; i++) {
4fe91d51 2023 struct task_struct *q = heap->ptrs[i];
31a7df01 2024 if (i == 0) {
4fe91d51
PJ
2025 latest_time = q->start_time;
2026 latest_task = q;
31a7df01
CW
2027 }
2028 /* Process the task per the caller's callback */
4fe91d51
PJ
2029 scan->process_task(q, scan);
2030 put_task_struct(q);
31a7df01
CW
2031 }
2032 /*
2033 * If we had to process any tasks at all, scan again
2034 * in case some of them were in the middle of forking
2035 * children that didn't get processed.
2036 * Not the most efficient way to do it, but it avoids
2037 * having to take callback_mutex in the fork path
2038 */
2039 goto again;
2040 }
2041 if (heap == &tmp_heap)
2042 heap_free(&tmp_heap);
2043 return 0;
2044}
2045
bbcb81d0
PM
2046/*
2047 * Stuff for reading the 'tasks' file.
2048 *
2049 * Reading this file can return large amounts of data if a cgroup has
2050 * *lots* of attached tasks. So it may need several calls to read(),
2051 * but we cannot guarantee that the information we produce is correct
2052 * unless we produce it entirely atomically.
2053 *
2054 * Upon tasks file open(), a struct ctr_struct is allocated, that
2055 * will have a pointer to an array (also allocated here). The struct
2056 * ctr_struct * is stored in file->private_data. Its resources will
2057 * be freed by release() when the file is closed. The array is used
2058 * to sprintf the PIDs and then used by read().
2059 */
2060struct ctr_struct {
2061 char *buf;
2062 int bufsz;
2063};
2064
2065/*
2066 * Load into 'pidarray' up to 'npids' of the tasks using cgroup
bd89aabc 2067 * 'cgrp'. Return actual number of pids loaded. No need to
bbcb81d0
PM
2068 * task_lock(p) when reading out p->cgroup, since we're in an RCU
2069 * read section, so the css_set can't go away, and is
2070 * immutable after creation.
2071 */
bd89aabc 2072static int pid_array_load(pid_t *pidarray, int npids, struct cgroup *cgrp)
bbcb81d0
PM
2073{
2074 int n = 0;
817929ec
PM
2075 struct cgroup_iter it;
2076 struct task_struct *tsk;
bd89aabc
PM
2077 cgroup_iter_start(cgrp, &it);
2078 while ((tsk = cgroup_iter_next(cgrp, &it))) {
817929ec
PM
2079 if (unlikely(n == npids))
2080 break;
73507f33 2081 pidarray[n++] = task_pid_vnr(tsk);
817929ec 2082 }
bd89aabc 2083 cgroup_iter_end(cgrp, &it);
bbcb81d0
PM
2084 return n;
2085}
2086
846c7bb0 2087/**
a043e3b2 2088 * cgroupstats_build - build and fill cgroupstats
846c7bb0
BS
2089 * @stats: cgroupstats to fill information into
2090 * @dentry: A dentry entry belonging to the cgroup for which stats have
2091 * been requested.
a043e3b2
LZ
2092 *
2093 * Build and fill cgroupstats so that taskstats can export it to user
2094 * space.
846c7bb0
BS
2095 */
2096int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
2097{
2098 int ret = -EINVAL;
bd89aabc 2099 struct cgroup *cgrp;
846c7bb0
BS
2100 struct cgroup_iter it;
2101 struct task_struct *tsk;
2102 /*
2103 * Validate dentry by checking the superblock operations
2104 */
2105 if (dentry->d_sb->s_op != &cgroup_ops)
2106 goto err;
2107
2108 ret = 0;
bd89aabc 2109 cgrp = dentry->d_fsdata;
846c7bb0
BS
2110 rcu_read_lock();
2111
bd89aabc
PM
2112 cgroup_iter_start(cgrp, &it);
2113 while ((tsk = cgroup_iter_next(cgrp, &it))) {
846c7bb0
BS
2114 switch (tsk->state) {
2115 case TASK_RUNNING:
2116 stats->nr_running++;
2117 break;
2118 case TASK_INTERRUPTIBLE:
2119 stats->nr_sleeping++;
2120 break;
2121 case TASK_UNINTERRUPTIBLE:
2122 stats->nr_uninterruptible++;
2123 break;
2124 case TASK_STOPPED:
2125 stats->nr_stopped++;
2126 break;
2127 default:
2128 if (delayacct_is_task_waiting_on_io(tsk))
2129 stats->nr_io_wait++;
2130 break;
2131 }
2132 }
bd89aabc 2133 cgroup_iter_end(cgrp, &it);
846c7bb0
BS
2134
2135 rcu_read_unlock();
2136err:
2137 return ret;
2138}
2139
bbcb81d0
PM
2140static int cmppid(const void *a, const void *b)
2141{
2142 return *(pid_t *)a - *(pid_t *)b;
2143}
2144
2145/*
2146 * Convert array 'a' of 'npids' pid_t's to a string of newline separated
2147 * decimal pids in 'buf'. Don't write more than 'sz' chars, but return
2148 * count 'cnt' of how many chars would be written if buf were large enough.
2149 */
2150static int pid_array_to_buf(char *buf, int sz, pid_t *a, int npids)
2151{
2152 int cnt = 0;
2153 int i;
2154
2155 for (i = 0; i < npids; i++)
2156 cnt += snprintf(buf + cnt, max(sz - cnt, 0), "%d\n", a[i]);
2157 return cnt;
2158}
2159
2160/*
2161 * Handle an open on 'tasks' file. Prepare a buffer listing the
2162 * process id's of tasks currently attached to the cgroup being opened.
2163 *
2164 * Does not require any specific cgroup mutexes, and does not take any.
2165 */
2166static int cgroup_tasks_open(struct inode *unused, struct file *file)
2167{
bd89aabc 2168 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
bbcb81d0
PM
2169 struct ctr_struct *ctr;
2170 pid_t *pidarray;
2171 int npids;
2172 char c;
2173
2174 if (!(file->f_mode & FMODE_READ))
2175 return 0;
2176
2177 ctr = kmalloc(sizeof(*ctr), GFP_KERNEL);
2178 if (!ctr)
2179 goto err0;
2180
2181 /*
2182 * If cgroup gets more users after we read count, we won't have
2183 * enough space - tough. This race is indistinguishable to the
2184 * caller from the case that the additional cgroup users didn't
2185 * show up until sometime later on.
2186 */
bd89aabc 2187 npids = cgroup_task_count(cgrp);
bbcb81d0
PM
2188 if (npids) {
2189 pidarray = kmalloc(npids * sizeof(pid_t), GFP_KERNEL);
2190 if (!pidarray)
2191 goto err1;
2192
bd89aabc 2193 npids = pid_array_load(pidarray, npids, cgrp);
bbcb81d0
PM
2194 sort(pidarray, npids, sizeof(pid_t), cmppid, NULL);
2195
2196 /* Call pid_array_to_buf() twice, first just to get bufsz */
2197 ctr->bufsz = pid_array_to_buf(&c, sizeof(c), pidarray, npids) + 1;
2198 ctr->buf = kmalloc(ctr->bufsz, GFP_KERNEL);
2199 if (!ctr->buf)
2200 goto err2;
2201 ctr->bufsz = pid_array_to_buf(ctr->buf, ctr->bufsz, pidarray, npids);
2202
2203 kfree(pidarray);
2204 } else {
9dce07f1 2205 ctr->buf = NULL;
bbcb81d0
PM
2206 ctr->bufsz = 0;
2207 }
2208 file->private_data = ctr;
2209 return 0;
2210
2211err2:
2212 kfree(pidarray);
2213err1:
2214 kfree(ctr);
2215err0:
2216 return -ENOMEM;
2217}
2218
bd89aabc 2219static ssize_t cgroup_tasks_read(struct cgroup *cgrp,
bbcb81d0
PM
2220 struct cftype *cft,
2221 struct file *file, char __user *buf,
2222 size_t nbytes, loff_t *ppos)
2223{
2224 struct ctr_struct *ctr = file->private_data;
2225
2226 return simple_read_from_buffer(buf, nbytes, ppos, ctr->buf, ctr->bufsz);
2227}
2228
2229static int cgroup_tasks_release(struct inode *unused_inode,
2230 struct file *file)
2231{
2232 struct ctr_struct *ctr;
2233
2234 if (file->f_mode & FMODE_READ) {
2235 ctr = file->private_data;
2236 kfree(ctr->buf);
2237 kfree(ctr);
2238 }
2239 return 0;
2240}
2241
bd89aabc 2242static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
81a6a5cd
PM
2243 struct cftype *cft)
2244{
bd89aabc 2245 return notify_on_release(cgrp);
81a6a5cd
PM
2246}
2247
6379c106
PM
2248static int cgroup_write_notify_on_release(struct cgroup *cgrp,
2249 struct cftype *cft,
2250 u64 val)
2251{
2252 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
2253 if (val)
2254 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
2255 else
2256 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
2257 return 0;
2258}
2259
bbcb81d0
PM
2260/*
2261 * for the common functions, 'private' gives the type of file
2262 */
81a6a5cd
PM
2263static struct cftype files[] = {
2264 {
2265 .name = "tasks",
2266 .open = cgroup_tasks_open,
2267 .read = cgroup_tasks_read,
2268 .write = cgroup_common_file_write,
2269 .release = cgroup_tasks_release,
2270 .private = FILE_TASKLIST,
2271 },
2272
2273 {
2274 .name = "notify_on_release",
f4c753b7 2275 .read_u64 = cgroup_read_notify_on_release,
6379c106 2276 .write_u64 = cgroup_write_notify_on_release,
81a6a5cd
PM
2277 .private = FILE_NOTIFY_ON_RELEASE,
2278 },
81a6a5cd
PM
2279};
2280
2281static struct cftype cft_release_agent = {
2282 .name = "release_agent",
e788e066
PM
2283 .read_seq_string = cgroup_release_agent_show,
2284 .write_string = cgroup_release_agent_write,
2285 .max_write_len = PATH_MAX,
81a6a5cd 2286 .private = FILE_RELEASE_AGENT,
bbcb81d0
PM
2287};
2288
bd89aabc 2289static int cgroup_populate_dir(struct cgroup *cgrp)
ddbcc7e8
PM
2290{
2291 int err;
2292 struct cgroup_subsys *ss;
2293
2294 /* First clear out any existing files */
bd89aabc 2295 cgroup_clear_directory(cgrp->dentry);
ddbcc7e8 2296
bd89aabc 2297 err = cgroup_add_files(cgrp, NULL, files, ARRAY_SIZE(files));
bbcb81d0
PM
2298 if (err < 0)
2299 return err;
2300
bd89aabc
PM
2301 if (cgrp == cgrp->top_cgroup) {
2302 if ((err = cgroup_add_file(cgrp, NULL, &cft_release_agent)) < 0)
81a6a5cd
PM
2303 return err;
2304 }
2305
bd89aabc
PM
2306 for_each_subsys(cgrp->root, ss) {
2307 if (ss->populate && (err = ss->populate(ss, cgrp)) < 0)
ddbcc7e8
PM
2308 return err;
2309 }
2310
2311 return 0;
2312}
2313
2314static void init_cgroup_css(struct cgroup_subsys_state *css,
2315 struct cgroup_subsys *ss,
bd89aabc 2316 struct cgroup *cgrp)
ddbcc7e8 2317{
bd89aabc 2318 css->cgroup = cgrp;
ddbcc7e8
PM
2319 atomic_set(&css->refcnt, 0);
2320 css->flags = 0;
bd89aabc 2321 if (cgrp == dummytop)
ddbcc7e8 2322 set_bit(CSS_ROOT, &css->flags);
bd89aabc
PM
2323 BUG_ON(cgrp->subsys[ss->subsys_id]);
2324 cgrp->subsys[ss->subsys_id] = css;
ddbcc7e8
PM
2325}
2326
2327/*
a043e3b2
LZ
2328 * cgroup_create - create a cgroup
2329 * @parent: cgroup that will be parent of the new cgroup
2330 * @dentry: dentry of the new cgroup
2331 * @mode: mode to set on new inode
ddbcc7e8 2332 *
a043e3b2 2333 * Must be called with the mutex on the parent inode held
ddbcc7e8 2334 */
ddbcc7e8
PM
2335static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
2336 int mode)
2337{
bd89aabc 2338 struct cgroup *cgrp;
ddbcc7e8
PM
2339 struct cgroupfs_root *root = parent->root;
2340 int err = 0;
2341 struct cgroup_subsys *ss;
2342 struct super_block *sb = root->sb;
2343
bd89aabc
PM
2344 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
2345 if (!cgrp)
ddbcc7e8
PM
2346 return -ENOMEM;
2347
2348 /* Grab a reference on the superblock so the hierarchy doesn't
2349 * get deleted on unmount if there are child cgroups. This
2350 * can be done outside cgroup_mutex, since the sb can't
2351 * disappear while someone has an open control file on the
2352 * fs */
2353 atomic_inc(&sb->s_active);
2354
2355 mutex_lock(&cgroup_mutex);
2356
bd89aabc
PM
2357 INIT_LIST_HEAD(&cgrp->sibling);
2358 INIT_LIST_HEAD(&cgrp->children);
2359 INIT_LIST_HEAD(&cgrp->css_sets);
2360 INIT_LIST_HEAD(&cgrp->release_list);
ddbcc7e8 2361
bd89aabc
PM
2362 cgrp->parent = parent;
2363 cgrp->root = parent->root;
2364 cgrp->top_cgroup = parent->top_cgroup;
ddbcc7e8 2365
b6abdb0e
LZ
2366 if (notify_on_release(parent))
2367 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
2368
ddbcc7e8 2369 for_each_subsys(root, ss) {
bd89aabc 2370 struct cgroup_subsys_state *css = ss->create(ss, cgrp);
ddbcc7e8
PM
2371 if (IS_ERR(css)) {
2372 err = PTR_ERR(css);
2373 goto err_destroy;
2374 }
bd89aabc 2375 init_cgroup_css(css, ss, cgrp);
ddbcc7e8
PM
2376 }
2377
bd89aabc 2378 list_add(&cgrp->sibling, &cgrp->parent->children);
ddbcc7e8
PM
2379 root->number_of_cgroups++;
2380
bd89aabc 2381 err = cgroup_create_dir(cgrp, dentry, mode);
ddbcc7e8
PM
2382 if (err < 0)
2383 goto err_remove;
2384
2385 /* The cgroup directory was pre-locked for us */
bd89aabc 2386 BUG_ON(!mutex_is_locked(&cgrp->dentry->d_inode->i_mutex));
ddbcc7e8 2387
bd89aabc 2388 err = cgroup_populate_dir(cgrp);
ddbcc7e8
PM
2389 /* If err < 0, we have a half-filled directory - oh well ;) */
2390
2391 mutex_unlock(&cgroup_mutex);
bd89aabc 2392 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
ddbcc7e8
PM
2393
2394 return 0;
2395
2396 err_remove:
2397
bd89aabc 2398 list_del(&cgrp->sibling);
ddbcc7e8
PM
2399 root->number_of_cgroups--;
2400
2401 err_destroy:
2402
2403 for_each_subsys(root, ss) {
bd89aabc
PM
2404 if (cgrp->subsys[ss->subsys_id])
2405 ss->destroy(ss, cgrp);
ddbcc7e8
PM
2406 }
2407
2408 mutex_unlock(&cgroup_mutex);
2409
2410 /* Release the reference count that we took on the superblock */
2411 deactivate_super(sb);
2412
bd89aabc 2413 kfree(cgrp);
ddbcc7e8
PM
2414 return err;
2415}
2416
2417static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2418{
2419 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
2420
2421 /* the vfs holds inode->i_mutex already */
2422 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
2423}
2424
bd89aabc 2425static inline int cgroup_has_css_refs(struct cgroup *cgrp)
81a6a5cd
PM
2426{
2427 /* Check the reference count on each subsystem. Since we
2428 * already established that there are no tasks in the
2429 * cgroup, if the css refcount is also 0, then there should
2430 * be no outstanding references, so the subsystem is safe to
2431 * destroy. We scan across all subsystems rather than using
2432 * the per-hierarchy linked list of mounted subsystems since
2433 * we can be called via check_for_release() with no
2434 * synchronization other than RCU, and the subsystem linked
2435 * list isn't RCU-safe */
2436 int i;
2437 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2438 struct cgroup_subsys *ss = subsys[i];
2439 struct cgroup_subsys_state *css;
2440 /* Skip subsystems not in this hierarchy */
bd89aabc 2441 if (ss->root != cgrp->root)
81a6a5cd 2442 continue;
bd89aabc 2443 css = cgrp->subsys[ss->subsys_id];
81a6a5cd
PM
2444 /* When called from check_for_release() it's possible
2445 * that by this point the cgroup has been removed
2446 * and the css deleted. But a false-positive doesn't
2447 * matter, since it can only happen if the cgroup
2448 * has been deleted and hence no longer needs the
2449 * release agent to be called anyway. */
e18f6318 2450 if (css && atomic_read(&css->refcnt))
81a6a5cd 2451 return 1;
81a6a5cd
PM
2452 }
2453 return 0;
2454}
2455
ddbcc7e8
PM
2456static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
2457{
bd89aabc 2458 struct cgroup *cgrp = dentry->d_fsdata;
ddbcc7e8
PM
2459 struct dentry *d;
2460 struct cgroup *parent;
ddbcc7e8
PM
2461 struct super_block *sb;
2462 struct cgroupfs_root *root;
ddbcc7e8
PM
2463
2464 /* the vfs holds both inode->i_mutex already */
2465
2466 mutex_lock(&cgroup_mutex);
bd89aabc 2467 if (atomic_read(&cgrp->count) != 0) {
ddbcc7e8
PM
2468 mutex_unlock(&cgroup_mutex);
2469 return -EBUSY;
2470 }
bd89aabc 2471 if (!list_empty(&cgrp->children)) {
ddbcc7e8
PM
2472 mutex_unlock(&cgroup_mutex);
2473 return -EBUSY;
2474 }
2475
bd89aabc
PM
2476 parent = cgrp->parent;
2477 root = cgrp->root;
ddbcc7e8 2478 sb = root->sb;
a043e3b2 2479
4fca88c8 2480 /*
a043e3b2
LZ
2481 * Call pre_destroy handlers of subsys. Notify subsystems
2482 * that rmdir() request comes.
4fca88c8
KH
2483 */
2484 cgroup_call_pre_destroy(cgrp);
ddbcc7e8 2485
bd89aabc 2486 if (cgroup_has_css_refs(cgrp)) {
ddbcc7e8
PM
2487 mutex_unlock(&cgroup_mutex);
2488 return -EBUSY;
2489 }
2490
81a6a5cd 2491 spin_lock(&release_list_lock);
bd89aabc
PM
2492 set_bit(CGRP_REMOVED, &cgrp->flags);
2493 if (!list_empty(&cgrp->release_list))
2494 list_del(&cgrp->release_list);
81a6a5cd 2495 spin_unlock(&release_list_lock);
ddbcc7e8 2496 /* delete my sibling from parent->children */
bd89aabc
PM
2497 list_del(&cgrp->sibling);
2498 spin_lock(&cgrp->dentry->d_lock);
2499 d = dget(cgrp->dentry);
2500 cgrp->dentry = NULL;
ddbcc7e8
PM
2501 spin_unlock(&d->d_lock);
2502
2503 cgroup_d_remove_dir(d);
2504 dput(d);
ddbcc7e8 2505
bd89aabc 2506 set_bit(CGRP_RELEASABLE, &parent->flags);
81a6a5cd
PM
2507 check_for_release(parent);
2508
ddbcc7e8 2509 mutex_unlock(&cgroup_mutex);
ddbcc7e8
PM
2510 return 0;
2511}
2512
06a11920 2513static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
ddbcc7e8 2514{
ddbcc7e8 2515 struct cgroup_subsys_state *css;
cfe36bde
DC
2516
2517 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
ddbcc7e8
PM
2518
2519 /* Create the top cgroup state for this subsystem */
2520 ss->root = &rootnode;
2521 css = ss->create(ss, dummytop);
2522 /* We don't handle early failures gracefully */
2523 BUG_ON(IS_ERR(css));
2524 init_cgroup_css(css, ss, dummytop);
2525
e8d55fde 2526 /* Update the init_css_set to contain a subsys
817929ec 2527 * pointer to this state - since the subsystem is
e8d55fde
LZ
2528 * newly registered, all tasks and hence the
2529 * init_css_set is in the subsystem's top cgroup. */
2530 init_css_set.subsys[ss->subsys_id] = dummytop->subsys[ss->subsys_id];
ddbcc7e8
PM
2531
2532 need_forkexit_callback |= ss->fork || ss->exit;
cf475ad2 2533 need_mm_owner_callback |= !!ss->mm_owner_changed;
ddbcc7e8 2534
e8d55fde
LZ
2535 /* At system boot, before all subsystems have been
2536 * registered, no tasks have been forked, so we don't
2537 * need to invoke fork callbacks here. */
2538 BUG_ON(!list_empty(&init_task.tasks));
2539
ddbcc7e8
PM
2540 ss->active = 1;
2541}
2542
2543/**
a043e3b2
LZ
2544 * cgroup_init_early - cgroup initialization at system boot
2545 *
2546 * Initialize cgroups at system boot, and initialize any
2547 * subsystems that request early init.
ddbcc7e8
PM
2548 */
2549int __init cgroup_init_early(void)
2550{
2551 int i;
817929ec
PM
2552 kref_init(&init_css_set.ref);
2553 kref_get(&init_css_set.ref);
817929ec
PM
2554 INIT_LIST_HEAD(&init_css_set.cg_links);
2555 INIT_LIST_HEAD(&init_css_set.tasks);
472b1053 2556 INIT_HLIST_NODE(&init_css_set.hlist);
817929ec 2557 css_set_count = 1;
ddbcc7e8
PM
2558 init_cgroup_root(&rootnode);
2559 list_add(&rootnode.root_list, &roots);
817929ec
PM
2560 root_count = 1;
2561 init_task.cgroups = &init_css_set;
2562
2563 init_css_set_link.cg = &init_css_set;
bd89aabc 2564 list_add(&init_css_set_link.cgrp_link_list,
817929ec
PM
2565 &rootnode.top_cgroup.css_sets);
2566 list_add(&init_css_set_link.cg_link_list,
2567 &init_css_set.cg_links);
ddbcc7e8 2568
472b1053
LZ
2569 for (i = 0; i < CSS_SET_TABLE_SIZE; i++)
2570 INIT_HLIST_HEAD(&css_set_table[i]);
2571
ddbcc7e8
PM
2572 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2573 struct cgroup_subsys *ss = subsys[i];
2574
2575 BUG_ON(!ss->name);
2576 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
2577 BUG_ON(!ss->create);
2578 BUG_ON(!ss->destroy);
2579 if (ss->subsys_id != i) {
cfe36bde 2580 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
ddbcc7e8
PM
2581 ss->name, ss->subsys_id);
2582 BUG();
2583 }
2584
2585 if (ss->early_init)
2586 cgroup_init_subsys(ss);
2587 }
2588 return 0;
2589}
2590
2591/**
a043e3b2
LZ
2592 * cgroup_init - cgroup initialization
2593 *
2594 * Register cgroup filesystem and /proc file, and initialize
2595 * any subsystems that didn't request early init.
ddbcc7e8
PM
2596 */
2597int __init cgroup_init(void)
2598{
2599 int err;
2600 int i;
472b1053 2601 struct hlist_head *hhead;
a424316c
PM
2602
2603 err = bdi_init(&cgroup_backing_dev_info);
2604 if (err)
2605 return err;
ddbcc7e8
PM
2606
2607 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2608 struct cgroup_subsys *ss = subsys[i];
2609 if (!ss->early_init)
2610 cgroup_init_subsys(ss);
2611 }
2612
472b1053
LZ
2613 /* Add init_css_set to the hash table */
2614 hhead = css_set_hash(init_css_set.subsys);
2615 hlist_add_head(&init_css_set.hlist, hhead);
2616
ddbcc7e8
PM
2617 err = register_filesystem(&cgroup_fs_type);
2618 if (err < 0)
2619 goto out;
2620
46ae220b 2621 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
a424316c 2622
ddbcc7e8 2623out:
a424316c
PM
2624 if (err)
2625 bdi_destroy(&cgroup_backing_dev_info);
2626
ddbcc7e8
PM
2627 return err;
2628}
b4f48b63 2629
a424316c
PM
2630/*
2631 * proc_cgroup_show()
2632 * - Print task's cgroup paths into seq_file, one line for each hierarchy
2633 * - Used for /proc/<pid>/cgroup.
2634 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
2635 * doesn't really matter if tsk->cgroup changes after we read it,
956db3ca 2636 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
a424316c
PM
2637 * anyway. No need to check that tsk->cgroup != NULL, thanks to
2638 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
2639 * cgroup to top_cgroup.
2640 */
2641
2642/* TODO: Use a proper seq_file iterator */
2643static int proc_cgroup_show(struct seq_file *m, void *v)
2644{
2645 struct pid *pid;
2646 struct task_struct *tsk;
2647 char *buf;
2648 int retval;
2649 struct cgroupfs_root *root;
2650
2651 retval = -ENOMEM;
2652 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2653 if (!buf)
2654 goto out;
2655
2656 retval = -ESRCH;
2657 pid = m->private;
2658 tsk = get_pid_task(pid, PIDTYPE_PID);
2659 if (!tsk)
2660 goto out_free;
2661
2662 retval = 0;
2663
2664 mutex_lock(&cgroup_mutex);
2665
2666 for_each_root(root) {
2667 struct cgroup_subsys *ss;
bd89aabc 2668 struct cgroup *cgrp;
a424316c
PM
2669 int subsys_id;
2670 int count = 0;
2671
2672 /* Skip this hierarchy if it has no active subsystems */
2673 if (!root->actual_subsys_bits)
2674 continue;
b6c3006d 2675 seq_printf(m, "%lu:", root->subsys_bits);
a424316c
PM
2676 for_each_subsys(root, ss)
2677 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
2678 seq_putc(m, ':');
2679 get_first_subsys(&root->top_cgroup, NULL, &subsys_id);
bd89aabc
PM
2680 cgrp = task_cgroup(tsk, subsys_id);
2681 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
a424316c
PM
2682 if (retval < 0)
2683 goto out_unlock;
2684 seq_puts(m, buf);
2685 seq_putc(m, '\n');
2686 }
2687
2688out_unlock:
2689 mutex_unlock(&cgroup_mutex);
2690 put_task_struct(tsk);
2691out_free:
2692 kfree(buf);
2693out:
2694 return retval;
2695}
2696
2697static int cgroup_open(struct inode *inode, struct file *file)
2698{
2699 struct pid *pid = PROC_I(inode)->pid;
2700 return single_open(file, proc_cgroup_show, pid);
2701}
2702
2703struct file_operations proc_cgroup_operations = {
2704 .open = cgroup_open,
2705 .read = seq_read,
2706 .llseek = seq_lseek,
2707 .release = single_release,
2708};
2709
2710/* Display information about each subsystem and each hierarchy */
2711static int proc_cgroupstats_show(struct seq_file *m, void *v)
2712{
2713 int i;
a424316c 2714
8bab8dde 2715 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
a424316c 2716 mutex_lock(&cgroup_mutex);
a424316c
PM
2717 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2718 struct cgroup_subsys *ss = subsys[i];
8bab8dde 2719 seq_printf(m, "%s\t%lu\t%d\t%d\n",
817929ec 2720 ss->name, ss->root->subsys_bits,
8bab8dde 2721 ss->root->number_of_cgroups, !ss->disabled);
a424316c
PM
2722 }
2723 mutex_unlock(&cgroup_mutex);
2724 return 0;
2725}
2726
2727static int cgroupstats_open(struct inode *inode, struct file *file)
2728{
9dce07f1 2729 return single_open(file, proc_cgroupstats_show, NULL);
a424316c
PM
2730}
2731
2732static struct file_operations proc_cgroupstats_operations = {
2733 .open = cgroupstats_open,
2734 .read = seq_read,
2735 .llseek = seq_lseek,
2736 .release = single_release,
2737};
2738
b4f48b63
PM
2739/**
2740 * cgroup_fork - attach newly forked task to its parents cgroup.
a043e3b2 2741 * @child: pointer to task_struct of forking parent process.
b4f48b63
PM
2742 *
2743 * Description: A task inherits its parent's cgroup at fork().
2744 *
2745 * A pointer to the shared css_set was automatically copied in
2746 * fork.c by dup_task_struct(). However, we ignore that copy, since
2747 * it was not made under the protection of RCU or cgroup_mutex, so
956db3ca 2748 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
817929ec
PM
2749 * have already changed current->cgroups, allowing the previously
2750 * referenced cgroup group to be removed and freed.
b4f48b63
PM
2751 *
2752 * At the point that cgroup_fork() is called, 'current' is the parent
2753 * task, and the passed argument 'child' points to the child task.
2754 */
2755void cgroup_fork(struct task_struct *child)
2756{
817929ec
PM
2757 task_lock(current);
2758 child->cgroups = current->cgroups;
2759 get_css_set(child->cgroups);
2760 task_unlock(current);
2761 INIT_LIST_HEAD(&child->cg_list);
b4f48b63
PM
2762}
2763
2764/**
a043e3b2
LZ
2765 * cgroup_fork_callbacks - run fork callbacks
2766 * @child: the new task
2767 *
2768 * Called on a new task very soon before adding it to the
2769 * tasklist. No need to take any locks since no-one can
2770 * be operating on this task.
b4f48b63
PM
2771 */
2772void cgroup_fork_callbacks(struct task_struct *child)
2773{
2774 if (need_forkexit_callback) {
2775 int i;
2776 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2777 struct cgroup_subsys *ss = subsys[i];
2778 if (ss->fork)
2779 ss->fork(ss, child);
2780 }
2781 }
2782}
2783
cf475ad2
BS
2784#ifdef CONFIG_MM_OWNER
2785/**
2786 * cgroup_mm_owner_callbacks - run callbacks when the mm->owner changes
2787 * @p: the new owner
2788 *
2789 * Called on every change to mm->owner. mm_init_owner() does not
2790 * invoke this routine, since it assigns the mm->owner the first time
2791 * and does not change it.
2792 */
2793void cgroup_mm_owner_callbacks(struct task_struct *old, struct task_struct *new)
2794{
2795 struct cgroup *oldcgrp, *newcgrp;
2796
2797 if (need_mm_owner_callback) {
2798 int i;
2799 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2800 struct cgroup_subsys *ss = subsys[i];
2801 oldcgrp = task_cgroup(old, ss->subsys_id);
2802 newcgrp = task_cgroup(new, ss->subsys_id);
2803 if (oldcgrp == newcgrp)
2804 continue;
2805 if (ss->mm_owner_changed)
2806 ss->mm_owner_changed(ss, oldcgrp, newcgrp);
2807 }
2808 }
2809}
2810#endif /* CONFIG_MM_OWNER */
2811
817929ec 2812/**
a043e3b2
LZ
2813 * cgroup_post_fork - called on a new task after adding it to the task list
2814 * @child: the task in question
2815 *
2816 * Adds the task to the list running through its css_set if necessary.
2817 * Has to be after the task is visible on the task list in case we race
2818 * with the first call to cgroup_iter_start() - to guarantee that the
2819 * new task ends up on its list.
2820 */
817929ec
PM
2821void cgroup_post_fork(struct task_struct *child)
2822{
2823 if (use_task_css_set_links) {
2824 write_lock(&css_set_lock);
2825 if (list_empty(&child->cg_list))
2826 list_add(&child->cg_list, &child->cgroups->tasks);
2827 write_unlock(&css_set_lock);
2828 }
2829}
b4f48b63
PM
2830/**
2831 * cgroup_exit - detach cgroup from exiting task
2832 * @tsk: pointer to task_struct of exiting process
a043e3b2 2833 * @run_callback: run exit callbacks?
b4f48b63
PM
2834 *
2835 * Description: Detach cgroup from @tsk and release it.
2836 *
2837 * Note that cgroups marked notify_on_release force every task in
2838 * them to take the global cgroup_mutex mutex when exiting.
2839 * This could impact scaling on very large systems. Be reluctant to
2840 * use notify_on_release cgroups where very high task exit scaling
2841 * is required on large systems.
2842 *
2843 * the_top_cgroup_hack:
2844 *
2845 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
2846 *
2847 * We call cgroup_exit() while the task is still competent to
2848 * handle notify_on_release(), then leave the task attached to the
2849 * root cgroup in each hierarchy for the remainder of its exit.
2850 *
2851 * To do this properly, we would increment the reference count on
2852 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
2853 * code we would add a second cgroup function call, to drop that
2854 * reference. This would just create an unnecessary hot spot on
2855 * the top_cgroup reference count, to no avail.
2856 *
2857 * Normally, holding a reference to a cgroup without bumping its
2858 * count is unsafe. The cgroup could go away, or someone could
2859 * attach us to a different cgroup, decrementing the count on
2860 * the first cgroup that we never incremented. But in this case,
2861 * top_cgroup isn't going away, and either task has PF_EXITING set,
956db3ca
CW
2862 * which wards off any cgroup_attach_task() attempts, or task is a failed
2863 * fork, never visible to cgroup_attach_task.
b4f48b63
PM
2864 */
2865void cgroup_exit(struct task_struct *tsk, int run_callbacks)
2866{
2867 int i;
817929ec 2868 struct css_set *cg;
b4f48b63
PM
2869
2870 if (run_callbacks && need_forkexit_callback) {
2871 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2872 struct cgroup_subsys *ss = subsys[i];
2873 if (ss->exit)
2874 ss->exit(ss, tsk);
2875 }
2876 }
817929ec
PM
2877
2878 /*
2879 * Unlink from the css_set task list if necessary.
2880 * Optimistically check cg_list before taking
2881 * css_set_lock
2882 */
2883 if (!list_empty(&tsk->cg_list)) {
2884 write_lock(&css_set_lock);
2885 if (!list_empty(&tsk->cg_list))
2886 list_del(&tsk->cg_list);
2887 write_unlock(&css_set_lock);
2888 }
2889
b4f48b63
PM
2890 /* Reassign the task to the init_css_set. */
2891 task_lock(tsk);
817929ec
PM
2892 cg = tsk->cgroups;
2893 tsk->cgroups = &init_css_set;
b4f48b63 2894 task_unlock(tsk);
817929ec 2895 if (cg)
81a6a5cd 2896 put_css_set_taskexit(cg);
b4f48b63 2897}
697f4161
PM
2898
2899/**
a043e3b2
LZ
2900 * cgroup_clone - clone the cgroup the given subsystem is attached to
2901 * @tsk: the task to be moved
2902 * @subsys: the given subsystem
2903 *
2904 * Duplicate the current cgroup in the hierarchy that the given
2905 * subsystem is attached to, and move this task into the new
2906 * child.
697f4161
PM
2907 */
2908int cgroup_clone(struct task_struct *tsk, struct cgroup_subsys *subsys)
2909{
2910 struct dentry *dentry;
2911 int ret = 0;
2912 char nodename[MAX_CGROUP_TYPE_NAMELEN];
2913 struct cgroup *parent, *child;
2914 struct inode *inode;
2915 struct css_set *cg;
2916 struct cgroupfs_root *root;
2917 struct cgroup_subsys *ss;
2918
2919 /* We shouldn't be called by an unregistered subsystem */
2920 BUG_ON(!subsys->active);
2921
2922 /* First figure out what hierarchy and cgroup we're dealing
2923 * with, and pin them so we can drop cgroup_mutex */
2924 mutex_lock(&cgroup_mutex);
2925 again:
2926 root = subsys->root;
2927 if (root == &rootnode) {
2928 printk(KERN_INFO
2929 "Not cloning cgroup for unused subsystem %s\n",
2930 subsys->name);
2931 mutex_unlock(&cgroup_mutex);
2932 return 0;
2933 }
817929ec 2934 cg = tsk->cgroups;
697f4161
PM
2935 parent = task_cgroup(tsk, subsys->subsys_id);
2936
5c02b575 2937 snprintf(nodename, MAX_CGROUP_TYPE_NAMELEN, "%d", tsk->pid);
697f4161
PM
2938
2939 /* Pin the hierarchy */
2940 atomic_inc(&parent->root->sb->s_active);
2941
817929ec
PM
2942 /* Keep the cgroup alive */
2943 get_css_set(cg);
697f4161
PM
2944 mutex_unlock(&cgroup_mutex);
2945
2946 /* Now do the VFS work to create a cgroup */
2947 inode = parent->dentry->d_inode;
2948
2949 /* Hold the parent directory mutex across this operation to
2950 * stop anyone else deleting the new cgroup */
2951 mutex_lock(&inode->i_mutex);
2952 dentry = lookup_one_len(nodename, parent->dentry, strlen(nodename));
2953 if (IS_ERR(dentry)) {
2954 printk(KERN_INFO
cfe36bde 2955 "cgroup: Couldn't allocate dentry for %s: %ld\n", nodename,
697f4161
PM
2956 PTR_ERR(dentry));
2957 ret = PTR_ERR(dentry);
2958 goto out_release;
2959 }
2960
2961 /* Create the cgroup directory, which also creates the cgroup */
2962 ret = vfs_mkdir(inode, dentry, S_IFDIR | 0755);
bd89aabc 2963 child = __d_cgrp(dentry);
697f4161
PM
2964 dput(dentry);
2965 if (ret) {
2966 printk(KERN_INFO
2967 "Failed to create cgroup %s: %d\n", nodename,
2968 ret);
2969 goto out_release;
2970 }
2971
2972 if (!child) {
2973 printk(KERN_INFO
2974 "Couldn't find new cgroup %s\n", nodename);
2975 ret = -ENOMEM;
2976 goto out_release;
2977 }
2978
2979 /* The cgroup now exists. Retake cgroup_mutex and check
2980 * that we're still in the same state that we thought we
2981 * were. */
2982 mutex_lock(&cgroup_mutex);
2983 if ((root != subsys->root) ||
2984 (parent != task_cgroup(tsk, subsys->subsys_id))) {
2985 /* Aargh, we raced ... */
2986 mutex_unlock(&inode->i_mutex);
817929ec 2987 put_css_set(cg);
697f4161
PM
2988
2989 deactivate_super(parent->root->sb);
2990 /* The cgroup is still accessible in the VFS, but
2991 * we're not going to try to rmdir() it at this
2992 * point. */
2993 printk(KERN_INFO
2994 "Race in cgroup_clone() - leaking cgroup %s\n",
2995 nodename);
2996 goto again;
2997 }
2998
2999 /* do any required auto-setup */
3000 for_each_subsys(root, ss) {
3001 if (ss->post_clone)
3002 ss->post_clone(ss, child);
3003 }
3004
3005 /* All seems fine. Finish by moving the task into the new cgroup */
956db3ca 3006 ret = cgroup_attach_task(child, tsk);
697f4161
PM
3007 mutex_unlock(&cgroup_mutex);
3008
3009 out_release:
3010 mutex_unlock(&inode->i_mutex);
81a6a5cd
PM
3011
3012 mutex_lock(&cgroup_mutex);
817929ec 3013 put_css_set(cg);
81a6a5cd 3014 mutex_unlock(&cgroup_mutex);
697f4161
PM
3015 deactivate_super(parent->root->sb);
3016 return ret;
3017}
3018
a043e3b2
LZ
3019/**
3020 * cgroup_is_descendant - see if @cgrp is a descendant of current task's cgrp
3021 * @cgrp: the cgroup in question
3022 *
3023 * See if @cgrp is a descendant of the current task's cgroup in
3024 * the appropriate hierarchy.
697f4161
PM
3025 *
3026 * If we are sending in dummytop, then presumably we are creating
3027 * the top cgroup in the subsystem.
3028 *
3029 * Called only by the ns (nsproxy) cgroup.
3030 */
bd89aabc 3031int cgroup_is_descendant(const struct cgroup *cgrp)
697f4161
PM
3032{
3033 int ret;
3034 struct cgroup *target;
3035 int subsys_id;
3036
bd89aabc 3037 if (cgrp == dummytop)
697f4161
PM
3038 return 1;
3039
bd89aabc 3040 get_first_subsys(cgrp, NULL, &subsys_id);
697f4161 3041 target = task_cgroup(current, subsys_id);
bd89aabc
PM
3042 while (cgrp != target && cgrp!= cgrp->top_cgroup)
3043 cgrp = cgrp->parent;
3044 ret = (cgrp == target);
697f4161
PM
3045 return ret;
3046}
81a6a5cd 3047
bd89aabc 3048static void check_for_release(struct cgroup *cgrp)
81a6a5cd
PM
3049{
3050 /* All of these checks rely on RCU to keep the cgroup
3051 * structure alive */
bd89aabc
PM
3052 if (cgroup_is_releasable(cgrp) && !atomic_read(&cgrp->count)
3053 && list_empty(&cgrp->children) && !cgroup_has_css_refs(cgrp)) {
81a6a5cd
PM
3054 /* Control Group is currently removeable. If it's not
3055 * already queued for a userspace notification, queue
3056 * it now */
3057 int need_schedule_work = 0;
3058 spin_lock(&release_list_lock);
bd89aabc
PM
3059 if (!cgroup_is_removed(cgrp) &&
3060 list_empty(&cgrp->release_list)) {
3061 list_add(&cgrp->release_list, &release_list);
81a6a5cd
PM
3062 need_schedule_work = 1;
3063 }
3064 spin_unlock(&release_list_lock);
3065 if (need_schedule_work)
3066 schedule_work(&release_agent_work);
3067 }
3068}
3069
3070void __css_put(struct cgroup_subsys_state *css)
3071{
bd89aabc 3072 struct cgroup *cgrp = css->cgroup;
81a6a5cd 3073 rcu_read_lock();
bd89aabc
PM
3074 if (atomic_dec_and_test(&css->refcnt) && notify_on_release(cgrp)) {
3075 set_bit(CGRP_RELEASABLE, &cgrp->flags);
3076 check_for_release(cgrp);
81a6a5cd
PM
3077 }
3078 rcu_read_unlock();
3079}
3080
3081/*
3082 * Notify userspace when a cgroup is released, by running the
3083 * configured release agent with the name of the cgroup (path
3084 * relative to the root of cgroup file system) as the argument.
3085 *
3086 * Most likely, this user command will try to rmdir this cgroup.
3087 *
3088 * This races with the possibility that some other task will be
3089 * attached to this cgroup before it is removed, or that some other
3090 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
3091 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
3092 * unused, and this cgroup will be reprieved from its death sentence,
3093 * to continue to serve a useful existence. Next time it's released,
3094 * we will get notified again, if it still has 'notify_on_release' set.
3095 *
3096 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
3097 * means only wait until the task is successfully execve()'d. The
3098 * separate release agent task is forked by call_usermodehelper(),
3099 * then control in this thread returns here, without waiting for the
3100 * release agent task. We don't bother to wait because the caller of
3101 * this routine has no use for the exit status of the release agent
3102 * task, so no sense holding our caller up for that.
81a6a5cd 3103 */
81a6a5cd
PM
3104static void cgroup_release_agent(struct work_struct *work)
3105{
3106 BUG_ON(work != &release_agent_work);
3107 mutex_lock(&cgroup_mutex);
3108 spin_lock(&release_list_lock);
3109 while (!list_empty(&release_list)) {
3110 char *argv[3], *envp[3];
3111 int i;
e788e066 3112 char *pathbuf = NULL, *agentbuf = NULL;
bd89aabc 3113 struct cgroup *cgrp = list_entry(release_list.next,
81a6a5cd
PM
3114 struct cgroup,
3115 release_list);
bd89aabc 3116 list_del_init(&cgrp->release_list);
81a6a5cd
PM
3117 spin_unlock(&release_list_lock);
3118 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
e788e066
PM
3119 if (!pathbuf)
3120 goto continue_free;
3121 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
3122 goto continue_free;
3123 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
3124 if (!agentbuf)
3125 goto continue_free;
81a6a5cd
PM
3126
3127 i = 0;
e788e066
PM
3128 argv[i++] = agentbuf;
3129 argv[i++] = pathbuf;
81a6a5cd
PM
3130 argv[i] = NULL;
3131
3132 i = 0;
3133 /* minimal command environment */
3134 envp[i++] = "HOME=/";
3135 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
3136 envp[i] = NULL;
3137
3138 /* Drop the lock while we invoke the usermode helper,
3139 * since the exec could involve hitting disk and hence
3140 * be a slow process */
3141 mutex_unlock(&cgroup_mutex);
3142 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
81a6a5cd 3143 mutex_lock(&cgroup_mutex);
e788e066
PM
3144 continue_free:
3145 kfree(pathbuf);
3146 kfree(agentbuf);
81a6a5cd
PM
3147 spin_lock(&release_list_lock);
3148 }
3149 spin_unlock(&release_list_lock);
3150 mutex_unlock(&cgroup_mutex);
3151}
8bab8dde
PM
3152
3153static int __init cgroup_disable(char *str)
3154{
3155 int i;
3156 char *token;
3157
3158 while ((token = strsep(&str, ",")) != NULL) {
3159 if (!*token)
3160 continue;
3161
3162 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3163 struct cgroup_subsys *ss = subsys[i];
3164
3165 if (!strcmp(token, ss->name)) {
3166 ss->disabled = 1;
3167 printk(KERN_INFO "Disabling %s control group"
3168 " subsystem\n", ss->name);
3169 break;
3170 }
3171 }
3172 }
3173 return 1;
3174}
3175__setup("cgroup_disable=", cgroup_disable);