2 * Generic process-grouping system.
4 * Based originally on the cpuset system, extracted by Paul Menage
5 * Copyright (C) 2006 Google, Inc
7 * Notifications support
8 * Copyright (C) 2009 Nokia Corporation
9 * Author: Kirill A. Shutemov
11 * Copyright notices from the original cpuset code:
12 * --------------------------------------------------
13 * Copyright (C) 2003 BULL SA.
14 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
16 * Portions derived from Patrick Mochel's sysfs code.
17 * sysfs is Copyright (c) 2001-3 Patrick Mochel
19 * 2003-10-10 Written by Simon Derr.
20 * 2003-10-22 Updates by Stephen Hemminger.
21 * 2004 May-July Rework by Paul Jackson.
22 * ---------------------------------------------------
24 * This file is subject to the terms and conditions of the GNU General Public
25 * License. See the file COPYING in the main directory of the Linux
26 * distribution for more details.
29 #include <linux/cgroup.h>
30 #include <linux/ctype.h>
31 #include <linux/errno.h>
33 #include <linux/kernel.h>
34 #include <linux/list.h>
36 #include <linux/mutex.h>
37 #include <linux/mount.h>
38 #include <linux/pagemap.h>
39 #include <linux/proc_fs.h>
40 #include <linux/rcupdate.h>
41 #include <linux/sched.h>
42 #include <linux/backing-dev.h>
43 #include <linux/seq_file.h>
44 #include <linux/slab.h>
45 #include <linux/magic.h>
46 #include <linux/spinlock.h>
47 #include <linux/string.h>
48 #include <linux/sort.h>
49 #include <linux/kmod.h>
50 #include <linux/module.h>
51 #include <linux/delayacct.h>
52 #include <linux/cgroupstats.h>
53 #include <linux/hash.h>
54 #include <linux/namei.h>
55 #include <linux/pid_namespace.h>
56 #include <linux/idr.h>
57 #include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
58 #include <linux/eventfd.h>
59 #include <linux/poll.h>
60 #include <linux/flex_array.h> /* used in cgroup_attach_proc */
62 #include <asm/atomic.h>
64 static DEFINE_MUTEX(cgroup_mutex
);
67 * Generate an array of cgroup subsystem pointers. At boot time, this is
68 * populated up to CGROUP_BUILTIN_SUBSYS_COUNT, and modular subsystems are
69 * registered after that. The mutable section of this array is protected by
72 #define SUBSYS(_x) &_x ## _subsys,
73 static struct cgroup_subsys
*subsys
[CGROUP_SUBSYS_COUNT
] = {
74 #include <linux/cgroup_subsys.h>
77 #define MAX_CGROUP_ROOT_NAMELEN 64
80 * A cgroupfs_root represents the root of a cgroup hierarchy,
81 * and may be associated with a superblock to form an active
84 struct cgroupfs_root
{
85 struct super_block
*sb
;
88 * The bitmask of subsystems intended to be attached to this
91 unsigned long subsys_bits
;
93 /* Unique id for this hierarchy. */
96 /* The bitmask of subsystems currently attached to this hierarchy */
97 unsigned long actual_subsys_bits
;
99 /* A list running through the attached subsystems */
100 struct list_head subsys_list
;
102 /* The root cgroup for this hierarchy */
103 struct cgroup top_cgroup
;
105 /* Tracks how many cgroups are currently defined in hierarchy.*/
106 int number_of_cgroups
;
108 /* A list running through the active hierarchies */
109 struct list_head root_list
;
111 /* Hierarchy-specific flags */
114 /* The path to use for release notifications. */
115 char release_agent_path
[PATH_MAX
];
117 /* The name for this hierarchy - may be empty */
118 char name
[MAX_CGROUP_ROOT_NAMELEN
];
122 * The "rootnode" hierarchy is the "dummy hierarchy", reserved for the
123 * subsystems that are otherwise unattached - it never has more than a
124 * single cgroup, and all tasks are part of that cgroup.
126 static struct cgroupfs_root rootnode
;
129 * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when
130 * cgroup_subsys->use_id != 0.
132 #define CSS_ID_MAX (65535)
135 * The css to which this ID points. This pointer is set to valid value
136 * after cgroup is populated. If cgroup is removed, this will be NULL.
137 * This pointer is expected to be RCU-safe because destroy()
138 * is called after synchronize_rcu(). But for safe use, css_is_removed()
139 * css_tryget() should be used for avoiding race.
141 struct cgroup_subsys_state __rcu
*css
;
147 * Depth in hierarchy which this ID belongs to.
149 unsigned short depth
;
151 * ID is freed by RCU. (and lookup routine is RCU safe.)
153 struct rcu_head rcu_head
;
155 * Hierarchy of CSS ID belongs to.
157 unsigned short stack
[0]; /* Array of Length (depth+1) */
161 * cgroup_event represents events which userspace want to receive.
163 struct cgroup_event
{
165 * Cgroup which the event belongs to.
169 * Control file which the event associated.
173 * eventfd to signal userspace about the event.
175 struct eventfd_ctx
*eventfd
;
177 * Each of these stored in a list by the cgroup.
179 struct list_head list
;
181 * All fields below needed to unregister event when
182 * userspace closes eventfd.
185 wait_queue_head_t
*wqh
;
187 struct work_struct remove
;
190 /* The list of hierarchy roots */
192 static LIST_HEAD(roots
);
193 static int root_count
;
195 static DEFINE_IDA(hierarchy_ida
);
196 static int next_hierarchy_id
;
197 static DEFINE_SPINLOCK(hierarchy_id_lock
);
199 /* dummytop is a shorthand for the dummy hierarchy's top cgroup */
200 #define dummytop (&rootnode.top_cgroup)
202 /* This flag indicates whether tasks in the fork and exit paths should
203 * check for fork/exit handlers to call. This avoids us having to do
204 * extra work in the fork/exit path if none of the subsystems need to
207 static int need_forkexit_callback __read_mostly
;
209 #ifdef CONFIG_PROVE_LOCKING
210 int cgroup_lock_is_held(void)
212 return lockdep_is_held(&cgroup_mutex
);
214 #else /* #ifdef CONFIG_PROVE_LOCKING */
215 int cgroup_lock_is_held(void)
217 return mutex_is_locked(&cgroup_mutex
);
219 #endif /* #else #ifdef CONFIG_PROVE_LOCKING */
221 EXPORT_SYMBOL_GPL(cgroup_lock_is_held
);
223 /* convenient tests for these bits */
224 inline int cgroup_is_removed(const struct cgroup
*cgrp
)
226 return test_bit(CGRP_REMOVED
, &cgrp
->flags
);
229 /* bits in struct cgroupfs_root flags field */
231 ROOT_NOPREFIX
, /* mounted subsystems have no named prefix */
234 static int cgroup_is_releasable(const struct cgroup
*cgrp
)
237 (1 << CGRP_RELEASABLE
) |
238 (1 << CGRP_NOTIFY_ON_RELEASE
);
239 return (cgrp
->flags
& bits
) == bits
;
242 static int notify_on_release(const struct cgroup
*cgrp
)
244 return test_bit(CGRP_NOTIFY_ON_RELEASE
, &cgrp
->flags
);
247 static int clone_children(const struct cgroup
*cgrp
)
249 return test_bit(CGRP_CLONE_CHILDREN
, &cgrp
->flags
);
253 * for_each_subsys() allows you to iterate on each subsystem attached to
254 * an active hierarchy
256 #define for_each_subsys(_root, _ss) \
257 list_for_each_entry(_ss, &_root->subsys_list, sibling)
259 /* for_each_active_root() allows you to iterate across the active hierarchies */
260 #define for_each_active_root(_root) \
261 list_for_each_entry(_root, &roots, root_list)
263 /* the list of cgroups eligible for automatic release. Protected by
264 * release_list_lock */
265 static LIST_HEAD(release_list
);
266 static DEFINE_SPINLOCK(release_list_lock
);
267 static void cgroup_release_agent(struct work_struct
*work
);
268 static DECLARE_WORK(release_agent_work
, cgroup_release_agent
);
269 static void check_for_release(struct cgroup
*cgrp
);
271 /* Link structure for associating css_set objects with cgroups */
272 struct cg_cgroup_link
{
274 * List running through cg_cgroup_links associated with a
275 * cgroup, anchored on cgroup->css_sets
277 struct list_head cgrp_link_list
;
280 * List running through cg_cgroup_links pointing at a
281 * single css_set object, anchored on css_set->cg_links
283 struct list_head cg_link_list
;
287 /* The default css_set - used by init and its children prior to any
288 * hierarchies being mounted. It contains a pointer to the root state
289 * for each subsystem. Also used to anchor the list of css_sets. Not
290 * reference-counted, to improve performance when child cgroups
291 * haven't been created.
294 static struct css_set init_css_set
;
295 static struct cg_cgroup_link init_css_set_link
;
297 static int cgroup_init_idr(struct cgroup_subsys
*ss
,
298 struct cgroup_subsys_state
*css
);
300 /* css_set_lock protects the list of css_set objects, and the
301 * chain of tasks off each css_set. Nests outside task->alloc_lock
302 * due to cgroup_iter_start() */
303 static DEFINE_RWLOCK(css_set_lock
);
304 static int css_set_count
;
307 * hash table for cgroup groups. This improves the performance to find
308 * an existing css_set. This hash doesn't (currently) take into
309 * account cgroups in empty hierarchies.
311 #define CSS_SET_HASH_BITS 7
312 #define CSS_SET_TABLE_SIZE (1 << CSS_SET_HASH_BITS)
313 static struct hlist_head css_set_table
[CSS_SET_TABLE_SIZE
];
315 static struct hlist_head
*css_set_hash(struct cgroup_subsys_state
*css
[])
319 unsigned long tmp
= 0UL;
321 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++)
322 tmp
+= (unsigned long)css
[i
];
323 tmp
= (tmp
>> 16) ^ tmp
;
325 index
= hash_long(tmp
, CSS_SET_HASH_BITS
);
327 return &css_set_table
[index
];
330 /* We don't maintain the lists running through each css_set to its
331 * task until after the first call to cgroup_iter_start(). This
332 * reduces the fork()/exit() overhead for people who have cgroups
333 * compiled into their kernel but not actually in use */
334 static int use_task_css_set_links __read_mostly
;
336 static void __put_css_set(struct css_set
*cg
, int taskexit
)
338 struct cg_cgroup_link
*link
;
339 struct cg_cgroup_link
*saved_link
;
341 * Ensure that the refcount doesn't hit zero while any readers
342 * can see it. Similar to atomic_dec_and_lock(), but for an
345 if (atomic_add_unless(&cg
->refcount
, -1, 1))
347 write_lock(&css_set_lock
);
348 if (!atomic_dec_and_test(&cg
->refcount
)) {
349 write_unlock(&css_set_lock
);
353 /* This css_set is dead. unlink it and release cgroup refcounts */
354 hlist_del(&cg
->hlist
);
357 list_for_each_entry_safe(link
, saved_link
, &cg
->cg_links
,
359 struct cgroup
*cgrp
= link
->cgrp
;
360 list_del(&link
->cg_link_list
);
361 list_del(&link
->cgrp_link_list
);
362 if (atomic_dec_and_test(&cgrp
->count
) &&
363 notify_on_release(cgrp
)) {
365 set_bit(CGRP_RELEASABLE
, &cgrp
->flags
);
366 check_for_release(cgrp
);
372 write_unlock(&css_set_lock
);
373 kfree_rcu(cg
, rcu_head
);
377 * refcounted get/put for css_set objects
379 static inline void get_css_set(struct css_set
*cg
)
381 atomic_inc(&cg
->refcount
);
384 static inline void put_css_set(struct css_set
*cg
)
386 __put_css_set(cg
, 0);
389 static inline void put_css_set_taskexit(struct css_set
*cg
)
391 __put_css_set(cg
, 1);
395 * compare_css_sets - helper function for find_existing_css_set().
396 * @cg: candidate css_set being tested
397 * @old_cg: existing css_set for a task
398 * @new_cgrp: cgroup that's being entered by the task
399 * @template: desired set of css pointers in css_set (pre-calculated)
401 * Returns true if "cg" matches "old_cg" except for the hierarchy
402 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
404 static bool compare_css_sets(struct css_set
*cg
,
405 struct css_set
*old_cg
,
406 struct cgroup
*new_cgrp
,
407 struct cgroup_subsys_state
*template[])
409 struct list_head
*l1
, *l2
;
411 if (memcmp(template, cg
->subsys
, sizeof(cg
->subsys
))) {
412 /* Not all subsystems matched */
417 * Compare cgroup pointers in order to distinguish between
418 * different cgroups in heirarchies with no subsystems. We
419 * could get by with just this check alone (and skip the
420 * memcmp above) but on most setups the memcmp check will
421 * avoid the need for this more expensive check on almost all
426 l2
= &old_cg
->cg_links
;
428 struct cg_cgroup_link
*cgl1
, *cgl2
;
429 struct cgroup
*cg1
, *cg2
;
433 /* See if we reached the end - both lists are equal length. */
434 if (l1
== &cg
->cg_links
) {
435 BUG_ON(l2
!= &old_cg
->cg_links
);
438 BUG_ON(l2
== &old_cg
->cg_links
);
440 /* Locate the cgroups associated with these links. */
441 cgl1
= list_entry(l1
, struct cg_cgroup_link
, cg_link_list
);
442 cgl2
= list_entry(l2
, struct cg_cgroup_link
, cg_link_list
);
445 /* Hierarchies should be linked in the same order. */
446 BUG_ON(cg1
->root
!= cg2
->root
);
449 * If this hierarchy is the hierarchy of the cgroup
450 * that's changing, then we need to check that this
451 * css_set points to the new cgroup; if it's any other
452 * hierarchy, then this css_set should point to the
453 * same cgroup as the old css_set.
455 if (cg1
->root
== new_cgrp
->root
) {
467 * find_existing_css_set() is a helper for
468 * find_css_set(), and checks to see whether an existing
469 * css_set is suitable.
471 * oldcg: the cgroup group that we're using before the cgroup
474 * cgrp: the cgroup that we're moving into
476 * template: location in which to build the desired set of subsystem
477 * state objects for the new cgroup group
479 static struct css_set
*find_existing_css_set(
480 struct css_set
*oldcg
,
482 struct cgroup_subsys_state
*template[])
485 struct cgroupfs_root
*root
= cgrp
->root
;
486 struct hlist_head
*hhead
;
487 struct hlist_node
*node
;
491 * Build the set of subsystem state objects that we want to see in the
492 * new css_set. while subsystems can change globally, the entries here
493 * won't change, so no need for locking.
495 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
496 if (root
->subsys_bits
& (1UL << i
)) {
497 /* Subsystem is in this hierarchy. So we want
498 * the subsystem state from the new
500 template[i
] = cgrp
->subsys
[i
];
502 /* Subsystem is not in this hierarchy, so we
503 * don't want to change the subsystem state */
504 template[i
] = oldcg
->subsys
[i
];
508 hhead
= css_set_hash(template);
509 hlist_for_each_entry(cg
, node
, hhead
, hlist
) {
510 if (!compare_css_sets(cg
, oldcg
, cgrp
, template))
513 /* This css_set matches what we need */
517 /* No existing cgroup group matched */
521 static void free_cg_links(struct list_head
*tmp
)
523 struct cg_cgroup_link
*link
;
524 struct cg_cgroup_link
*saved_link
;
526 list_for_each_entry_safe(link
, saved_link
, tmp
, cgrp_link_list
) {
527 list_del(&link
->cgrp_link_list
);
533 * allocate_cg_links() allocates "count" cg_cgroup_link structures
534 * and chains them on tmp through their cgrp_link_list fields. Returns 0 on
535 * success or a negative error
537 static int allocate_cg_links(int count
, struct list_head
*tmp
)
539 struct cg_cgroup_link
*link
;
542 for (i
= 0; i
< count
; i
++) {
543 link
= kmalloc(sizeof(*link
), GFP_KERNEL
);
548 list_add(&link
->cgrp_link_list
, tmp
);
554 * link_css_set - a helper function to link a css_set to a cgroup
555 * @tmp_cg_links: cg_cgroup_link objects allocated by allocate_cg_links()
556 * @cg: the css_set to be linked
557 * @cgrp: the destination cgroup
559 static void link_css_set(struct list_head
*tmp_cg_links
,
560 struct css_set
*cg
, struct cgroup
*cgrp
)
562 struct cg_cgroup_link
*link
;
564 BUG_ON(list_empty(tmp_cg_links
));
565 link
= list_first_entry(tmp_cg_links
, struct cg_cgroup_link
,
569 atomic_inc(&cgrp
->count
);
570 list_move(&link
->cgrp_link_list
, &cgrp
->css_sets
);
572 * Always add links to the tail of the list so that the list
573 * is sorted by order of hierarchy creation
575 list_add_tail(&link
->cg_link_list
, &cg
->cg_links
);
579 * find_css_set() takes an existing cgroup group and a
580 * cgroup object, and returns a css_set object that's
581 * equivalent to the old group, but with the given cgroup
582 * substituted into the appropriate hierarchy. Must be called with
585 static struct css_set
*find_css_set(
586 struct css_set
*oldcg
, struct cgroup
*cgrp
)
589 struct cgroup_subsys_state
*template[CGROUP_SUBSYS_COUNT
];
591 struct list_head tmp_cg_links
;
593 struct hlist_head
*hhead
;
594 struct cg_cgroup_link
*link
;
596 /* First see if we already have a cgroup group that matches
598 read_lock(&css_set_lock
);
599 res
= find_existing_css_set(oldcg
, cgrp
, template);
602 read_unlock(&css_set_lock
);
607 res
= kmalloc(sizeof(*res
), GFP_KERNEL
);
611 /* Allocate all the cg_cgroup_link objects that we'll need */
612 if (allocate_cg_links(root_count
, &tmp_cg_links
) < 0) {
617 atomic_set(&res
->refcount
, 1);
618 INIT_LIST_HEAD(&res
->cg_links
);
619 INIT_LIST_HEAD(&res
->tasks
);
620 INIT_HLIST_NODE(&res
->hlist
);
622 /* Copy the set of subsystem state objects generated in
623 * find_existing_css_set() */
624 memcpy(res
->subsys
, template, sizeof(res
->subsys
));
626 write_lock(&css_set_lock
);
627 /* Add reference counts and links from the new css_set. */
628 list_for_each_entry(link
, &oldcg
->cg_links
, cg_link_list
) {
629 struct cgroup
*c
= link
->cgrp
;
630 if (c
->root
== cgrp
->root
)
632 link_css_set(&tmp_cg_links
, res
, c
);
635 BUG_ON(!list_empty(&tmp_cg_links
));
639 /* Add this cgroup group to the hash table */
640 hhead
= css_set_hash(res
->subsys
);
641 hlist_add_head(&res
->hlist
, hhead
);
643 write_unlock(&css_set_lock
);
649 * Return the cgroup for "task" from the given hierarchy. Must be
650 * called with cgroup_mutex held.
652 static struct cgroup
*task_cgroup_from_root(struct task_struct
*task
,
653 struct cgroupfs_root
*root
)
656 struct cgroup
*res
= NULL
;
658 BUG_ON(!mutex_is_locked(&cgroup_mutex
));
659 read_lock(&css_set_lock
);
661 * No need to lock the task - since we hold cgroup_mutex the
662 * task can't change groups, so the only thing that can happen
663 * is that it exits and its css is set back to init_css_set.
666 if (css
== &init_css_set
) {
667 res
= &root
->top_cgroup
;
669 struct cg_cgroup_link
*link
;
670 list_for_each_entry(link
, &css
->cg_links
, cg_link_list
) {
671 struct cgroup
*c
= link
->cgrp
;
672 if (c
->root
== root
) {
678 read_unlock(&css_set_lock
);
684 * There is one global cgroup mutex. We also require taking
685 * task_lock() when dereferencing a task's cgroup subsys pointers.
686 * See "The task_lock() exception", at the end of this comment.
688 * A task must hold cgroup_mutex to modify cgroups.
690 * Any task can increment and decrement the count field without lock.
691 * So in general, code holding cgroup_mutex can't rely on the count
692 * field not changing. However, if the count goes to zero, then only
693 * cgroup_attach_task() can increment it again. Because a count of zero
694 * means that no tasks are currently attached, therefore there is no
695 * way a task attached to that cgroup can fork (the other way to
696 * increment the count). So code holding cgroup_mutex can safely
697 * assume that if the count is zero, it will stay zero. Similarly, if
698 * a task holds cgroup_mutex on a cgroup with zero count, it
699 * knows that the cgroup won't be removed, as cgroup_rmdir()
702 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
703 * (usually) take cgroup_mutex. These are the two most performance
704 * critical pieces of code here. The exception occurs on cgroup_exit(),
705 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
706 * is taken, and if the cgroup count is zero, a usermode call made
707 * to the release agent with the name of the cgroup (path relative to
708 * the root of cgroup file system) as the argument.
710 * A cgroup can only be deleted if both its 'count' of using tasks
711 * is zero, and its list of 'children' cgroups is empty. Since all
712 * tasks in the system use _some_ cgroup, and since there is always at
713 * least one task in the system (init, pid == 1), therefore, top_cgroup
714 * always has either children cgroups and/or using tasks. So we don't
715 * need a special hack to ensure that top_cgroup cannot be deleted.
717 * The task_lock() exception
719 * The need for this exception arises from the action of
720 * cgroup_attach_task(), which overwrites one tasks cgroup pointer with
721 * another. It does so using cgroup_mutex, however there are
722 * several performance critical places that need to reference
723 * task->cgroup without the expense of grabbing a system global
724 * mutex. Therefore except as noted below, when dereferencing or, as
725 * in cgroup_attach_task(), modifying a task'ss cgroup pointer we use
726 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
727 * the task_struct routinely used for such matters.
729 * P.S. One more locking exception. RCU is used to guard the
730 * update of a tasks cgroup pointer by cgroup_attach_task()
734 * cgroup_lock - lock out any changes to cgroup structures
737 void cgroup_lock(void)
739 mutex_lock(&cgroup_mutex
);
741 EXPORT_SYMBOL_GPL(cgroup_lock
);
744 * cgroup_unlock - release lock on cgroup changes
746 * Undo the lock taken in a previous cgroup_lock() call.
748 void cgroup_unlock(void)
750 mutex_unlock(&cgroup_mutex
);
752 EXPORT_SYMBOL_GPL(cgroup_unlock
);
755 * A couple of forward declarations required, due to cyclic reference loop:
756 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
757 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
761 static int cgroup_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
);
762 static struct dentry
*cgroup_lookup(struct inode
*, struct dentry
*, struct nameidata
*);
763 static int cgroup_rmdir(struct inode
*unused_dir
, struct dentry
*dentry
);
764 static int cgroup_populate_dir(struct cgroup
*cgrp
);
765 static const struct inode_operations cgroup_dir_inode_operations
;
766 static const struct file_operations proc_cgroupstats_operations
;
768 static struct backing_dev_info cgroup_backing_dev_info
= {
770 .capabilities
= BDI_CAP_NO_ACCT_AND_WRITEBACK
,
773 static int alloc_css_id(struct cgroup_subsys
*ss
,
774 struct cgroup
*parent
, struct cgroup
*child
);
776 static struct inode
*cgroup_new_inode(mode_t mode
, struct super_block
*sb
)
778 struct inode
*inode
= new_inode(sb
);
781 inode
->i_ino
= get_next_ino();
782 inode
->i_mode
= mode
;
783 inode
->i_uid
= current_fsuid();
784 inode
->i_gid
= current_fsgid();
785 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
786 inode
->i_mapping
->backing_dev_info
= &cgroup_backing_dev_info
;
792 * Call subsys's pre_destroy handler.
793 * This is called before css refcnt check.
795 static int cgroup_call_pre_destroy(struct cgroup
*cgrp
)
797 struct cgroup_subsys
*ss
;
800 for_each_subsys(cgrp
->root
, ss
)
801 if (ss
->pre_destroy
) {
802 ret
= ss
->pre_destroy(ss
, cgrp
);
810 static void cgroup_diput(struct dentry
*dentry
, struct inode
*inode
)
812 /* is dentry a directory ? if so, kfree() associated cgroup */
813 if (S_ISDIR(inode
->i_mode
)) {
814 struct cgroup
*cgrp
= dentry
->d_fsdata
;
815 struct cgroup_subsys
*ss
;
816 BUG_ON(!(cgroup_is_removed(cgrp
)));
817 /* It's possible for external users to be holding css
818 * reference counts on a cgroup; css_put() needs to
819 * be able to access the cgroup after decrementing
820 * the reference count in order to know if it needs to
821 * queue the cgroup to be handled by the release
825 mutex_lock(&cgroup_mutex
);
827 * Release the subsystem state objects.
829 for_each_subsys(cgrp
->root
, ss
)
830 ss
->destroy(ss
, cgrp
);
832 cgrp
->root
->number_of_cgroups
--;
833 mutex_unlock(&cgroup_mutex
);
836 * Drop the active superblock reference that we took when we
839 deactivate_super(cgrp
->root
->sb
);
842 * if we're getting rid of the cgroup, refcount should ensure
843 * that there are no pidlists left.
845 BUG_ON(!list_empty(&cgrp
->pidlists
));
847 kfree_rcu(cgrp
, rcu_head
);
852 static int cgroup_delete(const struct dentry
*d
)
857 static void remove_dir(struct dentry
*d
)
859 struct dentry
*parent
= dget(d
->d_parent
);
862 simple_rmdir(parent
->d_inode
, d
);
866 static void cgroup_clear_directory(struct dentry
*dentry
)
868 struct list_head
*node
;
870 BUG_ON(!mutex_is_locked(&dentry
->d_inode
->i_mutex
));
871 spin_lock(&dentry
->d_lock
);
872 node
= dentry
->d_subdirs
.next
;
873 while (node
!= &dentry
->d_subdirs
) {
874 struct dentry
*d
= list_entry(node
, struct dentry
, d_u
.d_child
);
876 spin_lock_nested(&d
->d_lock
, DENTRY_D_LOCK_NESTED
);
879 /* This should never be called on a cgroup
880 * directory with child cgroups */
881 BUG_ON(d
->d_inode
->i_mode
& S_IFDIR
);
883 spin_unlock(&d
->d_lock
);
884 spin_unlock(&dentry
->d_lock
);
886 simple_unlink(dentry
->d_inode
, d
);
888 spin_lock(&dentry
->d_lock
);
890 spin_unlock(&d
->d_lock
);
891 node
= dentry
->d_subdirs
.next
;
893 spin_unlock(&dentry
->d_lock
);
897 * NOTE : the dentry must have been dget()'ed
899 static void cgroup_d_remove_dir(struct dentry
*dentry
)
901 struct dentry
*parent
;
903 cgroup_clear_directory(dentry
);
905 parent
= dentry
->d_parent
;
906 spin_lock(&parent
->d_lock
);
907 spin_lock_nested(&dentry
->d_lock
, DENTRY_D_LOCK_NESTED
);
908 list_del_init(&dentry
->d_u
.d_child
);
909 spin_unlock(&dentry
->d_lock
);
910 spin_unlock(&parent
->d_lock
);
915 * A queue for waiters to do rmdir() cgroup. A tasks will sleep when
916 * cgroup->count == 0 && list_empty(&cgroup->children) && subsys has some
917 * reference to css->refcnt. In general, this refcnt is expected to goes down
920 * CGRP_WAIT_ON_RMDIR flag is set under cgroup's inode->i_mutex;
922 DECLARE_WAIT_QUEUE_HEAD(cgroup_rmdir_waitq
);
924 static void cgroup_wakeup_rmdir_waiter(struct cgroup
*cgrp
)
926 if (unlikely(test_and_clear_bit(CGRP_WAIT_ON_RMDIR
, &cgrp
->flags
)))
927 wake_up_all(&cgroup_rmdir_waitq
);
930 void cgroup_exclude_rmdir(struct cgroup_subsys_state
*css
)
935 void cgroup_release_and_wakeup_rmdir(struct cgroup_subsys_state
*css
)
937 cgroup_wakeup_rmdir_waiter(css
->cgroup
);
942 * Call with cgroup_mutex held. Drops reference counts on modules, including
943 * any duplicate ones that parse_cgroupfs_options took. If this function
944 * returns an error, no reference counts are touched.
946 static int rebind_subsystems(struct cgroupfs_root
*root
,
947 unsigned long final_bits
)
949 unsigned long added_bits
, removed_bits
;
950 struct cgroup
*cgrp
= &root
->top_cgroup
;
953 BUG_ON(!mutex_is_locked(&cgroup_mutex
));
955 removed_bits
= root
->actual_subsys_bits
& ~final_bits
;
956 added_bits
= final_bits
& ~root
->actual_subsys_bits
;
957 /* Check that any added subsystems are currently free */
958 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
959 unsigned long bit
= 1UL << i
;
960 struct cgroup_subsys
*ss
= subsys
[i
];
961 if (!(bit
& added_bits
))
964 * Nobody should tell us to do a subsys that doesn't exist:
965 * parse_cgroupfs_options should catch that case and refcounts
966 * ensure that subsystems won't disappear once selected.
969 if (ss
->root
!= &rootnode
) {
970 /* Subsystem isn't free */
975 /* Currently we don't handle adding/removing subsystems when
976 * any child cgroups exist. This is theoretically supportable
977 * but involves complex error handling, so it's being left until
979 if (root
->number_of_cgroups
> 1)
982 /* Process each subsystem */
983 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
984 struct cgroup_subsys
*ss
= subsys
[i
];
985 unsigned long bit
= 1UL << i
;
986 if (bit
& added_bits
) {
987 /* We're binding this subsystem to this hierarchy */
989 BUG_ON(cgrp
->subsys
[i
]);
990 BUG_ON(!dummytop
->subsys
[i
]);
991 BUG_ON(dummytop
->subsys
[i
]->cgroup
!= dummytop
);
992 mutex_lock(&ss
->hierarchy_mutex
);
993 cgrp
->subsys
[i
] = dummytop
->subsys
[i
];
994 cgrp
->subsys
[i
]->cgroup
= cgrp
;
995 list_move(&ss
->sibling
, &root
->subsys_list
);
999 mutex_unlock(&ss
->hierarchy_mutex
);
1000 /* refcount was already taken, and we're keeping it */
1001 } else if (bit
& removed_bits
) {
1002 /* We're removing this subsystem */
1004 BUG_ON(cgrp
->subsys
[i
] != dummytop
->subsys
[i
]);
1005 BUG_ON(cgrp
->subsys
[i
]->cgroup
!= cgrp
);
1006 mutex_lock(&ss
->hierarchy_mutex
);
1008 ss
->bind(ss
, dummytop
);
1009 dummytop
->subsys
[i
]->cgroup
= dummytop
;
1010 cgrp
->subsys
[i
] = NULL
;
1011 subsys
[i
]->root
= &rootnode
;
1012 list_move(&ss
->sibling
, &rootnode
.subsys_list
);
1013 mutex_unlock(&ss
->hierarchy_mutex
);
1014 /* subsystem is now free - drop reference on module */
1015 module_put(ss
->module
);
1016 } else if (bit
& final_bits
) {
1017 /* Subsystem state should already exist */
1019 BUG_ON(!cgrp
->subsys
[i
]);
1021 * a refcount was taken, but we already had one, so
1022 * drop the extra reference.
1024 module_put(ss
->module
);
1025 #ifdef CONFIG_MODULE_UNLOAD
1026 BUG_ON(ss
->module
&& !module_refcount(ss
->module
));
1029 /* Subsystem state shouldn't exist */
1030 BUG_ON(cgrp
->subsys
[i
]);
1033 root
->subsys_bits
= root
->actual_subsys_bits
= final_bits
;
1039 static int cgroup_show_options(struct seq_file
*seq
, struct vfsmount
*vfs
)
1041 struct cgroupfs_root
*root
= vfs
->mnt_sb
->s_fs_info
;
1042 struct cgroup_subsys
*ss
;
1044 mutex_lock(&cgroup_mutex
);
1045 for_each_subsys(root
, ss
)
1046 seq_printf(seq
, ",%s", ss
->name
);
1047 if (test_bit(ROOT_NOPREFIX
, &root
->flags
))
1048 seq_puts(seq
, ",noprefix");
1049 if (strlen(root
->release_agent_path
))
1050 seq_printf(seq
, ",release_agent=%s", root
->release_agent_path
);
1051 if (clone_children(&root
->top_cgroup
))
1052 seq_puts(seq
, ",clone_children");
1053 if (strlen(root
->name
))
1054 seq_printf(seq
, ",name=%s", root
->name
);
1055 mutex_unlock(&cgroup_mutex
);
1059 struct cgroup_sb_opts
{
1060 unsigned long subsys_bits
;
1061 unsigned long flags
;
1062 char *release_agent
;
1063 bool clone_children
;
1065 /* User explicitly requested empty subsystem */
1068 struct cgroupfs_root
*new_root
;
1073 * Convert a hierarchy specifier into a bitmask of subsystems and flags. Call
1074 * with cgroup_mutex held to protect the subsys[] array. This function takes
1075 * refcounts on subsystems to be used, unless it returns error, in which case
1076 * no refcounts are taken.
1078 static int parse_cgroupfs_options(char *data
, struct cgroup_sb_opts
*opts
)
1080 char *token
, *o
= data
;
1081 bool all_ss
= false, one_ss
= false;
1082 unsigned long mask
= (unsigned long)-1;
1084 bool module_pin_failed
= false;
1086 BUG_ON(!mutex_is_locked(&cgroup_mutex
));
1088 #ifdef CONFIG_CPUSETS
1089 mask
= ~(1UL << cpuset_subsys_id
);
1092 memset(opts
, 0, sizeof(*opts
));
1094 while ((token
= strsep(&o
, ",")) != NULL
) {
1097 if (!strcmp(token
, "none")) {
1098 /* Explicitly have no subsystems */
1102 if (!strcmp(token
, "all")) {
1103 /* Mutually exclusive option 'all' + subsystem name */
1109 if (!strcmp(token
, "noprefix")) {
1110 set_bit(ROOT_NOPREFIX
, &opts
->flags
);
1113 if (!strcmp(token
, "clone_children")) {
1114 opts
->clone_children
= true;
1117 if (!strncmp(token
, "release_agent=", 14)) {
1118 /* Specifying two release agents is forbidden */
1119 if (opts
->release_agent
)
1121 opts
->release_agent
=
1122 kstrndup(token
+ 14, PATH_MAX
- 1, GFP_KERNEL
);
1123 if (!opts
->release_agent
)
1127 if (!strncmp(token
, "name=", 5)) {
1128 const char *name
= token
+ 5;
1129 /* Can't specify an empty name */
1132 /* Must match [\w.-]+ */
1133 for (i
= 0; i
< strlen(name
); i
++) {
1137 if ((c
== '.') || (c
== '-') || (c
== '_'))
1141 /* Specifying two names is forbidden */
1144 opts
->name
= kstrndup(name
,
1145 MAX_CGROUP_ROOT_NAMELEN
- 1,
1153 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
1154 struct cgroup_subsys
*ss
= subsys
[i
];
1157 if (strcmp(token
, ss
->name
))
1162 /* Mutually exclusive option 'all' + subsystem name */
1165 set_bit(i
, &opts
->subsys_bits
);
1170 if (i
== CGROUP_SUBSYS_COUNT
)
1175 * If the 'all' option was specified select all the subsystems,
1176 * otherwise if 'none', 'name=' and a subsystem name options
1177 * were not specified, let's default to 'all'
1179 if (all_ss
|| (!one_ss
&& !opts
->none
&& !opts
->name
)) {
1180 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
1181 struct cgroup_subsys
*ss
= subsys
[i
];
1186 set_bit(i
, &opts
->subsys_bits
);
1190 /* Consistency checks */
1193 * Option noprefix was introduced just for backward compatibility
1194 * with the old cpuset, so we allow noprefix only if mounting just
1195 * the cpuset subsystem.
1197 if (test_bit(ROOT_NOPREFIX
, &opts
->flags
) &&
1198 (opts
->subsys_bits
& mask
))
1202 /* Can't specify "none" and some subsystems */
1203 if (opts
->subsys_bits
&& opts
->none
)
1207 * We either have to specify by name or by subsystems. (So all
1208 * empty hierarchies must have a name).
1210 if (!opts
->subsys_bits
&& !opts
->name
)
1214 * Grab references on all the modules we'll need, so the subsystems
1215 * don't dance around before rebind_subsystems attaches them. This may
1216 * take duplicate reference counts on a subsystem that's already used,
1217 * but rebind_subsystems handles this case.
1219 for (i
= CGROUP_BUILTIN_SUBSYS_COUNT
; i
< CGROUP_SUBSYS_COUNT
; i
++) {
1220 unsigned long bit
= 1UL << i
;
1222 if (!(bit
& opts
->subsys_bits
))
1224 if (!try_module_get(subsys
[i
]->module
)) {
1225 module_pin_failed
= true;
1229 if (module_pin_failed
) {
1231 * oops, one of the modules was going away. this means that we
1232 * raced with a module_delete call, and to the user this is
1233 * essentially a "subsystem doesn't exist" case.
1235 for (i
--; i
>= CGROUP_BUILTIN_SUBSYS_COUNT
; i
--) {
1236 /* drop refcounts only on the ones we took */
1237 unsigned long bit
= 1UL << i
;
1239 if (!(bit
& opts
->subsys_bits
))
1241 module_put(subsys
[i
]->module
);
1249 static void drop_parsed_module_refcounts(unsigned long subsys_bits
)
1252 for (i
= CGROUP_BUILTIN_SUBSYS_COUNT
; i
< CGROUP_SUBSYS_COUNT
; i
++) {
1253 unsigned long bit
= 1UL << i
;
1255 if (!(bit
& subsys_bits
))
1257 module_put(subsys
[i
]->module
);
1261 static int cgroup_remount(struct super_block
*sb
, int *flags
, char *data
)
1264 struct cgroupfs_root
*root
= sb
->s_fs_info
;
1265 struct cgroup
*cgrp
= &root
->top_cgroup
;
1266 struct cgroup_sb_opts opts
;
1268 mutex_lock(&cgrp
->dentry
->d_inode
->i_mutex
);
1269 mutex_lock(&cgroup_mutex
);
1271 /* See what subsystems are wanted */
1272 ret
= parse_cgroupfs_options(data
, &opts
);
1276 /* Don't allow flags or name to change at remount */
1277 if (opts
.flags
!= root
->flags
||
1278 (opts
.name
&& strcmp(opts
.name
, root
->name
))) {
1280 drop_parsed_module_refcounts(opts
.subsys_bits
);
1284 ret
= rebind_subsystems(root
, opts
.subsys_bits
);
1286 drop_parsed_module_refcounts(opts
.subsys_bits
);
1290 /* (re)populate subsystem files */
1291 cgroup_populate_dir(cgrp
);
1293 if (opts
.release_agent
)
1294 strcpy(root
->release_agent_path
, opts
.release_agent
);
1296 kfree(opts
.release_agent
);
1298 mutex_unlock(&cgroup_mutex
);
1299 mutex_unlock(&cgrp
->dentry
->d_inode
->i_mutex
);
1303 static const struct super_operations cgroup_ops
= {
1304 .statfs
= simple_statfs
,
1305 .drop_inode
= generic_delete_inode
,
1306 .show_options
= cgroup_show_options
,
1307 .remount_fs
= cgroup_remount
,
1310 static void init_cgroup_housekeeping(struct cgroup
*cgrp
)
1312 INIT_LIST_HEAD(&cgrp
->sibling
);
1313 INIT_LIST_HEAD(&cgrp
->children
);
1314 INIT_LIST_HEAD(&cgrp
->css_sets
);
1315 INIT_LIST_HEAD(&cgrp
->release_list
);
1316 INIT_LIST_HEAD(&cgrp
->pidlists
);
1317 mutex_init(&cgrp
->pidlist_mutex
);
1318 INIT_LIST_HEAD(&cgrp
->event_list
);
1319 spin_lock_init(&cgrp
->event_list_lock
);
1322 static void init_cgroup_root(struct cgroupfs_root
*root
)
1324 struct cgroup
*cgrp
= &root
->top_cgroup
;
1325 INIT_LIST_HEAD(&root
->subsys_list
);
1326 INIT_LIST_HEAD(&root
->root_list
);
1327 root
->number_of_cgroups
= 1;
1329 cgrp
->top_cgroup
= cgrp
;
1330 init_cgroup_housekeeping(cgrp
);
1333 static bool init_root_id(struct cgroupfs_root
*root
)
1338 if (!ida_pre_get(&hierarchy_ida
, GFP_KERNEL
))
1340 spin_lock(&hierarchy_id_lock
);
1341 /* Try to allocate the next unused ID */
1342 ret
= ida_get_new_above(&hierarchy_ida
, next_hierarchy_id
,
1343 &root
->hierarchy_id
);
1345 /* Try again starting from 0 */
1346 ret
= ida_get_new(&hierarchy_ida
, &root
->hierarchy_id
);
1348 next_hierarchy_id
= root
->hierarchy_id
+ 1;
1349 } else if (ret
!= -EAGAIN
) {
1350 /* Can only get here if the 31-bit IDR is full ... */
1353 spin_unlock(&hierarchy_id_lock
);
1358 static int cgroup_test_super(struct super_block
*sb
, void *data
)
1360 struct cgroup_sb_opts
*opts
= data
;
1361 struct cgroupfs_root
*root
= sb
->s_fs_info
;
1363 /* If we asked for a name then it must match */
1364 if (opts
->name
&& strcmp(opts
->name
, root
->name
))
1368 * If we asked for subsystems (or explicitly for no
1369 * subsystems) then they must match
1371 if ((opts
->subsys_bits
|| opts
->none
)
1372 && (opts
->subsys_bits
!= root
->subsys_bits
))
1378 static struct cgroupfs_root
*cgroup_root_from_opts(struct cgroup_sb_opts
*opts
)
1380 struct cgroupfs_root
*root
;
1382 if (!opts
->subsys_bits
&& !opts
->none
)
1385 root
= kzalloc(sizeof(*root
), GFP_KERNEL
);
1387 return ERR_PTR(-ENOMEM
);
1389 if (!init_root_id(root
)) {
1391 return ERR_PTR(-ENOMEM
);
1393 init_cgroup_root(root
);
1395 root
->subsys_bits
= opts
->subsys_bits
;
1396 root
->flags
= opts
->flags
;
1397 if (opts
->release_agent
)
1398 strcpy(root
->release_agent_path
, opts
->release_agent
);
1400 strcpy(root
->name
, opts
->name
);
1401 if (opts
->clone_children
)
1402 set_bit(CGRP_CLONE_CHILDREN
, &root
->top_cgroup
.flags
);
1406 static void cgroup_drop_root(struct cgroupfs_root
*root
)
1411 BUG_ON(!root
->hierarchy_id
);
1412 spin_lock(&hierarchy_id_lock
);
1413 ida_remove(&hierarchy_ida
, root
->hierarchy_id
);
1414 spin_unlock(&hierarchy_id_lock
);
1418 static int cgroup_set_super(struct super_block
*sb
, void *data
)
1421 struct cgroup_sb_opts
*opts
= data
;
1423 /* If we don't have a new root, we can't set up a new sb */
1424 if (!opts
->new_root
)
1427 BUG_ON(!opts
->subsys_bits
&& !opts
->none
);
1429 ret
= set_anon_super(sb
, NULL
);
1433 sb
->s_fs_info
= opts
->new_root
;
1434 opts
->new_root
->sb
= sb
;
1436 sb
->s_blocksize
= PAGE_CACHE_SIZE
;
1437 sb
->s_blocksize_bits
= PAGE_CACHE_SHIFT
;
1438 sb
->s_magic
= CGROUP_SUPER_MAGIC
;
1439 sb
->s_op
= &cgroup_ops
;
1444 static int cgroup_get_rootdir(struct super_block
*sb
)
1446 static const struct dentry_operations cgroup_dops
= {
1447 .d_iput
= cgroup_diput
,
1448 .d_delete
= cgroup_delete
,
1451 struct inode
*inode
=
1452 cgroup_new_inode(S_IFDIR
| S_IRUGO
| S_IXUGO
| S_IWUSR
, sb
);
1453 struct dentry
*dentry
;
1458 inode
->i_fop
= &simple_dir_operations
;
1459 inode
->i_op
= &cgroup_dir_inode_operations
;
1460 /* directories start off with i_nlink == 2 (for "." entry) */
1462 dentry
= d_alloc_root(inode
);
1467 sb
->s_root
= dentry
;
1468 /* for everything else we want ->d_op set */
1469 sb
->s_d_op
= &cgroup_dops
;
1473 static struct dentry
*cgroup_mount(struct file_system_type
*fs_type
,
1474 int flags
, const char *unused_dev_name
,
1477 struct cgroup_sb_opts opts
;
1478 struct cgroupfs_root
*root
;
1480 struct super_block
*sb
;
1481 struct cgroupfs_root
*new_root
;
1483 /* First find the desired set of subsystems */
1484 mutex_lock(&cgroup_mutex
);
1485 ret
= parse_cgroupfs_options(data
, &opts
);
1486 mutex_unlock(&cgroup_mutex
);
1491 * Allocate a new cgroup root. We may not need it if we're
1492 * reusing an existing hierarchy.
1494 new_root
= cgroup_root_from_opts(&opts
);
1495 if (IS_ERR(new_root
)) {
1496 ret
= PTR_ERR(new_root
);
1499 opts
.new_root
= new_root
;
1501 /* Locate an existing or new sb for this hierarchy */
1502 sb
= sget(fs_type
, cgroup_test_super
, cgroup_set_super
, &opts
);
1505 cgroup_drop_root(opts
.new_root
);
1509 root
= sb
->s_fs_info
;
1511 if (root
== opts
.new_root
) {
1512 /* We used the new root structure, so this is a new hierarchy */
1513 struct list_head tmp_cg_links
;
1514 struct cgroup
*root_cgrp
= &root
->top_cgroup
;
1515 struct inode
*inode
;
1516 struct cgroupfs_root
*existing_root
;
1519 BUG_ON(sb
->s_root
!= NULL
);
1521 ret
= cgroup_get_rootdir(sb
);
1523 goto drop_new_super
;
1524 inode
= sb
->s_root
->d_inode
;
1526 mutex_lock(&inode
->i_mutex
);
1527 mutex_lock(&cgroup_mutex
);
1529 if (strlen(root
->name
)) {
1530 /* Check for name clashes with existing mounts */
1531 for_each_active_root(existing_root
) {
1532 if (!strcmp(existing_root
->name
, root
->name
)) {
1534 mutex_unlock(&cgroup_mutex
);
1535 mutex_unlock(&inode
->i_mutex
);
1536 goto drop_new_super
;
1542 * We're accessing css_set_count without locking
1543 * css_set_lock here, but that's OK - it can only be
1544 * increased by someone holding cgroup_lock, and
1545 * that's us. The worst that can happen is that we
1546 * have some link structures left over
1548 ret
= allocate_cg_links(css_set_count
, &tmp_cg_links
);
1550 mutex_unlock(&cgroup_mutex
);
1551 mutex_unlock(&inode
->i_mutex
);
1552 goto drop_new_super
;
1555 ret
= rebind_subsystems(root
, root
->subsys_bits
);
1556 if (ret
== -EBUSY
) {
1557 mutex_unlock(&cgroup_mutex
);
1558 mutex_unlock(&inode
->i_mutex
);
1559 free_cg_links(&tmp_cg_links
);
1560 goto drop_new_super
;
1563 * There must be no failure case after here, since rebinding
1564 * takes care of subsystems' refcounts, which are explicitly
1565 * dropped in the failure exit path.
1568 /* EBUSY should be the only error here */
1571 list_add(&root
->root_list
, &roots
);
1574 sb
->s_root
->d_fsdata
= root_cgrp
;
1575 root
->top_cgroup
.dentry
= sb
->s_root
;
1577 /* Link the top cgroup in this hierarchy into all
1578 * the css_set objects */
1579 write_lock(&css_set_lock
);
1580 for (i
= 0; i
< CSS_SET_TABLE_SIZE
; i
++) {
1581 struct hlist_head
*hhead
= &css_set_table
[i
];
1582 struct hlist_node
*node
;
1585 hlist_for_each_entry(cg
, node
, hhead
, hlist
)
1586 link_css_set(&tmp_cg_links
, cg
, root_cgrp
);
1588 write_unlock(&css_set_lock
);
1590 free_cg_links(&tmp_cg_links
);
1592 BUG_ON(!list_empty(&root_cgrp
->sibling
));
1593 BUG_ON(!list_empty(&root_cgrp
->children
));
1594 BUG_ON(root
->number_of_cgroups
!= 1);
1596 cgroup_populate_dir(root_cgrp
);
1597 mutex_unlock(&cgroup_mutex
);
1598 mutex_unlock(&inode
->i_mutex
);
1601 * We re-used an existing hierarchy - the new root (if
1602 * any) is not needed
1604 cgroup_drop_root(opts
.new_root
);
1605 /* no subsys rebinding, so refcounts don't change */
1606 drop_parsed_module_refcounts(opts
.subsys_bits
);
1609 kfree(opts
.release_agent
);
1611 return dget(sb
->s_root
);
1614 deactivate_locked_super(sb
);
1616 drop_parsed_module_refcounts(opts
.subsys_bits
);
1618 kfree(opts
.release_agent
);
1620 return ERR_PTR(ret
);
1623 static void cgroup_kill_sb(struct super_block
*sb
) {
1624 struct cgroupfs_root
*root
= sb
->s_fs_info
;
1625 struct cgroup
*cgrp
= &root
->top_cgroup
;
1627 struct cg_cgroup_link
*link
;
1628 struct cg_cgroup_link
*saved_link
;
1632 BUG_ON(root
->number_of_cgroups
!= 1);
1633 BUG_ON(!list_empty(&cgrp
->children
));
1634 BUG_ON(!list_empty(&cgrp
->sibling
));
1636 mutex_lock(&cgroup_mutex
);
1638 /* Rebind all subsystems back to the default hierarchy */
1639 ret
= rebind_subsystems(root
, 0);
1640 /* Shouldn't be able to fail ... */
1644 * Release all the links from css_sets to this hierarchy's
1647 write_lock(&css_set_lock
);
1649 list_for_each_entry_safe(link
, saved_link
, &cgrp
->css_sets
,
1651 list_del(&link
->cg_link_list
);
1652 list_del(&link
->cgrp_link_list
);
1655 write_unlock(&css_set_lock
);
1657 if (!list_empty(&root
->root_list
)) {
1658 list_del(&root
->root_list
);
1662 mutex_unlock(&cgroup_mutex
);
1664 kill_litter_super(sb
);
1665 cgroup_drop_root(root
);
1668 static struct file_system_type cgroup_fs_type
= {
1670 .mount
= cgroup_mount
,
1671 .kill_sb
= cgroup_kill_sb
,
1674 static struct kobject
*cgroup_kobj
;
1676 static inline struct cgroup
*__d_cgrp(struct dentry
*dentry
)
1678 return dentry
->d_fsdata
;
1681 static inline struct cftype
*__d_cft(struct dentry
*dentry
)
1683 return dentry
->d_fsdata
;
1687 * cgroup_path - generate the path of a cgroup
1688 * @cgrp: the cgroup in question
1689 * @buf: the buffer to write the path into
1690 * @buflen: the length of the buffer
1692 * Called with cgroup_mutex held or else with an RCU-protected cgroup
1693 * reference. Writes path of cgroup into buf. Returns 0 on success,
1696 int cgroup_path(const struct cgroup
*cgrp
, char *buf
, int buflen
)
1699 struct dentry
*dentry
= rcu_dereference_check(cgrp
->dentry
,
1700 rcu_read_lock_held() ||
1701 cgroup_lock_is_held());
1703 if (!dentry
|| cgrp
== dummytop
) {
1705 * Inactive subsystems have no dentry for their root
1712 start
= buf
+ buflen
;
1716 int len
= dentry
->d_name
.len
;
1718 if ((start
-= len
) < buf
)
1719 return -ENAMETOOLONG
;
1720 memcpy(start
, dentry
->d_name
.name
, len
);
1721 cgrp
= cgrp
->parent
;
1725 dentry
= rcu_dereference_check(cgrp
->dentry
,
1726 rcu_read_lock_held() ||
1727 cgroup_lock_is_held());
1731 return -ENAMETOOLONG
;
1734 memmove(buf
, start
, buf
+ buflen
- start
);
1737 EXPORT_SYMBOL_GPL(cgroup_path
);
1740 * cgroup_task_migrate - move a task from one cgroup to another.
1742 * 'guarantee' is set if the caller promises that a new css_set for the task
1743 * will already exist. If not set, this function might sleep, and can fail with
1744 * -ENOMEM. Otherwise, it can only fail with -ESRCH.
1746 static int cgroup_task_migrate(struct cgroup
*cgrp
, struct cgroup
*oldcgrp
,
1747 struct task_struct
*tsk
, bool guarantee
)
1749 struct css_set
*oldcg
;
1750 struct css_set
*newcg
;
1753 * get old css_set. we need to take task_lock and refcount it, because
1754 * an exiting task can change its css_set to init_css_set and drop its
1755 * old one without taking cgroup_mutex.
1758 oldcg
= tsk
->cgroups
;
1762 /* locate or allocate a new css_set for this task. */
1764 /* we know the css_set we want already exists. */
1765 struct cgroup_subsys_state
*template[CGROUP_SUBSYS_COUNT
];
1766 read_lock(&css_set_lock
);
1767 newcg
= find_existing_css_set(oldcg
, cgrp
, template);
1770 read_unlock(&css_set_lock
);
1773 /* find_css_set will give us newcg already referenced. */
1774 newcg
= find_css_set(oldcg
, cgrp
);
1782 /* if PF_EXITING is set, the tsk->cgroups pointer is no longer safe. */
1784 if (tsk
->flags
& PF_EXITING
) {
1789 rcu_assign_pointer(tsk
->cgroups
, newcg
);
1792 /* Update the css_set linked lists if we're using them */
1793 write_lock(&css_set_lock
);
1794 if (!list_empty(&tsk
->cg_list
))
1795 list_move(&tsk
->cg_list
, &newcg
->tasks
);
1796 write_unlock(&css_set_lock
);
1799 * We just gained a reference on oldcg by taking it from the task. As
1800 * trading it for newcg is protected by cgroup_mutex, we're safe to drop
1801 * it here; it will be freed under RCU.
1803 set_bit(CGRP_RELEASABLE
, &oldcgrp
->flags
);
1809 * cgroup_attach_task - attach task 'tsk' to cgroup 'cgrp'
1810 * @cgrp: the cgroup the task is attaching to
1811 * @tsk: the task to be attached
1813 * Call holding cgroup_mutex. May take task_lock of
1814 * the task 'tsk' during call.
1816 int cgroup_attach_task(struct cgroup
*cgrp
, struct task_struct
*tsk
)
1819 struct cgroup_subsys
*ss
, *failed_ss
= NULL
;
1820 struct cgroup
*oldcgrp
;
1821 struct cgroupfs_root
*root
= cgrp
->root
;
1823 /* Nothing to do if the task is already in that cgroup */
1824 oldcgrp
= task_cgroup_from_root(tsk
, root
);
1825 if (cgrp
== oldcgrp
)
1828 for_each_subsys(root
, ss
) {
1829 if (ss
->can_attach
) {
1830 retval
= ss
->can_attach(ss
, cgrp
, tsk
);
1833 * Remember on which subsystem the can_attach()
1834 * failed, so that we only call cancel_attach()
1835 * against the subsystems whose can_attach()
1836 * succeeded. (See below)
1842 if (ss
->can_attach_task
) {
1843 retval
= ss
->can_attach_task(cgrp
, tsk
);
1851 retval
= cgroup_task_migrate(cgrp
, oldcgrp
, tsk
, false);
1855 for_each_subsys(root
, ss
) {
1857 ss
->pre_attach(cgrp
);
1858 if (ss
->attach_task
)
1859 ss
->attach_task(cgrp
, tsk
);
1861 ss
->attach(ss
, cgrp
, oldcgrp
, tsk
);
1867 * wake up rmdir() waiter. the rmdir should fail since the cgroup
1868 * is no longer empty.
1870 cgroup_wakeup_rmdir_waiter(cgrp
);
1873 for_each_subsys(root
, ss
) {
1874 if (ss
== failed_ss
)
1876 * This subsystem was the one that failed the
1877 * can_attach() check earlier, so we don't need
1878 * to call cancel_attach() against it or any
1879 * remaining subsystems.
1882 if (ss
->cancel_attach
)
1883 ss
->cancel_attach(ss
, cgrp
, tsk
);
1890 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
1891 * @from: attach to all cgroups of a given task
1892 * @tsk: the task to be attached
1894 int cgroup_attach_task_all(struct task_struct
*from
, struct task_struct
*tsk
)
1896 struct cgroupfs_root
*root
;
1900 for_each_active_root(root
) {
1901 struct cgroup
*from_cg
= task_cgroup_from_root(from
, root
);
1903 retval
= cgroup_attach_task(from_cg
, tsk
);
1911 EXPORT_SYMBOL_GPL(cgroup_attach_task_all
);
1914 * cgroup_attach_proc works in two stages, the first of which prefetches all
1915 * new css_sets needed (to make sure we have enough memory before committing
1916 * to the move) and stores them in a list of entries of the following type.
1917 * TODO: possible optimization: use css_set->rcu_head for chaining instead
1919 struct cg_list_entry
{
1921 struct list_head links
;
1924 static bool css_set_check_fetched(struct cgroup
*cgrp
,
1925 struct task_struct
*tsk
, struct css_set
*cg
,
1926 struct list_head
*newcg_list
)
1928 struct css_set
*newcg
;
1929 struct cg_list_entry
*cg_entry
;
1930 struct cgroup_subsys_state
*template[CGROUP_SUBSYS_COUNT
];
1932 read_lock(&css_set_lock
);
1933 newcg
= find_existing_css_set(cg
, cgrp
, template);
1936 read_unlock(&css_set_lock
);
1938 /* doesn't exist at all? */
1941 /* see if it's already in the list */
1942 list_for_each_entry(cg_entry
, newcg_list
, links
) {
1943 if (cg_entry
->cg
== newcg
) {
1955 * Find the new css_set and store it in the list in preparation for moving the
1956 * given task to the given cgroup. Returns 0 or -ENOMEM.
1958 static int css_set_prefetch(struct cgroup
*cgrp
, struct css_set
*cg
,
1959 struct list_head
*newcg_list
)
1961 struct css_set
*newcg
;
1962 struct cg_list_entry
*cg_entry
;
1964 /* ensure a new css_set will exist for this thread */
1965 newcg
= find_css_set(cg
, cgrp
);
1968 /* add it to the list */
1969 cg_entry
= kmalloc(sizeof(struct cg_list_entry
), GFP_KERNEL
);
1974 cg_entry
->cg
= newcg
;
1975 list_add(&cg_entry
->links
, newcg_list
);
1980 * cgroup_attach_proc - attach all threads in a threadgroup to a cgroup
1981 * @cgrp: the cgroup to attach to
1982 * @leader: the threadgroup leader task_struct of the group to be attached
1984 * Call holding cgroup_mutex and the threadgroup_fork_lock of the leader. Will
1985 * take task_lock of each thread in leader's threadgroup individually in turn.
1987 int cgroup_attach_proc(struct cgroup
*cgrp
, struct task_struct
*leader
)
1989 int retval
, i
, group_size
;
1990 struct cgroup_subsys
*ss
, *failed_ss
= NULL
;
1991 bool cancel_failed_ss
= false;
1992 /* guaranteed to be initialized later, but the compiler needs this */
1993 struct cgroup
*oldcgrp
= NULL
;
1994 struct css_set
*oldcg
;
1995 struct cgroupfs_root
*root
= cgrp
->root
;
1996 /* threadgroup list cursor and array */
1997 struct task_struct
*tsk
;
1998 struct flex_array
*group
;
2000 * we need to make sure we have css_sets for all the tasks we're
2001 * going to move -before- we actually start moving them, so that in
2002 * case we get an ENOMEM we can bail out before making any changes.
2004 struct list_head newcg_list
;
2005 struct cg_list_entry
*cg_entry
, *temp_nobe
;
2008 * step 0: in order to do expensive, possibly blocking operations for
2009 * every thread, we cannot iterate the thread group list, since it needs
2010 * rcu or tasklist locked. instead, build an array of all threads in the
2011 * group - threadgroup_fork_lock prevents new threads from appearing,
2012 * and if threads exit, this will just be an over-estimate.
2014 group_size
= get_nr_threads(leader
);
2015 /* flex_array supports very large thread-groups better than kmalloc. */
2016 group
= flex_array_alloc(sizeof(struct task_struct
*), group_size
,
2020 /* pre-allocate to guarantee space while iterating in rcu read-side. */
2021 retval
= flex_array_prealloc(group
, 0, group_size
- 1, GFP_KERNEL
);
2023 goto out_free_group_list
;
2025 /* prevent changes to the threadgroup list while we take a snapshot. */
2027 if (!thread_group_leader(leader
)) {
2029 * a race with de_thread from another thread's exec() may strip
2030 * us of our leadership, making while_each_thread unsafe to use
2031 * on this task. if this happens, there is no choice but to
2032 * throw this task away and try again (from cgroup_procs_write);
2033 * this is "double-double-toil-and-trouble-check locking".
2037 goto out_free_group_list
;
2039 /* take a reference on each task in the group to go in the array. */
2043 /* as per above, nr_threads may decrease, but not increase. */
2044 BUG_ON(i
>= group_size
);
2045 get_task_struct(tsk
);
2047 * saying GFP_ATOMIC has no effect here because we did prealloc
2048 * earlier, but it's good form to communicate our expectations.
2050 retval
= flex_array_put_ptr(group
, i
, tsk
, GFP_ATOMIC
);
2051 BUG_ON(retval
!= 0);
2053 } while_each_thread(leader
, tsk
);
2054 /* remember the number of threads in the array for later. */
2059 * step 1: check that we can legitimately attach to the cgroup.
2061 for_each_subsys(root
, ss
) {
2062 if (ss
->can_attach
) {
2063 retval
= ss
->can_attach(ss
, cgrp
, leader
);
2066 goto out_cancel_attach
;
2069 /* a callback to be run on every thread in the threadgroup. */
2070 if (ss
->can_attach_task
) {
2071 /* run on each task in the threadgroup. */
2072 for (i
= 0; i
< group_size
; i
++) {
2073 tsk
= flex_array_get_ptr(group
, i
);
2074 retval
= ss
->can_attach_task(cgrp
, tsk
);
2077 cancel_failed_ss
= true;
2078 goto out_cancel_attach
;
2085 * step 2: make sure css_sets exist for all threads to be migrated.
2086 * we use find_css_set, which allocates a new one if necessary.
2088 INIT_LIST_HEAD(&newcg_list
);
2089 for (i
= 0; i
< group_size
; i
++) {
2090 tsk
= flex_array_get_ptr(group
, i
);
2091 /* nothing to do if this task is already in the cgroup */
2092 oldcgrp
= task_cgroup_from_root(tsk
, root
);
2093 if (cgrp
== oldcgrp
)
2095 /* get old css_set pointer */
2097 oldcg
= tsk
->cgroups
;
2100 /* see if the new one for us is already in the list? */
2101 if (css_set_check_fetched(cgrp
, tsk
, oldcg
, &newcg_list
)) {
2102 /* was already there, nothing to do. */
2105 /* we don't already have it. get new one. */
2106 retval
= css_set_prefetch(cgrp
, oldcg
, &newcg_list
);
2109 goto out_list_teardown
;
2114 * step 3: now that we're guaranteed success wrt the css_sets, proceed
2115 * to move all tasks to the new cgroup, calling ss->attach_task for each
2116 * one along the way. there are no failure cases after here, so this is
2119 for_each_subsys(root
, ss
) {
2121 ss
->pre_attach(cgrp
);
2123 for (i
= 0; i
< group_size
; i
++) {
2124 tsk
= flex_array_get_ptr(group
, i
);
2125 /* leave current thread as it is if it's already there */
2126 oldcgrp
= task_cgroup_from_root(tsk
, root
);
2127 if (cgrp
== oldcgrp
)
2129 /* attach each task to each subsystem */
2130 for_each_subsys(root
, ss
) {
2131 if (ss
->attach_task
)
2132 ss
->attach_task(cgrp
, tsk
);
2134 /* if the thread is PF_EXITING, it can just get skipped. */
2135 retval
= cgroup_task_migrate(cgrp
, oldcgrp
, tsk
, true);
2136 BUG_ON(retval
!= 0 && retval
!= -ESRCH
);
2138 /* nothing is sensitive to fork() after this point. */
2141 * step 4: do expensive, non-thread-specific subsystem callbacks.
2142 * TODO: if ever a subsystem needs to know the oldcgrp for each task
2143 * being moved, this call will need to be reworked to communicate that.
2145 for_each_subsys(root
, ss
) {
2147 ss
->attach(ss
, cgrp
, oldcgrp
, leader
);
2151 * step 5: success! and cleanup
2154 cgroup_wakeup_rmdir_waiter(cgrp
);
2157 /* clean up the list of prefetched css_sets. */
2158 list_for_each_entry_safe(cg_entry
, temp_nobe
, &newcg_list
, links
) {
2159 list_del(&cg_entry
->links
);
2160 put_css_set(cg_entry
->cg
);
2164 /* same deal as in cgroup_attach_task */
2166 for_each_subsys(root
, ss
) {
2167 if (ss
== failed_ss
) {
2168 if (cancel_failed_ss
&& ss
->cancel_attach
)
2169 ss
->cancel_attach(ss
, cgrp
, leader
);
2172 if (ss
->cancel_attach
)
2173 ss
->cancel_attach(ss
, cgrp
, leader
);
2176 /* clean up the array of referenced threads in the group. */
2177 for (i
= 0; i
< group_size
; i
++) {
2178 tsk
= flex_array_get_ptr(group
, i
);
2179 put_task_struct(tsk
);
2181 out_free_group_list
:
2182 flex_array_free(group
);
2187 * Find the task_struct of the task to attach by vpid and pass it along to the
2188 * function to attach either it or all tasks in its threadgroup. Will take
2189 * cgroup_mutex; may take task_lock of task.
2191 static int attach_task_by_pid(struct cgroup
*cgrp
, u64 pid
, bool threadgroup
)
2193 struct task_struct
*tsk
;
2194 const struct cred
*cred
= current_cred(), *tcred
;
2197 if (!cgroup_lock_live_group(cgrp
))
2202 tsk
= find_task_by_vpid(pid
);
2210 * RCU protects this access, since tsk was found in the
2211 * tid map. a race with de_thread may cause group_leader
2212 * to stop being the leader, but cgroup_attach_proc will
2215 tsk
= tsk
->group_leader
;
2216 } else if (tsk
->flags
& PF_EXITING
) {
2217 /* optimization for the single-task-only case */
2224 * even if we're attaching all tasks in the thread group, we
2225 * only need to check permissions on one of them.
2227 tcred
= __task_cred(tsk
);
2229 cred
->euid
!= tcred
->uid
&&
2230 cred
->euid
!= tcred
->suid
) {
2235 get_task_struct(tsk
);
2239 tsk
= current
->group_leader
;
2242 get_task_struct(tsk
);
2246 threadgroup_fork_write_lock(tsk
);
2247 ret
= cgroup_attach_proc(cgrp
, tsk
);
2248 threadgroup_fork_write_unlock(tsk
);
2250 ret
= cgroup_attach_task(cgrp
, tsk
);
2252 put_task_struct(tsk
);
2257 static int cgroup_tasks_write(struct cgroup
*cgrp
, struct cftype
*cft
, u64 pid
)
2259 return attach_task_by_pid(cgrp
, pid
, false);
2262 static int cgroup_procs_write(struct cgroup
*cgrp
, struct cftype
*cft
, u64 tgid
)
2267 * attach_proc fails with -EAGAIN if threadgroup leadership
2268 * changes in the middle of the operation, in which case we need
2269 * to find the task_struct for the new leader and start over.
2271 ret
= attach_task_by_pid(cgrp
, tgid
, true);
2272 } while (ret
== -EAGAIN
);
2277 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
2278 * @cgrp: the cgroup to be checked for liveness
2280 * On success, returns true; the lock should be later released with
2281 * cgroup_unlock(). On failure returns false with no lock held.
2283 bool cgroup_lock_live_group(struct cgroup
*cgrp
)
2285 mutex_lock(&cgroup_mutex
);
2286 if (cgroup_is_removed(cgrp
)) {
2287 mutex_unlock(&cgroup_mutex
);
2292 EXPORT_SYMBOL_GPL(cgroup_lock_live_group
);
2294 static int cgroup_release_agent_write(struct cgroup
*cgrp
, struct cftype
*cft
,
2297 BUILD_BUG_ON(sizeof(cgrp
->root
->release_agent_path
) < PATH_MAX
);
2298 if (strlen(buffer
) >= PATH_MAX
)
2300 if (!cgroup_lock_live_group(cgrp
))
2302 strcpy(cgrp
->root
->release_agent_path
, buffer
);
2307 static int cgroup_release_agent_show(struct cgroup
*cgrp
, struct cftype
*cft
,
2308 struct seq_file
*seq
)
2310 if (!cgroup_lock_live_group(cgrp
))
2312 seq_puts(seq
, cgrp
->root
->release_agent_path
);
2313 seq_putc(seq
, '\n');
2318 /* A buffer size big enough for numbers or short strings */
2319 #define CGROUP_LOCAL_BUFFER_SIZE 64
2321 static ssize_t
cgroup_write_X64(struct cgroup
*cgrp
, struct cftype
*cft
,
2323 const char __user
*userbuf
,
2324 size_t nbytes
, loff_t
*unused_ppos
)
2326 char buffer
[CGROUP_LOCAL_BUFFER_SIZE
];
2332 if (nbytes
>= sizeof(buffer
))
2334 if (copy_from_user(buffer
, userbuf
, nbytes
))
2337 buffer
[nbytes
] = 0; /* nul-terminate */
2338 if (cft
->write_u64
) {
2339 u64 val
= simple_strtoull(strstrip(buffer
), &end
, 0);
2342 retval
= cft
->write_u64(cgrp
, cft
, val
);
2344 s64 val
= simple_strtoll(strstrip(buffer
), &end
, 0);
2347 retval
= cft
->write_s64(cgrp
, cft
, val
);
2354 static ssize_t
cgroup_write_string(struct cgroup
*cgrp
, struct cftype
*cft
,
2356 const char __user
*userbuf
,
2357 size_t nbytes
, loff_t
*unused_ppos
)
2359 char local_buffer
[CGROUP_LOCAL_BUFFER_SIZE
];
2361 size_t max_bytes
= cft
->max_write_len
;
2362 char *buffer
= local_buffer
;
2365 max_bytes
= sizeof(local_buffer
) - 1;
2366 if (nbytes
>= max_bytes
)
2368 /* Allocate a dynamic buffer if we need one */
2369 if (nbytes
>= sizeof(local_buffer
)) {
2370 buffer
= kmalloc(nbytes
+ 1, GFP_KERNEL
);
2374 if (nbytes
&& copy_from_user(buffer
, userbuf
, nbytes
)) {
2379 buffer
[nbytes
] = 0; /* nul-terminate */
2380 retval
= cft
->write_string(cgrp
, cft
, strstrip(buffer
));
2384 if (buffer
!= local_buffer
)
2389 static ssize_t
cgroup_file_write(struct file
*file
, const char __user
*buf
,
2390 size_t nbytes
, loff_t
*ppos
)
2392 struct cftype
*cft
= __d_cft(file
->f_dentry
);
2393 struct cgroup
*cgrp
= __d_cgrp(file
->f_dentry
->d_parent
);
2395 if (cgroup_is_removed(cgrp
))
2398 return cft
->write(cgrp
, cft
, file
, buf
, nbytes
, ppos
);
2399 if (cft
->write_u64
|| cft
->write_s64
)
2400 return cgroup_write_X64(cgrp
, cft
, file
, buf
, nbytes
, ppos
);
2401 if (cft
->write_string
)
2402 return cgroup_write_string(cgrp
, cft
, file
, buf
, nbytes
, ppos
);
2404 int ret
= cft
->trigger(cgrp
, (unsigned int)cft
->private);
2405 return ret
? ret
: nbytes
;
2410 static ssize_t
cgroup_read_u64(struct cgroup
*cgrp
, struct cftype
*cft
,
2412 char __user
*buf
, size_t nbytes
,
2415 char tmp
[CGROUP_LOCAL_BUFFER_SIZE
];
2416 u64 val
= cft
->read_u64(cgrp
, cft
);
2417 int len
= sprintf(tmp
, "%llu\n", (unsigned long long) val
);
2419 return simple_read_from_buffer(buf
, nbytes
, ppos
, tmp
, len
);
2422 static ssize_t
cgroup_read_s64(struct cgroup
*cgrp
, struct cftype
*cft
,
2424 char __user
*buf
, size_t nbytes
,
2427 char tmp
[CGROUP_LOCAL_BUFFER_SIZE
];
2428 s64 val
= cft
->read_s64(cgrp
, cft
);
2429 int len
= sprintf(tmp
, "%lld\n", (long long) val
);
2431 return simple_read_from_buffer(buf
, nbytes
, ppos
, tmp
, len
);
2434 static ssize_t
cgroup_file_read(struct file
*file
, char __user
*buf
,
2435 size_t nbytes
, loff_t
*ppos
)
2437 struct cftype
*cft
= __d_cft(file
->f_dentry
);
2438 struct cgroup
*cgrp
= __d_cgrp(file
->f_dentry
->d_parent
);
2440 if (cgroup_is_removed(cgrp
))
2444 return cft
->read(cgrp
, cft
, file
, buf
, nbytes
, ppos
);
2446 return cgroup_read_u64(cgrp
, cft
, file
, buf
, nbytes
, ppos
);
2448 return cgroup_read_s64(cgrp
, cft
, file
, buf
, nbytes
, ppos
);
2453 * seqfile ops/methods for returning structured data. Currently just
2454 * supports string->u64 maps, but can be extended in future.
2457 struct cgroup_seqfile_state
{
2459 struct cgroup
*cgroup
;
2462 static int cgroup_map_add(struct cgroup_map_cb
*cb
, const char *key
, u64 value
)
2464 struct seq_file
*sf
= cb
->state
;
2465 return seq_printf(sf
, "%s %llu\n", key
, (unsigned long long)value
);
2468 static int cgroup_seqfile_show(struct seq_file
*m
, void *arg
)
2470 struct cgroup_seqfile_state
*state
= m
->private;
2471 struct cftype
*cft
= state
->cft
;
2472 if (cft
->read_map
) {
2473 struct cgroup_map_cb cb
= {
2474 .fill
= cgroup_map_add
,
2477 return cft
->read_map(state
->cgroup
, cft
, &cb
);
2479 return cft
->read_seq_string(state
->cgroup
, cft
, m
);
2482 static int cgroup_seqfile_release(struct inode
*inode
, struct file
*file
)
2484 struct seq_file
*seq
= file
->private_data
;
2485 kfree(seq
->private);
2486 return single_release(inode
, file
);
2489 static const struct file_operations cgroup_seqfile_operations
= {
2491 .write
= cgroup_file_write
,
2492 .llseek
= seq_lseek
,
2493 .release
= cgroup_seqfile_release
,
2496 static int cgroup_file_open(struct inode
*inode
, struct file
*file
)
2501 err
= generic_file_open(inode
, file
);
2504 cft
= __d_cft(file
->f_dentry
);
2506 if (cft
->read_map
|| cft
->read_seq_string
) {
2507 struct cgroup_seqfile_state
*state
=
2508 kzalloc(sizeof(*state
), GFP_USER
);
2512 state
->cgroup
= __d_cgrp(file
->f_dentry
->d_parent
);
2513 file
->f_op
= &cgroup_seqfile_operations
;
2514 err
= single_open(file
, cgroup_seqfile_show
, state
);
2517 } else if (cft
->open
)
2518 err
= cft
->open(inode
, file
);
2525 static int cgroup_file_release(struct inode
*inode
, struct file
*file
)
2527 struct cftype
*cft
= __d_cft(file
->f_dentry
);
2529 return cft
->release(inode
, file
);
2534 * cgroup_rename - Only allow simple rename of directories in place.
2536 static int cgroup_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
2537 struct inode
*new_dir
, struct dentry
*new_dentry
)
2539 if (!S_ISDIR(old_dentry
->d_inode
->i_mode
))
2541 if (new_dentry
->d_inode
)
2543 if (old_dir
!= new_dir
)
2545 return simple_rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
2548 static const struct file_operations cgroup_file_operations
= {
2549 .read
= cgroup_file_read
,
2550 .write
= cgroup_file_write
,
2551 .llseek
= generic_file_llseek
,
2552 .open
= cgroup_file_open
,
2553 .release
= cgroup_file_release
,
2556 static const struct inode_operations cgroup_dir_inode_operations
= {
2557 .lookup
= cgroup_lookup
,
2558 .mkdir
= cgroup_mkdir
,
2559 .rmdir
= cgroup_rmdir
,
2560 .rename
= cgroup_rename
,
2563 static struct dentry
*cgroup_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
)
2565 if (dentry
->d_name
.len
> NAME_MAX
)
2566 return ERR_PTR(-ENAMETOOLONG
);
2567 d_add(dentry
, NULL
);
2572 * Check if a file is a control file
2574 static inline struct cftype
*__file_cft(struct file
*file
)
2576 if (file
->f_dentry
->d_inode
->i_fop
!= &cgroup_file_operations
)
2577 return ERR_PTR(-EINVAL
);
2578 return __d_cft(file
->f_dentry
);
2581 static int cgroup_create_file(struct dentry
*dentry
, mode_t mode
,
2582 struct super_block
*sb
)
2584 struct inode
*inode
;
2588 if (dentry
->d_inode
)
2591 inode
= cgroup_new_inode(mode
, sb
);
2595 if (S_ISDIR(mode
)) {
2596 inode
->i_op
= &cgroup_dir_inode_operations
;
2597 inode
->i_fop
= &simple_dir_operations
;
2599 /* start off with i_nlink == 2 (for "." entry) */
2602 /* start with the directory inode held, so that we can
2603 * populate it without racing with another mkdir */
2604 mutex_lock_nested(&inode
->i_mutex
, I_MUTEX_CHILD
);
2605 } else if (S_ISREG(mode
)) {
2607 inode
->i_fop
= &cgroup_file_operations
;
2609 d_instantiate(dentry
, inode
);
2610 dget(dentry
); /* Extra count - pin the dentry in core */
2615 * cgroup_create_dir - create a directory for an object.
2616 * @cgrp: the cgroup we create the directory for. It must have a valid
2617 * ->parent field. And we are going to fill its ->dentry field.
2618 * @dentry: dentry of the new cgroup
2619 * @mode: mode to set on new directory.
2621 static int cgroup_create_dir(struct cgroup
*cgrp
, struct dentry
*dentry
,
2624 struct dentry
*parent
;
2627 parent
= cgrp
->parent
->dentry
;
2628 error
= cgroup_create_file(dentry
, S_IFDIR
| mode
, cgrp
->root
->sb
);
2630 dentry
->d_fsdata
= cgrp
;
2631 inc_nlink(parent
->d_inode
);
2632 rcu_assign_pointer(cgrp
->dentry
, dentry
);
2639 * cgroup_file_mode - deduce file mode of a control file
2640 * @cft: the control file in question
2642 * returns cft->mode if ->mode is not 0
2643 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2644 * returns S_IRUGO if it has only a read handler
2645 * returns S_IWUSR if it has only a write hander
2647 static mode_t
cgroup_file_mode(const struct cftype
*cft
)
2654 if (cft
->read
|| cft
->read_u64
|| cft
->read_s64
||
2655 cft
->read_map
|| cft
->read_seq_string
)
2658 if (cft
->write
|| cft
->write_u64
|| cft
->write_s64
||
2659 cft
->write_string
|| cft
->trigger
)
2665 int cgroup_add_file(struct cgroup
*cgrp
,
2666 struct cgroup_subsys
*subsys
,
2667 const struct cftype
*cft
)
2669 struct dentry
*dir
= cgrp
->dentry
;
2670 struct dentry
*dentry
;
2674 char name
[MAX_CGROUP_TYPE_NAMELEN
+ MAX_CFTYPE_NAME
+ 2] = { 0 };
2675 if (subsys
&& !test_bit(ROOT_NOPREFIX
, &cgrp
->root
->flags
)) {
2676 strcpy(name
, subsys
->name
);
2679 strcat(name
, cft
->name
);
2680 BUG_ON(!mutex_is_locked(&dir
->d_inode
->i_mutex
));
2681 dentry
= lookup_one_len(name
, dir
, strlen(name
));
2682 if (!IS_ERR(dentry
)) {
2683 mode
= cgroup_file_mode(cft
);
2684 error
= cgroup_create_file(dentry
, mode
| S_IFREG
,
2687 dentry
->d_fsdata
= (void *)cft
;
2690 error
= PTR_ERR(dentry
);
2693 EXPORT_SYMBOL_GPL(cgroup_add_file
);
2695 int cgroup_add_files(struct cgroup
*cgrp
,
2696 struct cgroup_subsys
*subsys
,
2697 const struct cftype cft
[],
2701 for (i
= 0; i
< count
; i
++) {
2702 err
= cgroup_add_file(cgrp
, subsys
, &cft
[i
]);
2708 EXPORT_SYMBOL_GPL(cgroup_add_files
);
2711 * cgroup_task_count - count the number of tasks in a cgroup.
2712 * @cgrp: the cgroup in question
2714 * Return the number of tasks in the cgroup.
2716 int cgroup_task_count(const struct cgroup
*cgrp
)
2719 struct cg_cgroup_link
*link
;
2721 read_lock(&css_set_lock
);
2722 list_for_each_entry(link
, &cgrp
->css_sets
, cgrp_link_list
) {
2723 count
+= atomic_read(&link
->cg
->refcount
);
2725 read_unlock(&css_set_lock
);
2730 * Advance a list_head iterator. The iterator should be positioned at
2731 * the start of a css_set
2733 static void cgroup_advance_iter(struct cgroup
*cgrp
,
2734 struct cgroup_iter
*it
)
2736 struct list_head
*l
= it
->cg_link
;
2737 struct cg_cgroup_link
*link
;
2740 /* Advance to the next non-empty css_set */
2743 if (l
== &cgrp
->css_sets
) {
2747 link
= list_entry(l
, struct cg_cgroup_link
, cgrp_link_list
);
2749 } while (list_empty(&cg
->tasks
));
2751 it
->task
= cg
->tasks
.next
;
2755 * To reduce the fork() overhead for systems that are not actually
2756 * using their cgroups capability, we don't maintain the lists running
2757 * through each css_set to its tasks until we see the list actually
2758 * used - in other words after the first call to cgroup_iter_start().
2760 * The tasklist_lock is not held here, as do_each_thread() and
2761 * while_each_thread() are protected by RCU.
2763 static void cgroup_enable_task_cg_lists(void)
2765 struct task_struct
*p
, *g
;
2766 write_lock(&css_set_lock
);
2767 use_task_css_set_links
= 1;
2768 do_each_thread(g
, p
) {
2771 * We should check if the process is exiting, otherwise
2772 * it will race with cgroup_exit() in that the list
2773 * entry won't be deleted though the process has exited.
2775 if (!(p
->flags
& PF_EXITING
) && list_empty(&p
->cg_list
))
2776 list_add(&p
->cg_list
, &p
->cgroups
->tasks
);
2778 } while_each_thread(g
, p
);
2779 write_unlock(&css_set_lock
);
2782 void cgroup_iter_start(struct cgroup
*cgrp
, struct cgroup_iter
*it
)
2785 * The first time anyone tries to iterate across a cgroup,
2786 * we need to enable the list linking each css_set to its
2787 * tasks, and fix up all existing tasks.
2789 if (!use_task_css_set_links
)
2790 cgroup_enable_task_cg_lists();
2792 read_lock(&css_set_lock
);
2793 it
->cg_link
= &cgrp
->css_sets
;
2794 cgroup_advance_iter(cgrp
, it
);
2797 struct task_struct
*cgroup_iter_next(struct cgroup
*cgrp
,
2798 struct cgroup_iter
*it
)
2800 struct task_struct
*res
;
2801 struct list_head
*l
= it
->task
;
2802 struct cg_cgroup_link
*link
;
2804 /* If the iterator cg is NULL, we have no tasks */
2807 res
= list_entry(l
, struct task_struct
, cg_list
);
2808 /* Advance iterator to find next entry */
2810 link
= list_entry(it
->cg_link
, struct cg_cgroup_link
, cgrp_link_list
);
2811 if (l
== &link
->cg
->tasks
) {
2812 /* We reached the end of this task list - move on to
2813 * the next cg_cgroup_link */
2814 cgroup_advance_iter(cgrp
, it
);
2821 void cgroup_iter_end(struct cgroup
*cgrp
, struct cgroup_iter
*it
)
2823 read_unlock(&css_set_lock
);
2826 static inline int started_after_time(struct task_struct
*t1
,
2827 struct timespec
*time
,
2828 struct task_struct
*t2
)
2830 int start_diff
= timespec_compare(&t1
->start_time
, time
);
2831 if (start_diff
> 0) {
2833 } else if (start_diff
< 0) {
2837 * Arbitrarily, if two processes started at the same
2838 * time, we'll say that the lower pointer value
2839 * started first. Note that t2 may have exited by now
2840 * so this may not be a valid pointer any longer, but
2841 * that's fine - it still serves to distinguish
2842 * between two tasks started (effectively) simultaneously.
2849 * This function is a callback from heap_insert() and is used to order
2851 * In this case we order the heap in descending task start time.
2853 static inline int started_after(void *p1
, void *p2
)
2855 struct task_struct
*t1
= p1
;
2856 struct task_struct
*t2
= p2
;
2857 return started_after_time(t1
, &t2
->start_time
, t2
);
2861 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
2862 * @scan: struct cgroup_scanner containing arguments for the scan
2864 * Arguments include pointers to callback functions test_task() and
2866 * Iterate through all the tasks in a cgroup, calling test_task() for each,
2867 * and if it returns true, call process_task() for it also.
2868 * The test_task pointer may be NULL, meaning always true (select all tasks).
2869 * Effectively duplicates cgroup_iter_{start,next,end}()
2870 * but does not lock css_set_lock for the call to process_task().
2871 * The struct cgroup_scanner may be embedded in any structure of the caller's
2873 * It is guaranteed that process_task() will act on every task that
2874 * is a member of the cgroup for the duration of this call. This
2875 * function may or may not call process_task() for tasks that exit
2876 * or move to a different cgroup during the call, or are forked or
2877 * move into the cgroup during the call.
2879 * Note that test_task() may be called with locks held, and may in some
2880 * situations be called multiple times for the same task, so it should
2882 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
2883 * pre-allocated and will be used for heap operations (and its "gt" member will
2884 * be overwritten), else a temporary heap will be used (allocation of which
2885 * may cause this function to fail).
2887 int cgroup_scan_tasks(struct cgroup_scanner
*scan
)
2890 struct cgroup_iter it
;
2891 struct task_struct
*p
, *dropped
;
2892 /* Never dereference latest_task, since it's not refcounted */
2893 struct task_struct
*latest_task
= NULL
;
2894 struct ptr_heap tmp_heap
;
2895 struct ptr_heap
*heap
;
2896 struct timespec latest_time
= { 0, 0 };
2899 /* The caller supplied our heap and pre-allocated its memory */
2901 heap
->gt
= &started_after
;
2903 /* We need to allocate our own heap memory */
2905 retval
= heap_init(heap
, PAGE_SIZE
, GFP_KERNEL
, &started_after
);
2907 /* cannot allocate the heap */
2913 * Scan tasks in the cgroup, using the scanner's "test_task" callback
2914 * to determine which are of interest, and using the scanner's
2915 * "process_task" callback to process any of them that need an update.
2916 * Since we don't want to hold any locks during the task updates,
2917 * gather tasks to be processed in a heap structure.
2918 * The heap is sorted by descending task start time.
2919 * If the statically-sized heap fills up, we overflow tasks that
2920 * started later, and in future iterations only consider tasks that
2921 * started after the latest task in the previous pass. This
2922 * guarantees forward progress and that we don't miss any tasks.
2925 cgroup_iter_start(scan
->cg
, &it
);
2926 while ((p
= cgroup_iter_next(scan
->cg
, &it
))) {
2928 * Only affect tasks that qualify per the caller's callback,
2929 * if he provided one
2931 if (scan
->test_task
&& !scan
->test_task(p
, scan
))
2934 * Only process tasks that started after the last task
2937 if (!started_after_time(p
, &latest_time
, latest_task
))
2939 dropped
= heap_insert(heap
, p
);
2940 if (dropped
== NULL
) {
2942 * The new task was inserted; the heap wasn't
2946 } else if (dropped
!= p
) {
2948 * The new task was inserted, and pushed out a
2952 put_task_struct(dropped
);
2955 * Else the new task was newer than anything already in
2956 * the heap and wasn't inserted
2959 cgroup_iter_end(scan
->cg
, &it
);
2962 for (i
= 0; i
< heap
->size
; i
++) {
2963 struct task_struct
*q
= heap
->ptrs
[i
];
2965 latest_time
= q
->start_time
;
2968 /* Process the task per the caller's callback */
2969 scan
->process_task(q
, scan
);
2973 * If we had to process any tasks at all, scan again
2974 * in case some of them were in the middle of forking
2975 * children that didn't get processed.
2976 * Not the most efficient way to do it, but it avoids
2977 * having to take callback_mutex in the fork path
2981 if (heap
== &tmp_heap
)
2982 heap_free(&tmp_heap
);
2987 * Stuff for reading the 'tasks'/'procs' files.
2989 * Reading this file can return large amounts of data if a cgroup has
2990 * *lots* of attached tasks. So it may need several calls to read(),
2991 * but we cannot guarantee that the information we produce is correct
2992 * unless we produce it entirely atomically.
2997 * The following two functions "fix" the issue where there are more pids
2998 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
2999 * TODO: replace with a kernel-wide solution to this problem
3001 #define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3002 static void *pidlist_allocate(int count
)
3004 if (PIDLIST_TOO_LARGE(count
))
3005 return vmalloc(count
* sizeof(pid_t
));
3007 return kmalloc(count
* sizeof(pid_t
), GFP_KERNEL
);
3009 static void pidlist_free(void *p
)
3011 if (is_vmalloc_addr(p
))
3016 static void *pidlist_resize(void *p
, int newcount
)
3019 /* note: if new alloc fails, old p will still be valid either way */
3020 if (is_vmalloc_addr(p
)) {
3021 newlist
= vmalloc(newcount
* sizeof(pid_t
));
3024 memcpy(newlist
, p
, newcount
* sizeof(pid_t
));
3027 newlist
= krealloc(p
, newcount
* sizeof(pid_t
), GFP_KERNEL
);
3033 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
3034 * If the new stripped list is sufficiently smaller and there's enough memory
3035 * to allocate a new buffer, will let go of the unneeded memory. Returns the
3036 * number of unique elements.
3038 /* is the size difference enough that we should re-allocate the array? */
3039 #define PIDLIST_REALLOC_DIFFERENCE(old, new) ((old) - PAGE_SIZE >= (new))
3040 static int pidlist_uniq(pid_t
**p
, int length
)
3047 * we presume the 0th element is unique, so i starts at 1. trivial
3048 * edge cases first; no work needs to be done for either
3050 if (length
== 0 || length
== 1)
3052 /* src and dest walk down the list; dest counts unique elements */
3053 for (src
= 1; src
< length
; src
++) {
3054 /* find next unique element */
3055 while (list
[src
] == list
[src
-1]) {
3060 /* dest always points to where the next unique element goes */
3061 list
[dest
] = list
[src
];
3066 * if the length difference is large enough, we want to allocate a
3067 * smaller buffer to save memory. if this fails due to out of memory,
3068 * we'll just stay with what we've got.
3070 if (PIDLIST_REALLOC_DIFFERENCE(length
, dest
)) {
3071 newlist
= pidlist_resize(list
, dest
);
3078 static int cmppid(const void *a
, const void *b
)
3080 return *(pid_t
*)a
- *(pid_t
*)b
;
3084 * find the appropriate pidlist for our purpose (given procs vs tasks)
3085 * returns with the lock on that pidlist already held, and takes care
3086 * of the use count, or returns NULL with no locks held if we're out of
3089 static struct cgroup_pidlist
*cgroup_pidlist_find(struct cgroup
*cgrp
,
3090 enum cgroup_filetype type
)
3092 struct cgroup_pidlist
*l
;
3093 /* don't need task_nsproxy() if we're looking at ourself */
3094 struct pid_namespace
*ns
= current
->nsproxy
->pid_ns
;
3097 * We can't drop the pidlist_mutex before taking the l->mutex in case
3098 * the last ref-holder is trying to remove l from the list at the same
3099 * time. Holding the pidlist_mutex precludes somebody taking whichever
3100 * list we find out from under us - compare release_pid_array().
3102 mutex_lock(&cgrp
->pidlist_mutex
);
3103 list_for_each_entry(l
, &cgrp
->pidlists
, links
) {
3104 if (l
->key
.type
== type
&& l
->key
.ns
== ns
) {
3105 /* make sure l doesn't vanish out from under us */
3106 down_write(&l
->mutex
);
3107 mutex_unlock(&cgrp
->pidlist_mutex
);
3111 /* entry not found; create a new one */
3112 l
= kmalloc(sizeof(struct cgroup_pidlist
), GFP_KERNEL
);
3114 mutex_unlock(&cgrp
->pidlist_mutex
);
3117 init_rwsem(&l
->mutex
);
3118 down_write(&l
->mutex
);
3120 l
->key
.ns
= get_pid_ns(ns
);
3121 l
->use_count
= 0; /* don't increment here */
3124 list_add(&l
->links
, &cgrp
->pidlists
);
3125 mutex_unlock(&cgrp
->pidlist_mutex
);
3130 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3132 static int pidlist_array_load(struct cgroup
*cgrp
, enum cgroup_filetype type
,
3133 struct cgroup_pidlist
**lp
)
3137 int pid
, n
= 0; /* used for populating the array */
3138 struct cgroup_iter it
;
3139 struct task_struct
*tsk
;
3140 struct cgroup_pidlist
*l
;
3143 * If cgroup gets more users after we read count, we won't have
3144 * enough space - tough. This race is indistinguishable to the
3145 * caller from the case that the additional cgroup users didn't
3146 * show up until sometime later on.
3148 length
= cgroup_task_count(cgrp
);
3149 array
= pidlist_allocate(length
);
3152 /* now, populate the array */
3153 cgroup_iter_start(cgrp
, &it
);
3154 while ((tsk
= cgroup_iter_next(cgrp
, &it
))) {
3155 if (unlikely(n
== length
))
3157 /* get tgid or pid for procs or tasks file respectively */
3158 if (type
== CGROUP_FILE_PROCS
)
3159 pid
= task_tgid_vnr(tsk
);
3161 pid
= task_pid_vnr(tsk
);
3162 if (pid
> 0) /* make sure to only use valid results */
3165 cgroup_iter_end(cgrp
, &it
);
3167 /* now sort & (if procs) strip out duplicates */
3168 sort(array
, length
, sizeof(pid_t
), cmppid
, NULL
);
3169 if (type
== CGROUP_FILE_PROCS
)
3170 length
= pidlist_uniq(&array
, length
);
3171 l
= cgroup_pidlist_find(cgrp
, type
);
3173 pidlist_free(array
);
3176 /* store array, freeing old if necessary - lock already held */
3177 pidlist_free(l
->list
);
3181 up_write(&l
->mutex
);
3187 * cgroupstats_build - build and fill cgroupstats
3188 * @stats: cgroupstats to fill information into
3189 * @dentry: A dentry entry belonging to the cgroup for which stats have
3192 * Build and fill cgroupstats so that taskstats can export it to user
3195 int cgroupstats_build(struct cgroupstats
*stats
, struct dentry
*dentry
)
3198 struct cgroup
*cgrp
;
3199 struct cgroup_iter it
;
3200 struct task_struct
*tsk
;
3203 * Validate dentry by checking the superblock operations,
3204 * and make sure it's a directory.
3206 if (dentry
->d_sb
->s_op
!= &cgroup_ops
||
3207 !S_ISDIR(dentry
->d_inode
->i_mode
))
3211 cgrp
= dentry
->d_fsdata
;
3213 cgroup_iter_start(cgrp
, &it
);
3214 while ((tsk
= cgroup_iter_next(cgrp
, &it
))) {
3215 switch (tsk
->state
) {
3217 stats
->nr_running
++;
3219 case TASK_INTERRUPTIBLE
:
3220 stats
->nr_sleeping
++;
3222 case TASK_UNINTERRUPTIBLE
:
3223 stats
->nr_uninterruptible
++;
3226 stats
->nr_stopped
++;
3229 if (delayacct_is_task_waiting_on_io(tsk
))
3230 stats
->nr_io_wait
++;
3234 cgroup_iter_end(cgrp
, &it
);
3242 * seq_file methods for the tasks/procs files. The seq_file position is the
3243 * next pid to display; the seq_file iterator is a pointer to the pid
3244 * in the cgroup->l->list array.
3247 static void *cgroup_pidlist_start(struct seq_file
*s
, loff_t
*pos
)
3250 * Initially we receive a position value that corresponds to
3251 * one more than the last pid shown (or 0 on the first call or
3252 * after a seek to the start). Use a binary-search to find the
3253 * next pid to display, if any
3255 struct cgroup_pidlist
*l
= s
->private;
3256 int index
= 0, pid
= *pos
;
3259 down_read(&l
->mutex
);
3261 int end
= l
->length
;
3263 while (index
< end
) {
3264 int mid
= (index
+ end
) / 2;
3265 if (l
->list
[mid
] == pid
) {
3268 } else if (l
->list
[mid
] <= pid
)
3274 /* If we're off the end of the array, we're done */
3275 if (index
>= l
->length
)
3277 /* Update the abstract position to be the actual pid that we found */
3278 iter
= l
->list
+ index
;
3283 static void cgroup_pidlist_stop(struct seq_file
*s
, void *v
)
3285 struct cgroup_pidlist
*l
= s
->private;
3289 static void *cgroup_pidlist_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
3291 struct cgroup_pidlist
*l
= s
->private;
3293 pid_t
*end
= l
->list
+ l
->length
;
3295 * Advance to the next pid in the array. If this goes off the
3307 static int cgroup_pidlist_show(struct seq_file
*s
, void *v
)
3309 return seq_printf(s
, "%d\n", *(int *)v
);
3313 * seq_operations functions for iterating on pidlists through seq_file -
3314 * independent of whether it's tasks or procs
3316 static const struct seq_operations cgroup_pidlist_seq_operations
= {
3317 .start
= cgroup_pidlist_start
,
3318 .stop
= cgroup_pidlist_stop
,
3319 .next
= cgroup_pidlist_next
,
3320 .show
= cgroup_pidlist_show
,
3323 static void cgroup_release_pid_array(struct cgroup_pidlist
*l
)
3326 * the case where we're the last user of this particular pidlist will
3327 * have us remove it from the cgroup's list, which entails taking the
3328 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3329 * pidlist_mutex, we have to take pidlist_mutex first.
3331 mutex_lock(&l
->owner
->pidlist_mutex
);
3332 down_write(&l
->mutex
);
3333 BUG_ON(!l
->use_count
);
3334 if (!--l
->use_count
) {
3335 /* we're the last user if refcount is 0; remove and free */
3336 list_del(&l
->links
);
3337 mutex_unlock(&l
->owner
->pidlist_mutex
);
3338 pidlist_free(l
->list
);
3339 put_pid_ns(l
->key
.ns
);
3340 up_write(&l
->mutex
);
3344 mutex_unlock(&l
->owner
->pidlist_mutex
);
3345 up_write(&l
->mutex
);
3348 static int cgroup_pidlist_release(struct inode
*inode
, struct file
*file
)
3350 struct cgroup_pidlist
*l
;
3351 if (!(file
->f_mode
& FMODE_READ
))
3354 * the seq_file will only be initialized if the file was opened for
3355 * reading; hence we check if it's not null only in that case.
3357 l
= ((struct seq_file
*)file
->private_data
)->private;
3358 cgroup_release_pid_array(l
);
3359 return seq_release(inode
, file
);
3362 static const struct file_operations cgroup_pidlist_operations
= {
3364 .llseek
= seq_lseek
,
3365 .write
= cgroup_file_write
,
3366 .release
= cgroup_pidlist_release
,
3370 * The following functions handle opens on a file that displays a pidlist
3371 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3374 /* helper function for the two below it */
3375 static int cgroup_pidlist_open(struct file
*file
, enum cgroup_filetype type
)
3377 struct cgroup
*cgrp
= __d_cgrp(file
->f_dentry
->d_parent
);
3378 struct cgroup_pidlist
*l
;
3381 /* Nothing to do for write-only files */
3382 if (!(file
->f_mode
& FMODE_READ
))
3385 /* have the array populated */
3386 retval
= pidlist_array_load(cgrp
, type
, &l
);
3389 /* configure file information */
3390 file
->f_op
= &cgroup_pidlist_operations
;
3392 retval
= seq_open(file
, &cgroup_pidlist_seq_operations
);
3394 cgroup_release_pid_array(l
);
3397 ((struct seq_file
*)file
->private_data
)->private = l
;
3400 static int cgroup_tasks_open(struct inode
*unused
, struct file
*file
)
3402 return cgroup_pidlist_open(file
, CGROUP_FILE_TASKS
);
3404 static int cgroup_procs_open(struct inode
*unused
, struct file
*file
)
3406 return cgroup_pidlist_open(file
, CGROUP_FILE_PROCS
);
3409 static u64
cgroup_read_notify_on_release(struct cgroup
*cgrp
,
3412 return notify_on_release(cgrp
);
3415 static int cgroup_write_notify_on_release(struct cgroup
*cgrp
,
3419 clear_bit(CGRP_RELEASABLE
, &cgrp
->flags
);
3421 set_bit(CGRP_NOTIFY_ON_RELEASE
, &cgrp
->flags
);
3423 clear_bit(CGRP_NOTIFY_ON_RELEASE
, &cgrp
->flags
);
3428 * Unregister event and free resources.
3430 * Gets called from workqueue.
3432 static void cgroup_event_remove(struct work_struct
*work
)
3434 struct cgroup_event
*event
= container_of(work
, struct cgroup_event
,
3436 struct cgroup
*cgrp
= event
->cgrp
;
3438 event
->cft
->unregister_event(cgrp
, event
->cft
, event
->eventfd
);
3440 eventfd_ctx_put(event
->eventfd
);
3446 * Gets called on POLLHUP on eventfd when user closes it.
3448 * Called with wqh->lock held and interrupts disabled.
3450 static int cgroup_event_wake(wait_queue_t
*wait
, unsigned mode
,
3451 int sync
, void *key
)
3453 struct cgroup_event
*event
= container_of(wait
,
3454 struct cgroup_event
, wait
);
3455 struct cgroup
*cgrp
= event
->cgrp
;
3456 unsigned long flags
= (unsigned long)key
;
3458 if (flags
& POLLHUP
) {
3459 __remove_wait_queue(event
->wqh
, &event
->wait
);
3460 spin_lock(&cgrp
->event_list_lock
);
3461 list_del(&event
->list
);
3462 spin_unlock(&cgrp
->event_list_lock
);
3464 * We are in atomic context, but cgroup_event_remove() may
3465 * sleep, so we have to call it in workqueue.
3467 schedule_work(&event
->remove
);
3473 static void cgroup_event_ptable_queue_proc(struct file
*file
,
3474 wait_queue_head_t
*wqh
, poll_table
*pt
)
3476 struct cgroup_event
*event
= container_of(pt
,
3477 struct cgroup_event
, pt
);
3480 add_wait_queue(wqh
, &event
->wait
);
3484 * Parse input and register new cgroup event handler.
3486 * Input must be in format '<event_fd> <control_fd> <args>'.
3487 * Interpretation of args is defined by control file implementation.
3489 static int cgroup_write_event_control(struct cgroup
*cgrp
, struct cftype
*cft
,
3492 struct cgroup_event
*event
= NULL
;
3493 unsigned int efd
, cfd
;
3494 struct file
*efile
= NULL
;
3495 struct file
*cfile
= NULL
;
3499 efd
= simple_strtoul(buffer
, &endp
, 10);
3504 cfd
= simple_strtoul(buffer
, &endp
, 10);
3505 if ((*endp
!= ' ') && (*endp
!= '\0'))
3509 event
= kzalloc(sizeof(*event
), GFP_KERNEL
);
3513 INIT_LIST_HEAD(&event
->list
);
3514 init_poll_funcptr(&event
->pt
, cgroup_event_ptable_queue_proc
);
3515 init_waitqueue_func_entry(&event
->wait
, cgroup_event_wake
);
3516 INIT_WORK(&event
->remove
, cgroup_event_remove
);
3518 efile
= eventfd_fget(efd
);
3519 if (IS_ERR(efile
)) {
3520 ret
= PTR_ERR(efile
);
3524 event
->eventfd
= eventfd_ctx_fileget(efile
);
3525 if (IS_ERR(event
->eventfd
)) {
3526 ret
= PTR_ERR(event
->eventfd
);
3536 /* the process need read permission on control file */
3537 ret
= file_permission(cfile
, MAY_READ
);
3541 event
->cft
= __file_cft(cfile
);
3542 if (IS_ERR(event
->cft
)) {
3543 ret
= PTR_ERR(event
->cft
);
3547 if (!event
->cft
->register_event
|| !event
->cft
->unregister_event
) {
3552 ret
= event
->cft
->register_event(cgrp
, event
->cft
,
3553 event
->eventfd
, buffer
);
3557 if (efile
->f_op
->poll(efile
, &event
->pt
) & POLLHUP
) {
3558 event
->cft
->unregister_event(cgrp
, event
->cft
, event
->eventfd
);
3564 * Events should be removed after rmdir of cgroup directory, but before
3565 * destroying subsystem state objects. Let's take reference to cgroup
3566 * directory dentry to do that.
3570 spin_lock(&cgrp
->event_list_lock
);
3571 list_add(&event
->list
, &cgrp
->event_list
);
3572 spin_unlock(&cgrp
->event_list_lock
);
3583 if (event
&& event
->eventfd
&& !IS_ERR(event
->eventfd
))
3584 eventfd_ctx_put(event
->eventfd
);
3586 if (!IS_ERR_OR_NULL(efile
))
3594 static u64
cgroup_clone_children_read(struct cgroup
*cgrp
,
3597 return clone_children(cgrp
);
3600 static int cgroup_clone_children_write(struct cgroup
*cgrp
,
3605 set_bit(CGRP_CLONE_CHILDREN
, &cgrp
->flags
);
3607 clear_bit(CGRP_CLONE_CHILDREN
, &cgrp
->flags
);
3612 * for the common functions, 'private' gives the type of file
3614 /* for hysterical raisins, we can't put this on the older files */
3615 #define CGROUP_FILE_GENERIC_PREFIX "cgroup."
3616 static struct cftype files
[] = {
3619 .open
= cgroup_tasks_open
,
3620 .write_u64
= cgroup_tasks_write
,
3621 .release
= cgroup_pidlist_release
,
3622 .mode
= S_IRUGO
| S_IWUSR
,
3625 .name
= CGROUP_FILE_GENERIC_PREFIX
"procs",
3626 .open
= cgroup_procs_open
,
3627 .write_u64
= cgroup_procs_write
,
3628 .release
= cgroup_pidlist_release
,
3629 .mode
= S_IRUGO
| S_IWUSR
,
3632 .name
= "notify_on_release",
3633 .read_u64
= cgroup_read_notify_on_release
,
3634 .write_u64
= cgroup_write_notify_on_release
,
3637 .name
= CGROUP_FILE_GENERIC_PREFIX
"event_control",
3638 .write_string
= cgroup_write_event_control
,
3642 .name
= "cgroup.clone_children",
3643 .read_u64
= cgroup_clone_children_read
,
3644 .write_u64
= cgroup_clone_children_write
,
3648 static struct cftype cft_release_agent
= {
3649 .name
= "release_agent",
3650 .read_seq_string
= cgroup_release_agent_show
,
3651 .write_string
= cgroup_release_agent_write
,
3652 .max_write_len
= PATH_MAX
,
3655 static int cgroup_populate_dir(struct cgroup
*cgrp
)
3658 struct cgroup_subsys
*ss
;
3660 /* First clear out any existing files */
3661 cgroup_clear_directory(cgrp
->dentry
);
3663 err
= cgroup_add_files(cgrp
, NULL
, files
, ARRAY_SIZE(files
));
3667 if (cgrp
== cgrp
->top_cgroup
) {
3668 if ((err
= cgroup_add_file(cgrp
, NULL
, &cft_release_agent
)) < 0)
3672 for_each_subsys(cgrp
->root
, ss
) {
3673 if (ss
->populate
&& (err
= ss
->populate(ss
, cgrp
)) < 0)
3676 /* This cgroup is ready now */
3677 for_each_subsys(cgrp
->root
, ss
) {
3678 struct cgroup_subsys_state
*css
= cgrp
->subsys
[ss
->subsys_id
];
3680 * Update id->css pointer and make this css visible from
3681 * CSS ID functions. This pointer will be dereferened
3682 * from RCU-read-side without locks.
3685 rcu_assign_pointer(css
->id
->css
, css
);
3691 static void init_cgroup_css(struct cgroup_subsys_state
*css
,
3692 struct cgroup_subsys
*ss
,
3693 struct cgroup
*cgrp
)
3696 atomic_set(&css
->refcnt
, 1);
3699 if (cgrp
== dummytop
)
3700 set_bit(CSS_ROOT
, &css
->flags
);
3701 BUG_ON(cgrp
->subsys
[ss
->subsys_id
]);
3702 cgrp
->subsys
[ss
->subsys_id
] = css
;
3705 static void cgroup_lock_hierarchy(struct cgroupfs_root
*root
)
3707 /* We need to take each hierarchy_mutex in a consistent order */
3711 * No worry about a race with rebind_subsystems that might mess up the
3712 * locking order, since both parties are under cgroup_mutex.
3714 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
3715 struct cgroup_subsys
*ss
= subsys
[i
];
3718 if (ss
->root
== root
)
3719 mutex_lock(&ss
->hierarchy_mutex
);
3723 static void cgroup_unlock_hierarchy(struct cgroupfs_root
*root
)
3727 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
3728 struct cgroup_subsys
*ss
= subsys
[i
];
3731 if (ss
->root
== root
)
3732 mutex_unlock(&ss
->hierarchy_mutex
);
3737 * cgroup_create - create a cgroup
3738 * @parent: cgroup that will be parent of the new cgroup
3739 * @dentry: dentry of the new cgroup
3740 * @mode: mode to set on new inode
3742 * Must be called with the mutex on the parent inode held
3744 static long cgroup_create(struct cgroup
*parent
, struct dentry
*dentry
,
3747 struct cgroup
*cgrp
;
3748 struct cgroupfs_root
*root
= parent
->root
;
3750 struct cgroup_subsys
*ss
;
3751 struct super_block
*sb
= root
->sb
;
3753 cgrp
= kzalloc(sizeof(*cgrp
), GFP_KERNEL
);
3757 /* Grab a reference on the superblock so the hierarchy doesn't
3758 * get deleted on unmount if there are child cgroups. This
3759 * can be done outside cgroup_mutex, since the sb can't
3760 * disappear while someone has an open control file on the
3762 atomic_inc(&sb
->s_active
);
3764 mutex_lock(&cgroup_mutex
);
3766 init_cgroup_housekeeping(cgrp
);
3768 cgrp
->parent
= parent
;
3769 cgrp
->root
= parent
->root
;
3770 cgrp
->top_cgroup
= parent
->top_cgroup
;
3772 if (notify_on_release(parent
))
3773 set_bit(CGRP_NOTIFY_ON_RELEASE
, &cgrp
->flags
);
3775 if (clone_children(parent
))
3776 set_bit(CGRP_CLONE_CHILDREN
, &cgrp
->flags
);
3778 for_each_subsys(root
, ss
) {
3779 struct cgroup_subsys_state
*css
= ss
->create(ss
, cgrp
);
3785 init_cgroup_css(css
, ss
, cgrp
);
3787 err
= alloc_css_id(ss
, parent
, cgrp
);
3791 /* At error, ->destroy() callback has to free assigned ID. */
3792 if (clone_children(parent
) && ss
->post_clone
)
3793 ss
->post_clone(ss
, cgrp
);
3796 cgroup_lock_hierarchy(root
);
3797 list_add(&cgrp
->sibling
, &cgrp
->parent
->children
);
3798 cgroup_unlock_hierarchy(root
);
3799 root
->number_of_cgroups
++;
3801 err
= cgroup_create_dir(cgrp
, dentry
, mode
);
3805 /* The cgroup directory was pre-locked for us */
3806 BUG_ON(!mutex_is_locked(&cgrp
->dentry
->d_inode
->i_mutex
));
3808 err
= cgroup_populate_dir(cgrp
);
3809 /* If err < 0, we have a half-filled directory - oh well ;) */
3811 mutex_unlock(&cgroup_mutex
);
3812 mutex_unlock(&cgrp
->dentry
->d_inode
->i_mutex
);
3818 cgroup_lock_hierarchy(root
);
3819 list_del(&cgrp
->sibling
);
3820 cgroup_unlock_hierarchy(root
);
3821 root
->number_of_cgroups
--;
3825 for_each_subsys(root
, ss
) {
3826 if (cgrp
->subsys
[ss
->subsys_id
])
3827 ss
->destroy(ss
, cgrp
);
3830 mutex_unlock(&cgroup_mutex
);
3832 /* Release the reference count that we took on the superblock */
3833 deactivate_super(sb
);
3839 static int cgroup_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
3841 struct cgroup
*c_parent
= dentry
->d_parent
->d_fsdata
;
3843 /* the vfs holds inode->i_mutex already */
3844 return cgroup_create(c_parent
, dentry
, mode
| S_IFDIR
);
3847 static int cgroup_has_css_refs(struct cgroup
*cgrp
)
3849 /* Check the reference count on each subsystem. Since we
3850 * already established that there are no tasks in the
3851 * cgroup, if the css refcount is also 1, then there should
3852 * be no outstanding references, so the subsystem is safe to
3853 * destroy. We scan across all subsystems rather than using
3854 * the per-hierarchy linked list of mounted subsystems since
3855 * we can be called via check_for_release() with no
3856 * synchronization other than RCU, and the subsystem linked
3857 * list isn't RCU-safe */
3860 * We won't need to lock the subsys array, because the subsystems
3861 * we're concerned about aren't going anywhere since our cgroup root
3862 * has a reference on them.
3864 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
3865 struct cgroup_subsys
*ss
= subsys
[i
];
3866 struct cgroup_subsys_state
*css
;
3867 /* Skip subsystems not present or not in this hierarchy */
3868 if (ss
== NULL
|| ss
->root
!= cgrp
->root
)
3870 css
= cgrp
->subsys
[ss
->subsys_id
];
3871 /* When called from check_for_release() it's possible
3872 * that by this point the cgroup has been removed
3873 * and the css deleted. But a false-positive doesn't
3874 * matter, since it can only happen if the cgroup
3875 * has been deleted and hence no longer needs the
3876 * release agent to be called anyway. */
3877 if (css
&& (atomic_read(&css
->refcnt
) > 1))
3884 * Atomically mark all (or else none) of the cgroup's CSS objects as
3885 * CSS_REMOVED. Return true on success, or false if the cgroup has
3886 * busy subsystems. Call with cgroup_mutex held
3889 static int cgroup_clear_css_refs(struct cgroup
*cgrp
)
3891 struct cgroup_subsys
*ss
;
3892 unsigned long flags
;
3893 bool failed
= false;
3894 local_irq_save(flags
);
3895 for_each_subsys(cgrp
->root
, ss
) {
3896 struct cgroup_subsys_state
*css
= cgrp
->subsys
[ss
->subsys_id
];
3899 /* We can only remove a CSS with a refcnt==1 */
3900 refcnt
= atomic_read(&css
->refcnt
);
3907 * Drop the refcnt to 0 while we check other
3908 * subsystems. This will cause any racing
3909 * css_tryget() to spin until we set the
3910 * CSS_REMOVED bits or abort
3912 if (atomic_cmpxchg(&css
->refcnt
, refcnt
, 0) == refcnt
)
3918 for_each_subsys(cgrp
->root
, ss
) {
3919 struct cgroup_subsys_state
*css
= cgrp
->subsys
[ss
->subsys_id
];
3922 * Restore old refcnt if we previously managed
3923 * to clear it from 1 to 0
3925 if (!atomic_read(&css
->refcnt
))
3926 atomic_set(&css
->refcnt
, 1);
3928 /* Commit the fact that the CSS is removed */
3929 set_bit(CSS_REMOVED
, &css
->flags
);
3932 local_irq_restore(flags
);
3936 static int cgroup_rmdir(struct inode
*unused_dir
, struct dentry
*dentry
)
3938 struct cgroup
*cgrp
= dentry
->d_fsdata
;
3940 struct cgroup
*parent
;
3942 struct cgroup_event
*event
, *tmp
;
3945 /* the vfs holds both inode->i_mutex already */
3947 mutex_lock(&cgroup_mutex
);
3948 if (atomic_read(&cgrp
->count
) != 0) {
3949 mutex_unlock(&cgroup_mutex
);
3952 if (!list_empty(&cgrp
->children
)) {
3953 mutex_unlock(&cgroup_mutex
);
3956 mutex_unlock(&cgroup_mutex
);
3959 * In general, subsystem has no css->refcnt after pre_destroy(). But
3960 * in racy cases, subsystem may have to get css->refcnt after
3961 * pre_destroy() and it makes rmdir return with -EBUSY. This sometimes
3962 * make rmdir return -EBUSY too often. To avoid that, we use waitqueue
3963 * for cgroup's rmdir. CGRP_WAIT_ON_RMDIR is for synchronizing rmdir
3964 * and subsystem's reference count handling. Please see css_get/put
3965 * and css_tryget() and cgroup_wakeup_rmdir_waiter() implementation.
3967 set_bit(CGRP_WAIT_ON_RMDIR
, &cgrp
->flags
);
3970 * Call pre_destroy handlers of subsys. Notify subsystems
3971 * that rmdir() request comes.
3973 ret
= cgroup_call_pre_destroy(cgrp
);
3975 clear_bit(CGRP_WAIT_ON_RMDIR
, &cgrp
->flags
);
3979 mutex_lock(&cgroup_mutex
);
3980 parent
= cgrp
->parent
;
3981 if (atomic_read(&cgrp
->count
) || !list_empty(&cgrp
->children
)) {
3982 clear_bit(CGRP_WAIT_ON_RMDIR
, &cgrp
->flags
);
3983 mutex_unlock(&cgroup_mutex
);
3986 prepare_to_wait(&cgroup_rmdir_waitq
, &wait
, TASK_INTERRUPTIBLE
);
3987 if (!cgroup_clear_css_refs(cgrp
)) {
3988 mutex_unlock(&cgroup_mutex
);
3990 * Because someone may call cgroup_wakeup_rmdir_waiter() before
3991 * prepare_to_wait(), we need to check this flag.
3993 if (test_bit(CGRP_WAIT_ON_RMDIR
, &cgrp
->flags
))
3995 finish_wait(&cgroup_rmdir_waitq
, &wait
);
3996 clear_bit(CGRP_WAIT_ON_RMDIR
, &cgrp
->flags
);
3997 if (signal_pending(current
))
4001 /* NO css_tryget() can success after here. */
4002 finish_wait(&cgroup_rmdir_waitq
, &wait
);
4003 clear_bit(CGRP_WAIT_ON_RMDIR
, &cgrp
->flags
);
4005 spin_lock(&release_list_lock
);
4006 set_bit(CGRP_REMOVED
, &cgrp
->flags
);
4007 if (!list_empty(&cgrp
->release_list
))
4008 list_del_init(&cgrp
->release_list
);
4009 spin_unlock(&release_list_lock
);
4011 cgroup_lock_hierarchy(cgrp
->root
);
4012 /* delete this cgroup from parent->children */
4013 list_del_init(&cgrp
->sibling
);
4014 cgroup_unlock_hierarchy(cgrp
->root
);
4016 d
= dget(cgrp
->dentry
);
4018 cgroup_d_remove_dir(d
);
4021 set_bit(CGRP_RELEASABLE
, &parent
->flags
);
4022 check_for_release(parent
);
4025 * Unregister events and notify userspace.
4026 * Notify userspace about cgroup removing only after rmdir of cgroup
4027 * directory to avoid race between userspace and kernelspace
4029 spin_lock(&cgrp
->event_list_lock
);
4030 list_for_each_entry_safe(event
, tmp
, &cgrp
->event_list
, list
) {
4031 list_del(&event
->list
);
4032 remove_wait_queue(event
->wqh
, &event
->wait
);
4033 eventfd_signal(event
->eventfd
, 1);
4034 schedule_work(&event
->remove
);
4036 spin_unlock(&cgrp
->event_list_lock
);
4038 mutex_unlock(&cgroup_mutex
);
4042 static void __init
cgroup_init_subsys(struct cgroup_subsys
*ss
)
4044 struct cgroup_subsys_state
*css
;
4046 printk(KERN_INFO
"Initializing cgroup subsys %s\n", ss
->name
);
4048 /* Create the top cgroup state for this subsystem */
4049 list_add(&ss
->sibling
, &rootnode
.subsys_list
);
4050 ss
->root
= &rootnode
;
4051 css
= ss
->create(ss
, dummytop
);
4052 /* We don't handle early failures gracefully */
4053 BUG_ON(IS_ERR(css
));
4054 init_cgroup_css(css
, ss
, dummytop
);
4056 /* Update the init_css_set to contain a subsys
4057 * pointer to this state - since the subsystem is
4058 * newly registered, all tasks and hence the
4059 * init_css_set is in the subsystem's top cgroup. */
4060 init_css_set
.subsys
[ss
->subsys_id
] = dummytop
->subsys
[ss
->subsys_id
];
4062 need_forkexit_callback
|= ss
->fork
|| ss
->exit
;
4064 /* At system boot, before all subsystems have been
4065 * registered, no tasks have been forked, so we don't
4066 * need to invoke fork callbacks here. */
4067 BUG_ON(!list_empty(&init_task
.tasks
));
4069 mutex_init(&ss
->hierarchy_mutex
);
4070 lockdep_set_class(&ss
->hierarchy_mutex
, &ss
->subsys_key
);
4073 /* this function shouldn't be used with modular subsystems, since they
4074 * need to register a subsys_id, among other things */
4079 * cgroup_load_subsys: load and register a modular subsystem at runtime
4080 * @ss: the subsystem to load
4082 * This function should be called in a modular subsystem's initcall. If the
4083 * subsystem is built as a module, it will be assigned a new subsys_id and set
4084 * up for use. If the subsystem is built-in anyway, work is delegated to the
4085 * simpler cgroup_init_subsys.
4087 int __init_or_module
cgroup_load_subsys(struct cgroup_subsys
*ss
)
4090 struct cgroup_subsys_state
*css
;
4092 /* check name and function validity */
4093 if (ss
->name
== NULL
|| strlen(ss
->name
) > MAX_CGROUP_TYPE_NAMELEN
||
4094 ss
->create
== NULL
|| ss
->destroy
== NULL
)
4098 * we don't support callbacks in modular subsystems. this check is
4099 * before the ss->module check for consistency; a subsystem that could
4100 * be a module should still have no callbacks even if the user isn't
4101 * compiling it as one.
4103 if (ss
->fork
|| ss
->exit
)
4107 * an optionally modular subsystem is built-in: we want to do nothing,
4108 * since cgroup_init_subsys will have already taken care of it.
4110 if (ss
->module
== NULL
) {
4111 /* a few sanity checks */
4112 BUG_ON(ss
->subsys_id
>= CGROUP_BUILTIN_SUBSYS_COUNT
);
4113 BUG_ON(subsys
[ss
->subsys_id
] != ss
);
4118 * need to register a subsys id before anything else - for example,
4119 * init_cgroup_css needs it.
4121 mutex_lock(&cgroup_mutex
);
4122 /* find the first empty slot in the array */
4123 for (i
= CGROUP_BUILTIN_SUBSYS_COUNT
; i
< CGROUP_SUBSYS_COUNT
; i
++) {
4124 if (subsys
[i
] == NULL
)
4127 if (i
== CGROUP_SUBSYS_COUNT
) {
4128 /* maximum number of subsystems already registered! */
4129 mutex_unlock(&cgroup_mutex
);
4132 /* assign ourselves the subsys_id */
4137 * no ss->create seems to need anything important in the ss struct, so
4138 * this can happen first (i.e. before the rootnode attachment).
4140 css
= ss
->create(ss
, dummytop
);
4142 /* failure case - need to deassign the subsys[] slot. */
4144 mutex_unlock(&cgroup_mutex
);
4145 return PTR_ERR(css
);
4148 list_add(&ss
->sibling
, &rootnode
.subsys_list
);
4149 ss
->root
= &rootnode
;
4151 /* our new subsystem will be attached to the dummy hierarchy. */
4152 init_cgroup_css(css
, ss
, dummytop
);
4153 /* init_idr must be after init_cgroup_css because it sets css->id. */
4155 int ret
= cgroup_init_idr(ss
, css
);
4157 dummytop
->subsys
[ss
->subsys_id
] = NULL
;
4158 ss
->destroy(ss
, dummytop
);
4160 mutex_unlock(&cgroup_mutex
);
4166 * Now we need to entangle the css into the existing css_sets. unlike
4167 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4168 * will need a new pointer to it; done by iterating the css_set_table.
4169 * furthermore, modifying the existing css_sets will corrupt the hash
4170 * table state, so each changed css_set will need its hash recomputed.
4171 * this is all done under the css_set_lock.
4173 write_lock(&css_set_lock
);
4174 for (i
= 0; i
< CSS_SET_TABLE_SIZE
; i
++) {
4176 struct hlist_node
*node
, *tmp
;
4177 struct hlist_head
*bucket
= &css_set_table
[i
], *new_bucket
;
4179 hlist_for_each_entry_safe(cg
, node
, tmp
, bucket
, hlist
) {
4180 /* skip entries that we already rehashed */
4181 if (cg
->subsys
[ss
->subsys_id
])
4183 /* remove existing entry */
4184 hlist_del(&cg
->hlist
);
4186 cg
->subsys
[ss
->subsys_id
] = css
;
4187 /* recompute hash and restore entry */
4188 new_bucket
= css_set_hash(cg
->subsys
);
4189 hlist_add_head(&cg
->hlist
, new_bucket
);
4192 write_unlock(&css_set_lock
);
4194 mutex_init(&ss
->hierarchy_mutex
);
4195 lockdep_set_class(&ss
->hierarchy_mutex
, &ss
->subsys_key
);
4199 mutex_unlock(&cgroup_mutex
);
4202 EXPORT_SYMBOL_GPL(cgroup_load_subsys
);
4205 * cgroup_unload_subsys: unload a modular subsystem
4206 * @ss: the subsystem to unload
4208 * This function should be called in a modular subsystem's exitcall. When this
4209 * function is invoked, the refcount on the subsystem's module will be 0, so
4210 * the subsystem will not be attached to any hierarchy.
4212 void cgroup_unload_subsys(struct cgroup_subsys
*ss
)
4214 struct cg_cgroup_link
*link
;
4215 struct hlist_head
*hhead
;
4217 BUG_ON(ss
->module
== NULL
);
4220 * we shouldn't be called if the subsystem is in use, and the use of
4221 * try_module_get in parse_cgroupfs_options should ensure that it
4222 * doesn't start being used while we're killing it off.
4224 BUG_ON(ss
->root
!= &rootnode
);
4226 mutex_lock(&cgroup_mutex
);
4227 /* deassign the subsys_id */
4228 BUG_ON(ss
->subsys_id
< CGROUP_BUILTIN_SUBSYS_COUNT
);
4229 subsys
[ss
->subsys_id
] = NULL
;
4231 /* remove subsystem from rootnode's list of subsystems */
4232 list_del_init(&ss
->sibling
);
4235 * disentangle the css from all css_sets attached to the dummytop. as
4236 * in loading, we need to pay our respects to the hashtable gods.
4238 write_lock(&css_set_lock
);
4239 list_for_each_entry(link
, &dummytop
->css_sets
, cgrp_link_list
) {
4240 struct css_set
*cg
= link
->cg
;
4242 hlist_del(&cg
->hlist
);
4243 BUG_ON(!cg
->subsys
[ss
->subsys_id
]);
4244 cg
->subsys
[ss
->subsys_id
] = NULL
;
4245 hhead
= css_set_hash(cg
->subsys
);
4246 hlist_add_head(&cg
->hlist
, hhead
);
4248 write_unlock(&css_set_lock
);
4251 * remove subsystem's css from the dummytop and free it - need to free
4252 * before marking as null because ss->destroy needs the cgrp->subsys
4253 * pointer to find their state. note that this also takes care of
4254 * freeing the css_id.
4256 ss
->destroy(ss
, dummytop
);
4257 dummytop
->subsys
[ss
->subsys_id
] = NULL
;
4259 mutex_unlock(&cgroup_mutex
);
4261 EXPORT_SYMBOL_GPL(cgroup_unload_subsys
);
4264 * cgroup_init_early - cgroup initialization at system boot
4266 * Initialize cgroups at system boot, and initialize any
4267 * subsystems that request early init.
4269 int __init
cgroup_init_early(void)
4272 atomic_set(&init_css_set
.refcount
, 1);
4273 INIT_LIST_HEAD(&init_css_set
.cg_links
);
4274 INIT_LIST_HEAD(&init_css_set
.tasks
);
4275 INIT_HLIST_NODE(&init_css_set
.hlist
);
4277 init_cgroup_root(&rootnode
);
4279 init_task
.cgroups
= &init_css_set
;
4281 init_css_set_link
.cg
= &init_css_set
;
4282 init_css_set_link
.cgrp
= dummytop
;
4283 list_add(&init_css_set_link
.cgrp_link_list
,
4284 &rootnode
.top_cgroup
.css_sets
);
4285 list_add(&init_css_set_link
.cg_link_list
,
4286 &init_css_set
.cg_links
);
4288 for (i
= 0; i
< CSS_SET_TABLE_SIZE
; i
++)
4289 INIT_HLIST_HEAD(&css_set_table
[i
]);
4291 /* at bootup time, we don't worry about modular subsystems */
4292 for (i
= 0; i
< CGROUP_BUILTIN_SUBSYS_COUNT
; i
++) {
4293 struct cgroup_subsys
*ss
= subsys
[i
];
4296 BUG_ON(strlen(ss
->name
) > MAX_CGROUP_TYPE_NAMELEN
);
4297 BUG_ON(!ss
->create
);
4298 BUG_ON(!ss
->destroy
);
4299 if (ss
->subsys_id
!= i
) {
4300 printk(KERN_ERR
"cgroup: Subsys %s id == %d\n",
4301 ss
->name
, ss
->subsys_id
);
4306 cgroup_init_subsys(ss
);
4312 * cgroup_init - cgroup initialization
4314 * Register cgroup filesystem and /proc file, and initialize
4315 * any subsystems that didn't request early init.
4317 int __init
cgroup_init(void)
4321 struct hlist_head
*hhead
;
4323 err
= bdi_init(&cgroup_backing_dev_info
);
4327 /* at bootup time, we don't worry about modular subsystems */
4328 for (i
= 0; i
< CGROUP_BUILTIN_SUBSYS_COUNT
; i
++) {
4329 struct cgroup_subsys
*ss
= subsys
[i
];
4330 if (!ss
->early_init
)
4331 cgroup_init_subsys(ss
);
4333 cgroup_init_idr(ss
, init_css_set
.subsys
[ss
->subsys_id
]);
4336 /* Add init_css_set to the hash table */
4337 hhead
= css_set_hash(init_css_set
.subsys
);
4338 hlist_add_head(&init_css_set
.hlist
, hhead
);
4339 BUG_ON(!init_root_id(&rootnode
));
4341 cgroup_kobj
= kobject_create_and_add("cgroup", fs_kobj
);
4347 err
= register_filesystem(&cgroup_fs_type
);
4349 kobject_put(cgroup_kobj
);
4353 proc_create("cgroups", 0, NULL
, &proc_cgroupstats_operations
);
4357 bdi_destroy(&cgroup_backing_dev_info
);
4363 * proc_cgroup_show()
4364 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4365 * - Used for /proc/<pid>/cgroup.
4366 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4367 * doesn't really matter if tsk->cgroup changes after we read it,
4368 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
4369 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4370 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4371 * cgroup to top_cgroup.
4374 /* TODO: Use a proper seq_file iterator */
4375 static int proc_cgroup_show(struct seq_file
*m
, void *v
)
4378 struct task_struct
*tsk
;
4381 struct cgroupfs_root
*root
;
4384 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
4390 tsk
= get_pid_task(pid
, PIDTYPE_PID
);
4396 mutex_lock(&cgroup_mutex
);
4398 for_each_active_root(root
) {
4399 struct cgroup_subsys
*ss
;
4400 struct cgroup
*cgrp
;
4403 seq_printf(m
, "%d:", root
->hierarchy_id
);
4404 for_each_subsys(root
, ss
)
4405 seq_printf(m
, "%s%s", count
++ ? "," : "", ss
->name
);
4406 if (strlen(root
->name
))
4407 seq_printf(m
, "%sname=%s", count
? "," : "",
4410 cgrp
= task_cgroup_from_root(tsk
, root
);
4411 retval
= cgroup_path(cgrp
, buf
, PAGE_SIZE
);
4419 mutex_unlock(&cgroup_mutex
);
4420 put_task_struct(tsk
);
4427 static int cgroup_open(struct inode
*inode
, struct file
*file
)
4429 struct pid
*pid
= PROC_I(inode
)->pid
;
4430 return single_open(file
, proc_cgroup_show
, pid
);
4433 const struct file_operations proc_cgroup_operations
= {
4434 .open
= cgroup_open
,
4436 .llseek
= seq_lseek
,
4437 .release
= single_release
,
4440 /* Display information about each subsystem and each hierarchy */
4441 static int proc_cgroupstats_show(struct seq_file
*m
, void *v
)
4445 seq_puts(m
, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
4447 * ideally we don't want subsystems moving around while we do this.
4448 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4449 * subsys/hierarchy state.
4451 mutex_lock(&cgroup_mutex
);
4452 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
4453 struct cgroup_subsys
*ss
= subsys
[i
];
4456 seq_printf(m
, "%s\t%d\t%d\t%d\n",
4457 ss
->name
, ss
->root
->hierarchy_id
,
4458 ss
->root
->number_of_cgroups
, !ss
->disabled
);
4460 mutex_unlock(&cgroup_mutex
);
4464 static int cgroupstats_open(struct inode
*inode
, struct file
*file
)
4466 return single_open(file
, proc_cgroupstats_show
, NULL
);
4469 static const struct file_operations proc_cgroupstats_operations
= {
4470 .open
= cgroupstats_open
,
4472 .llseek
= seq_lseek
,
4473 .release
= single_release
,
4477 * cgroup_fork - attach newly forked task to its parents cgroup.
4478 * @child: pointer to task_struct of forking parent process.
4480 * Description: A task inherits its parent's cgroup at fork().
4482 * A pointer to the shared css_set was automatically copied in
4483 * fork.c by dup_task_struct(). However, we ignore that copy, since
4484 * it was not made under the protection of RCU or cgroup_mutex, so
4485 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
4486 * have already changed current->cgroups, allowing the previously
4487 * referenced cgroup group to be removed and freed.
4489 * At the point that cgroup_fork() is called, 'current' is the parent
4490 * task, and the passed argument 'child' points to the child task.
4492 void cgroup_fork(struct task_struct
*child
)
4495 child
->cgroups
= current
->cgroups
;
4496 get_css_set(child
->cgroups
);
4497 task_unlock(current
);
4498 INIT_LIST_HEAD(&child
->cg_list
);
4502 * cgroup_fork_callbacks - run fork callbacks
4503 * @child: the new task
4505 * Called on a new task very soon before adding it to the
4506 * tasklist. No need to take any locks since no-one can
4507 * be operating on this task.
4509 void cgroup_fork_callbacks(struct task_struct
*child
)
4511 if (need_forkexit_callback
) {
4514 * forkexit callbacks are only supported for builtin
4515 * subsystems, and the builtin section of the subsys array is
4516 * immutable, so we don't need to lock the subsys array here.
4518 for (i
= 0; i
< CGROUP_BUILTIN_SUBSYS_COUNT
; i
++) {
4519 struct cgroup_subsys
*ss
= subsys
[i
];
4521 ss
->fork(ss
, child
);
4527 * cgroup_post_fork - called on a new task after adding it to the task list
4528 * @child: the task in question
4530 * Adds the task to the list running through its css_set if necessary.
4531 * Has to be after the task is visible on the task list in case we race
4532 * with the first call to cgroup_iter_start() - to guarantee that the
4533 * new task ends up on its list.
4535 void cgroup_post_fork(struct task_struct
*child
)
4537 if (use_task_css_set_links
) {
4538 write_lock(&css_set_lock
);
4540 if (list_empty(&child
->cg_list
))
4541 list_add(&child
->cg_list
, &child
->cgroups
->tasks
);
4543 write_unlock(&css_set_lock
);
4547 * cgroup_exit - detach cgroup from exiting task
4548 * @tsk: pointer to task_struct of exiting process
4549 * @run_callback: run exit callbacks?
4551 * Description: Detach cgroup from @tsk and release it.
4553 * Note that cgroups marked notify_on_release force every task in
4554 * them to take the global cgroup_mutex mutex when exiting.
4555 * This could impact scaling on very large systems. Be reluctant to
4556 * use notify_on_release cgroups where very high task exit scaling
4557 * is required on large systems.
4559 * the_top_cgroup_hack:
4561 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
4563 * We call cgroup_exit() while the task is still competent to
4564 * handle notify_on_release(), then leave the task attached to the
4565 * root cgroup in each hierarchy for the remainder of its exit.
4567 * To do this properly, we would increment the reference count on
4568 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
4569 * code we would add a second cgroup function call, to drop that
4570 * reference. This would just create an unnecessary hot spot on
4571 * the top_cgroup reference count, to no avail.
4573 * Normally, holding a reference to a cgroup without bumping its
4574 * count is unsafe. The cgroup could go away, or someone could
4575 * attach us to a different cgroup, decrementing the count on
4576 * the first cgroup that we never incremented. But in this case,
4577 * top_cgroup isn't going away, and either task has PF_EXITING set,
4578 * which wards off any cgroup_attach_task() attempts, or task is a failed
4579 * fork, never visible to cgroup_attach_task.
4581 void cgroup_exit(struct task_struct
*tsk
, int run_callbacks
)
4587 * Unlink from the css_set task list if necessary.
4588 * Optimistically check cg_list before taking
4591 if (!list_empty(&tsk
->cg_list
)) {
4592 write_lock(&css_set_lock
);
4593 if (!list_empty(&tsk
->cg_list
))
4594 list_del_init(&tsk
->cg_list
);
4595 write_unlock(&css_set_lock
);
4598 /* Reassign the task to the init_css_set. */
4601 tsk
->cgroups
= &init_css_set
;
4603 if (run_callbacks
&& need_forkexit_callback
) {
4605 * modular subsystems can't use callbacks, so no need to lock
4608 for (i
= 0; i
< CGROUP_BUILTIN_SUBSYS_COUNT
; i
++) {
4609 struct cgroup_subsys
*ss
= subsys
[i
];
4611 struct cgroup
*old_cgrp
=
4612 rcu_dereference_raw(cg
->subsys
[i
])->cgroup
;
4613 struct cgroup
*cgrp
= task_cgroup(tsk
, i
);
4614 ss
->exit(ss
, cgrp
, old_cgrp
, tsk
);
4621 put_css_set_taskexit(cg
);
4625 * cgroup_is_descendant - see if @cgrp is a descendant of @task's cgrp
4626 * @cgrp: the cgroup in question
4627 * @task: the task in question
4629 * See if @cgrp is a descendant of @task's cgroup in the appropriate
4632 * If we are sending in dummytop, then presumably we are creating
4633 * the top cgroup in the subsystem.
4635 * Called only by the ns (nsproxy) cgroup.
4637 int cgroup_is_descendant(const struct cgroup
*cgrp
, struct task_struct
*task
)
4640 struct cgroup
*target
;
4642 if (cgrp
== dummytop
)
4645 target
= task_cgroup_from_root(task
, cgrp
->root
);
4646 while (cgrp
!= target
&& cgrp
!= cgrp
->top_cgroup
)
4647 cgrp
= cgrp
->parent
;
4648 ret
= (cgrp
== target
);
4652 static void check_for_release(struct cgroup
*cgrp
)
4654 /* All of these checks rely on RCU to keep the cgroup
4655 * structure alive */
4656 if (cgroup_is_releasable(cgrp
) && !atomic_read(&cgrp
->count
)
4657 && list_empty(&cgrp
->children
) && !cgroup_has_css_refs(cgrp
)) {
4658 /* Control Group is currently removeable. If it's not
4659 * already queued for a userspace notification, queue
4661 int need_schedule_work
= 0;
4662 spin_lock(&release_list_lock
);
4663 if (!cgroup_is_removed(cgrp
) &&
4664 list_empty(&cgrp
->release_list
)) {
4665 list_add(&cgrp
->release_list
, &release_list
);
4666 need_schedule_work
= 1;
4668 spin_unlock(&release_list_lock
);
4669 if (need_schedule_work
)
4670 schedule_work(&release_agent_work
);
4674 /* Caller must verify that the css is not for root cgroup */
4675 void __css_put(struct cgroup_subsys_state
*css
, int count
)
4677 struct cgroup
*cgrp
= css
->cgroup
;
4680 val
= atomic_sub_return(count
, &css
->refcnt
);
4682 if (notify_on_release(cgrp
)) {
4683 set_bit(CGRP_RELEASABLE
, &cgrp
->flags
);
4684 check_for_release(cgrp
);
4686 cgroup_wakeup_rmdir_waiter(cgrp
);
4689 WARN_ON_ONCE(val
< 1);
4691 EXPORT_SYMBOL_GPL(__css_put
);
4694 * Notify userspace when a cgroup is released, by running the
4695 * configured release agent with the name of the cgroup (path
4696 * relative to the root of cgroup file system) as the argument.
4698 * Most likely, this user command will try to rmdir this cgroup.
4700 * This races with the possibility that some other task will be
4701 * attached to this cgroup before it is removed, or that some other
4702 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
4703 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
4704 * unused, and this cgroup will be reprieved from its death sentence,
4705 * to continue to serve a useful existence. Next time it's released,
4706 * we will get notified again, if it still has 'notify_on_release' set.
4708 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
4709 * means only wait until the task is successfully execve()'d. The
4710 * separate release agent task is forked by call_usermodehelper(),
4711 * then control in this thread returns here, without waiting for the
4712 * release agent task. We don't bother to wait because the caller of
4713 * this routine has no use for the exit status of the release agent
4714 * task, so no sense holding our caller up for that.
4716 static void cgroup_release_agent(struct work_struct
*work
)
4718 BUG_ON(work
!= &release_agent_work
);
4719 mutex_lock(&cgroup_mutex
);
4720 spin_lock(&release_list_lock
);
4721 while (!list_empty(&release_list
)) {
4722 char *argv
[3], *envp
[3];
4724 char *pathbuf
= NULL
, *agentbuf
= NULL
;
4725 struct cgroup
*cgrp
= list_entry(release_list
.next
,
4728 list_del_init(&cgrp
->release_list
);
4729 spin_unlock(&release_list_lock
);
4730 pathbuf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
4733 if (cgroup_path(cgrp
, pathbuf
, PAGE_SIZE
) < 0)
4735 agentbuf
= kstrdup(cgrp
->root
->release_agent_path
, GFP_KERNEL
);
4740 argv
[i
++] = agentbuf
;
4741 argv
[i
++] = pathbuf
;
4745 /* minimal command environment */
4746 envp
[i
++] = "HOME=/";
4747 envp
[i
++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
4750 /* Drop the lock while we invoke the usermode helper,
4751 * since the exec could involve hitting disk and hence
4752 * be a slow process */
4753 mutex_unlock(&cgroup_mutex
);
4754 call_usermodehelper(argv
[0], argv
, envp
, UMH_WAIT_EXEC
);
4755 mutex_lock(&cgroup_mutex
);
4759 spin_lock(&release_list_lock
);
4761 spin_unlock(&release_list_lock
);
4762 mutex_unlock(&cgroup_mutex
);
4765 static int __init
cgroup_disable(char *str
)
4770 while ((token
= strsep(&str
, ",")) != NULL
) {
4774 * cgroup_disable, being at boot time, can't know about module
4775 * subsystems, so we don't worry about them.
4777 for (i
= 0; i
< CGROUP_BUILTIN_SUBSYS_COUNT
; i
++) {
4778 struct cgroup_subsys
*ss
= subsys
[i
];
4780 if (!strcmp(token
, ss
->name
)) {
4782 printk(KERN_INFO
"Disabling %s control group"
4783 " subsystem\n", ss
->name
);
4790 __setup("cgroup_disable=", cgroup_disable
);
4793 * Functons for CSS ID.
4797 *To get ID other than 0, this should be called when !cgroup_is_removed().
4799 unsigned short css_id(struct cgroup_subsys_state
*css
)
4801 struct css_id
*cssid
;
4804 * This css_id() can return correct value when somone has refcnt
4805 * on this or this is under rcu_read_lock(). Once css->id is allocated,
4806 * it's unchanged until freed.
4808 cssid
= rcu_dereference_check(css
->id
,
4809 rcu_read_lock_held() || atomic_read(&css
->refcnt
));
4815 EXPORT_SYMBOL_GPL(css_id
);
4817 unsigned short css_depth(struct cgroup_subsys_state
*css
)
4819 struct css_id
*cssid
;
4821 cssid
= rcu_dereference_check(css
->id
,
4822 rcu_read_lock_held() || atomic_read(&css
->refcnt
));
4825 return cssid
->depth
;
4828 EXPORT_SYMBOL_GPL(css_depth
);
4831 * css_is_ancestor - test "root" css is an ancestor of "child"
4832 * @child: the css to be tested.
4833 * @root: the css supporsed to be an ancestor of the child.
4835 * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
4836 * this function reads css->id, this use rcu_dereference() and rcu_read_lock().
4837 * But, considering usual usage, the csses should be valid objects after test.
4838 * Assuming that the caller will do some action to the child if this returns
4839 * returns true, the caller must take "child";s reference count.
4840 * If "child" is valid object and this returns true, "root" is valid, too.
4843 bool css_is_ancestor(struct cgroup_subsys_state
*child
,
4844 const struct cgroup_subsys_state
*root
)
4846 struct css_id
*child_id
;
4847 struct css_id
*root_id
;
4851 child_id
= rcu_dereference(child
->id
);
4852 root_id
= rcu_dereference(root
->id
);
4855 || (child_id
->depth
< root_id
->depth
)
4856 || (child_id
->stack
[root_id
->depth
] != root_id
->id
))
4862 void free_css_id(struct cgroup_subsys
*ss
, struct cgroup_subsys_state
*css
)
4864 struct css_id
*id
= css
->id
;
4865 /* When this is called before css_id initialization, id can be NULL */
4869 BUG_ON(!ss
->use_id
);
4871 rcu_assign_pointer(id
->css
, NULL
);
4872 rcu_assign_pointer(css
->id
, NULL
);
4873 spin_lock(&ss
->id_lock
);
4874 idr_remove(&ss
->idr
, id
->id
);
4875 spin_unlock(&ss
->id_lock
);
4876 kfree_rcu(id
, rcu_head
);
4878 EXPORT_SYMBOL_GPL(free_css_id
);
4881 * This is called by init or create(). Then, calls to this function are
4882 * always serialized (By cgroup_mutex() at create()).
4885 static struct css_id
*get_new_cssid(struct cgroup_subsys
*ss
, int depth
)
4887 struct css_id
*newid
;
4888 int myid
, error
, size
;
4890 BUG_ON(!ss
->use_id
);
4892 size
= sizeof(*newid
) + sizeof(unsigned short) * (depth
+ 1);
4893 newid
= kzalloc(size
, GFP_KERNEL
);
4895 return ERR_PTR(-ENOMEM
);
4897 if (unlikely(!idr_pre_get(&ss
->idr
, GFP_KERNEL
))) {
4901 spin_lock(&ss
->id_lock
);
4902 /* Don't use 0. allocates an ID of 1-65535 */
4903 error
= idr_get_new_above(&ss
->idr
, newid
, 1, &myid
);
4904 spin_unlock(&ss
->id_lock
);
4906 /* Returns error when there are no free spaces for new ID.*/
4911 if (myid
> CSS_ID_MAX
)
4915 newid
->depth
= depth
;
4919 spin_lock(&ss
->id_lock
);
4920 idr_remove(&ss
->idr
, myid
);
4921 spin_unlock(&ss
->id_lock
);
4924 return ERR_PTR(error
);
4928 static int __init_or_module
cgroup_init_idr(struct cgroup_subsys
*ss
,
4929 struct cgroup_subsys_state
*rootcss
)
4931 struct css_id
*newid
;
4933 spin_lock_init(&ss
->id_lock
);
4936 newid
= get_new_cssid(ss
, 0);
4938 return PTR_ERR(newid
);
4940 newid
->stack
[0] = newid
->id
;
4941 newid
->css
= rootcss
;
4942 rootcss
->id
= newid
;
4946 static int alloc_css_id(struct cgroup_subsys
*ss
, struct cgroup
*parent
,
4947 struct cgroup
*child
)
4949 int subsys_id
, i
, depth
= 0;
4950 struct cgroup_subsys_state
*parent_css
, *child_css
;
4951 struct css_id
*child_id
, *parent_id
;
4953 subsys_id
= ss
->subsys_id
;
4954 parent_css
= parent
->subsys
[subsys_id
];
4955 child_css
= child
->subsys
[subsys_id
];
4956 parent_id
= parent_css
->id
;
4957 depth
= parent_id
->depth
+ 1;
4959 child_id
= get_new_cssid(ss
, depth
);
4960 if (IS_ERR(child_id
))
4961 return PTR_ERR(child_id
);
4963 for (i
= 0; i
< depth
; i
++)
4964 child_id
->stack
[i
] = parent_id
->stack
[i
];
4965 child_id
->stack
[depth
] = child_id
->id
;
4967 * child_id->css pointer will be set after this cgroup is available
4968 * see cgroup_populate_dir()
4970 rcu_assign_pointer(child_css
->id
, child_id
);
4976 * css_lookup - lookup css by id
4977 * @ss: cgroup subsys to be looked into.
4980 * Returns pointer to cgroup_subsys_state if there is valid one with id.
4981 * NULL if not. Should be called under rcu_read_lock()
4983 struct cgroup_subsys_state
*css_lookup(struct cgroup_subsys
*ss
, int id
)
4985 struct css_id
*cssid
= NULL
;
4987 BUG_ON(!ss
->use_id
);
4988 cssid
= idr_find(&ss
->idr
, id
);
4990 if (unlikely(!cssid
))
4993 return rcu_dereference(cssid
->css
);
4995 EXPORT_SYMBOL_GPL(css_lookup
);
4998 * css_get_next - lookup next cgroup under specified hierarchy.
4999 * @ss: pointer to subsystem
5000 * @id: current position of iteration.
5001 * @root: pointer to css. search tree under this.
5002 * @foundid: position of found object.
5004 * Search next css under the specified hierarchy of rootid. Calling under
5005 * rcu_read_lock() is necessary. Returns NULL if it reaches the end.
5007 struct cgroup_subsys_state
*
5008 css_get_next(struct cgroup_subsys
*ss
, int id
,
5009 struct cgroup_subsys_state
*root
, int *foundid
)
5011 struct cgroup_subsys_state
*ret
= NULL
;
5014 int rootid
= css_id(root
);
5015 int depth
= css_depth(root
);
5020 BUG_ON(!ss
->use_id
);
5021 /* fill start point for scan */
5025 * scan next entry from bitmap(tree), tmpid is updated after
5028 spin_lock(&ss
->id_lock
);
5029 tmp
= idr_get_next(&ss
->idr
, &tmpid
);
5030 spin_unlock(&ss
->id_lock
);
5034 if (tmp
->depth
>= depth
&& tmp
->stack
[depth
] == rootid
) {
5035 ret
= rcu_dereference(tmp
->css
);
5041 /* continue to scan from next id */
5048 * get corresponding css from file open on cgroupfs directory
5050 struct cgroup_subsys_state
*cgroup_css_from_dir(struct file
*f
, int id
)
5052 struct cgroup
*cgrp
;
5053 struct inode
*inode
;
5054 struct cgroup_subsys_state
*css
;
5056 inode
= f
->f_dentry
->d_inode
;
5057 /* check in cgroup filesystem dir */
5058 if (inode
->i_op
!= &cgroup_dir_inode_operations
)
5059 return ERR_PTR(-EBADF
);
5061 if (id
< 0 || id
>= CGROUP_SUBSYS_COUNT
)
5062 return ERR_PTR(-EINVAL
);
5065 cgrp
= __d_cgrp(f
->f_dentry
);
5066 css
= cgrp
->subsys
[id
];
5067 return css
? css
: ERR_PTR(-ENOENT
);
5070 #ifdef CONFIG_CGROUP_DEBUG
5071 static struct cgroup_subsys_state
*debug_create(struct cgroup_subsys
*ss
,
5072 struct cgroup
*cont
)
5074 struct cgroup_subsys_state
*css
= kzalloc(sizeof(*css
), GFP_KERNEL
);
5077 return ERR_PTR(-ENOMEM
);
5082 static void debug_destroy(struct cgroup_subsys
*ss
, struct cgroup
*cont
)
5084 kfree(cont
->subsys
[debug_subsys_id
]);
5087 static u64
cgroup_refcount_read(struct cgroup
*cont
, struct cftype
*cft
)
5089 return atomic_read(&cont
->count
);
5092 static u64
debug_taskcount_read(struct cgroup
*cont
, struct cftype
*cft
)
5094 return cgroup_task_count(cont
);
5097 static u64
current_css_set_read(struct cgroup
*cont
, struct cftype
*cft
)
5099 return (u64
)(unsigned long)current
->cgroups
;
5102 static u64
current_css_set_refcount_read(struct cgroup
*cont
,
5108 count
= atomic_read(¤t
->cgroups
->refcount
);
5113 static int current_css_set_cg_links_read(struct cgroup
*cont
,
5115 struct seq_file
*seq
)
5117 struct cg_cgroup_link
*link
;
5120 read_lock(&css_set_lock
);
5122 cg
= rcu_dereference(current
->cgroups
);
5123 list_for_each_entry(link
, &cg
->cg_links
, cg_link_list
) {
5124 struct cgroup
*c
= link
->cgrp
;
5128 name
= c
->dentry
->d_name
.name
;
5131 seq_printf(seq
, "Root %d group %s\n",
5132 c
->root
->hierarchy_id
, name
);
5135 read_unlock(&css_set_lock
);
5139 #define MAX_TASKS_SHOWN_PER_CSS 25
5140 static int cgroup_css_links_read(struct cgroup
*cont
,
5142 struct seq_file
*seq
)
5144 struct cg_cgroup_link
*link
;
5146 read_lock(&css_set_lock
);
5147 list_for_each_entry(link
, &cont
->css_sets
, cgrp_link_list
) {
5148 struct css_set
*cg
= link
->cg
;
5149 struct task_struct
*task
;
5151 seq_printf(seq
, "css_set %p\n", cg
);
5152 list_for_each_entry(task
, &cg
->tasks
, cg_list
) {
5153 if (count
++ > MAX_TASKS_SHOWN_PER_CSS
) {
5154 seq_puts(seq
, " ...\n");
5157 seq_printf(seq
, " task %d\n",
5158 task_pid_vnr(task
));
5162 read_unlock(&css_set_lock
);
5166 static u64
releasable_read(struct cgroup
*cgrp
, struct cftype
*cft
)
5168 return test_bit(CGRP_RELEASABLE
, &cgrp
->flags
);
5171 static struct cftype debug_files
[] = {
5173 .name
= "cgroup_refcount",
5174 .read_u64
= cgroup_refcount_read
,
5177 .name
= "taskcount",
5178 .read_u64
= debug_taskcount_read
,
5182 .name
= "current_css_set",
5183 .read_u64
= current_css_set_read
,
5187 .name
= "current_css_set_refcount",
5188 .read_u64
= current_css_set_refcount_read
,
5192 .name
= "current_css_set_cg_links",
5193 .read_seq_string
= current_css_set_cg_links_read
,
5197 .name
= "cgroup_css_links",
5198 .read_seq_string
= cgroup_css_links_read
,
5202 .name
= "releasable",
5203 .read_u64
= releasable_read
,
5207 static int debug_populate(struct cgroup_subsys
*ss
, struct cgroup
*cont
)
5209 return cgroup_add_files(cont
, ss
, debug_files
,
5210 ARRAY_SIZE(debug_files
));
5213 struct cgroup_subsys debug_subsys
= {
5215 .create
= debug_create
,
5216 .destroy
= debug_destroy
,
5217 .populate
= debug_populate
,
5218 .subsys_id
= debug_subsys_id
,
5220 #endif /* CONFIG_CGROUP_DEBUG */