2 * Generic process-grouping system.
4 * Based originally on the cpuset system, extracted by Paul Menage
5 * Copyright (C) 2006 Google, Inc
7 * Copyright notices from the original cpuset code:
8 * --------------------------------------------------
9 * Copyright (C) 2003 BULL SA.
10 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
12 * Portions derived from Patrick Mochel's sysfs code.
13 * sysfs is Copyright (c) 2001-3 Patrick Mochel
15 * 2003-10-10 Written by Simon Derr.
16 * 2003-10-22 Updates by Stephen Hemminger.
17 * 2004 May-July Rework by Paul Jackson.
18 * ---------------------------------------------------
20 * This file is subject to the terms and conditions of the GNU General Public
21 * License. See the file COPYING in the main directory of the Linux
22 * distribution for more details.
25 #include <linux/cgroup.h>
26 #include <linux/module.h>
27 #include <linux/ctype.h>
28 #include <linux/errno.h>
30 #include <linux/kernel.h>
31 #include <linux/list.h>
33 #include <linux/mutex.h>
34 #include <linux/mount.h>
35 #include <linux/pagemap.h>
36 #include <linux/proc_fs.h>
37 #include <linux/rcupdate.h>
38 #include <linux/sched.h>
39 #include <linux/backing-dev.h>
40 #include <linux/seq_file.h>
41 #include <linux/slab.h>
42 #include <linux/magic.h>
43 #include <linux/spinlock.h>
44 #include <linux/string.h>
45 #include <linux/sort.h>
46 #include <linux/kmod.h>
47 #include <linux/module.h>
48 #include <linux/delayacct.h>
49 #include <linux/cgroupstats.h>
50 #include <linux/hash.h>
51 #include <linux/namei.h>
52 #include <linux/smp_lock.h>
53 #include <linux/pid_namespace.h>
54 #include <linux/idr.h>
55 #include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
57 #include <asm/atomic.h>
59 static DEFINE_MUTEX(cgroup_mutex
);
62 * Generate an array of cgroup subsystem pointers. At boot time, this is
63 * populated up to CGROUP_BUILTIN_SUBSYS_COUNT, and modular subsystems are
64 * registered after that. The mutable section of this array is protected by
67 #define SUBSYS(_x) &_x ## _subsys,
68 static struct cgroup_subsys
*subsys
[CGROUP_SUBSYS_COUNT
] = {
69 #include <linux/cgroup_subsys.h>
72 #define MAX_CGROUP_ROOT_NAMELEN 64
75 * A cgroupfs_root represents the root of a cgroup hierarchy,
76 * and may be associated with a superblock to form an active
79 struct cgroupfs_root
{
80 struct super_block
*sb
;
83 * The bitmask of subsystems intended to be attached to this
86 unsigned long subsys_bits
;
88 /* Unique id for this hierarchy. */
91 /* The bitmask of subsystems currently attached to this hierarchy */
92 unsigned long actual_subsys_bits
;
94 /* A list running through the attached subsystems */
95 struct list_head subsys_list
;
97 /* The root cgroup for this hierarchy */
98 struct cgroup top_cgroup
;
100 /* Tracks how many cgroups are currently defined in hierarchy.*/
101 int number_of_cgroups
;
103 /* A list running through the active hierarchies */
104 struct list_head root_list
;
106 /* Hierarchy-specific flags */
109 /* The path to use for release notifications. */
110 char release_agent_path
[PATH_MAX
];
112 /* The name for this hierarchy - may be empty */
113 char name
[MAX_CGROUP_ROOT_NAMELEN
];
117 * The "rootnode" hierarchy is the "dummy hierarchy", reserved for the
118 * subsystems that are otherwise unattached - it never has more than a
119 * single cgroup, and all tasks are part of that cgroup.
121 static struct cgroupfs_root rootnode
;
124 * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when
125 * cgroup_subsys->use_id != 0.
127 #define CSS_ID_MAX (65535)
130 * The css to which this ID points. This pointer is set to valid value
131 * after cgroup is populated. If cgroup is removed, this will be NULL.
132 * This pointer is expected to be RCU-safe because destroy()
133 * is called after synchronize_rcu(). But for safe use, css_is_removed()
134 * css_tryget() should be used for avoiding race.
136 struct cgroup_subsys_state
*css
;
142 * Depth in hierarchy which this ID belongs to.
144 unsigned short depth
;
146 * ID is freed by RCU. (and lookup routine is RCU safe.)
148 struct rcu_head rcu_head
;
150 * Hierarchy of CSS ID belongs to.
152 unsigned short stack
[0]; /* Array of Length (depth+1) */
156 /* The list of hierarchy roots */
158 static LIST_HEAD(roots
);
159 static int root_count
;
161 static DEFINE_IDA(hierarchy_ida
);
162 static int next_hierarchy_id
;
163 static DEFINE_SPINLOCK(hierarchy_id_lock
);
165 /* dummytop is a shorthand for the dummy hierarchy's top cgroup */
166 #define dummytop (&rootnode.top_cgroup)
168 /* This flag indicates whether tasks in the fork and exit paths should
169 * check for fork/exit handlers to call. This avoids us having to do
170 * extra work in the fork/exit path if none of the subsystems need to
173 static int need_forkexit_callback __read_mostly
;
175 #ifdef CONFIG_PROVE_LOCKING
176 int cgroup_lock_is_held(void)
178 return lockdep_is_held(&cgroup_mutex
);
180 #else /* #ifdef CONFIG_PROVE_LOCKING */
181 int cgroup_lock_is_held(void)
183 return mutex_is_locked(&cgroup_mutex
);
185 #endif /* #else #ifdef CONFIG_PROVE_LOCKING */
187 EXPORT_SYMBOL_GPL(cgroup_lock_is_held
);
189 /* convenient tests for these bits */
190 inline int cgroup_is_removed(const struct cgroup
*cgrp
)
192 return test_bit(CGRP_REMOVED
, &cgrp
->flags
);
195 /* bits in struct cgroupfs_root flags field */
197 ROOT_NOPREFIX
, /* mounted subsystems have no named prefix */
200 static int cgroup_is_releasable(const struct cgroup
*cgrp
)
203 (1 << CGRP_RELEASABLE
) |
204 (1 << CGRP_NOTIFY_ON_RELEASE
);
205 return (cgrp
->flags
& bits
) == bits
;
208 static int notify_on_release(const struct cgroup
*cgrp
)
210 return test_bit(CGRP_NOTIFY_ON_RELEASE
, &cgrp
->flags
);
214 * for_each_subsys() allows you to iterate on each subsystem attached to
215 * an active hierarchy
217 #define for_each_subsys(_root, _ss) \
218 list_for_each_entry(_ss, &_root->subsys_list, sibling)
220 /* for_each_active_root() allows you to iterate across the active hierarchies */
221 #define for_each_active_root(_root) \
222 list_for_each_entry(_root, &roots, root_list)
224 /* the list of cgroups eligible for automatic release. Protected by
225 * release_list_lock */
226 static LIST_HEAD(release_list
);
227 static DEFINE_SPINLOCK(release_list_lock
);
228 static void cgroup_release_agent(struct work_struct
*work
);
229 static DECLARE_WORK(release_agent_work
, cgroup_release_agent
);
230 static void check_for_release(struct cgroup
*cgrp
);
232 /* Link structure for associating css_set objects with cgroups */
233 struct cg_cgroup_link
{
235 * List running through cg_cgroup_links associated with a
236 * cgroup, anchored on cgroup->css_sets
238 struct list_head cgrp_link_list
;
241 * List running through cg_cgroup_links pointing at a
242 * single css_set object, anchored on css_set->cg_links
244 struct list_head cg_link_list
;
248 /* The default css_set - used by init and its children prior to any
249 * hierarchies being mounted. It contains a pointer to the root state
250 * for each subsystem. Also used to anchor the list of css_sets. Not
251 * reference-counted, to improve performance when child cgroups
252 * haven't been created.
255 static struct css_set init_css_set
;
256 static struct cg_cgroup_link init_css_set_link
;
258 static int cgroup_init_idr(struct cgroup_subsys
*ss
,
259 struct cgroup_subsys_state
*css
);
261 /* css_set_lock protects the list of css_set objects, and the
262 * chain of tasks off each css_set. Nests outside task->alloc_lock
263 * due to cgroup_iter_start() */
264 static DEFINE_RWLOCK(css_set_lock
);
265 static int css_set_count
;
268 * hash table for cgroup groups. This improves the performance to find
269 * an existing css_set. This hash doesn't (currently) take into
270 * account cgroups in empty hierarchies.
272 #define CSS_SET_HASH_BITS 7
273 #define CSS_SET_TABLE_SIZE (1 << CSS_SET_HASH_BITS)
274 static struct hlist_head css_set_table
[CSS_SET_TABLE_SIZE
];
276 static struct hlist_head
*css_set_hash(struct cgroup_subsys_state
*css
[])
280 unsigned long tmp
= 0UL;
282 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++)
283 tmp
+= (unsigned long)css
[i
];
284 tmp
= (tmp
>> 16) ^ tmp
;
286 index
= hash_long(tmp
, CSS_SET_HASH_BITS
);
288 return &css_set_table
[index
];
291 static void free_css_set_rcu(struct rcu_head
*obj
)
293 struct css_set
*cg
= container_of(obj
, struct css_set
, rcu_head
);
297 /* We don't maintain the lists running through each css_set to its
298 * task until after the first call to cgroup_iter_start(). This
299 * reduces the fork()/exit() overhead for people who have cgroups
300 * compiled into their kernel but not actually in use */
301 static int use_task_css_set_links __read_mostly
;
303 static void __put_css_set(struct css_set
*cg
, int taskexit
)
305 struct cg_cgroup_link
*link
;
306 struct cg_cgroup_link
*saved_link
;
308 * Ensure that the refcount doesn't hit zero while any readers
309 * can see it. Similar to atomic_dec_and_lock(), but for an
312 if (atomic_add_unless(&cg
->refcount
, -1, 1))
314 write_lock(&css_set_lock
);
315 if (!atomic_dec_and_test(&cg
->refcount
)) {
316 write_unlock(&css_set_lock
);
320 /* This css_set is dead. unlink it and release cgroup refcounts */
321 hlist_del(&cg
->hlist
);
324 list_for_each_entry_safe(link
, saved_link
, &cg
->cg_links
,
326 struct cgroup
*cgrp
= link
->cgrp
;
327 list_del(&link
->cg_link_list
);
328 list_del(&link
->cgrp_link_list
);
329 if (atomic_dec_and_test(&cgrp
->count
) &&
330 notify_on_release(cgrp
)) {
332 set_bit(CGRP_RELEASABLE
, &cgrp
->flags
);
333 check_for_release(cgrp
);
339 write_unlock(&css_set_lock
);
340 call_rcu(&cg
->rcu_head
, free_css_set_rcu
);
344 * refcounted get/put for css_set objects
346 static inline void get_css_set(struct css_set
*cg
)
348 atomic_inc(&cg
->refcount
);
351 static inline void put_css_set(struct css_set
*cg
)
353 __put_css_set(cg
, 0);
356 static inline void put_css_set_taskexit(struct css_set
*cg
)
358 __put_css_set(cg
, 1);
362 * compare_css_sets - helper function for find_existing_css_set().
363 * @cg: candidate css_set being tested
364 * @old_cg: existing css_set for a task
365 * @new_cgrp: cgroup that's being entered by the task
366 * @template: desired set of css pointers in css_set (pre-calculated)
368 * Returns true if "cg" matches "old_cg" except for the hierarchy
369 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
371 static bool compare_css_sets(struct css_set
*cg
,
372 struct css_set
*old_cg
,
373 struct cgroup
*new_cgrp
,
374 struct cgroup_subsys_state
*template[])
376 struct list_head
*l1
, *l2
;
378 if (memcmp(template, cg
->subsys
, sizeof(cg
->subsys
))) {
379 /* Not all subsystems matched */
384 * Compare cgroup pointers in order to distinguish between
385 * different cgroups in heirarchies with no subsystems. We
386 * could get by with just this check alone (and skip the
387 * memcmp above) but on most setups the memcmp check will
388 * avoid the need for this more expensive check on almost all
393 l2
= &old_cg
->cg_links
;
395 struct cg_cgroup_link
*cgl1
, *cgl2
;
396 struct cgroup
*cg1
, *cg2
;
400 /* See if we reached the end - both lists are equal length. */
401 if (l1
== &cg
->cg_links
) {
402 BUG_ON(l2
!= &old_cg
->cg_links
);
405 BUG_ON(l2
== &old_cg
->cg_links
);
407 /* Locate the cgroups associated with these links. */
408 cgl1
= list_entry(l1
, struct cg_cgroup_link
, cg_link_list
);
409 cgl2
= list_entry(l2
, struct cg_cgroup_link
, cg_link_list
);
412 /* Hierarchies should be linked in the same order. */
413 BUG_ON(cg1
->root
!= cg2
->root
);
416 * If this hierarchy is the hierarchy of the cgroup
417 * that's changing, then we need to check that this
418 * css_set points to the new cgroup; if it's any other
419 * hierarchy, then this css_set should point to the
420 * same cgroup as the old css_set.
422 if (cg1
->root
== new_cgrp
->root
) {
434 * find_existing_css_set() is a helper for
435 * find_css_set(), and checks to see whether an existing
436 * css_set is suitable.
438 * oldcg: the cgroup group that we're using before the cgroup
441 * cgrp: the cgroup that we're moving into
443 * template: location in which to build the desired set of subsystem
444 * state objects for the new cgroup group
446 static struct css_set
*find_existing_css_set(
447 struct css_set
*oldcg
,
449 struct cgroup_subsys_state
*template[])
452 struct cgroupfs_root
*root
= cgrp
->root
;
453 struct hlist_head
*hhead
;
454 struct hlist_node
*node
;
458 * Build the set of subsystem state objects that we want to see in the
459 * new css_set. while subsystems can change globally, the entries here
460 * won't change, so no need for locking.
462 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
463 if (root
->subsys_bits
& (1UL << i
)) {
464 /* Subsystem is in this hierarchy. So we want
465 * the subsystem state from the new
467 template[i
] = cgrp
->subsys
[i
];
469 /* Subsystem is not in this hierarchy, so we
470 * don't want to change the subsystem state */
471 template[i
] = oldcg
->subsys
[i
];
475 hhead
= css_set_hash(template);
476 hlist_for_each_entry(cg
, node
, hhead
, hlist
) {
477 if (!compare_css_sets(cg
, oldcg
, cgrp
, template))
480 /* This css_set matches what we need */
484 /* No existing cgroup group matched */
488 static void free_cg_links(struct list_head
*tmp
)
490 struct cg_cgroup_link
*link
;
491 struct cg_cgroup_link
*saved_link
;
493 list_for_each_entry_safe(link
, saved_link
, tmp
, cgrp_link_list
) {
494 list_del(&link
->cgrp_link_list
);
500 * allocate_cg_links() allocates "count" cg_cgroup_link structures
501 * and chains them on tmp through their cgrp_link_list fields. Returns 0 on
502 * success or a negative error
504 static int allocate_cg_links(int count
, struct list_head
*tmp
)
506 struct cg_cgroup_link
*link
;
509 for (i
= 0; i
< count
; i
++) {
510 link
= kmalloc(sizeof(*link
), GFP_KERNEL
);
515 list_add(&link
->cgrp_link_list
, tmp
);
521 * link_css_set - a helper function to link a css_set to a cgroup
522 * @tmp_cg_links: cg_cgroup_link objects allocated by allocate_cg_links()
523 * @cg: the css_set to be linked
524 * @cgrp: the destination cgroup
526 static void link_css_set(struct list_head
*tmp_cg_links
,
527 struct css_set
*cg
, struct cgroup
*cgrp
)
529 struct cg_cgroup_link
*link
;
531 BUG_ON(list_empty(tmp_cg_links
));
532 link
= list_first_entry(tmp_cg_links
, struct cg_cgroup_link
,
536 atomic_inc(&cgrp
->count
);
537 list_move(&link
->cgrp_link_list
, &cgrp
->css_sets
);
539 * Always add links to the tail of the list so that the list
540 * is sorted by order of hierarchy creation
542 list_add_tail(&link
->cg_link_list
, &cg
->cg_links
);
546 * find_css_set() takes an existing cgroup group and a
547 * cgroup object, and returns a css_set object that's
548 * equivalent to the old group, but with the given cgroup
549 * substituted into the appropriate hierarchy. Must be called with
552 static struct css_set
*find_css_set(
553 struct css_set
*oldcg
, struct cgroup
*cgrp
)
556 struct cgroup_subsys_state
*template[CGROUP_SUBSYS_COUNT
];
558 struct list_head tmp_cg_links
;
560 struct hlist_head
*hhead
;
561 struct cg_cgroup_link
*link
;
563 /* First see if we already have a cgroup group that matches
565 read_lock(&css_set_lock
);
566 res
= find_existing_css_set(oldcg
, cgrp
, template);
569 read_unlock(&css_set_lock
);
574 res
= kmalloc(sizeof(*res
), GFP_KERNEL
);
578 /* Allocate all the cg_cgroup_link objects that we'll need */
579 if (allocate_cg_links(root_count
, &tmp_cg_links
) < 0) {
584 atomic_set(&res
->refcount
, 1);
585 INIT_LIST_HEAD(&res
->cg_links
);
586 INIT_LIST_HEAD(&res
->tasks
);
587 INIT_HLIST_NODE(&res
->hlist
);
589 /* Copy the set of subsystem state objects generated in
590 * find_existing_css_set() */
591 memcpy(res
->subsys
, template, sizeof(res
->subsys
));
593 write_lock(&css_set_lock
);
594 /* Add reference counts and links from the new css_set. */
595 list_for_each_entry(link
, &oldcg
->cg_links
, cg_link_list
) {
596 struct cgroup
*c
= link
->cgrp
;
597 if (c
->root
== cgrp
->root
)
599 link_css_set(&tmp_cg_links
, res
, c
);
602 BUG_ON(!list_empty(&tmp_cg_links
));
606 /* Add this cgroup group to the hash table */
607 hhead
= css_set_hash(res
->subsys
);
608 hlist_add_head(&res
->hlist
, hhead
);
610 write_unlock(&css_set_lock
);
616 * Return the cgroup for "task" from the given hierarchy. Must be
617 * called with cgroup_mutex held.
619 static struct cgroup
*task_cgroup_from_root(struct task_struct
*task
,
620 struct cgroupfs_root
*root
)
623 struct cgroup
*res
= NULL
;
625 BUG_ON(!mutex_is_locked(&cgroup_mutex
));
626 read_lock(&css_set_lock
);
628 * No need to lock the task - since we hold cgroup_mutex the
629 * task can't change groups, so the only thing that can happen
630 * is that it exits and its css is set back to init_css_set.
633 if (css
== &init_css_set
) {
634 res
= &root
->top_cgroup
;
636 struct cg_cgroup_link
*link
;
637 list_for_each_entry(link
, &css
->cg_links
, cg_link_list
) {
638 struct cgroup
*c
= link
->cgrp
;
639 if (c
->root
== root
) {
645 read_unlock(&css_set_lock
);
651 * There is one global cgroup mutex. We also require taking
652 * task_lock() when dereferencing a task's cgroup subsys pointers.
653 * See "The task_lock() exception", at the end of this comment.
655 * A task must hold cgroup_mutex to modify cgroups.
657 * Any task can increment and decrement the count field without lock.
658 * So in general, code holding cgroup_mutex can't rely on the count
659 * field not changing. However, if the count goes to zero, then only
660 * cgroup_attach_task() can increment it again. Because a count of zero
661 * means that no tasks are currently attached, therefore there is no
662 * way a task attached to that cgroup can fork (the other way to
663 * increment the count). So code holding cgroup_mutex can safely
664 * assume that if the count is zero, it will stay zero. Similarly, if
665 * a task holds cgroup_mutex on a cgroup with zero count, it
666 * knows that the cgroup won't be removed, as cgroup_rmdir()
669 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
670 * (usually) take cgroup_mutex. These are the two most performance
671 * critical pieces of code here. The exception occurs on cgroup_exit(),
672 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
673 * is taken, and if the cgroup count is zero, a usermode call made
674 * to the release agent with the name of the cgroup (path relative to
675 * the root of cgroup file system) as the argument.
677 * A cgroup can only be deleted if both its 'count' of using tasks
678 * is zero, and its list of 'children' cgroups is empty. Since all
679 * tasks in the system use _some_ cgroup, and since there is always at
680 * least one task in the system (init, pid == 1), therefore, top_cgroup
681 * always has either children cgroups and/or using tasks. So we don't
682 * need a special hack to ensure that top_cgroup cannot be deleted.
684 * The task_lock() exception
686 * The need for this exception arises from the action of
687 * cgroup_attach_task(), which overwrites one tasks cgroup pointer with
688 * another. It does so using cgroup_mutex, however there are
689 * several performance critical places that need to reference
690 * task->cgroup without the expense of grabbing a system global
691 * mutex. Therefore except as noted below, when dereferencing or, as
692 * in cgroup_attach_task(), modifying a task'ss cgroup pointer we use
693 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
694 * the task_struct routinely used for such matters.
696 * P.S. One more locking exception. RCU is used to guard the
697 * update of a tasks cgroup pointer by cgroup_attach_task()
701 * cgroup_lock - lock out any changes to cgroup structures
704 void cgroup_lock(void)
706 mutex_lock(&cgroup_mutex
);
710 * cgroup_unlock - release lock on cgroup changes
712 * Undo the lock taken in a previous cgroup_lock() call.
714 void cgroup_unlock(void)
716 mutex_unlock(&cgroup_mutex
);
720 * A couple of forward declarations required, due to cyclic reference loop:
721 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
722 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
726 static int cgroup_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
);
727 static int cgroup_rmdir(struct inode
*unused_dir
, struct dentry
*dentry
);
728 static int cgroup_populate_dir(struct cgroup
*cgrp
);
729 static const struct inode_operations cgroup_dir_inode_operations
;
730 static const struct file_operations proc_cgroupstats_operations
;
732 static struct backing_dev_info cgroup_backing_dev_info
= {
734 .capabilities
= BDI_CAP_NO_ACCT_AND_WRITEBACK
,
737 static int alloc_css_id(struct cgroup_subsys
*ss
,
738 struct cgroup
*parent
, struct cgroup
*child
);
740 static struct inode
*cgroup_new_inode(mode_t mode
, struct super_block
*sb
)
742 struct inode
*inode
= new_inode(sb
);
745 inode
->i_mode
= mode
;
746 inode
->i_uid
= current_fsuid();
747 inode
->i_gid
= current_fsgid();
748 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
749 inode
->i_mapping
->backing_dev_info
= &cgroup_backing_dev_info
;
755 * Call subsys's pre_destroy handler.
756 * This is called before css refcnt check.
758 static int cgroup_call_pre_destroy(struct cgroup
*cgrp
)
760 struct cgroup_subsys
*ss
;
763 for_each_subsys(cgrp
->root
, ss
)
764 if (ss
->pre_destroy
) {
765 ret
= ss
->pre_destroy(ss
, cgrp
);
772 static void free_cgroup_rcu(struct rcu_head
*obj
)
774 struct cgroup
*cgrp
= container_of(obj
, struct cgroup
, rcu_head
);
779 static void cgroup_diput(struct dentry
*dentry
, struct inode
*inode
)
781 /* is dentry a directory ? if so, kfree() associated cgroup */
782 if (S_ISDIR(inode
->i_mode
)) {
783 struct cgroup
*cgrp
= dentry
->d_fsdata
;
784 struct cgroup_subsys
*ss
;
785 BUG_ON(!(cgroup_is_removed(cgrp
)));
786 /* It's possible for external users to be holding css
787 * reference counts on a cgroup; css_put() needs to
788 * be able to access the cgroup after decrementing
789 * the reference count in order to know if it needs to
790 * queue the cgroup to be handled by the release
794 mutex_lock(&cgroup_mutex
);
796 * Release the subsystem state objects.
798 for_each_subsys(cgrp
->root
, ss
)
799 ss
->destroy(ss
, cgrp
);
801 cgrp
->root
->number_of_cgroups
--;
802 mutex_unlock(&cgroup_mutex
);
805 * Drop the active superblock reference that we took when we
808 deactivate_super(cgrp
->root
->sb
);
811 * if we're getting rid of the cgroup, refcount should ensure
812 * that there are no pidlists left.
814 BUG_ON(!list_empty(&cgrp
->pidlists
));
816 call_rcu(&cgrp
->rcu_head
, free_cgroup_rcu
);
821 static void remove_dir(struct dentry
*d
)
823 struct dentry
*parent
= dget(d
->d_parent
);
826 simple_rmdir(parent
->d_inode
, d
);
830 static void cgroup_clear_directory(struct dentry
*dentry
)
832 struct list_head
*node
;
834 BUG_ON(!mutex_is_locked(&dentry
->d_inode
->i_mutex
));
835 spin_lock(&dcache_lock
);
836 node
= dentry
->d_subdirs
.next
;
837 while (node
!= &dentry
->d_subdirs
) {
838 struct dentry
*d
= list_entry(node
, struct dentry
, d_u
.d_child
);
841 /* This should never be called on a cgroup
842 * directory with child cgroups */
843 BUG_ON(d
->d_inode
->i_mode
& S_IFDIR
);
845 spin_unlock(&dcache_lock
);
847 simple_unlink(dentry
->d_inode
, d
);
849 spin_lock(&dcache_lock
);
851 node
= dentry
->d_subdirs
.next
;
853 spin_unlock(&dcache_lock
);
857 * NOTE : the dentry must have been dget()'ed
859 static void cgroup_d_remove_dir(struct dentry
*dentry
)
861 cgroup_clear_directory(dentry
);
863 spin_lock(&dcache_lock
);
864 list_del_init(&dentry
->d_u
.d_child
);
865 spin_unlock(&dcache_lock
);
870 * A queue for waiters to do rmdir() cgroup. A tasks will sleep when
871 * cgroup->count == 0 && list_empty(&cgroup->children) && subsys has some
872 * reference to css->refcnt. In general, this refcnt is expected to goes down
875 * CGRP_WAIT_ON_RMDIR flag is set under cgroup's inode->i_mutex;
877 DECLARE_WAIT_QUEUE_HEAD(cgroup_rmdir_waitq
);
879 static void cgroup_wakeup_rmdir_waiter(struct cgroup
*cgrp
)
881 if (unlikely(test_and_clear_bit(CGRP_WAIT_ON_RMDIR
, &cgrp
->flags
)))
882 wake_up_all(&cgroup_rmdir_waitq
);
885 void cgroup_exclude_rmdir(struct cgroup_subsys_state
*css
)
890 void cgroup_release_and_wakeup_rmdir(struct cgroup_subsys_state
*css
)
892 cgroup_wakeup_rmdir_waiter(css
->cgroup
);
897 * Call with cgroup_mutex held. Drops reference counts on modules, including
898 * any duplicate ones that parse_cgroupfs_options took. If this function
899 * returns an error, no reference counts are touched.
901 static int rebind_subsystems(struct cgroupfs_root
*root
,
902 unsigned long final_bits
)
904 unsigned long added_bits
, removed_bits
;
905 struct cgroup
*cgrp
= &root
->top_cgroup
;
908 BUG_ON(!mutex_is_locked(&cgroup_mutex
));
910 removed_bits
= root
->actual_subsys_bits
& ~final_bits
;
911 added_bits
= final_bits
& ~root
->actual_subsys_bits
;
912 /* Check that any added subsystems are currently free */
913 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
914 unsigned long bit
= 1UL << i
;
915 struct cgroup_subsys
*ss
= subsys
[i
];
916 if (!(bit
& added_bits
))
919 * Nobody should tell us to do a subsys that doesn't exist:
920 * parse_cgroupfs_options should catch that case and refcounts
921 * ensure that subsystems won't disappear once selected.
924 if (ss
->root
!= &rootnode
) {
925 /* Subsystem isn't free */
930 /* Currently we don't handle adding/removing subsystems when
931 * any child cgroups exist. This is theoretically supportable
932 * but involves complex error handling, so it's being left until
934 if (root
->number_of_cgroups
> 1)
937 /* Process each subsystem */
938 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
939 struct cgroup_subsys
*ss
= subsys
[i
];
940 unsigned long bit
= 1UL << i
;
941 if (bit
& added_bits
) {
942 /* We're binding this subsystem to this hierarchy */
944 BUG_ON(cgrp
->subsys
[i
]);
945 BUG_ON(!dummytop
->subsys
[i
]);
946 BUG_ON(dummytop
->subsys
[i
]->cgroup
!= dummytop
);
947 mutex_lock(&ss
->hierarchy_mutex
);
948 cgrp
->subsys
[i
] = dummytop
->subsys
[i
];
949 cgrp
->subsys
[i
]->cgroup
= cgrp
;
950 list_move(&ss
->sibling
, &root
->subsys_list
);
954 mutex_unlock(&ss
->hierarchy_mutex
);
955 /* refcount was already taken, and we're keeping it */
956 } else if (bit
& removed_bits
) {
957 /* We're removing this subsystem */
959 BUG_ON(cgrp
->subsys
[i
] != dummytop
->subsys
[i
]);
960 BUG_ON(cgrp
->subsys
[i
]->cgroup
!= cgrp
);
961 mutex_lock(&ss
->hierarchy_mutex
);
963 ss
->bind(ss
, dummytop
);
964 dummytop
->subsys
[i
]->cgroup
= dummytop
;
965 cgrp
->subsys
[i
] = NULL
;
966 subsys
[i
]->root
= &rootnode
;
967 list_move(&ss
->sibling
, &rootnode
.subsys_list
);
968 mutex_unlock(&ss
->hierarchy_mutex
);
969 /* subsystem is now free - drop reference on module */
970 module_put(ss
->module
);
971 } else if (bit
& final_bits
) {
972 /* Subsystem state should already exist */
974 BUG_ON(!cgrp
->subsys
[i
]);
976 * a refcount was taken, but we already had one, so
977 * drop the extra reference.
979 module_put(ss
->module
);
980 #ifdef CONFIG_MODULE_UNLOAD
981 BUG_ON(ss
->module
&& !module_refcount(ss
->module
));
984 /* Subsystem state shouldn't exist */
985 BUG_ON(cgrp
->subsys
[i
]);
988 root
->subsys_bits
= root
->actual_subsys_bits
= final_bits
;
994 static int cgroup_show_options(struct seq_file
*seq
, struct vfsmount
*vfs
)
996 struct cgroupfs_root
*root
= vfs
->mnt_sb
->s_fs_info
;
997 struct cgroup_subsys
*ss
;
999 mutex_lock(&cgroup_mutex
);
1000 for_each_subsys(root
, ss
)
1001 seq_printf(seq
, ",%s", ss
->name
);
1002 if (test_bit(ROOT_NOPREFIX
, &root
->flags
))
1003 seq_puts(seq
, ",noprefix");
1004 if (strlen(root
->release_agent_path
))
1005 seq_printf(seq
, ",release_agent=%s", root
->release_agent_path
);
1006 if (strlen(root
->name
))
1007 seq_printf(seq
, ",name=%s", root
->name
);
1008 mutex_unlock(&cgroup_mutex
);
1012 struct cgroup_sb_opts
{
1013 unsigned long subsys_bits
;
1014 unsigned long flags
;
1015 char *release_agent
;
1017 /* User explicitly requested empty subsystem */
1020 struct cgroupfs_root
*new_root
;
1025 * Convert a hierarchy specifier into a bitmask of subsystems and flags. Call
1026 * with cgroup_mutex held to protect the subsys[] array. This function takes
1027 * refcounts on subsystems to be used, unless it returns error, in which case
1028 * no refcounts are taken.
1030 static int parse_cgroupfs_options(char *data
, struct cgroup_sb_opts
*opts
)
1032 char *token
, *o
= data
?: "all";
1033 unsigned long mask
= (unsigned long)-1;
1035 bool module_pin_failed
= false;
1037 BUG_ON(!mutex_is_locked(&cgroup_mutex
));
1039 #ifdef CONFIG_CPUSETS
1040 mask
= ~(1UL << cpuset_subsys_id
);
1043 memset(opts
, 0, sizeof(*opts
));
1045 while ((token
= strsep(&o
, ",")) != NULL
) {
1048 if (!strcmp(token
, "all")) {
1049 /* Add all non-disabled subsystems */
1050 opts
->subsys_bits
= 0;
1051 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
1052 struct cgroup_subsys
*ss
= subsys
[i
];
1056 opts
->subsys_bits
|= 1ul << i
;
1058 } else if (!strcmp(token
, "none")) {
1059 /* Explicitly have no subsystems */
1061 } else if (!strcmp(token
, "noprefix")) {
1062 set_bit(ROOT_NOPREFIX
, &opts
->flags
);
1063 } else if (!strncmp(token
, "release_agent=", 14)) {
1064 /* Specifying two release agents is forbidden */
1065 if (opts
->release_agent
)
1067 opts
->release_agent
=
1068 kstrndup(token
+ 14, PATH_MAX
, GFP_KERNEL
);
1069 if (!opts
->release_agent
)
1071 } else if (!strncmp(token
, "name=", 5)) {
1072 const char *name
= token
+ 5;
1073 /* Can't specify an empty name */
1076 /* Must match [\w.-]+ */
1077 for (i
= 0; i
< strlen(name
); i
++) {
1081 if ((c
== '.') || (c
== '-') || (c
== '_'))
1085 /* Specifying two names is forbidden */
1088 opts
->name
= kstrndup(name
,
1089 MAX_CGROUP_ROOT_NAMELEN
,
1094 struct cgroup_subsys
*ss
;
1095 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
1099 if (!strcmp(token
, ss
->name
)) {
1101 set_bit(i
, &opts
->subsys_bits
);
1105 if (i
== CGROUP_SUBSYS_COUNT
)
1110 /* Consistency checks */
1113 * Option noprefix was introduced just for backward compatibility
1114 * with the old cpuset, so we allow noprefix only if mounting just
1115 * the cpuset subsystem.
1117 if (test_bit(ROOT_NOPREFIX
, &opts
->flags
) &&
1118 (opts
->subsys_bits
& mask
))
1122 /* Can't specify "none" and some subsystems */
1123 if (opts
->subsys_bits
&& opts
->none
)
1127 * We either have to specify by name or by subsystems. (So all
1128 * empty hierarchies must have a name).
1130 if (!opts
->subsys_bits
&& !opts
->name
)
1134 * Grab references on all the modules we'll need, so the subsystems
1135 * don't dance around before rebind_subsystems attaches them. This may
1136 * take duplicate reference counts on a subsystem that's already used,
1137 * but rebind_subsystems handles this case.
1139 for (i
= CGROUP_BUILTIN_SUBSYS_COUNT
; i
< CGROUP_SUBSYS_COUNT
; i
++) {
1140 unsigned long bit
= 1UL << i
;
1142 if (!(bit
& opts
->subsys_bits
))
1144 if (!try_module_get(subsys
[i
]->module
)) {
1145 module_pin_failed
= true;
1149 if (module_pin_failed
) {
1151 * oops, one of the modules was going away. this means that we
1152 * raced with a module_delete call, and to the user this is
1153 * essentially a "subsystem doesn't exist" case.
1155 for (i
--; i
>= CGROUP_BUILTIN_SUBSYS_COUNT
; i
--) {
1156 /* drop refcounts only on the ones we took */
1157 unsigned long bit
= 1UL << i
;
1159 if (!(bit
& opts
->subsys_bits
))
1161 module_put(subsys
[i
]->module
);
1169 static void drop_parsed_module_refcounts(unsigned long subsys_bits
)
1172 for (i
= CGROUP_BUILTIN_SUBSYS_COUNT
; i
< CGROUP_SUBSYS_COUNT
; i
++) {
1173 unsigned long bit
= 1UL << i
;
1175 if (!(bit
& subsys_bits
))
1177 module_put(subsys
[i
]->module
);
1181 static int cgroup_remount(struct super_block
*sb
, int *flags
, char *data
)
1184 struct cgroupfs_root
*root
= sb
->s_fs_info
;
1185 struct cgroup
*cgrp
= &root
->top_cgroup
;
1186 struct cgroup_sb_opts opts
;
1189 mutex_lock(&cgrp
->dentry
->d_inode
->i_mutex
);
1190 mutex_lock(&cgroup_mutex
);
1192 /* See what subsystems are wanted */
1193 ret
= parse_cgroupfs_options(data
, &opts
);
1197 /* Don't allow flags or name to change at remount */
1198 if (opts
.flags
!= root
->flags
||
1199 (opts
.name
&& strcmp(opts
.name
, root
->name
))) {
1201 drop_parsed_module_refcounts(opts
.subsys_bits
);
1205 ret
= rebind_subsystems(root
, opts
.subsys_bits
);
1207 drop_parsed_module_refcounts(opts
.subsys_bits
);
1211 /* (re)populate subsystem files */
1212 cgroup_populate_dir(cgrp
);
1214 if (opts
.release_agent
)
1215 strcpy(root
->release_agent_path
, opts
.release_agent
);
1217 kfree(opts
.release_agent
);
1219 mutex_unlock(&cgroup_mutex
);
1220 mutex_unlock(&cgrp
->dentry
->d_inode
->i_mutex
);
1225 static const struct super_operations cgroup_ops
= {
1226 .statfs
= simple_statfs
,
1227 .drop_inode
= generic_delete_inode
,
1228 .show_options
= cgroup_show_options
,
1229 .remount_fs
= cgroup_remount
,
1232 static void init_cgroup_housekeeping(struct cgroup
*cgrp
)
1234 INIT_LIST_HEAD(&cgrp
->sibling
);
1235 INIT_LIST_HEAD(&cgrp
->children
);
1236 INIT_LIST_HEAD(&cgrp
->css_sets
);
1237 INIT_LIST_HEAD(&cgrp
->release_list
);
1238 INIT_LIST_HEAD(&cgrp
->pidlists
);
1239 mutex_init(&cgrp
->pidlist_mutex
);
1242 static void init_cgroup_root(struct cgroupfs_root
*root
)
1244 struct cgroup
*cgrp
= &root
->top_cgroup
;
1245 INIT_LIST_HEAD(&root
->subsys_list
);
1246 INIT_LIST_HEAD(&root
->root_list
);
1247 root
->number_of_cgroups
= 1;
1249 cgrp
->top_cgroup
= cgrp
;
1250 init_cgroup_housekeeping(cgrp
);
1253 static bool init_root_id(struct cgroupfs_root
*root
)
1258 if (!ida_pre_get(&hierarchy_ida
, GFP_KERNEL
))
1260 spin_lock(&hierarchy_id_lock
);
1261 /* Try to allocate the next unused ID */
1262 ret
= ida_get_new_above(&hierarchy_ida
, next_hierarchy_id
,
1263 &root
->hierarchy_id
);
1265 /* Try again starting from 0 */
1266 ret
= ida_get_new(&hierarchy_ida
, &root
->hierarchy_id
);
1268 next_hierarchy_id
= root
->hierarchy_id
+ 1;
1269 } else if (ret
!= -EAGAIN
) {
1270 /* Can only get here if the 31-bit IDR is full ... */
1273 spin_unlock(&hierarchy_id_lock
);
1278 static int cgroup_test_super(struct super_block
*sb
, void *data
)
1280 struct cgroup_sb_opts
*opts
= data
;
1281 struct cgroupfs_root
*root
= sb
->s_fs_info
;
1283 /* If we asked for a name then it must match */
1284 if (opts
->name
&& strcmp(opts
->name
, root
->name
))
1288 * If we asked for subsystems (or explicitly for no
1289 * subsystems) then they must match
1291 if ((opts
->subsys_bits
|| opts
->none
)
1292 && (opts
->subsys_bits
!= root
->subsys_bits
))
1298 static struct cgroupfs_root
*cgroup_root_from_opts(struct cgroup_sb_opts
*opts
)
1300 struct cgroupfs_root
*root
;
1302 if (!opts
->subsys_bits
&& !opts
->none
)
1305 root
= kzalloc(sizeof(*root
), GFP_KERNEL
);
1307 return ERR_PTR(-ENOMEM
);
1309 if (!init_root_id(root
)) {
1311 return ERR_PTR(-ENOMEM
);
1313 init_cgroup_root(root
);
1315 root
->subsys_bits
= opts
->subsys_bits
;
1316 root
->flags
= opts
->flags
;
1317 if (opts
->release_agent
)
1318 strcpy(root
->release_agent_path
, opts
->release_agent
);
1320 strcpy(root
->name
, opts
->name
);
1324 static void cgroup_drop_root(struct cgroupfs_root
*root
)
1329 BUG_ON(!root
->hierarchy_id
);
1330 spin_lock(&hierarchy_id_lock
);
1331 ida_remove(&hierarchy_ida
, root
->hierarchy_id
);
1332 spin_unlock(&hierarchy_id_lock
);
1336 static int cgroup_set_super(struct super_block
*sb
, void *data
)
1339 struct cgroup_sb_opts
*opts
= data
;
1341 /* If we don't have a new root, we can't set up a new sb */
1342 if (!opts
->new_root
)
1345 BUG_ON(!opts
->subsys_bits
&& !opts
->none
);
1347 ret
= set_anon_super(sb
, NULL
);
1351 sb
->s_fs_info
= opts
->new_root
;
1352 opts
->new_root
->sb
= sb
;
1354 sb
->s_blocksize
= PAGE_CACHE_SIZE
;
1355 sb
->s_blocksize_bits
= PAGE_CACHE_SHIFT
;
1356 sb
->s_magic
= CGROUP_SUPER_MAGIC
;
1357 sb
->s_op
= &cgroup_ops
;
1362 static int cgroup_get_rootdir(struct super_block
*sb
)
1364 struct inode
*inode
=
1365 cgroup_new_inode(S_IFDIR
| S_IRUGO
| S_IXUGO
| S_IWUSR
, sb
);
1366 struct dentry
*dentry
;
1371 inode
->i_fop
= &simple_dir_operations
;
1372 inode
->i_op
= &cgroup_dir_inode_operations
;
1373 /* directories start off with i_nlink == 2 (for "." entry) */
1375 dentry
= d_alloc_root(inode
);
1380 sb
->s_root
= dentry
;
1384 static int cgroup_get_sb(struct file_system_type
*fs_type
,
1385 int flags
, const char *unused_dev_name
,
1386 void *data
, struct vfsmount
*mnt
)
1388 struct cgroup_sb_opts opts
;
1389 struct cgroupfs_root
*root
;
1391 struct super_block
*sb
;
1392 struct cgroupfs_root
*new_root
;
1394 /* First find the desired set of subsystems */
1395 mutex_lock(&cgroup_mutex
);
1396 ret
= parse_cgroupfs_options(data
, &opts
);
1397 mutex_unlock(&cgroup_mutex
);
1402 * Allocate a new cgroup root. We may not need it if we're
1403 * reusing an existing hierarchy.
1405 new_root
= cgroup_root_from_opts(&opts
);
1406 if (IS_ERR(new_root
)) {
1407 ret
= PTR_ERR(new_root
);
1410 opts
.new_root
= new_root
;
1412 /* Locate an existing or new sb for this hierarchy */
1413 sb
= sget(fs_type
, cgroup_test_super
, cgroup_set_super
, &opts
);
1416 cgroup_drop_root(opts
.new_root
);
1420 root
= sb
->s_fs_info
;
1422 if (root
== opts
.new_root
) {
1423 /* We used the new root structure, so this is a new hierarchy */
1424 struct list_head tmp_cg_links
;
1425 struct cgroup
*root_cgrp
= &root
->top_cgroup
;
1426 struct inode
*inode
;
1427 struct cgroupfs_root
*existing_root
;
1430 BUG_ON(sb
->s_root
!= NULL
);
1432 ret
= cgroup_get_rootdir(sb
);
1434 goto drop_new_super
;
1435 inode
= sb
->s_root
->d_inode
;
1437 mutex_lock(&inode
->i_mutex
);
1438 mutex_lock(&cgroup_mutex
);
1440 if (strlen(root
->name
)) {
1441 /* Check for name clashes with existing mounts */
1442 for_each_active_root(existing_root
) {
1443 if (!strcmp(existing_root
->name
, root
->name
)) {
1445 mutex_unlock(&cgroup_mutex
);
1446 mutex_unlock(&inode
->i_mutex
);
1447 goto drop_new_super
;
1453 * We're accessing css_set_count without locking
1454 * css_set_lock here, but that's OK - it can only be
1455 * increased by someone holding cgroup_lock, and
1456 * that's us. The worst that can happen is that we
1457 * have some link structures left over
1459 ret
= allocate_cg_links(css_set_count
, &tmp_cg_links
);
1461 mutex_unlock(&cgroup_mutex
);
1462 mutex_unlock(&inode
->i_mutex
);
1463 goto drop_new_super
;
1466 ret
= rebind_subsystems(root
, root
->subsys_bits
);
1467 if (ret
== -EBUSY
) {
1468 mutex_unlock(&cgroup_mutex
);
1469 mutex_unlock(&inode
->i_mutex
);
1470 free_cg_links(&tmp_cg_links
);
1471 goto drop_new_super
;
1474 * There must be no failure case after here, since rebinding
1475 * takes care of subsystems' refcounts, which are explicitly
1476 * dropped in the failure exit path.
1479 /* EBUSY should be the only error here */
1482 list_add(&root
->root_list
, &roots
);
1485 sb
->s_root
->d_fsdata
= root_cgrp
;
1486 root
->top_cgroup
.dentry
= sb
->s_root
;
1488 /* Link the top cgroup in this hierarchy into all
1489 * the css_set objects */
1490 write_lock(&css_set_lock
);
1491 for (i
= 0; i
< CSS_SET_TABLE_SIZE
; i
++) {
1492 struct hlist_head
*hhead
= &css_set_table
[i
];
1493 struct hlist_node
*node
;
1496 hlist_for_each_entry(cg
, node
, hhead
, hlist
)
1497 link_css_set(&tmp_cg_links
, cg
, root_cgrp
);
1499 write_unlock(&css_set_lock
);
1501 free_cg_links(&tmp_cg_links
);
1503 BUG_ON(!list_empty(&root_cgrp
->sibling
));
1504 BUG_ON(!list_empty(&root_cgrp
->children
));
1505 BUG_ON(root
->number_of_cgroups
!= 1);
1507 cgroup_populate_dir(root_cgrp
);
1508 mutex_unlock(&cgroup_mutex
);
1509 mutex_unlock(&inode
->i_mutex
);
1512 * We re-used an existing hierarchy - the new root (if
1513 * any) is not needed
1515 cgroup_drop_root(opts
.new_root
);
1516 /* no subsys rebinding, so refcounts don't change */
1517 drop_parsed_module_refcounts(opts
.subsys_bits
);
1520 simple_set_mnt(mnt
, sb
);
1521 kfree(opts
.release_agent
);
1526 deactivate_locked_super(sb
);
1528 drop_parsed_module_refcounts(opts
.subsys_bits
);
1530 kfree(opts
.release_agent
);
1536 static void cgroup_kill_sb(struct super_block
*sb
) {
1537 struct cgroupfs_root
*root
= sb
->s_fs_info
;
1538 struct cgroup
*cgrp
= &root
->top_cgroup
;
1540 struct cg_cgroup_link
*link
;
1541 struct cg_cgroup_link
*saved_link
;
1545 BUG_ON(root
->number_of_cgroups
!= 1);
1546 BUG_ON(!list_empty(&cgrp
->children
));
1547 BUG_ON(!list_empty(&cgrp
->sibling
));
1549 mutex_lock(&cgroup_mutex
);
1551 /* Rebind all subsystems back to the default hierarchy */
1552 ret
= rebind_subsystems(root
, 0);
1553 /* Shouldn't be able to fail ... */
1557 * Release all the links from css_sets to this hierarchy's
1560 write_lock(&css_set_lock
);
1562 list_for_each_entry_safe(link
, saved_link
, &cgrp
->css_sets
,
1564 list_del(&link
->cg_link_list
);
1565 list_del(&link
->cgrp_link_list
);
1568 write_unlock(&css_set_lock
);
1570 if (!list_empty(&root
->root_list
)) {
1571 list_del(&root
->root_list
);
1575 mutex_unlock(&cgroup_mutex
);
1577 kill_litter_super(sb
);
1578 cgroup_drop_root(root
);
1581 static struct file_system_type cgroup_fs_type
= {
1583 .get_sb
= cgroup_get_sb
,
1584 .kill_sb
= cgroup_kill_sb
,
1587 static inline struct cgroup
*__d_cgrp(struct dentry
*dentry
)
1589 return dentry
->d_fsdata
;
1592 static inline struct cftype
*__d_cft(struct dentry
*dentry
)
1594 return dentry
->d_fsdata
;
1598 * cgroup_path - generate the path of a cgroup
1599 * @cgrp: the cgroup in question
1600 * @buf: the buffer to write the path into
1601 * @buflen: the length of the buffer
1603 * Called with cgroup_mutex held or else with an RCU-protected cgroup
1604 * reference. Writes path of cgroup into buf. Returns 0 on success,
1607 int cgroup_path(const struct cgroup
*cgrp
, char *buf
, int buflen
)
1610 struct dentry
*dentry
= rcu_dereference(cgrp
->dentry
);
1612 if (!dentry
|| cgrp
== dummytop
) {
1614 * Inactive subsystems have no dentry for their root
1621 start
= buf
+ buflen
;
1625 int len
= dentry
->d_name
.len
;
1626 if ((start
-= len
) < buf
)
1627 return -ENAMETOOLONG
;
1628 memcpy(start
, cgrp
->dentry
->d_name
.name
, len
);
1629 cgrp
= cgrp
->parent
;
1632 dentry
= rcu_dereference(cgrp
->dentry
);
1636 return -ENAMETOOLONG
;
1639 memmove(buf
, start
, buf
+ buflen
- start
);
1644 * cgroup_attach_task - attach task 'tsk' to cgroup 'cgrp'
1645 * @cgrp: the cgroup the task is attaching to
1646 * @tsk: the task to be attached
1648 * Call holding cgroup_mutex. May take task_lock of
1649 * the task 'tsk' during call.
1651 int cgroup_attach_task(struct cgroup
*cgrp
, struct task_struct
*tsk
)
1654 struct cgroup_subsys
*ss
, *failed_ss
= NULL
;
1655 struct cgroup
*oldcgrp
;
1657 struct css_set
*newcg
;
1658 struct cgroupfs_root
*root
= cgrp
->root
;
1660 /* Nothing to do if the task is already in that cgroup */
1661 oldcgrp
= task_cgroup_from_root(tsk
, root
);
1662 if (cgrp
== oldcgrp
)
1665 for_each_subsys(root
, ss
) {
1666 if (ss
->can_attach
) {
1667 retval
= ss
->can_attach(ss
, cgrp
, tsk
, false);
1670 * Remember on which subsystem the can_attach()
1671 * failed, so that we only call cancel_attach()
1672 * against the subsystems whose can_attach()
1673 * succeeded. (See below)
1686 * Locate or allocate a new css_set for this task,
1687 * based on its final set of cgroups
1689 newcg
= find_css_set(cg
, cgrp
);
1697 if (tsk
->flags
& PF_EXITING
) {
1703 rcu_assign_pointer(tsk
->cgroups
, newcg
);
1706 /* Update the css_set linked lists if we're using them */
1707 write_lock(&css_set_lock
);
1708 if (!list_empty(&tsk
->cg_list
)) {
1709 list_del(&tsk
->cg_list
);
1710 list_add(&tsk
->cg_list
, &newcg
->tasks
);
1712 write_unlock(&css_set_lock
);
1714 for_each_subsys(root
, ss
) {
1716 ss
->attach(ss
, cgrp
, oldcgrp
, tsk
, false);
1718 set_bit(CGRP_RELEASABLE
, &oldcgrp
->flags
);
1723 * wake up rmdir() waiter. the rmdir should fail since the cgroup
1724 * is no longer empty.
1726 cgroup_wakeup_rmdir_waiter(cgrp
);
1729 for_each_subsys(root
, ss
) {
1730 if (ss
== failed_ss
)
1732 * This subsystem was the one that failed the
1733 * can_attach() check earlier, so we don't need
1734 * to call cancel_attach() against it or any
1735 * remaining subsystems.
1738 if (ss
->cancel_attach
)
1739 ss
->cancel_attach(ss
, cgrp
, tsk
, false);
1746 * Attach task with pid 'pid' to cgroup 'cgrp'. Call with cgroup_mutex
1747 * held. May take task_lock of task
1749 static int attach_task_by_pid(struct cgroup
*cgrp
, u64 pid
)
1751 struct task_struct
*tsk
;
1752 const struct cred
*cred
= current_cred(), *tcred
;
1757 tsk
= find_task_by_vpid(pid
);
1758 if (!tsk
|| tsk
->flags
& PF_EXITING
) {
1763 tcred
= __task_cred(tsk
);
1765 cred
->euid
!= tcred
->uid
&&
1766 cred
->euid
!= tcred
->suid
) {
1770 get_task_struct(tsk
);
1774 get_task_struct(tsk
);
1777 ret
= cgroup_attach_task(cgrp
, tsk
);
1778 put_task_struct(tsk
);
1782 static int cgroup_tasks_write(struct cgroup
*cgrp
, struct cftype
*cft
, u64 pid
)
1785 if (!cgroup_lock_live_group(cgrp
))
1787 ret
= attach_task_by_pid(cgrp
, pid
);
1793 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
1794 * @cgrp: the cgroup to be checked for liveness
1796 * On success, returns true; the lock should be later released with
1797 * cgroup_unlock(). On failure returns false with no lock held.
1799 bool cgroup_lock_live_group(struct cgroup
*cgrp
)
1801 mutex_lock(&cgroup_mutex
);
1802 if (cgroup_is_removed(cgrp
)) {
1803 mutex_unlock(&cgroup_mutex
);
1809 static int cgroup_release_agent_write(struct cgroup
*cgrp
, struct cftype
*cft
,
1812 BUILD_BUG_ON(sizeof(cgrp
->root
->release_agent_path
) < PATH_MAX
);
1813 if (!cgroup_lock_live_group(cgrp
))
1815 strcpy(cgrp
->root
->release_agent_path
, buffer
);
1820 static int cgroup_release_agent_show(struct cgroup
*cgrp
, struct cftype
*cft
,
1821 struct seq_file
*seq
)
1823 if (!cgroup_lock_live_group(cgrp
))
1825 seq_puts(seq
, cgrp
->root
->release_agent_path
);
1826 seq_putc(seq
, '\n');
1831 /* A buffer size big enough for numbers or short strings */
1832 #define CGROUP_LOCAL_BUFFER_SIZE 64
1834 static ssize_t
cgroup_write_X64(struct cgroup
*cgrp
, struct cftype
*cft
,
1836 const char __user
*userbuf
,
1837 size_t nbytes
, loff_t
*unused_ppos
)
1839 char buffer
[CGROUP_LOCAL_BUFFER_SIZE
];
1845 if (nbytes
>= sizeof(buffer
))
1847 if (copy_from_user(buffer
, userbuf
, nbytes
))
1850 buffer
[nbytes
] = 0; /* nul-terminate */
1851 if (cft
->write_u64
) {
1852 u64 val
= simple_strtoull(strstrip(buffer
), &end
, 0);
1855 retval
= cft
->write_u64(cgrp
, cft
, val
);
1857 s64 val
= simple_strtoll(strstrip(buffer
), &end
, 0);
1860 retval
= cft
->write_s64(cgrp
, cft
, val
);
1867 static ssize_t
cgroup_write_string(struct cgroup
*cgrp
, struct cftype
*cft
,
1869 const char __user
*userbuf
,
1870 size_t nbytes
, loff_t
*unused_ppos
)
1872 char local_buffer
[CGROUP_LOCAL_BUFFER_SIZE
];
1874 size_t max_bytes
= cft
->max_write_len
;
1875 char *buffer
= local_buffer
;
1878 max_bytes
= sizeof(local_buffer
) - 1;
1879 if (nbytes
>= max_bytes
)
1881 /* Allocate a dynamic buffer if we need one */
1882 if (nbytes
>= sizeof(local_buffer
)) {
1883 buffer
= kmalloc(nbytes
+ 1, GFP_KERNEL
);
1887 if (nbytes
&& copy_from_user(buffer
, userbuf
, nbytes
)) {
1892 buffer
[nbytes
] = 0; /* nul-terminate */
1893 retval
= cft
->write_string(cgrp
, cft
, strstrip(buffer
));
1897 if (buffer
!= local_buffer
)
1902 static ssize_t
cgroup_file_write(struct file
*file
, const char __user
*buf
,
1903 size_t nbytes
, loff_t
*ppos
)
1905 struct cftype
*cft
= __d_cft(file
->f_dentry
);
1906 struct cgroup
*cgrp
= __d_cgrp(file
->f_dentry
->d_parent
);
1908 if (cgroup_is_removed(cgrp
))
1911 return cft
->write(cgrp
, cft
, file
, buf
, nbytes
, ppos
);
1912 if (cft
->write_u64
|| cft
->write_s64
)
1913 return cgroup_write_X64(cgrp
, cft
, file
, buf
, nbytes
, ppos
);
1914 if (cft
->write_string
)
1915 return cgroup_write_string(cgrp
, cft
, file
, buf
, nbytes
, ppos
);
1917 int ret
= cft
->trigger(cgrp
, (unsigned int)cft
->private);
1918 return ret
? ret
: nbytes
;
1923 static ssize_t
cgroup_read_u64(struct cgroup
*cgrp
, struct cftype
*cft
,
1925 char __user
*buf
, size_t nbytes
,
1928 char tmp
[CGROUP_LOCAL_BUFFER_SIZE
];
1929 u64 val
= cft
->read_u64(cgrp
, cft
);
1930 int len
= sprintf(tmp
, "%llu\n", (unsigned long long) val
);
1932 return simple_read_from_buffer(buf
, nbytes
, ppos
, tmp
, len
);
1935 static ssize_t
cgroup_read_s64(struct cgroup
*cgrp
, struct cftype
*cft
,
1937 char __user
*buf
, size_t nbytes
,
1940 char tmp
[CGROUP_LOCAL_BUFFER_SIZE
];
1941 s64 val
= cft
->read_s64(cgrp
, cft
);
1942 int len
= sprintf(tmp
, "%lld\n", (long long) val
);
1944 return simple_read_from_buffer(buf
, nbytes
, ppos
, tmp
, len
);
1947 static ssize_t
cgroup_file_read(struct file
*file
, char __user
*buf
,
1948 size_t nbytes
, loff_t
*ppos
)
1950 struct cftype
*cft
= __d_cft(file
->f_dentry
);
1951 struct cgroup
*cgrp
= __d_cgrp(file
->f_dentry
->d_parent
);
1953 if (cgroup_is_removed(cgrp
))
1957 return cft
->read(cgrp
, cft
, file
, buf
, nbytes
, ppos
);
1959 return cgroup_read_u64(cgrp
, cft
, file
, buf
, nbytes
, ppos
);
1961 return cgroup_read_s64(cgrp
, cft
, file
, buf
, nbytes
, ppos
);
1966 * seqfile ops/methods for returning structured data. Currently just
1967 * supports string->u64 maps, but can be extended in future.
1970 struct cgroup_seqfile_state
{
1972 struct cgroup
*cgroup
;
1975 static int cgroup_map_add(struct cgroup_map_cb
*cb
, const char *key
, u64 value
)
1977 struct seq_file
*sf
= cb
->state
;
1978 return seq_printf(sf
, "%s %llu\n", key
, (unsigned long long)value
);
1981 static int cgroup_seqfile_show(struct seq_file
*m
, void *arg
)
1983 struct cgroup_seqfile_state
*state
= m
->private;
1984 struct cftype
*cft
= state
->cft
;
1985 if (cft
->read_map
) {
1986 struct cgroup_map_cb cb
= {
1987 .fill
= cgroup_map_add
,
1990 return cft
->read_map(state
->cgroup
, cft
, &cb
);
1992 return cft
->read_seq_string(state
->cgroup
, cft
, m
);
1995 static int cgroup_seqfile_release(struct inode
*inode
, struct file
*file
)
1997 struct seq_file
*seq
= file
->private_data
;
1998 kfree(seq
->private);
1999 return single_release(inode
, file
);
2002 static const struct file_operations cgroup_seqfile_operations
= {
2004 .write
= cgroup_file_write
,
2005 .llseek
= seq_lseek
,
2006 .release
= cgroup_seqfile_release
,
2009 static int cgroup_file_open(struct inode
*inode
, struct file
*file
)
2014 err
= generic_file_open(inode
, file
);
2017 cft
= __d_cft(file
->f_dentry
);
2019 if (cft
->read_map
|| cft
->read_seq_string
) {
2020 struct cgroup_seqfile_state
*state
=
2021 kzalloc(sizeof(*state
), GFP_USER
);
2025 state
->cgroup
= __d_cgrp(file
->f_dentry
->d_parent
);
2026 file
->f_op
= &cgroup_seqfile_operations
;
2027 err
= single_open(file
, cgroup_seqfile_show
, state
);
2030 } else if (cft
->open
)
2031 err
= cft
->open(inode
, file
);
2038 static int cgroup_file_release(struct inode
*inode
, struct file
*file
)
2040 struct cftype
*cft
= __d_cft(file
->f_dentry
);
2042 return cft
->release(inode
, file
);
2047 * cgroup_rename - Only allow simple rename of directories in place.
2049 static int cgroup_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
2050 struct inode
*new_dir
, struct dentry
*new_dentry
)
2052 if (!S_ISDIR(old_dentry
->d_inode
->i_mode
))
2054 if (new_dentry
->d_inode
)
2056 if (old_dir
!= new_dir
)
2058 return simple_rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
2061 static const struct file_operations cgroup_file_operations
= {
2062 .read
= cgroup_file_read
,
2063 .write
= cgroup_file_write
,
2064 .llseek
= generic_file_llseek
,
2065 .open
= cgroup_file_open
,
2066 .release
= cgroup_file_release
,
2069 static const struct inode_operations cgroup_dir_inode_operations
= {
2070 .lookup
= simple_lookup
,
2071 .mkdir
= cgroup_mkdir
,
2072 .rmdir
= cgroup_rmdir
,
2073 .rename
= cgroup_rename
,
2076 static int cgroup_create_file(struct dentry
*dentry
, mode_t mode
,
2077 struct super_block
*sb
)
2079 static const struct dentry_operations cgroup_dops
= {
2080 .d_iput
= cgroup_diput
,
2083 struct inode
*inode
;
2087 if (dentry
->d_inode
)
2090 inode
= cgroup_new_inode(mode
, sb
);
2094 if (S_ISDIR(mode
)) {
2095 inode
->i_op
= &cgroup_dir_inode_operations
;
2096 inode
->i_fop
= &simple_dir_operations
;
2098 /* start off with i_nlink == 2 (for "." entry) */
2101 /* start with the directory inode held, so that we can
2102 * populate it without racing with another mkdir */
2103 mutex_lock_nested(&inode
->i_mutex
, I_MUTEX_CHILD
);
2104 } else if (S_ISREG(mode
)) {
2106 inode
->i_fop
= &cgroup_file_operations
;
2108 dentry
->d_op
= &cgroup_dops
;
2109 d_instantiate(dentry
, inode
);
2110 dget(dentry
); /* Extra count - pin the dentry in core */
2115 * cgroup_create_dir - create a directory for an object.
2116 * @cgrp: the cgroup we create the directory for. It must have a valid
2117 * ->parent field. And we are going to fill its ->dentry field.
2118 * @dentry: dentry of the new cgroup
2119 * @mode: mode to set on new directory.
2121 static int cgroup_create_dir(struct cgroup
*cgrp
, struct dentry
*dentry
,
2124 struct dentry
*parent
;
2127 parent
= cgrp
->parent
->dentry
;
2128 error
= cgroup_create_file(dentry
, S_IFDIR
| mode
, cgrp
->root
->sb
);
2130 dentry
->d_fsdata
= cgrp
;
2131 inc_nlink(parent
->d_inode
);
2132 rcu_assign_pointer(cgrp
->dentry
, dentry
);
2141 * cgroup_file_mode - deduce file mode of a control file
2142 * @cft: the control file in question
2144 * returns cft->mode if ->mode is not 0
2145 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2146 * returns S_IRUGO if it has only a read handler
2147 * returns S_IWUSR if it has only a write hander
2149 static mode_t
cgroup_file_mode(const struct cftype
*cft
)
2156 if (cft
->read
|| cft
->read_u64
|| cft
->read_s64
||
2157 cft
->read_map
|| cft
->read_seq_string
)
2160 if (cft
->write
|| cft
->write_u64
|| cft
->write_s64
||
2161 cft
->write_string
|| cft
->trigger
)
2167 int cgroup_add_file(struct cgroup
*cgrp
,
2168 struct cgroup_subsys
*subsys
,
2169 const struct cftype
*cft
)
2171 struct dentry
*dir
= cgrp
->dentry
;
2172 struct dentry
*dentry
;
2176 char name
[MAX_CGROUP_TYPE_NAMELEN
+ MAX_CFTYPE_NAME
+ 2] = { 0 };
2177 if (subsys
&& !test_bit(ROOT_NOPREFIX
, &cgrp
->root
->flags
)) {
2178 strcpy(name
, subsys
->name
);
2181 strcat(name
, cft
->name
);
2182 BUG_ON(!mutex_is_locked(&dir
->d_inode
->i_mutex
));
2183 dentry
= lookup_one_len(name
, dir
, strlen(name
));
2184 if (!IS_ERR(dentry
)) {
2185 mode
= cgroup_file_mode(cft
);
2186 error
= cgroup_create_file(dentry
, mode
| S_IFREG
,
2189 dentry
->d_fsdata
= (void *)cft
;
2192 error
= PTR_ERR(dentry
);
2195 EXPORT_SYMBOL_GPL(cgroup_add_file
);
2197 int cgroup_add_files(struct cgroup
*cgrp
,
2198 struct cgroup_subsys
*subsys
,
2199 const struct cftype cft
[],
2203 for (i
= 0; i
< count
; i
++) {
2204 err
= cgroup_add_file(cgrp
, subsys
, &cft
[i
]);
2210 EXPORT_SYMBOL_GPL(cgroup_add_files
);
2213 * cgroup_task_count - count the number of tasks in a cgroup.
2214 * @cgrp: the cgroup in question
2216 * Return the number of tasks in the cgroup.
2218 int cgroup_task_count(const struct cgroup
*cgrp
)
2221 struct cg_cgroup_link
*link
;
2223 read_lock(&css_set_lock
);
2224 list_for_each_entry(link
, &cgrp
->css_sets
, cgrp_link_list
) {
2225 count
+= atomic_read(&link
->cg
->refcount
);
2227 read_unlock(&css_set_lock
);
2232 * Advance a list_head iterator. The iterator should be positioned at
2233 * the start of a css_set
2235 static void cgroup_advance_iter(struct cgroup
*cgrp
,
2236 struct cgroup_iter
*it
)
2238 struct list_head
*l
= it
->cg_link
;
2239 struct cg_cgroup_link
*link
;
2242 /* Advance to the next non-empty css_set */
2245 if (l
== &cgrp
->css_sets
) {
2249 link
= list_entry(l
, struct cg_cgroup_link
, cgrp_link_list
);
2251 } while (list_empty(&cg
->tasks
));
2253 it
->task
= cg
->tasks
.next
;
2257 * To reduce the fork() overhead for systems that are not actually
2258 * using their cgroups capability, we don't maintain the lists running
2259 * through each css_set to its tasks until we see the list actually
2260 * used - in other words after the first call to cgroup_iter_start().
2262 * The tasklist_lock is not held here, as do_each_thread() and
2263 * while_each_thread() are protected by RCU.
2265 static void cgroup_enable_task_cg_lists(void)
2267 struct task_struct
*p
, *g
;
2268 write_lock(&css_set_lock
);
2269 use_task_css_set_links
= 1;
2270 do_each_thread(g
, p
) {
2273 * We should check if the process is exiting, otherwise
2274 * it will race with cgroup_exit() in that the list
2275 * entry won't be deleted though the process has exited.
2277 if (!(p
->flags
& PF_EXITING
) && list_empty(&p
->cg_list
))
2278 list_add(&p
->cg_list
, &p
->cgroups
->tasks
);
2280 } while_each_thread(g
, p
);
2281 write_unlock(&css_set_lock
);
2284 void cgroup_iter_start(struct cgroup
*cgrp
, struct cgroup_iter
*it
)
2287 * The first time anyone tries to iterate across a cgroup,
2288 * we need to enable the list linking each css_set to its
2289 * tasks, and fix up all existing tasks.
2291 if (!use_task_css_set_links
)
2292 cgroup_enable_task_cg_lists();
2294 read_lock(&css_set_lock
);
2295 it
->cg_link
= &cgrp
->css_sets
;
2296 cgroup_advance_iter(cgrp
, it
);
2299 struct task_struct
*cgroup_iter_next(struct cgroup
*cgrp
,
2300 struct cgroup_iter
*it
)
2302 struct task_struct
*res
;
2303 struct list_head
*l
= it
->task
;
2304 struct cg_cgroup_link
*link
;
2306 /* If the iterator cg is NULL, we have no tasks */
2309 res
= list_entry(l
, struct task_struct
, cg_list
);
2310 /* Advance iterator to find next entry */
2312 link
= list_entry(it
->cg_link
, struct cg_cgroup_link
, cgrp_link_list
);
2313 if (l
== &link
->cg
->tasks
) {
2314 /* We reached the end of this task list - move on to
2315 * the next cg_cgroup_link */
2316 cgroup_advance_iter(cgrp
, it
);
2323 void cgroup_iter_end(struct cgroup
*cgrp
, struct cgroup_iter
*it
)
2325 read_unlock(&css_set_lock
);
2328 static inline int started_after_time(struct task_struct
*t1
,
2329 struct timespec
*time
,
2330 struct task_struct
*t2
)
2332 int start_diff
= timespec_compare(&t1
->start_time
, time
);
2333 if (start_diff
> 0) {
2335 } else if (start_diff
< 0) {
2339 * Arbitrarily, if two processes started at the same
2340 * time, we'll say that the lower pointer value
2341 * started first. Note that t2 may have exited by now
2342 * so this may not be a valid pointer any longer, but
2343 * that's fine - it still serves to distinguish
2344 * between two tasks started (effectively) simultaneously.
2351 * This function is a callback from heap_insert() and is used to order
2353 * In this case we order the heap in descending task start time.
2355 static inline int started_after(void *p1
, void *p2
)
2357 struct task_struct
*t1
= p1
;
2358 struct task_struct
*t2
= p2
;
2359 return started_after_time(t1
, &t2
->start_time
, t2
);
2363 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
2364 * @scan: struct cgroup_scanner containing arguments for the scan
2366 * Arguments include pointers to callback functions test_task() and
2368 * Iterate through all the tasks in a cgroup, calling test_task() for each,
2369 * and if it returns true, call process_task() for it also.
2370 * The test_task pointer may be NULL, meaning always true (select all tasks).
2371 * Effectively duplicates cgroup_iter_{start,next,end}()
2372 * but does not lock css_set_lock for the call to process_task().
2373 * The struct cgroup_scanner may be embedded in any structure of the caller's
2375 * It is guaranteed that process_task() will act on every task that
2376 * is a member of the cgroup for the duration of this call. This
2377 * function may or may not call process_task() for tasks that exit
2378 * or move to a different cgroup during the call, or are forked or
2379 * move into the cgroup during the call.
2381 * Note that test_task() may be called with locks held, and may in some
2382 * situations be called multiple times for the same task, so it should
2384 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
2385 * pre-allocated and will be used for heap operations (and its "gt" member will
2386 * be overwritten), else a temporary heap will be used (allocation of which
2387 * may cause this function to fail).
2389 int cgroup_scan_tasks(struct cgroup_scanner
*scan
)
2392 struct cgroup_iter it
;
2393 struct task_struct
*p
, *dropped
;
2394 /* Never dereference latest_task, since it's not refcounted */
2395 struct task_struct
*latest_task
= NULL
;
2396 struct ptr_heap tmp_heap
;
2397 struct ptr_heap
*heap
;
2398 struct timespec latest_time
= { 0, 0 };
2401 /* The caller supplied our heap and pre-allocated its memory */
2403 heap
->gt
= &started_after
;
2405 /* We need to allocate our own heap memory */
2407 retval
= heap_init(heap
, PAGE_SIZE
, GFP_KERNEL
, &started_after
);
2409 /* cannot allocate the heap */
2415 * Scan tasks in the cgroup, using the scanner's "test_task" callback
2416 * to determine which are of interest, and using the scanner's
2417 * "process_task" callback to process any of them that need an update.
2418 * Since we don't want to hold any locks during the task updates,
2419 * gather tasks to be processed in a heap structure.
2420 * The heap is sorted by descending task start time.
2421 * If the statically-sized heap fills up, we overflow tasks that
2422 * started later, and in future iterations only consider tasks that
2423 * started after the latest task in the previous pass. This
2424 * guarantees forward progress and that we don't miss any tasks.
2427 cgroup_iter_start(scan
->cg
, &it
);
2428 while ((p
= cgroup_iter_next(scan
->cg
, &it
))) {
2430 * Only affect tasks that qualify per the caller's callback,
2431 * if he provided one
2433 if (scan
->test_task
&& !scan
->test_task(p
, scan
))
2436 * Only process tasks that started after the last task
2439 if (!started_after_time(p
, &latest_time
, latest_task
))
2441 dropped
= heap_insert(heap
, p
);
2442 if (dropped
== NULL
) {
2444 * The new task was inserted; the heap wasn't
2448 } else if (dropped
!= p
) {
2450 * The new task was inserted, and pushed out a
2454 put_task_struct(dropped
);
2457 * Else the new task was newer than anything already in
2458 * the heap and wasn't inserted
2461 cgroup_iter_end(scan
->cg
, &it
);
2464 for (i
= 0; i
< heap
->size
; i
++) {
2465 struct task_struct
*q
= heap
->ptrs
[i
];
2467 latest_time
= q
->start_time
;
2470 /* Process the task per the caller's callback */
2471 scan
->process_task(q
, scan
);
2475 * If we had to process any tasks at all, scan again
2476 * in case some of them were in the middle of forking
2477 * children that didn't get processed.
2478 * Not the most efficient way to do it, but it avoids
2479 * having to take callback_mutex in the fork path
2483 if (heap
== &tmp_heap
)
2484 heap_free(&tmp_heap
);
2489 * Stuff for reading the 'tasks'/'procs' files.
2491 * Reading this file can return large amounts of data if a cgroup has
2492 * *lots* of attached tasks. So it may need several calls to read(),
2493 * but we cannot guarantee that the information we produce is correct
2494 * unless we produce it entirely atomically.
2499 * The following two functions "fix" the issue where there are more pids
2500 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
2501 * TODO: replace with a kernel-wide solution to this problem
2503 #define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
2504 static void *pidlist_allocate(int count
)
2506 if (PIDLIST_TOO_LARGE(count
))
2507 return vmalloc(count
* sizeof(pid_t
));
2509 return kmalloc(count
* sizeof(pid_t
), GFP_KERNEL
);
2511 static void pidlist_free(void *p
)
2513 if (is_vmalloc_addr(p
))
2518 static void *pidlist_resize(void *p
, int newcount
)
2521 /* note: if new alloc fails, old p will still be valid either way */
2522 if (is_vmalloc_addr(p
)) {
2523 newlist
= vmalloc(newcount
* sizeof(pid_t
));
2526 memcpy(newlist
, p
, newcount
* sizeof(pid_t
));
2529 newlist
= krealloc(p
, newcount
* sizeof(pid_t
), GFP_KERNEL
);
2535 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
2536 * If the new stripped list is sufficiently smaller and there's enough memory
2537 * to allocate a new buffer, will let go of the unneeded memory. Returns the
2538 * number of unique elements.
2540 /* is the size difference enough that we should re-allocate the array? */
2541 #define PIDLIST_REALLOC_DIFFERENCE(old, new) ((old) - PAGE_SIZE >= (new))
2542 static int pidlist_uniq(pid_t
**p
, int length
)
2549 * we presume the 0th element is unique, so i starts at 1. trivial
2550 * edge cases first; no work needs to be done for either
2552 if (length
== 0 || length
== 1)
2554 /* src and dest walk down the list; dest counts unique elements */
2555 for (src
= 1; src
< length
; src
++) {
2556 /* find next unique element */
2557 while (list
[src
] == list
[src
-1]) {
2562 /* dest always points to where the next unique element goes */
2563 list
[dest
] = list
[src
];
2568 * if the length difference is large enough, we want to allocate a
2569 * smaller buffer to save memory. if this fails due to out of memory,
2570 * we'll just stay with what we've got.
2572 if (PIDLIST_REALLOC_DIFFERENCE(length
, dest
)) {
2573 newlist
= pidlist_resize(list
, dest
);
2580 static int cmppid(const void *a
, const void *b
)
2582 return *(pid_t
*)a
- *(pid_t
*)b
;
2586 * find the appropriate pidlist for our purpose (given procs vs tasks)
2587 * returns with the lock on that pidlist already held, and takes care
2588 * of the use count, or returns NULL with no locks held if we're out of
2591 static struct cgroup_pidlist
*cgroup_pidlist_find(struct cgroup
*cgrp
,
2592 enum cgroup_filetype type
)
2594 struct cgroup_pidlist
*l
;
2595 /* don't need task_nsproxy() if we're looking at ourself */
2596 struct pid_namespace
*ns
= get_pid_ns(current
->nsproxy
->pid_ns
);
2598 * We can't drop the pidlist_mutex before taking the l->mutex in case
2599 * the last ref-holder is trying to remove l from the list at the same
2600 * time. Holding the pidlist_mutex precludes somebody taking whichever
2601 * list we find out from under us - compare release_pid_array().
2603 mutex_lock(&cgrp
->pidlist_mutex
);
2604 list_for_each_entry(l
, &cgrp
->pidlists
, links
) {
2605 if (l
->key
.type
== type
&& l
->key
.ns
== ns
) {
2606 /* found a matching list - drop the extra refcount */
2608 /* make sure l doesn't vanish out from under us */
2609 down_write(&l
->mutex
);
2610 mutex_unlock(&cgrp
->pidlist_mutex
);
2614 /* entry not found; create a new one */
2615 l
= kmalloc(sizeof(struct cgroup_pidlist
), GFP_KERNEL
);
2617 mutex_unlock(&cgrp
->pidlist_mutex
);
2621 init_rwsem(&l
->mutex
);
2622 down_write(&l
->mutex
);
2625 l
->use_count
= 0; /* don't increment here */
2628 list_add(&l
->links
, &cgrp
->pidlists
);
2629 mutex_unlock(&cgrp
->pidlist_mutex
);
2634 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
2636 static int pidlist_array_load(struct cgroup
*cgrp
, enum cgroup_filetype type
,
2637 struct cgroup_pidlist
**lp
)
2641 int pid
, n
= 0; /* used for populating the array */
2642 struct cgroup_iter it
;
2643 struct task_struct
*tsk
;
2644 struct cgroup_pidlist
*l
;
2647 * If cgroup gets more users after we read count, we won't have
2648 * enough space - tough. This race is indistinguishable to the
2649 * caller from the case that the additional cgroup users didn't
2650 * show up until sometime later on.
2652 length
= cgroup_task_count(cgrp
);
2653 array
= pidlist_allocate(length
);
2656 /* now, populate the array */
2657 cgroup_iter_start(cgrp
, &it
);
2658 while ((tsk
= cgroup_iter_next(cgrp
, &it
))) {
2659 if (unlikely(n
== length
))
2661 /* get tgid or pid for procs or tasks file respectively */
2662 if (type
== CGROUP_FILE_PROCS
)
2663 pid
= task_tgid_vnr(tsk
);
2665 pid
= task_pid_vnr(tsk
);
2666 if (pid
> 0) /* make sure to only use valid results */
2669 cgroup_iter_end(cgrp
, &it
);
2671 /* now sort & (if procs) strip out duplicates */
2672 sort(array
, length
, sizeof(pid_t
), cmppid
, NULL
);
2673 if (type
== CGROUP_FILE_PROCS
)
2674 length
= pidlist_uniq(&array
, length
);
2675 l
= cgroup_pidlist_find(cgrp
, type
);
2677 pidlist_free(array
);
2680 /* store array, freeing old if necessary - lock already held */
2681 pidlist_free(l
->list
);
2685 up_write(&l
->mutex
);
2691 * cgroupstats_build - build and fill cgroupstats
2692 * @stats: cgroupstats to fill information into
2693 * @dentry: A dentry entry belonging to the cgroup for which stats have
2696 * Build and fill cgroupstats so that taskstats can export it to user
2699 int cgroupstats_build(struct cgroupstats
*stats
, struct dentry
*dentry
)
2702 struct cgroup
*cgrp
;
2703 struct cgroup_iter it
;
2704 struct task_struct
*tsk
;
2707 * Validate dentry by checking the superblock operations,
2708 * and make sure it's a directory.
2710 if (dentry
->d_sb
->s_op
!= &cgroup_ops
||
2711 !S_ISDIR(dentry
->d_inode
->i_mode
))
2715 cgrp
= dentry
->d_fsdata
;
2717 cgroup_iter_start(cgrp
, &it
);
2718 while ((tsk
= cgroup_iter_next(cgrp
, &it
))) {
2719 switch (tsk
->state
) {
2721 stats
->nr_running
++;
2723 case TASK_INTERRUPTIBLE
:
2724 stats
->nr_sleeping
++;
2726 case TASK_UNINTERRUPTIBLE
:
2727 stats
->nr_uninterruptible
++;
2730 stats
->nr_stopped
++;
2733 if (delayacct_is_task_waiting_on_io(tsk
))
2734 stats
->nr_io_wait
++;
2738 cgroup_iter_end(cgrp
, &it
);
2746 * seq_file methods for the tasks/procs files. The seq_file position is the
2747 * next pid to display; the seq_file iterator is a pointer to the pid
2748 * in the cgroup->l->list array.
2751 static void *cgroup_pidlist_start(struct seq_file
*s
, loff_t
*pos
)
2754 * Initially we receive a position value that corresponds to
2755 * one more than the last pid shown (or 0 on the first call or
2756 * after a seek to the start). Use a binary-search to find the
2757 * next pid to display, if any
2759 struct cgroup_pidlist
*l
= s
->private;
2760 int index
= 0, pid
= *pos
;
2763 down_read(&l
->mutex
);
2765 int end
= l
->length
;
2767 while (index
< end
) {
2768 int mid
= (index
+ end
) / 2;
2769 if (l
->list
[mid
] == pid
) {
2772 } else if (l
->list
[mid
] <= pid
)
2778 /* If we're off the end of the array, we're done */
2779 if (index
>= l
->length
)
2781 /* Update the abstract position to be the actual pid that we found */
2782 iter
= l
->list
+ index
;
2787 static void cgroup_pidlist_stop(struct seq_file
*s
, void *v
)
2789 struct cgroup_pidlist
*l
= s
->private;
2793 static void *cgroup_pidlist_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
2795 struct cgroup_pidlist
*l
= s
->private;
2797 pid_t
*end
= l
->list
+ l
->length
;
2799 * Advance to the next pid in the array. If this goes off the
2811 static int cgroup_pidlist_show(struct seq_file
*s
, void *v
)
2813 return seq_printf(s
, "%d\n", *(int *)v
);
2817 * seq_operations functions for iterating on pidlists through seq_file -
2818 * independent of whether it's tasks or procs
2820 static const struct seq_operations cgroup_pidlist_seq_operations
= {
2821 .start
= cgroup_pidlist_start
,
2822 .stop
= cgroup_pidlist_stop
,
2823 .next
= cgroup_pidlist_next
,
2824 .show
= cgroup_pidlist_show
,
2827 static void cgroup_release_pid_array(struct cgroup_pidlist
*l
)
2830 * the case where we're the last user of this particular pidlist will
2831 * have us remove it from the cgroup's list, which entails taking the
2832 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
2833 * pidlist_mutex, we have to take pidlist_mutex first.
2835 mutex_lock(&l
->owner
->pidlist_mutex
);
2836 down_write(&l
->mutex
);
2837 BUG_ON(!l
->use_count
);
2838 if (!--l
->use_count
) {
2839 /* we're the last user if refcount is 0; remove and free */
2840 list_del(&l
->links
);
2841 mutex_unlock(&l
->owner
->pidlist_mutex
);
2842 pidlist_free(l
->list
);
2843 put_pid_ns(l
->key
.ns
);
2844 up_write(&l
->mutex
);
2848 mutex_unlock(&l
->owner
->pidlist_mutex
);
2849 up_write(&l
->mutex
);
2852 static int cgroup_pidlist_release(struct inode
*inode
, struct file
*file
)
2854 struct cgroup_pidlist
*l
;
2855 if (!(file
->f_mode
& FMODE_READ
))
2858 * the seq_file will only be initialized if the file was opened for
2859 * reading; hence we check if it's not null only in that case.
2861 l
= ((struct seq_file
*)file
->private_data
)->private;
2862 cgroup_release_pid_array(l
);
2863 return seq_release(inode
, file
);
2866 static const struct file_operations cgroup_pidlist_operations
= {
2868 .llseek
= seq_lseek
,
2869 .write
= cgroup_file_write
,
2870 .release
= cgroup_pidlist_release
,
2874 * The following functions handle opens on a file that displays a pidlist
2875 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
2878 /* helper function for the two below it */
2879 static int cgroup_pidlist_open(struct file
*file
, enum cgroup_filetype type
)
2881 struct cgroup
*cgrp
= __d_cgrp(file
->f_dentry
->d_parent
);
2882 struct cgroup_pidlist
*l
;
2885 /* Nothing to do for write-only files */
2886 if (!(file
->f_mode
& FMODE_READ
))
2889 /* have the array populated */
2890 retval
= pidlist_array_load(cgrp
, type
, &l
);
2893 /* configure file information */
2894 file
->f_op
= &cgroup_pidlist_operations
;
2896 retval
= seq_open(file
, &cgroup_pidlist_seq_operations
);
2898 cgroup_release_pid_array(l
);
2901 ((struct seq_file
*)file
->private_data
)->private = l
;
2904 static int cgroup_tasks_open(struct inode
*unused
, struct file
*file
)
2906 return cgroup_pidlist_open(file
, CGROUP_FILE_TASKS
);
2908 static int cgroup_procs_open(struct inode
*unused
, struct file
*file
)
2910 return cgroup_pidlist_open(file
, CGROUP_FILE_PROCS
);
2913 static u64
cgroup_read_notify_on_release(struct cgroup
*cgrp
,
2916 return notify_on_release(cgrp
);
2919 static int cgroup_write_notify_on_release(struct cgroup
*cgrp
,
2923 clear_bit(CGRP_RELEASABLE
, &cgrp
->flags
);
2925 set_bit(CGRP_NOTIFY_ON_RELEASE
, &cgrp
->flags
);
2927 clear_bit(CGRP_NOTIFY_ON_RELEASE
, &cgrp
->flags
);
2932 * for the common functions, 'private' gives the type of file
2934 /* for hysterical raisins, we can't put this on the older files */
2935 #define CGROUP_FILE_GENERIC_PREFIX "cgroup."
2936 static struct cftype files
[] = {
2939 .open
= cgroup_tasks_open
,
2940 .write_u64
= cgroup_tasks_write
,
2941 .release
= cgroup_pidlist_release
,
2942 .mode
= S_IRUGO
| S_IWUSR
,
2945 .name
= CGROUP_FILE_GENERIC_PREFIX
"procs",
2946 .open
= cgroup_procs_open
,
2947 /* .write_u64 = cgroup_procs_write, TODO */
2948 .release
= cgroup_pidlist_release
,
2952 .name
= "notify_on_release",
2953 .read_u64
= cgroup_read_notify_on_release
,
2954 .write_u64
= cgroup_write_notify_on_release
,
2958 static struct cftype cft_release_agent
= {
2959 .name
= "release_agent",
2960 .read_seq_string
= cgroup_release_agent_show
,
2961 .write_string
= cgroup_release_agent_write
,
2962 .max_write_len
= PATH_MAX
,
2965 static int cgroup_populate_dir(struct cgroup
*cgrp
)
2968 struct cgroup_subsys
*ss
;
2970 /* First clear out any existing files */
2971 cgroup_clear_directory(cgrp
->dentry
);
2973 err
= cgroup_add_files(cgrp
, NULL
, files
, ARRAY_SIZE(files
));
2977 if (cgrp
== cgrp
->top_cgroup
) {
2978 if ((err
= cgroup_add_file(cgrp
, NULL
, &cft_release_agent
)) < 0)
2982 for_each_subsys(cgrp
->root
, ss
) {
2983 if (ss
->populate
&& (err
= ss
->populate(ss
, cgrp
)) < 0)
2986 /* This cgroup is ready now */
2987 for_each_subsys(cgrp
->root
, ss
) {
2988 struct cgroup_subsys_state
*css
= cgrp
->subsys
[ss
->subsys_id
];
2990 * Update id->css pointer and make this css visible from
2991 * CSS ID functions. This pointer will be dereferened
2992 * from RCU-read-side without locks.
2995 rcu_assign_pointer(css
->id
->css
, css
);
3001 static void init_cgroup_css(struct cgroup_subsys_state
*css
,
3002 struct cgroup_subsys
*ss
,
3003 struct cgroup
*cgrp
)
3006 atomic_set(&css
->refcnt
, 1);
3009 if (cgrp
== dummytop
)
3010 set_bit(CSS_ROOT
, &css
->flags
);
3011 BUG_ON(cgrp
->subsys
[ss
->subsys_id
]);
3012 cgrp
->subsys
[ss
->subsys_id
] = css
;
3015 static void cgroup_lock_hierarchy(struct cgroupfs_root
*root
)
3017 /* We need to take each hierarchy_mutex in a consistent order */
3021 * No worry about a race with rebind_subsystems that might mess up the
3022 * locking order, since both parties are under cgroup_mutex.
3024 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
3025 struct cgroup_subsys
*ss
= subsys
[i
];
3028 if (ss
->root
== root
)
3029 mutex_lock(&ss
->hierarchy_mutex
);
3033 static void cgroup_unlock_hierarchy(struct cgroupfs_root
*root
)
3037 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
3038 struct cgroup_subsys
*ss
= subsys
[i
];
3041 if (ss
->root
== root
)
3042 mutex_unlock(&ss
->hierarchy_mutex
);
3047 * cgroup_create - create a cgroup
3048 * @parent: cgroup that will be parent of the new cgroup
3049 * @dentry: dentry of the new cgroup
3050 * @mode: mode to set on new inode
3052 * Must be called with the mutex on the parent inode held
3054 static long cgroup_create(struct cgroup
*parent
, struct dentry
*dentry
,
3057 struct cgroup
*cgrp
;
3058 struct cgroupfs_root
*root
= parent
->root
;
3060 struct cgroup_subsys
*ss
;
3061 struct super_block
*sb
= root
->sb
;
3063 cgrp
= kzalloc(sizeof(*cgrp
), GFP_KERNEL
);
3067 /* Grab a reference on the superblock so the hierarchy doesn't
3068 * get deleted on unmount if there are child cgroups. This
3069 * can be done outside cgroup_mutex, since the sb can't
3070 * disappear while someone has an open control file on the
3072 atomic_inc(&sb
->s_active
);
3074 mutex_lock(&cgroup_mutex
);
3076 init_cgroup_housekeeping(cgrp
);
3078 cgrp
->parent
= parent
;
3079 cgrp
->root
= parent
->root
;
3080 cgrp
->top_cgroup
= parent
->top_cgroup
;
3082 if (notify_on_release(parent
))
3083 set_bit(CGRP_NOTIFY_ON_RELEASE
, &cgrp
->flags
);
3085 for_each_subsys(root
, ss
) {
3086 struct cgroup_subsys_state
*css
= ss
->create(ss
, cgrp
);
3092 init_cgroup_css(css
, ss
, cgrp
);
3094 err
= alloc_css_id(ss
, parent
, cgrp
);
3098 /* At error, ->destroy() callback has to free assigned ID. */
3101 cgroup_lock_hierarchy(root
);
3102 list_add(&cgrp
->sibling
, &cgrp
->parent
->children
);
3103 cgroup_unlock_hierarchy(root
);
3104 root
->number_of_cgroups
++;
3106 err
= cgroup_create_dir(cgrp
, dentry
, mode
);
3110 /* The cgroup directory was pre-locked for us */
3111 BUG_ON(!mutex_is_locked(&cgrp
->dentry
->d_inode
->i_mutex
));
3113 err
= cgroup_populate_dir(cgrp
);
3114 /* If err < 0, we have a half-filled directory - oh well ;) */
3116 mutex_unlock(&cgroup_mutex
);
3117 mutex_unlock(&cgrp
->dentry
->d_inode
->i_mutex
);
3123 cgroup_lock_hierarchy(root
);
3124 list_del(&cgrp
->sibling
);
3125 cgroup_unlock_hierarchy(root
);
3126 root
->number_of_cgroups
--;
3130 for_each_subsys(root
, ss
) {
3131 if (cgrp
->subsys
[ss
->subsys_id
])
3132 ss
->destroy(ss
, cgrp
);
3135 mutex_unlock(&cgroup_mutex
);
3137 /* Release the reference count that we took on the superblock */
3138 deactivate_super(sb
);
3144 static int cgroup_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
3146 struct cgroup
*c_parent
= dentry
->d_parent
->d_fsdata
;
3148 /* the vfs holds inode->i_mutex already */
3149 return cgroup_create(c_parent
, dentry
, mode
| S_IFDIR
);
3152 static int cgroup_has_css_refs(struct cgroup
*cgrp
)
3154 /* Check the reference count on each subsystem. Since we
3155 * already established that there are no tasks in the
3156 * cgroup, if the css refcount is also 1, then there should
3157 * be no outstanding references, so the subsystem is safe to
3158 * destroy. We scan across all subsystems rather than using
3159 * the per-hierarchy linked list of mounted subsystems since
3160 * we can be called via check_for_release() with no
3161 * synchronization other than RCU, and the subsystem linked
3162 * list isn't RCU-safe */
3165 * We won't need to lock the subsys array, because the subsystems
3166 * we're concerned about aren't going anywhere since our cgroup root
3167 * has a reference on them.
3169 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
3170 struct cgroup_subsys
*ss
= subsys
[i
];
3171 struct cgroup_subsys_state
*css
;
3172 /* Skip subsystems not present or not in this hierarchy */
3173 if (ss
== NULL
|| ss
->root
!= cgrp
->root
)
3175 css
= cgrp
->subsys
[ss
->subsys_id
];
3176 /* When called from check_for_release() it's possible
3177 * that by this point the cgroup has been removed
3178 * and the css deleted. But a false-positive doesn't
3179 * matter, since it can only happen if the cgroup
3180 * has been deleted and hence no longer needs the
3181 * release agent to be called anyway. */
3182 if (css
&& (atomic_read(&css
->refcnt
) > 1))
3189 * Atomically mark all (or else none) of the cgroup's CSS objects as
3190 * CSS_REMOVED. Return true on success, or false if the cgroup has
3191 * busy subsystems. Call with cgroup_mutex held
3194 static int cgroup_clear_css_refs(struct cgroup
*cgrp
)
3196 struct cgroup_subsys
*ss
;
3197 unsigned long flags
;
3198 bool failed
= false;
3199 local_irq_save(flags
);
3200 for_each_subsys(cgrp
->root
, ss
) {
3201 struct cgroup_subsys_state
*css
= cgrp
->subsys
[ss
->subsys_id
];
3204 /* We can only remove a CSS with a refcnt==1 */
3205 refcnt
= atomic_read(&css
->refcnt
);
3212 * Drop the refcnt to 0 while we check other
3213 * subsystems. This will cause any racing
3214 * css_tryget() to spin until we set the
3215 * CSS_REMOVED bits or abort
3217 if (atomic_cmpxchg(&css
->refcnt
, refcnt
, 0) == refcnt
)
3223 for_each_subsys(cgrp
->root
, ss
) {
3224 struct cgroup_subsys_state
*css
= cgrp
->subsys
[ss
->subsys_id
];
3227 * Restore old refcnt if we previously managed
3228 * to clear it from 1 to 0
3230 if (!atomic_read(&css
->refcnt
))
3231 atomic_set(&css
->refcnt
, 1);
3233 /* Commit the fact that the CSS is removed */
3234 set_bit(CSS_REMOVED
, &css
->flags
);
3237 local_irq_restore(flags
);
3241 static int cgroup_rmdir(struct inode
*unused_dir
, struct dentry
*dentry
)
3243 struct cgroup
*cgrp
= dentry
->d_fsdata
;
3245 struct cgroup
*parent
;
3249 /* the vfs holds both inode->i_mutex already */
3251 mutex_lock(&cgroup_mutex
);
3252 if (atomic_read(&cgrp
->count
) != 0) {
3253 mutex_unlock(&cgroup_mutex
);
3256 if (!list_empty(&cgrp
->children
)) {
3257 mutex_unlock(&cgroup_mutex
);
3260 mutex_unlock(&cgroup_mutex
);
3263 * In general, subsystem has no css->refcnt after pre_destroy(). But
3264 * in racy cases, subsystem may have to get css->refcnt after
3265 * pre_destroy() and it makes rmdir return with -EBUSY. This sometimes
3266 * make rmdir return -EBUSY too often. To avoid that, we use waitqueue
3267 * for cgroup's rmdir. CGRP_WAIT_ON_RMDIR is for synchronizing rmdir
3268 * and subsystem's reference count handling. Please see css_get/put
3269 * and css_tryget() and cgroup_wakeup_rmdir_waiter() implementation.
3271 set_bit(CGRP_WAIT_ON_RMDIR
, &cgrp
->flags
);
3274 * Call pre_destroy handlers of subsys. Notify subsystems
3275 * that rmdir() request comes.
3277 ret
= cgroup_call_pre_destroy(cgrp
);
3279 clear_bit(CGRP_WAIT_ON_RMDIR
, &cgrp
->flags
);
3283 mutex_lock(&cgroup_mutex
);
3284 parent
= cgrp
->parent
;
3285 if (atomic_read(&cgrp
->count
) || !list_empty(&cgrp
->children
)) {
3286 clear_bit(CGRP_WAIT_ON_RMDIR
, &cgrp
->flags
);
3287 mutex_unlock(&cgroup_mutex
);
3290 prepare_to_wait(&cgroup_rmdir_waitq
, &wait
, TASK_INTERRUPTIBLE
);
3291 if (!cgroup_clear_css_refs(cgrp
)) {
3292 mutex_unlock(&cgroup_mutex
);
3294 * Because someone may call cgroup_wakeup_rmdir_waiter() before
3295 * prepare_to_wait(), we need to check this flag.
3297 if (test_bit(CGRP_WAIT_ON_RMDIR
, &cgrp
->flags
))
3299 finish_wait(&cgroup_rmdir_waitq
, &wait
);
3300 clear_bit(CGRP_WAIT_ON_RMDIR
, &cgrp
->flags
);
3301 if (signal_pending(current
))
3305 /* NO css_tryget() can success after here. */
3306 finish_wait(&cgroup_rmdir_waitq
, &wait
);
3307 clear_bit(CGRP_WAIT_ON_RMDIR
, &cgrp
->flags
);
3309 spin_lock(&release_list_lock
);
3310 set_bit(CGRP_REMOVED
, &cgrp
->flags
);
3311 if (!list_empty(&cgrp
->release_list
))
3312 list_del(&cgrp
->release_list
);
3313 spin_unlock(&release_list_lock
);
3315 cgroup_lock_hierarchy(cgrp
->root
);
3316 /* delete this cgroup from parent->children */
3317 list_del(&cgrp
->sibling
);
3318 cgroup_unlock_hierarchy(cgrp
->root
);
3320 spin_lock(&cgrp
->dentry
->d_lock
);
3321 d
= dget(cgrp
->dentry
);
3322 spin_unlock(&d
->d_lock
);
3324 cgroup_d_remove_dir(d
);
3327 set_bit(CGRP_RELEASABLE
, &parent
->flags
);
3328 check_for_release(parent
);
3330 mutex_unlock(&cgroup_mutex
);
3334 static void __init
cgroup_init_subsys(struct cgroup_subsys
*ss
)
3336 struct cgroup_subsys_state
*css
;
3338 printk(KERN_INFO
"Initializing cgroup subsys %s\n", ss
->name
);
3340 /* Create the top cgroup state for this subsystem */
3341 list_add(&ss
->sibling
, &rootnode
.subsys_list
);
3342 ss
->root
= &rootnode
;
3343 css
= ss
->create(ss
, dummytop
);
3344 /* We don't handle early failures gracefully */
3345 BUG_ON(IS_ERR(css
));
3346 init_cgroup_css(css
, ss
, dummytop
);
3348 /* Update the init_css_set to contain a subsys
3349 * pointer to this state - since the subsystem is
3350 * newly registered, all tasks and hence the
3351 * init_css_set is in the subsystem's top cgroup. */
3352 init_css_set
.subsys
[ss
->subsys_id
] = dummytop
->subsys
[ss
->subsys_id
];
3354 need_forkexit_callback
|= ss
->fork
|| ss
->exit
;
3356 /* At system boot, before all subsystems have been
3357 * registered, no tasks have been forked, so we don't
3358 * need to invoke fork callbacks here. */
3359 BUG_ON(!list_empty(&init_task
.tasks
));
3361 mutex_init(&ss
->hierarchy_mutex
);
3362 lockdep_set_class(&ss
->hierarchy_mutex
, &ss
->subsys_key
);
3365 /* this function shouldn't be used with modular subsystems, since they
3366 * need to register a subsys_id, among other things */
3371 * cgroup_load_subsys: load and register a modular subsystem at runtime
3372 * @ss: the subsystem to load
3374 * This function should be called in a modular subsystem's initcall. If the
3375 * subsytem is built as a module, it will be assigned a new subsys_id and set
3376 * up for use. If the subsystem is built-in anyway, work is delegated to the
3377 * simpler cgroup_init_subsys.
3379 int __init_or_module
cgroup_load_subsys(struct cgroup_subsys
*ss
)
3382 struct cgroup_subsys_state
*css
;
3384 /* check name and function validity */
3385 if (ss
->name
== NULL
|| strlen(ss
->name
) > MAX_CGROUP_TYPE_NAMELEN
||
3386 ss
->create
== NULL
|| ss
->destroy
== NULL
)
3390 * we don't support callbacks in modular subsystems. this check is
3391 * before the ss->module check for consistency; a subsystem that could
3392 * be a module should still have no callbacks even if the user isn't
3393 * compiling it as one.
3395 if (ss
->fork
|| ss
->exit
)
3399 * an optionally modular subsystem is built-in: we want to do nothing,
3400 * since cgroup_init_subsys will have already taken care of it.
3402 if (ss
->module
== NULL
) {
3403 /* a few sanity checks */
3404 BUG_ON(ss
->subsys_id
>= CGROUP_BUILTIN_SUBSYS_COUNT
);
3405 BUG_ON(subsys
[ss
->subsys_id
] != ss
);
3410 * need to register a subsys id before anything else - for example,
3411 * init_cgroup_css needs it.
3413 mutex_lock(&cgroup_mutex
);
3414 /* find the first empty slot in the array */
3415 for (i
= CGROUP_BUILTIN_SUBSYS_COUNT
; i
< CGROUP_SUBSYS_COUNT
; i
++) {
3416 if (subsys
[i
] == NULL
)
3419 if (i
== CGROUP_SUBSYS_COUNT
) {
3420 /* maximum number of subsystems already registered! */
3421 mutex_unlock(&cgroup_mutex
);
3424 /* assign ourselves the subsys_id */
3429 * no ss->create seems to need anything important in the ss struct, so
3430 * this can happen first (i.e. before the rootnode attachment).
3432 css
= ss
->create(ss
, dummytop
);
3434 /* failure case - need to deassign the subsys[] slot. */
3436 mutex_unlock(&cgroup_mutex
);
3437 return PTR_ERR(css
);
3440 list_add(&ss
->sibling
, &rootnode
.subsys_list
);
3441 ss
->root
= &rootnode
;
3443 /* our new subsystem will be attached to the dummy hierarchy. */
3444 init_cgroup_css(css
, ss
, dummytop
);
3445 /* init_idr must be after init_cgroup_css because it sets css->id. */
3447 int ret
= cgroup_init_idr(ss
, css
);
3449 dummytop
->subsys
[ss
->subsys_id
] = NULL
;
3450 ss
->destroy(ss
, dummytop
);
3452 mutex_unlock(&cgroup_mutex
);
3458 * Now we need to entangle the css into the existing css_sets. unlike
3459 * in cgroup_init_subsys, there are now multiple css_sets, so each one
3460 * will need a new pointer to it; done by iterating the css_set_table.
3461 * furthermore, modifying the existing css_sets will corrupt the hash
3462 * table state, so each changed css_set will need its hash recomputed.
3463 * this is all done under the css_set_lock.
3465 write_lock(&css_set_lock
);
3466 for (i
= 0; i
< CSS_SET_TABLE_SIZE
; i
++) {
3468 struct hlist_node
*node
, *tmp
;
3469 struct hlist_head
*bucket
= &css_set_table
[i
], *new_bucket
;
3471 hlist_for_each_entry_safe(cg
, node
, tmp
, bucket
, hlist
) {
3472 /* skip entries that we already rehashed */
3473 if (cg
->subsys
[ss
->subsys_id
])
3475 /* remove existing entry */
3476 hlist_del(&cg
->hlist
);
3478 cg
->subsys
[ss
->subsys_id
] = css
;
3479 /* recompute hash and restore entry */
3480 new_bucket
= css_set_hash(cg
->subsys
);
3481 hlist_add_head(&cg
->hlist
, new_bucket
);
3484 write_unlock(&css_set_lock
);
3486 mutex_init(&ss
->hierarchy_mutex
);
3487 lockdep_set_class(&ss
->hierarchy_mutex
, &ss
->subsys_key
);
3491 mutex_unlock(&cgroup_mutex
);
3494 EXPORT_SYMBOL_GPL(cgroup_load_subsys
);
3497 * cgroup_unload_subsys: unload a modular subsystem
3498 * @ss: the subsystem to unload
3500 * This function should be called in a modular subsystem's exitcall. When this
3501 * function is invoked, the refcount on the subsystem's module will be 0, so
3502 * the subsystem will not be attached to any hierarchy.
3504 void cgroup_unload_subsys(struct cgroup_subsys
*ss
)
3506 struct cg_cgroup_link
*link
;
3507 struct hlist_head
*hhead
;
3509 BUG_ON(ss
->module
== NULL
);
3512 * we shouldn't be called if the subsystem is in use, and the use of
3513 * try_module_get in parse_cgroupfs_options should ensure that it
3514 * doesn't start being used while we're killing it off.
3516 BUG_ON(ss
->root
!= &rootnode
);
3518 mutex_lock(&cgroup_mutex
);
3519 /* deassign the subsys_id */
3520 BUG_ON(ss
->subsys_id
< CGROUP_BUILTIN_SUBSYS_COUNT
);
3521 subsys
[ss
->subsys_id
] = NULL
;
3523 /* remove subsystem from rootnode's list of subsystems */
3524 list_del(&ss
->sibling
);
3527 * disentangle the css from all css_sets attached to the dummytop. as
3528 * in loading, we need to pay our respects to the hashtable gods.
3530 write_lock(&css_set_lock
);
3531 list_for_each_entry(link
, &dummytop
->css_sets
, cgrp_link_list
) {
3532 struct css_set
*cg
= link
->cg
;
3534 hlist_del(&cg
->hlist
);
3535 BUG_ON(!cg
->subsys
[ss
->subsys_id
]);
3536 cg
->subsys
[ss
->subsys_id
] = NULL
;
3537 hhead
= css_set_hash(cg
->subsys
);
3538 hlist_add_head(&cg
->hlist
, hhead
);
3540 write_unlock(&css_set_lock
);
3543 * remove subsystem's css from the dummytop and free it - need to free
3544 * before marking as null because ss->destroy needs the cgrp->subsys
3545 * pointer to find their state. note that this also takes care of
3546 * freeing the css_id.
3548 ss
->destroy(ss
, dummytop
);
3549 dummytop
->subsys
[ss
->subsys_id
] = NULL
;
3551 mutex_unlock(&cgroup_mutex
);
3553 EXPORT_SYMBOL_GPL(cgroup_unload_subsys
);
3556 * cgroup_init_early - cgroup initialization at system boot
3558 * Initialize cgroups at system boot, and initialize any
3559 * subsystems that request early init.
3561 int __init
cgroup_init_early(void)
3564 atomic_set(&init_css_set
.refcount
, 1);
3565 INIT_LIST_HEAD(&init_css_set
.cg_links
);
3566 INIT_LIST_HEAD(&init_css_set
.tasks
);
3567 INIT_HLIST_NODE(&init_css_set
.hlist
);
3569 init_cgroup_root(&rootnode
);
3571 init_task
.cgroups
= &init_css_set
;
3573 init_css_set_link
.cg
= &init_css_set
;
3574 init_css_set_link
.cgrp
= dummytop
;
3575 list_add(&init_css_set_link
.cgrp_link_list
,
3576 &rootnode
.top_cgroup
.css_sets
);
3577 list_add(&init_css_set_link
.cg_link_list
,
3578 &init_css_set
.cg_links
);
3580 for (i
= 0; i
< CSS_SET_TABLE_SIZE
; i
++)
3581 INIT_HLIST_HEAD(&css_set_table
[i
]);
3583 /* at bootup time, we don't worry about modular subsystems */
3584 for (i
= 0; i
< CGROUP_BUILTIN_SUBSYS_COUNT
; i
++) {
3585 struct cgroup_subsys
*ss
= subsys
[i
];
3588 BUG_ON(strlen(ss
->name
) > MAX_CGROUP_TYPE_NAMELEN
);
3589 BUG_ON(!ss
->create
);
3590 BUG_ON(!ss
->destroy
);
3591 if (ss
->subsys_id
!= i
) {
3592 printk(KERN_ERR
"cgroup: Subsys %s id == %d\n",
3593 ss
->name
, ss
->subsys_id
);
3598 cgroup_init_subsys(ss
);
3604 * cgroup_init - cgroup initialization
3606 * Register cgroup filesystem and /proc file, and initialize
3607 * any subsystems that didn't request early init.
3609 int __init
cgroup_init(void)
3613 struct hlist_head
*hhead
;
3615 err
= bdi_init(&cgroup_backing_dev_info
);
3619 /* at bootup time, we don't worry about modular subsystems */
3620 for (i
= 0; i
< CGROUP_BUILTIN_SUBSYS_COUNT
; i
++) {
3621 struct cgroup_subsys
*ss
= subsys
[i
];
3622 if (!ss
->early_init
)
3623 cgroup_init_subsys(ss
);
3625 cgroup_init_idr(ss
, init_css_set
.subsys
[ss
->subsys_id
]);
3628 /* Add init_css_set to the hash table */
3629 hhead
= css_set_hash(init_css_set
.subsys
);
3630 hlist_add_head(&init_css_set
.hlist
, hhead
);
3631 BUG_ON(!init_root_id(&rootnode
));
3632 err
= register_filesystem(&cgroup_fs_type
);
3636 proc_create("cgroups", 0, NULL
, &proc_cgroupstats_operations
);
3640 bdi_destroy(&cgroup_backing_dev_info
);
3646 * proc_cgroup_show()
3647 * - Print task's cgroup paths into seq_file, one line for each hierarchy
3648 * - Used for /proc/<pid>/cgroup.
3649 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
3650 * doesn't really matter if tsk->cgroup changes after we read it,
3651 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
3652 * anyway. No need to check that tsk->cgroup != NULL, thanks to
3653 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
3654 * cgroup to top_cgroup.
3657 /* TODO: Use a proper seq_file iterator */
3658 static int proc_cgroup_show(struct seq_file
*m
, void *v
)
3661 struct task_struct
*tsk
;
3664 struct cgroupfs_root
*root
;
3667 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
3673 tsk
= get_pid_task(pid
, PIDTYPE_PID
);
3679 mutex_lock(&cgroup_mutex
);
3681 for_each_active_root(root
) {
3682 struct cgroup_subsys
*ss
;
3683 struct cgroup
*cgrp
;
3686 seq_printf(m
, "%d:", root
->hierarchy_id
);
3687 for_each_subsys(root
, ss
)
3688 seq_printf(m
, "%s%s", count
++ ? "," : "", ss
->name
);
3689 if (strlen(root
->name
))
3690 seq_printf(m
, "%sname=%s", count
? "," : "",
3693 cgrp
= task_cgroup_from_root(tsk
, root
);
3694 retval
= cgroup_path(cgrp
, buf
, PAGE_SIZE
);
3702 mutex_unlock(&cgroup_mutex
);
3703 put_task_struct(tsk
);
3710 static int cgroup_open(struct inode
*inode
, struct file
*file
)
3712 struct pid
*pid
= PROC_I(inode
)->pid
;
3713 return single_open(file
, proc_cgroup_show
, pid
);
3716 const struct file_operations proc_cgroup_operations
= {
3717 .open
= cgroup_open
,
3719 .llseek
= seq_lseek
,
3720 .release
= single_release
,
3723 /* Display information about each subsystem and each hierarchy */
3724 static int proc_cgroupstats_show(struct seq_file
*m
, void *v
)
3728 seq_puts(m
, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
3730 * ideally we don't want subsystems moving around while we do this.
3731 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
3732 * subsys/hierarchy state.
3734 mutex_lock(&cgroup_mutex
);
3735 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
3736 struct cgroup_subsys
*ss
= subsys
[i
];
3739 seq_printf(m
, "%s\t%d\t%d\t%d\n",
3740 ss
->name
, ss
->root
->hierarchy_id
,
3741 ss
->root
->number_of_cgroups
, !ss
->disabled
);
3743 mutex_unlock(&cgroup_mutex
);
3747 static int cgroupstats_open(struct inode
*inode
, struct file
*file
)
3749 return single_open(file
, proc_cgroupstats_show
, NULL
);
3752 static const struct file_operations proc_cgroupstats_operations
= {
3753 .open
= cgroupstats_open
,
3755 .llseek
= seq_lseek
,
3756 .release
= single_release
,
3760 * cgroup_fork - attach newly forked task to its parents cgroup.
3761 * @child: pointer to task_struct of forking parent process.
3763 * Description: A task inherits its parent's cgroup at fork().
3765 * A pointer to the shared css_set was automatically copied in
3766 * fork.c by dup_task_struct(). However, we ignore that copy, since
3767 * it was not made under the protection of RCU or cgroup_mutex, so
3768 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
3769 * have already changed current->cgroups, allowing the previously
3770 * referenced cgroup group to be removed and freed.
3772 * At the point that cgroup_fork() is called, 'current' is the parent
3773 * task, and the passed argument 'child' points to the child task.
3775 void cgroup_fork(struct task_struct
*child
)
3778 child
->cgroups
= current
->cgroups
;
3779 get_css_set(child
->cgroups
);
3780 task_unlock(current
);
3781 INIT_LIST_HEAD(&child
->cg_list
);
3785 * cgroup_fork_callbacks - run fork callbacks
3786 * @child: the new task
3788 * Called on a new task very soon before adding it to the
3789 * tasklist. No need to take any locks since no-one can
3790 * be operating on this task.
3792 void cgroup_fork_callbacks(struct task_struct
*child
)
3794 if (need_forkexit_callback
) {
3797 * forkexit callbacks are only supported for builtin
3798 * subsystems, and the builtin section of the subsys array is
3799 * immutable, so we don't need to lock the subsys array here.
3801 for (i
= 0; i
< CGROUP_BUILTIN_SUBSYS_COUNT
; i
++) {
3802 struct cgroup_subsys
*ss
= subsys
[i
];
3804 ss
->fork(ss
, child
);
3810 * cgroup_post_fork - called on a new task after adding it to the task list
3811 * @child: the task in question
3813 * Adds the task to the list running through its css_set if necessary.
3814 * Has to be after the task is visible on the task list in case we race
3815 * with the first call to cgroup_iter_start() - to guarantee that the
3816 * new task ends up on its list.
3818 void cgroup_post_fork(struct task_struct
*child
)
3820 if (use_task_css_set_links
) {
3821 write_lock(&css_set_lock
);
3823 if (list_empty(&child
->cg_list
))
3824 list_add(&child
->cg_list
, &child
->cgroups
->tasks
);
3826 write_unlock(&css_set_lock
);
3830 * cgroup_exit - detach cgroup from exiting task
3831 * @tsk: pointer to task_struct of exiting process
3832 * @run_callback: run exit callbacks?
3834 * Description: Detach cgroup from @tsk and release it.
3836 * Note that cgroups marked notify_on_release force every task in
3837 * them to take the global cgroup_mutex mutex when exiting.
3838 * This could impact scaling on very large systems. Be reluctant to
3839 * use notify_on_release cgroups where very high task exit scaling
3840 * is required on large systems.
3842 * the_top_cgroup_hack:
3844 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
3846 * We call cgroup_exit() while the task is still competent to
3847 * handle notify_on_release(), then leave the task attached to the
3848 * root cgroup in each hierarchy for the remainder of its exit.
3850 * To do this properly, we would increment the reference count on
3851 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
3852 * code we would add a second cgroup function call, to drop that
3853 * reference. This would just create an unnecessary hot spot on
3854 * the top_cgroup reference count, to no avail.
3856 * Normally, holding a reference to a cgroup without bumping its
3857 * count is unsafe. The cgroup could go away, or someone could
3858 * attach us to a different cgroup, decrementing the count on
3859 * the first cgroup that we never incremented. But in this case,
3860 * top_cgroup isn't going away, and either task has PF_EXITING set,
3861 * which wards off any cgroup_attach_task() attempts, or task is a failed
3862 * fork, never visible to cgroup_attach_task.
3864 void cgroup_exit(struct task_struct
*tsk
, int run_callbacks
)
3869 if (run_callbacks
&& need_forkexit_callback
) {
3871 * modular subsystems can't use callbacks, so no need to lock
3874 for (i
= 0; i
< CGROUP_BUILTIN_SUBSYS_COUNT
; i
++) {
3875 struct cgroup_subsys
*ss
= subsys
[i
];
3882 * Unlink from the css_set task list if necessary.
3883 * Optimistically check cg_list before taking
3886 if (!list_empty(&tsk
->cg_list
)) {
3887 write_lock(&css_set_lock
);
3888 if (!list_empty(&tsk
->cg_list
))
3889 list_del(&tsk
->cg_list
);
3890 write_unlock(&css_set_lock
);
3893 /* Reassign the task to the init_css_set. */
3896 tsk
->cgroups
= &init_css_set
;
3899 put_css_set_taskexit(cg
);
3903 * cgroup_clone - clone the cgroup the given subsystem is attached to
3904 * @tsk: the task to be moved
3905 * @subsys: the given subsystem
3906 * @nodename: the name for the new cgroup
3908 * Duplicate the current cgroup in the hierarchy that the given
3909 * subsystem is attached to, and move this task into the new
3912 int cgroup_clone(struct task_struct
*tsk
, struct cgroup_subsys
*subsys
,
3915 struct dentry
*dentry
;
3917 struct cgroup
*parent
, *child
;
3918 struct inode
*inode
;
3920 struct cgroupfs_root
*root
;
3921 struct cgroup_subsys
*ss
;
3923 /* We shouldn't be called by an unregistered subsystem */
3924 BUG_ON(!subsys
->active
);
3926 /* First figure out what hierarchy and cgroup we're dealing
3927 * with, and pin them so we can drop cgroup_mutex */
3928 mutex_lock(&cgroup_mutex
);
3930 root
= subsys
->root
;
3931 if (root
== &rootnode
) {
3932 mutex_unlock(&cgroup_mutex
);
3936 /* Pin the hierarchy */
3937 if (!atomic_inc_not_zero(&root
->sb
->s_active
)) {
3938 /* We race with the final deactivate_super() */
3939 mutex_unlock(&cgroup_mutex
);
3943 /* Keep the cgroup alive */
3945 parent
= task_cgroup(tsk
, subsys
->subsys_id
);
3950 mutex_unlock(&cgroup_mutex
);
3952 /* Now do the VFS work to create a cgroup */
3953 inode
= parent
->dentry
->d_inode
;
3955 /* Hold the parent directory mutex across this operation to
3956 * stop anyone else deleting the new cgroup */
3957 mutex_lock(&inode
->i_mutex
);
3958 dentry
= lookup_one_len(nodename
, parent
->dentry
, strlen(nodename
));
3959 if (IS_ERR(dentry
)) {
3961 "cgroup: Couldn't allocate dentry for %s: %ld\n", nodename
,
3963 ret
= PTR_ERR(dentry
);
3967 /* Create the cgroup directory, which also creates the cgroup */
3968 ret
= vfs_mkdir(inode
, dentry
, 0755);
3969 child
= __d_cgrp(dentry
);
3973 "Failed to create cgroup %s: %d\n", nodename
,
3978 /* The cgroup now exists. Retake cgroup_mutex and check
3979 * that we're still in the same state that we thought we
3981 mutex_lock(&cgroup_mutex
);
3982 if ((root
!= subsys
->root
) ||
3983 (parent
!= task_cgroup(tsk
, subsys
->subsys_id
))) {
3984 /* Aargh, we raced ... */
3985 mutex_unlock(&inode
->i_mutex
);
3988 deactivate_super(root
->sb
);
3989 /* The cgroup is still accessible in the VFS, but
3990 * we're not going to try to rmdir() it at this
3993 "Race in cgroup_clone() - leaking cgroup %s\n",
3998 /* do any required auto-setup */
3999 for_each_subsys(root
, ss
) {
4001 ss
->post_clone(ss
, child
);
4004 /* All seems fine. Finish by moving the task into the new cgroup */
4005 ret
= cgroup_attach_task(child
, tsk
);
4006 mutex_unlock(&cgroup_mutex
);
4009 mutex_unlock(&inode
->i_mutex
);
4011 mutex_lock(&cgroup_mutex
);
4013 mutex_unlock(&cgroup_mutex
);
4014 deactivate_super(root
->sb
);
4019 * cgroup_is_descendant - see if @cgrp is a descendant of @task's cgrp
4020 * @cgrp: the cgroup in question
4021 * @task: the task in question
4023 * See if @cgrp is a descendant of @task's cgroup in the appropriate
4026 * If we are sending in dummytop, then presumably we are creating
4027 * the top cgroup in the subsystem.
4029 * Called only by the ns (nsproxy) cgroup.
4031 int cgroup_is_descendant(const struct cgroup
*cgrp
, struct task_struct
*task
)
4034 struct cgroup
*target
;
4036 if (cgrp
== dummytop
)
4039 target
= task_cgroup_from_root(task
, cgrp
->root
);
4040 while (cgrp
!= target
&& cgrp
!= cgrp
->top_cgroup
)
4041 cgrp
= cgrp
->parent
;
4042 ret
= (cgrp
== target
);
4046 static void check_for_release(struct cgroup
*cgrp
)
4048 /* All of these checks rely on RCU to keep the cgroup
4049 * structure alive */
4050 if (cgroup_is_releasable(cgrp
) && !atomic_read(&cgrp
->count
)
4051 && list_empty(&cgrp
->children
) && !cgroup_has_css_refs(cgrp
)) {
4052 /* Control Group is currently removeable. If it's not
4053 * already queued for a userspace notification, queue
4055 int need_schedule_work
= 0;
4056 spin_lock(&release_list_lock
);
4057 if (!cgroup_is_removed(cgrp
) &&
4058 list_empty(&cgrp
->release_list
)) {
4059 list_add(&cgrp
->release_list
, &release_list
);
4060 need_schedule_work
= 1;
4062 spin_unlock(&release_list_lock
);
4063 if (need_schedule_work
)
4064 schedule_work(&release_agent_work
);
4068 /* Caller must verify that the css is not for root cgroup */
4069 void __css_put(struct cgroup_subsys_state
*css
, int count
)
4071 struct cgroup
*cgrp
= css
->cgroup
;
4074 val
= atomic_sub_return(count
, &css
->refcnt
);
4076 if (notify_on_release(cgrp
)) {
4077 set_bit(CGRP_RELEASABLE
, &cgrp
->flags
);
4078 check_for_release(cgrp
);
4080 cgroup_wakeup_rmdir_waiter(cgrp
);
4083 WARN_ON_ONCE(val
< 1);
4087 * Notify userspace when a cgroup is released, by running the
4088 * configured release agent with the name of the cgroup (path
4089 * relative to the root of cgroup file system) as the argument.
4091 * Most likely, this user command will try to rmdir this cgroup.
4093 * This races with the possibility that some other task will be
4094 * attached to this cgroup before it is removed, or that some other
4095 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
4096 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
4097 * unused, and this cgroup will be reprieved from its death sentence,
4098 * to continue to serve a useful existence. Next time it's released,
4099 * we will get notified again, if it still has 'notify_on_release' set.
4101 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
4102 * means only wait until the task is successfully execve()'d. The
4103 * separate release agent task is forked by call_usermodehelper(),
4104 * then control in this thread returns here, without waiting for the
4105 * release agent task. We don't bother to wait because the caller of
4106 * this routine has no use for the exit status of the release agent
4107 * task, so no sense holding our caller up for that.
4109 static void cgroup_release_agent(struct work_struct
*work
)
4111 BUG_ON(work
!= &release_agent_work
);
4112 mutex_lock(&cgroup_mutex
);
4113 spin_lock(&release_list_lock
);
4114 while (!list_empty(&release_list
)) {
4115 char *argv
[3], *envp
[3];
4117 char *pathbuf
= NULL
, *agentbuf
= NULL
;
4118 struct cgroup
*cgrp
= list_entry(release_list
.next
,
4121 list_del_init(&cgrp
->release_list
);
4122 spin_unlock(&release_list_lock
);
4123 pathbuf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
4126 if (cgroup_path(cgrp
, pathbuf
, PAGE_SIZE
) < 0)
4128 agentbuf
= kstrdup(cgrp
->root
->release_agent_path
, GFP_KERNEL
);
4133 argv
[i
++] = agentbuf
;
4134 argv
[i
++] = pathbuf
;
4138 /* minimal command environment */
4139 envp
[i
++] = "HOME=/";
4140 envp
[i
++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
4143 /* Drop the lock while we invoke the usermode helper,
4144 * since the exec could involve hitting disk and hence
4145 * be a slow process */
4146 mutex_unlock(&cgroup_mutex
);
4147 call_usermodehelper(argv
[0], argv
, envp
, UMH_WAIT_EXEC
);
4148 mutex_lock(&cgroup_mutex
);
4152 spin_lock(&release_list_lock
);
4154 spin_unlock(&release_list_lock
);
4155 mutex_unlock(&cgroup_mutex
);
4158 static int __init
cgroup_disable(char *str
)
4163 while ((token
= strsep(&str
, ",")) != NULL
) {
4167 * cgroup_disable, being at boot time, can't know about module
4168 * subsystems, so we don't worry about them.
4170 for (i
= 0; i
< CGROUP_BUILTIN_SUBSYS_COUNT
; i
++) {
4171 struct cgroup_subsys
*ss
= subsys
[i
];
4173 if (!strcmp(token
, ss
->name
)) {
4175 printk(KERN_INFO
"Disabling %s control group"
4176 " subsystem\n", ss
->name
);
4183 __setup("cgroup_disable=", cgroup_disable
);
4186 * Functons for CSS ID.
4190 *To get ID other than 0, this should be called when !cgroup_is_removed().
4192 unsigned short css_id(struct cgroup_subsys_state
*css
)
4194 struct css_id
*cssid
= rcu_dereference(css
->id
);
4201 unsigned short css_depth(struct cgroup_subsys_state
*css
)
4203 struct css_id
*cssid
= rcu_dereference(css
->id
);
4206 return cssid
->depth
;
4210 bool css_is_ancestor(struct cgroup_subsys_state
*child
,
4211 const struct cgroup_subsys_state
*root
)
4213 struct css_id
*child_id
= rcu_dereference(child
->id
);
4214 struct css_id
*root_id
= rcu_dereference(root
->id
);
4216 if (!child_id
|| !root_id
|| (child_id
->depth
< root_id
->depth
))
4218 return child_id
->stack
[root_id
->depth
] == root_id
->id
;
4221 static void __free_css_id_cb(struct rcu_head
*head
)
4225 id
= container_of(head
, struct css_id
, rcu_head
);
4229 void free_css_id(struct cgroup_subsys
*ss
, struct cgroup_subsys_state
*css
)
4231 struct css_id
*id
= css
->id
;
4232 /* When this is called before css_id initialization, id can be NULL */
4236 BUG_ON(!ss
->use_id
);
4238 rcu_assign_pointer(id
->css
, NULL
);
4239 rcu_assign_pointer(css
->id
, NULL
);
4240 spin_lock(&ss
->id_lock
);
4241 idr_remove(&ss
->idr
, id
->id
);
4242 spin_unlock(&ss
->id_lock
);
4243 call_rcu(&id
->rcu_head
, __free_css_id_cb
);
4247 * This is called by init or create(). Then, calls to this function are
4248 * always serialized (By cgroup_mutex() at create()).
4251 static struct css_id
*get_new_cssid(struct cgroup_subsys
*ss
, int depth
)
4253 struct css_id
*newid
;
4254 int myid
, error
, size
;
4256 BUG_ON(!ss
->use_id
);
4258 size
= sizeof(*newid
) + sizeof(unsigned short) * (depth
+ 1);
4259 newid
= kzalloc(size
, GFP_KERNEL
);
4261 return ERR_PTR(-ENOMEM
);
4263 if (unlikely(!idr_pre_get(&ss
->idr
, GFP_KERNEL
))) {
4267 spin_lock(&ss
->id_lock
);
4268 /* Don't use 0. allocates an ID of 1-65535 */
4269 error
= idr_get_new_above(&ss
->idr
, newid
, 1, &myid
);
4270 spin_unlock(&ss
->id_lock
);
4272 /* Returns error when there are no free spaces for new ID.*/
4277 if (myid
> CSS_ID_MAX
)
4281 newid
->depth
= depth
;
4285 spin_lock(&ss
->id_lock
);
4286 idr_remove(&ss
->idr
, myid
);
4287 spin_unlock(&ss
->id_lock
);
4290 return ERR_PTR(error
);
4294 static int __init_or_module
cgroup_init_idr(struct cgroup_subsys
*ss
,
4295 struct cgroup_subsys_state
*rootcss
)
4297 struct css_id
*newid
;
4299 spin_lock_init(&ss
->id_lock
);
4302 newid
= get_new_cssid(ss
, 0);
4304 return PTR_ERR(newid
);
4306 newid
->stack
[0] = newid
->id
;
4307 newid
->css
= rootcss
;
4308 rootcss
->id
= newid
;
4312 static int alloc_css_id(struct cgroup_subsys
*ss
, struct cgroup
*parent
,
4313 struct cgroup
*child
)
4315 int subsys_id
, i
, depth
= 0;
4316 struct cgroup_subsys_state
*parent_css
, *child_css
;
4317 struct css_id
*child_id
, *parent_id
= NULL
;
4319 subsys_id
= ss
->subsys_id
;
4320 parent_css
= parent
->subsys
[subsys_id
];
4321 child_css
= child
->subsys
[subsys_id
];
4322 depth
= css_depth(parent_css
) + 1;
4323 parent_id
= parent_css
->id
;
4325 child_id
= get_new_cssid(ss
, depth
);
4326 if (IS_ERR(child_id
))
4327 return PTR_ERR(child_id
);
4329 for (i
= 0; i
< depth
; i
++)
4330 child_id
->stack
[i
] = parent_id
->stack
[i
];
4331 child_id
->stack
[depth
] = child_id
->id
;
4333 * child_id->css pointer will be set after this cgroup is available
4334 * see cgroup_populate_dir()
4336 rcu_assign_pointer(child_css
->id
, child_id
);
4342 * css_lookup - lookup css by id
4343 * @ss: cgroup subsys to be looked into.
4346 * Returns pointer to cgroup_subsys_state if there is valid one with id.
4347 * NULL if not. Should be called under rcu_read_lock()
4349 struct cgroup_subsys_state
*css_lookup(struct cgroup_subsys
*ss
, int id
)
4351 struct css_id
*cssid
= NULL
;
4353 BUG_ON(!ss
->use_id
);
4354 cssid
= idr_find(&ss
->idr
, id
);
4356 if (unlikely(!cssid
))
4359 return rcu_dereference(cssid
->css
);
4363 * css_get_next - lookup next cgroup under specified hierarchy.
4364 * @ss: pointer to subsystem
4365 * @id: current position of iteration.
4366 * @root: pointer to css. search tree under this.
4367 * @foundid: position of found object.
4369 * Search next css under the specified hierarchy of rootid. Calling under
4370 * rcu_read_lock() is necessary. Returns NULL if it reaches the end.
4372 struct cgroup_subsys_state
*
4373 css_get_next(struct cgroup_subsys
*ss
, int id
,
4374 struct cgroup_subsys_state
*root
, int *foundid
)
4376 struct cgroup_subsys_state
*ret
= NULL
;
4379 int rootid
= css_id(root
);
4380 int depth
= css_depth(root
);
4385 BUG_ON(!ss
->use_id
);
4386 /* fill start point for scan */
4390 * scan next entry from bitmap(tree), tmpid is updated after
4393 spin_lock(&ss
->id_lock
);
4394 tmp
= idr_get_next(&ss
->idr
, &tmpid
);
4395 spin_unlock(&ss
->id_lock
);
4399 if (tmp
->depth
>= depth
&& tmp
->stack
[depth
] == rootid
) {
4400 ret
= rcu_dereference(tmp
->css
);
4406 /* continue to scan from next id */
4412 #ifdef CONFIG_CGROUP_DEBUG
4413 static struct cgroup_subsys_state
*debug_create(struct cgroup_subsys
*ss
,
4414 struct cgroup
*cont
)
4416 struct cgroup_subsys_state
*css
= kzalloc(sizeof(*css
), GFP_KERNEL
);
4419 return ERR_PTR(-ENOMEM
);
4424 static void debug_destroy(struct cgroup_subsys
*ss
, struct cgroup
*cont
)
4426 kfree(cont
->subsys
[debug_subsys_id
]);
4429 static u64
cgroup_refcount_read(struct cgroup
*cont
, struct cftype
*cft
)
4431 return atomic_read(&cont
->count
);
4434 static u64
debug_taskcount_read(struct cgroup
*cont
, struct cftype
*cft
)
4436 return cgroup_task_count(cont
);
4439 static u64
current_css_set_read(struct cgroup
*cont
, struct cftype
*cft
)
4441 return (u64
)(unsigned long)current
->cgroups
;
4444 static u64
current_css_set_refcount_read(struct cgroup
*cont
,
4450 count
= atomic_read(¤t
->cgroups
->refcount
);
4455 static int current_css_set_cg_links_read(struct cgroup
*cont
,
4457 struct seq_file
*seq
)
4459 struct cg_cgroup_link
*link
;
4462 read_lock(&css_set_lock
);
4464 cg
= rcu_dereference(current
->cgroups
);
4465 list_for_each_entry(link
, &cg
->cg_links
, cg_link_list
) {
4466 struct cgroup
*c
= link
->cgrp
;
4470 name
= c
->dentry
->d_name
.name
;
4473 seq_printf(seq
, "Root %d group %s\n",
4474 c
->root
->hierarchy_id
, name
);
4477 read_unlock(&css_set_lock
);
4481 #define MAX_TASKS_SHOWN_PER_CSS 25
4482 static int cgroup_css_links_read(struct cgroup
*cont
,
4484 struct seq_file
*seq
)
4486 struct cg_cgroup_link
*link
;
4488 read_lock(&css_set_lock
);
4489 list_for_each_entry(link
, &cont
->css_sets
, cgrp_link_list
) {
4490 struct css_set
*cg
= link
->cg
;
4491 struct task_struct
*task
;
4493 seq_printf(seq
, "css_set %p\n", cg
);
4494 list_for_each_entry(task
, &cg
->tasks
, cg_list
) {
4495 if (count
++ > MAX_TASKS_SHOWN_PER_CSS
) {
4496 seq_puts(seq
, " ...\n");
4499 seq_printf(seq
, " task %d\n",
4500 task_pid_vnr(task
));
4504 read_unlock(&css_set_lock
);
4508 static u64
releasable_read(struct cgroup
*cgrp
, struct cftype
*cft
)
4510 return test_bit(CGRP_RELEASABLE
, &cgrp
->flags
);
4513 static struct cftype debug_files
[] = {
4515 .name
= "cgroup_refcount",
4516 .read_u64
= cgroup_refcount_read
,
4519 .name
= "taskcount",
4520 .read_u64
= debug_taskcount_read
,
4524 .name
= "current_css_set",
4525 .read_u64
= current_css_set_read
,
4529 .name
= "current_css_set_refcount",
4530 .read_u64
= current_css_set_refcount_read
,
4534 .name
= "current_css_set_cg_links",
4535 .read_seq_string
= current_css_set_cg_links_read
,
4539 .name
= "cgroup_css_links",
4540 .read_seq_string
= cgroup_css_links_read
,
4544 .name
= "releasable",
4545 .read_u64
= releasable_read
,
4549 static int debug_populate(struct cgroup_subsys
*ss
, struct cgroup
*cont
)
4551 return cgroup_add_files(cont
, ss
, debug_files
,
4552 ARRAY_SIZE(debug_files
));
4555 struct cgroup_subsys debug_subsys
= {
4557 .create
= debug_create
,
4558 .destroy
= debug_destroy
,
4559 .populate
= debug_populate
,
4560 .subsys_id
= debug_subsys_id
,
4562 #endif /* CONFIG_CGROUP_DEBUG */