Merge git://git.kernel.org/pub/scm/linux/kernel/git/rusty/linux-2.6-for-linus
[linux-2.6/lfs.git] / kernel / cgroup.c
blobfbc6fc8949b4e718be405f500e977bb10841cf64
1 /*
2 * Generic process-grouping system.
4 * Based originally on the cpuset system, extracted by Paul Menage
5 * Copyright (C) 2006 Google, Inc
7 * Copyright notices from the original cpuset code:
8 * --------------------------------------------------
9 * Copyright (C) 2003 BULL SA.
10 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
12 * Portions derived from Patrick Mochel's sysfs code.
13 * sysfs is Copyright (c) 2001-3 Patrick Mochel
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 * ---------------------------------------------------
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.
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>
34 #include <linux/proc_fs.h>
35 #include <linux/rcupdate.h>
36 #include <linux/sched.h>
37 #include <linux/backing-dev.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>
43 #include <linux/sort.h>
44 #include <linux/kmod.h>
45 #include <linux/delayacct.h>
46 #include <linux/cgroupstats.h>
47 #include <linux/hash.h>
49 #include <asm/atomic.h>
51 static DEFINE_MUTEX(cgroup_mutex);
53 /* Generate an array of cgroup subsystem pointers */
54 #define SUBSYS(_x) &_x ## _subsys,
56 static struct cgroup_subsys *subsys[] = {
57 #include <linux/cgroup_subsys.h>
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
65 struct cgroupfs_root {
66 struct super_block *sb;
69 * The bitmask of subsystems intended to be attached to this
70 * hierarchy
72 unsigned long subsys_bits;
74 /* The bitmask of subsystems currently attached to this hierarchy */
75 unsigned long actual_subsys_bits;
77 /* A list running through the attached subsystems */
78 struct list_head subsys_list;
80 /* The root cgroup for this hierarchy */
81 struct cgroup top_cgroup;
83 /* Tracks how many cgroups are currently defined in hierarchy.*/
84 int number_of_cgroups;
86 /* A list running through the mounted hierarchies */
87 struct list_head root_list;
89 /* Hierarchy-specific flags */
90 unsigned long flags;
92 /* The path to use for release notifications. No locking
93 * between setting and use - so if userspace updates this
94 * while child cgroups exist, you could miss a
95 * notification. We ensure that it's always a valid
96 * NUL-terminated string */
97 char release_agent_path[PATH_MAX];
102 * The "rootnode" hierarchy is the "dummy hierarchy", reserved for the
103 * subsystems that are otherwise unattached - it never has more than a
104 * single cgroup, and all tasks are part of that cgroup.
106 static struct cgroupfs_root rootnode;
108 /* The list of hierarchy roots */
110 static LIST_HEAD(roots);
111 static int root_count;
113 /* dummytop is a shorthand for the dummy hierarchy's top cgroup */
114 #define dummytop (&rootnode.top_cgroup)
116 /* This flag indicates whether tasks in the fork and exit paths should
117 * check for fork/exit handlers to call. This avoids us having to do
118 * extra work in the fork/exit path if none of the subsystems need to
119 * be called.
121 static int need_forkexit_callback;
122 static int need_mm_owner_callback __read_mostly;
124 /* convenient tests for these bits */
125 inline int cgroup_is_removed(const struct cgroup *cgrp)
127 return test_bit(CGRP_REMOVED, &cgrp->flags);
130 /* bits in struct cgroupfs_root flags field */
131 enum {
132 ROOT_NOPREFIX, /* mounted subsystems have no named prefix */
135 static int cgroup_is_releasable(const struct cgroup *cgrp)
137 const int bits =
138 (1 << CGRP_RELEASABLE) |
139 (1 << CGRP_NOTIFY_ON_RELEASE);
140 return (cgrp->flags & bits) == bits;
143 static int notify_on_release(const struct cgroup *cgrp)
145 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
149 * for_each_subsys() allows you to iterate on each subsystem attached to
150 * an active hierarchy
152 #define for_each_subsys(_root, _ss) \
153 list_for_each_entry(_ss, &_root->subsys_list, sibling)
155 /* for_each_root() allows you to iterate across the active hierarchies */
156 #define for_each_root(_root) \
157 list_for_each_entry(_root, &roots, root_list)
159 /* the list of cgroups eligible for automatic release. Protected by
160 * release_list_lock */
161 static LIST_HEAD(release_list);
162 static DEFINE_SPINLOCK(release_list_lock);
163 static void cgroup_release_agent(struct work_struct *work);
164 static DECLARE_WORK(release_agent_work, cgroup_release_agent);
165 static void check_for_release(struct cgroup *cgrp);
167 /* Link structure for associating css_set objects with cgroups */
168 struct cg_cgroup_link {
170 * List running through cg_cgroup_links associated with a
171 * cgroup, anchored on cgroup->css_sets
173 struct list_head cgrp_link_list;
175 * List running through cg_cgroup_links pointing at a
176 * single css_set object, anchored on css_set->cg_links
178 struct list_head cg_link_list;
179 struct css_set *cg;
182 /* The default css_set - used by init and its children prior to any
183 * hierarchies being mounted. It contains a pointer to the root state
184 * for each subsystem. Also used to anchor the list of css_sets. Not
185 * reference-counted, to improve performance when child cgroups
186 * haven't been created.
189 static struct css_set init_css_set;
190 static struct cg_cgroup_link init_css_set_link;
192 /* css_set_lock protects the list of css_set objects, and the
193 * chain of tasks off each css_set. Nests outside task->alloc_lock
194 * due to cgroup_iter_start() */
195 static DEFINE_RWLOCK(css_set_lock);
196 static int css_set_count;
198 /* hash table for cgroup groups. This improves the performance to
199 * find an existing css_set */
200 #define CSS_SET_HASH_BITS 7
201 #define CSS_SET_TABLE_SIZE (1 << CSS_SET_HASH_BITS)
202 static struct hlist_head css_set_table[CSS_SET_TABLE_SIZE];
204 static struct hlist_head *css_set_hash(struct cgroup_subsys_state *css[])
206 int i;
207 int index;
208 unsigned long tmp = 0UL;
210 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++)
211 tmp += (unsigned long)css[i];
212 tmp = (tmp >> 16) ^ tmp;
214 index = hash_long(tmp, CSS_SET_HASH_BITS);
216 return &css_set_table[index];
219 /* We don't maintain the lists running through each css_set to its
220 * task until after the first call to cgroup_iter_start(). This
221 * reduces the fork()/exit() overhead for people who have cgroups
222 * compiled into their kernel but not actually in use */
223 static int use_task_css_set_links;
225 /* When we create or destroy a css_set, the operation simply
226 * takes/releases a reference count on all the cgroups referenced
227 * by subsystems in this css_set. This can end up multiple-counting
228 * some cgroups, but that's OK - the ref-count is just a
229 * busy/not-busy indicator; ensuring that we only count each cgroup
230 * once would require taking a global lock to ensure that no
231 * subsystems moved between hierarchies while we were doing so.
233 * Possible TODO: decide at boot time based on the number of
234 * registered subsystems and the number of CPUs or NUMA nodes whether
235 * it's better for performance to ref-count every subsystem, or to
236 * take a global lock and only add one ref count to each hierarchy.
240 * unlink a css_set from the list and free it
242 static void unlink_css_set(struct css_set *cg)
244 write_lock(&css_set_lock);
245 hlist_del(&cg->hlist);
246 css_set_count--;
247 while (!list_empty(&cg->cg_links)) {
248 struct cg_cgroup_link *link;
249 link = list_entry(cg->cg_links.next,
250 struct cg_cgroup_link, cg_link_list);
251 list_del(&link->cg_link_list);
252 list_del(&link->cgrp_link_list);
253 kfree(link);
255 write_unlock(&css_set_lock);
258 static void __release_css_set(struct kref *k, int taskexit)
260 int i;
261 struct css_set *cg = container_of(k, struct css_set, ref);
263 unlink_css_set(cg);
265 rcu_read_lock();
266 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
267 struct cgroup *cgrp = cg->subsys[i]->cgroup;
268 if (atomic_dec_and_test(&cgrp->count) &&
269 notify_on_release(cgrp)) {
270 if (taskexit)
271 set_bit(CGRP_RELEASABLE, &cgrp->flags);
272 check_for_release(cgrp);
275 rcu_read_unlock();
276 kfree(cg);
279 static void release_css_set(struct kref *k)
281 __release_css_set(k, 0);
284 static void release_css_set_taskexit(struct kref *k)
286 __release_css_set(k, 1);
290 * refcounted get/put for css_set objects
292 static inline void get_css_set(struct css_set *cg)
294 kref_get(&cg->ref);
297 static inline void put_css_set(struct css_set *cg)
299 kref_put(&cg->ref, release_css_set);
302 static inline void put_css_set_taskexit(struct css_set *cg)
304 kref_put(&cg->ref, release_css_set_taskexit);
308 * find_existing_css_set() is a helper for
309 * find_css_set(), and checks to see whether an existing
310 * css_set is suitable.
312 * oldcg: the cgroup group that we're using before the cgroup
313 * transition
315 * cgrp: the cgroup that we're moving into
317 * template: location in which to build the desired set of subsystem
318 * state objects for the new cgroup group
320 static struct css_set *find_existing_css_set(
321 struct css_set *oldcg,
322 struct cgroup *cgrp,
323 struct cgroup_subsys_state *template[])
325 int i;
326 struct cgroupfs_root *root = cgrp->root;
327 struct hlist_head *hhead;
328 struct hlist_node *node;
329 struct css_set *cg;
331 /* Built the set of subsystem state objects that we want to
332 * see in the new css_set */
333 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
334 if (root->subsys_bits & (1UL << i)) {
335 /* Subsystem is in this hierarchy. So we want
336 * the subsystem state from the new
337 * cgroup */
338 template[i] = cgrp->subsys[i];
339 } else {
340 /* Subsystem is not in this hierarchy, so we
341 * don't want to change the subsystem state */
342 template[i] = oldcg->subsys[i];
346 hhead = css_set_hash(template);
347 hlist_for_each_entry(cg, node, hhead, hlist) {
348 if (!memcmp(template, cg->subsys, sizeof(cg->subsys))) {
349 /* All subsystems matched */
350 return cg;
354 /* No existing cgroup group matched */
355 return NULL;
359 * allocate_cg_links() allocates "count" cg_cgroup_link structures
360 * and chains them on tmp through their cgrp_link_list fields. Returns 0 on
361 * success or a negative error
363 static int allocate_cg_links(int count, struct list_head *tmp)
365 struct cg_cgroup_link *link;
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) {
371 while (!list_empty(tmp)) {
372 link = list_entry(tmp->next,
373 struct cg_cgroup_link,
374 cgrp_link_list);
375 list_del(&link->cgrp_link_list);
376 kfree(link);
378 return -ENOMEM;
380 list_add(&link->cgrp_link_list, tmp);
382 return 0;
385 static void free_cg_links(struct list_head *tmp)
387 while (!list_empty(tmp)) {
388 struct cg_cgroup_link *link;
389 link = list_entry(tmp->next,
390 struct cg_cgroup_link,
391 cgrp_link_list);
392 list_del(&link->cgrp_link_list);
393 kfree(link);
398 * find_css_set() takes an existing cgroup group and a
399 * cgroup object, and returns a css_set object that's
400 * equivalent to the old group, but with the given cgroup
401 * substituted into the appropriate hierarchy. Must be called with
402 * cgroup_mutex held
404 static struct css_set *find_css_set(
405 struct css_set *oldcg, struct cgroup *cgrp)
407 struct css_set *res;
408 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT];
409 int i;
411 struct list_head tmp_cg_links;
412 struct cg_cgroup_link *link;
414 struct hlist_head *hhead;
416 /* First see if we already have a cgroup group that matches
417 * the desired set */
418 write_lock(&css_set_lock);
419 res = find_existing_css_set(oldcg, cgrp, template);
420 if (res)
421 get_css_set(res);
422 write_unlock(&css_set_lock);
424 if (res)
425 return res;
427 res = kmalloc(sizeof(*res), GFP_KERNEL);
428 if (!res)
429 return NULL;
431 /* Allocate all the cg_cgroup_link objects that we'll need */
432 if (allocate_cg_links(root_count, &tmp_cg_links) < 0) {
433 kfree(res);
434 return NULL;
437 kref_init(&res->ref);
438 INIT_LIST_HEAD(&res->cg_links);
439 INIT_LIST_HEAD(&res->tasks);
440 INIT_HLIST_NODE(&res->hlist);
442 /* Copy the set of subsystem state objects generated in
443 * find_existing_css_set() */
444 memcpy(res->subsys, template, sizeof(res->subsys));
446 write_lock(&css_set_lock);
447 /* Add reference counts and links from the new css_set. */
448 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
449 struct cgroup *cgrp = res->subsys[i]->cgroup;
450 struct cgroup_subsys *ss = subsys[i];
451 atomic_inc(&cgrp->count);
453 * We want to add a link once per cgroup, so we
454 * only do it for the first subsystem in each
455 * hierarchy
457 if (ss->root->subsys_list.next == &ss->sibling) {
458 BUG_ON(list_empty(&tmp_cg_links));
459 link = list_entry(tmp_cg_links.next,
460 struct cg_cgroup_link,
461 cgrp_link_list);
462 list_del(&link->cgrp_link_list);
463 list_add(&link->cgrp_link_list, &cgrp->css_sets);
464 link->cg = res;
465 list_add(&link->cg_link_list, &res->cg_links);
468 if (list_empty(&rootnode.subsys_list)) {
469 link = list_entry(tmp_cg_links.next,
470 struct cg_cgroup_link,
471 cgrp_link_list);
472 list_del(&link->cgrp_link_list);
473 list_add(&link->cgrp_link_list, &dummytop->css_sets);
474 link->cg = res;
475 list_add(&link->cg_link_list, &res->cg_links);
478 BUG_ON(!list_empty(&tmp_cg_links));
480 css_set_count++;
482 /* Add this cgroup group to the hash table */
483 hhead = css_set_hash(res->subsys);
484 hlist_add_head(&res->hlist, hhead);
486 write_unlock(&css_set_lock);
488 return res;
492 * There is one global cgroup mutex. We also require taking
493 * task_lock() when dereferencing a task's cgroup subsys pointers.
494 * See "The task_lock() exception", at the end of this comment.
496 * A task must hold cgroup_mutex to modify cgroups.
498 * Any task can increment and decrement the count field without lock.
499 * So in general, code holding cgroup_mutex can't rely on the count
500 * field not changing. However, if the count goes to zero, then only
501 * cgroup_attach_task() can increment it again. Because a count of zero
502 * means that no tasks are currently attached, therefore there is no
503 * way a task attached to that cgroup can fork (the other way to
504 * increment the count). So code holding cgroup_mutex can safely
505 * assume that if the count is zero, it will stay zero. Similarly, if
506 * a task holds cgroup_mutex on a cgroup with zero count, it
507 * knows that the cgroup won't be removed, as cgroup_rmdir()
508 * needs that mutex.
510 * The cgroup_common_file_write handler for operations that modify
511 * the cgroup hierarchy holds cgroup_mutex across the entire operation,
512 * single threading all such cgroup modifications across the system.
514 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
515 * (usually) take cgroup_mutex. These are the two most performance
516 * critical pieces of code here. The exception occurs on cgroup_exit(),
517 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
518 * is taken, and if the cgroup count is zero, a usermode call made
519 * to the release agent with the name of the cgroup (path relative to
520 * the root of cgroup file system) as the argument.
522 * A cgroup can only be deleted if both its 'count' of using tasks
523 * is zero, and its list of 'children' cgroups is empty. Since all
524 * tasks in the system use _some_ cgroup, and since there is always at
525 * least one task in the system (init, pid == 1), therefore, top_cgroup
526 * always has either children cgroups and/or using tasks. So we don't
527 * need a special hack to ensure that top_cgroup cannot be deleted.
529 * The task_lock() exception
531 * The need for this exception arises from the action of
532 * cgroup_attach_task(), which overwrites one tasks cgroup pointer with
533 * another. It does so using cgroup_mutex, however there are
534 * several performance critical places that need to reference
535 * task->cgroup without the expense of grabbing a system global
536 * mutex. Therefore except as noted below, when dereferencing or, as
537 * in cgroup_attach_task(), modifying a task'ss cgroup pointer we use
538 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
539 * the task_struct routinely used for such matters.
541 * P.S. One more locking exception. RCU is used to guard the
542 * update of a tasks cgroup pointer by cgroup_attach_task()
546 * cgroup_lock - lock out any changes to cgroup structures
549 void cgroup_lock(void)
551 mutex_lock(&cgroup_mutex);
555 * cgroup_unlock - release lock on cgroup changes
557 * Undo the lock taken in a previous cgroup_lock() call.
559 void cgroup_unlock(void)
561 mutex_unlock(&cgroup_mutex);
565 * A couple of forward declarations required, due to cyclic reference loop:
566 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
567 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
568 * -> cgroup_mkdir.
571 static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode);
572 static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
573 static int cgroup_populate_dir(struct cgroup *cgrp);
574 static struct inode_operations cgroup_dir_inode_operations;
575 static struct file_operations proc_cgroupstats_operations;
577 static struct backing_dev_info cgroup_backing_dev_info = {
578 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
581 static struct inode *cgroup_new_inode(mode_t mode, struct super_block *sb)
583 struct inode *inode = new_inode(sb);
585 if (inode) {
586 inode->i_mode = mode;
587 inode->i_uid = current->fsuid;
588 inode->i_gid = current->fsgid;
589 inode->i_blocks = 0;
590 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
591 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
593 return inode;
597 * Call subsys's pre_destroy handler.
598 * This is called before css refcnt check.
600 static void cgroup_call_pre_destroy(struct cgroup *cgrp)
602 struct cgroup_subsys *ss;
603 for_each_subsys(cgrp->root, ss)
604 if (ss->pre_destroy && cgrp->subsys[ss->subsys_id])
605 ss->pre_destroy(ss, cgrp);
606 return;
609 static void cgroup_diput(struct dentry *dentry, struct inode *inode)
611 /* is dentry a directory ? if so, kfree() associated cgroup */
612 if (S_ISDIR(inode->i_mode)) {
613 struct cgroup *cgrp = dentry->d_fsdata;
614 struct cgroup_subsys *ss;
615 BUG_ON(!(cgroup_is_removed(cgrp)));
616 /* It's possible for external users to be holding css
617 * reference counts on a cgroup; css_put() needs to
618 * be able to access the cgroup after decrementing
619 * the reference count in order to know if it needs to
620 * queue the cgroup to be handled by the release
621 * agent */
622 synchronize_rcu();
624 mutex_lock(&cgroup_mutex);
626 * Release the subsystem state objects.
628 for_each_subsys(cgrp->root, ss) {
629 if (cgrp->subsys[ss->subsys_id])
630 ss->destroy(ss, cgrp);
633 cgrp->root->number_of_cgroups--;
634 mutex_unlock(&cgroup_mutex);
636 /* Drop the active superblock reference that we took when we
637 * created the cgroup */
638 deactivate_super(cgrp->root->sb);
640 kfree(cgrp);
642 iput(inode);
645 static void remove_dir(struct dentry *d)
647 struct dentry *parent = dget(d->d_parent);
649 d_delete(d);
650 simple_rmdir(parent->d_inode, d);
651 dput(parent);
654 static void cgroup_clear_directory(struct dentry *dentry)
656 struct list_head *node;
658 BUG_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
659 spin_lock(&dcache_lock);
660 node = dentry->d_subdirs.next;
661 while (node != &dentry->d_subdirs) {
662 struct dentry *d = list_entry(node, struct dentry, d_u.d_child);
663 list_del_init(node);
664 if (d->d_inode) {
665 /* This should never be called on a cgroup
666 * directory with child cgroups */
667 BUG_ON(d->d_inode->i_mode & S_IFDIR);
668 d = dget_locked(d);
669 spin_unlock(&dcache_lock);
670 d_delete(d);
671 simple_unlink(dentry->d_inode, d);
672 dput(d);
673 spin_lock(&dcache_lock);
675 node = dentry->d_subdirs.next;
677 spin_unlock(&dcache_lock);
681 * NOTE : the dentry must have been dget()'ed
683 static void cgroup_d_remove_dir(struct dentry *dentry)
685 cgroup_clear_directory(dentry);
687 spin_lock(&dcache_lock);
688 list_del_init(&dentry->d_u.d_child);
689 spin_unlock(&dcache_lock);
690 remove_dir(dentry);
693 static int rebind_subsystems(struct cgroupfs_root *root,
694 unsigned long final_bits)
696 unsigned long added_bits, removed_bits;
697 struct cgroup *cgrp = &root->top_cgroup;
698 int i;
700 removed_bits = root->actual_subsys_bits & ~final_bits;
701 added_bits = final_bits & ~root->actual_subsys_bits;
702 /* Check that any added subsystems are currently free */
703 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
704 unsigned long bit = 1UL << i;
705 struct cgroup_subsys *ss = subsys[i];
706 if (!(bit & added_bits))
707 continue;
708 if (ss->root != &rootnode) {
709 /* Subsystem isn't free */
710 return -EBUSY;
714 /* Currently we don't handle adding/removing subsystems when
715 * any child cgroups exist. This is theoretically supportable
716 * but involves complex error handling, so it's being left until
717 * later */
718 if (!list_empty(&cgrp->children))
719 return -EBUSY;
721 /* Process each subsystem */
722 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
723 struct cgroup_subsys *ss = subsys[i];
724 unsigned long bit = 1UL << i;
725 if (bit & added_bits) {
726 /* We're binding this subsystem to this hierarchy */
727 BUG_ON(cgrp->subsys[i]);
728 BUG_ON(!dummytop->subsys[i]);
729 BUG_ON(dummytop->subsys[i]->cgroup != dummytop);
730 cgrp->subsys[i] = dummytop->subsys[i];
731 cgrp->subsys[i]->cgroup = cgrp;
732 list_add(&ss->sibling, &root->subsys_list);
733 rcu_assign_pointer(ss->root, root);
734 if (ss->bind)
735 ss->bind(ss, cgrp);
737 } else if (bit & removed_bits) {
738 /* We're removing this subsystem */
739 BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]);
740 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
741 if (ss->bind)
742 ss->bind(ss, dummytop);
743 dummytop->subsys[i]->cgroup = dummytop;
744 cgrp->subsys[i] = NULL;
745 rcu_assign_pointer(subsys[i]->root, &rootnode);
746 list_del(&ss->sibling);
747 } else if (bit & final_bits) {
748 /* Subsystem state should already exist */
749 BUG_ON(!cgrp->subsys[i]);
750 } else {
751 /* Subsystem state shouldn't exist */
752 BUG_ON(cgrp->subsys[i]);
755 root->subsys_bits = root->actual_subsys_bits = final_bits;
756 synchronize_rcu();
758 return 0;
761 static int cgroup_show_options(struct seq_file *seq, struct vfsmount *vfs)
763 struct cgroupfs_root *root = vfs->mnt_sb->s_fs_info;
764 struct cgroup_subsys *ss;
766 mutex_lock(&cgroup_mutex);
767 for_each_subsys(root, ss)
768 seq_printf(seq, ",%s", ss->name);
769 if (test_bit(ROOT_NOPREFIX, &root->flags))
770 seq_puts(seq, ",noprefix");
771 if (strlen(root->release_agent_path))
772 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
773 mutex_unlock(&cgroup_mutex);
774 return 0;
777 struct cgroup_sb_opts {
778 unsigned long subsys_bits;
779 unsigned long flags;
780 char *release_agent;
783 /* Convert a hierarchy specifier into a bitmask of subsystems and
784 * flags. */
785 static int parse_cgroupfs_options(char *data,
786 struct cgroup_sb_opts *opts)
788 char *token, *o = data ?: "all";
790 opts->subsys_bits = 0;
791 opts->flags = 0;
792 opts->release_agent = NULL;
794 while ((token = strsep(&o, ",")) != NULL) {
795 if (!*token)
796 return -EINVAL;
797 if (!strcmp(token, "all")) {
798 /* Add all non-disabled subsystems */
799 int i;
800 opts->subsys_bits = 0;
801 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
802 struct cgroup_subsys *ss = subsys[i];
803 if (!ss->disabled)
804 opts->subsys_bits |= 1ul << i;
806 } else if (!strcmp(token, "noprefix")) {
807 set_bit(ROOT_NOPREFIX, &opts->flags);
808 } else if (!strncmp(token, "release_agent=", 14)) {
809 /* Specifying two release agents is forbidden */
810 if (opts->release_agent)
811 return -EINVAL;
812 opts->release_agent = kzalloc(PATH_MAX, GFP_KERNEL);
813 if (!opts->release_agent)
814 return -ENOMEM;
815 strncpy(opts->release_agent, token + 14, PATH_MAX - 1);
816 opts->release_agent[PATH_MAX - 1] = 0;
817 } else {
818 struct cgroup_subsys *ss;
819 int i;
820 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
821 ss = subsys[i];
822 if (!strcmp(token, ss->name)) {
823 if (!ss->disabled)
824 set_bit(i, &opts->subsys_bits);
825 break;
828 if (i == CGROUP_SUBSYS_COUNT)
829 return -ENOENT;
833 /* We can't have an empty hierarchy */
834 if (!opts->subsys_bits)
835 return -EINVAL;
837 return 0;
840 static int cgroup_remount(struct super_block *sb, int *flags, char *data)
842 int ret = 0;
843 struct cgroupfs_root *root = sb->s_fs_info;
844 struct cgroup *cgrp = &root->top_cgroup;
845 struct cgroup_sb_opts opts;
847 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
848 mutex_lock(&cgroup_mutex);
850 /* See what subsystems are wanted */
851 ret = parse_cgroupfs_options(data, &opts);
852 if (ret)
853 goto out_unlock;
855 /* Don't allow flags to change at remount */
856 if (opts.flags != root->flags) {
857 ret = -EINVAL;
858 goto out_unlock;
861 ret = rebind_subsystems(root, opts.subsys_bits);
863 /* (re)populate subsystem files */
864 if (!ret)
865 cgroup_populate_dir(cgrp);
867 if (opts.release_agent)
868 strcpy(root->release_agent_path, opts.release_agent);
869 out_unlock:
870 if (opts.release_agent)
871 kfree(opts.release_agent);
872 mutex_unlock(&cgroup_mutex);
873 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
874 return ret;
877 static struct super_operations cgroup_ops = {
878 .statfs = simple_statfs,
879 .drop_inode = generic_delete_inode,
880 .show_options = cgroup_show_options,
881 .remount_fs = cgroup_remount,
884 static void init_cgroup_root(struct cgroupfs_root *root)
886 struct cgroup *cgrp = &root->top_cgroup;
887 INIT_LIST_HEAD(&root->subsys_list);
888 INIT_LIST_HEAD(&root->root_list);
889 root->number_of_cgroups = 1;
890 cgrp->root = root;
891 cgrp->top_cgroup = cgrp;
892 INIT_LIST_HEAD(&cgrp->sibling);
893 INIT_LIST_HEAD(&cgrp->children);
894 INIT_LIST_HEAD(&cgrp->css_sets);
895 INIT_LIST_HEAD(&cgrp->release_list);
898 static int cgroup_test_super(struct super_block *sb, void *data)
900 struct cgroupfs_root *new = data;
901 struct cgroupfs_root *root = sb->s_fs_info;
903 /* First check subsystems */
904 if (new->subsys_bits != root->subsys_bits)
905 return 0;
907 /* Next check flags */
908 if (new->flags != root->flags)
909 return 0;
911 return 1;
914 static int cgroup_set_super(struct super_block *sb, void *data)
916 int ret;
917 struct cgroupfs_root *root = data;
919 ret = set_anon_super(sb, NULL);
920 if (ret)
921 return ret;
923 sb->s_fs_info = root;
924 root->sb = sb;
926 sb->s_blocksize = PAGE_CACHE_SIZE;
927 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
928 sb->s_magic = CGROUP_SUPER_MAGIC;
929 sb->s_op = &cgroup_ops;
931 return 0;
934 static int cgroup_get_rootdir(struct super_block *sb)
936 struct inode *inode =
937 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
938 struct dentry *dentry;
940 if (!inode)
941 return -ENOMEM;
943 inode->i_fop = &simple_dir_operations;
944 inode->i_op = &cgroup_dir_inode_operations;
945 /* directories start off with i_nlink == 2 (for "." entry) */
946 inc_nlink(inode);
947 dentry = d_alloc_root(inode);
948 if (!dentry) {
949 iput(inode);
950 return -ENOMEM;
952 sb->s_root = dentry;
953 return 0;
956 static int cgroup_get_sb(struct file_system_type *fs_type,
957 int flags, const char *unused_dev_name,
958 void *data, struct vfsmount *mnt)
960 struct cgroup_sb_opts opts;
961 int ret = 0;
962 struct super_block *sb;
963 struct cgroupfs_root *root;
964 struct list_head tmp_cg_links;
965 INIT_LIST_HEAD(&tmp_cg_links);
967 /* First find the desired set of subsystems */
968 ret = parse_cgroupfs_options(data, &opts);
969 if (ret) {
970 if (opts.release_agent)
971 kfree(opts.release_agent);
972 return ret;
975 root = kzalloc(sizeof(*root), GFP_KERNEL);
976 if (!root) {
977 if (opts.release_agent)
978 kfree(opts.release_agent);
979 return -ENOMEM;
982 init_cgroup_root(root);
983 root->subsys_bits = opts.subsys_bits;
984 root->flags = opts.flags;
985 if (opts.release_agent) {
986 strcpy(root->release_agent_path, opts.release_agent);
987 kfree(opts.release_agent);
990 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, root);
992 if (IS_ERR(sb)) {
993 kfree(root);
994 return PTR_ERR(sb);
997 if (sb->s_fs_info != root) {
998 /* Reusing an existing superblock */
999 BUG_ON(sb->s_root == NULL);
1000 kfree(root);
1001 root = NULL;
1002 } else {
1003 /* New superblock */
1004 struct cgroup *cgrp = &root->top_cgroup;
1005 struct inode *inode;
1006 int i;
1008 BUG_ON(sb->s_root != NULL);
1010 ret = cgroup_get_rootdir(sb);
1011 if (ret)
1012 goto drop_new_super;
1013 inode = sb->s_root->d_inode;
1015 mutex_lock(&inode->i_mutex);
1016 mutex_lock(&cgroup_mutex);
1019 * We're accessing css_set_count without locking
1020 * css_set_lock here, but that's OK - it can only be
1021 * increased by someone holding cgroup_lock, and
1022 * that's us. The worst that can happen is that we
1023 * have some link structures left over
1025 ret = allocate_cg_links(css_set_count, &tmp_cg_links);
1026 if (ret) {
1027 mutex_unlock(&cgroup_mutex);
1028 mutex_unlock(&inode->i_mutex);
1029 goto drop_new_super;
1032 ret = rebind_subsystems(root, root->subsys_bits);
1033 if (ret == -EBUSY) {
1034 mutex_unlock(&cgroup_mutex);
1035 mutex_unlock(&inode->i_mutex);
1036 goto drop_new_super;
1039 /* EBUSY should be the only error here */
1040 BUG_ON(ret);
1042 list_add(&root->root_list, &roots);
1043 root_count++;
1045 sb->s_root->d_fsdata = &root->top_cgroup;
1046 root->top_cgroup.dentry = sb->s_root;
1048 /* Link the top cgroup in this hierarchy into all
1049 * the css_set objects */
1050 write_lock(&css_set_lock);
1051 for (i = 0; i < CSS_SET_TABLE_SIZE; i++) {
1052 struct hlist_head *hhead = &css_set_table[i];
1053 struct hlist_node *node;
1054 struct css_set *cg;
1056 hlist_for_each_entry(cg, node, hhead, hlist) {
1057 struct cg_cgroup_link *link;
1059 BUG_ON(list_empty(&tmp_cg_links));
1060 link = list_entry(tmp_cg_links.next,
1061 struct cg_cgroup_link,
1062 cgrp_link_list);
1063 list_del(&link->cgrp_link_list);
1064 link->cg = cg;
1065 list_add(&link->cgrp_link_list,
1066 &root->top_cgroup.css_sets);
1067 list_add(&link->cg_link_list, &cg->cg_links);
1070 write_unlock(&css_set_lock);
1072 free_cg_links(&tmp_cg_links);
1074 BUG_ON(!list_empty(&cgrp->sibling));
1075 BUG_ON(!list_empty(&cgrp->children));
1076 BUG_ON(root->number_of_cgroups != 1);
1078 cgroup_populate_dir(cgrp);
1079 mutex_unlock(&inode->i_mutex);
1080 mutex_unlock(&cgroup_mutex);
1083 return simple_set_mnt(mnt, sb);
1085 drop_new_super:
1086 up_write(&sb->s_umount);
1087 deactivate_super(sb);
1088 free_cg_links(&tmp_cg_links);
1089 return ret;
1092 static void cgroup_kill_sb(struct super_block *sb) {
1093 struct cgroupfs_root *root = sb->s_fs_info;
1094 struct cgroup *cgrp = &root->top_cgroup;
1095 int ret;
1097 BUG_ON(!root);
1099 BUG_ON(root->number_of_cgroups != 1);
1100 BUG_ON(!list_empty(&cgrp->children));
1101 BUG_ON(!list_empty(&cgrp->sibling));
1103 mutex_lock(&cgroup_mutex);
1105 /* Rebind all subsystems back to the default hierarchy */
1106 ret = rebind_subsystems(root, 0);
1107 /* Shouldn't be able to fail ... */
1108 BUG_ON(ret);
1111 * Release all the links from css_sets to this hierarchy's
1112 * root cgroup
1114 write_lock(&css_set_lock);
1115 while (!list_empty(&cgrp->css_sets)) {
1116 struct cg_cgroup_link *link;
1117 link = list_entry(cgrp->css_sets.next,
1118 struct cg_cgroup_link, cgrp_link_list);
1119 list_del(&link->cg_link_list);
1120 list_del(&link->cgrp_link_list);
1121 kfree(link);
1123 write_unlock(&css_set_lock);
1125 if (!list_empty(&root->root_list)) {
1126 list_del(&root->root_list);
1127 root_count--;
1129 mutex_unlock(&cgroup_mutex);
1131 kfree(root);
1132 kill_litter_super(sb);
1135 static struct file_system_type cgroup_fs_type = {
1136 .name = "cgroup",
1137 .get_sb = cgroup_get_sb,
1138 .kill_sb = cgroup_kill_sb,
1141 static inline struct cgroup *__d_cgrp(struct dentry *dentry)
1143 return dentry->d_fsdata;
1146 static inline struct cftype *__d_cft(struct dentry *dentry)
1148 return dentry->d_fsdata;
1152 * cgroup_path - generate the path of a cgroup
1153 * @cgrp: the cgroup in question
1154 * @buf: the buffer to write the path into
1155 * @buflen: the length of the buffer
1157 * Called with cgroup_mutex held. Writes path of cgroup into buf.
1158 * Returns 0 on success, -errno on error.
1160 int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
1162 char *start;
1164 if (cgrp == dummytop) {
1166 * Inactive subsystems have no dentry for their root
1167 * cgroup
1169 strcpy(buf, "/");
1170 return 0;
1173 start = buf + buflen;
1175 *--start = '\0';
1176 for (;;) {
1177 int len = cgrp->dentry->d_name.len;
1178 if ((start -= len) < buf)
1179 return -ENAMETOOLONG;
1180 memcpy(start, cgrp->dentry->d_name.name, len);
1181 cgrp = cgrp->parent;
1182 if (!cgrp)
1183 break;
1184 if (!cgrp->parent)
1185 continue;
1186 if (--start < buf)
1187 return -ENAMETOOLONG;
1188 *start = '/';
1190 memmove(buf, start, buf + buflen - start);
1191 return 0;
1195 * Return the first subsystem attached to a cgroup's hierarchy, and
1196 * its subsystem id.
1199 static void get_first_subsys(const struct cgroup *cgrp,
1200 struct cgroup_subsys_state **css, int *subsys_id)
1202 const struct cgroupfs_root *root = cgrp->root;
1203 const struct cgroup_subsys *test_ss;
1204 BUG_ON(list_empty(&root->subsys_list));
1205 test_ss = list_entry(root->subsys_list.next,
1206 struct cgroup_subsys, sibling);
1207 if (css) {
1208 *css = cgrp->subsys[test_ss->subsys_id];
1209 BUG_ON(!*css);
1211 if (subsys_id)
1212 *subsys_id = test_ss->subsys_id;
1216 * cgroup_attach_task - attach task 'tsk' to cgroup 'cgrp'
1217 * @cgrp: the cgroup the task is attaching to
1218 * @tsk: the task to be attached
1220 * Call holding cgroup_mutex. May take task_lock of
1221 * the task 'tsk' during call.
1223 int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
1225 int retval = 0;
1226 struct cgroup_subsys *ss;
1227 struct cgroup *oldcgrp;
1228 struct css_set *cg = tsk->cgroups;
1229 struct css_set *newcg;
1230 struct cgroupfs_root *root = cgrp->root;
1231 int subsys_id;
1233 get_first_subsys(cgrp, NULL, &subsys_id);
1235 /* Nothing to do if the task is already in that cgroup */
1236 oldcgrp = task_cgroup(tsk, subsys_id);
1237 if (cgrp == oldcgrp)
1238 return 0;
1240 for_each_subsys(root, ss) {
1241 if (ss->can_attach) {
1242 retval = ss->can_attach(ss, cgrp, tsk);
1243 if (retval)
1244 return retval;
1249 * Locate or allocate a new css_set for this task,
1250 * based on its final set of cgroups
1252 newcg = find_css_set(cg, cgrp);
1253 if (!newcg)
1254 return -ENOMEM;
1256 task_lock(tsk);
1257 if (tsk->flags & PF_EXITING) {
1258 task_unlock(tsk);
1259 put_css_set(newcg);
1260 return -ESRCH;
1262 rcu_assign_pointer(tsk->cgroups, newcg);
1263 task_unlock(tsk);
1265 /* Update the css_set linked lists if we're using them */
1266 write_lock(&css_set_lock);
1267 if (!list_empty(&tsk->cg_list)) {
1268 list_del(&tsk->cg_list);
1269 list_add(&tsk->cg_list, &newcg->tasks);
1271 write_unlock(&css_set_lock);
1273 for_each_subsys(root, ss) {
1274 if (ss->attach)
1275 ss->attach(ss, cgrp, oldcgrp, tsk);
1277 set_bit(CGRP_RELEASABLE, &oldcgrp->flags);
1278 synchronize_rcu();
1279 put_css_set(cg);
1280 return 0;
1284 * Attach task with pid 'pid' to cgroup 'cgrp'. Call with
1285 * cgroup_mutex, may take task_lock of task
1287 static int attach_task_by_pid(struct cgroup *cgrp, char *pidbuf)
1289 pid_t pid;
1290 struct task_struct *tsk;
1291 int ret;
1293 if (sscanf(pidbuf, "%d", &pid) != 1)
1294 return -EIO;
1296 if (pid) {
1297 rcu_read_lock();
1298 tsk = find_task_by_vpid(pid);
1299 if (!tsk || tsk->flags & PF_EXITING) {
1300 rcu_read_unlock();
1301 return -ESRCH;
1303 get_task_struct(tsk);
1304 rcu_read_unlock();
1306 if ((current->euid) && (current->euid != tsk->uid)
1307 && (current->euid != tsk->suid)) {
1308 put_task_struct(tsk);
1309 return -EACCES;
1311 } else {
1312 tsk = current;
1313 get_task_struct(tsk);
1316 ret = cgroup_attach_task(cgrp, tsk);
1317 put_task_struct(tsk);
1318 return ret;
1321 /* The various types of files and directories in a cgroup file system */
1322 enum cgroup_filetype {
1323 FILE_ROOT,
1324 FILE_DIR,
1325 FILE_TASKLIST,
1326 FILE_NOTIFY_ON_RELEASE,
1327 FILE_RELEASE_AGENT,
1330 static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
1331 struct file *file,
1332 const char __user *userbuf,
1333 size_t nbytes, loff_t *unused_ppos)
1335 char buffer[64];
1336 int retval = 0;
1337 char *end;
1339 if (!nbytes)
1340 return -EINVAL;
1341 if (nbytes >= sizeof(buffer))
1342 return -E2BIG;
1343 if (copy_from_user(buffer, userbuf, nbytes))
1344 return -EFAULT;
1346 buffer[nbytes] = 0; /* nul-terminate */
1347 strstrip(buffer);
1348 if (cft->write_u64) {
1349 u64 val = simple_strtoull(buffer, &end, 0);
1350 if (*end)
1351 return -EINVAL;
1352 retval = cft->write_u64(cgrp, cft, val);
1353 } else {
1354 s64 val = simple_strtoll(buffer, &end, 0);
1355 if (*end)
1356 return -EINVAL;
1357 retval = cft->write_s64(cgrp, cft, val);
1359 if (!retval)
1360 retval = nbytes;
1361 return retval;
1364 static ssize_t cgroup_common_file_write(struct cgroup *cgrp,
1365 struct cftype *cft,
1366 struct file *file,
1367 const char __user *userbuf,
1368 size_t nbytes, loff_t *unused_ppos)
1370 enum cgroup_filetype type = cft->private;
1371 char *buffer;
1372 int retval = 0;
1374 if (nbytes >= PATH_MAX)
1375 return -E2BIG;
1377 /* +1 for nul-terminator */
1378 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
1379 if (buffer == NULL)
1380 return -ENOMEM;
1382 if (copy_from_user(buffer, userbuf, nbytes)) {
1383 retval = -EFAULT;
1384 goto out1;
1386 buffer[nbytes] = 0; /* nul-terminate */
1387 strstrip(buffer); /* strip -just- trailing whitespace */
1389 mutex_lock(&cgroup_mutex);
1392 * This was already checked for in cgroup_file_write(), but
1393 * check again now we're holding cgroup_mutex.
1395 if (cgroup_is_removed(cgrp)) {
1396 retval = -ENODEV;
1397 goto out2;
1400 switch (type) {
1401 case FILE_TASKLIST:
1402 retval = attach_task_by_pid(cgrp, buffer);
1403 break;
1404 case FILE_NOTIFY_ON_RELEASE:
1405 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
1406 if (simple_strtoul(buffer, NULL, 10) != 0)
1407 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
1408 else
1409 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
1410 break;
1411 case FILE_RELEASE_AGENT:
1412 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
1413 strcpy(cgrp->root->release_agent_path, buffer);
1414 break;
1415 default:
1416 retval = -EINVAL;
1417 goto out2;
1420 if (retval == 0)
1421 retval = nbytes;
1422 out2:
1423 mutex_unlock(&cgroup_mutex);
1424 out1:
1425 kfree(buffer);
1426 return retval;
1429 static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
1430 size_t nbytes, loff_t *ppos)
1432 struct cftype *cft = __d_cft(file->f_dentry);
1433 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
1435 if (!cft || cgroup_is_removed(cgrp))
1436 return -ENODEV;
1437 if (cft->write)
1438 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
1439 if (cft->write_u64 || cft->write_s64)
1440 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
1441 if (cft->trigger) {
1442 int ret = cft->trigger(cgrp, (unsigned int)cft->private);
1443 return ret ? ret : nbytes;
1445 return -EINVAL;
1448 static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
1449 struct file *file,
1450 char __user *buf, size_t nbytes,
1451 loff_t *ppos)
1453 char tmp[64];
1454 u64 val = cft->read_u64(cgrp, cft);
1455 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
1457 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
1460 static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
1461 struct file *file,
1462 char __user *buf, size_t nbytes,
1463 loff_t *ppos)
1465 char tmp[64];
1466 s64 val = cft->read_s64(cgrp, cft);
1467 int len = sprintf(tmp, "%lld\n", (long long) val);
1469 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
1472 static ssize_t cgroup_common_file_read(struct cgroup *cgrp,
1473 struct cftype *cft,
1474 struct file *file,
1475 char __user *buf,
1476 size_t nbytes, loff_t *ppos)
1478 enum cgroup_filetype type = cft->private;
1479 char *page;
1480 ssize_t retval = 0;
1481 char *s;
1483 if (!(page = (char *)__get_free_page(GFP_KERNEL)))
1484 return -ENOMEM;
1486 s = page;
1488 switch (type) {
1489 case FILE_RELEASE_AGENT:
1491 struct cgroupfs_root *root;
1492 size_t n;
1493 mutex_lock(&cgroup_mutex);
1494 root = cgrp->root;
1495 n = strnlen(root->release_agent_path,
1496 sizeof(root->release_agent_path));
1497 n = min(n, (size_t) PAGE_SIZE);
1498 strncpy(s, root->release_agent_path, n);
1499 mutex_unlock(&cgroup_mutex);
1500 s += n;
1501 break;
1503 default:
1504 retval = -EINVAL;
1505 goto out;
1507 *s++ = '\n';
1509 retval = simple_read_from_buffer(buf, nbytes, ppos, page, s - page);
1510 out:
1511 free_page((unsigned long)page);
1512 return retval;
1515 static ssize_t cgroup_file_read(struct file *file, char __user *buf,
1516 size_t nbytes, loff_t *ppos)
1518 struct cftype *cft = __d_cft(file->f_dentry);
1519 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
1521 if (!cft || cgroup_is_removed(cgrp))
1522 return -ENODEV;
1524 if (cft->read)
1525 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
1526 if (cft->read_u64)
1527 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
1528 if (cft->read_s64)
1529 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
1530 return -EINVAL;
1534 * seqfile ops/methods for returning structured data. Currently just
1535 * supports string->u64 maps, but can be extended in future.
1538 struct cgroup_seqfile_state {
1539 struct cftype *cft;
1540 struct cgroup *cgroup;
1543 static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
1545 struct seq_file *sf = cb->state;
1546 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
1549 static int cgroup_seqfile_show(struct seq_file *m, void *arg)
1551 struct cgroup_seqfile_state *state = m->private;
1552 struct cftype *cft = state->cft;
1553 if (cft->read_map) {
1554 struct cgroup_map_cb cb = {
1555 .fill = cgroup_map_add,
1556 .state = m,
1558 return cft->read_map(state->cgroup, cft, &cb);
1560 return cft->read_seq_string(state->cgroup, cft, m);
1563 int cgroup_seqfile_release(struct inode *inode, struct file *file)
1565 struct seq_file *seq = file->private_data;
1566 kfree(seq->private);
1567 return single_release(inode, file);
1570 static struct file_operations cgroup_seqfile_operations = {
1571 .read = seq_read,
1572 .llseek = seq_lseek,
1573 .release = cgroup_seqfile_release,
1576 static int cgroup_file_open(struct inode *inode, struct file *file)
1578 int err;
1579 struct cftype *cft;
1581 err = generic_file_open(inode, file);
1582 if (err)
1583 return err;
1585 cft = __d_cft(file->f_dentry);
1586 if (!cft)
1587 return -ENODEV;
1588 if (cft->read_map || cft->read_seq_string) {
1589 struct cgroup_seqfile_state *state =
1590 kzalloc(sizeof(*state), GFP_USER);
1591 if (!state)
1592 return -ENOMEM;
1593 state->cft = cft;
1594 state->cgroup = __d_cgrp(file->f_dentry->d_parent);
1595 file->f_op = &cgroup_seqfile_operations;
1596 err = single_open(file, cgroup_seqfile_show, state);
1597 if (err < 0)
1598 kfree(state);
1599 } else if (cft->open)
1600 err = cft->open(inode, file);
1601 else
1602 err = 0;
1604 return err;
1607 static int cgroup_file_release(struct inode *inode, struct file *file)
1609 struct cftype *cft = __d_cft(file->f_dentry);
1610 if (cft->release)
1611 return cft->release(inode, file);
1612 return 0;
1616 * cgroup_rename - Only allow simple rename of directories in place.
1618 static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
1619 struct inode *new_dir, struct dentry *new_dentry)
1621 if (!S_ISDIR(old_dentry->d_inode->i_mode))
1622 return -ENOTDIR;
1623 if (new_dentry->d_inode)
1624 return -EEXIST;
1625 if (old_dir != new_dir)
1626 return -EIO;
1627 return simple_rename(old_dir, old_dentry, new_dir, new_dentry);
1630 static struct file_operations cgroup_file_operations = {
1631 .read = cgroup_file_read,
1632 .write = cgroup_file_write,
1633 .llseek = generic_file_llseek,
1634 .open = cgroup_file_open,
1635 .release = cgroup_file_release,
1638 static struct inode_operations cgroup_dir_inode_operations = {
1639 .lookup = simple_lookup,
1640 .mkdir = cgroup_mkdir,
1641 .rmdir = cgroup_rmdir,
1642 .rename = cgroup_rename,
1645 static int cgroup_create_file(struct dentry *dentry, int mode,
1646 struct super_block *sb)
1648 static struct dentry_operations cgroup_dops = {
1649 .d_iput = cgroup_diput,
1652 struct inode *inode;
1654 if (!dentry)
1655 return -ENOENT;
1656 if (dentry->d_inode)
1657 return -EEXIST;
1659 inode = cgroup_new_inode(mode, sb);
1660 if (!inode)
1661 return -ENOMEM;
1663 if (S_ISDIR(mode)) {
1664 inode->i_op = &cgroup_dir_inode_operations;
1665 inode->i_fop = &simple_dir_operations;
1667 /* start off with i_nlink == 2 (for "." entry) */
1668 inc_nlink(inode);
1670 /* start with the directory inode held, so that we can
1671 * populate it without racing with another mkdir */
1672 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
1673 } else if (S_ISREG(mode)) {
1674 inode->i_size = 0;
1675 inode->i_fop = &cgroup_file_operations;
1677 dentry->d_op = &cgroup_dops;
1678 d_instantiate(dentry, inode);
1679 dget(dentry); /* Extra count - pin the dentry in core */
1680 return 0;
1684 * cgroup_create_dir - create a directory for an object.
1685 * @cgrp: the cgroup we create the directory for. It must have a valid
1686 * ->parent field. And we are going to fill its ->dentry field.
1687 * @dentry: dentry of the new cgroup
1688 * @mode: mode to set on new directory.
1690 static int cgroup_create_dir(struct cgroup *cgrp, struct dentry *dentry,
1691 int mode)
1693 struct dentry *parent;
1694 int error = 0;
1696 parent = cgrp->parent->dentry;
1697 error = cgroup_create_file(dentry, S_IFDIR | mode, cgrp->root->sb);
1698 if (!error) {
1699 dentry->d_fsdata = cgrp;
1700 inc_nlink(parent->d_inode);
1701 cgrp->dentry = dentry;
1702 dget(dentry);
1704 dput(dentry);
1706 return error;
1709 int cgroup_add_file(struct cgroup *cgrp,
1710 struct cgroup_subsys *subsys,
1711 const struct cftype *cft)
1713 struct dentry *dir = cgrp->dentry;
1714 struct dentry *dentry;
1715 int error;
1717 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
1718 if (subsys && !test_bit(ROOT_NOPREFIX, &cgrp->root->flags)) {
1719 strcpy(name, subsys->name);
1720 strcat(name, ".");
1722 strcat(name, cft->name);
1723 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
1724 dentry = lookup_one_len(name, dir, strlen(name));
1725 if (!IS_ERR(dentry)) {
1726 error = cgroup_create_file(dentry, 0644 | S_IFREG,
1727 cgrp->root->sb);
1728 if (!error)
1729 dentry->d_fsdata = (void *)cft;
1730 dput(dentry);
1731 } else
1732 error = PTR_ERR(dentry);
1733 return error;
1736 int cgroup_add_files(struct cgroup *cgrp,
1737 struct cgroup_subsys *subsys,
1738 const struct cftype cft[],
1739 int count)
1741 int i, err;
1742 for (i = 0; i < count; i++) {
1743 err = cgroup_add_file(cgrp, subsys, &cft[i]);
1744 if (err)
1745 return err;
1747 return 0;
1751 * cgroup_task_count - count the number of tasks in a cgroup.
1752 * @cgrp: the cgroup in question
1754 * Return the number of tasks in the cgroup.
1756 int cgroup_task_count(const struct cgroup *cgrp)
1758 int count = 0;
1759 struct list_head *l;
1761 read_lock(&css_set_lock);
1762 l = cgrp->css_sets.next;
1763 while (l != &cgrp->css_sets) {
1764 struct cg_cgroup_link *link =
1765 list_entry(l, struct cg_cgroup_link, cgrp_link_list);
1766 count += atomic_read(&link->cg->ref.refcount);
1767 l = l->next;
1769 read_unlock(&css_set_lock);
1770 return count;
1774 * Advance a list_head iterator. The iterator should be positioned at
1775 * the start of a css_set
1777 static void cgroup_advance_iter(struct cgroup *cgrp,
1778 struct cgroup_iter *it)
1780 struct list_head *l = it->cg_link;
1781 struct cg_cgroup_link *link;
1782 struct css_set *cg;
1784 /* Advance to the next non-empty css_set */
1785 do {
1786 l = l->next;
1787 if (l == &cgrp->css_sets) {
1788 it->cg_link = NULL;
1789 return;
1791 link = list_entry(l, struct cg_cgroup_link, cgrp_link_list);
1792 cg = link->cg;
1793 } while (list_empty(&cg->tasks));
1794 it->cg_link = l;
1795 it->task = cg->tasks.next;
1799 * To reduce the fork() overhead for systems that are not actually
1800 * using their cgroups capability, we don't maintain the lists running
1801 * through each css_set to its tasks until we see the list actually
1802 * used - in other words after the first call to cgroup_iter_start().
1804 * The tasklist_lock is not held here, as do_each_thread() and
1805 * while_each_thread() are protected by RCU.
1807 static void cgroup_enable_task_cg_lists(void)
1809 struct task_struct *p, *g;
1810 write_lock(&css_set_lock);
1811 use_task_css_set_links = 1;
1812 do_each_thread(g, p) {
1813 task_lock(p);
1815 * We should check if the process is exiting, otherwise
1816 * it will race with cgroup_exit() in that the list
1817 * entry won't be deleted though the process has exited.
1819 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
1820 list_add(&p->cg_list, &p->cgroups->tasks);
1821 task_unlock(p);
1822 } while_each_thread(g, p);
1823 write_unlock(&css_set_lock);
1826 void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
1829 * The first time anyone tries to iterate across a cgroup,
1830 * we need to enable the list linking each css_set to its
1831 * tasks, and fix up all existing tasks.
1833 if (!use_task_css_set_links)
1834 cgroup_enable_task_cg_lists();
1836 read_lock(&css_set_lock);
1837 it->cg_link = &cgrp->css_sets;
1838 cgroup_advance_iter(cgrp, it);
1841 struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
1842 struct cgroup_iter *it)
1844 struct task_struct *res;
1845 struct list_head *l = it->task;
1847 /* If the iterator cg is NULL, we have no tasks */
1848 if (!it->cg_link)
1849 return NULL;
1850 res = list_entry(l, struct task_struct, cg_list);
1851 /* Advance iterator to find next entry */
1852 l = l->next;
1853 if (l == &res->cgroups->tasks) {
1854 /* We reached the end of this task list - move on to
1855 * the next cg_cgroup_link */
1856 cgroup_advance_iter(cgrp, it);
1857 } else {
1858 it->task = l;
1860 return res;
1863 void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
1865 read_unlock(&css_set_lock);
1868 static inline int started_after_time(struct task_struct *t1,
1869 struct timespec *time,
1870 struct task_struct *t2)
1872 int start_diff = timespec_compare(&t1->start_time, time);
1873 if (start_diff > 0) {
1874 return 1;
1875 } else if (start_diff < 0) {
1876 return 0;
1877 } else {
1879 * Arbitrarily, if two processes started at the same
1880 * time, we'll say that the lower pointer value
1881 * started first. Note that t2 may have exited by now
1882 * so this may not be a valid pointer any longer, but
1883 * that's fine - it still serves to distinguish
1884 * between two tasks started (effectively) simultaneously.
1886 return t1 > t2;
1891 * This function is a callback from heap_insert() and is used to order
1892 * the heap.
1893 * In this case we order the heap in descending task start time.
1895 static inline int started_after(void *p1, void *p2)
1897 struct task_struct *t1 = p1;
1898 struct task_struct *t2 = p2;
1899 return started_after_time(t1, &t2->start_time, t2);
1903 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
1904 * @scan: struct cgroup_scanner containing arguments for the scan
1906 * Arguments include pointers to callback functions test_task() and
1907 * process_task().
1908 * Iterate through all the tasks in a cgroup, calling test_task() for each,
1909 * and if it returns true, call process_task() for it also.
1910 * The test_task pointer may be NULL, meaning always true (select all tasks).
1911 * Effectively duplicates cgroup_iter_{start,next,end}()
1912 * but does not lock css_set_lock for the call to process_task().
1913 * The struct cgroup_scanner may be embedded in any structure of the caller's
1914 * creation.
1915 * It is guaranteed that process_task() will act on every task that
1916 * is a member of the cgroup for the duration of this call. This
1917 * function may or may not call process_task() for tasks that exit
1918 * or move to a different cgroup during the call, or are forked or
1919 * move into the cgroup during the call.
1921 * Note that test_task() may be called with locks held, and may in some
1922 * situations be called multiple times for the same task, so it should
1923 * be cheap.
1924 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
1925 * pre-allocated and will be used for heap operations (and its "gt" member will
1926 * be overwritten), else a temporary heap will be used (allocation of which
1927 * may cause this function to fail).
1929 int cgroup_scan_tasks(struct cgroup_scanner *scan)
1931 int retval, i;
1932 struct cgroup_iter it;
1933 struct task_struct *p, *dropped;
1934 /* Never dereference latest_task, since it's not refcounted */
1935 struct task_struct *latest_task = NULL;
1936 struct ptr_heap tmp_heap;
1937 struct ptr_heap *heap;
1938 struct timespec latest_time = { 0, 0 };
1940 if (scan->heap) {
1941 /* The caller supplied our heap and pre-allocated its memory */
1942 heap = scan->heap;
1943 heap->gt = &started_after;
1944 } else {
1945 /* We need to allocate our own heap memory */
1946 heap = &tmp_heap;
1947 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
1948 if (retval)
1949 /* cannot allocate the heap */
1950 return retval;
1953 again:
1955 * Scan tasks in the cgroup, using the scanner's "test_task" callback
1956 * to determine which are of interest, and using the scanner's
1957 * "process_task" callback to process any of them that need an update.
1958 * Since we don't want to hold any locks during the task updates,
1959 * gather tasks to be processed in a heap structure.
1960 * The heap is sorted by descending task start time.
1961 * If the statically-sized heap fills up, we overflow tasks that
1962 * started later, and in future iterations only consider tasks that
1963 * started after the latest task in the previous pass. This
1964 * guarantees forward progress and that we don't miss any tasks.
1966 heap->size = 0;
1967 cgroup_iter_start(scan->cg, &it);
1968 while ((p = cgroup_iter_next(scan->cg, &it))) {
1970 * Only affect tasks that qualify per the caller's callback,
1971 * if he provided one
1973 if (scan->test_task && !scan->test_task(p, scan))
1974 continue;
1976 * Only process tasks that started after the last task
1977 * we processed
1979 if (!started_after_time(p, &latest_time, latest_task))
1980 continue;
1981 dropped = heap_insert(heap, p);
1982 if (dropped == NULL) {
1984 * The new task was inserted; the heap wasn't
1985 * previously full
1987 get_task_struct(p);
1988 } else if (dropped != p) {
1990 * The new task was inserted, and pushed out a
1991 * different task
1993 get_task_struct(p);
1994 put_task_struct(dropped);
1997 * Else the new task was newer than anything already in
1998 * the heap and wasn't inserted
2001 cgroup_iter_end(scan->cg, &it);
2003 if (heap->size) {
2004 for (i = 0; i < heap->size; i++) {
2005 struct task_struct *q = heap->ptrs[i];
2006 if (i == 0) {
2007 latest_time = q->start_time;
2008 latest_task = q;
2010 /* Process the task per the caller's callback */
2011 scan->process_task(q, scan);
2012 put_task_struct(q);
2015 * If we had to process any tasks at all, scan again
2016 * in case some of them were in the middle of forking
2017 * children that didn't get processed.
2018 * Not the most efficient way to do it, but it avoids
2019 * having to take callback_mutex in the fork path
2021 goto again;
2023 if (heap == &tmp_heap)
2024 heap_free(&tmp_heap);
2025 return 0;
2029 * Stuff for reading the 'tasks' file.
2031 * Reading this file can return large amounts of data if a cgroup has
2032 * *lots* of attached tasks. So it may need several calls to read(),
2033 * but we cannot guarantee that the information we produce is correct
2034 * unless we produce it entirely atomically.
2036 * Upon tasks file open(), a struct ctr_struct is allocated, that
2037 * will have a pointer to an array (also allocated here). The struct
2038 * ctr_struct * is stored in file->private_data. Its resources will
2039 * be freed by release() when the file is closed. The array is used
2040 * to sprintf the PIDs and then used by read().
2042 struct ctr_struct {
2043 char *buf;
2044 int bufsz;
2048 * Load into 'pidarray' up to 'npids' of the tasks using cgroup
2049 * 'cgrp'. Return actual number of pids loaded. No need to
2050 * task_lock(p) when reading out p->cgroup, since we're in an RCU
2051 * read section, so the css_set can't go away, and is
2052 * immutable after creation.
2054 static int pid_array_load(pid_t *pidarray, int npids, struct cgroup *cgrp)
2056 int n = 0;
2057 struct cgroup_iter it;
2058 struct task_struct *tsk;
2059 cgroup_iter_start(cgrp, &it);
2060 while ((tsk = cgroup_iter_next(cgrp, &it))) {
2061 if (unlikely(n == npids))
2062 break;
2063 pidarray[n++] = task_pid_vnr(tsk);
2065 cgroup_iter_end(cgrp, &it);
2066 return n;
2070 * cgroupstats_build - build and fill cgroupstats
2071 * @stats: cgroupstats to fill information into
2072 * @dentry: A dentry entry belonging to the cgroup for which stats have
2073 * been requested.
2075 * Build and fill cgroupstats so that taskstats can export it to user
2076 * space.
2078 int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
2080 int ret = -EINVAL;
2081 struct cgroup *cgrp;
2082 struct cgroup_iter it;
2083 struct task_struct *tsk;
2085 * Validate dentry by checking the superblock operations
2087 if (dentry->d_sb->s_op != &cgroup_ops)
2088 goto err;
2090 ret = 0;
2091 cgrp = dentry->d_fsdata;
2092 rcu_read_lock();
2094 cgroup_iter_start(cgrp, &it);
2095 while ((tsk = cgroup_iter_next(cgrp, &it))) {
2096 switch (tsk->state) {
2097 case TASK_RUNNING:
2098 stats->nr_running++;
2099 break;
2100 case TASK_INTERRUPTIBLE:
2101 stats->nr_sleeping++;
2102 break;
2103 case TASK_UNINTERRUPTIBLE:
2104 stats->nr_uninterruptible++;
2105 break;
2106 case TASK_STOPPED:
2107 stats->nr_stopped++;
2108 break;
2109 default:
2110 if (delayacct_is_task_waiting_on_io(tsk))
2111 stats->nr_io_wait++;
2112 break;
2115 cgroup_iter_end(cgrp, &it);
2117 rcu_read_unlock();
2118 err:
2119 return ret;
2122 static int cmppid(const void *a, const void *b)
2124 return *(pid_t *)a - *(pid_t *)b;
2128 * Convert array 'a' of 'npids' pid_t's to a string of newline separated
2129 * decimal pids in 'buf'. Don't write more than 'sz' chars, but return
2130 * count 'cnt' of how many chars would be written if buf were large enough.
2132 static int pid_array_to_buf(char *buf, int sz, pid_t *a, int npids)
2134 int cnt = 0;
2135 int i;
2137 for (i = 0; i < npids; i++)
2138 cnt += snprintf(buf + cnt, max(sz - cnt, 0), "%d\n", a[i]);
2139 return cnt;
2143 * Handle an open on 'tasks' file. Prepare a buffer listing the
2144 * process id's of tasks currently attached to the cgroup being opened.
2146 * Does not require any specific cgroup mutexes, and does not take any.
2148 static int cgroup_tasks_open(struct inode *unused, struct file *file)
2150 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
2151 struct ctr_struct *ctr;
2152 pid_t *pidarray;
2153 int npids;
2154 char c;
2156 if (!(file->f_mode & FMODE_READ))
2157 return 0;
2159 ctr = kmalloc(sizeof(*ctr), GFP_KERNEL);
2160 if (!ctr)
2161 goto err0;
2164 * If cgroup gets more users after we read count, we won't have
2165 * enough space - tough. This race is indistinguishable to the
2166 * caller from the case that the additional cgroup users didn't
2167 * show up until sometime later on.
2169 npids = cgroup_task_count(cgrp);
2170 if (npids) {
2171 pidarray = kmalloc(npids * sizeof(pid_t), GFP_KERNEL);
2172 if (!pidarray)
2173 goto err1;
2175 npids = pid_array_load(pidarray, npids, cgrp);
2176 sort(pidarray, npids, sizeof(pid_t), cmppid, NULL);
2178 /* Call pid_array_to_buf() twice, first just to get bufsz */
2179 ctr->bufsz = pid_array_to_buf(&c, sizeof(c), pidarray, npids) + 1;
2180 ctr->buf = kmalloc(ctr->bufsz, GFP_KERNEL);
2181 if (!ctr->buf)
2182 goto err2;
2183 ctr->bufsz = pid_array_to_buf(ctr->buf, ctr->bufsz, pidarray, npids);
2185 kfree(pidarray);
2186 } else {
2187 ctr->buf = NULL;
2188 ctr->bufsz = 0;
2190 file->private_data = ctr;
2191 return 0;
2193 err2:
2194 kfree(pidarray);
2195 err1:
2196 kfree(ctr);
2197 err0:
2198 return -ENOMEM;
2201 static ssize_t cgroup_tasks_read(struct cgroup *cgrp,
2202 struct cftype *cft,
2203 struct file *file, char __user *buf,
2204 size_t nbytes, loff_t *ppos)
2206 struct ctr_struct *ctr = file->private_data;
2208 return simple_read_from_buffer(buf, nbytes, ppos, ctr->buf, ctr->bufsz);
2211 static int cgroup_tasks_release(struct inode *unused_inode,
2212 struct file *file)
2214 struct ctr_struct *ctr;
2216 if (file->f_mode & FMODE_READ) {
2217 ctr = file->private_data;
2218 kfree(ctr->buf);
2219 kfree(ctr);
2221 return 0;
2224 static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
2225 struct cftype *cft)
2227 return notify_on_release(cgrp);
2231 * for the common functions, 'private' gives the type of file
2233 static struct cftype files[] = {
2235 .name = "tasks",
2236 .open = cgroup_tasks_open,
2237 .read = cgroup_tasks_read,
2238 .write = cgroup_common_file_write,
2239 .release = cgroup_tasks_release,
2240 .private = FILE_TASKLIST,
2244 .name = "notify_on_release",
2245 .read_u64 = cgroup_read_notify_on_release,
2246 .write = cgroup_common_file_write,
2247 .private = FILE_NOTIFY_ON_RELEASE,
2251 static struct cftype cft_release_agent = {
2252 .name = "release_agent",
2253 .read = cgroup_common_file_read,
2254 .write = cgroup_common_file_write,
2255 .private = FILE_RELEASE_AGENT,
2258 static int cgroup_populate_dir(struct cgroup *cgrp)
2260 int err;
2261 struct cgroup_subsys *ss;
2263 /* First clear out any existing files */
2264 cgroup_clear_directory(cgrp->dentry);
2266 err = cgroup_add_files(cgrp, NULL, files, ARRAY_SIZE(files));
2267 if (err < 0)
2268 return err;
2270 if (cgrp == cgrp->top_cgroup) {
2271 if ((err = cgroup_add_file(cgrp, NULL, &cft_release_agent)) < 0)
2272 return err;
2275 for_each_subsys(cgrp->root, ss) {
2276 if (ss->populate && (err = ss->populate(ss, cgrp)) < 0)
2277 return err;
2280 return 0;
2283 static void init_cgroup_css(struct cgroup_subsys_state *css,
2284 struct cgroup_subsys *ss,
2285 struct cgroup *cgrp)
2287 css->cgroup = cgrp;
2288 atomic_set(&css->refcnt, 0);
2289 css->flags = 0;
2290 if (cgrp == dummytop)
2291 set_bit(CSS_ROOT, &css->flags);
2292 BUG_ON(cgrp->subsys[ss->subsys_id]);
2293 cgrp->subsys[ss->subsys_id] = css;
2297 * cgroup_create - create a cgroup
2298 * @parent: cgroup that will be parent of the new cgroup
2299 * @dentry: dentry of the new cgroup
2300 * @mode: mode to set on new inode
2302 * Must be called with the mutex on the parent inode held
2304 static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
2305 int mode)
2307 struct cgroup *cgrp;
2308 struct cgroupfs_root *root = parent->root;
2309 int err = 0;
2310 struct cgroup_subsys *ss;
2311 struct super_block *sb = root->sb;
2313 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
2314 if (!cgrp)
2315 return -ENOMEM;
2317 /* Grab a reference on the superblock so the hierarchy doesn't
2318 * get deleted on unmount if there are child cgroups. This
2319 * can be done outside cgroup_mutex, since the sb can't
2320 * disappear while someone has an open control file on the
2321 * fs */
2322 atomic_inc(&sb->s_active);
2324 mutex_lock(&cgroup_mutex);
2326 INIT_LIST_HEAD(&cgrp->sibling);
2327 INIT_LIST_HEAD(&cgrp->children);
2328 INIT_LIST_HEAD(&cgrp->css_sets);
2329 INIT_LIST_HEAD(&cgrp->release_list);
2331 cgrp->parent = parent;
2332 cgrp->root = parent->root;
2333 cgrp->top_cgroup = parent->top_cgroup;
2335 if (notify_on_release(parent))
2336 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
2338 for_each_subsys(root, ss) {
2339 struct cgroup_subsys_state *css = ss->create(ss, cgrp);
2340 if (IS_ERR(css)) {
2341 err = PTR_ERR(css);
2342 goto err_destroy;
2344 init_cgroup_css(css, ss, cgrp);
2347 list_add(&cgrp->sibling, &cgrp->parent->children);
2348 root->number_of_cgroups++;
2350 err = cgroup_create_dir(cgrp, dentry, mode);
2351 if (err < 0)
2352 goto err_remove;
2354 /* The cgroup directory was pre-locked for us */
2355 BUG_ON(!mutex_is_locked(&cgrp->dentry->d_inode->i_mutex));
2357 err = cgroup_populate_dir(cgrp);
2358 /* If err < 0, we have a half-filled directory - oh well ;) */
2360 mutex_unlock(&cgroup_mutex);
2361 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
2363 return 0;
2365 err_remove:
2367 list_del(&cgrp->sibling);
2368 root->number_of_cgroups--;
2370 err_destroy:
2372 for_each_subsys(root, ss) {
2373 if (cgrp->subsys[ss->subsys_id])
2374 ss->destroy(ss, cgrp);
2377 mutex_unlock(&cgroup_mutex);
2379 /* Release the reference count that we took on the superblock */
2380 deactivate_super(sb);
2382 kfree(cgrp);
2383 return err;
2386 static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2388 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
2390 /* the vfs holds inode->i_mutex already */
2391 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
2394 static inline int cgroup_has_css_refs(struct cgroup *cgrp)
2396 /* Check the reference count on each subsystem. Since we
2397 * already established that there are no tasks in the
2398 * cgroup, if the css refcount is also 0, then there should
2399 * be no outstanding references, so the subsystem is safe to
2400 * destroy. We scan across all subsystems rather than using
2401 * the per-hierarchy linked list of mounted subsystems since
2402 * we can be called via check_for_release() with no
2403 * synchronization other than RCU, and the subsystem linked
2404 * list isn't RCU-safe */
2405 int i;
2406 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2407 struct cgroup_subsys *ss = subsys[i];
2408 struct cgroup_subsys_state *css;
2409 /* Skip subsystems not in this hierarchy */
2410 if (ss->root != cgrp->root)
2411 continue;
2412 css = cgrp->subsys[ss->subsys_id];
2413 /* When called from check_for_release() it's possible
2414 * that by this point the cgroup has been removed
2415 * and the css deleted. But a false-positive doesn't
2416 * matter, since it can only happen if the cgroup
2417 * has been deleted and hence no longer needs the
2418 * release agent to be called anyway. */
2419 if (css && atomic_read(&css->refcnt))
2420 return 1;
2422 return 0;
2425 static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
2427 struct cgroup *cgrp = dentry->d_fsdata;
2428 struct dentry *d;
2429 struct cgroup *parent;
2430 struct super_block *sb;
2431 struct cgroupfs_root *root;
2433 /* the vfs holds both inode->i_mutex already */
2435 mutex_lock(&cgroup_mutex);
2436 if (atomic_read(&cgrp->count) != 0) {
2437 mutex_unlock(&cgroup_mutex);
2438 return -EBUSY;
2440 if (!list_empty(&cgrp->children)) {
2441 mutex_unlock(&cgroup_mutex);
2442 return -EBUSY;
2445 parent = cgrp->parent;
2446 root = cgrp->root;
2447 sb = root->sb;
2450 * Call pre_destroy handlers of subsys. Notify subsystems
2451 * that rmdir() request comes.
2453 cgroup_call_pre_destroy(cgrp);
2455 if (cgroup_has_css_refs(cgrp)) {
2456 mutex_unlock(&cgroup_mutex);
2457 return -EBUSY;
2460 spin_lock(&release_list_lock);
2461 set_bit(CGRP_REMOVED, &cgrp->flags);
2462 if (!list_empty(&cgrp->release_list))
2463 list_del(&cgrp->release_list);
2464 spin_unlock(&release_list_lock);
2465 /* delete my sibling from parent->children */
2466 list_del(&cgrp->sibling);
2467 spin_lock(&cgrp->dentry->d_lock);
2468 d = dget(cgrp->dentry);
2469 cgrp->dentry = NULL;
2470 spin_unlock(&d->d_lock);
2472 cgroup_d_remove_dir(d);
2473 dput(d);
2475 set_bit(CGRP_RELEASABLE, &parent->flags);
2476 check_for_release(parent);
2478 mutex_unlock(&cgroup_mutex);
2479 return 0;
2482 static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
2484 struct cgroup_subsys_state *css;
2486 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
2488 /* Create the top cgroup state for this subsystem */
2489 ss->root = &rootnode;
2490 css = ss->create(ss, dummytop);
2491 /* We don't handle early failures gracefully */
2492 BUG_ON(IS_ERR(css));
2493 init_cgroup_css(css, ss, dummytop);
2495 /* Update the init_css_set to contain a subsys
2496 * pointer to this state - since the subsystem is
2497 * newly registered, all tasks and hence the
2498 * init_css_set is in the subsystem's top cgroup. */
2499 init_css_set.subsys[ss->subsys_id] = dummytop->subsys[ss->subsys_id];
2501 need_forkexit_callback |= ss->fork || ss->exit;
2502 need_mm_owner_callback |= !!ss->mm_owner_changed;
2504 /* At system boot, before all subsystems have been
2505 * registered, no tasks have been forked, so we don't
2506 * need to invoke fork callbacks here. */
2507 BUG_ON(!list_empty(&init_task.tasks));
2509 ss->active = 1;
2513 * cgroup_init_early - cgroup initialization at system boot
2515 * Initialize cgroups at system boot, and initialize any
2516 * subsystems that request early init.
2518 int __init cgroup_init_early(void)
2520 int i;
2521 kref_init(&init_css_set.ref);
2522 kref_get(&init_css_set.ref);
2523 INIT_LIST_HEAD(&init_css_set.cg_links);
2524 INIT_LIST_HEAD(&init_css_set.tasks);
2525 INIT_HLIST_NODE(&init_css_set.hlist);
2526 css_set_count = 1;
2527 init_cgroup_root(&rootnode);
2528 list_add(&rootnode.root_list, &roots);
2529 root_count = 1;
2530 init_task.cgroups = &init_css_set;
2532 init_css_set_link.cg = &init_css_set;
2533 list_add(&init_css_set_link.cgrp_link_list,
2534 &rootnode.top_cgroup.css_sets);
2535 list_add(&init_css_set_link.cg_link_list,
2536 &init_css_set.cg_links);
2538 for (i = 0; i < CSS_SET_TABLE_SIZE; i++)
2539 INIT_HLIST_HEAD(&css_set_table[i]);
2541 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2542 struct cgroup_subsys *ss = subsys[i];
2544 BUG_ON(!ss->name);
2545 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
2546 BUG_ON(!ss->create);
2547 BUG_ON(!ss->destroy);
2548 if (ss->subsys_id != i) {
2549 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
2550 ss->name, ss->subsys_id);
2551 BUG();
2554 if (ss->early_init)
2555 cgroup_init_subsys(ss);
2557 return 0;
2561 * cgroup_init - cgroup initialization
2563 * Register cgroup filesystem and /proc file, and initialize
2564 * any subsystems that didn't request early init.
2566 int __init cgroup_init(void)
2568 int err;
2569 int i;
2570 struct hlist_head *hhead;
2572 err = bdi_init(&cgroup_backing_dev_info);
2573 if (err)
2574 return err;
2576 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2577 struct cgroup_subsys *ss = subsys[i];
2578 if (!ss->early_init)
2579 cgroup_init_subsys(ss);
2582 /* Add init_css_set to the hash table */
2583 hhead = css_set_hash(init_css_set.subsys);
2584 hlist_add_head(&init_css_set.hlist, hhead);
2586 err = register_filesystem(&cgroup_fs_type);
2587 if (err < 0)
2588 goto out;
2590 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
2592 out:
2593 if (err)
2594 bdi_destroy(&cgroup_backing_dev_info);
2596 return err;
2600 * proc_cgroup_show()
2601 * - Print task's cgroup paths into seq_file, one line for each hierarchy
2602 * - Used for /proc/<pid>/cgroup.
2603 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
2604 * doesn't really matter if tsk->cgroup changes after we read it,
2605 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
2606 * anyway. No need to check that tsk->cgroup != NULL, thanks to
2607 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
2608 * cgroup to top_cgroup.
2611 /* TODO: Use a proper seq_file iterator */
2612 static int proc_cgroup_show(struct seq_file *m, void *v)
2614 struct pid *pid;
2615 struct task_struct *tsk;
2616 char *buf;
2617 int retval;
2618 struct cgroupfs_root *root;
2620 retval = -ENOMEM;
2621 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2622 if (!buf)
2623 goto out;
2625 retval = -ESRCH;
2626 pid = m->private;
2627 tsk = get_pid_task(pid, PIDTYPE_PID);
2628 if (!tsk)
2629 goto out_free;
2631 retval = 0;
2633 mutex_lock(&cgroup_mutex);
2635 for_each_root(root) {
2636 struct cgroup_subsys *ss;
2637 struct cgroup *cgrp;
2638 int subsys_id;
2639 int count = 0;
2641 /* Skip this hierarchy if it has no active subsystems */
2642 if (!root->actual_subsys_bits)
2643 continue;
2644 seq_printf(m, "%lu:", root->subsys_bits);
2645 for_each_subsys(root, ss)
2646 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
2647 seq_putc(m, ':');
2648 get_first_subsys(&root->top_cgroup, NULL, &subsys_id);
2649 cgrp = task_cgroup(tsk, subsys_id);
2650 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
2651 if (retval < 0)
2652 goto out_unlock;
2653 seq_puts(m, buf);
2654 seq_putc(m, '\n');
2657 out_unlock:
2658 mutex_unlock(&cgroup_mutex);
2659 put_task_struct(tsk);
2660 out_free:
2661 kfree(buf);
2662 out:
2663 return retval;
2666 static int cgroup_open(struct inode *inode, struct file *file)
2668 struct pid *pid = PROC_I(inode)->pid;
2669 return single_open(file, proc_cgroup_show, pid);
2672 struct file_operations proc_cgroup_operations = {
2673 .open = cgroup_open,
2674 .read = seq_read,
2675 .llseek = seq_lseek,
2676 .release = single_release,
2679 /* Display information about each subsystem and each hierarchy */
2680 static int proc_cgroupstats_show(struct seq_file *m, void *v)
2682 int i;
2684 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
2685 mutex_lock(&cgroup_mutex);
2686 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2687 struct cgroup_subsys *ss = subsys[i];
2688 seq_printf(m, "%s\t%lu\t%d\t%d\n",
2689 ss->name, ss->root->subsys_bits,
2690 ss->root->number_of_cgroups, !ss->disabled);
2692 mutex_unlock(&cgroup_mutex);
2693 return 0;
2696 static int cgroupstats_open(struct inode *inode, struct file *file)
2698 return single_open(file, proc_cgroupstats_show, NULL);
2701 static struct file_operations proc_cgroupstats_operations = {
2702 .open = cgroupstats_open,
2703 .read = seq_read,
2704 .llseek = seq_lseek,
2705 .release = single_release,
2709 * cgroup_fork - attach newly forked task to its parents cgroup.
2710 * @child: pointer to task_struct of forking parent process.
2712 * Description: A task inherits its parent's cgroup at fork().
2714 * A pointer to the shared css_set was automatically copied in
2715 * fork.c by dup_task_struct(). However, we ignore that copy, since
2716 * it was not made under the protection of RCU or cgroup_mutex, so
2717 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
2718 * have already changed current->cgroups, allowing the previously
2719 * referenced cgroup group to be removed and freed.
2721 * At the point that cgroup_fork() is called, 'current' is the parent
2722 * task, and the passed argument 'child' points to the child task.
2724 void cgroup_fork(struct task_struct *child)
2726 task_lock(current);
2727 child->cgroups = current->cgroups;
2728 get_css_set(child->cgroups);
2729 task_unlock(current);
2730 INIT_LIST_HEAD(&child->cg_list);
2734 * cgroup_fork_callbacks - run fork callbacks
2735 * @child: the new task
2737 * Called on a new task very soon before adding it to the
2738 * tasklist. No need to take any locks since no-one can
2739 * be operating on this task.
2741 void cgroup_fork_callbacks(struct task_struct *child)
2743 if (need_forkexit_callback) {
2744 int i;
2745 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2746 struct cgroup_subsys *ss = subsys[i];
2747 if (ss->fork)
2748 ss->fork(ss, child);
2753 #ifdef CONFIG_MM_OWNER
2755 * cgroup_mm_owner_callbacks - run callbacks when the mm->owner changes
2756 * @p: the new owner
2758 * Called on every change to mm->owner. mm_init_owner() does not
2759 * invoke this routine, since it assigns the mm->owner the first time
2760 * and does not change it.
2762 void cgroup_mm_owner_callbacks(struct task_struct *old, struct task_struct *new)
2764 struct cgroup *oldcgrp, *newcgrp;
2766 if (need_mm_owner_callback) {
2767 int i;
2768 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2769 struct cgroup_subsys *ss = subsys[i];
2770 oldcgrp = task_cgroup(old, ss->subsys_id);
2771 newcgrp = task_cgroup(new, ss->subsys_id);
2772 if (oldcgrp == newcgrp)
2773 continue;
2774 if (ss->mm_owner_changed)
2775 ss->mm_owner_changed(ss, oldcgrp, newcgrp);
2779 #endif /* CONFIG_MM_OWNER */
2782 * cgroup_post_fork - called on a new task after adding it to the task list
2783 * @child: the task in question
2785 * Adds the task to the list running through its css_set if necessary.
2786 * Has to be after the task is visible on the task list in case we race
2787 * with the first call to cgroup_iter_start() - to guarantee that the
2788 * new task ends up on its list.
2790 void cgroup_post_fork(struct task_struct *child)
2792 if (use_task_css_set_links) {
2793 write_lock(&css_set_lock);
2794 if (list_empty(&child->cg_list))
2795 list_add(&child->cg_list, &child->cgroups->tasks);
2796 write_unlock(&css_set_lock);
2800 * cgroup_exit - detach cgroup from exiting task
2801 * @tsk: pointer to task_struct of exiting process
2802 * @run_callback: run exit callbacks?
2804 * Description: Detach cgroup from @tsk and release it.
2806 * Note that cgroups marked notify_on_release force every task in
2807 * them to take the global cgroup_mutex mutex when exiting.
2808 * This could impact scaling on very large systems. Be reluctant to
2809 * use notify_on_release cgroups where very high task exit scaling
2810 * is required on large systems.
2812 * the_top_cgroup_hack:
2814 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
2816 * We call cgroup_exit() while the task is still competent to
2817 * handle notify_on_release(), then leave the task attached to the
2818 * root cgroup in each hierarchy for the remainder of its exit.
2820 * To do this properly, we would increment the reference count on
2821 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
2822 * code we would add a second cgroup function call, to drop that
2823 * reference. This would just create an unnecessary hot spot on
2824 * the top_cgroup reference count, to no avail.
2826 * Normally, holding a reference to a cgroup without bumping its
2827 * count is unsafe. The cgroup could go away, or someone could
2828 * attach us to a different cgroup, decrementing the count on
2829 * the first cgroup that we never incremented. But in this case,
2830 * top_cgroup isn't going away, and either task has PF_EXITING set,
2831 * which wards off any cgroup_attach_task() attempts, or task is a failed
2832 * fork, never visible to cgroup_attach_task.
2834 void cgroup_exit(struct task_struct *tsk, int run_callbacks)
2836 int i;
2837 struct css_set *cg;
2839 if (run_callbacks && need_forkexit_callback) {
2840 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2841 struct cgroup_subsys *ss = subsys[i];
2842 if (ss->exit)
2843 ss->exit(ss, tsk);
2848 * Unlink from the css_set task list if necessary.
2849 * Optimistically check cg_list before taking
2850 * css_set_lock
2852 if (!list_empty(&tsk->cg_list)) {
2853 write_lock(&css_set_lock);
2854 if (!list_empty(&tsk->cg_list))
2855 list_del(&tsk->cg_list);
2856 write_unlock(&css_set_lock);
2859 /* Reassign the task to the init_css_set. */
2860 task_lock(tsk);
2861 cg = tsk->cgroups;
2862 tsk->cgroups = &init_css_set;
2863 task_unlock(tsk);
2864 if (cg)
2865 put_css_set_taskexit(cg);
2869 * cgroup_clone - clone the cgroup the given subsystem is attached to
2870 * @tsk: the task to be moved
2871 * @subsys: the given subsystem
2873 * Duplicate the current cgroup in the hierarchy that the given
2874 * subsystem is attached to, and move this task into the new
2875 * child.
2877 int cgroup_clone(struct task_struct *tsk, struct cgroup_subsys *subsys)
2879 struct dentry *dentry;
2880 int ret = 0;
2881 char nodename[MAX_CGROUP_TYPE_NAMELEN];
2882 struct cgroup *parent, *child;
2883 struct inode *inode;
2884 struct css_set *cg;
2885 struct cgroupfs_root *root;
2886 struct cgroup_subsys *ss;
2888 /* We shouldn't be called by an unregistered subsystem */
2889 BUG_ON(!subsys->active);
2891 /* First figure out what hierarchy and cgroup we're dealing
2892 * with, and pin them so we can drop cgroup_mutex */
2893 mutex_lock(&cgroup_mutex);
2894 again:
2895 root = subsys->root;
2896 if (root == &rootnode) {
2897 printk(KERN_INFO
2898 "Not cloning cgroup for unused subsystem %s\n",
2899 subsys->name);
2900 mutex_unlock(&cgroup_mutex);
2901 return 0;
2903 cg = tsk->cgroups;
2904 parent = task_cgroup(tsk, subsys->subsys_id);
2906 snprintf(nodename, MAX_CGROUP_TYPE_NAMELEN, "node_%d", tsk->pid);
2908 /* Pin the hierarchy */
2909 atomic_inc(&parent->root->sb->s_active);
2911 /* Keep the cgroup alive */
2912 get_css_set(cg);
2913 mutex_unlock(&cgroup_mutex);
2915 /* Now do the VFS work to create a cgroup */
2916 inode = parent->dentry->d_inode;
2918 /* Hold the parent directory mutex across this operation to
2919 * stop anyone else deleting the new cgroup */
2920 mutex_lock(&inode->i_mutex);
2921 dentry = lookup_one_len(nodename, parent->dentry, strlen(nodename));
2922 if (IS_ERR(dentry)) {
2923 printk(KERN_INFO
2924 "cgroup: Couldn't allocate dentry for %s: %ld\n", nodename,
2925 PTR_ERR(dentry));
2926 ret = PTR_ERR(dentry);
2927 goto out_release;
2930 /* Create the cgroup directory, which also creates the cgroup */
2931 ret = vfs_mkdir(inode, dentry, S_IFDIR | 0755);
2932 child = __d_cgrp(dentry);
2933 dput(dentry);
2934 if (ret) {
2935 printk(KERN_INFO
2936 "Failed to create cgroup %s: %d\n", nodename,
2937 ret);
2938 goto out_release;
2941 if (!child) {
2942 printk(KERN_INFO
2943 "Couldn't find new cgroup %s\n", nodename);
2944 ret = -ENOMEM;
2945 goto out_release;
2948 /* The cgroup now exists. Retake cgroup_mutex and check
2949 * that we're still in the same state that we thought we
2950 * were. */
2951 mutex_lock(&cgroup_mutex);
2952 if ((root != subsys->root) ||
2953 (parent != task_cgroup(tsk, subsys->subsys_id))) {
2954 /* Aargh, we raced ... */
2955 mutex_unlock(&inode->i_mutex);
2956 put_css_set(cg);
2958 deactivate_super(parent->root->sb);
2959 /* The cgroup is still accessible in the VFS, but
2960 * we're not going to try to rmdir() it at this
2961 * point. */
2962 printk(KERN_INFO
2963 "Race in cgroup_clone() - leaking cgroup %s\n",
2964 nodename);
2965 goto again;
2968 /* do any required auto-setup */
2969 for_each_subsys(root, ss) {
2970 if (ss->post_clone)
2971 ss->post_clone(ss, child);
2974 /* All seems fine. Finish by moving the task into the new cgroup */
2975 ret = cgroup_attach_task(child, tsk);
2976 mutex_unlock(&cgroup_mutex);
2978 out_release:
2979 mutex_unlock(&inode->i_mutex);
2981 mutex_lock(&cgroup_mutex);
2982 put_css_set(cg);
2983 mutex_unlock(&cgroup_mutex);
2984 deactivate_super(parent->root->sb);
2985 return ret;
2989 * cgroup_is_descendant - see if @cgrp is a descendant of current task's cgrp
2990 * @cgrp: the cgroup in question
2992 * See if @cgrp is a descendant of the current task's cgroup in
2993 * the appropriate hierarchy.
2995 * If we are sending in dummytop, then presumably we are creating
2996 * the top cgroup in the subsystem.
2998 * Called only by the ns (nsproxy) cgroup.
3000 int cgroup_is_descendant(const struct cgroup *cgrp)
3002 int ret;
3003 struct cgroup *target;
3004 int subsys_id;
3006 if (cgrp == dummytop)
3007 return 1;
3009 get_first_subsys(cgrp, NULL, &subsys_id);
3010 target = task_cgroup(current, subsys_id);
3011 while (cgrp != target && cgrp!= cgrp->top_cgroup)
3012 cgrp = cgrp->parent;
3013 ret = (cgrp == target);
3014 return ret;
3017 static void check_for_release(struct cgroup *cgrp)
3019 /* All of these checks rely on RCU to keep the cgroup
3020 * structure alive */
3021 if (cgroup_is_releasable(cgrp) && !atomic_read(&cgrp->count)
3022 && list_empty(&cgrp->children) && !cgroup_has_css_refs(cgrp)) {
3023 /* Control Group is currently removeable. If it's not
3024 * already queued for a userspace notification, queue
3025 * it now */
3026 int need_schedule_work = 0;
3027 spin_lock(&release_list_lock);
3028 if (!cgroup_is_removed(cgrp) &&
3029 list_empty(&cgrp->release_list)) {
3030 list_add(&cgrp->release_list, &release_list);
3031 need_schedule_work = 1;
3033 spin_unlock(&release_list_lock);
3034 if (need_schedule_work)
3035 schedule_work(&release_agent_work);
3039 void __css_put(struct cgroup_subsys_state *css)
3041 struct cgroup *cgrp = css->cgroup;
3042 rcu_read_lock();
3043 if (atomic_dec_and_test(&css->refcnt) && notify_on_release(cgrp)) {
3044 set_bit(CGRP_RELEASABLE, &cgrp->flags);
3045 check_for_release(cgrp);
3047 rcu_read_unlock();
3051 * Notify userspace when a cgroup is released, by running the
3052 * configured release agent with the name of the cgroup (path
3053 * relative to the root of cgroup file system) as the argument.
3055 * Most likely, this user command will try to rmdir this cgroup.
3057 * This races with the possibility that some other task will be
3058 * attached to this cgroup before it is removed, or that some other
3059 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
3060 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
3061 * unused, and this cgroup will be reprieved from its death sentence,
3062 * to continue to serve a useful existence. Next time it's released,
3063 * we will get notified again, if it still has 'notify_on_release' set.
3065 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
3066 * means only wait until the task is successfully execve()'d. The
3067 * separate release agent task is forked by call_usermodehelper(),
3068 * then control in this thread returns here, without waiting for the
3069 * release agent task. We don't bother to wait because the caller of
3070 * this routine has no use for the exit status of the release agent
3071 * task, so no sense holding our caller up for that.
3073 static void cgroup_release_agent(struct work_struct *work)
3075 BUG_ON(work != &release_agent_work);
3076 mutex_lock(&cgroup_mutex);
3077 spin_lock(&release_list_lock);
3078 while (!list_empty(&release_list)) {
3079 char *argv[3], *envp[3];
3080 int i;
3081 char *pathbuf;
3082 struct cgroup *cgrp = list_entry(release_list.next,
3083 struct cgroup,
3084 release_list);
3085 list_del_init(&cgrp->release_list);
3086 spin_unlock(&release_list_lock);
3087 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3088 if (!pathbuf) {
3089 spin_lock(&release_list_lock);
3090 continue;
3093 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0) {
3094 kfree(pathbuf);
3095 spin_lock(&release_list_lock);
3096 continue;
3099 i = 0;
3100 argv[i++] = cgrp->root->release_agent_path;
3101 argv[i++] = (char *)pathbuf;
3102 argv[i] = NULL;
3104 i = 0;
3105 /* minimal command environment */
3106 envp[i++] = "HOME=/";
3107 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
3108 envp[i] = NULL;
3110 /* Drop the lock while we invoke the usermode helper,
3111 * since the exec could involve hitting disk and hence
3112 * be a slow process */
3113 mutex_unlock(&cgroup_mutex);
3114 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
3115 kfree(pathbuf);
3116 mutex_lock(&cgroup_mutex);
3117 spin_lock(&release_list_lock);
3119 spin_unlock(&release_list_lock);
3120 mutex_unlock(&cgroup_mutex);
3123 static int __init cgroup_disable(char *str)
3125 int i;
3126 char *token;
3128 while ((token = strsep(&str, ",")) != NULL) {
3129 if (!*token)
3130 continue;
3132 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3133 struct cgroup_subsys *ss = subsys[i];
3135 if (!strcmp(token, ss->name)) {
3136 ss->disabled = 1;
3137 printk(KERN_INFO "Disabling %s control group"
3138 " subsystem\n", ss->name);
3139 break;
3143 return 1;
3145 __setup("cgroup_disable=", cgroup_disable);