2 * Generic process-grouping system.
4 * Based originally on the cpuset system, extracted by Paul Menage
5 * Copyright (C) 2006 Google, Inc
7 * Notifications support
8 * Copyright (C) 2009 Nokia Corporation
9 * Author: Kirill A. Shutemov
11 * Copyright notices from the original cpuset code:
12 * --------------------------------------------------
13 * Copyright (C) 2003 BULL SA.
14 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
16 * Portions derived from Patrick Mochel's sysfs code.
17 * sysfs is Copyright (c) 2001-3 Patrick Mochel
19 * 2003-10-10 Written by Simon Derr.
20 * 2003-10-22 Updates by Stephen Hemminger.
21 * 2004 May-July Rework by Paul Jackson.
22 * ---------------------------------------------------
24 * This file is subject to the terms and conditions of the GNU General Public
25 * License. See the file COPYING in the main directory of the Linux
26 * distribution for more details.
29 #include <linux/cgroup.h>
30 #include <linux/cred.h>
31 #include <linux/ctype.h>
32 #include <linux/errno.h>
33 #include <linux/init_task.h>
34 #include <linux/kernel.h>
35 #include <linux/list.h>
37 #include <linux/mutex.h>
38 #include <linux/mount.h>
39 #include <linux/pagemap.h>
40 #include <linux/proc_fs.h>
41 #include <linux/rcupdate.h>
42 #include <linux/sched.h>
43 #include <linux/backing-dev.h>
44 #include <linux/seq_file.h>
45 #include <linux/slab.h>
46 #include <linux/magic.h>
47 #include <linux/spinlock.h>
48 #include <linux/string.h>
49 #include <linux/sort.h>
50 #include <linux/kmod.h>
51 #include <linux/module.h>
52 #include <linux/delayacct.h>
53 #include <linux/cgroupstats.h>
54 #include <linux/hashtable.h>
55 #include <linux/namei.h>
56 #include <linux/pid_namespace.h>
57 #include <linux/idr.h>
58 #include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
59 #include <linux/eventfd.h>
60 #include <linux/poll.h>
61 #include <linux/flex_array.h> /* used in cgroup_attach_task */
62 #include <linux/kthread.h>
64 #include <linux/atomic.h>
67 * cgroup_mutex is the master lock. Any modification to cgroup or its
68 * hierarchy must be performed while holding it.
70 * cgroup_root_mutex nests inside cgroup_mutex and should be held to modify
71 * cgroupfs_root of any cgroup hierarchy - subsys list, flags,
72 * release_agent_path and so on. Modifying requires both cgroup_mutex and
73 * cgroup_root_mutex. Readers can acquire either of the two. This is to
74 * break the following locking order cycle.
76 * A. cgroup_mutex -> cred_guard_mutex -> s_type->i_mutex_key -> namespace_sem
77 * B. namespace_sem -> cgroup_mutex
79 * B happens only through cgroup_show_options() and using cgroup_root_mutex
82 #ifdef CONFIG_PROVE_RCU
83 DEFINE_MUTEX(cgroup_mutex
);
84 EXPORT_SYMBOL_GPL(cgroup_mutex
); /* only for lockdep */
86 static DEFINE_MUTEX(cgroup_mutex
);
89 static DEFINE_MUTEX(cgroup_root_mutex
);
92 * Generate an array of cgroup subsystem pointers. At boot time, this is
93 * populated with the built in subsystems, and modular subsystems are
94 * registered after that. The mutable section of this array is protected by
97 #define SUBSYS(_x) [_x ## _subsys_id] = &_x ## _subsys,
98 #define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option)
99 static struct cgroup_subsys
*cgroup_subsys
[CGROUP_SUBSYS_COUNT
] = {
100 #include <linux/cgroup_subsys.h>
104 * The dummy hierarchy, reserved for the subsystems that are otherwise
105 * unattached - it never has more than a single cgroup, and all tasks are
106 * part of that cgroup.
108 static struct cgroupfs_root cgroup_dummy_root
;
110 /* dummy_top is a shorthand for the dummy hierarchy's top cgroup */
111 static struct cgroup
* const cgroup_dummy_top
= &cgroup_dummy_root
.top_cgroup
;
114 * cgroupfs file entry, pointed to from leaf dentry->d_fsdata.
117 struct list_head node
;
118 struct dentry
*dentry
;
120 struct cgroup_subsys_state
*css
;
123 struct simple_xattrs xattrs
;
127 * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when
128 * cgroup_subsys->use_id != 0.
130 #define CSS_ID_MAX (65535)
133 * The css to which this ID points. This pointer is set to valid value
134 * after cgroup is populated. If cgroup is removed, this will be NULL.
135 * This pointer is expected to be RCU-safe because destroy()
136 * is called after synchronize_rcu(). But for safe use, css_tryget()
137 * should be used for avoiding race.
139 struct cgroup_subsys_state __rcu
*css
;
145 * Depth in hierarchy which this ID belongs to.
147 unsigned short depth
;
149 * ID is freed by RCU. (and lookup routine is RCU safe.)
151 struct rcu_head rcu_head
;
153 * Hierarchy of CSS ID belongs to.
155 unsigned short stack
[0]; /* Array of Length (depth+1) */
159 * cgroup_event represents events which userspace want to receive.
161 struct cgroup_event
{
163 * css which the event belongs to.
165 struct cgroup_subsys_state
*css
;
167 * Control file which the event associated.
171 * eventfd to signal userspace about the event.
173 struct eventfd_ctx
*eventfd
;
175 * Each of these stored in a list by the cgroup.
177 struct list_head list
;
179 * All fields below needed to unregister event when
180 * userspace closes eventfd.
183 wait_queue_head_t
*wqh
;
185 struct work_struct remove
;
188 /* The list of hierarchy roots */
190 static LIST_HEAD(cgroup_roots
);
191 static int cgroup_root_count
;
194 * Hierarchy ID allocation and mapping. It follows the same exclusion
195 * rules as other root ops - both cgroup_mutex and cgroup_root_mutex for
196 * writes, either for reads.
198 static DEFINE_IDR(cgroup_hierarchy_idr
);
200 static struct cgroup_name root_cgroup_name
= { .name
= "/" };
203 * Assign a monotonically increasing serial number to cgroups. It
204 * guarantees cgroups with bigger numbers are newer than those with smaller
205 * numbers. Also, as cgroups are always appended to the parent's
206 * ->children list, it guarantees that sibling cgroups are always sorted in
207 * the ascending serial number order on the list. Protected by
210 static u64 cgroup_serial_nr_next
= 1;
212 /* This flag indicates whether tasks in the fork and exit paths should
213 * check for fork/exit handlers to call. This avoids us having to do
214 * extra work in the fork/exit path if none of the subsystems need to
217 static int need_forkexit_callback __read_mostly
;
219 static struct cftype cgroup_base_files
[];
221 static void cgroup_destroy_css_killed(struct cgroup
*cgrp
);
222 static int cgroup_destroy_locked(struct cgroup
*cgrp
);
223 static int cgroup_addrm_files(struct cgroup
*cgrp
, struct cftype cfts
[],
227 * cgroup_css - obtain a cgroup's css for the specified subsystem
228 * @cgrp: the cgroup of interest
229 * @ss: the subsystem of interest (%NULL returns the dummy_css)
231 * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This
232 * function must be called either under cgroup_mutex or rcu_read_lock() and
233 * the caller is responsible for pinning the returned css if it wants to
234 * keep accessing it outside the said locks. This function may return
235 * %NULL if @cgrp doesn't have @subsys_id enabled.
237 static struct cgroup_subsys_state
*cgroup_css(struct cgroup
*cgrp
,
238 struct cgroup_subsys
*ss
)
241 return rcu_dereference_check(cgrp
->subsys
[ss
->subsys_id
],
242 lockdep_is_held(&cgroup_mutex
));
244 return &cgrp
->dummy_css
;
247 /* convenient tests for these bits */
248 static inline bool cgroup_is_dead(const struct cgroup
*cgrp
)
250 return test_bit(CGRP_DEAD
, &cgrp
->flags
);
254 * cgroup_is_descendant - test ancestry
255 * @cgrp: the cgroup to be tested
256 * @ancestor: possible ancestor of @cgrp
258 * Test whether @cgrp is a descendant of @ancestor. It also returns %true
259 * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
260 * and @ancestor are accessible.
262 bool cgroup_is_descendant(struct cgroup
*cgrp
, struct cgroup
*ancestor
)
265 if (cgrp
== ancestor
)
271 EXPORT_SYMBOL_GPL(cgroup_is_descendant
);
273 static int cgroup_is_releasable(const struct cgroup
*cgrp
)
276 (1 << CGRP_RELEASABLE
) |
277 (1 << CGRP_NOTIFY_ON_RELEASE
);
278 return (cgrp
->flags
& bits
) == bits
;
281 static int notify_on_release(const struct cgroup
*cgrp
)
283 return test_bit(CGRP_NOTIFY_ON_RELEASE
, &cgrp
->flags
);
287 * for_each_subsys - iterate all loaded cgroup subsystems
288 * @ss: the iteration cursor
289 * @i: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
291 * Should be called under cgroup_mutex.
293 #define for_each_subsys(ss, i) \
294 for ((i) = 0; (i) < CGROUP_SUBSYS_COUNT; (i)++) \
295 if (({ lockdep_assert_held(&cgroup_mutex); \
296 !((ss) = cgroup_subsys[i]); })) { } \
300 * for_each_builtin_subsys - iterate all built-in cgroup subsystems
301 * @ss: the iteration cursor
302 * @i: the index of @ss, CGROUP_BUILTIN_SUBSYS_COUNT after reaching the end
304 * Bulit-in subsystems are always present and iteration itself doesn't
305 * require any synchronization.
307 #define for_each_builtin_subsys(ss, i) \
308 for ((i) = 0; (i) < CGROUP_BUILTIN_SUBSYS_COUNT && \
309 (((ss) = cgroup_subsys[i]) || true); (i)++)
311 /* iterate each subsystem attached to a hierarchy */
312 #define for_each_root_subsys(root, ss) \
313 list_for_each_entry((ss), &(root)->subsys_list, sibling)
315 /* iterate across the active hierarchies */
316 #define for_each_active_root(root) \
317 list_for_each_entry((root), &cgroup_roots, root_list)
319 static inline struct cgroup
*__d_cgrp(struct dentry
*dentry
)
321 return dentry
->d_fsdata
;
324 static inline struct cfent
*__d_cfe(struct dentry
*dentry
)
326 return dentry
->d_fsdata
;
329 static inline struct cftype
*__d_cft(struct dentry
*dentry
)
331 return __d_cfe(dentry
)->type
;
335 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
336 * @cgrp: the cgroup to be checked for liveness
338 * On success, returns true; the mutex should be later unlocked. On
339 * failure returns false with no lock held.
341 static bool cgroup_lock_live_group(struct cgroup
*cgrp
)
343 mutex_lock(&cgroup_mutex
);
344 if (cgroup_is_dead(cgrp
)) {
345 mutex_unlock(&cgroup_mutex
);
351 /* the list of cgroups eligible for automatic release. Protected by
352 * release_list_lock */
353 static LIST_HEAD(release_list
);
354 static DEFINE_RAW_SPINLOCK(release_list_lock
);
355 static void cgroup_release_agent(struct work_struct
*work
);
356 static DECLARE_WORK(release_agent_work
, cgroup_release_agent
);
357 static void check_for_release(struct cgroup
*cgrp
);
360 * A cgroup can be associated with multiple css_sets as different tasks may
361 * belong to different cgroups on different hierarchies. In the other
362 * direction, a css_set is naturally associated with multiple cgroups.
363 * This M:N relationship is represented by the following link structure
364 * which exists for each association and allows traversing the associations
367 struct cgrp_cset_link
{
368 /* the cgroup and css_set this link associates */
370 struct css_set
*cset
;
372 /* list of cgrp_cset_links anchored at cgrp->cset_links */
373 struct list_head cset_link
;
375 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
376 struct list_head cgrp_link
;
379 /* The default css_set - used by init and its children prior to any
380 * hierarchies being mounted. It contains a pointer to the root state
381 * for each subsystem. Also used to anchor the list of css_sets. Not
382 * reference-counted, to improve performance when child cgroups
383 * haven't been created.
386 static struct css_set init_css_set
;
387 static struct cgrp_cset_link init_cgrp_cset_link
;
389 static int cgroup_init_idr(struct cgroup_subsys
*ss
,
390 struct cgroup_subsys_state
*css
);
393 * css_set_lock protects the list of css_set objects, and the chain of
394 * tasks off each css_set. Nests outside task->alloc_lock due to
395 * css_task_iter_start().
397 static DEFINE_RWLOCK(css_set_lock
);
398 static int css_set_count
;
401 * hash table for cgroup groups. This improves the performance to find
402 * an existing css_set. This hash doesn't (currently) take into
403 * account cgroups in empty hierarchies.
405 #define CSS_SET_HASH_BITS 7
406 static DEFINE_HASHTABLE(css_set_table
, CSS_SET_HASH_BITS
);
408 static unsigned long css_set_hash(struct cgroup_subsys_state
*css
[])
410 unsigned long key
= 0UL;
411 struct cgroup_subsys
*ss
;
414 for_each_subsys(ss
, i
)
415 key
+= (unsigned long)css
[i
];
416 key
= (key
>> 16) ^ key
;
422 * We don't maintain the lists running through each css_set to its task
423 * until after the first call to css_task_iter_start(). This reduces the
424 * fork()/exit() overhead for people who have cgroups compiled into their
425 * kernel but not actually in use.
427 static int use_task_css_set_links __read_mostly
;
429 static void __put_css_set(struct css_set
*cset
, int taskexit
)
431 struct cgrp_cset_link
*link
, *tmp_link
;
434 * Ensure that the refcount doesn't hit zero while any readers
435 * can see it. Similar to atomic_dec_and_lock(), but for an
438 if (atomic_add_unless(&cset
->refcount
, -1, 1))
440 write_lock(&css_set_lock
);
441 if (!atomic_dec_and_test(&cset
->refcount
)) {
442 write_unlock(&css_set_lock
);
446 /* This css_set is dead. unlink it and release cgroup refcounts */
447 hash_del(&cset
->hlist
);
450 list_for_each_entry_safe(link
, tmp_link
, &cset
->cgrp_links
, cgrp_link
) {
451 struct cgroup
*cgrp
= link
->cgrp
;
453 list_del(&link
->cset_link
);
454 list_del(&link
->cgrp_link
);
456 /* @cgrp can't go away while we're holding css_set_lock */
457 if (list_empty(&cgrp
->cset_links
) && notify_on_release(cgrp
)) {
459 set_bit(CGRP_RELEASABLE
, &cgrp
->flags
);
460 check_for_release(cgrp
);
466 write_unlock(&css_set_lock
);
467 kfree_rcu(cset
, rcu_head
);
471 * refcounted get/put for css_set objects
473 static inline void get_css_set(struct css_set
*cset
)
475 atomic_inc(&cset
->refcount
);
478 static inline void put_css_set(struct css_set
*cset
)
480 __put_css_set(cset
, 0);
483 static inline void put_css_set_taskexit(struct css_set
*cset
)
485 __put_css_set(cset
, 1);
489 * compare_css_sets - helper function for find_existing_css_set().
490 * @cset: candidate css_set being tested
491 * @old_cset: existing css_set for a task
492 * @new_cgrp: cgroup that's being entered by the task
493 * @template: desired set of css pointers in css_set (pre-calculated)
495 * Returns true if "cset" matches "old_cset" except for the hierarchy
496 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
498 static bool compare_css_sets(struct css_set
*cset
,
499 struct css_set
*old_cset
,
500 struct cgroup
*new_cgrp
,
501 struct cgroup_subsys_state
*template[])
503 struct list_head
*l1
, *l2
;
505 if (memcmp(template, cset
->subsys
, sizeof(cset
->subsys
))) {
506 /* Not all subsystems matched */
511 * Compare cgroup pointers in order to distinguish between
512 * different cgroups in heirarchies with no subsystems. We
513 * could get by with just this check alone (and skip the
514 * memcmp above) but on most setups the memcmp check will
515 * avoid the need for this more expensive check on almost all
519 l1
= &cset
->cgrp_links
;
520 l2
= &old_cset
->cgrp_links
;
522 struct cgrp_cset_link
*link1
, *link2
;
523 struct cgroup
*cgrp1
, *cgrp2
;
527 /* See if we reached the end - both lists are equal length. */
528 if (l1
== &cset
->cgrp_links
) {
529 BUG_ON(l2
!= &old_cset
->cgrp_links
);
532 BUG_ON(l2
== &old_cset
->cgrp_links
);
534 /* Locate the cgroups associated with these links. */
535 link1
= list_entry(l1
, struct cgrp_cset_link
, cgrp_link
);
536 link2
= list_entry(l2
, struct cgrp_cset_link
, cgrp_link
);
539 /* Hierarchies should be linked in the same order. */
540 BUG_ON(cgrp1
->root
!= cgrp2
->root
);
543 * If this hierarchy is the hierarchy of the cgroup
544 * that's changing, then we need to check that this
545 * css_set points to the new cgroup; if it's any other
546 * hierarchy, then this css_set should point to the
547 * same cgroup as the old css_set.
549 if (cgrp1
->root
== new_cgrp
->root
) {
550 if (cgrp1
!= new_cgrp
)
561 * find_existing_css_set - init css array and find the matching css_set
562 * @old_cset: the css_set that we're using before the cgroup transition
563 * @cgrp: the cgroup that we're moving into
564 * @template: out param for the new set of csses, should be clear on entry
566 static struct css_set
*find_existing_css_set(struct css_set
*old_cset
,
568 struct cgroup_subsys_state
*template[])
570 struct cgroupfs_root
*root
= cgrp
->root
;
571 struct cgroup_subsys
*ss
;
572 struct css_set
*cset
;
577 * Build the set of subsystem state objects that we want to see in the
578 * new css_set. while subsystems can change globally, the entries here
579 * won't change, so no need for locking.
581 for_each_subsys(ss
, i
) {
582 if (root
->subsys_mask
& (1UL << i
)) {
583 /* Subsystem is in this hierarchy. So we want
584 * the subsystem state from the new
586 template[i
] = cgroup_css(cgrp
, ss
);
588 /* Subsystem is not in this hierarchy, so we
589 * don't want to change the subsystem state */
590 template[i
] = old_cset
->subsys
[i
];
594 key
= css_set_hash(template);
595 hash_for_each_possible(css_set_table
, cset
, hlist
, key
) {
596 if (!compare_css_sets(cset
, old_cset
, cgrp
, template))
599 /* This css_set matches what we need */
603 /* No existing cgroup group matched */
607 static void free_cgrp_cset_links(struct list_head
*links_to_free
)
609 struct cgrp_cset_link
*link
, *tmp_link
;
611 list_for_each_entry_safe(link
, tmp_link
, links_to_free
, cset_link
) {
612 list_del(&link
->cset_link
);
618 * allocate_cgrp_cset_links - allocate cgrp_cset_links
619 * @count: the number of links to allocate
620 * @tmp_links: list_head the allocated links are put on
622 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
623 * through ->cset_link. Returns 0 on success or -errno.
625 static int allocate_cgrp_cset_links(int count
, struct list_head
*tmp_links
)
627 struct cgrp_cset_link
*link
;
630 INIT_LIST_HEAD(tmp_links
);
632 for (i
= 0; i
< count
; i
++) {
633 link
= kzalloc(sizeof(*link
), GFP_KERNEL
);
635 free_cgrp_cset_links(tmp_links
);
638 list_add(&link
->cset_link
, tmp_links
);
644 * link_css_set - a helper function to link a css_set to a cgroup
645 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
646 * @cset: the css_set to be linked
647 * @cgrp: the destination cgroup
649 static void link_css_set(struct list_head
*tmp_links
, struct css_set
*cset
,
652 struct cgrp_cset_link
*link
;
654 BUG_ON(list_empty(tmp_links
));
655 link
= list_first_entry(tmp_links
, struct cgrp_cset_link
, cset_link
);
658 list_move(&link
->cset_link
, &cgrp
->cset_links
);
660 * Always add links to the tail of the list so that the list
661 * is sorted by order of hierarchy creation
663 list_add_tail(&link
->cgrp_link
, &cset
->cgrp_links
);
667 * find_css_set - return a new css_set with one cgroup updated
668 * @old_cset: the baseline css_set
669 * @cgrp: the cgroup to be updated
671 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
672 * substituted into the appropriate hierarchy.
674 static struct css_set
*find_css_set(struct css_set
*old_cset
,
677 struct cgroup_subsys_state
*template[CGROUP_SUBSYS_COUNT
] = { };
678 struct css_set
*cset
;
679 struct list_head tmp_links
;
680 struct cgrp_cset_link
*link
;
683 lockdep_assert_held(&cgroup_mutex
);
685 /* First see if we already have a cgroup group that matches
687 read_lock(&css_set_lock
);
688 cset
= find_existing_css_set(old_cset
, cgrp
, template);
691 read_unlock(&css_set_lock
);
696 cset
= kzalloc(sizeof(*cset
), GFP_KERNEL
);
700 /* Allocate all the cgrp_cset_link objects that we'll need */
701 if (allocate_cgrp_cset_links(cgroup_root_count
, &tmp_links
) < 0) {
706 atomic_set(&cset
->refcount
, 1);
707 INIT_LIST_HEAD(&cset
->cgrp_links
);
708 INIT_LIST_HEAD(&cset
->tasks
);
709 INIT_HLIST_NODE(&cset
->hlist
);
711 /* Copy the set of subsystem state objects generated in
712 * find_existing_css_set() */
713 memcpy(cset
->subsys
, template, sizeof(cset
->subsys
));
715 write_lock(&css_set_lock
);
716 /* Add reference counts and links from the new css_set. */
717 list_for_each_entry(link
, &old_cset
->cgrp_links
, cgrp_link
) {
718 struct cgroup
*c
= link
->cgrp
;
720 if (c
->root
== cgrp
->root
)
722 link_css_set(&tmp_links
, cset
, c
);
725 BUG_ON(!list_empty(&tmp_links
));
729 /* Add this cgroup group to the hash table */
730 key
= css_set_hash(cset
->subsys
);
731 hash_add(css_set_table
, &cset
->hlist
, key
);
733 write_unlock(&css_set_lock
);
739 * Return the cgroup for "task" from the given hierarchy. Must be
740 * called with cgroup_mutex held.
742 static struct cgroup
*task_cgroup_from_root(struct task_struct
*task
,
743 struct cgroupfs_root
*root
)
745 struct css_set
*cset
;
746 struct cgroup
*res
= NULL
;
748 BUG_ON(!mutex_is_locked(&cgroup_mutex
));
749 read_lock(&css_set_lock
);
751 * No need to lock the task - since we hold cgroup_mutex the
752 * task can't change groups, so the only thing that can happen
753 * is that it exits and its css is set back to init_css_set.
755 cset
= task_css_set(task
);
756 if (cset
== &init_css_set
) {
757 res
= &root
->top_cgroup
;
759 struct cgrp_cset_link
*link
;
761 list_for_each_entry(link
, &cset
->cgrp_links
, cgrp_link
) {
762 struct cgroup
*c
= link
->cgrp
;
764 if (c
->root
== root
) {
770 read_unlock(&css_set_lock
);
776 * There is one global cgroup mutex. We also require taking
777 * task_lock() when dereferencing a task's cgroup subsys pointers.
778 * See "The task_lock() exception", at the end of this comment.
780 * A task must hold cgroup_mutex to modify cgroups.
782 * Any task can increment and decrement the count field without lock.
783 * So in general, code holding cgroup_mutex can't rely on the count
784 * field not changing. However, if the count goes to zero, then only
785 * cgroup_attach_task() can increment it again. Because a count of zero
786 * means that no tasks are currently attached, therefore there is no
787 * way a task attached to that cgroup can fork (the other way to
788 * increment the count). So code holding cgroup_mutex can safely
789 * assume that if the count is zero, it will stay zero. Similarly, if
790 * a task holds cgroup_mutex on a cgroup with zero count, it
791 * knows that the cgroup won't be removed, as cgroup_rmdir()
794 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
795 * (usually) take cgroup_mutex. These are the two most performance
796 * critical pieces of code here. The exception occurs on cgroup_exit(),
797 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
798 * is taken, and if the cgroup count is zero, a usermode call made
799 * to the release agent with the name of the cgroup (path relative to
800 * the root of cgroup file system) as the argument.
802 * A cgroup can only be deleted if both its 'count' of using tasks
803 * is zero, and its list of 'children' cgroups is empty. Since all
804 * tasks in the system use _some_ cgroup, and since there is always at
805 * least one task in the system (init, pid == 1), therefore, top_cgroup
806 * always has either children cgroups and/or using tasks. So we don't
807 * need a special hack to ensure that top_cgroup cannot be deleted.
809 * The task_lock() exception
811 * The need for this exception arises from the action of
812 * cgroup_attach_task(), which overwrites one task's cgroup pointer with
813 * another. It does so using cgroup_mutex, however there are
814 * several performance critical places that need to reference
815 * task->cgroup without the expense of grabbing a system global
816 * mutex. Therefore except as noted below, when dereferencing or, as
817 * in cgroup_attach_task(), modifying a task's cgroup pointer we use
818 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
819 * the task_struct routinely used for such matters.
821 * P.S. One more locking exception. RCU is used to guard the
822 * update of a tasks cgroup pointer by cgroup_attach_task()
826 * A couple of forward declarations required, due to cyclic reference loop:
827 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
828 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
832 static int cgroup_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
);
833 static int cgroup_rmdir(struct inode
*unused_dir
, struct dentry
*dentry
);
834 static int cgroup_populate_dir(struct cgroup
*cgrp
, unsigned long subsys_mask
);
835 static const struct inode_operations cgroup_dir_inode_operations
;
836 static const struct file_operations proc_cgroupstats_operations
;
838 static struct backing_dev_info cgroup_backing_dev_info
= {
840 .capabilities
= BDI_CAP_NO_ACCT_AND_WRITEBACK
,
843 static int alloc_css_id(struct cgroup_subsys_state
*child_css
);
845 static struct inode
*cgroup_new_inode(umode_t mode
, struct super_block
*sb
)
847 struct inode
*inode
= new_inode(sb
);
850 inode
->i_ino
= get_next_ino();
851 inode
->i_mode
= mode
;
852 inode
->i_uid
= current_fsuid();
853 inode
->i_gid
= current_fsgid();
854 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
855 inode
->i_mapping
->backing_dev_info
= &cgroup_backing_dev_info
;
860 static struct cgroup_name
*cgroup_alloc_name(struct dentry
*dentry
)
862 struct cgroup_name
*name
;
864 name
= kmalloc(sizeof(*name
) + dentry
->d_name
.len
+ 1, GFP_KERNEL
);
867 strcpy(name
->name
, dentry
->d_name
.name
);
871 static void cgroup_free_fn(struct work_struct
*work
)
873 struct cgroup
*cgrp
= container_of(work
, struct cgroup
, destroy_work
);
875 mutex_lock(&cgroup_mutex
);
876 cgrp
->root
->number_of_cgroups
--;
877 mutex_unlock(&cgroup_mutex
);
880 * We get a ref to the parent's dentry, and put the ref when
881 * this cgroup is being freed, so it's guaranteed that the
882 * parent won't be destroyed before its children.
884 dput(cgrp
->parent
->dentry
);
887 * Drop the active superblock reference that we took when we
888 * created the cgroup. This will free cgrp->root, if we are
889 * holding the last reference to @sb.
891 deactivate_super(cgrp
->root
->sb
);
894 * if we're getting rid of the cgroup, refcount should ensure
895 * that there are no pidlists left.
897 BUG_ON(!list_empty(&cgrp
->pidlists
));
899 simple_xattrs_free(&cgrp
->xattrs
);
901 kfree(rcu_dereference_raw(cgrp
->name
));
905 static void cgroup_free_rcu(struct rcu_head
*head
)
907 struct cgroup
*cgrp
= container_of(head
, struct cgroup
, rcu_head
);
909 INIT_WORK(&cgrp
->destroy_work
, cgroup_free_fn
);
910 schedule_work(&cgrp
->destroy_work
);
913 static void cgroup_diput(struct dentry
*dentry
, struct inode
*inode
)
915 /* is dentry a directory ? if so, kfree() associated cgroup */
916 if (S_ISDIR(inode
->i_mode
)) {
917 struct cgroup
*cgrp
= dentry
->d_fsdata
;
919 BUG_ON(!(cgroup_is_dead(cgrp
)));
920 call_rcu(&cgrp
->rcu_head
, cgroup_free_rcu
);
922 struct cfent
*cfe
= __d_cfe(dentry
);
923 struct cgroup
*cgrp
= dentry
->d_parent
->d_fsdata
;
925 WARN_ONCE(!list_empty(&cfe
->node
) &&
926 cgrp
!= &cgrp
->root
->top_cgroup
,
927 "cfe still linked for %s\n", cfe
->type
->name
);
928 simple_xattrs_free(&cfe
->xattrs
);
934 static int cgroup_delete(const struct dentry
*d
)
939 static void remove_dir(struct dentry
*d
)
941 struct dentry
*parent
= dget(d
->d_parent
);
944 simple_rmdir(parent
->d_inode
, d
);
948 static void cgroup_rm_file(struct cgroup
*cgrp
, const struct cftype
*cft
)
952 lockdep_assert_held(&cgrp
->dentry
->d_inode
->i_mutex
);
953 lockdep_assert_held(&cgroup_mutex
);
956 * If we're doing cleanup due to failure of cgroup_create(),
957 * the corresponding @cfe may not exist.
959 list_for_each_entry(cfe
, &cgrp
->files
, node
) {
960 struct dentry
*d
= cfe
->dentry
;
962 if (cft
&& cfe
->type
!= cft
)
967 simple_unlink(cgrp
->dentry
->d_inode
, d
);
968 list_del_init(&cfe
->node
);
976 * cgroup_clear_dir - remove subsys files in a cgroup directory
977 * @cgrp: target cgroup
978 * @subsys_mask: mask of the subsystem ids whose files should be removed
980 static void cgroup_clear_dir(struct cgroup
*cgrp
, unsigned long subsys_mask
)
982 struct cgroup_subsys
*ss
;
985 for_each_subsys(ss
, i
) {
986 struct cftype_set
*set
;
988 if (!test_bit(i
, &subsys_mask
))
990 list_for_each_entry(set
, &ss
->cftsets
, node
)
991 cgroup_addrm_files(cgrp
, set
->cfts
, false);
996 * NOTE : the dentry must have been dget()'ed
998 static void cgroup_d_remove_dir(struct dentry
*dentry
)
1000 struct dentry
*parent
;
1002 parent
= dentry
->d_parent
;
1003 spin_lock(&parent
->d_lock
);
1004 spin_lock_nested(&dentry
->d_lock
, DENTRY_D_LOCK_NESTED
);
1005 list_del_init(&dentry
->d_u
.d_child
);
1006 spin_unlock(&dentry
->d_lock
);
1007 spin_unlock(&parent
->d_lock
);
1012 * Call with cgroup_mutex held. Drops reference counts on modules, including
1013 * any duplicate ones that parse_cgroupfs_options took. If this function
1014 * returns an error, no reference counts are touched.
1016 static int rebind_subsystems(struct cgroupfs_root
*root
,
1017 unsigned long added_mask
, unsigned removed_mask
)
1019 struct cgroup
*cgrp
= &root
->top_cgroup
;
1020 struct cgroup_subsys
*ss
;
1021 unsigned long pinned
= 0;
1024 BUG_ON(!mutex_is_locked(&cgroup_mutex
));
1025 BUG_ON(!mutex_is_locked(&cgroup_root_mutex
));
1027 /* Check that any added subsystems are currently free */
1028 for_each_subsys(ss
, i
) {
1029 if (!(added_mask
& (1 << i
)))
1032 /* is the subsystem mounted elsewhere? */
1033 if (ss
->root
!= &cgroup_dummy_root
) {
1038 /* pin the module */
1039 if (!try_module_get(ss
->module
)) {
1046 /* subsys could be missing if unloaded between parsing and here */
1047 if (added_mask
!= pinned
) {
1052 ret
= cgroup_populate_dir(cgrp
, added_mask
);
1057 * Nothing can fail from this point on. Remove files for the
1058 * removed subsystems and rebind each subsystem.
1060 cgroup_clear_dir(cgrp
, removed_mask
);
1062 for_each_subsys(ss
, i
) {
1063 unsigned long bit
= 1UL << i
;
1065 if (bit
& added_mask
) {
1066 /* We're binding this subsystem to this hierarchy */
1067 BUG_ON(cgroup_css(cgrp
, ss
));
1068 BUG_ON(!cgroup_css(cgroup_dummy_top
, ss
));
1069 BUG_ON(cgroup_css(cgroup_dummy_top
, ss
)->cgroup
!= cgroup_dummy_top
);
1071 rcu_assign_pointer(cgrp
->subsys
[i
],
1072 cgroup_css(cgroup_dummy_top
, ss
));
1073 cgroup_css(cgrp
, ss
)->cgroup
= cgrp
;
1075 list_move(&ss
->sibling
, &root
->subsys_list
);
1078 ss
->bind(cgroup_css(cgrp
, ss
));
1080 /* refcount was already taken, and we're keeping it */
1081 root
->subsys_mask
|= bit
;
1082 } else if (bit
& removed_mask
) {
1083 /* We're removing this subsystem */
1084 BUG_ON(cgroup_css(cgrp
, ss
) != cgroup_css(cgroup_dummy_top
, ss
));
1085 BUG_ON(cgroup_css(cgrp
, ss
)->cgroup
!= cgrp
);
1088 ss
->bind(cgroup_css(cgroup_dummy_top
, ss
));
1090 cgroup_css(cgroup_dummy_top
, ss
)->cgroup
= cgroup_dummy_top
;
1091 RCU_INIT_POINTER(cgrp
->subsys
[i
], NULL
);
1093 cgroup_subsys
[i
]->root
= &cgroup_dummy_root
;
1094 list_move(&ss
->sibling
, &cgroup_dummy_root
.subsys_list
);
1096 /* subsystem is now free - drop reference on module */
1097 module_put(ss
->module
);
1098 root
->subsys_mask
&= ~bit
;
1103 * Mark @root has finished binding subsystems. @root->subsys_mask
1104 * now matches the bound subsystems.
1106 root
->flags
|= CGRP_ROOT_SUBSYS_BOUND
;
1111 for_each_subsys(ss
, i
)
1112 if (pinned
& (1 << i
))
1113 module_put(ss
->module
);
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_root_subsys(root
, ss
)
1124 seq_printf(seq
, ",%s", ss
->name
);
1125 if (root
->flags
& CGRP_ROOT_SANE_BEHAVIOR
)
1126 seq_puts(seq
, ",sane_behavior");
1127 if (root
->flags
& CGRP_ROOT_NOPREFIX
)
1128 seq_puts(seq
, ",noprefix");
1129 if (root
->flags
& CGRP_ROOT_XATTR
)
1130 seq_puts(seq
, ",xattr");
1131 if (strlen(root
->release_agent_path
))
1132 seq_printf(seq
, ",release_agent=%s", root
->release_agent_path
);
1133 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN
, &root
->top_cgroup
.flags
))
1134 seq_puts(seq
, ",clone_children");
1135 if (strlen(root
->name
))
1136 seq_printf(seq
, ",name=%s", root
->name
);
1137 mutex_unlock(&cgroup_root_mutex
);
1141 struct cgroup_sb_opts
{
1142 unsigned long subsys_mask
;
1143 unsigned long flags
;
1144 char *release_agent
;
1145 bool cpuset_clone_children
;
1147 /* User explicitly requested empty subsystem */
1150 struct cgroupfs_root
*new_root
;
1155 * Convert a hierarchy specifier into a bitmask of subsystems and
1156 * flags. Call with cgroup_mutex held to protect the cgroup_subsys[]
1157 * array. This function takes refcounts on subsystems to be used, unless it
1158 * returns error, in which case no refcounts are taken.
1160 static int parse_cgroupfs_options(char *data
, struct cgroup_sb_opts
*opts
)
1162 char *token
, *o
= data
;
1163 bool all_ss
= false, one_ss
= false;
1164 unsigned long mask
= (unsigned long)-1;
1165 struct cgroup_subsys
*ss
;
1168 BUG_ON(!mutex_is_locked(&cgroup_mutex
));
1170 #ifdef CONFIG_CPUSETS
1171 mask
= ~(1UL << cpuset_subsys_id
);
1174 memset(opts
, 0, sizeof(*opts
));
1176 while ((token
= strsep(&o
, ",")) != NULL
) {
1179 if (!strcmp(token
, "none")) {
1180 /* Explicitly have no subsystems */
1184 if (!strcmp(token
, "all")) {
1185 /* Mutually exclusive option 'all' + subsystem name */
1191 if (!strcmp(token
, "__DEVEL__sane_behavior")) {
1192 opts
->flags
|= CGRP_ROOT_SANE_BEHAVIOR
;
1195 if (!strcmp(token
, "noprefix")) {
1196 opts
->flags
|= CGRP_ROOT_NOPREFIX
;
1199 if (!strcmp(token
, "clone_children")) {
1200 opts
->cpuset_clone_children
= true;
1203 if (!strcmp(token
, "xattr")) {
1204 opts
->flags
|= CGRP_ROOT_XATTR
;
1207 if (!strncmp(token
, "release_agent=", 14)) {
1208 /* Specifying two release agents is forbidden */
1209 if (opts
->release_agent
)
1211 opts
->release_agent
=
1212 kstrndup(token
+ 14, PATH_MAX
- 1, GFP_KERNEL
);
1213 if (!opts
->release_agent
)
1217 if (!strncmp(token
, "name=", 5)) {
1218 const char *name
= token
+ 5;
1219 /* Can't specify an empty name */
1222 /* Must match [\w.-]+ */
1223 for (i
= 0; i
< strlen(name
); i
++) {
1227 if ((c
== '.') || (c
== '-') || (c
== '_'))
1231 /* Specifying two names is forbidden */
1234 opts
->name
= kstrndup(name
,
1235 MAX_CGROUP_ROOT_NAMELEN
- 1,
1243 for_each_subsys(ss
, i
) {
1244 if (strcmp(token
, ss
->name
))
1249 /* Mutually exclusive option 'all' + subsystem name */
1252 set_bit(i
, &opts
->subsys_mask
);
1257 if (i
== CGROUP_SUBSYS_COUNT
)
1262 * If the 'all' option was specified select all the subsystems,
1263 * otherwise if 'none', 'name=' and a subsystem name options
1264 * were not specified, let's default to 'all'
1266 if (all_ss
|| (!one_ss
&& !opts
->none
&& !opts
->name
))
1267 for_each_subsys(ss
, i
)
1269 set_bit(i
, &opts
->subsys_mask
);
1271 /* Consistency checks */
1273 if (opts
->flags
& CGRP_ROOT_SANE_BEHAVIOR
) {
1274 pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
1276 if (opts
->flags
& CGRP_ROOT_NOPREFIX
) {
1277 pr_err("cgroup: sane_behavior: noprefix is not allowed\n");
1281 if (opts
->cpuset_clone_children
) {
1282 pr_err("cgroup: sane_behavior: clone_children is not allowed\n");
1288 * Option noprefix was introduced just for backward compatibility
1289 * with the old cpuset, so we allow noprefix only if mounting just
1290 * the cpuset subsystem.
1292 if ((opts
->flags
& CGRP_ROOT_NOPREFIX
) && (opts
->subsys_mask
& mask
))
1296 /* Can't specify "none" and some subsystems */
1297 if (opts
->subsys_mask
&& opts
->none
)
1301 * We either have to specify by name or by subsystems. (So all
1302 * empty hierarchies must have a name).
1304 if (!opts
->subsys_mask
&& !opts
->name
)
1310 static int cgroup_remount(struct super_block
*sb
, int *flags
, char *data
)
1313 struct cgroupfs_root
*root
= sb
->s_fs_info
;
1314 struct cgroup
*cgrp
= &root
->top_cgroup
;
1315 struct cgroup_sb_opts opts
;
1316 unsigned long added_mask
, removed_mask
;
1318 if (root
->flags
& CGRP_ROOT_SANE_BEHAVIOR
) {
1319 pr_err("cgroup: sane_behavior: remount is not allowed\n");
1323 mutex_lock(&cgrp
->dentry
->d_inode
->i_mutex
);
1324 mutex_lock(&cgroup_mutex
);
1325 mutex_lock(&cgroup_root_mutex
);
1327 /* See what subsystems are wanted */
1328 ret
= parse_cgroupfs_options(data
, &opts
);
1332 if (opts
.subsys_mask
!= root
->subsys_mask
|| opts
.release_agent
)
1333 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1334 task_tgid_nr(current
), current
->comm
);
1336 added_mask
= opts
.subsys_mask
& ~root
->subsys_mask
;
1337 removed_mask
= root
->subsys_mask
& ~opts
.subsys_mask
;
1339 /* Don't allow flags or name to change at remount */
1340 if (((opts
.flags
^ root
->flags
) & CGRP_ROOT_OPTION_MASK
) ||
1341 (opts
.name
&& strcmp(opts
.name
, root
->name
))) {
1342 pr_err("cgroup: option or name mismatch, new: 0x%lx \"%s\", old: 0x%lx \"%s\"\n",
1343 opts
.flags
& CGRP_ROOT_OPTION_MASK
, opts
.name
?: "",
1344 root
->flags
& CGRP_ROOT_OPTION_MASK
, root
->name
);
1349 /* remounting is not allowed for populated hierarchies */
1350 if (root
->number_of_cgroups
> 1) {
1355 ret
= rebind_subsystems(root
, added_mask
, removed_mask
);
1359 if (opts
.release_agent
)
1360 strcpy(root
->release_agent_path
, opts
.release_agent
);
1362 kfree(opts
.release_agent
);
1364 mutex_unlock(&cgroup_root_mutex
);
1365 mutex_unlock(&cgroup_mutex
);
1366 mutex_unlock(&cgrp
->dentry
->d_inode
->i_mutex
);
1370 static const struct super_operations cgroup_ops
= {
1371 .statfs
= simple_statfs
,
1372 .drop_inode
= generic_delete_inode
,
1373 .show_options
= cgroup_show_options
,
1374 .remount_fs
= cgroup_remount
,
1377 static void init_cgroup_housekeeping(struct cgroup
*cgrp
)
1379 INIT_LIST_HEAD(&cgrp
->sibling
);
1380 INIT_LIST_HEAD(&cgrp
->children
);
1381 INIT_LIST_HEAD(&cgrp
->files
);
1382 INIT_LIST_HEAD(&cgrp
->cset_links
);
1383 INIT_LIST_HEAD(&cgrp
->release_list
);
1384 INIT_LIST_HEAD(&cgrp
->pidlists
);
1385 mutex_init(&cgrp
->pidlist_mutex
);
1386 cgrp
->dummy_css
.cgroup
= cgrp
;
1387 INIT_LIST_HEAD(&cgrp
->event_list
);
1388 spin_lock_init(&cgrp
->event_list_lock
);
1389 simple_xattrs_init(&cgrp
->xattrs
);
1392 static void init_cgroup_root(struct cgroupfs_root
*root
)
1394 struct cgroup
*cgrp
= &root
->top_cgroup
;
1396 INIT_LIST_HEAD(&root
->subsys_list
);
1397 INIT_LIST_HEAD(&root
->root_list
);
1398 root
->number_of_cgroups
= 1;
1400 RCU_INIT_POINTER(cgrp
->name
, &root_cgroup_name
);
1401 init_cgroup_housekeeping(cgrp
);
1402 idr_init(&root
->cgroup_idr
);
1405 static int cgroup_init_root_id(struct cgroupfs_root
*root
, int start
, int end
)
1409 lockdep_assert_held(&cgroup_mutex
);
1410 lockdep_assert_held(&cgroup_root_mutex
);
1412 id
= idr_alloc_cyclic(&cgroup_hierarchy_idr
, root
, start
, end
,
1417 root
->hierarchy_id
= id
;
1421 static void cgroup_exit_root_id(struct cgroupfs_root
*root
)
1423 lockdep_assert_held(&cgroup_mutex
);
1424 lockdep_assert_held(&cgroup_root_mutex
);
1426 if (root
->hierarchy_id
) {
1427 idr_remove(&cgroup_hierarchy_idr
, root
->hierarchy_id
);
1428 root
->hierarchy_id
= 0;
1432 static int cgroup_test_super(struct super_block
*sb
, void *data
)
1434 struct cgroup_sb_opts
*opts
= data
;
1435 struct cgroupfs_root
*root
= sb
->s_fs_info
;
1437 /* If we asked for a name then it must match */
1438 if (opts
->name
&& strcmp(opts
->name
, root
->name
))
1442 * If we asked for subsystems (or explicitly for no
1443 * subsystems) then they must match
1445 if ((opts
->subsys_mask
|| opts
->none
)
1446 && (opts
->subsys_mask
!= root
->subsys_mask
))
1452 static struct cgroupfs_root
*cgroup_root_from_opts(struct cgroup_sb_opts
*opts
)
1454 struct cgroupfs_root
*root
;
1456 if (!opts
->subsys_mask
&& !opts
->none
)
1459 root
= kzalloc(sizeof(*root
), GFP_KERNEL
);
1461 return ERR_PTR(-ENOMEM
);
1463 init_cgroup_root(root
);
1466 * We need to set @root->subsys_mask now so that @root can be
1467 * matched by cgroup_test_super() before it finishes
1468 * initialization; otherwise, competing mounts with the same
1469 * options may try to bind the same subsystems instead of waiting
1470 * for the first one leading to unexpected mount errors.
1471 * SUBSYS_BOUND will be set once actual binding is complete.
1473 root
->subsys_mask
= opts
->subsys_mask
;
1474 root
->flags
= opts
->flags
;
1475 if (opts
->release_agent
)
1476 strcpy(root
->release_agent_path
, opts
->release_agent
);
1478 strcpy(root
->name
, opts
->name
);
1479 if (opts
->cpuset_clone_children
)
1480 set_bit(CGRP_CPUSET_CLONE_CHILDREN
, &root
->top_cgroup
.flags
);
1484 static void cgroup_free_root(struct cgroupfs_root
*root
)
1487 /* hierarhcy ID shoulid already have been released */
1488 WARN_ON_ONCE(root
->hierarchy_id
);
1490 idr_destroy(&root
->cgroup_idr
);
1495 static int cgroup_set_super(struct super_block
*sb
, void *data
)
1498 struct cgroup_sb_opts
*opts
= data
;
1500 /* If we don't have a new root, we can't set up a new sb */
1501 if (!opts
->new_root
)
1504 BUG_ON(!opts
->subsys_mask
&& !opts
->none
);
1506 ret
= set_anon_super(sb
, NULL
);
1510 sb
->s_fs_info
= opts
->new_root
;
1511 opts
->new_root
->sb
= sb
;
1513 sb
->s_blocksize
= PAGE_CACHE_SIZE
;
1514 sb
->s_blocksize_bits
= PAGE_CACHE_SHIFT
;
1515 sb
->s_magic
= CGROUP_SUPER_MAGIC
;
1516 sb
->s_op
= &cgroup_ops
;
1521 static int cgroup_get_rootdir(struct super_block
*sb
)
1523 static const struct dentry_operations cgroup_dops
= {
1524 .d_iput
= cgroup_diput
,
1525 .d_delete
= cgroup_delete
,
1528 struct inode
*inode
=
1529 cgroup_new_inode(S_IFDIR
| S_IRUGO
| S_IXUGO
| S_IWUSR
, sb
);
1534 inode
->i_fop
= &simple_dir_operations
;
1535 inode
->i_op
= &cgroup_dir_inode_operations
;
1536 /* directories start off with i_nlink == 2 (for "." entry) */
1538 sb
->s_root
= d_make_root(inode
);
1541 /* for everything else we want ->d_op set */
1542 sb
->s_d_op
= &cgroup_dops
;
1546 static struct dentry
*cgroup_mount(struct file_system_type
*fs_type
,
1547 int flags
, const char *unused_dev_name
,
1550 struct cgroup_sb_opts opts
;
1551 struct cgroupfs_root
*root
;
1553 struct super_block
*sb
;
1554 struct cgroupfs_root
*new_root
;
1555 struct list_head tmp_links
;
1556 struct inode
*inode
;
1557 const struct cred
*cred
;
1559 /* First find the desired set of subsystems */
1560 mutex_lock(&cgroup_mutex
);
1561 ret
= parse_cgroupfs_options(data
, &opts
);
1562 mutex_unlock(&cgroup_mutex
);
1567 * Allocate a new cgroup root. We may not need it if we're
1568 * reusing an existing hierarchy.
1570 new_root
= cgroup_root_from_opts(&opts
);
1571 if (IS_ERR(new_root
)) {
1572 ret
= PTR_ERR(new_root
);
1575 opts
.new_root
= new_root
;
1577 /* Locate an existing or new sb for this hierarchy */
1578 sb
= sget(fs_type
, cgroup_test_super
, cgroup_set_super
, 0, &opts
);
1581 cgroup_free_root(opts
.new_root
);
1585 root
= sb
->s_fs_info
;
1587 if (root
== opts
.new_root
) {
1588 /* We used the new root structure, so this is a new hierarchy */
1589 struct cgroup
*root_cgrp
= &root
->top_cgroup
;
1590 struct cgroupfs_root
*existing_root
;
1592 struct css_set
*cset
;
1594 BUG_ON(sb
->s_root
!= NULL
);
1596 ret
= cgroup_get_rootdir(sb
);
1598 goto drop_new_super
;
1599 inode
= sb
->s_root
->d_inode
;
1601 mutex_lock(&inode
->i_mutex
);
1602 mutex_lock(&cgroup_mutex
);
1603 mutex_lock(&cgroup_root_mutex
);
1605 root_cgrp
->id
= idr_alloc(&root
->cgroup_idr
, root_cgrp
,
1607 if (root_cgrp
->id
< 0)
1610 /* Check for name clashes with existing mounts */
1612 if (strlen(root
->name
))
1613 for_each_active_root(existing_root
)
1614 if (!strcmp(existing_root
->name
, root
->name
))
1618 * We're accessing css_set_count without locking
1619 * css_set_lock here, but that's OK - it can only be
1620 * increased by someone holding cgroup_lock, and
1621 * that's us. The worst that can happen is that we
1622 * have some link structures left over
1624 ret
= allocate_cgrp_cset_links(css_set_count
, &tmp_links
);
1628 /* ID 0 is reserved for dummy root, 1 for unified hierarchy */
1629 ret
= cgroup_init_root_id(root
, 2, 0);
1633 sb
->s_root
->d_fsdata
= root_cgrp
;
1634 root_cgrp
->dentry
= sb
->s_root
;
1637 * We're inside get_sb() and will call lookup_one_len() to
1638 * create the root files, which doesn't work if SELinux is
1639 * in use. The following cred dancing somehow works around
1640 * it. See 2ce9738ba ("cgroupfs: use init_cred when
1641 * populating new cgroupfs mount") for more details.
1643 cred
= override_creds(&init_cred
);
1645 ret
= cgroup_addrm_files(root_cgrp
, cgroup_base_files
, true);
1649 ret
= rebind_subsystems(root
, root
->subsys_mask
, 0);
1656 * There must be no failure case after here, since rebinding
1657 * takes care of subsystems' refcounts, which are explicitly
1658 * dropped in the failure exit path.
1661 list_add(&root
->root_list
, &cgroup_roots
);
1662 cgroup_root_count
++;
1664 /* Link the top cgroup in this hierarchy into all
1665 * the css_set objects */
1666 write_lock(&css_set_lock
);
1667 hash_for_each(css_set_table
, i
, cset
, hlist
)
1668 link_css_set(&tmp_links
, cset
, root_cgrp
);
1669 write_unlock(&css_set_lock
);
1671 free_cgrp_cset_links(&tmp_links
);
1673 BUG_ON(!list_empty(&root_cgrp
->children
));
1674 BUG_ON(root
->number_of_cgroups
!= 1);
1676 mutex_unlock(&cgroup_root_mutex
);
1677 mutex_unlock(&cgroup_mutex
);
1678 mutex_unlock(&inode
->i_mutex
);
1681 * We re-used an existing hierarchy - the new root (if
1682 * any) is not needed
1684 cgroup_free_root(opts
.new_root
);
1686 if ((root
->flags
^ opts
.flags
) & CGRP_ROOT_OPTION_MASK
) {
1687 if ((root
->flags
| opts
.flags
) & CGRP_ROOT_SANE_BEHAVIOR
) {
1688 pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
1690 goto drop_new_super
;
1692 pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n");
1697 kfree(opts
.release_agent
);
1699 return dget(sb
->s_root
);
1702 free_cgrp_cset_links(&tmp_links
);
1703 cgroup_addrm_files(&root
->top_cgroup
, cgroup_base_files
, false);
1706 cgroup_exit_root_id(root
);
1707 mutex_unlock(&cgroup_root_mutex
);
1708 mutex_unlock(&cgroup_mutex
);
1709 mutex_unlock(&inode
->i_mutex
);
1711 deactivate_locked_super(sb
);
1713 kfree(opts
.release_agent
);
1715 return ERR_PTR(ret
);
1718 static void cgroup_kill_sb(struct super_block
*sb
) {
1719 struct cgroupfs_root
*root
= sb
->s_fs_info
;
1720 struct cgroup
*cgrp
= &root
->top_cgroup
;
1721 struct cgrp_cset_link
*link
, *tmp_link
;
1726 BUG_ON(root
->number_of_cgroups
!= 1);
1727 BUG_ON(!list_empty(&cgrp
->children
));
1729 mutex_lock(&cgrp
->dentry
->d_inode
->i_mutex
);
1730 mutex_lock(&cgroup_mutex
);
1731 mutex_lock(&cgroup_root_mutex
);
1733 /* Rebind all subsystems back to the default hierarchy */
1734 if (root
->flags
& CGRP_ROOT_SUBSYS_BOUND
) {
1735 ret
= rebind_subsystems(root
, 0, root
->subsys_mask
);
1736 /* Shouldn't be able to fail ... */
1741 * Release all the links from cset_links to this hierarchy's
1744 write_lock(&css_set_lock
);
1746 list_for_each_entry_safe(link
, tmp_link
, &cgrp
->cset_links
, cset_link
) {
1747 list_del(&link
->cset_link
);
1748 list_del(&link
->cgrp_link
);
1751 write_unlock(&css_set_lock
);
1753 if (!list_empty(&root
->root_list
)) {
1754 list_del(&root
->root_list
);
1755 cgroup_root_count
--;
1758 cgroup_exit_root_id(root
);
1760 mutex_unlock(&cgroup_root_mutex
);
1761 mutex_unlock(&cgroup_mutex
);
1762 mutex_unlock(&cgrp
->dentry
->d_inode
->i_mutex
);
1764 simple_xattrs_free(&cgrp
->xattrs
);
1766 kill_litter_super(sb
);
1767 cgroup_free_root(root
);
1770 static struct file_system_type cgroup_fs_type
= {
1772 .mount
= cgroup_mount
,
1773 .kill_sb
= cgroup_kill_sb
,
1776 static struct kobject
*cgroup_kobj
;
1779 * cgroup_path - generate the path of a cgroup
1780 * @cgrp: the cgroup in question
1781 * @buf: the buffer to write the path into
1782 * @buflen: the length of the buffer
1784 * Writes path of cgroup into buf. Returns 0 on success, -errno on error.
1786 * We can't generate cgroup path using dentry->d_name, as accessing
1787 * dentry->name must be protected by irq-unsafe dentry->d_lock or parent
1788 * inode's i_mutex, while on the other hand cgroup_path() can be called
1789 * with some irq-safe spinlocks held.
1791 int cgroup_path(const struct cgroup
*cgrp
, char *buf
, int buflen
)
1793 int ret
= -ENAMETOOLONG
;
1796 if (!cgrp
->parent
) {
1797 if (strlcpy(buf
, "/", buflen
) >= buflen
)
1798 return -ENAMETOOLONG
;
1802 start
= buf
+ buflen
- 1;
1807 const char *name
= cgroup_name(cgrp
);
1811 if ((start
-= len
) < buf
)
1813 memcpy(start
, name
, len
);
1819 cgrp
= cgrp
->parent
;
1820 } while (cgrp
->parent
);
1822 memmove(buf
, start
, buf
+ buflen
- start
);
1827 EXPORT_SYMBOL_GPL(cgroup_path
);
1830 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
1831 * @task: target task
1832 * @buf: the buffer to write the path into
1833 * @buflen: the length of the buffer
1835 * Determine @task's cgroup on the first (the one with the lowest non-zero
1836 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
1837 * function grabs cgroup_mutex and shouldn't be used inside locks used by
1838 * cgroup controller callbacks.
1840 * Returns 0 on success, fails with -%ENAMETOOLONG if @buflen is too short.
1842 int task_cgroup_path(struct task_struct
*task
, char *buf
, size_t buflen
)
1844 struct cgroupfs_root
*root
;
1845 struct cgroup
*cgrp
;
1846 int hierarchy_id
= 1, ret
= 0;
1849 return -ENAMETOOLONG
;
1851 mutex_lock(&cgroup_mutex
);
1853 root
= idr_get_next(&cgroup_hierarchy_idr
, &hierarchy_id
);
1856 cgrp
= task_cgroup_from_root(task
, root
);
1857 ret
= cgroup_path(cgrp
, buf
, buflen
);
1859 /* if no hierarchy exists, everyone is in "/" */
1860 memcpy(buf
, "/", 2);
1863 mutex_unlock(&cgroup_mutex
);
1866 EXPORT_SYMBOL_GPL(task_cgroup_path
);
1869 * Control Group taskset
1871 struct task_and_cgroup
{
1872 struct task_struct
*task
;
1873 struct cgroup
*cgrp
;
1874 struct css_set
*cset
;
1877 struct cgroup_taskset
{
1878 struct task_and_cgroup single
;
1879 struct flex_array
*tc_array
;
1882 struct cgroup
*cur_cgrp
;
1886 * cgroup_taskset_first - reset taskset and return the first task
1887 * @tset: taskset of interest
1889 * @tset iteration is initialized and the first task is returned.
1891 struct task_struct
*cgroup_taskset_first(struct cgroup_taskset
*tset
)
1893 if (tset
->tc_array
) {
1895 return cgroup_taskset_next(tset
);
1897 tset
->cur_cgrp
= tset
->single
.cgrp
;
1898 return tset
->single
.task
;
1901 EXPORT_SYMBOL_GPL(cgroup_taskset_first
);
1904 * cgroup_taskset_next - iterate to the next task in taskset
1905 * @tset: taskset of interest
1907 * Return the next task in @tset. Iteration must have been initialized
1908 * with cgroup_taskset_first().
1910 struct task_struct
*cgroup_taskset_next(struct cgroup_taskset
*tset
)
1912 struct task_and_cgroup
*tc
;
1914 if (!tset
->tc_array
|| tset
->idx
>= tset
->tc_array_len
)
1917 tc
= flex_array_get(tset
->tc_array
, tset
->idx
++);
1918 tset
->cur_cgrp
= tc
->cgrp
;
1921 EXPORT_SYMBOL_GPL(cgroup_taskset_next
);
1924 * cgroup_taskset_cur_css - return the matching css for the current task
1925 * @tset: taskset of interest
1926 * @subsys_id: the ID of the target subsystem
1928 * Return the css for the current (last returned) task of @tset for
1929 * subsystem specified by @subsys_id. This function must be preceded by
1930 * either cgroup_taskset_first() or cgroup_taskset_next().
1932 struct cgroup_subsys_state
*cgroup_taskset_cur_css(struct cgroup_taskset
*tset
,
1935 return cgroup_css(tset
->cur_cgrp
, cgroup_subsys
[subsys_id
]);
1937 EXPORT_SYMBOL_GPL(cgroup_taskset_cur_css
);
1940 * cgroup_taskset_size - return the number of tasks in taskset
1941 * @tset: taskset of interest
1943 int cgroup_taskset_size(struct cgroup_taskset
*tset
)
1945 return tset
->tc_array
? tset
->tc_array_len
: 1;
1947 EXPORT_SYMBOL_GPL(cgroup_taskset_size
);
1951 * cgroup_task_migrate - move a task from one cgroup to another.
1953 * Must be called with cgroup_mutex and threadgroup locked.
1955 static void cgroup_task_migrate(struct cgroup
*old_cgrp
,
1956 struct task_struct
*tsk
,
1957 struct css_set
*new_cset
)
1959 struct css_set
*old_cset
;
1962 * We are synchronized through threadgroup_lock() against PF_EXITING
1963 * setting such that we can't race against cgroup_exit() changing the
1964 * css_set to init_css_set and dropping the old one.
1966 WARN_ON_ONCE(tsk
->flags
& PF_EXITING
);
1967 old_cset
= task_css_set(tsk
);
1970 rcu_assign_pointer(tsk
->cgroups
, new_cset
);
1973 /* Update the css_set linked lists if we're using them */
1974 write_lock(&css_set_lock
);
1975 if (!list_empty(&tsk
->cg_list
))
1976 list_move(&tsk
->cg_list
, &new_cset
->tasks
);
1977 write_unlock(&css_set_lock
);
1980 * We just gained a reference on old_cset by taking it from the
1981 * task. As trading it for new_cset is protected by cgroup_mutex,
1982 * we're safe to drop it here; it will be freed under RCU.
1984 set_bit(CGRP_RELEASABLE
, &old_cgrp
->flags
);
1985 put_css_set(old_cset
);
1989 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
1990 * @cgrp: the cgroup to attach to
1991 * @tsk: the task or the leader of the threadgroup to be attached
1992 * @threadgroup: attach the whole threadgroup?
1994 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
1995 * task_lock of @tsk or each thread in the threadgroup individually in turn.
1997 static int cgroup_attach_task(struct cgroup
*cgrp
, struct task_struct
*tsk
,
2000 int retval
, i
, group_size
;
2001 struct cgroup_subsys
*ss
, *failed_ss
= NULL
;
2002 struct cgroupfs_root
*root
= cgrp
->root
;
2003 /* threadgroup list cursor and array */
2004 struct task_struct
*leader
= tsk
;
2005 struct task_and_cgroup
*tc
;
2006 struct flex_array
*group
;
2007 struct cgroup_taskset tset
= { };
2010 * step 0: in order to do expensive, possibly blocking operations for
2011 * every thread, we cannot iterate the thread group list, since it needs
2012 * rcu or tasklist locked. instead, build an array of all threads in the
2013 * group - group_rwsem prevents new threads from appearing, and if
2014 * threads exit, this will just be an over-estimate.
2017 group_size
= get_nr_threads(tsk
);
2020 /* flex_array supports very large thread-groups better than kmalloc. */
2021 group
= flex_array_alloc(sizeof(*tc
), group_size
, GFP_KERNEL
);
2024 /* pre-allocate to guarantee space while iterating in rcu read-side. */
2025 retval
= flex_array_prealloc(group
, 0, group_size
, GFP_KERNEL
);
2027 goto out_free_group_list
;
2031 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2032 * already PF_EXITING could be freed from underneath us unless we
2033 * take an rcu_read_lock.
2037 struct task_and_cgroup ent
;
2039 /* @tsk either already exited or can't exit until the end */
2040 if (tsk
->flags
& PF_EXITING
)
2043 /* as per above, nr_threads may decrease, but not increase. */
2044 BUG_ON(i
>= group_size
);
2046 ent
.cgrp
= task_cgroup_from_root(tsk
, root
);
2047 /* nothing to do if this task is already in the cgroup */
2048 if (ent
.cgrp
== cgrp
)
2051 * saying GFP_ATOMIC has no effect here because we did prealloc
2052 * earlier, but it's good form to communicate our expectations.
2054 retval
= flex_array_put(group
, i
, &ent
, GFP_ATOMIC
);
2055 BUG_ON(retval
!= 0);
2060 } while_each_thread(leader
, tsk
);
2062 /* remember the number of threads in the array for later. */
2064 tset
.tc_array
= group
;
2065 tset
.tc_array_len
= group_size
;
2067 /* methods shouldn't be called if no task is actually migrating */
2070 goto out_free_group_list
;
2073 * step 1: check that we can legitimately attach to the cgroup.
2075 for_each_root_subsys(root
, ss
) {
2076 struct cgroup_subsys_state
*css
= cgroup_css(cgrp
, ss
);
2078 if (ss
->can_attach
) {
2079 retval
= ss
->can_attach(css
, &tset
);
2082 goto out_cancel_attach
;
2088 * step 2: make sure css_sets exist for all threads to be migrated.
2089 * we use find_css_set, which allocates a new one if necessary.
2091 for (i
= 0; i
< group_size
; i
++) {
2092 struct css_set
*old_cset
;
2094 tc
= flex_array_get(group
, i
);
2095 old_cset
= task_css_set(tc
->task
);
2096 tc
->cset
= find_css_set(old_cset
, cgrp
);
2099 goto out_put_css_set_refs
;
2104 * step 3: now that we're guaranteed success wrt the css_sets,
2105 * proceed to move all tasks to the new cgroup. There are no
2106 * failure cases after here, so this is the commit point.
2108 for (i
= 0; i
< group_size
; i
++) {
2109 tc
= flex_array_get(group
, i
);
2110 cgroup_task_migrate(tc
->cgrp
, tc
->task
, tc
->cset
);
2112 /* nothing is sensitive to fork() after this point. */
2115 * step 4: do subsystem attach callbacks.
2117 for_each_root_subsys(root
, ss
) {
2118 struct cgroup_subsys_state
*css
= cgroup_css(cgrp
, ss
);
2121 ss
->attach(css
, &tset
);
2125 * step 5: success! and cleanup
2128 out_put_css_set_refs
:
2130 for (i
= 0; i
< group_size
; i
++) {
2131 tc
= flex_array_get(group
, i
);
2134 put_css_set(tc
->cset
);
2139 for_each_root_subsys(root
, ss
) {
2140 struct cgroup_subsys_state
*css
= cgroup_css(cgrp
, ss
);
2142 if (ss
== failed_ss
)
2144 if (ss
->cancel_attach
)
2145 ss
->cancel_attach(css
, &tset
);
2148 out_free_group_list
:
2149 flex_array_free(group
);
2154 * Find the task_struct of the task to attach by vpid and pass it along to the
2155 * function to attach either it or all tasks in its threadgroup. Will lock
2156 * cgroup_mutex and threadgroup; may take task_lock of task.
2158 static int attach_task_by_pid(struct cgroup
*cgrp
, u64 pid
, bool threadgroup
)
2160 struct task_struct
*tsk
;
2161 const struct cred
*cred
= current_cred(), *tcred
;
2164 if (!cgroup_lock_live_group(cgrp
))
2170 tsk
= find_task_by_vpid(pid
);
2174 goto out_unlock_cgroup
;
2177 * even if we're attaching all tasks in the thread group, we
2178 * only need to check permissions on one of them.
2180 tcred
= __task_cred(tsk
);
2181 if (!uid_eq(cred
->euid
, GLOBAL_ROOT_UID
) &&
2182 !uid_eq(cred
->euid
, tcred
->uid
) &&
2183 !uid_eq(cred
->euid
, tcred
->suid
)) {
2186 goto out_unlock_cgroup
;
2192 tsk
= tsk
->group_leader
;
2195 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
2196 * trapped in a cpuset, or RT worker may be born in a cgroup
2197 * with no rt_runtime allocated. Just say no.
2199 if (tsk
== kthreadd_task
|| (tsk
->flags
& PF_NO_SETAFFINITY
)) {
2202 goto out_unlock_cgroup
;
2205 get_task_struct(tsk
);
2208 threadgroup_lock(tsk
);
2210 if (!thread_group_leader(tsk
)) {
2212 * a race with de_thread from another thread's exec()
2213 * may strip us of our leadership, if this happens,
2214 * there is no choice but to throw this task away and
2215 * try again; this is
2216 * "double-double-toil-and-trouble-check locking".
2218 threadgroup_unlock(tsk
);
2219 put_task_struct(tsk
);
2220 goto retry_find_task
;
2224 ret
= cgroup_attach_task(cgrp
, tsk
, threadgroup
);
2226 threadgroup_unlock(tsk
);
2228 put_task_struct(tsk
);
2230 mutex_unlock(&cgroup_mutex
);
2235 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2236 * @from: attach to all cgroups of a given task
2237 * @tsk: the task to be attached
2239 int cgroup_attach_task_all(struct task_struct
*from
, struct task_struct
*tsk
)
2241 struct cgroupfs_root
*root
;
2244 mutex_lock(&cgroup_mutex
);
2245 for_each_active_root(root
) {
2246 struct cgroup
*from_cgrp
= task_cgroup_from_root(from
, root
);
2248 retval
= cgroup_attach_task(from_cgrp
, tsk
, false);
2252 mutex_unlock(&cgroup_mutex
);
2256 EXPORT_SYMBOL_GPL(cgroup_attach_task_all
);
2258 static int cgroup_tasks_write(struct cgroup_subsys_state
*css
,
2259 struct cftype
*cft
, u64 pid
)
2261 return attach_task_by_pid(css
->cgroup
, pid
, false);
2264 static int cgroup_procs_write(struct cgroup_subsys_state
*css
,
2265 struct cftype
*cft
, u64 tgid
)
2267 return attach_task_by_pid(css
->cgroup
, tgid
, true);
2270 static int cgroup_release_agent_write(struct cgroup_subsys_state
*css
,
2271 struct cftype
*cft
, const char *buffer
)
2273 BUILD_BUG_ON(sizeof(css
->cgroup
->root
->release_agent_path
) < PATH_MAX
);
2274 if (strlen(buffer
) >= PATH_MAX
)
2276 if (!cgroup_lock_live_group(css
->cgroup
))
2278 mutex_lock(&cgroup_root_mutex
);
2279 strcpy(css
->cgroup
->root
->release_agent_path
, buffer
);
2280 mutex_unlock(&cgroup_root_mutex
);
2281 mutex_unlock(&cgroup_mutex
);
2285 static int cgroup_release_agent_show(struct cgroup_subsys_state
*css
,
2286 struct cftype
*cft
, struct seq_file
*seq
)
2288 struct cgroup
*cgrp
= css
->cgroup
;
2290 if (!cgroup_lock_live_group(cgrp
))
2292 seq_puts(seq
, cgrp
->root
->release_agent_path
);
2293 seq_putc(seq
, '\n');
2294 mutex_unlock(&cgroup_mutex
);
2298 static int cgroup_sane_behavior_show(struct cgroup_subsys_state
*css
,
2299 struct cftype
*cft
, struct seq_file
*seq
)
2301 seq_printf(seq
, "%d\n", cgroup_sane_behavior(css
->cgroup
));
2305 /* A buffer size big enough for numbers or short strings */
2306 #define CGROUP_LOCAL_BUFFER_SIZE 64
2308 static ssize_t
cgroup_write_X64(struct cgroup_subsys_state
*css
,
2309 struct cftype
*cft
, struct file
*file
,
2310 const char __user
*userbuf
, size_t nbytes
,
2311 loff_t
*unused_ppos
)
2313 char buffer
[CGROUP_LOCAL_BUFFER_SIZE
];
2319 if (nbytes
>= sizeof(buffer
))
2321 if (copy_from_user(buffer
, userbuf
, nbytes
))
2324 buffer
[nbytes
] = 0; /* nul-terminate */
2325 if (cft
->write_u64
) {
2326 u64 val
= simple_strtoull(strstrip(buffer
), &end
, 0);
2329 retval
= cft
->write_u64(css
, cft
, val
);
2331 s64 val
= simple_strtoll(strstrip(buffer
), &end
, 0);
2334 retval
= cft
->write_s64(css
, cft
, val
);
2341 static ssize_t
cgroup_write_string(struct cgroup_subsys_state
*css
,
2342 struct cftype
*cft
, struct file
*file
,
2343 const char __user
*userbuf
, size_t nbytes
,
2344 loff_t
*unused_ppos
)
2346 char local_buffer
[CGROUP_LOCAL_BUFFER_SIZE
];
2348 size_t max_bytes
= cft
->max_write_len
;
2349 char *buffer
= local_buffer
;
2352 max_bytes
= sizeof(local_buffer
) - 1;
2353 if (nbytes
>= max_bytes
)
2355 /* Allocate a dynamic buffer if we need one */
2356 if (nbytes
>= sizeof(local_buffer
)) {
2357 buffer
= kmalloc(nbytes
+ 1, GFP_KERNEL
);
2361 if (nbytes
&& copy_from_user(buffer
, userbuf
, nbytes
)) {
2366 buffer
[nbytes
] = 0; /* nul-terminate */
2367 retval
= cft
->write_string(css
, cft
, strstrip(buffer
));
2371 if (buffer
!= local_buffer
)
2376 static ssize_t
cgroup_file_write(struct file
*file
, const char __user
*buf
,
2377 size_t nbytes
, loff_t
*ppos
)
2379 struct cfent
*cfe
= __d_cfe(file
->f_dentry
);
2380 struct cftype
*cft
= __d_cft(file
->f_dentry
);
2381 struct cgroup_subsys_state
*css
= cfe
->css
;
2384 return cft
->write(css
, cft
, file
, buf
, nbytes
, ppos
);
2385 if (cft
->write_u64
|| cft
->write_s64
)
2386 return cgroup_write_X64(css
, cft
, file
, buf
, nbytes
, ppos
);
2387 if (cft
->write_string
)
2388 return cgroup_write_string(css
, cft
, file
, buf
, nbytes
, ppos
);
2390 int ret
= cft
->trigger(css
, (unsigned int)cft
->private);
2391 return ret
? ret
: nbytes
;
2396 static ssize_t
cgroup_read_u64(struct cgroup_subsys_state
*css
,
2397 struct cftype
*cft
, struct file
*file
,
2398 char __user
*buf
, size_t nbytes
, loff_t
*ppos
)
2400 char tmp
[CGROUP_LOCAL_BUFFER_SIZE
];
2401 u64 val
= cft
->read_u64(css
, cft
);
2402 int len
= sprintf(tmp
, "%llu\n", (unsigned long long) val
);
2404 return simple_read_from_buffer(buf
, nbytes
, ppos
, tmp
, len
);
2407 static ssize_t
cgroup_read_s64(struct cgroup_subsys_state
*css
,
2408 struct cftype
*cft
, struct file
*file
,
2409 char __user
*buf
, size_t nbytes
, loff_t
*ppos
)
2411 char tmp
[CGROUP_LOCAL_BUFFER_SIZE
];
2412 s64 val
= cft
->read_s64(css
, cft
);
2413 int len
= sprintf(tmp
, "%lld\n", (long long) val
);
2415 return simple_read_from_buffer(buf
, nbytes
, ppos
, tmp
, len
);
2418 static ssize_t
cgroup_file_read(struct file
*file
, char __user
*buf
,
2419 size_t nbytes
, loff_t
*ppos
)
2421 struct cfent
*cfe
= __d_cfe(file
->f_dentry
);
2422 struct cftype
*cft
= __d_cft(file
->f_dentry
);
2423 struct cgroup_subsys_state
*css
= cfe
->css
;
2426 return cft
->read(css
, cft
, file
, buf
, nbytes
, ppos
);
2428 return cgroup_read_u64(css
, cft
, file
, buf
, nbytes
, ppos
);
2430 return cgroup_read_s64(css
, cft
, file
, buf
, nbytes
, ppos
);
2435 * seqfile ops/methods for returning structured data. Currently just
2436 * supports string->u64 maps, but can be extended in future.
2439 static int cgroup_map_add(struct cgroup_map_cb
*cb
, const char *key
, u64 value
)
2441 struct seq_file
*sf
= cb
->state
;
2442 return seq_printf(sf
, "%s %llu\n", key
, (unsigned long long)value
);
2445 static int cgroup_seqfile_show(struct seq_file
*m
, void *arg
)
2447 struct cfent
*cfe
= m
->private;
2448 struct cftype
*cft
= cfe
->type
;
2449 struct cgroup_subsys_state
*css
= cfe
->css
;
2451 if (cft
->read_map
) {
2452 struct cgroup_map_cb cb
= {
2453 .fill
= cgroup_map_add
,
2456 return cft
->read_map(css
, cft
, &cb
);
2458 return cft
->read_seq_string(css
, cft
, m
);
2461 static const struct file_operations cgroup_seqfile_operations
= {
2463 .write
= cgroup_file_write
,
2464 .llseek
= seq_lseek
,
2465 .release
= single_release
,
2468 static int cgroup_file_open(struct inode
*inode
, struct file
*file
)
2470 struct cfent
*cfe
= __d_cfe(file
->f_dentry
);
2471 struct cftype
*cft
= __d_cft(file
->f_dentry
);
2472 struct cgroup
*cgrp
= __d_cgrp(cfe
->dentry
->d_parent
);
2473 struct cgroup_subsys_state
*css
;
2476 err
= generic_file_open(inode
, file
);
2481 * If the file belongs to a subsystem, pin the css. Will be
2482 * unpinned either on open failure or release. This ensures that
2483 * @css stays alive for all file operations.
2486 css
= cgroup_css(cgrp
, cft
->ss
);
2487 if (cft
->ss
&& !css_tryget(css
))
2495 * @cfe->css is used by read/write/close to determine the
2496 * associated css. @file->private_data would be a better place but
2497 * that's already used by seqfile. Multiple accessors may use it
2498 * simultaneously which is okay as the association never changes.
2500 WARN_ON_ONCE(cfe
->css
&& cfe
->css
!= css
);
2503 if (cft
->read_map
|| cft
->read_seq_string
) {
2504 file
->f_op
= &cgroup_seqfile_operations
;
2505 err
= single_open(file
, cgroup_seqfile_show
, cfe
);
2506 } else if (cft
->open
) {
2507 err
= cft
->open(inode
, file
);
2515 static int cgroup_file_release(struct inode
*inode
, struct file
*file
)
2517 struct cfent
*cfe
= __d_cfe(file
->f_dentry
);
2518 struct cftype
*cft
= __d_cft(file
->f_dentry
);
2519 struct cgroup_subsys_state
*css
= cfe
->css
;
2523 ret
= cft
->release(inode
, file
);
2530 * cgroup_rename - Only allow simple rename of directories in place.
2532 static int cgroup_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
2533 struct inode
*new_dir
, struct dentry
*new_dentry
)
2536 struct cgroup_name
*name
, *old_name
;
2537 struct cgroup
*cgrp
;
2540 * It's convinient to use parent dir's i_mutex to protected
2543 lockdep_assert_held(&old_dir
->i_mutex
);
2545 if (!S_ISDIR(old_dentry
->d_inode
->i_mode
))
2547 if (new_dentry
->d_inode
)
2549 if (old_dir
!= new_dir
)
2552 cgrp
= __d_cgrp(old_dentry
);
2555 * This isn't a proper migration and its usefulness is very
2556 * limited. Disallow if sane_behavior.
2558 if (cgroup_sane_behavior(cgrp
))
2561 name
= cgroup_alloc_name(new_dentry
);
2565 ret
= simple_rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
2571 old_name
= rcu_dereference_protected(cgrp
->name
, true);
2572 rcu_assign_pointer(cgrp
->name
, name
);
2574 kfree_rcu(old_name
, rcu_head
);
2578 static struct simple_xattrs
*__d_xattrs(struct dentry
*dentry
)
2580 if (S_ISDIR(dentry
->d_inode
->i_mode
))
2581 return &__d_cgrp(dentry
)->xattrs
;
2583 return &__d_cfe(dentry
)->xattrs
;
2586 static inline int xattr_enabled(struct dentry
*dentry
)
2588 struct cgroupfs_root
*root
= dentry
->d_sb
->s_fs_info
;
2589 return root
->flags
& CGRP_ROOT_XATTR
;
2592 static bool is_valid_xattr(const char *name
)
2594 if (!strncmp(name
, XATTR_TRUSTED_PREFIX
, XATTR_TRUSTED_PREFIX_LEN
) ||
2595 !strncmp(name
, XATTR_SECURITY_PREFIX
, XATTR_SECURITY_PREFIX_LEN
))
2600 static int cgroup_setxattr(struct dentry
*dentry
, const char *name
,
2601 const void *val
, size_t size
, int flags
)
2603 if (!xattr_enabled(dentry
))
2605 if (!is_valid_xattr(name
))
2607 return simple_xattr_set(__d_xattrs(dentry
), name
, val
, size
, flags
);
2610 static int cgroup_removexattr(struct dentry
*dentry
, const char *name
)
2612 if (!xattr_enabled(dentry
))
2614 if (!is_valid_xattr(name
))
2616 return simple_xattr_remove(__d_xattrs(dentry
), name
);
2619 static ssize_t
cgroup_getxattr(struct dentry
*dentry
, const char *name
,
2620 void *buf
, size_t size
)
2622 if (!xattr_enabled(dentry
))
2624 if (!is_valid_xattr(name
))
2626 return simple_xattr_get(__d_xattrs(dentry
), name
, buf
, size
);
2629 static ssize_t
cgroup_listxattr(struct dentry
*dentry
, char *buf
, size_t size
)
2631 if (!xattr_enabled(dentry
))
2633 return simple_xattr_list(__d_xattrs(dentry
), buf
, size
);
2636 static const struct file_operations cgroup_file_operations
= {
2637 .read
= cgroup_file_read
,
2638 .write
= cgroup_file_write
,
2639 .llseek
= generic_file_llseek
,
2640 .open
= cgroup_file_open
,
2641 .release
= cgroup_file_release
,
2644 static const struct inode_operations cgroup_file_inode_operations
= {
2645 .setxattr
= cgroup_setxattr
,
2646 .getxattr
= cgroup_getxattr
,
2647 .listxattr
= cgroup_listxattr
,
2648 .removexattr
= cgroup_removexattr
,
2651 static const struct inode_operations cgroup_dir_inode_operations
= {
2652 .lookup
= simple_lookup
,
2653 .mkdir
= cgroup_mkdir
,
2654 .rmdir
= cgroup_rmdir
,
2655 .rename
= cgroup_rename
,
2656 .setxattr
= cgroup_setxattr
,
2657 .getxattr
= cgroup_getxattr
,
2658 .listxattr
= cgroup_listxattr
,
2659 .removexattr
= cgroup_removexattr
,
2663 * Check if a file is a control file
2665 static inline struct cftype
*__file_cft(struct file
*file
)
2667 if (file_inode(file
)->i_fop
!= &cgroup_file_operations
)
2668 return ERR_PTR(-EINVAL
);
2669 return __d_cft(file
->f_dentry
);
2672 static int cgroup_create_file(struct dentry
*dentry
, umode_t mode
,
2673 struct super_block
*sb
)
2675 struct inode
*inode
;
2679 if (dentry
->d_inode
)
2682 inode
= cgroup_new_inode(mode
, sb
);
2686 if (S_ISDIR(mode
)) {
2687 inode
->i_op
= &cgroup_dir_inode_operations
;
2688 inode
->i_fop
= &simple_dir_operations
;
2690 /* start off with i_nlink == 2 (for "." entry) */
2692 inc_nlink(dentry
->d_parent
->d_inode
);
2695 * Control reaches here with cgroup_mutex held.
2696 * @inode->i_mutex should nest outside cgroup_mutex but we
2697 * want to populate it immediately without releasing
2698 * cgroup_mutex. As @inode isn't visible to anyone else
2699 * yet, trylock will always succeed without affecting
2702 WARN_ON_ONCE(!mutex_trylock(&inode
->i_mutex
));
2703 } else if (S_ISREG(mode
)) {
2705 inode
->i_fop
= &cgroup_file_operations
;
2706 inode
->i_op
= &cgroup_file_inode_operations
;
2708 d_instantiate(dentry
, inode
);
2709 dget(dentry
); /* Extra count - pin the dentry in core */
2714 * cgroup_file_mode - deduce file mode of a control file
2715 * @cft: the control file in question
2717 * returns cft->mode if ->mode is not 0
2718 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2719 * returns S_IRUGO if it has only a read handler
2720 * returns S_IWUSR if it has only a write hander
2722 static umode_t
cgroup_file_mode(const struct cftype
*cft
)
2729 if (cft
->read
|| cft
->read_u64
|| cft
->read_s64
||
2730 cft
->read_map
|| cft
->read_seq_string
)
2733 if (cft
->write
|| cft
->write_u64
|| cft
->write_s64
||
2734 cft
->write_string
|| cft
->trigger
)
2740 static int cgroup_add_file(struct cgroup
*cgrp
, struct cftype
*cft
)
2742 struct dentry
*dir
= cgrp
->dentry
;
2743 struct cgroup
*parent
= __d_cgrp(dir
);
2744 struct dentry
*dentry
;
2748 char name
[MAX_CGROUP_TYPE_NAMELEN
+ MAX_CFTYPE_NAME
+ 2] = { 0 };
2750 if (cft
->ss
&& !(cft
->flags
& CFTYPE_NO_PREFIX
) &&
2751 !(cgrp
->root
->flags
& CGRP_ROOT_NOPREFIX
)) {
2752 strcpy(name
, cft
->ss
->name
);
2755 strcat(name
, cft
->name
);
2757 BUG_ON(!mutex_is_locked(&dir
->d_inode
->i_mutex
));
2759 cfe
= kzalloc(sizeof(*cfe
), GFP_KERNEL
);
2763 dentry
= lookup_one_len(name
, dir
, strlen(name
));
2764 if (IS_ERR(dentry
)) {
2765 error
= PTR_ERR(dentry
);
2769 cfe
->type
= (void *)cft
;
2770 cfe
->dentry
= dentry
;
2771 dentry
->d_fsdata
= cfe
;
2772 simple_xattrs_init(&cfe
->xattrs
);
2774 mode
= cgroup_file_mode(cft
);
2775 error
= cgroup_create_file(dentry
, mode
| S_IFREG
, cgrp
->root
->sb
);
2777 list_add_tail(&cfe
->node
, &parent
->files
);
2787 * cgroup_addrm_files - add or remove files to a cgroup directory
2788 * @cgrp: the target cgroup
2789 * @cfts: array of cftypes to be added
2790 * @is_add: whether to add or remove
2792 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
2793 * For removals, this function never fails. If addition fails, this
2794 * function doesn't remove files already added. The caller is responsible
2797 static int cgroup_addrm_files(struct cgroup
*cgrp
, struct cftype cfts
[],
2803 lockdep_assert_held(&cgrp
->dentry
->d_inode
->i_mutex
);
2804 lockdep_assert_held(&cgroup_mutex
);
2806 for (cft
= cfts
; cft
->name
[0] != '\0'; cft
++) {
2807 /* does cft->flags tell us to skip this file on @cgrp? */
2808 if ((cft
->flags
& CFTYPE_INSANE
) && cgroup_sane_behavior(cgrp
))
2810 if ((cft
->flags
& CFTYPE_NOT_ON_ROOT
) && !cgrp
->parent
)
2812 if ((cft
->flags
& CFTYPE_ONLY_ON_ROOT
) && cgrp
->parent
)
2816 ret
= cgroup_add_file(cgrp
, cft
);
2818 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
2823 cgroup_rm_file(cgrp
, cft
);
2829 static void cgroup_cfts_prepare(void)
2830 __acquires(&cgroup_mutex
)
2833 * Thanks to the entanglement with vfs inode locking, we can't walk
2834 * the existing cgroups under cgroup_mutex and create files.
2835 * Instead, we use css_for_each_descendant_pre() and drop RCU read
2836 * lock before calling cgroup_addrm_files().
2838 mutex_lock(&cgroup_mutex
);
2841 static int cgroup_cfts_commit(struct cftype
*cfts
, bool is_add
)
2842 __releases(&cgroup_mutex
)
2845 struct cgroup_subsys
*ss
= cfts
[0].ss
;
2846 struct cgroup
*root
= &ss
->root
->top_cgroup
;
2847 struct super_block
*sb
= ss
->root
->sb
;
2848 struct dentry
*prev
= NULL
;
2849 struct inode
*inode
;
2850 struct cgroup_subsys_state
*css
;
2854 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
2855 if (!cfts
|| ss
->root
== &cgroup_dummy_root
||
2856 !atomic_inc_not_zero(&sb
->s_active
)) {
2857 mutex_unlock(&cgroup_mutex
);
2862 * All cgroups which are created after we drop cgroup_mutex will
2863 * have the updated set of files, so we only need to update the
2864 * cgroups created before the current @cgroup_serial_nr_next.
2866 update_before
= cgroup_serial_nr_next
;
2868 mutex_unlock(&cgroup_mutex
);
2870 /* add/rm files for all cgroups created before */
2872 css_for_each_descendant_pre(css
, cgroup_css(root
, ss
)) {
2873 struct cgroup
*cgrp
= css
->cgroup
;
2875 if (cgroup_is_dead(cgrp
))
2878 inode
= cgrp
->dentry
->d_inode
;
2883 prev
= cgrp
->dentry
;
2885 mutex_lock(&inode
->i_mutex
);
2886 mutex_lock(&cgroup_mutex
);
2887 if (cgrp
->serial_nr
< update_before
&& !cgroup_is_dead(cgrp
))
2888 ret
= cgroup_addrm_files(cgrp
, cfts
, is_add
);
2889 mutex_unlock(&cgroup_mutex
);
2890 mutex_unlock(&inode
->i_mutex
);
2898 deactivate_super(sb
);
2903 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2904 * @ss: target cgroup subsystem
2905 * @cfts: zero-length name terminated array of cftypes
2907 * Register @cfts to @ss. Files described by @cfts are created for all
2908 * existing cgroups to which @ss is attached and all future cgroups will
2909 * have them too. This function can be called anytime whether @ss is
2912 * Returns 0 on successful registration, -errno on failure. Note that this
2913 * function currently returns 0 as long as @cfts registration is successful
2914 * even if some file creation attempts on existing cgroups fail.
2916 int cgroup_add_cftypes(struct cgroup_subsys
*ss
, struct cftype
*cfts
)
2918 struct cftype_set
*set
;
2922 set
= kzalloc(sizeof(*set
), GFP_KERNEL
);
2926 for (cft
= cfts
; cft
->name
[0] != '\0'; cft
++)
2929 cgroup_cfts_prepare();
2931 list_add_tail(&set
->node
, &ss
->cftsets
);
2932 ret
= cgroup_cfts_commit(cfts
, true);
2934 cgroup_rm_cftypes(cfts
);
2937 EXPORT_SYMBOL_GPL(cgroup_add_cftypes
);
2940 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
2941 * @cfts: zero-length name terminated array of cftypes
2943 * Unregister @cfts. Files described by @cfts are removed from all
2944 * existing cgroups and all future cgroups won't have them either. This
2945 * function can be called anytime whether @cfts' subsys is attached or not.
2947 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
2950 int cgroup_rm_cftypes(struct cftype
*cfts
)
2952 struct cftype_set
*set
;
2954 if (!cfts
|| !cfts
[0].ss
)
2957 cgroup_cfts_prepare();
2959 list_for_each_entry(set
, &cfts
[0].ss
->cftsets
, node
) {
2960 if (set
->cfts
== cfts
) {
2961 list_del(&set
->node
);
2963 cgroup_cfts_commit(cfts
, false);
2968 cgroup_cfts_commit(NULL
, false);
2973 * cgroup_task_count - count the number of tasks in a cgroup.
2974 * @cgrp: the cgroup in question
2976 * Return the number of tasks in the cgroup.
2978 int cgroup_task_count(const struct cgroup
*cgrp
)
2981 struct cgrp_cset_link
*link
;
2983 read_lock(&css_set_lock
);
2984 list_for_each_entry(link
, &cgrp
->cset_links
, cset_link
)
2985 count
+= atomic_read(&link
->cset
->refcount
);
2986 read_unlock(&css_set_lock
);
2991 * To reduce the fork() overhead for systems that are not actually using
2992 * their cgroups capability, we don't maintain the lists running through
2993 * each css_set to its tasks until we see the list actually used - in other
2994 * words after the first call to css_task_iter_start().
2996 static void cgroup_enable_task_cg_lists(void)
2998 struct task_struct
*p
, *g
;
2999 write_lock(&css_set_lock
);
3000 use_task_css_set_links
= 1;
3002 * We need tasklist_lock because RCU is not safe against
3003 * while_each_thread(). Besides, a forking task that has passed
3004 * cgroup_post_fork() without seeing use_task_css_set_links = 1
3005 * is not guaranteed to have its child immediately visible in the
3006 * tasklist if we walk through it with RCU.
3008 read_lock(&tasklist_lock
);
3009 do_each_thread(g
, p
) {
3012 * We should check if the process is exiting, otherwise
3013 * it will race with cgroup_exit() in that the list
3014 * entry won't be deleted though the process has exited.
3016 if (!(p
->flags
& PF_EXITING
) && list_empty(&p
->cg_list
))
3017 list_add(&p
->cg_list
, &task_css_set(p
)->tasks
);
3019 } while_each_thread(g
, p
);
3020 read_unlock(&tasklist_lock
);
3021 write_unlock(&css_set_lock
);
3025 * css_next_child - find the next child of a given css
3026 * @pos_css: the current position (%NULL to initiate traversal)
3027 * @parent_css: css whose children to walk
3029 * This function returns the next child of @parent_css and should be called
3030 * under RCU read lock. The only requirement is that @parent_css and
3031 * @pos_css are accessible. The next sibling is guaranteed to be returned
3032 * regardless of their states.
3034 struct cgroup_subsys_state
*
3035 css_next_child(struct cgroup_subsys_state
*pos_css
,
3036 struct cgroup_subsys_state
*parent_css
)
3038 struct cgroup
*pos
= pos_css
? pos_css
->cgroup
: NULL
;
3039 struct cgroup
*cgrp
= parent_css
->cgroup
;
3040 struct cgroup
*next
;
3042 WARN_ON_ONCE(!rcu_read_lock_held());
3045 * @pos could already have been removed. Once a cgroup is removed,
3046 * its ->sibling.next is no longer updated when its next sibling
3047 * changes. As CGRP_DEAD assertion is serialized and happens
3048 * before the cgroup is taken off the ->sibling list, if we see it
3049 * unasserted, it's guaranteed that the next sibling hasn't
3050 * finished its grace period even if it's already removed, and thus
3051 * safe to dereference from this RCU critical section. If
3052 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
3053 * to be visible as %true here.
3055 * If @pos is dead, its next pointer can't be dereferenced;
3056 * however, as each cgroup is given a monotonically increasing
3057 * unique serial number and always appended to the sibling list,
3058 * the next one can be found by walking the parent's children until
3059 * we see a cgroup with higher serial number than @pos's. While
3060 * this path can be slower, it's taken only when either the current
3061 * cgroup is removed or iteration and removal race.
3064 next
= list_entry_rcu(cgrp
->children
.next
, struct cgroup
, sibling
);
3065 } else if (likely(!cgroup_is_dead(pos
))) {
3066 next
= list_entry_rcu(pos
->sibling
.next
, struct cgroup
, sibling
);
3068 list_for_each_entry_rcu(next
, &cgrp
->children
, sibling
)
3069 if (next
->serial_nr
> pos
->serial_nr
)
3073 if (&next
->sibling
== &cgrp
->children
)
3076 return cgroup_css(next
, parent_css
->ss
);
3078 EXPORT_SYMBOL_GPL(css_next_child
);
3081 * css_next_descendant_pre - find the next descendant for pre-order walk
3082 * @pos: the current position (%NULL to initiate traversal)
3083 * @root: css whose descendants to walk
3085 * To be used by css_for_each_descendant_pre(). Find the next descendant
3086 * to visit for pre-order traversal of @root's descendants. @root is
3087 * included in the iteration and the first node to be visited.
3089 * While this function requires RCU read locking, it doesn't require the
3090 * whole traversal to be contained in a single RCU critical section. This
3091 * function will return the correct next descendant as long as both @pos
3092 * and @root are accessible and @pos is a descendant of @root.
3094 struct cgroup_subsys_state
*
3095 css_next_descendant_pre(struct cgroup_subsys_state
*pos
,
3096 struct cgroup_subsys_state
*root
)
3098 struct cgroup_subsys_state
*next
;
3100 WARN_ON_ONCE(!rcu_read_lock_held());
3102 /* if first iteration, visit @root */
3106 /* visit the first child if exists */
3107 next
= css_next_child(NULL
, pos
);
3111 /* no child, visit my or the closest ancestor's next sibling */
3112 while (pos
!= root
) {
3113 next
= css_next_child(pos
, css_parent(pos
));
3116 pos
= css_parent(pos
);
3121 EXPORT_SYMBOL_GPL(css_next_descendant_pre
);
3124 * css_rightmost_descendant - return the rightmost descendant of a css
3125 * @pos: css of interest
3127 * Return the rightmost descendant of @pos. If there's no descendant, @pos
3128 * is returned. This can be used during pre-order traversal to skip
3131 * While this function requires RCU read locking, it doesn't require the
3132 * whole traversal to be contained in a single RCU critical section. This
3133 * function will return the correct rightmost descendant as long as @pos is
3136 struct cgroup_subsys_state
*
3137 css_rightmost_descendant(struct cgroup_subsys_state
*pos
)
3139 struct cgroup_subsys_state
*last
, *tmp
;
3141 WARN_ON_ONCE(!rcu_read_lock_held());
3145 /* ->prev isn't RCU safe, walk ->next till the end */
3147 css_for_each_child(tmp
, last
)
3153 EXPORT_SYMBOL_GPL(css_rightmost_descendant
);
3155 static struct cgroup_subsys_state
*
3156 css_leftmost_descendant(struct cgroup_subsys_state
*pos
)
3158 struct cgroup_subsys_state
*last
;
3162 pos
= css_next_child(NULL
, pos
);
3169 * css_next_descendant_post - find the next descendant for post-order walk
3170 * @pos: the current position (%NULL to initiate traversal)
3171 * @root: css whose descendants to walk
3173 * To be used by css_for_each_descendant_post(). Find the next descendant
3174 * to visit for post-order traversal of @root's descendants. @root is
3175 * included in the iteration and the last node to be visited.
3177 * While this function requires RCU read locking, it doesn't require the
3178 * whole traversal to be contained in a single RCU critical section. This
3179 * function will return the correct next descendant as long as both @pos
3180 * and @cgroup are accessible and @pos is a descendant of @cgroup.
3182 struct cgroup_subsys_state
*
3183 css_next_descendant_post(struct cgroup_subsys_state
*pos
,
3184 struct cgroup_subsys_state
*root
)
3186 struct cgroup_subsys_state
*next
;
3188 WARN_ON_ONCE(!rcu_read_lock_held());
3190 /* if first iteration, visit leftmost descendant which may be @root */
3192 return css_leftmost_descendant(root
);
3194 /* if we visited @root, we're done */
3198 /* if there's an unvisited sibling, visit its leftmost descendant */
3199 next
= css_next_child(pos
, css_parent(pos
));
3201 return css_leftmost_descendant(next
);
3203 /* no sibling left, visit parent */
3204 return css_parent(pos
);
3206 EXPORT_SYMBOL_GPL(css_next_descendant_post
);
3209 * css_advance_task_iter - advance a task itererator to the next css_set
3210 * @it: the iterator to advance
3212 * Advance @it to the next css_set to walk.
3214 static void css_advance_task_iter(struct css_task_iter
*it
)
3216 struct list_head
*l
= it
->cset_link
;
3217 struct cgrp_cset_link
*link
;
3218 struct css_set
*cset
;
3220 /* Advance to the next non-empty css_set */
3223 if (l
== &it
->origin_css
->cgroup
->cset_links
) {
3224 it
->cset_link
= NULL
;
3227 link
= list_entry(l
, struct cgrp_cset_link
, cset_link
);
3229 } while (list_empty(&cset
->tasks
));
3231 it
->task
= cset
->tasks
.next
;
3235 * css_task_iter_start - initiate task iteration
3236 * @css: the css to walk tasks of
3237 * @it: the task iterator to use
3239 * Initiate iteration through the tasks of @css. The caller can call
3240 * css_task_iter_next() to walk through the tasks until the function
3241 * returns NULL. On completion of iteration, css_task_iter_end() must be
3244 * Note that this function acquires a lock which is released when the
3245 * iteration finishes. The caller can't sleep while iteration is in
3248 void css_task_iter_start(struct cgroup_subsys_state
*css
,
3249 struct css_task_iter
*it
)
3250 __acquires(css_set_lock
)
3253 * The first time anyone tries to iterate across a css, we need to
3254 * enable the list linking each css_set to its tasks, and fix up
3255 * all existing tasks.
3257 if (!use_task_css_set_links
)
3258 cgroup_enable_task_cg_lists();
3260 read_lock(&css_set_lock
);
3262 it
->origin_css
= css
;
3263 it
->cset_link
= &css
->cgroup
->cset_links
;
3265 css_advance_task_iter(it
);
3269 * css_task_iter_next - return the next task for the iterator
3270 * @it: the task iterator being iterated
3272 * The "next" function for task iteration. @it should have been
3273 * initialized via css_task_iter_start(). Returns NULL when the iteration
3276 struct task_struct
*css_task_iter_next(struct css_task_iter
*it
)
3278 struct task_struct
*res
;
3279 struct list_head
*l
= it
->task
;
3280 struct cgrp_cset_link
*link
;
3282 /* If the iterator cg is NULL, we have no tasks */
3285 res
= list_entry(l
, struct task_struct
, cg_list
);
3286 /* Advance iterator to find next entry */
3288 link
= list_entry(it
->cset_link
, struct cgrp_cset_link
, cset_link
);
3289 if (l
== &link
->cset
->tasks
) {
3291 * We reached the end of this task list - move on to the
3292 * next cgrp_cset_link.
3294 css_advance_task_iter(it
);
3302 * css_task_iter_end - finish task iteration
3303 * @it: the task iterator to finish
3305 * Finish task iteration started by css_task_iter_start().
3307 void css_task_iter_end(struct css_task_iter
*it
)
3308 __releases(css_set_lock
)
3310 read_unlock(&css_set_lock
);
3313 static inline int started_after_time(struct task_struct
*t1
,
3314 struct timespec
*time
,
3315 struct task_struct
*t2
)
3317 int start_diff
= timespec_compare(&t1
->start_time
, time
);
3318 if (start_diff
> 0) {
3320 } else if (start_diff
< 0) {
3324 * Arbitrarily, if two processes started at the same
3325 * time, we'll say that the lower pointer value
3326 * started first. Note that t2 may have exited by now
3327 * so this may not be a valid pointer any longer, but
3328 * that's fine - it still serves to distinguish
3329 * between two tasks started (effectively) simultaneously.
3336 * This function is a callback from heap_insert() and is used to order
3338 * In this case we order the heap in descending task start time.
3340 static inline int started_after(void *p1
, void *p2
)
3342 struct task_struct
*t1
= p1
;
3343 struct task_struct
*t2
= p2
;
3344 return started_after_time(t1
, &t2
->start_time
, t2
);
3348 * css_scan_tasks - iterate though all the tasks in a css
3349 * @css: the css to iterate tasks of
3350 * @test: optional test callback
3351 * @process: process callback
3352 * @data: data passed to @test and @process
3353 * @heap: optional pre-allocated heap used for task iteration
3355 * Iterate through all the tasks in @css, calling @test for each, and if it
3356 * returns %true, call @process for it also.
3358 * @test may be NULL, meaning always true (select all tasks), which
3359 * effectively duplicates css_task_iter_{start,next,end}() but does not
3360 * lock css_set_lock for the call to @process.
3362 * It is guaranteed that @process will act on every task that is a member
3363 * of @css for the duration of this call. This function may or may not
3364 * call @process for tasks that exit or move to a different css during the
3365 * call, or are forked or move into the css during the call.
3367 * Note that @test may be called with locks held, and may in some
3368 * situations be called multiple times for the same task, so it should be
3371 * If @heap is non-NULL, a heap has been pre-allocated and will be used for
3372 * heap operations (and its "gt" member will be overwritten), else a
3373 * temporary heap will be used (allocation of which may cause this function
3376 int css_scan_tasks(struct cgroup_subsys_state
*css
,
3377 bool (*test
)(struct task_struct
*, void *),
3378 void (*process
)(struct task_struct
*, void *),
3379 void *data
, struct ptr_heap
*heap
)
3382 struct css_task_iter it
;
3383 struct task_struct
*p
, *dropped
;
3384 /* Never dereference latest_task, since it's not refcounted */
3385 struct task_struct
*latest_task
= NULL
;
3386 struct ptr_heap tmp_heap
;
3387 struct timespec latest_time
= { 0, 0 };
3390 /* The caller supplied our heap and pre-allocated its memory */
3391 heap
->gt
= &started_after
;
3393 /* We need to allocate our own heap memory */
3395 retval
= heap_init(heap
, PAGE_SIZE
, GFP_KERNEL
, &started_after
);
3397 /* cannot allocate the heap */
3403 * Scan tasks in the css, using the @test callback to determine
3404 * which are of interest, and invoking @process callback on the
3405 * ones which need an update. Since we don't want to hold any
3406 * locks during the task updates, gather tasks to be processed in a
3407 * heap structure. The heap is sorted by descending task start
3408 * time. If the statically-sized heap fills up, we overflow tasks
3409 * that started later, and in future iterations only consider tasks
3410 * that started after the latest task in the previous pass. This
3411 * guarantees forward progress and that we don't miss any tasks.
3414 css_task_iter_start(css
, &it
);
3415 while ((p
= css_task_iter_next(&it
))) {
3417 * Only affect tasks that qualify per the caller's callback,
3418 * if he provided one
3420 if (test
&& !test(p
, data
))
3423 * Only process tasks that started after the last task
3426 if (!started_after_time(p
, &latest_time
, latest_task
))
3428 dropped
= heap_insert(heap
, p
);
3429 if (dropped
== NULL
) {
3431 * The new task was inserted; the heap wasn't
3435 } else if (dropped
!= p
) {
3437 * The new task was inserted, and pushed out a
3441 put_task_struct(dropped
);
3444 * Else the new task was newer than anything already in
3445 * the heap and wasn't inserted
3448 css_task_iter_end(&it
);
3451 for (i
= 0; i
< heap
->size
; i
++) {
3452 struct task_struct
*q
= heap
->ptrs
[i
];
3454 latest_time
= q
->start_time
;
3457 /* Process the task per the caller's callback */
3462 * If we had to process any tasks at all, scan again
3463 * in case some of them were in the middle of forking
3464 * children that didn't get processed.
3465 * Not the most efficient way to do it, but it avoids
3466 * having to take callback_mutex in the fork path
3470 if (heap
== &tmp_heap
)
3471 heap_free(&tmp_heap
);
3475 static void cgroup_transfer_one_task(struct task_struct
*task
, void *data
)
3477 struct cgroup
*new_cgroup
= data
;
3479 mutex_lock(&cgroup_mutex
);
3480 cgroup_attach_task(new_cgroup
, task
, false);
3481 mutex_unlock(&cgroup_mutex
);
3485 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3486 * @to: cgroup to which the tasks will be moved
3487 * @from: cgroup in which the tasks currently reside
3489 int cgroup_transfer_tasks(struct cgroup
*to
, struct cgroup
*from
)
3491 return css_scan_tasks(&from
->dummy_css
, NULL
, cgroup_transfer_one_task
,
3496 * Stuff for reading the 'tasks'/'procs' files.
3498 * Reading this file can return large amounts of data if a cgroup has
3499 * *lots* of attached tasks. So it may need several calls to read(),
3500 * but we cannot guarantee that the information we produce is correct
3501 * unless we produce it entirely atomically.
3505 /* which pidlist file are we talking about? */
3506 enum cgroup_filetype
{
3512 * A pidlist is a list of pids that virtually represents the contents of one
3513 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3514 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3517 struct cgroup_pidlist
{
3519 * used to find which pidlist is wanted. doesn't change as long as
3520 * this particular list stays in the list.
3522 struct { enum cgroup_filetype type
; struct pid_namespace
*ns
; } key
;
3525 /* how many elements the above list has */
3527 /* how many files are using the current array */
3529 /* each of these stored in a list by its cgroup */
3530 struct list_head links
;
3531 /* pointer to the cgroup we belong to, for list removal purposes */
3532 struct cgroup
*owner
;
3533 /* protects the other fields */
3534 struct rw_semaphore rwsem
;
3538 * The following two functions "fix" the issue where there are more pids
3539 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3540 * TODO: replace with a kernel-wide solution to this problem
3542 #define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3543 static void *pidlist_allocate(int count
)
3545 if (PIDLIST_TOO_LARGE(count
))
3546 return vmalloc(count
* sizeof(pid_t
));
3548 return kmalloc(count
* sizeof(pid_t
), GFP_KERNEL
);
3550 static void pidlist_free(void *p
)
3552 if (is_vmalloc_addr(p
))
3559 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
3560 * Returns the number of unique elements.
3562 static int pidlist_uniq(pid_t
*list
, int length
)
3567 * we presume the 0th element is unique, so i starts at 1. trivial
3568 * edge cases first; no work needs to be done for either
3570 if (length
== 0 || length
== 1)
3572 /* src and dest walk down the list; dest counts unique elements */
3573 for (src
= 1; src
< length
; src
++) {
3574 /* find next unique element */
3575 while (list
[src
] == list
[src
-1]) {
3580 /* dest always points to where the next unique element goes */
3581 list
[dest
] = list
[src
];
3588 static int cmppid(const void *a
, const void *b
)
3590 return *(pid_t
*)a
- *(pid_t
*)b
;
3594 * find the appropriate pidlist for our purpose (given procs vs tasks)
3595 * returns with the lock on that pidlist already held, and takes care
3596 * of the use count, or returns NULL with no locks held if we're out of
3599 static struct cgroup_pidlist
*cgroup_pidlist_find(struct cgroup
*cgrp
,
3600 enum cgroup_filetype type
)
3602 struct cgroup_pidlist
*l
;
3603 /* don't need task_nsproxy() if we're looking at ourself */
3604 struct pid_namespace
*ns
= task_active_pid_ns(current
);
3607 * We can't drop the pidlist_mutex before taking the l->rwsem in case
3608 * the last ref-holder is trying to remove l from the list at the same
3609 * time. Holding the pidlist_mutex precludes somebody taking whichever
3610 * list we find out from under us - compare release_pid_array().
3612 mutex_lock(&cgrp
->pidlist_mutex
);
3613 list_for_each_entry(l
, &cgrp
->pidlists
, links
) {
3614 if (l
->key
.type
== type
&& l
->key
.ns
== ns
) {
3615 /* make sure l doesn't vanish out from under us */
3616 down_write(&l
->rwsem
);
3617 mutex_unlock(&cgrp
->pidlist_mutex
);
3621 /* entry not found; create a new one */
3622 l
= kzalloc(sizeof(struct cgroup_pidlist
), GFP_KERNEL
);
3624 mutex_unlock(&cgrp
->pidlist_mutex
);
3627 init_rwsem(&l
->rwsem
);
3628 down_write(&l
->rwsem
);
3630 l
->key
.ns
= get_pid_ns(ns
);
3632 list_add(&l
->links
, &cgrp
->pidlists
);
3633 mutex_unlock(&cgrp
->pidlist_mutex
);
3638 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3640 static int pidlist_array_load(struct cgroup
*cgrp
, enum cgroup_filetype type
,
3641 struct cgroup_pidlist
**lp
)
3645 int pid
, n
= 0; /* used for populating the array */
3646 struct css_task_iter it
;
3647 struct task_struct
*tsk
;
3648 struct cgroup_pidlist
*l
;
3651 * If cgroup gets more users after we read count, we won't have
3652 * enough space - tough. This race is indistinguishable to the
3653 * caller from the case that the additional cgroup users didn't
3654 * show up until sometime later on.
3656 length
= cgroup_task_count(cgrp
);
3657 array
= pidlist_allocate(length
);
3660 /* now, populate the array */
3661 css_task_iter_start(&cgrp
->dummy_css
, &it
);
3662 while ((tsk
= css_task_iter_next(&it
))) {
3663 if (unlikely(n
== length
))
3665 /* get tgid or pid for procs or tasks file respectively */
3666 if (type
== CGROUP_FILE_PROCS
)
3667 pid
= task_tgid_vnr(tsk
);
3669 pid
= task_pid_vnr(tsk
);
3670 if (pid
> 0) /* make sure to only use valid results */
3673 css_task_iter_end(&it
);
3675 /* now sort & (if procs) strip out duplicates */
3676 sort(array
, length
, sizeof(pid_t
), cmppid
, NULL
);
3677 if (type
== CGROUP_FILE_PROCS
)
3678 length
= pidlist_uniq(array
, length
);
3679 l
= cgroup_pidlist_find(cgrp
, type
);
3681 pidlist_free(array
);
3684 /* store array, freeing old if necessary - lock already held */
3685 pidlist_free(l
->list
);
3689 up_write(&l
->rwsem
);
3695 * cgroupstats_build - build and fill cgroupstats
3696 * @stats: cgroupstats to fill information into
3697 * @dentry: A dentry entry belonging to the cgroup for which stats have
3700 * Build and fill cgroupstats so that taskstats can export it to user
3703 int cgroupstats_build(struct cgroupstats
*stats
, struct dentry
*dentry
)
3706 struct cgroup
*cgrp
;
3707 struct css_task_iter it
;
3708 struct task_struct
*tsk
;
3711 * Validate dentry by checking the superblock operations,
3712 * and make sure it's a directory.
3714 if (dentry
->d_sb
->s_op
!= &cgroup_ops
||
3715 !S_ISDIR(dentry
->d_inode
->i_mode
))
3719 cgrp
= dentry
->d_fsdata
;
3721 css_task_iter_start(&cgrp
->dummy_css
, &it
);
3722 while ((tsk
= css_task_iter_next(&it
))) {
3723 switch (tsk
->state
) {
3725 stats
->nr_running
++;
3727 case TASK_INTERRUPTIBLE
:
3728 stats
->nr_sleeping
++;
3730 case TASK_UNINTERRUPTIBLE
:
3731 stats
->nr_uninterruptible
++;
3734 stats
->nr_stopped
++;
3737 if (delayacct_is_task_waiting_on_io(tsk
))
3738 stats
->nr_io_wait
++;
3742 css_task_iter_end(&it
);
3750 * seq_file methods for the tasks/procs files. The seq_file position is the
3751 * next pid to display; the seq_file iterator is a pointer to the pid
3752 * in the cgroup->l->list array.
3755 static void *cgroup_pidlist_start(struct seq_file
*s
, loff_t
*pos
)
3758 * Initially we receive a position value that corresponds to
3759 * one more than the last pid shown (or 0 on the first call or
3760 * after a seek to the start). Use a binary-search to find the
3761 * next pid to display, if any
3763 struct cgroup_pidlist
*l
= s
->private;
3764 int index
= 0, pid
= *pos
;
3767 down_read(&l
->rwsem
);
3769 int end
= l
->length
;
3771 while (index
< end
) {
3772 int mid
= (index
+ end
) / 2;
3773 if (l
->list
[mid
] == pid
) {
3776 } else if (l
->list
[mid
] <= pid
)
3782 /* If we're off the end of the array, we're done */
3783 if (index
>= l
->length
)
3785 /* Update the abstract position to be the actual pid that we found */
3786 iter
= l
->list
+ index
;
3791 static void cgroup_pidlist_stop(struct seq_file
*s
, void *v
)
3793 struct cgroup_pidlist
*l
= s
->private;
3797 static void *cgroup_pidlist_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
3799 struct cgroup_pidlist
*l
= s
->private;
3801 pid_t
*end
= l
->list
+ l
->length
;
3803 * Advance to the next pid in the array. If this goes off the
3815 static int cgroup_pidlist_show(struct seq_file
*s
, void *v
)
3817 return seq_printf(s
, "%d\n", *(int *)v
);
3821 * seq_operations functions for iterating on pidlists through seq_file -
3822 * independent of whether it's tasks or procs
3824 static const struct seq_operations cgroup_pidlist_seq_operations
= {
3825 .start
= cgroup_pidlist_start
,
3826 .stop
= cgroup_pidlist_stop
,
3827 .next
= cgroup_pidlist_next
,
3828 .show
= cgroup_pidlist_show
,
3831 static void cgroup_release_pid_array(struct cgroup_pidlist
*l
)
3834 * the case where we're the last user of this particular pidlist will
3835 * have us remove it from the cgroup's list, which entails taking the
3836 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3837 * pidlist_mutex, we have to take pidlist_mutex first.
3839 mutex_lock(&l
->owner
->pidlist_mutex
);
3840 down_write(&l
->rwsem
);
3841 BUG_ON(!l
->use_count
);
3842 if (!--l
->use_count
) {
3843 /* we're the last user if refcount is 0; remove and free */
3844 list_del(&l
->links
);
3845 mutex_unlock(&l
->owner
->pidlist_mutex
);
3846 pidlist_free(l
->list
);
3847 put_pid_ns(l
->key
.ns
);
3848 up_write(&l
->rwsem
);
3852 mutex_unlock(&l
->owner
->pidlist_mutex
);
3853 up_write(&l
->rwsem
);
3856 static int cgroup_pidlist_release(struct inode
*inode
, struct file
*file
)
3858 struct cgroup_pidlist
*l
;
3859 if (!(file
->f_mode
& FMODE_READ
))
3862 * the seq_file will only be initialized if the file was opened for
3863 * reading; hence we check if it's not null only in that case.
3865 l
= ((struct seq_file
*)file
->private_data
)->private;
3866 cgroup_release_pid_array(l
);
3867 return seq_release(inode
, file
);
3870 static const struct file_operations cgroup_pidlist_operations
= {
3872 .llseek
= seq_lseek
,
3873 .write
= cgroup_file_write
,
3874 .release
= cgroup_pidlist_release
,
3878 * The following functions handle opens on a file that displays a pidlist
3879 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3882 /* helper function for the two below it */
3883 static int cgroup_pidlist_open(struct file
*file
, enum cgroup_filetype type
)
3885 struct cgroup
*cgrp
= __d_cgrp(file
->f_dentry
->d_parent
);
3886 struct cgroup_pidlist
*l
;
3889 /* Nothing to do for write-only files */
3890 if (!(file
->f_mode
& FMODE_READ
))
3893 /* have the array populated */
3894 retval
= pidlist_array_load(cgrp
, type
, &l
);
3897 /* configure file information */
3898 file
->f_op
= &cgroup_pidlist_operations
;
3900 retval
= seq_open(file
, &cgroup_pidlist_seq_operations
);
3902 cgroup_release_pid_array(l
);
3905 ((struct seq_file
*)file
->private_data
)->private = l
;
3908 static int cgroup_tasks_open(struct inode
*unused
, struct file
*file
)
3910 return cgroup_pidlist_open(file
, CGROUP_FILE_TASKS
);
3912 static int cgroup_procs_open(struct inode
*unused
, struct file
*file
)
3914 return cgroup_pidlist_open(file
, CGROUP_FILE_PROCS
);
3917 static u64
cgroup_read_notify_on_release(struct cgroup_subsys_state
*css
,
3920 return notify_on_release(css
->cgroup
);
3923 static int cgroup_write_notify_on_release(struct cgroup_subsys_state
*css
,
3924 struct cftype
*cft
, u64 val
)
3926 clear_bit(CGRP_RELEASABLE
, &css
->cgroup
->flags
);
3928 set_bit(CGRP_NOTIFY_ON_RELEASE
, &css
->cgroup
->flags
);
3930 clear_bit(CGRP_NOTIFY_ON_RELEASE
, &css
->cgroup
->flags
);
3935 * When dput() is called asynchronously, if umount has been done and
3936 * then deactivate_super() in cgroup_free_fn() kills the superblock,
3937 * there's a small window that vfs will see the root dentry with non-zero
3938 * refcnt and trigger BUG().
3940 * That's why we hold a reference before dput() and drop it right after.
3942 static void cgroup_dput(struct cgroup
*cgrp
)
3944 struct super_block
*sb
= cgrp
->root
->sb
;
3946 atomic_inc(&sb
->s_active
);
3948 deactivate_super(sb
);
3952 * Unregister event and free resources.
3954 * Gets called from workqueue.
3956 static void cgroup_event_remove(struct work_struct
*work
)
3958 struct cgroup_event
*event
= container_of(work
, struct cgroup_event
,
3960 struct cgroup_subsys_state
*css
= event
->css
;
3962 remove_wait_queue(event
->wqh
, &event
->wait
);
3964 event
->cft
->unregister_event(css
, event
->cft
, event
->eventfd
);
3966 /* Notify userspace the event is going away. */
3967 eventfd_signal(event
->eventfd
, 1);
3969 eventfd_ctx_put(event
->eventfd
);
3975 * Gets called on POLLHUP on eventfd when user closes it.
3977 * Called with wqh->lock held and interrupts disabled.
3979 static int cgroup_event_wake(wait_queue_t
*wait
, unsigned mode
,
3980 int sync
, void *key
)
3982 struct cgroup_event
*event
= container_of(wait
,
3983 struct cgroup_event
, wait
);
3984 struct cgroup
*cgrp
= event
->css
->cgroup
;
3985 unsigned long flags
= (unsigned long)key
;
3987 if (flags
& POLLHUP
) {
3989 * If the event has been detached at cgroup removal, we
3990 * can simply return knowing the other side will cleanup
3993 * We can't race against event freeing since the other
3994 * side will require wqh->lock via remove_wait_queue(),
3997 spin_lock(&cgrp
->event_list_lock
);
3998 if (!list_empty(&event
->list
)) {
3999 list_del_init(&event
->list
);
4001 * We are in atomic context, but cgroup_event_remove()
4002 * may sleep, so we have to call it in workqueue.
4004 schedule_work(&event
->remove
);
4006 spin_unlock(&cgrp
->event_list_lock
);
4012 static void cgroup_event_ptable_queue_proc(struct file
*file
,
4013 wait_queue_head_t
*wqh
, poll_table
*pt
)
4015 struct cgroup_event
*event
= container_of(pt
,
4016 struct cgroup_event
, pt
);
4019 add_wait_queue(wqh
, &event
->wait
);
4023 * Parse input and register new cgroup event handler.
4025 * Input must be in format '<event_fd> <control_fd> <args>'.
4026 * Interpretation of args is defined by control file implementation.
4028 static int cgroup_write_event_control(struct cgroup_subsys_state
*dummy_css
,
4029 struct cftype
*cft
, const char *buffer
)
4031 struct cgroup
*cgrp
= dummy_css
->cgroup
;
4032 struct cgroup_event
*event
;
4033 struct cgroup_subsys_state
*cfile_css
;
4034 unsigned int efd
, cfd
;
4040 efd
= simple_strtoul(buffer
, &endp
, 10);
4045 cfd
= simple_strtoul(buffer
, &endp
, 10);
4046 if ((*endp
!= ' ') && (*endp
!= '\0'))
4050 event
= kzalloc(sizeof(*event
), GFP_KERNEL
);
4054 INIT_LIST_HEAD(&event
->list
);
4055 init_poll_funcptr(&event
->pt
, cgroup_event_ptable_queue_proc
);
4056 init_waitqueue_func_entry(&event
->wait
, cgroup_event_wake
);
4057 INIT_WORK(&event
->remove
, cgroup_event_remove
);
4059 efile
= eventfd_fget(efd
);
4060 if (IS_ERR(efile
)) {
4061 ret
= PTR_ERR(efile
);
4065 event
->eventfd
= eventfd_ctx_fileget(efile
);
4066 if (IS_ERR(event
->eventfd
)) {
4067 ret
= PTR_ERR(event
->eventfd
);
4074 goto out_put_eventfd
;
4077 /* the process need read permission on control file */
4078 /* AV: shouldn't we check that it's been opened for read instead? */
4079 ret
= inode_permission(file_inode(cfile
), MAY_READ
);
4083 event
->cft
= __file_cft(cfile
);
4084 if (IS_ERR(event
->cft
)) {
4085 ret
= PTR_ERR(event
->cft
);
4089 if (!event
->cft
->ss
) {
4095 * Determine the css of @cfile, verify it belongs to the same
4096 * cgroup as cgroup.event_control, and associate @event with it.
4097 * Remaining events are automatically removed on cgroup destruction
4098 * but the removal is asynchronous, so take an extra ref.
4103 event
->css
= cgroup_css(cgrp
, event
->cft
->ss
);
4104 cfile_css
= css_from_dir(cfile
->f_dentry
->d_parent
, event
->cft
->ss
);
4105 if (event
->css
&& event
->css
== cfile_css
&& css_tryget(event
->css
))
4112 if (!event
->cft
->register_event
|| !event
->cft
->unregister_event
) {
4117 ret
= event
->cft
->register_event(event
->css
, event
->cft
,
4118 event
->eventfd
, buffer
);
4122 efile
->f_op
->poll(efile
, &event
->pt
);
4124 spin_lock(&cgrp
->event_list_lock
);
4125 list_add(&event
->list
, &cgrp
->event_list
);
4126 spin_unlock(&cgrp
->event_list_lock
);
4134 css_put(event
->css
);
4138 eventfd_ctx_put(event
->eventfd
);
4147 static u64
cgroup_clone_children_read(struct cgroup_subsys_state
*css
,
4150 return test_bit(CGRP_CPUSET_CLONE_CHILDREN
, &css
->cgroup
->flags
);
4153 static int cgroup_clone_children_write(struct cgroup_subsys_state
*css
,
4154 struct cftype
*cft
, u64 val
)
4157 set_bit(CGRP_CPUSET_CLONE_CHILDREN
, &css
->cgroup
->flags
);
4159 clear_bit(CGRP_CPUSET_CLONE_CHILDREN
, &css
->cgroup
->flags
);
4163 static struct cftype cgroup_base_files
[] = {
4165 .name
= "cgroup.procs",
4166 .open
= cgroup_procs_open
,
4167 .write_u64
= cgroup_procs_write
,
4168 .release
= cgroup_pidlist_release
,
4169 .mode
= S_IRUGO
| S_IWUSR
,
4172 .name
= "cgroup.event_control",
4173 .write_string
= cgroup_write_event_control
,
4177 .name
= "cgroup.clone_children",
4178 .flags
= CFTYPE_INSANE
,
4179 .read_u64
= cgroup_clone_children_read
,
4180 .write_u64
= cgroup_clone_children_write
,
4183 .name
= "cgroup.sane_behavior",
4184 .flags
= CFTYPE_ONLY_ON_ROOT
,
4185 .read_seq_string
= cgroup_sane_behavior_show
,
4189 * Historical crazy stuff. These don't have "cgroup." prefix and
4190 * don't exist if sane_behavior. If you're depending on these, be
4191 * prepared to be burned.
4195 .flags
= CFTYPE_INSANE
, /* use "procs" instead */
4196 .open
= cgroup_tasks_open
,
4197 .write_u64
= cgroup_tasks_write
,
4198 .release
= cgroup_pidlist_release
,
4199 .mode
= S_IRUGO
| S_IWUSR
,
4202 .name
= "notify_on_release",
4203 .flags
= CFTYPE_INSANE
,
4204 .read_u64
= cgroup_read_notify_on_release
,
4205 .write_u64
= cgroup_write_notify_on_release
,
4208 .name
= "release_agent",
4209 .flags
= CFTYPE_INSANE
| CFTYPE_ONLY_ON_ROOT
,
4210 .read_seq_string
= cgroup_release_agent_show
,
4211 .write_string
= cgroup_release_agent_write
,
4212 .max_write_len
= PATH_MAX
,
4218 * cgroup_populate_dir - create subsys files in a cgroup directory
4219 * @cgrp: target cgroup
4220 * @subsys_mask: mask of the subsystem ids whose files should be added
4222 * On failure, no file is added.
4224 static int cgroup_populate_dir(struct cgroup
*cgrp
, unsigned long subsys_mask
)
4226 struct cgroup_subsys
*ss
;
4229 /* process cftsets of each subsystem */
4230 for_each_subsys(ss
, i
) {
4231 struct cftype_set
*set
;
4233 if (!test_bit(i
, &subsys_mask
))
4236 list_for_each_entry(set
, &ss
->cftsets
, node
) {
4237 ret
= cgroup_addrm_files(cgrp
, set
->cfts
, true);
4243 /* This cgroup is ready now */
4244 for_each_root_subsys(cgrp
->root
, ss
) {
4245 struct cgroup_subsys_state
*css
= cgroup_css(cgrp
, ss
);
4246 struct css_id
*id
= rcu_dereference_protected(css
->id
, true);
4249 * Update id->css pointer and make this css visible from
4250 * CSS ID functions. This pointer will be dereferened
4251 * from RCU-read-side without locks.
4254 rcu_assign_pointer(id
->css
, css
);
4259 cgroup_clear_dir(cgrp
, subsys_mask
);
4264 * css destruction is four-stage process.
4266 * 1. Destruction starts. Killing of the percpu_ref is initiated.
4267 * Implemented in kill_css().
4269 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
4270 * and thus css_tryget() is guaranteed to fail, the css can be offlined
4271 * by invoking offline_css(). After offlining, the base ref is put.
4272 * Implemented in css_killed_work_fn().
4274 * 3. When the percpu_ref reaches zero, the only possible remaining
4275 * accessors are inside RCU read sections. css_release() schedules the
4278 * 4. After the grace period, the css can be freed. Implemented in
4279 * css_free_work_fn().
4281 * It is actually hairier because both step 2 and 4 require process context
4282 * and thus involve punting to css->destroy_work adding two additional
4283 * steps to the already complex sequence.
4285 static void css_free_work_fn(struct work_struct
*work
)
4287 struct cgroup_subsys_state
*css
=
4288 container_of(work
, struct cgroup_subsys_state
, destroy_work
);
4289 struct cgroup
*cgrp
= css
->cgroup
;
4292 css_put(css
->parent
);
4294 css
->ss
->css_free(css
);
4298 static void css_free_rcu_fn(struct rcu_head
*rcu_head
)
4300 struct cgroup_subsys_state
*css
=
4301 container_of(rcu_head
, struct cgroup_subsys_state
, rcu_head
);
4304 * css holds an extra ref to @cgrp->dentry which is put on the last
4305 * css_put(). dput() requires process context which we don't have.
4307 INIT_WORK(&css
->destroy_work
, css_free_work_fn
);
4308 schedule_work(&css
->destroy_work
);
4311 static void css_release(struct percpu_ref
*ref
)
4313 struct cgroup_subsys_state
*css
=
4314 container_of(ref
, struct cgroup_subsys_state
, refcnt
);
4316 call_rcu(&css
->rcu_head
, css_free_rcu_fn
);
4319 static void init_css(struct cgroup_subsys_state
*css
, struct cgroup_subsys
*ss
,
4320 struct cgroup
*cgrp
)
4328 css
->parent
= cgroup_css(cgrp
->parent
, ss
);
4330 css
->flags
|= CSS_ROOT
;
4332 BUG_ON(cgroup_css(cgrp
, ss
));
4335 /* invoke ->css_online() on a new CSS and mark it online if successful */
4336 static int online_css(struct cgroup_subsys_state
*css
)
4338 struct cgroup_subsys
*ss
= css
->ss
;
4341 lockdep_assert_held(&cgroup_mutex
);
4344 ret
= ss
->css_online(css
);
4346 css
->flags
|= CSS_ONLINE
;
4347 css
->cgroup
->nr_css
++;
4348 rcu_assign_pointer(css
->cgroup
->subsys
[ss
->subsys_id
], css
);
4353 /* if the CSS is online, invoke ->css_offline() on it and mark it offline */
4354 static void offline_css(struct cgroup_subsys_state
*css
)
4356 struct cgroup_subsys
*ss
= css
->ss
;
4358 lockdep_assert_held(&cgroup_mutex
);
4360 if (!(css
->flags
& CSS_ONLINE
))
4363 if (ss
->css_offline
)
4364 ss
->css_offline(css
);
4366 css
->flags
&= ~CSS_ONLINE
;
4367 css
->cgroup
->nr_css
--;
4368 RCU_INIT_POINTER(css
->cgroup
->subsys
[ss
->subsys_id
], css
);
4372 * cgroup_create - create a cgroup
4373 * @parent: cgroup that will be parent of the new cgroup
4374 * @dentry: dentry of the new cgroup
4375 * @mode: mode to set on new inode
4377 * Must be called with the mutex on the parent inode held
4379 static long cgroup_create(struct cgroup
*parent
, struct dentry
*dentry
,
4382 struct cgroup_subsys_state
*css_ar
[CGROUP_SUBSYS_COUNT
] = { };
4383 struct cgroup
*cgrp
;
4384 struct cgroup_name
*name
;
4385 struct cgroupfs_root
*root
= parent
->root
;
4387 struct cgroup_subsys
*ss
;
4388 struct super_block
*sb
= root
->sb
;
4390 /* allocate the cgroup and its ID, 0 is reserved for the root */
4391 cgrp
= kzalloc(sizeof(*cgrp
), GFP_KERNEL
);
4395 name
= cgroup_alloc_name(dentry
);
4398 rcu_assign_pointer(cgrp
->name
, name
);
4401 * Temporarily set the pointer to NULL, so idr_find() won't return
4402 * a half-baked cgroup.
4404 cgrp
->id
= idr_alloc(&root
->cgroup_idr
, NULL
, 1, 0, GFP_KERNEL
);
4409 * Only live parents can have children. Note that the liveliness
4410 * check isn't strictly necessary because cgroup_mkdir() and
4411 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4412 * anyway so that locking is contained inside cgroup proper and we
4413 * don't get nasty surprises if we ever grow another caller.
4415 if (!cgroup_lock_live_group(parent
)) {
4420 /* Grab a reference on the superblock so the hierarchy doesn't
4421 * get deleted on unmount if there are child cgroups. This
4422 * can be done outside cgroup_mutex, since the sb can't
4423 * disappear while someone has an open control file on the
4425 atomic_inc(&sb
->s_active
);
4427 init_cgroup_housekeeping(cgrp
);
4429 dentry
->d_fsdata
= cgrp
;
4430 cgrp
->dentry
= dentry
;
4432 cgrp
->parent
= parent
;
4433 cgrp
->dummy_css
.parent
= &parent
->dummy_css
;
4434 cgrp
->root
= parent
->root
;
4436 if (notify_on_release(parent
))
4437 set_bit(CGRP_NOTIFY_ON_RELEASE
, &cgrp
->flags
);
4439 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN
, &parent
->flags
))
4440 set_bit(CGRP_CPUSET_CLONE_CHILDREN
, &cgrp
->flags
);
4442 for_each_root_subsys(root
, ss
) {
4443 struct cgroup_subsys_state
*css
;
4445 css
= ss
->css_alloc(cgroup_css(parent
, ss
));
4450 css_ar
[ss
->subsys_id
] = css
;
4452 err
= percpu_ref_init(&css
->refcnt
, css_release
);
4456 init_css(css
, ss
, cgrp
);
4459 err
= alloc_css_id(css
);
4466 * Create directory. cgroup_create_file() returns with the new
4467 * directory locked on success so that it can be populated without
4468 * dropping cgroup_mutex.
4470 err
= cgroup_create_file(dentry
, S_IFDIR
| mode
, sb
);
4473 lockdep_assert_held(&dentry
->d_inode
->i_mutex
);
4475 cgrp
->serial_nr
= cgroup_serial_nr_next
++;
4477 /* allocation complete, commit to creation */
4478 list_add_tail_rcu(&cgrp
->sibling
, &cgrp
->parent
->children
);
4479 root
->number_of_cgroups
++;
4481 /* each css holds a ref to the cgroup's dentry and the parent css */
4482 for_each_root_subsys(root
, ss
) {
4483 struct cgroup_subsys_state
*css
= css_ar
[ss
->subsys_id
];
4486 css_get(css
->parent
);
4489 /* hold a ref to the parent's dentry */
4490 dget(parent
->dentry
);
4492 /* creation succeeded, notify subsystems */
4493 for_each_root_subsys(root
, ss
) {
4494 struct cgroup_subsys_state
*css
= css_ar
[ss
->subsys_id
];
4496 err
= online_css(css
);
4500 if (ss
->broken_hierarchy
&& !ss
->warned_broken_hierarchy
&&
4502 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",
4503 current
->comm
, current
->pid
, ss
->name
);
4504 if (!strcmp(ss
->name
, "memory"))
4505 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4506 ss
->warned_broken_hierarchy
= true;
4510 idr_replace(&root
->cgroup_idr
, cgrp
, cgrp
->id
);
4512 err
= cgroup_addrm_files(cgrp
, cgroup_base_files
, true);
4516 err
= cgroup_populate_dir(cgrp
, root
->subsys_mask
);
4520 mutex_unlock(&cgroup_mutex
);
4521 mutex_unlock(&cgrp
->dentry
->d_inode
->i_mutex
);
4526 for_each_root_subsys(root
, ss
) {
4527 struct cgroup_subsys_state
*css
= css_ar
[ss
->subsys_id
];
4530 percpu_ref_cancel_init(&css
->refcnt
);
4534 mutex_unlock(&cgroup_mutex
);
4535 /* Release the reference count that we took on the superblock */
4536 deactivate_super(sb
);
4538 idr_remove(&root
->cgroup_idr
, cgrp
->id
);
4540 kfree(rcu_dereference_raw(cgrp
->name
));
4546 cgroup_destroy_locked(cgrp
);
4547 mutex_unlock(&cgroup_mutex
);
4548 mutex_unlock(&dentry
->d_inode
->i_mutex
);
4552 static int cgroup_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
4554 struct cgroup
*c_parent
= dentry
->d_parent
->d_fsdata
;
4556 /* the vfs holds inode->i_mutex already */
4557 return cgroup_create(c_parent
, dentry
, mode
| S_IFDIR
);
4561 * This is called when the refcnt of a css is confirmed to be killed.
4562 * css_tryget() is now guaranteed to fail.
4564 static void css_killed_work_fn(struct work_struct
*work
)
4566 struct cgroup_subsys_state
*css
=
4567 container_of(work
, struct cgroup_subsys_state
, destroy_work
);
4568 struct cgroup
*cgrp
= css
->cgroup
;
4570 mutex_lock(&cgroup_mutex
);
4573 * css_tryget() is guaranteed to fail now. Tell subsystems to
4574 * initate destruction.
4579 * If @cgrp is marked dead, it's waiting for refs of all css's to
4580 * be disabled before proceeding to the second phase of cgroup
4581 * destruction. If we are the last one, kick it off.
4583 if (!cgrp
->nr_css
&& cgroup_is_dead(cgrp
))
4584 cgroup_destroy_css_killed(cgrp
);
4586 mutex_unlock(&cgroup_mutex
);
4589 * Put the css refs from kill_css(). Each css holds an extra
4590 * reference to the cgroup's dentry and cgroup removal proceeds
4591 * regardless of css refs. On the last put of each css, whenever
4592 * that may be, the extra dentry ref is put so that dentry
4593 * destruction happens only after all css's are released.
4598 /* css kill confirmation processing requires process context, bounce */
4599 static void css_killed_ref_fn(struct percpu_ref
*ref
)
4601 struct cgroup_subsys_state
*css
=
4602 container_of(ref
, struct cgroup_subsys_state
, refcnt
);
4604 INIT_WORK(&css
->destroy_work
, css_killed_work_fn
);
4605 schedule_work(&css
->destroy_work
);
4609 * kill_css - destroy a css
4610 * @css: css to destroy
4612 * This function initiates destruction of @css by removing cgroup interface
4613 * files and putting its base reference. ->css_offline() will be invoked
4614 * asynchronously once css_tryget() is guaranteed to fail and when the
4615 * reference count reaches zero, @css will be released.
4617 static void kill_css(struct cgroup_subsys_state
*css
)
4619 cgroup_clear_dir(css
->cgroup
, 1 << css
->ss
->subsys_id
);
4622 * Killing would put the base ref, but we need to keep it alive
4623 * until after ->css_offline().
4628 * cgroup core guarantees that, by the time ->css_offline() is
4629 * invoked, no new css reference will be given out via
4630 * css_tryget(). We can't simply call percpu_ref_kill() and
4631 * proceed to offlining css's because percpu_ref_kill() doesn't
4632 * guarantee that the ref is seen as killed on all CPUs on return.
4634 * Use percpu_ref_kill_and_confirm() to get notifications as each
4635 * css is confirmed to be seen as killed on all CPUs.
4637 percpu_ref_kill_and_confirm(&css
->refcnt
, css_killed_ref_fn
);
4641 * cgroup_destroy_locked - the first stage of cgroup destruction
4642 * @cgrp: cgroup to be destroyed
4644 * css's make use of percpu refcnts whose killing latency shouldn't be
4645 * exposed to userland and are RCU protected. Also, cgroup core needs to
4646 * guarantee that css_tryget() won't succeed by the time ->css_offline() is
4647 * invoked. To satisfy all the requirements, destruction is implemented in
4648 * the following two steps.
4650 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4651 * userland visible parts and start killing the percpu refcnts of
4652 * css's. Set up so that the next stage will be kicked off once all
4653 * the percpu refcnts are confirmed to be killed.
4655 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4656 * rest of destruction. Once all cgroup references are gone, the
4657 * cgroup is RCU-freed.
4659 * This function implements s1. After this step, @cgrp is gone as far as
4660 * the userland is concerned and a new cgroup with the same name may be
4661 * created. As cgroup doesn't care about the names internally, this
4662 * doesn't cause any problem.
4664 static int cgroup_destroy_locked(struct cgroup
*cgrp
)
4665 __releases(&cgroup_mutex
) __acquires(&cgroup_mutex
)
4667 struct dentry
*d
= cgrp
->dentry
;
4668 struct cgroup_event
*event
, *tmp
;
4669 struct cgroup_subsys
*ss
;
4670 struct cgroup
*child
;
4673 lockdep_assert_held(&d
->d_inode
->i_mutex
);
4674 lockdep_assert_held(&cgroup_mutex
);
4677 * css_set_lock synchronizes access to ->cset_links and prevents
4678 * @cgrp from being removed while __put_css_set() is in progress.
4680 read_lock(&css_set_lock
);
4681 empty
= list_empty(&cgrp
->cset_links
);
4682 read_unlock(&css_set_lock
);
4687 * Make sure there's no live children. We can't test ->children
4688 * emptiness as dead children linger on it while being destroyed;
4689 * otherwise, "rmdir parent/child parent" may fail with -EBUSY.
4693 list_for_each_entry_rcu(child
, &cgrp
->children
, sibling
) {
4694 empty
= cgroup_is_dead(child
);
4703 * Initiate massacre of all css's. cgroup_destroy_css_killed()
4704 * will be invoked to perform the rest of destruction once the
4705 * percpu refs of all css's are confirmed to be killed.
4707 for_each_root_subsys(cgrp
->root
, ss
)
4708 kill_css(cgroup_css(cgrp
, ss
));
4711 * Mark @cgrp dead. This prevents further task migration and child
4712 * creation by disabling cgroup_lock_live_group(). Note that
4713 * CGRP_DEAD assertion is depended upon by css_next_child() to
4714 * resume iteration after dropping RCU read lock. See
4715 * css_next_child() for details.
4717 set_bit(CGRP_DEAD
, &cgrp
->flags
);
4719 /* CGRP_DEAD is set, remove from ->release_list for the last time */
4720 raw_spin_lock(&release_list_lock
);
4721 if (!list_empty(&cgrp
->release_list
))
4722 list_del_init(&cgrp
->release_list
);
4723 raw_spin_unlock(&release_list_lock
);
4726 * If @cgrp has css's attached, the second stage of cgroup
4727 * destruction is kicked off from css_killed_work_fn() after the
4728 * refs of all attached css's are killed. If @cgrp doesn't have
4729 * any css, we kick it off here.
4732 cgroup_destroy_css_killed(cgrp
);
4735 * Clear the base files and remove @cgrp directory. The removal
4736 * puts the base ref but we aren't quite done with @cgrp yet, so
4739 cgroup_addrm_files(cgrp
, cgroup_base_files
, false);
4741 cgroup_d_remove_dir(d
);
4744 * Unregister events and notify userspace.
4745 * Notify userspace about cgroup removing only after rmdir of cgroup
4746 * directory to avoid race between userspace and kernelspace.
4748 spin_lock(&cgrp
->event_list_lock
);
4749 list_for_each_entry_safe(event
, tmp
, &cgrp
->event_list
, list
) {
4750 list_del_init(&event
->list
);
4751 schedule_work(&event
->remove
);
4753 spin_unlock(&cgrp
->event_list_lock
);
4759 * cgroup_destroy_css_killed - the second step of cgroup destruction
4760 * @work: cgroup->destroy_free_work
4762 * This function is invoked from a work item for a cgroup which is being
4763 * destroyed after all css's are offlined and performs the rest of
4764 * destruction. This is the second step of destruction described in the
4765 * comment above cgroup_destroy_locked().
4767 static void cgroup_destroy_css_killed(struct cgroup
*cgrp
)
4769 struct cgroup
*parent
= cgrp
->parent
;
4770 struct dentry
*d
= cgrp
->dentry
;
4772 lockdep_assert_held(&cgroup_mutex
);
4774 /* delete this cgroup from parent->children */
4775 list_del_rcu(&cgrp
->sibling
);
4778 * We should remove the cgroup object from idr before its grace
4779 * period starts, so we won't be looking up a cgroup while the
4780 * cgroup is being freed.
4782 idr_remove(&cgrp
->root
->cgroup_idr
, cgrp
->id
);
4787 set_bit(CGRP_RELEASABLE
, &parent
->flags
);
4788 check_for_release(parent
);
4791 static int cgroup_rmdir(struct inode
*unused_dir
, struct dentry
*dentry
)
4795 mutex_lock(&cgroup_mutex
);
4796 ret
= cgroup_destroy_locked(dentry
->d_fsdata
);
4797 mutex_unlock(&cgroup_mutex
);
4802 static void __init_or_module
cgroup_init_cftsets(struct cgroup_subsys
*ss
)
4804 INIT_LIST_HEAD(&ss
->cftsets
);
4807 * base_cftset is embedded in subsys itself, no need to worry about
4810 if (ss
->base_cftypes
) {
4813 for (cft
= ss
->base_cftypes
; cft
->name
[0] != '\0'; cft
++)
4816 ss
->base_cftset
.cfts
= ss
->base_cftypes
;
4817 list_add_tail(&ss
->base_cftset
.node
, &ss
->cftsets
);
4821 static void __init
cgroup_init_subsys(struct cgroup_subsys
*ss
)
4823 struct cgroup_subsys_state
*css
;
4825 printk(KERN_INFO
"Initializing cgroup subsys %s\n", ss
->name
);
4827 mutex_lock(&cgroup_mutex
);
4829 /* init base cftset */
4830 cgroup_init_cftsets(ss
);
4832 /* Create the top cgroup state for this subsystem */
4833 list_add(&ss
->sibling
, &cgroup_dummy_root
.subsys_list
);
4834 ss
->root
= &cgroup_dummy_root
;
4835 css
= ss
->css_alloc(cgroup_css(cgroup_dummy_top
, ss
));
4836 /* We don't handle early failures gracefully */
4837 BUG_ON(IS_ERR(css
));
4838 init_css(css
, ss
, cgroup_dummy_top
);
4840 /* Update the init_css_set to contain a subsys
4841 * pointer to this state - since the subsystem is
4842 * newly registered, all tasks and hence the
4843 * init_css_set is in the subsystem's top cgroup. */
4844 init_css_set
.subsys
[ss
->subsys_id
] = css
;
4846 need_forkexit_callback
|= ss
->fork
|| ss
->exit
;
4848 /* At system boot, before all subsystems have been
4849 * registered, no tasks have been forked, so we don't
4850 * need to invoke fork callbacks here. */
4851 BUG_ON(!list_empty(&init_task
.tasks
));
4853 BUG_ON(online_css(css
));
4855 mutex_unlock(&cgroup_mutex
);
4857 /* this function shouldn't be used with modular subsystems, since they
4858 * need to register a subsys_id, among other things */
4863 * cgroup_load_subsys: load and register a modular subsystem at runtime
4864 * @ss: the subsystem to load
4866 * This function should be called in a modular subsystem's initcall. If the
4867 * subsystem is built as a module, it will be assigned a new subsys_id and set
4868 * up for use. If the subsystem is built-in anyway, work is delegated to the
4869 * simpler cgroup_init_subsys.
4871 int __init_or_module
cgroup_load_subsys(struct cgroup_subsys
*ss
)
4873 struct cgroup_subsys_state
*css
;
4875 struct hlist_node
*tmp
;
4876 struct css_set
*cset
;
4879 /* check name and function validity */
4880 if (ss
->name
== NULL
|| strlen(ss
->name
) > MAX_CGROUP_TYPE_NAMELEN
||
4881 ss
->css_alloc
== NULL
|| ss
->css_free
== NULL
)
4885 * we don't support callbacks in modular subsystems. this check is
4886 * before the ss->module check for consistency; a subsystem that could
4887 * be a module should still have no callbacks even if the user isn't
4888 * compiling it as one.
4890 if (ss
->fork
|| ss
->exit
)
4894 * an optionally modular subsystem is built-in: we want to do nothing,
4895 * since cgroup_init_subsys will have already taken care of it.
4897 if (ss
->module
== NULL
) {
4898 /* a sanity check */
4899 BUG_ON(cgroup_subsys
[ss
->subsys_id
] != ss
);
4903 /* init base cftset */
4904 cgroup_init_cftsets(ss
);
4906 mutex_lock(&cgroup_mutex
);
4907 cgroup_subsys
[ss
->subsys_id
] = ss
;
4910 * no ss->css_alloc seems to need anything important in the ss
4911 * struct, so this can happen first (i.e. before the dummy root
4914 css
= ss
->css_alloc(cgroup_css(cgroup_dummy_top
, ss
));
4916 /* failure case - need to deassign the cgroup_subsys[] slot. */
4917 cgroup_subsys
[ss
->subsys_id
] = NULL
;
4918 mutex_unlock(&cgroup_mutex
);
4919 return PTR_ERR(css
);
4922 list_add(&ss
->sibling
, &cgroup_dummy_root
.subsys_list
);
4923 ss
->root
= &cgroup_dummy_root
;
4925 /* our new subsystem will be attached to the dummy hierarchy. */
4926 init_css(css
, ss
, cgroup_dummy_top
);
4927 /* init_idr must be after init_css() because it sets css->id. */
4929 ret
= cgroup_init_idr(ss
, css
);
4935 * Now we need to entangle the css into the existing css_sets. unlike
4936 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4937 * will need a new pointer to it; done by iterating the css_set_table.
4938 * furthermore, modifying the existing css_sets will corrupt the hash
4939 * table state, so each changed css_set will need its hash recomputed.
4940 * this is all done under the css_set_lock.
4942 write_lock(&css_set_lock
);
4943 hash_for_each_safe(css_set_table
, i
, tmp
, cset
, hlist
) {
4944 /* skip entries that we already rehashed */
4945 if (cset
->subsys
[ss
->subsys_id
])
4947 /* remove existing entry */
4948 hash_del(&cset
->hlist
);
4950 cset
->subsys
[ss
->subsys_id
] = css
;
4951 /* recompute hash and restore entry */
4952 key
= css_set_hash(cset
->subsys
);
4953 hash_add(css_set_table
, &cset
->hlist
, key
);
4955 write_unlock(&css_set_lock
);
4957 ret
= online_css(css
);
4962 mutex_unlock(&cgroup_mutex
);
4966 mutex_unlock(&cgroup_mutex
);
4967 /* @ss can't be mounted here as try_module_get() would fail */
4968 cgroup_unload_subsys(ss
);
4971 EXPORT_SYMBOL_GPL(cgroup_load_subsys
);
4974 * cgroup_unload_subsys: unload a modular subsystem
4975 * @ss: the subsystem to unload
4977 * This function should be called in a modular subsystem's exitcall. When this
4978 * function is invoked, the refcount on the subsystem's module will be 0, so
4979 * the subsystem will not be attached to any hierarchy.
4981 void cgroup_unload_subsys(struct cgroup_subsys
*ss
)
4983 struct cgrp_cset_link
*link
;
4985 BUG_ON(ss
->module
== NULL
);
4988 * we shouldn't be called if the subsystem is in use, and the use of
4989 * try_module_get() in rebind_subsystems() should ensure that it
4990 * doesn't start being used while we're killing it off.
4992 BUG_ON(ss
->root
!= &cgroup_dummy_root
);
4994 mutex_lock(&cgroup_mutex
);
4996 offline_css(cgroup_css(cgroup_dummy_top
, ss
));
4999 idr_destroy(&ss
->idr
);
5001 /* deassign the subsys_id */
5002 cgroup_subsys
[ss
->subsys_id
] = NULL
;
5004 /* remove subsystem from the dummy root's list of subsystems */
5005 list_del_init(&ss
->sibling
);
5008 * disentangle the css from all css_sets attached to the dummy
5009 * top. as in loading, we need to pay our respects to the hashtable
5012 write_lock(&css_set_lock
);
5013 list_for_each_entry(link
, &cgroup_dummy_top
->cset_links
, cset_link
) {
5014 struct css_set
*cset
= link
->cset
;
5017 hash_del(&cset
->hlist
);
5018 cset
->subsys
[ss
->subsys_id
] = NULL
;
5019 key
= css_set_hash(cset
->subsys
);
5020 hash_add(css_set_table
, &cset
->hlist
, key
);
5022 write_unlock(&css_set_lock
);
5025 * remove subsystem's css from the cgroup_dummy_top and free it -
5026 * need to free before marking as null because ss->css_free needs
5027 * the cgrp->subsys pointer to find their state. note that this
5028 * also takes care of freeing the css_id.
5030 ss
->css_free(cgroup_css(cgroup_dummy_top
, ss
));
5031 RCU_INIT_POINTER(cgroup_dummy_top
->subsys
[ss
->subsys_id
], NULL
);
5033 mutex_unlock(&cgroup_mutex
);
5035 EXPORT_SYMBOL_GPL(cgroup_unload_subsys
);
5038 * cgroup_init_early - cgroup initialization at system boot
5040 * Initialize cgroups at system boot, and initialize any
5041 * subsystems that request early init.
5043 int __init
cgroup_init_early(void)
5045 struct cgroup_subsys
*ss
;
5048 atomic_set(&init_css_set
.refcount
, 1);
5049 INIT_LIST_HEAD(&init_css_set
.cgrp_links
);
5050 INIT_LIST_HEAD(&init_css_set
.tasks
);
5051 INIT_HLIST_NODE(&init_css_set
.hlist
);
5053 init_cgroup_root(&cgroup_dummy_root
);
5054 cgroup_root_count
= 1;
5055 RCU_INIT_POINTER(init_task
.cgroups
, &init_css_set
);
5057 init_cgrp_cset_link
.cset
= &init_css_set
;
5058 init_cgrp_cset_link
.cgrp
= cgroup_dummy_top
;
5059 list_add(&init_cgrp_cset_link
.cset_link
, &cgroup_dummy_top
->cset_links
);
5060 list_add(&init_cgrp_cset_link
.cgrp_link
, &init_css_set
.cgrp_links
);
5062 /* at bootup time, we don't worry about modular subsystems */
5063 for_each_builtin_subsys(ss
, i
) {
5065 BUG_ON(strlen(ss
->name
) > MAX_CGROUP_TYPE_NAMELEN
);
5066 BUG_ON(!ss
->css_alloc
);
5067 BUG_ON(!ss
->css_free
);
5068 if (ss
->subsys_id
!= i
) {
5069 printk(KERN_ERR
"cgroup: Subsys %s id == %d\n",
5070 ss
->name
, ss
->subsys_id
);
5075 cgroup_init_subsys(ss
);
5081 * cgroup_init - cgroup initialization
5083 * Register cgroup filesystem and /proc file, and initialize
5084 * any subsystems that didn't request early init.
5086 int __init
cgroup_init(void)
5088 struct cgroup_subsys
*ss
;
5092 err
= bdi_init(&cgroup_backing_dev_info
);
5096 for_each_builtin_subsys(ss
, i
) {
5097 if (!ss
->early_init
)
5098 cgroup_init_subsys(ss
);
5100 cgroup_init_idr(ss
, init_css_set
.subsys
[ss
->subsys_id
]);
5103 /* allocate id for the dummy hierarchy */
5104 mutex_lock(&cgroup_mutex
);
5105 mutex_lock(&cgroup_root_mutex
);
5107 /* Add init_css_set to the hash table */
5108 key
= css_set_hash(init_css_set
.subsys
);
5109 hash_add(css_set_table
, &init_css_set
.hlist
, key
);
5111 BUG_ON(cgroup_init_root_id(&cgroup_dummy_root
, 0, 1));
5113 err
= idr_alloc(&cgroup_dummy_root
.cgroup_idr
, cgroup_dummy_top
,
5117 mutex_unlock(&cgroup_root_mutex
);
5118 mutex_unlock(&cgroup_mutex
);
5120 cgroup_kobj
= kobject_create_and_add("cgroup", fs_kobj
);
5126 err
= register_filesystem(&cgroup_fs_type
);
5128 kobject_put(cgroup_kobj
);
5132 proc_create("cgroups", 0, NULL
, &proc_cgroupstats_operations
);
5136 bdi_destroy(&cgroup_backing_dev_info
);
5142 * proc_cgroup_show()
5143 * - Print task's cgroup paths into seq_file, one line for each hierarchy
5144 * - Used for /proc/<pid>/cgroup.
5145 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
5146 * doesn't really matter if tsk->cgroup changes after we read it,
5147 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
5148 * anyway. No need to check that tsk->cgroup != NULL, thanks to
5149 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
5150 * cgroup to top_cgroup.
5153 /* TODO: Use a proper seq_file iterator */
5154 int proc_cgroup_show(struct seq_file
*m
, void *v
)
5157 struct task_struct
*tsk
;
5160 struct cgroupfs_root
*root
;
5163 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
5169 tsk
= get_pid_task(pid
, PIDTYPE_PID
);
5175 mutex_lock(&cgroup_mutex
);
5177 for_each_active_root(root
) {
5178 struct cgroup_subsys
*ss
;
5179 struct cgroup
*cgrp
;
5182 seq_printf(m
, "%d:", root
->hierarchy_id
);
5183 for_each_root_subsys(root
, ss
)
5184 seq_printf(m
, "%s%s", count
++ ? "," : "", ss
->name
);
5185 if (strlen(root
->name
))
5186 seq_printf(m
, "%sname=%s", count
? "," : "",
5189 cgrp
= task_cgroup_from_root(tsk
, root
);
5190 retval
= cgroup_path(cgrp
, buf
, PAGE_SIZE
);
5198 mutex_unlock(&cgroup_mutex
);
5199 put_task_struct(tsk
);
5206 /* Display information about each subsystem and each hierarchy */
5207 static int proc_cgroupstats_show(struct seq_file
*m
, void *v
)
5209 struct cgroup_subsys
*ss
;
5212 seq_puts(m
, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
5214 * ideally we don't want subsystems moving around while we do this.
5215 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
5216 * subsys/hierarchy state.
5218 mutex_lock(&cgroup_mutex
);
5220 for_each_subsys(ss
, i
)
5221 seq_printf(m
, "%s\t%d\t%d\t%d\n",
5222 ss
->name
, ss
->root
->hierarchy_id
,
5223 ss
->root
->number_of_cgroups
, !ss
->disabled
);
5225 mutex_unlock(&cgroup_mutex
);
5229 static int cgroupstats_open(struct inode
*inode
, struct file
*file
)
5231 return single_open(file
, proc_cgroupstats_show
, NULL
);
5234 static const struct file_operations proc_cgroupstats_operations
= {
5235 .open
= cgroupstats_open
,
5237 .llseek
= seq_lseek
,
5238 .release
= single_release
,
5242 * cgroup_fork - attach newly forked task to its parents cgroup.
5243 * @child: pointer to task_struct of forking parent process.
5245 * Description: A task inherits its parent's cgroup at fork().
5247 * A pointer to the shared css_set was automatically copied in
5248 * fork.c by dup_task_struct(). However, we ignore that copy, since
5249 * it was not made under the protection of RCU or cgroup_mutex, so
5250 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
5251 * have already changed current->cgroups, allowing the previously
5252 * referenced cgroup group to be removed and freed.
5254 * At the point that cgroup_fork() is called, 'current' is the parent
5255 * task, and the passed argument 'child' points to the child task.
5257 void cgroup_fork(struct task_struct
*child
)
5260 get_css_set(task_css_set(current
));
5261 child
->cgroups
= current
->cgroups
;
5262 task_unlock(current
);
5263 INIT_LIST_HEAD(&child
->cg_list
);
5267 * cgroup_post_fork - called on a new task after adding it to the task list
5268 * @child: the task in question
5270 * Adds the task to the list running through its css_set if necessary and
5271 * call the subsystem fork() callbacks. Has to be after the task is
5272 * visible on the task list in case we race with the first call to
5273 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
5276 void cgroup_post_fork(struct task_struct
*child
)
5278 struct cgroup_subsys
*ss
;
5282 * use_task_css_set_links is set to 1 before we walk the tasklist
5283 * under the tasklist_lock and we read it here after we added the child
5284 * to the tasklist under the tasklist_lock as well. If the child wasn't
5285 * yet in the tasklist when we walked through it from
5286 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
5287 * should be visible now due to the paired locking and barriers implied
5288 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
5289 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
5292 if (use_task_css_set_links
) {
5293 write_lock(&css_set_lock
);
5295 if (list_empty(&child
->cg_list
))
5296 list_add(&child
->cg_list
, &task_css_set(child
)->tasks
);
5298 write_unlock(&css_set_lock
);
5302 * Call ss->fork(). This must happen after @child is linked on
5303 * css_set; otherwise, @child might change state between ->fork()
5304 * and addition to css_set.
5306 if (need_forkexit_callback
) {
5308 * fork/exit callbacks are supported only for builtin
5309 * subsystems, and the builtin section of the subsys
5310 * array is immutable, so we don't need to lock the
5311 * subsys array here. On the other hand, modular section
5312 * of the array can be freed at module unload, so we
5315 for_each_builtin_subsys(ss
, i
)
5322 * cgroup_exit - detach cgroup from exiting task
5323 * @tsk: pointer to task_struct of exiting process
5324 * @run_callback: run exit callbacks?
5326 * Description: Detach cgroup from @tsk and release it.
5328 * Note that cgroups marked notify_on_release force every task in
5329 * them to take the global cgroup_mutex mutex when exiting.
5330 * This could impact scaling on very large systems. Be reluctant to
5331 * use notify_on_release cgroups where very high task exit scaling
5332 * is required on large systems.
5334 * the_top_cgroup_hack:
5336 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
5338 * We call cgroup_exit() while the task is still competent to
5339 * handle notify_on_release(), then leave the task attached to the
5340 * root cgroup in each hierarchy for the remainder of its exit.
5342 * To do this properly, we would increment the reference count on
5343 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
5344 * code we would add a second cgroup function call, to drop that
5345 * reference. This would just create an unnecessary hot spot on
5346 * the top_cgroup reference count, to no avail.
5348 * Normally, holding a reference to a cgroup without bumping its
5349 * count is unsafe. The cgroup could go away, or someone could
5350 * attach us to a different cgroup, decrementing the count on
5351 * the first cgroup that we never incremented. But in this case,
5352 * top_cgroup isn't going away, and either task has PF_EXITING set,
5353 * which wards off any cgroup_attach_task() attempts, or task is a failed
5354 * fork, never visible to cgroup_attach_task.
5356 void cgroup_exit(struct task_struct
*tsk
, int run_callbacks
)
5358 struct cgroup_subsys
*ss
;
5359 struct css_set
*cset
;
5363 * Unlink from the css_set task list if necessary.
5364 * Optimistically check cg_list before taking
5367 if (!list_empty(&tsk
->cg_list
)) {
5368 write_lock(&css_set_lock
);
5369 if (!list_empty(&tsk
->cg_list
))
5370 list_del_init(&tsk
->cg_list
);
5371 write_unlock(&css_set_lock
);
5374 /* Reassign the task to the init_css_set. */
5376 cset
= task_css_set(tsk
);
5377 RCU_INIT_POINTER(tsk
->cgroups
, &init_css_set
);
5379 if (run_callbacks
&& need_forkexit_callback
) {
5381 * fork/exit callbacks are supported only for builtin
5382 * subsystems, see cgroup_post_fork() for details.
5384 for_each_builtin_subsys(ss
, i
) {
5386 struct cgroup_subsys_state
*old_css
= cset
->subsys
[i
];
5387 struct cgroup_subsys_state
*css
= task_css(tsk
, i
);
5389 ss
->exit(css
, old_css
, tsk
);
5395 put_css_set_taskexit(cset
);
5398 static void check_for_release(struct cgroup
*cgrp
)
5400 if (cgroup_is_releasable(cgrp
) &&
5401 list_empty(&cgrp
->cset_links
) && list_empty(&cgrp
->children
)) {
5403 * Control Group is currently removeable. If it's not
5404 * already queued for a userspace notification, queue
5407 int need_schedule_work
= 0;
5409 raw_spin_lock(&release_list_lock
);
5410 if (!cgroup_is_dead(cgrp
) &&
5411 list_empty(&cgrp
->release_list
)) {
5412 list_add(&cgrp
->release_list
, &release_list
);
5413 need_schedule_work
= 1;
5415 raw_spin_unlock(&release_list_lock
);
5416 if (need_schedule_work
)
5417 schedule_work(&release_agent_work
);
5422 * Notify userspace when a cgroup is released, by running the
5423 * configured release agent with the name of the cgroup (path
5424 * relative to the root of cgroup file system) as the argument.
5426 * Most likely, this user command will try to rmdir this cgroup.
5428 * This races with the possibility that some other task will be
5429 * attached to this cgroup before it is removed, or that some other
5430 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5431 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5432 * unused, and this cgroup will be reprieved from its death sentence,
5433 * to continue to serve a useful existence. Next time it's released,
5434 * we will get notified again, if it still has 'notify_on_release' set.
5436 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5437 * means only wait until the task is successfully execve()'d. The
5438 * separate release agent task is forked by call_usermodehelper(),
5439 * then control in this thread returns here, without waiting for the
5440 * release agent task. We don't bother to wait because the caller of
5441 * this routine has no use for the exit status of the release agent
5442 * task, so no sense holding our caller up for that.
5444 static void cgroup_release_agent(struct work_struct
*work
)
5446 BUG_ON(work
!= &release_agent_work
);
5447 mutex_lock(&cgroup_mutex
);
5448 raw_spin_lock(&release_list_lock
);
5449 while (!list_empty(&release_list
)) {
5450 char *argv
[3], *envp
[3];
5452 char *pathbuf
= NULL
, *agentbuf
= NULL
;
5453 struct cgroup
*cgrp
= list_entry(release_list
.next
,
5456 list_del_init(&cgrp
->release_list
);
5457 raw_spin_unlock(&release_list_lock
);
5458 pathbuf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
5461 if (cgroup_path(cgrp
, pathbuf
, PAGE_SIZE
) < 0)
5463 agentbuf
= kstrdup(cgrp
->root
->release_agent_path
, GFP_KERNEL
);
5468 argv
[i
++] = agentbuf
;
5469 argv
[i
++] = pathbuf
;
5473 /* minimal command environment */
5474 envp
[i
++] = "HOME=/";
5475 envp
[i
++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5478 /* Drop the lock while we invoke the usermode helper,
5479 * since the exec could involve hitting disk and hence
5480 * be a slow process */
5481 mutex_unlock(&cgroup_mutex
);
5482 call_usermodehelper(argv
[0], argv
, envp
, UMH_WAIT_EXEC
);
5483 mutex_lock(&cgroup_mutex
);
5487 raw_spin_lock(&release_list_lock
);
5489 raw_spin_unlock(&release_list_lock
);
5490 mutex_unlock(&cgroup_mutex
);
5493 static int __init
cgroup_disable(char *str
)
5495 struct cgroup_subsys
*ss
;
5499 while ((token
= strsep(&str
, ",")) != NULL
) {
5504 * cgroup_disable, being at boot time, can't know about
5505 * module subsystems, so we don't worry about them.
5507 for_each_builtin_subsys(ss
, i
) {
5508 if (!strcmp(token
, ss
->name
)) {
5510 printk(KERN_INFO
"Disabling %s control group"
5511 " subsystem\n", ss
->name
);
5518 __setup("cgroup_disable=", cgroup_disable
);
5521 * Functons for CSS ID.
5524 /* to get ID other than 0, this should be called when !cgroup_is_dead() */
5525 unsigned short css_id(struct cgroup_subsys_state
*css
)
5527 struct css_id
*cssid
;
5530 * This css_id() can return correct value when somone has refcnt
5531 * on this or this is under rcu_read_lock(). Once css->id is allocated,
5532 * it's unchanged until freed.
5534 cssid
= rcu_dereference_raw(css
->id
);
5540 EXPORT_SYMBOL_GPL(css_id
);
5543 * css_is_ancestor - test "root" css is an ancestor of "child"
5544 * @child: the css to be tested.
5545 * @root: the css supporsed to be an ancestor of the child.
5547 * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
5548 * this function reads css->id, the caller must hold rcu_read_lock().
5549 * But, considering usual usage, the csses should be valid objects after test.
5550 * Assuming that the caller will do some action to the child if this returns
5551 * returns true, the caller must take "child";s reference count.
5552 * If "child" is valid object and this returns true, "root" is valid, too.
5555 bool css_is_ancestor(struct cgroup_subsys_state
*child
,
5556 const struct cgroup_subsys_state
*root
)
5558 struct css_id
*child_id
;
5559 struct css_id
*root_id
;
5561 child_id
= rcu_dereference(child
->id
);
5564 root_id
= rcu_dereference(root
->id
);
5567 if (child_id
->depth
< root_id
->depth
)
5569 if (child_id
->stack
[root_id
->depth
] != root_id
->id
)
5574 void free_css_id(struct cgroup_subsys
*ss
, struct cgroup_subsys_state
*css
)
5576 struct css_id
*id
= rcu_dereference_protected(css
->id
, true);
5578 /* When this is called before css_id initialization, id can be NULL */
5582 BUG_ON(!ss
->use_id
);
5584 rcu_assign_pointer(id
->css
, NULL
);
5585 rcu_assign_pointer(css
->id
, NULL
);
5586 spin_lock(&ss
->id_lock
);
5587 idr_remove(&ss
->idr
, id
->id
);
5588 spin_unlock(&ss
->id_lock
);
5589 kfree_rcu(id
, rcu_head
);
5591 EXPORT_SYMBOL_GPL(free_css_id
);
5594 * This is called by init or create(). Then, calls to this function are
5595 * always serialized (By cgroup_mutex() at create()).
5598 static struct css_id
*get_new_cssid(struct cgroup_subsys
*ss
, int depth
)
5600 struct css_id
*newid
;
5603 BUG_ON(!ss
->use_id
);
5605 size
= sizeof(*newid
) + sizeof(unsigned short) * (depth
+ 1);
5606 newid
= kzalloc(size
, GFP_KERNEL
);
5608 return ERR_PTR(-ENOMEM
);
5610 idr_preload(GFP_KERNEL
);
5611 spin_lock(&ss
->id_lock
);
5612 /* Don't use 0. allocates an ID of 1-65535 */
5613 ret
= idr_alloc(&ss
->idr
, newid
, 1, CSS_ID_MAX
+ 1, GFP_NOWAIT
);
5614 spin_unlock(&ss
->id_lock
);
5617 /* Returns error when there are no free spaces for new ID.*/
5622 newid
->depth
= depth
;
5626 return ERR_PTR(ret
);
5630 static int __init_or_module
cgroup_init_idr(struct cgroup_subsys
*ss
,
5631 struct cgroup_subsys_state
*rootcss
)
5633 struct css_id
*newid
;
5635 spin_lock_init(&ss
->id_lock
);
5638 newid
= get_new_cssid(ss
, 0);
5640 return PTR_ERR(newid
);
5642 newid
->stack
[0] = newid
->id
;
5643 RCU_INIT_POINTER(newid
->css
, rootcss
);
5644 RCU_INIT_POINTER(rootcss
->id
, newid
);
5648 static int alloc_css_id(struct cgroup_subsys_state
*child_css
)
5650 struct cgroup_subsys_state
*parent_css
= css_parent(child_css
);
5651 struct css_id
*child_id
, *parent_id
;
5654 parent_id
= rcu_dereference_protected(parent_css
->id
, true);
5655 depth
= parent_id
->depth
+ 1;
5657 child_id
= get_new_cssid(child_css
->ss
, depth
);
5658 if (IS_ERR(child_id
))
5659 return PTR_ERR(child_id
);
5661 for (i
= 0; i
< depth
; i
++)
5662 child_id
->stack
[i
] = parent_id
->stack
[i
];
5663 child_id
->stack
[depth
] = child_id
->id
;
5665 * child_id->css pointer will be set after this cgroup is available
5666 * see cgroup_populate_dir()
5668 rcu_assign_pointer(child_css
->id
, child_id
);
5674 * css_lookup - lookup css by id
5675 * @ss: cgroup subsys to be looked into.
5678 * Returns pointer to cgroup_subsys_state if there is valid one with id.
5679 * NULL if not. Should be called under rcu_read_lock()
5681 struct cgroup_subsys_state
*css_lookup(struct cgroup_subsys
*ss
, int id
)
5683 struct css_id
*cssid
= NULL
;
5685 BUG_ON(!ss
->use_id
);
5686 cssid
= idr_find(&ss
->idr
, id
);
5688 if (unlikely(!cssid
))
5691 return rcu_dereference(cssid
->css
);
5693 EXPORT_SYMBOL_GPL(css_lookup
);
5696 * css_from_dir - get corresponding css from the dentry of a cgroup dir
5697 * @dentry: directory dentry of interest
5698 * @ss: subsystem of interest
5700 * Must be called under RCU read lock. The caller is responsible for
5701 * pinning the returned css if it needs to be accessed outside the RCU
5704 struct cgroup_subsys_state
*css_from_dir(struct dentry
*dentry
,
5705 struct cgroup_subsys
*ss
)
5707 struct cgroup
*cgrp
;
5709 WARN_ON_ONCE(!rcu_read_lock_held());
5711 /* is @dentry a cgroup dir? */
5712 if (!dentry
->d_inode
||
5713 dentry
->d_inode
->i_op
!= &cgroup_dir_inode_operations
)
5714 return ERR_PTR(-EBADF
);
5716 cgrp
= __d_cgrp(dentry
);
5717 return cgroup_css(cgrp
, ss
) ?: ERR_PTR(-ENOENT
);
5721 * css_from_id - lookup css by id
5722 * @id: the cgroup id
5723 * @ss: cgroup subsys to be looked into
5725 * Returns the css if there's valid one with @id, otherwise returns NULL.
5726 * Should be called under rcu_read_lock().
5728 struct cgroup_subsys_state
*css_from_id(int id
, struct cgroup_subsys
*ss
)
5730 struct cgroup
*cgrp
;
5732 rcu_lockdep_assert(rcu_read_lock_held() ||
5733 lockdep_is_held(&cgroup_mutex
),
5734 "css_from_id() needs proper protection");
5736 cgrp
= idr_find(&ss
->root
->cgroup_idr
, id
);
5738 return cgroup_css(cgrp
, ss
);
5742 #ifdef CONFIG_CGROUP_DEBUG
5743 static struct cgroup_subsys_state
*
5744 debug_css_alloc(struct cgroup_subsys_state
*parent_css
)
5746 struct cgroup_subsys_state
*css
= kzalloc(sizeof(*css
), GFP_KERNEL
);
5749 return ERR_PTR(-ENOMEM
);
5754 static void debug_css_free(struct cgroup_subsys_state
*css
)
5759 static u64
debug_taskcount_read(struct cgroup_subsys_state
*css
,
5762 return cgroup_task_count(css
->cgroup
);
5765 static u64
current_css_set_read(struct cgroup_subsys_state
*css
,
5768 return (u64
)(unsigned long)current
->cgroups
;
5771 static u64
current_css_set_refcount_read(struct cgroup_subsys_state
*css
,
5777 count
= atomic_read(&task_css_set(current
)->refcount
);
5782 static int current_css_set_cg_links_read(struct cgroup_subsys_state
*css
,
5784 struct seq_file
*seq
)
5786 struct cgrp_cset_link
*link
;
5787 struct css_set
*cset
;
5789 read_lock(&css_set_lock
);
5791 cset
= rcu_dereference(current
->cgroups
);
5792 list_for_each_entry(link
, &cset
->cgrp_links
, cgrp_link
) {
5793 struct cgroup
*c
= link
->cgrp
;
5797 name
= c
->dentry
->d_name
.name
;
5800 seq_printf(seq
, "Root %d group %s\n",
5801 c
->root
->hierarchy_id
, name
);
5804 read_unlock(&css_set_lock
);
5808 #define MAX_TASKS_SHOWN_PER_CSS 25
5809 static int cgroup_css_links_read(struct cgroup_subsys_state
*css
,
5810 struct cftype
*cft
, struct seq_file
*seq
)
5812 struct cgrp_cset_link
*link
;
5814 read_lock(&css_set_lock
);
5815 list_for_each_entry(link
, &css
->cgroup
->cset_links
, cset_link
) {
5816 struct css_set
*cset
= link
->cset
;
5817 struct task_struct
*task
;
5819 seq_printf(seq
, "css_set %p\n", cset
);
5820 list_for_each_entry(task
, &cset
->tasks
, cg_list
) {
5821 if (count
++ > MAX_TASKS_SHOWN_PER_CSS
) {
5822 seq_puts(seq
, " ...\n");
5825 seq_printf(seq
, " task %d\n",
5826 task_pid_vnr(task
));
5830 read_unlock(&css_set_lock
);
5834 static u64
releasable_read(struct cgroup_subsys_state
*css
, struct cftype
*cft
)
5836 return test_bit(CGRP_RELEASABLE
, &css
->cgroup
->flags
);
5839 static struct cftype debug_files
[] = {
5841 .name
= "taskcount",
5842 .read_u64
= debug_taskcount_read
,
5846 .name
= "current_css_set",
5847 .read_u64
= current_css_set_read
,
5851 .name
= "current_css_set_refcount",
5852 .read_u64
= current_css_set_refcount_read
,
5856 .name
= "current_css_set_cg_links",
5857 .read_seq_string
= current_css_set_cg_links_read
,
5861 .name
= "cgroup_css_links",
5862 .read_seq_string
= cgroup_css_links_read
,
5866 .name
= "releasable",
5867 .read_u64
= releasable_read
,
5873 struct cgroup_subsys debug_subsys
= {
5875 .css_alloc
= debug_css_alloc
,
5876 .css_free
= debug_css_free
,
5877 .subsys_id
= debug_subsys_id
,
5878 .base_cftypes
= debug_files
,
5880 #endif /* CONFIG_CGROUP_DEBUG */