2 * Generic process-grouping system.
4 * Based originally on the cpuset system, extracted by Paul Menage
5 * Copyright (C) 2006 Google, Inc
7 * Notifications support
8 * Copyright (C) 2009 Nokia Corporation
9 * Author: Kirill A. Shutemov
11 * Copyright notices from the original cpuset code:
12 * --------------------------------------------------
13 * Copyright (C) 2003 BULL SA.
14 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
16 * Portions derived from Patrick Mochel's sysfs code.
17 * sysfs is Copyright (c) 2001-3 Patrick Mochel
19 * 2003-10-10 Written by Simon Derr.
20 * 2003-10-22 Updates by Stephen Hemminger.
21 * 2004 May-July Rework by Paul Jackson.
22 * ---------------------------------------------------
24 * This file is subject to the terms and conditions of the GNU General Public
25 * License. See the file COPYING in the main directory of the Linux
26 * distribution for more details.
29 #include <linux/cgroup.h>
30 #include <linux/ctype.h>
31 #include <linux/errno.h>
33 #include <linux/kernel.h>
34 #include <linux/list.h>
36 #include <linux/mutex.h>
37 #include <linux/mount.h>
38 #include <linux/pagemap.h>
39 #include <linux/proc_fs.h>
40 #include <linux/rcupdate.h>
41 #include <linux/sched.h>
42 #include <linux/backing-dev.h>
43 #include <linux/seq_file.h>
44 #include <linux/slab.h>
45 #include <linux/magic.h>
46 #include <linux/spinlock.h>
47 #include <linux/string.h>
48 #include <linux/sort.h>
49 #include <linux/kmod.h>
50 #include <linux/module.h>
51 #include <linux/delayacct.h>
52 #include <linux/cgroupstats.h>
53 #include <linux/hash.h>
54 #include <linux/namei.h>
55 #include <linux/pid_namespace.h>
56 #include <linux/idr.h>
57 #include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
58 #include <linux/eventfd.h>
59 #include <linux/poll.h>
60 #include <linux/flex_array.h> /* used in cgroup_attach_proc */
61 #include <linux/capability.h>
63 #include <asm/atomic.h>
65 static DEFINE_MUTEX(cgroup_mutex);
68 * Generate an array of cgroup subsystem pointers. At boot time, this is
69 * populated up to CGROUP_BUILTIN_SUBSYS_COUNT, and modular subsystems are
70 * registered after that. The mutable section of this array is protected by
73 #define SUBSYS(_x) &_x ## _subsys,
74 static struct cgroup_subsys *subsys[CGROUP_SUBSYS_COUNT] = {
75 #include <linux/cgroup_subsys.h>
78 #define MAX_CGROUP_ROOT_NAMELEN 64
81 * A cgroupfs_root represents the root of a cgroup hierarchy,
82 * and may be associated with a superblock to form an active
85 struct cgroupfs_root {
86 struct super_block *sb;
89 * The bitmask of subsystems intended to be attached to this
92 unsigned long subsys_bits;
94 /* Unique id for this hierarchy. */
97 /* The bitmask of subsystems currently attached to this hierarchy */
98 unsigned long actual_subsys_bits;
100 /* A list running through the attached subsystems */
101 struct list_head subsys_list;
103 /* The root cgroup for this hierarchy */
104 struct cgroup top_cgroup;
106 /* Tracks how many cgroups are currently defined in hierarchy.*/
107 int number_of_cgroups;
109 /* A list running through the active hierarchies */
110 struct list_head root_list;
112 /* Hierarchy-specific flags */
115 /* The path to use for release notifications. */
116 char release_agent_path[PATH_MAX];
118 /* The name for this hierarchy - may be empty */
119 char name[MAX_CGROUP_ROOT_NAMELEN];
123 * The "rootnode" hierarchy is the "dummy hierarchy", reserved for the
124 * subsystems that are otherwise unattached - it never has more than a
125 * single cgroup, and all tasks are part of that cgroup.
127 static struct cgroupfs_root rootnode;
130 * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when
131 * cgroup_subsys->use_id != 0.
133 #define CSS_ID_MAX (65535)
136 * The css to which this ID points. This pointer is set to valid value
137 * after cgroup is populated. If cgroup is removed, this will be NULL.
138 * This pointer is expected to be RCU-safe because destroy()
139 * is called after synchronize_rcu(). But for safe use, css_is_removed()
140 * css_tryget() should be used for avoiding race.
142 struct cgroup_subsys_state __rcu *css;
148 * Depth in hierarchy which this ID belongs to.
150 unsigned short depth;
152 * ID is freed by RCU. (and lookup routine is RCU safe.)
154 struct rcu_head rcu_head;
156 * Hierarchy of CSS ID belongs to.
158 unsigned short stack[0]; /* Array of Length (depth+1) */
162 * cgroup_event represents events which userspace want to receive.
164 struct cgroup_event {
166 * Cgroup which the event belongs to.
170 * Control file which the event associated.
174 * eventfd to signal userspace about the event.
176 struct eventfd_ctx *eventfd;
178 * Each of these stored in a list by the cgroup.
180 struct list_head list;
182 * All fields below needed to unregister event when
183 * userspace closes eventfd.
186 wait_queue_head_t *wqh;
188 struct work_struct remove;
191 /* The list of hierarchy roots */
193 static LIST_HEAD(roots);
194 static int root_count;
196 static DEFINE_IDA(hierarchy_ida);
197 static int next_hierarchy_id;
198 static DEFINE_SPINLOCK(hierarchy_id_lock);
200 /* dummytop is a shorthand for the dummy hierarchy's top cgroup */
201 #define dummytop (&rootnode.top_cgroup)
203 /* This flag indicates whether tasks in the fork and exit paths should
204 * check for fork/exit handlers to call. This avoids us having to do
205 * extra work in the fork/exit path if none of the subsystems need to
208 static int need_forkexit_callback __read_mostly;
210 #ifdef CONFIG_PROVE_LOCKING
211 int cgroup_lock_is_held(void)
213 return lockdep_is_held(&cgroup_mutex);
215 #else /* #ifdef CONFIG_PROVE_LOCKING */
216 int cgroup_lock_is_held(void)
218 return mutex_is_locked(&cgroup_mutex);
220 #endif /* #else #ifdef CONFIG_PROVE_LOCKING */
222 EXPORT_SYMBOL_GPL(cgroup_lock_is_held);
224 /* convenient tests for these bits */
225 inline int cgroup_is_removed(const struct cgroup *cgrp)
227 return test_bit(CGRP_REMOVED, &cgrp->flags);
230 /* bits in struct cgroupfs_root flags field */
232 ROOT_NOPREFIX, /* mounted subsystems have no named prefix */
235 static int cgroup_is_releasable(const struct cgroup *cgrp)
238 (1 << CGRP_RELEASABLE) |
239 (1 << CGRP_NOTIFY_ON_RELEASE);
240 return (cgrp->flags & bits) == bits;
243 static int notify_on_release(const struct cgroup *cgrp)
245 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
248 static int clone_children(const struct cgroup *cgrp)
250 return test_bit(CGRP_CLONE_CHILDREN, &cgrp->flags);
254 * for_each_subsys() allows you to iterate on each subsystem attached to
255 * an active hierarchy
257 #define for_each_subsys(_root, _ss) \
258 list_for_each_entry(_ss, &_root->subsys_list, sibling)
260 /* for_each_active_root() allows you to iterate across the active hierarchies */
261 #define for_each_active_root(_root) \
262 list_for_each_entry(_root, &roots, root_list)
264 /* the list of cgroups eligible for automatic release. Protected by
265 * release_list_lock */
266 static LIST_HEAD(release_list);
267 static DEFINE_SPINLOCK(release_list_lock);
268 static void cgroup_release_agent(struct work_struct *work);
269 static DECLARE_WORK(release_agent_work, cgroup_release_agent);
270 static void check_for_release(struct cgroup *cgrp);
273 * A queue for waiters to do rmdir() cgroup. A tasks will sleep when
274 * cgroup->count == 0 && list_empty(&cgroup->children) && subsys has some
275 * reference to css->refcnt. In general, this refcnt is expected to goes down
278 * CGRP_WAIT_ON_RMDIR flag is set under cgroup's inode->i_mutex;
280 DECLARE_WAIT_QUEUE_HEAD(cgroup_rmdir_waitq);
282 static void cgroup_wakeup_rmdir_waiter(struct cgroup *cgrp)
284 if (unlikely(test_and_clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags)))
285 wake_up_all(&cgroup_rmdir_waitq);
288 void cgroup_exclude_rmdir(struct cgroup_subsys_state *css)
293 void cgroup_release_and_wakeup_rmdir(struct cgroup_subsys_state *css)
295 cgroup_wakeup_rmdir_waiter(css->cgroup);
299 /* Link structure for associating css_set objects with cgroups */
300 struct cg_cgroup_link {
302 * List running through cg_cgroup_links associated with a
303 * cgroup, anchored on cgroup->css_sets
305 struct list_head cgrp_link_list;
308 * List running through cg_cgroup_links pointing at a
309 * single css_set object, anchored on css_set->cg_links
311 struct list_head cg_link_list;
315 /* The default css_set - used by init and its children prior to any
316 * hierarchies being mounted. It contains a pointer to the root state
317 * for each subsystem. Also used to anchor the list of css_sets. Not
318 * reference-counted, to improve performance when child cgroups
319 * haven't been created.
322 static struct css_set init_css_set;
323 static struct cg_cgroup_link init_css_set_link;
325 static int cgroup_init_idr(struct cgroup_subsys *ss,
326 struct cgroup_subsys_state *css);
328 /* css_set_lock protects the list of css_set objects, and the
329 * chain of tasks off each css_set. Nests outside task->alloc_lock
330 * due to cgroup_iter_start() */
331 static DEFINE_RWLOCK(css_set_lock);
332 static int css_set_count;
335 * hash table for cgroup groups. This improves the performance to find
336 * an existing css_set. This hash doesn't (currently) take into
337 * account cgroups in empty hierarchies.
339 #define CSS_SET_HASH_BITS 7
340 #define CSS_SET_TABLE_SIZE (1 << CSS_SET_HASH_BITS)
341 static struct hlist_head css_set_table[CSS_SET_TABLE_SIZE];
343 static struct hlist_head *css_set_hash(struct cgroup_subsys_state *css[])
347 unsigned long tmp = 0UL;
349 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++)
350 tmp += (unsigned long)css[i];
351 tmp = (tmp >> 16) ^ tmp;
353 index = hash_long(tmp, CSS_SET_HASH_BITS);
355 return &css_set_table[index];
358 static void free_css_set_work(struct work_struct *work)
360 struct css_set *cg = container_of(work, struct css_set, work);
361 struct cg_cgroup_link *link;
362 struct cg_cgroup_link *saved_link;
364 write_lock(&css_set_lock);
365 list_for_each_entry_safe(link, saved_link, &cg->cg_links,
367 struct cgroup *cgrp = link->cgrp;
368 list_del(&link->cg_link_list);
369 list_del(&link->cgrp_link_list);
370 if (atomic_dec_and_test(&cgrp->count)) {
371 check_for_release(cgrp);
372 cgroup_wakeup_rmdir_waiter(cgrp);
376 write_unlock(&css_set_lock);
381 static void free_css_set_rcu(struct rcu_head *obj)
383 struct css_set *cg = container_of(obj, struct css_set, rcu_head);
385 INIT_WORK(&cg->work, free_css_set_work);
386 schedule_work(&cg->work);
389 /* We don't maintain the lists running through each css_set to its
390 * task until after the first call to cgroup_iter_start(). This
391 * reduces the fork()/exit() overhead for people who have cgroups
392 * compiled into their kernel but not actually in use */
393 static int use_task_css_set_links __read_mostly;
396 * refcounted get/put for css_set objects
398 static inline void get_css_set(struct css_set *cg)
400 atomic_inc(&cg->refcount);
403 static void put_css_set(struct css_set *cg)
406 * Ensure that the refcount doesn't hit zero while any readers
407 * can see it. Similar to atomic_dec_and_lock(), but for an
410 if (atomic_add_unless(&cg->refcount, -1, 1))
412 write_lock(&css_set_lock);
413 if (!atomic_dec_and_test(&cg->refcount)) {
414 write_unlock(&css_set_lock);
418 hlist_del(&cg->hlist);
421 write_unlock(&css_set_lock);
422 call_rcu(&cg->rcu_head, free_css_set_rcu);
426 * compare_css_sets - helper function for find_existing_css_set().
427 * @cg: candidate css_set being tested
428 * @old_cg: existing css_set for a task
429 * @new_cgrp: cgroup that's being entered by the task
430 * @template: desired set of css pointers in css_set (pre-calculated)
432 * Returns true if "cg" matches "old_cg" except for the hierarchy
433 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
435 static bool compare_css_sets(struct css_set *cg,
436 struct css_set *old_cg,
437 struct cgroup *new_cgrp,
438 struct cgroup_subsys_state *template[])
440 struct list_head *l1, *l2;
442 if (memcmp(template, cg->subsys, sizeof(cg->subsys))) {
443 /* Not all subsystems matched */
448 * Compare cgroup pointers in order to distinguish between
449 * different cgroups in heirarchies with no subsystems. We
450 * could get by with just this check alone (and skip the
451 * memcmp above) but on most setups the memcmp check will
452 * avoid the need for this more expensive check on almost all
457 l2 = &old_cg->cg_links;
459 struct cg_cgroup_link *cgl1, *cgl2;
460 struct cgroup *cg1, *cg2;
464 /* See if we reached the end - both lists are equal length. */
465 if (l1 == &cg->cg_links) {
466 BUG_ON(l2 != &old_cg->cg_links);
469 BUG_ON(l2 == &old_cg->cg_links);
471 /* Locate the cgroups associated with these links. */
472 cgl1 = list_entry(l1, struct cg_cgroup_link, cg_link_list);
473 cgl2 = list_entry(l2, struct cg_cgroup_link, cg_link_list);
476 /* Hierarchies should be linked in the same order. */
477 BUG_ON(cg1->root != cg2->root);
480 * If this hierarchy is the hierarchy of the cgroup
481 * that's changing, then we need to check that this
482 * css_set points to the new cgroup; if it's any other
483 * hierarchy, then this css_set should point to the
484 * same cgroup as the old css_set.
486 if (cg1->root == new_cgrp->root) {
498 * find_existing_css_set() is a helper for
499 * find_css_set(), and checks to see whether an existing
500 * css_set is suitable.
502 * oldcg: the cgroup group that we're using before the cgroup
505 * cgrp: the cgroup that we're moving into
507 * template: location in which to build the desired set of subsystem
508 * state objects for the new cgroup group
510 static struct css_set *find_existing_css_set(
511 struct css_set *oldcg,
513 struct cgroup_subsys_state *template[])
516 struct cgroupfs_root *root = cgrp->root;
517 struct hlist_head *hhead;
518 struct hlist_node *node;
522 * Build the set of subsystem state objects that we want to see in the
523 * new css_set. while subsystems can change globally, the entries here
524 * won't change, so no need for locking.
526 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
527 if (root->subsys_bits & (1UL << i)) {
528 /* Subsystem is in this hierarchy. So we want
529 * the subsystem state from the new
531 template[i] = cgrp->subsys[i];
533 /* Subsystem is not in this hierarchy, so we
534 * don't want to change the subsystem state */
535 template[i] = oldcg->subsys[i];
539 hhead = css_set_hash(template);
540 hlist_for_each_entry(cg, node, hhead, hlist) {
541 if (!compare_css_sets(cg, oldcg, cgrp, template))
544 /* This css_set matches what we need */
548 /* No existing cgroup group matched */
552 static void free_cg_links(struct list_head *tmp)
554 struct cg_cgroup_link *link;
555 struct cg_cgroup_link *saved_link;
557 list_for_each_entry_safe(link, saved_link, tmp, cgrp_link_list) {
558 list_del(&link->cgrp_link_list);
564 * allocate_cg_links() allocates "count" cg_cgroup_link structures
565 * and chains them on tmp through their cgrp_link_list fields. Returns 0 on
566 * success or a negative error
568 static int allocate_cg_links(int count, struct list_head *tmp)
570 struct cg_cgroup_link *link;
573 for (i = 0; i < count; i++) {
574 link = kmalloc(sizeof(*link), GFP_KERNEL);
579 list_add(&link->cgrp_link_list, tmp);
585 * link_css_set - a helper function to link a css_set to a cgroup
586 * @tmp_cg_links: cg_cgroup_link objects allocated by allocate_cg_links()
587 * @cg: the css_set to be linked
588 * @cgrp: the destination cgroup
590 static void link_css_set(struct list_head *tmp_cg_links,
591 struct css_set *cg, struct cgroup *cgrp)
593 struct cg_cgroup_link *link;
595 BUG_ON(list_empty(tmp_cg_links));
596 link = list_first_entry(tmp_cg_links, struct cg_cgroup_link,
600 atomic_inc(&cgrp->count);
601 list_move(&link->cgrp_link_list, &cgrp->css_sets);
603 * Always add links to the tail of the list so that the list
604 * is sorted by order of hierarchy creation
606 list_add_tail(&link->cg_link_list, &cg->cg_links);
610 * find_css_set() takes an existing cgroup group and a
611 * cgroup object, and returns a css_set object that's
612 * equivalent to the old group, but with the given cgroup
613 * substituted into the appropriate hierarchy. Must be called with
616 static struct css_set *find_css_set(
617 struct css_set *oldcg, struct cgroup *cgrp)
620 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT];
622 struct list_head tmp_cg_links;
624 struct hlist_head *hhead;
625 struct cg_cgroup_link *link;
627 /* First see if we already have a cgroup group that matches
629 read_lock(&css_set_lock);
630 res = find_existing_css_set(oldcg, cgrp, template);
633 read_unlock(&css_set_lock);
638 res = kmalloc(sizeof(*res), GFP_KERNEL);
642 /* Allocate all the cg_cgroup_link objects that we'll need */
643 if (allocate_cg_links(root_count, &tmp_cg_links) < 0) {
648 atomic_set(&res->refcount, 1);
649 INIT_LIST_HEAD(&res->cg_links);
650 INIT_LIST_HEAD(&res->tasks);
651 INIT_HLIST_NODE(&res->hlist);
653 /* Copy the set of subsystem state objects generated in
654 * find_existing_css_set() */
655 memcpy(res->subsys, template, sizeof(res->subsys));
657 write_lock(&css_set_lock);
658 /* Add reference counts and links from the new css_set. */
659 list_for_each_entry(link, &oldcg->cg_links, cg_link_list) {
660 struct cgroup *c = link->cgrp;
661 if (c->root == cgrp->root)
663 link_css_set(&tmp_cg_links, res, c);
666 BUG_ON(!list_empty(&tmp_cg_links));
670 /* Add this cgroup group to the hash table */
671 hhead = css_set_hash(res->subsys);
672 hlist_add_head(&res->hlist, hhead);
674 write_unlock(&css_set_lock);
680 * Return the cgroup for "task" from the given hierarchy. Must be
681 * called with cgroup_mutex held.
683 static struct cgroup *task_cgroup_from_root(struct task_struct *task,
684 struct cgroupfs_root *root)
687 struct cgroup *res = NULL;
689 BUG_ON(!mutex_is_locked(&cgroup_mutex));
690 read_lock(&css_set_lock);
692 * No need to lock the task - since we hold cgroup_mutex the
693 * task can't change groups, so the only thing that can happen
694 * is that it exits and its css is set back to init_css_set.
697 if (css == &init_css_set) {
698 res = &root->top_cgroup;
700 struct cg_cgroup_link *link;
701 list_for_each_entry(link, &css->cg_links, cg_link_list) {
702 struct cgroup *c = link->cgrp;
703 if (c->root == root) {
709 read_unlock(&css_set_lock);
715 * There is one global cgroup mutex. We also require taking
716 * task_lock() when dereferencing a task's cgroup subsys pointers.
717 * See "The task_lock() exception", at the end of this comment.
719 * A task must hold cgroup_mutex to modify cgroups.
721 * Any task can increment and decrement the count field without lock.
722 * So in general, code holding cgroup_mutex can't rely on the count
723 * field not changing. However, if the count goes to zero, then only
724 * cgroup_attach_task() can increment it again. Because a count of zero
725 * means that no tasks are currently attached, therefore there is no
726 * way a task attached to that cgroup can fork (the other way to
727 * increment the count). So code holding cgroup_mutex can safely
728 * assume that if the count is zero, it will stay zero. Similarly, if
729 * a task holds cgroup_mutex on a cgroup with zero count, it
730 * knows that the cgroup won't be removed, as cgroup_rmdir()
733 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
734 * (usually) take cgroup_mutex. These are the two most performance
735 * critical pieces of code here. The exception occurs on cgroup_exit(),
736 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
737 * is taken, and if the cgroup count is zero, a usermode call made
738 * to the release agent with the name of the cgroup (path relative to
739 * the root of cgroup file system) as the argument.
741 * A cgroup can only be deleted if both its 'count' of using tasks
742 * is zero, and its list of 'children' cgroups is empty. Since all
743 * tasks in the system use _some_ cgroup, and since there is always at
744 * least one task in the system (init, pid == 1), therefore, top_cgroup
745 * always has either children cgroups and/or using tasks. So we don't
746 * need a special hack to ensure that top_cgroup cannot be deleted.
748 * The task_lock() exception
750 * The need for this exception arises from the action of
751 * cgroup_attach_task(), which overwrites one tasks cgroup pointer with
752 * another. It does so using cgroup_mutex, however there are
753 * several performance critical places that need to reference
754 * task->cgroups without the expense of grabbing a system global
755 * mutex. Therefore except as noted below, when dereferencing or, as
756 * in cgroup_attach_task(), modifying a task's cgroups pointer we use
757 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
758 * the task_struct routinely used for such matters.
760 * P.S. One more locking exception. RCU is used to guard the
761 * update of a tasks cgroup pointer by cgroup_attach_task()
765 * cgroup_lock - lock out any changes to cgroup structures
768 void cgroup_lock(void)
770 mutex_lock(&cgroup_mutex);
772 EXPORT_SYMBOL_GPL(cgroup_lock);
775 * cgroup_unlock - release lock on cgroup changes
777 * Undo the lock taken in a previous cgroup_lock() call.
779 void cgroup_unlock(void)
781 mutex_unlock(&cgroup_mutex);
783 EXPORT_SYMBOL_GPL(cgroup_unlock);
786 * A couple of forward declarations required, due to cyclic reference loop:
787 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
788 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
792 static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode);
793 static struct dentry *cgroup_lookup(struct inode *, struct dentry *, struct nameidata *);
794 static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
795 static int cgroup_populate_dir(struct cgroup *cgrp);
796 static const struct inode_operations cgroup_dir_inode_operations;
797 static const struct file_operations proc_cgroupstats_operations;
799 static struct backing_dev_info cgroup_backing_dev_info = {
801 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
804 static int alloc_css_id(struct cgroup_subsys *ss,
805 struct cgroup *parent, struct cgroup *child);
807 static struct inode *cgroup_new_inode(mode_t mode, struct super_block *sb)
809 struct inode *inode = new_inode(sb);
812 inode->i_ino = get_next_ino();
813 inode->i_mode = mode;
814 inode->i_uid = current_fsuid();
815 inode->i_gid = current_fsgid();
816 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
817 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
823 * Call subsys's pre_destroy handler.
824 * This is called before css refcnt check.
826 static int cgroup_call_pre_destroy(struct cgroup *cgrp)
828 struct cgroup_subsys *ss;
831 for_each_subsys(cgrp->root, ss)
832 if (ss->pre_destroy) {
833 ret = ss->pre_destroy(ss, cgrp);
841 static void cgroup_diput(struct dentry *dentry, struct inode *inode)
843 /* is dentry a directory ? if so, kfree() associated cgroup */
844 if (S_ISDIR(inode->i_mode)) {
845 struct cgroup *cgrp = dentry->d_fsdata;
846 struct cgroup_subsys *ss;
847 BUG_ON(!(cgroup_is_removed(cgrp)));
848 /* It's possible for external users to be holding css
849 * reference counts on a cgroup; css_put() needs to
850 * be able to access the cgroup after decrementing
851 * the reference count in order to know if it needs to
852 * queue the cgroup to be handled by the release
856 mutex_lock(&cgroup_mutex);
858 * Release the subsystem state objects.
860 for_each_subsys(cgrp->root, ss)
861 ss->destroy(ss, cgrp);
863 cgrp->root->number_of_cgroups--;
864 mutex_unlock(&cgroup_mutex);
867 * Drop the active superblock reference that we took when we
870 deactivate_super(cgrp->root->sb);
873 * if we're getting rid of the cgroup, refcount should ensure
874 * that there are no pidlists left.
876 BUG_ON(!list_empty(&cgrp->pidlists));
878 kfree_rcu(cgrp, rcu_head);
883 static int cgroup_delete(const struct dentry *d)
888 static void remove_dir(struct dentry *d)
890 struct dentry *parent = dget(d->d_parent);
893 simple_rmdir(parent->d_inode, d);
897 static void cgroup_clear_directory(struct dentry *dentry)
899 struct list_head *node;
901 BUG_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
902 spin_lock(&dentry->d_lock);
903 node = dentry->d_subdirs.next;
904 while (node != &dentry->d_subdirs) {
905 struct dentry *d = list_entry(node, struct dentry, d_u.d_child);
907 spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
910 /* This should never be called on a cgroup
911 * directory with child cgroups */
912 BUG_ON(d->d_inode->i_mode & S_IFDIR);
914 spin_unlock(&d->d_lock);
915 spin_unlock(&dentry->d_lock);
917 simple_unlink(dentry->d_inode, d);
919 spin_lock(&dentry->d_lock);
921 spin_unlock(&d->d_lock);
922 node = dentry->d_subdirs.next;
924 spin_unlock(&dentry->d_lock);
928 * NOTE : the dentry must have been dget()'ed
930 static void cgroup_d_remove_dir(struct dentry *dentry)
932 struct dentry *parent;
934 cgroup_clear_directory(dentry);
936 parent = dentry->d_parent;
937 spin_lock(&parent->d_lock);
938 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
939 list_del_init(&dentry->d_u.d_child);
940 spin_unlock(&dentry->d_lock);
941 spin_unlock(&parent->d_lock);
946 * Call with cgroup_mutex held. Drops reference counts on modules, including
947 * any duplicate ones that parse_cgroupfs_options took. If this function
948 * returns an error, no reference counts are touched.
950 static int rebind_subsystems(struct cgroupfs_root *root,
951 unsigned long final_bits)
953 unsigned long added_bits, removed_bits;
954 struct cgroup *cgrp = &root->top_cgroup;
957 BUG_ON(!mutex_is_locked(&cgroup_mutex));
959 removed_bits = root->actual_subsys_bits & ~final_bits;
960 added_bits = final_bits & ~root->actual_subsys_bits;
961 /* Check that any added subsystems are currently free */
962 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
963 unsigned long bit = 1UL << i;
964 struct cgroup_subsys *ss = subsys[i];
965 if (!(bit & added_bits))
968 * Nobody should tell us to do a subsys that doesn't exist:
969 * parse_cgroupfs_options should catch that case and refcounts
970 * ensure that subsystems won't disappear once selected.
973 if (ss->root != &rootnode) {
974 /* Subsystem isn't free */
979 /* Currently we don't handle adding/removing subsystems when
980 * any child cgroups exist. This is theoretically supportable
981 * but involves complex error handling, so it's being left until
983 if (root->number_of_cgroups > 1)
986 /* Process each subsystem */
987 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
988 struct cgroup_subsys *ss = subsys[i];
989 unsigned long bit = 1UL << i;
990 if (bit & added_bits) {
991 /* We're binding this subsystem to this hierarchy */
993 BUG_ON(cgrp->subsys[i]);
994 BUG_ON(!dummytop->subsys[i]);
995 BUG_ON(dummytop->subsys[i]->cgroup != dummytop);
996 mutex_lock(&ss->hierarchy_mutex);
997 cgrp->subsys[i] = dummytop->subsys[i];
998 cgrp->subsys[i]->cgroup = cgrp;
999 list_move(&ss->sibling, &root->subsys_list);
1003 mutex_unlock(&ss->hierarchy_mutex);
1004 /* refcount was already taken, and we're keeping it */
1005 } else if (bit & removed_bits) {
1006 /* We're removing this subsystem */
1008 BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]);
1009 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
1010 mutex_lock(&ss->hierarchy_mutex);
1012 ss->bind(ss, dummytop);
1013 dummytop->subsys[i]->cgroup = dummytop;
1014 cgrp->subsys[i] = NULL;
1015 subsys[i]->root = &rootnode;
1016 list_move(&ss->sibling, &rootnode.subsys_list);
1017 mutex_unlock(&ss->hierarchy_mutex);
1018 /* subsystem is now free - drop reference on module */
1019 module_put(ss->module);
1020 } else if (bit & final_bits) {
1021 /* Subsystem state should already exist */
1023 BUG_ON(!cgrp->subsys[i]);
1025 * a refcount was taken, but we already had one, so
1026 * drop the extra reference.
1028 module_put(ss->module);
1029 #ifdef CONFIG_MODULE_UNLOAD
1030 BUG_ON(ss->module && !module_refcount(ss->module));
1033 /* Subsystem state shouldn't exist */
1034 BUG_ON(cgrp->subsys[i]);
1037 root->subsys_bits = root->actual_subsys_bits = final_bits;
1043 static int cgroup_show_options(struct seq_file *seq, struct vfsmount *vfs)
1045 struct cgroupfs_root *root = vfs->mnt_sb->s_fs_info;
1046 struct cgroup_subsys *ss;
1048 mutex_lock(&cgroup_mutex);
1049 for_each_subsys(root, ss)
1050 seq_printf(seq, ",%s", ss->name);
1051 if (test_bit(ROOT_NOPREFIX, &root->flags))
1052 seq_puts(seq, ",noprefix");
1053 if (strlen(root->release_agent_path))
1054 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
1055 if (clone_children(&root->top_cgroup))
1056 seq_puts(seq, ",clone_children");
1057 if (strlen(root->name))
1058 seq_printf(seq, ",name=%s", root->name);
1059 mutex_unlock(&cgroup_mutex);
1063 struct cgroup_sb_opts {
1064 unsigned long subsys_bits;
1065 unsigned long flags;
1066 char *release_agent;
1067 bool clone_children;
1069 /* User explicitly requested empty subsystem */
1072 struct cgroupfs_root *new_root;
1077 * Convert a hierarchy specifier into a bitmask of subsystems and flags. Call
1078 * with cgroup_mutex held to protect the subsys[] array. This function takes
1079 * refcounts on subsystems to be used, unless it returns error, in which case
1080 * no refcounts are taken.
1082 static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
1084 char *token, *o = data;
1085 bool all_ss = false, one_ss = false;
1086 unsigned long mask = (unsigned long)-1;
1088 bool module_pin_failed = false;
1090 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1092 #ifdef CONFIG_CPUSETS
1093 mask = ~(1UL << cpuset_subsys_id);
1096 memset(opts, 0, sizeof(*opts));
1098 while ((token = strsep(&o, ",")) != NULL) {
1101 if (!strcmp(token, "none")) {
1102 /* Explicitly have no subsystems */
1106 if (!strcmp(token, "all")) {
1107 /* Mutually exclusive option 'all' + subsystem name */
1113 if (!strcmp(token, "noprefix")) {
1114 set_bit(ROOT_NOPREFIX, &opts->flags);
1117 if (!strcmp(token, "clone_children")) {
1118 opts->clone_children = true;
1121 if (!strncmp(token, "release_agent=", 14)) {
1122 /* Specifying two release agents is forbidden */
1123 if (opts->release_agent)
1125 opts->release_agent =
1126 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
1127 if (!opts->release_agent)
1131 if (!strncmp(token, "name=", 5)) {
1132 const char *name = token + 5;
1133 /* Can't specify an empty name */
1136 /* Must match [\w.-]+ */
1137 for (i = 0; i < strlen(name); i++) {
1141 if ((c == '.') || (c == '-') || (c == '_'))
1145 /* Specifying two names is forbidden */
1148 opts->name = kstrndup(name,
1149 MAX_CGROUP_ROOT_NAMELEN - 1,
1157 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1158 struct cgroup_subsys *ss = subsys[i];
1161 if (strcmp(token, ss->name))
1166 /* Mutually exclusive option 'all' + subsystem name */
1169 set_bit(i, &opts->subsys_bits);
1174 if (i == CGROUP_SUBSYS_COUNT)
1179 * If the 'all' option was specified select all the subsystems,
1180 * otherwise 'all, 'none' and a subsystem name options were not
1181 * specified, let's default to 'all'
1183 if (all_ss || (!all_ss && !one_ss && !opts->none)) {
1184 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1185 struct cgroup_subsys *ss = subsys[i];
1190 set_bit(i, &opts->subsys_bits);
1194 /* Consistency checks */
1197 * Option noprefix was introduced just for backward compatibility
1198 * with the old cpuset, so we allow noprefix only if mounting just
1199 * the cpuset subsystem.
1201 if (test_bit(ROOT_NOPREFIX, &opts->flags) &&
1202 (opts->subsys_bits & mask))
1206 /* Can't specify "none" and some subsystems */
1207 if (opts->subsys_bits && opts->none)
1211 * We either have to specify by name or by subsystems. (So all
1212 * empty hierarchies must have a name).
1214 if (!opts->subsys_bits && !opts->name)
1218 * Grab references on all the modules we'll need, so the subsystems
1219 * don't dance around before rebind_subsystems attaches them. This may
1220 * take duplicate reference counts on a subsystem that's already used,
1221 * but rebind_subsystems handles this case.
1223 for (i = CGROUP_BUILTIN_SUBSYS_COUNT; i < CGROUP_SUBSYS_COUNT; i++) {
1224 unsigned long bit = 1UL << i;
1226 if (!(bit & opts->subsys_bits))
1228 if (!try_module_get(subsys[i]->module)) {
1229 module_pin_failed = true;
1233 if (module_pin_failed) {
1235 * oops, one of the modules was going away. this means that we
1236 * raced with a module_delete call, and to the user this is
1237 * essentially a "subsystem doesn't exist" case.
1239 for (i--; i >= CGROUP_BUILTIN_SUBSYS_COUNT; i--) {
1240 /* drop refcounts only on the ones we took */
1241 unsigned long bit = 1UL << i;
1243 if (!(bit & opts->subsys_bits))
1245 module_put(subsys[i]->module);
1253 static void drop_parsed_module_refcounts(unsigned long subsys_bits)
1256 for (i = CGROUP_BUILTIN_SUBSYS_COUNT; i < CGROUP_SUBSYS_COUNT; i++) {
1257 unsigned long bit = 1UL << i;
1259 if (!(bit & subsys_bits))
1261 module_put(subsys[i]->module);
1265 static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1268 struct cgroupfs_root *root = sb->s_fs_info;
1269 struct cgroup *cgrp = &root->top_cgroup;
1270 struct cgroup_sb_opts opts;
1272 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
1273 mutex_lock(&cgroup_mutex);
1275 /* See what subsystems are wanted */
1276 ret = parse_cgroupfs_options(data, &opts);
1280 /* Don't allow flags or name to change at remount */
1281 if (opts.flags != root->flags ||
1282 (opts.name && strcmp(opts.name, root->name))) {
1284 drop_parsed_module_refcounts(opts.subsys_bits);
1288 ret = rebind_subsystems(root, opts.subsys_bits);
1290 drop_parsed_module_refcounts(opts.subsys_bits);
1294 /* (re)populate subsystem files */
1295 cgroup_populate_dir(cgrp);
1297 if (opts.release_agent)
1298 strcpy(root->release_agent_path, opts.release_agent);
1300 kfree(opts.release_agent);
1302 mutex_unlock(&cgroup_mutex);
1303 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
1307 static const struct super_operations cgroup_ops = {
1308 .statfs = simple_statfs,
1309 .drop_inode = generic_delete_inode,
1310 .show_options = cgroup_show_options,
1311 .remount_fs = cgroup_remount,
1314 static void init_cgroup_housekeeping(struct cgroup *cgrp)
1316 INIT_LIST_HEAD(&cgrp->sibling);
1317 INIT_LIST_HEAD(&cgrp->children);
1318 INIT_LIST_HEAD(&cgrp->css_sets);
1319 INIT_LIST_HEAD(&cgrp->release_list);
1320 INIT_LIST_HEAD(&cgrp->pidlists);
1321 mutex_init(&cgrp->pidlist_mutex);
1322 INIT_LIST_HEAD(&cgrp->event_list);
1323 spin_lock_init(&cgrp->event_list_lock);
1326 static void init_cgroup_root(struct cgroupfs_root *root)
1328 struct cgroup *cgrp = &root->top_cgroup;
1329 INIT_LIST_HEAD(&root->subsys_list);
1330 INIT_LIST_HEAD(&root->root_list);
1331 root->number_of_cgroups = 1;
1333 cgrp->top_cgroup = cgrp;
1334 init_cgroup_housekeeping(cgrp);
1337 static bool init_root_id(struct cgroupfs_root *root)
1342 if (!ida_pre_get(&hierarchy_ida, GFP_KERNEL))
1344 spin_lock(&hierarchy_id_lock);
1345 /* Try to allocate the next unused ID */
1346 ret = ida_get_new_above(&hierarchy_ida, next_hierarchy_id,
1347 &root->hierarchy_id);
1349 /* Try again starting from 0 */
1350 ret = ida_get_new(&hierarchy_ida, &root->hierarchy_id);
1352 next_hierarchy_id = root->hierarchy_id + 1;
1353 } else if (ret != -EAGAIN) {
1354 /* Can only get here if the 31-bit IDR is full ... */
1357 spin_unlock(&hierarchy_id_lock);
1362 static int cgroup_test_super(struct super_block *sb, void *data)
1364 struct cgroup_sb_opts *opts = data;
1365 struct cgroupfs_root *root = sb->s_fs_info;
1367 /* If we asked for a name then it must match */
1368 if (opts->name && strcmp(opts->name, root->name))
1372 * If we asked for subsystems (or explicitly for no
1373 * subsystems) then they must match
1375 if ((opts->subsys_bits || opts->none)
1376 && (opts->subsys_bits != root->subsys_bits))
1382 static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1384 struct cgroupfs_root *root;
1386 if (!opts->subsys_bits && !opts->none)
1389 root = kzalloc(sizeof(*root), GFP_KERNEL);
1391 return ERR_PTR(-ENOMEM);
1393 if (!init_root_id(root)) {
1395 return ERR_PTR(-ENOMEM);
1397 init_cgroup_root(root);
1399 root->subsys_bits = opts->subsys_bits;
1400 root->flags = opts->flags;
1401 if (opts->release_agent)
1402 strcpy(root->release_agent_path, opts->release_agent);
1404 strcpy(root->name, opts->name);
1405 if (opts->clone_children)
1406 set_bit(CGRP_CLONE_CHILDREN, &root->top_cgroup.flags);
1410 static void cgroup_drop_root(struct cgroupfs_root *root)
1415 BUG_ON(!root->hierarchy_id);
1416 spin_lock(&hierarchy_id_lock);
1417 ida_remove(&hierarchy_ida, root->hierarchy_id);
1418 spin_unlock(&hierarchy_id_lock);
1422 static int cgroup_set_super(struct super_block *sb, void *data)
1425 struct cgroup_sb_opts *opts = data;
1427 /* If we don't have a new root, we can't set up a new sb */
1428 if (!opts->new_root)
1431 BUG_ON(!opts->subsys_bits && !opts->none);
1433 ret = set_anon_super(sb, NULL);
1437 sb->s_fs_info = opts->new_root;
1438 opts->new_root->sb = sb;
1440 sb->s_blocksize = PAGE_CACHE_SIZE;
1441 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1442 sb->s_magic = CGROUP_SUPER_MAGIC;
1443 sb->s_op = &cgroup_ops;
1448 static int cgroup_get_rootdir(struct super_block *sb)
1450 static const struct dentry_operations cgroup_dops = {
1451 .d_iput = cgroup_diput,
1452 .d_delete = cgroup_delete,
1455 struct inode *inode =
1456 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
1457 struct dentry *dentry;
1462 inode->i_fop = &simple_dir_operations;
1463 inode->i_op = &cgroup_dir_inode_operations;
1464 /* directories start off with i_nlink == 2 (for "." entry) */
1466 dentry = d_alloc_root(inode);
1471 sb->s_root = dentry;
1472 /* for everything else we want ->d_op set */
1473 sb->s_d_op = &cgroup_dops;
1477 static struct dentry *cgroup_mount(struct file_system_type *fs_type,
1478 int flags, const char *unused_dev_name,
1481 struct cgroup_sb_opts opts;
1482 struct cgroupfs_root *root;
1484 struct super_block *sb;
1485 struct cgroupfs_root *new_root;
1487 /* First find the desired set of subsystems */
1488 mutex_lock(&cgroup_mutex);
1489 ret = parse_cgroupfs_options(data, &opts);
1490 mutex_unlock(&cgroup_mutex);
1495 * Allocate a new cgroup root. We may not need it if we're
1496 * reusing an existing hierarchy.
1498 new_root = cgroup_root_from_opts(&opts);
1499 if (IS_ERR(new_root)) {
1500 ret = PTR_ERR(new_root);
1503 opts.new_root = new_root;
1505 /* Locate an existing or new sb for this hierarchy */
1506 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, &opts);
1509 cgroup_drop_root(opts.new_root);
1513 root = sb->s_fs_info;
1515 if (root == opts.new_root) {
1516 /* We used the new root structure, so this is a new hierarchy */
1517 struct list_head tmp_cg_links;
1518 struct cgroup *root_cgrp = &root->top_cgroup;
1519 struct inode *inode;
1520 struct cgroupfs_root *existing_root;
1523 BUG_ON(sb->s_root != NULL);
1525 ret = cgroup_get_rootdir(sb);
1527 goto drop_new_super;
1528 inode = sb->s_root->d_inode;
1530 mutex_lock(&inode->i_mutex);
1531 mutex_lock(&cgroup_mutex);
1533 if (strlen(root->name)) {
1534 /* Check for name clashes with existing mounts */
1535 for_each_active_root(existing_root) {
1536 if (!strcmp(existing_root->name, root->name)) {
1538 mutex_unlock(&cgroup_mutex);
1539 mutex_unlock(&inode->i_mutex);
1540 goto drop_new_super;
1546 * We're accessing css_set_count without locking
1547 * css_set_lock here, but that's OK - it can only be
1548 * increased by someone holding cgroup_lock, and
1549 * that's us. The worst that can happen is that we
1550 * have some link structures left over
1552 ret = allocate_cg_links(css_set_count, &tmp_cg_links);
1554 mutex_unlock(&cgroup_mutex);
1555 mutex_unlock(&inode->i_mutex);
1556 goto drop_new_super;
1559 ret = rebind_subsystems(root, root->subsys_bits);
1560 if (ret == -EBUSY) {
1561 mutex_unlock(&cgroup_mutex);
1562 mutex_unlock(&inode->i_mutex);
1563 free_cg_links(&tmp_cg_links);
1564 goto drop_new_super;
1567 * There must be no failure case after here, since rebinding
1568 * takes care of subsystems' refcounts, which are explicitly
1569 * dropped in the failure exit path.
1572 /* EBUSY should be the only error here */
1575 list_add(&root->root_list, &roots);
1578 sb->s_root->d_fsdata = root_cgrp;
1579 root->top_cgroup.dentry = sb->s_root;
1581 /* Link the top cgroup in this hierarchy into all
1582 * the css_set objects */
1583 write_lock(&css_set_lock);
1584 for (i = 0; i < CSS_SET_TABLE_SIZE; i++) {
1585 struct hlist_head *hhead = &css_set_table[i];
1586 struct hlist_node *node;
1589 hlist_for_each_entry(cg, node, hhead, hlist)
1590 link_css_set(&tmp_cg_links, cg, root_cgrp);
1592 write_unlock(&css_set_lock);
1594 free_cg_links(&tmp_cg_links);
1596 BUG_ON(!list_empty(&root_cgrp->sibling));
1597 BUG_ON(!list_empty(&root_cgrp->children));
1598 BUG_ON(root->number_of_cgroups != 1);
1600 cgroup_populate_dir(root_cgrp);
1601 mutex_unlock(&cgroup_mutex);
1602 mutex_unlock(&inode->i_mutex);
1605 * We re-used an existing hierarchy - the new root (if
1606 * any) is not needed
1608 cgroup_drop_root(opts.new_root);
1609 /* no subsys rebinding, so refcounts don't change */
1610 drop_parsed_module_refcounts(opts.subsys_bits);
1613 kfree(opts.release_agent);
1615 return dget(sb->s_root);
1618 deactivate_locked_super(sb);
1620 drop_parsed_module_refcounts(opts.subsys_bits);
1622 kfree(opts.release_agent);
1624 return ERR_PTR(ret);
1627 static void cgroup_kill_sb(struct super_block *sb) {
1628 struct cgroupfs_root *root = sb->s_fs_info;
1629 struct cgroup *cgrp = &root->top_cgroup;
1631 struct cg_cgroup_link *link;
1632 struct cg_cgroup_link *saved_link;
1636 BUG_ON(root->number_of_cgroups != 1);
1637 BUG_ON(!list_empty(&cgrp->children));
1638 BUG_ON(!list_empty(&cgrp->sibling));
1640 mutex_lock(&cgroup_mutex);
1642 /* Rebind all subsystems back to the default hierarchy */
1643 ret = rebind_subsystems(root, 0);
1644 /* Shouldn't be able to fail ... */
1648 * Release all the links from css_sets to this hierarchy's
1651 write_lock(&css_set_lock);
1653 list_for_each_entry_safe(link, saved_link, &cgrp->css_sets,
1655 list_del(&link->cg_link_list);
1656 list_del(&link->cgrp_link_list);
1659 write_unlock(&css_set_lock);
1661 if (!list_empty(&root->root_list)) {
1662 list_del(&root->root_list);
1666 mutex_unlock(&cgroup_mutex);
1668 kill_litter_super(sb);
1669 cgroup_drop_root(root);
1672 static struct file_system_type cgroup_fs_type = {
1674 .mount = cgroup_mount,
1675 .kill_sb = cgroup_kill_sb,
1678 static struct kobject *cgroup_kobj;
1680 static inline struct cgroup *__d_cgrp(struct dentry *dentry)
1682 return dentry->d_fsdata;
1685 static inline struct cftype *__d_cft(struct dentry *dentry)
1687 return dentry->d_fsdata;
1691 * cgroup_path - generate the path of a cgroup
1692 * @cgrp: the cgroup in question
1693 * @buf: the buffer to write the path into
1694 * @buflen: the length of the buffer
1696 * Called with cgroup_mutex held or else with an RCU-protected cgroup
1697 * reference. Writes path of cgroup into buf. Returns 0 on success,
1700 int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
1703 struct dentry *dentry = rcu_dereference_check(cgrp->dentry,
1704 rcu_read_lock_held() ||
1705 cgroup_lock_is_held());
1707 if (!dentry || cgrp == dummytop) {
1709 * Inactive subsystems have no dentry for their root
1716 start = buf + buflen;
1720 int len = dentry->d_name.len;
1722 if ((start -= len) < buf)
1723 return -ENAMETOOLONG;
1724 memcpy(start, dentry->d_name.name, len);
1725 cgrp = cgrp->parent;
1729 dentry = rcu_dereference_check(cgrp->dentry,
1730 rcu_read_lock_held() ||
1731 cgroup_lock_is_held());
1735 return -ENAMETOOLONG;
1738 memmove(buf, start, buf + buflen - start);
1741 EXPORT_SYMBOL_GPL(cgroup_path);
1744 * cgroup_task_migrate - move a task from one cgroup to another.
1746 * 'guarantee' is set if the caller promises that a new css_set for the task
1747 * will already exist. If not set, this function might sleep, and can fail with
1748 * -ENOMEM. Otherwise, it can only fail with -ESRCH.
1750 static int cgroup_task_migrate(struct cgroup *cgrp, struct cgroup *oldcgrp,
1751 struct task_struct *tsk, bool guarantee)
1753 struct css_set *oldcg;
1754 struct css_set *newcg;
1757 * get old css_set. we need to take task_lock and refcount it, because
1758 * an exiting task can change its css_set to init_css_set and drop its
1759 * old one without taking cgroup_mutex.
1762 oldcg = tsk->cgroups;
1766 /* locate or allocate a new css_set for this task. */
1768 /* we know the css_set we want already exists. */
1769 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT];
1770 read_lock(&css_set_lock);
1771 newcg = find_existing_css_set(oldcg, cgrp, template);
1774 read_unlock(&css_set_lock);
1777 /* find_css_set will give us newcg already referenced. */
1778 newcg = find_css_set(oldcg, cgrp);
1786 /* if PF_EXITING is set, the tsk->cgroups pointer is no longer safe. */
1788 if (tsk->flags & PF_EXITING) {
1793 rcu_assign_pointer(tsk->cgroups, newcg);
1796 /* Update the css_set linked lists if we're using them */
1797 write_lock(&css_set_lock);
1798 if (!list_empty(&tsk->cg_list))
1799 list_move(&tsk->cg_list, &newcg->tasks);
1800 write_unlock(&css_set_lock);
1803 * We just gained a reference on oldcg by taking it from the task. As
1804 * trading it for newcg is protected by cgroup_mutex, we're safe to drop
1805 * it here; it will be freed under RCU.
1809 set_bit(CGRP_RELEASABLE, &oldcgrp->flags);
1814 * cgroup_attach_task - attach task 'tsk' to cgroup 'cgrp'
1815 * @cgrp: the cgroup the task is attaching to
1816 * @tsk: the task to be attached
1818 * Call holding cgroup_mutex. May take task_lock of
1819 * the task 'tsk' during call.
1821 int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
1824 struct cgroup_subsys *ss, *failed_ss = NULL;
1825 struct cgroup *oldcgrp;
1826 struct cgroupfs_root *root = cgrp->root;
1829 /* Nothing to do if the task is already in that cgroup */
1830 oldcgrp = task_cgroup_from_root(tsk, root);
1831 if (cgrp == oldcgrp)
1834 for_each_subsys(root, ss) {
1835 if (ss->can_attach) {
1836 retval = ss->can_attach(ss, cgrp, tsk);
1839 * Remember on which subsystem the can_attach()
1840 * failed, so that we only call cancel_attach()
1841 * against the subsystems whose can_attach()
1842 * succeeded. (See below)
1847 } else if (!capable(CAP_SYS_ADMIN)) {
1848 const struct cred *cred = current_cred(), *tcred;
1850 /* No can_attach() - check perms generically */
1851 tcred = __task_cred(tsk);
1852 if (cred->euid != tcred->uid &&
1853 cred->euid != tcred->suid) {
1857 if (ss->can_attach_task) {
1858 retval = ss->can_attach_task(cgrp, tsk);
1871 retval = cgroup_task_migrate(cgrp, oldcgrp, tsk, false);
1875 for_each_subsys(root, ss) {
1877 ss->pre_attach(cgrp);
1878 if (ss->attach_task)
1879 ss->attach_task(cgrp, tsk);
1881 ss->attach(ss, cgrp, oldcgrp, tsk);
1883 set_bit(CGRP_RELEASABLE, &cgrp->flags);
1884 /* put_css_set will not destroy cg until after an RCU grace period */
1888 * wake up rmdir() waiter. the rmdir should fail since the cgroup
1889 * is no longer empty.
1891 cgroup_wakeup_rmdir_waiter(cgrp);
1894 for_each_subsys(root, ss) {
1895 if (ss == failed_ss)
1897 * This subsystem was the one that failed the
1898 * can_attach() check earlier, so we don't need
1899 * to call cancel_attach() against it or any
1900 * remaining subsystems.
1903 if (ss->cancel_attach)
1904 ss->cancel_attach(ss, cgrp, tsk);
1911 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
1912 * @from: attach to all cgroups of a given task
1913 * @tsk: the task to be attached
1915 int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
1917 struct cgroupfs_root *root;
1921 for_each_active_root(root) {
1922 struct cgroup *from_cg = task_cgroup_from_root(from, root);
1924 retval = cgroup_attach_task(from_cg, tsk);
1932 EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
1935 * cgroup_attach_proc works in two stages, the first of which prefetches all
1936 * new css_sets needed (to make sure we have enough memory before committing
1937 * to the move) and stores them in a list of entries of the following type.
1938 * TODO: possible optimization: use css_set->rcu_head for chaining instead
1940 struct cg_list_entry {
1942 struct list_head links;
1945 static bool css_set_check_fetched(struct cgroup *cgrp,
1946 struct task_struct *tsk, struct css_set *cg,
1947 struct list_head *newcg_list)
1949 struct css_set *newcg;
1950 struct cg_list_entry *cg_entry;
1951 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT];
1953 read_lock(&css_set_lock);
1954 newcg = find_existing_css_set(cg, cgrp, template);
1957 read_unlock(&css_set_lock);
1959 /* doesn't exist at all? */
1962 /* see if it's already in the list */
1963 list_for_each_entry(cg_entry, newcg_list, links) {
1964 if (cg_entry->cg == newcg) {
1976 * Find the new css_set and store it in the list in preparation for moving the
1977 * given task to the given cgroup. Returns 0 or -ENOMEM.
1979 static int css_set_prefetch(struct cgroup *cgrp, struct css_set *cg,
1980 struct list_head *newcg_list)
1982 struct css_set *newcg;
1983 struct cg_list_entry *cg_entry;
1985 /* ensure a new css_set will exist for this thread */
1986 newcg = find_css_set(cg, cgrp);
1989 /* add it to the list */
1990 cg_entry = kmalloc(sizeof(struct cg_list_entry), GFP_KERNEL);
1995 cg_entry->cg = newcg;
1996 list_add(&cg_entry->links, newcg_list);
2001 * cgroup_attach_proc - attach all threads in a threadgroup to a cgroup
2002 * @cgrp: the cgroup to attach to
2003 * @leader: the threadgroup leader task_struct of the group to be attached
2005 * Call holding cgroup_mutex and the threadgroup_fork_lock of the leader. Will
2006 * take task_lock of each thread in leader's threadgroup individually in turn.
2008 int cgroup_attach_proc(struct cgroup *cgrp, struct task_struct *leader)
2010 int retval, i, group_size;
2011 struct cgroup_subsys *ss, *failed_ss = NULL;
2012 bool cancel_failed_ss = false;
2013 /* guaranteed to be initialized later, but the compiler needs this */
2014 struct cgroup *oldcgrp = NULL;
2015 struct css_set *oldcg;
2016 struct cgroupfs_root *root = cgrp->root;
2017 /* threadgroup list cursor and array */
2018 struct task_struct *tsk;
2019 struct flex_array *group;
2021 * we need to make sure we have css_sets for all the tasks we're
2022 * going to move -before- we actually start moving them, so that in
2023 * case we get an ENOMEM we can bail out before making any changes.
2025 struct list_head newcg_list;
2026 struct cg_list_entry *cg_entry, *temp_nobe;
2029 * step 0: in order to do expensive, possibly blocking operations for
2030 * every thread, we cannot iterate the thread group list, since it needs
2031 * rcu or tasklist locked. instead, build an array of all threads in the
2032 * group - threadgroup_fork_lock prevents new threads from appearing,
2033 * and if threads exit, this will just be an over-estimate.
2035 group_size = get_nr_threads(leader);
2036 /* flex_array supports very large thread-groups better than kmalloc. */
2037 group = flex_array_alloc(sizeof(struct task_struct *), group_size,
2041 /* pre-allocate to guarantee space while iterating in rcu read-side. */
2042 retval = flex_array_prealloc(group, 0, group_size - 1, GFP_KERNEL);
2044 goto out_free_group_list;
2046 /* prevent changes to the threadgroup list while we take a snapshot. */
2048 if (!thread_group_leader(leader)) {
2050 * a race with de_thread from another thread's exec() may strip
2051 * us of our leadership, making while_each_thread unsafe to use
2052 * on this task. if this happens, there is no choice but to
2053 * throw this task away and try again (from cgroup_procs_write);
2054 * this is "double-double-toil-and-trouble-check locking".
2058 goto out_free_group_list;
2060 /* take a reference on each task in the group to go in the array. */
2064 /* as per above, nr_threads may decrease, but not increase. */
2065 BUG_ON(i >= group_size);
2066 get_task_struct(tsk);
2068 * saying GFP_ATOMIC has no effect here because we did prealloc
2069 * earlier, but it's good form to communicate our expectations.
2071 retval = flex_array_put_ptr(group, i, tsk, GFP_ATOMIC);
2072 BUG_ON(retval != 0);
2074 } while_each_thread(leader, tsk);
2075 /* remember the number of threads in the array for later. */
2080 * step 1: check that we can legitimately attach to the cgroup.
2082 for_each_subsys(root, ss) {
2083 if (ss->can_attach) {
2084 retval = ss->can_attach(ss, cgrp, leader);
2087 goto out_cancel_attach;
2090 /* a callback to be run on every thread in the threadgroup. */
2091 if (ss->can_attach_task) {
2092 /* run on each task in the threadgroup. */
2093 for (i = 0; i < group_size; i++) {
2094 tsk = flex_array_get_ptr(group, i);
2095 retval = ss->can_attach_task(cgrp, tsk);
2098 cancel_failed_ss = true;
2099 goto out_cancel_attach;
2106 * step 2: make sure css_sets exist for all threads to be migrated.
2107 * we use find_css_set, which allocates a new one if necessary.
2109 INIT_LIST_HEAD(&newcg_list);
2110 for (i = 0; i < group_size; i++) {
2111 tsk = flex_array_get_ptr(group, i);
2112 /* nothing to do if this task is already in the cgroup */
2113 oldcgrp = task_cgroup_from_root(tsk, root);
2114 if (cgrp == oldcgrp)
2116 /* get old css_set pointer */
2118 if (tsk->flags & PF_EXITING) {
2119 /* ignore this task if it's going away */
2123 oldcg = tsk->cgroups;
2126 /* see if the new one for us is already in the list? */
2127 if (css_set_check_fetched(cgrp, tsk, oldcg, &newcg_list)) {
2128 /* was already there, nothing to do. */
2131 /* we don't already have it. get new one. */
2132 retval = css_set_prefetch(cgrp, oldcg, &newcg_list);
2135 goto out_list_teardown;
2140 * step 3: now that we're guaranteed success wrt the css_sets, proceed
2141 * to move all tasks to the new cgroup, calling ss->attach_task for each
2142 * one along the way. there are no failure cases after here, so this is
2145 for_each_subsys(root, ss) {
2147 ss->pre_attach(cgrp);
2149 for (i = 0; i < group_size; i++) {
2150 tsk = flex_array_get_ptr(group, i);
2151 /* leave current thread as it is if it's already there */
2152 oldcgrp = task_cgroup_from_root(tsk, root);
2153 if (cgrp == oldcgrp)
2155 /* attach each task to each subsystem */
2156 for_each_subsys(root, ss) {
2157 if (ss->attach_task)
2158 ss->attach_task(cgrp, tsk);
2160 /* if the thread is PF_EXITING, it can just get skipped. */
2161 retval = cgroup_task_migrate(cgrp, oldcgrp, tsk, true);
2162 BUG_ON(retval != 0 && retval != -ESRCH);
2164 /* nothing is sensitive to fork() after this point. */
2167 * step 4: do expensive, non-thread-specific subsystem callbacks.
2168 * TODO: if ever a subsystem needs to know the oldcgrp for each task
2169 * being moved, this call will need to be reworked to communicate that.
2171 for_each_subsys(root, ss) {
2173 ss->attach(ss, cgrp, oldcgrp, leader);
2177 * step 5: success! and cleanup
2180 cgroup_wakeup_rmdir_waiter(cgrp);
2183 /* clean up the list of prefetched css_sets. */
2184 list_for_each_entry_safe(cg_entry, temp_nobe, &newcg_list, links) {
2185 list_del(&cg_entry->links);
2186 put_css_set(cg_entry->cg);
2190 /* same deal as in cgroup_attach_task */
2192 for_each_subsys(root, ss) {
2193 if (ss == failed_ss) {
2194 if (cancel_failed_ss && ss->cancel_attach)
2195 ss->cancel_attach(ss, cgrp, leader);
2198 if (ss->cancel_attach)
2199 ss->cancel_attach(ss, cgrp, leader);
2202 /* clean up the array of referenced threads in the group. */
2203 for (i = 0; i < group_size; i++) {
2204 tsk = flex_array_get_ptr(group, i);
2205 put_task_struct(tsk);
2207 out_free_group_list:
2208 flex_array_free(group);
2213 * Find the task_struct of the task to attach by vpid and pass it along to the
2214 * function to attach either it or all tasks in its threadgroup. Will take
2215 * cgroup_mutex; may take task_lock of task.
2217 static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
2219 struct task_struct *tsk;
2220 const struct cred *cred = current_cred(), *tcred;
2223 if (!cgroup_lock_live_group(cgrp))
2228 tsk = find_task_by_vpid(pid);
2236 * RCU protects this access, since tsk was found in the
2237 * tid map. a race with de_thread may cause group_leader
2238 * to stop being the leader, but cgroup_attach_proc will
2241 tsk = tsk->group_leader;
2242 } else if (tsk->flags & PF_EXITING) {
2243 /* optimization for the single-task-only case */
2250 * even if we're attaching all tasks in the thread group, we
2251 * only need to check permissions on one of them.
2253 tcred = __task_cred(tsk);
2255 cred->euid != tcred->uid &&
2256 cred->euid != tcred->suid) {
2261 get_task_struct(tsk);
2265 tsk = current->group_leader;
2268 get_task_struct(tsk);
2272 threadgroup_fork_write_lock(tsk);
2273 ret = cgroup_attach_proc(cgrp, tsk);
2274 threadgroup_fork_write_unlock(tsk);
2276 ret = cgroup_attach_task(cgrp, tsk);
2278 put_task_struct(tsk);
2283 static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid)
2285 return attach_task_by_pid(cgrp, pid, false);
2288 static int cgroup_procs_write(struct cgroup *cgrp, struct cftype *cft, u64 tgid)
2293 * attach_proc fails with -EAGAIN if threadgroup leadership
2294 * changes in the middle of the operation, in which case we need
2295 * to find the task_struct for the new leader and start over.
2297 ret = attach_task_by_pid(cgrp, tgid, true);
2298 } while (ret == -EAGAIN);
2303 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
2304 * @cgrp: the cgroup to be checked for liveness
2306 * On success, returns true; the lock should be later released with
2307 * cgroup_unlock(). On failure returns false with no lock held.
2309 bool cgroup_lock_live_group(struct cgroup *cgrp)
2311 mutex_lock(&cgroup_mutex);
2312 if (cgroup_is_removed(cgrp)) {
2313 mutex_unlock(&cgroup_mutex);
2318 EXPORT_SYMBOL_GPL(cgroup_lock_live_group);
2320 static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
2323 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
2324 if (strlen(buffer) >= PATH_MAX)
2326 if (!cgroup_lock_live_group(cgrp))
2328 strcpy(cgrp->root->release_agent_path, buffer);
2333 static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
2334 struct seq_file *seq)
2336 if (!cgroup_lock_live_group(cgrp))
2338 seq_puts(seq, cgrp->root->release_agent_path);
2339 seq_putc(seq, '\n');
2344 /* A buffer size big enough for numbers or short strings */
2345 #define CGROUP_LOCAL_BUFFER_SIZE 64
2347 static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
2349 const char __user *userbuf,
2350 size_t nbytes, loff_t *unused_ppos)
2352 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
2358 if (nbytes >= sizeof(buffer))
2360 if (copy_from_user(buffer, userbuf, nbytes))
2363 buffer[nbytes] = 0; /* nul-terminate */
2364 if (cft->write_u64) {
2365 u64 val = simple_strtoull(strstrip(buffer), &end, 0);
2368 retval = cft->write_u64(cgrp, cft, val);
2370 s64 val = simple_strtoll(strstrip(buffer), &end, 0);
2373 retval = cft->write_s64(cgrp, cft, val);
2380 static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
2382 const char __user *userbuf,
2383 size_t nbytes, loff_t *unused_ppos)
2385 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
2387 size_t max_bytes = cft->max_write_len;
2388 char *buffer = local_buffer;
2391 max_bytes = sizeof(local_buffer) - 1;
2392 if (nbytes >= max_bytes)
2394 /* Allocate a dynamic buffer if we need one */
2395 if (nbytes >= sizeof(local_buffer)) {
2396 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
2400 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
2405 buffer[nbytes] = 0; /* nul-terminate */
2406 retval = cft->write_string(cgrp, cft, strstrip(buffer));
2410 if (buffer != local_buffer)
2415 static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
2416 size_t nbytes, loff_t *ppos)
2418 struct cftype *cft = __d_cft(file->f_dentry);
2419 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
2421 if (cgroup_is_removed(cgrp))
2424 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
2425 if (cft->write_u64 || cft->write_s64)
2426 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
2427 if (cft->write_string)
2428 return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
2430 int ret = cft->trigger(cgrp, (unsigned int)cft->private);
2431 return ret ? ret : nbytes;
2436 static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
2438 char __user *buf, size_t nbytes,
2441 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
2442 u64 val = cft->read_u64(cgrp, cft);
2443 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
2445 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2448 static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
2450 char __user *buf, size_t nbytes,
2453 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
2454 s64 val = cft->read_s64(cgrp, cft);
2455 int len = sprintf(tmp, "%lld\n", (long long) val);
2457 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2460 static ssize_t cgroup_file_read(struct file *file, char __user *buf,
2461 size_t nbytes, loff_t *ppos)
2463 struct cftype *cft = __d_cft(file->f_dentry);
2464 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
2466 if (cgroup_is_removed(cgrp))
2470 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
2472 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
2474 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
2479 * seqfile ops/methods for returning structured data. Currently just
2480 * supports string->u64 maps, but can be extended in future.
2483 struct cgroup_seqfile_state {
2485 struct cgroup *cgroup;
2488 static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
2490 struct seq_file *sf = cb->state;
2491 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
2494 static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2496 struct cgroup_seqfile_state *state = m->private;
2497 struct cftype *cft = state->cft;
2498 if (cft->read_map) {
2499 struct cgroup_map_cb cb = {
2500 .fill = cgroup_map_add,
2503 return cft->read_map(state->cgroup, cft, &cb);
2505 return cft->read_seq_string(state->cgroup, cft, m);
2508 static int cgroup_seqfile_release(struct inode *inode, struct file *file)
2510 struct seq_file *seq = file->private_data;
2511 kfree(seq->private);
2512 return single_release(inode, file);
2515 static const struct file_operations cgroup_seqfile_operations = {
2517 .write = cgroup_file_write,
2518 .llseek = seq_lseek,
2519 .release = cgroup_seqfile_release,
2522 static int cgroup_file_open(struct inode *inode, struct file *file)
2527 err = generic_file_open(inode, file);
2530 cft = __d_cft(file->f_dentry);
2532 if (cft->read_map || cft->read_seq_string) {
2533 struct cgroup_seqfile_state *state =
2534 kzalloc(sizeof(*state), GFP_USER);
2538 state->cgroup = __d_cgrp(file->f_dentry->d_parent);
2539 file->f_op = &cgroup_seqfile_operations;
2540 err = single_open(file, cgroup_seqfile_show, state);
2543 } else if (cft->open)
2544 err = cft->open(inode, file);
2551 static int cgroup_file_release(struct inode *inode, struct file *file)
2553 struct cftype *cft = __d_cft(file->f_dentry);
2555 return cft->release(inode, file);
2560 * cgroup_rename - Only allow simple rename of directories in place.
2562 static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2563 struct inode *new_dir, struct dentry *new_dentry)
2565 if (!S_ISDIR(old_dentry->d_inode->i_mode))
2567 if (new_dentry->d_inode)
2569 if (old_dir != new_dir)
2571 return simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2574 static const struct file_operations cgroup_file_operations = {
2575 .read = cgroup_file_read,
2576 .write = cgroup_file_write,
2577 .llseek = generic_file_llseek,
2578 .open = cgroup_file_open,
2579 .release = cgroup_file_release,
2582 static const struct inode_operations cgroup_dir_inode_operations = {
2583 .lookup = cgroup_lookup,
2584 .mkdir = cgroup_mkdir,
2585 .rmdir = cgroup_rmdir,
2586 .rename = cgroup_rename,
2589 static struct dentry *cgroup_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
2591 if (dentry->d_name.len > NAME_MAX)
2592 return ERR_PTR(-ENAMETOOLONG);
2593 d_add(dentry, NULL);
2598 * Check if a file is a control file
2600 static inline struct cftype *__file_cft(struct file *file)
2602 if (file->f_dentry->d_inode->i_fop != &cgroup_file_operations)
2603 return ERR_PTR(-EINVAL);
2604 return __d_cft(file->f_dentry);
2607 static int cgroup_create_file(struct dentry *dentry, mode_t mode,
2608 struct super_block *sb)
2610 struct inode *inode;
2614 if (dentry->d_inode)
2617 inode = cgroup_new_inode(mode, sb);
2621 if (S_ISDIR(mode)) {
2622 inode->i_op = &cgroup_dir_inode_operations;
2623 inode->i_fop = &simple_dir_operations;
2625 /* start off with i_nlink == 2 (for "." entry) */
2628 /* start with the directory inode held, so that we can
2629 * populate it without racing with another mkdir */
2630 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
2631 } else if (S_ISREG(mode)) {
2633 inode->i_fop = &cgroup_file_operations;
2635 d_instantiate(dentry, inode);
2636 dget(dentry); /* Extra count - pin the dentry in core */
2641 * cgroup_create_dir - create a directory for an object.
2642 * @cgrp: the cgroup we create the directory for. It must have a valid
2643 * ->parent field. And we are going to fill its ->dentry field.
2644 * @dentry: dentry of the new cgroup
2645 * @mode: mode to set on new directory.
2647 static int cgroup_create_dir(struct cgroup *cgrp, struct dentry *dentry,
2650 struct dentry *parent;
2653 parent = cgrp->parent->dentry;
2654 error = cgroup_create_file(dentry, S_IFDIR | mode, cgrp->root->sb);
2656 dentry->d_fsdata = cgrp;
2657 inc_nlink(parent->d_inode);
2658 rcu_assign_pointer(cgrp->dentry, dentry);
2667 * cgroup_file_mode - deduce file mode of a control file
2668 * @cft: the control file in question
2670 * returns cft->mode if ->mode is not 0
2671 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2672 * returns S_IRUGO if it has only a read handler
2673 * returns S_IWUSR if it has only a write hander
2675 static mode_t cgroup_file_mode(const struct cftype *cft)
2682 if (cft->read || cft->read_u64 || cft->read_s64 ||
2683 cft->read_map || cft->read_seq_string)
2686 if (cft->write || cft->write_u64 || cft->write_s64 ||
2687 cft->write_string || cft->trigger)
2693 int cgroup_add_file(struct cgroup *cgrp,
2694 struct cgroup_subsys *subsys,
2695 const struct cftype *cft)
2697 struct dentry *dir = cgrp->dentry;
2698 struct dentry *dentry;
2702 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
2703 if (subsys && !test_bit(ROOT_NOPREFIX, &cgrp->root->flags)) {
2704 strcpy(name, subsys->name);
2707 strcat(name, cft->name);
2708 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
2709 dentry = lookup_one_len(name, dir, strlen(name));
2710 if (!IS_ERR(dentry)) {
2711 mode = cgroup_file_mode(cft);
2712 error = cgroup_create_file(dentry, mode | S_IFREG,
2715 dentry->d_fsdata = (void *)cft;
2718 error = PTR_ERR(dentry);
2721 EXPORT_SYMBOL_GPL(cgroup_add_file);
2723 int cgroup_add_files(struct cgroup *cgrp,
2724 struct cgroup_subsys *subsys,
2725 const struct cftype cft[],
2729 for (i = 0; i < count; i++) {
2730 err = cgroup_add_file(cgrp, subsys, &cft[i]);
2736 EXPORT_SYMBOL_GPL(cgroup_add_files);
2739 * cgroup_task_count - count the number of tasks in a cgroup.
2740 * @cgrp: the cgroup in question
2742 * Return the number of tasks in the cgroup.
2744 int cgroup_task_count(const struct cgroup *cgrp)
2747 struct cg_cgroup_link *link;
2749 read_lock(&css_set_lock);
2750 list_for_each_entry(link, &cgrp->css_sets, cgrp_link_list) {
2751 count += atomic_read(&link->cg->refcount);
2753 read_unlock(&css_set_lock);
2758 * Advance a list_head iterator. The iterator should be positioned at
2759 * the start of a css_set
2761 static void cgroup_advance_iter(struct cgroup *cgrp,
2762 struct cgroup_iter *it)
2764 struct list_head *l = it->cg_link;
2765 struct cg_cgroup_link *link;
2768 /* Advance to the next non-empty css_set */
2771 if (l == &cgrp->css_sets) {
2775 link = list_entry(l, struct cg_cgroup_link, cgrp_link_list);
2777 } while (list_empty(&cg->tasks));
2779 it->task = cg->tasks.next;
2783 * To reduce the fork() overhead for systems that are not actually
2784 * using their cgroups capability, we don't maintain the lists running
2785 * through each css_set to its tasks until we see the list actually
2786 * used - in other words after the first call to cgroup_iter_start().
2788 * The tasklist_lock is not held here, as do_each_thread() and
2789 * while_each_thread() are protected by RCU.
2791 static void cgroup_enable_task_cg_lists(void)
2793 struct task_struct *p, *g;
2794 write_lock(&css_set_lock);
2795 use_task_css_set_links = 1;
2796 do_each_thread(g, p) {
2799 * We should check if the process is exiting, otherwise
2800 * it will race with cgroup_exit() in that the list
2801 * entry won't be deleted though the process has exited.
2803 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
2804 list_add(&p->cg_list, &p->cgroups->tasks);
2806 } while_each_thread(g, p);
2807 write_unlock(&css_set_lock);
2810 void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
2813 * The first time anyone tries to iterate across a cgroup,
2814 * we need to enable the list linking each css_set to its
2815 * tasks, and fix up all existing tasks.
2817 if (!use_task_css_set_links)
2818 cgroup_enable_task_cg_lists();
2820 read_lock(&css_set_lock);
2821 it->cg_link = &cgrp->css_sets;
2822 cgroup_advance_iter(cgrp, it);
2825 struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
2826 struct cgroup_iter *it)
2828 struct task_struct *res;
2829 struct list_head *l = it->task;
2830 struct cg_cgroup_link *link;
2832 /* If the iterator cg is NULL, we have no tasks */
2835 res = list_entry(l, struct task_struct, cg_list);
2836 /* Advance iterator to find next entry */
2838 link = list_entry(it->cg_link, struct cg_cgroup_link, cgrp_link_list);
2839 if (l == &link->cg->tasks) {
2840 /* We reached the end of this task list - move on to
2841 * the next cg_cgroup_link */
2842 cgroup_advance_iter(cgrp, it);
2849 void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
2851 read_unlock(&css_set_lock);
2854 static inline int started_after_time(struct task_struct *t1,
2855 struct timespec *time,
2856 struct task_struct *t2)
2858 int start_diff = timespec_compare(&t1->start_time, time);
2859 if (start_diff > 0) {
2861 } else if (start_diff < 0) {
2865 * Arbitrarily, if two processes started at the same
2866 * time, we'll say that the lower pointer value
2867 * started first. Note that t2 may have exited by now
2868 * so this may not be a valid pointer any longer, but
2869 * that's fine - it still serves to distinguish
2870 * between two tasks started (effectively) simultaneously.
2877 * This function is a callback from heap_insert() and is used to order
2879 * In this case we order the heap in descending task start time.
2881 static inline int started_after(void *p1, void *p2)
2883 struct task_struct *t1 = p1;
2884 struct task_struct *t2 = p2;
2885 return started_after_time(t1, &t2->start_time, t2);
2889 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
2890 * @scan: struct cgroup_scanner containing arguments for the scan
2892 * Arguments include pointers to callback functions test_task() and
2894 * Iterate through all the tasks in a cgroup, calling test_task() for each,
2895 * and if it returns true, call process_task() for it also.
2896 * The test_task pointer may be NULL, meaning always true (select all tasks).
2897 * Effectively duplicates cgroup_iter_{start,next,end}()
2898 * but does not lock css_set_lock for the call to process_task().
2899 * The struct cgroup_scanner may be embedded in any structure of the caller's
2901 * It is guaranteed that process_task() will act on every task that
2902 * is a member of the cgroup for the duration of this call. This
2903 * function may or may not call process_task() for tasks that exit
2904 * or move to a different cgroup during the call, or are forked or
2905 * move into the cgroup during the call.
2907 * Note that test_task() may be called with locks held, and may in some
2908 * situations be called multiple times for the same task, so it should
2910 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
2911 * pre-allocated and will be used for heap operations (and its "gt" member will
2912 * be overwritten), else a temporary heap will be used (allocation of which
2913 * may cause this function to fail).
2915 int cgroup_scan_tasks(struct cgroup_scanner *scan)
2918 struct cgroup_iter it;
2919 struct task_struct *p, *dropped;
2920 /* Never dereference latest_task, since it's not refcounted */
2921 struct task_struct *latest_task = NULL;
2922 struct ptr_heap tmp_heap;
2923 struct ptr_heap *heap;
2924 struct timespec latest_time = { 0, 0 };
2927 /* The caller supplied our heap and pre-allocated its memory */
2929 heap->gt = &started_after;
2931 /* We need to allocate our own heap memory */
2933 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
2935 /* cannot allocate the heap */
2941 * Scan tasks in the cgroup, using the scanner's "test_task" callback
2942 * to determine which are of interest, and using the scanner's
2943 * "process_task" callback to process any of them that need an update.
2944 * Since we don't want to hold any locks during the task updates,
2945 * gather tasks to be processed in a heap structure.
2946 * The heap is sorted by descending task start time.
2947 * If the statically-sized heap fills up, we overflow tasks that
2948 * started later, and in future iterations only consider tasks that
2949 * started after the latest task in the previous pass. This
2950 * guarantees forward progress and that we don't miss any tasks.
2953 cgroup_iter_start(scan->cg, &it);
2954 while ((p = cgroup_iter_next(scan->cg, &it))) {
2956 * Only affect tasks that qualify per the caller's callback,
2957 * if he provided one
2959 if (scan->test_task && !scan->test_task(p, scan))
2962 * Only process tasks that started after the last task
2965 if (!started_after_time(p, &latest_time, latest_task))
2967 dropped = heap_insert(heap, p);
2968 if (dropped == NULL) {
2970 * The new task was inserted; the heap wasn't
2974 } else if (dropped != p) {
2976 * The new task was inserted, and pushed out a
2980 put_task_struct(dropped);
2983 * Else the new task was newer than anything already in
2984 * the heap and wasn't inserted
2987 cgroup_iter_end(scan->cg, &it);
2990 for (i = 0; i < heap->size; i++) {
2991 struct task_struct *q = heap->ptrs[i];
2993 latest_time = q->start_time;
2996 /* Process the task per the caller's callback */
2997 scan->process_task(q, scan);
3001 * If we had to process any tasks at all, scan again
3002 * in case some of them were in the middle of forking
3003 * children that didn't get processed.
3004 * Not the most efficient way to do it, but it avoids
3005 * having to take callback_mutex in the fork path
3009 if (heap == &tmp_heap)
3010 heap_free(&tmp_heap);
3015 * Stuff for reading the 'tasks'/'procs' files.
3017 * Reading this file can return large amounts of data if a cgroup has
3018 * *lots* of attached tasks. So it may need several calls to read(),
3019 * but we cannot guarantee that the information we produce is correct
3020 * unless we produce it entirely atomically.
3025 * The following two functions "fix" the issue where there are more pids
3026 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3027 * TODO: replace with a kernel-wide solution to this problem
3029 #define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3030 static void *pidlist_allocate(int count)
3032 if (PIDLIST_TOO_LARGE(count))
3033 return vmalloc(count * sizeof(pid_t));
3035 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3037 static void pidlist_free(void *p)
3039 if (is_vmalloc_addr(p))
3044 static void *pidlist_resize(void *p, int newcount)
3047 /* note: if new alloc fails, old p will still be valid either way */
3048 if (is_vmalloc_addr(p)) {
3049 newlist = vmalloc(newcount * sizeof(pid_t));
3052 memcpy(newlist, p, newcount * sizeof(pid_t));
3055 newlist = krealloc(p, newcount * sizeof(pid_t), GFP_KERNEL);
3061 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
3062 * If the new stripped list is sufficiently smaller and there's enough memory
3063 * to allocate a new buffer, will let go of the unneeded memory. Returns the
3064 * number of unique elements.
3066 /* is the size difference enough that we should re-allocate the array? */
3067 #define PIDLIST_REALLOC_DIFFERENCE(old, new) ((old) - PAGE_SIZE >= (new))
3068 static int pidlist_uniq(pid_t **p, int length)
3075 * we presume the 0th element is unique, so i starts at 1. trivial
3076 * edge cases first; no work needs to be done for either
3078 if (length == 0 || length == 1)
3080 /* src and dest walk down the list; dest counts unique elements */
3081 for (src = 1; src < length; src++) {
3082 /* find next unique element */
3083 while (list[src] == list[src-1]) {
3088 /* dest always points to where the next unique element goes */
3089 list[dest] = list[src];
3094 * if the length difference is large enough, we want to allocate a
3095 * smaller buffer to save memory. if this fails due to out of memory,
3096 * we'll just stay with what we've got.
3098 if (PIDLIST_REALLOC_DIFFERENCE(length, dest)) {
3099 newlist = pidlist_resize(list, dest);
3106 static int cmppid(const void *a, const void *b)
3108 return *(pid_t *)a - *(pid_t *)b;
3112 * find the appropriate pidlist for our purpose (given procs vs tasks)
3113 * returns with the lock on that pidlist already held, and takes care
3114 * of the use count, or returns NULL with no locks held if we're out of
3117 static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3118 enum cgroup_filetype type)
3120 struct cgroup_pidlist *l;
3121 /* don't need task_nsproxy() if we're looking at ourself */
3122 struct pid_namespace *ns = current->nsproxy->pid_ns;
3125 * We can't drop the pidlist_mutex before taking the l->mutex in case
3126 * the last ref-holder is trying to remove l from the list at the same
3127 * time. Holding the pidlist_mutex precludes somebody taking whichever
3128 * list we find out from under us - compare release_pid_array().
3130 mutex_lock(&cgrp->pidlist_mutex);
3131 list_for_each_entry(l, &cgrp->pidlists, links) {
3132 if (l->key.type == type && l->key.ns == ns) {
3133 /* make sure l doesn't vanish out from under us */
3134 down_write(&l->mutex);
3135 mutex_unlock(&cgrp->pidlist_mutex);
3139 /* entry not found; create a new one */
3140 l = kmalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
3142 mutex_unlock(&cgrp->pidlist_mutex);
3145 init_rwsem(&l->mutex);
3146 down_write(&l->mutex);
3148 l->key.ns = get_pid_ns(ns);
3149 l->use_count = 0; /* don't increment here */
3152 list_add(&l->links, &cgrp->pidlists);
3153 mutex_unlock(&cgrp->pidlist_mutex);
3158 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3160 static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3161 struct cgroup_pidlist **lp)
3165 int pid, n = 0; /* used for populating the array */
3166 struct cgroup_iter it;
3167 struct task_struct *tsk;
3168 struct cgroup_pidlist *l;
3171 * If cgroup gets more users after we read count, we won't have
3172 * enough space - tough. This race is indistinguishable to the
3173 * caller from the case that the additional cgroup users didn't
3174 * show up until sometime later on.
3176 length = cgroup_task_count(cgrp);
3177 array = pidlist_allocate(length);
3180 /* now, populate the array */
3181 cgroup_iter_start(cgrp, &it);
3182 while ((tsk = cgroup_iter_next(cgrp, &it))) {
3183 if (unlikely(n == length))
3185 /* get tgid or pid for procs or tasks file respectively */
3186 if (type == CGROUP_FILE_PROCS)
3187 pid = task_tgid_vnr(tsk);
3189 pid = task_pid_vnr(tsk);
3190 if (pid > 0) /* make sure to only use valid results */
3193 cgroup_iter_end(cgrp, &it);
3195 /* now sort & (if procs) strip out duplicates */
3196 sort(array, length, sizeof(pid_t), cmppid, NULL);
3197 if (type == CGROUP_FILE_PROCS)
3198 length = pidlist_uniq(&array, length);
3199 l = cgroup_pidlist_find(cgrp, type);
3201 pidlist_free(array);
3204 /* store array, freeing old if necessary - lock already held */
3205 pidlist_free(l->list);
3209 up_write(&l->mutex);
3215 * cgroupstats_build - build and fill cgroupstats
3216 * @stats: cgroupstats to fill information into
3217 * @dentry: A dentry entry belonging to the cgroup for which stats have
3220 * Build and fill cgroupstats so that taskstats can export it to user
3223 int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3226 struct cgroup *cgrp;
3227 struct cgroup_iter it;
3228 struct task_struct *tsk;
3231 * Validate dentry by checking the superblock operations,
3232 * and make sure it's a directory.
3234 if (dentry->d_sb->s_op != &cgroup_ops ||
3235 !S_ISDIR(dentry->d_inode->i_mode))
3239 cgrp = dentry->d_fsdata;
3241 cgroup_iter_start(cgrp, &it);
3242 while ((tsk = cgroup_iter_next(cgrp, &it))) {
3243 switch (tsk->state) {
3245 stats->nr_running++;
3247 case TASK_INTERRUPTIBLE:
3248 stats->nr_sleeping++;
3250 case TASK_UNINTERRUPTIBLE:
3251 stats->nr_uninterruptible++;
3254 stats->nr_stopped++;
3257 if (delayacct_is_task_waiting_on_io(tsk))
3258 stats->nr_io_wait++;
3262 cgroup_iter_end(cgrp, &it);
3270 * seq_file methods for the tasks/procs files. The seq_file position is the
3271 * next pid to display; the seq_file iterator is a pointer to the pid
3272 * in the cgroup->l->list array.
3275 static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
3278 * Initially we receive a position value that corresponds to
3279 * one more than the last pid shown (or 0 on the first call or
3280 * after a seek to the start). Use a binary-search to find the
3281 * next pid to display, if any
3283 struct cgroup_pidlist *l = s->private;
3284 int index = 0, pid = *pos;
3287 down_read(&l->mutex);
3289 int end = l->length;
3291 while (index < end) {
3292 int mid = (index + end) / 2;
3293 if (l->list[mid] == pid) {
3296 } else if (l->list[mid] <= pid)
3302 /* If we're off the end of the array, we're done */
3303 if (index >= l->length)
3305 /* Update the abstract position to be the actual pid that we found */
3306 iter = l->list + index;
3311 static void cgroup_pidlist_stop(struct seq_file *s, void *v)
3313 struct cgroup_pidlist *l = s->private;
3317 static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
3319 struct cgroup_pidlist *l = s->private;
3321 pid_t *end = l->list + l->length;
3323 * Advance to the next pid in the array. If this goes off the
3335 static int cgroup_pidlist_show(struct seq_file *s, void *v)
3337 return seq_printf(s, "%d\n", *(int *)v);
3341 * seq_operations functions for iterating on pidlists through seq_file -
3342 * independent of whether it's tasks or procs
3344 static const struct seq_operations cgroup_pidlist_seq_operations = {
3345 .start = cgroup_pidlist_start,
3346 .stop = cgroup_pidlist_stop,
3347 .next = cgroup_pidlist_next,
3348 .show = cgroup_pidlist_show,
3351 static void cgroup_release_pid_array(struct cgroup_pidlist *l)
3354 * the case where we're the last user of this particular pidlist will
3355 * have us remove it from the cgroup's list, which entails taking the
3356 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3357 * pidlist_mutex, we have to take pidlist_mutex first.
3359 mutex_lock(&l->owner->pidlist_mutex);
3360 down_write(&l->mutex);
3361 BUG_ON(!l->use_count);
3362 if (!--l->use_count) {
3363 /* we're the last user if refcount is 0; remove and free */
3364 list_del(&l->links);
3365 mutex_unlock(&l->owner->pidlist_mutex);
3366 pidlist_free(l->list);
3367 put_pid_ns(l->key.ns);
3368 up_write(&l->mutex);
3372 mutex_unlock(&l->owner->pidlist_mutex);
3373 up_write(&l->mutex);
3376 static int cgroup_pidlist_release(struct inode *inode, struct file *file)
3378 struct cgroup_pidlist *l;
3379 if (!(file->f_mode & FMODE_READ))
3382 * the seq_file will only be initialized if the file was opened for
3383 * reading; hence we check if it's not null only in that case.
3385 l = ((struct seq_file *)file->private_data)->private;
3386 cgroup_release_pid_array(l);
3387 return seq_release(inode, file);
3390 static const struct file_operations cgroup_pidlist_operations = {
3392 .llseek = seq_lseek,
3393 .write = cgroup_file_write,
3394 .release = cgroup_pidlist_release,
3398 * The following functions handle opens on a file that displays a pidlist
3399 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3402 /* helper function for the two below it */
3403 static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
3405 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
3406 struct cgroup_pidlist *l;
3409 /* Nothing to do for write-only files */
3410 if (!(file->f_mode & FMODE_READ))
3413 /* have the array populated */
3414 retval = pidlist_array_load(cgrp, type, &l);
3417 /* configure file information */
3418 file->f_op = &cgroup_pidlist_operations;
3420 retval = seq_open(file, &cgroup_pidlist_seq_operations);
3422 cgroup_release_pid_array(l);
3425 ((struct seq_file *)file->private_data)->private = l;
3428 static int cgroup_tasks_open(struct inode *unused, struct file *file)
3430 return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
3432 static int cgroup_procs_open(struct inode *unused, struct file *file)
3434 return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
3437 static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
3440 return notify_on_release(cgrp);
3443 static int cgroup_write_notify_on_release(struct cgroup *cgrp,
3447 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
3449 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3451 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3456 * Unregister event and free resources.
3458 * Gets called from workqueue.
3460 static void cgroup_event_remove(struct work_struct *work)
3462 struct cgroup_event *event = container_of(work, struct cgroup_event,
3464 struct cgroup *cgrp = event->cgrp;
3466 event->cft->unregister_event(cgrp, event->cft, event->eventfd);
3468 eventfd_ctx_put(event->eventfd);
3474 * Gets called on POLLHUP on eventfd when user closes it.
3476 * Called with wqh->lock held and interrupts disabled.
3478 static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
3479 int sync, void *key)
3481 struct cgroup_event *event = container_of(wait,
3482 struct cgroup_event, wait);
3483 struct cgroup *cgrp = event->cgrp;
3484 unsigned long flags = (unsigned long)key;
3486 if (flags & POLLHUP) {
3487 __remove_wait_queue(event->wqh, &event->wait);
3488 spin_lock(&cgrp->event_list_lock);
3489 list_del(&event->list);
3490 spin_unlock(&cgrp->event_list_lock);
3492 * We are in atomic context, but cgroup_event_remove() may
3493 * sleep, so we have to call it in workqueue.
3495 schedule_work(&event->remove);
3501 static void cgroup_event_ptable_queue_proc(struct file *file,
3502 wait_queue_head_t *wqh, poll_table *pt)
3504 struct cgroup_event *event = container_of(pt,
3505 struct cgroup_event, pt);
3508 add_wait_queue(wqh, &event->wait);
3512 * Parse input and register new cgroup event handler.
3514 * Input must be in format '<event_fd> <control_fd> <args>'.
3515 * Interpretation of args is defined by control file implementation.
3517 static int cgroup_write_event_control(struct cgroup *cgrp, struct cftype *cft,
3520 struct cgroup_event *event = NULL;
3521 unsigned int efd, cfd;
3522 struct file *efile = NULL;
3523 struct file *cfile = NULL;
3527 efd = simple_strtoul(buffer, &endp, 10);
3532 cfd = simple_strtoul(buffer, &endp, 10);
3533 if ((*endp != ' ') && (*endp != '\0'))
3537 event = kzalloc(sizeof(*event), GFP_KERNEL);
3541 INIT_LIST_HEAD(&event->list);
3542 init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
3543 init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
3544 INIT_WORK(&event->remove, cgroup_event_remove);
3546 efile = eventfd_fget(efd);
3547 if (IS_ERR(efile)) {
3548 ret = PTR_ERR(efile);
3552 event->eventfd = eventfd_ctx_fileget(efile);
3553 if (IS_ERR(event->eventfd)) {
3554 ret = PTR_ERR(event->eventfd);
3564 /* the process need read permission on control file */
3565 ret = file_permission(cfile, MAY_READ);
3569 event->cft = __file_cft(cfile);
3570 if (IS_ERR(event->cft)) {
3571 ret = PTR_ERR(event->cft);
3575 if (!event->cft->register_event || !event->cft->unregister_event) {
3580 ret = event->cft->register_event(cgrp, event->cft,
3581 event->eventfd, buffer);
3585 if (efile->f_op->poll(efile, &event->pt) & POLLHUP) {
3586 event->cft->unregister_event(cgrp, event->cft, event->eventfd);
3592 * Events should be removed after rmdir of cgroup directory, but before
3593 * destroying subsystem state objects. Let's take reference to cgroup
3594 * directory dentry to do that.
3598 spin_lock(&cgrp->event_list_lock);
3599 list_add(&event->list, &cgrp->event_list);
3600 spin_unlock(&cgrp->event_list_lock);
3611 if (event && event->eventfd && !IS_ERR(event->eventfd))
3612 eventfd_ctx_put(event->eventfd);
3614 if (!IS_ERR_OR_NULL(efile))
3622 static u64 cgroup_clone_children_read(struct cgroup *cgrp,
3625 return clone_children(cgrp);
3628 static int cgroup_clone_children_write(struct cgroup *cgrp,
3633 set_bit(CGRP_CLONE_CHILDREN, &cgrp->flags);
3635 clear_bit(CGRP_CLONE_CHILDREN, &cgrp->flags);
3640 * for the common functions, 'private' gives the type of file
3642 /* for hysterical raisins, we can't put this on the older files */
3643 #define CGROUP_FILE_GENERIC_PREFIX "cgroup."
3644 static struct cftype files[] = {
3647 .open = cgroup_tasks_open,
3648 .write_u64 = cgroup_tasks_write,
3649 .release = cgroup_pidlist_release,
3650 .mode = S_IRUGO | S_IWUSR,
3653 .name = CGROUP_FILE_GENERIC_PREFIX "procs",
3654 .open = cgroup_procs_open,
3655 .write_u64 = cgroup_procs_write,
3656 .release = cgroup_pidlist_release,
3657 .mode = S_IRUGO | S_IWUSR,
3660 .name = "notify_on_release",
3661 .read_u64 = cgroup_read_notify_on_release,
3662 .write_u64 = cgroup_write_notify_on_release,
3665 .name = CGROUP_FILE_GENERIC_PREFIX "event_control",
3666 .write_string = cgroup_write_event_control,
3670 .name = "cgroup.clone_children",
3671 .read_u64 = cgroup_clone_children_read,
3672 .write_u64 = cgroup_clone_children_write,
3676 static struct cftype cft_release_agent = {
3677 .name = "release_agent",
3678 .read_seq_string = cgroup_release_agent_show,
3679 .write_string = cgroup_release_agent_write,
3680 .max_write_len = PATH_MAX,
3683 static int cgroup_populate_dir(struct cgroup *cgrp)
3686 struct cgroup_subsys *ss;
3688 /* First clear out any existing files */
3689 cgroup_clear_directory(cgrp->dentry);
3691 err = cgroup_add_files(cgrp, NULL, files, ARRAY_SIZE(files));
3695 if (cgrp == cgrp->top_cgroup) {
3696 if ((err = cgroup_add_file(cgrp, NULL, &cft_release_agent)) < 0)
3700 for_each_subsys(cgrp->root, ss) {
3701 if (ss->populate && (err = ss->populate(ss, cgrp)) < 0)
3704 /* This cgroup is ready now */
3705 for_each_subsys(cgrp->root, ss) {
3706 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
3708 * Update id->css pointer and make this css visible from
3709 * CSS ID functions. This pointer will be dereferened
3710 * from RCU-read-side without locks.
3713 rcu_assign_pointer(css->id->css, css);
3719 static void init_cgroup_css(struct cgroup_subsys_state *css,
3720 struct cgroup_subsys *ss,
3721 struct cgroup *cgrp)
3724 atomic_set(&css->refcnt, 1);
3727 if (cgrp == dummytop)
3728 set_bit(CSS_ROOT, &css->flags);
3729 BUG_ON(cgrp->subsys[ss->subsys_id]);
3730 cgrp->subsys[ss->subsys_id] = css;
3733 static void cgroup_lock_hierarchy(struct cgroupfs_root *root)
3735 /* We need to take each hierarchy_mutex in a consistent order */
3739 * No worry about a race with rebind_subsystems that might mess up the
3740 * locking order, since both parties are under cgroup_mutex.
3742 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3743 struct cgroup_subsys *ss = subsys[i];
3746 if (ss->root == root)
3747 mutex_lock(&ss->hierarchy_mutex);
3751 static void cgroup_unlock_hierarchy(struct cgroupfs_root *root)
3755 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3756 struct cgroup_subsys *ss = subsys[i];
3759 if (ss->root == root)
3760 mutex_unlock(&ss->hierarchy_mutex);
3765 * cgroup_create - create a cgroup
3766 * @parent: cgroup that will be parent of the new cgroup
3767 * @dentry: dentry of the new cgroup
3768 * @mode: mode to set on new inode
3770 * Must be called with the mutex on the parent inode held
3772 static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
3775 struct cgroup *cgrp;
3776 struct cgroupfs_root *root = parent->root;
3778 struct cgroup_subsys *ss;
3779 struct super_block *sb = root->sb;
3781 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
3785 /* Grab a reference on the superblock so the hierarchy doesn't
3786 * get deleted on unmount if there are child cgroups. This
3787 * can be done outside cgroup_mutex, since the sb can't
3788 * disappear while someone has an open control file on the
3790 atomic_inc(&sb->s_active);
3792 mutex_lock(&cgroup_mutex);
3794 init_cgroup_housekeeping(cgrp);
3796 cgrp->parent = parent;
3797 cgrp->root = parent->root;
3798 cgrp->top_cgroup = parent->top_cgroup;
3800 if (notify_on_release(parent))
3801 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3803 if (clone_children(parent))
3804 set_bit(CGRP_CLONE_CHILDREN, &cgrp->flags);
3806 for_each_subsys(root, ss) {
3807 struct cgroup_subsys_state *css = ss->create(ss, cgrp);
3813 init_cgroup_css(css, ss, cgrp);
3815 err = alloc_css_id(ss, parent, cgrp);
3819 /* At error, ->destroy() callback has to free assigned ID. */
3820 if (clone_children(parent) && ss->post_clone)
3821 ss->post_clone(ss, cgrp);
3824 cgroup_lock_hierarchy(root);
3825 list_add(&cgrp->sibling, &cgrp->parent->children);
3826 cgroup_unlock_hierarchy(root);
3827 root->number_of_cgroups++;
3829 err = cgroup_create_dir(cgrp, dentry, mode);
3833 set_bit(CGRP_RELEASABLE, &parent->flags);
3835 /* The cgroup directory was pre-locked for us */
3836 BUG_ON(!mutex_is_locked(&cgrp->dentry->d_inode->i_mutex));
3838 err = cgroup_populate_dir(cgrp);
3839 /* If err < 0, we have a half-filled directory - oh well ;) */
3841 mutex_unlock(&cgroup_mutex);
3842 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
3848 cgroup_lock_hierarchy(root);
3849 list_del(&cgrp->sibling);
3850 cgroup_unlock_hierarchy(root);
3851 root->number_of_cgroups--;
3855 for_each_subsys(root, ss) {
3856 if (cgrp->subsys[ss->subsys_id])
3857 ss->destroy(ss, cgrp);
3860 mutex_unlock(&cgroup_mutex);
3862 /* Release the reference count that we took on the superblock */
3863 deactivate_super(sb);
3869 static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode)
3871 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
3873 /* the vfs holds inode->i_mutex already */
3874 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
3877 static int cgroup_has_css_refs(struct cgroup *cgrp)
3879 /* Check the reference count on each subsystem. Since we
3880 * already established that there are no tasks in the
3881 * cgroup, if the css refcount is also 1, then there should
3882 * be no outstanding references, so the subsystem is safe to
3883 * destroy. We scan across all subsystems rather than using
3884 * the per-hierarchy linked list of mounted subsystems since
3885 * we can be called via check_for_release() with no
3886 * synchronization other than RCU, and the subsystem linked
3887 * list isn't RCU-safe */
3890 * We won't need to lock the subsys array, because the subsystems
3891 * we're concerned about aren't going anywhere since our cgroup root
3892 * has a reference on them.
3894 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3895 struct cgroup_subsys *ss = subsys[i];
3896 struct cgroup_subsys_state *css;
3897 /* Skip subsystems not present or not in this hierarchy */
3898 if (ss == NULL || ss->root != cgrp->root)
3900 css = cgrp->subsys[ss->subsys_id];
3901 /* When called from check_for_release() it's possible
3902 * that by this point the cgroup has been removed
3903 * and the css deleted. But a false-positive doesn't
3904 * matter, since it can only happen if the cgroup
3905 * has been deleted and hence no longer needs the
3906 * release agent to be called anyway. */
3907 if (css && (atomic_read(&css->refcnt) > 1))
3914 * Atomically mark all (or else none) of the cgroup's CSS objects as
3915 * CSS_REMOVED. Return true on success, or false if the cgroup has
3916 * busy subsystems. Call with cgroup_mutex held
3919 static int cgroup_clear_css_refs(struct cgroup *cgrp)
3921 struct cgroup_subsys *ss;
3922 unsigned long flags;
3923 bool failed = false;
3924 local_irq_save(flags);
3925 for_each_subsys(cgrp->root, ss) {
3926 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
3929 /* We can only remove a CSS with a refcnt==1 */
3930 refcnt = atomic_read(&css->refcnt);
3937 * Drop the refcnt to 0 while we check other
3938 * subsystems. This will cause any racing
3939 * css_tryget() to spin until we set the
3940 * CSS_REMOVED bits or abort
3942 if (atomic_cmpxchg(&css->refcnt, refcnt, 0) == refcnt)
3948 for_each_subsys(cgrp->root, ss) {
3949 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
3952 * Restore old refcnt if we previously managed
3953 * to clear it from 1 to 0
3955 if (!atomic_read(&css->refcnt))
3956 atomic_set(&css->refcnt, 1);
3958 /* Commit the fact that the CSS is removed */
3959 set_bit(CSS_REMOVED, &css->flags);
3962 local_irq_restore(flags);
3966 /* checks if all of the css_sets attached to a cgroup have a refcount of 0.
3967 * Must be called with css_set_lock held */
3968 static int cgroup_css_sets_empty(struct cgroup *cgrp)
3970 struct cg_cgroup_link *link;
3972 list_for_each_entry(link, &cgrp->css_sets, cgrp_link_list) {
3973 struct css_set *cg = link->cg;
3974 if (atomic_read(&cg->refcount) > 0)
3981 static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
3983 struct cgroup *cgrp = dentry->d_fsdata;
3985 struct cgroup *parent;
3987 struct cgroup_event *event, *tmp;
3990 /* the vfs holds both inode->i_mutex already */
3992 mutex_lock(&cgroup_mutex);
3993 if (!cgroup_css_sets_empty(cgrp)) {
3994 mutex_unlock(&cgroup_mutex);
3997 if (!list_empty(&cgrp->children)) {
3998 mutex_unlock(&cgroup_mutex);
4001 mutex_unlock(&cgroup_mutex);
4004 * In general, subsystem has no css->refcnt after pre_destroy(). But
4005 * in racy cases, subsystem may have to get css->refcnt after
4006 * pre_destroy() and it makes rmdir return with -EBUSY. This sometimes
4007 * make rmdir return -EBUSY too often. To avoid that, we use waitqueue
4008 * for cgroup's rmdir. CGRP_WAIT_ON_RMDIR is for synchronizing rmdir
4009 * and subsystem's reference count handling. Please see css_get/put
4010 * and css_tryget() and cgroup_wakeup_rmdir_waiter() implementation.
4012 set_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
4015 * Call pre_destroy handlers of subsys. Notify subsystems
4016 * that rmdir() request comes.
4018 ret = cgroup_call_pre_destroy(cgrp);
4020 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
4024 mutex_lock(&cgroup_mutex);
4025 parent = cgrp->parent;
4026 if (!cgroup_css_sets_empty(cgrp) || !list_empty(&cgrp->children)) {
4027 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
4028 mutex_unlock(&cgroup_mutex);
4031 prepare_to_wait(&cgroup_rmdir_waitq, &wait, TASK_INTERRUPTIBLE);
4032 if (!cgroup_clear_css_refs(cgrp)) {
4033 mutex_unlock(&cgroup_mutex);
4035 * Because someone may call cgroup_wakeup_rmdir_waiter() before
4036 * prepare_to_wait(), we need to check this flag.
4038 if (test_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags))
4040 finish_wait(&cgroup_rmdir_waitq, &wait);
4041 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
4042 if (signal_pending(current))
4046 /* NO css_tryget() can success after here. */
4047 finish_wait(&cgroup_rmdir_waitq, &wait);
4048 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
4050 spin_lock(&release_list_lock);
4051 set_bit(CGRP_REMOVED, &cgrp->flags);
4052 if (!list_empty(&cgrp->release_list))
4053 list_del_init(&cgrp->release_list);
4054 spin_unlock(&release_list_lock);
4056 cgroup_lock_hierarchy(cgrp->root);
4057 /* delete this cgroup from parent->children */
4058 list_del_init(&cgrp->sibling);
4059 cgroup_unlock_hierarchy(cgrp->root);
4061 d = dget(cgrp->dentry);
4063 cgroup_d_remove_dir(d);
4066 check_for_release(parent);
4069 * Unregister events and notify userspace.
4070 * Notify userspace about cgroup removing only after rmdir of cgroup
4071 * directory to avoid race between userspace and kernelspace
4073 spin_lock(&cgrp->event_list_lock);
4074 list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) {
4075 list_del(&event->list);
4076 remove_wait_queue(event->wqh, &event->wait);
4077 eventfd_signal(event->eventfd, 1);
4078 schedule_work(&event->remove);
4080 spin_unlock(&cgrp->event_list_lock);
4082 mutex_unlock(&cgroup_mutex);
4086 static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
4088 struct cgroup_subsys_state *css;
4090 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
4092 /* Create the top cgroup state for this subsystem */
4093 list_add(&ss->sibling, &rootnode.subsys_list);
4094 ss->root = &rootnode;
4095 css = ss->create(ss, dummytop);
4096 /* We don't handle early failures gracefully */
4097 BUG_ON(IS_ERR(css));
4098 init_cgroup_css(css, ss, dummytop);
4100 /* Update the init_css_set to contain a subsys
4101 * pointer to this state - since the subsystem is
4102 * newly registered, all tasks and hence the
4103 * init_css_set is in the subsystem's top cgroup. */
4104 init_css_set.subsys[ss->subsys_id] = dummytop->subsys[ss->subsys_id];
4106 need_forkexit_callback |= ss->fork || ss->exit;
4108 /* At system boot, before all subsystems have been
4109 * registered, no tasks have been forked, so we don't
4110 * need to invoke fork callbacks here. */
4111 BUG_ON(!list_empty(&init_task.tasks));
4113 mutex_init(&ss->hierarchy_mutex);
4114 lockdep_set_class(&ss->hierarchy_mutex, &ss->subsys_key);
4117 /* this function shouldn't be used with modular subsystems, since they
4118 * need to register a subsys_id, among other things */
4123 * cgroup_load_subsys: load and register a modular subsystem at runtime
4124 * @ss: the subsystem to load
4126 * This function should be called in a modular subsystem's initcall. If the
4127 * subsystem is built as a module, it will be assigned a new subsys_id and set
4128 * up for use. If the subsystem is built-in anyway, work is delegated to the
4129 * simpler cgroup_init_subsys.
4131 int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
4134 struct cgroup_subsys_state *css;
4136 /* check name and function validity */
4137 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
4138 ss->create == NULL || ss->destroy == NULL)
4142 * we don't support callbacks in modular subsystems. this check is
4143 * before the ss->module check for consistency; a subsystem that could
4144 * be a module should still have no callbacks even if the user isn't
4145 * compiling it as one.
4147 if (ss->fork || ss->exit)
4151 * an optionally modular subsystem is built-in: we want to do nothing,
4152 * since cgroup_init_subsys will have already taken care of it.
4154 if (ss->module == NULL) {
4155 /* a few sanity checks */
4156 BUG_ON(ss->subsys_id >= CGROUP_BUILTIN_SUBSYS_COUNT);
4157 BUG_ON(subsys[ss->subsys_id] != ss);
4162 * need to register a subsys id before anything else - for example,
4163 * init_cgroup_css needs it.
4165 mutex_lock(&cgroup_mutex);
4166 /* find the first empty slot in the array */
4167 for (i = CGROUP_BUILTIN_SUBSYS_COUNT; i < CGROUP_SUBSYS_COUNT; i++) {
4168 if (subsys[i] == NULL)
4171 if (i == CGROUP_SUBSYS_COUNT) {
4172 /* maximum number of subsystems already registered! */
4173 mutex_unlock(&cgroup_mutex);
4176 /* assign ourselves the subsys_id */
4181 * no ss->create seems to need anything important in the ss struct, so
4182 * this can happen first (i.e. before the rootnode attachment).
4184 css = ss->create(ss, dummytop);
4186 /* failure case - need to deassign the subsys[] slot. */
4188 mutex_unlock(&cgroup_mutex);
4189 return PTR_ERR(css);
4192 list_add(&ss->sibling, &rootnode.subsys_list);
4193 ss->root = &rootnode;
4195 /* our new subsystem will be attached to the dummy hierarchy. */
4196 init_cgroup_css(css, ss, dummytop);
4197 /* init_idr must be after init_cgroup_css because it sets css->id. */
4199 int ret = cgroup_init_idr(ss, css);
4201 dummytop->subsys[ss->subsys_id] = NULL;
4202 ss->destroy(ss, dummytop);
4204 mutex_unlock(&cgroup_mutex);
4210 * Now we need to entangle the css into the existing css_sets. unlike
4211 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4212 * will need a new pointer to it; done by iterating the css_set_table.
4213 * furthermore, modifying the existing css_sets will corrupt the hash
4214 * table state, so each changed css_set will need its hash recomputed.
4215 * this is all done under the css_set_lock.
4217 write_lock(&css_set_lock);
4218 for (i = 0; i < CSS_SET_TABLE_SIZE; i++) {
4220 struct hlist_node *node, *tmp;
4221 struct hlist_head *bucket = &css_set_table[i], *new_bucket;
4223 hlist_for_each_entry_safe(cg, node, tmp, bucket, hlist) {
4224 /* skip entries that we already rehashed */
4225 if (cg->subsys[ss->subsys_id])
4227 /* remove existing entry */
4228 hlist_del(&cg->hlist);
4230 cg->subsys[ss->subsys_id] = css;
4231 /* recompute hash and restore entry */
4232 new_bucket = css_set_hash(cg->subsys);
4233 hlist_add_head(&cg->hlist, new_bucket);
4236 write_unlock(&css_set_lock);
4238 mutex_init(&ss->hierarchy_mutex);
4239 lockdep_set_class(&ss->hierarchy_mutex, &ss->subsys_key);
4243 mutex_unlock(&cgroup_mutex);
4246 EXPORT_SYMBOL_GPL(cgroup_load_subsys);
4249 * cgroup_unload_subsys: unload a modular subsystem
4250 * @ss: the subsystem to unload
4252 * This function should be called in a modular subsystem's exitcall. When this
4253 * function is invoked, the refcount on the subsystem's module will be 0, so
4254 * the subsystem will not be attached to any hierarchy.
4256 void cgroup_unload_subsys(struct cgroup_subsys *ss)
4258 struct cg_cgroup_link *link;
4259 struct hlist_head *hhead;
4261 BUG_ON(ss->module == NULL);
4264 * we shouldn't be called if the subsystem is in use, and the use of
4265 * try_module_get in parse_cgroupfs_options should ensure that it
4266 * doesn't start being used while we're killing it off.
4268 BUG_ON(ss->root != &rootnode);
4270 mutex_lock(&cgroup_mutex);
4271 /* deassign the subsys_id */
4272 BUG_ON(ss->subsys_id < CGROUP_BUILTIN_SUBSYS_COUNT);
4273 subsys[ss->subsys_id] = NULL;
4275 /* remove subsystem from rootnode's list of subsystems */
4276 list_del_init(&ss->sibling);
4279 * disentangle the css from all css_sets attached to the dummytop. as
4280 * in loading, we need to pay our respects to the hashtable gods.
4282 write_lock(&css_set_lock);
4283 list_for_each_entry(link, &dummytop->css_sets, cgrp_link_list) {
4284 struct css_set *cg = link->cg;
4286 hlist_del(&cg->hlist);
4287 BUG_ON(!cg->subsys[ss->subsys_id]);
4288 cg->subsys[ss->subsys_id] = NULL;
4289 hhead = css_set_hash(cg->subsys);
4290 hlist_add_head(&cg->hlist, hhead);
4292 write_unlock(&css_set_lock);
4295 * remove subsystem's css from the dummytop and free it - need to free
4296 * before marking as null because ss->destroy needs the cgrp->subsys
4297 * pointer to find their state. note that this also takes care of
4298 * freeing the css_id.
4300 ss->destroy(ss, dummytop);
4301 dummytop->subsys[ss->subsys_id] = NULL;
4303 mutex_unlock(&cgroup_mutex);
4305 EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
4308 * cgroup_init_early - cgroup initialization at system boot
4310 * Initialize cgroups at system boot, and initialize any
4311 * subsystems that request early init.
4313 int __init cgroup_init_early(void)
4316 atomic_set(&init_css_set.refcount, 1);
4317 INIT_LIST_HEAD(&init_css_set.cg_links);
4318 INIT_LIST_HEAD(&init_css_set.tasks);
4319 INIT_HLIST_NODE(&init_css_set.hlist);
4321 init_cgroup_root(&rootnode);
4323 init_task.cgroups = &init_css_set;
4325 init_css_set_link.cg = &init_css_set;
4326 init_css_set_link.cgrp = dummytop;
4327 list_add(&init_css_set_link.cgrp_link_list,
4328 &rootnode.top_cgroup.css_sets);
4329 list_add(&init_css_set_link.cg_link_list,
4330 &init_css_set.cg_links);
4332 for (i = 0; i < CSS_SET_TABLE_SIZE; i++)
4333 INIT_HLIST_HEAD(&css_set_table[i]);
4335 /* at bootup time, we don't worry about modular subsystems */
4336 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
4337 struct cgroup_subsys *ss = subsys[i];
4340 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
4341 BUG_ON(!ss->create);
4342 BUG_ON(!ss->destroy);
4343 if (ss->subsys_id != i) {
4344 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
4345 ss->name, ss->subsys_id);
4350 cgroup_init_subsys(ss);
4356 * cgroup_init - cgroup initialization
4358 * Register cgroup filesystem and /proc file, and initialize
4359 * any subsystems that didn't request early init.
4361 int __init cgroup_init(void)
4365 struct hlist_head *hhead;
4367 err = bdi_init(&cgroup_backing_dev_info);
4371 /* at bootup time, we don't worry about modular subsystems */
4372 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
4373 struct cgroup_subsys *ss = subsys[i];
4374 if (!ss->early_init)
4375 cgroup_init_subsys(ss);
4377 cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]);
4380 /* Add init_css_set to the hash table */
4381 hhead = css_set_hash(init_css_set.subsys);
4382 hlist_add_head(&init_css_set.hlist, hhead);
4383 BUG_ON(!init_root_id(&rootnode));
4385 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
4391 err = register_filesystem(&cgroup_fs_type);
4393 kobject_put(cgroup_kobj);
4397 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
4401 bdi_destroy(&cgroup_backing_dev_info);
4407 * proc_cgroup_show()
4408 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4409 * - Used for /proc/<pid>/cgroup.
4410 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4411 * doesn't really matter if tsk->cgroup changes after we read it,
4412 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
4413 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4414 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4415 * cgroup to top_cgroup.
4418 /* TODO: Use a proper seq_file iterator */
4419 static int proc_cgroup_show(struct seq_file *m, void *v)
4422 struct task_struct *tsk;
4425 struct cgroupfs_root *root;
4428 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4434 tsk = get_pid_task(pid, PIDTYPE_PID);
4440 mutex_lock(&cgroup_mutex);
4442 for_each_active_root(root) {
4443 struct cgroup_subsys *ss;
4444 struct cgroup *cgrp;
4447 seq_printf(m, "%d:", root->hierarchy_id);
4448 for_each_subsys(root, ss)
4449 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
4450 if (strlen(root->name))
4451 seq_printf(m, "%sname=%s", count ? "," : "",
4454 cgrp = task_cgroup_from_root(tsk, root);
4455 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
4463 mutex_unlock(&cgroup_mutex);
4464 put_task_struct(tsk);
4471 static int cgroup_open(struct inode *inode, struct file *file)
4473 struct pid *pid = PROC_I(inode)->pid;
4474 return single_open(file, proc_cgroup_show, pid);
4477 const struct file_operations proc_cgroup_operations = {
4478 .open = cgroup_open,
4480 .llseek = seq_lseek,
4481 .release = single_release,
4484 /* Display information about each subsystem and each hierarchy */
4485 static int proc_cgroupstats_show(struct seq_file *m, void *v)
4489 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
4491 * ideally we don't want subsystems moving around while we do this.
4492 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4493 * subsys/hierarchy state.
4495 mutex_lock(&cgroup_mutex);
4496 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
4497 struct cgroup_subsys *ss = subsys[i];
4500 seq_printf(m, "%s\t%d\t%d\t%d\n",
4501 ss->name, ss->root->hierarchy_id,
4502 ss->root->number_of_cgroups, !ss->disabled);
4504 mutex_unlock(&cgroup_mutex);
4508 static int cgroupstats_open(struct inode *inode, struct file *file)
4510 return single_open(file, proc_cgroupstats_show, NULL);
4513 static const struct file_operations proc_cgroupstats_operations = {
4514 .open = cgroupstats_open,
4516 .llseek = seq_lseek,
4517 .release = single_release,
4521 * cgroup_fork - attach newly forked task to its parents cgroup.
4522 * @child: pointer to task_struct of forking parent process.
4524 * Description: A task inherits its parent's cgroup at fork().
4526 * A pointer to the shared css_set was automatically copied in
4527 * fork.c by dup_task_struct(). However, we ignore that copy, since
4528 * it was not made under the protection of RCU or cgroup_mutex, so
4529 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
4530 * have already changed current->cgroups, allowing the previously
4531 * referenced cgroup group to be removed and freed.
4533 * At the point that cgroup_fork() is called, 'current' is the parent
4534 * task, and the passed argument 'child' points to the child task.
4536 void cgroup_fork(struct task_struct *child)
4539 child->cgroups = current->cgroups;
4540 get_css_set(child->cgroups);
4541 task_unlock(current);
4542 INIT_LIST_HEAD(&child->cg_list);
4546 * cgroup_fork_callbacks - run fork callbacks
4547 * @child: the new task
4549 * Called on a new task very soon before adding it to the
4550 * tasklist. No need to take any locks since no-one can
4551 * be operating on this task.
4553 void cgroup_fork_callbacks(struct task_struct *child)
4555 if (need_forkexit_callback) {
4558 * forkexit callbacks are only supported for builtin
4559 * subsystems, and the builtin section of the subsys array is
4560 * immutable, so we don't need to lock the subsys array here.
4562 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
4563 struct cgroup_subsys *ss = subsys[i];
4565 ss->fork(ss, child);
4571 * cgroup_post_fork - called on a new task after adding it to the task list
4572 * @child: the task in question
4574 * Adds the task to the list running through its css_set if necessary.
4575 * Has to be after the task is visible on the task list in case we race
4576 * with the first call to cgroup_iter_start() - to guarantee that the
4577 * new task ends up on its list.
4579 void cgroup_post_fork(struct task_struct *child)
4581 if (use_task_css_set_links) {
4582 write_lock(&css_set_lock);
4584 if (list_empty(&child->cg_list))
4585 list_add(&child->cg_list, &child->cgroups->tasks);
4587 write_unlock(&css_set_lock);
4591 * cgroup_exit - detach cgroup from exiting task
4592 * @tsk: pointer to task_struct of exiting process
4593 * @run_callback: run exit callbacks?
4595 * Description: Detach cgroup from @tsk and release it.
4597 * Note that cgroups marked notify_on_release force every task in
4598 * them to take the global cgroup_mutex mutex when exiting.
4599 * This could impact scaling on very large systems. Be reluctant to
4600 * use notify_on_release cgroups where very high task exit scaling
4601 * is required on large systems.
4603 * the_top_cgroup_hack:
4605 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
4607 * We call cgroup_exit() while the task is still competent to
4608 * handle notify_on_release(), then leave the task attached to the
4609 * root cgroup in each hierarchy for the remainder of its exit.
4611 * To do this properly, we would increment the reference count on
4612 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
4613 * code we would add a second cgroup function call, to drop that
4614 * reference. This would just create an unnecessary hot spot on
4615 * the top_cgroup reference count, to no avail.
4617 * Normally, holding a reference to a cgroup without bumping its
4618 * count is unsafe. The cgroup could go away, or someone could
4619 * attach us to a different cgroup, decrementing the count on
4620 * the first cgroup that we never incremented. But in this case,
4621 * top_cgroup isn't going away, and either task has PF_EXITING set,
4622 * which wards off any cgroup_attach_task() attempts, or task is a failed
4623 * fork, never visible to cgroup_attach_task.
4625 void cgroup_exit(struct task_struct *tsk, int run_callbacks)
4631 * Unlink from the css_set task list if necessary.
4632 * Optimistically check cg_list before taking
4635 if (!list_empty(&tsk->cg_list)) {
4636 write_lock(&css_set_lock);
4637 if (!list_empty(&tsk->cg_list))
4638 list_del_init(&tsk->cg_list);
4639 write_unlock(&css_set_lock);
4642 /* Reassign the task to the init_css_set. */
4645 tsk->cgroups = &init_css_set;
4647 if (run_callbacks && need_forkexit_callback) {
4649 * modular subsystems can't use callbacks, so no need to lock
4652 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
4653 struct cgroup_subsys *ss = subsys[i];
4655 struct cgroup *old_cgrp =
4656 rcu_dereference_raw(cg->subsys[i])->cgroup;
4657 struct cgroup *cgrp = task_cgroup(tsk, i);
4658 ss->exit(ss, cgrp, old_cgrp, tsk);
4669 * cgroup_is_descendant - see if @cgrp is a descendant of @task's cgrp
4670 * @cgrp: the cgroup in question
4671 * @task: the task in question
4673 * See if @cgrp is a descendant of @task's cgroup in the appropriate
4676 * If we are sending in dummytop, then presumably we are creating
4677 * the top cgroup in the subsystem.
4679 * Called only by the ns (nsproxy) cgroup.
4681 int cgroup_is_descendant(const struct cgroup *cgrp, struct task_struct *task)
4684 struct cgroup *target;
4686 if (cgrp == dummytop)
4689 target = task_cgroup_from_root(task, cgrp->root);
4690 while (cgrp != target && cgrp!= cgrp->top_cgroup)
4691 cgrp = cgrp->parent;
4692 ret = (cgrp == target);
4696 static void check_for_release(struct cgroup *cgrp)
4698 /* All of these checks rely on RCU to keep the cgroup
4699 * structure alive */
4700 if (cgroup_is_releasable(cgrp) && !atomic_read(&cgrp->count)
4701 && list_empty(&cgrp->children) && !cgroup_has_css_refs(cgrp)) {
4702 /* Control Group is currently removeable. If it's not
4703 * already queued for a userspace notification, queue
4705 int need_schedule_work = 0;
4706 spin_lock(&release_list_lock);
4707 if (!cgroup_is_removed(cgrp) &&
4708 list_empty(&cgrp->release_list)) {
4709 list_add(&cgrp->release_list, &release_list);
4710 need_schedule_work = 1;
4712 spin_unlock(&release_list_lock);
4713 if (need_schedule_work)
4714 schedule_work(&release_agent_work);
4718 /* Caller must verify that the css is not for root cgroup */
4719 void __css_get(struct cgroup_subsys_state *css, int count)
4721 atomic_add(count, &css->refcnt);
4722 set_bit(CGRP_RELEASABLE, &css->cgroup->flags);
4724 EXPORT_SYMBOL_GPL(__css_get);
4726 /* Caller must verify that the css is not for root cgroup */
4727 void __css_put(struct cgroup_subsys_state *css, int count)
4729 struct cgroup *cgrp = css->cgroup;
4732 val = atomic_sub_return(count, &css->refcnt);
4734 check_for_release(cgrp);
4735 cgroup_wakeup_rmdir_waiter(cgrp);
4738 WARN_ON_ONCE(val < 1);
4740 EXPORT_SYMBOL_GPL(__css_put);
4743 * Notify userspace when a cgroup is released, by running the
4744 * configured release agent with the name of the cgroup (path
4745 * relative to the root of cgroup file system) as the argument.
4747 * Most likely, this user command will try to rmdir this cgroup.
4749 * This races with the possibility that some other task will be
4750 * attached to this cgroup before it is removed, or that some other
4751 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
4752 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
4753 * unused, and this cgroup will be reprieved from its death sentence,
4754 * to continue to serve a useful existence. Next time it's released,
4755 * we will get notified again, if it still has 'notify_on_release' set.
4757 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
4758 * means only wait until the task is successfully execve()'d. The
4759 * separate release agent task is forked by call_usermodehelper(),
4760 * then control in this thread returns here, without waiting for the
4761 * release agent task. We don't bother to wait because the caller of
4762 * this routine has no use for the exit status of the release agent
4763 * task, so no sense holding our caller up for that.
4765 static void cgroup_release_agent(struct work_struct *work)
4767 BUG_ON(work != &release_agent_work);
4768 mutex_lock(&cgroup_mutex);
4769 spin_lock(&release_list_lock);
4770 while (!list_empty(&release_list)) {
4771 char *argv[3], *envp[3];
4773 char *pathbuf = NULL, *agentbuf = NULL;
4774 struct cgroup *cgrp = list_entry(release_list.next,
4777 list_del_init(&cgrp->release_list);
4778 spin_unlock(&release_list_lock);
4779 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4782 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
4784 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
4789 argv[i++] = agentbuf;
4790 argv[i++] = pathbuf;
4794 /* minimal command environment */
4795 envp[i++] = "HOME=/";
4796 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
4799 /* Drop the lock while we invoke the usermode helper,
4800 * since the exec could involve hitting disk and hence
4801 * be a slow process */
4802 mutex_unlock(&cgroup_mutex);
4803 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
4804 mutex_lock(&cgroup_mutex);
4808 spin_lock(&release_list_lock);
4810 spin_unlock(&release_list_lock);
4811 mutex_unlock(&cgroup_mutex);
4814 static int __init cgroup_disable(char *str)
4819 while ((token = strsep(&str, ",")) != NULL) {
4823 * cgroup_disable, being at boot time, can't know about module
4824 * subsystems, so we don't worry about them.
4826 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
4827 struct cgroup_subsys *ss = subsys[i];
4829 if (!strcmp(token, ss->name)) {
4831 printk(KERN_INFO "Disabling %s control group"
4832 " subsystem\n", ss->name);
4839 __setup("cgroup_disable=", cgroup_disable);
4842 * Functons for CSS ID.
4846 *To get ID other than 0, this should be called when !cgroup_is_removed().
4848 unsigned short css_id(struct cgroup_subsys_state *css)
4850 struct css_id *cssid;
4853 * This css_id() can return correct value when somone has refcnt
4854 * on this or this is under rcu_read_lock(). Once css->id is allocated,
4855 * it's unchanged until freed.
4857 cssid = rcu_dereference_check(css->id,
4858 rcu_read_lock_held() || atomic_read(&css->refcnt));
4864 EXPORT_SYMBOL_GPL(css_id);
4866 unsigned short css_depth(struct cgroup_subsys_state *css)
4868 struct css_id *cssid;
4870 cssid = rcu_dereference_check(css->id,
4871 rcu_read_lock_held() || atomic_read(&css->refcnt));
4874 return cssid->depth;
4877 EXPORT_SYMBOL_GPL(css_depth);
4880 * css_is_ancestor - test "root" css is an ancestor of "child"
4881 * @child: the css to be tested.
4882 * @root: the css supporsed to be an ancestor of the child.
4884 * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
4885 * this function reads css->id, this use rcu_dereference() and rcu_read_lock().
4886 * But, considering usual usage, the csses should be valid objects after test.
4887 * Assuming that the caller will do some action to the child if this returns
4888 * returns true, the caller must take "child";s reference count.
4889 * If "child" is valid object and this returns true, "root" is valid, too.
4892 bool css_is_ancestor(struct cgroup_subsys_state *child,
4893 const struct cgroup_subsys_state *root)
4895 struct css_id *child_id;
4896 struct css_id *root_id;
4900 child_id = rcu_dereference(child->id);
4901 root_id = rcu_dereference(root->id);
4904 || (child_id->depth < root_id->depth)
4905 || (child_id->stack[root_id->depth] != root_id->id))
4911 void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
4913 struct css_id *id = css->id;
4914 /* When this is called before css_id initialization, id can be NULL */
4918 BUG_ON(!ss->use_id);
4920 rcu_assign_pointer(id->css, NULL);
4921 rcu_assign_pointer(css->id, NULL);
4922 spin_lock(&ss->id_lock);
4923 idr_remove(&ss->idr, id->id);
4924 spin_unlock(&ss->id_lock);
4925 kfree_rcu(id, rcu_head);
4927 EXPORT_SYMBOL_GPL(free_css_id);
4930 * This is called by init or create(). Then, calls to this function are
4931 * always serialized (By cgroup_mutex() at create()).
4934 static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
4936 struct css_id *newid;
4937 int myid, error, size;
4939 BUG_ON(!ss->use_id);
4941 size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
4942 newid = kzalloc(size, GFP_KERNEL);
4944 return ERR_PTR(-ENOMEM);
4946 if (unlikely(!idr_pre_get(&ss->idr, GFP_KERNEL))) {
4950 spin_lock(&ss->id_lock);
4951 /* Don't use 0. allocates an ID of 1-65535 */
4952 error = idr_get_new_above(&ss->idr, newid, 1, &myid);
4953 spin_unlock(&ss->id_lock);
4955 /* Returns error when there are no free spaces for new ID.*/
4960 if (myid > CSS_ID_MAX)
4964 newid->depth = depth;
4968 spin_lock(&ss->id_lock);
4969 idr_remove(&ss->idr, myid);
4970 spin_unlock(&ss->id_lock);
4973 return ERR_PTR(error);
4977 static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss,
4978 struct cgroup_subsys_state *rootcss)
4980 struct css_id *newid;
4982 spin_lock_init(&ss->id_lock);
4985 newid = get_new_cssid(ss, 0);
4987 return PTR_ERR(newid);
4989 newid->stack[0] = newid->id;
4990 newid->css = rootcss;
4991 rootcss->id = newid;
4995 static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent,
4996 struct cgroup *child)
4998 int subsys_id, i, depth = 0;
4999 struct cgroup_subsys_state *parent_css, *child_css;
5000 struct css_id *child_id, *parent_id;
5002 subsys_id = ss->subsys_id;
5003 parent_css = parent->subsys[subsys_id];
5004 child_css = child->subsys[subsys_id];
5005 parent_id = parent_css->id;
5006 depth = parent_id->depth + 1;
5008 child_id = get_new_cssid(ss, depth);
5009 if (IS_ERR(child_id))
5010 return PTR_ERR(child_id);
5012 for (i = 0; i < depth; i++)
5013 child_id->stack[i] = parent_id->stack[i];
5014 child_id->stack[depth] = child_id->id;
5016 * child_id->css pointer will be set after this cgroup is available
5017 * see cgroup_populate_dir()
5019 rcu_assign_pointer(child_css->id, child_id);
5025 * css_lookup - lookup css by id
5026 * @ss: cgroup subsys to be looked into.
5029 * Returns pointer to cgroup_subsys_state if there is valid one with id.
5030 * NULL if not. Should be called under rcu_read_lock()
5032 struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
5034 struct css_id *cssid = NULL;
5036 BUG_ON(!ss->use_id);
5037 cssid = idr_find(&ss->idr, id);
5039 if (unlikely(!cssid))
5042 return rcu_dereference(cssid->css);
5044 EXPORT_SYMBOL_GPL(css_lookup);
5047 * css_get_next - lookup next cgroup under specified hierarchy.
5048 * @ss: pointer to subsystem
5049 * @id: current position of iteration.
5050 * @root: pointer to css. search tree under this.
5051 * @foundid: position of found object.
5053 * Search next css under the specified hierarchy of rootid. Calling under
5054 * rcu_read_lock() is necessary. Returns NULL if it reaches the end.
5056 struct cgroup_subsys_state *
5057 css_get_next(struct cgroup_subsys *ss, int id,
5058 struct cgroup_subsys_state *root, int *foundid)
5060 struct cgroup_subsys_state *ret = NULL;
5063 int rootid = css_id(root);
5064 int depth = css_depth(root);
5069 BUG_ON(!ss->use_id);
5070 /* fill start point for scan */
5074 * scan next entry from bitmap(tree), tmpid is updated after
5077 spin_lock(&ss->id_lock);
5078 tmp = idr_get_next(&ss->idr, &tmpid);
5079 spin_unlock(&ss->id_lock);
5083 if (tmp->depth >= depth && tmp->stack[depth] == rootid) {
5084 ret = rcu_dereference(tmp->css);
5090 /* continue to scan from next id */
5097 * get corresponding css from file open on cgroupfs directory
5099 struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id)
5101 struct cgroup *cgrp;
5102 struct inode *inode;
5103 struct cgroup_subsys_state *css;
5105 inode = f->f_dentry->d_inode;
5106 /* check in cgroup filesystem dir */
5107 if (inode->i_op != &cgroup_dir_inode_operations)
5108 return ERR_PTR(-EBADF);
5110 if (id < 0 || id >= CGROUP_SUBSYS_COUNT)
5111 return ERR_PTR(-EINVAL);
5114 cgrp = __d_cgrp(f->f_dentry);
5115 css = cgrp->subsys[id];
5116 return css ? css : ERR_PTR(-ENOENT);
5119 #ifdef CONFIG_CGROUP_DEBUG
5120 static struct cgroup_subsys_state *debug_create(struct cgroup_subsys *ss,
5121 struct cgroup *cont)
5123 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5126 return ERR_PTR(-ENOMEM);
5131 static void debug_destroy(struct cgroup_subsys *ss, struct cgroup *cont)
5133 kfree(cont->subsys[debug_subsys_id]);
5136 static u64 cgroup_refcount_read(struct cgroup *cont, struct cftype *cft)
5138 return atomic_read(&cont->count);
5141 static u64 debug_taskcount_read(struct cgroup *cont, struct cftype *cft)
5143 return cgroup_task_count(cont);
5146 static u64 current_css_set_read(struct cgroup *cont, struct cftype *cft)
5148 return (u64)(unsigned long)current->cgroups;
5151 static u64 current_css_set_refcount_read(struct cgroup *cont,
5157 count = atomic_read(¤t->cgroups->refcount);
5162 static int current_css_set_cg_links_read(struct cgroup *cont,
5164 struct seq_file *seq)
5166 struct cg_cgroup_link *link;
5169 read_lock(&css_set_lock);
5171 cg = rcu_dereference(current->cgroups);
5172 list_for_each_entry(link, &cg->cg_links, cg_link_list) {
5173 struct cgroup *c = link->cgrp;
5177 name = c->dentry->d_name.name;
5180 seq_printf(seq, "Root %d group %s\n",
5181 c->root->hierarchy_id, name);
5184 read_unlock(&css_set_lock);
5188 #define MAX_TASKS_SHOWN_PER_CSS 25
5189 static int cgroup_css_links_read(struct cgroup *cont,
5191 struct seq_file *seq)
5193 struct cg_cgroup_link *link;
5195 read_lock(&css_set_lock);
5196 list_for_each_entry(link, &cont->css_sets, cgrp_link_list) {
5197 struct css_set *cg = link->cg;
5198 struct task_struct *task;
5200 seq_printf(seq, "css_set %p\n", cg);
5201 list_for_each_entry(task, &cg->tasks, cg_list) {
5202 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5203 seq_puts(seq, " ...\n");
5206 seq_printf(seq, " task %d\n",
5207 task_pid_vnr(task));
5211 read_unlock(&css_set_lock);
5215 static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft)
5217 return test_bit(CGRP_RELEASABLE, &cgrp->flags);
5220 static struct cftype debug_files[] = {
5222 .name = "cgroup_refcount",
5223 .read_u64 = cgroup_refcount_read,
5226 .name = "taskcount",
5227 .read_u64 = debug_taskcount_read,
5231 .name = "current_css_set",
5232 .read_u64 = current_css_set_read,
5236 .name = "current_css_set_refcount",
5237 .read_u64 = current_css_set_refcount_read,
5241 .name = "current_css_set_cg_links",
5242 .read_seq_string = current_css_set_cg_links_read,
5246 .name = "cgroup_css_links",
5247 .read_seq_string = cgroup_css_links_read,
5251 .name = "releasable",
5252 .read_u64 = releasable_read,
5256 static int debug_populate(struct cgroup_subsys *ss, struct cgroup *cont)
5258 return cgroup_add_files(cont, ss, debug_files,
5259 ARRAY_SIZE(debug_files));
5262 struct cgroup_subsys debug_subsys = {
5264 .create = debug_create,
5265 .destroy = debug_destroy,
5266 .populate = debug_populate,
5267 .subsys_id = debug_subsys_id,
5269 #endif /* CONFIG_CGROUP_DEBUG */