blob: f4c4dce9558f8c40630cc2f15b5576a8520ca86f [file] [log] [blame]
Paul Menageddbcc7e2007-10-18 23:39:30 -07001/*
2 * kernel/cgroup.c
3 *
4 * Generic process-grouping system.
5 *
6 * Based originally on the cpuset system, extracted by Paul Menage
7 * Copyright (C) 2006 Google, Inc
8 *
9 * Copyright notices from the original cpuset code:
10 * --------------------------------------------------
11 * Copyright (C) 2003 BULL SA.
12 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
13 *
14 * Portions derived from Patrick Mochel's sysfs code.
15 * sysfs is Copyright (c) 2001-3 Patrick Mochel
16 *
17 * 2003-10-10 Written by Simon Derr.
18 * 2003-10-22 Updates by Stephen Hemminger.
19 * 2004 May-July Rework by Paul Jackson.
20 * ---------------------------------------------------
21 *
22 * This file is subject to the terms and conditions of the GNU General Public
23 * License. See the file COPYING in the main directory of the Linux
24 * distribution for more details.
25 */
26
27#include <linux/cgroup.h>
28#include <linux/errno.h>
29#include <linux/fs.h>
30#include <linux/kernel.h>
31#include <linux/list.h>
32#include <linux/mm.h>
33#include <linux/mutex.h>
34#include <linux/mount.h>
35#include <linux/pagemap.h>
36#include <linux/rcupdate.h>
37#include <linux/sched.h>
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>
Paul Menagebbcb81d2007-10-18 23:39:32 -070043#include <linux/sort.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070044#include <asm/atomic.h>
45
46/* Generate an array of cgroup subsystem pointers */
47#define SUBSYS(_x) &_x ## _subsys,
48
49static struct cgroup_subsys *subsys[] = {
50#include <linux/cgroup_subsys.h>
51};
52
53/*
54 * A cgroupfs_root represents the root of a cgroup hierarchy,
55 * and may be associated with a superblock to form an active
56 * hierarchy
57 */
58struct cgroupfs_root {
59 struct super_block *sb;
60
61 /*
62 * The bitmask of subsystems intended to be attached to this
63 * hierarchy
64 */
65 unsigned long subsys_bits;
66
67 /* The bitmask of subsystems currently attached to this hierarchy */
68 unsigned long actual_subsys_bits;
69
70 /* A list running through the attached subsystems */
71 struct list_head subsys_list;
72
73 /* The root cgroup for this hierarchy */
74 struct cgroup top_cgroup;
75
76 /* Tracks how many cgroups are currently defined in hierarchy.*/
77 int number_of_cgroups;
78
79 /* A list running through the mounted hierarchies */
80 struct list_head root_list;
81
82 /* Hierarchy-specific flags */
83 unsigned long flags;
84};
85
86
87/*
88 * The "rootnode" hierarchy is the "dummy hierarchy", reserved for the
89 * subsystems that are otherwise unattached - it never has more than a
90 * single cgroup, and all tasks are part of that cgroup.
91 */
92static struct cgroupfs_root rootnode;
93
94/* The list of hierarchy roots */
95
96static LIST_HEAD(roots);
97
98/* dummytop is a shorthand for the dummy hierarchy's top cgroup */
99#define dummytop (&rootnode.top_cgroup)
100
101/* This flag indicates whether tasks in the fork and exit paths should
102 * take callback_mutex and check for fork/exit handlers to call. This
103 * avoids us having to do extra work in the fork/exit path if none of the
104 * subsystems need to be called.
105 */
106static int need_forkexit_callback;
107
108/* bits in struct cgroup flags field */
109enum {
110 CONT_REMOVED,
111};
112
113/* convenient tests for these bits */
114inline int cgroup_is_removed(const struct cgroup *cont)
115{
116 return test_bit(CONT_REMOVED, &cont->flags);
117}
118
119/* bits in struct cgroupfs_root flags field */
120enum {
121 ROOT_NOPREFIX, /* mounted subsystems have no named prefix */
122};
123
124/*
125 * for_each_subsys() allows you to iterate on each subsystem attached to
126 * an active hierarchy
127 */
128#define for_each_subsys(_root, _ss) \
129list_for_each_entry(_ss, &_root->subsys_list, sibling)
130
131/* for_each_root() allows you to iterate across the active hierarchies */
132#define for_each_root(_root) \
133list_for_each_entry(_root, &roots, root_list)
134
135/*
136 * There is one global cgroup mutex. We also require taking
137 * task_lock() when dereferencing a task's cgroup subsys pointers.
138 * See "The task_lock() exception", at the end of this comment.
139 *
140 * A task must hold cgroup_mutex to modify cgroups.
141 *
142 * Any task can increment and decrement the count field without lock.
143 * So in general, code holding cgroup_mutex can't rely on the count
144 * field not changing. However, if the count goes to zero, then only
145 * attach_task() can increment it again. Because a count of zero
146 * means that no tasks are currently attached, therefore there is no
147 * way a task attached to that cgroup can fork (the other way to
148 * increment the count). So code holding cgroup_mutex can safely
149 * assume that if the count is zero, it will stay zero. Similarly, if
150 * a task holds cgroup_mutex on a cgroup with zero count, it
151 * knows that the cgroup won't be removed, as cgroup_rmdir()
152 * needs that mutex.
153 *
154 * The cgroup_common_file_write handler for operations that modify
155 * the cgroup hierarchy holds cgroup_mutex across the entire operation,
156 * single threading all such cgroup modifications across the system.
157 *
158 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
159 * (usually) take cgroup_mutex. These are the two most performance
160 * critical pieces of code here. The exception occurs on cgroup_exit(),
161 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
162 * is taken, and if the cgroup count is zero, a usermode call made
163 * to /sbin/cgroup_release_agent with the name of the cgroup (path
164 * relative to the root of cgroup file system) as the argument.
165 *
166 * A cgroup can only be deleted if both its 'count' of using tasks
167 * is zero, and its list of 'children' cgroups is empty. Since all
168 * tasks in the system use _some_ cgroup, and since there is always at
169 * least one task in the system (init, pid == 1), therefore, top_cgroup
170 * always has either children cgroups and/or using tasks. So we don't
171 * need a special hack to ensure that top_cgroup cannot be deleted.
172 *
173 * The task_lock() exception
174 *
175 * The need for this exception arises from the action of
176 * attach_task(), which overwrites one tasks cgroup pointer with
177 * another. It does so using cgroup_mutexe, however there are
178 * several performance critical places that need to reference
179 * task->cgroup without the expense of grabbing a system global
180 * mutex. Therefore except as noted below, when dereferencing or, as
181 * in attach_task(), modifying a task'ss cgroup pointer we use
182 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
183 * the task_struct routinely used for such matters.
184 *
185 * P.S. One more locking exception. RCU is used to guard the
186 * update of a tasks cgroup pointer by attach_task()
187 */
188
189static DEFINE_MUTEX(cgroup_mutex);
190
191/**
192 * cgroup_lock - lock out any changes to cgroup structures
193 *
194 */
195
196void cgroup_lock(void)
197{
198 mutex_lock(&cgroup_mutex);
199}
200
201/**
202 * cgroup_unlock - release lock on cgroup changes
203 *
204 * Undo the lock taken in a previous cgroup_lock() call.
205 */
206
207void cgroup_unlock(void)
208{
209 mutex_unlock(&cgroup_mutex);
210}
211
212/*
213 * A couple of forward declarations required, due to cyclic reference loop:
214 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
215 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
216 * -> cgroup_mkdir.
217 */
218
219static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode);
220static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
221static int cgroup_populate_dir(struct cgroup *cont);
222static struct inode_operations cgroup_dir_inode_operations;
223
224static struct inode *cgroup_new_inode(mode_t mode, struct super_block *sb)
225{
226 struct inode *inode = new_inode(sb);
227 static struct backing_dev_info cgroup_backing_dev_info = {
228 .capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK,
229 };
230
231 if (inode) {
232 inode->i_mode = mode;
233 inode->i_uid = current->fsuid;
234 inode->i_gid = current->fsgid;
235 inode->i_blocks = 0;
236 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
237 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
238 }
239 return inode;
240}
241
242static void cgroup_diput(struct dentry *dentry, struct inode *inode)
243{
244 /* is dentry a directory ? if so, kfree() associated cgroup */
245 if (S_ISDIR(inode->i_mode)) {
246 struct cgroup *cont = dentry->d_fsdata;
247 BUG_ON(!(cgroup_is_removed(cont)));
248 kfree(cont);
249 }
250 iput(inode);
251}
252
253static void remove_dir(struct dentry *d)
254{
255 struct dentry *parent = dget(d->d_parent);
256
257 d_delete(d);
258 simple_rmdir(parent->d_inode, d);
259 dput(parent);
260}
261
262static void cgroup_clear_directory(struct dentry *dentry)
263{
264 struct list_head *node;
265
266 BUG_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
267 spin_lock(&dcache_lock);
268 node = dentry->d_subdirs.next;
269 while (node != &dentry->d_subdirs) {
270 struct dentry *d = list_entry(node, struct dentry, d_u.d_child);
271 list_del_init(node);
272 if (d->d_inode) {
273 /* This should never be called on a cgroup
274 * directory with child cgroups */
275 BUG_ON(d->d_inode->i_mode & S_IFDIR);
276 d = dget_locked(d);
277 spin_unlock(&dcache_lock);
278 d_delete(d);
279 simple_unlink(dentry->d_inode, d);
280 dput(d);
281 spin_lock(&dcache_lock);
282 }
283 node = dentry->d_subdirs.next;
284 }
285 spin_unlock(&dcache_lock);
286}
287
288/*
289 * NOTE : the dentry must have been dget()'ed
290 */
291static void cgroup_d_remove_dir(struct dentry *dentry)
292{
293 cgroup_clear_directory(dentry);
294
295 spin_lock(&dcache_lock);
296 list_del_init(&dentry->d_u.d_child);
297 spin_unlock(&dcache_lock);
298 remove_dir(dentry);
299}
300
301static int rebind_subsystems(struct cgroupfs_root *root,
302 unsigned long final_bits)
303{
304 unsigned long added_bits, removed_bits;
305 struct cgroup *cont = &root->top_cgroup;
306 int i;
307
308 removed_bits = root->actual_subsys_bits & ~final_bits;
309 added_bits = final_bits & ~root->actual_subsys_bits;
310 /* Check that any added subsystems are currently free */
311 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
312 unsigned long long bit = 1ull << i;
313 struct cgroup_subsys *ss = subsys[i];
314 if (!(bit & added_bits))
315 continue;
316 if (ss->root != &rootnode) {
317 /* Subsystem isn't free */
318 return -EBUSY;
319 }
320 }
321
322 /* Currently we don't handle adding/removing subsystems when
323 * any child cgroups exist. This is theoretically supportable
324 * but involves complex error handling, so it's being left until
325 * later */
326 if (!list_empty(&cont->children))
327 return -EBUSY;
328
329 /* Process each subsystem */
330 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
331 struct cgroup_subsys *ss = subsys[i];
332 unsigned long bit = 1UL << i;
333 if (bit & added_bits) {
334 /* We're binding this subsystem to this hierarchy */
335 BUG_ON(cont->subsys[i]);
336 BUG_ON(!dummytop->subsys[i]);
337 BUG_ON(dummytop->subsys[i]->cgroup != dummytop);
338 cont->subsys[i] = dummytop->subsys[i];
339 cont->subsys[i]->cgroup = cont;
340 list_add(&ss->sibling, &root->subsys_list);
341 rcu_assign_pointer(ss->root, root);
342 if (ss->bind)
343 ss->bind(ss, cont);
344
345 } else if (bit & removed_bits) {
346 /* We're removing this subsystem */
347 BUG_ON(cont->subsys[i] != dummytop->subsys[i]);
348 BUG_ON(cont->subsys[i]->cgroup != cont);
349 if (ss->bind)
350 ss->bind(ss, dummytop);
351 dummytop->subsys[i]->cgroup = dummytop;
352 cont->subsys[i] = NULL;
353 rcu_assign_pointer(subsys[i]->root, &rootnode);
354 list_del(&ss->sibling);
355 } else if (bit & final_bits) {
356 /* Subsystem state should already exist */
357 BUG_ON(!cont->subsys[i]);
358 } else {
359 /* Subsystem state shouldn't exist */
360 BUG_ON(cont->subsys[i]);
361 }
362 }
363 root->subsys_bits = root->actual_subsys_bits = final_bits;
364 synchronize_rcu();
365
366 return 0;
367}
368
369static int cgroup_show_options(struct seq_file *seq, struct vfsmount *vfs)
370{
371 struct cgroupfs_root *root = vfs->mnt_sb->s_fs_info;
372 struct cgroup_subsys *ss;
373
374 mutex_lock(&cgroup_mutex);
375 for_each_subsys(root, ss)
376 seq_printf(seq, ",%s", ss->name);
377 if (test_bit(ROOT_NOPREFIX, &root->flags))
378 seq_puts(seq, ",noprefix");
379 mutex_unlock(&cgroup_mutex);
380 return 0;
381}
382
383struct cgroup_sb_opts {
384 unsigned long subsys_bits;
385 unsigned long flags;
386};
387
388/* Convert a hierarchy specifier into a bitmask of subsystems and
389 * flags. */
390static int parse_cgroupfs_options(char *data,
391 struct cgroup_sb_opts *opts)
392{
393 char *token, *o = data ?: "all";
394
395 opts->subsys_bits = 0;
396 opts->flags = 0;
397
398 while ((token = strsep(&o, ",")) != NULL) {
399 if (!*token)
400 return -EINVAL;
401 if (!strcmp(token, "all")) {
402 opts->subsys_bits = (1 << CGROUP_SUBSYS_COUNT) - 1;
403 } else if (!strcmp(token, "noprefix")) {
404 set_bit(ROOT_NOPREFIX, &opts->flags);
405 } else {
406 struct cgroup_subsys *ss;
407 int i;
408 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
409 ss = subsys[i];
410 if (!strcmp(token, ss->name)) {
411 set_bit(i, &opts->subsys_bits);
412 break;
413 }
414 }
415 if (i == CGROUP_SUBSYS_COUNT)
416 return -ENOENT;
417 }
418 }
419
420 /* We can't have an empty hierarchy */
421 if (!opts->subsys_bits)
422 return -EINVAL;
423
424 return 0;
425}
426
427static int cgroup_remount(struct super_block *sb, int *flags, char *data)
428{
429 int ret = 0;
430 struct cgroupfs_root *root = sb->s_fs_info;
431 struct cgroup *cont = &root->top_cgroup;
432 struct cgroup_sb_opts opts;
433
434 mutex_lock(&cont->dentry->d_inode->i_mutex);
435 mutex_lock(&cgroup_mutex);
436
437 /* See what subsystems are wanted */
438 ret = parse_cgroupfs_options(data, &opts);
439 if (ret)
440 goto out_unlock;
441
442 /* Don't allow flags to change at remount */
443 if (opts.flags != root->flags) {
444 ret = -EINVAL;
445 goto out_unlock;
446 }
447
448 ret = rebind_subsystems(root, opts.subsys_bits);
449
450 /* (re)populate subsystem files */
451 if (!ret)
452 cgroup_populate_dir(cont);
453
454 out_unlock:
455 mutex_unlock(&cgroup_mutex);
456 mutex_unlock(&cont->dentry->d_inode->i_mutex);
457 return ret;
458}
459
460static struct super_operations cgroup_ops = {
461 .statfs = simple_statfs,
462 .drop_inode = generic_delete_inode,
463 .show_options = cgroup_show_options,
464 .remount_fs = cgroup_remount,
465};
466
467static void init_cgroup_root(struct cgroupfs_root *root)
468{
469 struct cgroup *cont = &root->top_cgroup;
470 INIT_LIST_HEAD(&root->subsys_list);
471 INIT_LIST_HEAD(&root->root_list);
472 root->number_of_cgroups = 1;
473 cont->root = root;
474 cont->top_cgroup = cont;
475 INIT_LIST_HEAD(&cont->sibling);
476 INIT_LIST_HEAD(&cont->children);
477}
478
479static int cgroup_test_super(struct super_block *sb, void *data)
480{
481 struct cgroupfs_root *new = data;
482 struct cgroupfs_root *root = sb->s_fs_info;
483
484 /* First check subsystems */
485 if (new->subsys_bits != root->subsys_bits)
486 return 0;
487
488 /* Next check flags */
489 if (new->flags != root->flags)
490 return 0;
491
492 return 1;
493}
494
495static int cgroup_set_super(struct super_block *sb, void *data)
496{
497 int ret;
498 struct cgroupfs_root *root = data;
499
500 ret = set_anon_super(sb, NULL);
501 if (ret)
502 return ret;
503
504 sb->s_fs_info = root;
505 root->sb = sb;
506
507 sb->s_blocksize = PAGE_CACHE_SIZE;
508 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
509 sb->s_magic = CGROUP_SUPER_MAGIC;
510 sb->s_op = &cgroup_ops;
511
512 return 0;
513}
514
515static int cgroup_get_rootdir(struct super_block *sb)
516{
517 struct inode *inode =
518 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
519 struct dentry *dentry;
520
521 if (!inode)
522 return -ENOMEM;
523
524 inode->i_op = &simple_dir_inode_operations;
525 inode->i_fop = &simple_dir_operations;
526 inode->i_op = &cgroup_dir_inode_operations;
527 /* directories start off with i_nlink == 2 (for "." entry) */
528 inc_nlink(inode);
529 dentry = d_alloc_root(inode);
530 if (!dentry) {
531 iput(inode);
532 return -ENOMEM;
533 }
534 sb->s_root = dentry;
535 return 0;
536}
537
538static int cgroup_get_sb(struct file_system_type *fs_type,
539 int flags, const char *unused_dev_name,
540 void *data, struct vfsmount *mnt)
541{
542 struct cgroup_sb_opts opts;
543 int ret = 0;
544 struct super_block *sb;
545 struct cgroupfs_root *root;
546
547 /* First find the desired set of subsystems */
548 ret = parse_cgroupfs_options(data, &opts);
549 if (ret)
550 return ret;
551
552 root = kzalloc(sizeof(*root), GFP_KERNEL);
553 if (!root)
554 return -ENOMEM;
555
556 init_cgroup_root(root);
557 root->subsys_bits = opts.subsys_bits;
558 root->flags = opts.flags;
559
560 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, root);
561
562 if (IS_ERR(sb)) {
563 kfree(root);
564 return PTR_ERR(sb);
565 }
566
567 if (sb->s_fs_info != root) {
568 /* Reusing an existing superblock */
569 BUG_ON(sb->s_root == NULL);
570 kfree(root);
571 root = NULL;
572 } else {
573 /* New superblock */
574 struct cgroup *cont = &root->top_cgroup;
575
576 BUG_ON(sb->s_root != NULL);
577
578 ret = cgroup_get_rootdir(sb);
579 if (ret)
580 goto drop_new_super;
581
582 mutex_lock(&cgroup_mutex);
583
584 ret = rebind_subsystems(root, root->subsys_bits);
585 if (ret == -EBUSY) {
586 mutex_unlock(&cgroup_mutex);
587 goto drop_new_super;
588 }
589
590 /* EBUSY should be the only error here */
591 BUG_ON(ret);
592
593 list_add(&root->root_list, &roots);
594
595 sb->s_root->d_fsdata = &root->top_cgroup;
596 root->top_cgroup.dentry = sb->s_root;
597
598 BUG_ON(!list_empty(&cont->sibling));
599 BUG_ON(!list_empty(&cont->children));
600 BUG_ON(root->number_of_cgroups != 1);
601
602 /*
603 * I believe that it's safe to nest i_mutex inside
604 * cgroup_mutex in this case, since no-one else can
605 * be accessing this directory yet. But we still need
606 * to teach lockdep that this is the case - currently
607 * a cgroupfs remount triggers a lockdep warning
608 */
609 mutex_lock(&cont->dentry->d_inode->i_mutex);
610 cgroup_populate_dir(cont);
611 mutex_unlock(&cont->dentry->d_inode->i_mutex);
612 mutex_unlock(&cgroup_mutex);
613 }
614
615 return simple_set_mnt(mnt, sb);
616
617 drop_new_super:
618 up_write(&sb->s_umount);
619 deactivate_super(sb);
620 return ret;
621}
622
623static void cgroup_kill_sb(struct super_block *sb) {
624 struct cgroupfs_root *root = sb->s_fs_info;
625 struct cgroup *cont = &root->top_cgroup;
626 int ret;
627
628 BUG_ON(!root);
629
630 BUG_ON(root->number_of_cgroups != 1);
631 BUG_ON(!list_empty(&cont->children));
632 BUG_ON(!list_empty(&cont->sibling));
633
634 mutex_lock(&cgroup_mutex);
635
636 /* Rebind all subsystems back to the default hierarchy */
637 ret = rebind_subsystems(root, 0);
638 /* Shouldn't be able to fail ... */
639 BUG_ON(ret);
640
641 if (!list_empty(&root->root_list))
642 list_del(&root->root_list);
643 mutex_unlock(&cgroup_mutex);
644
645 kfree(root);
646 kill_litter_super(sb);
647}
648
649static struct file_system_type cgroup_fs_type = {
650 .name = "cgroup",
651 .get_sb = cgroup_get_sb,
652 .kill_sb = cgroup_kill_sb,
653};
654
655static inline struct cgroup *__d_cont(struct dentry *dentry)
656{
657 return dentry->d_fsdata;
658}
659
660static inline struct cftype *__d_cft(struct dentry *dentry)
661{
662 return dentry->d_fsdata;
663}
664
665/*
666 * Called with cgroup_mutex held. Writes path of cgroup into buf.
667 * Returns 0 on success, -errno on error.
668 */
669int cgroup_path(const struct cgroup *cont, char *buf, int buflen)
670{
671 char *start;
672
673 if (cont == dummytop) {
674 /*
675 * Inactive subsystems have no dentry for their root
676 * cgroup
677 */
678 strcpy(buf, "/");
679 return 0;
680 }
681
682 start = buf + buflen;
683
684 *--start = '\0';
685 for (;;) {
686 int len = cont->dentry->d_name.len;
687 if ((start -= len) < buf)
688 return -ENAMETOOLONG;
689 memcpy(start, cont->dentry->d_name.name, len);
690 cont = cont->parent;
691 if (!cont)
692 break;
693 if (!cont->parent)
694 continue;
695 if (--start < buf)
696 return -ENAMETOOLONG;
697 *start = '/';
698 }
699 memmove(buf, start, buf + buflen - start);
700 return 0;
701}
702
Paul Menagebbcb81d2007-10-18 23:39:32 -0700703/*
704 * Return the first subsystem attached to a cgroup's hierarchy, and
705 * its subsystem id.
706 */
707
708static void get_first_subsys(const struct cgroup *cont,
709 struct cgroup_subsys_state **css, int *subsys_id)
710{
711 const struct cgroupfs_root *root = cont->root;
712 const struct cgroup_subsys *test_ss;
713 BUG_ON(list_empty(&root->subsys_list));
714 test_ss = list_entry(root->subsys_list.next,
715 struct cgroup_subsys, sibling);
716 if (css) {
717 *css = cont->subsys[test_ss->subsys_id];
718 BUG_ON(!*css);
719 }
720 if (subsys_id)
721 *subsys_id = test_ss->subsys_id;
722}
723
724/*
725 * Attach task 'tsk' to cgroup 'cont'
726 *
727 * Call holding cgroup_mutex. May take task_lock of
728 * the task 'pid' during call.
729 */
730static int attach_task(struct cgroup *cont, struct task_struct *tsk)
731{
732 int retval = 0;
733 struct cgroup_subsys *ss;
734 struct cgroup *oldcont;
735 struct css_set *cg = &tsk->cgroups;
736 struct cgroupfs_root *root = cont->root;
737 int i;
738 int subsys_id;
739
740 get_first_subsys(cont, NULL, &subsys_id);
741
742 /* Nothing to do if the task is already in that cgroup */
743 oldcont = task_cgroup(tsk, subsys_id);
744 if (cont == oldcont)
745 return 0;
746
747 for_each_subsys(root, ss) {
748 if (ss->can_attach) {
749 retval = ss->can_attach(ss, cont, tsk);
750 if (retval) {
751 return retval;
752 }
753 }
754 }
755
756 task_lock(tsk);
757 if (tsk->flags & PF_EXITING) {
758 task_unlock(tsk);
759 return -ESRCH;
760 }
761 /* Update the css_set pointers for the subsystems in this
762 * hierarchy */
763 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
764 if (root->subsys_bits & (1ull << i)) {
765 /* Subsystem is in this hierarchy. So we want
766 * the subsystem state from the new
767 * cgroup. Transfer the refcount from the
768 * old to the new */
769 atomic_inc(&cont->count);
770 atomic_dec(&cg->subsys[i]->cgroup->count);
771 rcu_assign_pointer(cg->subsys[i], cont->subsys[i]);
772 }
773 }
774 task_unlock(tsk);
775
776 for_each_subsys(root, ss) {
777 if (ss->attach) {
778 ss->attach(ss, cont, oldcont, tsk);
779 }
780 }
781
782 synchronize_rcu();
783 return 0;
784}
785
786/*
787 * Attach task with pid 'pid' to cgroup 'cont'. Call with
788 * cgroup_mutex, may take task_lock of task
789 */
790static int attach_task_by_pid(struct cgroup *cont, char *pidbuf)
791{
792 pid_t pid;
793 struct task_struct *tsk;
794 int ret;
795
796 if (sscanf(pidbuf, "%d", &pid) != 1)
797 return -EIO;
798
799 if (pid) {
800 rcu_read_lock();
801 tsk = find_task_by_pid(pid);
802 if (!tsk || tsk->flags & PF_EXITING) {
803 rcu_read_unlock();
804 return -ESRCH;
805 }
806 get_task_struct(tsk);
807 rcu_read_unlock();
808
809 if ((current->euid) && (current->euid != tsk->uid)
810 && (current->euid != tsk->suid)) {
811 put_task_struct(tsk);
812 return -EACCES;
813 }
814 } else {
815 tsk = current;
816 get_task_struct(tsk);
817 }
818
819 ret = attach_task(cont, tsk);
820 put_task_struct(tsk);
821 return ret;
822}
823
Paul Menageddbcc7e2007-10-18 23:39:30 -0700824/* The various types of files and directories in a cgroup file system */
825
826enum cgroup_filetype {
827 FILE_ROOT,
828 FILE_DIR,
829 FILE_TASKLIST,
830};
831
Paul Menage355e0c42007-10-18 23:39:33 -0700832static ssize_t cgroup_write_uint(struct cgroup *cont, struct cftype *cft,
833 struct file *file,
834 const char __user *userbuf,
835 size_t nbytes, loff_t *unused_ppos)
836{
837 char buffer[64];
838 int retval = 0;
839 u64 val;
840 char *end;
841
842 if (!nbytes)
843 return -EINVAL;
844 if (nbytes >= sizeof(buffer))
845 return -E2BIG;
846 if (copy_from_user(buffer, userbuf, nbytes))
847 return -EFAULT;
848
849 buffer[nbytes] = 0; /* nul-terminate */
850
851 /* strip newline if necessary */
852 if (nbytes && (buffer[nbytes-1] == '\n'))
853 buffer[nbytes-1] = 0;
854 val = simple_strtoull(buffer, &end, 0);
855 if (*end)
856 return -EINVAL;
857
858 /* Pass to subsystem */
859 retval = cft->write_uint(cont, cft, val);
860 if (!retval)
861 retval = nbytes;
862 return retval;
863}
864
Paul Menagebbcb81d2007-10-18 23:39:32 -0700865static ssize_t cgroup_common_file_write(struct cgroup *cont,
866 struct cftype *cft,
867 struct file *file,
868 const char __user *userbuf,
869 size_t nbytes, loff_t *unused_ppos)
870{
871 enum cgroup_filetype type = cft->private;
872 char *buffer;
873 int retval = 0;
874
875 if (nbytes >= PATH_MAX)
876 return -E2BIG;
877
878 /* +1 for nul-terminator */
879 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
880 if (buffer == NULL)
881 return -ENOMEM;
882
883 if (copy_from_user(buffer, userbuf, nbytes)) {
884 retval = -EFAULT;
885 goto out1;
886 }
887 buffer[nbytes] = 0; /* nul-terminate */
888
889 mutex_lock(&cgroup_mutex);
890
891 if (cgroup_is_removed(cont)) {
892 retval = -ENODEV;
893 goto out2;
894 }
895
896 switch (type) {
897 case FILE_TASKLIST:
898 retval = attach_task_by_pid(cont, buffer);
899 break;
900 default:
901 retval = -EINVAL;
902 goto out2;
903 }
904
905 if (retval == 0)
906 retval = nbytes;
907out2:
908 mutex_unlock(&cgroup_mutex);
909out1:
910 kfree(buffer);
911 return retval;
912}
913
Paul Menageddbcc7e2007-10-18 23:39:30 -0700914static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
915 size_t nbytes, loff_t *ppos)
916{
917 struct cftype *cft = __d_cft(file->f_dentry);
918 struct cgroup *cont = __d_cont(file->f_dentry->d_parent);
919
920 if (!cft)
921 return -ENODEV;
Paul Menage355e0c42007-10-18 23:39:33 -0700922 if (cft->write)
923 return cft->write(cont, cft, file, buf, nbytes, ppos);
924 if (cft->write_uint)
925 return cgroup_write_uint(cont, cft, file, buf, nbytes, ppos);
926 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700927}
928
929static ssize_t cgroup_read_uint(struct cgroup *cont, struct cftype *cft,
930 struct file *file,
931 char __user *buf, size_t nbytes,
932 loff_t *ppos)
933{
934 char tmp[64];
935 u64 val = cft->read_uint(cont, cft);
936 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
937
938 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
939}
940
941static ssize_t cgroup_file_read(struct file *file, char __user *buf,
942 size_t nbytes, loff_t *ppos)
943{
944 struct cftype *cft = __d_cft(file->f_dentry);
945 struct cgroup *cont = __d_cont(file->f_dentry->d_parent);
946
947 if (!cft)
948 return -ENODEV;
949
950 if (cft->read)
951 return cft->read(cont, cft, file, buf, nbytes, ppos);
952 if (cft->read_uint)
953 return cgroup_read_uint(cont, cft, file, buf, nbytes, ppos);
954 return -EINVAL;
955}
956
957static int cgroup_file_open(struct inode *inode, struct file *file)
958{
959 int err;
960 struct cftype *cft;
961
962 err = generic_file_open(inode, file);
963 if (err)
964 return err;
965
966 cft = __d_cft(file->f_dentry);
967 if (!cft)
968 return -ENODEV;
969 if (cft->open)
970 err = cft->open(inode, file);
971 else
972 err = 0;
973
974 return err;
975}
976
977static int cgroup_file_release(struct inode *inode, struct file *file)
978{
979 struct cftype *cft = __d_cft(file->f_dentry);
980 if (cft->release)
981 return cft->release(inode, file);
982 return 0;
983}
984
985/*
986 * cgroup_rename - Only allow simple rename of directories in place.
987 */
988static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
989 struct inode *new_dir, struct dentry *new_dentry)
990{
991 if (!S_ISDIR(old_dentry->d_inode->i_mode))
992 return -ENOTDIR;
993 if (new_dentry->d_inode)
994 return -EEXIST;
995 if (old_dir != new_dir)
996 return -EIO;
997 return simple_rename(old_dir, old_dentry, new_dir, new_dentry);
998}
999
1000static struct file_operations cgroup_file_operations = {
1001 .read = cgroup_file_read,
1002 .write = cgroup_file_write,
1003 .llseek = generic_file_llseek,
1004 .open = cgroup_file_open,
1005 .release = cgroup_file_release,
1006};
1007
1008static struct inode_operations cgroup_dir_inode_operations = {
1009 .lookup = simple_lookup,
1010 .mkdir = cgroup_mkdir,
1011 .rmdir = cgroup_rmdir,
1012 .rename = cgroup_rename,
1013};
1014
1015static int cgroup_create_file(struct dentry *dentry, int mode,
1016 struct super_block *sb)
1017{
1018 static struct dentry_operations cgroup_dops = {
1019 .d_iput = cgroup_diput,
1020 };
1021
1022 struct inode *inode;
1023
1024 if (!dentry)
1025 return -ENOENT;
1026 if (dentry->d_inode)
1027 return -EEXIST;
1028
1029 inode = cgroup_new_inode(mode, sb);
1030 if (!inode)
1031 return -ENOMEM;
1032
1033 if (S_ISDIR(mode)) {
1034 inode->i_op = &cgroup_dir_inode_operations;
1035 inode->i_fop = &simple_dir_operations;
1036
1037 /* start off with i_nlink == 2 (for "." entry) */
1038 inc_nlink(inode);
1039
1040 /* start with the directory inode held, so that we can
1041 * populate it without racing with another mkdir */
1042 mutex_lock(&inode->i_mutex);
1043 } else if (S_ISREG(mode)) {
1044 inode->i_size = 0;
1045 inode->i_fop = &cgroup_file_operations;
1046 }
1047 dentry->d_op = &cgroup_dops;
1048 d_instantiate(dentry, inode);
1049 dget(dentry); /* Extra count - pin the dentry in core */
1050 return 0;
1051}
1052
1053/*
1054 * cgroup_create_dir - create a directory for an object.
1055 * cont: the cgroup we create the directory for.
1056 * It must have a valid ->parent field
1057 * And we are going to fill its ->dentry field.
1058 * dentry: dentry of the new container
1059 * mode: mode to set on new directory.
1060 */
1061static int cgroup_create_dir(struct cgroup *cont, struct dentry *dentry,
1062 int mode)
1063{
1064 struct dentry *parent;
1065 int error = 0;
1066
1067 parent = cont->parent->dentry;
1068 error = cgroup_create_file(dentry, S_IFDIR | mode, cont->root->sb);
1069 if (!error) {
1070 dentry->d_fsdata = cont;
1071 inc_nlink(parent->d_inode);
1072 cont->dentry = dentry;
1073 dget(dentry);
1074 }
1075 dput(dentry);
1076
1077 return error;
1078}
1079
1080int cgroup_add_file(struct cgroup *cont,
1081 struct cgroup_subsys *subsys,
1082 const struct cftype *cft)
1083{
1084 struct dentry *dir = cont->dentry;
1085 struct dentry *dentry;
1086 int error;
1087
1088 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
1089 if (subsys && !test_bit(ROOT_NOPREFIX, &cont->root->flags)) {
1090 strcpy(name, subsys->name);
1091 strcat(name, ".");
1092 }
1093 strcat(name, cft->name);
1094 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
1095 dentry = lookup_one_len(name, dir, strlen(name));
1096 if (!IS_ERR(dentry)) {
1097 error = cgroup_create_file(dentry, 0644 | S_IFREG,
1098 cont->root->sb);
1099 if (!error)
1100 dentry->d_fsdata = (void *)cft;
1101 dput(dentry);
1102 } else
1103 error = PTR_ERR(dentry);
1104 return error;
1105}
1106
1107int cgroup_add_files(struct cgroup *cont,
1108 struct cgroup_subsys *subsys,
1109 const struct cftype cft[],
1110 int count)
1111{
1112 int i, err;
1113 for (i = 0; i < count; i++) {
1114 err = cgroup_add_file(cont, subsys, &cft[i]);
1115 if (err)
1116 return err;
1117 }
1118 return 0;
1119}
1120
Paul Menagebbcb81d2007-10-18 23:39:32 -07001121/* Count the number of tasks in a cgroup. Could be made more
1122 * time-efficient but less space-efficient with more linked lists
1123 * running through each cgroup and the css_set structures that
1124 * referenced it. Must be called with tasklist_lock held for read or
1125 * write or in an rcu critical section.
1126 */
1127int __cgroup_task_count(const struct cgroup *cont)
1128{
1129 int count = 0;
1130 struct task_struct *g, *p;
1131 struct cgroup_subsys_state *css;
1132 int subsys_id;
1133
1134 get_first_subsys(cont, &css, &subsys_id);
1135 do_each_thread(g, p) {
1136 if (task_subsys_state(p, subsys_id) == css)
1137 count ++;
1138 } while_each_thread(g, p);
1139 return count;
1140}
1141
1142/*
1143 * Stuff for reading the 'tasks' file.
1144 *
1145 * Reading this file can return large amounts of data if a cgroup has
1146 * *lots* of attached tasks. So it may need several calls to read(),
1147 * but we cannot guarantee that the information we produce is correct
1148 * unless we produce it entirely atomically.
1149 *
1150 * Upon tasks file open(), a struct ctr_struct is allocated, that
1151 * will have a pointer to an array (also allocated here). The struct
1152 * ctr_struct * is stored in file->private_data. Its resources will
1153 * be freed by release() when the file is closed. The array is used
1154 * to sprintf the PIDs and then used by read().
1155 */
1156struct ctr_struct {
1157 char *buf;
1158 int bufsz;
1159};
1160
1161/*
1162 * Load into 'pidarray' up to 'npids' of the tasks using cgroup
1163 * 'cont'. Return actual number of pids loaded. No need to
1164 * task_lock(p) when reading out p->cgroup, since we're in an RCU
1165 * read section, so the css_set can't go away, and is
1166 * immutable after creation.
1167 */
1168static int pid_array_load(pid_t *pidarray, int npids, struct cgroup *cont)
1169{
1170 int n = 0;
1171 struct task_struct *g, *p;
1172 struct cgroup_subsys_state *css;
1173 int subsys_id;
1174
1175 get_first_subsys(cont, &css, &subsys_id);
1176 rcu_read_lock();
1177 do_each_thread(g, p) {
1178 if (task_subsys_state(p, subsys_id) == css) {
1179 pidarray[n++] = pid_nr(task_pid(p));
1180 if (unlikely(n == npids))
1181 goto array_full;
1182 }
1183 } while_each_thread(g, p);
1184
1185array_full:
1186 rcu_read_unlock();
1187 return n;
1188}
1189
1190static int cmppid(const void *a, const void *b)
1191{
1192 return *(pid_t *)a - *(pid_t *)b;
1193}
1194
1195/*
1196 * Convert array 'a' of 'npids' pid_t's to a string of newline separated
1197 * decimal pids in 'buf'. Don't write more than 'sz' chars, but return
1198 * count 'cnt' of how many chars would be written if buf were large enough.
1199 */
1200static int pid_array_to_buf(char *buf, int sz, pid_t *a, int npids)
1201{
1202 int cnt = 0;
1203 int i;
1204
1205 for (i = 0; i < npids; i++)
1206 cnt += snprintf(buf + cnt, max(sz - cnt, 0), "%d\n", a[i]);
1207 return cnt;
1208}
1209
1210/*
1211 * Handle an open on 'tasks' file. Prepare a buffer listing the
1212 * process id's of tasks currently attached to the cgroup being opened.
1213 *
1214 * Does not require any specific cgroup mutexes, and does not take any.
1215 */
1216static int cgroup_tasks_open(struct inode *unused, struct file *file)
1217{
1218 struct cgroup *cont = __d_cont(file->f_dentry->d_parent);
1219 struct ctr_struct *ctr;
1220 pid_t *pidarray;
1221 int npids;
1222 char c;
1223
1224 if (!(file->f_mode & FMODE_READ))
1225 return 0;
1226
1227 ctr = kmalloc(sizeof(*ctr), GFP_KERNEL);
1228 if (!ctr)
1229 goto err0;
1230
1231 /*
1232 * If cgroup gets more users after we read count, we won't have
1233 * enough space - tough. This race is indistinguishable to the
1234 * caller from the case that the additional cgroup users didn't
1235 * show up until sometime later on.
1236 */
1237 npids = cgroup_task_count(cont);
1238 if (npids) {
1239 pidarray = kmalloc(npids * sizeof(pid_t), GFP_KERNEL);
1240 if (!pidarray)
1241 goto err1;
1242
1243 npids = pid_array_load(pidarray, npids, cont);
1244 sort(pidarray, npids, sizeof(pid_t), cmppid, NULL);
1245
1246 /* Call pid_array_to_buf() twice, first just to get bufsz */
1247 ctr->bufsz = pid_array_to_buf(&c, sizeof(c), pidarray, npids) + 1;
1248 ctr->buf = kmalloc(ctr->bufsz, GFP_KERNEL);
1249 if (!ctr->buf)
1250 goto err2;
1251 ctr->bufsz = pid_array_to_buf(ctr->buf, ctr->bufsz, pidarray, npids);
1252
1253 kfree(pidarray);
1254 } else {
1255 ctr->buf = 0;
1256 ctr->bufsz = 0;
1257 }
1258 file->private_data = ctr;
1259 return 0;
1260
1261err2:
1262 kfree(pidarray);
1263err1:
1264 kfree(ctr);
1265err0:
1266 return -ENOMEM;
1267}
1268
1269static ssize_t cgroup_tasks_read(struct cgroup *cont,
1270 struct cftype *cft,
1271 struct file *file, char __user *buf,
1272 size_t nbytes, loff_t *ppos)
1273{
1274 struct ctr_struct *ctr = file->private_data;
1275
1276 return simple_read_from_buffer(buf, nbytes, ppos, ctr->buf, ctr->bufsz);
1277}
1278
1279static int cgroup_tasks_release(struct inode *unused_inode,
1280 struct file *file)
1281{
1282 struct ctr_struct *ctr;
1283
1284 if (file->f_mode & FMODE_READ) {
1285 ctr = file->private_data;
1286 kfree(ctr->buf);
1287 kfree(ctr);
1288 }
1289 return 0;
1290}
1291
1292/*
1293 * for the common functions, 'private' gives the type of file
1294 */
1295static struct cftype cft_tasks = {
1296 .name = "tasks",
1297 .open = cgroup_tasks_open,
1298 .read = cgroup_tasks_read,
1299 .write = cgroup_common_file_write,
1300 .release = cgroup_tasks_release,
1301 .private = FILE_TASKLIST,
1302};
1303
Paul Menageddbcc7e2007-10-18 23:39:30 -07001304static int cgroup_populate_dir(struct cgroup *cont)
1305{
1306 int err;
1307 struct cgroup_subsys *ss;
1308
1309 /* First clear out any existing files */
1310 cgroup_clear_directory(cont->dentry);
1311
Paul Menagebbcb81d2007-10-18 23:39:32 -07001312 err = cgroup_add_file(cont, NULL, &cft_tasks);
1313 if (err < 0)
1314 return err;
1315
Paul Menageddbcc7e2007-10-18 23:39:30 -07001316 for_each_subsys(cont->root, ss) {
1317 if (ss->populate && (err = ss->populate(ss, cont)) < 0)
1318 return err;
1319 }
1320
1321 return 0;
1322}
1323
1324static void init_cgroup_css(struct cgroup_subsys_state *css,
1325 struct cgroup_subsys *ss,
1326 struct cgroup *cont)
1327{
1328 css->cgroup = cont;
1329 atomic_set(&css->refcnt, 0);
1330 css->flags = 0;
1331 if (cont == dummytop)
1332 set_bit(CSS_ROOT, &css->flags);
1333 BUG_ON(cont->subsys[ss->subsys_id]);
1334 cont->subsys[ss->subsys_id] = css;
1335}
1336
1337/*
1338 * cgroup_create - create a cgroup
1339 * parent: cgroup that will be parent of the new cgroup.
1340 * name: name of the new cgroup. Will be strcpy'ed.
1341 * mode: mode to set on new inode
1342 *
1343 * Must be called with the mutex on the parent inode held
1344 */
1345
1346static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
1347 int mode)
1348{
1349 struct cgroup *cont;
1350 struct cgroupfs_root *root = parent->root;
1351 int err = 0;
1352 struct cgroup_subsys *ss;
1353 struct super_block *sb = root->sb;
1354
1355 cont = kzalloc(sizeof(*cont), GFP_KERNEL);
1356 if (!cont)
1357 return -ENOMEM;
1358
1359 /* Grab a reference on the superblock so the hierarchy doesn't
1360 * get deleted on unmount if there are child cgroups. This
1361 * can be done outside cgroup_mutex, since the sb can't
1362 * disappear while someone has an open control file on the
1363 * fs */
1364 atomic_inc(&sb->s_active);
1365
1366 mutex_lock(&cgroup_mutex);
1367
1368 cont->flags = 0;
1369 INIT_LIST_HEAD(&cont->sibling);
1370 INIT_LIST_HEAD(&cont->children);
1371
1372 cont->parent = parent;
1373 cont->root = parent->root;
1374 cont->top_cgroup = parent->top_cgroup;
1375
1376 for_each_subsys(root, ss) {
1377 struct cgroup_subsys_state *css = ss->create(ss, cont);
1378 if (IS_ERR(css)) {
1379 err = PTR_ERR(css);
1380 goto err_destroy;
1381 }
1382 init_cgroup_css(css, ss, cont);
1383 }
1384
1385 list_add(&cont->sibling, &cont->parent->children);
1386 root->number_of_cgroups++;
1387
1388 err = cgroup_create_dir(cont, dentry, mode);
1389 if (err < 0)
1390 goto err_remove;
1391
1392 /* The cgroup directory was pre-locked for us */
1393 BUG_ON(!mutex_is_locked(&cont->dentry->d_inode->i_mutex));
1394
1395 err = cgroup_populate_dir(cont);
1396 /* If err < 0, we have a half-filled directory - oh well ;) */
1397
1398 mutex_unlock(&cgroup_mutex);
1399 mutex_unlock(&cont->dentry->d_inode->i_mutex);
1400
1401 return 0;
1402
1403 err_remove:
1404
1405 list_del(&cont->sibling);
1406 root->number_of_cgroups--;
1407
1408 err_destroy:
1409
1410 for_each_subsys(root, ss) {
1411 if (cont->subsys[ss->subsys_id])
1412 ss->destroy(ss, cont);
1413 }
1414
1415 mutex_unlock(&cgroup_mutex);
1416
1417 /* Release the reference count that we took on the superblock */
1418 deactivate_super(sb);
1419
1420 kfree(cont);
1421 return err;
1422}
1423
1424static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1425{
1426 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
1427
1428 /* the vfs holds inode->i_mutex already */
1429 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
1430}
1431
1432static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
1433{
1434 struct cgroup *cont = dentry->d_fsdata;
1435 struct dentry *d;
1436 struct cgroup *parent;
1437 struct cgroup_subsys *ss;
1438 struct super_block *sb;
1439 struct cgroupfs_root *root;
1440 int css_busy = 0;
1441
1442 /* the vfs holds both inode->i_mutex already */
1443
1444 mutex_lock(&cgroup_mutex);
1445 if (atomic_read(&cont->count) != 0) {
1446 mutex_unlock(&cgroup_mutex);
1447 return -EBUSY;
1448 }
1449 if (!list_empty(&cont->children)) {
1450 mutex_unlock(&cgroup_mutex);
1451 return -EBUSY;
1452 }
1453
1454 parent = cont->parent;
1455 root = cont->root;
1456 sb = root->sb;
1457
1458 /* Check the reference count on each subsystem. Since we
1459 * already established that there are no tasks in the
1460 * cgroup, if the css refcount is also 0, then there should
1461 * be no outstanding references, so the subsystem is safe to
1462 * destroy */
1463 for_each_subsys(root, ss) {
1464 struct cgroup_subsys_state *css;
1465 css = cont->subsys[ss->subsys_id];
1466 if (atomic_read(&css->refcnt)) {
1467 css_busy = 1;
1468 break;
1469 }
1470 }
1471 if (css_busy) {
1472 mutex_unlock(&cgroup_mutex);
1473 return -EBUSY;
1474 }
1475
1476 for_each_subsys(root, ss) {
1477 if (cont->subsys[ss->subsys_id])
1478 ss->destroy(ss, cont);
1479 }
1480
1481 set_bit(CONT_REMOVED, &cont->flags);
1482 /* delete my sibling from parent->children */
1483 list_del(&cont->sibling);
1484 spin_lock(&cont->dentry->d_lock);
1485 d = dget(cont->dentry);
1486 cont->dentry = NULL;
1487 spin_unlock(&d->d_lock);
1488
1489 cgroup_d_remove_dir(d);
1490 dput(d);
1491 root->number_of_cgroups--;
1492
1493 mutex_unlock(&cgroup_mutex);
1494 /* Drop the active superblock reference that we took when we
1495 * created the cgroup */
1496 deactivate_super(sb);
1497 return 0;
1498}
1499
1500static void cgroup_init_subsys(struct cgroup_subsys *ss)
1501{
1502 struct task_struct *g, *p;
1503 struct cgroup_subsys_state *css;
1504 printk(KERN_ERR "Initializing cgroup subsys %s\n", ss->name);
1505
1506 /* Create the top cgroup state for this subsystem */
1507 ss->root = &rootnode;
1508 css = ss->create(ss, dummytop);
1509 /* We don't handle early failures gracefully */
1510 BUG_ON(IS_ERR(css));
1511 init_cgroup_css(css, ss, dummytop);
1512
1513 /* Update all tasks to contain a subsys pointer to this state
1514 * - since the subsystem is newly registered, all tasks are in
1515 * the subsystem's top cgroup. */
1516
1517 /* If this subsystem requested that it be notified with fork
1518 * events, we should send it one now for every process in the
1519 * system */
1520
1521 read_lock(&tasklist_lock);
1522 init_task.cgroups.subsys[ss->subsys_id] = css;
1523 if (ss->fork)
1524 ss->fork(ss, &init_task);
1525
1526 do_each_thread(g, p) {
1527 printk(KERN_INFO "Setting task %p css to %p (%d)\n", css, p, p->pid);
1528 p->cgroups.subsys[ss->subsys_id] = css;
1529 if (ss->fork)
1530 ss->fork(ss, p);
1531 } while_each_thread(g, p);
1532 read_unlock(&tasklist_lock);
1533
1534 need_forkexit_callback |= ss->fork || ss->exit;
1535
1536 ss->active = 1;
1537}
1538
1539/**
1540 * cgroup_init_early - initialize cgroups at system boot, and
1541 * initialize any subsystems that request early init.
1542 */
1543int __init cgroup_init_early(void)
1544{
1545 int i;
1546 init_cgroup_root(&rootnode);
1547 list_add(&rootnode.root_list, &roots);
1548
1549 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1550 struct cgroup_subsys *ss = subsys[i];
1551
1552 BUG_ON(!ss->name);
1553 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
1554 BUG_ON(!ss->create);
1555 BUG_ON(!ss->destroy);
1556 if (ss->subsys_id != i) {
1557 printk(KERN_ERR "Subsys %s id == %d\n",
1558 ss->name, ss->subsys_id);
1559 BUG();
1560 }
1561
1562 if (ss->early_init)
1563 cgroup_init_subsys(ss);
1564 }
1565 return 0;
1566}
1567
1568/**
1569 * cgroup_init - register cgroup filesystem and /proc file, and
1570 * initialize any subsystems that didn't request early init.
1571 */
1572int __init cgroup_init(void)
1573{
1574 int err;
1575 int i;
1576
1577 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1578 struct cgroup_subsys *ss = subsys[i];
1579 if (!ss->early_init)
1580 cgroup_init_subsys(ss);
1581 }
1582
1583 err = register_filesystem(&cgroup_fs_type);
1584 if (err < 0)
1585 goto out;
1586
1587out:
1588 return err;
1589}