2 * Generic process-grouping system.
4 * Based originally on the cpuset system, extracted by Paul Menage
5 * Copyright (C) 2006 Google, Inc
7 * Notifications support
8 * Copyright (C) 2009 Nokia Corporation
9 * Author: Kirill A. Shutemov
11 * Copyright notices from the original cpuset code:
12 * --------------------------------------------------
13 * Copyright (C) 2003 BULL SA.
14 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
16 * Portions derived from Patrick Mochel's sysfs code.
17 * sysfs is Copyright (c) 2001-3 Patrick Mochel
19 * 2003-10-10 Written by Simon Derr.
20 * 2003-10-22 Updates by Stephen Hemminger.
21 * 2004 May-July Rework by Paul Jackson.
22 * ---------------------------------------------------
24 * This file is subject to the terms and conditions of the GNU General Public
25 * License. See the file COPYING in the main directory of the Linux
26 * distribution for more details.
29 #include <linux/cgroup.h>
30 #include <linux/cred.h>
31 #include <linux/ctype.h>
32 #include <linux/errno.h>
33 #include <linux/init_task.h>
34 #include <linux/kernel.h>
35 #include <linux/list.h>
37 #include <linux/mutex.h>
38 #include <linux/mount.h>
39 #include <linux/pagemap.h>
40 #include <linux/proc_fs.h>
41 #include <linux/rcupdate.h>
42 #include <linux/sched.h>
43 #include <linux/backing-dev.h>
44 #include <linux/seq_file.h>
45 #include <linux/slab.h>
46 #include <linux/magic.h>
47 #include <linux/spinlock.h>
48 #include <linux/string.h>
49 #include <linux/sort.h>
50 #include <linux/kmod.h>
51 #include <linux/module.h>
52 #include <linux/delayacct.h>
53 #include <linux/cgroupstats.h>
54 #include <linux/hashtable.h>
55 #include <linux/namei.h>
56 #include <linux/pid_namespace.h>
57 #include <linux/idr.h>
58 #include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
59 #include <linux/eventfd.h>
60 #include <linux/poll.h>
61 #include <linux/flex_array.h> /* used in cgroup_attach_task */
62 #include <linux/kthread.h>
64 #include <linux/atomic.h>
67 * cgroup_mutex is the master lock. Any modification to cgroup or its
68 * hierarchy must be performed while holding it.
70 * cgroup_root_mutex nests inside cgroup_mutex and should be held to modify
71 * cgroupfs_root of any cgroup hierarchy - subsys list, flags,
72 * release_agent_path and so on. Modifying requires both cgroup_mutex and
73 * cgroup_root_mutex. Readers can acquire either of the two. This is to
74 * break the following locking order cycle.
76 * A. cgroup_mutex -> cred_guard_mutex -> s_type->i_mutex_key -> namespace_sem
77 * B. namespace_sem -> cgroup_mutex
79 * B happens only through cgroup_show_options() and using cgroup_root_mutex
82 #ifdef CONFIG_PROVE_RCU
83 DEFINE_MUTEX(cgroup_mutex);
84 EXPORT_SYMBOL_GPL(cgroup_mutex); /* only for task_subsys_state_check() */
86 static DEFINE_MUTEX(cgroup_mutex);
89 static DEFINE_MUTEX(cgroup_root_mutex);
92 * Generate an array of cgroup subsystem pointers. At boot time, this is
93 * populated with the built in subsystems, and modular subsystems are
94 * registered after that. The mutable section of this array is protected by
97 #define SUBSYS(_x) [_x ## _subsys_id] = &_x ## _subsys,
98 #define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option)
99 static struct cgroup_subsys *cgroup_subsys[CGROUP_SUBSYS_COUNT] = {
100 #include <linux/cgroup_subsys.h>
104 * The dummy hierarchy, reserved for the subsystems that are otherwise
105 * unattached - it never has more than a single cgroup, and all tasks are
106 * part of that cgroup.
108 static struct cgroupfs_root cgroup_dummy_root;
110 /* dummy_top is a shorthand for the dummy hierarchy's top cgroup */
111 static struct cgroup * const cgroup_dummy_top = &cgroup_dummy_root.top_cgroup;
114 * cgroupfs file entry, pointed to from leaf dentry->d_fsdata.
117 struct list_head node;
118 struct dentry *dentry;
122 struct simple_xattrs xattrs;
126 * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when
127 * cgroup_subsys->use_id != 0.
129 #define CSS_ID_MAX (65535)
132 * The css to which this ID points. This pointer is set to valid value
133 * after cgroup is populated. If cgroup is removed, this will be NULL.
134 * This pointer is expected to be RCU-safe because destroy()
135 * is called after synchronize_rcu(). But for safe use, css_tryget()
136 * should be used for avoiding race.
138 struct cgroup_subsys_state __rcu *css;
144 * Depth in hierarchy which this ID belongs to.
146 unsigned short depth;
148 * ID is freed by RCU. (and lookup routine is RCU safe.)
150 struct rcu_head rcu_head;
152 * Hierarchy of CSS ID belongs to.
154 unsigned short stack[0]; /* Array of Length (depth+1) */
158 * cgroup_event represents events which userspace want to receive.
160 struct cgroup_event {
162 * Cgroup which the event belongs to.
166 * Control file which the event associated.
170 * eventfd to signal userspace about the event.
172 struct eventfd_ctx *eventfd;
174 * Each of these stored in a list by the cgroup.
176 struct list_head list;
178 * All fields below needed to unregister event when
179 * userspace closes eventfd.
182 wait_queue_head_t *wqh;
184 struct work_struct remove;
187 /* The list of hierarchy roots */
189 static LIST_HEAD(cgroup_roots);
190 static int cgroup_root_count;
193 * Hierarchy ID allocation and mapping. It follows the same exclusion
194 * rules as other root ops - both cgroup_mutex and cgroup_root_mutex for
195 * writes, either for reads.
197 static DEFINE_IDR(cgroup_hierarchy_idr);
199 static struct cgroup_name root_cgroup_name = { .name = "/" };
202 * Assign a monotonically increasing serial number to cgroups. It
203 * guarantees cgroups with bigger numbers are newer than those with smaller
204 * numbers. Also, as cgroups are always appended to the parent's
205 * ->children list, it guarantees that sibling cgroups are always sorted in
206 * the ascending serial number order on the list. Protected by
209 static u64 cgroup_serial_nr_next = 1;
211 /* This flag indicates whether tasks in the fork and exit paths should
212 * check for fork/exit handlers to call. This avoids us having to do
213 * extra work in the fork/exit path if none of the subsystems need to
216 static int need_forkexit_callback __read_mostly;
218 static void cgroup_offline_fn(struct work_struct *work);
219 static int cgroup_destroy_locked(struct cgroup *cgrp);
220 static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys,
221 struct cftype cfts[], bool is_add);
223 /* convenient tests for these bits */
224 static inline bool cgroup_is_dead(const struct cgroup *cgrp)
226 return test_bit(CGRP_DEAD, &cgrp->flags);
230 * cgroup_is_descendant - test ancestry
231 * @cgrp: the cgroup to be tested
232 * @ancestor: possible ancestor of @cgrp
234 * Test whether @cgrp is a descendant of @ancestor. It also returns %true
235 * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
236 * and @ancestor are accessible.
238 bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor)
241 if (cgrp == ancestor)
247 EXPORT_SYMBOL_GPL(cgroup_is_descendant);
249 static int cgroup_is_releasable(const struct cgroup *cgrp)
252 (1 << CGRP_RELEASABLE) |
253 (1 << CGRP_NOTIFY_ON_RELEASE);
254 return (cgrp->flags & bits) == bits;
257 static int notify_on_release(const struct cgroup *cgrp)
259 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
263 * for_each_subsys - iterate all loaded cgroup subsystems
264 * @ss: the iteration cursor
265 * @i: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
267 * Should be called under cgroup_mutex.
269 #define for_each_subsys(ss, i) \
270 for ((i) = 0; (i) < CGROUP_SUBSYS_COUNT; (i)++) \
271 if (({ lockdep_assert_held(&cgroup_mutex); \
272 !((ss) = cgroup_subsys[i]); })) { } \
276 * for_each_builtin_subsys - iterate all built-in cgroup subsystems
277 * @ss: the iteration cursor
278 * @i: the index of @ss, CGROUP_BUILTIN_SUBSYS_COUNT after reaching the end
280 * Bulit-in subsystems are always present and iteration itself doesn't
281 * require any synchronization.
283 #define for_each_builtin_subsys(ss, i) \
284 for ((i) = 0; (i) < CGROUP_BUILTIN_SUBSYS_COUNT && \
285 (((ss) = cgroup_subsys[i]) || true); (i)++)
287 /* iterate each subsystem attached to a hierarchy */
288 #define for_each_root_subsys(root, ss) \
289 list_for_each_entry((ss), &(root)->subsys_list, sibling)
291 /* iterate across the active hierarchies */
292 #define for_each_active_root(root) \
293 list_for_each_entry((root), &cgroup_roots, root_list)
295 static inline struct cgroup *__d_cgrp(struct dentry *dentry)
297 return dentry->d_fsdata;
300 static inline struct cfent *__d_cfe(struct dentry *dentry)
302 return dentry->d_fsdata;
305 static inline struct cftype *__d_cft(struct dentry *dentry)
307 return __d_cfe(dentry)->type;
311 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
312 * @cgrp: the cgroup to be checked for liveness
314 * On success, returns true; the mutex should be later unlocked. On
315 * failure returns false with no lock held.
317 static bool cgroup_lock_live_group(struct cgroup *cgrp)
319 mutex_lock(&cgroup_mutex);
320 if (cgroup_is_dead(cgrp)) {
321 mutex_unlock(&cgroup_mutex);
327 /* the list of cgroups eligible for automatic release. Protected by
328 * release_list_lock */
329 static LIST_HEAD(release_list);
330 static DEFINE_RAW_SPINLOCK(release_list_lock);
331 static void cgroup_release_agent(struct work_struct *work);
332 static DECLARE_WORK(release_agent_work, cgroup_release_agent);
333 static void check_for_release(struct cgroup *cgrp);
336 * A cgroup can be associated with multiple css_sets as different tasks may
337 * belong to different cgroups on different hierarchies. In the other
338 * direction, a css_set is naturally associated with multiple cgroups.
339 * This M:N relationship is represented by the following link structure
340 * which exists for each association and allows traversing the associations
343 struct cgrp_cset_link {
344 /* the cgroup and css_set this link associates */
346 struct css_set *cset;
348 /* list of cgrp_cset_links anchored at cgrp->cset_links */
349 struct list_head cset_link;
351 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
352 struct list_head cgrp_link;
355 /* The default css_set - used by init and its children prior to any
356 * hierarchies being mounted. It contains a pointer to the root state
357 * for each subsystem. Also used to anchor the list of css_sets. Not
358 * reference-counted, to improve performance when child cgroups
359 * haven't been created.
362 static struct css_set init_css_set;
363 static struct cgrp_cset_link init_cgrp_cset_link;
365 static int cgroup_init_idr(struct cgroup_subsys *ss,
366 struct cgroup_subsys_state *css);
368 /* css_set_lock protects the list of css_set objects, and the
369 * chain of tasks off each css_set. Nests outside task->alloc_lock
370 * due to cgroup_iter_start() */
371 static DEFINE_RWLOCK(css_set_lock);
372 static int css_set_count;
375 * hash table for cgroup groups. This improves the performance to find
376 * an existing css_set. This hash doesn't (currently) take into
377 * account cgroups in empty hierarchies.
379 #define CSS_SET_HASH_BITS 7
380 static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
382 static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
384 unsigned long key = 0UL;
385 struct cgroup_subsys *ss;
388 for_each_subsys(ss, i)
389 key += (unsigned long)css[i];
390 key = (key >> 16) ^ key;
395 /* We don't maintain the lists running through each css_set to its
396 * task until after the first call to cgroup_iter_start(). This
397 * reduces the fork()/exit() overhead for people who have cgroups
398 * compiled into their kernel but not actually in use */
399 static int use_task_css_set_links __read_mostly;
401 static void __put_css_set(struct css_set *cset, int taskexit)
403 struct cgrp_cset_link *link, *tmp_link;
406 * Ensure that the refcount doesn't hit zero while any readers
407 * can see it. Similar to atomic_dec_and_lock(), but for an
410 if (atomic_add_unless(&cset->refcount, -1, 1))
412 write_lock(&css_set_lock);
413 if (!atomic_dec_and_test(&cset->refcount)) {
414 write_unlock(&css_set_lock);
418 /* This css_set is dead. unlink it and release cgroup refcounts */
419 hash_del(&cset->hlist);
422 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
423 struct cgroup *cgrp = link->cgrp;
425 list_del(&link->cset_link);
426 list_del(&link->cgrp_link);
428 /* @cgrp can't go away while we're holding css_set_lock */
429 if (list_empty(&cgrp->cset_links) && notify_on_release(cgrp)) {
431 set_bit(CGRP_RELEASABLE, &cgrp->flags);
432 check_for_release(cgrp);
438 write_unlock(&css_set_lock);
439 kfree_rcu(cset, rcu_head);
443 * refcounted get/put for css_set objects
445 static inline void get_css_set(struct css_set *cset)
447 atomic_inc(&cset->refcount);
450 static inline void put_css_set(struct css_set *cset)
452 __put_css_set(cset, 0);
455 static inline void put_css_set_taskexit(struct css_set *cset)
457 __put_css_set(cset, 1);
461 * compare_css_sets - helper function for find_existing_css_set().
462 * @cset: candidate css_set being tested
463 * @old_cset: existing css_set for a task
464 * @new_cgrp: cgroup that's being entered by the task
465 * @template: desired set of css pointers in css_set (pre-calculated)
467 * Returns true if "cg" matches "old_cg" except for the hierarchy
468 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
470 static bool compare_css_sets(struct css_set *cset,
471 struct css_set *old_cset,
472 struct cgroup *new_cgrp,
473 struct cgroup_subsys_state *template[])
475 struct list_head *l1, *l2;
477 if (memcmp(template, cset->subsys, sizeof(cset->subsys))) {
478 /* Not all subsystems matched */
483 * Compare cgroup pointers in order to distinguish between
484 * different cgroups in heirarchies with no subsystems. We
485 * could get by with just this check alone (and skip the
486 * memcmp above) but on most setups the memcmp check will
487 * avoid the need for this more expensive check on almost all
491 l1 = &cset->cgrp_links;
492 l2 = &old_cset->cgrp_links;
494 struct cgrp_cset_link *link1, *link2;
495 struct cgroup *cgrp1, *cgrp2;
499 /* See if we reached the end - both lists are equal length. */
500 if (l1 == &cset->cgrp_links) {
501 BUG_ON(l2 != &old_cset->cgrp_links);
504 BUG_ON(l2 == &old_cset->cgrp_links);
506 /* Locate the cgroups associated with these links. */
507 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
508 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
511 /* Hierarchies should be linked in the same order. */
512 BUG_ON(cgrp1->root != cgrp2->root);
515 * If this hierarchy is the hierarchy of the cgroup
516 * that's changing, then we need to check that this
517 * css_set points to the new cgroup; if it's any other
518 * hierarchy, then this css_set should point to the
519 * same cgroup as the old css_set.
521 if (cgrp1->root == new_cgrp->root) {
522 if (cgrp1 != new_cgrp)
533 * find_existing_css_set - init css array and find the matching css_set
534 * @old_cset: the css_set that we're using before the cgroup transition
535 * @cgrp: the cgroup that we're moving into
536 * @template: out param for the new set of csses, should be clear on entry
538 static struct css_set *find_existing_css_set(struct css_set *old_cset,
540 struct cgroup_subsys_state *template[])
542 struct cgroupfs_root *root = cgrp->root;
543 struct cgroup_subsys *ss;
544 struct css_set *cset;
549 * Build the set of subsystem state objects that we want to see in the
550 * new css_set. while subsystems can change globally, the entries here
551 * won't change, so no need for locking.
553 for_each_subsys(ss, i) {
554 if (root->subsys_mask & (1UL << i)) {
555 /* Subsystem is in this hierarchy. So we want
556 * the subsystem state from the new
558 template[i] = cgrp->subsys[i];
560 /* Subsystem is not in this hierarchy, so we
561 * don't want to change the subsystem state */
562 template[i] = old_cset->subsys[i];
566 key = css_set_hash(template);
567 hash_for_each_possible(css_set_table, cset, hlist, key) {
568 if (!compare_css_sets(cset, old_cset, cgrp, template))
571 /* This css_set matches what we need */
575 /* No existing cgroup group matched */
579 static void free_cgrp_cset_links(struct list_head *links_to_free)
581 struct cgrp_cset_link *link, *tmp_link;
583 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
584 list_del(&link->cset_link);
590 * allocate_cgrp_cset_links - allocate cgrp_cset_links
591 * @count: the number of links to allocate
592 * @tmp_links: list_head the allocated links are put on
594 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
595 * through ->cset_link. Returns 0 on success or -errno.
597 static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
599 struct cgrp_cset_link *link;
602 INIT_LIST_HEAD(tmp_links);
604 for (i = 0; i < count; i++) {
605 link = kzalloc(sizeof(*link), GFP_KERNEL);
607 free_cgrp_cset_links(tmp_links);
610 list_add(&link->cset_link, tmp_links);
616 * link_css_set - a helper function to link a css_set to a cgroup
617 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
618 * @cset: the css_set to be linked
619 * @cgrp: the destination cgroup
621 static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
624 struct cgrp_cset_link *link;
626 BUG_ON(list_empty(tmp_links));
627 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
630 list_move(&link->cset_link, &cgrp->cset_links);
632 * Always add links to the tail of the list so that the list
633 * is sorted by order of hierarchy creation
635 list_add_tail(&link->cgrp_link, &cset->cgrp_links);
639 * find_css_set - return a new css_set with one cgroup updated
640 * @old_cset: the baseline css_set
641 * @cgrp: the cgroup to be updated
643 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
644 * substituted into the appropriate hierarchy.
646 static struct css_set *find_css_set(struct css_set *old_cset,
649 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
650 struct css_set *cset;
651 struct list_head tmp_links;
652 struct cgrp_cset_link *link;
655 lockdep_assert_held(&cgroup_mutex);
657 /* First see if we already have a cgroup group that matches
659 read_lock(&css_set_lock);
660 cset = find_existing_css_set(old_cset, cgrp, template);
663 read_unlock(&css_set_lock);
668 cset = kzalloc(sizeof(*cset), GFP_KERNEL);
672 /* Allocate all the cgrp_cset_link objects that we'll need */
673 if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
678 atomic_set(&cset->refcount, 1);
679 INIT_LIST_HEAD(&cset->cgrp_links);
680 INIT_LIST_HEAD(&cset->tasks);
681 INIT_HLIST_NODE(&cset->hlist);
683 /* Copy the set of subsystem state objects generated in
684 * find_existing_css_set() */
685 memcpy(cset->subsys, template, sizeof(cset->subsys));
687 write_lock(&css_set_lock);
688 /* Add reference counts and links from the new css_set. */
689 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
690 struct cgroup *c = link->cgrp;
692 if (c->root == cgrp->root)
694 link_css_set(&tmp_links, cset, c);
697 BUG_ON(!list_empty(&tmp_links));
701 /* Add this cgroup group to the hash table */
702 key = css_set_hash(cset->subsys);
703 hash_add(css_set_table, &cset->hlist, key);
705 write_unlock(&css_set_lock);
711 * Return the cgroup for "task" from the given hierarchy. Must be
712 * called with cgroup_mutex held.
714 static struct cgroup *task_cgroup_from_root(struct task_struct *task,
715 struct cgroupfs_root *root)
717 struct css_set *cset;
718 struct cgroup *res = NULL;
720 BUG_ON(!mutex_is_locked(&cgroup_mutex));
721 read_lock(&css_set_lock);
723 * No need to lock the task - since we hold cgroup_mutex the
724 * task can't change groups, so the only thing that can happen
725 * is that it exits and its css is set back to init_css_set.
727 cset = task_css_set(task);
728 if (cset == &init_css_set) {
729 res = &root->top_cgroup;
731 struct cgrp_cset_link *link;
733 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
734 struct cgroup *c = link->cgrp;
736 if (c->root == root) {
742 read_unlock(&css_set_lock);
748 * There is one global cgroup mutex. We also require taking
749 * task_lock() when dereferencing a task's cgroup subsys pointers.
750 * See "The task_lock() exception", at the end of this comment.
752 * A task must hold cgroup_mutex to modify cgroups.
754 * Any task can increment and decrement the count field without lock.
755 * So in general, code holding cgroup_mutex can't rely on the count
756 * field not changing. However, if the count goes to zero, then only
757 * cgroup_attach_task() can increment it again. Because a count of zero
758 * means that no tasks are currently attached, therefore there is no
759 * way a task attached to that cgroup can fork (the other way to
760 * increment the count). So code holding cgroup_mutex can safely
761 * assume that if the count is zero, it will stay zero. Similarly, if
762 * a task holds cgroup_mutex on a cgroup with zero count, it
763 * knows that the cgroup won't be removed, as cgroup_rmdir()
766 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
767 * (usually) take cgroup_mutex. These are the two most performance
768 * critical pieces of code here. The exception occurs on cgroup_exit(),
769 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
770 * is taken, and if the cgroup count is zero, a usermode call made
771 * to the release agent with the name of the cgroup (path relative to
772 * the root of cgroup file system) as the argument.
774 * A cgroup can only be deleted if both its 'count' of using tasks
775 * is zero, and its list of 'children' cgroups is empty. Since all
776 * tasks in the system use _some_ cgroup, and since there is always at
777 * least one task in the system (init, pid == 1), therefore, top_cgroup
778 * always has either children cgroups and/or using tasks. So we don't
779 * need a special hack to ensure that top_cgroup cannot be deleted.
781 * The task_lock() exception
783 * The need for this exception arises from the action of
784 * cgroup_attach_task(), which overwrites one task's cgroup pointer with
785 * another. It does so using cgroup_mutex, however there are
786 * several performance critical places that need to reference
787 * task->cgroup without the expense of grabbing a system global
788 * mutex. Therefore except as noted below, when dereferencing or, as
789 * in cgroup_attach_task(), modifying a task's cgroup pointer we use
790 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
791 * the task_struct routinely used for such matters.
793 * P.S. One more locking exception. RCU is used to guard the
794 * update of a tasks cgroup pointer by cgroup_attach_task()
798 * A couple of forward declarations required, due to cyclic reference loop:
799 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
800 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
804 static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
805 static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
806 static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files,
807 unsigned long subsys_mask);
808 static const struct inode_operations cgroup_dir_inode_operations;
809 static const struct file_operations proc_cgroupstats_operations;
811 static struct backing_dev_info cgroup_backing_dev_info = {
813 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
816 static int alloc_css_id(struct cgroup_subsys *ss,
817 struct cgroup *parent, struct cgroup *child);
819 static struct inode *cgroup_new_inode(umode_t mode, struct super_block *sb)
821 struct inode *inode = new_inode(sb);
824 inode->i_ino = get_next_ino();
825 inode->i_mode = mode;
826 inode->i_uid = current_fsuid();
827 inode->i_gid = current_fsgid();
828 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
829 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
834 static struct cgroup_name *cgroup_alloc_name(struct dentry *dentry)
836 struct cgroup_name *name;
838 name = kmalloc(sizeof(*name) + dentry->d_name.len + 1, GFP_KERNEL);
841 strcpy(name->name, dentry->d_name.name);
845 static void cgroup_free_fn(struct work_struct *work)
847 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
848 struct cgroup_subsys *ss;
850 mutex_lock(&cgroup_mutex);
852 * Release the subsystem state objects.
854 for_each_root_subsys(cgrp->root, ss)
857 cgrp->root->number_of_cgroups--;
858 mutex_unlock(&cgroup_mutex);
861 * We get a ref to the parent's dentry, and put the ref when
862 * this cgroup is being freed, so it's guaranteed that the
863 * parent won't be destroyed before its children.
865 dput(cgrp->parent->dentry);
867 ida_simple_remove(&cgrp->root->cgroup_ida, cgrp->id);
870 * Drop the active superblock reference that we took when we
871 * created the cgroup. This will free cgrp->root, if we are
872 * holding the last reference to @sb.
874 deactivate_super(cgrp->root->sb);
877 * if we're getting rid of the cgroup, refcount should ensure
878 * that there are no pidlists left.
880 BUG_ON(!list_empty(&cgrp->pidlists));
882 simple_xattrs_free(&cgrp->xattrs);
884 kfree(rcu_dereference_raw(cgrp->name));
888 static void cgroup_free_rcu(struct rcu_head *head)
890 struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
892 INIT_WORK(&cgrp->destroy_work, cgroup_free_fn);
893 schedule_work(&cgrp->destroy_work);
896 static void cgroup_diput(struct dentry *dentry, struct inode *inode)
898 /* is dentry a directory ? if so, kfree() associated cgroup */
899 if (S_ISDIR(inode->i_mode)) {
900 struct cgroup *cgrp = dentry->d_fsdata;
902 BUG_ON(!(cgroup_is_dead(cgrp)));
903 call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
905 struct cfent *cfe = __d_cfe(dentry);
906 struct cgroup *cgrp = dentry->d_parent->d_fsdata;
908 WARN_ONCE(!list_empty(&cfe->node) &&
909 cgrp != &cgrp->root->top_cgroup,
910 "cfe still linked for %s\n", cfe->type->name);
911 simple_xattrs_free(&cfe->xattrs);
917 static int cgroup_delete(const struct dentry *d)
922 static void remove_dir(struct dentry *d)
924 struct dentry *parent = dget(d->d_parent);
927 simple_rmdir(parent->d_inode, d);
931 static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
935 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
936 lockdep_assert_held(&cgroup_mutex);
939 * If we're doing cleanup due to failure of cgroup_create(),
940 * the corresponding @cfe may not exist.
942 list_for_each_entry(cfe, &cgrp->files, node) {
943 struct dentry *d = cfe->dentry;
945 if (cft && cfe->type != cft)
950 simple_unlink(cgrp->dentry->d_inode, d);
951 list_del_init(&cfe->node);
959 * cgroup_clear_directory - selective removal of base and subsystem files
960 * @dir: directory containing the files
961 * @base_files: true if the base files should be removed
962 * @subsys_mask: mask of the subsystem ids whose files should be removed
964 static void cgroup_clear_directory(struct dentry *dir, bool base_files,
965 unsigned long subsys_mask)
967 struct cgroup *cgrp = __d_cgrp(dir);
968 struct cgroup_subsys *ss;
970 for_each_root_subsys(cgrp->root, ss) {
971 struct cftype_set *set;
972 if (!test_bit(ss->subsys_id, &subsys_mask))
974 list_for_each_entry(set, &ss->cftsets, node)
975 cgroup_addrm_files(cgrp, NULL, set->cfts, false);
978 while (!list_empty(&cgrp->files))
979 cgroup_rm_file(cgrp, NULL);
984 * NOTE : the dentry must have been dget()'ed
986 static void cgroup_d_remove_dir(struct dentry *dentry)
988 struct dentry *parent;
989 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
991 cgroup_clear_directory(dentry, true, root->subsys_mask);
993 parent = dentry->d_parent;
994 spin_lock(&parent->d_lock);
995 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
996 list_del_init(&dentry->d_u.d_child);
997 spin_unlock(&dentry->d_lock);
998 spin_unlock(&parent->d_lock);
1003 * Call with cgroup_mutex held. Drops reference counts on modules, including
1004 * any duplicate ones that parse_cgroupfs_options took. If this function
1005 * returns an error, no reference counts are touched.
1007 static int rebind_subsystems(struct cgroupfs_root *root,
1008 unsigned long added_mask, unsigned removed_mask)
1010 struct cgroup *cgrp = &root->top_cgroup;
1011 struct cgroup_subsys *ss;
1014 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1015 BUG_ON(!mutex_is_locked(&cgroup_root_mutex));
1017 /* Check that any added subsystems are currently free */
1018 for_each_subsys(ss, i) {
1019 unsigned long bit = 1UL << i;
1021 if (!(bit & added_mask))
1024 if (ss->root != &cgroup_dummy_root) {
1025 /* Subsystem isn't free */
1030 /* Currently we don't handle adding/removing subsystems when
1031 * any child cgroups exist. This is theoretically supportable
1032 * but involves complex error handling, so it's being left until
1034 if (root->number_of_cgroups > 1)
1037 /* Process each subsystem */
1038 for_each_subsys(ss, i) {
1039 unsigned long bit = 1UL << i;
1041 if (bit & added_mask) {
1042 /* We're binding this subsystem to this hierarchy */
1043 BUG_ON(cgrp->subsys[i]);
1044 BUG_ON(!cgroup_dummy_top->subsys[i]);
1045 BUG_ON(cgroup_dummy_top->subsys[i]->cgroup != cgroup_dummy_top);
1047 cgrp->subsys[i] = cgroup_dummy_top->subsys[i];
1048 cgrp->subsys[i]->cgroup = cgrp;
1049 list_move(&ss->sibling, &root->subsys_list);
1054 /* refcount was already taken, and we're keeping it */
1055 root->subsys_mask |= bit;
1056 } else if (bit & removed_mask) {
1057 /* We're removing this subsystem */
1058 BUG_ON(cgrp->subsys[i] != cgroup_dummy_top->subsys[i]);
1059 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
1062 ss->bind(cgroup_dummy_top);
1063 cgroup_dummy_top->subsys[i]->cgroup = cgroup_dummy_top;
1064 cgrp->subsys[i] = NULL;
1065 cgroup_subsys[i]->root = &cgroup_dummy_root;
1066 list_move(&ss->sibling, &cgroup_dummy_root.subsys_list);
1068 /* subsystem is now free - drop reference on module */
1069 module_put(ss->module);
1070 root->subsys_mask &= ~bit;
1071 } else if (bit & root->subsys_mask) {
1072 /* Subsystem state should already exist */
1073 BUG_ON(!cgrp->subsys[i]);
1075 * a refcount was taken, but we already had one, so
1076 * drop the extra reference.
1078 module_put(ss->module);
1079 #ifdef CONFIG_MODULE_UNLOAD
1080 BUG_ON(ss->module && !module_refcount(ss->module));
1083 /* Subsystem state shouldn't exist */
1084 BUG_ON(cgrp->subsys[i]);
1089 * Mark @root has finished binding subsystems. @root->subsys_mask
1090 * now matches the bound subsystems.
1092 root->flags |= CGRP_ROOT_SUBSYS_BOUND;
1097 static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry)
1099 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
1100 struct cgroup_subsys *ss;
1102 mutex_lock(&cgroup_root_mutex);
1103 for_each_root_subsys(root, ss)
1104 seq_printf(seq, ",%s", ss->name);
1105 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
1106 seq_puts(seq, ",sane_behavior");
1107 if (root->flags & CGRP_ROOT_NOPREFIX)
1108 seq_puts(seq, ",noprefix");
1109 if (root->flags & CGRP_ROOT_XATTR)
1110 seq_puts(seq, ",xattr");
1111 if (strlen(root->release_agent_path))
1112 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
1113 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags))
1114 seq_puts(seq, ",clone_children");
1115 if (strlen(root->name))
1116 seq_printf(seq, ",name=%s", root->name);
1117 mutex_unlock(&cgroup_root_mutex);
1121 struct cgroup_sb_opts {
1122 unsigned long subsys_mask;
1123 unsigned long flags;
1124 char *release_agent;
1125 bool cpuset_clone_children;
1127 /* User explicitly requested empty subsystem */
1130 struct cgroupfs_root *new_root;
1135 * Convert a hierarchy specifier into a bitmask of subsystems and
1136 * flags. Call with cgroup_mutex held to protect the cgroup_subsys[]
1137 * array. This function takes refcounts on subsystems to be used, unless it
1138 * returns error, in which case no refcounts are taken.
1140 static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
1142 char *token, *o = data;
1143 bool all_ss = false, one_ss = false;
1144 unsigned long mask = (unsigned long)-1;
1145 bool module_pin_failed = false;
1146 struct cgroup_subsys *ss;
1149 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1151 #ifdef CONFIG_CPUSETS
1152 mask = ~(1UL << cpuset_subsys_id);
1155 memset(opts, 0, sizeof(*opts));
1157 while ((token = strsep(&o, ",")) != NULL) {
1160 if (!strcmp(token, "none")) {
1161 /* Explicitly have no subsystems */
1165 if (!strcmp(token, "all")) {
1166 /* Mutually exclusive option 'all' + subsystem name */
1172 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1173 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1176 if (!strcmp(token, "noprefix")) {
1177 opts->flags |= CGRP_ROOT_NOPREFIX;
1180 if (!strcmp(token, "clone_children")) {
1181 opts->cpuset_clone_children = true;
1184 if (!strcmp(token, "xattr")) {
1185 opts->flags |= CGRP_ROOT_XATTR;
1188 if (!strncmp(token, "release_agent=", 14)) {
1189 /* Specifying two release agents is forbidden */
1190 if (opts->release_agent)
1192 opts->release_agent =
1193 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
1194 if (!opts->release_agent)
1198 if (!strncmp(token, "name=", 5)) {
1199 const char *name = token + 5;
1200 /* Can't specify an empty name */
1203 /* Must match [\w.-]+ */
1204 for (i = 0; i < strlen(name); i++) {
1208 if ((c == '.') || (c == '-') || (c == '_'))
1212 /* Specifying two names is forbidden */
1215 opts->name = kstrndup(name,
1216 MAX_CGROUP_ROOT_NAMELEN - 1,
1224 for_each_subsys(ss, i) {
1225 if (strcmp(token, ss->name))
1230 /* Mutually exclusive option 'all' + subsystem name */
1233 set_bit(i, &opts->subsys_mask);
1238 if (i == CGROUP_SUBSYS_COUNT)
1243 * If the 'all' option was specified select all the subsystems,
1244 * otherwise if 'none', 'name=' and a subsystem name options
1245 * were not specified, let's default to 'all'
1247 if (all_ss || (!one_ss && !opts->none && !opts->name))
1248 for_each_subsys(ss, i)
1250 set_bit(i, &opts->subsys_mask);
1252 /* Consistency checks */
1254 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1255 pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
1257 if (opts->flags & CGRP_ROOT_NOPREFIX) {
1258 pr_err("cgroup: sane_behavior: noprefix is not allowed\n");
1262 if (opts->cpuset_clone_children) {
1263 pr_err("cgroup: sane_behavior: clone_children is not allowed\n");
1269 * Option noprefix was introduced just for backward compatibility
1270 * with the old cpuset, so we allow noprefix only if mounting just
1271 * the cpuset subsystem.
1273 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
1277 /* Can't specify "none" and some subsystems */
1278 if (opts->subsys_mask && opts->none)
1282 * We either have to specify by name or by subsystems. (So all
1283 * empty hierarchies must have a name).
1285 if (!opts->subsys_mask && !opts->name)
1289 * Grab references on all the modules we'll need, so the subsystems
1290 * don't dance around before rebind_subsystems attaches them. This may
1291 * take duplicate reference counts on a subsystem that's already used,
1292 * but rebind_subsystems handles this case.
1294 for_each_subsys(ss, i) {
1295 if (!(opts->subsys_mask & (1UL << i)))
1297 if (!try_module_get(cgroup_subsys[i]->module)) {
1298 module_pin_failed = true;
1302 if (module_pin_failed) {
1304 * oops, one of the modules was going away. this means that we
1305 * raced with a module_delete call, and to the user this is
1306 * essentially a "subsystem doesn't exist" case.
1308 for (i--; i >= 0; i--) {
1309 /* drop refcounts only on the ones we took */
1310 unsigned long bit = 1UL << i;
1312 if (!(bit & opts->subsys_mask))
1314 module_put(cgroup_subsys[i]->module);
1322 static void drop_parsed_module_refcounts(unsigned long subsys_mask)
1324 struct cgroup_subsys *ss;
1327 mutex_lock(&cgroup_mutex);
1328 for_each_subsys(ss, i)
1329 if (subsys_mask & (1UL << i))
1330 module_put(cgroup_subsys[i]->module);
1331 mutex_unlock(&cgroup_mutex);
1334 static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1337 struct cgroupfs_root *root = sb->s_fs_info;
1338 struct cgroup *cgrp = &root->top_cgroup;
1339 struct cgroup_sb_opts opts;
1340 unsigned long added_mask, removed_mask;
1342 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1343 pr_err("cgroup: sane_behavior: remount is not allowed\n");
1347 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
1348 mutex_lock(&cgroup_mutex);
1349 mutex_lock(&cgroup_root_mutex);
1351 /* See what subsystems are wanted */
1352 ret = parse_cgroupfs_options(data, &opts);
1356 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
1357 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1358 task_tgid_nr(current), current->comm);
1360 added_mask = opts.subsys_mask & ~root->subsys_mask;
1361 removed_mask = root->subsys_mask & ~opts.subsys_mask;
1363 /* Don't allow flags or name to change at remount */
1364 if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) ||
1365 (opts.name && strcmp(opts.name, root->name))) {
1366 pr_err("cgroup: option or name mismatch, new: 0x%lx \"%s\", old: 0x%lx \"%s\"\n",
1367 opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "",
1368 root->flags & CGRP_ROOT_OPTION_MASK, root->name);
1374 * Clear out the files of subsystems that should be removed, do
1375 * this before rebind_subsystems, since rebind_subsystems may
1376 * change this hierarchy's subsys_list.
1378 cgroup_clear_directory(cgrp->dentry, false, removed_mask);
1380 ret = rebind_subsystems(root, added_mask, removed_mask);
1382 /* rebind_subsystems failed, re-populate the removed files */
1383 cgroup_populate_dir(cgrp, false, removed_mask);
1387 /* re-populate subsystem files */
1388 cgroup_populate_dir(cgrp, false, added_mask);
1390 if (opts.release_agent)
1391 strcpy(root->release_agent_path, opts.release_agent);
1393 kfree(opts.release_agent);
1395 mutex_unlock(&cgroup_root_mutex);
1396 mutex_unlock(&cgroup_mutex);
1397 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
1399 drop_parsed_module_refcounts(opts.subsys_mask);
1403 static const struct super_operations cgroup_ops = {
1404 .statfs = simple_statfs,
1405 .drop_inode = generic_delete_inode,
1406 .show_options = cgroup_show_options,
1407 .remount_fs = cgroup_remount,
1410 static void init_cgroup_housekeeping(struct cgroup *cgrp)
1412 INIT_LIST_HEAD(&cgrp->sibling);
1413 INIT_LIST_HEAD(&cgrp->children);
1414 INIT_LIST_HEAD(&cgrp->files);
1415 INIT_LIST_HEAD(&cgrp->cset_links);
1416 INIT_LIST_HEAD(&cgrp->release_list);
1417 INIT_LIST_HEAD(&cgrp->pidlists);
1418 mutex_init(&cgrp->pidlist_mutex);
1419 INIT_LIST_HEAD(&cgrp->event_list);
1420 spin_lock_init(&cgrp->event_list_lock);
1421 simple_xattrs_init(&cgrp->xattrs);
1424 static void init_cgroup_root(struct cgroupfs_root *root)
1426 struct cgroup *cgrp = &root->top_cgroup;
1428 INIT_LIST_HEAD(&root->subsys_list);
1429 INIT_LIST_HEAD(&root->root_list);
1430 root->number_of_cgroups = 1;
1432 RCU_INIT_POINTER(cgrp->name, &root_cgroup_name);
1433 init_cgroup_housekeeping(cgrp);
1436 static int cgroup_init_root_id(struct cgroupfs_root *root, int start, int end)
1440 lockdep_assert_held(&cgroup_mutex);
1441 lockdep_assert_held(&cgroup_root_mutex);
1443 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, start, end,
1448 root->hierarchy_id = id;
1452 static void cgroup_exit_root_id(struct cgroupfs_root *root)
1454 lockdep_assert_held(&cgroup_mutex);
1455 lockdep_assert_held(&cgroup_root_mutex);
1457 if (root->hierarchy_id) {
1458 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
1459 root->hierarchy_id = 0;
1463 static int cgroup_test_super(struct super_block *sb, void *data)
1465 struct cgroup_sb_opts *opts = data;
1466 struct cgroupfs_root *root = sb->s_fs_info;
1468 /* If we asked for a name then it must match */
1469 if (opts->name && strcmp(opts->name, root->name))
1473 * If we asked for subsystems (or explicitly for no
1474 * subsystems) then they must match
1476 if ((opts->subsys_mask || opts->none)
1477 && (opts->subsys_mask != root->subsys_mask))
1483 static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1485 struct cgroupfs_root *root;
1487 if (!opts->subsys_mask && !opts->none)
1490 root = kzalloc(sizeof(*root), GFP_KERNEL);
1492 return ERR_PTR(-ENOMEM);
1494 init_cgroup_root(root);
1497 * We need to set @root->subsys_mask now so that @root can be
1498 * matched by cgroup_test_super() before it finishes
1499 * initialization; otherwise, competing mounts with the same
1500 * options may try to bind the same subsystems instead of waiting
1501 * for the first one leading to unexpected mount errors.
1502 * SUBSYS_BOUND will be set once actual binding is complete.
1504 root->subsys_mask = opts->subsys_mask;
1505 root->flags = opts->flags;
1506 ida_init(&root->cgroup_ida);
1507 if (opts->release_agent)
1508 strcpy(root->release_agent_path, opts->release_agent);
1510 strcpy(root->name, opts->name);
1511 if (opts->cpuset_clone_children)
1512 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags);
1516 static void cgroup_free_root(struct cgroupfs_root *root)
1519 /* hierarhcy ID shoulid already have been released */
1520 WARN_ON_ONCE(root->hierarchy_id);
1522 ida_destroy(&root->cgroup_ida);
1527 static int cgroup_set_super(struct super_block *sb, void *data)
1530 struct cgroup_sb_opts *opts = data;
1532 /* If we don't have a new root, we can't set up a new sb */
1533 if (!opts->new_root)
1536 BUG_ON(!opts->subsys_mask && !opts->none);
1538 ret = set_anon_super(sb, NULL);
1542 sb->s_fs_info = opts->new_root;
1543 opts->new_root->sb = sb;
1545 sb->s_blocksize = PAGE_CACHE_SIZE;
1546 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1547 sb->s_magic = CGROUP_SUPER_MAGIC;
1548 sb->s_op = &cgroup_ops;
1553 static int cgroup_get_rootdir(struct super_block *sb)
1555 static const struct dentry_operations cgroup_dops = {
1556 .d_iput = cgroup_diput,
1557 .d_delete = cgroup_delete,
1560 struct inode *inode =
1561 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
1566 inode->i_fop = &simple_dir_operations;
1567 inode->i_op = &cgroup_dir_inode_operations;
1568 /* directories start off with i_nlink == 2 (for "." entry) */
1570 sb->s_root = d_make_root(inode);
1573 /* for everything else we want ->d_op set */
1574 sb->s_d_op = &cgroup_dops;
1578 static struct dentry *cgroup_mount(struct file_system_type *fs_type,
1579 int flags, const char *unused_dev_name,
1582 struct cgroup_sb_opts opts;
1583 struct cgroupfs_root *root;
1585 struct super_block *sb;
1586 struct cgroupfs_root *new_root;
1587 struct inode *inode;
1589 /* First find the desired set of subsystems */
1590 mutex_lock(&cgroup_mutex);
1591 ret = parse_cgroupfs_options(data, &opts);
1592 mutex_unlock(&cgroup_mutex);
1597 * Allocate a new cgroup root. We may not need it if we're
1598 * reusing an existing hierarchy.
1600 new_root = cgroup_root_from_opts(&opts);
1601 if (IS_ERR(new_root)) {
1602 ret = PTR_ERR(new_root);
1605 opts.new_root = new_root;
1607 /* Locate an existing or new sb for this hierarchy */
1608 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts);
1611 cgroup_free_root(opts.new_root);
1615 root = sb->s_fs_info;
1617 if (root == opts.new_root) {
1618 /* We used the new root structure, so this is a new hierarchy */
1619 struct list_head tmp_links;
1620 struct cgroup *root_cgrp = &root->top_cgroup;
1621 struct cgroupfs_root *existing_root;
1622 const struct cred *cred;
1624 struct css_set *cset;
1626 BUG_ON(sb->s_root != NULL);
1628 ret = cgroup_get_rootdir(sb);
1630 goto drop_new_super;
1631 inode = sb->s_root->d_inode;
1633 mutex_lock(&inode->i_mutex);
1634 mutex_lock(&cgroup_mutex);
1635 mutex_lock(&cgroup_root_mutex);
1637 /* Check for name clashes with existing mounts */
1639 if (strlen(root->name))
1640 for_each_active_root(existing_root)
1641 if (!strcmp(existing_root->name, root->name))
1645 * We're accessing css_set_count without locking
1646 * css_set_lock here, but that's OK - it can only be
1647 * increased by someone holding cgroup_lock, and
1648 * that's us. The worst that can happen is that we
1649 * have some link structures left over
1651 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
1655 /* ID 0 is reserved for dummy root, 1 for unified hierarchy */
1656 ret = cgroup_init_root_id(root, 2, 0);
1660 ret = rebind_subsystems(root, root->subsys_mask, 0);
1661 if (ret == -EBUSY) {
1662 free_cgrp_cset_links(&tmp_links);
1666 * There must be no failure case after here, since rebinding
1667 * takes care of subsystems' refcounts, which are explicitly
1668 * dropped in the failure exit path.
1671 /* EBUSY should be the only error here */
1674 list_add(&root->root_list, &cgroup_roots);
1675 cgroup_root_count++;
1677 sb->s_root->d_fsdata = root_cgrp;
1678 root->top_cgroup.dentry = sb->s_root;
1680 /* Link the top cgroup in this hierarchy into all
1681 * the css_set objects */
1682 write_lock(&css_set_lock);
1683 hash_for_each(css_set_table, i, cset, hlist)
1684 link_css_set(&tmp_links, cset, root_cgrp);
1685 write_unlock(&css_set_lock);
1687 free_cgrp_cset_links(&tmp_links);
1689 BUG_ON(!list_empty(&root_cgrp->children));
1690 BUG_ON(root->number_of_cgroups != 1);
1692 cred = override_creds(&init_cred);
1693 cgroup_populate_dir(root_cgrp, true, root->subsys_mask);
1695 mutex_unlock(&cgroup_root_mutex);
1696 mutex_unlock(&cgroup_mutex);
1697 mutex_unlock(&inode->i_mutex);
1700 * We re-used an existing hierarchy - the new root (if
1701 * any) is not needed
1703 cgroup_free_root(opts.new_root);
1705 if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) {
1706 if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) {
1707 pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
1709 goto drop_new_super;
1711 pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n");
1715 /* no subsys rebinding, so refcounts don't change */
1716 drop_parsed_module_refcounts(opts.subsys_mask);
1719 kfree(opts.release_agent);
1721 return dget(sb->s_root);
1724 cgroup_exit_root_id(root);
1725 mutex_unlock(&cgroup_root_mutex);
1726 mutex_unlock(&cgroup_mutex);
1727 mutex_unlock(&inode->i_mutex);
1729 deactivate_locked_super(sb);
1731 drop_parsed_module_refcounts(opts.subsys_mask);
1733 kfree(opts.release_agent);
1735 return ERR_PTR(ret);
1738 static void cgroup_kill_sb(struct super_block *sb) {
1739 struct cgroupfs_root *root = sb->s_fs_info;
1740 struct cgroup *cgrp = &root->top_cgroup;
1741 struct cgrp_cset_link *link, *tmp_link;
1746 BUG_ON(root->number_of_cgroups != 1);
1747 BUG_ON(!list_empty(&cgrp->children));
1749 mutex_lock(&cgroup_mutex);
1750 mutex_lock(&cgroup_root_mutex);
1752 /* Rebind all subsystems back to the default hierarchy */
1753 if (root->flags & CGRP_ROOT_SUBSYS_BOUND) {
1754 ret = rebind_subsystems(root, 0, root->subsys_mask);
1755 /* Shouldn't be able to fail ... */
1760 * Release all the links from cset_links to this hierarchy's
1763 write_lock(&css_set_lock);
1765 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
1766 list_del(&link->cset_link);
1767 list_del(&link->cgrp_link);
1770 write_unlock(&css_set_lock);
1772 if (!list_empty(&root->root_list)) {
1773 list_del(&root->root_list);
1774 cgroup_root_count--;
1777 cgroup_exit_root_id(root);
1779 mutex_unlock(&cgroup_root_mutex);
1780 mutex_unlock(&cgroup_mutex);
1782 simple_xattrs_free(&cgrp->xattrs);
1784 kill_litter_super(sb);
1785 cgroup_free_root(root);
1788 static struct file_system_type cgroup_fs_type = {
1790 .mount = cgroup_mount,
1791 .kill_sb = cgroup_kill_sb,
1794 static struct kobject *cgroup_kobj;
1797 * cgroup_path - generate the path of a cgroup
1798 * @cgrp: the cgroup in question
1799 * @buf: the buffer to write the path into
1800 * @buflen: the length of the buffer
1802 * Writes path of cgroup into buf. Returns 0 on success, -errno on error.
1804 * We can't generate cgroup path using dentry->d_name, as accessing
1805 * dentry->name must be protected by irq-unsafe dentry->d_lock or parent
1806 * inode's i_mutex, while on the other hand cgroup_path() can be called
1807 * with some irq-safe spinlocks held.
1809 int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
1811 int ret = -ENAMETOOLONG;
1814 if (!cgrp->parent) {
1815 if (strlcpy(buf, "/", buflen) >= buflen)
1816 return -ENAMETOOLONG;
1820 start = buf + buflen - 1;
1825 const char *name = cgroup_name(cgrp);
1829 if ((start -= len) < buf)
1831 memcpy(start, name, len);
1837 cgrp = cgrp->parent;
1838 } while (cgrp->parent);
1840 memmove(buf, start, buf + buflen - start);
1845 EXPORT_SYMBOL_GPL(cgroup_path);
1848 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
1849 * @task: target task
1850 * @buf: the buffer to write the path into
1851 * @buflen: the length of the buffer
1853 * Determine @task's cgroup on the first (the one with the lowest non-zero
1854 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
1855 * function grabs cgroup_mutex and shouldn't be used inside locks used by
1856 * cgroup controller callbacks.
1858 * Returns 0 on success, fails with -%ENAMETOOLONG if @buflen is too short.
1860 int task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
1862 struct cgroupfs_root *root;
1863 struct cgroup *cgrp;
1864 int hierarchy_id = 1, ret = 0;
1867 return -ENAMETOOLONG;
1869 mutex_lock(&cgroup_mutex);
1871 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
1874 cgrp = task_cgroup_from_root(task, root);
1875 ret = cgroup_path(cgrp, buf, buflen);
1877 /* if no hierarchy exists, everyone is in "/" */
1878 memcpy(buf, "/", 2);
1881 mutex_unlock(&cgroup_mutex);
1884 EXPORT_SYMBOL_GPL(task_cgroup_path);
1887 * Control Group taskset
1889 struct task_and_cgroup {
1890 struct task_struct *task;
1891 struct cgroup *cgrp;
1895 struct cgroup_taskset {
1896 struct task_and_cgroup single;
1897 struct flex_array *tc_array;
1900 struct cgroup *cur_cgrp;
1904 * cgroup_taskset_first - reset taskset and return the first task
1905 * @tset: taskset of interest
1907 * @tset iteration is initialized and the first task is returned.
1909 struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1911 if (tset->tc_array) {
1913 return cgroup_taskset_next(tset);
1915 tset->cur_cgrp = tset->single.cgrp;
1916 return tset->single.task;
1919 EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1922 * cgroup_taskset_next - iterate to the next task in taskset
1923 * @tset: taskset of interest
1925 * Return the next task in @tset. Iteration must have been initialized
1926 * with cgroup_taskset_first().
1928 struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1930 struct task_and_cgroup *tc;
1932 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1935 tc = flex_array_get(tset->tc_array, tset->idx++);
1936 tset->cur_cgrp = tc->cgrp;
1939 EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1942 * cgroup_taskset_cur_cgroup - return the matching cgroup for the current task
1943 * @tset: taskset of interest
1945 * Return the cgroup for the current (last returned) task of @tset. This
1946 * function must be preceded by either cgroup_taskset_first() or
1947 * cgroup_taskset_next().
1949 struct cgroup *cgroup_taskset_cur_cgroup(struct cgroup_taskset *tset)
1951 return tset->cur_cgrp;
1953 EXPORT_SYMBOL_GPL(cgroup_taskset_cur_cgroup);
1956 * cgroup_taskset_size - return the number of tasks in taskset
1957 * @tset: taskset of interest
1959 int cgroup_taskset_size(struct cgroup_taskset *tset)
1961 return tset->tc_array ? tset->tc_array_len : 1;
1963 EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1967 * cgroup_task_migrate - move a task from one cgroup to another.
1969 * Must be called with cgroup_mutex and threadgroup locked.
1971 static void cgroup_task_migrate(struct cgroup *old_cgrp,
1972 struct task_struct *tsk,
1973 struct css_set *new_cset)
1975 struct css_set *old_cset;
1978 * We are synchronized through threadgroup_lock() against PF_EXITING
1979 * setting such that we can't race against cgroup_exit() changing the
1980 * css_set to init_css_set and dropping the old one.
1982 WARN_ON_ONCE(tsk->flags & PF_EXITING);
1983 old_cset = task_css_set(tsk);
1986 rcu_assign_pointer(tsk->cgroups, new_cset);
1989 /* Update the css_set linked lists if we're using them */
1990 write_lock(&css_set_lock);
1991 if (!list_empty(&tsk->cg_list))
1992 list_move(&tsk->cg_list, &new_cset->tasks);
1993 write_unlock(&css_set_lock);
1996 * We just gained a reference on old_cset by taking it from the
1997 * task. As trading it for new_cset is protected by cgroup_mutex,
1998 * we're safe to drop it here; it will be freed under RCU.
2000 set_bit(CGRP_RELEASABLE, &old_cgrp->flags);
2001 put_css_set(old_cset);
2005 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
2006 * @cgrp: the cgroup to attach to
2007 * @tsk: the task or the leader of the threadgroup to be attached
2008 * @threadgroup: attach the whole threadgroup?
2010 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
2011 * task_lock of @tsk or each thread in the threadgroup individually in turn.
2013 static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk,
2016 int retval, i, group_size;
2017 struct cgroup_subsys *ss, *failed_ss = NULL;
2018 struct cgroupfs_root *root = cgrp->root;
2019 /* threadgroup list cursor and array */
2020 struct task_struct *leader = tsk;
2021 struct task_and_cgroup *tc;
2022 struct flex_array *group;
2023 struct cgroup_taskset tset = { };
2026 * step 0: in order to do expensive, possibly blocking operations for
2027 * every thread, we cannot iterate the thread group list, since it needs
2028 * rcu or tasklist locked. instead, build an array of all threads in the
2029 * group - group_rwsem prevents new threads from appearing, and if
2030 * threads exit, this will just be an over-estimate.
2033 group_size = get_nr_threads(tsk);
2036 /* flex_array supports very large thread-groups better than kmalloc. */
2037 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
2040 /* pre-allocate to guarantee space while iterating in rcu read-side. */
2041 retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL);
2043 goto out_free_group_list;
2047 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2048 * already PF_EXITING could be freed from underneath us unless we
2049 * take an rcu_read_lock.
2053 struct task_and_cgroup ent;
2055 /* @tsk either already exited or can't exit until the end */
2056 if (tsk->flags & PF_EXITING)
2059 /* as per above, nr_threads may decrease, but not increase. */
2060 BUG_ON(i >= group_size);
2062 ent.cgrp = task_cgroup_from_root(tsk, root);
2063 /* nothing to do if this task is already in the cgroup */
2064 if (ent.cgrp == cgrp)
2067 * saying GFP_ATOMIC has no effect here because we did prealloc
2068 * earlier, but it's good form to communicate our expectations.
2070 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
2071 BUG_ON(retval != 0);
2076 } while_each_thread(leader, tsk);
2078 /* remember the number of threads in the array for later. */
2080 tset.tc_array = group;
2081 tset.tc_array_len = group_size;
2083 /* methods shouldn't be called if no task is actually migrating */
2086 goto out_free_group_list;
2089 * step 1: check that we can legitimately attach to the cgroup.
2091 for_each_root_subsys(root, ss) {
2092 if (ss->can_attach) {
2093 retval = ss->can_attach(cgrp, &tset);
2096 goto out_cancel_attach;
2102 * step 2: make sure css_sets exist for all threads to be migrated.
2103 * we use find_css_set, which allocates a new one if necessary.
2105 for (i = 0; i < group_size; i++) {
2106 struct css_set *old_cset;
2108 tc = flex_array_get(group, i);
2109 old_cset = task_css_set(tc->task);
2110 tc->cg = find_css_set(old_cset, cgrp);
2113 goto out_put_css_set_refs;
2118 * step 3: now that we're guaranteed success wrt the css_sets,
2119 * proceed to move all tasks to the new cgroup. There are no
2120 * failure cases after here, so this is the commit point.
2122 for (i = 0; i < group_size; i++) {
2123 tc = flex_array_get(group, i);
2124 cgroup_task_migrate(tc->cgrp, tc->task, tc->cg);
2126 /* nothing is sensitive to fork() after this point. */
2129 * step 4: do subsystem attach callbacks.
2131 for_each_root_subsys(root, ss) {
2133 ss->attach(cgrp, &tset);
2137 * step 5: success! and cleanup
2140 out_put_css_set_refs:
2142 for (i = 0; i < group_size; i++) {
2143 tc = flex_array_get(group, i);
2146 put_css_set(tc->cg);
2151 for_each_root_subsys(root, ss) {
2152 if (ss == failed_ss)
2154 if (ss->cancel_attach)
2155 ss->cancel_attach(cgrp, &tset);
2158 out_free_group_list:
2159 flex_array_free(group);
2164 * Find the task_struct of the task to attach by vpid and pass it along to the
2165 * function to attach either it or all tasks in its threadgroup. Will lock
2166 * cgroup_mutex and threadgroup; may take task_lock of task.
2168 static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
2170 struct task_struct *tsk;
2171 const struct cred *cred = current_cred(), *tcred;
2174 if (!cgroup_lock_live_group(cgrp))
2180 tsk = find_task_by_vpid(pid);
2184 goto out_unlock_cgroup;
2187 * even if we're attaching all tasks in the thread group, we
2188 * only need to check permissions on one of them.
2190 tcred = __task_cred(tsk);
2191 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2192 !uid_eq(cred->euid, tcred->uid) &&
2193 !uid_eq(cred->euid, tcred->suid)) {
2196 goto out_unlock_cgroup;
2202 tsk = tsk->group_leader;
2205 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
2206 * trapped in a cpuset, or RT worker may be born in a cgroup
2207 * with no rt_runtime allocated. Just say no.
2209 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
2212 goto out_unlock_cgroup;
2215 get_task_struct(tsk);
2218 threadgroup_lock(tsk);
2220 if (!thread_group_leader(tsk)) {
2222 * a race with de_thread from another thread's exec()
2223 * may strip us of our leadership, if this happens,
2224 * there is no choice but to throw this task away and
2225 * try again; this is
2226 * "double-double-toil-and-trouble-check locking".
2228 threadgroup_unlock(tsk);
2229 put_task_struct(tsk);
2230 goto retry_find_task;
2234 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2236 threadgroup_unlock(tsk);
2238 put_task_struct(tsk);
2240 mutex_unlock(&cgroup_mutex);
2245 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2246 * @from: attach to all cgroups of a given task
2247 * @tsk: the task to be attached
2249 int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2251 struct cgroupfs_root *root;
2254 mutex_lock(&cgroup_mutex);
2255 for_each_active_root(root) {
2256 struct cgroup *from_cg = task_cgroup_from_root(from, root);
2258 retval = cgroup_attach_task(from_cg, tsk, false);
2262 mutex_unlock(&cgroup_mutex);
2266 EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2268 static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid)
2270 return attach_task_by_pid(cgrp, pid, false);
2273 static int cgroup_procs_write(struct cgroup *cgrp, struct cftype *cft, u64 tgid)
2275 return attach_task_by_pid(cgrp, tgid, true);
2278 static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
2281 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
2282 if (strlen(buffer) >= PATH_MAX)
2284 if (!cgroup_lock_live_group(cgrp))
2286 mutex_lock(&cgroup_root_mutex);
2287 strcpy(cgrp->root->release_agent_path, buffer);
2288 mutex_unlock(&cgroup_root_mutex);
2289 mutex_unlock(&cgroup_mutex);
2293 static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
2294 struct seq_file *seq)
2296 if (!cgroup_lock_live_group(cgrp))
2298 seq_puts(seq, cgrp->root->release_agent_path);
2299 seq_putc(seq, '\n');
2300 mutex_unlock(&cgroup_mutex);
2304 static int cgroup_sane_behavior_show(struct cgroup *cgrp, struct cftype *cft,
2305 struct seq_file *seq)
2307 seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp));
2311 /* A buffer size big enough for numbers or short strings */
2312 #define CGROUP_LOCAL_BUFFER_SIZE 64
2314 static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
2316 const char __user *userbuf,
2317 size_t nbytes, loff_t *unused_ppos)
2319 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
2325 if (nbytes >= sizeof(buffer))
2327 if (copy_from_user(buffer, userbuf, nbytes))
2330 buffer[nbytes] = 0; /* nul-terminate */
2331 if (cft->write_u64) {
2332 u64 val = simple_strtoull(strstrip(buffer), &end, 0);
2335 retval = cft->write_u64(cgrp, cft, val);
2337 s64 val = simple_strtoll(strstrip(buffer), &end, 0);
2340 retval = cft->write_s64(cgrp, cft, val);
2347 static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
2349 const char __user *userbuf,
2350 size_t nbytes, loff_t *unused_ppos)
2352 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
2354 size_t max_bytes = cft->max_write_len;
2355 char *buffer = local_buffer;
2358 max_bytes = sizeof(local_buffer) - 1;
2359 if (nbytes >= max_bytes)
2361 /* Allocate a dynamic buffer if we need one */
2362 if (nbytes >= sizeof(local_buffer)) {
2363 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
2367 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
2372 buffer[nbytes] = 0; /* nul-terminate */
2373 retval = cft->write_string(cgrp, cft, strstrip(buffer));
2377 if (buffer != local_buffer)
2382 static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
2383 size_t nbytes, loff_t *ppos)
2385 struct cftype *cft = __d_cft(file->f_dentry);
2386 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
2388 if (cgroup_is_dead(cgrp))
2391 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
2392 if (cft->write_u64 || cft->write_s64)
2393 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
2394 if (cft->write_string)
2395 return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
2397 int ret = cft->trigger(cgrp, (unsigned int)cft->private);
2398 return ret ? ret : nbytes;
2403 static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
2405 char __user *buf, size_t nbytes,
2408 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
2409 u64 val = cft->read_u64(cgrp, cft);
2410 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
2412 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2415 static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
2417 char __user *buf, size_t nbytes,
2420 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
2421 s64 val = cft->read_s64(cgrp, cft);
2422 int len = sprintf(tmp, "%lld\n", (long long) val);
2424 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2427 static ssize_t cgroup_file_read(struct file *file, char __user *buf,
2428 size_t nbytes, loff_t *ppos)
2430 struct cftype *cft = __d_cft(file->f_dentry);
2431 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
2433 if (cgroup_is_dead(cgrp))
2437 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
2439 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
2441 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
2446 * seqfile ops/methods for returning structured data. Currently just
2447 * supports string->u64 maps, but can be extended in future.
2450 struct cgroup_seqfile_state {
2452 struct cgroup *cgroup;
2455 static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
2457 struct seq_file *sf = cb->state;
2458 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
2461 static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2463 struct cgroup_seqfile_state *state = m->private;
2464 struct cftype *cft = state->cft;
2465 if (cft->read_map) {
2466 struct cgroup_map_cb cb = {
2467 .fill = cgroup_map_add,
2470 return cft->read_map(state->cgroup, cft, &cb);
2472 return cft->read_seq_string(state->cgroup, cft, m);
2475 static int cgroup_seqfile_release(struct inode *inode, struct file *file)
2477 struct seq_file *seq = file->private_data;
2478 kfree(seq->private);
2479 return single_release(inode, file);
2482 static const struct file_operations cgroup_seqfile_operations = {
2484 .write = cgroup_file_write,
2485 .llseek = seq_lseek,
2486 .release = cgroup_seqfile_release,
2489 static int cgroup_file_open(struct inode *inode, struct file *file)
2494 err = generic_file_open(inode, file);
2497 cft = __d_cft(file->f_dentry);
2499 if (cft->read_map || cft->read_seq_string) {
2500 struct cgroup_seqfile_state *state;
2502 state = kzalloc(sizeof(*state), GFP_USER);
2507 state->cgroup = __d_cgrp(file->f_dentry->d_parent);
2508 file->f_op = &cgroup_seqfile_operations;
2509 err = single_open(file, cgroup_seqfile_show, state);
2512 } else if (cft->open)
2513 err = cft->open(inode, file);
2520 static int cgroup_file_release(struct inode *inode, struct file *file)
2522 struct cftype *cft = __d_cft(file->f_dentry);
2524 return cft->release(inode, file);
2529 * cgroup_rename - Only allow simple rename of directories in place.
2531 static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2532 struct inode *new_dir, struct dentry *new_dentry)
2535 struct cgroup_name *name, *old_name;
2536 struct cgroup *cgrp;
2539 * It's convinient to use parent dir's i_mutex to protected
2542 lockdep_assert_held(&old_dir->i_mutex);
2544 if (!S_ISDIR(old_dentry->d_inode->i_mode))
2546 if (new_dentry->d_inode)
2548 if (old_dir != new_dir)
2551 cgrp = __d_cgrp(old_dentry);
2554 * This isn't a proper migration and its usefulness is very
2555 * limited. Disallow if sane_behavior.
2557 if (cgroup_sane_behavior(cgrp))
2560 name = cgroup_alloc_name(new_dentry);
2564 ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2570 old_name = rcu_dereference_protected(cgrp->name, true);
2571 rcu_assign_pointer(cgrp->name, name);
2573 kfree_rcu(old_name, rcu_head);
2577 static struct simple_xattrs *__d_xattrs(struct dentry *dentry)
2579 if (S_ISDIR(dentry->d_inode->i_mode))
2580 return &__d_cgrp(dentry)->xattrs;
2582 return &__d_cfe(dentry)->xattrs;
2585 static inline int xattr_enabled(struct dentry *dentry)
2587 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
2588 return root->flags & CGRP_ROOT_XATTR;
2591 static bool is_valid_xattr(const char *name)
2593 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
2594 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
2599 static int cgroup_setxattr(struct dentry *dentry, const char *name,
2600 const void *val, size_t size, int flags)
2602 if (!xattr_enabled(dentry))
2604 if (!is_valid_xattr(name))
2606 return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags);
2609 static int cgroup_removexattr(struct dentry *dentry, const char *name)
2611 if (!xattr_enabled(dentry))
2613 if (!is_valid_xattr(name))
2615 return simple_xattr_remove(__d_xattrs(dentry), name);
2618 static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name,
2619 void *buf, size_t size)
2621 if (!xattr_enabled(dentry))
2623 if (!is_valid_xattr(name))
2625 return simple_xattr_get(__d_xattrs(dentry), name, buf, size);
2628 static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size)
2630 if (!xattr_enabled(dentry))
2632 return simple_xattr_list(__d_xattrs(dentry), buf, size);
2635 static const struct file_operations cgroup_file_operations = {
2636 .read = cgroup_file_read,
2637 .write = cgroup_file_write,
2638 .llseek = generic_file_llseek,
2639 .open = cgroup_file_open,
2640 .release = cgroup_file_release,
2643 static const struct inode_operations cgroup_file_inode_operations = {
2644 .setxattr = cgroup_setxattr,
2645 .getxattr = cgroup_getxattr,
2646 .listxattr = cgroup_listxattr,
2647 .removexattr = cgroup_removexattr,
2650 static const struct inode_operations cgroup_dir_inode_operations = {
2651 .lookup = simple_lookup,
2652 .mkdir = cgroup_mkdir,
2653 .rmdir = cgroup_rmdir,
2654 .rename = cgroup_rename,
2655 .setxattr = cgroup_setxattr,
2656 .getxattr = cgroup_getxattr,
2657 .listxattr = cgroup_listxattr,
2658 .removexattr = cgroup_removexattr,
2662 * Check if a file is a control file
2664 static inline struct cftype *__file_cft(struct file *file)
2666 if (file_inode(file)->i_fop != &cgroup_file_operations)
2667 return ERR_PTR(-EINVAL);
2668 return __d_cft(file->f_dentry);
2671 static int cgroup_create_file(struct dentry *dentry, umode_t mode,
2672 struct super_block *sb)
2674 struct inode *inode;
2678 if (dentry->d_inode)
2681 inode = cgroup_new_inode(mode, sb);
2685 if (S_ISDIR(mode)) {
2686 inode->i_op = &cgroup_dir_inode_operations;
2687 inode->i_fop = &simple_dir_operations;
2689 /* start off with i_nlink == 2 (for "." entry) */
2691 inc_nlink(dentry->d_parent->d_inode);
2694 * Control reaches here with cgroup_mutex held.
2695 * @inode->i_mutex should nest outside cgroup_mutex but we
2696 * want to populate it immediately without releasing
2697 * cgroup_mutex. As @inode isn't visible to anyone else
2698 * yet, trylock will always succeed without affecting
2701 WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex));
2702 } else if (S_ISREG(mode)) {
2704 inode->i_fop = &cgroup_file_operations;
2705 inode->i_op = &cgroup_file_inode_operations;
2707 d_instantiate(dentry, inode);
2708 dget(dentry); /* Extra count - pin the dentry in core */
2713 * cgroup_file_mode - deduce file mode of a control file
2714 * @cft: the control file in question
2716 * returns cft->mode if ->mode is not 0
2717 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2718 * returns S_IRUGO if it has only a read handler
2719 * returns S_IWUSR if it has only a write hander
2721 static umode_t cgroup_file_mode(const struct cftype *cft)
2728 if (cft->read || cft->read_u64 || cft->read_s64 ||
2729 cft->read_map || cft->read_seq_string)
2732 if (cft->write || cft->write_u64 || cft->write_s64 ||
2733 cft->write_string || cft->trigger)
2739 static int cgroup_add_file(struct cgroup *cgrp, struct cgroup_subsys *subsys,
2742 struct dentry *dir = cgrp->dentry;
2743 struct cgroup *parent = __d_cgrp(dir);
2744 struct dentry *dentry;
2748 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
2750 if (subsys && !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) {
2751 strcpy(name, subsys->name);
2754 strcat(name, cft->name);
2756 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
2758 cfe = kzalloc(sizeof(*cfe), GFP_KERNEL);
2762 dentry = lookup_one_len(name, dir, strlen(name));
2763 if (IS_ERR(dentry)) {
2764 error = PTR_ERR(dentry);
2768 cfe->type = (void *)cft;
2769 cfe->dentry = dentry;
2770 dentry->d_fsdata = cfe;
2771 simple_xattrs_init(&cfe->xattrs);
2773 mode = cgroup_file_mode(cft);
2774 error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb);
2776 list_add_tail(&cfe->node, &parent->files);
2785 static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys,
2786 struct cftype cfts[], bool is_add)
2791 for (cft = cfts; cft->name[0] != '\0'; cft++) {
2792 /* does cft->flags tell us to skip this file on @cgrp? */
2793 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2795 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2797 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2801 err = cgroup_add_file(cgrp, subsys, cft);
2803 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
2807 cgroup_rm_file(cgrp, cft);
2813 static void cgroup_cfts_prepare(void)
2814 __acquires(&cgroup_mutex)
2817 * Thanks to the entanglement with vfs inode locking, we can't walk
2818 * the existing cgroups under cgroup_mutex and create files.
2819 * Instead, we use cgroup_for_each_descendant_pre() and drop RCU
2820 * read lock before calling cgroup_addrm_files().
2822 mutex_lock(&cgroup_mutex);
2825 static void cgroup_cfts_commit(struct cgroup_subsys *ss,
2826 struct cftype *cfts, bool is_add)
2827 __releases(&cgroup_mutex)
2830 struct cgroup *cgrp, *root = &ss->root->top_cgroup;
2831 struct super_block *sb = ss->root->sb;
2832 struct dentry *prev = NULL;
2833 struct inode *inode;
2836 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
2837 if (!cfts || ss->root == &cgroup_dummy_root ||
2838 !atomic_inc_not_zero(&sb->s_active)) {
2839 mutex_unlock(&cgroup_mutex);
2844 * All cgroups which are created after we drop cgroup_mutex will
2845 * have the updated set of files, so we only need to update the
2846 * cgroups created before the current @cgroup_serial_nr_next.
2848 update_before = cgroup_serial_nr_next;
2850 mutex_unlock(&cgroup_mutex);
2852 /* @root always needs to be updated */
2853 inode = root->dentry->d_inode;
2854 mutex_lock(&inode->i_mutex);
2855 mutex_lock(&cgroup_mutex);
2856 cgroup_addrm_files(root, ss, cfts, is_add);
2857 mutex_unlock(&cgroup_mutex);
2858 mutex_unlock(&inode->i_mutex);
2860 /* add/rm files for all cgroups created before */
2862 cgroup_for_each_descendant_pre(cgrp, root) {
2863 if (cgroup_is_dead(cgrp))
2866 inode = cgrp->dentry->d_inode;
2871 prev = cgrp->dentry;
2873 mutex_lock(&inode->i_mutex);
2874 mutex_lock(&cgroup_mutex);
2875 if (cgrp->serial_nr < update_before && !cgroup_is_dead(cgrp))
2876 cgroup_addrm_files(cgrp, ss, cfts, is_add);
2877 mutex_unlock(&cgroup_mutex);
2878 mutex_unlock(&inode->i_mutex);
2884 deactivate_super(sb);
2888 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2889 * @ss: target cgroup subsystem
2890 * @cfts: zero-length name terminated array of cftypes
2892 * Register @cfts to @ss. Files described by @cfts are created for all
2893 * existing cgroups to which @ss is attached and all future cgroups will
2894 * have them too. This function can be called anytime whether @ss is
2897 * Returns 0 on successful registration, -errno on failure. Note that this
2898 * function currently returns 0 as long as @cfts registration is successful
2899 * even if some file creation attempts on existing cgroups fail.
2901 int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
2903 struct cftype_set *set;
2905 set = kzalloc(sizeof(*set), GFP_KERNEL);
2909 cgroup_cfts_prepare();
2911 list_add_tail(&set->node, &ss->cftsets);
2912 cgroup_cfts_commit(ss, cfts, true);
2916 EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2919 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
2920 * @ss: target cgroup subsystem
2921 * @cfts: zero-length name terminated array of cftypes
2923 * Unregister @cfts from @ss. Files described by @cfts are removed from
2924 * all existing cgroups to which @ss is attached and all future cgroups
2925 * won't have them either. This function can be called anytime whether @ss
2926 * is attached or not.
2928 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
2929 * registered with @ss.
2931 int cgroup_rm_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
2933 struct cftype_set *set;
2935 cgroup_cfts_prepare();
2937 list_for_each_entry(set, &ss->cftsets, node) {
2938 if (set->cfts == cfts) {
2939 list_del(&set->node);
2941 cgroup_cfts_commit(ss, cfts, false);
2946 cgroup_cfts_commit(ss, NULL, false);
2951 * cgroup_task_count - count the number of tasks in a cgroup.
2952 * @cgrp: the cgroup in question
2954 * Return the number of tasks in the cgroup.
2956 int cgroup_task_count(const struct cgroup *cgrp)
2959 struct cgrp_cset_link *link;
2961 read_lock(&css_set_lock);
2962 list_for_each_entry(link, &cgrp->cset_links, cset_link)
2963 count += atomic_read(&link->cset->refcount);
2964 read_unlock(&css_set_lock);
2969 * Advance a list_head iterator. The iterator should be positioned at
2970 * the start of a css_set
2972 static void cgroup_advance_iter(struct cgroup *cgrp, struct cgroup_iter *it)
2974 struct list_head *l = it->cset_link;
2975 struct cgrp_cset_link *link;
2976 struct css_set *cset;
2978 /* Advance to the next non-empty css_set */
2981 if (l == &cgrp->cset_links) {
2982 it->cset_link = NULL;
2985 link = list_entry(l, struct cgrp_cset_link, cset_link);
2987 } while (list_empty(&cset->tasks));
2989 it->task = cset->tasks.next;
2993 * To reduce the fork() overhead for systems that are not actually
2994 * using their cgroups capability, we don't maintain the lists running
2995 * through each css_set to its tasks until we see the list actually
2996 * used - in other words after the first call to cgroup_iter_start().
2998 static void cgroup_enable_task_cg_lists(void)
3000 struct task_struct *p, *g;
3001 write_lock(&css_set_lock);
3002 use_task_css_set_links = 1;
3004 * We need tasklist_lock because RCU is not safe against
3005 * while_each_thread(). Besides, a forking task that has passed
3006 * cgroup_post_fork() without seeing use_task_css_set_links = 1
3007 * is not guaranteed to have its child immediately visible in the
3008 * tasklist if we walk through it with RCU.
3010 read_lock(&tasklist_lock);
3011 do_each_thread(g, p) {
3014 * We should check if the process is exiting, otherwise
3015 * it will race with cgroup_exit() in that the list
3016 * entry won't be deleted though the process has exited.
3018 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
3019 list_add(&p->cg_list, &task_css_set(p)->tasks);
3021 } while_each_thread(g, p);
3022 read_unlock(&tasklist_lock);
3023 write_unlock(&css_set_lock);
3027 * cgroup_next_sibling - find the next sibling of a given cgroup
3028 * @pos: the current cgroup
3030 * This function returns the next sibling of @pos and should be called
3031 * under RCU read lock. The only requirement is that @pos is accessible.
3032 * The next sibling is guaranteed to be returned regardless of @pos's
3035 struct cgroup *cgroup_next_sibling(struct cgroup *pos)
3037 struct cgroup *next;
3039 WARN_ON_ONCE(!rcu_read_lock_held());
3042 * @pos could already have been removed. Once a cgroup is removed,
3043 * its ->sibling.next is no longer updated when its next sibling
3044 * changes. As CGRP_DEAD assertion is serialized and happens
3045 * before the cgroup is taken off the ->sibling list, if we see it
3046 * unasserted, it's guaranteed that the next sibling hasn't
3047 * finished its grace period even if it's already removed, and thus
3048 * safe to dereference from this RCU critical section. If
3049 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
3050 * to be visible as %true here.
3052 if (likely(!cgroup_is_dead(pos))) {
3053 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
3054 if (&next->sibling != &pos->parent->children)
3060 * Can't dereference the next pointer. Each cgroup is given a
3061 * monotonically increasing unique serial number and always
3062 * appended to the sibling list, so the next one can be found by
3063 * walking the parent's children until we see a cgroup with higher
3064 * serial number than @pos's.
3066 * While this path can be slow, it's taken only when either the
3067 * current cgroup is removed or iteration and removal race.
3069 list_for_each_entry_rcu(next, &pos->parent->children, sibling)
3070 if (next->serial_nr > pos->serial_nr)
3074 EXPORT_SYMBOL_GPL(cgroup_next_sibling);
3077 * cgroup_next_descendant_pre - find the next descendant for pre-order walk
3078 * @pos: the current position (%NULL to initiate traversal)
3079 * @cgroup: cgroup whose descendants to walk
3081 * To be used by cgroup_for_each_descendant_pre(). Find the next
3082 * descendant to visit for pre-order traversal of @cgroup's descendants.
3084 * While this function requires RCU read locking, it doesn't require the
3085 * whole traversal to be contained in a single RCU critical section. This
3086 * function will return the correct next descendant as long as both @pos
3087 * and @cgroup are accessible and @pos is a descendant of @cgroup.
3089 struct cgroup *cgroup_next_descendant_pre(struct cgroup *pos,
3090 struct cgroup *cgroup)
3092 struct cgroup *next;
3094 WARN_ON_ONCE(!rcu_read_lock_held());
3096 /* if first iteration, pretend we just visited @cgroup */
3100 /* visit the first child if exists */
3101 next = list_first_or_null_rcu(&pos->children, struct cgroup, sibling);
3105 /* no child, visit my or the closest ancestor's next sibling */
3106 while (pos != cgroup) {
3107 next = cgroup_next_sibling(pos);
3115 EXPORT_SYMBOL_GPL(cgroup_next_descendant_pre);
3118 * cgroup_rightmost_descendant - return the rightmost descendant of a cgroup
3119 * @pos: cgroup of interest
3121 * Return the rightmost descendant of @pos. If there's no descendant,
3122 * @pos is returned. This can be used during pre-order traversal to skip
3125 * While this function requires RCU read locking, it doesn't require the
3126 * whole traversal to be contained in a single RCU critical section. This
3127 * function will return the correct rightmost descendant as long as @pos is
3130 struct cgroup *cgroup_rightmost_descendant(struct cgroup *pos)
3132 struct cgroup *last, *tmp;
3134 WARN_ON_ONCE(!rcu_read_lock_held());
3138 /* ->prev isn't RCU safe, walk ->next till the end */
3140 list_for_each_entry_rcu(tmp, &last->children, sibling)
3146 EXPORT_SYMBOL_GPL(cgroup_rightmost_descendant);
3148 static struct cgroup *cgroup_leftmost_descendant(struct cgroup *pos)
3150 struct cgroup *last;
3154 pos = list_first_or_null_rcu(&pos->children, struct cgroup,
3162 * cgroup_next_descendant_post - find the next descendant for post-order walk
3163 * @pos: the current position (%NULL to initiate traversal)
3164 * @cgroup: cgroup whose descendants to walk
3166 * To be used by cgroup_for_each_descendant_post(). Find the next
3167 * descendant to visit for post-order traversal of @cgroup's descendants.
3169 * While this function requires RCU read locking, it doesn't require the
3170 * whole traversal to be contained in a single RCU critical section. This
3171 * function will return the correct next descendant as long as both @pos
3172 * and @cgroup are accessible and @pos is a descendant of @cgroup.
3174 struct cgroup *cgroup_next_descendant_post(struct cgroup *pos,
3175 struct cgroup *cgroup)
3177 struct cgroup *next;
3179 WARN_ON_ONCE(!rcu_read_lock_held());
3181 /* if first iteration, visit the leftmost descendant */
3183 next = cgroup_leftmost_descendant(cgroup);
3184 return next != cgroup ? next : NULL;
3187 /* if there's an unvisited sibling, visit its leftmost descendant */
3188 next = cgroup_next_sibling(pos);
3190 return cgroup_leftmost_descendant(next);
3192 /* no sibling left, visit parent */
3194 return next != cgroup ? next : NULL;
3196 EXPORT_SYMBOL_GPL(cgroup_next_descendant_post);
3198 void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
3199 __acquires(css_set_lock)
3202 * The first time anyone tries to iterate across a cgroup,
3203 * we need to enable the list linking each css_set to its
3204 * tasks, and fix up all existing tasks.
3206 if (!use_task_css_set_links)
3207 cgroup_enable_task_cg_lists();
3209 read_lock(&css_set_lock);
3210 it->cset_link = &cgrp->cset_links;
3211 cgroup_advance_iter(cgrp, it);
3214 struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
3215 struct cgroup_iter *it)
3217 struct task_struct *res;
3218 struct list_head *l = it->task;
3219 struct cgrp_cset_link *link;
3221 /* If the iterator cg is NULL, we have no tasks */
3224 res = list_entry(l, struct task_struct, cg_list);
3225 /* Advance iterator to find next entry */
3227 link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link);
3228 if (l == &link->cset->tasks) {
3229 /* We reached the end of this task list - move on to
3230 * the next cg_cgroup_link */
3231 cgroup_advance_iter(cgrp, it);
3238 void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
3239 __releases(css_set_lock)
3241 read_unlock(&css_set_lock);
3244 static inline int started_after_time(struct task_struct *t1,
3245 struct timespec *time,
3246 struct task_struct *t2)
3248 int start_diff = timespec_compare(&t1->start_time, time);
3249 if (start_diff > 0) {
3251 } else if (start_diff < 0) {
3255 * Arbitrarily, if two processes started at the same
3256 * time, we'll say that the lower pointer value
3257 * started first. Note that t2 may have exited by now
3258 * so this may not be a valid pointer any longer, but
3259 * that's fine - it still serves to distinguish
3260 * between two tasks started (effectively) simultaneously.
3267 * This function is a callback from heap_insert() and is used to order
3269 * In this case we order the heap in descending task start time.
3271 static inline int started_after(void *p1, void *p2)
3273 struct task_struct *t1 = p1;
3274 struct task_struct *t2 = p2;
3275 return started_after_time(t1, &t2->start_time, t2);
3279 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
3280 * @scan: struct cgroup_scanner containing arguments for the scan
3282 * Arguments include pointers to callback functions test_task() and
3284 * Iterate through all the tasks in a cgroup, calling test_task() for each,
3285 * and if it returns true, call process_task() for it also.
3286 * The test_task pointer may be NULL, meaning always true (select all tasks).
3287 * Effectively duplicates cgroup_iter_{start,next,end}()
3288 * but does not lock css_set_lock for the call to process_task().
3289 * The struct cgroup_scanner may be embedded in any structure of the caller's
3291 * It is guaranteed that process_task() will act on every task that
3292 * is a member of the cgroup for the duration of this call. This
3293 * function may or may not call process_task() for tasks that exit
3294 * or move to a different cgroup during the call, or are forked or
3295 * move into the cgroup during the call.
3297 * Note that test_task() may be called with locks held, and may in some
3298 * situations be called multiple times for the same task, so it should
3300 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
3301 * pre-allocated and will be used for heap operations (and its "gt" member will
3302 * be overwritten), else a temporary heap will be used (allocation of which
3303 * may cause this function to fail).
3305 int cgroup_scan_tasks(struct cgroup_scanner *scan)
3308 struct cgroup_iter it;
3309 struct task_struct *p, *dropped;
3310 /* Never dereference latest_task, since it's not refcounted */
3311 struct task_struct *latest_task = NULL;
3312 struct ptr_heap tmp_heap;
3313 struct ptr_heap *heap;
3314 struct timespec latest_time = { 0, 0 };
3317 /* The caller supplied our heap and pre-allocated its memory */
3319 heap->gt = &started_after;
3321 /* We need to allocate our own heap memory */
3323 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
3325 /* cannot allocate the heap */
3331 * Scan tasks in the cgroup, using the scanner's "test_task" callback
3332 * to determine which are of interest, and using the scanner's
3333 * "process_task" callback to process any of them that need an update.
3334 * Since we don't want to hold any locks during the task updates,
3335 * gather tasks to be processed in a heap structure.
3336 * The heap is sorted by descending task start time.
3337 * If the statically-sized heap fills up, we overflow tasks that
3338 * started later, and in future iterations only consider tasks that
3339 * started after the latest task in the previous pass. This
3340 * guarantees forward progress and that we don't miss any tasks.
3343 cgroup_iter_start(scan->cg, &it);
3344 while ((p = cgroup_iter_next(scan->cg, &it))) {
3346 * Only affect tasks that qualify per the caller's callback,
3347 * if he provided one
3349 if (scan->test_task && !scan->test_task(p, scan))
3352 * Only process tasks that started after the last task
3355 if (!started_after_time(p, &latest_time, latest_task))
3357 dropped = heap_insert(heap, p);
3358 if (dropped == NULL) {
3360 * The new task was inserted; the heap wasn't
3364 } else if (dropped != p) {
3366 * The new task was inserted, and pushed out a
3370 put_task_struct(dropped);
3373 * Else the new task was newer than anything already in
3374 * the heap and wasn't inserted
3377 cgroup_iter_end(scan->cg, &it);
3380 for (i = 0; i < heap->size; i++) {
3381 struct task_struct *q = heap->ptrs[i];
3383 latest_time = q->start_time;
3386 /* Process the task per the caller's callback */
3387 scan->process_task(q, scan);
3391 * If we had to process any tasks at all, scan again
3392 * in case some of them were in the middle of forking
3393 * children that didn't get processed.
3394 * Not the most efficient way to do it, but it avoids
3395 * having to take callback_mutex in the fork path
3399 if (heap == &tmp_heap)
3400 heap_free(&tmp_heap);
3404 static void cgroup_transfer_one_task(struct task_struct *task,
3405 struct cgroup_scanner *scan)
3407 struct cgroup *new_cgroup = scan->data;
3409 mutex_lock(&cgroup_mutex);
3410 cgroup_attach_task(new_cgroup, task, false);
3411 mutex_unlock(&cgroup_mutex);
3415 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3416 * @to: cgroup to which the tasks will be moved
3417 * @from: cgroup in which the tasks currently reside
3419 int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3421 struct cgroup_scanner scan;
3424 scan.test_task = NULL; /* select all tasks in cgroup */
3425 scan.process_task = cgroup_transfer_one_task;
3429 return cgroup_scan_tasks(&scan);
3433 * Stuff for reading the 'tasks'/'procs' files.
3435 * Reading this file can return large amounts of data if a cgroup has
3436 * *lots* of attached tasks. So it may need several calls to read(),
3437 * but we cannot guarantee that the information we produce is correct
3438 * unless we produce it entirely atomically.
3442 /* which pidlist file are we talking about? */
3443 enum cgroup_filetype {
3449 * A pidlist is a list of pids that virtually represents the contents of one
3450 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3451 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3454 struct cgroup_pidlist {
3456 * used to find which pidlist is wanted. doesn't change as long as
3457 * this particular list stays in the list.
3459 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3462 /* how many elements the above list has */
3464 /* how many files are using the current array */
3466 /* each of these stored in a list by its cgroup */
3467 struct list_head links;
3468 /* pointer to the cgroup we belong to, for list removal purposes */
3469 struct cgroup *owner;
3470 /* protects the other fields */
3471 struct rw_semaphore mutex;
3475 * The following two functions "fix" the issue where there are more pids
3476 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3477 * TODO: replace with a kernel-wide solution to this problem
3479 #define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3480 static void *pidlist_allocate(int count)
3482 if (PIDLIST_TOO_LARGE(count))
3483 return vmalloc(count * sizeof(pid_t));
3485 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3487 static void pidlist_free(void *p)
3489 if (is_vmalloc_addr(p))
3496 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
3497 * Returns the number of unique elements.
3499 static int pidlist_uniq(pid_t *list, int length)
3504 * we presume the 0th element is unique, so i starts at 1. trivial
3505 * edge cases first; no work needs to be done for either
3507 if (length == 0 || length == 1)
3509 /* src and dest walk down the list; dest counts unique elements */
3510 for (src = 1; src < length; src++) {
3511 /* find next unique element */
3512 while (list[src] == list[src-1]) {
3517 /* dest always points to where the next unique element goes */
3518 list[dest] = list[src];
3525 static int cmppid(const void *a, const void *b)
3527 return *(pid_t *)a - *(pid_t *)b;
3531 * find the appropriate pidlist for our purpose (given procs vs tasks)
3532 * returns with the lock on that pidlist already held, and takes care
3533 * of the use count, or returns NULL with no locks held if we're out of
3536 static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3537 enum cgroup_filetype type)
3539 struct cgroup_pidlist *l;
3540 /* don't need task_nsproxy() if we're looking at ourself */
3541 struct pid_namespace *ns = task_active_pid_ns(current);
3544 * We can't drop the pidlist_mutex before taking the l->mutex in case
3545 * the last ref-holder is trying to remove l from the list at the same
3546 * time. Holding the pidlist_mutex precludes somebody taking whichever
3547 * list we find out from under us - compare release_pid_array().
3549 mutex_lock(&cgrp->pidlist_mutex);
3550 list_for_each_entry(l, &cgrp->pidlists, links) {
3551 if (l->key.type == type && l->key.ns == ns) {
3552 /* make sure l doesn't vanish out from under us */
3553 down_write(&l->mutex);
3554 mutex_unlock(&cgrp->pidlist_mutex);
3558 /* entry not found; create a new one */
3559 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
3561 mutex_unlock(&cgrp->pidlist_mutex);
3564 init_rwsem(&l->mutex);
3565 down_write(&l->mutex);
3567 l->key.ns = get_pid_ns(ns);
3569 list_add(&l->links, &cgrp->pidlists);
3570 mutex_unlock(&cgrp->pidlist_mutex);
3575 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3577 static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3578 struct cgroup_pidlist **lp)
3582 int pid, n = 0; /* used for populating the array */
3583 struct cgroup_iter it;
3584 struct task_struct *tsk;
3585 struct cgroup_pidlist *l;
3588 * If cgroup gets more users after we read count, we won't have
3589 * enough space - tough. This race is indistinguishable to the
3590 * caller from the case that the additional cgroup users didn't
3591 * show up until sometime later on.
3593 length = cgroup_task_count(cgrp);
3594 array = pidlist_allocate(length);
3597 /* now, populate the array */
3598 cgroup_iter_start(cgrp, &it);
3599 while ((tsk = cgroup_iter_next(cgrp, &it))) {
3600 if (unlikely(n == length))
3602 /* get tgid or pid for procs or tasks file respectively */
3603 if (type == CGROUP_FILE_PROCS)
3604 pid = task_tgid_vnr(tsk);
3606 pid = task_pid_vnr(tsk);
3607 if (pid > 0) /* make sure to only use valid results */
3610 cgroup_iter_end(cgrp, &it);
3612 /* now sort & (if procs) strip out duplicates */
3613 sort(array, length, sizeof(pid_t), cmppid, NULL);
3614 if (type == CGROUP_FILE_PROCS)
3615 length = pidlist_uniq(array, length);
3616 l = cgroup_pidlist_find(cgrp, type);
3618 pidlist_free(array);
3621 /* store array, freeing old if necessary - lock already held */
3622 pidlist_free(l->list);
3626 up_write(&l->mutex);
3632 * cgroupstats_build - build and fill cgroupstats
3633 * @stats: cgroupstats to fill information into
3634 * @dentry: A dentry entry belonging to the cgroup for which stats have
3637 * Build and fill cgroupstats so that taskstats can export it to user
3640 int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3643 struct cgroup *cgrp;
3644 struct cgroup_iter it;
3645 struct task_struct *tsk;
3648 * Validate dentry by checking the superblock operations,
3649 * and make sure it's a directory.
3651 if (dentry->d_sb->s_op != &cgroup_ops ||
3652 !S_ISDIR(dentry->d_inode->i_mode))
3656 cgrp = dentry->d_fsdata;
3658 cgroup_iter_start(cgrp, &it);
3659 while ((tsk = cgroup_iter_next(cgrp, &it))) {
3660 switch (tsk->state) {
3662 stats->nr_running++;
3664 case TASK_INTERRUPTIBLE:
3665 stats->nr_sleeping++;
3667 case TASK_UNINTERRUPTIBLE:
3668 stats->nr_uninterruptible++;
3671 stats->nr_stopped++;
3674 if (delayacct_is_task_waiting_on_io(tsk))
3675 stats->nr_io_wait++;
3679 cgroup_iter_end(cgrp, &it);
3687 * seq_file methods for the tasks/procs files. The seq_file position is the
3688 * next pid to display; the seq_file iterator is a pointer to the pid
3689 * in the cgroup->l->list array.
3692 static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
3695 * Initially we receive a position value that corresponds to
3696 * one more than the last pid shown (or 0 on the first call or
3697 * after a seek to the start). Use a binary-search to find the
3698 * next pid to display, if any
3700 struct cgroup_pidlist *l = s->private;
3701 int index = 0, pid = *pos;
3704 down_read(&l->mutex);
3706 int end = l->length;
3708 while (index < end) {
3709 int mid = (index + end) / 2;
3710 if (l->list[mid] == pid) {
3713 } else if (l->list[mid] <= pid)
3719 /* If we're off the end of the array, we're done */
3720 if (index >= l->length)
3722 /* Update the abstract position to be the actual pid that we found */
3723 iter = l->list + index;
3728 static void cgroup_pidlist_stop(struct seq_file *s, void *v)
3730 struct cgroup_pidlist *l = s->private;
3734 static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
3736 struct cgroup_pidlist *l = s->private;
3738 pid_t *end = l->list + l->length;
3740 * Advance to the next pid in the array. If this goes off the
3752 static int cgroup_pidlist_show(struct seq_file *s, void *v)
3754 return seq_printf(s, "%d\n", *(int *)v);
3758 * seq_operations functions for iterating on pidlists through seq_file -
3759 * independent of whether it's tasks or procs
3761 static const struct seq_operations cgroup_pidlist_seq_operations = {
3762 .start = cgroup_pidlist_start,
3763 .stop = cgroup_pidlist_stop,
3764 .next = cgroup_pidlist_next,
3765 .show = cgroup_pidlist_show,
3768 static void cgroup_release_pid_array(struct cgroup_pidlist *l)
3771 * the case where we're the last user of this particular pidlist will
3772 * have us remove it from the cgroup's list, which entails taking the
3773 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3774 * pidlist_mutex, we have to take pidlist_mutex first.
3776 mutex_lock(&l->owner->pidlist_mutex);
3777 down_write(&l->mutex);
3778 BUG_ON(!l->use_count);
3779 if (!--l->use_count) {
3780 /* we're the last user if refcount is 0; remove and free */
3781 list_del(&l->links);
3782 mutex_unlock(&l->owner->pidlist_mutex);
3783 pidlist_free(l->list);
3784 put_pid_ns(l->key.ns);
3785 up_write(&l->mutex);
3789 mutex_unlock(&l->owner->pidlist_mutex);
3790 up_write(&l->mutex);
3793 static int cgroup_pidlist_release(struct inode *inode, struct file *file)
3795 struct cgroup_pidlist *l;
3796 if (!(file->f_mode & FMODE_READ))
3799 * the seq_file will only be initialized if the file was opened for
3800 * reading; hence we check if it's not null only in that case.
3802 l = ((struct seq_file *)file->private_data)->private;
3803 cgroup_release_pid_array(l);
3804 return seq_release(inode, file);
3807 static const struct file_operations cgroup_pidlist_operations = {
3809 .llseek = seq_lseek,
3810 .write = cgroup_file_write,
3811 .release = cgroup_pidlist_release,
3815 * The following functions handle opens on a file that displays a pidlist
3816 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3819 /* helper function for the two below it */
3820 static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
3822 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
3823 struct cgroup_pidlist *l;
3826 /* Nothing to do for write-only files */
3827 if (!(file->f_mode & FMODE_READ))
3830 /* have the array populated */
3831 retval = pidlist_array_load(cgrp, type, &l);
3834 /* configure file information */
3835 file->f_op = &cgroup_pidlist_operations;
3837 retval = seq_open(file, &cgroup_pidlist_seq_operations);
3839 cgroup_release_pid_array(l);
3842 ((struct seq_file *)file->private_data)->private = l;
3845 static int cgroup_tasks_open(struct inode *unused, struct file *file)
3847 return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
3849 static int cgroup_procs_open(struct inode *unused, struct file *file)
3851 return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
3854 static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
3857 return notify_on_release(cgrp);
3860 static int cgroup_write_notify_on_release(struct cgroup *cgrp,
3864 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
3866 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3868 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3873 * When dput() is called asynchronously, if umount has been done and
3874 * then deactivate_super() in cgroup_free_fn() kills the superblock,
3875 * there's a small window that vfs will see the root dentry with non-zero
3876 * refcnt and trigger BUG().
3878 * That's why we hold a reference before dput() and drop it right after.
3880 static void cgroup_dput(struct cgroup *cgrp)
3882 struct super_block *sb = cgrp->root->sb;
3884 atomic_inc(&sb->s_active);
3886 deactivate_super(sb);
3890 * Unregister event and free resources.
3892 * Gets called from workqueue.
3894 static void cgroup_event_remove(struct work_struct *work)
3896 struct cgroup_event *event = container_of(work, struct cgroup_event,
3898 struct cgroup *cgrp = event->cgrp;
3900 remove_wait_queue(event->wqh, &event->wait);
3902 event->cft->unregister_event(cgrp, event->cft, event->eventfd);
3904 /* Notify userspace the event is going away. */
3905 eventfd_signal(event->eventfd, 1);
3907 eventfd_ctx_put(event->eventfd);
3913 * Gets called on POLLHUP on eventfd when user closes it.
3915 * Called with wqh->lock held and interrupts disabled.
3917 static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
3918 int sync, void *key)
3920 struct cgroup_event *event = container_of(wait,
3921 struct cgroup_event, wait);
3922 struct cgroup *cgrp = event->cgrp;
3923 unsigned long flags = (unsigned long)key;
3925 if (flags & POLLHUP) {
3927 * If the event has been detached at cgroup removal, we
3928 * can simply return knowing the other side will cleanup
3931 * We can't race against event freeing since the other
3932 * side will require wqh->lock via remove_wait_queue(),
3935 spin_lock(&cgrp->event_list_lock);
3936 if (!list_empty(&event->list)) {
3937 list_del_init(&event->list);
3939 * We are in atomic context, but cgroup_event_remove()
3940 * may sleep, so we have to call it in workqueue.
3942 schedule_work(&event->remove);
3944 spin_unlock(&cgrp->event_list_lock);
3950 static void cgroup_event_ptable_queue_proc(struct file *file,
3951 wait_queue_head_t *wqh, poll_table *pt)
3953 struct cgroup_event *event = container_of(pt,
3954 struct cgroup_event, pt);
3957 add_wait_queue(wqh, &event->wait);
3961 * Parse input and register new cgroup event handler.
3963 * Input must be in format '<event_fd> <control_fd> <args>'.
3964 * Interpretation of args is defined by control file implementation.
3966 static int cgroup_write_event_control(struct cgroup *cgrp, struct cftype *cft,
3969 struct cgroup_event *event = NULL;
3970 struct cgroup *cgrp_cfile;
3971 unsigned int efd, cfd;
3972 struct file *efile = NULL;
3973 struct file *cfile = NULL;
3977 efd = simple_strtoul(buffer, &endp, 10);
3982 cfd = simple_strtoul(buffer, &endp, 10);
3983 if ((*endp != ' ') && (*endp != '\0'))
3987 event = kzalloc(sizeof(*event), GFP_KERNEL);
3991 INIT_LIST_HEAD(&event->list);
3992 init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
3993 init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
3994 INIT_WORK(&event->remove, cgroup_event_remove);
3996 efile = eventfd_fget(efd);
3997 if (IS_ERR(efile)) {
3998 ret = PTR_ERR(efile);
4002 event->eventfd = eventfd_ctx_fileget(efile);
4003 if (IS_ERR(event->eventfd)) {
4004 ret = PTR_ERR(event->eventfd);
4014 /* the process need read permission on control file */
4015 /* AV: shouldn't we check that it's been opened for read instead? */
4016 ret = inode_permission(file_inode(cfile), MAY_READ);
4020 event->cft = __file_cft(cfile);
4021 if (IS_ERR(event->cft)) {
4022 ret = PTR_ERR(event->cft);
4027 * The file to be monitored must be in the same cgroup as
4028 * cgroup.event_control is.
4030 cgrp_cfile = __d_cgrp(cfile->f_dentry->d_parent);
4031 if (cgrp_cfile != cgrp) {
4036 if (!event->cft->register_event || !event->cft->unregister_event) {
4041 ret = event->cft->register_event(cgrp, event->cft,
4042 event->eventfd, buffer);
4046 efile->f_op->poll(efile, &event->pt);
4049 * Events should be removed after rmdir of cgroup directory, but before
4050 * destroying subsystem state objects. Let's take reference to cgroup
4051 * directory dentry to do that.
4055 spin_lock(&cgrp->event_list_lock);
4056 list_add(&event->list, &cgrp->event_list);
4057 spin_unlock(&cgrp->event_list_lock);
4068 if (event && event->eventfd && !IS_ERR(event->eventfd))
4069 eventfd_ctx_put(event->eventfd);
4071 if (!IS_ERR_OR_NULL(efile))
4079 static u64 cgroup_clone_children_read(struct cgroup *cgrp,
4082 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
4085 static int cgroup_clone_children_write(struct cgroup *cgrp,
4090 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
4092 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
4096 static struct cftype cgroup_base_files[] = {
4098 .name = "cgroup.procs",
4099 .open = cgroup_procs_open,
4100 .write_u64 = cgroup_procs_write,
4101 .release = cgroup_pidlist_release,
4102 .mode = S_IRUGO | S_IWUSR,
4105 .name = "cgroup.event_control",
4106 .write_string = cgroup_write_event_control,
4110 .name = "cgroup.clone_children",
4111 .flags = CFTYPE_INSANE,
4112 .read_u64 = cgroup_clone_children_read,
4113 .write_u64 = cgroup_clone_children_write,
4116 .name = "cgroup.sane_behavior",
4117 .flags = CFTYPE_ONLY_ON_ROOT,
4118 .read_seq_string = cgroup_sane_behavior_show,
4122 * Historical crazy stuff. These don't have "cgroup." prefix and
4123 * don't exist if sane_behavior. If you're depending on these, be
4124 * prepared to be burned.
4128 .flags = CFTYPE_INSANE, /* use "procs" instead */
4129 .open = cgroup_tasks_open,
4130 .write_u64 = cgroup_tasks_write,
4131 .release = cgroup_pidlist_release,
4132 .mode = S_IRUGO | S_IWUSR,
4135 .name = "notify_on_release",
4136 .flags = CFTYPE_INSANE,
4137 .read_u64 = cgroup_read_notify_on_release,
4138 .write_u64 = cgroup_write_notify_on_release,
4141 .name = "release_agent",
4142 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
4143 .read_seq_string = cgroup_release_agent_show,
4144 .write_string = cgroup_release_agent_write,
4145 .max_write_len = PATH_MAX,
4151 * cgroup_populate_dir - selectively creation of files in a directory
4152 * @cgrp: target cgroup
4153 * @base_files: true if the base files should be added
4154 * @subsys_mask: mask of the subsystem ids whose files should be added
4156 static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files,
4157 unsigned long subsys_mask)
4160 struct cgroup_subsys *ss;
4163 err = cgroup_addrm_files(cgrp, NULL, cgroup_base_files, true);
4168 /* process cftsets of each subsystem */
4169 for_each_root_subsys(cgrp->root, ss) {
4170 struct cftype_set *set;
4171 if (!test_bit(ss->subsys_id, &subsys_mask))
4174 list_for_each_entry(set, &ss->cftsets, node)
4175 cgroup_addrm_files(cgrp, ss, set->cfts, true);
4178 /* This cgroup is ready now */
4179 for_each_root_subsys(cgrp->root, ss) {
4180 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4181 struct css_id *id = rcu_dereference_protected(css->id, true);
4184 * Update id->css pointer and make this css visible from
4185 * CSS ID functions. This pointer will be dereferened
4186 * from RCU-read-side without locks.
4189 rcu_assign_pointer(id->css, css);
4195 static void css_dput_fn(struct work_struct *work)
4197 struct cgroup_subsys_state *css =
4198 container_of(work, struct cgroup_subsys_state, dput_work);
4200 cgroup_dput(css->cgroup);
4203 static void css_release(struct percpu_ref *ref)
4205 struct cgroup_subsys_state *css =
4206 container_of(ref, struct cgroup_subsys_state, refcnt);
4208 schedule_work(&css->dput_work);
4211 static void init_cgroup_css(struct cgroup_subsys_state *css,
4212 struct cgroup_subsys *ss,
4213 struct cgroup *cgrp)
4218 if (cgrp == cgroup_dummy_top)
4219 css->flags |= CSS_ROOT;
4220 BUG_ON(cgrp->subsys[ss->subsys_id]);
4221 cgrp->subsys[ss->subsys_id] = css;
4224 * css holds an extra ref to @cgrp->dentry which is put on the last
4225 * css_put(). dput() requires process context, which css_put() may
4226 * be called without. @css->dput_work will be used to invoke
4227 * dput() asynchronously from css_put().
4229 INIT_WORK(&css->dput_work, css_dput_fn);
4232 /* invoke ->post_create() on a new CSS and mark it online if successful */
4233 static int online_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
4237 lockdep_assert_held(&cgroup_mutex);
4240 ret = ss->css_online(cgrp);
4242 cgrp->subsys[ss->subsys_id]->flags |= CSS_ONLINE;
4246 /* if the CSS is online, invoke ->pre_destory() on it and mark it offline */
4247 static void offline_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
4248 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
4250 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4252 lockdep_assert_held(&cgroup_mutex);
4254 if (!(css->flags & CSS_ONLINE))
4257 if (ss->css_offline)
4258 ss->css_offline(cgrp);
4260 cgrp->subsys[ss->subsys_id]->flags &= ~CSS_ONLINE;
4264 * cgroup_create - create a cgroup
4265 * @parent: cgroup that will be parent of the new cgroup
4266 * @dentry: dentry of the new cgroup
4267 * @mode: mode to set on new inode
4269 * Must be called with the mutex on the parent inode held
4271 static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
4274 struct cgroup *cgrp;
4275 struct cgroup_name *name;
4276 struct cgroupfs_root *root = parent->root;
4278 struct cgroup_subsys *ss;
4279 struct super_block *sb = root->sb;
4281 /* allocate the cgroup and its ID, 0 is reserved for the root */
4282 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4286 name = cgroup_alloc_name(dentry);
4289 rcu_assign_pointer(cgrp->name, name);
4291 cgrp->id = ida_simple_get(&root->cgroup_ida, 1, 0, GFP_KERNEL);
4296 * Only live parents can have children. Note that the liveliness
4297 * check isn't strictly necessary because cgroup_mkdir() and
4298 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4299 * anyway so that locking is contained inside cgroup proper and we
4300 * don't get nasty surprises if we ever grow another caller.
4302 if (!cgroup_lock_live_group(parent)) {
4307 /* Grab a reference on the superblock so the hierarchy doesn't
4308 * get deleted on unmount if there are child cgroups. This
4309 * can be done outside cgroup_mutex, since the sb can't
4310 * disappear while someone has an open control file on the
4312 atomic_inc(&sb->s_active);
4314 init_cgroup_housekeeping(cgrp);
4316 dentry->d_fsdata = cgrp;
4317 cgrp->dentry = dentry;
4319 cgrp->parent = parent;
4320 cgrp->root = parent->root;
4322 if (notify_on_release(parent))
4323 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4325 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4326 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
4328 for_each_root_subsys(root, ss) {
4329 struct cgroup_subsys_state *css;
4331 css = ss->css_alloc(cgrp);
4337 err = percpu_ref_init(&css->refcnt, css_release);
4341 init_cgroup_css(css, ss, cgrp);
4344 err = alloc_css_id(ss, parent, cgrp);
4351 * Create directory. cgroup_create_file() returns with the new
4352 * directory locked on success so that it can be populated without
4353 * dropping cgroup_mutex.
4355 err = cgroup_create_file(dentry, S_IFDIR | mode, sb);
4358 lockdep_assert_held(&dentry->d_inode->i_mutex);
4360 cgrp->serial_nr = cgroup_serial_nr_next++;
4362 /* allocation complete, commit to creation */
4363 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
4364 root->number_of_cgroups++;
4366 /* each css holds a ref to the cgroup's dentry */
4367 for_each_root_subsys(root, ss)
4370 /* hold a ref to the parent's dentry */
4371 dget(parent->dentry);
4373 /* creation succeeded, notify subsystems */
4374 for_each_root_subsys(root, ss) {
4375 err = online_css(ss, cgrp);
4379 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4381 pr_warning("cgroup: %s (%d) created nested cgroup for controller \"%s\" which has incomplete hierarchy support. Nested cgroups may change behavior in the future.\n",
4382 current->comm, current->pid, ss->name);
4383 if (!strcmp(ss->name, "memory"))
4384 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4385 ss->warned_broken_hierarchy = true;
4389 err = cgroup_populate_dir(cgrp, true, root->subsys_mask);
4393 mutex_unlock(&cgroup_mutex);
4394 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
4399 for_each_root_subsys(root, ss) {
4400 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4403 percpu_ref_cancel_init(&css->refcnt);
4407 mutex_unlock(&cgroup_mutex);
4408 /* Release the reference count that we took on the superblock */
4409 deactivate_super(sb);
4411 ida_simple_remove(&root->cgroup_ida, cgrp->id);
4413 kfree(rcu_dereference_raw(cgrp->name));
4419 cgroup_destroy_locked(cgrp);
4420 mutex_unlock(&cgroup_mutex);
4421 mutex_unlock(&dentry->d_inode->i_mutex);
4425 static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
4427 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
4429 /* the vfs holds inode->i_mutex already */
4430 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
4433 static void cgroup_css_killed(struct cgroup *cgrp)
4435 if (!atomic_dec_and_test(&cgrp->css_kill_cnt))
4438 /* percpu ref's of all css's are killed, kick off the next step */
4439 INIT_WORK(&cgrp->destroy_work, cgroup_offline_fn);
4440 schedule_work(&cgrp->destroy_work);
4443 static void css_ref_killed_fn(struct percpu_ref *ref)
4445 struct cgroup_subsys_state *css =
4446 container_of(ref, struct cgroup_subsys_state, refcnt);
4448 cgroup_css_killed(css->cgroup);
4452 * cgroup_destroy_locked - the first stage of cgroup destruction
4453 * @cgrp: cgroup to be destroyed
4455 * css's make use of percpu refcnts whose killing latency shouldn't be
4456 * exposed to userland and are RCU protected. Also, cgroup core needs to
4457 * guarantee that css_tryget() won't succeed by the time ->css_offline() is
4458 * invoked. To satisfy all the requirements, destruction is implemented in
4459 * the following two steps.
4461 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4462 * userland visible parts and start killing the percpu refcnts of
4463 * css's. Set up so that the next stage will be kicked off once all
4464 * the percpu refcnts are confirmed to be killed.
4466 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4467 * rest of destruction. Once all cgroup references are gone, the
4468 * cgroup is RCU-freed.
4470 * This function implements s1. After this step, @cgrp is gone as far as
4471 * the userland is concerned and a new cgroup with the same name may be
4472 * created. As cgroup doesn't care about the names internally, this
4473 * doesn't cause any problem.
4475 static int cgroup_destroy_locked(struct cgroup *cgrp)
4476 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
4478 struct dentry *d = cgrp->dentry;
4479 struct cgroup_event *event, *tmp;
4480 struct cgroup_subsys *ss;
4483 lockdep_assert_held(&d->d_inode->i_mutex);
4484 lockdep_assert_held(&cgroup_mutex);
4487 * css_set_lock synchronizes access to ->cset_links and prevents
4488 * @cgrp from being removed while __put_css_set() is in progress.
4490 read_lock(&css_set_lock);
4491 empty = list_empty(&cgrp->cset_links) && list_empty(&cgrp->children);
4492 read_unlock(&css_set_lock);
4497 * Block new css_tryget() by killing css refcnts. cgroup core
4498 * guarantees that, by the time ->css_offline() is invoked, no new
4499 * css reference will be given out via css_tryget(). We can't
4500 * simply call percpu_ref_kill() and proceed to offlining css's
4501 * because percpu_ref_kill() doesn't guarantee that the ref is seen
4502 * as killed on all CPUs on return.
4504 * Use percpu_ref_kill_and_confirm() to get notifications as each
4505 * css is confirmed to be seen as killed on all CPUs. The
4506 * notification callback keeps track of the number of css's to be
4507 * killed and schedules cgroup_offline_fn() to perform the rest of
4508 * destruction once the percpu refs of all css's are confirmed to
4511 atomic_set(&cgrp->css_kill_cnt, 1);
4512 for_each_root_subsys(cgrp->root, ss) {
4513 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4516 * Killing would put the base ref, but we need to keep it
4517 * alive until after ->css_offline.
4519 percpu_ref_get(&css->refcnt);
4521 atomic_inc(&cgrp->css_kill_cnt);
4522 percpu_ref_kill_and_confirm(&css->refcnt, css_ref_killed_fn);
4524 cgroup_css_killed(cgrp);
4527 * Mark @cgrp dead. This prevents further task migration and child
4528 * creation by disabling cgroup_lock_live_group(). Note that
4529 * CGRP_DEAD assertion is depended upon by cgroup_next_sibling() to
4530 * resume iteration after dropping RCU read lock. See
4531 * cgroup_next_sibling() for details.
4533 set_bit(CGRP_DEAD, &cgrp->flags);
4535 /* CGRP_DEAD is set, remove from ->release_list for the last time */
4536 raw_spin_lock(&release_list_lock);
4537 if (!list_empty(&cgrp->release_list))
4538 list_del_init(&cgrp->release_list);
4539 raw_spin_unlock(&release_list_lock);
4542 * Remove @cgrp directory. The removal puts the base ref but we
4543 * aren't quite done with @cgrp yet, so hold onto it.
4546 cgroup_d_remove_dir(d);
4549 * Unregister events and notify userspace.
4550 * Notify userspace about cgroup removing only after rmdir of cgroup
4551 * directory to avoid race between userspace and kernelspace.
4553 spin_lock(&cgrp->event_list_lock);
4554 list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) {
4555 list_del_init(&event->list);
4556 schedule_work(&event->remove);
4558 spin_unlock(&cgrp->event_list_lock);
4564 * cgroup_offline_fn - the second step of cgroup destruction
4565 * @work: cgroup->destroy_free_work
4567 * This function is invoked from a work item for a cgroup which is being
4568 * destroyed after the percpu refcnts of all css's are guaranteed to be
4569 * seen as killed on all CPUs, and performs the rest of destruction. This
4570 * is the second step of destruction described in the comment above
4571 * cgroup_destroy_locked().
4573 static void cgroup_offline_fn(struct work_struct *work)
4575 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
4576 struct cgroup *parent = cgrp->parent;
4577 struct dentry *d = cgrp->dentry;
4578 struct cgroup_subsys *ss;
4580 mutex_lock(&cgroup_mutex);
4583 * css_tryget() is guaranteed to fail now. Tell subsystems to
4584 * initate destruction.
4586 for_each_root_subsys(cgrp->root, ss)
4587 offline_css(ss, cgrp);
4590 * Put the css refs from cgroup_destroy_locked(). Each css holds
4591 * an extra reference to the cgroup's dentry and cgroup removal
4592 * proceeds regardless of css refs. On the last put of each css,
4593 * whenever that may be, the extra dentry ref is put so that dentry
4594 * destruction happens only after all css's are released.
4596 for_each_root_subsys(cgrp->root, ss)
4597 css_put(cgrp->subsys[ss->subsys_id]);
4599 /* delete this cgroup from parent->children */
4600 list_del_rcu(&cgrp->sibling);
4604 set_bit(CGRP_RELEASABLE, &parent->flags);
4605 check_for_release(parent);
4607 mutex_unlock(&cgroup_mutex);
4610 static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
4614 mutex_lock(&cgroup_mutex);
4615 ret = cgroup_destroy_locked(dentry->d_fsdata);
4616 mutex_unlock(&cgroup_mutex);
4621 static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss)
4623 INIT_LIST_HEAD(&ss->cftsets);
4626 * base_cftset is embedded in subsys itself, no need to worry about
4629 if (ss->base_cftypes) {
4630 ss->base_cftset.cfts = ss->base_cftypes;
4631 list_add_tail(&ss->base_cftset.node, &ss->cftsets);
4635 static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
4637 struct cgroup_subsys_state *css;
4639 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
4641 mutex_lock(&cgroup_mutex);
4643 /* init base cftset */
4644 cgroup_init_cftsets(ss);
4646 /* Create the top cgroup state for this subsystem */
4647 list_add(&ss->sibling, &cgroup_dummy_root.subsys_list);
4648 ss->root = &cgroup_dummy_root;
4649 css = ss->css_alloc(cgroup_dummy_top);
4650 /* We don't handle early failures gracefully */
4651 BUG_ON(IS_ERR(css));
4652 init_cgroup_css(css, ss, cgroup_dummy_top);
4654 /* Update the init_css_set to contain a subsys
4655 * pointer to this state - since the subsystem is
4656 * newly registered, all tasks and hence the
4657 * init_css_set is in the subsystem's top cgroup. */
4658 init_css_set.subsys[ss->subsys_id] = css;
4660 need_forkexit_callback |= ss->fork || ss->exit;
4662 /* At system boot, before all subsystems have been
4663 * registered, no tasks have been forked, so we don't
4664 * need to invoke fork callbacks here. */
4665 BUG_ON(!list_empty(&init_task.tasks));
4667 BUG_ON(online_css(ss, cgroup_dummy_top));
4669 mutex_unlock(&cgroup_mutex);
4671 /* this function shouldn't be used with modular subsystems, since they
4672 * need to register a subsys_id, among other things */
4677 * cgroup_load_subsys: load and register a modular subsystem at runtime
4678 * @ss: the subsystem to load
4680 * This function should be called in a modular subsystem's initcall. If the
4681 * subsystem is built as a module, it will be assigned a new subsys_id and set
4682 * up for use. If the subsystem is built-in anyway, work is delegated to the
4683 * simpler cgroup_init_subsys.
4685 int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
4687 struct cgroup_subsys_state *css;
4689 struct hlist_node *tmp;
4690 struct css_set *cset;
4693 /* check name and function validity */
4694 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
4695 ss->css_alloc == NULL || ss->css_free == NULL)
4699 * we don't support callbacks in modular subsystems. this check is
4700 * before the ss->module check for consistency; a subsystem that could
4701 * be a module should still have no callbacks even if the user isn't
4702 * compiling it as one.
4704 if (ss->fork || ss->exit)
4708 * an optionally modular subsystem is built-in: we want to do nothing,
4709 * since cgroup_init_subsys will have already taken care of it.
4711 if (ss->module == NULL) {
4712 /* a sanity check */
4713 BUG_ON(cgroup_subsys[ss->subsys_id] != ss);
4717 /* init base cftset */
4718 cgroup_init_cftsets(ss);
4720 mutex_lock(&cgroup_mutex);
4721 cgroup_subsys[ss->subsys_id] = ss;
4724 * no ss->css_alloc seems to need anything important in the ss
4725 * struct, so this can happen first (i.e. before the dummy root
4728 css = ss->css_alloc(cgroup_dummy_top);
4730 /* failure case - need to deassign the cgroup_subsys[] slot. */
4731 cgroup_subsys[ss->subsys_id] = NULL;
4732 mutex_unlock(&cgroup_mutex);
4733 return PTR_ERR(css);
4736 list_add(&ss->sibling, &cgroup_dummy_root.subsys_list);
4737 ss->root = &cgroup_dummy_root;
4739 /* our new subsystem will be attached to the dummy hierarchy. */
4740 init_cgroup_css(css, ss, cgroup_dummy_top);
4741 /* init_idr must be after init_cgroup_css because it sets css->id. */
4743 ret = cgroup_init_idr(ss, css);
4749 * Now we need to entangle the css into the existing css_sets. unlike
4750 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4751 * will need a new pointer to it; done by iterating the css_set_table.
4752 * furthermore, modifying the existing css_sets will corrupt the hash
4753 * table state, so each changed css_set will need its hash recomputed.
4754 * this is all done under the css_set_lock.
4756 write_lock(&css_set_lock);
4757 hash_for_each_safe(css_set_table, i, tmp, cset, hlist) {
4758 /* skip entries that we already rehashed */
4759 if (cset->subsys[ss->subsys_id])
4761 /* remove existing entry */
4762 hash_del(&cset->hlist);
4764 cset->subsys[ss->subsys_id] = css;
4765 /* recompute hash and restore entry */
4766 key = css_set_hash(cset->subsys);
4767 hash_add(css_set_table, &cset->hlist, key);
4769 write_unlock(&css_set_lock);
4771 ret = online_css(ss, cgroup_dummy_top);
4776 mutex_unlock(&cgroup_mutex);
4780 mutex_unlock(&cgroup_mutex);
4781 /* @ss can't be mounted here as try_module_get() would fail */
4782 cgroup_unload_subsys(ss);
4785 EXPORT_SYMBOL_GPL(cgroup_load_subsys);
4788 * cgroup_unload_subsys: unload a modular subsystem
4789 * @ss: the subsystem to unload
4791 * This function should be called in a modular subsystem's exitcall. When this
4792 * function is invoked, the refcount on the subsystem's module will be 0, so
4793 * the subsystem will not be attached to any hierarchy.
4795 void cgroup_unload_subsys(struct cgroup_subsys *ss)
4797 struct cgrp_cset_link *link;
4799 BUG_ON(ss->module == NULL);
4802 * we shouldn't be called if the subsystem is in use, and the use of
4803 * try_module_get in parse_cgroupfs_options should ensure that it
4804 * doesn't start being used while we're killing it off.
4806 BUG_ON(ss->root != &cgroup_dummy_root);
4808 mutex_lock(&cgroup_mutex);
4810 offline_css(ss, cgroup_dummy_top);
4813 idr_destroy(&ss->idr);
4815 /* deassign the subsys_id */
4816 cgroup_subsys[ss->subsys_id] = NULL;
4818 /* remove subsystem from the dummy root's list of subsystems */
4819 list_del_init(&ss->sibling);
4822 * disentangle the css from all css_sets attached to the dummy
4823 * top. as in loading, we need to pay our respects to the hashtable
4826 write_lock(&css_set_lock);
4827 list_for_each_entry(link, &cgroup_dummy_top->cset_links, cset_link) {
4828 struct css_set *cset = link->cset;
4831 hash_del(&cset->hlist);
4832 cset->subsys[ss->subsys_id] = NULL;
4833 key = css_set_hash(cset->subsys);
4834 hash_add(css_set_table, &cset->hlist, key);
4836 write_unlock(&css_set_lock);
4839 * remove subsystem's css from the cgroup_dummy_top and free it -
4840 * need to free before marking as null because ss->css_free needs
4841 * the cgrp->subsys pointer to find their state. note that this
4842 * also takes care of freeing the css_id.
4844 ss->css_free(cgroup_dummy_top);
4845 cgroup_dummy_top->subsys[ss->subsys_id] = NULL;
4847 mutex_unlock(&cgroup_mutex);
4849 EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
4852 * cgroup_init_early - cgroup initialization at system boot
4854 * Initialize cgroups at system boot, and initialize any
4855 * subsystems that request early init.
4857 int __init cgroup_init_early(void)
4859 struct cgroup_subsys *ss;
4862 atomic_set(&init_css_set.refcount, 1);
4863 INIT_LIST_HEAD(&init_css_set.cgrp_links);
4864 INIT_LIST_HEAD(&init_css_set.tasks);
4865 INIT_HLIST_NODE(&init_css_set.hlist);
4867 init_cgroup_root(&cgroup_dummy_root);
4868 cgroup_root_count = 1;
4869 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
4871 init_cgrp_cset_link.cset = &init_css_set;
4872 init_cgrp_cset_link.cgrp = cgroup_dummy_top;
4873 list_add(&init_cgrp_cset_link.cset_link, &cgroup_dummy_top->cset_links);
4874 list_add(&init_cgrp_cset_link.cgrp_link, &init_css_set.cgrp_links);
4876 /* at bootup time, we don't worry about modular subsystems */
4877 for_each_builtin_subsys(ss, i) {
4879 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
4880 BUG_ON(!ss->css_alloc);
4881 BUG_ON(!ss->css_free);
4882 if (ss->subsys_id != i) {
4883 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
4884 ss->name, ss->subsys_id);
4889 cgroup_init_subsys(ss);
4895 * cgroup_init - cgroup initialization
4897 * Register cgroup filesystem and /proc file, and initialize
4898 * any subsystems that didn't request early init.
4900 int __init cgroup_init(void)
4902 struct cgroup_subsys *ss;
4906 err = bdi_init(&cgroup_backing_dev_info);
4910 for_each_builtin_subsys(ss, i) {
4911 if (!ss->early_init)
4912 cgroup_init_subsys(ss);
4914 cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]);
4917 /* allocate id for the dummy hierarchy */
4918 mutex_lock(&cgroup_mutex);
4919 mutex_lock(&cgroup_root_mutex);
4921 /* Add init_css_set to the hash table */
4922 key = css_set_hash(init_css_set.subsys);
4923 hash_add(css_set_table, &init_css_set.hlist, key);
4925 BUG_ON(cgroup_init_root_id(&cgroup_dummy_root, 0, 1));
4927 mutex_unlock(&cgroup_root_mutex);
4928 mutex_unlock(&cgroup_mutex);
4930 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
4936 err = register_filesystem(&cgroup_fs_type);
4938 kobject_put(cgroup_kobj);
4942 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
4946 bdi_destroy(&cgroup_backing_dev_info);
4952 * proc_cgroup_show()
4953 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4954 * - Used for /proc/<pid>/cgroup.
4955 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4956 * doesn't really matter if tsk->cgroup changes after we read it,
4957 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
4958 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4959 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4960 * cgroup to top_cgroup.
4963 /* TODO: Use a proper seq_file iterator */
4964 int proc_cgroup_show(struct seq_file *m, void *v)
4967 struct task_struct *tsk;
4970 struct cgroupfs_root *root;
4973 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4979 tsk = get_pid_task(pid, PIDTYPE_PID);
4985 mutex_lock(&cgroup_mutex);
4987 for_each_active_root(root) {
4988 struct cgroup_subsys *ss;
4989 struct cgroup *cgrp;
4992 seq_printf(m, "%d:", root->hierarchy_id);
4993 for_each_root_subsys(root, ss)
4994 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
4995 if (strlen(root->name))
4996 seq_printf(m, "%sname=%s", count ? "," : "",
4999 cgrp = task_cgroup_from_root(tsk, root);
5000 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
5008 mutex_unlock(&cgroup_mutex);
5009 put_task_struct(tsk);
5016 /* Display information about each subsystem and each hierarchy */
5017 static int proc_cgroupstats_show(struct seq_file *m, void *v)
5019 struct cgroup_subsys *ss;
5022 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
5024 * ideally we don't want subsystems moving around while we do this.
5025 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
5026 * subsys/hierarchy state.
5028 mutex_lock(&cgroup_mutex);
5030 for_each_subsys(ss, i)
5031 seq_printf(m, "%s\t%d\t%d\t%d\n",
5032 ss->name, ss->root->hierarchy_id,
5033 ss->root->number_of_cgroups, !ss->disabled);
5035 mutex_unlock(&cgroup_mutex);
5039 static int cgroupstats_open(struct inode *inode, struct file *file)
5041 return single_open(file, proc_cgroupstats_show, NULL);
5044 static const struct file_operations proc_cgroupstats_operations = {
5045 .open = cgroupstats_open,
5047 .llseek = seq_lseek,
5048 .release = single_release,
5052 * cgroup_fork - attach newly forked task to its parents cgroup.
5053 * @child: pointer to task_struct of forking parent process.
5055 * Description: A task inherits its parent's cgroup at fork().
5057 * A pointer to the shared css_set was automatically copied in
5058 * fork.c by dup_task_struct(). However, we ignore that copy, since
5059 * it was not made under the protection of RCU or cgroup_mutex, so
5060 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
5061 * have already changed current->cgroups, allowing the previously
5062 * referenced cgroup group to be removed and freed.
5064 * At the point that cgroup_fork() is called, 'current' is the parent
5065 * task, and the passed argument 'child' points to the child task.
5067 void cgroup_fork(struct task_struct *child)
5070 get_css_set(task_css_set(current));
5071 child->cgroups = current->cgroups;
5072 task_unlock(current);
5073 INIT_LIST_HEAD(&child->cg_list);
5077 * cgroup_post_fork - called on a new task after adding it to the task list
5078 * @child: the task in question
5080 * Adds the task to the list running through its css_set if necessary and
5081 * call the subsystem fork() callbacks. Has to be after the task is
5082 * visible on the task list in case we race with the first call to
5083 * cgroup_iter_start() - to guarantee that the new task ends up on its
5086 void cgroup_post_fork(struct task_struct *child)
5088 struct cgroup_subsys *ss;
5092 * use_task_css_set_links is set to 1 before we walk the tasklist
5093 * under the tasklist_lock and we read it here after we added the child
5094 * to the tasklist under the tasklist_lock as well. If the child wasn't
5095 * yet in the tasklist when we walked through it from
5096 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
5097 * should be visible now due to the paired locking and barriers implied
5098 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
5099 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
5102 if (use_task_css_set_links) {
5103 write_lock(&css_set_lock);
5105 if (list_empty(&child->cg_list))
5106 list_add(&child->cg_list, &task_css_set(child)->tasks);
5108 write_unlock(&css_set_lock);
5112 * Call ss->fork(). This must happen after @child is linked on
5113 * css_set; otherwise, @child might change state between ->fork()
5114 * and addition to css_set.
5116 if (need_forkexit_callback) {
5118 * fork/exit callbacks are supported only for builtin
5119 * subsystems, and the builtin section of the subsys
5120 * array is immutable, so we don't need to lock the
5121 * subsys array here. On the other hand, modular section
5122 * of the array can be freed at module unload, so we
5125 for_each_builtin_subsys(ss, i)
5132 * cgroup_exit - detach cgroup from exiting task
5133 * @tsk: pointer to task_struct of exiting process
5134 * @run_callback: run exit callbacks?
5136 * Description: Detach cgroup from @tsk and release it.
5138 * Note that cgroups marked notify_on_release force every task in
5139 * them to take the global cgroup_mutex mutex when exiting.
5140 * This could impact scaling on very large systems. Be reluctant to
5141 * use notify_on_release cgroups where very high task exit scaling
5142 * is required on large systems.
5144 * the_top_cgroup_hack:
5146 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
5148 * We call cgroup_exit() while the task is still competent to
5149 * handle notify_on_release(), then leave the task attached to the
5150 * root cgroup in each hierarchy for the remainder of its exit.
5152 * To do this properly, we would increment the reference count on
5153 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
5154 * code we would add a second cgroup function call, to drop that
5155 * reference. This would just create an unnecessary hot spot on
5156 * the top_cgroup reference count, to no avail.
5158 * Normally, holding a reference to a cgroup without bumping its
5159 * count is unsafe. The cgroup could go away, or someone could
5160 * attach us to a different cgroup, decrementing the count on
5161 * the first cgroup that we never incremented. But in this case,
5162 * top_cgroup isn't going away, and either task has PF_EXITING set,
5163 * which wards off any cgroup_attach_task() attempts, or task is a failed
5164 * fork, never visible to cgroup_attach_task.
5166 void cgroup_exit(struct task_struct *tsk, int run_callbacks)
5168 struct cgroup_subsys *ss;
5169 struct css_set *cset;
5173 * Unlink from the css_set task list if necessary.
5174 * Optimistically check cg_list before taking
5177 if (!list_empty(&tsk->cg_list)) {
5178 write_lock(&css_set_lock);
5179 if (!list_empty(&tsk->cg_list))
5180 list_del_init(&tsk->cg_list);
5181 write_unlock(&css_set_lock);
5184 /* Reassign the task to the init_css_set. */
5186 cset = task_css_set(tsk);
5187 RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
5189 if (run_callbacks && need_forkexit_callback) {
5191 * fork/exit callbacks are supported only for builtin
5192 * subsystems, see cgroup_post_fork() for details.
5194 for_each_builtin_subsys(ss, i) {
5196 struct cgroup *old_cgrp = cset->subsys[i]->cgroup;
5197 struct cgroup *cgrp = task_cgroup(tsk, i);
5199 ss->exit(cgrp, old_cgrp, tsk);
5205 put_css_set_taskexit(cset);
5208 static void check_for_release(struct cgroup *cgrp)
5210 if (cgroup_is_releasable(cgrp) &&
5211 list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) {
5213 * Control Group is currently removeable. If it's not
5214 * already queued for a userspace notification, queue
5217 int need_schedule_work = 0;
5219 raw_spin_lock(&release_list_lock);
5220 if (!cgroup_is_dead(cgrp) &&
5221 list_empty(&cgrp->release_list)) {
5222 list_add(&cgrp->release_list, &release_list);
5223 need_schedule_work = 1;
5225 raw_spin_unlock(&release_list_lock);
5226 if (need_schedule_work)
5227 schedule_work(&release_agent_work);
5232 * Notify userspace when a cgroup is released, by running the
5233 * configured release agent with the name of the cgroup (path
5234 * relative to the root of cgroup file system) as the argument.
5236 * Most likely, this user command will try to rmdir this cgroup.
5238 * This races with the possibility that some other task will be
5239 * attached to this cgroup before it is removed, or that some other
5240 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5241 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5242 * unused, and this cgroup will be reprieved from its death sentence,
5243 * to continue to serve a useful existence. Next time it's released,
5244 * we will get notified again, if it still has 'notify_on_release' set.
5246 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5247 * means only wait until the task is successfully execve()'d. The
5248 * separate release agent task is forked by call_usermodehelper(),
5249 * then control in this thread returns here, without waiting for the
5250 * release agent task. We don't bother to wait because the caller of
5251 * this routine has no use for the exit status of the release agent
5252 * task, so no sense holding our caller up for that.
5254 static void cgroup_release_agent(struct work_struct *work)
5256 BUG_ON(work != &release_agent_work);
5257 mutex_lock(&cgroup_mutex);
5258 raw_spin_lock(&release_list_lock);
5259 while (!list_empty(&release_list)) {
5260 char *argv[3], *envp[3];
5262 char *pathbuf = NULL, *agentbuf = NULL;
5263 struct cgroup *cgrp = list_entry(release_list.next,
5266 list_del_init(&cgrp->release_list);
5267 raw_spin_unlock(&release_list_lock);
5268 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
5271 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
5273 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5278 argv[i++] = agentbuf;
5279 argv[i++] = pathbuf;
5283 /* minimal command environment */
5284 envp[i++] = "HOME=/";
5285 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5288 /* Drop the lock while we invoke the usermode helper,
5289 * since the exec could involve hitting disk and hence
5290 * be a slow process */
5291 mutex_unlock(&cgroup_mutex);
5292 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
5293 mutex_lock(&cgroup_mutex);
5297 raw_spin_lock(&release_list_lock);
5299 raw_spin_unlock(&release_list_lock);
5300 mutex_unlock(&cgroup_mutex);
5303 static int __init cgroup_disable(char *str)
5305 struct cgroup_subsys *ss;
5309 while ((token = strsep(&str, ",")) != NULL) {
5314 * cgroup_disable, being at boot time, can't know about
5315 * module subsystems, so we don't worry about them.
5317 for_each_builtin_subsys(ss, i) {
5318 if (!strcmp(token, ss->name)) {
5320 printk(KERN_INFO "Disabling %s control group"
5321 " subsystem\n", ss->name);
5328 __setup("cgroup_disable=", cgroup_disable);
5331 * Functons for CSS ID.
5334 /* to get ID other than 0, this should be called when !cgroup_is_dead() */
5335 unsigned short css_id(struct cgroup_subsys_state *css)
5337 struct css_id *cssid;
5340 * This css_id() can return correct value when somone has refcnt
5341 * on this or this is under rcu_read_lock(). Once css->id is allocated,
5342 * it's unchanged until freed.
5344 cssid = rcu_dereference_raw(css->id);
5350 EXPORT_SYMBOL_GPL(css_id);
5353 * css_is_ancestor - test "root" css is an ancestor of "child"
5354 * @child: the css to be tested.
5355 * @root: the css supporsed to be an ancestor of the child.
5357 * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
5358 * this function reads css->id, the caller must hold rcu_read_lock().
5359 * But, considering usual usage, the csses should be valid objects after test.
5360 * Assuming that the caller will do some action to the child if this returns
5361 * returns true, the caller must take "child";s reference count.
5362 * If "child" is valid object and this returns true, "root" is valid, too.
5365 bool css_is_ancestor(struct cgroup_subsys_state *child,
5366 const struct cgroup_subsys_state *root)
5368 struct css_id *child_id;
5369 struct css_id *root_id;
5371 child_id = rcu_dereference(child->id);
5374 root_id = rcu_dereference(root->id);
5377 if (child_id->depth < root_id->depth)
5379 if (child_id->stack[root_id->depth] != root_id->id)
5384 void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
5386 struct css_id *id = rcu_dereference_protected(css->id, true);
5388 /* When this is called before css_id initialization, id can be NULL */
5392 BUG_ON(!ss->use_id);
5394 rcu_assign_pointer(id->css, NULL);
5395 rcu_assign_pointer(css->id, NULL);
5396 spin_lock(&ss->id_lock);
5397 idr_remove(&ss->idr, id->id);
5398 spin_unlock(&ss->id_lock);
5399 kfree_rcu(id, rcu_head);
5401 EXPORT_SYMBOL_GPL(free_css_id);
5404 * This is called by init or create(). Then, calls to this function are
5405 * always serialized (By cgroup_mutex() at create()).
5408 static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
5410 struct css_id *newid;
5413 BUG_ON(!ss->use_id);
5415 size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
5416 newid = kzalloc(size, GFP_KERNEL);
5418 return ERR_PTR(-ENOMEM);
5420 idr_preload(GFP_KERNEL);
5421 spin_lock(&ss->id_lock);
5422 /* Don't use 0. allocates an ID of 1-65535 */
5423 ret = idr_alloc(&ss->idr, newid, 1, CSS_ID_MAX + 1, GFP_NOWAIT);
5424 spin_unlock(&ss->id_lock);
5427 /* Returns error when there are no free spaces for new ID.*/
5432 newid->depth = depth;
5436 return ERR_PTR(ret);
5440 static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss,
5441 struct cgroup_subsys_state *rootcss)
5443 struct css_id *newid;
5445 spin_lock_init(&ss->id_lock);
5448 newid = get_new_cssid(ss, 0);
5450 return PTR_ERR(newid);
5452 newid->stack[0] = newid->id;
5453 RCU_INIT_POINTER(newid->css, rootcss);
5454 RCU_INIT_POINTER(rootcss->id, newid);
5458 static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent,
5459 struct cgroup *child)
5461 int subsys_id, i, depth = 0;
5462 struct cgroup_subsys_state *parent_css, *child_css;
5463 struct css_id *child_id, *parent_id;
5465 subsys_id = ss->subsys_id;
5466 parent_css = parent->subsys[subsys_id];
5467 child_css = child->subsys[subsys_id];
5468 parent_id = rcu_dereference_protected(parent_css->id, true);
5469 depth = parent_id->depth + 1;
5471 child_id = get_new_cssid(ss, depth);
5472 if (IS_ERR(child_id))
5473 return PTR_ERR(child_id);
5475 for (i = 0; i < depth; i++)
5476 child_id->stack[i] = parent_id->stack[i];
5477 child_id->stack[depth] = child_id->id;
5479 * child_id->css pointer will be set after this cgroup is available
5480 * see cgroup_populate_dir()
5482 rcu_assign_pointer(child_css->id, child_id);
5488 * css_lookup - lookup css by id
5489 * @ss: cgroup subsys to be looked into.
5492 * Returns pointer to cgroup_subsys_state if there is valid one with id.
5493 * NULL if not. Should be called under rcu_read_lock()
5495 struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
5497 struct css_id *cssid = NULL;
5499 BUG_ON(!ss->use_id);
5500 cssid = idr_find(&ss->idr, id);
5502 if (unlikely(!cssid))
5505 return rcu_dereference(cssid->css);
5507 EXPORT_SYMBOL_GPL(css_lookup);
5510 * get corresponding css from file open on cgroupfs directory
5512 struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id)
5514 struct cgroup *cgrp;
5515 struct inode *inode;
5516 struct cgroup_subsys_state *css;
5518 inode = file_inode(f);
5519 /* check in cgroup filesystem dir */
5520 if (inode->i_op != &cgroup_dir_inode_operations)
5521 return ERR_PTR(-EBADF);
5523 if (id < 0 || id >= CGROUP_SUBSYS_COUNT)
5524 return ERR_PTR(-EINVAL);
5527 cgrp = __d_cgrp(f->f_dentry);
5528 css = cgrp->subsys[id];
5529 return css ? css : ERR_PTR(-ENOENT);
5532 #ifdef CONFIG_CGROUP_DEBUG
5533 static struct cgroup_subsys_state *debug_css_alloc(struct cgroup *cgrp)
5535 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5538 return ERR_PTR(-ENOMEM);
5543 static void debug_css_free(struct cgroup *cgrp)
5545 kfree(cgrp->subsys[debug_subsys_id]);
5548 static u64 debug_taskcount_read(struct cgroup *cgrp, struct cftype *cft)
5550 return cgroup_task_count(cgrp);
5553 static u64 current_css_set_read(struct cgroup *cgrp, struct cftype *cft)
5555 return (u64)(unsigned long)current->cgroups;
5558 static u64 current_css_set_refcount_read(struct cgroup *cgrp,
5564 count = atomic_read(&task_css_set(current)->refcount);
5569 static int current_css_set_cg_links_read(struct cgroup *cgrp,
5571 struct seq_file *seq)
5573 struct cgrp_cset_link *link;
5574 struct css_set *cset;
5576 read_lock(&css_set_lock);
5578 cset = rcu_dereference(current->cgroups);
5579 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
5580 struct cgroup *c = link->cgrp;
5584 name = c->dentry->d_name.name;
5587 seq_printf(seq, "Root %d group %s\n",
5588 c->root->hierarchy_id, name);
5591 read_unlock(&css_set_lock);
5595 #define MAX_TASKS_SHOWN_PER_CSS 25
5596 static int cgroup_css_links_read(struct cgroup *cgrp,
5598 struct seq_file *seq)
5600 struct cgrp_cset_link *link;
5602 read_lock(&css_set_lock);
5603 list_for_each_entry(link, &cgrp->cset_links, cset_link) {
5604 struct css_set *cset = link->cset;
5605 struct task_struct *task;
5607 seq_printf(seq, "css_set %p\n", cset);
5608 list_for_each_entry(task, &cset->tasks, cg_list) {
5609 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5610 seq_puts(seq, " ...\n");
5613 seq_printf(seq, " task %d\n",
5614 task_pid_vnr(task));
5618 read_unlock(&css_set_lock);
5622 static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft)
5624 return test_bit(CGRP_RELEASABLE, &cgrp->flags);
5627 static struct cftype debug_files[] = {
5629 .name = "taskcount",
5630 .read_u64 = debug_taskcount_read,
5634 .name = "current_css_set",
5635 .read_u64 = current_css_set_read,
5639 .name = "current_css_set_refcount",
5640 .read_u64 = current_css_set_refcount_read,
5644 .name = "current_css_set_cg_links",
5645 .read_seq_string = current_css_set_cg_links_read,
5649 .name = "cgroup_css_links",
5650 .read_seq_string = cgroup_css_links_read,
5654 .name = "releasable",
5655 .read_u64 = releasable_read,
5661 struct cgroup_subsys debug_subsys = {
5663 .css_alloc = debug_css_alloc,
5664 .css_free = debug_css_free,
5665 .subsys_id = debug_subsys_id,
5666 .base_cftypes = debug_files,
5668 #endif /* CONFIG_CGROUP_DEBUG */