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 mounted 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];
99 * The "rootnode" hierarchy is the "dummy hierarchy", reserved for the
100 * subsystems that are otherwise unattached - it never has more than a
101 * single cgroup, and all tasks are part of that cgroup.
103 static struct cgroupfs_root rootnode;
105 /* The list of hierarchy roots */
107 static LIST_HEAD(roots);
108 static int root_count;
110 /* dummytop is a shorthand for the dummy hierarchy's top cgroup */
111 #define dummytop (&rootnode.top_cgroup)
113 /* This flag indicates whether tasks in the fork and exit paths should
114 * check for fork/exit handlers to call. This avoids us having to do
115 * extra work in the fork/exit path if none of the subsystems need to
118 static int need_forkexit_callback __read_mostly;
120 /* convenient tests for these bits */
121 inline int cgroup_is_removed(const struct cgroup *cgrp)
123 return test_bit(CGRP_REMOVED, &cgrp->flags);
126 /* bits in struct cgroupfs_root flags field */
128 ROOT_NOPREFIX, /* mounted subsystems have no named prefix */
131 static int cgroup_is_releasable(const struct cgroup *cgrp)
134 (1 << CGRP_RELEASABLE) |
135 (1 << CGRP_NOTIFY_ON_RELEASE);
136 return (cgrp->flags & bits) == bits;
139 static int notify_on_release(const struct cgroup *cgrp)
141 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
145 * for_each_subsys() allows you to iterate on each subsystem attached to
146 * an active hierarchy
148 #define for_each_subsys(_root, _ss) \
149 list_for_each_entry(_ss, &_root->subsys_list, sibling)
151 /* for_each_root() allows you to iterate across the active hierarchies */
152 #define for_each_root(_root) \
153 list_for_each_entry(_root, &roots, root_list)
155 /* the list of cgroups eligible for automatic release. Protected by
156 * release_list_lock */
157 static LIST_HEAD(release_list);
158 static DEFINE_SPINLOCK(release_list_lock);
159 static void cgroup_release_agent(struct work_struct *work);
160 static DECLARE_WORK(release_agent_work, cgroup_release_agent);
161 static void check_for_release(struct cgroup *cgrp);
163 /* Link structure for associating css_set objects with cgroups */
164 struct cg_cgroup_link {
166 * List running through cg_cgroup_links associated with a
167 * cgroup, anchored on cgroup->css_sets
169 struct list_head cgrp_link_list;
171 * List running through cg_cgroup_links pointing at a
172 * single css_set object, anchored on css_set->cg_links
174 struct list_head cg_link_list;
178 /* The default css_set - used by init and its children prior to any
179 * hierarchies being mounted. It contains a pointer to the root state
180 * for each subsystem. Also used to anchor the list of css_sets. Not
181 * reference-counted, to improve performance when child cgroups
182 * haven't been created.
185 static struct css_set init_css_set;
186 static struct cg_cgroup_link init_css_set_link;
188 /* css_set_lock protects the list of css_set objects, and the
189 * chain of tasks off each css_set. Nests outside task->alloc_lock
190 * due to cgroup_iter_start() */
191 static DEFINE_RWLOCK(css_set_lock);
192 static int css_set_count;
194 /* hash table for cgroup groups. This improves the performance to
195 * find an existing css_set */
196 #define CSS_SET_HASH_BITS 7
197 #define CSS_SET_TABLE_SIZE (1 << CSS_SET_HASH_BITS)
198 static struct hlist_head css_set_table[CSS_SET_TABLE_SIZE];
200 static struct hlist_head *css_set_hash(struct cgroup_subsys_state *css[])
204 unsigned long tmp = 0UL;
206 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++)
207 tmp += (unsigned long)css[i];
208 tmp = (tmp >> 16) ^ tmp;
210 index = hash_long(tmp, CSS_SET_HASH_BITS);
212 return &css_set_table[index];
215 /* We don't maintain the lists running through each css_set to its
216 * task until after the first call to cgroup_iter_start(). This
217 * reduces the fork()/exit() overhead for people who have cgroups
218 * compiled into their kernel but not actually in use */
219 static int use_task_css_set_links __read_mostly;
221 /* When we create or destroy a css_set, the operation simply
222 * takes/releases a reference count on all the cgroups referenced
223 * by subsystems in this css_set. This can end up multiple-counting
224 * some cgroups, but that's OK - the ref-count is just a
225 * busy/not-busy indicator; ensuring that we only count each cgroup
226 * once would require taking a global lock to ensure that no
227 * subsystems moved between hierarchies while we were doing so.
229 * Possible TODO: decide at boot time based on the number of
230 * registered subsystems and the number of CPUs or NUMA nodes whether
231 * it's better for performance to ref-count every subsystem, or to
232 * take a global lock and only add one ref count to each hierarchy.
236 * unlink a css_set from the list and free it
238 static void unlink_css_set(struct css_set *cg)
240 struct cg_cgroup_link *link;
241 struct cg_cgroup_link *saved_link;
243 hlist_del(&cg->hlist);
246 list_for_each_entry_safe(link, saved_link, &cg->cg_links,
248 list_del(&link->cg_link_list);
249 list_del(&link->cgrp_link_list);
254 static void __put_css_set(struct css_set *cg, int taskexit)
258 * Ensure that the refcount doesn't hit zero while any readers
259 * can see it. Similar to atomic_dec_and_lock(), but for an
262 if (atomic_add_unless(&cg->refcount, -1, 1))
264 write_lock(&css_set_lock);
265 if (!atomic_dec_and_test(&cg->refcount)) {
266 write_unlock(&css_set_lock);
270 write_unlock(&css_set_lock);
273 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
274 struct cgroup *cgrp = cg->subsys[i]->cgroup;
275 if (atomic_dec_and_test(&cgrp->count) &&
276 notify_on_release(cgrp)) {
278 set_bit(CGRP_RELEASABLE, &cgrp->flags);
279 check_for_release(cgrp);
287 * refcounted get/put for css_set objects
289 static inline void get_css_set(struct css_set *cg)
291 atomic_inc(&cg->refcount);
294 static inline void put_css_set(struct css_set *cg)
296 __put_css_set(cg, 0);
299 static inline void put_css_set_taskexit(struct css_set *cg)
301 __put_css_set(cg, 1);
305 * find_existing_css_set() is a helper for
306 * find_css_set(), and checks to see whether an existing
307 * css_set is suitable.
309 * oldcg: the cgroup group that we're using before the cgroup
312 * cgrp: the cgroup that we're moving into
314 * template: location in which to build the desired set of subsystem
315 * state objects for the new cgroup group
317 static struct css_set *find_existing_css_set(
318 struct css_set *oldcg,
320 struct cgroup_subsys_state *template[])
323 struct cgroupfs_root *root = cgrp->root;
324 struct hlist_head *hhead;
325 struct hlist_node *node;
328 /* Built the set of subsystem state objects that we want to
329 * see in the new css_set */
330 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
331 if (root->subsys_bits & (1UL << i)) {
332 /* Subsystem is in this hierarchy. So we want
333 * the subsystem state from the new
335 template[i] = cgrp->subsys[i];
337 /* Subsystem is not in this hierarchy, so we
338 * don't want to change the subsystem state */
339 template[i] = oldcg->subsys[i];
343 hhead = css_set_hash(template);
344 hlist_for_each_entry(cg, node, hhead, hlist) {
345 if (!memcmp(template, cg->subsys, sizeof(cg->subsys))) {
346 /* All subsystems matched */
351 /* No existing cgroup group matched */
355 static void free_cg_links(struct list_head *tmp)
357 struct cg_cgroup_link *link;
358 struct cg_cgroup_link *saved_link;
360 list_for_each_entry_safe(link, saved_link, tmp, cgrp_link_list) {
361 list_del(&link->cgrp_link_list);
367 * allocate_cg_links() allocates "count" cg_cgroup_link structures
368 * and chains them on tmp through their cgrp_link_list fields. Returns 0 on
369 * success or a negative error
371 static int allocate_cg_links(int count, struct list_head *tmp)
373 struct cg_cgroup_link *link;
376 for (i = 0; i < count; i++) {
377 link = kmalloc(sizeof(*link), GFP_KERNEL);
382 list_add(&link->cgrp_link_list, tmp);
388 * find_css_set() takes an existing cgroup group and a
389 * cgroup object, and returns a css_set object that's
390 * equivalent to the old group, but with the given cgroup
391 * substituted into the appropriate hierarchy. Must be called with
394 static struct css_set *find_css_set(
395 struct css_set *oldcg, struct cgroup *cgrp)
398 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT];
401 struct list_head tmp_cg_links;
402 struct cg_cgroup_link *link;
404 struct hlist_head *hhead;
406 /* First see if we already have a cgroup group that matches
408 read_lock(&css_set_lock);
409 res = find_existing_css_set(oldcg, cgrp, template);
412 read_unlock(&css_set_lock);
417 res = kmalloc(sizeof(*res), GFP_KERNEL);
421 /* Allocate all the cg_cgroup_link objects that we'll need */
422 if (allocate_cg_links(root_count, &tmp_cg_links) < 0) {
427 atomic_set(&res->refcount, 1);
428 INIT_LIST_HEAD(&res->cg_links);
429 INIT_LIST_HEAD(&res->tasks);
430 INIT_HLIST_NODE(&res->hlist);
432 /* Copy the set of subsystem state objects generated in
433 * find_existing_css_set() */
434 memcpy(res->subsys, template, sizeof(res->subsys));
436 write_lock(&css_set_lock);
437 /* Add reference counts and links from the new css_set. */
438 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
439 struct cgroup *cgrp = res->subsys[i]->cgroup;
440 struct cgroup_subsys *ss = subsys[i];
441 atomic_inc(&cgrp->count);
443 * We want to add a link once per cgroup, so we
444 * only do it for the first subsystem in each
447 if (ss->root->subsys_list.next == &ss->sibling) {
448 BUG_ON(list_empty(&tmp_cg_links));
449 link = list_entry(tmp_cg_links.next,
450 struct cg_cgroup_link,
452 list_del(&link->cgrp_link_list);
453 list_add(&link->cgrp_link_list, &cgrp->css_sets);
455 list_add(&link->cg_link_list, &res->cg_links);
458 if (list_empty(&rootnode.subsys_list)) {
459 link = list_entry(tmp_cg_links.next,
460 struct cg_cgroup_link,
462 list_del(&link->cgrp_link_list);
463 list_add(&link->cgrp_link_list, &dummytop->css_sets);
465 list_add(&link->cg_link_list, &res->cg_links);
468 BUG_ON(!list_empty(&tmp_cg_links));
472 /* Add this cgroup group to the hash table */
473 hhead = css_set_hash(res->subsys);
474 hlist_add_head(&res->hlist, hhead);
476 write_unlock(&css_set_lock);
482 * There is one global cgroup mutex. We also require taking
483 * task_lock() when dereferencing a task's cgroup subsys pointers.
484 * See "The task_lock() exception", at the end of this comment.
486 * A task must hold cgroup_mutex to modify cgroups.
488 * Any task can increment and decrement the count field without lock.
489 * So in general, code holding cgroup_mutex can't rely on the count
490 * field not changing. However, if the count goes to zero, then only
491 * cgroup_attach_task() can increment it again. Because a count of zero
492 * means that no tasks are currently attached, therefore there is no
493 * way a task attached to that cgroup can fork (the other way to
494 * increment the count). So code holding cgroup_mutex can safely
495 * assume that if the count is zero, it will stay zero. Similarly, if
496 * a task holds cgroup_mutex on a cgroup with zero count, it
497 * knows that the cgroup won't be removed, as cgroup_rmdir()
500 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
501 * (usually) take cgroup_mutex. These are the two most performance
502 * critical pieces of code here. The exception occurs on cgroup_exit(),
503 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
504 * is taken, and if the cgroup count is zero, a usermode call made
505 * to the release agent with the name of the cgroup (path relative to
506 * the root of cgroup file system) as the argument.
508 * A cgroup can only be deleted if both its 'count' of using tasks
509 * is zero, and its list of 'children' cgroups is empty. Since all
510 * tasks in the system use _some_ cgroup, and since there is always at
511 * least one task in the system (init, pid == 1), therefore, top_cgroup
512 * always has either children cgroups and/or using tasks. So we don't
513 * need a special hack to ensure that top_cgroup cannot be deleted.
515 * The task_lock() exception
517 * The need for this exception arises from the action of
518 * cgroup_attach_task(), which overwrites one tasks cgroup pointer with
519 * another. It does so using cgroup_mutex, however there are
520 * several performance critical places that need to reference
521 * task->cgroup without the expense of grabbing a system global
522 * mutex. Therefore except as noted below, when dereferencing or, as
523 * in cgroup_attach_task(), modifying a task'ss cgroup pointer we use
524 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
525 * the task_struct routinely used for such matters.
527 * P.S. One more locking exception. RCU is used to guard the
528 * update of a tasks cgroup pointer by cgroup_attach_task()
532 * cgroup_lock - lock out any changes to cgroup structures
535 void cgroup_lock(void)
537 mutex_lock(&cgroup_mutex);
541 * cgroup_unlock - release lock on cgroup changes
543 * Undo the lock taken in a previous cgroup_lock() call.
545 void cgroup_unlock(void)
547 mutex_unlock(&cgroup_mutex);
551 * A couple of forward declarations required, due to cyclic reference loop:
552 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
553 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
557 static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode);
558 static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
559 static int cgroup_populate_dir(struct cgroup *cgrp);
560 static struct inode_operations cgroup_dir_inode_operations;
561 static struct file_operations proc_cgroupstats_operations;
563 static struct backing_dev_info cgroup_backing_dev_info = {
564 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
567 static struct inode *cgroup_new_inode(mode_t mode, struct super_block *sb)
569 struct inode *inode = new_inode(sb);
572 inode->i_mode = mode;
573 inode->i_uid = current_fsuid();
574 inode->i_gid = current_fsgid();
575 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
576 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
582 * Call subsys's pre_destroy handler.
583 * This is called before css refcnt check.
585 static void cgroup_call_pre_destroy(struct cgroup *cgrp)
587 struct cgroup_subsys *ss;
588 for_each_subsys(cgrp->root, ss)
590 ss->pre_destroy(ss, cgrp);
594 static void cgroup_diput(struct dentry *dentry, struct inode *inode)
596 /* is dentry a directory ? if so, kfree() associated cgroup */
597 if (S_ISDIR(inode->i_mode)) {
598 struct cgroup *cgrp = dentry->d_fsdata;
599 struct cgroup_subsys *ss;
600 BUG_ON(!(cgroup_is_removed(cgrp)));
601 /* It's possible for external users to be holding css
602 * reference counts on a cgroup; css_put() needs to
603 * be able to access the cgroup after decrementing
604 * the reference count in order to know if it needs to
605 * queue the cgroup to be handled by the release
609 mutex_lock(&cgroup_mutex);
611 * Release the subsystem state objects.
613 for_each_subsys(cgrp->root, ss)
614 ss->destroy(ss, cgrp);
616 cgrp->root->number_of_cgroups--;
617 mutex_unlock(&cgroup_mutex);
619 /* Drop the active superblock reference that we took when we
620 * created the cgroup */
621 deactivate_super(cgrp->root->sb);
628 static void remove_dir(struct dentry *d)
630 struct dentry *parent = dget(d->d_parent);
633 simple_rmdir(parent->d_inode, d);
637 static void cgroup_clear_directory(struct dentry *dentry)
639 struct list_head *node;
641 BUG_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
642 spin_lock(&dcache_lock);
643 node = dentry->d_subdirs.next;
644 while (node != &dentry->d_subdirs) {
645 struct dentry *d = list_entry(node, struct dentry, d_u.d_child);
648 /* This should never be called on a cgroup
649 * directory with child cgroups */
650 BUG_ON(d->d_inode->i_mode & S_IFDIR);
652 spin_unlock(&dcache_lock);
654 simple_unlink(dentry->d_inode, d);
656 spin_lock(&dcache_lock);
658 node = dentry->d_subdirs.next;
660 spin_unlock(&dcache_lock);
664 * NOTE : the dentry must have been dget()'ed
666 static void cgroup_d_remove_dir(struct dentry *dentry)
668 cgroup_clear_directory(dentry);
670 spin_lock(&dcache_lock);
671 list_del_init(&dentry->d_u.d_child);
672 spin_unlock(&dcache_lock);
676 static int rebind_subsystems(struct cgroupfs_root *root,
677 unsigned long final_bits)
679 unsigned long added_bits, removed_bits;
680 struct cgroup *cgrp = &root->top_cgroup;
683 removed_bits = root->actual_subsys_bits & ~final_bits;
684 added_bits = final_bits & ~root->actual_subsys_bits;
685 /* Check that any added subsystems are currently free */
686 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
687 unsigned long bit = 1UL << i;
688 struct cgroup_subsys *ss = subsys[i];
689 if (!(bit & added_bits))
691 if (ss->root != &rootnode) {
692 /* Subsystem isn't free */
697 /* Currently we don't handle adding/removing subsystems when
698 * any child cgroups exist. This is theoretically supportable
699 * but involves complex error handling, so it's being left until
701 if (root->number_of_cgroups > 1)
704 /* Process each subsystem */
705 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
706 struct cgroup_subsys *ss = subsys[i];
707 unsigned long bit = 1UL << i;
708 if (bit & added_bits) {
709 /* We're binding this subsystem to this hierarchy */
710 BUG_ON(cgrp->subsys[i]);
711 BUG_ON(!dummytop->subsys[i]);
712 BUG_ON(dummytop->subsys[i]->cgroup != dummytop);
713 cgrp->subsys[i] = dummytop->subsys[i];
714 cgrp->subsys[i]->cgroup = cgrp;
715 list_add(&ss->sibling, &root->subsys_list);
720 } else if (bit & removed_bits) {
721 /* We're removing this subsystem */
722 BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]);
723 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
725 ss->bind(ss, dummytop);
726 dummytop->subsys[i]->cgroup = dummytop;
727 cgrp->subsys[i] = NULL;
728 subsys[i]->root = &rootnode;
729 list_del(&ss->sibling);
730 } else if (bit & final_bits) {
731 /* Subsystem state should already exist */
732 BUG_ON(!cgrp->subsys[i]);
734 /* Subsystem state shouldn't exist */
735 BUG_ON(cgrp->subsys[i]);
738 root->subsys_bits = root->actual_subsys_bits = final_bits;
744 static int cgroup_show_options(struct seq_file *seq, struct vfsmount *vfs)
746 struct cgroupfs_root *root = vfs->mnt_sb->s_fs_info;
747 struct cgroup_subsys *ss;
749 mutex_lock(&cgroup_mutex);
750 for_each_subsys(root, ss)
751 seq_printf(seq, ",%s", ss->name);
752 if (test_bit(ROOT_NOPREFIX, &root->flags))
753 seq_puts(seq, ",noprefix");
754 if (strlen(root->release_agent_path))
755 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
756 mutex_unlock(&cgroup_mutex);
760 struct cgroup_sb_opts {
761 unsigned long subsys_bits;
766 /* Convert a hierarchy specifier into a bitmask of subsystems and
768 static int parse_cgroupfs_options(char *data,
769 struct cgroup_sb_opts *opts)
771 char *token, *o = data ?: "all";
773 opts->subsys_bits = 0;
775 opts->release_agent = NULL;
777 while ((token = strsep(&o, ",")) != NULL) {
780 if (!strcmp(token, "all")) {
781 /* Add all non-disabled subsystems */
783 opts->subsys_bits = 0;
784 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
785 struct cgroup_subsys *ss = subsys[i];
787 opts->subsys_bits |= 1ul << i;
789 } else if (!strcmp(token, "noprefix")) {
790 set_bit(ROOT_NOPREFIX, &opts->flags);
791 } else if (!strncmp(token, "release_agent=", 14)) {
792 /* Specifying two release agents is forbidden */
793 if (opts->release_agent)
795 opts->release_agent = kzalloc(PATH_MAX, GFP_KERNEL);
796 if (!opts->release_agent)
798 strncpy(opts->release_agent, token + 14, PATH_MAX - 1);
799 opts->release_agent[PATH_MAX - 1] = 0;
801 struct cgroup_subsys *ss;
803 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
805 if (!strcmp(token, ss->name)) {
807 set_bit(i, &opts->subsys_bits);
811 if (i == CGROUP_SUBSYS_COUNT)
816 /* We can't have an empty hierarchy */
817 if (!opts->subsys_bits)
823 static int cgroup_remount(struct super_block *sb, int *flags, char *data)
826 struct cgroupfs_root *root = sb->s_fs_info;
827 struct cgroup *cgrp = &root->top_cgroup;
828 struct cgroup_sb_opts opts;
830 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
831 mutex_lock(&cgroup_mutex);
833 /* See what subsystems are wanted */
834 ret = parse_cgroupfs_options(data, &opts);
838 /* Don't allow flags to change at remount */
839 if (opts.flags != root->flags) {
844 ret = rebind_subsystems(root, opts.subsys_bits);
846 /* (re)populate subsystem files */
848 cgroup_populate_dir(cgrp);
850 if (opts.release_agent)
851 strcpy(root->release_agent_path, opts.release_agent);
853 if (opts.release_agent)
854 kfree(opts.release_agent);
855 mutex_unlock(&cgroup_mutex);
856 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
860 static struct super_operations cgroup_ops = {
861 .statfs = simple_statfs,
862 .drop_inode = generic_delete_inode,
863 .show_options = cgroup_show_options,
864 .remount_fs = cgroup_remount,
867 static void init_cgroup_housekeeping(struct cgroup *cgrp)
869 INIT_LIST_HEAD(&cgrp->sibling);
870 INIT_LIST_HEAD(&cgrp->children);
871 INIT_LIST_HEAD(&cgrp->css_sets);
872 INIT_LIST_HEAD(&cgrp->release_list);
873 init_rwsem(&cgrp->pids_mutex);
875 static void init_cgroup_root(struct cgroupfs_root *root)
877 struct cgroup *cgrp = &root->top_cgroup;
878 INIT_LIST_HEAD(&root->subsys_list);
879 INIT_LIST_HEAD(&root->root_list);
880 root->number_of_cgroups = 1;
882 cgrp->top_cgroup = cgrp;
883 init_cgroup_housekeeping(cgrp);
886 static int cgroup_test_super(struct super_block *sb, void *data)
888 struct cgroupfs_root *new = data;
889 struct cgroupfs_root *root = sb->s_fs_info;
891 /* First check subsystems */
892 if (new->subsys_bits != root->subsys_bits)
895 /* Next check flags */
896 if (new->flags != root->flags)
902 static int cgroup_set_super(struct super_block *sb, void *data)
905 struct cgroupfs_root *root = data;
907 ret = set_anon_super(sb, NULL);
911 sb->s_fs_info = root;
914 sb->s_blocksize = PAGE_CACHE_SIZE;
915 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
916 sb->s_magic = CGROUP_SUPER_MAGIC;
917 sb->s_op = &cgroup_ops;
922 static int cgroup_get_rootdir(struct super_block *sb)
924 struct inode *inode =
925 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
926 struct dentry *dentry;
931 inode->i_fop = &simple_dir_operations;
932 inode->i_op = &cgroup_dir_inode_operations;
933 /* directories start off with i_nlink == 2 (for "." entry) */
935 dentry = d_alloc_root(inode);
944 static int cgroup_get_sb(struct file_system_type *fs_type,
945 int flags, const char *unused_dev_name,
946 void *data, struct vfsmount *mnt)
948 struct cgroup_sb_opts opts;
950 struct super_block *sb;
951 struct cgroupfs_root *root;
952 struct list_head tmp_cg_links;
954 /* First find the desired set of subsystems */
955 ret = parse_cgroupfs_options(data, &opts);
957 if (opts.release_agent)
958 kfree(opts.release_agent);
962 root = kzalloc(sizeof(*root), GFP_KERNEL);
964 if (opts.release_agent)
965 kfree(opts.release_agent);
969 init_cgroup_root(root);
970 root->subsys_bits = opts.subsys_bits;
971 root->flags = opts.flags;
972 if (opts.release_agent) {
973 strcpy(root->release_agent_path, opts.release_agent);
974 kfree(opts.release_agent);
977 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, root);
984 if (sb->s_fs_info != root) {
985 /* Reusing an existing superblock */
986 BUG_ON(sb->s_root == NULL);
991 struct cgroup *cgrp = &root->top_cgroup;
995 BUG_ON(sb->s_root != NULL);
997 ret = cgroup_get_rootdir(sb);
1000 inode = sb->s_root->d_inode;
1002 mutex_lock(&inode->i_mutex);
1003 mutex_lock(&cgroup_mutex);
1006 * We're accessing css_set_count without locking
1007 * css_set_lock here, but that's OK - it can only be
1008 * increased by someone holding cgroup_lock, and
1009 * that's us. The worst that can happen is that we
1010 * have some link structures left over
1012 ret = allocate_cg_links(css_set_count, &tmp_cg_links);
1014 mutex_unlock(&cgroup_mutex);
1015 mutex_unlock(&inode->i_mutex);
1016 goto drop_new_super;
1019 ret = rebind_subsystems(root, root->subsys_bits);
1020 if (ret == -EBUSY) {
1021 mutex_unlock(&cgroup_mutex);
1022 mutex_unlock(&inode->i_mutex);
1026 /* EBUSY should be the only error here */
1029 list_add(&root->root_list, &roots);
1032 sb->s_root->d_fsdata = &root->top_cgroup;
1033 root->top_cgroup.dentry = sb->s_root;
1035 /* Link the top cgroup in this hierarchy into all
1036 * the css_set objects */
1037 write_lock(&css_set_lock);
1038 for (i = 0; i < CSS_SET_TABLE_SIZE; i++) {
1039 struct hlist_head *hhead = &css_set_table[i];
1040 struct hlist_node *node;
1043 hlist_for_each_entry(cg, node, hhead, hlist) {
1044 struct cg_cgroup_link *link;
1046 BUG_ON(list_empty(&tmp_cg_links));
1047 link = list_entry(tmp_cg_links.next,
1048 struct cg_cgroup_link,
1050 list_del(&link->cgrp_link_list);
1052 list_add(&link->cgrp_link_list,
1053 &root->top_cgroup.css_sets);
1054 list_add(&link->cg_link_list, &cg->cg_links);
1057 write_unlock(&css_set_lock);
1059 free_cg_links(&tmp_cg_links);
1061 BUG_ON(!list_empty(&cgrp->sibling));
1062 BUG_ON(!list_empty(&cgrp->children));
1063 BUG_ON(root->number_of_cgroups != 1);
1065 cgroup_populate_dir(cgrp);
1066 mutex_unlock(&inode->i_mutex);
1067 mutex_unlock(&cgroup_mutex);
1070 return simple_set_mnt(mnt, sb);
1073 free_cg_links(&tmp_cg_links);
1075 up_write(&sb->s_umount);
1076 deactivate_super(sb);
1080 static void cgroup_kill_sb(struct super_block *sb) {
1081 struct cgroupfs_root *root = sb->s_fs_info;
1082 struct cgroup *cgrp = &root->top_cgroup;
1084 struct cg_cgroup_link *link;
1085 struct cg_cgroup_link *saved_link;
1089 BUG_ON(root->number_of_cgroups != 1);
1090 BUG_ON(!list_empty(&cgrp->children));
1091 BUG_ON(!list_empty(&cgrp->sibling));
1093 mutex_lock(&cgroup_mutex);
1095 /* Rebind all subsystems back to the default hierarchy */
1096 ret = rebind_subsystems(root, 0);
1097 /* Shouldn't be able to fail ... */
1101 * Release all the links from css_sets to this hierarchy's
1104 write_lock(&css_set_lock);
1106 list_for_each_entry_safe(link, saved_link, &cgrp->css_sets,
1108 list_del(&link->cg_link_list);
1109 list_del(&link->cgrp_link_list);
1112 write_unlock(&css_set_lock);
1114 if (!list_empty(&root->root_list)) {
1115 list_del(&root->root_list);
1118 mutex_unlock(&cgroup_mutex);
1121 kill_litter_super(sb);
1124 static struct file_system_type cgroup_fs_type = {
1126 .get_sb = cgroup_get_sb,
1127 .kill_sb = cgroup_kill_sb,
1130 static inline struct cgroup *__d_cgrp(struct dentry *dentry)
1132 return dentry->d_fsdata;
1135 static inline struct cftype *__d_cft(struct dentry *dentry)
1137 return dentry->d_fsdata;
1141 * cgroup_path - generate the path of a cgroup
1142 * @cgrp: the cgroup in question
1143 * @buf: the buffer to write the path into
1144 * @buflen: the length of the buffer
1146 * Called with cgroup_mutex held. Writes path of cgroup into buf.
1147 * Returns 0 on success, -errno on error.
1149 int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
1153 if (cgrp == dummytop) {
1155 * Inactive subsystems have no dentry for their root
1162 start = buf + buflen;
1166 int len = cgrp->dentry->d_name.len;
1167 if ((start -= len) < buf)
1168 return -ENAMETOOLONG;
1169 memcpy(start, cgrp->dentry->d_name.name, len);
1170 cgrp = cgrp->parent;
1176 return -ENAMETOOLONG;
1179 memmove(buf, start, buf + buflen - start);
1184 * Return the first subsystem attached to a cgroup's hierarchy, and
1188 static void get_first_subsys(const struct cgroup *cgrp,
1189 struct cgroup_subsys_state **css, int *subsys_id)
1191 const struct cgroupfs_root *root = cgrp->root;
1192 const struct cgroup_subsys *test_ss;
1193 BUG_ON(list_empty(&root->subsys_list));
1194 test_ss = list_entry(root->subsys_list.next,
1195 struct cgroup_subsys, sibling);
1197 *css = cgrp->subsys[test_ss->subsys_id];
1201 *subsys_id = test_ss->subsys_id;
1205 * cgroup_attach_task - attach task 'tsk' to cgroup 'cgrp'
1206 * @cgrp: the cgroup the task is attaching to
1207 * @tsk: the task to be attached
1209 * Call holding cgroup_mutex. May take task_lock of
1210 * the task 'tsk' during call.
1212 int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
1215 struct cgroup_subsys *ss;
1216 struct cgroup *oldcgrp;
1218 struct css_set *newcg;
1219 struct cgroupfs_root *root = cgrp->root;
1222 get_first_subsys(cgrp, NULL, &subsys_id);
1224 /* Nothing to do if the task is already in that cgroup */
1225 oldcgrp = task_cgroup(tsk, subsys_id);
1226 if (cgrp == oldcgrp)
1229 for_each_subsys(root, ss) {
1230 if (ss->can_attach) {
1231 retval = ss->can_attach(ss, cgrp, tsk);
1242 * Locate or allocate a new css_set for this task,
1243 * based on its final set of cgroups
1245 newcg = find_css_set(cg, cgrp);
1251 if (tsk->flags & PF_EXITING) {
1256 rcu_assign_pointer(tsk->cgroups, newcg);
1259 /* Update the css_set linked lists if we're using them */
1260 write_lock(&css_set_lock);
1261 if (!list_empty(&tsk->cg_list)) {
1262 list_del(&tsk->cg_list);
1263 list_add(&tsk->cg_list, &newcg->tasks);
1265 write_unlock(&css_set_lock);
1267 for_each_subsys(root, ss) {
1269 ss->attach(ss, cgrp, oldcgrp, tsk);
1271 set_bit(CGRP_RELEASABLE, &oldcgrp->flags);
1278 * Attach task with pid 'pid' to cgroup 'cgrp'. Call with cgroup_mutex
1279 * held. May take task_lock of task
1281 static int attach_task_by_pid(struct cgroup *cgrp, u64 pid)
1283 struct task_struct *tsk;
1284 const struct cred *cred = current_cred(), *tcred;
1289 tsk = find_task_by_vpid(pid);
1290 if (!tsk || tsk->flags & PF_EXITING) {
1295 tcred = __task_cred(tsk);
1297 cred->euid != tcred->uid &&
1298 cred->euid != tcred->suid) {
1302 get_task_struct(tsk);
1306 get_task_struct(tsk);
1309 ret = cgroup_attach_task(cgrp, tsk);
1310 put_task_struct(tsk);
1314 static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid)
1317 if (!cgroup_lock_live_group(cgrp))
1319 ret = attach_task_by_pid(cgrp, pid);
1324 /* The various types of files and directories in a cgroup file system */
1325 enum cgroup_filetype {
1329 FILE_NOTIFY_ON_RELEASE,
1334 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
1335 * @cgrp: the cgroup to be checked for liveness
1337 * On success, returns true; the lock should be later released with
1338 * cgroup_unlock(). On failure returns false with no lock held.
1340 bool cgroup_lock_live_group(struct cgroup *cgrp)
1342 mutex_lock(&cgroup_mutex);
1343 if (cgroup_is_removed(cgrp)) {
1344 mutex_unlock(&cgroup_mutex);
1350 static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
1353 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
1354 if (!cgroup_lock_live_group(cgrp))
1356 strcpy(cgrp->root->release_agent_path, buffer);
1361 static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
1362 struct seq_file *seq)
1364 if (!cgroup_lock_live_group(cgrp))
1366 seq_puts(seq, cgrp->root->release_agent_path);
1367 seq_putc(seq, '\n');
1372 /* A buffer size big enough for numbers or short strings */
1373 #define CGROUP_LOCAL_BUFFER_SIZE 64
1375 static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
1377 const char __user *userbuf,
1378 size_t nbytes, loff_t *unused_ppos)
1380 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
1386 if (nbytes >= sizeof(buffer))
1388 if (copy_from_user(buffer, userbuf, nbytes))
1391 buffer[nbytes] = 0; /* nul-terminate */
1393 if (cft->write_u64) {
1394 u64 val = simple_strtoull(buffer, &end, 0);
1397 retval = cft->write_u64(cgrp, cft, val);
1399 s64 val = simple_strtoll(buffer, &end, 0);
1402 retval = cft->write_s64(cgrp, cft, val);
1409 static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
1411 const char __user *userbuf,
1412 size_t nbytes, loff_t *unused_ppos)
1414 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
1416 size_t max_bytes = cft->max_write_len;
1417 char *buffer = local_buffer;
1420 max_bytes = sizeof(local_buffer) - 1;
1421 if (nbytes >= max_bytes)
1423 /* Allocate a dynamic buffer if we need one */
1424 if (nbytes >= sizeof(local_buffer)) {
1425 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
1429 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
1434 buffer[nbytes] = 0; /* nul-terminate */
1436 retval = cft->write_string(cgrp, cft, buffer);
1440 if (buffer != local_buffer)
1445 static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
1446 size_t nbytes, loff_t *ppos)
1448 struct cftype *cft = __d_cft(file->f_dentry);
1449 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
1451 if (cgroup_is_removed(cgrp))
1454 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
1455 if (cft->write_u64 || cft->write_s64)
1456 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
1457 if (cft->write_string)
1458 return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
1460 int ret = cft->trigger(cgrp, (unsigned int)cft->private);
1461 return ret ? ret : nbytes;
1466 static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
1468 char __user *buf, size_t nbytes,
1471 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
1472 u64 val = cft->read_u64(cgrp, cft);
1473 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
1475 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
1478 static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
1480 char __user *buf, size_t nbytes,
1483 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
1484 s64 val = cft->read_s64(cgrp, cft);
1485 int len = sprintf(tmp, "%lld\n", (long long) val);
1487 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
1490 static ssize_t cgroup_file_read(struct file *file, char __user *buf,
1491 size_t nbytes, loff_t *ppos)
1493 struct cftype *cft = __d_cft(file->f_dentry);
1494 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
1496 if (cgroup_is_removed(cgrp))
1500 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
1502 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
1504 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
1509 * seqfile ops/methods for returning structured data. Currently just
1510 * supports string->u64 maps, but can be extended in future.
1513 struct cgroup_seqfile_state {
1515 struct cgroup *cgroup;
1518 static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
1520 struct seq_file *sf = cb->state;
1521 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
1524 static int cgroup_seqfile_show(struct seq_file *m, void *arg)
1526 struct cgroup_seqfile_state *state = m->private;
1527 struct cftype *cft = state->cft;
1528 if (cft->read_map) {
1529 struct cgroup_map_cb cb = {
1530 .fill = cgroup_map_add,
1533 return cft->read_map(state->cgroup, cft, &cb);
1535 return cft->read_seq_string(state->cgroup, cft, m);
1538 static int cgroup_seqfile_release(struct inode *inode, struct file *file)
1540 struct seq_file *seq = file->private_data;
1541 kfree(seq->private);
1542 return single_release(inode, file);
1545 static struct file_operations cgroup_seqfile_operations = {
1547 .write = cgroup_file_write,
1548 .llseek = seq_lseek,
1549 .release = cgroup_seqfile_release,
1552 static int cgroup_file_open(struct inode *inode, struct file *file)
1557 err = generic_file_open(inode, file);
1560 cft = __d_cft(file->f_dentry);
1562 if (cft->read_map || cft->read_seq_string) {
1563 struct cgroup_seqfile_state *state =
1564 kzalloc(sizeof(*state), GFP_USER);
1568 state->cgroup = __d_cgrp(file->f_dentry->d_parent);
1569 file->f_op = &cgroup_seqfile_operations;
1570 err = single_open(file, cgroup_seqfile_show, state);
1573 } else if (cft->open)
1574 err = cft->open(inode, file);
1581 static int cgroup_file_release(struct inode *inode, struct file *file)
1583 struct cftype *cft = __d_cft(file->f_dentry);
1585 return cft->release(inode, file);
1590 * cgroup_rename - Only allow simple rename of directories in place.
1592 static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
1593 struct inode *new_dir, struct dentry *new_dentry)
1595 if (!S_ISDIR(old_dentry->d_inode->i_mode))
1597 if (new_dentry->d_inode)
1599 if (old_dir != new_dir)
1601 return simple_rename(old_dir, old_dentry, new_dir, new_dentry);
1604 static struct file_operations cgroup_file_operations = {
1605 .read = cgroup_file_read,
1606 .write = cgroup_file_write,
1607 .llseek = generic_file_llseek,
1608 .open = cgroup_file_open,
1609 .release = cgroup_file_release,
1612 static struct inode_operations cgroup_dir_inode_operations = {
1613 .lookup = simple_lookup,
1614 .mkdir = cgroup_mkdir,
1615 .rmdir = cgroup_rmdir,
1616 .rename = cgroup_rename,
1619 static int cgroup_create_file(struct dentry *dentry, int mode,
1620 struct super_block *sb)
1622 static struct dentry_operations cgroup_dops = {
1623 .d_iput = cgroup_diput,
1626 struct inode *inode;
1630 if (dentry->d_inode)
1633 inode = cgroup_new_inode(mode, sb);
1637 if (S_ISDIR(mode)) {
1638 inode->i_op = &cgroup_dir_inode_operations;
1639 inode->i_fop = &simple_dir_operations;
1641 /* start off with i_nlink == 2 (for "." entry) */
1644 /* start with the directory inode held, so that we can
1645 * populate it without racing with another mkdir */
1646 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
1647 } else if (S_ISREG(mode)) {
1649 inode->i_fop = &cgroup_file_operations;
1651 dentry->d_op = &cgroup_dops;
1652 d_instantiate(dentry, inode);
1653 dget(dentry); /* Extra count - pin the dentry in core */
1658 * cgroup_create_dir - create a directory for an object.
1659 * @cgrp: the cgroup we create the directory for. It must have a valid
1660 * ->parent field. And we are going to fill its ->dentry field.
1661 * @dentry: dentry of the new cgroup
1662 * @mode: mode to set on new directory.
1664 static int cgroup_create_dir(struct cgroup *cgrp, struct dentry *dentry,
1667 struct dentry *parent;
1670 parent = cgrp->parent->dentry;
1671 error = cgroup_create_file(dentry, S_IFDIR | mode, cgrp->root->sb);
1673 dentry->d_fsdata = cgrp;
1674 inc_nlink(parent->d_inode);
1675 cgrp->dentry = dentry;
1683 int cgroup_add_file(struct cgroup *cgrp,
1684 struct cgroup_subsys *subsys,
1685 const struct cftype *cft)
1687 struct dentry *dir = cgrp->dentry;
1688 struct dentry *dentry;
1691 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
1692 if (subsys && !test_bit(ROOT_NOPREFIX, &cgrp->root->flags)) {
1693 strcpy(name, subsys->name);
1696 strcat(name, cft->name);
1697 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
1698 dentry = lookup_one_len(name, dir, strlen(name));
1699 if (!IS_ERR(dentry)) {
1700 error = cgroup_create_file(dentry, 0644 | S_IFREG,
1703 dentry->d_fsdata = (void *)cft;
1706 error = PTR_ERR(dentry);
1710 int cgroup_add_files(struct cgroup *cgrp,
1711 struct cgroup_subsys *subsys,
1712 const struct cftype cft[],
1716 for (i = 0; i < count; i++) {
1717 err = cgroup_add_file(cgrp, subsys, &cft[i]);
1725 * cgroup_task_count - count the number of tasks in a cgroup.
1726 * @cgrp: the cgroup in question
1728 * Return the number of tasks in the cgroup.
1730 int cgroup_task_count(const struct cgroup *cgrp)
1733 struct cg_cgroup_link *link;
1735 read_lock(&css_set_lock);
1736 list_for_each_entry(link, &cgrp->css_sets, cgrp_link_list) {
1737 count += atomic_read(&link->cg->refcount);
1739 read_unlock(&css_set_lock);
1744 * Advance a list_head iterator. The iterator should be positioned at
1745 * the start of a css_set
1747 static void cgroup_advance_iter(struct cgroup *cgrp,
1748 struct cgroup_iter *it)
1750 struct list_head *l = it->cg_link;
1751 struct cg_cgroup_link *link;
1754 /* Advance to the next non-empty css_set */
1757 if (l == &cgrp->css_sets) {
1761 link = list_entry(l, struct cg_cgroup_link, cgrp_link_list);
1763 } while (list_empty(&cg->tasks));
1765 it->task = cg->tasks.next;
1769 * To reduce the fork() overhead for systems that are not actually
1770 * using their cgroups capability, we don't maintain the lists running
1771 * through each css_set to its tasks until we see the list actually
1772 * used - in other words after the first call to cgroup_iter_start().
1774 * The tasklist_lock is not held here, as do_each_thread() and
1775 * while_each_thread() are protected by RCU.
1777 static void cgroup_enable_task_cg_lists(void)
1779 struct task_struct *p, *g;
1780 write_lock(&css_set_lock);
1781 use_task_css_set_links = 1;
1782 do_each_thread(g, p) {
1785 * We should check if the process is exiting, otherwise
1786 * it will race with cgroup_exit() in that the list
1787 * entry won't be deleted though the process has exited.
1789 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
1790 list_add(&p->cg_list, &p->cgroups->tasks);
1792 } while_each_thread(g, p);
1793 write_unlock(&css_set_lock);
1796 void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
1799 * The first time anyone tries to iterate across a cgroup,
1800 * we need to enable the list linking each css_set to its
1801 * tasks, and fix up all existing tasks.
1803 if (!use_task_css_set_links)
1804 cgroup_enable_task_cg_lists();
1806 read_lock(&css_set_lock);
1807 it->cg_link = &cgrp->css_sets;
1808 cgroup_advance_iter(cgrp, it);
1811 struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
1812 struct cgroup_iter *it)
1814 struct task_struct *res;
1815 struct list_head *l = it->task;
1816 struct cg_cgroup_link *link;
1818 /* If the iterator cg is NULL, we have no tasks */
1821 res = list_entry(l, struct task_struct, cg_list);
1822 /* Advance iterator to find next entry */
1824 link = list_entry(it->cg_link, struct cg_cgroup_link, cgrp_link_list);
1825 if (l == &link->cg->tasks) {
1826 /* We reached the end of this task list - move on to
1827 * the next cg_cgroup_link */
1828 cgroup_advance_iter(cgrp, it);
1835 void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
1837 read_unlock(&css_set_lock);
1840 static inline int started_after_time(struct task_struct *t1,
1841 struct timespec *time,
1842 struct task_struct *t2)
1844 int start_diff = timespec_compare(&t1->start_time, time);
1845 if (start_diff > 0) {
1847 } else if (start_diff < 0) {
1851 * Arbitrarily, if two processes started at the same
1852 * time, we'll say that the lower pointer value
1853 * started first. Note that t2 may have exited by now
1854 * so this may not be a valid pointer any longer, but
1855 * that's fine - it still serves to distinguish
1856 * between two tasks started (effectively) simultaneously.
1863 * This function is a callback from heap_insert() and is used to order
1865 * In this case we order the heap in descending task start time.
1867 static inline int started_after(void *p1, void *p2)
1869 struct task_struct *t1 = p1;
1870 struct task_struct *t2 = p2;
1871 return started_after_time(t1, &t2->start_time, t2);
1875 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
1876 * @scan: struct cgroup_scanner containing arguments for the scan
1878 * Arguments include pointers to callback functions test_task() and
1880 * Iterate through all the tasks in a cgroup, calling test_task() for each,
1881 * and if it returns true, call process_task() for it also.
1882 * The test_task pointer may be NULL, meaning always true (select all tasks).
1883 * Effectively duplicates cgroup_iter_{start,next,end}()
1884 * but does not lock css_set_lock for the call to process_task().
1885 * The struct cgroup_scanner may be embedded in any structure of the caller's
1887 * It is guaranteed that process_task() will act on every task that
1888 * is a member of the cgroup for the duration of this call. This
1889 * function may or may not call process_task() for tasks that exit
1890 * or move to a different cgroup during the call, or are forked or
1891 * move into the cgroup during the call.
1893 * Note that test_task() may be called with locks held, and may in some
1894 * situations be called multiple times for the same task, so it should
1896 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
1897 * pre-allocated and will be used for heap operations (and its "gt" member will
1898 * be overwritten), else a temporary heap will be used (allocation of which
1899 * may cause this function to fail).
1901 int cgroup_scan_tasks(struct cgroup_scanner *scan)
1904 struct cgroup_iter it;
1905 struct task_struct *p, *dropped;
1906 /* Never dereference latest_task, since it's not refcounted */
1907 struct task_struct *latest_task = NULL;
1908 struct ptr_heap tmp_heap;
1909 struct ptr_heap *heap;
1910 struct timespec latest_time = { 0, 0 };
1913 /* The caller supplied our heap and pre-allocated its memory */
1915 heap->gt = &started_after;
1917 /* We need to allocate our own heap memory */
1919 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
1921 /* cannot allocate the heap */
1927 * Scan tasks in the cgroup, using the scanner's "test_task" callback
1928 * to determine which are of interest, and using the scanner's
1929 * "process_task" callback to process any of them that need an update.
1930 * Since we don't want to hold any locks during the task updates,
1931 * gather tasks to be processed in a heap structure.
1932 * The heap is sorted by descending task start time.
1933 * If the statically-sized heap fills up, we overflow tasks that
1934 * started later, and in future iterations only consider tasks that
1935 * started after the latest task in the previous pass. This
1936 * guarantees forward progress and that we don't miss any tasks.
1939 cgroup_iter_start(scan->cg, &it);
1940 while ((p = cgroup_iter_next(scan->cg, &it))) {
1942 * Only affect tasks that qualify per the caller's callback,
1943 * if he provided one
1945 if (scan->test_task && !scan->test_task(p, scan))
1948 * Only process tasks that started after the last task
1951 if (!started_after_time(p, &latest_time, latest_task))
1953 dropped = heap_insert(heap, p);
1954 if (dropped == NULL) {
1956 * The new task was inserted; the heap wasn't
1960 } else if (dropped != p) {
1962 * The new task was inserted, and pushed out a
1966 put_task_struct(dropped);
1969 * Else the new task was newer than anything already in
1970 * the heap and wasn't inserted
1973 cgroup_iter_end(scan->cg, &it);
1976 for (i = 0; i < heap->size; i++) {
1977 struct task_struct *q = heap->ptrs[i];
1979 latest_time = q->start_time;
1982 /* Process the task per the caller's callback */
1983 scan->process_task(q, scan);
1987 * If we had to process any tasks at all, scan again
1988 * in case some of them were in the middle of forking
1989 * children that didn't get processed.
1990 * Not the most efficient way to do it, but it avoids
1991 * having to take callback_mutex in the fork path
1995 if (heap == &tmp_heap)
1996 heap_free(&tmp_heap);
2001 * Stuff for reading the 'tasks' file.
2003 * Reading this file can return large amounts of data if a cgroup has
2004 * *lots* of attached tasks. So it may need several calls to read(),
2005 * but we cannot guarantee that the information we produce is correct
2006 * unless we produce it entirely atomically.
2011 * Load into 'pidarray' up to 'npids' of the tasks using cgroup
2012 * 'cgrp'. Return actual number of pids loaded. No need to
2013 * task_lock(p) when reading out p->cgroup, since we're in an RCU
2014 * read section, so the css_set can't go away, and is
2015 * immutable after creation.
2017 static int pid_array_load(pid_t *pidarray, int npids, struct cgroup *cgrp)
2020 struct cgroup_iter it;
2021 struct task_struct *tsk;
2022 cgroup_iter_start(cgrp, &it);
2023 while ((tsk = cgroup_iter_next(cgrp, &it))) {
2024 if (unlikely(n == npids))
2026 pidarray[n++] = task_pid_vnr(tsk);
2028 cgroup_iter_end(cgrp, &it);
2033 * cgroupstats_build - build and fill cgroupstats
2034 * @stats: cgroupstats to fill information into
2035 * @dentry: A dentry entry belonging to the cgroup for which stats have
2038 * Build and fill cgroupstats so that taskstats can export it to user
2041 int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
2044 struct cgroup *cgrp;
2045 struct cgroup_iter it;
2046 struct task_struct *tsk;
2049 * Validate dentry by checking the superblock operations,
2050 * and make sure it's a directory.
2052 if (dentry->d_sb->s_op != &cgroup_ops ||
2053 !S_ISDIR(dentry->d_inode->i_mode))
2057 cgrp = dentry->d_fsdata;
2059 cgroup_iter_start(cgrp, &it);
2060 while ((tsk = cgroup_iter_next(cgrp, &it))) {
2061 switch (tsk->state) {
2063 stats->nr_running++;
2065 case TASK_INTERRUPTIBLE:
2066 stats->nr_sleeping++;
2068 case TASK_UNINTERRUPTIBLE:
2069 stats->nr_uninterruptible++;
2072 stats->nr_stopped++;
2075 if (delayacct_is_task_waiting_on_io(tsk))
2076 stats->nr_io_wait++;
2080 cgroup_iter_end(cgrp, &it);
2086 static int cmppid(const void *a, const void *b)
2088 return *(pid_t *)a - *(pid_t *)b;
2093 * seq_file methods for the "tasks" file. The seq_file position is the
2094 * next pid to display; the seq_file iterator is a pointer to the pid
2095 * in the cgroup->tasks_pids array.
2098 static void *cgroup_tasks_start(struct seq_file *s, loff_t *pos)
2101 * Initially we receive a position value that corresponds to
2102 * one more than the last pid shown (or 0 on the first call or
2103 * after a seek to the start). Use a binary-search to find the
2104 * next pid to display, if any
2106 struct cgroup *cgrp = s->private;
2107 int index = 0, pid = *pos;
2110 down_read(&cgrp->pids_mutex);
2112 int end = cgrp->pids_length;
2114 while (index < end) {
2115 int mid = (index + end) / 2;
2116 if (cgrp->tasks_pids[mid] == pid) {
2119 } else if (cgrp->tasks_pids[mid] <= pid)
2125 /* If we're off the end of the array, we're done */
2126 if (index >= cgrp->pids_length)
2128 /* Update the abstract position to be the actual pid that we found */
2129 iter = cgrp->tasks_pids + index;
2134 static void cgroup_tasks_stop(struct seq_file *s, void *v)
2136 struct cgroup *cgrp = s->private;
2137 up_read(&cgrp->pids_mutex);
2140 static void *cgroup_tasks_next(struct seq_file *s, void *v, loff_t *pos)
2142 struct cgroup *cgrp = s->private;
2144 int *end = cgrp->tasks_pids + cgrp->pids_length;
2147 * Advance to the next pid in the array. If this goes off the
2159 static int cgroup_tasks_show(struct seq_file *s, void *v)
2161 return seq_printf(s, "%d\n", *(int *)v);
2164 static struct seq_operations cgroup_tasks_seq_operations = {
2165 .start = cgroup_tasks_start,
2166 .stop = cgroup_tasks_stop,
2167 .next = cgroup_tasks_next,
2168 .show = cgroup_tasks_show,
2171 static void release_cgroup_pid_array(struct cgroup *cgrp)
2173 down_write(&cgrp->pids_mutex);
2174 BUG_ON(!cgrp->pids_use_count);
2175 if (!--cgrp->pids_use_count) {
2176 kfree(cgrp->tasks_pids);
2177 cgrp->tasks_pids = NULL;
2178 cgrp->pids_length = 0;
2180 up_write(&cgrp->pids_mutex);
2183 static int cgroup_tasks_release(struct inode *inode, struct file *file)
2185 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
2187 if (!(file->f_mode & FMODE_READ))
2190 release_cgroup_pid_array(cgrp);
2191 return seq_release(inode, file);
2194 static struct file_operations cgroup_tasks_operations = {
2196 .llseek = seq_lseek,
2197 .write = cgroup_file_write,
2198 .release = cgroup_tasks_release,
2202 * Handle an open on 'tasks' file. Prepare an array containing the
2203 * process id's of tasks currently attached to the cgroup being opened.
2206 static int cgroup_tasks_open(struct inode *unused, struct file *file)
2208 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
2213 /* Nothing to do for write-only files */
2214 if (!(file->f_mode & FMODE_READ))
2218 * If cgroup gets more users after we read count, we won't have
2219 * enough space - tough. This race is indistinguishable to the
2220 * caller from the case that the additional cgroup users didn't
2221 * show up until sometime later on.
2223 npids = cgroup_task_count(cgrp);
2224 pidarray = kmalloc(npids * sizeof(pid_t), GFP_KERNEL);
2227 npids = pid_array_load(pidarray, npids, cgrp);
2228 sort(pidarray, npids, sizeof(pid_t), cmppid, NULL);
2231 * Store the array in the cgroup, freeing the old
2232 * array if necessary
2234 down_write(&cgrp->pids_mutex);
2235 kfree(cgrp->tasks_pids);
2236 cgrp->tasks_pids = pidarray;
2237 cgrp->pids_length = npids;
2238 cgrp->pids_use_count++;
2239 up_write(&cgrp->pids_mutex);
2241 file->f_op = &cgroup_tasks_operations;
2243 retval = seq_open(file, &cgroup_tasks_seq_operations);
2245 release_cgroup_pid_array(cgrp);
2248 ((struct seq_file *)file->private_data)->private = cgrp;
2252 static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
2255 return notify_on_release(cgrp);
2258 static int cgroup_write_notify_on_release(struct cgroup *cgrp,
2262 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
2264 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
2266 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
2271 * for the common functions, 'private' gives the type of file
2273 static struct cftype files[] = {
2276 .open = cgroup_tasks_open,
2277 .write_u64 = cgroup_tasks_write,
2278 .release = cgroup_tasks_release,
2279 .private = FILE_TASKLIST,
2283 .name = "notify_on_release",
2284 .read_u64 = cgroup_read_notify_on_release,
2285 .write_u64 = cgroup_write_notify_on_release,
2286 .private = FILE_NOTIFY_ON_RELEASE,
2290 static struct cftype cft_release_agent = {
2291 .name = "release_agent",
2292 .read_seq_string = cgroup_release_agent_show,
2293 .write_string = cgroup_release_agent_write,
2294 .max_write_len = PATH_MAX,
2295 .private = FILE_RELEASE_AGENT,
2298 static int cgroup_populate_dir(struct cgroup *cgrp)
2301 struct cgroup_subsys *ss;
2303 /* First clear out any existing files */
2304 cgroup_clear_directory(cgrp->dentry);
2306 err = cgroup_add_files(cgrp, NULL, files, ARRAY_SIZE(files));
2310 if (cgrp == cgrp->top_cgroup) {
2311 if ((err = cgroup_add_file(cgrp, NULL, &cft_release_agent)) < 0)
2315 for_each_subsys(cgrp->root, ss) {
2316 if (ss->populate && (err = ss->populate(ss, cgrp)) < 0)
2323 static void init_cgroup_css(struct cgroup_subsys_state *css,
2324 struct cgroup_subsys *ss,
2325 struct cgroup *cgrp)
2328 atomic_set(&css->refcnt, 0);
2330 if (cgrp == dummytop)
2331 set_bit(CSS_ROOT, &css->flags);
2332 BUG_ON(cgrp->subsys[ss->subsys_id]);
2333 cgrp->subsys[ss->subsys_id] = css;
2337 * cgroup_create - create a cgroup
2338 * @parent: cgroup that will be parent of the new cgroup
2339 * @dentry: dentry of the new cgroup
2340 * @mode: mode to set on new inode
2342 * Must be called with the mutex on the parent inode held
2344 static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
2347 struct cgroup *cgrp;
2348 struct cgroupfs_root *root = parent->root;
2350 struct cgroup_subsys *ss;
2351 struct super_block *sb = root->sb;
2353 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
2357 /* Grab a reference on the superblock so the hierarchy doesn't
2358 * get deleted on unmount if there are child cgroups. This
2359 * can be done outside cgroup_mutex, since the sb can't
2360 * disappear while someone has an open control file on the
2362 atomic_inc(&sb->s_active);
2364 mutex_lock(&cgroup_mutex);
2366 init_cgroup_housekeeping(cgrp);
2368 cgrp->parent = parent;
2369 cgrp->root = parent->root;
2370 cgrp->top_cgroup = parent->top_cgroup;
2372 if (notify_on_release(parent))
2373 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
2375 for_each_subsys(root, ss) {
2376 struct cgroup_subsys_state *css = ss->create(ss, cgrp);
2381 init_cgroup_css(css, ss, cgrp);
2384 list_add(&cgrp->sibling, &cgrp->parent->children);
2385 root->number_of_cgroups++;
2387 err = cgroup_create_dir(cgrp, dentry, mode);
2391 /* The cgroup directory was pre-locked for us */
2392 BUG_ON(!mutex_is_locked(&cgrp->dentry->d_inode->i_mutex));
2394 err = cgroup_populate_dir(cgrp);
2395 /* If err < 0, we have a half-filled directory - oh well ;) */
2397 mutex_unlock(&cgroup_mutex);
2398 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
2404 list_del(&cgrp->sibling);
2405 root->number_of_cgroups--;
2409 for_each_subsys(root, ss) {
2410 if (cgrp->subsys[ss->subsys_id])
2411 ss->destroy(ss, cgrp);
2414 mutex_unlock(&cgroup_mutex);
2416 /* Release the reference count that we took on the superblock */
2417 deactivate_super(sb);
2423 static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2425 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
2427 /* the vfs holds inode->i_mutex already */
2428 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
2431 static int cgroup_has_css_refs(struct cgroup *cgrp)
2433 /* Check the reference count on each subsystem. Since we
2434 * already established that there are no tasks in the
2435 * cgroup, if the css refcount is also 0, then there should
2436 * be no outstanding references, so the subsystem is safe to
2437 * destroy. We scan across all subsystems rather than using
2438 * the per-hierarchy linked list of mounted subsystems since
2439 * we can be called via check_for_release() with no
2440 * synchronization other than RCU, and the subsystem linked
2441 * list isn't RCU-safe */
2443 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2444 struct cgroup_subsys *ss = subsys[i];
2445 struct cgroup_subsys_state *css;
2446 /* Skip subsystems not in this hierarchy */
2447 if (ss->root != cgrp->root)
2449 css = cgrp->subsys[ss->subsys_id];
2450 /* When called from check_for_release() it's possible
2451 * that by this point the cgroup has been removed
2452 * and the css deleted. But a false-positive doesn't
2453 * matter, since it can only happen if the cgroup
2454 * has been deleted and hence no longer needs the
2455 * release agent to be called anyway. */
2456 if (css && atomic_read(&css->refcnt))
2462 static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
2464 struct cgroup *cgrp = dentry->d_fsdata;
2466 struct cgroup *parent;
2468 /* the vfs holds both inode->i_mutex already */
2470 mutex_lock(&cgroup_mutex);
2471 if (atomic_read(&cgrp->count) != 0) {
2472 mutex_unlock(&cgroup_mutex);
2475 if (!list_empty(&cgrp->children)) {
2476 mutex_unlock(&cgroup_mutex);
2479 mutex_unlock(&cgroup_mutex);
2482 * Call pre_destroy handlers of subsys. Notify subsystems
2483 * that rmdir() request comes.
2485 cgroup_call_pre_destroy(cgrp);
2487 mutex_lock(&cgroup_mutex);
2488 parent = cgrp->parent;
2490 if (atomic_read(&cgrp->count)
2491 || !list_empty(&cgrp->children)
2492 || cgroup_has_css_refs(cgrp)) {
2493 mutex_unlock(&cgroup_mutex);
2497 spin_lock(&release_list_lock);
2498 set_bit(CGRP_REMOVED, &cgrp->flags);
2499 if (!list_empty(&cgrp->release_list))
2500 list_del(&cgrp->release_list);
2501 spin_unlock(&release_list_lock);
2502 /* delete my sibling from parent->children */
2503 list_del(&cgrp->sibling);
2504 spin_lock(&cgrp->dentry->d_lock);
2505 d = dget(cgrp->dentry);
2506 spin_unlock(&d->d_lock);
2508 cgroup_d_remove_dir(d);
2511 set_bit(CGRP_RELEASABLE, &parent->flags);
2512 check_for_release(parent);
2514 mutex_unlock(&cgroup_mutex);
2518 static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
2520 struct cgroup_subsys_state *css;
2522 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
2524 /* Create the top cgroup state for this subsystem */
2525 ss->root = &rootnode;
2526 css = ss->create(ss, dummytop);
2527 /* We don't handle early failures gracefully */
2528 BUG_ON(IS_ERR(css));
2529 init_cgroup_css(css, ss, dummytop);
2531 /* Update the init_css_set to contain a subsys
2532 * pointer to this state - since the subsystem is
2533 * newly registered, all tasks and hence the
2534 * init_css_set is in the subsystem's top cgroup. */
2535 init_css_set.subsys[ss->subsys_id] = dummytop->subsys[ss->subsys_id];
2537 need_forkexit_callback |= ss->fork || ss->exit;
2539 /* At system boot, before all subsystems have been
2540 * registered, no tasks have been forked, so we don't
2541 * need to invoke fork callbacks here. */
2542 BUG_ON(!list_empty(&init_task.tasks));
2548 * cgroup_init_early - cgroup initialization at system boot
2550 * Initialize cgroups at system boot, and initialize any
2551 * subsystems that request early init.
2553 int __init cgroup_init_early(void)
2556 atomic_set(&init_css_set.refcount, 1);
2557 INIT_LIST_HEAD(&init_css_set.cg_links);
2558 INIT_LIST_HEAD(&init_css_set.tasks);
2559 INIT_HLIST_NODE(&init_css_set.hlist);
2561 init_cgroup_root(&rootnode);
2562 list_add(&rootnode.root_list, &roots);
2564 init_task.cgroups = &init_css_set;
2566 init_css_set_link.cg = &init_css_set;
2567 list_add(&init_css_set_link.cgrp_link_list,
2568 &rootnode.top_cgroup.css_sets);
2569 list_add(&init_css_set_link.cg_link_list,
2570 &init_css_set.cg_links);
2572 for (i = 0; i < CSS_SET_TABLE_SIZE; i++)
2573 INIT_HLIST_HEAD(&css_set_table[i]);
2575 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2576 struct cgroup_subsys *ss = subsys[i];
2579 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
2580 BUG_ON(!ss->create);
2581 BUG_ON(!ss->destroy);
2582 if (ss->subsys_id != i) {
2583 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
2584 ss->name, ss->subsys_id);
2589 cgroup_init_subsys(ss);
2595 * cgroup_init - cgroup initialization
2597 * Register cgroup filesystem and /proc file, and initialize
2598 * any subsystems that didn't request early init.
2600 int __init cgroup_init(void)
2604 struct hlist_head *hhead;
2606 err = bdi_init(&cgroup_backing_dev_info);
2610 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2611 struct cgroup_subsys *ss = subsys[i];
2612 if (!ss->early_init)
2613 cgroup_init_subsys(ss);
2616 /* Add init_css_set to the hash table */
2617 hhead = css_set_hash(init_css_set.subsys);
2618 hlist_add_head(&init_css_set.hlist, hhead);
2620 err = register_filesystem(&cgroup_fs_type);
2624 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
2628 bdi_destroy(&cgroup_backing_dev_info);
2634 * proc_cgroup_show()
2635 * - Print task's cgroup paths into seq_file, one line for each hierarchy
2636 * - Used for /proc/<pid>/cgroup.
2637 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
2638 * doesn't really matter if tsk->cgroup changes after we read it,
2639 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
2640 * anyway. No need to check that tsk->cgroup != NULL, thanks to
2641 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
2642 * cgroup to top_cgroup.
2645 /* TODO: Use a proper seq_file iterator */
2646 static int proc_cgroup_show(struct seq_file *m, void *v)
2649 struct task_struct *tsk;
2652 struct cgroupfs_root *root;
2655 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2661 tsk = get_pid_task(pid, PIDTYPE_PID);
2667 mutex_lock(&cgroup_mutex);
2669 for_each_root(root) {
2670 struct cgroup_subsys *ss;
2671 struct cgroup *cgrp;
2675 /* Skip this hierarchy if it has no active subsystems */
2676 if (!root->actual_subsys_bits)
2678 seq_printf(m, "%lu:", root->subsys_bits);
2679 for_each_subsys(root, ss)
2680 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
2682 get_first_subsys(&root->top_cgroup, NULL, &subsys_id);
2683 cgrp = task_cgroup(tsk, subsys_id);
2684 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
2692 mutex_unlock(&cgroup_mutex);
2693 put_task_struct(tsk);
2700 static int cgroup_open(struct inode *inode, struct file *file)
2702 struct pid *pid = PROC_I(inode)->pid;
2703 return single_open(file, proc_cgroup_show, pid);
2706 struct file_operations proc_cgroup_operations = {
2707 .open = cgroup_open,
2709 .llseek = seq_lseek,
2710 .release = single_release,
2713 /* Display information about each subsystem and each hierarchy */
2714 static int proc_cgroupstats_show(struct seq_file *m, void *v)
2718 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
2719 mutex_lock(&cgroup_mutex);
2720 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2721 struct cgroup_subsys *ss = subsys[i];
2722 seq_printf(m, "%s\t%lu\t%d\t%d\n",
2723 ss->name, ss->root->subsys_bits,
2724 ss->root->number_of_cgroups, !ss->disabled);
2726 mutex_unlock(&cgroup_mutex);
2730 static int cgroupstats_open(struct inode *inode, struct file *file)
2732 return single_open(file, proc_cgroupstats_show, NULL);
2735 static struct file_operations proc_cgroupstats_operations = {
2736 .open = cgroupstats_open,
2738 .llseek = seq_lseek,
2739 .release = single_release,
2743 * cgroup_fork - attach newly forked task to its parents cgroup.
2744 * @child: pointer to task_struct of forking parent process.
2746 * Description: A task inherits its parent's cgroup at fork().
2748 * A pointer to the shared css_set was automatically copied in
2749 * fork.c by dup_task_struct(). However, we ignore that copy, since
2750 * it was not made under the protection of RCU or cgroup_mutex, so
2751 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
2752 * have already changed current->cgroups, allowing the previously
2753 * referenced cgroup group to be removed and freed.
2755 * At the point that cgroup_fork() is called, 'current' is the parent
2756 * task, and the passed argument 'child' points to the child task.
2758 void cgroup_fork(struct task_struct *child)
2761 child->cgroups = current->cgroups;
2762 get_css_set(child->cgroups);
2763 task_unlock(current);
2764 INIT_LIST_HEAD(&child->cg_list);
2768 * cgroup_fork_callbacks - run fork callbacks
2769 * @child: the new task
2771 * Called on a new task very soon before adding it to the
2772 * tasklist. No need to take any locks since no-one can
2773 * be operating on this task.
2775 void cgroup_fork_callbacks(struct task_struct *child)
2777 if (need_forkexit_callback) {
2779 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2780 struct cgroup_subsys *ss = subsys[i];
2782 ss->fork(ss, child);
2788 * cgroup_post_fork - called on a new task after adding it to the task list
2789 * @child: the task in question
2791 * Adds the task to the list running through its css_set if necessary.
2792 * Has to be after the task is visible on the task list in case we race
2793 * with the first call to cgroup_iter_start() - to guarantee that the
2794 * new task ends up on its list.
2796 void cgroup_post_fork(struct task_struct *child)
2798 if (use_task_css_set_links) {
2799 write_lock(&css_set_lock);
2801 if (list_empty(&child->cg_list))
2802 list_add(&child->cg_list, &child->cgroups->tasks);
2804 write_unlock(&css_set_lock);
2808 * cgroup_exit - detach cgroup from exiting task
2809 * @tsk: pointer to task_struct of exiting process
2810 * @run_callback: run exit callbacks?
2812 * Description: Detach cgroup from @tsk and release it.
2814 * Note that cgroups marked notify_on_release force every task in
2815 * them to take the global cgroup_mutex mutex when exiting.
2816 * This could impact scaling on very large systems. Be reluctant to
2817 * use notify_on_release cgroups where very high task exit scaling
2818 * is required on large systems.
2820 * the_top_cgroup_hack:
2822 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
2824 * We call cgroup_exit() while the task is still competent to
2825 * handle notify_on_release(), then leave the task attached to the
2826 * root cgroup in each hierarchy for the remainder of its exit.
2828 * To do this properly, we would increment the reference count on
2829 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
2830 * code we would add a second cgroup function call, to drop that
2831 * reference. This would just create an unnecessary hot spot on
2832 * the top_cgroup reference count, to no avail.
2834 * Normally, holding a reference to a cgroup without bumping its
2835 * count is unsafe. The cgroup could go away, or someone could
2836 * attach us to a different cgroup, decrementing the count on
2837 * the first cgroup that we never incremented. But in this case,
2838 * top_cgroup isn't going away, and either task has PF_EXITING set,
2839 * which wards off any cgroup_attach_task() attempts, or task is a failed
2840 * fork, never visible to cgroup_attach_task.
2842 void cgroup_exit(struct task_struct *tsk, int run_callbacks)
2847 if (run_callbacks && need_forkexit_callback) {
2848 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
2849 struct cgroup_subsys *ss = subsys[i];
2856 * Unlink from the css_set task list if necessary.
2857 * Optimistically check cg_list before taking
2860 if (!list_empty(&tsk->cg_list)) {
2861 write_lock(&css_set_lock);
2862 if (!list_empty(&tsk->cg_list))
2863 list_del(&tsk->cg_list);
2864 write_unlock(&css_set_lock);
2867 /* Reassign the task to the init_css_set. */
2870 tsk->cgroups = &init_css_set;
2873 put_css_set_taskexit(cg);
2877 * cgroup_clone - clone the cgroup the given subsystem is attached to
2878 * @tsk: the task to be moved
2879 * @subsys: the given subsystem
2880 * @nodename: the name for the new cgroup
2882 * Duplicate the current cgroup in the hierarchy that the given
2883 * subsystem is attached to, and move this task into the new
2886 int cgroup_clone(struct task_struct *tsk, struct cgroup_subsys *subsys,
2889 struct dentry *dentry;
2891 struct cgroup *parent, *child;
2892 struct inode *inode;
2894 struct cgroupfs_root *root;
2895 struct cgroup_subsys *ss;
2897 /* We shouldn't be called by an unregistered subsystem */
2898 BUG_ON(!subsys->active);
2900 /* First figure out what hierarchy and cgroup we're dealing
2901 * with, and pin them so we can drop cgroup_mutex */
2902 mutex_lock(&cgroup_mutex);
2904 root = subsys->root;
2905 if (root == &rootnode) {
2906 mutex_unlock(&cgroup_mutex);
2911 parent = task_cgroup(tsk, subsys->subsys_id);
2913 /* Pin the hierarchy */
2914 if (!atomic_inc_not_zero(&parent->root->sb->s_active)) {
2915 /* We race with the final deactivate_super() */
2916 mutex_unlock(&cgroup_mutex);
2920 /* Keep the cgroup alive */
2923 mutex_unlock(&cgroup_mutex);
2925 /* Now do the VFS work to create a cgroup */
2926 inode = parent->dentry->d_inode;
2928 /* Hold the parent directory mutex across this operation to
2929 * stop anyone else deleting the new cgroup */
2930 mutex_lock(&inode->i_mutex);
2931 dentry = lookup_one_len(nodename, parent->dentry, strlen(nodename));
2932 if (IS_ERR(dentry)) {
2934 "cgroup: Couldn't allocate dentry for %s: %ld\n", nodename,
2936 ret = PTR_ERR(dentry);
2940 /* Create the cgroup directory, which also creates the cgroup */
2941 ret = vfs_mkdir(inode, dentry, 0755);
2942 child = __d_cgrp(dentry);
2946 "Failed to create cgroup %s: %d\n", nodename,
2951 /* The cgroup now exists. Retake cgroup_mutex and check
2952 * that we're still in the same state that we thought we
2954 mutex_lock(&cgroup_mutex);
2955 if ((root != subsys->root) ||
2956 (parent != task_cgroup(tsk, subsys->subsys_id))) {
2957 /* Aargh, we raced ... */
2958 mutex_unlock(&inode->i_mutex);
2961 deactivate_super(parent->root->sb);
2962 /* The cgroup is still accessible in the VFS, but
2963 * we're not going to try to rmdir() it at this
2966 "Race in cgroup_clone() - leaking cgroup %s\n",
2971 /* do any required auto-setup */
2972 for_each_subsys(root, ss) {
2974 ss->post_clone(ss, child);
2977 /* All seems fine. Finish by moving the task into the new cgroup */
2978 ret = cgroup_attach_task(child, tsk);
2979 mutex_unlock(&cgroup_mutex);
2982 mutex_unlock(&inode->i_mutex);
2984 mutex_lock(&cgroup_mutex);
2986 mutex_unlock(&cgroup_mutex);
2987 deactivate_super(parent->root->sb);
2992 * cgroup_is_descendant - see if @cgrp is a descendant of current task's cgrp
2993 * @cgrp: the cgroup in question
2995 * See if @cgrp is a descendant of the current task's cgroup in
2996 * the appropriate hierarchy.
2998 * If we are sending in dummytop, then presumably we are creating
2999 * the top cgroup in the subsystem.
3001 * Called only by the ns (nsproxy) cgroup.
3003 int cgroup_is_descendant(const struct cgroup *cgrp)
3006 struct cgroup *target;
3009 if (cgrp == dummytop)
3012 get_first_subsys(cgrp, NULL, &subsys_id);
3013 target = task_cgroup(current, subsys_id);
3014 while (cgrp != target && cgrp!= cgrp->top_cgroup)
3015 cgrp = cgrp->parent;
3016 ret = (cgrp == target);
3020 static void check_for_release(struct cgroup *cgrp)
3022 /* All of these checks rely on RCU to keep the cgroup
3023 * structure alive */
3024 if (cgroup_is_releasable(cgrp) && !atomic_read(&cgrp->count)
3025 && list_empty(&cgrp->children) && !cgroup_has_css_refs(cgrp)) {
3026 /* Control Group is currently removeable. If it's not
3027 * already queued for a userspace notification, queue
3029 int need_schedule_work = 0;
3030 spin_lock(&release_list_lock);
3031 if (!cgroup_is_removed(cgrp) &&
3032 list_empty(&cgrp->release_list)) {
3033 list_add(&cgrp->release_list, &release_list);
3034 need_schedule_work = 1;
3036 spin_unlock(&release_list_lock);
3037 if (need_schedule_work)
3038 schedule_work(&release_agent_work);
3042 void __css_put(struct cgroup_subsys_state *css)
3044 struct cgroup *cgrp = css->cgroup;
3046 if (atomic_dec_and_test(&css->refcnt) && notify_on_release(cgrp)) {
3047 set_bit(CGRP_RELEASABLE, &cgrp->flags);
3048 check_for_release(cgrp);
3054 * Notify userspace when a cgroup is released, by running the
3055 * configured release agent with the name of the cgroup (path
3056 * relative to the root of cgroup file system) as the argument.
3058 * Most likely, this user command will try to rmdir this cgroup.
3060 * This races with the possibility that some other task will be
3061 * attached to this cgroup before it is removed, or that some other
3062 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
3063 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
3064 * unused, and this cgroup will be reprieved from its death sentence,
3065 * to continue to serve a useful existence. Next time it's released,
3066 * we will get notified again, if it still has 'notify_on_release' set.
3068 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
3069 * means only wait until the task is successfully execve()'d. The
3070 * separate release agent task is forked by call_usermodehelper(),
3071 * then control in this thread returns here, without waiting for the
3072 * release agent task. We don't bother to wait because the caller of
3073 * this routine has no use for the exit status of the release agent
3074 * task, so no sense holding our caller up for that.
3076 static void cgroup_release_agent(struct work_struct *work)
3078 BUG_ON(work != &release_agent_work);
3079 mutex_lock(&cgroup_mutex);
3080 spin_lock(&release_list_lock);
3081 while (!list_empty(&release_list)) {
3082 char *argv[3], *envp[3];
3084 char *pathbuf = NULL, *agentbuf = NULL;
3085 struct cgroup *cgrp = list_entry(release_list.next,
3088 list_del_init(&cgrp->release_list);
3089 spin_unlock(&release_list_lock);
3090 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3093 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
3095 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
3100 argv[i++] = agentbuf;
3101 argv[i++] = pathbuf;
3105 /* minimal command environment */
3106 envp[i++] = "HOME=/";
3107 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
3110 /* Drop the lock while we invoke the usermode helper,
3111 * since the exec could involve hitting disk and hence
3112 * be a slow process */
3113 mutex_unlock(&cgroup_mutex);
3114 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
3115 mutex_lock(&cgroup_mutex);
3119 spin_lock(&release_list_lock);
3121 spin_unlock(&release_list_lock);
3122 mutex_unlock(&cgroup_mutex);
3125 static int __init cgroup_disable(char *str)
3130 while ((token = strsep(&str, ",")) != NULL) {
3134 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
3135 struct cgroup_subsys *ss = subsys[i];
3137 if (!strcmp(token, ss->name)) {
3139 printk(KERN_INFO "Disabling %s control group"
3140 " subsystem\n", ss->name);
3147 __setup("cgroup_disable=", cgroup_disable);