2 * Generic process-grouping system.
4 * Based originally on the cpuset system, extracted by Paul Menage
5 * Copyright (C) 2006 Google, Inc
7 * Notifications support
8 * Copyright (C) 2009 Nokia Corporation
9 * Author: Kirill A. Shutemov
11 * Copyright notices from the original cpuset code:
12 * --------------------------------------------------
13 * Copyright (C) 2003 BULL SA.
14 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
16 * Portions derived from Patrick Mochel's sysfs code.
17 * sysfs is Copyright (c) 2001-3 Patrick Mochel
19 * 2003-10-10 Written by Simon Derr.
20 * 2003-10-22 Updates by Stephen Hemminger.
21 * 2004 May-July Rework by Paul Jackson.
22 * ---------------------------------------------------
24 * This file is subject to the terms and conditions of the GNU General Public
25 * License. See the file COPYING in the main directory of the Linux
26 * distribution for more details.
29 #include <linux/cgroup.h>
30 #include <linux/cred.h>
31 #include <linux/ctype.h>
32 #include <linux/errno.h>
34 #include <linux/init_task.h>
35 #include <linux/kernel.h>
36 #include <linux/list.h>
38 #include <linux/mutex.h>
39 #include <linux/mount.h>
40 #include <linux/pagemap.h>
41 #include <linux/proc_fs.h>
42 #include <linux/rcupdate.h>
43 #include <linux/sched.h>
44 #include <linux/backing-dev.h>
45 #include <linux/seq_file.h>
46 #include <linux/slab.h>
47 #include <linux/magic.h>
48 #include <linux/spinlock.h>
49 #include <linux/string.h>
50 #include <linux/sort.h>
51 #include <linux/kmod.h>
52 #include <linux/module.h>
53 #include <linux/delayacct.h>
54 #include <linux/cgroupstats.h>
55 #include <linux/hashtable.h>
56 #include <linux/namei.h>
57 #include <linux/pid_namespace.h>
58 #include <linux/idr.h>
59 #include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
60 #include <linux/eventfd.h>
61 #include <linux/poll.h>
62 #include <linux/flex_array.h> /* used in cgroup_attach_task */
63 #include <linux/kthread.h>
65 #include <linux/atomic.h>
67 /* css deactivation bias, makes css->refcnt negative to deny new trygets */
68 #define CSS_DEACT_BIAS INT_MIN
71 * cgroup_mutex is the master lock. Any modification to cgroup or its
72 * hierarchy must be performed while holding it.
74 * cgroup_root_mutex nests inside cgroup_mutex and should be held to modify
75 * cgroupfs_root of any cgroup hierarchy - subsys list, flags,
76 * release_agent_path and so on. Modifying requires both cgroup_mutex and
77 * cgroup_root_mutex. Readers can acquire either of the two. This is to
78 * break the following locking order cycle.
80 * A. cgroup_mutex -> cred_guard_mutex -> s_type->i_mutex_key -> namespace_sem
81 * B. namespace_sem -> cgroup_mutex
83 * B happens only through cgroup_show_options() and using cgroup_root_mutex
86 static DEFINE_MUTEX(cgroup_mutex
);
87 static DEFINE_MUTEX(cgroup_root_mutex
);
90 * Generate an array of cgroup subsystem pointers. At boot time, this is
91 * populated with the built in subsystems, and modular subsystems are
92 * registered after that. The mutable section of this array is protected by
95 #define SUBSYS(_x) [_x ## _subsys_id] = &_x ## _subsys,
96 #define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option)
97 static struct cgroup_subsys
*subsys
[CGROUP_SUBSYS_COUNT
] = {
98 #include <linux/cgroup_subsys.h>
101 #define MAX_CGROUP_ROOT_NAMELEN 64
104 * A cgroupfs_root represents the root of a cgroup hierarchy,
105 * and may be associated with a superblock to form an active
108 struct cgroupfs_root
{
109 struct super_block
*sb
;
112 * The bitmask of subsystems intended to be attached to this
115 unsigned long subsys_mask
;
117 /* Unique id for this hierarchy. */
120 /* The bitmask of subsystems currently attached to this hierarchy */
121 unsigned long actual_subsys_mask
;
123 /* A list running through the attached subsystems */
124 struct list_head subsys_list
;
126 /* The root cgroup for this hierarchy */
127 struct cgroup top_cgroup
;
129 /* Tracks how many cgroups are currently defined in hierarchy.*/
130 int number_of_cgroups
;
132 /* A list running through the active hierarchies */
133 struct list_head root_list
;
135 /* All cgroups on this root, cgroup_mutex protected */
136 struct list_head allcg_list
;
138 /* Hierarchy-specific flags */
141 /* IDs for cgroups in this hierarchy */
142 struct ida cgroup_ida
;
144 /* The path to use for release notifications. */
145 char release_agent_path
[PATH_MAX
];
147 /* The name for this hierarchy - may be empty */
148 char name
[MAX_CGROUP_ROOT_NAMELEN
];
152 * The "rootnode" hierarchy is the "dummy hierarchy", reserved for the
153 * subsystems that are otherwise unattached - it never has more than a
154 * single cgroup, and all tasks are part of that cgroup.
156 static struct cgroupfs_root rootnode
;
159 * cgroupfs file entry, pointed to from leaf dentry->d_fsdata.
162 struct list_head node
;
163 struct dentry
*dentry
;
168 * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when
169 * cgroup_subsys->use_id != 0.
171 #define CSS_ID_MAX (65535)
174 * The css to which this ID points. This pointer is set to valid value
175 * after cgroup is populated. If cgroup is removed, this will be NULL.
176 * This pointer is expected to be RCU-safe because destroy()
177 * is called after synchronize_rcu(). But for safe use, css_tryget()
178 * should be used for avoiding race.
180 struct cgroup_subsys_state __rcu
*css
;
186 * Depth in hierarchy which this ID belongs to.
188 unsigned short depth
;
190 * ID is freed by RCU. (and lookup routine is RCU safe.)
192 struct rcu_head rcu_head
;
194 * Hierarchy of CSS ID belongs to.
196 unsigned short stack
[0]; /* Array of Length (depth+1) */
200 * cgroup_event represents events which userspace want to receive.
202 struct cgroup_event
{
204 * Cgroup which the event belongs to.
208 * Control file which the event associated.
212 * eventfd to signal userspace about the event.
214 struct eventfd_ctx
*eventfd
;
216 * Each of these stored in a list by the cgroup.
218 struct list_head list
;
220 * All fields below needed to unregister event when
221 * userspace closes eventfd.
224 wait_queue_head_t
*wqh
;
226 struct work_struct remove
;
229 /* The list of hierarchy roots */
231 static LIST_HEAD(roots
);
232 static int root_count
;
234 static DEFINE_IDA(hierarchy_ida
);
235 static int next_hierarchy_id
;
236 static DEFINE_SPINLOCK(hierarchy_id_lock
);
238 /* dummytop is a shorthand for the dummy hierarchy's top cgroup */
239 #define dummytop (&rootnode.top_cgroup)
241 static struct cgroup_name root_cgroup_name
= { .name
= "/" };
243 /* This flag indicates whether tasks in the fork and exit paths should
244 * check for fork/exit handlers to call. This avoids us having to do
245 * extra work in the fork/exit path if none of the subsystems need to
248 static int need_forkexit_callback __read_mostly
;
250 static int cgroup_destroy_locked(struct cgroup
*cgrp
);
251 static int cgroup_addrm_files(struct cgroup
*cgrp
, struct cgroup_subsys
*subsys
,
252 struct cftype cfts
[], bool is_add
);
254 #ifdef CONFIG_PROVE_LOCKING
255 int cgroup_lock_is_held(void)
257 return lockdep_is_held(&cgroup_mutex
);
259 #else /* #ifdef CONFIG_PROVE_LOCKING */
260 int cgroup_lock_is_held(void)
262 return mutex_is_locked(&cgroup_mutex
);
264 #endif /* #else #ifdef CONFIG_PROVE_LOCKING */
266 EXPORT_SYMBOL_GPL(cgroup_lock_is_held
);
268 static int css_unbias_refcnt(int refcnt
)
270 return refcnt
>= 0 ? refcnt
: refcnt
- CSS_DEACT_BIAS
;
273 /* the current nr of refs, always >= 0 whether @css is deactivated or not */
274 static int css_refcnt(struct cgroup_subsys_state
*css
)
276 int v
= atomic_read(&css
->refcnt
);
278 return css_unbias_refcnt(v
);
281 /* convenient tests for these bits */
282 inline int cgroup_is_removed(const struct cgroup
*cgrp
)
284 return test_bit(CGRP_REMOVED
, &cgrp
->flags
);
287 /* bits in struct cgroupfs_root flags field */
289 ROOT_NOPREFIX
, /* mounted subsystems have no named prefix */
290 ROOT_XATTR
, /* supports extended attributes */
293 static int cgroup_is_releasable(const struct cgroup
*cgrp
)
296 (1 << CGRP_RELEASABLE
) |
297 (1 << CGRP_NOTIFY_ON_RELEASE
);
298 return (cgrp
->flags
& bits
) == bits
;
301 static int notify_on_release(const struct cgroup
*cgrp
)
303 return test_bit(CGRP_NOTIFY_ON_RELEASE
, &cgrp
->flags
);
307 * for_each_subsys() allows you to iterate on each subsystem attached to
308 * an active hierarchy
310 #define for_each_subsys(_root, _ss) \
311 list_for_each_entry(_ss, &_root->subsys_list, sibling)
313 /* for_each_active_root() allows you to iterate across the active hierarchies */
314 #define for_each_active_root(_root) \
315 list_for_each_entry(_root, &roots, root_list)
317 static inline struct cgroup
*__d_cgrp(struct dentry
*dentry
)
319 return dentry
->d_fsdata
;
322 static inline struct cfent
*__d_cfe(struct dentry
*dentry
)
324 return dentry
->d_fsdata
;
327 static inline struct cftype
*__d_cft(struct dentry
*dentry
)
329 return __d_cfe(dentry
)->type
;
332 /* the list of cgroups eligible for automatic release. Protected by
333 * release_list_lock */
334 static LIST_HEAD(release_list
);
335 static DEFINE_RAW_SPINLOCK(release_list_lock
);
336 static void cgroup_release_agent(struct work_struct
*work
);
337 static DECLARE_WORK(release_agent_work
, cgroup_release_agent
);
338 static void check_for_release(struct cgroup
*cgrp
);
340 /* Link structure for associating css_set objects with cgroups */
341 struct cg_cgroup_link
{
343 * List running through cg_cgroup_links associated with a
344 * cgroup, anchored on cgroup->css_sets
346 struct list_head cgrp_link_list
;
349 * List running through cg_cgroup_links pointing at a
350 * single css_set object, anchored on css_set->cg_links
352 struct list_head cg_link_list
;
356 /* The default css_set - used by init and its children prior to any
357 * hierarchies being mounted. It contains a pointer to the root state
358 * for each subsystem. Also used to anchor the list of css_sets. Not
359 * reference-counted, to improve performance when child cgroups
360 * haven't been created.
363 static struct css_set init_css_set
;
364 static struct cg_cgroup_link init_css_set_link
;
366 static int cgroup_init_idr(struct cgroup_subsys
*ss
,
367 struct cgroup_subsys_state
*css
);
369 /* css_set_lock protects the list of css_set objects, and the
370 * chain of tasks off each css_set. Nests outside task->alloc_lock
371 * due to cgroup_iter_start() */
372 static DEFINE_RWLOCK(css_set_lock
);
373 static int css_set_count
;
376 * hash table for cgroup groups. This improves the performance to find
377 * an existing css_set. This hash doesn't (currently) take into
378 * account cgroups in empty hierarchies.
380 #define CSS_SET_HASH_BITS 7
381 static DEFINE_HASHTABLE(css_set_table
, CSS_SET_HASH_BITS
);
383 static unsigned long css_set_hash(struct cgroup_subsys_state
*css
[])
386 unsigned long key
= 0UL;
388 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++)
389 key
+= (unsigned long)css
[i
];
390 key
= (key
>> 16) ^ key
;
395 /* We don't maintain the lists running through each css_set to its
396 * task until after the first call to cgroup_iter_start(). This
397 * reduces the fork()/exit() overhead for people who have cgroups
398 * compiled into their kernel but not actually in use */
399 static int use_task_css_set_links __read_mostly
;
401 static void __put_css_set(struct css_set
*cg
, int taskexit
)
403 struct cg_cgroup_link
*link
;
404 struct cg_cgroup_link
*saved_link
;
406 * Ensure that the refcount doesn't hit zero while any readers
407 * can see it. Similar to atomic_dec_and_lock(), but for an
410 if (atomic_add_unless(&cg
->refcount
, -1, 1))
412 write_lock(&css_set_lock
);
413 if (!atomic_dec_and_test(&cg
->refcount
)) {
414 write_unlock(&css_set_lock
);
418 /* This css_set is dead. unlink it and release cgroup refcounts */
419 hash_del(&cg
->hlist
);
422 list_for_each_entry_safe(link
, saved_link
, &cg
->cg_links
,
424 struct cgroup
*cgrp
= link
->cgrp
;
425 list_del(&link
->cg_link_list
);
426 list_del(&link
->cgrp_link_list
);
429 * We may not be holding cgroup_mutex, and if cgrp->count is
430 * dropped to 0 the cgroup can be destroyed at any time, hence
431 * rcu_read_lock is used to keep it alive.
434 if (atomic_dec_and_test(&cgrp
->count
) &&
435 notify_on_release(cgrp
)) {
437 set_bit(CGRP_RELEASABLE
, &cgrp
->flags
);
438 check_for_release(cgrp
);
445 write_unlock(&css_set_lock
);
446 kfree_rcu(cg
, rcu_head
);
450 * refcounted get/put for css_set objects
452 static inline void get_css_set(struct css_set
*cg
)
454 atomic_inc(&cg
->refcount
);
457 static inline void put_css_set(struct css_set
*cg
)
459 __put_css_set(cg
, 0);
462 static inline void put_css_set_taskexit(struct css_set
*cg
)
464 __put_css_set(cg
, 1);
468 * compare_css_sets - helper function for find_existing_css_set().
469 * @cg: candidate css_set being tested
470 * @old_cg: existing css_set for a task
471 * @new_cgrp: cgroup that's being entered by the task
472 * @template: desired set of css pointers in css_set (pre-calculated)
474 * Returns true if "cg" matches "old_cg" except for the hierarchy
475 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
477 static bool compare_css_sets(struct css_set
*cg
,
478 struct css_set
*old_cg
,
479 struct cgroup
*new_cgrp
,
480 struct cgroup_subsys_state
*template[])
482 struct list_head
*l1
, *l2
;
484 if (memcmp(template, cg
->subsys
, sizeof(cg
->subsys
))) {
485 /* Not all subsystems matched */
490 * Compare cgroup pointers in order to distinguish between
491 * different cgroups in heirarchies with no subsystems. We
492 * could get by with just this check alone (and skip the
493 * memcmp above) but on most setups the memcmp check will
494 * avoid the need for this more expensive check on almost all
499 l2
= &old_cg
->cg_links
;
501 struct cg_cgroup_link
*cgl1
, *cgl2
;
502 struct cgroup
*cg1
, *cg2
;
506 /* See if we reached the end - both lists are equal length. */
507 if (l1
== &cg
->cg_links
) {
508 BUG_ON(l2
!= &old_cg
->cg_links
);
511 BUG_ON(l2
== &old_cg
->cg_links
);
513 /* Locate the cgroups associated with these links. */
514 cgl1
= list_entry(l1
, struct cg_cgroup_link
, cg_link_list
);
515 cgl2
= list_entry(l2
, struct cg_cgroup_link
, cg_link_list
);
518 /* Hierarchies should be linked in the same order. */
519 BUG_ON(cg1
->root
!= cg2
->root
);
522 * If this hierarchy is the hierarchy of the cgroup
523 * that's changing, then we need to check that this
524 * css_set points to the new cgroup; if it's any other
525 * hierarchy, then this css_set should point to the
526 * same cgroup as the old css_set.
528 if (cg1
->root
== new_cgrp
->root
) {
540 * find_existing_css_set() is a helper for
541 * find_css_set(), and checks to see whether an existing
542 * css_set is suitable.
544 * oldcg: the cgroup group that we're using before the cgroup
547 * cgrp: the cgroup that we're moving into
549 * template: location in which to build the desired set of subsystem
550 * state objects for the new cgroup group
552 static struct css_set
*find_existing_css_set(
553 struct css_set
*oldcg
,
555 struct cgroup_subsys_state
*template[])
558 struct cgroupfs_root
*root
= cgrp
->root
;
563 * Build the set of subsystem state objects that we want to see in the
564 * new css_set. while subsystems can change globally, the entries here
565 * won't change, so no need for locking.
567 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
568 if (root
->subsys_mask
& (1UL << i
)) {
569 /* Subsystem is in this hierarchy. So we want
570 * the subsystem state from the new
572 template[i
] = cgrp
->subsys
[i
];
574 /* Subsystem is not in this hierarchy, so we
575 * don't want to change the subsystem state */
576 template[i
] = oldcg
->subsys
[i
];
580 key
= css_set_hash(template);
581 hash_for_each_possible(css_set_table
, cg
, hlist
, key
) {
582 if (!compare_css_sets(cg
, oldcg
, cgrp
, template))
585 /* This css_set matches what we need */
589 /* No existing cgroup group matched */
593 static void free_cg_links(struct list_head
*tmp
)
595 struct cg_cgroup_link
*link
;
596 struct cg_cgroup_link
*saved_link
;
598 list_for_each_entry_safe(link
, saved_link
, tmp
, cgrp_link_list
) {
599 list_del(&link
->cgrp_link_list
);
605 * allocate_cg_links() allocates "count" cg_cgroup_link structures
606 * and chains them on tmp through their cgrp_link_list fields. Returns 0 on
607 * success or a negative error
609 static int allocate_cg_links(int count
, struct list_head
*tmp
)
611 struct cg_cgroup_link
*link
;
614 for (i
= 0; i
< count
; i
++) {
615 link
= kmalloc(sizeof(*link
), GFP_KERNEL
);
620 list_add(&link
->cgrp_link_list
, tmp
);
626 * link_css_set - a helper function to link a css_set to a cgroup
627 * @tmp_cg_links: cg_cgroup_link objects allocated by allocate_cg_links()
628 * @cg: the css_set to be linked
629 * @cgrp: the destination cgroup
631 static void link_css_set(struct list_head
*tmp_cg_links
,
632 struct css_set
*cg
, struct cgroup
*cgrp
)
634 struct cg_cgroup_link
*link
;
636 BUG_ON(list_empty(tmp_cg_links
));
637 link
= list_first_entry(tmp_cg_links
, struct cg_cgroup_link
,
641 atomic_inc(&cgrp
->count
);
642 list_move(&link
->cgrp_link_list
, &cgrp
->css_sets
);
644 * Always add links to the tail of the list so that the list
645 * is sorted by order of hierarchy creation
647 list_add_tail(&link
->cg_link_list
, &cg
->cg_links
);
651 * find_css_set() takes an existing cgroup group and a
652 * cgroup object, and returns a css_set object that's
653 * equivalent to the old group, but with the given cgroup
654 * substituted into the appropriate hierarchy. Must be called with
657 static struct css_set
*find_css_set(
658 struct css_set
*oldcg
, struct cgroup
*cgrp
)
661 struct cgroup_subsys_state
*template[CGROUP_SUBSYS_COUNT
];
663 struct list_head tmp_cg_links
;
665 struct cg_cgroup_link
*link
;
668 /* First see if we already have a cgroup group that matches
670 read_lock(&css_set_lock
);
671 res
= find_existing_css_set(oldcg
, cgrp
, template);
674 read_unlock(&css_set_lock
);
679 res
= kmalloc(sizeof(*res
), GFP_KERNEL
);
683 /* Allocate all the cg_cgroup_link objects that we'll need */
684 if (allocate_cg_links(root_count
, &tmp_cg_links
) < 0) {
689 atomic_set(&res
->refcount
, 1);
690 INIT_LIST_HEAD(&res
->cg_links
);
691 INIT_LIST_HEAD(&res
->tasks
);
692 INIT_HLIST_NODE(&res
->hlist
);
694 /* Copy the set of subsystem state objects generated in
695 * find_existing_css_set() */
696 memcpy(res
->subsys
, template, sizeof(res
->subsys
));
698 write_lock(&css_set_lock
);
699 /* Add reference counts and links from the new css_set. */
700 list_for_each_entry(link
, &oldcg
->cg_links
, cg_link_list
) {
701 struct cgroup
*c
= link
->cgrp
;
702 if (c
->root
== cgrp
->root
)
704 link_css_set(&tmp_cg_links
, res
, c
);
707 BUG_ON(!list_empty(&tmp_cg_links
));
711 /* Add this cgroup group to the hash table */
712 key
= css_set_hash(res
->subsys
);
713 hash_add(css_set_table
, &res
->hlist
, key
);
715 write_unlock(&css_set_lock
);
721 * Return the cgroup for "task" from the given hierarchy. Must be
722 * called with cgroup_mutex held.
724 static struct cgroup
*task_cgroup_from_root(struct task_struct
*task
,
725 struct cgroupfs_root
*root
)
728 struct cgroup
*res
= NULL
;
730 BUG_ON(!mutex_is_locked(&cgroup_mutex
));
731 read_lock(&css_set_lock
);
733 * No need to lock the task - since we hold cgroup_mutex the
734 * task can't change groups, so the only thing that can happen
735 * is that it exits and its css is set back to init_css_set.
738 if (css
== &init_css_set
) {
739 res
= &root
->top_cgroup
;
741 struct cg_cgroup_link
*link
;
742 list_for_each_entry(link
, &css
->cg_links
, cg_link_list
) {
743 struct cgroup
*c
= link
->cgrp
;
744 if (c
->root
== root
) {
750 read_unlock(&css_set_lock
);
756 * There is one global cgroup mutex. We also require taking
757 * task_lock() when dereferencing a task's cgroup subsys pointers.
758 * See "The task_lock() exception", at the end of this comment.
760 * A task must hold cgroup_mutex to modify cgroups.
762 * Any task can increment and decrement the count field without lock.
763 * So in general, code holding cgroup_mutex can't rely on the count
764 * field not changing. However, if the count goes to zero, then only
765 * cgroup_attach_task() can increment it again. Because a count of zero
766 * means that no tasks are currently attached, therefore there is no
767 * way a task attached to that cgroup can fork (the other way to
768 * increment the count). So code holding cgroup_mutex can safely
769 * assume that if the count is zero, it will stay zero. Similarly, if
770 * a task holds cgroup_mutex on a cgroup with zero count, it
771 * knows that the cgroup won't be removed, as cgroup_rmdir()
774 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
775 * (usually) take cgroup_mutex. These are the two most performance
776 * critical pieces of code here. The exception occurs on cgroup_exit(),
777 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
778 * is taken, and if the cgroup count is zero, a usermode call made
779 * to the release agent with the name of the cgroup (path relative to
780 * the root of cgroup file system) as the argument.
782 * A cgroup can only be deleted if both its 'count' of using tasks
783 * is zero, and its list of 'children' cgroups is empty. Since all
784 * tasks in the system use _some_ cgroup, and since there is always at
785 * least one task in the system (init, pid == 1), therefore, top_cgroup
786 * always has either children cgroups and/or using tasks. So we don't
787 * need a special hack to ensure that top_cgroup cannot be deleted.
789 * The task_lock() exception
791 * The need for this exception arises from the action of
792 * cgroup_attach_task(), which overwrites one task's cgroup pointer with
793 * another. It does so using cgroup_mutex, however there are
794 * several performance critical places that need to reference
795 * task->cgroup without the expense of grabbing a system global
796 * mutex. Therefore except as noted below, when dereferencing or, as
797 * in cgroup_attach_task(), modifying a task's cgroup pointer we use
798 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
799 * the task_struct routinely used for such matters.
801 * P.S. One more locking exception. RCU is used to guard the
802 * update of a tasks cgroup pointer by cgroup_attach_task()
806 * cgroup_lock - lock out any changes to cgroup structures
809 void cgroup_lock(void)
811 mutex_lock(&cgroup_mutex
);
813 EXPORT_SYMBOL_GPL(cgroup_lock
);
816 * cgroup_unlock - release lock on cgroup changes
818 * Undo the lock taken in a previous cgroup_lock() call.
820 void cgroup_unlock(void)
822 mutex_unlock(&cgroup_mutex
);
824 EXPORT_SYMBOL_GPL(cgroup_unlock
);
827 * A couple of forward declarations required, due to cyclic reference loop:
828 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
829 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
833 static int cgroup_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
);
834 static struct dentry
*cgroup_lookup(struct inode
*, struct dentry
*, unsigned int);
835 static int cgroup_rmdir(struct inode
*unused_dir
, struct dentry
*dentry
);
836 static int cgroup_populate_dir(struct cgroup
*cgrp
, bool base_files
,
837 unsigned long subsys_mask
);
838 static const struct inode_operations cgroup_dir_inode_operations
;
839 static const struct file_operations proc_cgroupstats_operations
;
841 static struct backing_dev_info cgroup_backing_dev_info
= {
843 .capabilities
= BDI_CAP_NO_ACCT_AND_WRITEBACK
,
846 static int alloc_css_id(struct cgroup_subsys
*ss
,
847 struct cgroup
*parent
, struct cgroup
*child
);
849 static struct inode
*cgroup_new_inode(umode_t mode
, struct super_block
*sb
)
851 struct inode
*inode
= new_inode(sb
);
854 inode
->i_ino
= get_next_ino();
855 inode
->i_mode
= mode
;
856 inode
->i_uid
= current_fsuid();
857 inode
->i_gid
= current_fsgid();
858 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
859 inode
->i_mapping
->backing_dev_info
= &cgroup_backing_dev_info
;
864 static struct cgroup_name
*cgroup_alloc_name(struct dentry
*dentry
)
866 struct cgroup_name
*name
;
868 name
= kmalloc(sizeof(*name
) + dentry
->d_name
.len
+ 1, GFP_KERNEL
);
871 strcpy(name
->name
, dentry
->d_name
.name
);
875 static void cgroup_free_fn(struct work_struct
*work
)
877 struct cgroup
*cgrp
= container_of(work
, struct cgroup
, free_work
);
878 struct cgroup_subsys
*ss
;
880 mutex_lock(&cgroup_mutex
);
882 * Release the subsystem state objects.
884 for_each_subsys(cgrp
->root
, ss
)
887 cgrp
->root
->number_of_cgroups
--;
888 mutex_unlock(&cgroup_mutex
);
891 * Drop the active superblock reference that we took when we
894 deactivate_super(cgrp
->root
->sb
);
897 * if we're getting rid of the cgroup, refcount should ensure
898 * that there are no pidlists left.
900 BUG_ON(!list_empty(&cgrp
->pidlists
));
902 simple_xattrs_free(&cgrp
->xattrs
);
904 ida_simple_remove(&cgrp
->root
->cgroup_ida
, cgrp
->id
);
905 kfree(rcu_dereference_raw(cgrp
->name
));
909 static void cgroup_free_rcu(struct rcu_head
*head
)
911 struct cgroup
*cgrp
= container_of(head
, struct cgroup
, rcu_head
);
913 schedule_work(&cgrp
->free_work
);
916 static void cgroup_diput(struct dentry
*dentry
, struct inode
*inode
)
918 /* is dentry a directory ? if so, kfree() associated cgroup */
919 if (S_ISDIR(inode
->i_mode
)) {
920 struct cgroup
*cgrp
= dentry
->d_fsdata
;
922 BUG_ON(!(cgroup_is_removed(cgrp
)));
923 call_rcu(&cgrp
->rcu_head
, cgroup_free_rcu
);
925 struct cfent
*cfe
= __d_cfe(dentry
);
926 struct cgroup
*cgrp
= dentry
->d_parent
->d_fsdata
;
927 struct cftype
*cft
= cfe
->type
;
929 WARN_ONCE(!list_empty(&cfe
->node
) &&
930 cgrp
!= &cgrp
->root
->top_cgroup
,
931 "cfe still linked for %s\n", cfe
->type
->name
);
933 simple_xattrs_free(&cft
->xattrs
);
938 static int cgroup_delete(const struct dentry
*d
)
943 static void remove_dir(struct dentry
*d
)
945 struct dentry
*parent
= dget(d
->d_parent
);
948 simple_rmdir(parent
->d_inode
, d
);
952 static void cgroup_rm_file(struct cgroup
*cgrp
, const struct cftype
*cft
)
956 lockdep_assert_held(&cgrp
->dentry
->d_inode
->i_mutex
);
957 lockdep_assert_held(&cgroup_mutex
);
960 * If we're doing cleanup due to failure of cgroup_create(),
961 * the corresponding @cfe may not exist.
963 list_for_each_entry(cfe
, &cgrp
->files
, node
) {
964 struct dentry
*d
= cfe
->dentry
;
966 if (cft
&& cfe
->type
!= cft
)
971 simple_unlink(cgrp
->dentry
->d_inode
, d
);
972 list_del_init(&cfe
->node
);
980 * cgroup_clear_directory - selective removal of base and subsystem files
981 * @dir: directory containing the files
982 * @base_files: true if the base files should be removed
983 * @subsys_mask: mask of the subsystem ids whose files should be removed
985 static void cgroup_clear_directory(struct dentry
*dir
, bool base_files
,
986 unsigned long subsys_mask
)
988 struct cgroup
*cgrp
= __d_cgrp(dir
);
989 struct cgroup_subsys
*ss
;
991 for_each_subsys(cgrp
->root
, ss
) {
992 struct cftype_set
*set
;
993 if (!test_bit(ss
->subsys_id
, &subsys_mask
))
995 list_for_each_entry(set
, &ss
->cftsets
, node
)
996 cgroup_addrm_files(cgrp
, NULL
, set
->cfts
, false);
999 while (!list_empty(&cgrp
->files
))
1000 cgroup_rm_file(cgrp
, NULL
);
1005 * NOTE : the dentry must have been dget()'ed
1007 static void cgroup_d_remove_dir(struct dentry
*dentry
)
1009 struct dentry
*parent
;
1010 struct cgroupfs_root
*root
= dentry
->d_sb
->s_fs_info
;
1012 cgroup_clear_directory(dentry
, true, root
->subsys_mask
);
1014 parent
= dentry
->d_parent
;
1015 spin_lock(&parent
->d_lock
);
1016 spin_lock_nested(&dentry
->d_lock
, DENTRY_D_LOCK_NESTED
);
1017 list_del_init(&dentry
->d_u
.d_child
);
1018 spin_unlock(&dentry
->d_lock
);
1019 spin_unlock(&parent
->d_lock
);
1024 * Call with cgroup_mutex held. Drops reference counts on modules, including
1025 * any duplicate ones that parse_cgroupfs_options took. If this function
1026 * returns an error, no reference counts are touched.
1028 static int rebind_subsystems(struct cgroupfs_root
*root
,
1029 unsigned long final_subsys_mask
)
1031 unsigned long added_mask
, removed_mask
;
1032 struct cgroup
*cgrp
= &root
->top_cgroup
;
1035 BUG_ON(!mutex_is_locked(&cgroup_mutex
));
1036 BUG_ON(!mutex_is_locked(&cgroup_root_mutex
));
1038 removed_mask
= root
->actual_subsys_mask
& ~final_subsys_mask
;
1039 added_mask
= final_subsys_mask
& ~root
->actual_subsys_mask
;
1040 /* Check that any added subsystems are currently free */
1041 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
1042 unsigned long bit
= 1UL << i
;
1043 struct cgroup_subsys
*ss
= subsys
[i
];
1044 if (!(bit
& added_mask
))
1047 * Nobody should tell us to do a subsys that doesn't exist:
1048 * parse_cgroupfs_options should catch that case and refcounts
1049 * ensure that subsystems won't disappear once selected.
1052 if (ss
->root
!= &rootnode
) {
1053 /* Subsystem isn't free */
1058 /* Currently we don't handle adding/removing subsystems when
1059 * any child cgroups exist. This is theoretically supportable
1060 * but involves complex error handling, so it's being left until
1062 if (root
->number_of_cgroups
> 1)
1065 /* Process each subsystem */
1066 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
1067 struct cgroup_subsys
*ss
= subsys
[i
];
1068 unsigned long bit
= 1UL << i
;
1069 if (bit
& added_mask
) {
1070 /* We're binding this subsystem to this hierarchy */
1072 BUG_ON(cgrp
->subsys
[i
]);
1073 BUG_ON(!dummytop
->subsys
[i
]);
1074 BUG_ON(dummytop
->subsys
[i
]->cgroup
!= dummytop
);
1075 cgrp
->subsys
[i
] = dummytop
->subsys
[i
];
1076 cgrp
->subsys
[i
]->cgroup
= cgrp
;
1077 list_move(&ss
->sibling
, &root
->subsys_list
);
1081 /* refcount was already taken, and we're keeping it */
1082 } else if (bit
& removed_mask
) {
1083 /* We're removing this subsystem */
1085 BUG_ON(cgrp
->subsys
[i
] != dummytop
->subsys
[i
]);
1086 BUG_ON(cgrp
->subsys
[i
]->cgroup
!= cgrp
);
1089 dummytop
->subsys
[i
]->cgroup
= dummytop
;
1090 cgrp
->subsys
[i
] = NULL
;
1091 subsys
[i
]->root
= &rootnode
;
1092 list_move(&ss
->sibling
, &rootnode
.subsys_list
);
1093 /* subsystem is now free - drop reference on module */
1094 module_put(ss
->module
);
1095 } else if (bit
& final_subsys_mask
) {
1096 /* Subsystem state should already exist */
1098 BUG_ON(!cgrp
->subsys
[i
]);
1100 * a refcount was taken, but we already had one, so
1101 * drop the extra reference.
1103 module_put(ss
->module
);
1104 #ifdef CONFIG_MODULE_UNLOAD
1105 BUG_ON(ss
->module
&& !module_refcount(ss
->module
));
1108 /* Subsystem state shouldn't exist */
1109 BUG_ON(cgrp
->subsys
[i
]);
1112 root
->subsys_mask
= root
->actual_subsys_mask
= final_subsys_mask
;
1117 static int cgroup_show_options(struct seq_file
*seq
, struct dentry
*dentry
)
1119 struct cgroupfs_root
*root
= dentry
->d_sb
->s_fs_info
;
1120 struct cgroup_subsys
*ss
;
1122 mutex_lock(&cgroup_root_mutex
);
1123 for_each_subsys(root
, ss
)
1124 seq_printf(seq
, ",%s", ss
->name
);
1125 if (test_bit(ROOT_NOPREFIX
, &root
->flags
))
1126 seq_puts(seq
, ",noprefix");
1127 if (test_bit(ROOT_XATTR
, &root
->flags
))
1128 seq_puts(seq
, ",xattr");
1129 if (strlen(root
->release_agent_path
))
1130 seq_printf(seq
, ",release_agent=%s", root
->release_agent_path
);
1131 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN
, &root
->top_cgroup
.flags
))
1132 seq_puts(seq
, ",clone_children");
1133 if (strlen(root
->name
))
1134 seq_printf(seq
, ",name=%s", root
->name
);
1135 mutex_unlock(&cgroup_root_mutex
);
1139 struct cgroup_sb_opts
{
1140 unsigned long subsys_mask
;
1141 unsigned long flags
;
1142 char *release_agent
;
1143 bool cpuset_clone_children
;
1145 /* User explicitly requested empty subsystem */
1148 struct cgroupfs_root
*new_root
;
1153 * Convert a hierarchy specifier into a bitmask of subsystems and flags. Call
1154 * with cgroup_mutex held to protect the subsys[] array. This function takes
1155 * refcounts on subsystems to be used, unless it returns error, in which case
1156 * no refcounts are taken.
1158 static int parse_cgroupfs_options(char *data
, struct cgroup_sb_opts
*opts
)
1160 char *token
, *o
= data
;
1161 bool all_ss
= false, one_ss
= false;
1162 unsigned long mask
= (unsigned long)-1;
1164 bool module_pin_failed
= false;
1166 BUG_ON(!mutex_is_locked(&cgroup_mutex
));
1168 #ifdef CONFIG_CPUSETS
1169 mask
= ~(1UL << cpuset_subsys_id
);
1172 memset(opts
, 0, sizeof(*opts
));
1174 while ((token
= strsep(&o
, ",")) != NULL
) {
1177 if (!strcmp(token
, "none")) {
1178 /* Explicitly have no subsystems */
1182 if (!strcmp(token
, "all")) {
1183 /* Mutually exclusive option 'all' + subsystem name */
1189 if (!strcmp(token
, "noprefix")) {
1190 set_bit(ROOT_NOPREFIX
, &opts
->flags
);
1193 if (!strcmp(token
, "clone_children")) {
1194 opts
->cpuset_clone_children
= true;
1197 if (!strcmp(token
, "xattr")) {
1198 set_bit(ROOT_XATTR
, &opts
->flags
);
1201 if (!strncmp(token
, "release_agent=", 14)) {
1202 /* Specifying two release agents is forbidden */
1203 if (opts
->release_agent
)
1205 opts
->release_agent
=
1206 kstrndup(token
+ 14, PATH_MAX
- 1, GFP_KERNEL
);
1207 if (!opts
->release_agent
)
1211 if (!strncmp(token
, "name=", 5)) {
1212 const char *name
= token
+ 5;
1213 /* Can't specify an empty name */
1216 /* Must match [\w.-]+ */
1217 for (i
= 0; i
< strlen(name
); i
++) {
1221 if ((c
== '.') || (c
== '-') || (c
== '_'))
1225 /* Specifying two names is forbidden */
1228 opts
->name
= kstrndup(name
,
1229 MAX_CGROUP_ROOT_NAMELEN
- 1,
1237 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
1238 struct cgroup_subsys
*ss
= subsys
[i
];
1241 if (strcmp(token
, ss
->name
))
1246 /* Mutually exclusive option 'all' + subsystem name */
1249 set_bit(i
, &opts
->subsys_mask
);
1254 if (i
== CGROUP_SUBSYS_COUNT
)
1259 * If the 'all' option was specified select all the subsystems,
1260 * otherwise if 'none', 'name=' and a subsystem name options
1261 * were not specified, let's default to 'all'
1263 if (all_ss
|| (!one_ss
&& !opts
->none
&& !opts
->name
)) {
1264 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
1265 struct cgroup_subsys
*ss
= subsys
[i
];
1270 set_bit(i
, &opts
->subsys_mask
);
1274 /* Consistency checks */
1277 * Option noprefix was introduced just for backward compatibility
1278 * with the old cpuset, so we allow noprefix only if mounting just
1279 * the cpuset subsystem.
1281 if (test_bit(ROOT_NOPREFIX
, &opts
->flags
) &&
1282 (opts
->subsys_mask
& mask
))
1286 /* Can't specify "none" and some subsystems */
1287 if (opts
->subsys_mask
&& opts
->none
)
1291 * We either have to specify by name or by subsystems. (So all
1292 * empty hierarchies must have a name).
1294 if (!opts
->subsys_mask
&& !opts
->name
)
1298 * Grab references on all the modules we'll need, so the subsystems
1299 * don't dance around before rebind_subsystems attaches them. This may
1300 * take duplicate reference counts on a subsystem that's already used,
1301 * but rebind_subsystems handles this case.
1303 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
1304 unsigned long bit
= 1UL << i
;
1306 if (!(bit
& opts
->subsys_mask
))
1308 if (!try_module_get(subsys
[i
]->module
)) {
1309 module_pin_failed
= true;
1313 if (module_pin_failed
) {
1315 * oops, one of the modules was going away. this means that we
1316 * raced with a module_delete call, and to the user this is
1317 * essentially a "subsystem doesn't exist" case.
1319 for (i
--; i
>= 0; i
--) {
1320 /* drop refcounts only on the ones we took */
1321 unsigned long bit
= 1UL << i
;
1323 if (!(bit
& opts
->subsys_mask
))
1325 module_put(subsys
[i
]->module
);
1333 static void drop_parsed_module_refcounts(unsigned long subsys_mask
)
1336 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
1337 unsigned long bit
= 1UL << i
;
1339 if (!(bit
& subsys_mask
))
1341 module_put(subsys
[i
]->module
);
1345 static int cgroup_remount(struct super_block
*sb
, int *flags
, char *data
)
1348 struct cgroupfs_root
*root
= sb
->s_fs_info
;
1349 struct cgroup
*cgrp
= &root
->top_cgroup
;
1350 struct cgroup_sb_opts opts
;
1351 unsigned long added_mask
, removed_mask
;
1353 mutex_lock(&cgrp
->dentry
->d_inode
->i_mutex
);
1354 mutex_lock(&cgroup_mutex
);
1355 mutex_lock(&cgroup_root_mutex
);
1357 /* See what subsystems are wanted */
1358 ret
= parse_cgroupfs_options(data
, &opts
);
1362 if (opts
.subsys_mask
!= root
->actual_subsys_mask
|| opts
.release_agent
)
1363 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1364 task_tgid_nr(current
), current
->comm
);
1366 added_mask
= opts
.subsys_mask
& ~root
->subsys_mask
;
1367 removed_mask
= root
->subsys_mask
& ~opts
.subsys_mask
;
1369 /* Don't allow flags or name to change at remount */
1370 if (opts
.flags
!= root
->flags
||
1371 (opts
.name
&& strcmp(opts
.name
, root
->name
))) {
1373 drop_parsed_module_refcounts(opts
.subsys_mask
);
1378 * Clear out the files of subsystems that should be removed, do
1379 * this before rebind_subsystems, since rebind_subsystems may
1380 * change this hierarchy's subsys_list.
1382 cgroup_clear_directory(cgrp
->dentry
, false, removed_mask
);
1384 ret
= rebind_subsystems(root
, opts
.subsys_mask
);
1386 /* rebind_subsystems failed, re-populate the removed files */
1387 cgroup_populate_dir(cgrp
, false, removed_mask
);
1388 drop_parsed_module_refcounts(opts
.subsys_mask
);
1392 /* re-populate subsystem files */
1393 cgroup_populate_dir(cgrp
, false, added_mask
);
1395 if (opts
.release_agent
)
1396 strcpy(root
->release_agent_path
, opts
.release_agent
);
1398 kfree(opts
.release_agent
);
1400 mutex_unlock(&cgroup_root_mutex
);
1401 mutex_unlock(&cgroup_mutex
);
1402 mutex_unlock(&cgrp
->dentry
->d_inode
->i_mutex
);
1406 static const struct super_operations cgroup_ops
= {
1407 .statfs
= simple_statfs
,
1408 .drop_inode
= generic_delete_inode
,
1409 .show_options
= cgroup_show_options
,
1410 .remount_fs
= cgroup_remount
,
1413 static void init_cgroup_housekeeping(struct cgroup
*cgrp
)
1415 INIT_LIST_HEAD(&cgrp
->sibling
);
1416 INIT_LIST_HEAD(&cgrp
->children
);
1417 INIT_LIST_HEAD(&cgrp
->files
);
1418 INIT_LIST_HEAD(&cgrp
->css_sets
);
1419 INIT_LIST_HEAD(&cgrp
->allcg_node
);
1420 INIT_LIST_HEAD(&cgrp
->release_list
);
1421 INIT_LIST_HEAD(&cgrp
->pidlists
);
1422 INIT_WORK(&cgrp
->free_work
, cgroup_free_fn
);
1423 mutex_init(&cgrp
->pidlist_mutex
);
1424 INIT_LIST_HEAD(&cgrp
->event_list
);
1425 spin_lock_init(&cgrp
->event_list_lock
);
1426 simple_xattrs_init(&cgrp
->xattrs
);
1429 static void init_cgroup_root(struct cgroupfs_root
*root
)
1431 struct cgroup
*cgrp
= &root
->top_cgroup
;
1433 INIT_LIST_HEAD(&root
->subsys_list
);
1434 INIT_LIST_HEAD(&root
->root_list
);
1435 INIT_LIST_HEAD(&root
->allcg_list
);
1436 root
->number_of_cgroups
= 1;
1438 cgrp
->name
= &root_cgroup_name
;
1439 cgrp
->top_cgroup
= cgrp
;
1440 init_cgroup_housekeeping(cgrp
);
1441 list_add_tail(&cgrp
->allcg_node
, &root
->allcg_list
);
1444 static bool init_root_id(struct cgroupfs_root
*root
)
1449 if (!ida_pre_get(&hierarchy_ida
, GFP_KERNEL
))
1451 spin_lock(&hierarchy_id_lock
);
1452 /* Try to allocate the next unused ID */
1453 ret
= ida_get_new_above(&hierarchy_ida
, next_hierarchy_id
,
1454 &root
->hierarchy_id
);
1456 /* Try again starting from 0 */
1457 ret
= ida_get_new(&hierarchy_ida
, &root
->hierarchy_id
);
1459 next_hierarchy_id
= root
->hierarchy_id
+ 1;
1460 } else if (ret
!= -EAGAIN
) {
1461 /* Can only get here if the 31-bit IDR is full ... */
1464 spin_unlock(&hierarchy_id_lock
);
1469 static int cgroup_test_super(struct super_block
*sb
, void *data
)
1471 struct cgroup_sb_opts
*opts
= data
;
1472 struct cgroupfs_root
*root
= sb
->s_fs_info
;
1474 /* If we asked for a name then it must match */
1475 if (opts
->name
&& strcmp(opts
->name
, root
->name
))
1479 * If we asked for subsystems (or explicitly for no
1480 * subsystems) then they must match
1482 if ((opts
->subsys_mask
|| opts
->none
)
1483 && (opts
->subsys_mask
!= root
->subsys_mask
))
1489 static struct cgroupfs_root
*cgroup_root_from_opts(struct cgroup_sb_opts
*opts
)
1491 struct cgroupfs_root
*root
;
1493 if (!opts
->subsys_mask
&& !opts
->none
)
1496 root
= kzalloc(sizeof(*root
), GFP_KERNEL
);
1498 return ERR_PTR(-ENOMEM
);
1500 if (!init_root_id(root
)) {
1502 return ERR_PTR(-ENOMEM
);
1504 init_cgroup_root(root
);
1506 root
->subsys_mask
= opts
->subsys_mask
;
1507 root
->flags
= opts
->flags
;
1508 ida_init(&root
->cgroup_ida
);
1509 if (opts
->release_agent
)
1510 strcpy(root
->release_agent_path
, opts
->release_agent
);
1512 strcpy(root
->name
, opts
->name
);
1513 if (opts
->cpuset_clone_children
)
1514 set_bit(CGRP_CPUSET_CLONE_CHILDREN
, &root
->top_cgroup
.flags
);
1518 static void cgroup_drop_root(struct cgroupfs_root
*root
)
1523 BUG_ON(!root
->hierarchy_id
);
1524 spin_lock(&hierarchy_id_lock
);
1525 ida_remove(&hierarchy_ida
, root
->hierarchy_id
);
1526 spin_unlock(&hierarchy_id_lock
);
1527 ida_destroy(&root
->cgroup_ida
);
1531 static int cgroup_set_super(struct super_block
*sb
, void *data
)
1534 struct cgroup_sb_opts
*opts
= data
;
1536 /* If we don't have a new root, we can't set up a new sb */
1537 if (!opts
->new_root
)
1540 BUG_ON(!opts
->subsys_mask
&& !opts
->none
);
1542 ret
= set_anon_super(sb
, NULL
);
1546 sb
->s_fs_info
= opts
->new_root
;
1547 opts
->new_root
->sb
= sb
;
1549 sb
->s_blocksize
= PAGE_CACHE_SIZE
;
1550 sb
->s_blocksize_bits
= PAGE_CACHE_SHIFT
;
1551 sb
->s_magic
= CGROUP_SUPER_MAGIC
;
1552 sb
->s_op
= &cgroup_ops
;
1557 static int cgroup_get_rootdir(struct super_block
*sb
)
1559 static const struct dentry_operations cgroup_dops
= {
1560 .d_iput
= cgroup_diput
,
1561 .d_delete
= cgroup_delete
,
1564 struct inode
*inode
=
1565 cgroup_new_inode(S_IFDIR
| S_IRUGO
| S_IXUGO
| S_IWUSR
, sb
);
1570 inode
->i_fop
= &simple_dir_operations
;
1571 inode
->i_op
= &cgroup_dir_inode_operations
;
1572 /* directories start off with i_nlink == 2 (for "." entry) */
1574 sb
->s_root
= d_make_root(inode
);
1577 /* for everything else we want ->d_op set */
1578 sb
->s_d_op
= &cgroup_dops
;
1582 static struct dentry
*cgroup_mount(struct file_system_type
*fs_type
,
1583 int flags
, const char *unused_dev_name
,
1586 struct cgroup_sb_opts opts
;
1587 struct cgroupfs_root
*root
;
1589 struct super_block
*sb
;
1590 struct cgroupfs_root
*new_root
;
1591 struct inode
*inode
;
1593 /* First find the desired set of subsystems */
1594 mutex_lock(&cgroup_mutex
);
1595 ret
= parse_cgroupfs_options(data
, &opts
);
1596 mutex_unlock(&cgroup_mutex
);
1601 * Allocate a new cgroup root. We may not need it if we're
1602 * reusing an existing hierarchy.
1604 new_root
= cgroup_root_from_opts(&opts
);
1605 if (IS_ERR(new_root
)) {
1606 ret
= PTR_ERR(new_root
);
1609 opts
.new_root
= new_root
;
1611 /* Locate an existing or new sb for this hierarchy */
1612 sb
= sget(fs_type
, cgroup_test_super
, cgroup_set_super
, 0, &opts
);
1615 cgroup_drop_root(opts
.new_root
);
1619 root
= sb
->s_fs_info
;
1621 if (root
== opts
.new_root
) {
1622 /* We used the new root structure, so this is a new hierarchy */
1623 struct list_head tmp_cg_links
;
1624 struct cgroup
*root_cgrp
= &root
->top_cgroup
;
1625 struct cgroupfs_root
*existing_root
;
1626 const struct cred
*cred
;
1630 BUG_ON(sb
->s_root
!= NULL
);
1632 ret
= cgroup_get_rootdir(sb
);
1634 goto drop_new_super
;
1635 inode
= sb
->s_root
->d_inode
;
1637 mutex_lock(&inode
->i_mutex
);
1638 mutex_lock(&cgroup_mutex
);
1639 mutex_lock(&cgroup_root_mutex
);
1641 /* Check for name clashes with existing mounts */
1643 if (strlen(root
->name
))
1644 for_each_active_root(existing_root
)
1645 if (!strcmp(existing_root
->name
, root
->name
))
1649 * We're accessing css_set_count without locking
1650 * css_set_lock here, but that's OK - it can only be
1651 * increased by someone holding cgroup_lock, and
1652 * that's us. The worst that can happen is that we
1653 * have some link structures left over
1655 ret
= allocate_cg_links(css_set_count
, &tmp_cg_links
);
1659 ret
= rebind_subsystems(root
, root
->subsys_mask
);
1660 if (ret
== -EBUSY
) {
1661 free_cg_links(&tmp_cg_links
);
1665 * There must be no failure case after here, since rebinding
1666 * takes care of subsystems' refcounts, which are explicitly
1667 * dropped in the failure exit path.
1670 /* EBUSY should be the only error here */
1673 list_add(&root
->root_list
, &roots
);
1676 sb
->s_root
->d_fsdata
= root_cgrp
;
1677 root
->top_cgroup
.dentry
= sb
->s_root
;
1679 /* Link the top cgroup in this hierarchy into all
1680 * the css_set objects */
1681 write_lock(&css_set_lock
);
1682 hash_for_each(css_set_table
, i
, cg
, hlist
)
1683 link_css_set(&tmp_cg_links
, cg
, root_cgrp
);
1684 write_unlock(&css_set_lock
);
1686 free_cg_links(&tmp_cg_links
);
1688 BUG_ON(!list_empty(&root_cgrp
->children
));
1689 BUG_ON(root
->number_of_cgroups
!= 1);
1691 cred
= override_creds(&init_cred
);
1692 cgroup_populate_dir(root_cgrp
, true, root
->subsys_mask
);
1694 mutex_unlock(&cgroup_root_mutex
);
1695 mutex_unlock(&cgroup_mutex
);
1696 mutex_unlock(&inode
->i_mutex
);
1699 * We re-used an existing hierarchy - the new root (if
1700 * any) is not needed
1702 cgroup_drop_root(opts
.new_root
);
1703 /* no subsys rebinding, so refcounts don't change */
1704 drop_parsed_module_refcounts(opts
.subsys_mask
);
1707 kfree(opts
.release_agent
);
1709 return dget(sb
->s_root
);
1712 mutex_unlock(&cgroup_root_mutex
);
1713 mutex_unlock(&cgroup_mutex
);
1714 mutex_unlock(&inode
->i_mutex
);
1716 deactivate_locked_super(sb
);
1718 drop_parsed_module_refcounts(opts
.subsys_mask
);
1720 kfree(opts
.release_agent
);
1722 return ERR_PTR(ret
);
1725 static void cgroup_kill_sb(struct super_block
*sb
) {
1726 struct cgroupfs_root
*root
= sb
->s_fs_info
;
1727 struct cgroup
*cgrp
= &root
->top_cgroup
;
1729 struct cg_cgroup_link
*link
;
1730 struct cg_cgroup_link
*saved_link
;
1734 BUG_ON(root
->number_of_cgroups
!= 1);
1735 BUG_ON(!list_empty(&cgrp
->children
));
1737 mutex_lock(&cgroup_mutex
);
1738 mutex_lock(&cgroup_root_mutex
);
1740 /* Rebind all subsystems back to the default hierarchy */
1741 ret
= rebind_subsystems(root
, 0);
1742 /* Shouldn't be able to fail ... */
1746 * Release all the links from css_sets to this hierarchy's
1749 write_lock(&css_set_lock
);
1751 list_for_each_entry_safe(link
, saved_link
, &cgrp
->css_sets
,
1753 list_del(&link
->cg_link_list
);
1754 list_del(&link
->cgrp_link_list
);
1757 write_unlock(&css_set_lock
);
1759 if (!list_empty(&root
->root_list
)) {
1760 list_del(&root
->root_list
);
1764 mutex_unlock(&cgroup_root_mutex
);
1765 mutex_unlock(&cgroup_mutex
);
1767 simple_xattrs_free(&cgrp
->xattrs
);
1769 kill_litter_super(sb
);
1770 cgroup_drop_root(root
);
1773 static struct file_system_type cgroup_fs_type
= {
1775 .mount
= cgroup_mount
,
1776 .kill_sb
= cgroup_kill_sb
,
1779 static struct kobject
*cgroup_kobj
;
1782 * cgroup_path - generate the path of a cgroup
1783 * @cgrp: the cgroup in question
1784 * @buf: the buffer to write the path into
1785 * @buflen: the length of the buffer
1787 * Writes path of cgroup into buf. Returns 0 on success, -errno on error.
1789 * We can't generate cgroup path using dentry->d_name, as accessing
1790 * dentry->name must be protected by irq-unsafe dentry->d_lock or parent
1791 * inode's i_mutex, while on the other hand cgroup_path() can be called
1792 * with some irq-safe spinlocks held.
1794 int cgroup_path(const struct cgroup
*cgrp
, char *buf
, int buflen
)
1796 int ret
= -ENAMETOOLONG
;
1799 start
= buf
+ buflen
- 1;
1804 const char *name
= cgroup_name(cgrp
);
1808 if ((start
-= len
) < buf
)
1810 memcpy(start
, name
, len
);
1819 cgrp
= cgrp
->parent
;
1822 memmove(buf
, start
, buf
+ buflen
- start
);
1827 EXPORT_SYMBOL_GPL(cgroup_path
);
1830 * Control Group taskset
1832 struct task_and_cgroup
{
1833 struct task_struct
*task
;
1834 struct cgroup
*cgrp
;
1838 struct cgroup_taskset
{
1839 struct task_and_cgroup single
;
1840 struct flex_array
*tc_array
;
1843 struct cgroup
*cur_cgrp
;
1847 * cgroup_taskset_first - reset taskset and return the first task
1848 * @tset: taskset of interest
1850 * @tset iteration is initialized and the first task is returned.
1852 struct task_struct
*cgroup_taskset_first(struct cgroup_taskset
*tset
)
1854 if (tset
->tc_array
) {
1856 return cgroup_taskset_next(tset
);
1858 tset
->cur_cgrp
= tset
->single
.cgrp
;
1859 return tset
->single
.task
;
1862 EXPORT_SYMBOL_GPL(cgroup_taskset_first
);
1865 * cgroup_taskset_next - iterate to the next task in taskset
1866 * @tset: taskset of interest
1868 * Return the next task in @tset. Iteration must have been initialized
1869 * with cgroup_taskset_first().
1871 struct task_struct
*cgroup_taskset_next(struct cgroup_taskset
*tset
)
1873 struct task_and_cgroup
*tc
;
1875 if (!tset
->tc_array
|| tset
->idx
>= tset
->tc_array_len
)
1878 tc
= flex_array_get(tset
->tc_array
, tset
->idx
++);
1879 tset
->cur_cgrp
= tc
->cgrp
;
1882 EXPORT_SYMBOL_GPL(cgroup_taskset_next
);
1885 * cgroup_taskset_cur_cgroup - return the matching cgroup for the current task
1886 * @tset: taskset of interest
1888 * Return the cgroup for the current (last returned) task of @tset. This
1889 * function must be preceded by either cgroup_taskset_first() or
1890 * cgroup_taskset_next().
1892 struct cgroup
*cgroup_taskset_cur_cgroup(struct cgroup_taskset
*tset
)
1894 return tset
->cur_cgrp
;
1896 EXPORT_SYMBOL_GPL(cgroup_taskset_cur_cgroup
);
1899 * cgroup_taskset_size - return the number of tasks in taskset
1900 * @tset: taskset of interest
1902 int cgroup_taskset_size(struct cgroup_taskset
*tset
)
1904 return tset
->tc_array
? tset
->tc_array_len
: 1;
1906 EXPORT_SYMBOL_GPL(cgroup_taskset_size
);
1910 * cgroup_task_migrate - move a task from one cgroup to another.
1912 * Must be called with cgroup_mutex and threadgroup locked.
1914 static void cgroup_task_migrate(struct cgroup
*oldcgrp
,
1915 struct task_struct
*tsk
, struct css_set
*newcg
)
1917 struct css_set
*oldcg
;
1920 * We are synchronized through threadgroup_lock() against PF_EXITING
1921 * setting such that we can't race against cgroup_exit() changing the
1922 * css_set to init_css_set and dropping the old one.
1924 WARN_ON_ONCE(tsk
->flags
& PF_EXITING
);
1925 oldcg
= tsk
->cgroups
;
1928 rcu_assign_pointer(tsk
->cgroups
, newcg
);
1931 /* Update the css_set linked lists if we're using them */
1932 write_lock(&css_set_lock
);
1933 if (!list_empty(&tsk
->cg_list
))
1934 list_move(&tsk
->cg_list
, &newcg
->tasks
);
1935 write_unlock(&css_set_lock
);
1938 * We just gained a reference on oldcg by taking it from the task. As
1939 * trading it for newcg is protected by cgroup_mutex, we're safe to drop
1940 * it here; it will be freed under RCU.
1942 set_bit(CGRP_RELEASABLE
, &oldcgrp
->flags
);
1947 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
1948 * @from: attach to all cgroups of a given task
1949 * @tsk: the task to be attached
1951 int cgroup_attach_task_all(struct task_struct
*from
, struct task_struct
*tsk
)
1953 struct cgroupfs_root
*root
;
1957 for_each_active_root(root
) {
1958 struct cgroup
*from_cg
= task_cgroup_from_root(from
, root
);
1960 retval
= cgroup_attach_task(from_cg
, tsk
, false);
1968 EXPORT_SYMBOL_GPL(cgroup_attach_task_all
);
1971 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
1972 * @cgrp: the cgroup to attach to
1973 * @tsk: the task or the leader of the threadgroup to be attached
1974 * @threadgroup: attach the whole threadgroup?
1976 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
1977 * task_lock of @tsk or each thread in the threadgroup individually in turn.
1979 int cgroup_attach_task(struct cgroup
*cgrp
, struct task_struct
*tsk
,
1982 int retval
, i
, group_size
;
1983 struct cgroup_subsys
*ss
, *failed_ss
= NULL
;
1984 struct cgroupfs_root
*root
= cgrp
->root
;
1985 /* threadgroup list cursor and array */
1986 struct task_struct
*leader
= tsk
;
1987 struct task_and_cgroup
*tc
;
1988 struct flex_array
*group
;
1989 struct cgroup_taskset tset
= { };
1992 * step 0: in order to do expensive, possibly blocking operations for
1993 * every thread, we cannot iterate the thread group list, since it needs
1994 * rcu or tasklist locked. instead, build an array of all threads in the
1995 * group - group_rwsem prevents new threads from appearing, and if
1996 * threads exit, this will just be an over-estimate.
1999 group_size
= get_nr_threads(tsk
);
2002 /* flex_array supports very large thread-groups better than kmalloc. */
2003 group
= flex_array_alloc(sizeof(*tc
), group_size
, GFP_KERNEL
);
2006 /* pre-allocate to guarantee space while iterating in rcu read-side. */
2007 retval
= flex_array_prealloc(group
, 0, group_size
, GFP_KERNEL
);
2009 goto out_free_group_list
;
2013 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2014 * already PF_EXITING could be freed from underneath us unless we
2015 * take an rcu_read_lock.
2019 struct task_and_cgroup ent
;
2021 /* @tsk either already exited or can't exit until the end */
2022 if (tsk
->flags
& PF_EXITING
)
2025 /* as per above, nr_threads may decrease, but not increase. */
2026 BUG_ON(i
>= group_size
);
2028 ent
.cgrp
= task_cgroup_from_root(tsk
, root
);
2029 /* nothing to do if this task is already in the cgroup */
2030 if (ent
.cgrp
== cgrp
)
2033 * saying GFP_ATOMIC has no effect here because we did prealloc
2034 * earlier, but it's good form to communicate our expectations.
2036 retval
= flex_array_put(group
, i
, &ent
, GFP_ATOMIC
);
2037 BUG_ON(retval
!= 0);
2042 } while_each_thread(leader
, tsk
);
2044 /* remember the number of threads in the array for later. */
2046 tset
.tc_array
= group
;
2047 tset
.tc_array_len
= group_size
;
2049 /* methods shouldn't be called if no task is actually migrating */
2052 goto out_free_group_list
;
2055 * step 1: check that we can legitimately attach to the cgroup.
2057 for_each_subsys(root
, ss
) {
2058 if (ss
->can_attach
) {
2059 retval
= ss
->can_attach(cgrp
, &tset
);
2062 goto out_cancel_attach
;
2068 * step 2: make sure css_sets exist for all threads to be migrated.
2069 * we use find_css_set, which allocates a new one if necessary.
2071 for (i
= 0; i
< group_size
; i
++) {
2072 tc
= flex_array_get(group
, i
);
2073 tc
->cg
= find_css_set(tc
->task
->cgroups
, cgrp
);
2076 goto out_put_css_set_refs
;
2081 * step 3: now that we're guaranteed success wrt the css_sets,
2082 * proceed to move all tasks to the new cgroup. There are no
2083 * failure cases after here, so this is the commit point.
2085 for (i
= 0; i
< group_size
; i
++) {
2086 tc
= flex_array_get(group
, i
);
2087 cgroup_task_migrate(tc
->cgrp
, tc
->task
, tc
->cg
);
2089 /* nothing is sensitive to fork() after this point. */
2092 * step 4: do subsystem attach callbacks.
2094 for_each_subsys(root
, ss
) {
2096 ss
->attach(cgrp
, &tset
);
2100 * step 5: success! and cleanup
2103 out_put_css_set_refs
:
2105 for (i
= 0; i
< group_size
; i
++) {
2106 tc
= flex_array_get(group
, i
);
2109 put_css_set(tc
->cg
);
2114 for_each_subsys(root
, ss
) {
2115 if (ss
== failed_ss
)
2117 if (ss
->cancel_attach
)
2118 ss
->cancel_attach(cgrp
, &tset
);
2121 out_free_group_list
:
2122 flex_array_free(group
);
2127 * Find the task_struct of the task to attach by vpid and pass it along to the
2128 * function to attach either it or all tasks in its threadgroup. Will lock
2129 * cgroup_mutex and threadgroup; may take task_lock of task.
2131 static int attach_task_by_pid(struct cgroup
*cgrp
, u64 pid
, bool threadgroup
)
2133 struct task_struct
*tsk
;
2134 const struct cred
*cred
= current_cred(), *tcred
;
2137 if (!cgroup_lock_live_group(cgrp
))
2143 tsk
= find_task_by_vpid(pid
);
2147 goto out_unlock_cgroup
;
2150 * even if we're attaching all tasks in the thread group, we
2151 * only need to check permissions on one of them.
2153 tcred
= __task_cred(tsk
);
2154 if (!uid_eq(cred
->euid
, GLOBAL_ROOT_UID
) &&
2155 !uid_eq(cred
->euid
, tcred
->uid
) &&
2156 !uid_eq(cred
->euid
, tcred
->suid
)) {
2159 goto out_unlock_cgroup
;
2165 tsk
= tsk
->group_leader
;
2168 * Workqueue threads may acquire PF_THREAD_BOUND and become
2169 * trapped in a cpuset, or RT worker may be born in a cgroup
2170 * with no rt_runtime allocated. Just say no.
2172 if (tsk
== kthreadd_task
|| (tsk
->flags
& PF_THREAD_BOUND
)) {
2175 goto out_unlock_cgroup
;
2178 get_task_struct(tsk
);
2181 threadgroup_lock(tsk
);
2183 if (!thread_group_leader(tsk
)) {
2185 * a race with de_thread from another thread's exec()
2186 * may strip us of our leadership, if this happens,
2187 * there is no choice but to throw this task away and
2188 * try again; this is
2189 * "double-double-toil-and-trouble-check locking".
2191 threadgroup_unlock(tsk
);
2192 put_task_struct(tsk
);
2193 goto retry_find_task
;
2197 ret
= cgroup_attach_task(cgrp
, tsk
, threadgroup
);
2199 threadgroup_unlock(tsk
);
2201 put_task_struct(tsk
);
2207 static int cgroup_tasks_write(struct cgroup
*cgrp
, struct cftype
*cft
, u64 pid
)
2209 return attach_task_by_pid(cgrp
, pid
, false);
2212 static int cgroup_procs_write(struct cgroup
*cgrp
, struct cftype
*cft
, u64 tgid
)
2214 return attach_task_by_pid(cgrp
, tgid
, true);
2218 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
2219 * @cgrp: the cgroup to be checked for liveness
2221 * On success, returns true; the lock should be later released with
2222 * cgroup_unlock(). On failure returns false with no lock held.
2224 bool cgroup_lock_live_group(struct cgroup
*cgrp
)
2226 mutex_lock(&cgroup_mutex
);
2227 if (cgroup_is_removed(cgrp
)) {
2228 mutex_unlock(&cgroup_mutex
);
2233 EXPORT_SYMBOL_GPL(cgroup_lock_live_group
);
2235 static int cgroup_release_agent_write(struct cgroup
*cgrp
, struct cftype
*cft
,
2238 BUILD_BUG_ON(sizeof(cgrp
->root
->release_agent_path
) < PATH_MAX
);
2239 if (strlen(buffer
) >= PATH_MAX
)
2241 if (!cgroup_lock_live_group(cgrp
))
2243 mutex_lock(&cgroup_root_mutex
);
2244 strcpy(cgrp
->root
->release_agent_path
, buffer
);
2245 mutex_unlock(&cgroup_root_mutex
);
2250 static int cgroup_release_agent_show(struct cgroup
*cgrp
, struct cftype
*cft
,
2251 struct seq_file
*seq
)
2253 if (!cgroup_lock_live_group(cgrp
))
2255 seq_puts(seq
, cgrp
->root
->release_agent_path
);
2256 seq_putc(seq
, '\n');
2261 /* A buffer size big enough for numbers or short strings */
2262 #define CGROUP_LOCAL_BUFFER_SIZE 64
2264 static ssize_t
cgroup_write_X64(struct cgroup
*cgrp
, struct cftype
*cft
,
2266 const char __user
*userbuf
,
2267 size_t nbytes
, loff_t
*unused_ppos
)
2269 char buffer
[CGROUP_LOCAL_BUFFER_SIZE
];
2275 if (nbytes
>= sizeof(buffer
))
2277 if (copy_from_user(buffer
, userbuf
, nbytes
))
2280 buffer
[nbytes
] = 0; /* nul-terminate */
2281 if (cft
->write_u64
) {
2282 u64 val
= simple_strtoull(strstrip(buffer
), &end
, 0);
2285 retval
= cft
->write_u64(cgrp
, cft
, val
);
2287 s64 val
= simple_strtoll(strstrip(buffer
), &end
, 0);
2290 retval
= cft
->write_s64(cgrp
, cft
, val
);
2297 static ssize_t
cgroup_write_string(struct cgroup
*cgrp
, struct cftype
*cft
,
2299 const char __user
*userbuf
,
2300 size_t nbytes
, loff_t
*unused_ppos
)
2302 char local_buffer
[CGROUP_LOCAL_BUFFER_SIZE
];
2304 size_t max_bytes
= cft
->max_write_len
;
2305 char *buffer
= local_buffer
;
2308 max_bytes
= sizeof(local_buffer
) - 1;
2309 if (nbytes
>= max_bytes
)
2311 /* Allocate a dynamic buffer if we need one */
2312 if (nbytes
>= sizeof(local_buffer
)) {
2313 buffer
= kmalloc(nbytes
+ 1, GFP_KERNEL
);
2317 if (nbytes
&& copy_from_user(buffer
, userbuf
, nbytes
)) {
2322 buffer
[nbytes
] = 0; /* nul-terminate */
2323 retval
= cft
->write_string(cgrp
, cft
, strstrip(buffer
));
2327 if (buffer
!= local_buffer
)
2332 static ssize_t
cgroup_file_write(struct file
*file
, const char __user
*buf
,
2333 size_t nbytes
, loff_t
*ppos
)
2335 struct cftype
*cft
= __d_cft(file
->f_dentry
);
2336 struct cgroup
*cgrp
= __d_cgrp(file
->f_dentry
->d_parent
);
2338 if (cgroup_is_removed(cgrp
))
2341 return cft
->write(cgrp
, cft
, file
, buf
, nbytes
, ppos
);
2342 if (cft
->write_u64
|| cft
->write_s64
)
2343 return cgroup_write_X64(cgrp
, cft
, file
, buf
, nbytes
, ppos
);
2344 if (cft
->write_string
)
2345 return cgroup_write_string(cgrp
, cft
, file
, buf
, nbytes
, ppos
);
2347 int ret
= cft
->trigger(cgrp
, (unsigned int)cft
->private);
2348 return ret
? ret
: nbytes
;
2353 static ssize_t
cgroup_read_u64(struct cgroup
*cgrp
, struct cftype
*cft
,
2355 char __user
*buf
, size_t nbytes
,
2358 char tmp
[CGROUP_LOCAL_BUFFER_SIZE
];
2359 u64 val
= cft
->read_u64(cgrp
, cft
);
2360 int len
= sprintf(tmp
, "%llu\n", (unsigned long long) val
);
2362 return simple_read_from_buffer(buf
, nbytes
, ppos
, tmp
, len
);
2365 static ssize_t
cgroup_read_s64(struct cgroup
*cgrp
, struct cftype
*cft
,
2367 char __user
*buf
, size_t nbytes
,
2370 char tmp
[CGROUP_LOCAL_BUFFER_SIZE
];
2371 s64 val
= cft
->read_s64(cgrp
, cft
);
2372 int len
= sprintf(tmp
, "%lld\n", (long long) val
);
2374 return simple_read_from_buffer(buf
, nbytes
, ppos
, tmp
, len
);
2377 static ssize_t
cgroup_file_read(struct file
*file
, char __user
*buf
,
2378 size_t nbytes
, loff_t
*ppos
)
2380 struct cftype
*cft
= __d_cft(file
->f_dentry
);
2381 struct cgroup
*cgrp
= __d_cgrp(file
->f_dentry
->d_parent
);
2383 if (cgroup_is_removed(cgrp
))
2387 return cft
->read(cgrp
, cft
, file
, buf
, nbytes
, ppos
);
2389 return cgroup_read_u64(cgrp
, cft
, file
, buf
, nbytes
, ppos
);
2391 return cgroup_read_s64(cgrp
, cft
, file
, buf
, nbytes
, ppos
);
2396 * seqfile ops/methods for returning structured data. Currently just
2397 * supports string->u64 maps, but can be extended in future.
2400 struct cgroup_seqfile_state
{
2402 struct cgroup
*cgroup
;
2405 static int cgroup_map_add(struct cgroup_map_cb
*cb
, const char *key
, u64 value
)
2407 struct seq_file
*sf
= cb
->state
;
2408 return seq_printf(sf
, "%s %llu\n", key
, (unsigned long long)value
);
2411 static int cgroup_seqfile_show(struct seq_file
*m
, void *arg
)
2413 struct cgroup_seqfile_state
*state
= m
->private;
2414 struct cftype
*cft
= state
->cft
;
2415 if (cft
->read_map
) {
2416 struct cgroup_map_cb cb
= {
2417 .fill
= cgroup_map_add
,
2420 return cft
->read_map(state
->cgroup
, cft
, &cb
);
2422 return cft
->read_seq_string(state
->cgroup
, cft
, m
);
2425 static int cgroup_seqfile_release(struct inode
*inode
, struct file
*file
)
2427 struct seq_file
*seq
= file
->private_data
;
2428 kfree(seq
->private);
2429 return single_release(inode
, file
);
2432 static const struct file_operations cgroup_seqfile_operations
= {
2434 .write
= cgroup_file_write
,
2435 .llseek
= seq_lseek
,
2436 .release
= cgroup_seqfile_release
,
2439 static int cgroup_file_open(struct inode
*inode
, struct file
*file
)
2444 err
= generic_file_open(inode
, file
);
2447 cft
= __d_cft(file
->f_dentry
);
2449 if (cft
->read_map
|| cft
->read_seq_string
) {
2450 struct cgroup_seqfile_state
*state
=
2451 kzalloc(sizeof(*state
), GFP_USER
);
2455 state
->cgroup
= __d_cgrp(file
->f_dentry
->d_parent
);
2456 file
->f_op
= &cgroup_seqfile_operations
;
2457 err
= single_open(file
, cgroup_seqfile_show
, state
);
2460 } else if (cft
->open
)
2461 err
= cft
->open(inode
, file
);
2468 static int cgroup_file_release(struct inode
*inode
, struct file
*file
)
2470 struct cftype
*cft
= __d_cft(file
->f_dentry
);
2472 return cft
->release(inode
, file
);
2477 * cgroup_rename - Only allow simple rename of directories in place.
2479 static int cgroup_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
2480 struct inode
*new_dir
, struct dentry
*new_dentry
)
2483 struct cgroup_name
*name
, *old_name
;
2484 struct cgroup
*cgrp
;
2487 * It's convinient to use parent dir's i_mutex to protected
2490 lockdep_assert_held(&old_dir
->i_mutex
);
2492 if (!S_ISDIR(old_dentry
->d_inode
->i_mode
))
2494 if (new_dentry
->d_inode
)
2496 if (old_dir
!= new_dir
)
2499 cgrp
= __d_cgrp(old_dentry
);
2501 name
= cgroup_alloc_name(new_dentry
);
2505 ret
= simple_rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
2511 old_name
= cgrp
->name
;
2512 rcu_assign_pointer(cgrp
->name
, name
);
2514 kfree_rcu(old_name
, rcu_head
);
2518 static struct simple_xattrs
*__d_xattrs(struct dentry
*dentry
)
2520 if (S_ISDIR(dentry
->d_inode
->i_mode
))
2521 return &__d_cgrp(dentry
)->xattrs
;
2523 return &__d_cft(dentry
)->xattrs
;
2526 static inline int xattr_enabled(struct dentry
*dentry
)
2528 struct cgroupfs_root
*root
= dentry
->d_sb
->s_fs_info
;
2529 return test_bit(ROOT_XATTR
, &root
->flags
);
2532 static bool is_valid_xattr(const char *name
)
2534 if (!strncmp(name
, XATTR_TRUSTED_PREFIX
, XATTR_TRUSTED_PREFIX_LEN
) ||
2535 !strncmp(name
, XATTR_SECURITY_PREFIX
, XATTR_SECURITY_PREFIX_LEN
))
2540 static int cgroup_setxattr(struct dentry
*dentry
, const char *name
,
2541 const void *val
, size_t size
, int flags
)
2543 if (!xattr_enabled(dentry
))
2545 if (!is_valid_xattr(name
))
2547 return simple_xattr_set(__d_xattrs(dentry
), name
, val
, size
, flags
);
2550 static int cgroup_removexattr(struct dentry
*dentry
, const char *name
)
2552 if (!xattr_enabled(dentry
))
2554 if (!is_valid_xattr(name
))
2556 return simple_xattr_remove(__d_xattrs(dentry
), name
);
2559 static ssize_t
cgroup_getxattr(struct dentry
*dentry
, const char *name
,
2560 void *buf
, size_t size
)
2562 if (!xattr_enabled(dentry
))
2564 if (!is_valid_xattr(name
))
2566 return simple_xattr_get(__d_xattrs(dentry
), name
, buf
, size
);
2569 static ssize_t
cgroup_listxattr(struct dentry
*dentry
, char *buf
, size_t size
)
2571 if (!xattr_enabled(dentry
))
2573 return simple_xattr_list(__d_xattrs(dentry
), buf
, size
);
2576 static const struct file_operations cgroup_file_operations
= {
2577 .read
= cgroup_file_read
,
2578 .write
= cgroup_file_write
,
2579 .llseek
= generic_file_llseek
,
2580 .open
= cgroup_file_open
,
2581 .release
= cgroup_file_release
,
2584 static const struct inode_operations cgroup_file_inode_operations
= {
2585 .setxattr
= cgroup_setxattr
,
2586 .getxattr
= cgroup_getxattr
,
2587 .listxattr
= cgroup_listxattr
,
2588 .removexattr
= cgroup_removexattr
,
2591 static const struct inode_operations cgroup_dir_inode_operations
= {
2592 .lookup
= cgroup_lookup
,
2593 .mkdir
= cgroup_mkdir
,
2594 .rmdir
= cgroup_rmdir
,
2595 .rename
= cgroup_rename
,
2596 .setxattr
= cgroup_setxattr
,
2597 .getxattr
= cgroup_getxattr
,
2598 .listxattr
= cgroup_listxattr
,
2599 .removexattr
= cgroup_removexattr
,
2602 static struct dentry
*cgroup_lookup(struct inode
*dir
, struct dentry
*dentry
, unsigned int flags
)
2604 if (dentry
->d_name
.len
> NAME_MAX
)
2605 return ERR_PTR(-ENAMETOOLONG
);
2606 d_add(dentry
, NULL
);
2611 * Check if a file is a control file
2613 static inline struct cftype
*__file_cft(struct file
*file
)
2615 if (file_inode(file
)->i_fop
!= &cgroup_file_operations
)
2616 return ERR_PTR(-EINVAL
);
2617 return __d_cft(file
->f_dentry
);
2620 static int cgroup_create_file(struct dentry
*dentry
, umode_t mode
,
2621 struct super_block
*sb
)
2623 struct inode
*inode
;
2627 if (dentry
->d_inode
)
2630 inode
= cgroup_new_inode(mode
, sb
);
2634 if (S_ISDIR(mode
)) {
2635 inode
->i_op
= &cgroup_dir_inode_operations
;
2636 inode
->i_fop
= &simple_dir_operations
;
2638 /* start off with i_nlink == 2 (for "." entry) */
2640 inc_nlink(dentry
->d_parent
->d_inode
);
2643 * Control reaches here with cgroup_mutex held.
2644 * @inode->i_mutex should nest outside cgroup_mutex but we
2645 * want to populate it immediately without releasing
2646 * cgroup_mutex. As @inode isn't visible to anyone else
2647 * yet, trylock will always succeed without affecting
2650 WARN_ON_ONCE(!mutex_trylock(&inode
->i_mutex
));
2651 } else if (S_ISREG(mode
)) {
2653 inode
->i_fop
= &cgroup_file_operations
;
2654 inode
->i_op
= &cgroup_file_inode_operations
;
2656 d_instantiate(dentry
, inode
);
2657 dget(dentry
); /* Extra count - pin the dentry in core */
2662 * cgroup_file_mode - deduce file mode of a control file
2663 * @cft: the control file in question
2665 * returns cft->mode if ->mode is not 0
2666 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2667 * returns S_IRUGO if it has only a read handler
2668 * returns S_IWUSR if it has only a write hander
2670 static umode_t
cgroup_file_mode(const struct cftype
*cft
)
2677 if (cft
->read
|| cft
->read_u64
|| cft
->read_s64
||
2678 cft
->read_map
|| cft
->read_seq_string
)
2681 if (cft
->write
|| cft
->write_u64
|| cft
->write_s64
||
2682 cft
->write_string
|| cft
->trigger
)
2688 static int cgroup_add_file(struct cgroup
*cgrp
, struct cgroup_subsys
*subsys
,
2691 struct dentry
*dir
= cgrp
->dentry
;
2692 struct cgroup
*parent
= __d_cgrp(dir
);
2693 struct dentry
*dentry
;
2697 char name
[MAX_CGROUP_TYPE_NAMELEN
+ MAX_CFTYPE_NAME
+ 2] = { 0 };
2699 simple_xattrs_init(&cft
->xattrs
);
2701 if (subsys
&& !test_bit(ROOT_NOPREFIX
, &cgrp
->root
->flags
)) {
2702 strcpy(name
, subsys
->name
);
2705 strcat(name
, cft
->name
);
2707 BUG_ON(!mutex_is_locked(&dir
->d_inode
->i_mutex
));
2709 cfe
= kzalloc(sizeof(*cfe
), GFP_KERNEL
);
2713 dentry
= lookup_one_len(name
, dir
, strlen(name
));
2714 if (IS_ERR(dentry
)) {
2715 error
= PTR_ERR(dentry
);
2719 mode
= cgroup_file_mode(cft
);
2720 error
= cgroup_create_file(dentry
, mode
| S_IFREG
, cgrp
->root
->sb
);
2722 cfe
->type
= (void *)cft
;
2723 cfe
->dentry
= dentry
;
2724 dentry
->d_fsdata
= cfe
;
2725 list_add_tail(&cfe
->node
, &parent
->files
);
2734 static int cgroup_addrm_files(struct cgroup
*cgrp
, struct cgroup_subsys
*subsys
,
2735 struct cftype cfts
[], bool is_add
)
2740 for (cft
= cfts
; cft
->name
[0] != '\0'; cft
++) {
2741 /* does cft->flags tell us to skip this file on @cgrp? */
2742 if ((cft
->flags
& CFTYPE_NOT_ON_ROOT
) && !cgrp
->parent
)
2744 if ((cft
->flags
& CFTYPE_ONLY_ON_ROOT
) && cgrp
->parent
)
2748 err
= cgroup_add_file(cgrp
, subsys
, cft
);
2750 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
2754 cgroup_rm_file(cgrp
, cft
);
2760 static DEFINE_MUTEX(cgroup_cft_mutex
);
2762 static void cgroup_cfts_prepare(void)
2763 __acquires(&cgroup_cft_mutex
) __acquires(&cgroup_mutex
)
2766 * Thanks to the entanglement with vfs inode locking, we can't walk
2767 * the existing cgroups under cgroup_mutex and create files.
2768 * Instead, we increment reference on all cgroups and build list of
2769 * them using @cgrp->cft_q_node. Grab cgroup_cft_mutex to ensure
2770 * exclusive access to the field.
2772 mutex_lock(&cgroup_cft_mutex
);
2773 mutex_lock(&cgroup_mutex
);
2776 static void cgroup_cfts_commit(struct cgroup_subsys
*ss
,
2777 struct cftype
*cfts
, bool is_add
)
2778 __releases(&cgroup_mutex
) __releases(&cgroup_cft_mutex
)
2781 struct cgroup
*cgrp
, *n
;
2783 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
2784 if (cfts
&& ss
->root
!= &rootnode
) {
2785 list_for_each_entry(cgrp
, &ss
->root
->allcg_list
, allcg_node
) {
2787 list_add_tail(&cgrp
->cft_q_node
, &pending
);
2791 mutex_unlock(&cgroup_mutex
);
2794 * All new cgroups will see @cfts update on @ss->cftsets. Add/rm
2795 * files for all cgroups which were created before.
2797 list_for_each_entry_safe(cgrp
, n
, &pending
, cft_q_node
) {
2798 struct inode
*inode
= cgrp
->dentry
->d_inode
;
2800 mutex_lock(&inode
->i_mutex
);
2801 mutex_lock(&cgroup_mutex
);
2802 if (!cgroup_is_removed(cgrp
))
2803 cgroup_addrm_files(cgrp
, ss
, cfts
, is_add
);
2804 mutex_unlock(&cgroup_mutex
);
2805 mutex_unlock(&inode
->i_mutex
);
2807 list_del_init(&cgrp
->cft_q_node
);
2811 mutex_unlock(&cgroup_cft_mutex
);
2815 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2816 * @ss: target cgroup subsystem
2817 * @cfts: zero-length name terminated array of cftypes
2819 * Register @cfts to @ss. Files described by @cfts are created for all
2820 * existing cgroups to which @ss is attached and all future cgroups will
2821 * have them too. This function can be called anytime whether @ss is
2824 * Returns 0 on successful registration, -errno on failure. Note that this
2825 * function currently returns 0 as long as @cfts registration is successful
2826 * even if some file creation attempts on existing cgroups fail.
2828 int cgroup_add_cftypes(struct cgroup_subsys
*ss
, struct cftype
*cfts
)
2830 struct cftype_set
*set
;
2832 set
= kzalloc(sizeof(*set
), GFP_KERNEL
);
2836 cgroup_cfts_prepare();
2838 list_add_tail(&set
->node
, &ss
->cftsets
);
2839 cgroup_cfts_commit(ss
, cfts
, true);
2843 EXPORT_SYMBOL_GPL(cgroup_add_cftypes
);
2846 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
2847 * @ss: target cgroup subsystem
2848 * @cfts: zero-length name terminated array of cftypes
2850 * Unregister @cfts from @ss. Files described by @cfts are removed from
2851 * all existing cgroups to which @ss is attached and all future cgroups
2852 * won't have them either. This function can be called anytime whether @ss
2853 * is attached or not.
2855 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
2856 * registered with @ss.
2858 int cgroup_rm_cftypes(struct cgroup_subsys
*ss
, struct cftype
*cfts
)
2860 struct cftype_set
*set
;
2862 cgroup_cfts_prepare();
2864 list_for_each_entry(set
, &ss
->cftsets
, node
) {
2865 if (set
->cfts
== cfts
) {
2866 list_del_init(&set
->node
);
2867 cgroup_cfts_commit(ss
, cfts
, false);
2872 cgroup_cfts_commit(ss
, NULL
, false);
2877 * cgroup_task_count - count the number of tasks in a cgroup.
2878 * @cgrp: the cgroup in question
2880 * Return the number of tasks in the cgroup.
2882 int cgroup_task_count(const struct cgroup
*cgrp
)
2885 struct cg_cgroup_link
*link
;
2887 read_lock(&css_set_lock
);
2888 list_for_each_entry(link
, &cgrp
->css_sets
, cgrp_link_list
) {
2889 count
+= atomic_read(&link
->cg
->refcount
);
2891 read_unlock(&css_set_lock
);
2896 * Advance a list_head iterator. The iterator should be positioned at
2897 * the start of a css_set
2899 static void cgroup_advance_iter(struct cgroup
*cgrp
,
2900 struct cgroup_iter
*it
)
2902 struct list_head
*l
= it
->cg_link
;
2903 struct cg_cgroup_link
*link
;
2906 /* Advance to the next non-empty css_set */
2909 if (l
== &cgrp
->css_sets
) {
2913 link
= list_entry(l
, struct cg_cgroup_link
, cgrp_link_list
);
2915 } while (list_empty(&cg
->tasks
));
2917 it
->task
= cg
->tasks
.next
;
2921 * To reduce the fork() overhead for systems that are not actually
2922 * using their cgroups capability, we don't maintain the lists running
2923 * through each css_set to its tasks until we see the list actually
2924 * used - in other words after the first call to cgroup_iter_start().
2926 static void cgroup_enable_task_cg_lists(void)
2928 struct task_struct
*p
, *g
;
2929 write_lock(&css_set_lock
);
2930 use_task_css_set_links
= 1;
2932 * We need tasklist_lock because RCU is not safe against
2933 * while_each_thread(). Besides, a forking task that has passed
2934 * cgroup_post_fork() without seeing use_task_css_set_links = 1
2935 * is not guaranteed to have its child immediately visible in the
2936 * tasklist if we walk through it with RCU.
2938 read_lock(&tasklist_lock
);
2939 do_each_thread(g
, p
) {
2942 * We should check if the process is exiting, otherwise
2943 * it will race with cgroup_exit() in that the list
2944 * entry won't be deleted though the process has exited.
2946 if (!(p
->flags
& PF_EXITING
) && list_empty(&p
->cg_list
))
2947 list_add(&p
->cg_list
, &p
->cgroups
->tasks
);
2949 } while_each_thread(g
, p
);
2950 read_unlock(&tasklist_lock
);
2951 write_unlock(&css_set_lock
);
2955 * cgroup_next_descendant_pre - find the next descendant for pre-order walk
2956 * @pos: the current position (%NULL to initiate traversal)
2957 * @cgroup: cgroup whose descendants to walk
2959 * To be used by cgroup_for_each_descendant_pre(). Find the next
2960 * descendant to visit for pre-order traversal of @cgroup's descendants.
2962 struct cgroup
*cgroup_next_descendant_pre(struct cgroup
*pos
,
2963 struct cgroup
*cgroup
)
2965 struct cgroup
*next
;
2967 WARN_ON_ONCE(!rcu_read_lock_held());
2969 /* if first iteration, pretend we just visited @cgroup */
2971 if (list_empty(&cgroup
->children
))
2976 /* visit the first child if exists */
2977 next
= list_first_or_null_rcu(&pos
->children
, struct cgroup
, sibling
);
2981 /* no child, visit my or the closest ancestor's next sibling */
2983 next
= list_entry_rcu(pos
->sibling
.next
, struct cgroup
,
2985 if (&next
->sibling
!= &pos
->parent
->children
)
2989 } while (pos
!= cgroup
);
2993 EXPORT_SYMBOL_GPL(cgroup_next_descendant_pre
);
2996 * cgroup_rightmost_descendant - return the rightmost descendant of a cgroup
2997 * @pos: cgroup of interest
2999 * Return the rightmost descendant of @pos. If there's no descendant,
3000 * @pos is returned. This can be used during pre-order traversal to skip
3003 struct cgroup
*cgroup_rightmost_descendant(struct cgroup
*pos
)
3005 struct cgroup
*last
, *tmp
;
3007 WARN_ON_ONCE(!rcu_read_lock_held());
3011 /* ->prev isn't RCU safe, walk ->next till the end */
3013 list_for_each_entry_rcu(tmp
, &last
->children
, sibling
)
3019 EXPORT_SYMBOL_GPL(cgroup_rightmost_descendant
);
3021 static struct cgroup
*cgroup_leftmost_descendant(struct cgroup
*pos
)
3023 struct cgroup
*last
;
3027 pos
= list_first_or_null_rcu(&pos
->children
, struct cgroup
,
3035 * cgroup_next_descendant_post - find the next descendant for post-order walk
3036 * @pos: the current position (%NULL to initiate traversal)
3037 * @cgroup: cgroup whose descendants to walk
3039 * To be used by cgroup_for_each_descendant_post(). Find the next
3040 * descendant to visit for post-order traversal of @cgroup's descendants.
3042 struct cgroup
*cgroup_next_descendant_post(struct cgroup
*pos
,
3043 struct cgroup
*cgroup
)
3045 struct cgroup
*next
;
3047 WARN_ON_ONCE(!rcu_read_lock_held());
3049 /* if first iteration, visit the leftmost descendant */
3051 next
= cgroup_leftmost_descendant(cgroup
);
3052 return next
!= cgroup
? next
: NULL
;
3055 /* if there's an unvisited sibling, visit its leftmost descendant */
3056 next
= list_entry_rcu(pos
->sibling
.next
, struct cgroup
, sibling
);
3057 if (&next
->sibling
!= &pos
->parent
->children
)
3058 return cgroup_leftmost_descendant(next
);
3060 /* no sibling left, visit parent */
3062 return next
!= cgroup
? next
: NULL
;
3064 EXPORT_SYMBOL_GPL(cgroup_next_descendant_post
);
3066 void cgroup_iter_start(struct cgroup
*cgrp
, struct cgroup_iter
*it
)
3067 __acquires(css_set_lock
)
3070 * The first time anyone tries to iterate across a cgroup,
3071 * we need to enable the list linking each css_set to its
3072 * tasks, and fix up all existing tasks.
3074 if (!use_task_css_set_links
)
3075 cgroup_enable_task_cg_lists();
3077 read_lock(&css_set_lock
);
3078 it
->cg_link
= &cgrp
->css_sets
;
3079 cgroup_advance_iter(cgrp
, it
);
3082 struct task_struct
*cgroup_iter_next(struct cgroup
*cgrp
,
3083 struct cgroup_iter
*it
)
3085 struct task_struct
*res
;
3086 struct list_head
*l
= it
->task
;
3087 struct cg_cgroup_link
*link
;
3089 /* If the iterator cg is NULL, we have no tasks */
3092 res
= list_entry(l
, struct task_struct
, cg_list
);
3093 /* Advance iterator to find next entry */
3095 link
= list_entry(it
->cg_link
, struct cg_cgroup_link
, cgrp_link_list
);
3096 if (l
== &link
->cg
->tasks
) {
3097 /* We reached the end of this task list - move on to
3098 * the next cg_cgroup_link */
3099 cgroup_advance_iter(cgrp
, it
);
3106 void cgroup_iter_end(struct cgroup
*cgrp
, struct cgroup_iter
*it
)
3107 __releases(css_set_lock
)
3109 read_unlock(&css_set_lock
);
3112 static inline int started_after_time(struct task_struct
*t1
,
3113 struct timespec
*time
,
3114 struct task_struct
*t2
)
3116 int start_diff
= timespec_compare(&t1
->start_time
, time
);
3117 if (start_diff
> 0) {
3119 } else if (start_diff
< 0) {
3123 * Arbitrarily, if two processes started at the same
3124 * time, we'll say that the lower pointer value
3125 * started first. Note that t2 may have exited by now
3126 * so this may not be a valid pointer any longer, but
3127 * that's fine - it still serves to distinguish
3128 * between two tasks started (effectively) simultaneously.
3135 * This function is a callback from heap_insert() and is used to order
3137 * In this case we order the heap in descending task start time.
3139 static inline int started_after(void *p1
, void *p2
)
3141 struct task_struct
*t1
= p1
;
3142 struct task_struct
*t2
= p2
;
3143 return started_after_time(t1
, &t2
->start_time
, t2
);
3147 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
3148 * @scan: struct cgroup_scanner containing arguments for the scan
3150 * Arguments include pointers to callback functions test_task() and
3152 * Iterate through all the tasks in a cgroup, calling test_task() for each,
3153 * and if it returns true, call process_task() for it also.
3154 * The test_task pointer may be NULL, meaning always true (select all tasks).
3155 * Effectively duplicates cgroup_iter_{start,next,end}()
3156 * but does not lock css_set_lock for the call to process_task().
3157 * The struct cgroup_scanner may be embedded in any structure of the caller's
3159 * It is guaranteed that process_task() will act on every task that
3160 * is a member of the cgroup for the duration of this call. This
3161 * function may or may not call process_task() for tasks that exit
3162 * or move to a different cgroup during the call, or are forked or
3163 * move into the cgroup during the call.
3165 * Note that test_task() may be called with locks held, and may in some
3166 * situations be called multiple times for the same task, so it should
3168 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
3169 * pre-allocated and will be used for heap operations (and its "gt" member will
3170 * be overwritten), else a temporary heap will be used (allocation of which
3171 * may cause this function to fail).
3173 int cgroup_scan_tasks(struct cgroup_scanner
*scan
)
3176 struct cgroup_iter it
;
3177 struct task_struct
*p
, *dropped
;
3178 /* Never dereference latest_task, since it's not refcounted */
3179 struct task_struct
*latest_task
= NULL
;
3180 struct ptr_heap tmp_heap
;
3181 struct ptr_heap
*heap
;
3182 struct timespec latest_time
= { 0, 0 };
3185 /* The caller supplied our heap and pre-allocated its memory */
3187 heap
->gt
= &started_after
;
3189 /* We need to allocate our own heap memory */
3191 retval
= heap_init(heap
, PAGE_SIZE
, GFP_KERNEL
, &started_after
);
3193 /* cannot allocate the heap */
3199 * Scan tasks in the cgroup, using the scanner's "test_task" callback
3200 * to determine which are of interest, and using the scanner's
3201 * "process_task" callback to process any of them that need an update.
3202 * Since we don't want to hold any locks during the task updates,
3203 * gather tasks to be processed in a heap structure.
3204 * The heap is sorted by descending task start time.
3205 * If the statically-sized heap fills up, we overflow tasks that
3206 * started later, and in future iterations only consider tasks that
3207 * started after the latest task in the previous pass. This
3208 * guarantees forward progress and that we don't miss any tasks.
3211 cgroup_iter_start(scan
->cg
, &it
);
3212 while ((p
= cgroup_iter_next(scan
->cg
, &it
))) {
3214 * Only affect tasks that qualify per the caller's callback,
3215 * if he provided one
3217 if (scan
->test_task
&& !scan
->test_task(p
, scan
))
3220 * Only process tasks that started after the last task
3223 if (!started_after_time(p
, &latest_time
, latest_task
))
3225 dropped
= heap_insert(heap
, p
);
3226 if (dropped
== NULL
) {
3228 * The new task was inserted; the heap wasn't
3232 } else if (dropped
!= p
) {
3234 * The new task was inserted, and pushed out a
3238 put_task_struct(dropped
);
3241 * Else the new task was newer than anything already in
3242 * the heap and wasn't inserted
3245 cgroup_iter_end(scan
->cg
, &it
);
3248 for (i
= 0; i
< heap
->size
; i
++) {
3249 struct task_struct
*q
= heap
->ptrs
[i
];
3251 latest_time
= q
->start_time
;
3254 /* Process the task per the caller's callback */
3255 scan
->process_task(q
, scan
);
3259 * If we had to process any tasks at all, scan again
3260 * in case some of them were in the middle of forking
3261 * children that didn't get processed.
3262 * Not the most efficient way to do it, but it avoids
3263 * having to take callback_mutex in the fork path
3267 if (heap
== &tmp_heap
)
3268 heap_free(&tmp_heap
);
3273 * Stuff for reading the 'tasks'/'procs' files.
3275 * Reading this file can return large amounts of data if a cgroup has
3276 * *lots* of attached tasks. So it may need several calls to read(),
3277 * but we cannot guarantee that the information we produce is correct
3278 * unless we produce it entirely atomically.
3282 /* which pidlist file are we talking about? */
3283 enum cgroup_filetype
{
3289 * A pidlist is a list of pids that virtually represents the contents of one
3290 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3291 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3294 struct cgroup_pidlist
{
3296 * used to find which pidlist is wanted. doesn't change as long as
3297 * this particular list stays in the list.
3299 struct { enum cgroup_filetype type
; struct pid_namespace
*ns
; } key
;
3302 /* how many elements the above list has */
3304 /* how many files are using the current array */
3306 /* each of these stored in a list by its cgroup */
3307 struct list_head links
;
3308 /* pointer to the cgroup we belong to, for list removal purposes */
3309 struct cgroup
*owner
;
3310 /* protects the other fields */
3311 struct rw_semaphore mutex
;
3315 * The following two functions "fix" the issue where there are more pids
3316 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3317 * TODO: replace with a kernel-wide solution to this problem
3319 #define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3320 static void *pidlist_allocate(int count
)
3322 if (PIDLIST_TOO_LARGE(count
))
3323 return vmalloc(count
* sizeof(pid_t
));
3325 return kmalloc(count
* sizeof(pid_t
), GFP_KERNEL
);
3327 static void pidlist_free(void *p
)
3329 if (is_vmalloc_addr(p
))
3336 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
3337 * Returns the number of unique elements.
3339 static int pidlist_uniq(pid_t
*list
, int length
)
3344 * we presume the 0th element is unique, so i starts at 1. trivial
3345 * edge cases first; no work needs to be done for either
3347 if (length
== 0 || length
== 1)
3349 /* src and dest walk down the list; dest counts unique elements */
3350 for (src
= 1; src
< length
; src
++) {
3351 /* find next unique element */
3352 while (list
[src
] == list
[src
-1]) {
3357 /* dest always points to where the next unique element goes */
3358 list
[dest
] = list
[src
];
3365 static int cmppid(const void *a
, const void *b
)
3367 return *(pid_t
*)a
- *(pid_t
*)b
;
3371 * find the appropriate pidlist for our purpose (given procs vs tasks)
3372 * returns with the lock on that pidlist already held, and takes care
3373 * of the use count, or returns NULL with no locks held if we're out of
3376 static struct cgroup_pidlist
*cgroup_pidlist_find(struct cgroup
*cgrp
,
3377 enum cgroup_filetype type
)
3379 struct cgroup_pidlist
*l
;
3380 /* don't need task_nsproxy() if we're looking at ourself */
3381 struct pid_namespace
*ns
= task_active_pid_ns(current
);
3384 * We can't drop the pidlist_mutex before taking the l->mutex in case
3385 * the last ref-holder is trying to remove l from the list at the same
3386 * time. Holding the pidlist_mutex precludes somebody taking whichever
3387 * list we find out from under us - compare release_pid_array().
3389 mutex_lock(&cgrp
->pidlist_mutex
);
3390 list_for_each_entry(l
, &cgrp
->pidlists
, links
) {
3391 if (l
->key
.type
== type
&& l
->key
.ns
== ns
) {
3392 /* make sure l doesn't vanish out from under us */
3393 down_write(&l
->mutex
);
3394 mutex_unlock(&cgrp
->pidlist_mutex
);
3398 /* entry not found; create a new one */
3399 l
= kmalloc(sizeof(struct cgroup_pidlist
), GFP_KERNEL
);
3401 mutex_unlock(&cgrp
->pidlist_mutex
);
3404 init_rwsem(&l
->mutex
);
3405 down_write(&l
->mutex
);
3407 l
->key
.ns
= get_pid_ns(ns
);
3408 l
->use_count
= 0; /* don't increment here */
3411 list_add(&l
->links
, &cgrp
->pidlists
);
3412 mutex_unlock(&cgrp
->pidlist_mutex
);
3417 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3419 static int pidlist_array_load(struct cgroup
*cgrp
, enum cgroup_filetype type
,
3420 struct cgroup_pidlist
**lp
)
3424 int pid
, n
= 0; /* used for populating the array */
3425 struct cgroup_iter it
;
3426 struct task_struct
*tsk
;
3427 struct cgroup_pidlist
*l
;
3430 * If cgroup gets more users after we read count, we won't have
3431 * enough space - tough. This race is indistinguishable to the
3432 * caller from the case that the additional cgroup users didn't
3433 * show up until sometime later on.
3435 length
= cgroup_task_count(cgrp
);
3436 array
= pidlist_allocate(length
);
3439 /* now, populate the array */
3440 cgroup_iter_start(cgrp
, &it
);
3441 while ((tsk
= cgroup_iter_next(cgrp
, &it
))) {
3442 if (unlikely(n
== length
))
3444 /* get tgid or pid for procs or tasks file respectively */
3445 if (type
== CGROUP_FILE_PROCS
)
3446 pid
= task_tgid_vnr(tsk
);
3448 pid
= task_pid_vnr(tsk
);
3449 if (pid
> 0) /* make sure to only use valid results */
3452 cgroup_iter_end(cgrp
, &it
);
3454 /* now sort & (if procs) strip out duplicates */
3455 sort(array
, length
, sizeof(pid_t
), cmppid
, NULL
);
3456 if (type
== CGROUP_FILE_PROCS
)
3457 length
= pidlist_uniq(array
, length
);
3458 l
= cgroup_pidlist_find(cgrp
, type
);
3460 pidlist_free(array
);
3463 /* store array, freeing old if necessary - lock already held */
3464 pidlist_free(l
->list
);
3468 up_write(&l
->mutex
);
3474 * cgroupstats_build - build and fill cgroupstats
3475 * @stats: cgroupstats to fill information into
3476 * @dentry: A dentry entry belonging to the cgroup for which stats have
3479 * Build and fill cgroupstats so that taskstats can export it to user
3482 int cgroupstats_build(struct cgroupstats
*stats
, struct dentry
*dentry
)
3485 struct cgroup
*cgrp
;
3486 struct cgroup_iter it
;
3487 struct task_struct
*tsk
;
3490 * Validate dentry by checking the superblock operations,
3491 * and make sure it's a directory.
3493 if (dentry
->d_sb
->s_op
!= &cgroup_ops
||
3494 !S_ISDIR(dentry
->d_inode
->i_mode
))
3498 cgrp
= dentry
->d_fsdata
;
3500 cgroup_iter_start(cgrp
, &it
);
3501 while ((tsk
= cgroup_iter_next(cgrp
, &it
))) {
3502 switch (tsk
->state
) {
3504 stats
->nr_running
++;
3506 case TASK_INTERRUPTIBLE
:
3507 stats
->nr_sleeping
++;
3509 case TASK_UNINTERRUPTIBLE
:
3510 stats
->nr_uninterruptible
++;
3513 stats
->nr_stopped
++;
3516 if (delayacct_is_task_waiting_on_io(tsk
))
3517 stats
->nr_io_wait
++;
3521 cgroup_iter_end(cgrp
, &it
);
3529 * seq_file methods for the tasks/procs files. The seq_file position is the
3530 * next pid to display; the seq_file iterator is a pointer to the pid
3531 * in the cgroup->l->list array.
3534 static void *cgroup_pidlist_start(struct seq_file
*s
, loff_t
*pos
)
3537 * Initially we receive a position value that corresponds to
3538 * one more than the last pid shown (or 0 on the first call or
3539 * after a seek to the start). Use a binary-search to find the
3540 * next pid to display, if any
3542 struct cgroup_pidlist
*l
= s
->private;
3543 int index
= 0, pid
= *pos
;
3546 down_read(&l
->mutex
);
3548 int end
= l
->length
;
3550 while (index
< end
) {
3551 int mid
= (index
+ end
) / 2;
3552 if (l
->list
[mid
] == pid
) {
3555 } else if (l
->list
[mid
] <= pid
)
3561 /* If we're off the end of the array, we're done */
3562 if (index
>= l
->length
)
3564 /* Update the abstract position to be the actual pid that we found */
3565 iter
= l
->list
+ index
;
3570 static void cgroup_pidlist_stop(struct seq_file
*s
, void *v
)
3572 struct cgroup_pidlist
*l
= s
->private;
3576 static void *cgroup_pidlist_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
3578 struct cgroup_pidlist
*l
= s
->private;
3580 pid_t
*end
= l
->list
+ l
->length
;
3582 * Advance to the next pid in the array. If this goes off the
3594 static int cgroup_pidlist_show(struct seq_file
*s
, void *v
)
3596 return seq_printf(s
, "%d\n", *(int *)v
);
3600 * seq_operations functions for iterating on pidlists through seq_file -
3601 * independent of whether it's tasks or procs
3603 static const struct seq_operations cgroup_pidlist_seq_operations
= {
3604 .start
= cgroup_pidlist_start
,
3605 .stop
= cgroup_pidlist_stop
,
3606 .next
= cgroup_pidlist_next
,
3607 .show
= cgroup_pidlist_show
,
3610 static void cgroup_release_pid_array(struct cgroup_pidlist
*l
)
3613 * the case where we're the last user of this particular pidlist will
3614 * have us remove it from the cgroup's list, which entails taking the
3615 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3616 * pidlist_mutex, we have to take pidlist_mutex first.
3618 mutex_lock(&l
->owner
->pidlist_mutex
);
3619 down_write(&l
->mutex
);
3620 BUG_ON(!l
->use_count
);
3621 if (!--l
->use_count
) {
3622 /* we're the last user if refcount is 0; remove and free */
3623 list_del(&l
->links
);
3624 mutex_unlock(&l
->owner
->pidlist_mutex
);
3625 pidlist_free(l
->list
);
3626 put_pid_ns(l
->key
.ns
);
3627 up_write(&l
->mutex
);
3631 mutex_unlock(&l
->owner
->pidlist_mutex
);
3632 up_write(&l
->mutex
);
3635 static int cgroup_pidlist_release(struct inode
*inode
, struct file
*file
)
3637 struct cgroup_pidlist
*l
;
3638 if (!(file
->f_mode
& FMODE_READ
))
3641 * the seq_file will only be initialized if the file was opened for
3642 * reading; hence we check if it's not null only in that case.
3644 l
= ((struct seq_file
*)file
->private_data
)->private;
3645 cgroup_release_pid_array(l
);
3646 return seq_release(inode
, file
);
3649 static const struct file_operations cgroup_pidlist_operations
= {
3651 .llseek
= seq_lseek
,
3652 .write
= cgroup_file_write
,
3653 .release
= cgroup_pidlist_release
,
3657 * The following functions handle opens on a file that displays a pidlist
3658 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3661 /* helper function for the two below it */
3662 static int cgroup_pidlist_open(struct file
*file
, enum cgroup_filetype type
)
3664 struct cgroup
*cgrp
= __d_cgrp(file
->f_dentry
->d_parent
);
3665 struct cgroup_pidlist
*l
;
3668 /* Nothing to do for write-only files */
3669 if (!(file
->f_mode
& FMODE_READ
))
3672 /* have the array populated */
3673 retval
= pidlist_array_load(cgrp
, type
, &l
);
3676 /* configure file information */
3677 file
->f_op
= &cgroup_pidlist_operations
;
3679 retval
= seq_open(file
, &cgroup_pidlist_seq_operations
);
3681 cgroup_release_pid_array(l
);
3684 ((struct seq_file
*)file
->private_data
)->private = l
;
3687 static int cgroup_tasks_open(struct inode
*unused
, struct file
*file
)
3689 return cgroup_pidlist_open(file
, CGROUP_FILE_TASKS
);
3691 static int cgroup_procs_open(struct inode
*unused
, struct file
*file
)
3693 return cgroup_pidlist_open(file
, CGROUP_FILE_PROCS
);
3696 static u64
cgroup_read_notify_on_release(struct cgroup
*cgrp
,
3699 return notify_on_release(cgrp
);
3702 static int cgroup_write_notify_on_release(struct cgroup
*cgrp
,
3706 clear_bit(CGRP_RELEASABLE
, &cgrp
->flags
);
3708 set_bit(CGRP_NOTIFY_ON_RELEASE
, &cgrp
->flags
);
3710 clear_bit(CGRP_NOTIFY_ON_RELEASE
, &cgrp
->flags
);
3715 * Unregister event and free resources.
3717 * Gets called from workqueue.
3719 static void cgroup_event_remove(struct work_struct
*work
)
3721 struct cgroup_event
*event
= container_of(work
, struct cgroup_event
,
3723 struct cgroup
*cgrp
= event
->cgrp
;
3725 remove_wait_queue(event
->wqh
, &event
->wait
);
3727 event
->cft
->unregister_event(cgrp
, event
->cft
, event
->eventfd
);
3729 /* Notify userspace the event is going away. */
3730 eventfd_signal(event
->eventfd
, 1);
3732 eventfd_ctx_put(event
->eventfd
);
3738 * Gets called on POLLHUP on eventfd when user closes it.
3740 * Called with wqh->lock held and interrupts disabled.
3742 static int cgroup_event_wake(wait_queue_t
*wait
, unsigned mode
,
3743 int sync
, void *key
)
3745 struct cgroup_event
*event
= container_of(wait
,
3746 struct cgroup_event
, wait
);
3747 struct cgroup
*cgrp
= event
->cgrp
;
3748 unsigned long flags
= (unsigned long)key
;
3750 if (flags
& POLLHUP
) {
3752 * If the event has been detached at cgroup removal, we
3753 * can simply return knowing the other side will cleanup
3756 * We can't race against event freeing since the other
3757 * side will require wqh->lock via remove_wait_queue(),
3760 spin_lock(&cgrp
->event_list_lock
);
3761 if (!list_empty(&event
->list
)) {
3762 list_del_init(&event
->list
);
3764 * We are in atomic context, but cgroup_event_remove()
3765 * may sleep, so we have to call it in workqueue.
3767 schedule_work(&event
->remove
);
3769 spin_unlock(&cgrp
->event_list_lock
);
3775 static void cgroup_event_ptable_queue_proc(struct file
*file
,
3776 wait_queue_head_t
*wqh
, poll_table
*pt
)
3778 struct cgroup_event
*event
= container_of(pt
,
3779 struct cgroup_event
, pt
);
3782 add_wait_queue(wqh
, &event
->wait
);
3786 * Parse input and register new cgroup event handler.
3788 * Input must be in format '<event_fd> <control_fd> <args>'.
3789 * Interpretation of args is defined by control file implementation.
3791 static int cgroup_write_event_control(struct cgroup
*cgrp
, struct cftype
*cft
,
3794 struct cgroup_event
*event
= NULL
;
3795 struct cgroup
*cgrp_cfile
;
3796 unsigned int efd
, cfd
;
3797 struct file
*efile
= NULL
;
3798 struct file
*cfile
= NULL
;
3802 efd
= simple_strtoul(buffer
, &endp
, 10);
3807 cfd
= simple_strtoul(buffer
, &endp
, 10);
3808 if ((*endp
!= ' ') && (*endp
!= '\0'))
3812 event
= kzalloc(sizeof(*event
), GFP_KERNEL
);
3816 INIT_LIST_HEAD(&event
->list
);
3817 init_poll_funcptr(&event
->pt
, cgroup_event_ptable_queue_proc
);
3818 init_waitqueue_func_entry(&event
->wait
, cgroup_event_wake
);
3819 INIT_WORK(&event
->remove
, cgroup_event_remove
);
3821 efile
= eventfd_fget(efd
);
3822 if (IS_ERR(efile
)) {
3823 ret
= PTR_ERR(efile
);
3827 event
->eventfd
= eventfd_ctx_fileget(efile
);
3828 if (IS_ERR(event
->eventfd
)) {
3829 ret
= PTR_ERR(event
->eventfd
);
3839 /* the process need read permission on control file */
3840 /* AV: shouldn't we check that it's been opened for read instead? */
3841 ret
= inode_permission(file_inode(cfile
), MAY_READ
);
3845 event
->cft
= __file_cft(cfile
);
3846 if (IS_ERR(event
->cft
)) {
3847 ret
= PTR_ERR(event
->cft
);
3852 * The file to be monitored must be in the same cgroup as
3853 * cgroup.event_control is.
3855 cgrp_cfile
= __d_cgrp(cfile
->f_dentry
->d_parent
);
3856 if (cgrp_cfile
!= cgrp
) {
3861 if (!event
->cft
->register_event
|| !event
->cft
->unregister_event
) {
3866 ret
= event
->cft
->register_event(cgrp
, event
->cft
,
3867 event
->eventfd
, buffer
);
3872 * Events should be removed after rmdir of cgroup directory, but before
3873 * destroying subsystem state objects. Let's take reference to cgroup
3874 * directory dentry to do that.
3878 spin_lock(&cgrp
->event_list_lock
);
3879 list_add(&event
->list
, &cgrp
->event_list
);
3880 spin_unlock(&cgrp
->event_list_lock
);
3891 if (event
&& event
->eventfd
&& !IS_ERR(event
->eventfd
))
3892 eventfd_ctx_put(event
->eventfd
);
3894 if (!IS_ERR_OR_NULL(efile
))
3902 static u64
cgroup_clone_children_read(struct cgroup
*cgrp
,
3905 return test_bit(CGRP_CPUSET_CLONE_CHILDREN
, &cgrp
->flags
);
3908 static int cgroup_clone_children_write(struct cgroup
*cgrp
,
3913 set_bit(CGRP_CPUSET_CLONE_CHILDREN
, &cgrp
->flags
);
3915 clear_bit(CGRP_CPUSET_CLONE_CHILDREN
, &cgrp
->flags
);
3920 * for the common functions, 'private' gives the type of file
3922 /* for hysterical raisins, we can't put this on the older files */
3923 #define CGROUP_FILE_GENERIC_PREFIX "cgroup."
3924 static struct cftype files
[] = {
3927 .open
= cgroup_tasks_open
,
3928 .write_u64
= cgroup_tasks_write
,
3929 .release
= cgroup_pidlist_release
,
3930 .mode
= S_IRUGO
| S_IWUSR
,
3933 .name
= CGROUP_FILE_GENERIC_PREFIX
"procs",
3934 .open
= cgroup_procs_open
,
3935 .write_u64
= cgroup_procs_write
,
3936 .release
= cgroup_pidlist_release
,
3937 .mode
= S_IRUGO
| S_IWUSR
,
3940 .name
= "notify_on_release",
3941 .read_u64
= cgroup_read_notify_on_release
,
3942 .write_u64
= cgroup_write_notify_on_release
,
3945 .name
= CGROUP_FILE_GENERIC_PREFIX
"event_control",
3946 .write_string
= cgroup_write_event_control
,
3950 .name
= "cgroup.clone_children",
3951 .read_u64
= cgroup_clone_children_read
,
3952 .write_u64
= cgroup_clone_children_write
,
3955 .name
= "release_agent",
3956 .flags
= CFTYPE_ONLY_ON_ROOT
,
3957 .read_seq_string
= cgroup_release_agent_show
,
3958 .write_string
= cgroup_release_agent_write
,
3959 .max_write_len
= PATH_MAX
,
3965 * cgroup_populate_dir - selectively creation of files in a directory
3966 * @cgrp: target cgroup
3967 * @base_files: true if the base files should be added
3968 * @subsys_mask: mask of the subsystem ids whose files should be added
3970 static int cgroup_populate_dir(struct cgroup
*cgrp
, bool base_files
,
3971 unsigned long subsys_mask
)
3974 struct cgroup_subsys
*ss
;
3977 err
= cgroup_addrm_files(cgrp
, NULL
, files
, true);
3982 /* process cftsets of each subsystem */
3983 for_each_subsys(cgrp
->root
, ss
) {
3984 struct cftype_set
*set
;
3985 if (!test_bit(ss
->subsys_id
, &subsys_mask
))
3988 list_for_each_entry(set
, &ss
->cftsets
, node
)
3989 cgroup_addrm_files(cgrp
, ss
, set
->cfts
, true);
3992 /* This cgroup is ready now */
3993 for_each_subsys(cgrp
->root
, ss
) {
3994 struct cgroup_subsys_state
*css
= cgrp
->subsys
[ss
->subsys_id
];
3996 * Update id->css pointer and make this css visible from
3997 * CSS ID functions. This pointer will be dereferened
3998 * from RCU-read-side without locks.
4001 rcu_assign_pointer(css
->id
->css
, css
);
4007 static void css_dput_fn(struct work_struct
*work
)
4009 struct cgroup_subsys_state
*css
=
4010 container_of(work
, struct cgroup_subsys_state
, dput_work
);
4011 struct dentry
*dentry
= css
->cgroup
->dentry
;
4012 struct super_block
*sb
= dentry
->d_sb
;
4014 atomic_inc(&sb
->s_active
);
4016 deactivate_super(sb
);
4019 static void init_cgroup_css(struct cgroup_subsys_state
*css
,
4020 struct cgroup_subsys
*ss
,
4021 struct cgroup
*cgrp
)
4024 atomic_set(&css
->refcnt
, 1);
4027 if (cgrp
== dummytop
)
4028 css
->flags
|= CSS_ROOT
;
4029 BUG_ON(cgrp
->subsys
[ss
->subsys_id
]);
4030 cgrp
->subsys
[ss
->subsys_id
] = css
;
4033 * css holds an extra ref to @cgrp->dentry which is put on the last
4034 * css_put(). dput() requires process context, which css_put() may
4035 * be called without. @css->dput_work will be used to invoke
4036 * dput() asynchronously from css_put().
4038 INIT_WORK(&css
->dput_work
, css_dput_fn
);
4041 /* invoke ->post_create() on a new CSS and mark it online if successful */
4042 static int online_css(struct cgroup_subsys
*ss
, struct cgroup
*cgrp
)
4046 lockdep_assert_held(&cgroup_mutex
);
4049 ret
= ss
->css_online(cgrp
);
4051 cgrp
->subsys
[ss
->subsys_id
]->flags
|= CSS_ONLINE
;
4055 /* if the CSS is online, invoke ->pre_destory() on it and mark it offline */
4056 static void offline_css(struct cgroup_subsys
*ss
, struct cgroup
*cgrp
)
4057 __releases(&cgroup_mutex
) __acquires(&cgroup_mutex
)
4059 struct cgroup_subsys_state
*css
= cgrp
->subsys
[ss
->subsys_id
];
4061 lockdep_assert_held(&cgroup_mutex
);
4063 if (!(css
->flags
& CSS_ONLINE
))
4066 if (ss
->css_offline
)
4067 ss
->css_offline(cgrp
);
4069 cgrp
->subsys
[ss
->subsys_id
]->flags
&= ~CSS_ONLINE
;
4073 * cgroup_create - create a cgroup
4074 * @parent: cgroup that will be parent of the new cgroup
4075 * @dentry: dentry of the new cgroup
4076 * @mode: mode to set on new inode
4078 * Must be called with the mutex on the parent inode held
4080 static long cgroup_create(struct cgroup
*parent
, struct dentry
*dentry
,
4083 struct cgroup
*cgrp
;
4084 struct cgroup_name
*name
;
4085 struct cgroupfs_root
*root
= parent
->root
;
4087 struct cgroup_subsys
*ss
;
4088 struct super_block
*sb
= root
->sb
;
4090 /* allocate the cgroup and its ID, 0 is reserved for the root */
4091 cgrp
= kzalloc(sizeof(*cgrp
), GFP_KERNEL
);
4095 name
= cgroup_alloc_name(dentry
);
4098 rcu_assign_pointer(cgrp
->name
, name
);
4100 cgrp
->id
= ida_simple_get(&root
->cgroup_ida
, 1, 0, GFP_KERNEL
);
4105 * Only live parents can have children. Note that the liveliness
4106 * check isn't strictly necessary because cgroup_mkdir() and
4107 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4108 * anyway so that locking is contained inside cgroup proper and we
4109 * don't get nasty surprises if we ever grow another caller.
4111 if (!cgroup_lock_live_group(parent
)) {
4116 /* Grab a reference on the superblock so the hierarchy doesn't
4117 * get deleted on unmount if there are child cgroups. This
4118 * can be done outside cgroup_mutex, since the sb can't
4119 * disappear while someone has an open control file on the
4121 atomic_inc(&sb
->s_active
);
4123 init_cgroup_housekeeping(cgrp
);
4125 dentry
->d_fsdata
= cgrp
;
4126 cgrp
->dentry
= dentry
;
4128 cgrp
->parent
= parent
;
4129 cgrp
->root
= parent
->root
;
4130 cgrp
->top_cgroup
= parent
->top_cgroup
;
4132 if (notify_on_release(parent
))
4133 set_bit(CGRP_NOTIFY_ON_RELEASE
, &cgrp
->flags
);
4135 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN
, &parent
->flags
))
4136 set_bit(CGRP_CPUSET_CLONE_CHILDREN
, &cgrp
->flags
);
4138 for_each_subsys(root
, ss
) {
4139 struct cgroup_subsys_state
*css
;
4141 css
= ss
->css_alloc(cgrp
);
4146 init_cgroup_css(css
, ss
, cgrp
);
4148 err
= alloc_css_id(ss
, parent
, cgrp
);
4155 * Create directory. cgroup_create_file() returns with the new
4156 * directory locked on success so that it can be populated without
4157 * dropping cgroup_mutex.
4159 err
= cgroup_create_file(dentry
, S_IFDIR
| mode
, sb
);
4162 lockdep_assert_held(&dentry
->d_inode
->i_mutex
);
4164 /* allocation complete, commit to creation */
4165 list_add_tail(&cgrp
->allcg_node
, &root
->allcg_list
);
4166 list_add_tail_rcu(&cgrp
->sibling
, &cgrp
->parent
->children
);
4167 root
->number_of_cgroups
++;
4169 /* each css holds a ref to the cgroup's dentry */
4170 for_each_subsys(root
, ss
)
4173 /* creation succeeded, notify subsystems */
4174 for_each_subsys(root
, ss
) {
4175 err
= online_css(ss
, cgrp
);
4179 if (ss
->broken_hierarchy
&& !ss
->warned_broken_hierarchy
&&
4181 pr_warning("cgroup: %s (%d) created nested cgroup for controller \"%s\" which has incomplete hierarchy support. Nested cgroups may change behavior in the future.\n",
4182 current
->comm
, current
->pid
, ss
->name
);
4183 if (!strcmp(ss
->name
, "memory"))
4184 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4185 ss
->warned_broken_hierarchy
= true;
4189 err
= cgroup_populate_dir(cgrp
, true, root
->subsys_mask
);
4193 mutex_unlock(&cgroup_mutex
);
4194 mutex_unlock(&cgrp
->dentry
->d_inode
->i_mutex
);
4199 for_each_subsys(root
, ss
) {
4200 if (cgrp
->subsys
[ss
->subsys_id
])
4203 mutex_unlock(&cgroup_mutex
);
4204 /* Release the reference count that we took on the superblock */
4205 deactivate_super(sb
);
4207 ida_simple_remove(&root
->cgroup_ida
, cgrp
->id
);
4209 kfree(rcu_dereference_raw(cgrp
->name
));
4215 cgroup_destroy_locked(cgrp
);
4216 mutex_unlock(&cgroup_mutex
);
4217 mutex_unlock(&dentry
->d_inode
->i_mutex
);
4221 static int cgroup_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
4223 struct cgroup
*c_parent
= dentry
->d_parent
->d_fsdata
;
4225 /* the vfs holds inode->i_mutex already */
4226 return cgroup_create(c_parent
, dentry
, mode
| S_IFDIR
);
4229 static int cgroup_destroy_locked(struct cgroup
*cgrp
)
4230 __releases(&cgroup_mutex
) __acquires(&cgroup_mutex
)
4232 struct dentry
*d
= cgrp
->dentry
;
4233 struct cgroup
*parent
= cgrp
->parent
;
4234 struct cgroup_event
*event
, *tmp
;
4235 struct cgroup_subsys
*ss
;
4237 lockdep_assert_held(&d
->d_inode
->i_mutex
);
4238 lockdep_assert_held(&cgroup_mutex
);
4240 if (atomic_read(&cgrp
->count
) || !list_empty(&cgrp
->children
))
4244 * Block new css_tryget() by deactivating refcnt and mark @cgrp
4245 * removed. This makes future css_tryget() and child creation
4246 * attempts fail thus maintaining the removal conditions verified
4249 for_each_subsys(cgrp
->root
, ss
) {
4250 struct cgroup_subsys_state
*css
= cgrp
->subsys
[ss
->subsys_id
];
4252 WARN_ON(atomic_read(&css
->refcnt
) < 0);
4253 atomic_add(CSS_DEACT_BIAS
, &css
->refcnt
);
4255 set_bit(CGRP_REMOVED
, &cgrp
->flags
);
4257 /* tell subsystems to initate destruction */
4258 for_each_subsys(cgrp
->root
, ss
)
4259 offline_css(ss
, cgrp
);
4262 * Put all the base refs. Each css holds an extra reference to the
4263 * cgroup's dentry and cgroup removal proceeds regardless of css
4264 * refs. On the last put of each css, whenever that may be, the
4265 * extra dentry ref is put so that dentry destruction happens only
4266 * after all css's are released.
4268 for_each_subsys(cgrp
->root
, ss
)
4269 css_put(cgrp
->subsys
[ss
->subsys_id
]);
4271 raw_spin_lock(&release_list_lock
);
4272 if (!list_empty(&cgrp
->release_list
))
4273 list_del_init(&cgrp
->release_list
);
4274 raw_spin_unlock(&release_list_lock
);
4276 /* delete this cgroup from parent->children */
4277 list_del_rcu(&cgrp
->sibling
);
4278 list_del_init(&cgrp
->allcg_node
);
4281 cgroup_d_remove_dir(d
);
4284 set_bit(CGRP_RELEASABLE
, &parent
->flags
);
4285 check_for_release(parent
);
4288 * Unregister events and notify userspace.
4289 * Notify userspace about cgroup removing only after rmdir of cgroup
4290 * directory to avoid race between userspace and kernelspace.
4292 spin_lock(&cgrp
->event_list_lock
);
4293 list_for_each_entry_safe(event
, tmp
, &cgrp
->event_list
, list
) {
4294 list_del_init(&event
->list
);
4295 schedule_work(&event
->remove
);
4297 spin_unlock(&cgrp
->event_list_lock
);
4302 static int cgroup_rmdir(struct inode
*unused_dir
, struct dentry
*dentry
)
4306 mutex_lock(&cgroup_mutex
);
4307 ret
= cgroup_destroy_locked(dentry
->d_fsdata
);
4308 mutex_unlock(&cgroup_mutex
);
4313 static void __init_or_module
cgroup_init_cftsets(struct cgroup_subsys
*ss
)
4315 INIT_LIST_HEAD(&ss
->cftsets
);
4318 * base_cftset is embedded in subsys itself, no need to worry about
4321 if (ss
->base_cftypes
) {
4322 ss
->base_cftset
.cfts
= ss
->base_cftypes
;
4323 list_add_tail(&ss
->base_cftset
.node
, &ss
->cftsets
);
4327 static void __init
cgroup_init_subsys(struct cgroup_subsys
*ss
)
4329 struct cgroup_subsys_state
*css
;
4331 printk(KERN_INFO
"Initializing cgroup subsys %s\n", ss
->name
);
4333 mutex_lock(&cgroup_mutex
);
4335 /* init base cftset */
4336 cgroup_init_cftsets(ss
);
4338 /* Create the top cgroup state for this subsystem */
4339 list_add(&ss
->sibling
, &rootnode
.subsys_list
);
4340 ss
->root
= &rootnode
;
4341 css
= ss
->css_alloc(dummytop
);
4342 /* We don't handle early failures gracefully */
4343 BUG_ON(IS_ERR(css
));
4344 init_cgroup_css(css
, ss
, dummytop
);
4346 /* Update the init_css_set to contain a subsys
4347 * pointer to this state - since the subsystem is
4348 * newly registered, all tasks and hence the
4349 * init_css_set is in the subsystem's top cgroup. */
4350 init_css_set
.subsys
[ss
->subsys_id
] = css
;
4352 need_forkexit_callback
|= ss
->fork
|| ss
->exit
;
4354 /* At system boot, before all subsystems have been
4355 * registered, no tasks have been forked, so we don't
4356 * need to invoke fork callbacks here. */
4357 BUG_ON(!list_empty(&init_task
.tasks
));
4360 BUG_ON(online_css(ss
, dummytop
));
4362 mutex_unlock(&cgroup_mutex
);
4364 /* this function shouldn't be used with modular subsystems, since they
4365 * need to register a subsys_id, among other things */
4370 * cgroup_load_subsys: load and register a modular subsystem at runtime
4371 * @ss: the subsystem to load
4373 * This function should be called in a modular subsystem's initcall. If the
4374 * subsystem is built as a module, it will be assigned a new subsys_id and set
4375 * up for use. If the subsystem is built-in anyway, work is delegated to the
4376 * simpler cgroup_init_subsys.
4378 int __init_or_module
cgroup_load_subsys(struct cgroup_subsys
*ss
)
4380 struct cgroup_subsys_state
*css
;
4382 struct hlist_node
*tmp
;
4386 /* check name and function validity */
4387 if (ss
->name
== NULL
|| strlen(ss
->name
) > MAX_CGROUP_TYPE_NAMELEN
||
4388 ss
->css_alloc
== NULL
|| ss
->css_free
== NULL
)
4392 * we don't support callbacks in modular subsystems. this check is
4393 * before the ss->module check for consistency; a subsystem that could
4394 * be a module should still have no callbacks even if the user isn't
4395 * compiling it as one.
4397 if (ss
->fork
|| ss
->exit
)
4401 * an optionally modular subsystem is built-in: we want to do nothing,
4402 * since cgroup_init_subsys will have already taken care of it.
4404 if (ss
->module
== NULL
) {
4405 /* a sanity check */
4406 BUG_ON(subsys
[ss
->subsys_id
] != ss
);
4410 /* init base cftset */
4411 cgroup_init_cftsets(ss
);
4413 mutex_lock(&cgroup_mutex
);
4414 subsys
[ss
->subsys_id
] = ss
;
4417 * no ss->css_alloc seems to need anything important in the ss
4418 * struct, so this can happen first (i.e. before the rootnode
4421 css
= ss
->css_alloc(dummytop
);
4423 /* failure case - need to deassign the subsys[] slot. */
4424 subsys
[ss
->subsys_id
] = NULL
;
4425 mutex_unlock(&cgroup_mutex
);
4426 return PTR_ERR(css
);
4429 list_add(&ss
->sibling
, &rootnode
.subsys_list
);
4430 ss
->root
= &rootnode
;
4432 /* our new subsystem will be attached to the dummy hierarchy. */
4433 init_cgroup_css(css
, ss
, dummytop
);
4434 /* init_idr must be after init_cgroup_css because it sets css->id. */
4436 ret
= cgroup_init_idr(ss
, css
);
4442 * Now we need to entangle the css into the existing css_sets. unlike
4443 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4444 * will need a new pointer to it; done by iterating the css_set_table.
4445 * furthermore, modifying the existing css_sets will corrupt the hash
4446 * table state, so each changed css_set will need its hash recomputed.
4447 * this is all done under the css_set_lock.
4449 write_lock(&css_set_lock
);
4450 hash_for_each_safe(css_set_table
, i
, tmp
, cg
, hlist
) {
4451 /* skip entries that we already rehashed */
4452 if (cg
->subsys
[ss
->subsys_id
])
4454 /* remove existing entry */
4455 hash_del(&cg
->hlist
);
4457 cg
->subsys
[ss
->subsys_id
] = css
;
4458 /* recompute hash and restore entry */
4459 key
= css_set_hash(cg
->subsys
);
4460 hash_add(css_set_table
, &cg
->hlist
, key
);
4462 write_unlock(&css_set_lock
);
4465 ret
= online_css(ss
, dummytop
);
4470 mutex_unlock(&cgroup_mutex
);
4474 mutex_unlock(&cgroup_mutex
);
4475 /* @ss can't be mounted here as try_module_get() would fail */
4476 cgroup_unload_subsys(ss
);
4479 EXPORT_SYMBOL_GPL(cgroup_load_subsys
);
4482 * cgroup_unload_subsys: unload a modular subsystem
4483 * @ss: the subsystem to unload
4485 * This function should be called in a modular subsystem's exitcall. When this
4486 * function is invoked, the refcount on the subsystem's module will be 0, so
4487 * the subsystem will not be attached to any hierarchy.
4489 void cgroup_unload_subsys(struct cgroup_subsys
*ss
)
4491 struct cg_cgroup_link
*link
;
4493 BUG_ON(ss
->module
== NULL
);
4496 * we shouldn't be called if the subsystem is in use, and the use of
4497 * try_module_get in parse_cgroupfs_options should ensure that it
4498 * doesn't start being used while we're killing it off.
4500 BUG_ON(ss
->root
!= &rootnode
);
4502 mutex_lock(&cgroup_mutex
);
4504 offline_css(ss
, dummytop
);
4508 idr_destroy(&ss
->idr
);
4510 /* deassign the subsys_id */
4511 subsys
[ss
->subsys_id
] = NULL
;
4513 /* remove subsystem from rootnode's list of subsystems */
4514 list_del_init(&ss
->sibling
);
4517 * disentangle the css from all css_sets attached to the dummytop. as
4518 * in loading, we need to pay our respects to the hashtable gods.
4520 write_lock(&css_set_lock
);
4521 list_for_each_entry(link
, &dummytop
->css_sets
, cgrp_link_list
) {
4522 struct css_set
*cg
= link
->cg
;
4525 hash_del(&cg
->hlist
);
4526 cg
->subsys
[ss
->subsys_id
] = NULL
;
4527 key
= css_set_hash(cg
->subsys
);
4528 hash_add(css_set_table
, &cg
->hlist
, key
);
4530 write_unlock(&css_set_lock
);
4533 * remove subsystem's css from the dummytop and free it - need to
4534 * free before marking as null because ss->css_free needs the
4535 * cgrp->subsys pointer to find their state. note that this also
4536 * takes care of freeing the css_id.
4538 ss
->css_free(dummytop
);
4539 dummytop
->subsys
[ss
->subsys_id
] = NULL
;
4541 mutex_unlock(&cgroup_mutex
);
4543 EXPORT_SYMBOL_GPL(cgroup_unload_subsys
);
4546 * cgroup_init_early - cgroup initialization at system boot
4548 * Initialize cgroups at system boot, and initialize any
4549 * subsystems that request early init.
4551 int __init
cgroup_init_early(void)
4554 atomic_set(&init_css_set
.refcount
, 1);
4555 INIT_LIST_HEAD(&init_css_set
.cg_links
);
4556 INIT_LIST_HEAD(&init_css_set
.tasks
);
4557 INIT_HLIST_NODE(&init_css_set
.hlist
);
4559 init_cgroup_root(&rootnode
);
4561 init_task
.cgroups
= &init_css_set
;
4563 init_css_set_link
.cg
= &init_css_set
;
4564 init_css_set_link
.cgrp
= dummytop
;
4565 list_add(&init_css_set_link
.cgrp_link_list
,
4566 &rootnode
.top_cgroup
.css_sets
);
4567 list_add(&init_css_set_link
.cg_link_list
,
4568 &init_css_set
.cg_links
);
4570 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
4571 struct cgroup_subsys
*ss
= subsys
[i
];
4573 /* at bootup time, we don't worry about modular subsystems */
4574 if (!ss
|| ss
->module
)
4578 BUG_ON(strlen(ss
->name
) > MAX_CGROUP_TYPE_NAMELEN
);
4579 BUG_ON(!ss
->css_alloc
);
4580 BUG_ON(!ss
->css_free
);
4581 if (ss
->subsys_id
!= i
) {
4582 printk(KERN_ERR
"cgroup: Subsys %s id == %d\n",
4583 ss
->name
, ss
->subsys_id
);
4588 cgroup_init_subsys(ss
);
4594 * cgroup_init - cgroup initialization
4596 * Register cgroup filesystem and /proc file, and initialize
4597 * any subsystems that didn't request early init.
4599 int __init
cgroup_init(void)
4605 err
= bdi_init(&cgroup_backing_dev_info
);
4609 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
4610 struct cgroup_subsys
*ss
= subsys
[i
];
4612 /* at bootup time, we don't worry about modular subsystems */
4613 if (!ss
|| ss
->module
)
4615 if (!ss
->early_init
)
4616 cgroup_init_subsys(ss
);
4618 cgroup_init_idr(ss
, init_css_set
.subsys
[ss
->subsys_id
]);
4621 /* Add init_css_set to the hash table */
4622 key
= css_set_hash(init_css_set
.subsys
);
4623 hash_add(css_set_table
, &init_css_set
.hlist
, key
);
4624 BUG_ON(!init_root_id(&rootnode
));
4626 cgroup_kobj
= kobject_create_and_add("cgroup", fs_kobj
);
4632 err
= register_filesystem(&cgroup_fs_type
);
4634 kobject_put(cgroup_kobj
);
4638 proc_create("cgroups", 0, NULL
, &proc_cgroupstats_operations
);
4642 bdi_destroy(&cgroup_backing_dev_info
);
4648 * proc_cgroup_show()
4649 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4650 * - Used for /proc/<pid>/cgroup.
4651 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4652 * doesn't really matter if tsk->cgroup changes after we read it,
4653 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
4654 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4655 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4656 * cgroup to top_cgroup.
4659 /* TODO: Use a proper seq_file iterator */
4660 static int proc_cgroup_show(struct seq_file
*m
, void *v
)
4663 struct task_struct
*tsk
;
4666 struct cgroupfs_root
*root
;
4669 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
4675 tsk
= get_pid_task(pid
, PIDTYPE_PID
);
4681 mutex_lock(&cgroup_mutex
);
4683 for_each_active_root(root
) {
4684 struct cgroup_subsys
*ss
;
4685 struct cgroup
*cgrp
;
4688 seq_printf(m
, "%d:", root
->hierarchy_id
);
4689 for_each_subsys(root
, ss
)
4690 seq_printf(m
, "%s%s", count
++ ? "," : "", ss
->name
);
4691 if (strlen(root
->name
))
4692 seq_printf(m
, "%sname=%s", count
? "," : "",
4695 cgrp
= task_cgroup_from_root(tsk
, root
);
4696 retval
= cgroup_path(cgrp
, buf
, PAGE_SIZE
);
4704 mutex_unlock(&cgroup_mutex
);
4705 put_task_struct(tsk
);
4712 static int cgroup_open(struct inode
*inode
, struct file
*file
)
4714 struct pid
*pid
= PROC_I(inode
)->pid
;
4715 return single_open(file
, proc_cgroup_show
, pid
);
4718 const struct file_operations proc_cgroup_operations
= {
4719 .open
= cgroup_open
,
4721 .llseek
= seq_lseek
,
4722 .release
= single_release
,
4725 /* Display information about each subsystem and each hierarchy */
4726 static int proc_cgroupstats_show(struct seq_file
*m
, void *v
)
4730 seq_puts(m
, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
4732 * ideally we don't want subsystems moving around while we do this.
4733 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4734 * subsys/hierarchy state.
4736 mutex_lock(&cgroup_mutex
);
4737 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
4738 struct cgroup_subsys
*ss
= subsys
[i
];
4741 seq_printf(m
, "%s\t%d\t%d\t%d\n",
4742 ss
->name
, ss
->root
->hierarchy_id
,
4743 ss
->root
->number_of_cgroups
, !ss
->disabled
);
4745 mutex_unlock(&cgroup_mutex
);
4749 static int cgroupstats_open(struct inode
*inode
, struct file
*file
)
4751 return single_open(file
, proc_cgroupstats_show
, NULL
);
4754 static const struct file_operations proc_cgroupstats_operations
= {
4755 .open
= cgroupstats_open
,
4757 .llseek
= seq_lseek
,
4758 .release
= single_release
,
4762 * cgroup_fork - attach newly forked task to its parents cgroup.
4763 * @child: pointer to task_struct of forking parent process.
4765 * Description: A task inherits its parent's cgroup at fork().
4767 * A pointer to the shared css_set was automatically copied in
4768 * fork.c by dup_task_struct(). However, we ignore that copy, since
4769 * it was not made under the protection of RCU or cgroup_mutex, so
4770 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
4771 * have already changed current->cgroups, allowing the previously
4772 * referenced cgroup group to be removed and freed.
4774 * At the point that cgroup_fork() is called, 'current' is the parent
4775 * task, and the passed argument 'child' points to the child task.
4777 void cgroup_fork(struct task_struct
*child
)
4780 child
->cgroups
= current
->cgroups
;
4781 get_css_set(child
->cgroups
);
4782 task_unlock(current
);
4783 INIT_LIST_HEAD(&child
->cg_list
);
4787 * cgroup_post_fork - called on a new task after adding it to the task list
4788 * @child: the task in question
4790 * Adds the task to the list running through its css_set if necessary and
4791 * call the subsystem fork() callbacks. Has to be after the task is
4792 * visible on the task list in case we race with the first call to
4793 * cgroup_iter_start() - to guarantee that the new task ends up on its
4796 void cgroup_post_fork(struct task_struct
*child
)
4801 * use_task_css_set_links is set to 1 before we walk the tasklist
4802 * under the tasklist_lock and we read it here after we added the child
4803 * to the tasklist under the tasklist_lock as well. If the child wasn't
4804 * yet in the tasklist when we walked through it from
4805 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
4806 * should be visible now due to the paired locking and barriers implied
4807 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
4808 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
4811 if (use_task_css_set_links
) {
4812 write_lock(&css_set_lock
);
4814 if (list_empty(&child
->cg_list
))
4815 list_add(&child
->cg_list
, &child
->cgroups
->tasks
);
4817 write_unlock(&css_set_lock
);
4821 * Call ss->fork(). This must happen after @child is linked on
4822 * css_set; otherwise, @child might change state between ->fork()
4823 * and addition to css_set.
4825 if (need_forkexit_callback
) {
4827 * fork/exit callbacks are supported only for builtin
4828 * subsystems, and the builtin section of the subsys
4829 * array is immutable, so we don't need to lock the
4830 * subsys array here. On the other hand, modular section
4831 * of the array can be freed at module unload, so we
4834 for (i
= 0; i
< CGROUP_BUILTIN_SUBSYS_COUNT
; i
++) {
4835 struct cgroup_subsys
*ss
= subsys
[i
];
4844 * cgroup_exit - detach cgroup from exiting task
4845 * @tsk: pointer to task_struct of exiting process
4846 * @run_callback: run exit callbacks?
4848 * Description: Detach cgroup from @tsk and release it.
4850 * Note that cgroups marked notify_on_release force every task in
4851 * them to take the global cgroup_mutex mutex when exiting.
4852 * This could impact scaling on very large systems. Be reluctant to
4853 * use notify_on_release cgroups where very high task exit scaling
4854 * is required on large systems.
4856 * the_top_cgroup_hack:
4858 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
4860 * We call cgroup_exit() while the task is still competent to
4861 * handle notify_on_release(), then leave the task attached to the
4862 * root cgroup in each hierarchy for the remainder of its exit.
4864 * To do this properly, we would increment the reference count on
4865 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
4866 * code we would add a second cgroup function call, to drop that
4867 * reference. This would just create an unnecessary hot spot on
4868 * the top_cgroup reference count, to no avail.
4870 * Normally, holding a reference to a cgroup without bumping its
4871 * count is unsafe. The cgroup could go away, or someone could
4872 * attach us to a different cgroup, decrementing the count on
4873 * the first cgroup that we never incremented. But in this case,
4874 * top_cgroup isn't going away, and either task has PF_EXITING set,
4875 * which wards off any cgroup_attach_task() attempts, or task is a failed
4876 * fork, never visible to cgroup_attach_task.
4878 void cgroup_exit(struct task_struct
*tsk
, int run_callbacks
)
4884 * Unlink from the css_set task list if necessary.
4885 * Optimistically check cg_list before taking
4888 if (!list_empty(&tsk
->cg_list
)) {
4889 write_lock(&css_set_lock
);
4890 if (!list_empty(&tsk
->cg_list
))
4891 list_del_init(&tsk
->cg_list
);
4892 write_unlock(&css_set_lock
);
4895 /* Reassign the task to the init_css_set. */
4898 tsk
->cgroups
= &init_css_set
;
4900 if (run_callbacks
&& need_forkexit_callback
) {
4902 * fork/exit callbacks are supported only for builtin
4903 * subsystems, see cgroup_post_fork() for details.
4905 for (i
= 0; i
< CGROUP_BUILTIN_SUBSYS_COUNT
; i
++) {
4906 struct cgroup_subsys
*ss
= subsys
[i
];
4909 struct cgroup
*old_cgrp
=
4910 rcu_dereference_raw(cg
->subsys
[i
])->cgroup
;
4911 struct cgroup
*cgrp
= task_cgroup(tsk
, i
);
4912 ss
->exit(cgrp
, old_cgrp
, tsk
);
4918 put_css_set_taskexit(cg
);
4921 static void check_for_release(struct cgroup
*cgrp
)
4923 /* All of these checks rely on RCU to keep the cgroup
4924 * structure alive */
4925 if (cgroup_is_releasable(cgrp
) &&
4926 !atomic_read(&cgrp
->count
) && list_empty(&cgrp
->children
)) {
4928 * Control Group is currently removeable. If it's not
4929 * already queued for a userspace notification, queue
4932 int need_schedule_work
= 0;
4934 raw_spin_lock(&release_list_lock
);
4935 if (!cgroup_is_removed(cgrp
) &&
4936 list_empty(&cgrp
->release_list
)) {
4937 list_add(&cgrp
->release_list
, &release_list
);
4938 need_schedule_work
= 1;
4940 raw_spin_unlock(&release_list_lock
);
4941 if (need_schedule_work
)
4942 schedule_work(&release_agent_work
);
4946 /* Caller must verify that the css is not for root cgroup */
4947 bool __css_tryget(struct cgroup_subsys_state
*css
)
4952 v
= css_refcnt(css
);
4953 t
= atomic_cmpxchg(&css
->refcnt
, v
, v
+ 1);
4961 EXPORT_SYMBOL_GPL(__css_tryget
);
4963 /* Caller must verify that the css is not for root cgroup */
4964 void __css_put(struct cgroup_subsys_state
*css
)
4968 v
= css_unbias_refcnt(atomic_dec_return(&css
->refcnt
));
4970 schedule_work(&css
->dput_work
);
4972 EXPORT_SYMBOL_GPL(__css_put
);
4975 * Notify userspace when a cgroup is released, by running the
4976 * configured release agent with the name of the cgroup (path
4977 * relative to the root of cgroup file system) as the argument.
4979 * Most likely, this user command will try to rmdir this cgroup.
4981 * This races with the possibility that some other task will be
4982 * attached to this cgroup before it is removed, or that some other
4983 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
4984 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
4985 * unused, and this cgroup will be reprieved from its death sentence,
4986 * to continue to serve a useful existence. Next time it's released,
4987 * we will get notified again, if it still has 'notify_on_release' set.
4989 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
4990 * means only wait until the task is successfully execve()'d. The
4991 * separate release agent task is forked by call_usermodehelper(),
4992 * then control in this thread returns here, without waiting for the
4993 * release agent task. We don't bother to wait because the caller of
4994 * this routine has no use for the exit status of the release agent
4995 * task, so no sense holding our caller up for that.
4997 static void cgroup_release_agent(struct work_struct
*work
)
4999 BUG_ON(work
!= &release_agent_work
);
5000 mutex_lock(&cgroup_mutex
);
5001 raw_spin_lock(&release_list_lock
);
5002 while (!list_empty(&release_list
)) {
5003 char *argv
[3], *envp
[3];
5005 char *pathbuf
= NULL
, *agentbuf
= NULL
;
5006 struct cgroup
*cgrp
= list_entry(release_list
.next
,
5009 list_del_init(&cgrp
->release_list
);
5010 raw_spin_unlock(&release_list_lock
);
5011 pathbuf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
5014 if (cgroup_path(cgrp
, pathbuf
, PAGE_SIZE
) < 0)
5016 agentbuf
= kstrdup(cgrp
->root
->release_agent_path
, GFP_KERNEL
);
5021 argv
[i
++] = agentbuf
;
5022 argv
[i
++] = pathbuf
;
5026 /* minimal command environment */
5027 envp
[i
++] = "HOME=/";
5028 envp
[i
++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5031 /* Drop the lock while we invoke the usermode helper,
5032 * since the exec could involve hitting disk and hence
5033 * be a slow process */
5034 mutex_unlock(&cgroup_mutex
);
5035 call_usermodehelper(argv
[0], argv
, envp
, UMH_WAIT_EXEC
);
5036 mutex_lock(&cgroup_mutex
);
5040 raw_spin_lock(&release_list_lock
);
5042 raw_spin_unlock(&release_list_lock
);
5043 mutex_unlock(&cgroup_mutex
);
5046 static int __init
cgroup_disable(char *str
)
5051 while ((token
= strsep(&str
, ",")) != NULL
) {
5054 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
5055 struct cgroup_subsys
*ss
= subsys
[i
];
5058 * cgroup_disable, being at boot time, can't
5059 * know about module subsystems, so we don't
5062 if (!ss
|| ss
->module
)
5065 if (!strcmp(token
, ss
->name
)) {
5067 printk(KERN_INFO
"Disabling %s control group"
5068 " subsystem\n", ss
->name
);
5075 __setup("cgroup_disable=", cgroup_disable
);
5078 * Functons for CSS ID.
5082 *To get ID other than 0, this should be called when !cgroup_is_removed().
5084 unsigned short css_id(struct cgroup_subsys_state
*css
)
5086 struct css_id
*cssid
;
5089 * This css_id() can return correct value when somone has refcnt
5090 * on this or this is under rcu_read_lock(). Once css->id is allocated,
5091 * it's unchanged until freed.
5093 cssid
= rcu_dereference_check(css
->id
, css_refcnt(css
));
5099 EXPORT_SYMBOL_GPL(css_id
);
5101 unsigned short css_depth(struct cgroup_subsys_state
*css
)
5103 struct css_id
*cssid
;
5105 cssid
= rcu_dereference_check(css
->id
, css_refcnt(css
));
5108 return cssid
->depth
;
5111 EXPORT_SYMBOL_GPL(css_depth
);
5114 * css_is_ancestor - test "root" css is an ancestor of "child"
5115 * @child: the css to be tested.
5116 * @root: the css supporsed to be an ancestor of the child.
5118 * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
5119 * this function reads css->id, the caller must hold rcu_read_lock().
5120 * But, considering usual usage, the csses should be valid objects after test.
5121 * Assuming that the caller will do some action to the child if this returns
5122 * returns true, the caller must take "child";s reference count.
5123 * If "child" is valid object and this returns true, "root" is valid, too.
5126 bool css_is_ancestor(struct cgroup_subsys_state
*child
,
5127 const struct cgroup_subsys_state
*root
)
5129 struct css_id
*child_id
;
5130 struct css_id
*root_id
;
5132 child_id
= rcu_dereference(child
->id
);
5135 root_id
= rcu_dereference(root
->id
);
5138 if (child_id
->depth
< root_id
->depth
)
5140 if (child_id
->stack
[root_id
->depth
] != root_id
->id
)
5145 void free_css_id(struct cgroup_subsys
*ss
, struct cgroup_subsys_state
*css
)
5147 struct css_id
*id
= css
->id
;
5148 /* When this is called before css_id initialization, id can be NULL */
5152 BUG_ON(!ss
->use_id
);
5154 rcu_assign_pointer(id
->css
, NULL
);
5155 rcu_assign_pointer(css
->id
, NULL
);
5156 spin_lock(&ss
->id_lock
);
5157 idr_remove(&ss
->idr
, id
->id
);
5158 spin_unlock(&ss
->id_lock
);
5159 kfree_rcu(id
, rcu_head
);
5161 EXPORT_SYMBOL_GPL(free_css_id
);
5164 * This is called by init or create(). Then, calls to this function are
5165 * always serialized (By cgroup_mutex() at create()).
5168 static struct css_id
*get_new_cssid(struct cgroup_subsys
*ss
, int depth
)
5170 struct css_id
*newid
;
5173 BUG_ON(!ss
->use_id
);
5175 size
= sizeof(*newid
) + sizeof(unsigned short) * (depth
+ 1);
5176 newid
= kzalloc(size
, GFP_KERNEL
);
5178 return ERR_PTR(-ENOMEM
);
5180 idr_preload(GFP_KERNEL
);
5181 spin_lock(&ss
->id_lock
);
5182 /* Don't use 0. allocates an ID of 1-65535 */
5183 ret
= idr_alloc(&ss
->idr
, newid
, 1, CSS_ID_MAX
+ 1, GFP_NOWAIT
);
5184 spin_unlock(&ss
->id_lock
);
5187 /* Returns error when there are no free spaces for new ID.*/
5192 newid
->depth
= depth
;
5196 return ERR_PTR(ret
);
5200 static int __init_or_module
cgroup_init_idr(struct cgroup_subsys
*ss
,
5201 struct cgroup_subsys_state
*rootcss
)
5203 struct css_id
*newid
;
5205 spin_lock_init(&ss
->id_lock
);
5208 newid
= get_new_cssid(ss
, 0);
5210 return PTR_ERR(newid
);
5212 newid
->stack
[0] = newid
->id
;
5213 newid
->css
= rootcss
;
5214 rootcss
->id
= newid
;
5218 static int alloc_css_id(struct cgroup_subsys
*ss
, struct cgroup
*parent
,
5219 struct cgroup
*child
)
5221 int subsys_id
, i
, depth
= 0;
5222 struct cgroup_subsys_state
*parent_css
, *child_css
;
5223 struct css_id
*child_id
, *parent_id
;
5225 subsys_id
= ss
->subsys_id
;
5226 parent_css
= parent
->subsys
[subsys_id
];
5227 child_css
= child
->subsys
[subsys_id
];
5228 parent_id
= parent_css
->id
;
5229 depth
= parent_id
->depth
+ 1;
5231 child_id
= get_new_cssid(ss
, depth
);
5232 if (IS_ERR(child_id
))
5233 return PTR_ERR(child_id
);
5235 for (i
= 0; i
< depth
; i
++)
5236 child_id
->stack
[i
] = parent_id
->stack
[i
];
5237 child_id
->stack
[depth
] = child_id
->id
;
5239 * child_id->css pointer will be set after this cgroup is available
5240 * see cgroup_populate_dir()
5242 rcu_assign_pointer(child_css
->id
, child_id
);
5248 * css_lookup - lookup css by id
5249 * @ss: cgroup subsys to be looked into.
5252 * Returns pointer to cgroup_subsys_state if there is valid one with id.
5253 * NULL if not. Should be called under rcu_read_lock()
5255 struct cgroup_subsys_state
*css_lookup(struct cgroup_subsys
*ss
, int id
)
5257 struct css_id
*cssid
= NULL
;
5259 BUG_ON(!ss
->use_id
);
5260 cssid
= idr_find(&ss
->idr
, id
);
5262 if (unlikely(!cssid
))
5265 return rcu_dereference(cssid
->css
);
5267 EXPORT_SYMBOL_GPL(css_lookup
);
5270 * css_get_next - lookup next cgroup under specified hierarchy.
5271 * @ss: pointer to subsystem
5272 * @id: current position of iteration.
5273 * @root: pointer to css. search tree under this.
5274 * @foundid: position of found object.
5276 * Search next css under the specified hierarchy of rootid. Calling under
5277 * rcu_read_lock() is necessary. Returns NULL if it reaches the end.
5279 struct cgroup_subsys_state
*
5280 css_get_next(struct cgroup_subsys
*ss
, int id
,
5281 struct cgroup_subsys_state
*root
, int *foundid
)
5283 struct cgroup_subsys_state
*ret
= NULL
;
5286 int rootid
= css_id(root
);
5287 int depth
= css_depth(root
);
5292 BUG_ON(!ss
->use_id
);
5293 WARN_ON_ONCE(!rcu_read_lock_held());
5295 /* fill start point for scan */
5299 * scan next entry from bitmap(tree), tmpid is updated after
5302 tmp
= idr_get_next(&ss
->idr
, &tmpid
);
5305 if (tmp
->depth
>= depth
&& tmp
->stack
[depth
] == rootid
) {
5306 ret
= rcu_dereference(tmp
->css
);
5312 /* continue to scan from next id */
5319 * get corresponding css from file open on cgroupfs directory
5321 struct cgroup_subsys_state
*cgroup_css_from_dir(struct file
*f
, int id
)
5323 struct cgroup
*cgrp
;
5324 struct inode
*inode
;
5325 struct cgroup_subsys_state
*css
;
5327 inode
= file_inode(f
);
5328 /* check in cgroup filesystem dir */
5329 if (inode
->i_op
!= &cgroup_dir_inode_operations
)
5330 return ERR_PTR(-EBADF
);
5332 if (id
< 0 || id
>= CGROUP_SUBSYS_COUNT
)
5333 return ERR_PTR(-EINVAL
);
5336 cgrp
= __d_cgrp(f
->f_dentry
);
5337 css
= cgrp
->subsys
[id
];
5338 return css
? css
: ERR_PTR(-ENOENT
);
5341 #ifdef CONFIG_CGROUP_DEBUG
5342 static struct cgroup_subsys_state
*debug_css_alloc(struct cgroup
*cont
)
5344 struct cgroup_subsys_state
*css
= kzalloc(sizeof(*css
), GFP_KERNEL
);
5347 return ERR_PTR(-ENOMEM
);
5352 static void debug_css_free(struct cgroup
*cont
)
5354 kfree(cont
->subsys
[debug_subsys_id
]);
5357 static u64
cgroup_refcount_read(struct cgroup
*cont
, struct cftype
*cft
)
5359 return atomic_read(&cont
->count
);
5362 static u64
debug_taskcount_read(struct cgroup
*cont
, struct cftype
*cft
)
5364 return cgroup_task_count(cont
);
5367 static u64
current_css_set_read(struct cgroup
*cont
, struct cftype
*cft
)
5369 return (u64
)(unsigned long)current
->cgroups
;
5372 static u64
current_css_set_refcount_read(struct cgroup
*cont
,
5378 count
= atomic_read(¤t
->cgroups
->refcount
);
5383 static int current_css_set_cg_links_read(struct cgroup
*cont
,
5385 struct seq_file
*seq
)
5387 struct cg_cgroup_link
*link
;
5390 read_lock(&css_set_lock
);
5392 cg
= rcu_dereference(current
->cgroups
);
5393 list_for_each_entry(link
, &cg
->cg_links
, cg_link_list
) {
5394 struct cgroup
*c
= link
->cgrp
;
5398 name
= c
->dentry
->d_name
.name
;
5401 seq_printf(seq
, "Root %d group %s\n",
5402 c
->root
->hierarchy_id
, name
);
5405 read_unlock(&css_set_lock
);
5409 #define MAX_TASKS_SHOWN_PER_CSS 25
5410 static int cgroup_css_links_read(struct cgroup
*cont
,
5412 struct seq_file
*seq
)
5414 struct cg_cgroup_link
*link
;
5416 read_lock(&css_set_lock
);
5417 list_for_each_entry(link
, &cont
->css_sets
, cgrp_link_list
) {
5418 struct css_set
*cg
= link
->cg
;
5419 struct task_struct
*task
;
5421 seq_printf(seq
, "css_set %p\n", cg
);
5422 list_for_each_entry(task
, &cg
->tasks
, cg_list
) {
5423 if (count
++ > MAX_TASKS_SHOWN_PER_CSS
) {
5424 seq_puts(seq
, " ...\n");
5427 seq_printf(seq
, " task %d\n",
5428 task_pid_vnr(task
));
5432 read_unlock(&css_set_lock
);
5436 static u64
releasable_read(struct cgroup
*cgrp
, struct cftype
*cft
)
5438 return test_bit(CGRP_RELEASABLE
, &cgrp
->flags
);
5441 static struct cftype debug_files
[] = {
5443 .name
= "cgroup_refcount",
5444 .read_u64
= cgroup_refcount_read
,
5447 .name
= "taskcount",
5448 .read_u64
= debug_taskcount_read
,
5452 .name
= "current_css_set",
5453 .read_u64
= current_css_set_read
,
5457 .name
= "current_css_set_refcount",
5458 .read_u64
= current_css_set_refcount_read
,
5462 .name
= "current_css_set_cg_links",
5463 .read_seq_string
= current_css_set_cg_links_read
,
5467 .name
= "cgroup_css_links",
5468 .read_seq_string
= cgroup_css_links_read
,
5472 .name
= "releasable",
5473 .read_u64
= releasable_read
,
5479 struct cgroup_subsys debug_subsys
= {
5481 .css_alloc
= debug_css_alloc
,
5482 .css_free
= debug_css_free
,
5483 .subsys_id
= debug_subsys_id
,
5484 .base_cftypes
= debug_files
,
5486 #endif /* CONFIG_CGROUP_DEBUG */