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>
63 #include <linux/file.h>
65 #include <linux/atomic.h>
68 * cgroup_mutex is the master lock. Any modification to cgroup or its
69 * hierarchy must be performed while holding it.
71 * cgroup_root_mutex nests inside cgroup_mutex and should be held to modify
72 * cgroupfs_root of any cgroup hierarchy - subsys list, flags,
73 * release_agent_path and so on. Modifying requires both cgroup_mutex and
74 * cgroup_root_mutex. Readers can acquire either of the two. This is to
75 * break the following locking order cycle.
77 * A. cgroup_mutex -> cred_guard_mutex -> s_type->i_mutex_key -> namespace_sem
78 * B. namespace_sem -> cgroup_mutex
80 * B happens only through cgroup_show_options() and using cgroup_root_mutex
83 #ifdef CONFIG_PROVE_RCU
84 DEFINE_MUTEX(cgroup_mutex
);
85 EXPORT_SYMBOL_GPL(cgroup_mutex
); /* only for lockdep */
87 static DEFINE_MUTEX(cgroup_mutex
);
90 static DEFINE_MUTEX(cgroup_root_mutex
);
93 * Generate an array of cgroup subsystem pointers. At boot time, this is
94 * populated with the built in subsystems, and modular subsystems are
95 * registered after that. The mutable section of this array is protected by
98 #define SUBSYS(_x) [_x ## _subsys_id] = &_x ## _subsys,
99 #define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option)
100 static struct cgroup_subsys
*cgroup_subsys
[CGROUP_SUBSYS_COUNT
] = {
101 #include <linux/cgroup_subsys.h>
105 * The dummy hierarchy, reserved for the subsystems that are otherwise
106 * unattached - it never has more than a single cgroup, and all tasks are
107 * part of that cgroup.
109 static struct cgroupfs_root cgroup_dummy_root
;
111 /* dummy_top is a shorthand for the dummy hierarchy's top cgroup */
112 static struct cgroup
* const cgroup_dummy_top
= &cgroup_dummy_root
.top_cgroup
;
115 * cgroupfs file entry, pointed to from leaf dentry->d_fsdata.
118 struct list_head node
;
119 struct dentry
*dentry
;
121 struct cgroup_subsys_state
*css
;
124 struct simple_xattrs xattrs
;
128 * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when
129 * cgroup_subsys->use_id != 0.
131 #define CSS_ID_MAX (65535)
134 * The css to which this ID points. This pointer is set to valid value
135 * after cgroup is populated. If cgroup is removed, this will be NULL.
136 * This pointer is expected to be RCU-safe because destroy()
137 * is called after synchronize_rcu(). But for safe use, css_tryget()
138 * should be used for avoiding race.
140 struct cgroup_subsys_state __rcu
*css
;
146 * Depth in hierarchy which this ID belongs to.
148 unsigned short depth
;
150 * ID is freed by RCU. (and lookup routine is RCU safe.)
152 struct rcu_head rcu_head
;
154 * Hierarchy of CSS ID belongs to.
156 unsigned short stack
[0]; /* Array of Length (depth+1) */
160 * cgroup_event represents events which userspace want to receive.
162 struct cgroup_event
{
164 * css which the event belongs to.
166 struct cgroup_subsys_state
*css
;
168 * Control file which the event associated.
172 * eventfd to signal userspace about the event.
174 struct eventfd_ctx
*eventfd
;
176 * Each of these stored in a list by the cgroup.
178 struct list_head list
;
180 * All fields below needed to unregister event when
181 * userspace closes eventfd.
184 wait_queue_head_t
*wqh
;
186 struct work_struct remove
;
189 /* The list of hierarchy roots */
191 static LIST_HEAD(cgroup_roots
);
192 static int cgroup_root_count
;
195 * Hierarchy ID allocation and mapping. It follows the same exclusion
196 * rules as other root ops - both cgroup_mutex and cgroup_root_mutex for
197 * writes, either for reads.
199 static DEFINE_IDR(cgroup_hierarchy_idr
);
201 static struct cgroup_name root_cgroup_name
= { .name
= "/" };
204 * Assign a monotonically increasing serial number to cgroups. It
205 * guarantees cgroups with bigger numbers are newer than those with smaller
206 * numbers. Also, as cgroups are always appended to the parent's
207 * ->children list, it guarantees that sibling cgroups are always sorted in
208 * the ascending serial number order on the list. Protected by
211 static u64 cgroup_serial_nr_next
= 1;
213 /* This flag indicates whether tasks in the fork and exit paths should
214 * check for fork/exit handlers to call. This avoids us having to do
215 * extra work in the fork/exit path if none of the subsystems need to
218 static int need_forkexit_callback __read_mostly
;
220 static struct cftype cgroup_base_files
[];
222 static void cgroup_destroy_css_killed(struct cgroup
*cgrp
);
223 static int cgroup_destroy_locked(struct cgroup
*cgrp
);
224 static int cgroup_addrm_files(struct cgroup
*cgrp
, struct cftype cfts
[],
228 * cgroup_css - obtain a cgroup's css for the specified subsystem
229 * @cgrp: the cgroup of interest
230 * @ss: the subsystem of interest (%NULL returns the dummy_css)
232 * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This
233 * function must be called either under cgroup_mutex or rcu_read_lock() and
234 * the caller is responsible for pinning the returned css if it wants to
235 * keep accessing it outside the said locks. This function may return
236 * %NULL if @cgrp doesn't have @subsys_id enabled.
238 static struct cgroup_subsys_state
*cgroup_css(struct cgroup
*cgrp
,
239 struct cgroup_subsys
*ss
)
242 return rcu_dereference_check(cgrp
->subsys
[ss
->subsys_id
],
243 lockdep_is_held(&cgroup_mutex
));
245 return &cgrp
->dummy_css
;
248 /* convenient tests for these bits */
249 static inline bool cgroup_is_dead(const struct cgroup
*cgrp
)
251 return test_bit(CGRP_DEAD
, &cgrp
->flags
);
255 * cgroup_is_descendant - test ancestry
256 * @cgrp: the cgroup to be tested
257 * @ancestor: possible ancestor of @cgrp
259 * Test whether @cgrp is a descendant of @ancestor. It also returns %true
260 * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
261 * and @ancestor are accessible.
263 bool cgroup_is_descendant(struct cgroup
*cgrp
, struct cgroup
*ancestor
)
266 if (cgrp
== ancestor
)
272 EXPORT_SYMBOL_GPL(cgroup_is_descendant
);
274 static int cgroup_is_releasable(const struct cgroup
*cgrp
)
277 (1 << CGRP_RELEASABLE
) |
278 (1 << CGRP_NOTIFY_ON_RELEASE
);
279 return (cgrp
->flags
& bits
) == bits
;
282 static int notify_on_release(const struct cgroup
*cgrp
)
284 return test_bit(CGRP_NOTIFY_ON_RELEASE
, &cgrp
->flags
);
288 * for_each_subsys - iterate all loaded cgroup subsystems
289 * @ss: the iteration cursor
290 * @i: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
292 * Should be called under cgroup_mutex.
294 #define for_each_subsys(ss, i) \
295 for ((i) = 0; (i) < CGROUP_SUBSYS_COUNT; (i)++) \
296 if (({ lockdep_assert_held(&cgroup_mutex); \
297 !((ss) = cgroup_subsys[i]); })) { } \
301 * for_each_builtin_subsys - iterate all built-in cgroup subsystems
302 * @ss: the iteration cursor
303 * @i: the index of @ss, CGROUP_BUILTIN_SUBSYS_COUNT after reaching the end
305 * Bulit-in subsystems are always present and iteration itself doesn't
306 * require any synchronization.
308 #define for_each_builtin_subsys(ss, i) \
309 for ((i) = 0; (i) < CGROUP_BUILTIN_SUBSYS_COUNT && \
310 (((ss) = cgroup_subsys[i]) || true); (i)++)
312 /* iterate each subsystem attached to a hierarchy */
313 #define for_each_root_subsys(root, ss) \
314 list_for_each_entry((ss), &(root)->subsys_list, sibling)
316 /* iterate across the active hierarchies */
317 #define for_each_active_root(root) \
318 list_for_each_entry((root), &cgroup_roots, root_list)
320 static inline struct cgroup
*__d_cgrp(struct dentry
*dentry
)
322 return dentry
->d_fsdata
;
325 static inline struct cfent
*__d_cfe(struct dentry
*dentry
)
327 return dentry
->d_fsdata
;
330 static inline struct cftype
*__d_cft(struct dentry
*dentry
)
332 return __d_cfe(dentry
)->type
;
336 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
337 * @cgrp: the cgroup to be checked for liveness
339 * On success, returns true; the mutex should be later unlocked. On
340 * failure returns false with no lock held.
342 static bool cgroup_lock_live_group(struct cgroup
*cgrp
)
344 mutex_lock(&cgroup_mutex
);
345 if (cgroup_is_dead(cgrp
)) {
346 mutex_unlock(&cgroup_mutex
);
352 /* the list of cgroups eligible for automatic release. Protected by
353 * release_list_lock */
354 static LIST_HEAD(release_list
);
355 static DEFINE_RAW_SPINLOCK(release_list_lock
);
356 static void cgroup_release_agent(struct work_struct
*work
);
357 static DECLARE_WORK(release_agent_work
, cgroup_release_agent
);
358 static void check_for_release(struct cgroup
*cgrp
);
361 * A cgroup can be associated with multiple css_sets as different tasks may
362 * belong to different cgroups on different hierarchies. In the other
363 * direction, a css_set is naturally associated with multiple cgroups.
364 * This M:N relationship is represented by the following link structure
365 * which exists for each association and allows traversing the associations
368 struct cgrp_cset_link
{
369 /* the cgroup and css_set this link associates */
371 struct css_set
*cset
;
373 /* list of cgrp_cset_links anchored at cgrp->cset_links */
374 struct list_head cset_link
;
376 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
377 struct list_head cgrp_link
;
380 /* The default css_set - used by init and its children prior to any
381 * hierarchies being mounted. It contains a pointer to the root state
382 * for each subsystem. Also used to anchor the list of css_sets. Not
383 * reference-counted, to improve performance when child cgroups
384 * haven't been created.
387 static struct css_set init_css_set
;
388 static struct cgrp_cset_link init_cgrp_cset_link
;
390 static int cgroup_init_idr(struct cgroup_subsys
*ss
,
391 struct cgroup_subsys_state
*css
);
394 * css_set_lock protects the list of css_set objects, and the chain of
395 * tasks off each css_set. Nests outside task->alloc_lock due to
396 * css_task_iter_start().
398 static DEFINE_RWLOCK(css_set_lock
);
399 static int css_set_count
;
402 * hash table for cgroup groups. This improves the performance to find
403 * an existing css_set. This hash doesn't (currently) take into
404 * account cgroups in empty hierarchies.
406 #define CSS_SET_HASH_BITS 7
407 static DEFINE_HASHTABLE(css_set_table
, CSS_SET_HASH_BITS
);
409 static unsigned long css_set_hash(struct cgroup_subsys_state
*css
[])
411 unsigned long key
= 0UL;
412 struct cgroup_subsys
*ss
;
415 for_each_subsys(ss
, i
)
416 key
+= (unsigned long)css
[i
];
417 key
= (key
>> 16) ^ key
;
423 * We don't maintain the lists running through each css_set to its task
424 * until after the first call to css_task_iter_start(). This reduces the
425 * fork()/exit() overhead for people who have cgroups compiled into their
426 * kernel but not actually in use.
428 static int use_task_css_set_links __read_mostly
;
430 static void __put_css_set(struct css_set
*cset
, int taskexit
)
432 struct cgrp_cset_link
*link
, *tmp_link
;
435 * Ensure that the refcount doesn't hit zero while any readers
436 * can see it. Similar to atomic_dec_and_lock(), but for an
439 if (atomic_add_unless(&cset
->refcount
, -1, 1))
441 write_lock(&css_set_lock
);
442 if (!atomic_dec_and_test(&cset
->refcount
)) {
443 write_unlock(&css_set_lock
);
447 /* This css_set is dead. unlink it and release cgroup refcounts */
448 hash_del(&cset
->hlist
);
451 list_for_each_entry_safe(link
, tmp_link
, &cset
->cgrp_links
, cgrp_link
) {
452 struct cgroup
*cgrp
= link
->cgrp
;
454 list_del(&link
->cset_link
);
455 list_del(&link
->cgrp_link
);
457 /* @cgrp can't go away while we're holding css_set_lock */
458 if (list_empty(&cgrp
->cset_links
) && notify_on_release(cgrp
)) {
460 set_bit(CGRP_RELEASABLE
, &cgrp
->flags
);
461 check_for_release(cgrp
);
467 write_unlock(&css_set_lock
);
468 kfree_rcu(cset
, rcu_head
);
472 * refcounted get/put for css_set objects
474 static inline void get_css_set(struct css_set
*cset
)
476 atomic_inc(&cset
->refcount
);
479 static inline void put_css_set(struct css_set
*cset
)
481 __put_css_set(cset
, 0);
484 static inline void put_css_set_taskexit(struct css_set
*cset
)
486 __put_css_set(cset
, 1);
490 * compare_css_sets - helper function for find_existing_css_set().
491 * @cset: candidate css_set being tested
492 * @old_cset: existing css_set for a task
493 * @new_cgrp: cgroup that's being entered by the task
494 * @template: desired set of css pointers in css_set (pre-calculated)
496 * Returns true if "cset" matches "old_cset" except for the hierarchy
497 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
499 static bool compare_css_sets(struct css_set
*cset
,
500 struct css_set
*old_cset
,
501 struct cgroup
*new_cgrp
,
502 struct cgroup_subsys_state
*template[])
504 struct list_head
*l1
, *l2
;
506 if (memcmp(template, cset
->subsys
, sizeof(cset
->subsys
))) {
507 /* Not all subsystems matched */
512 * Compare cgroup pointers in order to distinguish between
513 * different cgroups in heirarchies with no subsystems. We
514 * could get by with just this check alone (and skip the
515 * memcmp above) but on most setups the memcmp check will
516 * avoid the need for this more expensive check on almost all
520 l1
= &cset
->cgrp_links
;
521 l2
= &old_cset
->cgrp_links
;
523 struct cgrp_cset_link
*link1
, *link2
;
524 struct cgroup
*cgrp1
, *cgrp2
;
528 /* See if we reached the end - both lists are equal length. */
529 if (l1
== &cset
->cgrp_links
) {
530 BUG_ON(l2
!= &old_cset
->cgrp_links
);
533 BUG_ON(l2
== &old_cset
->cgrp_links
);
535 /* Locate the cgroups associated with these links. */
536 link1
= list_entry(l1
, struct cgrp_cset_link
, cgrp_link
);
537 link2
= list_entry(l2
, struct cgrp_cset_link
, cgrp_link
);
540 /* Hierarchies should be linked in the same order. */
541 BUG_ON(cgrp1
->root
!= cgrp2
->root
);
544 * If this hierarchy is the hierarchy of the cgroup
545 * that's changing, then we need to check that this
546 * css_set points to the new cgroup; if it's any other
547 * hierarchy, then this css_set should point to the
548 * same cgroup as the old css_set.
550 if (cgrp1
->root
== new_cgrp
->root
) {
551 if (cgrp1
!= new_cgrp
)
562 * find_existing_css_set - init css array and find the matching css_set
563 * @old_cset: the css_set that we're using before the cgroup transition
564 * @cgrp: the cgroup that we're moving into
565 * @template: out param for the new set of csses, should be clear on entry
567 static struct css_set
*find_existing_css_set(struct css_set
*old_cset
,
569 struct cgroup_subsys_state
*template[])
571 struct cgroupfs_root
*root
= cgrp
->root
;
572 struct cgroup_subsys
*ss
;
573 struct css_set
*cset
;
578 * Build the set of subsystem state objects that we want to see in the
579 * new css_set. while subsystems can change globally, the entries here
580 * won't change, so no need for locking.
582 for_each_subsys(ss
, i
) {
583 if (root
->subsys_mask
& (1UL << i
)) {
584 /* Subsystem is in this hierarchy. So we want
585 * the subsystem state from the new
587 template[i
] = cgroup_css(cgrp
, ss
);
589 /* Subsystem is not in this hierarchy, so we
590 * don't want to change the subsystem state */
591 template[i
] = old_cset
->subsys
[i
];
595 key
= css_set_hash(template);
596 hash_for_each_possible(css_set_table
, cset
, hlist
, key
) {
597 if (!compare_css_sets(cset
, old_cset
, cgrp
, template))
600 /* This css_set matches what we need */
604 /* No existing cgroup group matched */
608 static void free_cgrp_cset_links(struct list_head
*links_to_free
)
610 struct cgrp_cset_link
*link
, *tmp_link
;
612 list_for_each_entry_safe(link
, tmp_link
, links_to_free
, cset_link
) {
613 list_del(&link
->cset_link
);
619 * allocate_cgrp_cset_links - allocate cgrp_cset_links
620 * @count: the number of links to allocate
621 * @tmp_links: list_head the allocated links are put on
623 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
624 * through ->cset_link. Returns 0 on success or -errno.
626 static int allocate_cgrp_cset_links(int count
, struct list_head
*tmp_links
)
628 struct cgrp_cset_link
*link
;
631 INIT_LIST_HEAD(tmp_links
);
633 for (i
= 0; i
< count
; i
++) {
634 link
= kzalloc(sizeof(*link
), GFP_KERNEL
);
636 free_cgrp_cset_links(tmp_links
);
639 list_add(&link
->cset_link
, tmp_links
);
645 * link_css_set - a helper function to link a css_set to a cgroup
646 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
647 * @cset: the css_set to be linked
648 * @cgrp: the destination cgroup
650 static void link_css_set(struct list_head
*tmp_links
, struct css_set
*cset
,
653 struct cgrp_cset_link
*link
;
655 BUG_ON(list_empty(tmp_links
));
656 link
= list_first_entry(tmp_links
, struct cgrp_cset_link
, cset_link
);
659 list_move(&link
->cset_link
, &cgrp
->cset_links
);
661 * Always add links to the tail of the list so that the list
662 * is sorted by order of hierarchy creation
664 list_add_tail(&link
->cgrp_link
, &cset
->cgrp_links
);
668 * find_css_set - return a new css_set with one cgroup updated
669 * @old_cset: the baseline css_set
670 * @cgrp: the cgroup to be updated
672 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
673 * substituted into the appropriate hierarchy.
675 static struct css_set
*find_css_set(struct css_set
*old_cset
,
678 struct cgroup_subsys_state
*template[CGROUP_SUBSYS_COUNT
] = { };
679 struct css_set
*cset
;
680 struct list_head tmp_links
;
681 struct cgrp_cset_link
*link
;
684 lockdep_assert_held(&cgroup_mutex
);
686 /* First see if we already have a cgroup group that matches
688 read_lock(&css_set_lock
);
689 cset
= find_existing_css_set(old_cset
, cgrp
, template);
692 read_unlock(&css_set_lock
);
697 cset
= kzalloc(sizeof(*cset
), GFP_KERNEL
);
701 /* Allocate all the cgrp_cset_link objects that we'll need */
702 if (allocate_cgrp_cset_links(cgroup_root_count
, &tmp_links
) < 0) {
707 atomic_set(&cset
->refcount
, 1);
708 INIT_LIST_HEAD(&cset
->cgrp_links
);
709 INIT_LIST_HEAD(&cset
->tasks
);
710 INIT_HLIST_NODE(&cset
->hlist
);
712 /* Copy the set of subsystem state objects generated in
713 * find_existing_css_set() */
714 memcpy(cset
->subsys
, template, sizeof(cset
->subsys
));
716 write_lock(&css_set_lock
);
717 /* Add reference counts and links from the new css_set. */
718 list_for_each_entry(link
, &old_cset
->cgrp_links
, cgrp_link
) {
719 struct cgroup
*c
= link
->cgrp
;
721 if (c
->root
== cgrp
->root
)
723 link_css_set(&tmp_links
, cset
, c
);
726 BUG_ON(!list_empty(&tmp_links
));
730 /* Add this cgroup group to the hash table */
731 key
= css_set_hash(cset
->subsys
);
732 hash_add(css_set_table
, &cset
->hlist
, key
);
734 write_unlock(&css_set_lock
);
740 * Return the cgroup for "task" from the given hierarchy. Must be
741 * called with cgroup_mutex held.
743 static struct cgroup
*task_cgroup_from_root(struct task_struct
*task
,
744 struct cgroupfs_root
*root
)
746 struct css_set
*cset
;
747 struct cgroup
*res
= NULL
;
749 BUG_ON(!mutex_is_locked(&cgroup_mutex
));
750 read_lock(&css_set_lock
);
752 * No need to lock the task - since we hold cgroup_mutex the
753 * task can't change groups, so the only thing that can happen
754 * is that it exits and its css is set back to init_css_set.
756 cset
= task_css_set(task
);
757 if (cset
== &init_css_set
) {
758 res
= &root
->top_cgroup
;
760 struct cgrp_cset_link
*link
;
762 list_for_each_entry(link
, &cset
->cgrp_links
, cgrp_link
) {
763 struct cgroup
*c
= link
->cgrp
;
765 if (c
->root
== root
) {
771 read_unlock(&css_set_lock
);
777 * There is one global cgroup mutex. We also require taking
778 * task_lock() when dereferencing a task's cgroup subsys pointers.
779 * See "The task_lock() exception", at the end of this comment.
781 * A task must hold cgroup_mutex to modify cgroups.
783 * Any task can increment and decrement the count field without lock.
784 * So in general, code holding cgroup_mutex can't rely on the count
785 * field not changing. However, if the count goes to zero, then only
786 * cgroup_attach_task() can increment it again. Because a count of zero
787 * means that no tasks are currently attached, therefore there is no
788 * way a task attached to that cgroup can fork (the other way to
789 * increment the count). So code holding cgroup_mutex can safely
790 * assume that if the count is zero, it will stay zero. Similarly, if
791 * a task holds cgroup_mutex on a cgroup with zero count, it
792 * knows that the cgroup won't be removed, as cgroup_rmdir()
795 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
796 * (usually) take cgroup_mutex. These are the two most performance
797 * critical pieces of code here. The exception occurs on cgroup_exit(),
798 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
799 * is taken, and if the cgroup count is zero, a usermode call made
800 * to the release agent with the name of the cgroup (path relative to
801 * the root of cgroup file system) as the argument.
803 * A cgroup can only be deleted if both its 'count' of using tasks
804 * is zero, and its list of 'children' cgroups is empty. Since all
805 * tasks in the system use _some_ cgroup, and since there is always at
806 * least one task in the system (init, pid == 1), therefore, top_cgroup
807 * always has either children cgroups and/or using tasks. So we don't
808 * need a special hack to ensure that top_cgroup cannot be deleted.
810 * The task_lock() exception
812 * The need for this exception arises from the action of
813 * cgroup_attach_task(), which overwrites one task's cgroup pointer with
814 * another. It does so using cgroup_mutex, however there are
815 * several performance critical places that need to reference
816 * task->cgroup without the expense of grabbing a system global
817 * mutex. Therefore except as noted below, when dereferencing or, as
818 * in cgroup_attach_task(), modifying a task's cgroup pointer we use
819 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
820 * the task_struct routinely used for such matters.
822 * P.S. One more locking exception. RCU is used to guard the
823 * update of a tasks cgroup pointer by cgroup_attach_task()
827 * A couple of forward declarations required, due to cyclic reference loop:
828 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
829 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
833 static int cgroup_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
);
834 static int cgroup_rmdir(struct inode
*unused_dir
, struct dentry
*dentry
);
835 static int cgroup_populate_dir(struct cgroup
*cgrp
, unsigned long subsys_mask
);
836 static const struct inode_operations cgroup_dir_inode_operations
;
837 static const struct file_operations proc_cgroupstats_operations
;
839 static struct backing_dev_info cgroup_backing_dev_info
= {
841 .capabilities
= BDI_CAP_NO_ACCT_AND_WRITEBACK
,
844 static int alloc_css_id(struct cgroup_subsys_state
*child_css
);
846 static struct inode
*cgroup_new_inode(umode_t mode
, struct super_block
*sb
)
848 struct inode
*inode
= new_inode(sb
);
851 inode
->i_ino
= get_next_ino();
852 inode
->i_mode
= mode
;
853 inode
->i_uid
= current_fsuid();
854 inode
->i_gid
= current_fsgid();
855 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
856 inode
->i_mapping
->backing_dev_info
= &cgroup_backing_dev_info
;
861 static struct cgroup_name
*cgroup_alloc_name(struct dentry
*dentry
)
863 struct cgroup_name
*name
;
865 name
= kmalloc(sizeof(*name
) + dentry
->d_name
.len
+ 1, GFP_KERNEL
);
868 strcpy(name
->name
, dentry
->d_name
.name
);
872 static void cgroup_free_fn(struct work_struct
*work
)
874 struct cgroup
*cgrp
= container_of(work
, struct cgroup
, destroy_work
);
876 mutex_lock(&cgroup_mutex
);
877 cgrp
->root
->number_of_cgroups
--;
878 mutex_unlock(&cgroup_mutex
);
881 * We get a ref to the parent's dentry, and put the ref when
882 * this cgroup is being freed, so it's guaranteed that the
883 * parent won't be destroyed before its children.
885 dput(cgrp
->parent
->dentry
);
888 * Drop the active superblock reference that we took when we
889 * created the cgroup. This will free cgrp->root, if we are
890 * holding the last reference to @sb.
892 deactivate_super(cgrp
->root
->sb
);
895 * if we're getting rid of the cgroup, refcount should ensure
896 * that there are no pidlists left.
898 BUG_ON(!list_empty(&cgrp
->pidlists
));
900 simple_xattrs_free(&cgrp
->xattrs
);
902 kfree(rcu_dereference_raw(cgrp
->name
));
906 static void cgroup_free_rcu(struct rcu_head
*head
)
908 struct cgroup
*cgrp
= container_of(head
, struct cgroup
, rcu_head
);
910 INIT_WORK(&cgrp
->destroy_work
, cgroup_free_fn
);
911 schedule_work(&cgrp
->destroy_work
);
914 static void cgroup_diput(struct dentry
*dentry
, struct inode
*inode
)
916 /* is dentry a directory ? if so, kfree() associated cgroup */
917 if (S_ISDIR(inode
->i_mode
)) {
918 struct cgroup
*cgrp
= dentry
->d_fsdata
;
920 BUG_ON(!(cgroup_is_dead(cgrp
)));
921 call_rcu(&cgrp
->rcu_head
, cgroup_free_rcu
);
923 struct cfent
*cfe
= __d_cfe(dentry
);
924 struct cgroup
*cgrp
= dentry
->d_parent
->d_fsdata
;
926 WARN_ONCE(!list_empty(&cfe
->node
) &&
927 cgrp
!= &cgrp
->root
->top_cgroup
,
928 "cfe still linked for %s\n", cfe
->type
->name
);
929 simple_xattrs_free(&cfe
->xattrs
);
935 static int cgroup_delete(const struct dentry
*d
)
940 static void remove_dir(struct dentry
*d
)
942 struct dentry
*parent
= dget(d
->d_parent
);
945 simple_rmdir(parent
->d_inode
, d
);
949 static void cgroup_rm_file(struct cgroup
*cgrp
, const struct cftype
*cft
)
953 lockdep_assert_held(&cgrp
->dentry
->d_inode
->i_mutex
);
954 lockdep_assert_held(&cgroup_mutex
);
957 * If we're doing cleanup due to failure of cgroup_create(),
958 * the corresponding @cfe may not exist.
960 list_for_each_entry(cfe
, &cgrp
->files
, node
) {
961 struct dentry
*d
= cfe
->dentry
;
963 if (cft
&& cfe
->type
!= cft
)
968 simple_unlink(cgrp
->dentry
->d_inode
, d
);
969 list_del_init(&cfe
->node
);
977 * cgroup_clear_dir - remove subsys files in a cgroup directory
978 * @cgrp: target cgroup
979 * @subsys_mask: mask of the subsystem ids whose files should be removed
981 static void cgroup_clear_dir(struct cgroup
*cgrp
, unsigned long subsys_mask
)
983 struct cgroup_subsys
*ss
;
986 for_each_subsys(ss
, i
) {
987 struct cftype_set
*set
;
989 if (!test_bit(i
, &subsys_mask
))
991 list_for_each_entry(set
, &ss
->cftsets
, node
)
992 cgroup_addrm_files(cgrp
, set
->cfts
, false);
997 * NOTE : the dentry must have been dget()'ed
999 static void cgroup_d_remove_dir(struct dentry
*dentry
)
1001 struct dentry
*parent
;
1003 parent
= dentry
->d_parent
;
1004 spin_lock(&parent
->d_lock
);
1005 spin_lock_nested(&dentry
->d_lock
, DENTRY_D_LOCK_NESTED
);
1006 list_del_init(&dentry
->d_u
.d_child
);
1007 spin_unlock(&dentry
->d_lock
);
1008 spin_unlock(&parent
->d_lock
);
1013 * Call with cgroup_mutex held. Drops reference counts on modules, including
1014 * any duplicate ones that parse_cgroupfs_options took. If this function
1015 * returns an error, no reference counts are touched.
1017 static int rebind_subsystems(struct cgroupfs_root
*root
,
1018 unsigned long added_mask
, unsigned removed_mask
)
1020 struct cgroup
*cgrp
= &root
->top_cgroup
;
1021 struct cgroup_subsys
*ss
;
1022 unsigned long pinned
= 0;
1025 BUG_ON(!mutex_is_locked(&cgroup_mutex
));
1026 BUG_ON(!mutex_is_locked(&cgroup_root_mutex
));
1028 /* Check that any added subsystems are currently free */
1029 for_each_subsys(ss
, i
) {
1030 if (!(added_mask
& (1 << i
)))
1033 /* is the subsystem mounted elsewhere? */
1034 if (ss
->root
!= &cgroup_dummy_root
) {
1039 /* pin the module */
1040 if (!try_module_get(ss
->module
)) {
1047 /* subsys could be missing if unloaded between parsing and here */
1048 if (added_mask
!= pinned
) {
1053 ret
= cgroup_populate_dir(cgrp
, added_mask
);
1058 * Nothing can fail from this point on. Remove files for the
1059 * removed subsystems and rebind each subsystem.
1061 cgroup_clear_dir(cgrp
, removed_mask
);
1063 for_each_subsys(ss
, i
) {
1064 unsigned long bit
= 1UL << i
;
1066 if (bit
& added_mask
) {
1067 /* We're binding this subsystem to this hierarchy */
1068 BUG_ON(cgroup_css(cgrp
, ss
));
1069 BUG_ON(!cgroup_css(cgroup_dummy_top
, ss
));
1070 BUG_ON(cgroup_css(cgroup_dummy_top
, ss
)->cgroup
!= cgroup_dummy_top
);
1072 rcu_assign_pointer(cgrp
->subsys
[i
],
1073 cgroup_css(cgroup_dummy_top
, ss
));
1074 cgroup_css(cgrp
, ss
)->cgroup
= cgrp
;
1076 list_move(&ss
->sibling
, &root
->subsys_list
);
1079 ss
->bind(cgroup_css(cgrp
, ss
));
1081 /* refcount was already taken, and we're keeping it */
1082 root
->subsys_mask
|= bit
;
1083 } else if (bit
& removed_mask
) {
1084 /* We're removing this subsystem */
1085 BUG_ON(cgroup_css(cgrp
, ss
) != cgroup_css(cgroup_dummy_top
, ss
));
1086 BUG_ON(cgroup_css(cgrp
, ss
)->cgroup
!= cgrp
);
1089 ss
->bind(cgroup_css(cgroup_dummy_top
, ss
));
1091 cgroup_css(cgroup_dummy_top
, ss
)->cgroup
= cgroup_dummy_top
;
1092 RCU_INIT_POINTER(cgrp
->subsys
[i
], NULL
);
1094 cgroup_subsys
[i
]->root
= &cgroup_dummy_root
;
1095 list_move(&ss
->sibling
, &cgroup_dummy_root
.subsys_list
);
1097 /* subsystem is now free - drop reference on module */
1098 module_put(ss
->module
);
1099 root
->subsys_mask
&= ~bit
;
1104 * Mark @root has finished binding subsystems. @root->subsys_mask
1105 * now matches the bound subsystems.
1107 root
->flags
|= CGRP_ROOT_SUBSYS_BOUND
;
1112 for_each_subsys(ss
, i
)
1113 if (pinned
& (1 << i
))
1114 module_put(ss
->module
);
1118 static int cgroup_show_options(struct seq_file
*seq
, struct dentry
*dentry
)
1120 struct cgroupfs_root
*root
= dentry
->d_sb
->s_fs_info
;
1121 struct cgroup_subsys
*ss
;
1123 mutex_lock(&cgroup_root_mutex
);
1124 for_each_root_subsys(root
, ss
)
1125 seq_printf(seq
, ",%s", ss
->name
);
1126 if (root
->flags
& CGRP_ROOT_SANE_BEHAVIOR
)
1127 seq_puts(seq
, ",sane_behavior");
1128 if (root
->flags
& CGRP_ROOT_NOPREFIX
)
1129 seq_puts(seq
, ",noprefix");
1130 if (root
->flags
& CGRP_ROOT_XATTR
)
1131 seq_puts(seq
, ",xattr");
1132 if (strlen(root
->release_agent_path
))
1133 seq_printf(seq
, ",release_agent=%s", root
->release_agent_path
);
1134 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN
, &root
->top_cgroup
.flags
))
1135 seq_puts(seq
, ",clone_children");
1136 if (strlen(root
->name
))
1137 seq_printf(seq
, ",name=%s", root
->name
);
1138 mutex_unlock(&cgroup_root_mutex
);
1142 struct cgroup_sb_opts
{
1143 unsigned long subsys_mask
;
1144 unsigned long flags
;
1145 char *release_agent
;
1146 bool cpuset_clone_children
;
1148 /* User explicitly requested empty subsystem */
1151 struct cgroupfs_root
*new_root
;
1156 * Convert a hierarchy specifier into a bitmask of subsystems and
1157 * flags. Call with cgroup_mutex held to protect the cgroup_subsys[]
1158 * array. This function takes refcounts on subsystems to be used, unless it
1159 * returns error, in which case no refcounts are taken.
1161 static int parse_cgroupfs_options(char *data
, struct cgroup_sb_opts
*opts
)
1163 char *token
, *o
= data
;
1164 bool all_ss
= false, one_ss
= false;
1165 unsigned long mask
= (unsigned long)-1;
1166 struct cgroup_subsys
*ss
;
1169 BUG_ON(!mutex_is_locked(&cgroup_mutex
));
1171 #ifdef CONFIG_CPUSETS
1172 mask
= ~(1UL << cpuset_subsys_id
);
1175 memset(opts
, 0, sizeof(*opts
));
1177 while ((token
= strsep(&o
, ",")) != NULL
) {
1180 if (!strcmp(token
, "none")) {
1181 /* Explicitly have no subsystems */
1185 if (!strcmp(token
, "all")) {
1186 /* Mutually exclusive option 'all' + subsystem name */
1192 if (!strcmp(token
, "__DEVEL__sane_behavior")) {
1193 opts
->flags
|= CGRP_ROOT_SANE_BEHAVIOR
;
1196 if (!strcmp(token
, "noprefix")) {
1197 opts
->flags
|= CGRP_ROOT_NOPREFIX
;
1200 if (!strcmp(token
, "clone_children")) {
1201 opts
->cpuset_clone_children
= true;
1204 if (!strcmp(token
, "xattr")) {
1205 opts
->flags
|= CGRP_ROOT_XATTR
;
1208 if (!strncmp(token
, "release_agent=", 14)) {
1209 /* Specifying two release agents is forbidden */
1210 if (opts
->release_agent
)
1212 opts
->release_agent
=
1213 kstrndup(token
+ 14, PATH_MAX
- 1, GFP_KERNEL
);
1214 if (!opts
->release_agent
)
1218 if (!strncmp(token
, "name=", 5)) {
1219 const char *name
= token
+ 5;
1220 /* Can't specify an empty name */
1223 /* Must match [\w.-]+ */
1224 for (i
= 0; i
< strlen(name
); i
++) {
1228 if ((c
== '.') || (c
== '-') || (c
== '_'))
1232 /* Specifying two names is forbidden */
1235 opts
->name
= kstrndup(name
,
1236 MAX_CGROUP_ROOT_NAMELEN
- 1,
1244 for_each_subsys(ss
, i
) {
1245 if (strcmp(token
, ss
->name
))
1250 /* Mutually exclusive option 'all' + subsystem name */
1253 set_bit(i
, &opts
->subsys_mask
);
1258 if (i
== CGROUP_SUBSYS_COUNT
)
1263 * If the 'all' option was specified select all the subsystems,
1264 * otherwise if 'none', 'name=' and a subsystem name options
1265 * were not specified, let's default to 'all'
1267 if (all_ss
|| (!one_ss
&& !opts
->none
&& !opts
->name
))
1268 for_each_subsys(ss
, i
)
1270 set_bit(i
, &opts
->subsys_mask
);
1272 /* Consistency checks */
1274 if (opts
->flags
& CGRP_ROOT_SANE_BEHAVIOR
) {
1275 pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
1277 if (opts
->flags
& CGRP_ROOT_NOPREFIX
) {
1278 pr_err("cgroup: sane_behavior: noprefix is not allowed\n");
1282 if (opts
->cpuset_clone_children
) {
1283 pr_err("cgroup: sane_behavior: clone_children is not allowed\n");
1289 * Option noprefix was introduced just for backward compatibility
1290 * with the old cpuset, so we allow noprefix only if mounting just
1291 * the cpuset subsystem.
1293 if ((opts
->flags
& CGRP_ROOT_NOPREFIX
) && (opts
->subsys_mask
& mask
))
1297 /* Can't specify "none" and some subsystems */
1298 if (opts
->subsys_mask
&& opts
->none
)
1302 * We either have to specify by name or by subsystems. (So all
1303 * empty hierarchies must have a name).
1305 if (!opts
->subsys_mask
&& !opts
->name
)
1311 static int cgroup_remount(struct super_block
*sb
, int *flags
, char *data
)
1314 struct cgroupfs_root
*root
= sb
->s_fs_info
;
1315 struct cgroup
*cgrp
= &root
->top_cgroup
;
1316 struct cgroup_sb_opts opts
;
1317 unsigned long added_mask
, removed_mask
;
1319 if (root
->flags
& CGRP_ROOT_SANE_BEHAVIOR
) {
1320 pr_err("cgroup: sane_behavior: remount is not allowed\n");
1324 mutex_lock(&cgrp
->dentry
->d_inode
->i_mutex
);
1325 mutex_lock(&cgroup_mutex
);
1326 mutex_lock(&cgroup_root_mutex
);
1328 /* See what subsystems are wanted */
1329 ret
= parse_cgroupfs_options(data
, &opts
);
1333 if (opts
.subsys_mask
!= root
->subsys_mask
|| opts
.release_agent
)
1334 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1335 task_tgid_nr(current
), current
->comm
);
1337 added_mask
= opts
.subsys_mask
& ~root
->subsys_mask
;
1338 removed_mask
= root
->subsys_mask
& ~opts
.subsys_mask
;
1340 /* Don't allow flags or name to change at remount */
1341 if (((opts
.flags
^ root
->flags
) & CGRP_ROOT_OPTION_MASK
) ||
1342 (opts
.name
&& strcmp(opts
.name
, root
->name
))) {
1343 pr_err("cgroup: option or name mismatch, new: 0x%lx \"%s\", old: 0x%lx \"%s\"\n",
1344 opts
.flags
& CGRP_ROOT_OPTION_MASK
, opts
.name
?: "",
1345 root
->flags
& CGRP_ROOT_OPTION_MASK
, root
->name
);
1350 /* remounting is not allowed for populated hierarchies */
1351 if (root
->number_of_cgroups
> 1) {
1356 ret
= rebind_subsystems(root
, added_mask
, removed_mask
);
1360 if (opts
.release_agent
)
1361 strcpy(root
->release_agent_path
, opts
.release_agent
);
1363 kfree(opts
.release_agent
);
1365 mutex_unlock(&cgroup_root_mutex
);
1366 mutex_unlock(&cgroup_mutex
);
1367 mutex_unlock(&cgrp
->dentry
->d_inode
->i_mutex
);
1371 static const struct super_operations cgroup_ops
= {
1372 .statfs
= simple_statfs
,
1373 .drop_inode
= generic_delete_inode
,
1374 .show_options
= cgroup_show_options
,
1375 .remount_fs
= cgroup_remount
,
1378 static void init_cgroup_housekeeping(struct cgroup
*cgrp
)
1380 INIT_LIST_HEAD(&cgrp
->sibling
);
1381 INIT_LIST_HEAD(&cgrp
->children
);
1382 INIT_LIST_HEAD(&cgrp
->files
);
1383 INIT_LIST_HEAD(&cgrp
->cset_links
);
1384 INIT_LIST_HEAD(&cgrp
->release_list
);
1385 INIT_LIST_HEAD(&cgrp
->pidlists
);
1386 mutex_init(&cgrp
->pidlist_mutex
);
1387 cgrp
->dummy_css
.cgroup
= cgrp
;
1388 INIT_LIST_HEAD(&cgrp
->event_list
);
1389 spin_lock_init(&cgrp
->event_list_lock
);
1390 simple_xattrs_init(&cgrp
->xattrs
);
1393 static void init_cgroup_root(struct cgroupfs_root
*root
)
1395 struct cgroup
*cgrp
= &root
->top_cgroup
;
1397 INIT_LIST_HEAD(&root
->subsys_list
);
1398 INIT_LIST_HEAD(&root
->root_list
);
1399 root
->number_of_cgroups
= 1;
1401 RCU_INIT_POINTER(cgrp
->name
, &root_cgroup_name
);
1402 init_cgroup_housekeeping(cgrp
);
1403 idr_init(&root
->cgroup_idr
);
1406 static int cgroup_init_root_id(struct cgroupfs_root
*root
, int start
, int end
)
1410 lockdep_assert_held(&cgroup_mutex
);
1411 lockdep_assert_held(&cgroup_root_mutex
);
1413 id
= idr_alloc_cyclic(&cgroup_hierarchy_idr
, root
, start
, end
,
1418 root
->hierarchy_id
= id
;
1422 static void cgroup_exit_root_id(struct cgroupfs_root
*root
)
1424 lockdep_assert_held(&cgroup_mutex
);
1425 lockdep_assert_held(&cgroup_root_mutex
);
1427 if (root
->hierarchy_id
) {
1428 idr_remove(&cgroup_hierarchy_idr
, root
->hierarchy_id
);
1429 root
->hierarchy_id
= 0;
1433 static int cgroup_test_super(struct super_block
*sb
, void *data
)
1435 struct cgroup_sb_opts
*opts
= data
;
1436 struct cgroupfs_root
*root
= sb
->s_fs_info
;
1438 /* If we asked for a name then it must match */
1439 if (opts
->name
&& strcmp(opts
->name
, root
->name
))
1443 * If we asked for subsystems (or explicitly for no
1444 * subsystems) then they must match
1446 if ((opts
->subsys_mask
|| opts
->none
)
1447 && (opts
->subsys_mask
!= root
->subsys_mask
))
1453 static struct cgroupfs_root
*cgroup_root_from_opts(struct cgroup_sb_opts
*opts
)
1455 struct cgroupfs_root
*root
;
1457 if (!opts
->subsys_mask
&& !opts
->none
)
1460 root
= kzalloc(sizeof(*root
), GFP_KERNEL
);
1462 return ERR_PTR(-ENOMEM
);
1464 init_cgroup_root(root
);
1467 * We need to set @root->subsys_mask now so that @root can be
1468 * matched by cgroup_test_super() before it finishes
1469 * initialization; otherwise, competing mounts with the same
1470 * options may try to bind the same subsystems instead of waiting
1471 * for the first one leading to unexpected mount errors.
1472 * SUBSYS_BOUND will be set once actual binding is complete.
1474 root
->subsys_mask
= opts
->subsys_mask
;
1475 root
->flags
= opts
->flags
;
1476 if (opts
->release_agent
)
1477 strcpy(root
->release_agent_path
, opts
->release_agent
);
1479 strcpy(root
->name
, opts
->name
);
1480 if (opts
->cpuset_clone_children
)
1481 set_bit(CGRP_CPUSET_CLONE_CHILDREN
, &root
->top_cgroup
.flags
);
1485 static void cgroup_free_root(struct cgroupfs_root
*root
)
1488 /* hierarhcy ID shoulid already have been released */
1489 WARN_ON_ONCE(root
->hierarchy_id
);
1491 idr_destroy(&root
->cgroup_idr
);
1496 static int cgroup_set_super(struct super_block
*sb
, void *data
)
1499 struct cgroup_sb_opts
*opts
= data
;
1501 /* If we don't have a new root, we can't set up a new sb */
1502 if (!opts
->new_root
)
1505 BUG_ON(!opts
->subsys_mask
&& !opts
->none
);
1507 ret
= set_anon_super(sb
, NULL
);
1511 sb
->s_fs_info
= opts
->new_root
;
1512 opts
->new_root
->sb
= sb
;
1514 sb
->s_blocksize
= PAGE_CACHE_SIZE
;
1515 sb
->s_blocksize_bits
= PAGE_CACHE_SHIFT
;
1516 sb
->s_magic
= CGROUP_SUPER_MAGIC
;
1517 sb
->s_op
= &cgroup_ops
;
1522 static int cgroup_get_rootdir(struct super_block
*sb
)
1524 static const struct dentry_operations cgroup_dops
= {
1525 .d_iput
= cgroup_diput
,
1526 .d_delete
= cgroup_delete
,
1529 struct inode
*inode
=
1530 cgroup_new_inode(S_IFDIR
| S_IRUGO
| S_IXUGO
| S_IWUSR
, sb
);
1535 inode
->i_fop
= &simple_dir_operations
;
1536 inode
->i_op
= &cgroup_dir_inode_operations
;
1537 /* directories start off with i_nlink == 2 (for "." entry) */
1539 sb
->s_root
= d_make_root(inode
);
1542 /* for everything else we want ->d_op set */
1543 sb
->s_d_op
= &cgroup_dops
;
1547 static struct dentry
*cgroup_mount(struct file_system_type
*fs_type
,
1548 int flags
, const char *unused_dev_name
,
1551 struct cgroup_sb_opts opts
;
1552 struct cgroupfs_root
*root
;
1554 struct super_block
*sb
;
1555 struct cgroupfs_root
*new_root
;
1556 struct list_head tmp_links
;
1557 struct inode
*inode
;
1558 const struct cred
*cred
;
1560 /* First find the desired set of subsystems */
1561 mutex_lock(&cgroup_mutex
);
1562 ret
= parse_cgroupfs_options(data
, &opts
);
1563 mutex_unlock(&cgroup_mutex
);
1568 * Allocate a new cgroup root. We may not need it if we're
1569 * reusing an existing hierarchy.
1571 new_root
= cgroup_root_from_opts(&opts
);
1572 if (IS_ERR(new_root
)) {
1573 ret
= PTR_ERR(new_root
);
1576 opts
.new_root
= new_root
;
1578 /* Locate an existing or new sb for this hierarchy */
1579 sb
= sget(fs_type
, cgroup_test_super
, cgroup_set_super
, 0, &opts
);
1582 cgroup_free_root(opts
.new_root
);
1586 root
= sb
->s_fs_info
;
1588 if (root
== opts
.new_root
) {
1589 /* We used the new root structure, so this is a new hierarchy */
1590 struct cgroup
*root_cgrp
= &root
->top_cgroup
;
1591 struct cgroupfs_root
*existing_root
;
1593 struct css_set
*cset
;
1595 BUG_ON(sb
->s_root
!= NULL
);
1597 ret
= cgroup_get_rootdir(sb
);
1599 goto drop_new_super
;
1600 inode
= sb
->s_root
->d_inode
;
1602 mutex_lock(&inode
->i_mutex
);
1603 mutex_lock(&cgroup_mutex
);
1604 mutex_lock(&cgroup_root_mutex
);
1606 root_cgrp
->id
= idr_alloc(&root
->cgroup_idr
, root_cgrp
,
1608 if (root_cgrp
->id
< 0)
1611 /* Check for name clashes with existing mounts */
1613 if (strlen(root
->name
))
1614 for_each_active_root(existing_root
)
1615 if (!strcmp(existing_root
->name
, root
->name
))
1619 * We're accessing css_set_count without locking
1620 * css_set_lock here, but that's OK - it can only be
1621 * increased by someone holding cgroup_lock, and
1622 * that's us. The worst that can happen is that we
1623 * have some link structures left over
1625 ret
= allocate_cgrp_cset_links(css_set_count
, &tmp_links
);
1629 /* ID 0 is reserved for dummy root, 1 for unified hierarchy */
1630 ret
= cgroup_init_root_id(root
, 2, 0);
1634 sb
->s_root
->d_fsdata
= root_cgrp
;
1635 root_cgrp
->dentry
= sb
->s_root
;
1638 * We're inside get_sb() and will call lookup_one_len() to
1639 * create the root files, which doesn't work if SELinux is
1640 * in use. The following cred dancing somehow works around
1641 * it. See 2ce9738ba ("cgroupfs: use init_cred when
1642 * populating new cgroupfs mount") for more details.
1644 cred
= override_creds(&init_cred
);
1646 ret
= cgroup_addrm_files(root_cgrp
, cgroup_base_files
, true);
1650 ret
= rebind_subsystems(root
, root
->subsys_mask
, 0);
1657 * There must be no failure case after here, since rebinding
1658 * takes care of subsystems' refcounts, which are explicitly
1659 * dropped in the failure exit path.
1662 list_add(&root
->root_list
, &cgroup_roots
);
1663 cgroup_root_count
++;
1665 /* Link the top cgroup in this hierarchy into all
1666 * the css_set objects */
1667 write_lock(&css_set_lock
);
1668 hash_for_each(css_set_table
, i
, cset
, hlist
)
1669 link_css_set(&tmp_links
, cset
, root_cgrp
);
1670 write_unlock(&css_set_lock
);
1672 free_cgrp_cset_links(&tmp_links
);
1674 BUG_ON(!list_empty(&root_cgrp
->children
));
1675 BUG_ON(root
->number_of_cgroups
!= 1);
1677 mutex_unlock(&cgroup_root_mutex
);
1678 mutex_unlock(&cgroup_mutex
);
1679 mutex_unlock(&inode
->i_mutex
);
1682 * We re-used an existing hierarchy - the new root (if
1683 * any) is not needed
1685 cgroup_free_root(opts
.new_root
);
1687 if ((root
->flags
^ opts
.flags
) & CGRP_ROOT_OPTION_MASK
) {
1688 if ((root
->flags
| opts
.flags
) & CGRP_ROOT_SANE_BEHAVIOR
) {
1689 pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
1691 goto drop_new_super
;
1693 pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n");
1698 kfree(opts
.release_agent
);
1700 return dget(sb
->s_root
);
1703 free_cgrp_cset_links(&tmp_links
);
1704 cgroup_addrm_files(&root
->top_cgroup
, cgroup_base_files
, false);
1707 cgroup_exit_root_id(root
);
1708 mutex_unlock(&cgroup_root_mutex
);
1709 mutex_unlock(&cgroup_mutex
);
1710 mutex_unlock(&inode
->i_mutex
);
1712 deactivate_locked_super(sb
);
1714 kfree(opts
.release_agent
);
1716 return ERR_PTR(ret
);
1719 static void cgroup_kill_sb(struct super_block
*sb
) {
1720 struct cgroupfs_root
*root
= sb
->s_fs_info
;
1721 struct cgroup
*cgrp
= &root
->top_cgroup
;
1722 struct cgrp_cset_link
*link
, *tmp_link
;
1727 BUG_ON(root
->number_of_cgroups
!= 1);
1728 BUG_ON(!list_empty(&cgrp
->children
));
1730 mutex_lock(&cgrp
->dentry
->d_inode
->i_mutex
);
1731 mutex_lock(&cgroup_mutex
);
1732 mutex_lock(&cgroup_root_mutex
);
1734 /* Rebind all subsystems back to the default hierarchy */
1735 if (root
->flags
& CGRP_ROOT_SUBSYS_BOUND
) {
1736 ret
= rebind_subsystems(root
, 0, root
->subsys_mask
);
1737 /* Shouldn't be able to fail ... */
1742 * Release all the links from cset_links to this hierarchy's
1745 write_lock(&css_set_lock
);
1747 list_for_each_entry_safe(link
, tmp_link
, &cgrp
->cset_links
, cset_link
) {
1748 list_del(&link
->cset_link
);
1749 list_del(&link
->cgrp_link
);
1752 write_unlock(&css_set_lock
);
1754 if (!list_empty(&root
->root_list
)) {
1755 list_del(&root
->root_list
);
1756 cgroup_root_count
--;
1759 cgroup_exit_root_id(root
);
1761 mutex_unlock(&cgroup_root_mutex
);
1762 mutex_unlock(&cgroup_mutex
);
1763 mutex_unlock(&cgrp
->dentry
->d_inode
->i_mutex
);
1765 simple_xattrs_free(&cgrp
->xattrs
);
1767 kill_litter_super(sb
);
1768 cgroup_free_root(root
);
1771 static struct file_system_type cgroup_fs_type
= {
1773 .mount
= cgroup_mount
,
1774 .kill_sb
= cgroup_kill_sb
,
1777 static struct kobject
*cgroup_kobj
;
1780 * cgroup_path - generate the path of a cgroup
1781 * @cgrp: the cgroup in question
1782 * @buf: the buffer to write the path into
1783 * @buflen: the length of the buffer
1785 * Writes path of cgroup into buf. Returns 0 on success, -errno on error.
1787 * We can't generate cgroup path using dentry->d_name, as accessing
1788 * dentry->name must be protected by irq-unsafe dentry->d_lock or parent
1789 * inode's i_mutex, while on the other hand cgroup_path() can be called
1790 * with some irq-safe spinlocks held.
1792 int cgroup_path(const struct cgroup
*cgrp
, char *buf
, int buflen
)
1794 int ret
= -ENAMETOOLONG
;
1797 if (!cgrp
->parent
) {
1798 if (strlcpy(buf
, "/", buflen
) >= buflen
)
1799 return -ENAMETOOLONG
;
1803 start
= buf
+ buflen
- 1;
1808 const char *name
= cgroup_name(cgrp
);
1812 if ((start
-= len
) < buf
)
1814 memcpy(start
, name
, len
);
1820 cgrp
= cgrp
->parent
;
1821 } while (cgrp
->parent
);
1823 memmove(buf
, start
, buf
+ buflen
- start
);
1828 EXPORT_SYMBOL_GPL(cgroup_path
);
1831 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
1832 * @task: target task
1833 * @buf: the buffer to write the path into
1834 * @buflen: the length of the buffer
1836 * Determine @task's cgroup on the first (the one with the lowest non-zero
1837 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
1838 * function grabs cgroup_mutex and shouldn't be used inside locks used by
1839 * cgroup controller callbacks.
1841 * Returns 0 on success, fails with -%ENAMETOOLONG if @buflen is too short.
1843 int task_cgroup_path(struct task_struct
*task
, char *buf
, size_t buflen
)
1845 struct cgroupfs_root
*root
;
1846 struct cgroup
*cgrp
;
1847 int hierarchy_id
= 1, ret
= 0;
1850 return -ENAMETOOLONG
;
1852 mutex_lock(&cgroup_mutex
);
1854 root
= idr_get_next(&cgroup_hierarchy_idr
, &hierarchy_id
);
1857 cgrp
= task_cgroup_from_root(task
, root
);
1858 ret
= cgroup_path(cgrp
, buf
, buflen
);
1860 /* if no hierarchy exists, everyone is in "/" */
1861 memcpy(buf
, "/", 2);
1864 mutex_unlock(&cgroup_mutex
);
1867 EXPORT_SYMBOL_GPL(task_cgroup_path
);
1870 * Control Group taskset
1872 struct task_and_cgroup
{
1873 struct task_struct
*task
;
1874 struct cgroup
*cgrp
;
1875 struct css_set
*cset
;
1878 struct cgroup_taskset
{
1879 struct task_and_cgroup single
;
1880 struct flex_array
*tc_array
;
1883 struct cgroup
*cur_cgrp
;
1887 * cgroup_taskset_first - reset taskset and return the first task
1888 * @tset: taskset of interest
1890 * @tset iteration is initialized and the first task is returned.
1892 struct task_struct
*cgroup_taskset_first(struct cgroup_taskset
*tset
)
1894 if (tset
->tc_array
) {
1896 return cgroup_taskset_next(tset
);
1898 tset
->cur_cgrp
= tset
->single
.cgrp
;
1899 return tset
->single
.task
;
1902 EXPORT_SYMBOL_GPL(cgroup_taskset_first
);
1905 * cgroup_taskset_next - iterate to the next task in taskset
1906 * @tset: taskset of interest
1908 * Return the next task in @tset. Iteration must have been initialized
1909 * with cgroup_taskset_first().
1911 struct task_struct
*cgroup_taskset_next(struct cgroup_taskset
*tset
)
1913 struct task_and_cgroup
*tc
;
1915 if (!tset
->tc_array
|| tset
->idx
>= tset
->tc_array_len
)
1918 tc
= flex_array_get(tset
->tc_array
, tset
->idx
++);
1919 tset
->cur_cgrp
= tc
->cgrp
;
1922 EXPORT_SYMBOL_GPL(cgroup_taskset_next
);
1925 * cgroup_taskset_cur_css - return the matching css for the current task
1926 * @tset: taskset of interest
1927 * @subsys_id: the ID of the target subsystem
1929 * Return the css for the current (last returned) task of @tset for
1930 * subsystem specified by @subsys_id. This function must be preceded by
1931 * either cgroup_taskset_first() or cgroup_taskset_next().
1933 struct cgroup_subsys_state
*cgroup_taskset_cur_css(struct cgroup_taskset
*tset
,
1936 return cgroup_css(tset
->cur_cgrp
, cgroup_subsys
[subsys_id
]);
1938 EXPORT_SYMBOL_GPL(cgroup_taskset_cur_css
);
1941 * cgroup_taskset_size - return the number of tasks in taskset
1942 * @tset: taskset of interest
1944 int cgroup_taskset_size(struct cgroup_taskset
*tset
)
1946 return tset
->tc_array
? tset
->tc_array_len
: 1;
1948 EXPORT_SYMBOL_GPL(cgroup_taskset_size
);
1952 * cgroup_task_migrate - move a task from one cgroup to another.
1954 * Must be called with cgroup_mutex and threadgroup locked.
1956 static void cgroup_task_migrate(struct cgroup
*old_cgrp
,
1957 struct task_struct
*tsk
,
1958 struct css_set
*new_cset
)
1960 struct css_set
*old_cset
;
1963 * We are synchronized through threadgroup_lock() against PF_EXITING
1964 * setting such that we can't race against cgroup_exit() changing the
1965 * css_set to init_css_set and dropping the old one.
1967 WARN_ON_ONCE(tsk
->flags
& PF_EXITING
);
1968 old_cset
= task_css_set(tsk
);
1971 rcu_assign_pointer(tsk
->cgroups
, new_cset
);
1974 /* Update the css_set linked lists if we're using them */
1975 write_lock(&css_set_lock
);
1976 if (!list_empty(&tsk
->cg_list
))
1977 list_move(&tsk
->cg_list
, &new_cset
->tasks
);
1978 write_unlock(&css_set_lock
);
1981 * We just gained a reference on old_cset by taking it from the
1982 * task. As trading it for new_cset is protected by cgroup_mutex,
1983 * we're safe to drop it here; it will be freed under RCU.
1985 set_bit(CGRP_RELEASABLE
, &old_cgrp
->flags
);
1986 put_css_set(old_cset
);
1990 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
1991 * @cgrp: the cgroup to attach to
1992 * @tsk: the task or the leader of the threadgroup to be attached
1993 * @threadgroup: attach the whole threadgroup?
1995 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
1996 * task_lock of @tsk or each thread in the threadgroup individually in turn.
1998 static int cgroup_attach_task(struct cgroup
*cgrp
, struct task_struct
*tsk
,
2001 int retval
, i
, group_size
;
2002 struct cgroup_subsys
*ss
, *failed_ss
= NULL
;
2003 struct cgroupfs_root
*root
= cgrp
->root
;
2004 /* threadgroup list cursor and array */
2005 struct task_struct
*leader
= tsk
;
2006 struct task_and_cgroup
*tc
;
2007 struct flex_array
*group
;
2008 struct cgroup_taskset tset
= { };
2011 * step 0: in order to do expensive, possibly blocking operations for
2012 * every thread, we cannot iterate the thread group list, since it needs
2013 * rcu or tasklist locked. instead, build an array of all threads in the
2014 * group - group_rwsem prevents new threads from appearing, and if
2015 * threads exit, this will just be an over-estimate.
2018 group_size
= get_nr_threads(tsk
);
2021 /* flex_array supports very large thread-groups better than kmalloc. */
2022 group
= flex_array_alloc(sizeof(*tc
), group_size
, GFP_KERNEL
);
2025 /* pre-allocate to guarantee space while iterating in rcu read-side. */
2026 retval
= flex_array_prealloc(group
, 0, group_size
, GFP_KERNEL
);
2028 goto out_free_group_list
;
2032 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2033 * already PF_EXITING could be freed from underneath us unless we
2034 * take an rcu_read_lock.
2038 struct task_and_cgroup ent
;
2040 /* @tsk either already exited or can't exit until the end */
2041 if (tsk
->flags
& PF_EXITING
)
2044 /* as per above, nr_threads may decrease, but not increase. */
2045 BUG_ON(i
>= group_size
);
2047 ent
.cgrp
= task_cgroup_from_root(tsk
, root
);
2048 /* nothing to do if this task is already in the cgroup */
2049 if (ent
.cgrp
== cgrp
)
2052 * saying GFP_ATOMIC has no effect here because we did prealloc
2053 * earlier, but it's good form to communicate our expectations.
2055 retval
= flex_array_put(group
, i
, &ent
, GFP_ATOMIC
);
2056 BUG_ON(retval
!= 0);
2061 } while_each_thread(leader
, tsk
);
2063 /* remember the number of threads in the array for later. */
2065 tset
.tc_array
= group
;
2066 tset
.tc_array_len
= group_size
;
2068 /* methods shouldn't be called if no task is actually migrating */
2071 goto out_free_group_list
;
2074 * step 1: check that we can legitimately attach to the cgroup.
2076 for_each_root_subsys(root
, ss
) {
2077 struct cgroup_subsys_state
*css
= cgroup_css(cgrp
, ss
);
2079 if (ss
->can_attach
) {
2080 retval
= ss
->can_attach(css
, &tset
);
2083 goto out_cancel_attach
;
2089 * step 2: make sure css_sets exist for all threads to be migrated.
2090 * we use find_css_set, which allocates a new one if necessary.
2092 for (i
= 0; i
< group_size
; i
++) {
2093 struct css_set
*old_cset
;
2095 tc
= flex_array_get(group
, i
);
2096 old_cset
= task_css_set(tc
->task
);
2097 tc
->cset
= find_css_set(old_cset
, cgrp
);
2100 goto out_put_css_set_refs
;
2105 * step 3: now that we're guaranteed success wrt the css_sets,
2106 * proceed to move all tasks to the new cgroup. There are no
2107 * failure cases after here, so this is the commit point.
2109 for (i
= 0; i
< group_size
; i
++) {
2110 tc
= flex_array_get(group
, i
);
2111 cgroup_task_migrate(tc
->cgrp
, tc
->task
, tc
->cset
);
2113 /* nothing is sensitive to fork() after this point. */
2116 * step 4: do subsystem attach callbacks.
2118 for_each_root_subsys(root
, ss
) {
2119 struct cgroup_subsys_state
*css
= cgroup_css(cgrp
, ss
);
2122 ss
->attach(css
, &tset
);
2126 * step 5: success! and cleanup
2129 out_put_css_set_refs
:
2131 for (i
= 0; i
< group_size
; i
++) {
2132 tc
= flex_array_get(group
, i
);
2135 put_css_set(tc
->cset
);
2140 for_each_root_subsys(root
, ss
) {
2141 struct cgroup_subsys_state
*css
= cgroup_css(cgrp
, ss
);
2143 if (ss
== failed_ss
)
2145 if (ss
->cancel_attach
)
2146 ss
->cancel_attach(css
, &tset
);
2149 out_free_group_list
:
2150 flex_array_free(group
);
2155 * Find the task_struct of the task to attach by vpid and pass it along to the
2156 * function to attach either it or all tasks in its threadgroup. Will lock
2157 * cgroup_mutex and threadgroup; may take task_lock of task.
2159 static int attach_task_by_pid(struct cgroup
*cgrp
, u64 pid
, bool threadgroup
)
2161 struct task_struct
*tsk
;
2162 const struct cred
*cred
= current_cred(), *tcred
;
2165 if (!cgroup_lock_live_group(cgrp
))
2171 tsk
= find_task_by_vpid(pid
);
2175 goto out_unlock_cgroup
;
2178 * even if we're attaching all tasks in the thread group, we
2179 * only need to check permissions on one of them.
2181 tcred
= __task_cred(tsk
);
2182 if (!uid_eq(cred
->euid
, GLOBAL_ROOT_UID
) &&
2183 !uid_eq(cred
->euid
, tcred
->uid
) &&
2184 !uid_eq(cred
->euid
, tcred
->suid
)) {
2187 goto out_unlock_cgroup
;
2193 tsk
= tsk
->group_leader
;
2196 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
2197 * trapped in a cpuset, or RT worker may be born in a cgroup
2198 * with no rt_runtime allocated. Just say no.
2200 if (tsk
== kthreadd_task
|| (tsk
->flags
& PF_NO_SETAFFINITY
)) {
2203 goto out_unlock_cgroup
;
2206 get_task_struct(tsk
);
2209 threadgroup_lock(tsk
);
2211 if (!thread_group_leader(tsk
)) {
2213 * a race with de_thread from another thread's exec()
2214 * may strip us of our leadership, if this happens,
2215 * there is no choice but to throw this task away and
2216 * try again; this is
2217 * "double-double-toil-and-trouble-check locking".
2219 threadgroup_unlock(tsk
);
2220 put_task_struct(tsk
);
2221 goto retry_find_task
;
2225 ret
= cgroup_attach_task(cgrp
, tsk
, threadgroup
);
2227 threadgroup_unlock(tsk
);
2229 put_task_struct(tsk
);
2231 mutex_unlock(&cgroup_mutex
);
2236 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2237 * @from: attach to all cgroups of a given task
2238 * @tsk: the task to be attached
2240 int cgroup_attach_task_all(struct task_struct
*from
, struct task_struct
*tsk
)
2242 struct cgroupfs_root
*root
;
2245 mutex_lock(&cgroup_mutex
);
2246 for_each_active_root(root
) {
2247 struct cgroup
*from_cgrp
= task_cgroup_from_root(from
, root
);
2249 retval
= cgroup_attach_task(from_cgrp
, tsk
, false);
2253 mutex_unlock(&cgroup_mutex
);
2257 EXPORT_SYMBOL_GPL(cgroup_attach_task_all
);
2259 static int cgroup_tasks_write(struct cgroup_subsys_state
*css
,
2260 struct cftype
*cft
, u64 pid
)
2262 return attach_task_by_pid(css
->cgroup
, pid
, false);
2265 static int cgroup_procs_write(struct cgroup_subsys_state
*css
,
2266 struct cftype
*cft
, u64 tgid
)
2268 return attach_task_by_pid(css
->cgroup
, tgid
, true);
2271 static int cgroup_release_agent_write(struct cgroup_subsys_state
*css
,
2272 struct cftype
*cft
, const char *buffer
)
2274 BUILD_BUG_ON(sizeof(css
->cgroup
->root
->release_agent_path
) < PATH_MAX
);
2275 if (strlen(buffer
) >= PATH_MAX
)
2277 if (!cgroup_lock_live_group(css
->cgroup
))
2279 mutex_lock(&cgroup_root_mutex
);
2280 strcpy(css
->cgroup
->root
->release_agent_path
, buffer
);
2281 mutex_unlock(&cgroup_root_mutex
);
2282 mutex_unlock(&cgroup_mutex
);
2286 static int cgroup_release_agent_show(struct cgroup_subsys_state
*css
,
2287 struct cftype
*cft
, struct seq_file
*seq
)
2289 struct cgroup
*cgrp
= css
->cgroup
;
2291 if (!cgroup_lock_live_group(cgrp
))
2293 seq_puts(seq
, cgrp
->root
->release_agent_path
);
2294 seq_putc(seq
, '\n');
2295 mutex_unlock(&cgroup_mutex
);
2299 static int cgroup_sane_behavior_show(struct cgroup_subsys_state
*css
,
2300 struct cftype
*cft
, struct seq_file
*seq
)
2302 seq_printf(seq
, "%d\n", cgroup_sane_behavior(css
->cgroup
));
2306 /* A buffer size big enough for numbers or short strings */
2307 #define CGROUP_LOCAL_BUFFER_SIZE 64
2309 static ssize_t
cgroup_write_X64(struct cgroup_subsys_state
*css
,
2310 struct cftype
*cft
, struct file
*file
,
2311 const char __user
*userbuf
, size_t nbytes
,
2312 loff_t
*unused_ppos
)
2314 char buffer
[CGROUP_LOCAL_BUFFER_SIZE
];
2320 if (nbytes
>= sizeof(buffer
))
2322 if (copy_from_user(buffer
, userbuf
, nbytes
))
2325 buffer
[nbytes
] = 0; /* nul-terminate */
2326 if (cft
->write_u64
) {
2327 u64 val
= simple_strtoull(strstrip(buffer
), &end
, 0);
2330 retval
= cft
->write_u64(css
, cft
, val
);
2332 s64 val
= simple_strtoll(strstrip(buffer
), &end
, 0);
2335 retval
= cft
->write_s64(css
, cft
, val
);
2342 static ssize_t
cgroup_write_string(struct cgroup_subsys_state
*css
,
2343 struct cftype
*cft
, struct file
*file
,
2344 const char __user
*userbuf
, size_t nbytes
,
2345 loff_t
*unused_ppos
)
2347 char local_buffer
[CGROUP_LOCAL_BUFFER_SIZE
];
2349 size_t max_bytes
= cft
->max_write_len
;
2350 char *buffer
= local_buffer
;
2353 max_bytes
= sizeof(local_buffer
) - 1;
2354 if (nbytes
>= max_bytes
)
2356 /* Allocate a dynamic buffer if we need one */
2357 if (nbytes
>= sizeof(local_buffer
)) {
2358 buffer
= kmalloc(nbytes
+ 1, GFP_KERNEL
);
2362 if (nbytes
&& copy_from_user(buffer
, userbuf
, nbytes
)) {
2367 buffer
[nbytes
] = 0; /* nul-terminate */
2368 retval
= cft
->write_string(css
, cft
, strstrip(buffer
));
2372 if (buffer
!= local_buffer
)
2377 static ssize_t
cgroup_file_write(struct file
*file
, const char __user
*buf
,
2378 size_t nbytes
, loff_t
*ppos
)
2380 struct cfent
*cfe
= __d_cfe(file
->f_dentry
);
2381 struct cftype
*cft
= __d_cft(file
->f_dentry
);
2382 struct cgroup_subsys_state
*css
= cfe
->css
;
2385 return cft
->write(css
, cft
, file
, buf
, nbytes
, ppos
);
2386 if (cft
->write_u64
|| cft
->write_s64
)
2387 return cgroup_write_X64(css
, cft
, file
, buf
, nbytes
, ppos
);
2388 if (cft
->write_string
)
2389 return cgroup_write_string(css
, cft
, file
, buf
, nbytes
, ppos
);
2391 int ret
= cft
->trigger(css
, (unsigned int)cft
->private);
2392 return ret
? ret
: nbytes
;
2397 static ssize_t
cgroup_read_u64(struct cgroup_subsys_state
*css
,
2398 struct cftype
*cft
, struct file
*file
,
2399 char __user
*buf
, size_t nbytes
, loff_t
*ppos
)
2401 char tmp
[CGROUP_LOCAL_BUFFER_SIZE
];
2402 u64 val
= cft
->read_u64(css
, cft
);
2403 int len
= sprintf(tmp
, "%llu\n", (unsigned long long) val
);
2405 return simple_read_from_buffer(buf
, nbytes
, ppos
, tmp
, len
);
2408 static ssize_t
cgroup_read_s64(struct cgroup_subsys_state
*css
,
2409 struct cftype
*cft
, struct file
*file
,
2410 char __user
*buf
, size_t nbytes
, loff_t
*ppos
)
2412 char tmp
[CGROUP_LOCAL_BUFFER_SIZE
];
2413 s64 val
= cft
->read_s64(css
, cft
);
2414 int len
= sprintf(tmp
, "%lld\n", (long long) val
);
2416 return simple_read_from_buffer(buf
, nbytes
, ppos
, tmp
, len
);
2419 static ssize_t
cgroup_file_read(struct file
*file
, char __user
*buf
,
2420 size_t nbytes
, loff_t
*ppos
)
2422 struct cfent
*cfe
= __d_cfe(file
->f_dentry
);
2423 struct cftype
*cft
= __d_cft(file
->f_dentry
);
2424 struct cgroup_subsys_state
*css
= cfe
->css
;
2427 return cft
->read(css
, cft
, file
, buf
, nbytes
, ppos
);
2429 return cgroup_read_u64(css
, cft
, file
, buf
, nbytes
, ppos
);
2431 return cgroup_read_s64(css
, cft
, file
, buf
, nbytes
, ppos
);
2436 * seqfile ops/methods for returning structured data. Currently just
2437 * supports string->u64 maps, but can be extended in future.
2440 static int cgroup_map_add(struct cgroup_map_cb
*cb
, const char *key
, u64 value
)
2442 struct seq_file
*sf
= cb
->state
;
2443 return seq_printf(sf
, "%s %llu\n", key
, (unsigned long long)value
);
2446 static int cgroup_seqfile_show(struct seq_file
*m
, void *arg
)
2448 struct cfent
*cfe
= m
->private;
2449 struct cftype
*cft
= cfe
->type
;
2450 struct cgroup_subsys_state
*css
= cfe
->css
;
2452 if (cft
->read_map
) {
2453 struct cgroup_map_cb cb
= {
2454 .fill
= cgroup_map_add
,
2457 return cft
->read_map(css
, cft
, &cb
);
2459 return cft
->read_seq_string(css
, cft
, m
);
2462 static const struct file_operations cgroup_seqfile_operations
= {
2464 .write
= cgroup_file_write
,
2465 .llseek
= seq_lseek
,
2466 .release
= single_release
,
2469 static int cgroup_file_open(struct inode
*inode
, struct file
*file
)
2471 struct cfent
*cfe
= __d_cfe(file
->f_dentry
);
2472 struct cftype
*cft
= __d_cft(file
->f_dentry
);
2473 struct cgroup
*cgrp
= __d_cgrp(cfe
->dentry
->d_parent
);
2474 struct cgroup_subsys_state
*css
;
2477 err
= generic_file_open(inode
, file
);
2482 * If the file belongs to a subsystem, pin the css. Will be
2483 * unpinned either on open failure or release. This ensures that
2484 * @css stays alive for all file operations.
2487 css
= cgroup_css(cgrp
, cft
->ss
);
2488 if (cft
->ss
&& !css_tryget(css
))
2496 * @cfe->css is used by read/write/close to determine the
2497 * associated css. @file->private_data would be a better place but
2498 * that's already used by seqfile. Multiple accessors may use it
2499 * simultaneously which is okay as the association never changes.
2501 WARN_ON_ONCE(cfe
->css
&& cfe
->css
!= css
);
2504 if (cft
->read_map
|| cft
->read_seq_string
) {
2505 file
->f_op
= &cgroup_seqfile_operations
;
2506 err
= single_open(file
, cgroup_seqfile_show
, cfe
);
2507 } else if (cft
->open
) {
2508 err
= cft
->open(inode
, file
);
2516 static int cgroup_file_release(struct inode
*inode
, struct file
*file
)
2518 struct cfent
*cfe
= __d_cfe(file
->f_dentry
);
2519 struct cftype
*cft
= __d_cft(file
->f_dentry
);
2520 struct cgroup_subsys_state
*css
= cfe
->css
;
2524 ret
= cft
->release(inode
, file
);
2531 * cgroup_rename - Only allow simple rename of directories in place.
2533 static int cgroup_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
2534 struct inode
*new_dir
, struct dentry
*new_dentry
)
2537 struct cgroup_name
*name
, *old_name
;
2538 struct cgroup
*cgrp
;
2541 * It's convinient to use parent dir's i_mutex to protected
2544 lockdep_assert_held(&old_dir
->i_mutex
);
2546 if (!S_ISDIR(old_dentry
->d_inode
->i_mode
))
2548 if (new_dentry
->d_inode
)
2550 if (old_dir
!= new_dir
)
2553 cgrp
= __d_cgrp(old_dentry
);
2556 * This isn't a proper migration and its usefulness is very
2557 * limited. Disallow if sane_behavior.
2559 if (cgroup_sane_behavior(cgrp
))
2562 name
= cgroup_alloc_name(new_dentry
);
2566 ret
= simple_rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
2572 old_name
= rcu_dereference_protected(cgrp
->name
, true);
2573 rcu_assign_pointer(cgrp
->name
, name
);
2575 kfree_rcu(old_name
, rcu_head
);
2579 static struct simple_xattrs
*__d_xattrs(struct dentry
*dentry
)
2581 if (S_ISDIR(dentry
->d_inode
->i_mode
))
2582 return &__d_cgrp(dentry
)->xattrs
;
2584 return &__d_cfe(dentry
)->xattrs
;
2587 static inline int xattr_enabled(struct dentry
*dentry
)
2589 struct cgroupfs_root
*root
= dentry
->d_sb
->s_fs_info
;
2590 return root
->flags
& CGRP_ROOT_XATTR
;
2593 static bool is_valid_xattr(const char *name
)
2595 if (!strncmp(name
, XATTR_TRUSTED_PREFIX
, XATTR_TRUSTED_PREFIX_LEN
) ||
2596 !strncmp(name
, XATTR_SECURITY_PREFIX
, XATTR_SECURITY_PREFIX_LEN
))
2601 static int cgroup_setxattr(struct dentry
*dentry
, const char *name
,
2602 const void *val
, size_t size
, int flags
)
2604 if (!xattr_enabled(dentry
))
2606 if (!is_valid_xattr(name
))
2608 return simple_xattr_set(__d_xattrs(dentry
), name
, val
, size
, flags
);
2611 static int cgroup_removexattr(struct dentry
*dentry
, const char *name
)
2613 if (!xattr_enabled(dentry
))
2615 if (!is_valid_xattr(name
))
2617 return simple_xattr_remove(__d_xattrs(dentry
), name
);
2620 static ssize_t
cgroup_getxattr(struct dentry
*dentry
, const char *name
,
2621 void *buf
, size_t size
)
2623 if (!xattr_enabled(dentry
))
2625 if (!is_valid_xattr(name
))
2627 return simple_xattr_get(__d_xattrs(dentry
), name
, buf
, size
);
2630 static ssize_t
cgroup_listxattr(struct dentry
*dentry
, char *buf
, size_t size
)
2632 if (!xattr_enabled(dentry
))
2634 return simple_xattr_list(__d_xattrs(dentry
), buf
, size
);
2637 static const struct file_operations cgroup_file_operations
= {
2638 .read
= cgroup_file_read
,
2639 .write
= cgroup_file_write
,
2640 .llseek
= generic_file_llseek
,
2641 .open
= cgroup_file_open
,
2642 .release
= cgroup_file_release
,
2645 static const struct inode_operations cgroup_file_inode_operations
= {
2646 .setxattr
= cgroup_setxattr
,
2647 .getxattr
= cgroup_getxattr
,
2648 .listxattr
= cgroup_listxattr
,
2649 .removexattr
= cgroup_removexattr
,
2652 static const struct inode_operations cgroup_dir_inode_operations
= {
2653 .lookup
= simple_lookup
,
2654 .mkdir
= cgroup_mkdir
,
2655 .rmdir
= cgroup_rmdir
,
2656 .rename
= cgroup_rename
,
2657 .setxattr
= cgroup_setxattr
,
2658 .getxattr
= cgroup_getxattr
,
2659 .listxattr
= cgroup_listxattr
,
2660 .removexattr
= cgroup_removexattr
,
2664 * Check if a file is a control file
2666 static inline struct cftype
*__file_cft(struct file
*file
)
2668 if (file_inode(file
)->i_fop
!= &cgroup_file_operations
)
2669 return ERR_PTR(-EINVAL
);
2670 return __d_cft(file
->f_dentry
);
2673 static int cgroup_create_file(struct dentry
*dentry
, umode_t mode
,
2674 struct super_block
*sb
)
2676 struct inode
*inode
;
2680 if (dentry
->d_inode
)
2683 inode
= cgroup_new_inode(mode
, sb
);
2687 if (S_ISDIR(mode
)) {
2688 inode
->i_op
= &cgroup_dir_inode_operations
;
2689 inode
->i_fop
= &simple_dir_operations
;
2691 /* start off with i_nlink == 2 (for "." entry) */
2693 inc_nlink(dentry
->d_parent
->d_inode
);
2696 * Control reaches here with cgroup_mutex held.
2697 * @inode->i_mutex should nest outside cgroup_mutex but we
2698 * want to populate it immediately without releasing
2699 * cgroup_mutex. As @inode isn't visible to anyone else
2700 * yet, trylock will always succeed without affecting
2703 WARN_ON_ONCE(!mutex_trylock(&inode
->i_mutex
));
2704 } else if (S_ISREG(mode
)) {
2706 inode
->i_fop
= &cgroup_file_operations
;
2707 inode
->i_op
= &cgroup_file_inode_operations
;
2709 d_instantiate(dentry
, inode
);
2710 dget(dentry
); /* Extra count - pin the dentry in core */
2715 * cgroup_file_mode - deduce file mode of a control file
2716 * @cft: the control file in question
2718 * returns cft->mode if ->mode is not 0
2719 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2720 * returns S_IRUGO if it has only a read handler
2721 * returns S_IWUSR if it has only a write hander
2723 static umode_t
cgroup_file_mode(const struct cftype
*cft
)
2730 if (cft
->read
|| cft
->read_u64
|| cft
->read_s64
||
2731 cft
->read_map
|| cft
->read_seq_string
)
2734 if (cft
->write
|| cft
->write_u64
|| cft
->write_s64
||
2735 cft
->write_string
|| cft
->trigger
)
2741 static int cgroup_add_file(struct cgroup
*cgrp
, struct cftype
*cft
)
2743 struct dentry
*dir
= cgrp
->dentry
;
2744 struct cgroup
*parent
= __d_cgrp(dir
);
2745 struct dentry
*dentry
;
2749 char name
[MAX_CGROUP_TYPE_NAMELEN
+ MAX_CFTYPE_NAME
+ 2] = { 0 };
2751 if (cft
->ss
&& !(cft
->flags
& CFTYPE_NO_PREFIX
) &&
2752 !(cgrp
->root
->flags
& CGRP_ROOT_NOPREFIX
)) {
2753 strcpy(name
, cft
->ss
->name
);
2756 strcat(name
, cft
->name
);
2758 BUG_ON(!mutex_is_locked(&dir
->d_inode
->i_mutex
));
2760 cfe
= kzalloc(sizeof(*cfe
), GFP_KERNEL
);
2764 dentry
= lookup_one_len(name
, dir
, strlen(name
));
2765 if (IS_ERR(dentry
)) {
2766 error
= PTR_ERR(dentry
);
2770 cfe
->type
= (void *)cft
;
2771 cfe
->dentry
= dentry
;
2772 dentry
->d_fsdata
= cfe
;
2773 simple_xattrs_init(&cfe
->xattrs
);
2775 mode
= cgroup_file_mode(cft
);
2776 error
= cgroup_create_file(dentry
, mode
| S_IFREG
, cgrp
->root
->sb
);
2778 list_add_tail(&cfe
->node
, &parent
->files
);
2788 * cgroup_addrm_files - add or remove files to a cgroup directory
2789 * @cgrp: the target cgroup
2790 * @cfts: array of cftypes to be added
2791 * @is_add: whether to add or remove
2793 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
2794 * For removals, this function never fails. If addition fails, this
2795 * function doesn't remove files already added. The caller is responsible
2798 static int cgroup_addrm_files(struct cgroup
*cgrp
, struct cftype cfts
[],
2804 lockdep_assert_held(&cgrp
->dentry
->d_inode
->i_mutex
);
2805 lockdep_assert_held(&cgroup_mutex
);
2807 for (cft
= cfts
; cft
->name
[0] != '\0'; cft
++) {
2808 /* does cft->flags tell us to skip this file on @cgrp? */
2809 if ((cft
->flags
& CFTYPE_INSANE
) && cgroup_sane_behavior(cgrp
))
2811 if ((cft
->flags
& CFTYPE_NOT_ON_ROOT
) && !cgrp
->parent
)
2813 if ((cft
->flags
& CFTYPE_ONLY_ON_ROOT
) && cgrp
->parent
)
2817 ret
= cgroup_add_file(cgrp
, cft
);
2819 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
2824 cgroup_rm_file(cgrp
, cft
);
2830 static void cgroup_cfts_prepare(void)
2831 __acquires(&cgroup_mutex
)
2834 * Thanks to the entanglement with vfs inode locking, we can't walk
2835 * the existing cgroups under cgroup_mutex and create files.
2836 * Instead, we use css_for_each_descendant_pre() and drop RCU read
2837 * lock before calling cgroup_addrm_files().
2839 mutex_lock(&cgroup_mutex
);
2842 static int cgroup_cfts_commit(struct cftype
*cfts
, bool is_add
)
2843 __releases(&cgroup_mutex
)
2846 struct cgroup_subsys
*ss
= cfts
[0].ss
;
2847 struct cgroup
*root
= &ss
->root
->top_cgroup
;
2848 struct super_block
*sb
= ss
->root
->sb
;
2849 struct dentry
*prev
= NULL
;
2850 struct inode
*inode
;
2851 struct cgroup_subsys_state
*css
;
2855 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
2856 if (!cfts
|| ss
->root
== &cgroup_dummy_root
||
2857 !atomic_inc_not_zero(&sb
->s_active
)) {
2858 mutex_unlock(&cgroup_mutex
);
2863 * All cgroups which are created after we drop cgroup_mutex will
2864 * have the updated set of files, so we only need to update the
2865 * cgroups created before the current @cgroup_serial_nr_next.
2867 update_before
= cgroup_serial_nr_next
;
2869 mutex_unlock(&cgroup_mutex
);
2871 /* add/rm files for all cgroups created before */
2873 css_for_each_descendant_pre(css
, cgroup_css(root
, ss
)) {
2874 struct cgroup
*cgrp
= css
->cgroup
;
2876 if (cgroup_is_dead(cgrp
))
2879 inode
= cgrp
->dentry
->d_inode
;
2884 prev
= cgrp
->dentry
;
2886 mutex_lock(&inode
->i_mutex
);
2887 mutex_lock(&cgroup_mutex
);
2888 if (cgrp
->serial_nr
< update_before
&& !cgroup_is_dead(cgrp
))
2889 ret
= cgroup_addrm_files(cgrp
, cfts
, is_add
);
2890 mutex_unlock(&cgroup_mutex
);
2891 mutex_unlock(&inode
->i_mutex
);
2899 deactivate_super(sb
);
2904 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2905 * @ss: target cgroup subsystem
2906 * @cfts: zero-length name terminated array of cftypes
2908 * Register @cfts to @ss. Files described by @cfts are created for all
2909 * existing cgroups to which @ss is attached and all future cgroups will
2910 * have them too. This function can be called anytime whether @ss is
2913 * Returns 0 on successful registration, -errno on failure. Note that this
2914 * function currently returns 0 as long as @cfts registration is successful
2915 * even if some file creation attempts on existing cgroups fail.
2917 int cgroup_add_cftypes(struct cgroup_subsys
*ss
, struct cftype
*cfts
)
2919 struct cftype_set
*set
;
2923 set
= kzalloc(sizeof(*set
), GFP_KERNEL
);
2927 for (cft
= cfts
; cft
->name
[0] != '\0'; cft
++)
2930 cgroup_cfts_prepare();
2932 list_add_tail(&set
->node
, &ss
->cftsets
);
2933 ret
= cgroup_cfts_commit(cfts
, true);
2935 cgroup_rm_cftypes(cfts
);
2938 EXPORT_SYMBOL_GPL(cgroup_add_cftypes
);
2941 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
2942 * @cfts: zero-length name terminated array of cftypes
2944 * Unregister @cfts. Files described by @cfts are removed from all
2945 * existing cgroups and all future cgroups won't have them either. This
2946 * function can be called anytime whether @cfts' subsys is attached or not.
2948 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
2951 int cgroup_rm_cftypes(struct cftype
*cfts
)
2953 struct cftype_set
*set
;
2955 if (!cfts
|| !cfts
[0].ss
)
2958 cgroup_cfts_prepare();
2960 list_for_each_entry(set
, &cfts
[0].ss
->cftsets
, node
) {
2961 if (set
->cfts
== cfts
) {
2962 list_del(&set
->node
);
2964 cgroup_cfts_commit(cfts
, false);
2969 cgroup_cfts_commit(NULL
, false);
2974 * cgroup_task_count - count the number of tasks in a cgroup.
2975 * @cgrp: the cgroup in question
2977 * Return the number of tasks in the cgroup.
2979 int cgroup_task_count(const struct cgroup
*cgrp
)
2982 struct cgrp_cset_link
*link
;
2984 read_lock(&css_set_lock
);
2985 list_for_each_entry(link
, &cgrp
->cset_links
, cset_link
)
2986 count
+= atomic_read(&link
->cset
->refcount
);
2987 read_unlock(&css_set_lock
);
2992 * To reduce the fork() overhead for systems that are not actually using
2993 * their cgroups capability, we don't maintain the lists running through
2994 * each css_set to its tasks until we see the list actually used - in other
2995 * words after the first call to css_task_iter_start().
2997 static void cgroup_enable_task_cg_lists(void)
2999 struct task_struct
*p
, *g
;
3000 write_lock(&css_set_lock
);
3001 use_task_css_set_links
= 1;
3003 * We need tasklist_lock because RCU is not safe against
3004 * while_each_thread(). Besides, a forking task that has passed
3005 * cgroup_post_fork() without seeing use_task_css_set_links = 1
3006 * is not guaranteed to have its child immediately visible in the
3007 * tasklist if we walk through it with RCU.
3009 read_lock(&tasklist_lock
);
3010 do_each_thread(g
, p
) {
3013 * We should check if the process is exiting, otherwise
3014 * it will race with cgroup_exit() in that the list
3015 * entry won't be deleted though the process has exited.
3017 if (!(p
->flags
& PF_EXITING
) && list_empty(&p
->cg_list
))
3018 list_add(&p
->cg_list
, &task_css_set(p
)->tasks
);
3020 } while_each_thread(g
, p
);
3021 read_unlock(&tasklist_lock
);
3022 write_unlock(&css_set_lock
);
3026 * css_next_child - find the next child of a given css
3027 * @pos_css: the current position (%NULL to initiate traversal)
3028 * @parent_css: css whose children to walk
3030 * This function returns the next child of @parent_css and should be called
3031 * under RCU read lock. The only requirement is that @parent_css and
3032 * @pos_css are accessible. The next sibling is guaranteed to be returned
3033 * regardless of their states.
3035 struct cgroup_subsys_state
*
3036 css_next_child(struct cgroup_subsys_state
*pos_css
,
3037 struct cgroup_subsys_state
*parent_css
)
3039 struct cgroup
*pos
= pos_css
? pos_css
->cgroup
: NULL
;
3040 struct cgroup
*cgrp
= parent_css
->cgroup
;
3041 struct cgroup
*next
;
3043 WARN_ON_ONCE(!rcu_read_lock_held());
3046 * @pos could already have been removed. Once a cgroup is removed,
3047 * its ->sibling.next is no longer updated when its next sibling
3048 * changes. As CGRP_DEAD assertion is serialized and happens
3049 * before the cgroup is taken off the ->sibling list, if we see it
3050 * unasserted, it's guaranteed that the next sibling hasn't
3051 * finished its grace period even if it's already removed, and thus
3052 * safe to dereference from this RCU critical section. If
3053 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
3054 * to be visible as %true here.
3056 * If @pos is dead, its next pointer can't be dereferenced;
3057 * however, as each cgroup is given a monotonically increasing
3058 * unique serial number and always appended to the sibling list,
3059 * the next one can be found by walking the parent's children until
3060 * we see a cgroup with higher serial number than @pos's. While
3061 * this path can be slower, it's taken only when either the current
3062 * cgroup is removed or iteration and removal race.
3065 next
= list_entry_rcu(cgrp
->children
.next
, struct cgroup
, sibling
);
3066 } else if (likely(!cgroup_is_dead(pos
))) {
3067 next
= list_entry_rcu(pos
->sibling
.next
, struct cgroup
, sibling
);
3069 list_for_each_entry_rcu(next
, &cgrp
->children
, sibling
)
3070 if (next
->serial_nr
> pos
->serial_nr
)
3074 if (&next
->sibling
== &cgrp
->children
)
3077 return cgroup_css(next
, parent_css
->ss
);
3079 EXPORT_SYMBOL_GPL(css_next_child
);
3082 * css_next_descendant_pre - find the next descendant for pre-order walk
3083 * @pos: the current position (%NULL to initiate traversal)
3084 * @root: css whose descendants to walk
3086 * To be used by css_for_each_descendant_pre(). Find the next descendant
3087 * to visit for pre-order traversal of @root's descendants. @root is
3088 * included in the iteration and the first node to be visited.
3090 * While this function requires RCU read locking, it doesn't require the
3091 * whole traversal to be contained in a single RCU critical section. This
3092 * function will return the correct next descendant as long as both @pos
3093 * and @root are accessible and @pos is a descendant of @root.
3095 struct cgroup_subsys_state
*
3096 css_next_descendant_pre(struct cgroup_subsys_state
*pos
,
3097 struct cgroup_subsys_state
*root
)
3099 struct cgroup_subsys_state
*next
;
3101 WARN_ON_ONCE(!rcu_read_lock_held());
3103 /* if first iteration, visit @root */
3107 /* visit the first child if exists */
3108 next
= css_next_child(NULL
, pos
);
3112 /* no child, visit my or the closest ancestor's next sibling */
3113 while (pos
!= root
) {
3114 next
= css_next_child(pos
, css_parent(pos
));
3117 pos
= css_parent(pos
);
3122 EXPORT_SYMBOL_GPL(css_next_descendant_pre
);
3125 * css_rightmost_descendant - return the rightmost descendant of a css
3126 * @pos: css of interest
3128 * Return the rightmost descendant of @pos. If there's no descendant, @pos
3129 * is returned. This can be used during pre-order traversal to skip
3132 * While this function requires RCU read locking, it doesn't require the
3133 * whole traversal to be contained in a single RCU critical section. This
3134 * function will return the correct rightmost descendant as long as @pos is
3137 struct cgroup_subsys_state
*
3138 css_rightmost_descendant(struct cgroup_subsys_state
*pos
)
3140 struct cgroup_subsys_state
*last
, *tmp
;
3142 WARN_ON_ONCE(!rcu_read_lock_held());
3146 /* ->prev isn't RCU safe, walk ->next till the end */
3148 css_for_each_child(tmp
, last
)
3154 EXPORT_SYMBOL_GPL(css_rightmost_descendant
);
3156 static struct cgroup_subsys_state
*
3157 css_leftmost_descendant(struct cgroup_subsys_state
*pos
)
3159 struct cgroup_subsys_state
*last
;
3163 pos
= css_next_child(NULL
, pos
);
3170 * css_next_descendant_post - find the next descendant for post-order walk
3171 * @pos: the current position (%NULL to initiate traversal)
3172 * @root: css whose descendants to walk
3174 * To be used by css_for_each_descendant_post(). Find the next descendant
3175 * to visit for post-order traversal of @root's descendants. @root is
3176 * included in the iteration and the last node to be visited.
3178 * While this function requires RCU read locking, it doesn't require the
3179 * whole traversal to be contained in a single RCU critical section. This
3180 * function will return the correct next descendant as long as both @pos
3181 * and @cgroup are accessible and @pos is a descendant of @cgroup.
3183 struct cgroup_subsys_state
*
3184 css_next_descendant_post(struct cgroup_subsys_state
*pos
,
3185 struct cgroup_subsys_state
*root
)
3187 struct cgroup_subsys_state
*next
;
3189 WARN_ON_ONCE(!rcu_read_lock_held());
3191 /* if first iteration, visit the leftmost descendant */
3193 next
= css_leftmost_descendant(root
);
3194 return next
!= root
? next
: NULL
;
3197 /* if we visited @root, we're done */
3201 /* if there's an unvisited sibling, visit its leftmost descendant */
3202 next
= css_next_child(pos
, css_parent(pos
));
3204 return css_leftmost_descendant(next
);
3206 /* no sibling left, visit parent */
3207 return css_parent(pos
);
3209 EXPORT_SYMBOL_GPL(css_next_descendant_post
);
3212 * css_advance_task_iter - advance a task itererator to the next css_set
3213 * @it: the iterator to advance
3215 * Advance @it to the next css_set to walk.
3217 static void css_advance_task_iter(struct css_task_iter
*it
)
3219 struct list_head
*l
= it
->cset_link
;
3220 struct cgrp_cset_link
*link
;
3221 struct css_set
*cset
;
3223 /* Advance to the next non-empty css_set */
3226 if (l
== &it
->origin_css
->cgroup
->cset_links
) {
3227 it
->cset_link
= NULL
;
3230 link
= list_entry(l
, struct cgrp_cset_link
, cset_link
);
3232 } while (list_empty(&cset
->tasks
));
3234 it
->task
= cset
->tasks
.next
;
3238 * css_task_iter_start - initiate task iteration
3239 * @css: the css to walk tasks of
3240 * @it: the task iterator to use
3242 * Initiate iteration through the tasks of @css. The caller can call
3243 * css_task_iter_next() to walk through the tasks until the function
3244 * returns NULL. On completion of iteration, css_task_iter_end() must be
3247 * Note that this function acquires a lock which is released when the
3248 * iteration finishes. The caller can't sleep while iteration is in
3251 void css_task_iter_start(struct cgroup_subsys_state
*css
,
3252 struct css_task_iter
*it
)
3253 __acquires(css_set_lock
)
3256 * The first time anyone tries to iterate across a css, we need to
3257 * enable the list linking each css_set to its tasks, and fix up
3258 * all existing tasks.
3260 if (!use_task_css_set_links
)
3261 cgroup_enable_task_cg_lists();
3263 read_lock(&css_set_lock
);
3265 it
->origin_css
= css
;
3266 it
->cset_link
= &css
->cgroup
->cset_links
;
3268 css_advance_task_iter(it
);
3272 * css_task_iter_next - return the next task for the iterator
3273 * @it: the task iterator being iterated
3275 * The "next" function for task iteration. @it should have been
3276 * initialized via css_task_iter_start(). Returns NULL when the iteration
3279 struct task_struct
*css_task_iter_next(struct css_task_iter
*it
)
3281 struct task_struct
*res
;
3282 struct list_head
*l
= it
->task
;
3283 struct cgrp_cset_link
*link
;
3285 /* If the iterator cg is NULL, we have no tasks */
3288 res
= list_entry(l
, struct task_struct
, cg_list
);
3289 /* Advance iterator to find next entry */
3291 link
= list_entry(it
->cset_link
, struct cgrp_cset_link
, cset_link
);
3292 if (l
== &link
->cset
->tasks
) {
3294 * We reached the end of this task list - move on to the
3295 * next cgrp_cset_link.
3297 css_advance_task_iter(it
);
3305 * css_task_iter_end - finish task iteration
3306 * @it: the task iterator to finish
3308 * Finish task iteration started by css_task_iter_start().
3310 void css_task_iter_end(struct css_task_iter
*it
)
3311 __releases(css_set_lock
)
3313 read_unlock(&css_set_lock
);
3316 static inline int started_after_time(struct task_struct
*t1
,
3317 struct timespec
*time
,
3318 struct task_struct
*t2
)
3320 int start_diff
= timespec_compare(&t1
->start_time
, time
);
3321 if (start_diff
> 0) {
3323 } else if (start_diff
< 0) {
3327 * Arbitrarily, if two processes started at the same
3328 * time, we'll say that the lower pointer value
3329 * started first. Note that t2 may have exited by now
3330 * so this may not be a valid pointer any longer, but
3331 * that's fine - it still serves to distinguish
3332 * between two tasks started (effectively) simultaneously.
3339 * This function is a callback from heap_insert() and is used to order
3341 * In this case we order the heap in descending task start time.
3343 static inline int started_after(void *p1
, void *p2
)
3345 struct task_struct
*t1
= p1
;
3346 struct task_struct
*t2
= p2
;
3347 return started_after_time(t1
, &t2
->start_time
, t2
);
3351 * css_scan_tasks - iterate though all the tasks in a css
3352 * @css: the css to iterate tasks of
3353 * @test: optional test callback
3354 * @process: process callback
3355 * @data: data passed to @test and @process
3356 * @heap: optional pre-allocated heap used for task iteration
3358 * Iterate through all the tasks in @css, calling @test for each, and if it
3359 * returns %true, call @process for it also.
3361 * @test may be NULL, meaning always true (select all tasks), which
3362 * effectively duplicates css_task_iter_{start,next,end}() but does not
3363 * lock css_set_lock for the call to @process.
3365 * It is guaranteed that @process will act on every task that is a member
3366 * of @css for the duration of this call. This function may or may not
3367 * call @process for tasks that exit or move to a different css during the
3368 * call, or are forked or move into the css during the call.
3370 * Note that @test may be called with locks held, and may in some
3371 * situations be called multiple times for the same task, so it should be
3374 * If @heap is non-NULL, a heap has been pre-allocated and will be used for
3375 * heap operations (and its "gt" member will be overwritten), else a
3376 * temporary heap will be used (allocation of which may cause this function
3379 int css_scan_tasks(struct cgroup_subsys_state
*css
,
3380 bool (*test
)(struct task_struct
*, void *),
3381 void (*process
)(struct task_struct
*, void *),
3382 void *data
, struct ptr_heap
*heap
)
3385 struct css_task_iter it
;
3386 struct task_struct
*p
, *dropped
;
3387 /* Never dereference latest_task, since it's not refcounted */
3388 struct task_struct
*latest_task
= NULL
;
3389 struct ptr_heap tmp_heap
;
3390 struct timespec latest_time
= { 0, 0 };
3393 /* The caller supplied our heap and pre-allocated its memory */
3394 heap
->gt
= &started_after
;
3396 /* We need to allocate our own heap memory */
3398 retval
= heap_init(heap
, PAGE_SIZE
, GFP_KERNEL
, &started_after
);
3400 /* cannot allocate the heap */
3406 * Scan tasks in the css, using the @test callback to determine
3407 * which are of interest, and invoking @process callback on the
3408 * ones which need an update. Since we don't want to hold any
3409 * locks during the task updates, gather tasks to be processed in a
3410 * heap structure. The heap is sorted by descending task start
3411 * time. If the statically-sized heap fills up, we overflow tasks
3412 * that started later, and in future iterations only consider tasks
3413 * that started after the latest task in the previous pass. This
3414 * guarantees forward progress and that we don't miss any tasks.
3417 css_task_iter_start(css
, &it
);
3418 while ((p
= css_task_iter_next(&it
))) {
3420 * Only affect tasks that qualify per the caller's callback,
3421 * if he provided one
3423 if (test
&& !test(p
, data
))
3426 * Only process tasks that started after the last task
3429 if (!started_after_time(p
, &latest_time
, latest_task
))
3431 dropped
= heap_insert(heap
, p
);
3432 if (dropped
== NULL
) {
3434 * The new task was inserted; the heap wasn't
3438 } else if (dropped
!= p
) {
3440 * The new task was inserted, and pushed out a
3444 put_task_struct(dropped
);
3447 * Else the new task was newer than anything already in
3448 * the heap and wasn't inserted
3451 css_task_iter_end(&it
);
3454 for (i
= 0; i
< heap
->size
; i
++) {
3455 struct task_struct
*q
= heap
->ptrs
[i
];
3457 latest_time
= q
->start_time
;
3460 /* Process the task per the caller's callback */
3465 * If we had to process any tasks at all, scan again
3466 * in case some of them were in the middle of forking
3467 * children that didn't get processed.
3468 * Not the most efficient way to do it, but it avoids
3469 * having to take callback_mutex in the fork path
3473 if (heap
== &tmp_heap
)
3474 heap_free(&tmp_heap
);
3478 static void cgroup_transfer_one_task(struct task_struct
*task
, void *data
)
3480 struct cgroup
*new_cgroup
= data
;
3482 mutex_lock(&cgroup_mutex
);
3483 cgroup_attach_task(new_cgroup
, task
, false);
3484 mutex_unlock(&cgroup_mutex
);
3488 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3489 * @to: cgroup to which the tasks will be moved
3490 * @from: cgroup in which the tasks currently reside
3492 int cgroup_transfer_tasks(struct cgroup
*to
, struct cgroup
*from
)
3494 return css_scan_tasks(&from
->dummy_css
, NULL
, cgroup_transfer_one_task
,
3499 * Stuff for reading the 'tasks'/'procs' files.
3501 * Reading this file can return large amounts of data if a cgroup has
3502 * *lots* of attached tasks. So it may need several calls to read(),
3503 * but we cannot guarantee that the information we produce is correct
3504 * unless we produce it entirely atomically.
3508 /* which pidlist file are we talking about? */
3509 enum cgroup_filetype
{
3515 * A pidlist is a list of pids that virtually represents the contents of one
3516 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3517 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3520 struct cgroup_pidlist
{
3522 * used to find which pidlist is wanted. doesn't change as long as
3523 * this particular list stays in the list.
3525 struct { enum cgroup_filetype type
; struct pid_namespace
*ns
; } key
;
3528 /* how many elements the above list has */
3530 /* how many files are using the current array */
3532 /* each of these stored in a list by its cgroup */
3533 struct list_head links
;
3534 /* pointer to the cgroup we belong to, for list removal purposes */
3535 struct cgroup
*owner
;
3536 /* protects the other fields */
3537 struct rw_semaphore rwsem
;
3541 * The following two functions "fix" the issue where there are more pids
3542 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3543 * TODO: replace with a kernel-wide solution to this problem
3545 #define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3546 static void *pidlist_allocate(int count
)
3548 if (PIDLIST_TOO_LARGE(count
))
3549 return vmalloc(count
* sizeof(pid_t
));
3551 return kmalloc(count
* sizeof(pid_t
), GFP_KERNEL
);
3553 static void pidlist_free(void *p
)
3555 if (is_vmalloc_addr(p
))
3562 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
3563 * Returns the number of unique elements.
3565 static int pidlist_uniq(pid_t
*list
, int length
)
3570 * we presume the 0th element is unique, so i starts at 1. trivial
3571 * edge cases first; no work needs to be done for either
3573 if (length
== 0 || length
== 1)
3575 /* src and dest walk down the list; dest counts unique elements */
3576 for (src
= 1; src
< length
; src
++) {
3577 /* find next unique element */
3578 while (list
[src
] == list
[src
-1]) {
3583 /* dest always points to where the next unique element goes */
3584 list
[dest
] = list
[src
];
3591 static int cmppid(const void *a
, const void *b
)
3593 return *(pid_t
*)a
- *(pid_t
*)b
;
3597 * find the appropriate pidlist for our purpose (given procs vs tasks)
3598 * returns with the lock on that pidlist already held, and takes care
3599 * of the use count, or returns NULL with no locks held if we're out of
3602 static struct cgroup_pidlist
*cgroup_pidlist_find(struct cgroup
*cgrp
,
3603 enum cgroup_filetype type
)
3605 struct cgroup_pidlist
*l
;
3606 /* don't need task_nsproxy() if we're looking at ourself */
3607 struct pid_namespace
*ns
= task_active_pid_ns(current
);
3610 * We can't drop the pidlist_mutex before taking the l->rwsem in case
3611 * the last ref-holder is trying to remove l from the list at the same
3612 * time. Holding the pidlist_mutex precludes somebody taking whichever
3613 * list we find out from under us - compare release_pid_array().
3615 mutex_lock(&cgrp
->pidlist_mutex
);
3616 list_for_each_entry(l
, &cgrp
->pidlists
, links
) {
3617 if (l
->key
.type
== type
&& l
->key
.ns
== ns
) {
3618 /* make sure l doesn't vanish out from under us */
3619 down_write(&l
->rwsem
);
3620 mutex_unlock(&cgrp
->pidlist_mutex
);
3624 /* entry not found; create a new one */
3625 l
= kzalloc(sizeof(struct cgroup_pidlist
), GFP_KERNEL
);
3627 mutex_unlock(&cgrp
->pidlist_mutex
);
3630 init_rwsem(&l
->rwsem
);
3631 down_write(&l
->rwsem
);
3633 l
->key
.ns
= get_pid_ns(ns
);
3635 list_add(&l
->links
, &cgrp
->pidlists
);
3636 mutex_unlock(&cgrp
->pidlist_mutex
);
3641 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3643 static int pidlist_array_load(struct cgroup
*cgrp
, enum cgroup_filetype type
,
3644 struct cgroup_pidlist
**lp
)
3648 int pid
, n
= 0; /* used for populating the array */
3649 struct css_task_iter it
;
3650 struct task_struct
*tsk
;
3651 struct cgroup_pidlist
*l
;
3654 * If cgroup gets more users after we read count, we won't have
3655 * enough space - tough. This race is indistinguishable to the
3656 * caller from the case that the additional cgroup users didn't
3657 * show up until sometime later on.
3659 length
= cgroup_task_count(cgrp
);
3660 array
= pidlist_allocate(length
);
3663 /* now, populate the array */
3664 css_task_iter_start(&cgrp
->dummy_css
, &it
);
3665 while ((tsk
= css_task_iter_next(&it
))) {
3666 if (unlikely(n
== length
))
3668 /* get tgid or pid for procs or tasks file respectively */
3669 if (type
== CGROUP_FILE_PROCS
)
3670 pid
= task_tgid_vnr(tsk
);
3672 pid
= task_pid_vnr(tsk
);
3673 if (pid
> 0) /* make sure to only use valid results */
3676 css_task_iter_end(&it
);
3678 /* now sort & (if procs) strip out duplicates */
3679 sort(array
, length
, sizeof(pid_t
), cmppid
, NULL
);
3680 if (type
== CGROUP_FILE_PROCS
)
3681 length
= pidlist_uniq(array
, length
);
3682 l
= cgroup_pidlist_find(cgrp
, type
);
3684 pidlist_free(array
);
3687 /* store array, freeing old if necessary - lock already held */
3688 pidlist_free(l
->list
);
3692 up_write(&l
->rwsem
);
3698 * cgroupstats_build - build and fill cgroupstats
3699 * @stats: cgroupstats to fill information into
3700 * @dentry: A dentry entry belonging to the cgroup for which stats have
3703 * Build and fill cgroupstats so that taskstats can export it to user
3706 int cgroupstats_build(struct cgroupstats
*stats
, struct dentry
*dentry
)
3709 struct cgroup
*cgrp
;
3710 struct css_task_iter it
;
3711 struct task_struct
*tsk
;
3714 * Validate dentry by checking the superblock operations,
3715 * and make sure it's a directory.
3717 if (dentry
->d_sb
->s_op
!= &cgroup_ops
||
3718 !S_ISDIR(dentry
->d_inode
->i_mode
))
3722 cgrp
= dentry
->d_fsdata
;
3724 css_task_iter_start(&cgrp
->dummy_css
, &it
);
3725 while ((tsk
= css_task_iter_next(&it
))) {
3726 switch (tsk
->state
) {
3728 stats
->nr_running
++;
3730 case TASK_INTERRUPTIBLE
:
3731 stats
->nr_sleeping
++;
3733 case TASK_UNINTERRUPTIBLE
:
3734 stats
->nr_uninterruptible
++;
3737 stats
->nr_stopped
++;
3740 if (delayacct_is_task_waiting_on_io(tsk
))
3741 stats
->nr_io_wait
++;
3745 css_task_iter_end(&it
);
3753 * seq_file methods for the tasks/procs files. The seq_file position is the
3754 * next pid to display; the seq_file iterator is a pointer to the pid
3755 * in the cgroup->l->list array.
3758 static void *cgroup_pidlist_start(struct seq_file
*s
, loff_t
*pos
)
3761 * Initially we receive a position value that corresponds to
3762 * one more than the last pid shown (or 0 on the first call or
3763 * after a seek to the start). Use a binary-search to find the
3764 * next pid to display, if any
3766 struct cgroup_pidlist
*l
= s
->private;
3767 int index
= 0, pid
= *pos
;
3770 down_read(&l
->rwsem
);
3772 int end
= l
->length
;
3774 while (index
< end
) {
3775 int mid
= (index
+ end
) / 2;
3776 if (l
->list
[mid
] == pid
) {
3779 } else if (l
->list
[mid
] <= pid
)
3785 /* If we're off the end of the array, we're done */
3786 if (index
>= l
->length
)
3788 /* Update the abstract position to be the actual pid that we found */
3789 iter
= l
->list
+ index
;
3794 static void cgroup_pidlist_stop(struct seq_file
*s
, void *v
)
3796 struct cgroup_pidlist
*l
= s
->private;
3800 static void *cgroup_pidlist_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
3802 struct cgroup_pidlist
*l
= s
->private;
3804 pid_t
*end
= l
->list
+ l
->length
;
3806 * Advance to the next pid in the array. If this goes off the
3818 static int cgroup_pidlist_show(struct seq_file
*s
, void *v
)
3820 return seq_printf(s
, "%d\n", *(int *)v
);
3824 * seq_operations functions for iterating on pidlists through seq_file -
3825 * independent of whether it's tasks or procs
3827 static const struct seq_operations cgroup_pidlist_seq_operations
= {
3828 .start
= cgroup_pidlist_start
,
3829 .stop
= cgroup_pidlist_stop
,
3830 .next
= cgroup_pidlist_next
,
3831 .show
= cgroup_pidlist_show
,
3834 static void cgroup_release_pid_array(struct cgroup_pidlist
*l
)
3837 * the case where we're the last user of this particular pidlist will
3838 * have us remove it from the cgroup's list, which entails taking the
3839 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3840 * pidlist_mutex, we have to take pidlist_mutex first.
3842 mutex_lock(&l
->owner
->pidlist_mutex
);
3843 down_write(&l
->rwsem
);
3844 BUG_ON(!l
->use_count
);
3845 if (!--l
->use_count
) {
3846 /* we're the last user if refcount is 0; remove and free */
3847 list_del(&l
->links
);
3848 mutex_unlock(&l
->owner
->pidlist_mutex
);
3849 pidlist_free(l
->list
);
3850 put_pid_ns(l
->key
.ns
);
3851 up_write(&l
->rwsem
);
3855 mutex_unlock(&l
->owner
->pidlist_mutex
);
3856 up_write(&l
->rwsem
);
3859 static int cgroup_pidlist_release(struct inode
*inode
, struct file
*file
)
3861 struct cgroup_pidlist
*l
;
3862 if (!(file
->f_mode
& FMODE_READ
))
3865 * the seq_file will only be initialized if the file was opened for
3866 * reading; hence we check if it's not null only in that case.
3868 l
= ((struct seq_file
*)file
->private_data
)->private;
3869 cgroup_release_pid_array(l
);
3870 return seq_release(inode
, file
);
3873 static const struct file_operations cgroup_pidlist_operations
= {
3875 .llseek
= seq_lseek
,
3876 .write
= cgroup_file_write
,
3877 .release
= cgroup_pidlist_release
,
3881 * The following functions handle opens on a file that displays a pidlist
3882 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3885 /* helper function for the two below it */
3886 static int cgroup_pidlist_open(struct file
*file
, enum cgroup_filetype type
)
3888 struct cgroup
*cgrp
= __d_cgrp(file
->f_dentry
->d_parent
);
3889 struct cgroup_pidlist
*l
;
3892 /* Nothing to do for write-only files */
3893 if (!(file
->f_mode
& FMODE_READ
))
3896 /* have the array populated */
3897 retval
= pidlist_array_load(cgrp
, type
, &l
);
3900 /* configure file information */
3901 file
->f_op
= &cgroup_pidlist_operations
;
3903 retval
= seq_open(file
, &cgroup_pidlist_seq_operations
);
3905 cgroup_release_pid_array(l
);
3908 ((struct seq_file
*)file
->private_data
)->private = l
;
3911 static int cgroup_tasks_open(struct inode
*unused
, struct file
*file
)
3913 return cgroup_pidlist_open(file
, CGROUP_FILE_TASKS
);
3915 static int cgroup_procs_open(struct inode
*unused
, struct file
*file
)
3917 return cgroup_pidlist_open(file
, CGROUP_FILE_PROCS
);
3920 static u64
cgroup_read_notify_on_release(struct cgroup_subsys_state
*css
,
3923 return notify_on_release(css
->cgroup
);
3926 static int cgroup_write_notify_on_release(struct cgroup_subsys_state
*css
,
3927 struct cftype
*cft
, u64 val
)
3929 clear_bit(CGRP_RELEASABLE
, &css
->cgroup
->flags
);
3931 set_bit(CGRP_NOTIFY_ON_RELEASE
, &css
->cgroup
->flags
);
3933 clear_bit(CGRP_NOTIFY_ON_RELEASE
, &css
->cgroup
->flags
);
3938 * When dput() is called asynchronously, if umount has been done and
3939 * then deactivate_super() in cgroup_free_fn() kills the superblock,
3940 * there's a small window that vfs will see the root dentry with non-zero
3941 * refcnt and trigger BUG().
3943 * That's why we hold a reference before dput() and drop it right after.
3945 static void cgroup_dput(struct cgroup
*cgrp
)
3947 struct super_block
*sb
= cgrp
->root
->sb
;
3949 atomic_inc(&sb
->s_active
);
3951 deactivate_super(sb
);
3955 * Unregister event and free resources.
3957 * Gets called from workqueue.
3959 static void cgroup_event_remove(struct work_struct
*work
)
3961 struct cgroup_event
*event
= container_of(work
, struct cgroup_event
,
3963 struct cgroup_subsys_state
*css
= event
->css
;
3965 remove_wait_queue(event
->wqh
, &event
->wait
);
3967 event
->cft
->unregister_event(css
, event
->cft
, event
->eventfd
);
3969 /* Notify userspace the event is going away. */
3970 eventfd_signal(event
->eventfd
, 1);
3972 eventfd_ctx_put(event
->eventfd
);
3978 * Gets called on POLLHUP on eventfd when user closes it.
3980 * Called with wqh->lock held and interrupts disabled.
3982 static int cgroup_event_wake(wait_queue_t
*wait
, unsigned mode
,
3983 int sync
, void *key
)
3985 struct cgroup_event
*event
= container_of(wait
,
3986 struct cgroup_event
, wait
);
3987 struct cgroup
*cgrp
= event
->css
->cgroup
;
3988 unsigned long flags
= (unsigned long)key
;
3990 if (flags
& POLLHUP
) {
3992 * If the event has been detached at cgroup removal, we
3993 * can simply return knowing the other side will cleanup
3996 * We can't race against event freeing since the other
3997 * side will require wqh->lock via remove_wait_queue(),
4000 spin_lock(&cgrp
->event_list_lock
);
4001 if (!list_empty(&event
->list
)) {
4002 list_del_init(&event
->list
);
4004 * We are in atomic context, but cgroup_event_remove()
4005 * may sleep, so we have to call it in workqueue.
4007 schedule_work(&event
->remove
);
4009 spin_unlock(&cgrp
->event_list_lock
);
4015 static void cgroup_event_ptable_queue_proc(struct file
*file
,
4016 wait_queue_head_t
*wqh
, poll_table
*pt
)
4018 struct cgroup_event
*event
= container_of(pt
,
4019 struct cgroup_event
, pt
);
4022 add_wait_queue(wqh
, &event
->wait
);
4026 * Parse input and register new cgroup event handler.
4028 * Input must be in format '<event_fd> <control_fd> <args>'.
4029 * Interpretation of args is defined by control file implementation.
4031 static int cgroup_write_event_control(struct cgroup_subsys_state
*dummy_css
,
4032 struct cftype
*cft
, const char *buffer
)
4034 struct cgroup
*cgrp
= dummy_css
->cgroup
;
4035 struct cgroup_event
*event
;
4036 struct cgroup_subsys_state
*cfile_css
;
4037 unsigned int efd
, cfd
;
4043 efd
= simple_strtoul(buffer
, &endp
, 10);
4048 cfd
= simple_strtoul(buffer
, &endp
, 10);
4049 if ((*endp
!= ' ') && (*endp
!= '\0'))
4053 event
= kzalloc(sizeof(*event
), GFP_KERNEL
);
4057 INIT_LIST_HEAD(&event
->list
);
4058 init_poll_funcptr(&event
->pt
, cgroup_event_ptable_queue_proc
);
4059 init_waitqueue_func_entry(&event
->wait
, cgroup_event_wake
);
4060 INIT_WORK(&event
->remove
, cgroup_event_remove
);
4068 event
->eventfd
= eventfd_ctx_fileget(efile
.file
);
4069 if (IS_ERR(event
->eventfd
)) {
4070 ret
= PTR_ERR(event
->eventfd
);
4077 goto out_put_eventfd
;
4080 /* the process need read permission on control file */
4081 /* AV: shouldn't we check that it's been opened for read instead? */
4082 ret
= inode_permission(file_inode(cfile
.file
), MAY_READ
);
4086 event
->cft
= __file_cft(cfile
.file
);
4087 if (IS_ERR(event
->cft
)) {
4088 ret
= PTR_ERR(event
->cft
);
4092 if (!event
->cft
->ss
) {
4098 * Determine the css of @cfile, verify it belongs to the same
4099 * cgroup as cgroup.event_control, and associate @event with it.
4100 * Remaining events are automatically removed on cgroup destruction
4101 * but the removal is asynchronous, so take an extra ref.
4106 event
->css
= cgroup_css(cgrp
, event
->cft
->ss
);
4107 cfile_css
= css_from_dir(cfile
.file
->f_dentry
->d_parent
, event
->cft
->ss
);
4108 if (event
->css
&& event
->css
== cfile_css
&& css_tryget(event
->css
))
4115 if (!event
->cft
->register_event
|| !event
->cft
->unregister_event
) {
4120 ret
= event
->cft
->register_event(event
->css
, event
->cft
,
4121 event
->eventfd
, buffer
);
4125 efile
.file
->f_op
->poll(efile
.file
, &event
->pt
);
4127 spin_lock(&cgrp
->event_list_lock
);
4128 list_add(&event
->list
, &cgrp
->event_list
);
4129 spin_unlock(&cgrp
->event_list_lock
);
4137 css_put(event
->css
);
4141 eventfd_ctx_put(event
->eventfd
);
4150 static u64
cgroup_clone_children_read(struct cgroup_subsys_state
*css
,
4153 return test_bit(CGRP_CPUSET_CLONE_CHILDREN
, &css
->cgroup
->flags
);
4156 static int cgroup_clone_children_write(struct cgroup_subsys_state
*css
,
4157 struct cftype
*cft
, u64 val
)
4160 set_bit(CGRP_CPUSET_CLONE_CHILDREN
, &css
->cgroup
->flags
);
4162 clear_bit(CGRP_CPUSET_CLONE_CHILDREN
, &css
->cgroup
->flags
);
4166 static struct cftype cgroup_base_files
[] = {
4168 .name
= "cgroup.procs",
4169 .open
= cgroup_procs_open
,
4170 .write_u64
= cgroup_procs_write
,
4171 .release
= cgroup_pidlist_release
,
4172 .mode
= S_IRUGO
| S_IWUSR
,
4175 .name
= "cgroup.event_control",
4176 .write_string
= cgroup_write_event_control
,
4180 .name
= "cgroup.clone_children",
4181 .flags
= CFTYPE_INSANE
,
4182 .read_u64
= cgroup_clone_children_read
,
4183 .write_u64
= cgroup_clone_children_write
,
4186 .name
= "cgroup.sane_behavior",
4187 .flags
= CFTYPE_ONLY_ON_ROOT
,
4188 .read_seq_string
= cgroup_sane_behavior_show
,
4192 * Historical crazy stuff. These don't have "cgroup." prefix and
4193 * don't exist if sane_behavior. If you're depending on these, be
4194 * prepared to be burned.
4198 .flags
= CFTYPE_INSANE
, /* use "procs" instead */
4199 .open
= cgroup_tasks_open
,
4200 .write_u64
= cgroup_tasks_write
,
4201 .release
= cgroup_pidlist_release
,
4202 .mode
= S_IRUGO
| S_IWUSR
,
4205 .name
= "notify_on_release",
4206 .flags
= CFTYPE_INSANE
,
4207 .read_u64
= cgroup_read_notify_on_release
,
4208 .write_u64
= cgroup_write_notify_on_release
,
4211 .name
= "release_agent",
4212 .flags
= CFTYPE_INSANE
| CFTYPE_ONLY_ON_ROOT
,
4213 .read_seq_string
= cgroup_release_agent_show
,
4214 .write_string
= cgroup_release_agent_write
,
4215 .max_write_len
= PATH_MAX
,
4221 * cgroup_populate_dir - create subsys files in a cgroup directory
4222 * @cgrp: target cgroup
4223 * @subsys_mask: mask of the subsystem ids whose files should be added
4225 * On failure, no file is added.
4227 static int cgroup_populate_dir(struct cgroup
*cgrp
, unsigned long subsys_mask
)
4229 struct cgroup_subsys
*ss
;
4232 /* process cftsets of each subsystem */
4233 for_each_subsys(ss
, i
) {
4234 struct cftype_set
*set
;
4236 if (!test_bit(i
, &subsys_mask
))
4239 list_for_each_entry(set
, &ss
->cftsets
, node
) {
4240 ret
= cgroup_addrm_files(cgrp
, set
->cfts
, true);
4246 /* This cgroup is ready now */
4247 for_each_root_subsys(cgrp
->root
, ss
) {
4248 struct cgroup_subsys_state
*css
= cgroup_css(cgrp
, ss
);
4249 struct css_id
*id
= rcu_dereference_protected(css
->id
, true);
4252 * Update id->css pointer and make this css visible from
4253 * CSS ID functions. This pointer will be dereferened
4254 * from RCU-read-side without locks.
4257 rcu_assign_pointer(id
->css
, css
);
4262 cgroup_clear_dir(cgrp
, subsys_mask
);
4267 * css destruction is four-stage process.
4269 * 1. Destruction starts. Killing of the percpu_ref is initiated.
4270 * Implemented in kill_css().
4272 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
4273 * and thus css_tryget() is guaranteed to fail, the css can be offlined
4274 * by invoking offline_css(). After offlining, the base ref is put.
4275 * Implemented in css_killed_work_fn().
4277 * 3. When the percpu_ref reaches zero, the only possible remaining
4278 * accessors are inside RCU read sections. css_release() schedules the
4281 * 4. After the grace period, the css can be freed. Implemented in
4282 * css_free_work_fn().
4284 * It is actually hairier because both step 2 and 4 require process context
4285 * and thus involve punting to css->destroy_work adding two additional
4286 * steps to the already complex sequence.
4288 static void css_free_work_fn(struct work_struct
*work
)
4290 struct cgroup_subsys_state
*css
=
4291 container_of(work
, struct cgroup_subsys_state
, destroy_work
);
4292 struct cgroup
*cgrp
= css
->cgroup
;
4295 css_put(css
->parent
);
4297 css
->ss
->css_free(css
);
4301 static void css_free_rcu_fn(struct rcu_head
*rcu_head
)
4303 struct cgroup_subsys_state
*css
=
4304 container_of(rcu_head
, struct cgroup_subsys_state
, rcu_head
);
4307 * css holds an extra ref to @cgrp->dentry which is put on the last
4308 * css_put(). dput() requires process context which we don't have.
4310 INIT_WORK(&css
->destroy_work
, css_free_work_fn
);
4311 schedule_work(&css
->destroy_work
);
4314 static void css_release(struct percpu_ref
*ref
)
4316 struct cgroup_subsys_state
*css
=
4317 container_of(ref
, struct cgroup_subsys_state
, refcnt
);
4319 call_rcu(&css
->rcu_head
, css_free_rcu_fn
);
4322 static void init_css(struct cgroup_subsys_state
*css
, struct cgroup_subsys
*ss
,
4323 struct cgroup
*cgrp
)
4331 css
->parent
= cgroup_css(cgrp
->parent
, ss
);
4333 css
->flags
|= CSS_ROOT
;
4335 BUG_ON(cgroup_css(cgrp
, ss
));
4338 /* invoke ->css_online() on a new CSS and mark it online if successful */
4339 static int online_css(struct cgroup_subsys_state
*css
)
4341 struct cgroup_subsys
*ss
= css
->ss
;
4344 lockdep_assert_held(&cgroup_mutex
);
4347 ret
= ss
->css_online(css
);
4349 css
->flags
|= CSS_ONLINE
;
4350 css
->cgroup
->nr_css
++;
4351 rcu_assign_pointer(css
->cgroup
->subsys
[ss
->subsys_id
], css
);
4356 /* if the CSS is online, invoke ->css_offline() on it and mark it offline */
4357 static void offline_css(struct cgroup_subsys_state
*css
)
4359 struct cgroup_subsys
*ss
= css
->ss
;
4361 lockdep_assert_held(&cgroup_mutex
);
4363 if (!(css
->flags
& CSS_ONLINE
))
4366 if (ss
->css_offline
)
4367 ss
->css_offline(css
);
4369 css
->flags
&= ~CSS_ONLINE
;
4370 css
->cgroup
->nr_css
--;
4371 RCU_INIT_POINTER(css
->cgroup
->subsys
[ss
->subsys_id
], css
);
4375 * cgroup_create - create a cgroup
4376 * @parent: cgroup that will be parent of the new cgroup
4377 * @dentry: dentry of the new cgroup
4378 * @mode: mode to set on new inode
4380 * Must be called with the mutex on the parent inode held
4382 static long cgroup_create(struct cgroup
*parent
, struct dentry
*dentry
,
4385 struct cgroup_subsys_state
*css_ar
[CGROUP_SUBSYS_COUNT
] = { };
4386 struct cgroup
*cgrp
;
4387 struct cgroup_name
*name
;
4388 struct cgroupfs_root
*root
= parent
->root
;
4390 struct cgroup_subsys
*ss
;
4391 struct super_block
*sb
= root
->sb
;
4393 /* allocate the cgroup and its ID, 0 is reserved for the root */
4394 cgrp
= kzalloc(sizeof(*cgrp
), GFP_KERNEL
);
4398 name
= cgroup_alloc_name(dentry
);
4401 rcu_assign_pointer(cgrp
->name
, name
);
4404 * Temporarily set the pointer to NULL, so idr_find() won't return
4405 * a half-baked cgroup.
4407 cgrp
->id
= idr_alloc(&root
->cgroup_idr
, NULL
, 1, 0, GFP_KERNEL
);
4412 * Only live parents can have children. Note that the liveliness
4413 * check isn't strictly necessary because cgroup_mkdir() and
4414 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4415 * anyway so that locking is contained inside cgroup proper and we
4416 * don't get nasty surprises if we ever grow another caller.
4418 if (!cgroup_lock_live_group(parent
)) {
4423 /* Grab a reference on the superblock so the hierarchy doesn't
4424 * get deleted on unmount if there are child cgroups. This
4425 * can be done outside cgroup_mutex, since the sb can't
4426 * disappear while someone has an open control file on the
4428 atomic_inc(&sb
->s_active
);
4430 init_cgroup_housekeeping(cgrp
);
4432 dentry
->d_fsdata
= cgrp
;
4433 cgrp
->dentry
= dentry
;
4435 cgrp
->parent
= parent
;
4436 cgrp
->dummy_css
.parent
= &parent
->dummy_css
;
4437 cgrp
->root
= parent
->root
;
4439 if (notify_on_release(parent
))
4440 set_bit(CGRP_NOTIFY_ON_RELEASE
, &cgrp
->flags
);
4442 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN
, &parent
->flags
))
4443 set_bit(CGRP_CPUSET_CLONE_CHILDREN
, &cgrp
->flags
);
4445 for_each_root_subsys(root
, ss
) {
4446 struct cgroup_subsys_state
*css
;
4448 css
= ss
->css_alloc(cgroup_css(parent
, ss
));
4453 css_ar
[ss
->subsys_id
] = css
;
4455 err
= percpu_ref_init(&css
->refcnt
, css_release
);
4459 init_css(css
, ss
, cgrp
);
4462 err
= alloc_css_id(css
);
4469 * Create directory. cgroup_create_file() returns with the new
4470 * directory locked on success so that it can be populated without
4471 * dropping cgroup_mutex.
4473 err
= cgroup_create_file(dentry
, S_IFDIR
| mode
, sb
);
4476 lockdep_assert_held(&dentry
->d_inode
->i_mutex
);
4478 cgrp
->serial_nr
= cgroup_serial_nr_next
++;
4480 /* allocation complete, commit to creation */
4481 list_add_tail_rcu(&cgrp
->sibling
, &cgrp
->parent
->children
);
4482 root
->number_of_cgroups
++;
4484 /* each css holds a ref to the cgroup's dentry and the parent css */
4485 for_each_root_subsys(root
, ss
) {
4486 struct cgroup_subsys_state
*css
= css_ar
[ss
->subsys_id
];
4489 css_get(css
->parent
);
4492 /* hold a ref to the parent's dentry */
4493 dget(parent
->dentry
);
4495 /* creation succeeded, notify subsystems */
4496 for_each_root_subsys(root
, ss
) {
4497 struct cgroup_subsys_state
*css
= css_ar
[ss
->subsys_id
];
4499 err
= online_css(css
);
4503 if (ss
->broken_hierarchy
&& !ss
->warned_broken_hierarchy
&&
4505 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",
4506 current
->comm
, current
->pid
, ss
->name
);
4507 if (!strcmp(ss
->name
, "memory"))
4508 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4509 ss
->warned_broken_hierarchy
= true;
4513 idr_replace(&root
->cgroup_idr
, cgrp
, cgrp
->id
);
4515 err
= cgroup_addrm_files(cgrp
, cgroup_base_files
, true);
4519 err
= cgroup_populate_dir(cgrp
, root
->subsys_mask
);
4523 mutex_unlock(&cgroup_mutex
);
4524 mutex_unlock(&cgrp
->dentry
->d_inode
->i_mutex
);
4529 for_each_root_subsys(root
, ss
) {
4530 struct cgroup_subsys_state
*css
= css_ar
[ss
->subsys_id
];
4533 percpu_ref_cancel_init(&css
->refcnt
);
4537 mutex_unlock(&cgroup_mutex
);
4538 /* Release the reference count that we took on the superblock */
4539 deactivate_super(sb
);
4541 idr_remove(&root
->cgroup_idr
, cgrp
->id
);
4543 kfree(rcu_dereference_raw(cgrp
->name
));
4549 cgroup_destroy_locked(cgrp
);
4550 mutex_unlock(&cgroup_mutex
);
4551 mutex_unlock(&dentry
->d_inode
->i_mutex
);
4555 static int cgroup_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
4557 struct cgroup
*c_parent
= dentry
->d_parent
->d_fsdata
;
4559 /* the vfs holds inode->i_mutex already */
4560 return cgroup_create(c_parent
, dentry
, mode
| S_IFDIR
);
4564 * This is called when the refcnt of a css is confirmed to be killed.
4565 * css_tryget() is now guaranteed to fail.
4567 static void css_killed_work_fn(struct work_struct
*work
)
4569 struct cgroup_subsys_state
*css
=
4570 container_of(work
, struct cgroup_subsys_state
, destroy_work
);
4571 struct cgroup
*cgrp
= css
->cgroup
;
4573 mutex_lock(&cgroup_mutex
);
4576 * css_tryget() is guaranteed to fail now. Tell subsystems to
4577 * initate destruction.
4582 * If @cgrp is marked dead, it's waiting for refs of all css's to
4583 * be disabled before proceeding to the second phase of cgroup
4584 * destruction. If we are the last one, kick it off.
4586 if (!cgrp
->nr_css
&& cgroup_is_dead(cgrp
))
4587 cgroup_destroy_css_killed(cgrp
);
4589 mutex_unlock(&cgroup_mutex
);
4592 * Put the css refs from kill_css(). Each css holds an extra
4593 * reference to the cgroup's dentry and cgroup removal proceeds
4594 * regardless of css refs. On the last put of each css, whenever
4595 * that may be, the extra dentry ref is put so that dentry
4596 * destruction happens only after all css's are released.
4601 /* css kill confirmation processing requires process context, bounce */
4602 static void css_killed_ref_fn(struct percpu_ref
*ref
)
4604 struct cgroup_subsys_state
*css
=
4605 container_of(ref
, struct cgroup_subsys_state
, refcnt
);
4607 INIT_WORK(&css
->destroy_work
, css_killed_work_fn
);
4608 schedule_work(&css
->destroy_work
);
4612 * kill_css - destroy a css
4613 * @css: css to destroy
4615 * This function initiates destruction of @css by removing cgroup interface
4616 * files and putting its base reference. ->css_offline() will be invoked
4617 * asynchronously once css_tryget() is guaranteed to fail and when the
4618 * reference count reaches zero, @css will be released.
4620 static void kill_css(struct cgroup_subsys_state
*css
)
4622 cgroup_clear_dir(css
->cgroup
, 1 << css
->ss
->subsys_id
);
4625 * Killing would put the base ref, but we need to keep it alive
4626 * until after ->css_offline().
4631 * cgroup core guarantees that, by the time ->css_offline() is
4632 * invoked, no new css reference will be given out via
4633 * css_tryget(). We can't simply call percpu_ref_kill() and
4634 * proceed to offlining css's because percpu_ref_kill() doesn't
4635 * guarantee that the ref is seen as killed on all CPUs on return.
4637 * Use percpu_ref_kill_and_confirm() to get notifications as each
4638 * css is confirmed to be seen as killed on all CPUs.
4640 percpu_ref_kill_and_confirm(&css
->refcnt
, css_killed_ref_fn
);
4644 * cgroup_destroy_locked - the first stage of cgroup destruction
4645 * @cgrp: cgroup to be destroyed
4647 * css's make use of percpu refcnts whose killing latency shouldn't be
4648 * exposed to userland and are RCU protected. Also, cgroup core needs to
4649 * guarantee that css_tryget() won't succeed by the time ->css_offline() is
4650 * invoked. To satisfy all the requirements, destruction is implemented in
4651 * the following two steps.
4653 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4654 * userland visible parts and start killing the percpu refcnts of
4655 * css's. Set up so that the next stage will be kicked off once all
4656 * the percpu refcnts are confirmed to be killed.
4658 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4659 * rest of destruction. Once all cgroup references are gone, the
4660 * cgroup is RCU-freed.
4662 * This function implements s1. After this step, @cgrp is gone as far as
4663 * the userland is concerned and a new cgroup with the same name may be
4664 * created. As cgroup doesn't care about the names internally, this
4665 * doesn't cause any problem.
4667 static int cgroup_destroy_locked(struct cgroup
*cgrp
)
4668 __releases(&cgroup_mutex
) __acquires(&cgroup_mutex
)
4670 struct dentry
*d
= cgrp
->dentry
;
4671 struct cgroup_event
*event
, *tmp
;
4672 struct cgroup_subsys
*ss
;
4673 struct cgroup
*child
;
4676 lockdep_assert_held(&d
->d_inode
->i_mutex
);
4677 lockdep_assert_held(&cgroup_mutex
);
4680 * css_set_lock synchronizes access to ->cset_links and prevents
4681 * @cgrp from being removed while __put_css_set() is in progress.
4683 read_lock(&css_set_lock
);
4684 empty
= list_empty(&cgrp
->cset_links
);
4685 read_unlock(&css_set_lock
);
4690 * Make sure there's no live children. We can't test ->children
4691 * emptiness as dead children linger on it while being destroyed;
4692 * otherwise, "rmdir parent/child parent" may fail with -EBUSY.
4696 list_for_each_entry_rcu(child
, &cgrp
->children
, sibling
) {
4697 empty
= cgroup_is_dead(child
);
4706 * Initiate massacre of all css's. cgroup_destroy_css_killed()
4707 * will be invoked to perform the rest of destruction once the
4708 * percpu refs of all css's are confirmed to be killed.
4710 for_each_root_subsys(cgrp
->root
, ss
)
4711 kill_css(cgroup_css(cgrp
, ss
));
4714 * Mark @cgrp dead. This prevents further task migration and child
4715 * creation by disabling cgroup_lock_live_group(). Note that
4716 * CGRP_DEAD assertion is depended upon by css_next_child() to
4717 * resume iteration after dropping RCU read lock. See
4718 * css_next_child() for details.
4720 set_bit(CGRP_DEAD
, &cgrp
->flags
);
4722 /* CGRP_DEAD is set, remove from ->release_list for the last time */
4723 raw_spin_lock(&release_list_lock
);
4724 if (!list_empty(&cgrp
->release_list
))
4725 list_del_init(&cgrp
->release_list
);
4726 raw_spin_unlock(&release_list_lock
);
4729 * If @cgrp has css's attached, the second stage of cgroup
4730 * destruction is kicked off from css_killed_work_fn() after the
4731 * refs of all attached css's are killed. If @cgrp doesn't have
4732 * any css, we kick it off here.
4735 cgroup_destroy_css_killed(cgrp
);
4738 * Clear the base files and remove @cgrp directory. The removal
4739 * puts the base ref but we aren't quite done with @cgrp yet, so
4742 cgroup_addrm_files(cgrp
, cgroup_base_files
, false);
4744 cgroup_d_remove_dir(d
);
4747 * Unregister events and notify userspace.
4748 * Notify userspace about cgroup removing only after rmdir of cgroup
4749 * directory to avoid race between userspace and kernelspace.
4751 spin_lock(&cgrp
->event_list_lock
);
4752 list_for_each_entry_safe(event
, tmp
, &cgrp
->event_list
, list
) {
4753 list_del_init(&event
->list
);
4754 schedule_work(&event
->remove
);
4756 spin_unlock(&cgrp
->event_list_lock
);
4762 * cgroup_destroy_css_killed - the second step of cgroup destruction
4763 * @work: cgroup->destroy_free_work
4765 * This function is invoked from a work item for a cgroup which is being
4766 * destroyed after all css's are offlined and performs the rest of
4767 * destruction. This is the second step of destruction described in the
4768 * comment above cgroup_destroy_locked().
4770 static void cgroup_destroy_css_killed(struct cgroup
*cgrp
)
4772 struct cgroup
*parent
= cgrp
->parent
;
4773 struct dentry
*d
= cgrp
->dentry
;
4775 lockdep_assert_held(&cgroup_mutex
);
4777 /* delete this cgroup from parent->children */
4778 list_del_rcu(&cgrp
->sibling
);
4781 * We should remove the cgroup object from idr before its grace
4782 * period starts, so we won't be looking up a cgroup while the
4783 * cgroup is being freed.
4785 idr_remove(&cgrp
->root
->cgroup_idr
, cgrp
->id
);
4790 set_bit(CGRP_RELEASABLE
, &parent
->flags
);
4791 check_for_release(parent
);
4794 static int cgroup_rmdir(struct inode
*unused_dir
, struct dentry
*dentry
)
4798 mutex_lock(&cgroup_mutex
);
4799 ret
= cgroup_destroy_locked(dentry
->d_fsdata
);
4800 mutex_unlock(&cgroup_mutex
);
4805 static void __init_or_module
cgroup_init_cftsets(struct cgroup_subsys
*ss
)
4807 INIT_LIST_HEAD(&ss
->cftsets
);
4810 * base_cftset is embedded in subsys itself, no need to worry about
4813 if (ss
->base_cftypes
) {
4816 for (cft
= ss
->base_cftypes
; cft
->name
[0] != '\0'; cft
++)
4819 ss
->base_cftset
.cfts
= ss
->base_cftypes
;
4820 list_add_tail(&ss
->base_cftset
.node
, &ss
->cftsets
);
4824 static void __init
cgroup_init_subsys(struct cgroup_subsys
*ss
)
4826 struct cgroup_subsys_state
*css
;
4828 printk(KERN_INFO
"Initializing cgroup subsys %s\n", ss
->name
);
4830 mutex_lock(&cgroup_mutex
);
4832 /* init base cftset */
4833 cgroup_init_cftsets(ss
);
4835 /* Create the top cgroup state for this subsystem */
4836 list_add(&ss
->sibling
, &cgroup_dummy_root
.subsys_list
);
4837 ss
->root
= &cgroup_dummy_root
;
4838 css
= ss
->css_alloc(cgroup_css(cgroup_dummy_top
, ss
));
4839 /* We don't handle early failures gracefully */
4840 BUG_ON(IS_ERR(css
));
4841 init_css(css
, ss
, cgroup_dummy_top
);
4843 /* Update the init_css_set to contain a subsys
4844 * pointer to this state - since the subsystem is
4845 * newly registered, all tasks and hence the
4846 * init_css_set is in the subsystem's top cgroup. */
4847 init_css_set
.subsys
[ss
->subsys_id
] = css
;
4849 need_forkexit_callback
|= ss
->fork
|| ss
->exit
;
4851 /* At system boot, before all subsystems have been
4852 * registered, no tasks have been forked, so we don't
4853 * need to invoke fork callbacks here. */
4854 BUG_ON(!list_empty(&init_task
.tasks
));
4856 BUG_ON(online_css(css
));
4858 mutex_unlock(&cgroup_mutex
);
4860 /* this function shouldn't be used with modular subsystems, since they
4861 * need to register a subsys_id, among other things */
4866 * cgroup_load_subsys: load and register a modular subsystem at runtime
4867 * @ss: the subsystem to load
4869 * This function should be called in a modular subsystem's initcall. If the
4870 * subsystem is built as a module, it will be assigned a new subsys_id and set
4871 * up for use. If the subsystem is built-in anyway, work is delegated to the
4872 * simpler cgroup_init_subsys.
4874 int __init_or_module
cgroup_load_subsys(struct cgroup_subsys
*ss
)
4876 struct cgroup_subsys_state
*css
;
4878 struct hlist_node
*tmp
;
4879 struct css_set
*cset
;
4882 /* check name and function validity */
4883 if (ss
->name
== NULL
|| strlen(ss
->name
) > MAX_CGROUP_TYPE_NAMELEN
||
4884 ss
->css_alloc
== NULL
|| ss
->css_free
== NULL
)
4888 * we don't support callbacks in modular subsystems. this check is
4889 * before the ss->module check for consistency; a subsystem that could
4890 * be a module should still have no callbacks even if the user isn't
4891 * compiling it as one.
4893 if (ss
->fork
|| ss
->exit
)
4897 * an optionally modular subsystem is built-in: we want to do nothing,
4898 * since cgroup_init_subsys will have already taken care of it.
4900 if (ss
->module
== NULL
) {
4901 /* a sanity check */
4902 BUG_ON(cgroup_subsys
[ss
->subsys_id
] != ss
);
4906 /* init base cftset */
4907 cgroup_init_cftsets(ss
);
4909 mutex_lock(&cgroup_mutex
);
4910 cgroup_subsys
[ss
->subsys_id
] = ss
;
4913 * no ss->css_alloc seems to need anything important in the ss
4914 * struct, so this can happen first (i.e. before the dummy root
4917 css
= ss
->css_alloc(cgroup_css(cgroup_dummy_top
, ss
));
4919 /* failure case - need to deassign the cgroup_subsys[] slot. */
4920 cgroup_subsys
[ss
->subsys_id
] = NULL
;
4921 mutex_unlock(&cgroup_mutex
);
4922 return PTR_ERR(css
);
4925 list_add(&ss
->sibling
, &cgroup_dummy_root
.subsys_list
);
4926 ss
->root
= &cgroup_dummy_root
;
4928 /* our new subsystem will be attached to the dummy hierarchy. */
4929 init_css(css
, ss
, cgroup_dummy_top
);
4930 /* init_idr must be after init_css() because it sets css->id. */
4932 ret
= cgroup_init_idr(ss
, css
);
4938 * Now we need to entangle the css into the existing css_sets. unlike
4939 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4940 * will need a new pointer to it; done by iterating the css_set_table.
4941 * furthermore, modifying the existing css_sets will corrupt the hash
4942 * table state, so each changed css_set will need its hash recomputed.
4943 * this is all done under the css_set_lock.
4945 write_lock(&css_set_lock
);
4946 hash_for_each_safe(css_set_table
, i
, tmp
, cset
, hlist
) {
4947 /* skip entries that we already rehashed */
4948 if (cset
->subsys
[ss
->subsys_id
])
4950 /* remove existing entry */
4951 hash_del(&cset
->hlist
);
4953 cset
->subsys
[ss
->subsys_id
] = css
;
4954 /* recompute hash and restore entry */
4955 key
= css_set_hash(cset
->subsys
);
4956 hash_add(css_set_table
, &cset
->hlist
, key
);
4958 write_unlock(&css_set_lock
);
4960 ret
= online_css(css
);
4965 mutex_unlock(&cgroup_mutex
);
4969 mutex_unlock(&cgroup_mutex
);
4970 /* @ss can't be mounted here as try_module_get() would fail */
4971 cgroup_unload_subsys(ss
);
4974 EXPORT_SYMBOL_GPL(cgroup_load_subsys
);
4977 * cgroup_unload_subsys: unload a modular subsystem
4978 * @ss: the subsystem to unload
4980 * This function should be called in a modular subsystem's exitcall. When this
4981 * function is invoked, the refcount on the subsystem's module will be 0, so
4982 * the subsystem will not be attached to any hierarchy.
4984 void cgroup_unload_subsys(struct cgroup_subsys
*ss
)
4986 struct cgrp_cset_link
*link
;
4988 BUG_ON(ss
->module
== NULL
);
4991 * we shouldn't be called if the subsystem is in use, and the use of
4992 * try_module_get() in rebind_subsystems() should ensure that it
4993 * doesn't start being used while we're killing it off.
4995 BUG_ON(ss
->root
!= &cgroup_dummy_root
);
4997 mutex_lock(&cgroup_mutex
);
4999 offline_css(cgroup_css(cgroup_dummy_top
, ss
));
5002 idr_destroy(&ss
->idr
);
5004 /* deassign the subsys_id */
5005 cgroup_subsys
[ss
->subsys_id
] = NULL
;
5007 /* remove subsystem from the dummy root's list of subsystems */
5008 list_del_init(&ss
->sibling
);
5011 * disentangle the css from all css_sets attached to the dummy
5012 * top. as in loading, we need to pay our respects to the hashtable
5015 write_lock(&css_set_lock
);
5016 list_for_each_entry(link
, &cgroup_dummy_top
->cset_links
, cset_link
) {
5017 struct css_set
*cset
= link
->cset
;
5020 hash_del(&cset
->hlist
);
5021 cset
->subsys
[ss
->subsys_id
] = NULL
;
5022 key
= css_set_hash(cset
->subsys
);
5023 hash_add(css_set_table
, &cset
->hlist
, key
);
5025 write_unlock(&css_set_lock
);
5028 * remove subsystem's css from the cgroup_dummy_top and free it -
5029 * need to free before marking as null because ss->css_free needs
5030 * the cgrp->subsys pointer to find their state. note that this
5031 * also takes care of freeing the css_id.
5033 ss
->css_free(cgroup_css(cgroup_dummy_top
, ss
));
5034 RCU_INIT_POINTER(cgroup_dummy_top
->subsys
[ss
->subsys_id
], NULL
);
5036 mutex_unlock(&cgroup_mutex
);
5038 EXPORT_SYMBOL_GPL(cgroup_unload_subsys
);
5041 * cgroup_init_early - cgroup initialization at system boot
5043 * Initialize cgroups at system boot, and initialize any
5044 * subsystems that request early init.
5046 int __init
cgroup_init_early(void)
5048 struct cgroup_subsys
*ss
;
5051 atomic_set(&init_css_set
.refcount
, 1);
5052 INIT_LIST_HEAD(&init_css_set
.cgrp_links
);
5053 INIT_LIST_HEAD(&init_css_set
.tasks
);
5054 INIT_HLIST_NODE(&init_css_set
.hlist
);
5056 init_cgroup_root(&cgroup_dummy_root
);
5057 cgroup_root_count
= 1;
5058 RCU_INIT_POINTER(init_task
.cgroups
, &init_css_set
);
5060 init_cgrp_cset_link
.cset
= &init_css_set
;
5061 init_cgrp_cset_link
.cgrp
= cgroup_dummy_top
;
5062 list_add(&init_cgrp_cset_link
.cset_link
, &cgroup_dummy_top
->cset_links
);
5063 list_add(&init_cgrp_cset_link
.cgrp_link
, &init_css_set
.cgrp_links
);
5065 /* at bootup time, we don't worry about modular subsystems */
5066 for_each_builtin_subsys(ss
, i
) {
5068 BUG_ON(strlen(ss
->name
) > MAX_CGROUP_TYPE_NAMELEN
);
5069 BUG_ON(!ss
->css_alloc
);
5070 BUG_ON(!ss
->css_free
);
5071 if (ss
->subsys_id
!= i
) {
5072 printk(KERN_ERR
"cgroup: Subsys %s id == %d\n",
5073 ss
->name
, ss
->subsys_id
);
5078 cgroup_init_subsys(ss
);
5084 * cgroup_init - cgroup initialization
5086 * Register cgroup filesystem and /proc file, and initialize
5087 * any subsystems that didn't request early init.
5089 int __init
cgroup_init(void)
5091 struct cgroup_subsys
*ss
;
5095 err
= bdi_init(&cgroup_backing_dev_info
);
5099 for_each_builtin_subsys(ss
, i
) {
5100 if (!ss
->early_init
)
5101 cgroup_init_subsys(ss
);
5103 cgroup_init_idr(ss
, init_css_set
.subsys
[ss
->subsys_id
]);
5106 /* allocate id for the dummy hierarchy */
5107 mutex_lock(&cgroup_mutex
);
5108 mutex_lock(&cgroup_root_mutex
);
5110 /* Add init_css_set to the hash table */
5111 key
= css_set_hash(init_css_set
.subsys
);
5112 hash_add(css_set_table
, &init_css_set
.hlist
, key
);
5114 BUG_ON(cgroup_init_root_id(&cgroup_dummy_root
, 0, 1));
5116 err
= idr_alloc(&cgroup_dummy_root
.cgroup_idr
, cgroup_dummy_top
,
5120 mutex_unlock(&cgroup_root_mutex
);
5121 mutex_unlock(&cgroup_mutex
);
5123 cgroup_kobj
= kobject_create_and_add("cgroup", fs_kobj
);
5129 err
= register_filesystem(&cgroup_fs_type
);
5131 kobject_put(cgroup_kobj
);
5135 proc_create("cgroups", 0, NULL
, &proc_cgroupstats_operations
);
5139 bdi_destroy(&cgroup_backing_dev_info
);
5145 * proc_cgroup_show()
5146 * - Print task's cgroup paths into seq_file, one line for each hierarchy
5147 * - Used for /proc/<pid>/cgroup.
5148 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
5149 * doesn't really matter if tsk->cgroup changes after we read it,
5150 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
5151 * anyway. No need to check that tsk->cgroup != NULL, thanks to
5152 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
5153 * cgroup to top_cgroup.
5156 /* TODO: Use a proper seq_file iterator */
5157 int proc_cgroup_show(struct seq_file
*m
, void *v
)
5160 struct task_struct
*tsk
;
5163 struct cgroupfs_root
*root
;
5166 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
5172 tsk
= get_pid_task(pid
, PIDTYPE_PID
);
5178 mutex_lock(&cgroup_mutex
);
5180 for_each_active_root(root
) {
5181 struct cgroup_subsys
*ss
;
5182 struct cgroup
*cgrp
;
5185 seq_printf(m
, "%d:", root
->hierarchy_id
);
5186 for_each_root_subsys(root
, ss
)
5187 seq_printf(m
, "%s%s", count
++ ? "," : "", ss
->name
);
5188 if (strlen(root
->name
))
5189 seq_printf(m
, "%sname=%s", count
? "," : "",
5192 cgrp
= task_cgroup_from_root(tsk
, root
);
5193 retval
= cgroup_path(cgrp
, buf
, PAGE_SIZE
);
5201 mutex_unlock(&cgroup_mutex
);
5202 put_task_struct(tsk
);
5209 /* Display information about each subsystem and each hierarchy */
5210 static int proc_cgroupstats_show(struct seq_file
*m
, void *v
)
5212 struct cgroup_subsys
*ss
;
5215 seq_puts(m
, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
5217 * ideally we don't want subsystems moving around while we do this.
5218 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
5219 * subsys/hierarchy state.
5221 mutex_lock(&cgroup_mutex
);
5223 for_each_subsys(ss
, i
)
5224 seq_printf(m
, "%s\t%d\t%d\t%d\n",
5225 ss
->name
, ss
->root
->hierarchy_id
,
5226 ss
->root
->number_of_cgroups
, !ss
->disabled
);
5228 mutex_unlock(&cgroup_mutex
);
5232 static int cgroupstats_open(struct inode
*inode
, struct file
*file
)
5234 return single_open(file
, proc_cgroupstats_show
, NULL
);
5237 static const struct file_operations proc_cgroupstats_operations
= {
5238 .open
= cgroupstats_open
,
5240 .llseek
= seq_lseek
,
5241 .release
= single_release
,
5245 * cgroup_fork - attach newly forked task to its parents cgroup.
5246 * @child: pointer to task_struct of forking parent process.
5248 * Description: A task inherits its parent's cgroup at fork().
5250 * A pointer to the shared css_set was automatically copied in
5251 * fork.c by dup_task_struct(). However, we ignore that copy, since
5252 * it was not made under the protection of RCU or cgroup_mutex, so
5253 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
5254 * have already changed current->cgroups, allowing the previously
5255 * referenced cgroup group to be removed and freed.
5257 * At the point that cgroup_fork() is called, 'current' is the parent
5258 * task, and the passed argument 'child' points to the child task.
5260 void cgroup_fork(struct task_struct
*child
)
5263 get_css_set(task_css_set(current
));
5264 child
->cgroups
= current
->cgroups
;
5265 task_unlock(current
);
5266 INIT_LIST_HEAD(&child
->cg_list
);
5270 * cgroup_post_fork - called on a new task after adding it to the task list
5271 * @child: the task in question
5273 * Adds the task to the list running through its css_set if necessary and
5274 * call the subsystem fork() callbacks. Has to be after the task is
5275 * visible on the task list in case we race with the first call to
5276 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
5279 void cgroup_post_fork(struct task_struct
*child
)
5281 struct cgroup_subsys
*ss
;
5285 * use_task_css_set_links is set to 1 before we walk the tasklist
5286 * under the tasklist_lock and we read it here after we added the child
5287 * to the tasklist under the tasklist_lock as well. If the child wasn't
5288 * yet in the tasklist when we walked through it from
5289 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
5290 * should be visible now due to the paired locking and barriers implied
5291 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
5292 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
5295 if (use_task_css_set_links
) {
5296 write_lock(&css_set_lock
);
5298 if (list_empty(&child
->cg_list
))
5299 list_add(&child
->cg_list
, &task_css_set(child
)->tasks
);
5301 write_unlock(&css_set_lock
);
5305 * Call ss->fork(). This must happen after @child is linked on
5306 * css_set; otherwise, @child might change state between ->fork()
5307 * and addition to css_set.
5309 if (need_forkexit_callback
) {
5311 * fork/exit callbacks are supported only for builtin
5312 * subsystems, and the builtin section of the subsys
5313 * array is immutable, so we don't need to lock the
5314 * subsys array here. On the other hand, modular section
5315 * of the array can be freed at module unload, so we
5318 for_each_builtin_subsys(ss
, i
)
5325 * cgroup_exit - detach cgroup from exiting task
5326 * @tsk: pointer to task_struct of exiting process
5327 * @run_callback: run exit callbacks?
5329 * Description: Detach cgroup from @tsk and release it.
5331 * Note that cgroups marked notify_on_release force every task in
5332 * them to take the global cgroup_mutex mutex when exiting.
5333 * This could impact scaling on very large systems. Be reluctant to
5334 * use notify_on_release cgroups where very high task exit scaling
5335 * is required on large systems.
5337 * the_top_cgroup_hack:
5339 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
5341 * We call cgroup_exit() while the task is still competent to
5342 * handle notify_on_release(), then leave the task attached to the
5343 * root cgroup in each hierarchy for the remainder of its exit.
5345 * To do this properly, we would increment the reference count on
5346 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
5347 * code we would add a second cgroup function call, to drop that
5348 * reference. This would just create an unnecessary hot spot on
5349 * the top_cgroup reference count, to no avail.
5351 * Normally, holding a reference to a cgroup without bumping its
5352 * count is unsafe. The cgroup could go away, or someone could
5353 * attach us to a different cgroup, decrementing the count on
5354 * the first cgroup that we never incremented. But in this case,
5355 * top_cgroup isn't going away, and either task has PF_EXITING set,
5356 * which wards off any cgroup_attach_task() attempts, or task is a failed
5357 * fork, never visible to cgroup_attach_task.
5359 void cgroup_exit(struct task_struct
*tsk
, int run_callbacks
)
5361 struct cgroup_subsys
*ss
;
5362 struct css_set
*cset
;
5366 * Unlink from the css_set task list if necessary.
5367 * Optimistically check cg_list before taking
5370 if (!list_empty(&tsk
->cg_list
)) {
5371 write_lock(&css_set_lock
);
5372 if (!list_empty(&tsk
->cg_list
))
5373 list_del_init(&tsk
->cg_list
);
5374 write_unlock(&css_set_lock
);
5377 /* Reassign the task to the init_css_set. */
5379 cset
= task_css_set(tsk
);
5380 RCU_INIT_POINTER(tsk
->cgroups
, &init_css_set
);
5382 if (run_callbacks
&& need_forkexit_callback
) {
5384 * fork/exit callbacks are supported only for builtin
5385 * subsystems, see cgroup_post_fork() for details.
5387 for_each_builtin_subsys(ss
, i
) {
5389 struct cgroup_subsys_state
*old_css
= cset
->subsys
[i
];
5390 struct cgroup_subsys_state
*css
= task_css(tsk
, i
);
5392 ss
->exit(css
, old_css
, tsk
);
5398 put_css_set_taskexit(cset
);
5401 static void check_for_release(struct cgroup
*cgrp
)
5403 if (cgroup_is_releasable(cgrp
) &&
5404 list_empty(&cgrp
->cset_links
) && list_empty(&cgrp
->children
)) {
5406 * Control Group is currently removeable. If it's not
5407 * already queued for a userspace notification, queue
5410 int need_schedule_work
= 0;
5412 raw_spin_lock(&release_list_lock
);
5413 if (!cgroup_is_dead(cgrp
) &&
5414 list_empty(&cgrp
->release_list
)) {
5415 list_add(&cgrp
->release_list
, &release_list
);
5416 need_schedule_work
= 1;
5418 raw_spin_unlock(&release_list_lock
);
5419 if (need_schedule_work
)
5420 schedule_work(&release_agent_work
);
5425 * Notify userspace when a cgroup is released, by running the
5426 * configured release agent with the name of the cgroup (path
5427 * relative to the root of cgroup file system) as the argument.
5429 * Most likely, this user command will try to rmdir this cgroup.
5431 * This races with the possibility that some other task will be
5432 * attached to this cgroup before it is removed, or that some other
5433 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5434 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5435 * unused, and this cgroup will be reprieved from its death sentence,
5436 * to continue to serve a useful existence. Next time it's released,
5437 * we will get notified again, if it still has 'notify_on_release' set.
5439 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5440 * means only wait until the task is successfully execve()'d. The
5441 * separate release agent task is forked by call_usermodehelper(),
5442 * then control in this thread returns here, without waiting for the
5443 * release agent task. We don't bother to wait because the caller of
5444 * this routine has no use for the exit status of the release agent
5445 * task, so no sense holding our caller up for that.
5447 static void cgroup_release_agent(struct work_struct
*work
)
5449 BUG_ON(work
!= &release_agent_work
);
5450 mutex_lock(&cgroup_mutex
);
5451 raw_spin_lock(&release_list_lock
);
5452 while (!list_empty(&release_list
)) {
5453 char *argv
[3], *envp
[3];
5455 char *pathbuf
= NULL
, *agentbuf
= NULL
;
5456 struct cgroup
*cgrp
= list_entry(release_list
.next
,
5459 list_del_init(&cgrp
->release_list
);
5460 raw_spin_unlock(&release_list_lock
);
5461 pathbuf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
5464 if (cgroup_path(cgrp
, pathbuf
, PAGE_SIZE
) < 0)
5466 agentbuf
= kstrdup(cgrp
->root
->release_agent_path
, GFP_KERNEL
);
5471 argv
[i
++] = agentbuf
;
5472 argv
[i
++] = pathbuf
;
5476 /* minimal command environment */
5477 envp
[i
++] = "HOME=/";
5478 envp
[i
++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5481 /* Drop the lock while we invoke the usermode helper,
5482 * since the exec could involve hitting disk and hence
5483 * be a slow process */
5484 mutex_unlock(&cgroup_mutex
);
5485 call_usermodehelper(argv
[0], argv
, envp
, UMH_WAIT_EXEC
);
5486 mutex_lock(&cgroup_mutex
);
5490 raw_spin_lock(&release_list_lock
);
5492 raw_spin_unlock(&release_list_lock
);
5493 mutex_unlock(&cgroup_mutex
);
5496 static int __init
cgroup_disable(char *str
)
5498 struct cgroup_subsys
*ss
;
5502 while ((token
= strsep(&str
, ",")) != NULL
) {
5507 * cgroup_disable, being at boot time, can't know about
5508 * module subsystems, so we don't worry about them.
5510 for_each_builtin_subsys(ss
, i
) {
5511 if (!strcmp(token
, ss
->name
)) {
5513 printk(KERN_INFO
"Disabling %s control group"
5514 " subsystem\n", ss
->name
);
5521 __setup("cgroup_disable=", cgroup_disable
);
5524 * Functons for CSS ID.
5527 /* to get ID other than 0, this should be called when !cgroup_is_dead() */
5528 unsigned short css_id(struct cgroup_subsys_state
*css
)
5530 struct css_id
*cssid
;
5533 * This css_id() can return correct value when somone has refcnt
5534 * on this or this is under rcu_read_lock(). Once css->id is allocated,
5535 * it's unchanged until freed.
5537 cssid
= rcu_dereference_raw(css
->id
);
5543 EXPORT_SYMBOL_GPL(css_id
);
5546 * css_is_ancestor - test "root" css is an ancestor of "child"
5547 * @child: the css to be tested.
5548 * @root: the css supporsed to be an ancestor of the child.
5550 * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
5551 * this function reads css->id, the caller must hold rcu_read_lock().
5552 * But, considering usual usage, the csses should be valid objects after test.
5553 * Assuming that the caller will do some action to the child if this returns
5554 * returns true, the caller must take "child";s reference count.
5555 * If "child" is valid object and this returns true, "root" is valid, too.
5558 bool css_is_ancestor(struct cgroup_subsys_state
*child
,
5559 const struct cgroup_subsys_state
*root
)
5561 struct css_id
*child_id
;
5562 struct css_id
*root_id
;
5564 child_id
= rcu_dereference(child
->id
);
5567 root_id
= rcu_dereference(root
->id
);
5570 if (child_id
->depth
< root_id
->depth
)
5572 if (child_id
->stack
[root_id
->depth
] != root_id
->id
)
5577 void free_css_id(struct cgroup_subsys
*ss
, struct cgroup_subsys_state
*css
)
5579 struct css_id
*id
= rcu_dereference_protected(css
->id
, true);
5581 /* When this is called before css_id initialization, id can be NULL */
5585 BUG_ON(!ss
->use_id
);
5587 rcu_assign_pointer(id
->css
, NULL
);
5588 rcu_assign_pointer(css
->id
, NULL
);
5589 spin_lock(&ss
->id_lock
);
5590 idr_remove(&ss
->idr
, id
->id
);
5591 spin_unlock(&ss
->id_lock
);
5592 kfree_rcu(id
, rcu_head
);
5594 EXPORT_SYMBOL_GPL(free_css_id
);
5597 * This is called by init or create(). Then, calls to this function are
5598 * always serialized (By cgroup_mutex() at create()).
5601 static struct css_id
*get_new_cssid(struct cgroup_subsys
*ss
, int depth
)
5603 struct css_id
*newid
;
5606 BUG_ON(!ss
->use_id
);
5608 size
= sizeof(*newid
) + sizeof(unsigned short) * (depth
+ 1);
5609 newid
= kzalloc(size
, GFP_KERNEL
);
5611 return ERR_PTR(-ENOMEM
);
5613 idr_preload(GFP_KERNEL
);
5614 spin_lock(&ss
->id_lock
);
5615 /* Don't use 0. allocates an ID of 1-65535 */
5616 ret
= idr_alloc(&ss
->idr
, newid
, 1, CSS_ID_MAX
+ 1, GFP_NOWAIT
);
5617 spin_unlock(&ss
->id_lock
);
5620 /* Returns error when there are no free spaces for new ID.*/
5625 newid
->depth
= depth
;
5629 return ERR_PTR(ret
);
5633 static int __init_or_module
cgroup_init_idr(struct cgroup_subsys
*ss
,
5634 struct cgroup_subsys_state
*rootcss
)
5636 struct css_id
*newid
;
5638 spin_lock_init(&ss
->id_lock
);
5641 newid
= get_new_cssid(ss
, 0);
5643 return PTR_ERR(newid
);
5645 newid
->stack
[0] = newid
->id
;
5646 RCU_INIT_POINTER(newid
->css
, rootcss
);
5647 RCU_INIT_POINTER(rootcss
->id
, newid
);
5651 static int alloc_css_id(struct cgroup_subsys_state
*child_css
)
5653 struct cgroup_subsys_state
*parent_css
= css_parent(child_css
);
5654 struct css_id
*child_id
, *parent_id
;
5657 parent_id
= rcu_dereference_protected(parent_css
->id
, true);
5658 depth
= parent_id
->depth
+ 1;
5660 child_id
= get_new_cssid(child_css
->ss
, depth
);
5661 if (IS_ERR(child_id
))
5662 return PTR_ERR(child_id
);
5664 for (i
= 0; i
< depth
; i
++)
5665 child_id
->stack
[i
] = parent_id
->stack
[i
];
5666 child_id
->stack
[depth
] = child_id
->id
;
5668 * child_id->css pointer will be set after this cgroup is available
5669 * see cgroup_populate_dir()
5671 rcu_assign_pointer(child_css
->id
, child_id
);
5677 * css_lookup - lookup css by id
5678 * @ss: cgroup subsys to be looked into.
5681 * Returns pointer to cgroup_subsys_state if there is valid one with id.
5682 * NULL if not. Should be called under rcu_read_lock()
5684 struct cgroup_subsys_state
*css_lookup(struct cgroup_subsys
*ss
, int id
)
5686 struct css_id
*cssid
= NULL
;
5688 BUG_ON(!ss
->use_id
);
5689 cssid
= idr_find(&ss
->idr
, id
);
5691 if (unlikely(!cssid
))
5694 return rcu_dereference(cssid
->css
);
5696 EXPORT_SYMBOL_GPL(css_lookup
);
5699 * css_from_dir - get corresponding css from the dentry of a cgroup dir
5700 * @dentry: directory dentry of interest
5701 * @ss: subsystem of interest
5703 * Must be called under RCU read lock. The caller is responsible for
5704 * pinning the returned css if it needs to be accessed outside the RCU
5707 struct cgroup_subsys_state
*css_from_dir(struct dentry
*dentry
,
5708 struct cgroup_subsys
*ss
)
5710 struct cgroup
*cgrp
;
5712 WARN_ON_ONCE(!rcu_read_lock_held());
5714 /* is @dentry a cgroup dir? */
5715 if (!dentry
->d_inode
||
5716 dentry
->d_inode
->i_op
!= &cgroup_dir_inode_operations
)
5717 return ERR_PTR(-EBADF
);
5719 cgrp
= __d_cgrp(dentry
);
5720 return cgroup_css(cgrp
, ss
) ?: ERR_PTR(-ENOENT
);
5724 * css_from_id - lookup css by id
5725 * @id: the cgroup id
5726 * @ss: cgroup subsys to be looked into
5728 * Returns the css if there's valid one with @id, otherwise returns NULL.
5729 * Should be called under rcu_read_lock().
5731 struct cgroup_subsys_state
*css_from_id(int id
, struct cgroup_subsys
*ss
)
5733 struct cgroup
*cgrp
;
5735 rcu_lockdep_assert(rcu_read_lock_held() ||
5736 lockdep_is_held(&cgroup_mutex
),
5737 "css_from_id() needs proper protection");
5739 cgrp
= idr_find(&ss
->root
->cgroup_idr
, id
);
5741 return cgroup_css(cgrp
, ss
);
5745 #ifdef CONFIG_CGROUP_DEBUG
5746 static struct cgroup_subsys_state
*
5747 debug_css_alloc(struct cgroup_subsys_state
*parent_css
)
5749 struct cgroup_subsys_state
*css
= kzalloc(sizeof(*css
), GFP_KERNEL
);
5752 return ERR_PTR(-ENOMEM
);
5757 static void debug_css_free(struct cgroup_subsys_state
*css
)
5762 static u64
debug_taskcount_read(struct cgroup_subsys_state
*css
,
5765 return cgroup_task_count(css
->cgroup
);
5768 static u64
current_css_set_read(struct cgroup_subsys_state
*css
,
5771 return (u64
)(unsigned long)current
->cgroups
;
5774 static u64
current_css_set_refcount_read(struct cgroup_subsys_state
*css
,
5780 count
= atomic_read(&task_css_set(current
)->refcount
);
5785 static int current_css_set_cg_links_read(struct cgroup_subsys_state
*css
,
5787 struct seq_file
*seq
)
5789 struct cgrp_cset_link
*link
;
5790 struct css_set
*cset
;
5792 read_lock(&css_set_lock
);
5794 cset
= rcu_dereference(current
->cgroups
);
5795 list_for_each_entry(link
, &cset
->cgrp_links
, cgrp_link
) {
5796 struct cgroup
*c
= link
->cgrp
;
5800 name
= c
->dentry
->d_name
.name
;
5803 seq_printf(seq
, "Root %d group %s\n",
5804 c
->root
->hierarchy_id
, name
);
5807 read_unlock(&css_set_lock
);
5811 #define MAX_TASKS_SHOWN_PER_CSS 25
5812 static int cgroup_css_links_read(struct cgroup_subsys_state
*css
,
5813 struct cftype
*cft
, struct seq_file
*seq
)
5815 struct cgrp_cset_link
*link
;
5817 read_lock(&css_set_lock
);
5818 list_for_each_entry(link
, &css
->cgroup
->cset_links
, cset_link
) {
5819 struct css_set
*cset
= link
->cset
;
5820 struct task_struct
*task
;
5822 seq_printf(seq
, "css_set %p\n", cset
);
5823 list_for_each_entry(task
, &cset
->tasks
, cg_list
) {
5824 if (count
++ > MAX_TASKS_SHOWN_PER_CSS
) {
5825 seq_puts(seq
, " ...\n");
5828 seq_printf(seq
, " task %d\n",
5829 task_pid_vnr(task
));
5833 read_unlock(&css_set_lock
);
5837 static u64
releasable_read(struct cgroup_subsys_state
*css
, struct cftype
*cft
)
5839 return test_bit(CGRP_RELEASABLE
, &css
->cgroup
->flags
);
5842 static struct cftype debug_files
[] = {
5844 .name
= "taskcount",
5845 .read_u64
= debug_taskcount_read
,
5849 .name
= "current_css_set",
5850 .read_u64
= current_css_set_read
,
5854 .name
= "current_css_set_refcount",
5855 .read_u64
= current_css_set_refcount_read
,
5859 .name
= "current_css_set_cg_links",
5860 .read_seq_string
= current_css_set_cg_links_read
,
5864 .name
= "cgroup_css_links",
5865 .read_seq_string
= cgroup_css_links_read
,
5869 .name
= "releasable",
5870 .read_u64
= releasable_read
,
5876 struct cgroup_subsys debug_subsys
= {
5878 .css_alloc
= debug_css_alloc
,
5879 .css_free
= debug_css_free
,
5880 .subsys_id
= debug_subsys_id
,
5881 .base_cftypes
= debug_files
,
5883 #endif /* CONFIG_CGROUP_DEBUG */