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/module.h>
27 #include <linux/ctype.h>
28 #include <linux/errno.h>
30 #include <linux/kernel.h>
31 #include <linux/list.h>
33 #include <linux/mutex.h>
34 #include <linux/mount.h>
35 #include <linux/pagemap.h>
36 #include <linux/proc_fs.h>
37 #include <linux/rcupdate.h>
38 #include <linux/sched.h>
39 #include <linux/backing-dev.h>
40 #include <linux/seq_file.h>
41 #include <linux/slab.h>
42 #include <linux/magic.h>
43 #include <linux/spinlock.h>
44 #include <linux/string.h>
45 #include <linux/sort.h>
46 #include <linux/kmod.h>
47 #include <linux/module.h>
48 #include <linux/delayacct.h>
49 #include <linux/cgroupstats.h>
50 #include <linux/hash.h>
51 #include <linux/namei.h>
52 #include <linux/smp_lock.h>
53 #include <linux/pid_namespace.h>
54 #include <linux/idr.h>
55 #include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
57 #include <asm/atomic.h>
59 static DEFINE_MUTEX(cgroup_mutex);
62 * Generate an array of cgroup subsystem pointers. At boot time, this is
63 * populated up to CGROUP_BUILTIN_SUBSYS_COUNT, and modular subsystems are
64 * registered after that. The mutable section of this array is protected by
67 #define SUBSYS(_x) &_x ## _subsys,
68 static struct cgroup_subsys *subsys[CGROUP_SUBSYS_COUNT] = {
69 #include <linux/cgroup_subsys.h>
72 #define MAX_CGROUP_ROOT_NAMELEN 64
75 * A cgroupfs_root represents the root of a cgroup hierarchy,
76 * and may be associated with a superblock to form an active
79 struct cgroupfs_root {
80 struct super_block *sb;
83 * The bitmask of subsystems intended to be attached to this
86 unsigned long subsys_bits;
88 /* Unique id for this hierarchy. */
91 /* The bitmask of subsystems currently attached to this hierarchy */
92 unsigned long actual_subsys_bits;
94 /* A list running through the attached subsystems */
95 struct list_head subsys_list;
97 /* The root cgroup for this hierarchy */
98 struct cgroup top_cgroup;
100 /* Tracks how many cgroups are currently defined in hierarchy.*/
101 int number_of_cgroups;
103 /* A list running through the active hierarchies */
104 struct list_head root_list;
106 /* Hierarchy-specific flags */
109 /* The path to use for release notifications. */
110 char release_agent_path[PATH_MAX];
112 /* The name for this hierarchy - may be empty */
113 char name[MAX_CGROUP_ROOT_NAMELEN];
117 * The "rootnode" hierarchy is the "dummy hierarchy", reserved for the
118 * subsystems that are otherwise unattached - it never has more than a
119 * single cgroup, and all tasks are part of that cgroup.
121 static struct cgroupfs_root rootnode;
124 * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when
125 * cgroup_subsys->use_id != 0.
127 #define CSS_ID_MAX (65535)
130 * The css to which this ID points. This pointer is set to valid value
131 * after cgroup is populated. If cgroup is removed, this will be NULL.
132 * This pointer is expected to be RCU-safe because destroy()
133 * is called after synchronize_rcu(). But for safe use, css_is_removed()
134 * css_tryget() should be used for avoiding race.
136 struct cgroup_subsys_state *css;
142 * Depth in hierarchy which this ID belongs to.
144 unsigned short depth;
146 * ID is freed by RCU. (and lookup routine is RCU safe.)
148 struct rcu_head rcu_head;
150 * Hierarchy of CSS ID belongs to.
152 unsigned short stack[0]; /* Array of Length (depth+1) */
156 /* The list of hierarchy roots */
158 static LIST_HEAD(roots);
159 static int root_count;
161 static DEFINE_IDA(hierarchy_ida);
162 static int next_hierarchy_id;
163 static DEFINE_SPINLOCK(hierarchy_id_lock);
165 /* dummytop is a shorthand for the dummy hierarchy's top cgroup */
166 #define dummytop (&rootnode.top_cgroup)
168 /* This flag indicates whether tasks in the fork and exit paths should
169 * check for fork/exit handlers to call. This avoids us having to do
170 * extra work in the fork/exit path if none of the subsystems need to
173 static int need_forkexit_callback __read_mostly;
175 #ifdef CONFIG_PROVE_LOCKING
176 int cgroup_lock_is_held(void)
178 return lockdep_is_held(&cgroup_mutex);
180 #else /* #ifdef CONFIG_PROVE_LOCKING */
181 int cgroup_lock_is_held(void)
183 return mutex_is_locked(&cgroup_mutex);
185 #endif /* #else #ifdef CONFIG_PROVE_LOCKING */
187 EXPORT_SYMBOL_GPL(cgroup_lock_is_held);
189 /* convenient tests for these bits */
190 inline int cgroup_is_removed(const struct cgroup *cgrp)
192 return test_bit(CGRP_REMOVED, &cgrp->flags);
195 /* bits in struct cgroupfs_root flags field */
197 ROOT_NOPREFIX, /* mounted subsystems have no named prefix */
200 static int cgroup_is_releasable(const struct cgroup *cgrp)
203 (1 << CGRP_RELEASABLE) |
204 (1 << CGRP_NOTIFY_ON_RELEASE);
205 return (cgrp->flags & bits) == bits;
208 static int notify_on_release(const struct cgroup *cgrp)
210 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
214 * for_each_subsys() allows you to iterate on each subsystem attached to
215 * an active hierarchy
217 #define for_each_subsys(_root, _ss) \
218 list_for_each_entry(_ss, &_root->subsys_list, sibling)
220 /* for_each_active_root() allows you to iterate across the active hierarchies */
221 #define for_each_active_root(_root) \
222 list_for_each_entry(_root, &roots, root_list)
224 /* the list of cgroups eligible for automatic release. Protected by
225 * release_list_lock */
226 static LIST_HEAD(release_list);
227 static DEFINE_SPINLOCK(release_list_lock);
228 static void cgroup_release_agent(struct work_struct *work);
229 static DECLARE_WORK(release_agent_work, cgroup_release_agent);
230 static void check_for_release(struct cgroup *cgrp);
232 /* Link structure for associating css_set objects with cgroups */
233 struct cg_cgroup_link {
235 * List running through cg_cgroup_links associated with a
236 * cgroup, anchored on cgroup->css_sets
238 struct list_head cgrp_link_list;
241 * List running through cg_cgroup_links pointing at a
242 * single css_set object, anchored on css_set->cg_links
244 struct list_head cg_link_list;
248 /* The default css_set - used by init and its children prior to any
249 * hierarchies being mounted. It contains a pointer to the root state
250 * for each subsystem. Also used to anchor the list of css_sets. Not
251 * reference-counted, to improve performance when child cgroups
252 * haven't been created.
255 static struct css_set init_css_set;
256 static struct cg_cgroup_link init_css_set_link;
258 static int cgroup_init_idr(struct cgroup_subsys *ss,
259 struct cgroup_subsys_state *css);
261 /* css_set_lock protects the list of css_set objects, and the
262 * chain of tasks off each css_set. Nests outside task->alloc_lock
263 * due to cgroup_iter_start() */
264 static DEFINE_RWLOCK(css_set_lock);
265 static int css_set_count;
268 * hash table for cgroup groups. This improves the performance to find
269 * an existing css_set. This hash doesn't (currently) take into
270 * account cgroups in empty hierarchies.
272 #define CSS_SET_HASH_BITS 7
273 #define CSS_SET_TABLE_SIZE (1 << CSS_SET_HASH_BITS)
274 static struct hlist_head css_set_table[CSS_SET_TABLE_SIZE];
276 static struct hlist_head *css_set_hash(struct cgroup_subsys_state *css[])
280 unsigned long tmp = 0UL;
282 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++)
283 tmp += (unsigned long)css[i];
284 tmp = (tmp >> 16) ^ tmp;
286 index = hash_long(tmp, CSS_SET_HASH_BITS);
288 return &css_set_table[index];
291 static void free_css_set_rcu(struct rcu_head *obj)
293 struct css_set *cg = container_of(obj, struct css_set, rcu_head);
297 /* We don't maintain the lists running through each css_set to its
298 * task until after the first call to cgroup_iter_start(). This
299 * reduces the fork()/exit() overhead for people who have cgroups
300 * compiled into their kernel but not actually in use */
301 static int use_task_css_set_links __read_mostly;
303 static void __put_css_set(struct css_set *cg, int taskexit)
305 struct cg_cgroup_link *link;
306 struct cg_cgroup_link *saved_link;
308 * Ensure that the refcount doesn't hit zero while any readers
309 * can see it. Similar to atomic_dec_and_lock(), but for an
312 if (atomic_add_unless(&cg->refcount, -1, 1))
314 write_lock(&css_set_lock);
315 if (!atomic_dec_and_test(&cg->refcount)) {
316 write_unlock(&css_set_lock);
320 /* This css_set is dead. unlink it and release cgroup refcounts */
321 hlist_del(&cg->hlist);
324 list_for_each_entry_safe(link, saved_link, &cg->cg_links,
326 struct cgroup *cgrp = link->cgrp;
327 list_del(&link->cg_link_list);
328 list_del(&link->cgrp_link_list);
329 if (atomic_dec_and_test(&cgrp->count) &&
330 notify_on_release(cgrp)) {
332 set_bit(CGRP_RELEASABLE, &cgrp->flags);
333 check_for_release(cgrp);
339 write_unlock(&css_set_lock);
340 call_rcu(&cg->rcu_head, free_css_set_rcu);
344 * refcounted get/put for css_set objects
346 static inline void get_css_set(struct css_set *cg)
348 atomic_inc(&cg->refcount);
351 static inline void put_css_set(struct css_set *cg)
353 __put_css_set(cg, 0);
356 static inline void put_css_set_taskexit(struct css_set *cg)
358 __put_css_set(cg, 1);
362 * compare_css_sets - helper function for find_existing_css_set().
363 * @cg: candidate css_set being tested
364 * @old_cg: existing css_set for a task
365 * @new_cgrp: cgroup that's being entered by the task
366 * @template: desired set of css pointers in css_set (pre-calculated)
368 * Returns true if "cg" matches "old_cg" except for the hierarchy
369 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
371 static bool compare_css_sets(struct css_set *cg,
372 struct css_set *old_cg,
373 struct cgroup *new_cgrp,
374 struct cgroup_subsys_state *template[])
376 struct list_head *l1, *l2;
378 if (memcmp(template, cg->subsys, sizeof(cg->subsys))) {
379 /* Not all subsystems matched */
384 * Compare cgroup pointers in order to distinguish between
385 * different cgroups in heirarchies with no subsystems. We
386 * could get by with just this check alone (and skip the
387 * memcmp above) but on most setups the memcmp check will
388 * avoid the need for this more expensive check on almost all
393 l2 = &old_cg->cg_links;
395 struct cg_cgroup_link *cgl1, *cgl2;
396 struct cgroup *cg1, *cg2;
400 /* See if we reached the end - both lists are equal length. */
401 if (l1 == &cg->cg_links) {
402 BUG_ON(l2 != &old_cg->cg_links);
405 BUG_ON(l2 == &old_cg->cg_links);
407 /* Locate the cgroups associated with these links. */
408 cgl1 = list_entry(l1, struct cg_cgroup_link, cg_link_list);
409 cgl2 = list_entry(l2, struct cg_cgroup_link, cg_link_list);
412 /* Hierarchies should be linked in the same order. */
413 BUG_ON(cg1->root != cg2->root);
416 * If this hierarchy is the hierarchy of the cgroup
417 * that's changing, then we need to check that this
418 * css_set points to the new cgroup; if it's any other
419 * hierarchy, then this css_set should point to the
420 * same cgroup as the old css_set.
422 if (cg1->root == new_cgrp->root) {
434 * find_existing_css_set() is a helper for
435 * find_css_set(), and checks to see whether an existing
436 * css_set is suitable.
438 * oldcg: the cgroup group that we're using before the cgroup
441 * cgrp: the cgroup that we're moving into
443 * template: location in which to build the desired set of subsystem
444 * state objects for the new cgroup group
446 static struct css_set *find_existing_css_set(
447 struct css_set *oldcg,
449 struct cgroup_subsys_state *template[])
452 struct cgroupfs_root *root = cgrp->root;
453 struct hlist_head *hhead;
454 struct hlist_node *node;
458 * Build the set of subsystem state objects that we want to see in the
459 * new css_set. while subsystems can change globally, the entries here
460 * won't change, so no need for locking.
462 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
463 if (root->subsys_bits & (1UL << i)) {
464 /* Subsystem is in this hierarchy. So we want
465 * the subsystem state from the new
467 template[i] = cgrp->subsys[i];
469 /* Subsystem is not in this hierarchy, so we
470 * don't want to change the subsystem state */
471 template[i] = oldcg->subsys[i];
475 hhead = css_set_hash(template);
476 hlist_for_each_entry(cg, node, hhead, hlist) {
477 if (!compare_css_sets(cg, oldcg, cgrp, template))
480 /* This css_set matches what we need */
484 /* No existing cgroup group matched */
488 static void free_cg_links(struct list_head *tmp)
490 struct cg_cgroup_link *link;
491 struct cg_cgroup_link *saved_link;
493 list_for_each_entry_safe(link, saved_link, tmp, cgrp_link_list) {
494 list_del(&link->cgrp_link_list);
500 * allocate_cg_links() allocates "count" cg_cgroup_link structures
501 * and chains them on tmp through their cgrp_link_list fields. Returns 0 on
502 * success or a negative error
504 static int allocate_cg_links(int count, struct list_head *tmp)
506 struct cg_cgroup_link *link;
509 for (i = 0; i < count; i++) {
510 link = kmalloc(sizeof(*link), GFP_KERNEL);
515 list_add(&link->cgrp_link_list, tmp);
521 * link_css_set - a helper function to link a css_set to a cgroup
522 * @tmp_cg_links: cg_cgroup_link objects allocated by allocate_cg_links()
523 * @cg: the css_set to be linked
524 * @cgrp: the destination cgroup
526 static void link_css_set(struct list_head *tmp_cg_links,
527 struct css_set *cg, struct cgroup *cgrp)
529 struct cg_cgroup_link *link;
531 BUG_ON(list_empty(tmp_cg_links));
532 link = list_first_entry(tmp_cg_links, struct cg_cgroup_link,
536 atomic_inc(&cgrp->count);
537 list_move(&link->cgrp_link_list, &cgrp->css_sets);
539 * Always add links to the tail of the list so that the list
540 * is sorted by order of hierarchy creation
542 list_add_tail(&link->cg_link_list, &cg->cg_links);
546 * find_css_set() takes an existing cgroup group and a
547 * cgroup object, and returns a css_set object that's
548 * equivalent to the old group, but with the given cgroup
549 * substituted into the appropriate hierarchy. Must be called with
552 static struct css_set *find_css_set(
553 struct css_set *oldcg, struct cgroup *cgrp)
556 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT];
558 struct list_head tmp_cg_links;
560 struct hlist_head *hhead;
561 struct cg_cgroup_link *link;
563 /* First see if we already have a cgroup group that matches
565 read_lock(&css_set_lock);
566 res = find_existing_css_set(oldcg, cgrp, template);
569 read_unlock(&css_set_lock);
574 res = kmalloc(sizeof(*res), GFP_KERNEL);
578 /* Allocate all the cg_cgroup_link objects that we'll need */
579 if (allocate_cg_links(root_count, &tmp_cg_links) < 0) {
584 atomic_set(&res->refcount, 1);
585 INIT_LIST_HEAD(&res->cg_links);
586 INIT_LIST_HEAD(&res->tasks);
587 INIT_HLIST_NODE(&res->hlist);
589 /* Copy the set of subsystem state objects generated in
590 * find_existing_css_set() */
591 memcpy(res->subsys, template, sizeof(res->subsys));
593 write_lock(&css_set_lock);
594 /* Add reference counts and links from the new css_set. */
595 list_for_each_entry(link, &oldcg->cg_links, cg_link_list) {
596 struct cgroup *c = link->cgrp;
597 if (c->root == cgrp->root)
599 link_css_set(&tmp_cg_links, res, c);
602 BUG_ON(!list_empty(&tmp_cg_links));
606 /* Add this cgroup group to the hash table */
607 hhead = css_set_hash(res->subsys);
608 hlist_add_head(&res->hlist, hhead);
610 write_unlock(&css_set_lock);
616 * Return the cgroup for "task" from the given hierarchy. Must be
617 * called with cgroup_mutex held.
619 static struct cgroup *task_cgroup_from_root(struct task_struct *task,
620 struct cgroupfs_root *root)
623 struct cgroup *res = NULL;
625 BUG_ON(!mutex_is_locked(&cgroup_mutex));
626 read_lock(&css_set_lock);
628 * No need to lock the task - since we hold cgroup_mutex the
629 * task can't change groups, so the only thing that can happen
630 * is that it exits and its css is set back to init_css_set.
633 if (css == &init_css_set) {
634 res = &root->top_cgroup;
636 struct cg_cgroup_link *link;
637 list_for_each_entry(link, &css->cg_links, cg_link_list) {
638 struct cgroup *c = link->cgrp;
639 if (c->root == root) {
645 read_unlock(&css_set_lock);
651 * There is one global cgroup mutex. We also require taking
652 * task_lock() when dereferencing a task's cgroup subsys pointers.
653 * See "The task_lock() exception", at the end of this comment.
655 * A task must hold cgroup_mutex to modify cgroups.
657 * Any task can increment and decrement the count field without lock.
658 * So in general, code holding cgroup_mutex can't rely on the count
659 * field not changing. However, if the count goes to zero, then only
660 * cgroup_attach_task() can increment it again. Because a count of zero
661 * means that no tasks are currently attached, therefore there is no
662 * way a task attached to that cgroup can fork (the other way to
663 * increment the count). So code holding cgroup_mutex can safely
664 * assume that if the count is zero, it will stay zero. Similarly, if
665 * a task holds cgroup_mutex on a cgroup with zero count, it
666 * knows that the cgroup won't be removed, as cgroup_rmdir()
669 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
670 * (usually) take cgroup_mutex. These are the two most performance
671 * critical pieces of code here. The exception occurs on cgroup_exit(),
672 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
673 * is taken, and if the cgroup count is zero, a usermode call made
674 * to the release agent with the name of the cgroup (path relative to
675 * the root of cgroup file system) as the argument.
677 * A cgroup can only be deleted if both its 'count' of using tasks
678 * is zero, and its list of 'children' cgroups is empty. Since all
679 * tasks in the system use _some_ cgroup, and since there is always at
680 * least one task in the system (init, pid == 1), therefore, top_cgroup
681 * always has either children cgroups and/or using tasks. So we don't
682 * need a special hack to ensure that top_cgroup cannot be deleted.
684 * The task_lock() exception
686 * The need for this exception arises from the action of
687 * cgroup_attach_task(), which overwrites one tasks cgroup pointer with
688 * another. It does so using cgroup_mutex, however there are
689 * several performance critical places that need to reference
690 * task->cgroup without the expense of grabbing a system global
691 * mutex. Therefore except as noted below, when dereferencing or, as
692 * in cgroup_attach_task(), modifying a task'ss cgroup pointer we use
693 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
694 * the task_struct routinely used for such matters.
696 * P.S. One more locking exception. RCU is used to guard the
697 * update of a tasks cgroup pointer by cgroup_attach_task()
701 * cgroup_lock - lock out any changes to cgroup structures
704 void cgroup_lock(void)
706 mutex_lock(&cgroup_mutex);
710 * cgroup_unlock - release lock on cgroup changes
712 * Undo the lock taken in a previous cgroup_lock() call.
714 void cgroup_unlock(void)
716 mutex_unlock(&cgroup_mutex);
720 * A couple of forward declarations required, due to cyclic reference loop:
721 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
722 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
726 static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode);
727 static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
728 static int cgroup_populate_dir(struct cgroup *cgrp);
729 static const struct inode_operations cgroup_dir_inode_operations;
730 static const struct file_operations proc_cgroupstats_operations;
732 static struct backing_dev_info cgroup_backing_dev_info = {
734 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
737 static int alloc_css_id(struct cgroup_subsys *ss,
738 struct cgroup *parent, struct cgroup *child);
740 static struct inode *cgroup_new_inode(mode_t mode, struct super_block *sb)
742 struct inode *inode = new_inode(sb);
745 inode->i_mode = mode;
746 inode->i_uid = current_fsuid();
747 inode->i_gid = current_fsgid();
748 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
749 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
755 * Call subsys's pre_destroy handler.
756 * This is called before css refcnt check.
758 static int cgroup_call_pre_destroy(struct cgroup *cgrp)
760 struct cgroup_subsys *ss;
763 for_each_subsys(cgrp->root, ss)
764 if (ss->pre_destroy) {
765 ret = ss->pre_destroy(ss, cgrp);
772 static void free_cgroup_rcu(struct rcu_head *obj)
774 struct cgroup *cgrp = container_of(obj, struct cgroup, rcu_head);
779 static void cgroup_diput(struct dentry *dentry, struct inode *inode)
781 /* is dentry a directory ? if so, kfree() associated cgroup */
782 if (S_ISDIR(inode->i_mode)) {
783 struct cgroup *cgrp = dentry->d_fsdata;
784 struct cgroup_subsys *ss;
785 BUG_ON(!(cgroup_is_removed(cgrp)));
786 /* It's possible for external users to be holding css
787 * reference counts on a cgroup; css_put() needs to
788 * be able to access the cgroup after decrementing
789 * the reference count in order to know if it needs to
790 * queue the cgroup to be handled by the release
794 mutex_lock(&cgroup_mutex);
796 * Release the subsystem state objects.
798 for_each_subsys(cgrp->root, ss)
799 ss->destroy(ss, cgrp);
801 cgrp->root->number_of_cgroups--;
802 mutex_unlock(&cgroup_mutex);
805 * Drop the active superblock reference that we took when we
808 deactivate_super(cgrp->root->sb);
811 * if we're getting rid of the cgroup, refcount should ensure
812 * that there are no pidlists left.
814 BUG_ON(!list_empty(&cgrp->pidlists));
816 call_rcu(&cgrp->rcu_head, free_cgroup_rcu);
821 static void remove_dir(struct dentry *d)
823 struct dentry *parent = dget(d->d_parent);
826 simple_rmdir(parent->d_inode, d);
830 static void cgroup_clear_directory(struct dentry *dentry)
832 struct list_head *node;
834 BUG_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
835 spin_lock(&dcache_lock);
836 node = dentry->d_subdirs.next;
837 while (node != &dentry->d_subdirs) {
838 struct dentry *d = list_entry(node, struct dentry, d_u.d_child);
841 /* This should never be called on a cgroup
842 * directory with child cgroups */
843 BUG_ON(d->d_inode->i_mode & S_IFDIR);
845 spin_unlock(&dcache_lock);
847 simple_unlink(dentry->d_inode, d);
849 spin_lock(&dcache_lock);
851 node = dentry->d_subdirs.next;
853 spin_unlock(&dcache_lock);
857 * NOTE : the dentry must have been dget()'ed
859 static void cgroup_d_remove_dir(struct dentry *dentry)
861 cgroup_clear_directory(dentry);
863 spin_lock(&dcache_lock);
864 list_del_init(&dentry->d_u.d_child);
865 spin_unlock(&dcache_lock);
870 * A queue for waiters to do rmdir() cgroup. A tasks will sleep when
871 * cgroup->count == 0 && list_empty(&cgroup->children) && subsys has some
872 * reference to css->refcnt. In general, this refcnt is expected to goes down
875 * CGRP_WAIT_ON_RMDIR flag is set under cgroup's inode->i_mutex;
877 DECLARE_WAIT_QUEUE_HEAD(cgroup_rmdir_waitq);
879 static void cgroup_wakeup_rmdir_waiter(struct cgroup *cgrp)
881 if (unlikely(test_and_clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags)))
882 wake_up_all(&cgroup_rmdir_waitq);
885 void cgroup_exclude_rmdir(struct cgroup_subsys_state *css)
890 void cgroup_release_and_wakeup_rmdir(struct cgroup_subsys_state *css)
892 cgroup_wakeup_rmdir_waiter(css->cgroup);
897 * Call with cgroup_mutex held.
899 static int rebind_subsystems(struct cgroupfs_root *root,
900 unsigned long final_bits)
902 unsigned long added_bits, removed_bits;
903 struct cgroup *cgrp = &root->top_cgroup;
906 BUG_ON(!mutex_is_locked(&cgroup_mutex));
908 removed_bits = root->actual_subsys_bits & ~final_bits;
909 added_bits = final_bits & ~root->actual_subsys_bits;
910 /* Check that any added subsystems are currently free */
911 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
912 unsigned long bit = 1UL << i;
913 struct cgroup_subsys *ss = subsys[i];
914 if (!(bit & added_bits))
917 * Nobody should tell us to do a subsys that doesn't exist:
918 * parse_cgroupfs_options should catch that case and refcounts
919 * ensure that subsystems won't disappear once selected.
922 if (ss->root != &rootnode) {
923 /* Subsystem isn't free */
928 /* Currently we don't handle adding/removing subsystems when
929 * any child cgroups exist. This is theoretically supportable
930 * but involves complex error handling, so it's being left until
932 if (root->number_of_cgroups > 1)
935 /* Process each subsystem */
936 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
937 struct cgroup_subsys *ss = subsys[i];
938 unsigned long bit = 1UL << i;
939 if (bit & added_bits) {
940 /* We're binding this subsystem to this hierarchy */
942 BUG_ON(cgrp->subsys[i]);
943 BUG_ON(!dummytop->subsys[i]);
944 BUG_ON(dummytop->subsys[i]->cgroup != dummytop);
945 mutex_lock(&ss->hierarchy_mutex);
946 cgrp->subsys[i] = dummytop->subsys[i];
947 cgrp->subsys[i]->cgroup = cgrp;
948 list_move(&ss->sibling, &root->subsys_list);
952 mutex_unlock(&ss->hierarchy_mutex);
953 } else if (bit & removed_bits) {
954 /* We're removing this subsystem */
956 BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]);
957 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
958 mutex_lock(&ss->hierarchy_mutex);
960 ss->bind(ss, dummytop);
961 dummytop->subsys[i]->cgroup = dummytop;
962 cgrp->subsys[i] = NULL;
963 subsys[i]->root = &rootnode;
964 list_move(&ss->sibling, &rootnode.subsys_list);
965 mutex_unlock(&ss->hierarchy_mutex);
966 } else if (bit & final_bits) {
967 /* Subsystem state should already exist */
969 BUG_ON(!cgrp->subsys[i]);
971 /* Subsystem state shouldn't exist */
972 BUG_ON(cgrp->subsys[i]);
975 root->subsys_bits = root->actual_subsys_bits = final_bits;
981 static int cgroup_show_options(struct seq_file *seq, struct vfsmount *vfs)
983 struct cgroupfs_root *root = vfs->mnt_sb->s_fs_info;
984 struct cgroup_subsys *ss;
986 mutex_lock(&cgroup_mutex);
987 for_each_subsys(root, ss)
988 seq_printf(seq, ",%s", ss->name);
989 if (test_bit(ROOT_NOPREFIX, &root->flags))
990 seq_puts(seq, ",noprefix");
991 if (strlen(root->release_agent_path))
992 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
993 if (strlen(root->name))
994 seq_printf(seq, ",name=%s", root->name);
995 mutex_unlock(&cgroup_mutex);
999 struct cgroup_sb_opts {
1000 unsigned long subsys_bits;
1001 unsigned long flags;
1002 char *release_agent;
1004 /* User explicitly requested empty subsystem */
1007 struct cgroupfs_root *new_root;
1012 * Convert a hierarchy specifier into a bitmask of subsystems and flags. Call
1013 * with cgroup_mutex held to protect the subsys[] array.
1015 static int parse_cgroupfs_options(char *data,
1016 struct cgroup_sb_opts *opts)
1018 char *token, *o = data ?: "all";
1019 unsigned long mask = (unsigned long)-1;
1021 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1023 #ifdef CONFIG_CPUSETS
1024 mask = ~(1UL << cpuset_subsys_id);
1027 memset(opts, 0, sizeof(*opts));
1029 while ((token = strsep(&o, ",")) != NULL) {
1032 if (!strcmp(token, "all")) {
1033 /* Add all non-disabled subsystems */
1035 opts->subsys_bits = 0;
1036 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1037 struct cgroup_subsys *ss = subsys[i];
1041 opts->subsys_bits |= 1ul << i;
1043 } else if (!strcmp(token, "none")) {
1044 /* Explicitly have no subsystems */
1046 } else if (!strcmp(token, "noprefix")) {
1047 set_bit(ROOT_NOPREFIX, &opts->flags);
1048 } else if (!strncmp(token, "release_agent=", 14)) {
1049 /* Specifying two release agents is forbidden */
1050 if (opts->release_agent)
1052 opts->release_agent =
1053 kstrndup(token + 14, PATH_MAX, GFP_KERNEL);
1054 if (!opts->release_agent)
1056 } else if (!strncmp(token, "name=", 5)) {
1058 const char *name = token + 5;
1059 /* Can't specify an empty name */
1062 /* Must match [\w.-]+ */
1063 for (i = 0; i < strlen(name); i++) {
1067 if ((c == '.') || (c == '-') || (c == '_'))
1071 /* Specifying two names is forbidden */
1074 opts->name = kstrndup(name,
1075 MAX_CGROUP_ROOT_NAMELEN,
1080 struct cgroup_subsys *ss;
1082 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1086 if (!strcmp(token, ss->name)) {
1088 set_bit(i, &opts->subsys_bits);
1092 if (i == CGROUP_SUBSYS_COUNT)
1097 /* Consistency checks */
1100 * Option noprefix was introduced just for backward compatibility
1101 * with the old cpuset, so we allow noprefix only if mounting just
1102 * the cpuset subsystem.
1104 if (test_bit(ROOT_NOPREFIX, &opts->flags) &&
1105 (opts->subsys_bits & mask))
1109 /* Can't specify "none" and some subsystems */
1110 if (opts->subsys_bits && opts->none)
1114 * We either have to specify by name or by subsystems. (So all
1115 * empty hierarchies must have a name).
1117 if (!opts->subsys_bits && !opts->name)
1123 static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1126 struct cgroupfs_root *root = sb->s_fs_info;
1127 struct cgroup *cgrp = &root->top_cgroup;
1128 struct cgroup_sb_opts opts;
1131 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
1132 mutex_lock(&cgroup_mutex);
1134 /* See what subsystems are wanted */
1135 ret = parse_cgroupfs_options(data, &opts);
1139 /* Don't allow flags to change at remount */
1140 if (opts.flags != root->flags) {
1145 /* Don't allow name to change at remount */
1146 if (opts.name && strcmp(opts.name, root->name)) {
1151 ret = rebind_subsystems(root, opts.subsys_bits);
1155 /* (re)populate subsystem files */
1156 cgroup_populate_dir(cgrp);
1158 if (opts.release_agent)
1159 strcpy(root->release_agent_path, opts.release_agent);
1161 kfree(opts.release_agent);
1163 mutex_unlock(&cgroup_mutex);
1164 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
1169 static const struct super_operations cgroup_ops = {
1170 .statfs = simple_statfs,
1171 .drop_inode = generic_delete_inode,
1172 .show_options = cgroup_show_options,
1173 .remount_fs = cgroup_remount,
1176 static void init_cgroup_housekeeping(struct cgroup *cgrp)
1178 INIT_LIST_HEAD(&cgrp->sibling);
1179 INIT_LIST_HEAD(&cgrp->children);
1180 INIT_LIST_HEAD(&cgrp->css_sets);
1181 INIT_LIST_HEAD(&cgrp->release_list);
1182 INIT_LIST_HEAD(&cgrp->pidlists);
1183 mutex_init(&cgrp->pidlist_mutex);
1186 static void init_cgroup_root(struct cgroupfs_root *root)
1188 struct cgroup *cgrp = &root->top_cgroup;
1189 INIT_LIST_HEAD(&root->subsys_list);
1190 INIT_LIST_HEAD(&root->root_list);
1191 root->number_of_cgroups = 1;
1193 cgrp->top_cgroup = cgrp;
1194 init_cgroup_housekeeping(cgrp);
1197 static bool init_root_id(struct cgroupfs_root *root)
1202 if (!ida_pre_get(&hierarchy_ida, GFP_KERNEL))
1204 spin_lock(&hierarchy_id_lock);
1205 /* Try to allocate the next unused ID */
1206 ret = ida_get_new_above(&hierarchy_ida, next_hierarchy_id,
1207 &root->hierarchy_id);
1209 /* Try again starting from 0 */
1210 ret = ida_get_new(&hierarchy_ida, &root->hierarchy_id);
1212 next_hierarchy_id = root->hierarchy_id + 1;
1213 } else if (ret != -EAGAIN) {
1214 /* Can only get here if the 31-bit IDR is full ... */
1217 spin_unlock(&hierarchy_id_lock);
1222 static int cgroup_test_super(struct super_block *sb, void *data)
1224 struct cgroup_sb_opts *opts = data;
1225 struct cgroupfs_root *root = sb->s_fs_info;
1227 /* If we asked for a name then it must match */
1228 if (opts->name && strcmp(opts->name, root->name))
1232 * If we asked for subsystems (or explicitly for no
1233 * subsystems) then they must match
1235 if ((opts->subsys_bits || opts->none)
1236 && (opts->subsys_bits != root->subsys_bits))
1242 static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1244 struct cgroupfs_root *root;
1246 if (!opts->subsys_bits && !opts->none)
1249 root = kzalloc(sizeof(*root), GFP_KERNEL);
1251 return ERR_PTR(-ENOMEM);
1253 if (!init_root_id(root)) {
1255 return ERR_PTR(-ENOMEM);
1257 init_cgroup_root(root);
1259 root->subsys_bits = opts->subsys_bits;
1260 root->flags = opts->flags;
1261 if (opts->release_agent)
1262 strcpy(root->release_agent_path, opts->release_agent);
1264 strcpy(root->name, opts->name);
1268 static void cgroup_drop_root(struct cgroupfs_root *root)
1273 BUG_ON(!root->hierarchy_id);
1274 spin_lock(&hierarchy_id_lock);
1275 ida_remove(&hierarchy_ida, root->hierarchy_id);
1276 spin_unlock(&hierarchy_id_lock);
1280 static int cgroup_set_super(struct super_block *sb, void *data)
1283 struct cgroup_sb_opts *opts = data;
1285 /* If we don't have a new root, we can't set up a new sb */
1286 if (!opts->new_root)
1289 BUG_ON(!opts->subsys_bits && !opts->none);
1291 ret = set_anon_super(sb, NULL);
1295 sb->s_fs_info = opts->new_root;
1296 opts->new_root->sb = sb;
1298 sb->s_blocksize = PAGE_CACHE_SIZE;
1299 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1300 sb->s_magic = CGROUP_SUPER_MAGIC;
1301 sb->s_op = &cgroup_ops;
1306 static int cgroup_get_rootdir(struct super_block *sb)
1308 struct inode *inode =
1309 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
1310 struct dentry *dentry;
1315 inode->i_fop = &simple_dir_operations;
1316 inode->i_op = &cgroup_dir_inode_operations;
1317 /* directories start off with i_nlink == 2 (for "." entry) */
1319 dentry = d_alloc_root(inode);
1324 sb->s_root = dentry;
1328 static int cgroup_get_sb(struct file_system_type *fs_type,
1329 int flags, const char *unused_dev_name,
1330 void *data, struct vfsmount *mnt)
1332 struct cgroup_sb_opts opts;
1333 struct cgroupfs_root *root;
1335 struct super_block *sb;
1336 struct cgroupfs_root *new_root;
1338 /* First find the desired set of subsystems */
1339 mutex_lock(&cgroup_mutex);
1340 ret = parse_cgroupfs_options(data, &opts);
1341 mutex_unlock(&cgroup_mutex);
1346 * Allocate a new cgroup root. We may not need it if we're
1347 * reusing an existing hierarchy.
1349 new_root = cgroup_root_from_opts(&opts);
1350 if (IS_ERR(new_root)) {
1351 ret = PTR_ERR(new_root);
1354 opts.new_root = new_root;
1356 /* Locate an existing or new sb for this hierarchy */
1357 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, &opts);
1360 cgroup_drop_root(opts.new_root);
1364 root = sb->s_fs_info;
1366 if (root == opts.new_root) {
1367 /* We used the new root structure, so this is a new hierarchy */
1368 struct list_head tmp_cg_links;
1369 struct cgroup *root_cgrp = &root->top_cgroup;
1370 struct inode *inode;
1371 struct cgroupfs_root *existing_root;
1374 BUG_ON(sb->s_root != NULL);
1376 ret = cgroup_get_rootdir(sb);
1378 goto drop_new_super;
1379 inode = sb->s_root->d_inode;
1381 mutex_lock(&inode->i_mutex);
1382 mutex_lock(&cgroup_mutex);
1384 if (strlen(root->name)) {
1385 /* Check for name clashes with existing mounts */
1386 for_each_active_root(existing_root) {
1387 if (!strcmp(existing_root->name, root->name)) {
1389 mutex_unlock(&cgroup_mutex);
1390 mutex_unlock(&inode->i_mutex);
1391 goto drop_new_super;
1397 * We're accessing css_set_count without locking
1398 * css_set_lock here, but that's OK - it can only be
1399 * increased by someone holding cgroup_lock, and
1400 * that's us. The worst that can happen is that we
1401 * have some link structures left over
1403 ret = allocate_cg_links(css_set_count, &tmp_cg_links);
1405 mutex_unlock(&cgroup_mutex);
1406 mutex_unlock(&inode->i_mutex);
1407 goto drop_new_super;
1410 ret = rebind_subsystems(root, root->subsys_bits);
1411 if (ret == -EBUSY) {
1412 mutex_unlock(&cgroup_mutex);
1413 mutex_unlock(&inode->i_mutex);
1414 free_cg_links(&tmp_cg_links);
1415 goto drop_new_super;
1418 /* EBUSY should be the only error here */
1421 list_add(&root->root_list, &roots);
1424 sb->s_root->d_fsdata = root_cgrp;
1425 root->top_cgroup.dentry = sb->s_root;
1427 /* Link the top cgroup in this hierarchy into all
1428 * the css_set objects */
1429 write_lock(&css_set_lock);
1430 for (i = 0; i < CSS_SET_TABLE_SIZE; i++) {
1431 struct hlist_head *hhead = &css_set_table[i];
1432 struct hlist_node *node;
1435 hlist_for_each_entry(cg, node, hhead, hlist)
1436 link_css_set(&tmp_cg_links, cg, root_cgrp);
1438 write_unlock(&css_set_lock);
1440 free_cg_links(&tmp_cg_links);
1442 BUG_ON(!list_empty(&root_cgrp->sibling));
1443 BUG_ON(!list_empty(&root_cgrp->children));
1444 BUG_ON(root->number_of_cgroups != 1);
1446 cgroup_populate_dir(root_cgrp);
1447 mutex_unlock(&cgroup_mutex);
1448 mutex_unlock(&inode->i_mutex);
1451 * We re-used an existing hierarchy - the new root (if
1452 * any) is not needed
1454 cgroup_drop_root(opts.new_root);
1457 simple_set_mnt(mnt, sb);
1458 kfree(opts.release_agent);
1463 deactivate_locked_super(sb);
1465 kfree(opts.release_agent);
1471 static void cgroup_kill_sb(struct super_block *sb) {
1472 struct cgroupfs_root *root = sb->s_fs_info;
1473 struct cgroup *cgrp = &root->top_cgroup;
1475 struct cg_cgroup_link *link;
1476 struct cg_cgroup_link *saved_link;
1480 BUG_ON(root->number_of_cgroups != 1);
1481 BUG_ON(!list_empty(&cgrp->children));
1482 BUG_ON(!list_empty(&cgrp->sibling));
1484 mutex_lock(&cgroup_mutex);
1486 /* Rebind all subsystems back to the default hierarchy */
1487 ret = rebind_subsystems(root, 0);
1488 /* Shouldn't be able to fail ... */
1492 * Release all the links from css_sets to this hierarchy's
1495 write_lock(&css_set_lock);
1497 list_for_each_entry_safe(link, saved_link, &cgrp->css_sets,
1499 list_del(&link->cg_link_list);
1500 list_del(&link->cgrp_link_list);
1503 write_unlock(&css_set_lock);
1505 if (!list_empty(&root->root_list)) {
1506 list_del(&root->root_list);
1510 mutex_unlock(&cgroup_mutex);
1512 kill_litter_super(sb);
1513 cgroup_drop_root(root);
1516 static struct file_system_type cgroup_fs_type = {
1518 .get_sb = cgroup_get_sb,
1519 .kill_sb = cgroup_kill_sb,
1522 static inline struct cgroup *__d_cgrp(struct dentry *dentry)
1524 return dentry->d_fsdata;
1527 static inline struct cftype *__d_cft(struct dentry *dentry)
1529 return dentry->d_fsdata;
1533 * cgroup_path - generate the path of a cgroup
1534 * @cgrp: the cgroup in question
1535 * @buf: the buffer to write the path into
1536 * @buflen: the length of the buffer
1538 * Called with cgroup_mutex held or else with an RCU-protected cgroup
1539 * reference. Writes path of cgroup into buf. Returns 0 on success,
1542 int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
1545 struct dentry *dentry = rcu_dereference(cgrp->dentry);
1547 if (!dentry || cgrp == dummytop) {
1549 * Inactive subsystems have no dentry for their root
1556 start = buf + buflen;
1560 int len = dentry->d_name.len;
1561 if ((start -= len) < buf)
1562 return -ENAMETOOLONG;
1563 memcpy(start, cgrp->dentry->d_name.name, len);
1564 cgrp = cgrp->parent;
1567 dentry = rcu_dereference(cgrp->dentry);
1571 return -ENAMETOOLONG;
1574 memmove(buf, start, buf + buflen - start);
1579 * cgroup_attach_task - attach task 'tsk' to cgroup 'cgrp'
1580 * @cgrp: the cgroup the task is attaching to
1581 * @tsk: the task to be attached
1583 * Call holding cgroup_mutex. May take task_lock of
1584 * the task 'tsk' during call.
1586 int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
1589 struct cgroup_subsys *ss, *failed_ss = NULL;
1590 struct cgroup *oldcgrp;
1592 struct css_set *newcg;
1593 struct cgroupfs_root *root = cgrp->root;
1595 /* Nothing to do if the task is already in that cgroup */
1596 oldcgrp = task_cgroup_from_root(tsk, root);
1597 if (cgrp == oldcgrp)
1600 for_each_subsys(root, ss) {
1601 if (ss->can_attach) {
1602 retval = ss->can_attach(ss, cgrp, tsk, false);
1605 * Remember on which subsystem the can_attach()
1606 * failed, so that we only call cancel_attach()
1607 * against the subsystems whose can_attach()
1608 * succeeded. (See below)
1621 * Locate or allocate a new css_set for this task,
1622 * based on its final set of cgroups
1624 newcg = find_css_set(cg, cgrp);
1632 if (tsk->flags & PF_EXITING) {
1638 rcu_assign_pointer(tsk->cgroups, newcg);
1641 /* Update the css_set linked lists if we're using them */
1642 write_lock(&css_set_lock);
1643 if (!list_empty(&tsk->cg_list)) {
1644 list_del(&tsk->cg_list);
1645 list_add(&tsk->cg_list, &newcg->tasks);
1647 write_unlock(&css_set_lock);
1649 for_each_subsys(root, ss) {
1651 ss->attach(ss, cgrp, oldcgrp, tsk, false);
1653 set_bit(CGRP_RELEASABLE, &oldcgrp->flags);
1658 * wake up rmdir() waiter. the rmdir should fail since the cgroup
1659 * is no longer empty.
1661 cgroup_wakeup_rmdir_waiter(cgrp);
1664 for_each_subsys(root, ss) {
1665 if (ss == failed_ss)
1667 * This subsystem was the one that failed the
1668 * can_attach() check earlier, so we don't need
1669 * to call cancel_attach() against it or any
1670 * remaining subsystems.
1673 if (ss->cancel_attach)
1674 ss->cancel_attach(ss, cgrp, tsk, false);
1681 * Attach task with pid 'pid' to cgroup 'cgrp'. Call with cgroup_mutex
1682 * held. May take task_lock of task
1684 static int attach_task_by_pid(struct cgroup *cgrp, u64 pid)
1686 struct task_struct *tsk;
1687 const struct cred *cred = current_cred(), *tcred;
1692 tsk = find_task_by_vpid(pid);
1693 if (!tsk || tsk->flags & PF_EXITING) {
1698 tcred = __task_cred(tsk);
1700 cred->euid != tcred->uid &&
1701 cred->euid != tcred->suid) {
1705 get_task_struct(tsk);
1709 get_task_struct(tsk);
1712 ret = cgroup_attach_task(cgrp, tsk);
1713 put_task_struct(tsk);
1717 static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid)
1720 if (!cgroup_lock_live_group(cgrp))
1722 ret = attach_task_by_pid(cgrp, pid);
1728 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
1729 * @cgrp: the cgroup to be checked for liveness
1731 * On success, returns true; the lock should be later released with
1732 * cgroup_unlock(). On failure returns false with no lock held.
1734 bool cgroup_lock_live_group(struct cgroup *cgrp)
1736 mutex_lock(&cgroup_mutex);
1737 if (cgroup_is_removed(cgrp)) {
1738 mutex_unlock(&cgroup_mutex);
1744 static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
1747 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
1748 if (!cgroup_lock_live_group(cgrp))
1750 strcpy(cgrp->root->release_agent_path, buffer);
1755 static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
1756 struct seq_file *seq)
1758 if (!cgroup_lock_live_group(cgrp))
1760 seq_puts(seq, cgrp->root->release_agent_path);
1761 seq_putc(seq, '\n');
1766 /* A buffer size big enough for numbers or short strings */
1767 #define CGROUP_LOCAL_BUFFER_SIZE 64
1769 static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
1771 const char __user *userbuf,
1772 size_t nbytes, loff_t *unused_ppos)
1774 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
1780 if (nbytes >= sizeof(buffer))
1782 if (copy_from_user(buffer, userbuf, nbytes))
1785 buffer[nbytes] = 0; /* nul-terminate */
1786 if (cft->write_u64) {
1787 u64 val = simple_strtoull(strstrip(buffer), &end, 0);
1790 retval = cft->write_u64(cgrp, cft, val);
1792 s64 val = simple_strtoll(strstrip(buffer), &end, 0);
1795 retval = cft->write_s64(cgrp, cft, val);
1802 static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
1804 const char __user *userbuf,
1805 size_t nbytes, loff_t *unused_ppos)
1807 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
1809 size_t max_bytes = cft->max_write_len;
1810 char *buffer = local_buffer;
1813 max_bytes = sizeof(local_buffer) - 1;
1814 if (nbytes >= max_bytes)
1816 /* Allocate a dynamic buffer if we need one */
1817 if (nbytes >= sizeof(local_buffer)) {
1818 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
1822 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
1827 buffer[nbytes] = 0; /* nul-terminate */
1828 retval = cft->write_string(cgrp, cft, strstrip(buffer));
1832 if (buffer != local_buffer)
1837 static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
1838 size_t nbytes, loff_t *ppos)
1840 struct cftype *cft = __d_cft(file->f_dentry);
1841 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
1843 if (cgroup_is_removed(cgrp))
1846 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
1847 if (cft->write_u64 || cft->write_s64)
1848 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
1849 if (cft->write_string)
1850 return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
1852 int ret = cft->trigger(cgrp, (unsigned int)cft->private);
1853 return ret ? ret : nbytes;
1858 static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
1860 char __user *buf, size_t nbytes,
1863 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
1864 u64 val = cft->read_u64(cgrp, cft);
1865 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
1867 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
1870 static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
1872 char __user *buf, size_t nbytes,
1875 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
1876 s64 val = cft->read_s64(cgrp, cft);
1877 int len = sprintf(tmp, "%lld\n", (long long) val);
1879 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
1882 static ssize_t cgroup_file_read(struct file *file, char __user *buf,
1883 size_t nbytes, loff_t *ppos)
1885 struct cftype *cft = __d_cft(file->f_dentry);
1886 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
1888 if (cgroup_is_removed(cgrp))
1892 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
1894 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
1896 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
1901 * seqfile ops/methods for returning structured data. Currently just
1902 * supports string->u64 maps, but can be extended in future.
1905 struct cgroup_seqfile_state {
1907 struct cgroup *cgroup;
1910 static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
1912 struct seq_file *sf = cb->state;
1913 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
1916 static int cgroup_seqfile_show(struct seq_file *m, void *arg)
1918 struct cgroup_seqfile_state *state = m->private;
1919 struct cftype *cft = state->cft;
1920 if (cft->read_map) {
1921 struct cgroup_map_cb cb = {
1922 .fill = cgroup_map_add,
1925 return cft->read_map(state->cgroup, cft, &cb);
1927 return cft->read_seq_string(state->cgroup, cft, m);
1930 static int cgroup_seqfile_release(struct inode *inode, struct file *file)
1932 struct seq_file *seq = file->private_data;
1933 kfree(seq->private);
1934 return single_release(inode, file);
1937 static const struct file_operations cgroup_seqfile_operations = {
1939 .write = cgroup_file_write,
1940 .llseek = seq_lseek,
1941 .release = cgroup_seqfile_release,
1944 static int cgroup_file_open(struct inode *inode, struct file *file)
1949 err = generic_file_open(inode, file);
1952 cft = __d_cft(file->f_dentry);
1954 if (cft->read_map || cft->read_seq_string) {
1955 struct cgroup_seqfile_state *state =
1956 kzalloc(sizeof(*state), GFP_USER);
1960 state->cgroup = __d_cgrp(file->f_dentry->d_parent);
1961 file->f_op = &cgroup_seqfile_operations;
1962 err = single_open(file, cgroup_seqfile_show, state);
1965 } else if (cft->open)
1966 err = cft->open(inode, file);
1973 static int cgroup_file_release(struct inode *inode, struct file *file)
1975 struct cftype *cft = __d_cft(file->f_dentry);
1977 return cft->release(inode, file);
1982 * cgroup_rename - Only allow simple rename of directories in place.
1984 static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
1985 struct inode *new_dir, struct dentry *new_dentry)
1987 if (!S_ISDIR(old_dentry->d_inode->i_mode))
1989 if (new_dentry->d_inode)
1991 if (old_dir != new_dir)
1993 return simple_rename(old_dir, old_dentry, new_dir, new_dentry);
1996 static const struct file_operations cgroup_file_operations = {
1997 .read = cgroup_file_read,
1998 .write = cgroup_file_write,
1999 .llseek = generic_file_llseek,
2000 .open = cgroup_file_open,
2001 .release = cgroup_file_release,
2004 static const struct inode_operations cgroup_dir_inode_operations = {
2005 .lookup = simple_lookup,
2006 .mkdir = cgroup_mkdir,
2007 .rmdir = cgroup_rmdir,
2008 .rename = cgroup_rename,
2011 static int cgroup_create_file(struct dentry *dentry, mode_t mode,
2012 struct super_block *sb)
2014 static const struct dentry_operations cgroup_dops = {
2015 .d_iput = cgroup_diput,
2018 struct inode *inode;
2022 if (dentry->d_inode)
2025 inode = cgroup_new_inode(mode, sb);
2029 if (S_ISDIR(mode)) {
2030 inode->i_op = &cgroup_dir_inode_operations;
2031 inode->i_fop = &simple_dir_operations;
2033 /* start off with i_nlink == 2 (for "." entry) */
2036 /* start with the directory inode held, so that we can
2037 * populate it without racing with another mkdir */
2038 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
2039 } else if (S_ISREG(mode)) {
2041 inode->i_fop = &cgroup_file_operations;
2043 dentry->d_op = &cgroup_dops;
2044 d_instantiate(dentry, inode);
2045 dget(dentry); /* Extra count - pin the dentry in core */
2050 * cgroup_create_dir - create a directory for an object.
2051 * @cgrp: the cgroup we create the directory for. It must have a valid
2052 * ->parent field. And we are going to fill its ->dentry field.
2053 * @dentry: dentry of the new cgroup
2054 * @mode: mode to set on new directory.
2056 static int cgroup_create_dir(struct cgroup *cgrp, struct dentry *dentry,
2059 struct dentry *parent;
2062 parent = cgrp->parent->dentry;
2063 error = cgroup_create_file(dentry, S_IFDIR | mode, cgrp->root->sb);
2065 dentry->d_fsdata = cgrp;
2066 inc_nlink(parent->d_inode);
2067 rcu_assign_pointer(cgrp->dentry, dentry);
2076 * cgroup_file_mode - deduce file mode of a control file
2077 * @cft: the control file in question
2079 * returns cft->mode if ->mode is not 0
2080 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2081 * returns S_IRUGO if it has only a read handler
2082 * returns S_IWUSR if it has only a write hander
2084 static mode_t cgroup_file_mode(const struct cftype *cft)
2091 if (cft->read || cft->read_u64 || cft->read_s64 ||
2092 cft->read_map || cft->read_seq_string)
2095 if (cft->write || cft->write_u64 || cft->write_s64 ||
2096 cft->write_string || cft->trigger)
2102 int cgroup_add_file(struct cgroup *cgrp,
2103 struct cgroup_subsys *subsys,
2104 const struct cftype *cft)
2106 struct dentry *dir = cgrp->dentry;
2107 struct dentry *dentry;
2111 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
2112 if (subsys && !test_bit(ROOT_NOPREFIX, &cgrp->root->flags)) {
2113 strcpy(name, subsys->name);
2116 strcat(name, cft->name);
2117 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
2118 dentry = lookup_one_len(name, dir, strlen(name));
2119 if (!IS_ERR(dentry)) {
2120 mode = cgroup_file_mode(cft);
2121 error = cgroup_create_file(dentry, mode | S_IFREG,
2124 dentry->d_fsdata = (void *)cft;
2127 error = PTR_ERR(dentry);
2130 EXPORT_SYMBOL_GPL(cgroup_add_file);
2132 int cgroup_add_files(struct cgroup *cgrp,
2133 struct cgroup_subsys *subsys,
2134 const struct cftype cft[],
2138 for (i = 0; i < count; i++) {
2139 err = cgroup_add_file(cgrp, subsys, &cft[i]);
2145 EXPORT_SYMBOL_GPL(cgroup_add_files);
2148 * cgroup_task_count - count the number of tasks in a cgroup.
2149 * @cgrp: the cgroup in question
2151 * Return the number of tasks in the cgroup.
2153 int cgroup_task_count(const struct cgroup *cgrp)
2156 struct cg_cgroup_link *link;
2158 read_lock(&css_set_lock);
2159 list_for_each_entry(link, &cgrp->css_sets, cgrp_link_list) {
2160 count += atomic_read(&link->cg->refcount);
2162 read_unlock(&css_set_lock);
2167 * Advance a list_head iterator. The iterator should be positioned at
2168 * the start of a css_set
2170 static void cgroup_advance_iter(struct cgroup *cgrp,
2171 struct cgroup_iter *it)
2173 struct list_head *l = it->cg_link;
2174 struct cg_cgroup_link *link;
2177 /* Advance to the next non-empty css_set */
2180 if (l == &cgrp->css_sets) {
2184 link = list_entry(l, struct cg_cgroup_link, cgrp_link_list);
2186 } while (list_empty(&cg->tasks));
2188 it->task = cg->tasks.next;
2192 * To reduce the fork() overhead for systems that are not actually
2193 * using their cgroups capability, we don't maintain the lists running
2194 * through each css_set to its tasks until we see the list actually
2195 * used - in other words after the first call to cgroup_iter_start().
2197 * The tasklist_lock is not held here, as do_each_thread() and
2198 * while_each_thread() are protected by RCU.
2200 static void cgroup_enable_task_cg_lists(void)
2202 struct task_struct *p, *g;
2203 write_lock(&css_set_lock);
2204 use_task_css_set_links = 1;
2205 do_each_thread(g, p) {
2208 * We should check if the process is exiting, otherwise
2209 * it will race with cgroup_exit() in that the list
2210 * entry won't be deleted though the process has exited.
2212 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
2213 list_add(&p->cg_list, &p->cgroups->tasks);
2215 } while_each_thread(g, p);
2216 write_unlock(&css_set_lock);
2219 void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
2222 * The first time anyone tries to iterate across a cgroup,
2223 * we need to enable the list linking each css_set to its
2224 * tasks, and fix up all existing tasks.
2226 if (!use_task_css_set_links)
2227 cgroup_enable_task_cg_lists();
2229 read_lock(&css_set_lock);
2230 it->cg_link = &cgrp->css_sets;
2231 cgroup_advance_iter(cgrp, it);
2234 struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
2235 struct cgroup_iter *it)
2237 struct task_struct *res;
2238 struct list_head *l = it->task;
2239 struct cg_cgroup_link *link;
2241 /* If the iterator cg is NULL, we have no tasks */
2244 res = list_entry(l, struct task_struct, cg_list);
2245 /* Advance iterator to find next entry */
2247 link = list_entry(it->cg_link, struct cg_cgroup_link, cgrp_link_list);
2248 if (l == &link->cg->tasks) {
2249 /* We reached the end of this task list - move on to
2250 * the next cg_cgroup_link */
2251 cgroup_advance_iter(cgrp, it);
2258 void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
2260 read_unlock(&css_set_lock);
2263 static inline int started_after_time(struct task_struct *t1,
2264 struct timespec *time,
2265 struct task_struct *t2)
2267 int start_diff = timespec_compare(&t1->start_time, time);
2268 if (start_diff > 0) {
2270 } else if (start_diff < 0) {
2274 * Arbitrarily, if two processes started at the same
2275 * time, we'll say that the lower pointer value
2276 * started first. Note that t2 may have exited by now
2277 * so this may not be a valid pointer any longer, but
2278 * that's fine - it still serves to distinguish
2279 * between two tasks started (effectively) simultaneously.
2286 * This function is a callback from heap_insert() and is used to order
2288 * In this case we order the heap in descending task start time.
2290 static inline int started_after(void *p1, void *p2)
2292 struct task_struct *t1 = p1;
2293 struct task_struct *t2 = p2;
2294 return started_after_time(t1, &t2->start_time, t2);
2298 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
2299 * @scan: struct cgroup_scanner containing arguments for the scan
2301 * Arguments include pointers to callback functions test_task() and
2303 * Iterate through all the tasks in a cgroup, calling test_task() for each,
2304 * and if it returns true, call process_task() for it also.
2305 * The test_task pointer may be NULL, meaning always true (select all tasks).
2306 * Effectively duplicates cgroup_iter_{start,next,end}()
2307 * but does not lock css_set_lock for the call to process_task().
2308 * The struct cgroup_scanner may be embedded in any structure of the caller's
2310 * It is guaranteed that process_task() will act on every task that
2311 * is a member of the cgroup for the duration of this call. This
2312 * function may or may not call process_task() for tasks that exit
2313 * or move to a different cgroup during the call, or are forked or
2314 * move into the cgroup during the call.
2316 * Note that test_task() may be called with locks held, and may in some
2317 * situations be called multiple times for the same task, so it should
2319 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
2320 * pre-allocated and will be used for heap operations (and its "gt" member will
2321 * be overwritten), else a temporary heap will be used (allocation of which
2322 * may cause this function to fail).
2324 int cgroup_scan_tasks(struct cgroup_scanner *scan)
2327 struct cgroup_iter it;
2328 struct task_struct *p, *dropped;
2329 /* Never dereference latest_task, since it's not refcounted */
2330 struct task_struct *latest_task = NULL;
2331 struct ptr_heap tmp_heap;
2332 struct ptr_heap *heap;
2333 struct timespec latest_time = { 0, 0 };
2336 /* The caller supplied our heap and pre-allocated its memory */
2338 heap->gt = &started_after;
2340 /* We need to allocate our own heap memory */
2342 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
2344 /* cannot allocate the heap */
2350 * Scan tasks in the cgroup, using the scanner's "test_task" callback
2351 * to determine which are of interest, and using the scanner's
2352 * "process_task" callback to process any of them that need an update.
2353 * Since we don't want to hold any locks during the task updates,
2354 * gather tasks to be processed in a heap structure.
2355 * The heap is sorted by descending task start time.
2356 * If the statically-sized heap fills up, we overflow tasks that
2357 * started later, and in future iterations only consider tasks that
2358 * started after the latest task in the previous pass. This
2359 * guarantees forward progress and that we don't miss any tasks.
2362 cgroup_iter_start(scan->cg, &it);
2363 while ((p = cgroup_iter_next(scan->cg, &it))) {
2365 * Only affect tasks that qualify per the caller's callback,
2366 * if he provided one
2368 if (scan->test_task && !scan->test_task(p, scan))
2371 * Only process tasks that started after the last task
2374 if (!started_after_time(p, &latest_time, latest_task))
2376 dropped = heap_insert(heap, p);
2377 if (dropped == NULL) {
2379 * The new task was inserted; the heap wasn't
2383 } else if (dropped != p) {
2385 * The new task was inserted, and pushed out a
2389 put_task_struct(dropped);
2392 * Else the new task was newer than anything already in
2393 * the heap and wasn't inserted
2396 cgroup_iter_end(scan->cg, &it);
2399 for (i = 0; i < heap->size; i++) {
2400 struct task_struct *q = heap->ptrs[i];
2402 latest_time = q->start_time;
2405 /* Process the task per the caller's callback */
2406 scan->process_task(q, scan);
2410 * If we had to process any tasks at all, scan again
2411 * in case some of them were in the middle of forking
2412 * children that didn't get processed.
2413 * Not the most efficient way to do it, but it avoids
2414 * having to take callback_mutex in the fork path
2418 if (heap == &tmp_heap)
2419 heap_free(&tmp_heap);
2424 * Stuff for reading the 'tasks'/'procs' files.
2426 * Reading this file can return large amounts of data if a cgroup has
2427 * *lots* of attached tasks. So it may need several calls to read(),
2428 * but we cannot guarantee that the information we produce is correct
2429 * unless we produce it entirely atomically.
2434 * The following two functions "fix" the issue where there are more pids
2435 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
2436 * TODO: replace with a kernel-wide solution to this problem
2438 #define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
2439 static void *pidlist_allocate(int count)
2441 if (PIDLIST_TOO_LARGE(count))
2442 return vmalloc(count * sizeof(pid_t));
2444 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
2446 static void pidlist_free(void *p)
2448 if (is_vmalloc_addr(p))
2453 static void *pidlist_resize(void *p, int newcount)
2456 /* note: if new alloc fails, old p will still be valid either way */
2457 if (is_vmalloc_addr(p)) {
2458 newlist = vmalloc(newcount * sizeof(pid_t));
2461 memcpy(newlist, p, newcount * sizeof(pid_t));
2464 newlist = krealloc(p, newcount * sizeof(pid_t), GFP_KERNEL);
2470 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
2471 * If the new stripped list is sufficiently smaller and there's enough memory
2472 * to allocate a new buffer, will let go of the unneeded memory. Returns the
2473 * number of unique elements.
2475 /* is the size difference enough that we should re-allocate the array? */
2476 #define PIDLIST_REALLOC_DIFFERENCE(old, new) ((old) - PAGE_SIZE >= (new))
2477 static int pidlist_uniq(pid_t **p, int length)
2484 * we presume the 0th element is unique, so i starts at 1. trivial
2485 * edge cases first; no work needs to be done for either
2487 if (length == 0 || length == 1)
2489 /* src and dest walk down the list; dest counts unique elements */
2490 for (src = 1; src < length; src++) {
2491 /* find next unique element */
2492 while (list[src] == list[src-1]) {
2497 /* dest always points to where the next unique element goes */
2498 list[dest] = list[src];
2503 * if the length difference is large enough, we want to allocate a
2504 * smaller buffer to save memory. if this fails due to out of memory,
2505 * we'll just stay with what we've got.
2507 if (PIDLIST_REALLOC_DIFFERENCE(length, dest)) {
2508 newlist = pidlist_resize(list, dest);
2515 static int cmppid(const void *a, const void *b)
2517 return *(pid_t *)a - *(pid_t *)b;
2521 * find the appropriate pidlist for our purpose (given procs vs tasks)
2522 * returns with the lock on that pidlist already held, and takes care
2523 * of the use count, or returns NULL with no locks held if we're out of
2526 static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
2527 enum cgroup_filetype type)
2529 struct cgroup_pidlist *l;
2530 /* don't need task_nsproxy() if we're looking at ourself */
2531 struct pid_namespace *ns = get_pid_ns(current->nsproxy->pid_ns);
2533 * We can't drop the pidlist_mutex before taking the l->mutex in case
2534 * the last ref-holder is trying to remove l from the list at the same
2535 * time. Holding the pidlist_mutex precludes somebody taking whichever
2536 * list we find out from under us - compare release_pid_array().
2538 mutex_lock(&cgrp->pidlist_mutex);
2539 list_for_each_entry(l, &cgrp->pidlists, links) {
2540 if (l->key.type == type && l->key.ns == ns) {
2541 /* found a matching list - drop the extra refcount */
2543 /* make sure l doesn't vanish out from under us */
2544 down_write(&l->mutex);
2545 mutex_unlock(&cgrp->pidlist_mutex);
2549 /* entry not found; create a new one */
2550 l = kmalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
2552 mutex_unlock(&cgrp->pidlist_mutex);
2556 init_rwsem(&l->mutex);
2557 down_write(&l->mutex);
2560 l->use_count = 0; /* don't increment here */
2563 list_add(&l->links, &cgrp->pidlists);
2564 mutex_unlock(&cgrp->pidlist_mutex);
2569 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
2571 static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
2572 struct cgroup_pidlist **lp)
2576 int pid, n = 0; /* used for populating the array */
2577 struct cgroup_iter it;
2578 struct task_struct *tsk;
2579 struct cgroup_pidlist *l;
2582 * If cgroup gets more users after we read count, we won't have
2583 * enough space - tough. This race is indistinguishable to the
2584 * caller from the case that the additional cgroup users didn't
2585 * show up until sometime later on.
2587 length = cgroup_task_count(cgrp);
2588 array = pidlist_allocate(length);
2591 /* now, populate the array */
2592 cgroup_iter_start(cgrp, &it);
2593 while ((tsk = cgroup_iter_next(cgrp, &it))) {
2594 if (unlikely(n == length))
2596 /* get tgid or pid for procs or tasks file respectively */
2597 if (type == CGROUP_FILE_PROCS)
2598 pid = task_tgid_vnr(tsk);
2600 pid = task_pid_vnr(tsk);
2601 if (pid > 0) /* make sure to only use valid results */
2604 cgroup_iter_end(cgrp, &it);
2606 /* now sort & (if procs) strip out duplicates */
2607 sort(array, length, sizeof(pid_t), cmppid, NULL);
2608 if (type == CGROUP_FILE_PROCS)
2609 length = pidlist_uniq(&array, length);
2610 l = cgroup_pidlist_find(cgrp, type);
2612 pidlist_free(array);
2615 /* store array, freeing old if necessary - lock already held */
2616 pidlist_free(l->list);
2620 up_write(&l->mutex);
2626 * cgroupstats_build - build and fill cgroupstats
2627 * @stats: cgroupstats to fill information into
2628 * @dentry: A dentry entry belonging to the cgroup for which stats have
2631 * Build and fill cgroupstats so that taskstats can export it to user
2634 int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
2637 struct cgroup *cgrp;
2638 struct cgroup_iter it;
2639 struct task_struct *tsk;
2642 * Validate dentry by checking the superblock operations,
2643 * and make sure it's a directory.
2645 if (dentry->d_sb->s_op != &cgroup_ops ||
2646 !S_ISDIR(dentry->d_inode->i_mode))
2650 cgrp = dentry->d_fsdata;
2652 cgroup_iter_start(cgrp, &it);
2653 while ((tsk = cgroup_iter_next(cgrp, &it))) {
2654 switch (tsk->state) {
2656 stats->nr_running++;
2658 case TASK_INTERRUPTIBLE:
2659 stats->nr_sleeping++;
2661 case TASK_UNINTERRUPTIBLE:
2662 stats->nr_uninterruptible++;
2665 stats->nr_stopped++;
2668 if (delayacct_is_task_waiting_on_io(tsk))
2669 stats->nr_io_wait++;
2673 cgroup_iter_end(cgrp, &it);
2681 * seq_file methods for the tasks/procs files. The seq_file position is the
2682 * next pid to display; the seq_file iterator is a pointer to the pid
2683 * in the cgroup->l->list array.
2686 static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
2689 * Initially we receive a position value that corresponds to
2690 * one more than the last pid shown (or 0 on the first call or
2691 * after a seek to the start). Use a binary-search to find the
2692 * next pid to display, if any
2694 struct cgroup_pidlist *l = s->private;
2695 int index = 0, pid = *pos;
2698 down_read(&l->mutex);
2700 int end = l->length;
2702 while (index < end) {
2703 int mid = (index + end) / 2;
2704 if (l->list[mid] == pid) {
2707 } else if (l->list[mid] <= pid)
2713 /* If we're off the end of the array, we're done */
2714 if (index >= l->length)
2716 /* Update the abstract position to be the actual pid that we found */
2717 iter = l->list + index;
2722 static void cgroup_pidlist_stop(struct seq_file *s, void *v)
2724 struct cgroup_pidlist *l = s->private;
2728 static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
2730 struct cgroup_pidlist *l = s->private;
2732 pid_t *end = l->list + l->length;
2734 * Advance to the next pid in the array. If this goes off the
2746 static int cgroup_pidlist_show(struct seq_file *s, void *v)
2748 return seq_printf(s, "%d\n", *(int *)v);
2752 * seq_operations functions for iterating on pidlists through seq_file -
2753 * independent of whether it's tasks or procs
2755 static const struct seq_operations cgroup_pidlist_seq_operations = {
2756 .start = cgroup_pidlist_start,
2757 .stop = cgroup_pidlist_stop,
2758 .next = cgroup_pidlist_next,
2759 .show = cgroup_pidlist_show,
2762 static void cgroup_release_pid_array(struct cgroup_pidlist *l)
2765 * the case where we're the last user of this particular pidlist will
2766 * have us remove it from the cgroup's list, which entails taking the
2767 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
2768 * pidlist_mutex, we have to take pidlist_mutex first.
2770 mutex_lock(&l->owner->pidlist_mutex);
2771 down_write(&l->mutex);
2772 BUG_ON(!l->use_count);
2773 if (!--l->use_count) {
2774 /* we're the last user if refcount is 0; remove and free */
2775 list_del(&l->links);
2776 mutex_unlock(&l->owner->pidlist_mutex);
2777 pidlist_free(l->list);
2778 put_pid_ns(l->key.ns);
2779 up_write(&l->mutex);
2783 mutex_unlock(&l->owner->pidlist_mutex);
2784 up_write(&l->mutex);
2787 static int cgroup_pidlist_release(struct inode *inode, struct file *file)
2789 struct cgroup_pidlist *l;
2790 if (!(file->f_mode & FMODE_READ))
2793 * the seq_file will only be initialized if the file was opened for
2794 * reading; hence we check if it's not null only in that case.
2796 l = ((struct seq_file *)file->private_data)->private;
2797 cgroup_release_pid_array(l);
2798 return seq_release(inode, file);
2801 static const struct file_operations cgroup_pidlist_operations = {
2803 .llseek = seq_lseek,
2804 .write = cgroup_file_write,
2805 .release = cgroup_pidlist_release,
2809 * The following functions handle opens on a file that displays a pidlist
2810 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
2813 /* helper function for the two below it */
2814 static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
2816 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
2817 struct cgroup_pidlist *l;
2820 /* Nothing to do for write-only files */
2821 if (!(file->f_mode & FMODE_READ))
2824 /* have the array populated */
2825 retval = pidlist_array_load(cgrp, type, &l);
2828 /* configure file information */
2829 file->f_op = &cgroup_pidlist_operations;
2831 retval = seq_open(file, &cgroup_pidlist_seq_operations);
2833 cgroup_release_pid_array(l);
2836 ((struct seq_file *)file->private_data)->private = l;
2839 static int cgroup_tasks_open(struct inode *unused, struct file *file)
2841 return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
2843 static int cgroup_procs_open(struct inode *unused, struct file *file)
2845 return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
2848 static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
2851 return notify_on_release(cgrp);
2854 static int cgroup_write_notify_on_release(struct cgroup *cgrp,
2858 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
2860 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
2862 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
2867 * for the common functions, 'private' gives the type of file
2869 /* for hysterical raisins, we can't put this on the older files */
2870 #define CGROUP_FILE_GENERIC_PREFIX "cgroup."
2871 static struct cftype files[] = {
2874 .open = cgroup_tasks_open,
2875 .write_u64 = cgroup_tasks_write,
2876 .release = cgroup_pidlist_release,
2877 .mode = S_IRUGO | S_IWUSR,
2880 .name = CGROUP_FILE_GENERIC_PREFIX "procs",
2881 .open = cgroup_procs_open,
2882 /* .write_u64 = cgroup_procs_write, TODO */
2883 .release = cgroup_pidlist_release,
2887 .name = "notify_on_release",
2888 .read_u64 = cgroup_read_notify_on_release,
2889 .write_u64 = cgroup_write_notify_on_release,
2893 static struct cftype cft_release_agent = {
2894 .name = "release_agent",
2895 .read_seq_string = cgroup_release_agent_show,
2896 .write_string = cgroup_release_agent_write,
2897 .max_write_len = PATH_MAX,
2900 static int cgroup_populate_dir(struct cgroup *cgrp)
2903 struct cgroup_subsys *ss;
2905 /* First clear out any existing files */
2906 cgroup_clear_directory(cgrp->dentry);
2908 err = cgroup_add_files(cgrp, NULL, files, ARRAY_SIZE(files));
2912 if (cgrp == cgrp->top_cgroup) {
2913 if ((err = cgroup_add_file(cgrp, NULL, &cft_release_agent)) < 0)
2917 for_each_subsys(cgrp->root, ss) {
2918 if (ss->populate && (err = ss->populate(ss, cgrp)) < 0)
2921 /* This cgroup is ready now */
2922 for_each_subsys(cgrp->root, ss) {
2923 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
2925 * Update id->css pointer and make this css visible from
2926 * CSS ID functions. This pointer will be dereferened
2927 * from RCU-read-side without locks.
2930 rcu_assign_pointer(css->id->css, css);
2936 static void init_cgroup_css(struct cgroup_subsys_state *css,
2937 struct cgroup_subsys *ss,
2938 struct cgroup *cgrp)
2941 atomic_set(&css->refcnt, 1);
2944 if (cgrp == dummytop)
2945 set_bit(CSS_ROOT, &css->flags);
2946 BUG_ON(cgrp->subsys[ss->subsys_id]);
2947 cgrp->subsys[ss->subsys_id] = css;
2950 static void cgroup_lock_hierarchy(struct cgroupfs_root *root)
2952 /* We need to take each hierarchy_mutex in a consistent order */
2956 * No worry about a race with rebind_subsystems that might mess up the
2957 * locking order, since both parties are under cgroup_mutex.
2959 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2960 struct cgroup_subsys *ss = subsys[i];
2963 if (ss->root == root)
2964 mutex_lock(&ss->hierarchy_mutex);
2968 static void cgroup_unlock_hierarchy(struct cgroupfs_root *root)
2972 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2973 struct cgroup_subsys *ss = subsys[i];
2976 if (ss->root == root)
2977 mutex_unlock(&ss->hierarchy_mutex);
2982 * cgroup_create - create a cgroup
2983 * @parent: cgroup that will be parent of the new cgroup
2984 * @dentry: dentry of the new cgroup
2985 * @mode: mode to set on new inode
2987 * Must be called with the mutex on the parent inode held
2989 static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
2992 struct cgroup *cgrp;
2993 struct cgroupfs_root *root = parent->root;
2995 struct cgroup_subsys *ss;
2996 struct super_block *sb = root->sb;
2998 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
3002 /* Grab a reference on the superblock so the hierarchy doesn't
3003 * get deleted on unmount if there are child cgroups. This
3004 * can be done outside cgroup_mutex, since the sb can't
3005 * disappear while someone has an open control file on the
3007 atomic_inc(&sb->s_active);
3009 mutex_lock(&cgroup_mutex);
3011 init_cgroup_housekeeping(cgrp);
3013 cgrp->parent = parent;
3014 cgrp->root = parent->root;
3015 cgrp->top_cgroup = parent->top_cgroup;
3017 if (notify_on_release(parent))
3018 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3020 for_each_subsys(root, ss) {
3021 struct cgroup_subsys_state *css = ss->create(ss, cgrp);
3027 init_cgroup_css(css, ss, cgrp);
3029 err = alloc_css_id(ss, parent, cgrp);
3033 /* At error, ->destroy() callback has to free assigned ID. */
3036 cgroup_lock_hierarchy(root);
3037 list_add(&cgrp->sibling, &cgrp->parent->children);
3038 cgroup_unlock_hierarchy(root);
3039 root->number_of_cgroups++;
3041 err = cgroup_create_dir(cgrp, dentry, mode);
3045 /* The cgroup directory was pre-locked for us */
3046 BUG_ON(!mutex_is_locked(&cgrp->dentry->d_inode->i_mutex));
3048 err = cgroup_populate_dir(cgrp);
3049 /* If err < 0, we have a half-filled directory - oh well ;) */
3051 mutex_unlock(&cgroup_mutex);
3052 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
3058 cgroup_lock_hierarchy(root);
3059 list_del(&cgrp->sibling);
3060 cgroup_unlock_hierarchy(root);
3061 root->number_of_cgroups--;
3065 for_each_subsys(root, ss) {
3066 if (cgrp->subsys[ss->subsys_id])
3067 ss->destroy(ss, cgrp);
3070 mutex_unlock(&cgroup_mutex);
3072 /* Release the reference count that we took on the superblock */
3073 deactivate_super(sb);
3079 static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode)
3081 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
3083 /* the vfs holds inode->i_mutex already */
3084 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
3087 static int cgroup_has_css_refs(struct cgroup *cgrp)
3089 /* Check the reference count on each subsystem. Since we
3090 * already established that there are no tasks in the
3091 * cgroup, if the css refcount is also 1, then there should
3092 * be no outstanding references, so the subsystem is safe to
3093 * destroy. We scan across all subsystems rather than using
3094 * the per-hierarchy linked list of mounted subsystems since
3095 * we can be called via check_for_release() with no
3096 * synchronization other than RCU, and the subsystem linked
3097 * list isn't RCU-safe */
3100 * We won't need to lock the subsys array, because the subsystems
3101 * we're concerned about aren't going anywhere since our cgroup root
3102 * has a reference on them.
3104 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3105 struct cgroup_subsys *ss = subsys[i];
3106 struct cgroup_subsys_state *css;
3107 /* Skip subsystems not present or not in this hierarchy */
3108 if (ss == NULL || ss->root != cgrp->root)
3110 css = cgrp->subsys[ss->subsys_id];
3111 /* When called from check_for_release() it's possible
3112 * that by this point the cgroup has been removed
3113 * and the css deleted. But a false-positive doesn't
3114 * matter, since it can only happen if the cgroup
3115 * has been deleted and hence no longer needs the
3116 * release agent to be called anyway. */
3117 if (css && (atomic_read(&css->refcnt) > 1))
3124 * Atomically mark all (or else none) of the cgroup's CSS objects as
3125 * CSS_REMOVED. Return true on success, or false if the cgroup has
3126 * busy subsystems. Call with cgroup_mutex held
3129 static int cgroup_clear_css_refs(struct cgroup *cgrp)
3131 struct cgroup_subsys *ss;
3132 unsigned long flags;
3133 bool failed = false;
3134 local_irq_save(flags);
3135 for_each_subsys(cgrp->root, ss) {
3136 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
3139 /* We can only remove a CSS with a refcnt==1 */
3140 refcnt = atomic_read(&css->refcnt);
3147 * Drop the refcnt to 0 while we check other
3148 * subsystems. This will cause any racing
3149 * css_tryget() to spin until we set the
3150 * CSS_REMOVED bits or abort
3152 if (atomic_cmpxchg(&css->refcnt, refcnt, 0) == refcnt)
3158 for_each_subsys(cgrp->root, ss) {
3159 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
3162 * Restore old refcnt if we previously managed
3163 * to clear it from 1 to 0
3165 if (!atomic_read(&css->refcnt))
3166 atomic_set(&css->refcnt, 1);
3168 /* Commit the fact that the CSS is removed */
3169 set_bit(CSS_REMOVED, &css->flags);
3172 local_irq_restore(flags);
3176 static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
3178 struct cgroup *cgrp = dentry->d_fsdata;
3180 struct cgroup *parent;
3184 /* the vfs holds both inode->i_mutex already */
3186 mutex_lock(&cgroup_mutex);
3187 if (atomic_read(&cgrp->count) != 0) {
3188 mutex_unlock(&cgroup_mutex);
3191 if (!list_empty(&cgrp->children)) {
3192 mutex_unlock(&cgroup_mutex);
3195 mutex_unlock(&cgroup_mutex);
3198 * In general, subsystem has no css->refcnt after pre_destroy(). But
3199 * in racy cases, subsystem may have to get css->refcnt after
3200 * pre_destroy() and it makes rmdir return with -EBUSY. This sometimes
3201 * make rmdir return -EBUSY too often. To avoid that, we use waitqueue
3202 * for cgroup's rmdir. CGRP_WAIT_ON_RMDIR is for synchronizing rmdir
3203 * and subsystem's reference count handling. Please see css_get/put
3204 * and css_tryget() and cgroup_wakeup_rmdir_waiter() implementation.
3206 set_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
3209 * Call pre_destroy handlers of subsys. Notify subsystems
3210 * that rmdir() request comes.
3212 ret = cgroup_call_pre_destroy(cgrp);
3214 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
3218 mutex_lock(&cgroup_mutex);
3219 parent = cgrp->parent;
3220 if (atomic_read(&cgrp->count) || !list_empty(&cgrp->children)) {
3221 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
3222 mutex_unlock(&cgroup_mutex);
3225 prepare_to_wait(&cgroup_rmdir_waitq, &wait, TASK_INTERRUPTIBLE);
3226 if (!cgroup_clear_css_refs(cgrp)) {
3227 mutex_unlock(&cgroup_mutex);
3229 * Because someone may call cgroup_wakeup_rmdir_waiter() before
3230 * prepare_to_wait(), we need to check this flag.
3232 if (test_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags))
3234 finish_wait(&cgroup_rmdir_waitq, &wait);
3235 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
3236 if (signal_pending(current))
3240 /* NO css_tryget() can success after here. */
3241 finish_wait(&cgroup_rmdir_waitq, &wait);
3242 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
3244 spin_lock(&release_list_lock);
3245 set_bit(CGRP_REMOVED, &cgrp->flags);
3246 if (!list_empty(&cgrp->release_list))
3247 list_del(&cgrp->release_list);
3248 spin_unlock(&release_list_lock);
3250 cgroup_lock_hierarchy(cgrp->root);
3251 /* delete this cgroup from parent->children */
3252 list_del(&cgrp->sibling);
3253 cgroup_unlock_hierarchy(cgrp->root);
3255 spin_lock(&cgrp->dentry->d_lock);
3256 d = dget(cgrp->dentry);
3257 spin_unlock(&d->d_lock);
3259 cgroup_d_remove_dir(d);
3262 set_bit(CGRP_RELEASABLE, &parent->flags);
3263 check_for_release(parent);
3265 mutex_unlock(&cgroup_mutex);
3269 static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
3271 struct cgroup_subsys_state *css;
3273 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
3275 /* Create the top cgroup state for this subsystem */
3276 list_add(&ss->sibling, &rootnode.subsys_list);
3277 ss->root = &rootnode;
3278 css = ss->create(ss, dummytop);
3279 /* We don't handle early failures gracefully */
3280 BUG_ON(IS_ERR(css));
3281 init_cgroup_css(css, ss, dummytop);
3283 /* Update the init_css_set to contain a subsys
3284 * pointer to this state - since the subsystem is
3285 * newly registered, all tasks and hence the
3286 * init_css_set is in the subsystem's top cgroup. */
3287 init_css_set.subsys[ss->subsys_id] = dummytop->subsys[ss->subsys_id];
3289 need_forkexit_callback |= ss->fork || ss->exit;
3291 /* At system boot, before all subsystems have been
3292 * registered, no tasks have been forked, so we don't
3293 * need to invoke fork callbacks here. */
3294 BUG_ON(!list_empty(&init_task.tasks));
3296 mutex_init(&ss->hierarchy_mutex);
3297 lockdep_set_class(&ss->hierarchy_mutex, &ss->subsys_key);
3300 /* this function shouldn't be used with modular subsystems, since they
3301 * need to register a subsys_id, among other things */
3306 * cgroup_load_subsys: load and register a modular subsystem at runtime
3307 * @ss: the subsystem to load
3309 * This function should be called in a modular subsystem's initcall. If the
3310 * subsytem is built as a module, it will be assigned a new subsys_id and set
3311 * up for use. If the subsystem is built-in anyway, work is delegated to the
3312 * simpler cgroup_init_subsys.
3314 int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
3317 struct cgroup_subsys_state *css;
3319 /* check name and function validity */
3320 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
3321 ss->create == NULL || ss->destroy == NULL)
3325 * we don't support callbacks in modular subsystems. this check is
3326 * before the ss->module check for consistency; a subsystem that could
3327 * be a module should still have no callbacks even if the user isn't
3328 * compiling it as one.
3330 if (ss->fork || ss->exit)
3334 * an optionally modular subsystem is built-in: we want to do nothing,
3335 * since cgroup_init_subsys will have already taken care of it.
3337 if (ss->module == NULL) {
3338 /* a few sanity checks */
3339 BUG_ON(ss->subsys_id >= CGROUP_BUILTIN_SUBSYS_COUNT);
3340 BUG_ON(subsys[ss->subsys_id] != ss);
3345 * need to register a subsys id before anything else - for example,
3346 * init_cgroup_css needs it.
3348 mutex_lock(&cgroup_mutex);
3349 /* find the first empty slot in the array */
3350 for (i = CGROUP_BUILTIN_SUBSYS_COUNT; i < CGROUP_SUBSYS_COUNT; i++) {
3351 if (subsys[i] == NULL)
3354 if (i == CGROUP_SUBSYS_COUNT) {
3355 /* maximum number of subsystems already registered! */
3356 mutex_unlock(&cgroup_mutex);
3359 /* assign ourselves the subsys_id */
3364 * no ss->create seems to need anything important in the ss struct, so
3365 * this can happen first (i.e. before the rootnode attachment).
3367 css = ss->create(ss, dummytop);
3369 /* failure case - need to deassign the subsys[] slot. */
3371 mutex_unlock(&cgroup_mutex);
3372 return PTR_ERR(css);
3375 list_add(&ss->sibling, &rootnode.subsys_list);
3376 ss->root = &rootnode;
3378 /* our new subsystem will be attached to the dummy hierarchy. */
3379 init_cgroup_css(css, ss, dummytop);
3380 /* init_idr must be after init_cgroup_css because it sets css->id. */
3382 int ret = cgroup_init_idr(ss, css);
3384 dummytop->subsys[ss->subsys_id] = NULL;
3385 ss->destroy(ss, dummytop);
3387 mutex_unlock(&cgroup_mutex);
3393 * Now we need to entangle the css into the existing css_sets. unlike
3394 * in cgroup_init_subsys, there are now multiple css_sets, so each one
3395 * will need a new pointer to it; done by iterating the css_set_table.
3396 * furthermore, modifying the existing css_sets will corrupt the hash
3397 * table state, so each changed css_set will need its hash recomputed.
3398 * this is all done under the css_set_lock.
3400 write_lock(&css_set_lock);
3401 for (i = 0; i < CSS_SET_TABLE_SIZE; i++) {
3403 struct hlist_node *node, *tmp;
3404 struct hlist_head *bucket = &css_set_table[i], *new_bucket;
3406 hlist_for_each_entry_safe(cg, node, tmp, bucket, hlist) {
3407 /* skip entries that we already rehashed */
3408 if (cg->subsys[ss->subsys_id])
3410 /* remove existing entry */
3411 hlist_del(&cg->hlist);
3413 cg->subsys[ss->subsys_id] = css;
3414 /* recompute hash and restore entry */
3415 new_bucket = css_set_hash(cg->subsys);
3416 hlist_add_head(&cg->hlist, new_bucket);
3419 write_unlock(&css_set_lock);
3421 mutex_init(&ss->hierarchy_mutex);
3422 lockdep_set_class(&ss->hierarchy_mutex, &ss->subsys_key);
3426 * pin the subsystem's module so it doesn't go away. this shouldn't
3427 * fail, since the module's initcall calls us.
3428 * TODO: with module unloading, move this elsewhere
3430 BUG_ON(!try_module_get(ss->module));
3433 mutex_unlock(&cgroup_mutex);
3436 EXPORT_SYMBOL_GPL(cgroup_load_subsys);
3439 * cgroup_init_early - cgroup initialization at system boot
3441 * Initialize cgroups at system boot, and initialize any
3442 * subsystems that request early init.
3444 int __init cgroup_init_early(void)
3447 atomic_set(&init_css_set.refcount, 1);
3448 INIT_LIST_HEAD(&init_css_set.cg_links);
3449 INIT_LIST_HEAD(&init_css_set.tasks);
3450 INIT_HLIST_NODE(&init_css_set.hlist);
3452 init_cgroup_root(&rootnode);
3454 init_task.cgroups = &init_css_set;
3456 init_css_set_link.cg = &init_css_set;
3457 init_css_set_link.cgrp = dummytop;
3458 list_add(&init_css_set_link.cgrp_link_list,
3459 &rootnode.top_cgroup.css_sets);
3460 list_add(&init_css_set_link.cg_link_list,
3461 &init_css_set.cg_links);
3463 for (i = 0; i < CSS_SET_TABLE_SIZE; i++)
3464 INIT_HLIST_HEAD(&css_set_table[i]);
3466 /* at bootup time, we don't worry about modular subsystems */
3467 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
3468 struct cgroup_subsys *ss = subsys[i];
3471 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
3472 BUG_ON(!ss->create);
3473 BUG_ON(!ss->destroy);
3474 if (ss->subsys_id != i) {
3475 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
3476 ss->name, ss->subsys_id);
3481 cgroup_init_subsys(ss);
3487 * cgroup_init - cgroup initialization
3489 * Register cgroup filesystem and /proc file, and initialize
3490 * any subsystems that didn't request early init.
3492 int __init cgroup_init(void)
3496 struct hlist_head *hhead;
3498 err = bdi_init(&cgroup_backing_dev_info);
3502 /* at bootup time, we don't worry about modular subsystems */
3503 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
3504 struct cgroup_subsys *ss = subsys[i];
3505 if (!ss->early_init)
3506 cgroup_init_subsys(ss);
3508 cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]);
3511 /* Add init_css_set to the hash table */
3512 hhead = css_set_hash(init_css_set.subsys);
3513 hlist_add_head(&init_css_set.hlist, hhead);
3514 BUG_ON(!init_root_id(&rootnode));
3515 err = register_filesystem(&cgroup_fs_type);
3519 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
3523 bdi_destroy(&cgroup_backing_dev_info);
3529 * proc_cgroup_show()
3530 * - Print task's cgroup paths into seq_file, one line for each hierarchy
3531 * - Used for /proc/<pid>/cgroup.
3532 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
3533 * doesn't really matter if tsk->cgroup changes after we read it,
3534 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
3535 * anyway. No need to check that tsk->cgroup != NULL, thanks to
3536 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
3537 * cgroup to top_cgroup.
3540 /* TODO: Use a proper seq_file iterator */
3541 static int proc_cgroup_show(struct seq_file *m, void *v)
3544 struct task_struct *tsk;
3547 struct cgroupfs_root *root;
3550 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3556 tsk = get_pid_task(pid, PIDTYPE_PID);
3562 mutex_lock(&cgroup_mutex);
3564 for_each_active_root(root) {
3565 struct cgroup_subsys *ss;
3566 struct cgroup *cgrp;
3569 seq_printf(m, "%d:", root->hierarchy_id);
3570 for_each_subsys(root, ss)
3571 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
3572 if (strlen(root->name))
3573 seq_printf(m, "%sname=%s", count ? "," : "",
3576 cgrp = task_cgroup_from_root(tsk, root);
3577 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
3585 mutex_unlock(&cgroup_mutex);
3586 put_task_struct(tsk);
3593 static int cgroup_open(struct inode *inode, struct file *file)
3595 struct pid *pid = PROC_I(inode)->pid;
3596 return single_open(file, proc_cgroup_show, pid);
3599 const struct file_operations proc_cgroup_operations = {
3600 .open = cgroup_open,
3602 .llseek = seq_lseek,
3603 .release = single_release,
3606 /* Display information about each subsystem and each hierarchy */
3607 static int proc_cgroupstats_show(struct seq_file *m, void *v)
3611 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
3613 * ideally we don't want subsystems moving around while we do this.
3614 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
3615 * subsys/hierarchy state.
3617 mutex_lock(&cgroup_mutex);
3618 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3619 struct cgroup_subsys *ss = subsys[i];
3622 seq_printf(m, "%s\t%d\t%d\t%d\n",
3623 ss->name, ss->root->hierarchy_id,
3624 ss->root->number_of_cgroups, !ss->disabled);
3626 mutex_unlock(&cgroup_mutex);
3630 static int cgroupstats_open(struct inode *inode, struct file *file)
3632 return single_open(file, proc_cgroupstats_show, NULL);
3635 static const struct file_operations proc_cgroupstats_operations = {
3636 .open = cgroupstats_open,
3638 .llseek = seq_lseek,
3639 .release = single_release,
3643 * cgroup_fork - attach newly forked task to its parents cgroup.
3644 * @child: pointer to task_struct of forking parent process.
3646 * Description: A task inherits its parent's cgroup at fork().
3648 * A pointer to the shared css_set was automatically copied in
3649 * fork.c by dup_task_struct(). However, we ignore that copy, since
3650 * it was not made under the protection of RCU or cgroup_mutex, so
3651 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
3652 * have already changed current->cgroups, allowing the previously
3653 * referenced cgroup group to be removed and freed.
3655 * At the point that cgroup_fork() is called, 'current' is the parent
3656 * task, and the passed argument 'child' points to the child task.
3658 void cgroup_fork(struct task_struct *child)
3661 child->cgroups = current->cgroups;
3662 get_css_set(child->cgroups);
3663 task_unlock(current);
3664 INIT_LIST_HEAD(&child->cg_list);
3668 * cgroup_fork_callbacks - run fork callbacks
3669 * @child: the new task
3671 * Called on a new task very soon before adding it to the
3672 * tasklist. No need to take any locks since no-one can
3673 * be operating on this task.
3675 void cgroup_fork_callbacks(struct task_struct *child)
3677 if (need_forkexit_callback) {
3680 * forkexit callbacks are only supported for builtin
3681 * subsystems, and the builtin section of the subsys array is
3682 * immutable, so we don't need to lock the subsys array here.
3684 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
3685 struct cgroup_subsys *ss = subsys[i];
3687 ss->fork(ss, child);
3693 * cgroup_post_fork - called on a new task after adding it to the task list
3694 * @child: the task in question
3696 * Adds the task to the list running through its css_set if necessary.
3697 * Has to be after the task is visible on the task list in case we race
3698 * with the first call to cgroup_iter_start() - to guarantee that the
3699 * new task ends up on its list.
3701 void cgroup_post_fork(struct task_struct *child)
3703 if (use_task_css_set_links) {
3704 write_lock(&css_set_lock);
3706 if (list_empty(&child->cg_list))
3707 list_add(&child->cg_list, &child->cgroups->tasks);
3709 write_unlock(&css_set_lock);
3713 * cgroup_exit - detach cgroup from exiting task
3714 * @tsk: pointer to task_struct of exiting process
3715 * @run_callback: run exit callbacks?
3717 * Description: Detach cgroup from @tsk and release it.
3719 * Note that cgroups marked notify_on_release force every task in
3720 * them to take the global cgroup_mutex mutex when exiting.
3721 * This could impact scaling on very large systems. Be reluctant to
3722 * use notify_on_release cgroups where very high task exit scaling
3723 * is required on large systems.
3725 * the_top_cgroup_hack:
3727 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
3729 * We call cgroup_exit() while the task is still competent to
3730 * handle notify_on_release(), then leave the task attached to the
3731 * root cgroup in each hierarchy for the remainder of its exit.
3733 * To do this properly, we would increment the reference count on
3734 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
3735 * code we would add a second cgroup function call, to drop that
3736 * reference. This would just create an unnecessary hot spot on
3737 * the top_cgroup reference count, to no avail.
3739 * Normally, holding a reference to a cgroup without bumping its
3740 * count is unsafe. The cgroup could go away, or someone could
3741 * attach us to a different cgroup, decrementing the count on
3742 * the first cgroup that we never incremented. But in this case,
3743 * top_cgroup isn't going away, and either task has PF_EXITING set,
3744 * which wards off any cgroup_attach_task() attempts, or task is a failed
3745 * fork, never visible to cgroup_attach_task.
3747 void cgroup_exit(struct task_struct *tsk, int run_callbacks)
3752 if (run_callbacks && need_forkexit_callback) {
3754 * modular subsystems can't use callbacks, so no need to lock
3757 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
3758 struct cgroup_subsys *ss = subsys[i];
3765 * Unlink from the css_set task list if necessary.
3766 * Optimistically check cg_list before taking
3769 if (!list_empty(&tsk->cg_list)) {
3770 write_lock(&css_set_lock);
3771 if (!list_empty(&tsk->cg_list))
3772 list_del(&tsk->cg_list);
3773 write_unlock(&css_set_lock);
3776 /* Reassign the task to the init_css_set. */
3779 tsk->cgroups = &init_css_set;
3782 put_css_set_taskexit(cg);
3786 * cgroup_clone - clone the cgroup the given subsystem is attached to
3787 * @tsk: the task to be moved
3788 * @subsys: the given subsystem
3789 * @nodename: the name for the new cgroup
3791 * Duplicate the current cgroup in the hierarchy that the given
3792 * subsystem is attached to, and move this task into the new
3795 int cgroup_clone(struct task_struct *tsk, struct cgroup_subsys *subsys,
3798 struct dentry *dentry;
3800 struct cgroup *parent, *child;
3801 struct inode *inode;
3803 struct cgroupfs_root *root;
3804 struct cgroup_subsys *ss;
3806 /* We shouldn't be called by an unregistered subsystem */
3807 BUG_ON(!subsys->active);
3809 /* First figure out what hierarchy and cgroup we're dealing
3810 * with, and pin them so we can drop cgroup_mutex */
3811 mutex_lock(&cgroup_mutex);
3813 root = subsys->root;
3814 if (root == &rootnode) {
3815 mutex_unlock(&cgroup_mutex);
3819 /* Pin the hierarchy */
3820 if (!atomic_inc_not_zero(&root->sb->s_active)) {
3821 /* We race with the final deactivate_super() */
3822 mutex_unlock(&cgroup_mutex);
3826 /* Keep the cgroup alive */
3828 parent = task_cgroup(tsk, subsys->subsys_id);
3833 mutex_unlock(&cgroup_mutex);
3835 /* Now do the VFS work to create a cgroup */
3836 inode = parent->dentry->d_inode;
3838 /* Hold the parent directory mutex across this operation to
3839 * stop anyone else deleting the new cgroup */
3840 mutex_lock(&inode->i_mutex);
3841 dentry = lookup_one_len(nodename, parent->dentry, strlen(nodename));
3842 if (IS_ERR(dentry)) {
3844 "cgroup: Couldn't allocate dentry for %s: %ld\n", nodename,
3846 ret = PTR_ERR(dentry);
3850 /* Create the cgroup directory, which also creates the cgroup */
3851 ret = vfs_mkdir(inode, dentry, 0755);
3852 child = __d_cgrp(dentry);
3856 "Failed to create cgroup %s: %d\n", nodename,
3861 /* The cgroup now exists. Retake cgroup_mutex and check
3862 * that we're still in the same state that we thought we
3864 mutex_lock(&cgroup_mutex);
3865 if ((root != subsys->root) ||
3866 (parent != task_cgroup(tsk, subsys->subsys_id))) {
3867 /* Aargh, we raced ... */
3868 mutex_unlock(&inode->i_mutex);
3871 deactivate_super(root->sb);
3872 /* The cgroup is still accessible in the VFS, but
3873 * we're not going to try to rmdir() it at this
3876 "Race in cgroup_clone() - leaking cgroup %s\n",
3881 /* do any required auto-setup */
3882 for_each_subsys(root, ss) {
3884 ss->post_clone(ss, child);
3887 /* All seems fine. Finish by moving the task into the new cgroup */
3888 ret = cgroup_attach_task(child, tsk);
3889 mutex_unlock(&cgroup_mutex);
3892 mutex_unlock(&inode->i_mutex);
3894 mutex_lock(&cgroup_mutex);
3896 mutex_unlock(&cgroup_mutex);
3897 deactivate_super(root->sb);
3902 * cgroup_is_descendant - see if @cgrp is a descendant of @task's cgrp
3903 * @cgrp: the cgroup in question
3904 * @task: the task in question
3906 * See if @cgrp is a descendant of @task's cgroup in the appropriate
3909 * If we are sending in dummytop, then presumably we are creating
3910 * the top cgroup in the subsystem.
3912 * Called only by the ns (nsproxy) cgroup.
3914 int cgroup_is_descendant(const struct cgroup *cgrp, struct task_struct *task)
3917 struct cgroup *target;
3919 if (cgrp == dummytop)
3922 target = task_cgroup_from_root(task, cgrp->root);
3923 while (cgrp != target && cgrp!= cgrp->top_cgroup)
3924 cgrp = cgrp->parent;
3925 ret = (cgrp == target);
3929 static void check_for_release(struct cgroup *cgrp)
3931 /* All of these checks rely on RCU to keep the cgroup
3932 * structure alive */
3933 if (cgroup_is_releasable(cgrp) && !atomic_read(&cgrp->count)
3934 && list_empty(&cgrp->children) && !cgroup_has_css_refs(cgrp)) {
3935 /* Control Group is currently removeable. If it's not
3936 * already queued for a userspace notification, queue
3938 int need_schedule_work = 0;
3939 spin_lock(&release_list_lock);
3940 if (!cgroup_is_removed(cgrp) &&
3941 list_empty(&cgrp->release_list)) {
3942 list_add(&cgrp->release_list, &release_list);
3943 need_schedule_work = 1;
3945 spin_unlock(&release_list_lock);
3946 if (need_schedule_work)
3947 schedule_work(&release_agent_work);
3951 /* Caller must verify that the css is not for root cgroup */
3952 void __css_put(struct cgroup_subsys_state *css, int count)
3954 struct cgroup *cgrp = css->cgroup;
3957 val = atomic_sub_return(count, &css->refcnt);
3959 if (notify_on_release(cgrp)) {
3960 set_bit(CGRP_RELEASABLE, &cgrp->flags);
3961 check_for_release(cgrp);
3963 cgroup_wakeup_rmdir_waiter(cgrp);
3966 WARN_ON_ONCE(val < 1);
3970 * Notify userspace when a cgroup is released, by running the
3971 * configured release agent with the name of the cgroup (path
3972 * relative to the root of cgroup file system) as the argument.
3974 * Most likely, this user command will try to rmdir this cgroup.
3976 * This races with the possibility that some other task will be
3977 * attached to this cgroup before it is removed, or that some other
3978 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
3979 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
3980 * unused, and this cgroup will be reprieved from its death sentence,
3981 * to continue to serve a useful existence. Next time it's released,
3982 * we will get notified again, if it still has 'notify_on_release' set.
3984 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
3985 * means only wait until the task is successfully execve()'d. The
3986 * separate release agent task is forked by call_usermodehelper(),
3987 * then control in this thread returns here, without waiting for the
3988 * release agent task. We don't bother to wait because the caller of
3989 * this routine has no use for the exit status of the release agent
3990 * task, so no sense holding our caller up for that.
3992 static void cgroup_release_agent(struct work_struct *work)
3994 BUG_ON(work != &release_agent_work);
3995 mutex_lock(&cgroup_mutex);
3996 spin_lock(&release_list_lock);
3997 while (!list_empty(&release_list)) {
3998 char *argv[3], *envp[3];
4000 char *pathbuf = NULL, *agentbuf = NULL;
4001 struct cgroup *cgrp = list_entry(release_list.next,
4004 list_del_init(&cgrp->release_list);
4005 spin_unlock(&release_list_lock);
4006 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4009 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
4011 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
4016 argv[i++] = agentbuf;
4017 argv[i++] = pathbuf;
4021 /* minimal command environment */
4022 envp[i++] = "HOME=/";
4023 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
4026 /* Drop the lock while we invoke the usermode helper,
4027 * since the exec could involve hitting disk and hence
4028 * be a slow process */
4029 mutex_unlock(&cgroup_mutex);
4030 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
4031 mutex_lock(&cgroup_mutex);
4035 spin_lock(&release_list_lock);
4037 spin_unlock(&release_list_lock);
4038 mutex_unlock(&cgroup_mutex);
4041 static int __init cgroup_disable(char *str)
4046 while ((token = strsep(&str, ",")) != NULL) {
4050 * cgroup_disable, being at boot time, can't know about module
4051 * subsystems, so we don't worry about them.
4053 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
4054 struct cgroup_subsys *ss = subsys[i];
4056 if (!strcmp(token, ss->name)) {
4058 printk(KERN_INFO "Disabling %s control group"
4059 " subsystem\n", ss->name);
4066 __setup("cgroup_disable=", cgroup_disable);
4069 * Functons for CSS ID.
4073 *To get ID other than 0, this should be called when !cgroup_is_removed().
4075 unsigned short css_id(struct cgroup_subsys_state *css)
4077 struct css_id *cssid = rcu_dereference(css->id);
4084 unsigned short css_depth(struct cgroup_subsys_state *css)
4086 struct css_id *cssid = rcu_dereference(css->id);
4089 return cssid->depth;
4093 bool css_is_ancestor(struct cgroup_subsys_state *child,
4094 const struct cgroup_subsys_state *root)
4096 struct css_id *child_id = rcu_dereference(child->id);
4097 struct css_id *root_id = rcu_dereference(root->id);
4099 if (!child_id || !root_id || (child_id->depth < root_id->depth))
4101 return child_id->stack[root_id->depth] == root_id->id;
4104 static void __free_css_id_cb(struct rcu_head *head)
4108 id = container_of(head, struct css_id, rcu_head);
4112 void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
4114 struct css_id *id = css->id;
4115 /* When this is called before css_id initialization, id can be NULL */
4119 BUG_ON(!ss->use_id);
4121 rcu_assign_pointer(id->css, NULL);
4122 rcu_assign_pointer(css->id, NULL);
4123 spin_lock(&ss->id_lock);
4124 idr_remove(&ss->idr, id->id);
4125 spin_unlock(&ss->id_lock);
4126 call_rcu(&id->rcu_head, __free_css_id_cb);
4130 * This is called by init or create(). Then, calls to this function are
4131 * always serialized (By cgroup_mutex() at create()).
4134 static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
4136 struct css_id *newid;
4137 int myid, error, size;
4139 BUG_ON(!ss->use_id);
4141 size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
4142 newid = kzalloc(size, GFP_KERNEL);
4144 return ERR_PTR(-ENOMEM);
4146 if (unlikely(!idr_pre_get(&ss->idr, GFP_KERNEL))) {
4150 spin_lock(&ss->id_lock);
4151 /* Don't use 0. allocates an ID of 1-65535 */
4152 error = idr_get_new_above(&ss->idr, newid, 1, &myid);
4153 spin_unlock(&ss->id_lock);
4155 /* Returns error when there are no free spaces for new ID.*/
4160 if (myid > CSS_ID_MAX)
4164 newid->depth = depth;
4168 spin_lock(&ss->id_lock);
4169 idr_remove(&ss->idr, myid);
4170 spin_unlock(&ss->id_lock);
4173 return ERR_PTR(error);
4177 static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss,
4178 struct cgroup_subsys_state *rootcss)
4180 struct css_id *newid;
4182 spin_lock_init(&ss->id_lock);
4185 newid = get_new_cssid(ss, 0);
4187 return PTR_ERR(newid);
4189 newid->stack[0] = newid->id;
4190 newid->css = rootcss;
4191 rootcss->id = newid;
4195 static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent,
4196 struct cgroup *child)
4198 int subsys_id, i, depth = 0;
4199 struct cgroup_subsys_state *parent_css, *child_css;
4200 struct css_id *child_id, *parent_id = NULL;
4202 subsys_id = ss->subsys_id;
4203 parent_css = parent->subsys[subsys_id];
4204 child_css = child->subsys[subsys_id];
4205 depth = css_depth(parent_css) + 1;
4206 parent_id = parent_css->id;
4208 child_id = get_new_cssid(ss, depth);
4209 if (IS_ERR(child_id))
4210 return PTR_ERR(child_id);
4212 for (i = 0; i < depth; i++)
4213 child_id->stack[i] = parent_id->stack[i];
4214 child_id->stack[depth] = child_id->id;
4216 * child_id->css pointer will be set after this cgroup is available
4217 * see cgroup_populate_dir()
4219 rcu_assign_pointer(child_css->id, child_id);
4225 * css_lookup - lookup css by id
4226 * @ss: cgroup subsys to be looked into.
4229 * Returns pointer to cgroup_subsys_state if there is valid one with id.
4230 * NULL if not. Should be called under rcu_read_lock()
4232 struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
4234 struct css_id *cssid = NULL;
4236 BUG_ON(!ss->use_id);
4237 cssid = idr_find(&ss->idr, id);
4239 if (unlikely(!cssid))
4242 return rcu_dereference(cssid->css);
4246 * css_get_next - lookup next cgroup under specified hierarchy.
4247 * @ss: pointer to subsystem
4248 * @id: current position of iteration.
4249 * @root: pointer to css. search tree under this.
4250 * @foundid: position of found object.
4252 * Search next css under the specified hierarchy of rootid. Calling under
4253 * rcu_read_lock() is necessary. Returns NULL if it reaches the end.
4255 struct cgroup_subsys_state *
4256 css_get_next(struct cgroup_subsys *ss, int id,
4257 struct cgroup_subsys_state *root, int *foundid)
4259 struct cgroup_subsys_state *ret = NULL;
4262 int rootid = css_id(root);
4263 int depth = css_depth(root);
4268 BUG_ON(!ss->use_id);
4269 /* fill start point for scan */
4273 * scan next entry from bitmap(tree), tmpid is updated after
4276 spin_lock(&ss->id_lock);
4277 tmp = idr_get_next(&ss->idr, &tmpid);
4278 spin_unlock(&ss->id_lock);
4282 if (tmp->depth >= depth && tmp->stack[depth] == rootid) {
4283 ret = rcu_dereference(tmp->css);
4289 /* continue to scan from next id */
4295 #ifdef CONFIG_CGROUP_DEBUG
4296 static struct cgroup_subsys_state *debug_create(struct cgroup_subsys *ss,
4297 struct cgroup *cont)
4299 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
4302 return ERR_PTR(-ENOMEM);
4307 static void debug_destroy(struct cgroup_subsys *ss, struct cgroup *cont)
4309 kfree(cont->subsys[debug_subsys_id]);
4312 static u64 cgroup_refcount_read(struct cgroup *cont, struct cftype *cft)
4314 return atomic_read(&cont->count);
4317 static u64 debug_taskcount_read(struct cgroup *cont, struct cftype *cft)
4319 return cgroup_task_count(cont);
4322 static u64 current_css_set_read(struct cgroup *cont, struct cftype *cft)
4324 return (u64)(unsigned long)current->cgroups;
4327 static u64 current_css_set_refcount_read(struct cgroup *cont,
4333 count = atomic_read(¤t->cgroups->refcount);
4338 static int current_css_set_cg_links_read(struct cgroup *cont,
4340 struct seq_file *seq)
4342 struct cg_cgroup_link *link;
4345 read_lock(&css_set_lock);
4347 cg = rcu_dereference(current->cgroups);
4348 list_for_each_entry(link, &cg->cg_links, cg_link_list) {
4349 struct cgroup *c = link->cgrp;
4353 name = c->dentry->d_name.name;
4356 seq_printf(seq, "Root %d group %s\n",
4357 c->root->hierarchy_id, name);
4360 read_unlock(&css_set_lock);
4364 #define MAX_TASKS_SHOWN_PER_CSS 25
4365 static int cgroup_css_links_read(struct cgroup *cont,
4367 struct seq_file *seq)
4369 struct cg_cgroup_link *link;
4371 read_lock(&css_set_lock);
4372 list_for_each_entry(link, &cont->css_sets, cgrp_link_list) {
4373 struct css_set *cg = link->cg;
4374 struct task_struct *task;
4376 seq_printf(seq, "css_set %p\n", cg);
4377 list_for_each_entry(task, &cg->tasks, cg_list) {
4378 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
4379 seq_puts(seq, " ...\n");
4382 seq_printf(seq, " task %d\n",
4383 task_pid_vnr(task));
4387 read_unlock(&css_set_lock);
4391 static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft)
4393 return test_bit(CGRP_RELEASABLE, &cgrp->flags);
4396 static struct cftype debug_files[] = {
4398 .name = "cgroup_refcount",
4399 .read_u64 = cgroup_refcount_read,
4402 .name = "taskcount",
4403 .read_u64 = debug_taskcount_read,
4407 .name = "current_css_set",
4408 .read_u64 = current_css_set_read,
4412 .name = "current_css_set_refcount",
4413 .read_u64 = current_css_set_refcount_read,
4417 .name = "current_css_set_cg_links",
4418 .read_seq_string = current_css_set_cg_links_read,
4422 .name = "cgroup_css_links",
4423 .read_seq_string = cgroup_css_links_read,
4427 .name = "releasable",
4428 .read_u64 = releasable_read,
4432 static int debug_populate(struct cgroup_subsys *ss, struct cgroup *cont)
4434 return cgroup_add_files(cont, ss, debug_files,
4435 ARRAY_SIZE(debug_files));
4438 struct cgroup_subsys debug_subsys = {
4440 .create = debug_create,
4441 .destroy = debug_destroy,
4442 .populate = debug_populate,
4443 .subsys_id = debug_subsys_id,
4445 #endif /* CONFIG_CGROUP_DEBUG */