2 * Generic process-grouping system.
4 * Based originally on the cpuset system, extracted by Paul Menage
5 * Copyright (C) 2006 Google, Inc
7 * Copyright notices from the original cpuset code:
8 * --------------------------------------------------
9 * Copyright (C) 2003 BULL SA.
10 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
12 * Portions derived from Patrick Mochel's sysfs code.
13 * sysfs is Copyright (c) 2001-3 Patrick Mochel
15 * 2003-10-10 Written by Simon Derr.
16 * 2003-10-22 Updates by Stephen Hemminger.
17 * 2004 May-July Rework by Paul Jackson.
18 * ---------------------------------------------------
20 * This file is subject to the terms and conditions of the GNU General Public
21 * License. See the file COPYING in the main directory of the Linux
22 * distribution for more details.
25 #include <linux/cgroup.h>
26 #include <linux/errno.h>
28 #include <linux/kernel.h>
29 #include <linux/list.h>
31 #include <linux/mutex.h>
32 #include <linux/mount.h>
33 #include <linux/pagemap.h>
34 #include <linux/proc_fs.h>
35 #include <linux/rcupdate.h>
36 #include <linux/sched.h>
37 #include <linux/backing-dev.h>
38 #include <linux/seq_file.h>
39 #include <linux/slab.h>
40 #include <linux/magic.h>
41 #include <linux/spinlock.h>
42 #include <linux/string.h>
43 #include <linux/sort.h>
44 #include <linux/kmod.h>
45 #include <linux/delayacct.h>
46 #include <linux/cgroupstats.h>
47 #include <linux/hash.h>
48 #include <linux/namei.h>
50 #include <asm/atomic.h>
52 static DEFINE_MUTEX(cgroup_mutex);
54 /* Generate an array of cgroup subsystem pointers */
55 #define SUBSYS(_x) &_x ## _subsys,
57 static struct cgroup_subsys *subsys[] = {
58 #include <linux/cgroup_subsys.h>
62 * A cgroupfs_root represents the root of a cgroup hierarchy,
63 * and may be associated with a superblock to form an active
66 struct cgroupfs_root {
67 struct super_block *sb;
70 * The bitmask of subsystems intended to be attached to this
73 unsigned long subsys_bits;
75 /* The bitmask of subsystems currently attached to this hierarchy */
76 unsigned long actual_subsys_bits;
78 /* A list running through the attached subsystems */
79 struct list_head subsys_list;
81 /* The root cgroup for this hierarchy */
82 struct cgroup top_cgroup;
84 /* Tracks how many cgroups are currently defined in hierarchy.*/
85 int number_of_cgroups;
87 /* A list running through the active hierarchies */
88 struct list_head root_list;
90 /* Hierarchy-specific flags */
93 /* The path to use for release notifications. */
94 char release_agent_path[PATH_MAX];
98 * The "rootnode" hierarchy is the "dummy hierarchy", reserved for the
99 * subsystems that are otherwise unattached - it never has more than a
100 * single cgroup, and all tasks are part of that cgroup.
102 static struct cgroupfs_root rootnode;
105 * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when
106 * cgroup_subsys->use_id != 0.
108 #define CSS_ID_MAX (65535)
111 * The css to which this ID points. This pointer is set to valid value
112 * after cgroup is populated. If cgroup is removed, this will be NULL.
113 * This pointer is expected to be RCU-safe because destroy()
114 * is called after synchronize_rcu(). But for safe use, css_is_removed()
115 * css_tryget() should be used for avoiding race.
117 struct cgroup_subsys_state *css;
123 * Depth in hierarchy which this ID belongs to.
125 unsigned short depth;
127 * ID is freed by RCU. (and lookup routine is RCU safe.)
129 struct rcu_head rcu_head;
131 * Hierarchy of CSS ID belongs to.
133 unsigned short stack[0]; /* Array of Length (depth+1) */
137 /* The list of hierarchy roots */
139 static LIST_HEAD(roots);
140 static int root_count;
142 /* dummytop is a shorthand for the dummy hierarchy's top cgroup */
143 #define dummytop (&rootnode.top_cgroup)
145 /* This flag indicates whether tasks in the fork and exit paths should
146 * check for fork/exit handlers to call. This avoids us having to do
147 * extra work in the fork/exit path if none of the subsystems need to
150 static int need_forkexit_callback __read_mostly;
152 /* convenient tests for these bits */
153 inline int cgroup_is_removed(const struct cgroup *cgrp)
155 return test_bit(CGRP_REMOVED, &cgrp->flags);
158 /* bits in struct cgroupfs_root flags field */
160 ROOT_NOPREFIX, /* mounted subsystems have no named prefix */
163 static int cgroup_is_releasable(const struct cgroup *cgrp)
166 (1 << CGRP_RELEASABLE) |
167 (1 << CGRP_NOTIFY_ON_RELEASE);
168 return (cgrp->flags & bits) == bits;
171 static int notify_on_release(const struct cgroup *cgrp)
173 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
177 * for_each_subsys() allows you to iterate on each subsystem attached to
178 * an active hierarchy
180 #define for_each_subsys(_root, _ss) \
181 list_for_each_entry(_ss, &_root->subsys_list, sibling)
183 /* for_each_active_root() allows you to iterate across the active hierarchies */
184 #define for_each_active_root(_root) \
185 list_for_each_entry(_root, &roots, root_list)
187 /* the list of cgroups eligible for automatic release. Protected by
188 * release_list_lock */
189 static LIST_HEAD(release_list);
190 static DEFINE_SPINLOCK(release_list_lock);
191 static void cgroup_release_agent(struct work_struct *work);
192 static DECLARE_WORK(release_agent_work, cgroup_release_agent);
193 static void check_for_release(struct cgroup *cgrp);
195 /* Link structure for associating css_set objects with cgroups */
196 struct cg_cgroup_link {
198 * List running through cg_cgroup_links associated with a
199 * cgroup, anchored on cgroup->css_sets
201 struct list_head cgrp_link_list;
203 * List running through cg_cgroup_links pointing at a
204 * single css_set object, anchored on css_set->cg_links
206 struct list_head cg_link_list;
210 /* The default css_set - used by init and its children prior to any
211 * hierarchies being mounted. It contains a pointer to the root state
212 * for each subsystem. Also used to anchor the list of css_sets. Not
213 * reference-counted, to improve performance when child cgroups
214 * haven't been created.
217 static struct css_set init_css_set;
218 static struct cg_cgroup_link init_css_set_link;
220 static int cgroup_subsys_init_idr(struct cgroup_subsys *ss);
222 /* css_set_lock protects the list of css_set objects, and the
223 * chain of tasks off each css_set. Nests outside task->alloc_lock
224 * due to cgroup_iter_start() */
225 static DEFINE_RWLOCK(css_set_lock);
226 static int css_set_count;
228 /* hash table for cgroup groups. This improves the performance to
229 * find an existing css_set */
230 #define CSS_SET_HASH_BITS 7
231 #define CSS_SET_TABLE_SIZE (1 << CSS_SET_HASH_BITS)
232 static struct hlist_head css_set_table[CSS_SET_TABLE_SIZE];
234 static struct hlist_head *css_set_hash(struct cgroup_subsys_state *css[])
238 unsigned long tmp = 0UL;
240 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++)
241 tmp += (unsigned long)css[i];
242 tmp = (tmp >> 16) ^ tmp;
244 index = hash_long(tmp, CSS_SET_HASH_BITS);
246 return &css_set_table[index];
249 /* We don't maintain the lists running through each css_set to its
250 * task until after the first call to cgroup_iter_start(). This
251 * reduces the fork()/exit() overhead for people who have cgroups
252 * compiled into their kernel but not actually in use */
253 static int use_task_css_set_links __read_mostly;
255 /* When we create or destroy a css_set, the operation simply
256 * takes/releases a reference count on all the cgroups referenced
257 * by subsystems in this css_set. This can end up multiple-counting
258 * some cgroups, but that's OK - the ref-count is just a
259 * busy/not-busy indicator; ensuring that we only count each cgroup
260 * once would require taking a global lock to ensure that no
261 * subsystems moved between hierarchies while we were doing so.
263 * Possible TODO: decide at boot time based on the number of
264 * registered subsystems and the number of CPUs or NUMA nodes whether
265 * it's better for performance to ref-count every subsystem, or to
266 * take a global lock and only add one ref count to each hierarchy.
270 * unlink a css_set from the list and free it
272 static void unlink_css_set(struct css_set *cg)
274 struct cg_cgroup_link *link;
275 struct cg_cgroup_link *saved_link;
277 hlist_del(&cg->hlist);
280 list_for_each_entry_safe(link, saved_link, &cg->cg_links,
282 list_del(&link->cg_link_list);
283 list_del(&link->cgrp_link_list);
288 static void __put_css_set(struct css_set *cg, int taskexit)
292 * Ensure that the refcount doesn't hit zero while any readers
293 * can see it. Similar to atomic_dec_and_lock(), but for an
296 if (atomic_add_unless(&cg->refcount, -1, 1))
298 write_lock(&css_set_lock);
299 if (!atomic_dec_and_test(&cg->refcount)) {
300 write_unlock(&css_set_lock);
304 write_unlock(&css_set_lock);
307 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
308 struct cgroup *cgrp = rcu_dereference(cg->subsys[i]->cgroup);
309 if (atomic_dec_and_test(&cgrp->count) &&
310 notify_on_release(cgrp)) {
312 set_bit(CGRP_RELEASABLE, &cgrp->flags);
313 check_for_release(cgrp);
321 * refcounted get/put for css_set objects
323 static inline void get_css_set(struct css_set *cg)
325 atomic_inc(&cg->refcount);
328 static inline void put_css_set(struct css_set *cg)
330 __put_css_set(cg, 0);
333 static inline void put_css_set_taskexit(struct css_set *cg)
335 __put_css_set(cg, 1);
339 * find_existing_css_set() is a helper for
340 * find_css_set(), and checks to see whether an existing
341 * css_set is suitable.
343 * oldcg: the cgroup group that we're using before the cgroup
346 * cgrp: the cgroup that we're moving into
348 * template: location in which to build the desired set of subsystem
349 * state objects for the new cgroup group
351 static struct css_set *find_existing_css_set(
352 struct css_set *oldcg,
354 struct cgroup_subsys_state *template[])
357 struct cgroupfs_root *root = cgrp->root;
358 struct hlist_head *hhead;
359 struct hlist_node *node;
362 /* Built the set of subsystem state objects that we want to
363 * see in the new css_set */
364 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
365 if (root->subsys_bits & (1UL << i)) {
366 /* Subsystem is in this hierarchy. So we want
367 * the subsystem state from the new
369 template[i] = cgrp->subsys[i];
371 /* Subsystem is not in this hierarchy, so we
372 * don't want to change the subsystem state */
373 template[i] = oldcg->subsys[i];
377 hhead = css_set_hash(template);
378 hlist_for_each_entry(cg, node, hhead, hlist) {
379 if (!memcmp(template, cg->subsys, sizeof(cg->subsys))) {
380 /* All subsystems matched */
385 /* No existing cgroup group matched */
389 static void free_cg_links(struct list_head *tmp)
391 struct cg_cgroup_link *link;
392 struct cg_cgroup_link *saved_link;
394 list_for_each_entry_safe(link, saved_link, tmp, cgrp_link_list) {
395 list_del(&link->cgrp_link_list);
401 * allocate_cg_links() allocates "count" cg_cgroup_link structures
402 * and chains them on tmp through their cgrp_link_list fields. Returns 0 on
403 * success or a negative error
405 static int allocate_cg_links(int count, struct list_head *tmp)
407 struct cg_cgroup_link *link;
410 for (i = 0; i < count; i++) {
411 link = kmalloc(sizeof(*link), GFP_KERNEL);
416 list_add(&link->cgrp_link_list, tmp);
422 * link_css_set - a helper function to link a css_set to a cgroup
423 * @tmp_cg_links: cg_cgroup_link objects allocated by allocate_cg_links()
424 * @cg: the css_set to be linked
425 * @cgrp: the destination cgroup
427 static void link_css_set(struct list_head *tmp_cg_links,
428 struct css_set *cg, struct cgroup *cgrp)
430 struct cg_cgroup_link *link;
432 BUG_ON(list_empty(tmp_cg_links));
433 link = list_first_entry(tmp_cg_links, struct cg_cgroup_link,
436 list_move(&link->cgrp_link_list, &cgrp->css_sets);
437 list_add(&link->cg_link_list, &cg->cg_links);
441 * find_css_set() takes an existing cgroup group and a
442 * cgroup object, and returns a css_set object that's
443 * equivalent to the old group, but with the given cgroup
444 * substituted into the appropriate hierarchy. Must be called with
447 static struct css_set *find_css_set(
448 struct css_set *oldcg, struct cgroup *cgrp)
451 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT];
454 struct list_head tmp_cg_links;
456 struct hlist_head *hhead;
458 /* First see if we already have a cgroup group that matches
460 read_lock(&css_set_lock);
461 res = find_existing_css_set(oldcg, cgrp, template);
464 read_unlock(&css_set_lock);
469 res = kmalloc(sizeof(*res), GFP_KERNEL);
473 /* Allocate all the cg_cgroup_link objects that we'll need */
474 if (allocate_cg_links(root_count, &tmp_cg_links) < 0) {
479 atomic_set(&res->refcount, 1);
480 INIT_LIST_HEAD(&res->cg_links);
481 INIT_LIST_HEAD(&res->tasks);
482 INIT_HLIST_NODE(&res->hlist);
484 /* Copy the set of subsystem state objects generated in
485 * find_existing_css_set() */
486 memcpy(res->subsys, template, sizeof(res->subsys));
488 write_lock(&css_set_lock);
489 /* Add reference counts and links from the new css_set. */
490 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
491 struct cgroup *cgrp = res->subsys[i]->cgroup;
492 struct cgroup_subsys *ss = subsys[i];
493 atomic_inc(&cgrp->count);
495 * We want to add a link once per cgroup, so we
496 * only do it for the first subsystem in each
499 if (ss->root->subsys_list.next == &ss->sibling)
500 link_css_set(&tmp_cg_links, res, cgrp);
502 if (list_empty(&rootnode.subsys_list))
503 link_css_set(&tmp_cg_links, res, dummytop);
505 BUG_ON(!list_empty(&tmp_cg_links));
509 /* Add this cgroup group to the hash table */
510 hhead = css_set_hash(res->subsys);
511 hlist_add_head(&res->hlist, hhead);
513 write_unlock(&css_set_lock);
519 * There is one global cgroup mutex. We also require taking
520 * task_lock() when dereferencing a task's cgroup subsys pointers.
521 * See "The task_lock() exception", at the end of this comment.
523 * A task must hold cgroup_mutex to modify cgroups.
525 * Any task can increment and decrement the count field without lock.
526 * So in general, code holding cgroup_mutex can't rely on the count
527 * field not changing. However, if the count goes to zero, then only
528 * cgroup_attach_task() can increment it again. Because a count of zero
529 * means that no tasks are currently attached, therefore there is no
530 * way a task attached to that cgroup can fork (the other way to
531 * increment the count). So code holding cgroup_mutex can safely
532 * assume that if the count is zero, it will stay zero. Similarly, if
533 * a task holds cgroup_mutex on a cgroup with zero count, it
534 * knows that the cgroup won't be removed, as cgroup_rmdir()
537 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
538 * (usually) take cgroup_mutex. These are the two most performance
539 * critical pieces of code here. The exception occurs on cgroup_exit(),
540 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
541 * is taken, and if the cgroup count is zero, a usermode call made
542 * to the release agent with the name of the cgroup (path relative to
543 * the root of cgroup file system) as the argument.
545 * A cgroup can only be deleted if both its 'count' of using tasks
546 * is zero, and its list of 'children' cgroups is empty. Since all
547 * tasks in the system use _some_ cgroup, and since there is always at
548 * least one task in the system (init, pid == 1), therefore, top_cgroup
549 * always has either children cgroups and/or using tasks. So we don't
550 * need a special hack to ensure that top_cgroup cannot be deleted.
552 * The task_lock() exception
554 * The need for this exception arises from the action of
555 * cgroup_attach_task(), which overwrites one tasks cgroup pointer with
556 * another. It does so using cgroup_mutex, however there are
557 * several performance critical places that need to reference
558 * task->cgroup without the expense of grabbing a system global
559 * mutex. Therefore except as noted below, when dereferencing or, as
560 * in cgroup_attach_task(), modifying a task'ss cgroup pointer we use
561 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
562 * the task_struct routinely used for such matters.
564 * P.S. One more locking exception. RCU is used to guard the
565 * update of a tasks cgroup pointer by cgroup_attach_task()
569 * cgroup_lock - lock out any changes to cgroup structures
572 void cgroup_lock(void)
574 mutex_lock(&cgroup_mutex);
578 * cgroup_unlock - release lock on cgroup changes
580 * Undo the lock taken in a previous cgroup_lock() call.
582 void cgroup_unlock(void)
584 mutex_unlock(&cgroup_mutex);
588 * A couple of forward declarations required, due to cyclic reference loop:
589 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
590 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
594 static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode);
595 static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
596 static int cgroup_populate_dir(struct cgroup *cgrp);
597 static struct inode_operations cgroup_dir_inode_operations;
598 static struct file_operations proc_cgroupstats_operations;
600 static struct backing_dev_info cgroup_backing_dev_info = {
601 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
604 static int alloc_css_id(struct cgroup_subsys *ss,
605 struct cgroup *parent, struct cgroup *child);
607 static struct inode *cgroup_new_inode(mode_t mode, struct super_block *sb)
609 struct inode *inode = new_inode(sb);
612 inode->i_mode = mode;
613 inode->i_uid = current_fsuid();
614 inode->i_gid = current_fsgid();
615 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
616 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
622 * Call subsys's pre_destroy handler.
623 * This is called before css refcnt check.
625 static int cgroup_call_pre_destroy(struct cgroup *cgrp)
627 struct cgroup_subsys *ss;
630 for_each_subsys(cgrp->root, ss)
631 if (ss->pre_destroy) {
632 ret = ss->pre_destroy(ss, cgrp);
639 static void free_cgroup_rcu(struct rcu_head *obj)
641 struct cgroup *cgrp = container_of(obj, struct cgroup, rcu_head);
646 static void cgroup_diput(struct dentry *dentry, struct inode *inode)
648 /* is dentry a directory ? if so, kfree() associated cgroup */
649 if (S_ISDIR(inode->i_mode)) {
650 struct cgroup *cgrp = dentry->d_fsdata;
651 struct cgroup_subsys *ss;
652 BUG_ON(!(cgroup_is_removed(cgrp)));
653 /* It's possible for external users to be holding css
654 * reference counts on a cgroup; css_put() needs to
655 * be able to access the cgroup after decrementing
656 * the reference count in order to know if it needs to
657 * queue the cgroup to be handled by the release
661 mutex_lock(&cgroup_mutex);
663 * Release the subsystem state objects.
665 for_each_subsys(cgrp->root, ss)
666 ss->destroy(ss, cgrp);
668 cgrp->root->number_of_cgroups--;
669 mutex_unlock(&cgroup_mutex);
672 * Drop the active superblock reference that we took when we
675 deactivate_super(cgrp->root->sb);
677 call_rcu(&cgrp->rcu_head, free_cgroup_rcu);
682 static void remove_dir(struct dentry *d)
684 struct dentry *parent = dget(d->d_parent);
687 simple_rmdir(parent->d_inode, d);
691 static void cgroup_clear_directory(struct dentry *dentry)
693 struct list_head *node;
695 BUG_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
696 spin_lock(&dcache_lock);
697 node = dentry->d_subdirs.next;
698 while (node != &dentry->d_subdirs) {
699 struct dentry *d = list_entry(node, struct dentry, d_u.d_child);
702 /* This should never be called on a cgroup
703 * directory with child cgroups */
704 BUG_ON(d->d_inode->i_mode & S_IFDIR);
706 spin_unlock(&dcache_lock);
708 simple_unlink(dentry->d_inode, d);
710 spin_lock(&dcache_lock);
712 node = dentry->d_subdirs.next;
714 spin_unlock(&dcache_lock);
718 * NOTE : the dentry must have been dget()'ed
720 static void cgroup_d_remove_dir(struct dentry *dentry)
722 cgroup_clear_directory(dentry);
724 spin_lock(&dcache_lock);
725 list_del_init(&dentry->d_u.d_child);
726 spin_unlock(&dcache_lock);
731 * A queue for waiters to do rmdir() cgroup. A tasks will sleep when
732 * cgroup->count == 0 && list_empty(&cgroup->children) && subsys has some
733 * reference to css->refcnt. In general, this refcnt is expected to goes down
736 * CGRP_WAIT_ON_RMDIR flag is modified under cgroup's inode->i_mutex;
738 DECLARE_WAIT_QUEUE_HEAD(cgroup_rmdir_waitq);
740 static void cgroup_wakeup_rmdir_waiters(const struct cgroup *cgrp)
742 if (unlikely(test_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags)))
743 wake_up_all(&cgroup_rmdir_waitq);
746 static int rebind_subsystems(struct cgroupfs_root *root,
747 unsigned long final_bits)
749 unsigned long added_bits, removed_bits;
750 struct cgroup *cgrp = &root->top_cgroup;
753 removed_bits = root->actual_subsys_bits & ~final_bits;
754 added_bits = final_bits & ~root->actual_subsys_bits;
755 /* Check that any added subsystems are currently free */
756 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
757 unsigned long bit = 1UL << i;
758 struct cgroup_subsys *ss = subsys[i];
759 if (!(bit & added_bits))
761 if (ss->root != &rootnode) {
762 /* Subsystem isn't free */
767 /* Currently we don't handle adding/removing subsystems when
768 * any child cgroups exist. This is theoretically supportable
769 * but involves complex error handling, so it's being left until
771 if (root->number_of_cgroups > 1)
774 /* Process each subsystem */
775 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
776 struct cgroup_subsys *ss = subsys[i];
777 unsigned long bit = 1UL << i;
778 if (bit & added_bits) {
779 /* We're binding this subsystem to this hierarchy */
780 BUG_ON(cgrp->subsys[i]);
781 BUG_ON(!dummytop->subsys[i]);
782 BUG_ON(dummytop->subsys[i]->cgroup != dummytop);
783 mutex_lock(&ss->hierarchy_mutex);
784 cgrp->subsys[i] = dummytop->subsys[i];
785 cgrp->subsys[i]->cgroup = cgrp;
786 list_move(&ss->sibling, &root->subsys_list);
790 mutex_unlock(&ss->hierarchy_mutex);
791 } else if (bit & removed_bits) {
792 /* We're removing this subsystem */
793 BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]);
794 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
795 mutex_lock(&ss->hierarchy_mutex);
797 ss->bind(ss, dummytop);
798 dummytop->subsys[i]->cgroup = dummytop;
799 cgrp->subsys[i] = NULL;
800 subsys[i]->root = &rootnode;
801 list_move(&ss->sibling, &rootnode.subsys_list);
802 mutex_unlock(&ss->hierarchy_mutex);
803 } else if (bit & final_bits) {
804 /* Subsystem state should already exist */
805 BUG_ON(!cgrp->subsys[i]);
807 /* Subsystem state shouldn't exist */
808 BUG_ON(cgrp->subsys[i]);
811 root->subsys_bits = root->actual_subsys_bits = final_bits;
817 static int cgroup_show_options(struct seq_file *seq, struct vfsmount *vfs)
819 struct cgroupfs_root *root = vfs->mnt_sb->s_fs_info;
820 struct cgroup_subsys *ss;
822 mutex_lock(&cgroup_mutex);
823 for_each_subsys(root, ss)
824 seq_printf(seq, ",%s", ss->name);
825 if (test_bit(ROOT_NOPREFIX, &root->flags))
826 seq_puts(seq, ",noprefix");
827 if (strlen(root->release_agent_path))
828 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
829 mutex_unlock(&cgroup_mutex);
833 struct cgroup_sb_opts {
834 unsigned long subsys_bits;
839 /* Convert a hierarchy specifier into a bitmask of subsystems and
841 static int parse_cgroupfs_options(char *data,
842 struct cgroup_sb_opts *opts)
844 char *token, *o = data ?: "all";
846 opts->subsys_bits = 0;
848 opts->release_agent = NULL;
850 while ((token = strsep(&o, ",")) != NULL) {
853 if (!strcmp(token, "all")) {
854 /* Add all non-disabled subsystems */
856 opts->subsys_bits = 0;
857 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
858 struct cgroup_subsys *ss = subsys[i];
860 opts->subsys_bits |= 1ul << i;
862 } else if (!strcmp(token, "noprefix")) {
863 set_bit(ROOT_NOPREFIX, &opts->flags);
864 } else if (!strncmp(token, "release_agent=", 14)) {
865 /* Specifying two release agents is forbidden */
866 if (opts->release_agent)
868 opts->release_agent = kzalloc(PATH_MAX, GFP_KERNEL);
869 if (!opts->release_agent)
871 strncpy(opts->release_agent, token + 14, PATH_MAX - 1);
872 opts->release_agent[PATH_MAX - 1] = 0;
874 struct cgroup_subsys *ss;
876 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
878 if (!strcmp(token, ss->name)) {
880 set_bit(i, &opts->subsys_bits);
884 if (i == CGROUP_SUBSYS_COUNT)
889 /* We can't have an empty hierarchy */
890 if (!opts->subsys_bits)
896 static int cgroup_remount(struct super_block *sb, int *flags, char *data)
899 struct cgroupfs_root *root = sb->s_fs_info;
900 struct cgroup *cgrp = &root->top_cgroup;
901 struct cgroup_sb_opts opts;
903 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
904 mutex_lock(&cgroup_mutex);
906 /* See what subsystems are wanted */
907 ret = parse_cgroupfs_options(data, &opts);
911 /* Don't allow flags to change at remount */
912 if (opts.flags != root->flags) {
917 ret = rebind_subsystems(root, opts.subsys_bits);
919 /* (re)populate subsystem files */
921 cgroup_populate_dir(cgrp);
923 if (opts.release_agent)
924 strcpy(root->release_agent_path, opts.release_agent);
926 if (opts.release_agent)
927 kfree(opts.release_agent);
928 mutex_unlock(&cgroup_mutex);
929 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
933 static struct super_operations cgroup_ops = {
934 .statfs = simple_statfs,
935 .drop_inode = generic_delete_inode,
936 .show_options = cgroup_show_options,
937 .remount_fs = cgroup_remount,
940 static void init_cgroup_housekeeping(struct cgroup *cgrp)
942 INIT_LIST_HEAD(&cgrp->sibling);
943 INIT_LIST_HEAD(&cgrp->children);
944 INIT_LIST_HEAD(&cgrp->css_sets);
945 INIT_LIST_HEAD(&cgrp->release_list);
946 init_rwsem(&cgrp->pids_mutex);
948 static void init_cgroup_root(struct cgroupfs_root *root)
950 struct cgroup *cgrp = &root->top_cgroup;
951 INIT_LIST_HEAD(&root->subsys_list);
952 INIT_LIST_HEAD(&root->root_list);
953 root->number_of_cgroups = 1;
955 cgrp->top_cgroup = cgrp;
956 init_cgroup_housekeeping(cgrp);
959 static int cgroup_test_super(struct super_block *sb, void *data)
961 struct cgroupfs_root *new = data;
962 struct cgroupfs_root *root = sb->s_fs_info;
964 /* First check subsystems */
965 if (new->subsys_bits != root->subsys_bits)
968 /* Next check flags */
969 if (new->flags != root->flags)
975 static int cgroup_set_super(struct super_block *sb, void *data)
978 struct cgroupfs_root *root = data;
980 ret = set_anon_super(sb, NULL);
984 sb->s_fs_info = root;
987 sb->s_blocksize = PAGE_CACHE_SIZE;
988 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
989 sb->s_magic = CGROUP_SUPER_MAGIC;
990 sb->s_op = &cgroup_ops;
995 static int cgroup_get_rootdir(struct super_block *sb)
997 struct inode *inode =
998 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
999 struct dentry *dentry;
1004 inode->i_fop = &simple_dir_operations;
1005 inode->i_op = &cgroup_dir_inode_operations;
1006 /* directories start off with i_nlink == 2 (for "." entry) */
1008 dentry = d_alloc_root(inode);
1013 sb->s_root = dentry;
1017 static int cgroup_get_sb(struct file_system_type *fs_type,
1018 int flags, const char *unused_dev_name,
1019 void *data, struct vfsmount *mnt)
1021 struct cgroup_sb_opts opts;
1023 struct super_block *sb;
1024 struct cgroupfs_root *root;
1025 struct list_head tmp_cg_links;
1027 /* First find the desired set of subsystems */
1028 ret = parse_cgroupfs_options(data, &opts);
1030 if (opts.release_agent)
1031 kfree(opts.release_agent);
1035 root = kzalloc(sizeof(*root), GFP_KERNEL);
1037 if (opts.release_agent)
1038 kfree(opts.release_agent);
1042 init_cgroup_root(root);
1043 root->subsys_bits = opts.subsys_bits;
1044 root->flags = opts.flags;
1045 if (opts.release_agent) {
1046 strcpy(root->release_agent_path, opts.release_agent);
1047 kfree(opts.release_agent);
1050 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, root);
1057 if (sb->s_fs_info != root) {
1058 /* Reusing an existing superblock */
1059 BUG_ON(sb->s_root == NULL);
1063 /* New superblock */
1064 struct cgroup *root_cgrp = &root->top_cgroup;
1065 struct inode *inode;
1068 BUG_ON(sb->s_root != NULL);
1070 ret = cgroup_get_rootdir(sb);
1072 goto drop_new_super;
1073 inode = sb->s_root->d_inode;
1075 mutex_lock(&inode->i_mutex);
1076 mutex_lock(&cgroup_mutex);
1079 * We're accessing css_set_count without locking
1080 * css_set_lock here, but that's OK - it can only be
1081 * increased by someone holding cgroup_lock, and
1082 * that's us. The worst that can happen is that we
1083 * have some link structures left over
1085 ret = allocate_cg_links(css_set_count, &tmp_cg_links);
1087 mutex_unlock(&cgroup_mutex);
1088 mutex_unlock(&inode->i_mutex);
1089 goto drop_new_super;
1092 ret = rebind_subsystems(root, root->subsys_bits);
1093 if (ret == -EBUSY) {
1094 mutex_unlock(&cgroup_mutex);
1095 mutex_unlock(&inode->i_mutex);
1099 /* EBUSY should be the only error here */
1102 list_add(&root->root_list, &roots);
1105 sb->s_root->d_fsdata = root_cgrp;
1106 root->top_cgroup.dentry = sb->s_root;
1108 /* Link the top cgroup in this hierarchy into all
1109 * the css_set objects */
1110 write_lock(&css_set_lock);
1111 for (i = 0; i < CSS_SET_TABLE_SIZE; i++) {
1112 struct hlist_head *hhead = &css_set_table[i];
1113 struct hlist_node *node;
1116 hlist_for_each_entry(cg, node, hhead, hlist)
1117 link_css_set(&tmp_cg_links, cg, root_cgrp);
1119 write_unlock(&css_set_lock);
1121 free_cg_links(&tmp_cg_links);
1123 BUG_ON(!list_empty(&root_cgrp->sibling));
1124 BUG_ON(!list_empty(&root_cgrp->children));
1125 BUG_ON(root->number_of_cgroups != 1);
1127 cgroup_populate_dir(root_cgrp);
1128 mutex_unlock(&inode->i_mutex);
1129 mutex_unlock(&cgroup_mutex);
1132 simple_set_mnt(mnt, sb);
1136 free_cg_links(&tmp_cg_links);
1138 up_write(&sb->s_umount);
1139 deactivate_super(sb);
1143 static void cgroup_kill_sb(struct super_block *sb) {
1144 struct cgroupfs_root *root = sb->s_fs_info;
1145 struct cgroup *cgrp = &root->top_cgroup;
1147 struct cg_cgroup_link *link;
1148 struct cg_cgroup_link *saved_link;
1152 BUG_ON(root->number_of_cgroups != 1);
1153 BUG_ON(!list_empty(&cgrp->children));
1154 BUG_ON(!list_empty(&cgrp->sibling));
1156 mutex_lock(&cgroup_mutex);
1158 /* Rebind all subsystems back to the default hierarchy */
1159 ret = rebind_subsystems(root, 0);
1160 /* Shouldn't be able to fail ... */
1164 * Release all the links from css_sets to this hierarchy's
1167 write_lock(&css_set_lock);
1169 list_for_each_entry_safe(link, saved_link, &cgrp->css_sets,
1171 list_del(&link->cg_link_list);
1172 list_del(&link->cgrp_link_list);
1175 write_unlock(&css_set_lock);
1177 if (!list_empty(&root->root_list)) {
1178 list_del(&root->root_list);
1182 mutex_unlock(&cgroup_mutex);
1184 kill_litter_super(sb);
1188 static struct file_system_type cgroup_fs_type = {
1190 .get_sb = cgroup_get_sb,
1191 .kill_sb = cgroup_kill_sb,
1194 static inline struct cgroup *__d_cgrp(struct dentry *dentry)
1196 return dentry->d_fsdata;
1199 static inline struct cftype *__d_cft(struct dentry *dentry)
1201 return dentry->d_fsdata;
1205 * cgroup_path - generate the path of a cgroup
1206 * @cgrp: the cgroup in question
1207 * @buf: the buffer to write the path into
1208 * @buflen: the length of the buffer
1210 * Called with cgroup_mutex held or else with an RCU-protected cgroup
1211 * reference. Writes path of cgroup into buf. Returns 0 on success,
1214 int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
1217 struct dentry *dentry = rcu_dereference(cgrp->dentry);
1219 if (!dentry || cgrp == dummytop) {
1221 * Inactive subsystems have no dentry for their root
1228 start = buf + buflen;
1232 int len = dentry->d_name.len;
1233 if ((start -= len) < buf)
1234 return -ENAMETOOLONG;
1235 memcpy(start, cgrp->dentry->d_name.name, len);
1236 cgrp = cgrp->parent;
1239 dentry = rcu_dereference(cgrp->dentry);
1243 return -ENAMETOOLONG;
1246 memmove(buf, start, buf + buflen - start);
1251 * Return the first subsystem attached to a cgroup's hierarchy, and
1255 static void get_first_subsys(const struct cgroup *cgrp,
1256 struct cgroup_subsys_state **css, int *subsys_id)
1258 const struct cgroupfs_root *root = cgrp->root;
1259 const struct cgroup_subsys *test_ss;
1260 BUG_ON(list_empty(&root->subsys_list));
1261 test_ss = list_entry(root->subsys_list.next,
1262 struct cgroup_subsys, sibling);
1264 *css = cgrp->subsys[test_ss->subsys_id];
1268 *subsys_id = test_ss->subsys_id;
1272 * cgroup_attach_task - attach task 'tsk' to cgroup 'cgrp'
1273 * @cgrp: the cgroup the task is attaching to
1274 * @tsk: the task to be attached
1276 * Call holding cgroup_mutex. May take task_lock of
1277 * the task 'tsk' during call.
1279 int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
1282 struct cgroup_subsys *ss;
1283 struct cgroup *oldcgrp;
1285 struct css_set *newcg;
1286 struct cgroupfs_root *root = cgrp->root;
1289 get_first_subsys(cgrp, NULL, &subsys_id);
1291 /* Nothing to do if the task is already in that cgroup */
1292 oldcgrp = task_cgroup(tsk, subsys_id);
1293 if (cgrp == oldcgrp)
1296 for_each_subsys(root, ss) {
1297 if (ss->can_attach) {
1298 retval = ss->can_attach(ss, cgrp, tsk);
1309 * Locate or allocate a new css_set for this task,
1310 * based on its final set of cgroups
1312 newcg = find_css_set(cg, cgrp);
1318 if (tsk->flags & PF_EXITING) {
1323 rcu_assign_pointer(tsk->cgroups, newcg);
1326 /* Update the css_set linked lists if we're using them */
1327 write_lock(&css_set_lock);
1328 if (!list_empty(&tsk->cg_list)) {
1329 list_del(&tsk->cg_list);
1330 list_add(&tsk->cg_list, &newcg->tasks);
1332 write_unlock(&css_set_lock);
1334 for_each_subsys(root, ss) {
1336 ss->attach(ss, cgrp, oldcgrp, tsk);
1338 set_bit(CGRP_RELEASABLE, &oldcgrp->flags);
1343 * wake up rmdir() waiter. the rmdir should fail since the cgroup
1344 * is no longer empty.
1346 cgroup_wakeup_rmdir_waiters(cgrp);
1351 * Attach task with pid 'pid' to cgroup 'cgrp'. Call with cgroup_mutex
1352 * held. May take task_lock of task
1354 static int attach_task_by_pid(struct cgroup *cgrp, u64 pid)
1356 struct task_struct *tsk;
1357 const struct cred *cred = current_cred(), *tcred;
1362 tsk = find_task_by_vpid(pid);
1363 if (!tsk || tsk->flags & PF_EXITING) {
1368 tcred = __task_cred(tsk);
1370 cred->euid != tcred->uid &&
1371 cred->euid != tcred->suid) {
1375 get_task_struct(tsk);
1379 get_task_struct(tsk);
1382 ret = cgroup_attach_task(cgrp, tsk);
1383 put_task_struct(tsk);
1387 static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid)
1390 if (!cgroup_lock_live_group(cgrp))
1392 ret = attach_task_by_pid(cgrp, pid);
1397 /* The various types of files and directories in a cgroup file system */
1398 enum cgroup_filetype {
1402 FILE_NOTIFY_ON_RELEASE,
1407 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
1408 * @cgrp: the cgroup to be checked for liveness
1410 * On success, returns true; the lock should be later released with
1411 * cgroup_unlock(). On failure returns false with no lock held.
1413 bool cgroup_lock_live_group(struct cgroup *cgrp)
1415 mutex_lock(&cgroup_mutex);
1416 if (cgroup_is_removed(cgrp)) {
1417 mutex_unlock(&cgroup_mutex);
1423 static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
1426 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
1427 if (!cgroup_lock_live_group(cgrp))
1429 strcpy(cgrp->root->release_agent_path, buffer);
1434 static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
1435 struct seq_file *seq)
1437 if (!cgroup_lock_live_group(cgrp))
1439 seq_puts(seq, cgrp->root->release_agent_path);
1440 seq_putc(seq, '\n');
1445 /* A buffer size big enough for numbers or short strings */
1446 #define CGROUP_LOCAL_BUFFER_SIZE 64
1448 static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
1450 const char __user *userbuf,
1451 size_t nbytes, loff_t *unused_ppos)
1453 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
1459 if (nbytes >= sizeof(buffer))
1461 if (copy_from_user(buffer, userbuf, nbytes))
1464 buffer[nbytes] = 0; /* nul-terminate */
1466 if (cft->write_u64) {
1467 u64 val = simple_strtoull(buffer, &end, 0);
1470 retval = cft->write_u64(cgrp, cft, val);
1472 s64 val = simple_strtoll(buffer, &end, 0);
1475 retval = cft->write_s64(cgrp, cft, val);
1482 static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
1484 const char __user *userbuf,
1485 size_t nbytes, loff_t *unused_ppos)
1487 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
1489 size_t max_bytes = cft->max_write_len;
1490 char *buffer = local_buffer;
1493 max_bytes = sizeof(local_buffer) - 1;
1494 if (nbytes >= max_bytes)
1496 /* Allocate a dynamic buffer if we need one */
1497 if (nbytes >= sizeof(local_buffer)) {
1498 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
1502 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
1507 buffer[nbytes] = 0; /* nul-terminate */
1509 retval = cft->write_string(cgrp, cft, buffer);
1513 if (buffer != local_buffer)
1518 static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
1519 size_t nbytes, loff_t *ppos)
1521 struct cftype *cft = __d_cft(file->f_dentry);
1522 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
1524 if (cgroup_is_removed(cgrp))
1527 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
1528 if (cft->write_u64 || cft->write_s64)
1529 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
1530 if (cft->write_string)
1531 return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
1533 int ret = cft->trigger(cgrp, (unsigned int)cft->private);
1534 return ret ? ret : nbytes;
1539 static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
1541 char __user *buf, size_t nbytes,
1544 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
1545 u64 val = cft->read_u64(cgrp, cft);
1546 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
1548 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
1551 static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
1553 char __user *buf, size_t nbytes,
1556 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
1557 s64 val = cft->read_s64(cgrp, cft);
1558 int len = sprintf(tmp, "%lld\n", (long long) val);
1560 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
1563 static ssize_t cgroup_file_read(struct file *file, char __user *buf,
1564 size_t nbytes, loff_t *ppos)
1566 struct cftype *cft = __d_cft(file->f_dentry);
1567 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
1569 if (cgroup_is_removed(cgrp))
1573 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
1575 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
1577 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
1582 * seqfile ops/methods for returning structured data. Currently just
1583 * supports string->u64 maps, but can be extended in future.
1586 struct cgroup_seqfile_state {
1588 struct cgroup *cgroup;
1591 static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
1593 struct seq_file *sf = cb->state;
1594 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
1597 static int cgroup_seqfile_show(struct seq_file *m, void *arg)
1599 struct cgroup_seqfile_state *state = m->private;
1600 struct cftype *cft = state->cft;
1601 if (cft->read_map) {
1602 struct cgroup_map_cb cb = {
1603 .fill = cgroup_map_add,
1606 return cft->read_map(state->cgroup, cft, &cb);
1608 return cft->read_seq_string(state->cgroup, cft, m);
1611 static int cgroup_seqfile_release(struct inode *inode, struct file *file)
1613 struct seq_file *seq = file->private_data;
1614 kfree(seq->private);
1615 return single_release(inode, file);
1618 static struct file_operations cgroup_seqfile_operations = {
1620 .write = cgroup_file_write,
1621 .llseek = seq_lseek,
1622 .release = cgroup_seqfile_release,
1625 static int cgroup_file_open(struct inode *inode, struct file *file)
1630 err = generic_file_open(inode, file);
1633 cft = __d_cft(file->f_dentry);
1635 if (cft->read_map || cft->read_seq_string) {
1636 struct cgroup_seqfile_state *state =
1637 kzalloc(sizeof(*state), GFP_USER);
1641 state->cgroup = __d_cgrp(file->f_dentry->d_parent);
1642 file->f_op = &cgroup_seqfile_operations;
1643 err = single_open(file, cgroup_seqfile_show, state);
1646 } else if (cft->open)
1647 err = cft->open(inode, file);
1654 static int cgroup_file_release(struct inode *inode, struct file *file)
1656 struct cftype *cft = __d_cft(file->f_dentry);
1658 return cft->release(inode, file);
1663 * cgroup_rename - Only allow simple rename of directories in place.
1665 static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
1666 struct inode *new_dir, struct dentry *new_dentry)
1668 if (!S_ISDIR(old_dentry->d_inode->i_mode))
1670 if (new_dentry->d_inode)
1672 if (old_dir != new_dir)
1674 return simple_rename(old_dir, old_dentry, new_dir, new_dentry);
1677 static struct file_operations cgroup_file_operations = {
1678 .read = cgroup_file_read,
1679 .write = cgroup_file_write,
1680 .llseek = generic_file_llseek,
1681 .open = cgroup_file_open,
1682 .release = cgroup_file_release,
1685 static struct inode_operations cgroup_dir_inode_operations = {
1686 .lookup = simple_lookup,
1687 .mkdir = cgroup_mkdir,
1688 .rmdir = cgroup_rmdir,
1689 .rename = cgroup_rename,
1692 static int cgroup_create_file(struct dentry *dentry, int mode,
1693 struct super_block *sb)
1695 static const struct dentry_operations cgroup_dops = {
1696 .d_iput = cgroup_diput,
1699 struct inode *inode;
1703 if (dentry->d_inode)
1706 inode = cgroup_new_inode(mode, sb);
1710 if (S_ISDIR(mode)) {
1711 inode->i_op = &cgroup_dir_inode_operations;
1712 inode->i_fop = &simple_dir_operations;
1714 /* start off with i_nlink == 2 (for "." entry) */
1717 /* start with the directory inode held, so that we can
1718 * populate it without racing with another mkdir */
1719 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
1720 } else if (S_ISREG(mode)) {
1722 inode->i_fop = &cgroup_file_operations;
1724 dentry->d_op = &cgroup_dops;
1725 d_instantiate(dentry, inode);
1726 dget(dentry); /* Extra count - pin the dentry in core */
1731 * cgroup_create_dir - create a directory for an object.
1732 * @cgrp: the cgroup we create the directory for. It must have a valid
1733 * ->parent field. And we are going to fill its ->dentry field.
1734 * @dentry: dentry of the new cgroup
1735 * @mode: mode to set on new directory.
1737 static int cgroup_create_dir(struct cgroup *cgrp, struct dentry *dentry,
1740 struct dentry *parent;
1743 parent = cgrp->parent->dentry;
1744 error = cgroup_create_file(dentry, S_IFDIR | mode, cgrp->root->sb);
1746 dentry->d_fsdata = cgrp;
1747 inc_nlink(parent->d_inode);
1748 rcu_assign_pointer(cgrp->dentry, dentry);
1756 int cgroup_add_file(struct cgroup *cgrp,
1757 struct cgroup_subsys *subsys,
1758 const struct cftype *cft)
1760 struct dentry *dir = cgrp->dentry;
1761 struct dentry *dentry;
1764 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
1765 if (subsys && !test_bit(ROOT_NOPREFIX, &cgrp->root->flags)) {
1766 strcpy(name, subsys->name);
1769 strcat(name, cft->name);
1770 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
1771 dentry = lookup_one_len(name, dir, strlen(name));
1772 if (!IS_ERR(dentry)) {
1773 error = cgroup_create_file(dentry, 0644 | S_IFREG,
1776 dentry->d_fsdata = (void *)cft;
1779 error = PTR_ERR(dentry);
1783 int cgroup_add_files(struct cgroup *cgrp,
1784 struct cgroup_subsys *subsys,
1785 const struct cftype cft[],
1789 for (i = 0; i < count; i++) {
1790 err = cgroup_add_file(cgrp, subsys, &cft[i]);
1798 * cgroup_task_count - count the number of tasks in a cgroup.
1799 * @cgrp: the cgroup in question
1801 * Return the number of tasks in the cgroup.
1803 int cgroup_task_count(const struct cgroup *cgrp)
1806 struct cg_cgroup_link *link;
1808 read_lock(&css_set_lock);
1809 list_for_each_entry(link, &cgrp->css_sets, cgrp_link_list) {
1810 count += atomic_read(&link->cg->refcount);
1812 read_unlock(&css_set_lock);
1817 * Advance a list_head iterator. The iterator should be positioned at
1818 * the start of a css_set
1820 static void cgroup_advance_iter(struct cgroup *cgrp,
1821 struct cgroup_iter *it)
1823 struct list_head *l = it->cg_link;
1824 struct cg_cgroup_link *link;
1827 /* Advance to the next non-empty css_set */
1830 if (l == &cgrp->css_sets) {
1834 link = list_entry(l, struct cg_cgroup_link, cgrp_link_list);
1836 } while (list_empty(&cg->tasks));
1838 it->task = cg->tasks.next;
1842 * To reduce the fork() overhead for systems that are not actually
1843 * using their cgroups capability, we don't maintain the lists running
1844 * through each css_set to its tasks until we see the list actually
1845 * used - in other words after the first call to cgroup_iter_start().
1847 * The tasklist_lock is not held here, as do_each_thread() and
1848 * while_each_thread() are protected by RCU.
1850 static void cgroup_enable_task_cg_lists(void)
1852 struct task_struct *p, *g;
1853 write_lock(&css_set_lock);
1854 use_task_css_set_links = 1;
1855 do_each_thread(g, p) {
1858 * We should check if the process is exiting, otherwise
1859 * it will race with cgroup_exit() in that the list
1860 * entry won't be deleted though the process has exited.
1862 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
1863 list_add(&p->cg_list, &p->cgroups->tasks);
1865 } while_each_thread(g, p);
1866 write_unlock(&css_set_lock);
1869 void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
1872 * The first time anyone tries to iterate across a cgroup,
1873 * we need to enable the list linking each css_set to its
1874 * tasks, and fix up all existing tasks.
1876 if (!use_task_css_set_links)
1877 cgroup_enable_task_cg_lists();
1879 read_lock(&css_set_lock);
1880 it->cg_link = &cgrp->css_sets;
1881 cgroup_advance_iter(cgrp, it);
1884 struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
1885 struct cgroup_iter *it)
1887 struct task_struct *res;
1888 struct list_head *l = it->task;
1889 struct cg_cgroup_link *link;
1891 /* If the iterator cg is NULL, we have no tasks */
1894 res = list_entry(l, struct task_struct, cg_list);
1895 /* Advance iterator to find next entry */
1897 link = list_entry(it->cg_link, struct cg_cgroup_link, cgrp_link_list);
1898 if (l == &link->cg->tasks) {
1899 /* We reached the end of this task list - move on to
1900 * the next cg_cgroup_link */
1901 cgroup_advance_iter(cgrp, it);
1908 void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
1910 read_unlock(&css_set_lock);
1913 static inline int started_after_time(struct task_struct *t1,
1914 struct timespec *time,
1915 struct task_struct *t2)
1917 int start_diff = timespec_compare(&t1->start_time, time);
1918 if (start_diff > 0) {
1920 } else if (start_diff < 0) {
1924 * Arbitrarily, if two processes started at the same
1925 * time, we'll say that the lower pointer value
1926 * started first. Note that t2 may have exited by now
1927 * so this may not be a valid pointer any longer, but
1928 * that's fine - it still serves to distinguish
1929 * between two tasks started (effectively) simultaneously.
1936 * This function is a callback from heap_insert() and is used to order
1938 * In this case we order the heap in descending task start time.
1940 static inline int started_after(void *p1, void *p2)
1942 struct task_struct *t1 = p1;
1943 struct task_struct *t2 = p2;
1944 return started_after_time(t1, &t2->start_time, t2);
1948 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
1949 * @scan: struct cgroup_scanner containing arguments for the scan
1951 * Arguments include pointers to callback functions test_task() and
1953 * Iterate through all the tasks in a cgroup, calling test_task() for each,
1954 * and if it returns true, call process_task() for it also.
1955 * The test_task pointer may be NULL, meaning always true (select all tasks).
1956 * Effectively duplicates cgroup_iter_{start,next,end}()
1957 * but does not lock css_set_lock for the call to process_task().
1958 * The struct cgroup_scanner may be embedded in any structure of the caller's
1960 * It is guaranteed that process_task() will act on every task that
1961 * is a member of the cgroup for the duration of this call. This
1962 * function may or may not call process_task() for tasks that exit
1963 * or move to a different cgroup during the call, or are forked or
1964 * move into the cgroup during the call.
1966 * Note that test_task() may be called with locks held, and may in some
1967 * situations be called multiple times for the same task, so it should
1969 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
1970 * pre-allocated and will be used for heap operations (and its "gt" member will
1971 * be overwritten), else a temporary heap will be used (allocation of which
1972 * may cause this function to fail).
1974 int cgroup_scan_tasks(struct cgroup_scanner *scan)
1977 struct cgroup_iter it;
1978 struct task_struct *p, *dropped;
1979 /* Never dereference latest_task, since it's not refcounted */
1980 struct task_struct *latest_task = NULL;
1981 struct ptr_heap tmp_heap;
1982 struct ptr_heap *heap;
1983 struct timespec latest_time = { 0, 0 };
1986 /* The caller supplied our heap and pre-allocated its memory */
1988 heap->gt = &started_after;
1990 /* We need to allocate our own heap memory */
1992 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
1994 /* cannot allocate the heap */
2000 * Scan tasks in the cgroup, using the scanner's "test_task" callback
2001 * to determine which are of interest, and using the scanner's
2002 * "process_task" callback to process any of them that need an update.
2003 * Since we don't want to hold any locks during the task updates,
2004 * gather tasks to be processed in a heap structure.
2005 * The heap is sorted by descending task start time.
2006 * If the statically-sized heap fills up, we overflow tasks that
2007 * started later, and in future iterations only consider tasks that
2008 * started after the latest task in the previous pass. This
2009 * guarantees forward progress and that we don't miss any tasks.
2012 cgroup_iter_start(scan->cg, &it);
2013 while ((p = cgroup_iter_next(scan->cg, &it))) {
2015 * Only affect tasks that qualify per the caller's callback,
2016 * if he provided one
2018 if (scan->test_task && !scan->test_task(p, scan))
2021 * Only process tasks that started after the last task
2024 if (!started_after_time(p, &latest_time, latest_task))
2026 dropped = heap_insert(heap, p);
2027 if (dropped == NULL) {
2029 * The new task was inserted; the heap wasn't
2033 } else if (dropped != p) {
2035 * The new task was inserted, and pushed out a
2039 put_task_struct(dropped);
2042 * Else the new task was newer than anything already in
2043 * the heap and wasn't inserted
2046 cgroup_iter_end(scan->cg, &it);
2049 for (i = 0; i < heap->size; i++) {
2050 struct task_struct *q = heap->ptrs[i];
2052 latest_time = q->start_time;
2055 /* Process the task per the caller's callback */
2056 scan->process_task(q, scan);
2060 * If we had to process any tasks at all, scan again
2061 * in case some of them were in the middle of forking
2062 * children that didn't get processed.
2063 * Not the most efficient way to do it, but it avoids
2064 * having to take callback_mutex in the fork path
2068 if (heap == &tmp_heap)
2069 heap_free(&tmp_heap);
2074 * Stuff for reading the 'tasks' file.
2076 * Reading this file can return large amounts of data if a cgroup has
2077 * *lots* of attached tasks. So it may need several calls to read(),
2078 * but we cannot guarantee that the information we produce is correct
2079 * unless we produce it entirely atomically.
2084 * Load into 'pidarray' up to 'npids' of the tasks using cgroup
2085 * 'cgrp'. Return actual number of pids loaded. No need to
2086 * task_lock(p) when reading out p->cgroup, since we're in an RCU
2087 * read section, so the css_set can't go away, and is
2088 * immutable after creation.
2090 static int pid_array_load(pid_t *pidarray, int npids, struct cgroup *cgrp)
2093 struct cgroup_iter it;
2094 struct task_struct *tsk;
2095 cgroup_iter_start(cgrp, &it);
2096 while ((tsk = cgroup_iter_next(cgrp, &it))) {
2097 if (unlikely(n == npids))
2099 pid = task_pid_vnr(tsk);
2101 pidarray[n++] = pid;
2103 cgroup_iter_end(cgrp, &it);
2108 * cgroupstats_build - build and fill cgroupstats
2109 * @stats: cgroupstats to fill information into
2110 * @dentry: A dentry entry belonging to the cgroup for which stats have
2113 * Build and fill cgroupstats so that taskstats can export it to user
2116 int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
2119 struct cgroup *cgrp;
2120 struct cgroup_iter it;
2121 struct task_struct *tsk;
2124 * Validate dentry by checking the superblock operations,
2125 * and make sure it's a directory.
2127 if (dentry->d_sb->s_op != &cgroup_ops ||
2128 !S_ISDIR(dentry->d_inode->i_mode))
2132 cgrp = dentry->d_fsdata;
2134 cgroup_iter_start(cgrp, &it);
2135 while ((tsk = cgroup_iter_next(cgrp, &it))) {
2136 switch (tsk->state) {
2138 stats->nr_running++;
2140 case TASK_INTERRUPTIBLE:
2141 stats->nr_sleeping++;
2143 case TASK_UNINTERRUPTIBLE:
2144 stats->nr_uninterruptible++;
2147 stats->nr_stopped++;
2150 if (delayacct_is_task_waiting_on_io(tsk))
2151 stats->nr_io_wait++;
2155 cgroup_iter_end(cgrp, &it);
2161 static int cmppid(const void *a, const void *b)
2163 return *(pid_t *)a - *(pid_t *)b;
2168 * seq_file methods for the "tasks" file. The seq_file position is the
2169 * next pid to display; the seq_file iterator is a pointer to the pid
2170 * in the cgroup->tasks_pids array.
2173 static void *cgroup_tasks_start(struct seq_file *s, loff_t *pos)
2176 * Initially we receive a position value that corresponds to
2177 * one more than the last pid shown (or 0 on the first call or
2178 * after a seek to the start). Use a binary-search to find the
2179 * next pid to display, if any
2181 struct cgroup *cgrp = s->private;
2182 int index = 0, pid = *pos;
2185 down_read(&cgrp->pids_mutex);
2187 int end = cgrp->pids_length;
2189 while (index < end) {
2190 int mid = (index + end) / 2;
2191 if (cgrp->tasks_pids[mid] == pid) {
2194 } else if (cgrp->tasks_pids[mid] <= pid)
2200 /* If we're off the end of the array, we're done */
2201 if (index >= cgrp->pids_length)
2203 /* Update the abstract position to be the actual pid that we found */
2204 iter = cgrp->tasks_pids + index;
2209 static void cgroup_tasks_stop(struct seq_file *s, void *v)
2211 struct cgroup *cgrp = s->private;
2212 up_read(&cgrp->pids_mutex);
2215 static void *cgroup_tasks_next(struct seq_file *s, void *v, loff_t *pos)
2217 struct cgroup *cgrp = s->private;
2219 int *end = cgrp->tasks_pids + cgrp->pids_length;
2222 * Advance to the next pid in the array. If this goes off the
2234 static int cgroup_tasks_show(struct seq_file *s, void *v)
2236 return seq_printf(s, "%d\n", *(int *)v);
2239 static struct seq_operations cgroup_tasks_seq_operations = {
2240 .start = cgroup_tasks_start,
2241 .stop = cgroup_tasks_stop,
2242 .next = cgroup_tasks_next,
2243 .show = cgroup_tasks_show,
2246 static void release_cgroup_pid_array(struct cgroup *cgrp)
2248 down_write(&cgrp->pids_mutex);
2249 BUG_ON(!cgrp->pids_use_count);
2250 if (!--cgrp->pids_use_count) {
2251 kfree(cgrp->tasks_pids);
2252 cgrp->tasks_pids = NULL;
2253 cgrp->pids_length = 0;
2255 up_write(&cgrp->pids_mutex);
2258 static int cgroup_tasks_release(struct inode *inode, struct file *file)
2260 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
2262 if (!(file->f_mode & FMODE_READ))
2265 release_cgroup_pid_array(cgrp);
2266 return seq_release(inode, file);
2269 static struct file_operations cgroup_tasks_operations = {
2271 .llseek = seq_lseek,
2272 .write = cgroup_file_write,
2273 .release = cgroup_tasks_release,
2277 * Handle an open on 'tasks' file. Prepare an array containing the
2278 * process id's of tasks currently attached to the cgroup being opened.
2281 static int cgroup_tasks_open(struct inode *unused, struct file *file)
2283 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
2288 /* Nothing to do for write-only files */
2289 if (!(file->f_mode & FMODE_READ))
2293 * If cgroup gets more users after we read count, we won't have
2294 * enough space - tough. This race is indistinguishable to the
2295 * caller from the case that the additional cgroup users didn't
2296 * show up until sometime later on.
2298 npids = cgroup_task_count(cgrp);
2299 pidarray = kmalloc(npids * sizeof(pid_t), GFP_KERNEL);
2302 npids = pid_array_load(pidarray, npids, cgrp);
2303 sort(pidarray, npids, sizeof(pid_t), cmppid, NULL);
2306 * Store the array in the cgroup, freeing the old
2307 * array if necessary
2309 down_write(&cgrp->pids_mutex);
2310 kfree(cgrp->tasks_pids);
2311 cgrp->tasks_pids = pidarray;
2312 cgrp->pids_length = npids;
2313 cgrp->pids_use_count++;
2314 up_write(&cgrp->pids_mutex);
2316 file->f_op = &cgroup_tasks_operations;
2318 retval = seq_open(file, &cgroup_tasks_seq_operations);
2320 release_cgroup_pid_array(cgrp);
2323 ((struct seq_file *)file->private_data)->private = cgrp;
2327 static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
2330 return notify_on_release(cgrp);
2333 static int cgroup_write_notify_on_release(struct cgroup *cgrp,
2337 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
2339 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
2341 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
2346 * for the common functions, 'private' gives the type of file
2348 static struct cftype files[] = {
2351 .open = cgroup_tasks_open,
2352 .write_u64 = cgroup_tasks_write,
2353 .release = cgroup_tasks_release,
2354 .private = FILE_TASKLIST,
2358 .name = "notify_on_release",
2359 .read_u64 = cgroup_read_notify_on_release,
2360 .write_u64 = cgroup_write_notify_on_release,
2361 .private = FILE_NOTIFY_ON_RELEASE,
2365 static struct cftype cft_release_agent = {
2366 .name = "release_agent",
2367 .read_seq_string = cgroup_release_agent_show,
2368 .write_string = cgroup_release_agent_write,
2369 .max_write_len = PATH_MAX,
2370 .private = FILE_RELEASE_AGENT,
2373 static int cgroup_populate_dir(struct cgroup *cgrp)
2376 struct cgroup_subsys *ss;
2378 /* First clear out any existing files */
2379 cgroup_clear_directory(cgrp->dentry);
2381 err = cgroup_add_files(cgrp, NULL, files, ARRAY_SIZE(files));
2385 if (cgrp == cgrp->top_cgroup) {
2386 if ((err = cgroup_add_file(cgrp, NULL, &cft_release_agent)) < 0)
2390 for_each_subsys(cgrp->root, ss) {
2391 if (ss->populate && (err = ss->populate(ss, cgrp)) < 0)
2394 /* This cgroup is ready now */
2395 for_each_subsys(cgrp->root, ss) {
2396 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
2398 * Update id->css pointer and make this css visible from
2399 * CSS ID functions. This pointer will be dereferened
2400 * from RCU-read-side without locks.
2403 rcu_assign_pointer(css->id->css, css);
2409 static void init_cgroup_css(struct cgroup_subsys_state *css,
2410 struct cgroup_subsys *ss,
2411 struct cgroup *cgrp)
2414 atomic_set(&css->refcnt, 1);
2417 if (cgrp == dummytop)
2418 set_bit(CSS_ROOT, &css->flags);
2419 BUG_ON(cgrp->subsys[ss->subsys_id]);
2420 cgrp->subsys[ss->subsys_id] = css;
2423 static void cgroup_lock_hierarchy(struct cgroupfs_root *root)
2425 /* We need to take each hierarchy_mutex in a consistent order */
2428 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2429 struct cgroup_subsys *ss = subsys[i];
2430 if (ss->root == root)
2431 mutex_lock(&ss->hierarchy_mutex);
2435 static void cgroup_unlock_hierarchy(struct cgroupfs_root *root)
2439 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2440 struct cgroup_subsys *ss = subsys[i];
2441 if (ss->root == root)
2442 mutex_unlock(&ss->hierarchy_mutex);
2447 * cgroup_create - create a cgroup
2448 * @parent: cgroup that will be parent of the new cgroup
2449 * @dentry: dentry of the new cgroup
2450 * @mode: mode to set on new inode
2452 * Must be called with the mutex on the parent inode held
2454 static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
2457 struct cgroup *cgrp;
2458 struct cgroupfs_root *root = parent->root;
2460 struct cgroup_subsys *ss;
2461 struct super_block *sb = root->sb;
2463 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
2467 /* Grab a reference on the superblock so the hierarchy doesn't
2468 * get deleted on unmount if there are child cgroups. This
2469 * can be done outside cgroup_mutex, since the sb can't
2470 * disappear while someone has an open control file on the
2472 atomic_inc(&sb->s_active);
2474 mutex_lock(&cgroup_mutex);
2476 init_cgroup_housekeeping(cgrp);
2478 cgrp->parent = parent;
2479 cgrp->root = parent->root;
2480 cgrp->top_cgroup = parent->top_cgroup;
2482 if (notify_on_release(parent))
2483 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
2485 for_each_subsys(root, ss) {
2486 struct cgroup_subsys_state *css = ss->create(ss, cgrp);
2491 init_cgroup_css(css, ss, cgrp);
2493 if (alloc_css_id(ss, parent, cgrp))
2495 /* At error, ->destroy() callback has to free assigned ID. */
2498 cgroup_lock_hierarchy(root);
2499 list_add(&cgrp->sibling, &cgrp->parent->children);
2500 cgroup_unlock_hierarchy(root);
2501 root->number_of_cgroups++;
2503 err = cgroup_create_dir(cgrp, dentry, mode);
2507 /* The cgroup directory was pre-locked for us */
2508 BUG_ON(!mutex_is_locked(&cgrp->dentry->d_inode->i_mutex));
2510 err = cgroup_populate_dir(cgrp);
2511 /* If err < 0, we have a half-filled directory - oh well ;) */
2513 mutex_unlock(&cgroup_mutex);
2514 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
2520 cgroup_lock_hierarchy(root);
2521 list_del(&cgrp->sibling);
2522 cgroup_unlock_hierarchy(root);
2523 root->number_of_cgroups--;
2527 for_each_subsys(root, ss) {
2528 if (cgrp->subsys[ss->subsys_id])
2529 ss->destroy(ss, cgrp);
2532 mutex_unlock(&cgroup_mutex);
2534 /* Release the reference count that we took on the superblock */
2535 deactivate_super(sb);
2541 static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2543 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
2545 /* the vfs holds inode->i_mutex already */
2546 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
2549 static int cgroup_has_css_refs(struct cgroup *cgrp)
2551 /* Check the reference count on each subsystem. Since we
2552 * already established that there are no tasks in the
2553 * cgroup, if the css refcount is also 1, then there should
2554 * be no outstanding references, so the subsystem is safe to
2555 * destroy. We scan across all subsystems rather than using
2556 * the per-hierarchy linked list of mounted subsystems since
2557 * we can be called via check_for_release() with no
2558 * synchronization other than RCU, and the subsystem linked
2559 * list isn't RCU-safe */
2561 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2562 struct cgroup_subsys *ss = subsys[i];
2563 struct cgroup_subsys_state *css;
2564 /* Skip subsystems not in this hierarchy */
2565 if (ss->root != cgrp->root)
2567 css = cgrp->subsys[ss->subsys_id];
2568 /* When called from check_for_release() it's possible
2569 * that by this point the cgroup has been removed
2570 * and the css deleted. But a false-positive doesn't
2571 * matter, since it can only happen if the cgroup
2572 * has been deleted and hence no longer needs the
2573 * release agent to be called anyway. */
2574 if (css && (atomic_read(&css->refcnt) > 1))
2581 * Atomically mark all (or else none) of the cgroup's CSS objects as
2582 * CSS_REMOVED. Return true on success, or false if the cgroup has
2583 * busy subsystems. Call with cgroup_mutex held
2586 static int cgroup_clear_css_refs(struct cgroup *cgrp)
2588 struct cgroup_subsys *ss;
2589 unsigned long flags;
2590 bool failed = false;
2591 local_irq_save(flags);
2592 for_each_subsys(cgrp->root, ss) {
2593 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
2596 /* We can only remove a CSS with a refcnt==1 */
2597 refcnt = atomic_read(&css->refcnt);
2604 * Drop the refcnt to 0 while we check other
2605 * subsystems. This will cause any racing
2606 * css_tryget() to spin until we set the
2607 * CSS_REMOVED bits or abort
2609 if (atomic_cmpxchg(&css->refcnt, refcnt, 0) == refcnt)
2615 for_each_subsys(cgrp->root, ss) {
2616 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
2619 * Restore old refcnt if we previously managed
2620 * to clear it from 1 to 0
2622 if (!atomic_read(&css->refcnt))
2623 atomic_set(&css->refcnt, 1);
2625 /* Commit the fact that the CSS is removed */
2626 set_bit(CSS_REMOVED, &css->flags);
2629 local_irq_restore(flags);
2633 static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
2635 struct cgroup *cgrp = dentry->d_fsdata;
2637 struct cgroup *parent;
2641 /* the vfs holds both inode->i_mutex already */
2643 mutex_lock(&cgroup_mutex);
2644 if (atomic_read(&cgrp->count) != 0) {
2645 mutex_unlock(&cgroup_mutex);
2648 if (!list_empty(&cgrp->children)) {
2649 mutex_unlock(&cgroup_mutex);
2652 mutex_unlock(&cgroup_mutex);
2655 * Call pre_destroy handlers of subsys. Notify subsystems
2656 * that rmdir() request comes.
2658 ret = cgroup_call_pre_destroy(cgrp);
2662 mutex_lock(&cgroup_mutex);
2663 parent = cgrp->parent;
2664 if (atomic_read(&cgrp->count) || !list_empty(&cgrp->children)) {
2665 mutex_unlock(&cgroup_mutex);
2669 * css_put/get is provided for subsys to grab refcnt to css. In typical
2670 * case, subsystem has no reference after pre_destroy(). But, under
2671 * hierarchy management, some *temporal* refcnt can be hold.
2672 * To avoid returning -EBUSY to a user, waitqueue is used. If subsys
2673 * is really busy, it should return -EBUSY at pre_destroy(). wake_up
2674 * is called when css_put() is called and refcnt goes down to 0.
2676 set_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
2677 prepare_to_wait(&cgroup_rmdir_waitq, &wait, TASK_INTERRUPTIBLE);
2679 if (!cgroup_clear_css_refs(cgrp)) {
2680 mutex_unlock(&cgroup_mutex);
2682 finish_wait(&cgroup_rmdir_waitq, &wait);
2683 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
2684 if (signal_pending(current))
2688 /* NO css_tryget() can success after here. */
2689 finish_wait(&cgroup_rmdir_waitq, &wait);
2690 clear_bit(CGRP_WAIT_ON_RMDIR, &cgrp->flags);
2692 spin_lock(&release_list_lock);
2693 set_bit(CGRP_REMOVED, &cgrp->flags);
2694 if (!list_empty(&cgrp->release_list))
2695 list_del(&cgrp->release_list);
2696 spin_unlock(&release_list_lock);
2698 cgroup_lock_hierarchy(cgrp->root);
2699 /* delete this cgroup from parent->children */
2700 list_del(&cgrp->sibling);
2701 cgroup_unlock_hierarchy(cgrp->root);
2703 spin_lock(&cgrp->dentry->d_lock);
2704 d = dget(cgrp->dentry);
2705 spin_unlock(&d->d_lock);
2707 cgroup_d_remove_dir(d);
2710 set_bit(CGRP_RELEASABLE, &parent->flags);
2711 check_for_release(parent);
2713 mutex_unlock(&cgroup_mutex);
2717 static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
2719 struct cgroup_subsys_state *css;
2721 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
2723 /* Create the top cgroup state for this subsystem */
2724 list_add(&ss->sibling, &rootnode.subsys_list);
2725 ss->root = &rootnode;
2726 css = ss->create(ss, dummytop);
2727 /* We don't handle early failures gracefully */
2728 BUG_ON(IS_ERR(css));
2729 init_cgroup_css(css, ss, dummytop);
2731 /* Update the init_css_set to contain a subsys
2732 * pointer to this state - since the subsystem is
2733 * newly registered, all tasks and hence the
2734 * init_css_set is in the subsystem's top cgroup. */
2735 init_css_set.subsys[ss->subsys_id] = dummytop->subsys[ss->subsys_id];
2737 need_forkexit_callback |= ss->fork || ss->exit;
2739 /* At system boot, before all subsystems have been
2740 * registered, no tasks have been forked, so we don't
2741 * need to invoke fork callbacks here. */
2742 BUG_ON(!list_empty(&init_task.tasks));
2744 mutex_init(&ss->hierarchy_mutex);
2745 lockdep_set_class(&ss->hierarchy_mutex, &ss->subsys_key);
2750 * cgroup_init_early - cgroup initialization at system boot
2752 * Initialize cgroups at system boot, and initialize any
2753 * subsystems that request early init.
2755 int __init cgroup_init_early(void)
2758 atomic_set(&init_css_set.refcount, 1);
2759 INIT_LIST_HEAD(&init_css_set.cg_links);
2760 INIT_LIST_HEAD(&init_css_set.tasks);
2761 INIT_HLIST_NODE(&init_css_set.hlist);
2763 init_cgroup_root(&rootnode);
2765 init_task.cgroups = &init_css_set;
2767 init_css_set_link.cg = &init_css_set;
2768 list_add(&init_css_set_link.cgrp_link_list,
2769 &rootnode.top_cgroup.css_sets);
2770 list_add(&init_css_set_link.cg_link_list,
2771 &init_css_set.cg_links);
2773 for (i = 0; i < CSS_SET_TABLE_SIZE; i++)
2774 INIT_HLIST_HEAD(&css_set_table[i]);
2776 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2777 struct cgroup_subsys *ss = subsys[i];
2780 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
2781 BUG_ON(!ss->create);
2782 BUG_ON(!ss->destroy);
2783 if (ss->subsys_id != i) {
2784 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
2785 ss->name, ss->subsys_id);
2790 cgroup_init_subsys(ss);
2796 * cgroup_init - cgroup initialization
2798 * Register cgroup filesystem and /proc file, and initialize
2799 * any subsystems that didn't request early init.
2801 int __init cgroup_init(void)
2805 struct hlist_head *hhead;
2807 err = bdi_init(&cgroup_backing_dev_info);
2811 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2812 struct cgroup_subsys *ss = subsys[i];
2813 if (!ss->early_init)
2814 cgroup_init_subsys(ss);
2816 cgroup_subsys_init_idr(ss);
2819 /* Add init_css_set to the hash table */
2820 hhead = css_set_hash(init_css_set.subsys);
2821 hlist_add_head(&init_css_set.hlist, hhead);
2823 err = register_filesystem(&cgroup_fs_type);
2827 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
2831 bdi_destroy(&cgroup_backing_dev_info);
2837 * proc_cgroup_show()
2838 * - Print task's cgroup paths into seq_file, one line for each hierarchy
2839 * - Used for /proc/<pid>/cgroup.
2840 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
2841 * doesn't really matter if tsk->cgroup changes after we read it,
2842 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
2843 * anyway. No need to check that tsk->cgroup != NULL, thanks to
2844 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
2845 * cgroup to top_cgroup.
2848 /* TODO: Use a proper seq_file iterator */
2849 static int proc_cgroup_show(struct seq_file *m, void *v)
2852 struct task_struct *tsk;
2855 struct cgroupfs_root *root;
2858 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2864 tsk = get_pid_task(pid, PIDTYPE_PID);
2870 mutex_lock(&cgroup_mutex);
2872 for_each_active_root(root) {
2873 struct cgroup_subsys *ss;
2874 struct cgroup *cgrp;
2878 seq_printf(m, "%lu:", root->subsys_bits);
2879 for_each_subsys(root, ss)
2880 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
2882 get_first_subsys(&root->top_cgroup, NULL, &subsys_id);
2883 cgrp = task_cgroup(tsk, subsys_id);
2884 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
2892 mutex_unlock(&cgroup_mutex);
2893 put_task_struct(tsk);
2900 static int cgroup_open(struct inode *inode, struct file *file)
2902 struct pid *pid = PROC_I(inode)->pid;
2903 return single_open(file, proc_cgroup_show, pid);
2906 struct file_operations proc_cgroup_operations = {
2907 .open = cgroup_open,
2909 .llseek = seq_lseek,
2910 .release = single_release,
2913 /* Display information about each subsystem and each hierarchy */
2914 static int proc_cgroupstats_show(struct seq_file *m, void *v)
2918 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
2919 mutex_lock(&cgroup_mutex);
2920 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2921 struct cgroup_subsys *ss = subsys[i];
2922 seq_printf(m, "%s\t%lu\t%d\t%d\n",
2923 ss->name, ss->root->subsys_bits,
2924 ss->root->number_of_cgroups, !ss->disabled);
2926 mutex_unlock(&cgroup_mutex);
2930 static int cgroupstats_open(struct inode *inode, struct file *file)
2932 return single_open(file, proc_cgroupstats_show, NULL);
2935 static struct file_operations proc_cgroupstats_operations = {
2936 .open = cgroupstats_open,
2938 .llseek = seq_lseek,
2939 .release = single_release,
2943 * cgroup_fork - attach newly forked task to its parents cgroup.
2944 * @child: pointer to task_struct of forking parent process.
2946 * Description: A task inherits its parent's cgroup at fork().
2948 * A pointer to the shared css_set was automatically copied in
2949 * fork.c by dup_task_struct(). However, we ignore that copy, since
2950 * it was not made under the protection of RCU or cgroup_mutex, so
2951 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
2952 * have already changed current->cgroups, allowing the previously
2953 * referenced cgroup group to be removed and freed.
2955 * At the point that cgroup_fork() is called, 'current' is the parent
2956 * task, and the passed argument 'child' points to the child task.
2958 void cgroup_fork(struct task_struct *child)
2961 child->cgroups = current->cgroups;
2962 get_css_set(child->cgroups);
2963 task_unlock(current);
2964 INIT_LIST_HEAD(&child->cg_list);
2968 * cgroup_fork_callbacks - run fork callbacks
2969 * @child: the new task
2971 * Called on a new task very soon before adding it to the
2972 * tasklist. No need to take any locks since no-one can
2973 * be operating on this task.
2975 void cgroup_fork_callbacks(struct task_struct *child)
2977 if (need_forkexit_callback) {
2979 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2980 struct cgroup_subsys *ss = subsys[i];
2982 ss->fork(ss, child);
2988 * cgroup_post_fork - called on a new task after adding it to the task list
2989 * @child: the task in question
2991 * Adds the task to the list running through its css_set if necessary.
2992 * Has to be after the task is visible on the task list in case we race
2993 * with the first call to cgroup_iter_start() - to guarantee that the
2994 * new task ends up on its list.
2996 void cgroup_post_fork(struct task_struct *child)
2998 if (use_task_css_set_links) {
2999 write_lock(&css_set_lock);
3001 if (list_empty(&child->cg_list))
3002 list_add(&child->cg_list, &child->cgroups->tasks);
3004 write_unlock(&css_set_lock);
3008 * cgroup_exit - detach cgroup from exiting task
3009 * @tsk: pointer to task_struct of exiting process
3010 * @run_callback: run exit callbacks?
3012 * Description: Detach cgroup from @tsk and release it.
3014 * Note that cgroups marked notify_on_release force every task in
3015 * them to take the global cgroup_mutex mutex when exiting.
3016 * This could impact scaling on very large systems. Be reluctant to
3017 * use notify_on_release cgroups where very high task exit scaling
3018 * is required on large systems.
3020 * the_top_cgroup_hack:
3022 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
3024 * We call cgroup_exit() while the task is still competent to
3025 * handle notify_on_release(), then leave the task attached to the
3026 * root cgroup in each hierarchy for the remainder of its exit.
3028 * To do this properly, we would increment the reference count on
3029 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
3030 * code we would add a second cgroup function call, to drop that
3031 * reference. This would just create an unnecessary hot spot on
3032 * the top_cgroup reference count, to no avail.
3034 * Normally, holding a reference to a cgroup without bumping its
3035 * count is unsafe. The cgroup could go away, or someone could
3036 * attach us to a different cgroup, decrementing the count on
3037 * the first cgroup that we never incremented. But in this case,
3038 * top_cgroup isn't going away, and either task has PF_EXITING set,
3039 * which wards off any cgroup_attach_task() attempts, or task is a failed
3040 * fork, never visible to cgroup_attach_task.
3042 void cgroup_exit(struct task_struct *tsk, int run_callbacks)
3047 if (run_callbacks && need_forkexit_callback) {
3048 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3049 struct cgroup_subsys *ss = subsys[i];
3056 * Unlink from the css_set task list if necessary.
3057 * Optimistically check cg_list before taking
3060 if (!list_empty(&tsk->cg_list)) {
3061 write_lock(&css_set_lock);
3062 if (!list_empty(&tsk->cg_list))
3063 list_del(&tsk->cg_list);
3064 write_unlock(&css_set_lock);
3067 /* Reassign the task to the init_css_set. */
3070 tsk->cgroups = &init_css_set;
3073 put_css_set_taskexit(cg);
3077 * cgroup_clone - clone the cgroup the given subsystem is attached to
3078 * @tsk: the task to be moved
3079 * @subsys: the given subsystem
3080 * @nodename: the name for the new cgroup
3082 * Duplicate the current cgroup in the hierarchy that the given
3083 * subsystem is attached to, and move this task into the new
3086 int cgroup_clone(struct task_struct *tsk, struct cgroup_subsys *subsys,
3089 struct dentry *dentry;
3091 struct cgroup *parent, *child;
3092 struct inode *inode;
3094 struct cgroupfs_root *root;
3095 struct cgroup_subsys *ss;
3097 /* We shouldn't be called by an unregistered subsystem */
3098 BUG_ON(!subsys->active);
3100 /* First figure out what hierarchy and cgroup we're dealing
3101 * with, and pin them so we can drop cgroup_mutex */
3102 mutex_lock(&cgroup_mutex);
3104 root = subsys->root;
3105 if (root == &rootnode) {
3106 mutex_unlock(&cgroup_mutex);
3110 /* Pin the hierarchy */
3111 if (!atomic_inc_not_zero(&root->sb->s_active)) {
3112 /* We race with the final deactivate_super() */
3113 mutex_unlock(&cgroup_mutex);
3117 /* Keep the cgroup alive */
3119 parent = task_cgroup(tsk, subsys->subsys_id);
3124 mutex_unlock(&cgroup_mutex);
3126 /* Now do the VFS work to create a cgroup */
3127 inode = parent->dentry->d_inode;
3129 /* Hold the parent directory mutex across this operation to
3130 * stop anyone else deleting the new cgroup */
3131 mutex_lock(&inode->i_mutex);
3132 dentry = lookup_one_len(nodename, parent->dentry, strlen(nodename));
3133 if (IS_ERR(dentry)) {
3135 "cgroup: Couldn't allocate dentry for %s: %ld\n", nodename,
3137 ret = PTR_ERR(dentry);
3141 /* Create the cgroup directory, which also creates the cgroup */
3142 ret = vfs_mkdir(inode, dentry, 0755);
3143 child = __d_cgrp(dentry);
3147 "Failed to create cgroup %s: %d\n", nodename,
3152 /* The cgroup now exists. Retake cgroup_mutex and check
3153 * that we're still in the same state that we thought we
3155 mutex_lock(&cgroup_mutex);
3156 if ((root != subsys->root) ||
3157 (parent != task_cgroup(tsk, subsys->subsys_id))) {
3158 /* Aargh, we raced ... */
3159 mutex_unlock(&inode->i_mutex);
3162 deactivate_super(root->sb);
3163 /* The cgroup is still accessible in the VFS, but
3164 * we're not going to try to rmdir() it at this
3167 "Race in cgroup_clone() - leaking cgroup %s\n",
3172 /* do any required auto-setup */
3173 for_each_subsys(root, ss) {
3175 ss->post_clone(ss, child);
3178 /* All seems fine. Finish by moving the task into the new cgroup */
3179 ret = cgroup_attach_task(child, tsk);
3180 mutex_unlock(&cgroup_mutex);
3183 mutex_unlock(&inode->i_mutex);
3185 mutex_lock(&cgroup_mutex);
3187 mutex_unlock(&cgroup_mutex);
3188 deactivate_super(root->sb);
3193 * cgroup_is_descendant - see if @cgrp is a descendant of @task's cgrp
3194 * @cgrp: the cgroup in question
3195 * @task: the task in question
3197 * See if @cgrp is a descendant of @task's cgroup in the appropriate
3200 * If we are sending in dummytop, then presumably we are creating
3201 * the top cgroup in the subsystem.
3203 * Called only by the ns (nsproxy) cgroup.
3205 int cgroup_is_descendant(const struct cgroup *cgrp, struct task_struct *task)
3208 struct cgroup *target;
3211 if (cgrp == dummytop)
3214 get_first_subsys(cgrp, NULL, &subsys_id);
3215 target = task_cgroup(task, subsys_id);
3216 while (cgrp != target && cgrp!= cgrp->top_cgroup)
3217 cgrp = cgrp->parent;
3218 ret = (cgrp == target);
3222 static void check_for_release(struct cgroup *cgrp)
3224 /* All of these checks rely on RCU to keep the cgroup
3225 * structure alive */
3226 if (cgroup_is_releasable(cgrp) && !atomic_read(&cgrp->count)
3227 && list_empty(&cgrp->children) && !cgroup_has_css_refs(cgrp)) {
3228 /* Control Group is currently removeable. If it's not
3229 * already queued for a userspace notification, queue
3231 int need_schedule_work = 0;
3232 spin_lock(&release_list_lock);
3233 if (!cgroup_is_removed(cgrp) &&
3234 list_empty(&cgrp->release_list)) {
3235 list_add(&cgrp->release_list, &release_list);
3236 need_schedule_work = 1;
3238 spin_unlock(&release_list_lock);
3239 if (need_schedule_work)
3240 schedule_work(&release_agent_work);
3244 void __css_put(struct cgroup_subsys_state *css)
3246 struct cgroup *cgrp = css->cgroup;
3248 if (atomic_dec_return(&css->refcnt) == 1) {
3249 if (notify_on_release(cgrp)) {
3250 set_bit(CGRP_RELEASABLE, &cgrp->flags);
3251 check_for_release(cgrp);
3253 cgroup_wakeup_rmdir_waiters(cgrp);
3259 * Notify userspace when a cgroup is released, by running the
3260 * configured release agent with the name of the cgroup (path
3261 * relative to the root of cgroup file system) as the argument.
3263 * Most likely, this user command will try to rmdir this cgroup.
3265 * This races with the possibility that some other task will be
3266 * attached to this cgroup before it is removed, or that some other
3267 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
3268 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
3269 * unused, and this cgroup will be reprieved from its death sentence,
3270 * to continue to serve a useful existence. Next time it's released,
3271 * we will get notified again, if it still has 'notify_on_release' set.
3273 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
3274 * means only wait until the task is successfully execve()'d. The
3275 * separate release agent task is forked by call_usermodehelper(),
3276 * then control in this thread returns here, without waiting for the
3277 * release agent task. We don't bother to wait because the caller of
3278 * this routine has no use for the exit status of the release agent
3279 * task, so no sense holding our caller up for that.
3281 static void cgroup_release_agent(struct work_struct *work)
3283 BUG_ON(work != &release_agent_work);
3284 mutex_lock(&cgroup_mutex);
3285 spin_lock(&release_list_lock);
3286 while (!list_empty(&release_list)) {
3287 char *argv[3], *envp[3];
3289 char *pathbuf = NULL, *agentbuf = NULL;
3290 struct cgroup *cgrp = list_entry(release_list.next,
3293 list_del_init(&cgrp->release_list);
3294 spin_unlock(&release_list_lock);
3295 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3298 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
3300 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
3305 argv[i++] = agentbuf;
3306 argv[i++] = pathbuf;
3310 /* minimal command environment */
3311 envp[i++] = "HOME=/";
3312 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
3315 /* Drop the lock while we invoke the usermode helper,
3316 * since the exec could involve hitting disk and hence
3317 * be a slow process */
3318 mutex_unlock(&cgroup_mutex);
3319 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
3320 mutex_lock(&cgroup_mutex);
3324 spin_lock(&release_list_lock);
3326 spin_unlock(&release_list_lock);
3327 mutex_unlock(&cgroup_mutex);
3330 static int __init cgroup_disable(char *str)
3335 while ((token = strsep(&str, ",")) != NULL) {
3339 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3340 struct cgroup_subsys *ss = subsys[i];
3342 if (!strcmp(token, ss->name)) {
3344 printk(KERN_INFO "Disabling %s control group"
3345 " subsystem\n", ss->name);
3352 __setup("cgroup_disable=", cgroup_disable);
3355 * Functons for CSS ID.
3359 *To get ID other than 0, this should be called when !cgroup_is_removed().
3361 unsigned short css_id(struct cgroup_subsys_state *css)
3363 struct css_id *cssid = rcu_dereference(css->id);
3370 unsigned short css_depth(struct cgroup_subsys_state *css)
3372 struct css_id *cssid = rcu_dereference(css->id);
3375 return cssid->depth;
3379 bool css_is_ancestor(struct cgroup_subsys_state *child,
3380 struct cgroup_subsys_state *root)
3382 struct css_id *child_id = rcu_dereference(child->id);
3383 struct css_id *root_id = rcu_dereference(root->id);
3385 if (!child_id || !root_id || (child_id->depth < root_id->depth))
3387 return child_id->stack[root_id->depth] == root_id->id;
3390 static void __free_css_id_cb(struct rcu_head *head)
3394 id = container_of(head, struct css_id, rcu_head);
3398 void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
3400 struct css_id *id = css->id;
3401 /* When this is called before css_id initialization, id can be NULL */
3405 BUG_ON(!ss->use_id);
3407 rcu_assign_pointer(id->css, NULL);
3408 rcu_assign_pointer(css->id, NULL);
3409 spin_lock(&ss->id_lock);
3410 idr_remove(&ss->idr, id->id);
3411 spin_unlock(&ss->id_lock);
3412 call_rcu(&id->rcu_head, __free_css_id_cb);
3416 * This is called by init or create(). Then, calls to this function are
3417 * always serialized (By cgroup_mutex() at create()).
3420 static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
3422 struct css_id *newid;
3423 int myid, error, size;
3425 BUG_ON(!ss->use_id);
3427 size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
3428 newid = kzalloc(size, GFP_KERNEL);
3430 return ERR_PTR(-ENOMEM);
3432 if (unlikely(!idr_pre_get(&ss->idr, GFP_KERNEL))) {
3436 spin_lock(&ss->id_lock);
3437 /* Don't use 0. allocates an ID of 1-65535 */
3438 error = idr_get_new_above(&ss->idr, newid, 1, &myid);
3439 spin_unlock(&ss->id_lock);
3441 /* Returns error when there are no free spaces for new ID.*/
3446 if (myid > CSS_ID_MAX)
3450 newid->depth = depth;
3454 spin_lock(&ss->id_lock);
3455 idr_remove(&ss->idr, myid);
3456 spin_unlock(&ss->id_lock);
3459 return ERR_PTR(error);
3463 static int __init cgroup_subsys_init_idr(struct cgroup_subsys *ss)
3465 struct css_id *newid;
3466 struct cgroup_subsys_state *rootcss;
3468 spin_lock_init(&ss->id_lock);
3471 rootcss = init_css_set.subsys[ss->subsys_id];
3472 newid = get_new_cssid(ss, 0);
3474 return PTR_ERR(newid);
3476 newid->stack[0] = newid->id;
3477 newid->css = rootcss;
3478 rootcss->id = newid;
3482 static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent,
3483 struct cgroup *child)
3485 int subsys_id, i, depth = 0;
3486 struct cgroup_subsys_state *parent_css, *child_css;
3487 struct css_id *child_id, *parent_id = NULL;
3489 subsys_id = ss->subsys_id;
3490 parent_css = parent->subsys[subsys_id];
3491 child_css = child->subsys[subsys_id];
3492 depth = css_depth(parent_css) + 1;
3493 parent_id = parent_css->id;
3495 child_id = get_new_cssid(ss, depth);
3496 if (IS_ERR(child_id))
3497 return PTR_ERR(child_id);
3499 for (i = 0; i < depth; i++)
3500 child_id->stack[i] = parent_id->stack[i];
3501 child_id->stack[depth] = child_id->id;
3503 * child_id->css pointer will be set after this cgroup is available
3504 * see cgroup_populate_dir()
3506 rcu_assign_pointer(child_css->id, child_id);
3512 * css_lookup - lookup css by id
3513 * @ss: cgroup subsys to be looked into.
3516 * Returns pointer to cgroup_subsys_state if there is valid one with id.
3517 * NULL if not. Should be called under rcu_read_lock()
3519 struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
3521 struct css_id *cssid = NULL;
3523 BUG_ON(!ss->use_id);
3524 cssid = idr_find(&ss->idr, id);
3526 if (unlikely(!cssid))
3529 return rcu_dereference(cssid->css);
3533 * css_get_next - lookup next cgroup under specified hierarchy.
3534 * @ss: pointer to subsystem
3535 * @id: current position of iteration.
3536 * @root: pointer to css. search tree under this.
3537 * @foundid: position of found object.
3539 * Search next css under the specified hierarchy of rootid. Calling under
3540 * rcu_read_lock() is necessary. Returns NULL if it reaches the end.
3542 struct cgroup_subsys_state *
3543 css_get_next(struct cgroup_subsys *ss, int id,
3544 struct cgroup_subsys_state *root, int *foundid)
3546 struct cgroup_subsys_state *ret = NULL;
3549 int rootid = css_id(root);
3550 int depth = css_depth(root);
3555 BUG_ON(!ss->use_id);
3556 /* fill start point for scan */
3560 * scan next entry from bitmap(tree), tmpid is updated after
3563 spin_lock(&ss->id_lock);
3564 tmp = idr_get_next(&ss->idr, &tmpid);
3565 spin_unlock(&ss->id_lock);
3569 if (tmp->depth >= depth && tmp->stack[depth] == rootid) {
3570 ret = rcu_dereference(tmp->css);
3576 /* continue to scan from next id */