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/slab.h>
45 #include <linux/magic.h>
46 #include <linux/spinlock.h>
47 #include <linux/string.h>
48 #include <linux/sort.h>
49 #include <linux/kmod.h>
50 #include <linux/module.h>
51 #include <linux/delayacct.h>
52 #include <linux/cgroupstats.h>
53 #include <linux/hashtable.h>
54 #include <linux/namei.h>
55 #include <linux/pid_namespace.h>
56 #include <linux/idr.h>
57 #include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
58 #include <linux/flex_array.h> /* used in cgroup_attach_task */
59 #include <linux/kthread.h>
61 #include <linux/atomic.h>
64 * pidlists linger the following amount before being destroyed. The goal
65 * is avoiding frequent destruction in the middle of consecutive read calls
66 * Expiring in the middle is a performance problem not a correctness one.
67 * 1 sec should be enough.
69 #define CGROUP_PIDLIST_DESTROY_DELAY HZ
72 * cgroup_mutex is the master lock. Any modification to cgroup or its
73 * hierarchy must be performed while holding it.
75 * cgroup_root_mutex nests inside cgroup_mutex and should be held to modify
76 * cgroupfs_root of any cgroup hierarchy - subsys list, flags,
77 * release_agent_path and so on. Modifying requires both cgroup_mutex and
78 * cgroup_root_mutex. Readers can acquire either of the two. This is to
79 * break the following locking order cycle.
81 * A. cgroup_mutex -> cred_guard_mutex -> s_type->i_mutex_key -> namespace_sem
82 * B. namespace_sem -> cgroup_mutex
84 * B happens only through cgroup_show_options() and using cgroup_root_mutex
87 #ifdef CONFIG_PROVE_RCU
88 DEFINE_MUTEX(cgroup_mutex
);
89 EXPORT_SYMBOL_GPL(cgroup_mutex
); /* only for lockdep */
91 static DEFINE_MUTEX(cgroup_mutex
);
94 static DEFINE_MUTEX(cgroup_root_mutex
);
96 #define cgroup_assert_mutex_or_rcu_locked() \
97 rcu_lockdep_assert(rcu_read_lock_held() || \
98 lockdep_is_held(&cgroup_mutex), \
99 "cgroup_mutex or RCU read lock required");
101 #ifdef CONFIG_LOCKDEP
102 #define cgroup_assert_mutex_or_root_locked() \
103 WARN_ON_ONCE(debug_locks && (!lockdep_is_held(&cgroup_mutex) && \
104 !lockdep_is_held(&cgroup_root_mutex)))
106 #define cgroup_assert_mutex_or_root_locked() do { } while (0)
110 * cgroup destruction makes heavy use of work items and there can be a lot
111 * of concurrent destructions. Use a separate workqueue so that cgroup
112 * destruction work items don't end up filling up max_active of system_wq
113 * which may lead to deadlock.
115 static struct workqueue_struct
*cgroup_destroy_wq
;
118 * pidlist destructions need to be flushed on cgroup destruction. Use a
119 * separate workqueue as flush domain.
121 static struct workqueue_struct
*cgroup_pidlist_destroy_wq
;
124 * Generate an array of cgroup subsystem pointers. At boot time, this is
125 * populated with the built in subsystems, and modular subsystems are
126 * registered after that. The mutable section of this array is protected by
129 #define SUBSYS(_x) [_x ## _subsys_id] = &_x ## _subsys,
130 #define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option)
131 static struct cgroup_subsys
*cgroup_subsys
[CGROUP_SUBSYS_COUNT
] = {
132 #include <linux/cgroup_subsys.h>
136 * The dummy hierarchy, reserved for the subsystems that are otherwise
137 * unattached - it never has more than a single cgroup, and all tasks are
138 * part of that cgroup.
140 static struct cgroupfs_root cgroup_dummy_root
;
142 /* dummy_top is a shorthand for the dummy hierarchy's top cgroup */
143 static struct cgroup
* const cgroup_dummy_top
= &cgroup_dummy_root
.top_cgroup
;
145 /* The list of hierarchy roots */
147 static LIST_HEAD(cgroup_roots
);
148 static int cgroup_root_count
;
151 * Hierarchy ID allocation and mapping. It follows the same exclusion
152 * rules as other root ops - both cgroup_mutex and cgroup_root_mutex for
153 * writes, either for reads.
155 static DEFINE_IDR(cgroup_hierarchy_idr
);
157 static struct cgroup_name root_cgroup_name
= { .name
= "/" };
160 * Assign a monotonically increasing serial number to cgroups. It
161 * guarantees cgroups with bigger numbers are newer than those with smaller
162 * numbers. Also, as cgroups are always appended to the parent's
163 * ->children list, it guarantees that sibling cgroups are always sorted in
164 * the ascending serial number order on the list. Protected by
167 static u64 cgroup_serial_nr_next
= 1;
169 /* This flag indicates whether tasks in the fork and exit paths should
170 * check for fork/exit handlers to call. This avoids us having to do
171 * extra work in the fork/exit path if none of the subsystems need to
174 static int need_forkexit_callback __read_mostly
;
176 static struct cftype cgroup_base_files
[];
178 static void cgroup_destroy_css_killed(struct cgroup
*cgrp
);
179 static int cgroup_destroy_locked(struct cgroup
*cgrp
);
180 static int cgroup_addrm_files(struct cgroup
*cgrp
, struct cftype cfts
[],
182 static int cgroup_file_release(struct inode
*inode
, struct file
*file
);
183 static void cgroup_pidlist_destroy_all(struct cgroup
*cgrp
);
186 * cgroup_css - obtain a cgroup's css for the specified subsystem
187 * @cgrp: the cgroup of interest
188 * @ss: the subsystem of interest (%NULL returns the dummy_css)
190 * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This
191 * function must be called either under cgroup_mutex or rcu_read_lock() and
192 * the caller is responsible for pinning the returned css if it wants to
193 * keep accessing it outside the said locks. This function may return
194 * %NULL if @cgrp doesn't have @subsys_id enabled.
196 static struct cgroup_subsys_state
*cgroup_css(struct cgroup
*cgrp
,
197 struct cgroup_subsys
*ss
)
200 return rcu_dereference_check(cgrp
->subsys
[ss
->subsys_id
],
201 lockdep_is_held(&cgroup_mutex
));
203 return &cgrp
->dummy_css
;
206 /* convenient tests for these bits */
207 static inline bool cgroup_is_dead(const struct cgroup
*cgrp
)
209 return test_bit(CGRP_DEAD
, &cgrp
->flags
);
213 * cgroup_is_descendant - test ancestry
214 * @cgrp: the cgroup to be tested
215 * @ancestor: possible ancestor of @cgrp
217 * Test whether @cgrp is a descendant of @ancestor. It also returns %true
218 * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
219 * and @ancestor are accessible.
221 bool cgroup_is_descendant(struct cgroup
*cgrp
, struct cgroup
*ancestor
)
224 if (cgrp
== ancestor
)
230 EXPORT_SYMBOL_GPL(cgroup_is_descendant
);
232 static int cgroup_is_releasable(const struct cgroup
*cgrp
)
235 (1 << CGRP_RELEASABLE
) |
236 (1 << CGRP_NOTIFY_ON_RELEASE
);
237 return (cgrp
->flags
& bits
) == bits
;
240 static int notify_on_release(const struct cgroup
*cgrp
)
242 return test_bit(CGRP_NOTIFY_ON_RELEASE
, &cgrp
->flags
);
246 * for_each_css - iterate all css's of a cgroup
247 * @css: the iteration cursor
248 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
249 * @cgrp: the target cgroup to iterate css's of
251 * Should be called under cgroup_mutex.
253 #define for_each_css(css, ssid, cgrp) \
254 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
255 if (!((css) = rcu_dereference_check( \
256 (cgrp)->subsys[(ssid)], \
257 lockdep_is_held(&cgroup_mutex)))) { } \
261 * for_each_subsys - iterate all loaded cgroup subsystems
262 * @ss: the iteration cursor
263 * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
265 * Iterates through all loaded subsystems. Should be called under
266 * cgroup_mutex or cgroup_root_mutex.
268 #define for_each_subsys(ss, ssid) \
269 for (({ cgroup_assert_mutex_or_root_locked(); (ssid) = 0; }); \
270 (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
271 if (!((ss) = cgroup_subsys[(ssid)])) { } \
275 * for_each_builtin_subsys - iterate all built-in cgroup subsystems
276 * @ss: the iteration cursor
277 * @i: the index of @ss, CGROUP_BUILTIN_SUBSYS_COUNT after reaching the end
279 * Bulit-in subsystems are always present and iteration itself doesn't
280 * require any synchronization.
282 #define for_each_builtin_subsys(ss, i) \
283 for ((i) = 0; (i) < CGROUP_BUILTIN_SUBSYS_COUNT && \
284 (((ss) = cgroup_subsys[i]) || true); (i)++)
286 /* iterate across the active hierarchies */
287 #define for_each_active_root(root) \
288 list_for_each_entry((root), &cgroup_roots, root_list)
290 static inline struct cgroup
*__d_cgrp(struct dentry
*dentry
)
292 return dentry
->d_fsdata
;
295 static inline struct cfent
*__d_cfe(struct dentry
*dentry
)
297 return dentry
->d_fsdata
;
300 static inline struct cftype
*__d_cft(struct dentry
*dentry
)
302 return __d_cfe(dentry
)->type
;
306 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
307 * @cgrp: the cgroup to be checked for liveness
309 * On success, returns true; the mutex should be later unlocked. On
310 * failure returns false with no lock held.
312 static bool cgroup_lock_live_group(struct cgroup
*cgrp
)
314 mutex_lock(&cgroup_mutex
);
315 if (cgroup_is_dead(cgrp
)) {
316 mutex_unlock(&cgroup_mutex
);
322 /* the list of cgroups eligible for automatic release. Protected by
323 * release_list_lock */
324 static LIST_HEAD(release_list
);
325 static DEFINE_RAW_SPINLOCK(release_list_lock
);
326 static void cgroup_release_agent(struct work_struct
*work
);
327 static DECLARE_WORK(release_agent_work
, cgroup_release_agent
);
328 static void check_for_release(struct cgroup
*cgrp
);
331 * A cgroup can be associated with multiple css_sets as different tasks may
332 * belong to different cgroups on different hierarchies. In the other
333 * direction, a css_set is naturally associated with multiple cgroups.
334 * This M:N relationship is represented by the following link structure
335 * which exists for each association and allows traversing the associations
338 struct cgrp_cset_link
{
339 /* the cgroup and css_set this link associates */
341 struct css_set
*cset
;
343 /* list of cgrp_cset_links anchored at cgrp->cset_links */
344 struct list_head cset_link
;
346 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
347 struct list_head cgrp_link
;
350 /* The default css_set - used by init and its children prior to any
351 * hierarchies being mounted. It contains a pointer to the root state
352 * for each subsystem. Also used to anchor the list of css_sets. Not
353 * reference-counted, to improve performance when child cgroups
354 * haven't been created.
357 static struct css_set init_css_set
;
358 static struct cgrp_cset_link init_cgrp_cset_link
;
361 * css_set_lock protects the list of css_set objects, and the chain of
362 * tasks off each css_set. Nests outside task->alloc_lock due to
363 * css_task_iter_start().
365 static DEFINE_RWLOCK(css_set_lock
);
366 static int css_set_count
;
369 * hash table for cgroup groups. This improves the performance to find
370 * an existing css_set. This hash doesn't (currently) take into
371 * account cgroups in empty hierarchies.
373 #define CSS_SET_HASH_BITS 7
374 static DEFINE_HASHTABLE(css_set_table
, CSS_SET_HASH_BITS
);
376 static unsigned long css_set_hash(struct cgroup_subsys_state
*css
[])
378 unsigned long key
= 0UL;
379 struct cgroup_subsys
*ss
;
382 for_each_subsys(ss
, i
)
383 key
+= (unsigned long)css
[i
];
384 key
= (key
>> 16) ^ key
;
390 * We don't maintain the lists running through each css_set to its task
391 * until after the first call to css_task_iter_start(). This reduces the
392 * fork()/exit() overhead for people who have cgroups compiled into their
393 * kernel but not actually in use.
395 static int use_task_css_set_links __read_mostly
;
397 static void __put_css_set(struct css_set
*cset
, int taskexit
)
399 struct cgrp_cset_link
*link
, *tmp_link
;
402 * Ensure that the refcount doesn't hit zero while any readers
403 * can see it. Similar to atomic_dec_and_lock(), but for an
406 if (atomic_add_unless(&cset
->refcount
, -1, 1))
408 write_lock(&css_set_lock
);
409 if (!atomic_dec_and_test(&cset
->refcount
)) {
410 write_unlock(&css_set_lock
);
414 /* This css_set is dead. unlink it and release cgroup refcounts */
415 hash_del(&cset
->hlist
);
418 list_for_each_entry_safe(link
, tmp_link
, &cset
->cgrp_links
, cgrp_link
) {
419 struct cgroup
*cgrp
= link
->cgrp
;
421 list_del(&link
->cset_link
);
422 list_del(&link
->cgrp_link
);
424 /* @cgrp can't go away while we're holding css_set_lock */
425 if (list_empty(&cgrp
->cset_links
) && notify_on_release(cgrp
)) {
427 set_bit(CGRP_RELEASABLE
, &cgrp
->flags
);
428 check_for_release(cgrp
);
434 write_unlock(&css_set_lock
);
435 kfree_rcu(cset
, rcu_head
);
439 * refcounted get/put for css_set objects
441 static inline void get_css_set(struct css_set
*cset
)
443 atomic_inc(&cset
->refcount
);
446 static inline void put_css_set(struct css_set
*cset
)
448 __put_css_set(cset
, 0);
451 static inline void put_css_set_taskexit(struct css_set
*cset
)
453 __put_css_set(cset
, 1);
457 * compare_css_sets - helper function for find_existing_css_set().
458 * @cset: candidate css_set being tested
459 * @old_cset: existing css_set for a task
460 * @new_cgrp: cgroup that's being entered by the task
461 * @template: desired set of css pointers in css_set (pre-calculated)
463 * Returns true if "cset" matches "old_cset" except for the hierarchy
464 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
466 static bool compare_css_sets(struct css_set
*cset
,
467 struct css_set
*old_cset
,
468 struct cgroup
*new_cgrp
,
469 struct cgroup_subsys_state
*template[])
471 struct list_head
*l1
, *l2
;
473 if (memcmp(template, cset
->subsys
, sizeof(cset
->subsys
))) {
474 /* Not all subsystems matched */
479 * Compare cgroup pointers in order to distinguish between
480 * different cgroups in heirarchies with no subsystems. We
481 * could get by with just this check alone (and skip the
482 * memcmp above) but on most setups the memcmp check will
483 * avoid the need for this more expensive check on almost all
487 l1
= &cset
->cgrp_links
;
488 l2
= &old_cset
->cgrp_links
;
490 struct cgrp_cset_link
*link1
, *link2
;
491 struct cgroup
*cgrp1
, *cgrp2
;
495 /* See if we reached the end - both lists are equal length. */
496 if (l1
== &cset
->cgrp_links
) {
497 BUG_ON(l2
!= &old_cset
->cgrp_links
);
500 BUG_ON(l2
== &old_cset
->cgrp_links
);
502 /* Locate the cgroups associated with these links. */
503 link1
= list_entry(l1
, struct cgrp_cset_link
, cgrp_link
);
504 link2
= list_entry(l2
, struct cgrp_cset_link
, cgrp_link
);
507 /* Hierarchies should be linked in the same order. */
508 BUG_ON(cgrp1
->root
!= cgrp2
->root
);
511 * If this hierarchy is the hierarchy of the cgroup
512 * that's changing, then we need to check that this
513 * css_set points to the new cgroup; if it's any other
514 * hierarchy, then this css_set should point to the
515 * same cgroup as the old css_set.
517 if (cgrp1
->root
== new_cgrp
->root
) {
518 if (cgrp1
!= new_cgrp
)
529 * find_existing_css_set - init css array and find the matching css_set
530 * @old_cset: the css_set that we're using before the cgroup transition
531 * @cgrp: the cgroup that we're moving into
532 * @template: out param for the new set of csses, should be clear on entry
534 static struct css_set
*find_existing_css_set(struct css_set
*old_cset
,
536 struct cgroup_subsys_state
*template[])
538 struct cgroupfs_root
*root
= cgrp
->root
;
539 struct cgroup_subsys
*ss
;
540 struct css_set
*cset
;
545 * Build the set of subsystem state objects that we want to see in the
546 * new css_set. while subsystems can change globally, the entries here
547 * won't change, so no need for locking.
549 for_each_subsys(ss
, i
) {
550 if (root
->subsys_mask
& (1UL << i
)) {
551 /* Subsystem is in this hierarchy. So we want
552 * the subsystem state from the new
554 template[i
] = cgroup_css(cgrp
, ss
);
556 /* Subsystem is not in this hierarchy, so we
557 * don't want to change the subsystem state */
558 template[i
] = old_cset
->subsys
[i
];
562 key
= css_set_hash(template);
563 hash_for_each_possible(css_set_table
, cset
, hlist
, key
) {
564 if (!compare_css_sets(cset
, old_cset
, cgrp
, template))
567 /* This css_set matches what we need */
571 /* No existing cgroup group matched */
575 static void free_cgrp_cset_links(struct list_head
*links_to_free
)
577 struct cgrp_cset_link
*link
, *tmp_link
;
579 list_for_each_entry_safe(link
, tmp_link
, links_to_free
, cset_link
) {
580 list_del(&link
->cset_link
);
586 * allocate_cgrp_cset_links - allocate cgrp_cset_links
587 * @count: the number of links to allocate
588 * @tmp_links: list_head the allocated links are put on
590 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
591 * through ->cset_link. Returns 0 on success or -errno.
593 static int allocate_cgrp_cset_links(int count
, struct list_head
*tmp_links
)
595 struct cgrp_cset_link
*link
;
598 INIT_LIST_HEAD(tmp_links
);
600 for (i
= 0; i
< count
; i
++) {
601 link
= kzalloc(sizeof(*link
), GFP_KERNEL
);
603 free_cgrp_cset_links(tmp_links
);
606 list_add(&link
->cset_link
, tmp_links
);
612 * link_css_set - a helper function to link a css_set to a cgroup
613 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
614 * @cset: the css_set to be linked
615 * @cgrp: the destination cgroup
617 static void link_css_set(struct list_head
*tmp_links
, struct css_set
*cset
,
620 struct cgrp_cset_link
*link
;
622 BUG_ON(list_empty(tmp_links
));
623 link
= list_first_entry(tmp_links
, struct cgrp_cset_link
, cset_link
);
626 list_move(&link
->cset_link
, &cgrp
->cset_links
);
628 * Always add links to the tail of the list so that the list
629 * is sorted by order of hierarchy creation
631 list_add_tail(&link
->cgrp_link
, &cset
->cgrp_links
);
635 * find_css_set - return a new css_set with one cgroup updated
636 * @old_cset: the baseline css_set
637 * @cgrp: the cgroup to be updated
639 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
640 * substituted into the appropriate hierarchy.
642 static struct css_set
*find_css_set(struct css_set
*old_cset
,
645 struct cgroup_subsys_state
*template[CGROUP_SUBSYS_COUNT
] = { };
646 struct css_set
*cset
;
647 struct list_head tmp_links
;
648 struct cgrp_cset_link
*link
;
651 lockdep_assert_held(&cgroup_mutex
);
653 /* First see if we already have a cgroup group that matches
655 read_lock(&css_set_lock
);
656 cset
= find_existing_css_set(old_cset
, cgrp
, template);
659 read_unlock(&css_set_lock
);
664 cset
= kzalloc(sizeof(*cset
), GFP_KERNEL
);
668 /* Allocate all the cgrp_cset_link objects that we'll need */
669 if (allocate_cgrp_cset_links(cgroup_root_count
, &tmp_links
) < 0) {
674 atomic_set(&cset
->refcount
, 1);
675 INIT_LIST_HEAD(&cset
->cgrp_links
);
676 INIT_LIST_HEAD(&cset
->tasks
);
677 INIT_HLIST_NODE(&cset
->hlist
);
679 /* Copy the set of subsystem state objects generated in
680 * find_existing_css_set() */
681 memcpy(cset
->subsys
, template, sizeof(cset
->subsys
));
683 write_lock(&css_set_lock
);
684 /* Add reference counts and links from the new css_set. */
685 list_for_each_entry(link
, &old_cset
->cgrp_links
, cgrp_link
) {
686 struct cgroup
*c
= link
->cgrp
;
688 if (c
->root
== cgrp
->root
)
690 link_css_set(&tmp_links
, cset
, c
);
693 BUG_ON(!list_empty(&tmp_links
));
697 /* Add this cgroup group to the hash table */
698 key
= css_set_hash(cset
->subsys
);
699 hash_add(css_set_table
, &cset
->hlist
, key
);
701 write_unlock(&css_set_lock
);
707 * Return the cgroup for "task" from the given hierarchy. Must be
708 * called with cgroup_mutex held.
710 static struct cgroup
*task_cgroup_from_root(struct task_struct
*task
,
711 struct cgroupfs_root
*root
)
713 struct css_set
*cset
;
714 struct cgroup
*res
= NULL
;
716 BUG_ON(!mutex_is_locked(&cgroup_mutex
));
717 read_lock(&css_set_lock
);
719 * No need to lock the task - since we hold cgroup_mutex the
720 * task can't change groups, so the only thing that can happen
721 * is that it exits and its css is set back to init_css_set.
723 cset
= task_css_set(task
);
724 if (cset
== &init_css_set
) {
725 res
= &root
->top_cgroup
;
727 struct cgrp_cset_link
*link
;
729 list_for_each_entry(link
, &cset
->cgrp_links
, cgrp_link
) {
730 struct cgroup
*c
= link
->cgrp
;
732 if (c
->root
== root
) {
738 read_unlock(&css_set_lock
);
744 * There is one global cgroup mutex. We also require taking
745 * task_lock() when dereferencing a task's cgroup subsys pointers.
746 * See "The task_lock() exception", at the end of this comment.
748 * A task must hold cgroup_mutex to modify cgroups.
750 * Any task can increment and decrement the count field without lock.
751 * So in general, code holding cgroup_mutex can't rely on the count
752 * field not changing. However, if the count goes to zero, then only
753 * cgroup_attach_task() can increment it again. Because a count of zero
754 * means that no tasks are currently attached, therefore there is no
755 * way a task attached to that cgroup can fork (the other way to
756 * increment the count). So code holding cgroup_mutex can safely
757 * assume that if the count is zero, it will stay zero. Similarly, if
758 * a task holds cgroup_mutex on a cgroup with zero count, it
759 * knows that the cgroup won't be removed, as cgroup_rmdir()
762 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
763 * (usually) take cgroup_mutex. These are the two most performance
764 * critical pieces of code here. The exception occurs on cgroup_exit(),
765 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
766 * is taken, and if the cgroup count is zero, a usermode call made
767 * to the release agent with the name of the cgroup (path relative to
768 * the root of cgroup file system) as the argument.
770 * A cgroup can only be deleted if both its 'count' of using tasks
771 * is zero, and its list of 'children' cgroups is empty. Since all
772 * tasks in the system use _some_ cgroup, and since there is always at
773 * least one task in the system (init, pid == 1), therefore, top_cgroup
774 * always has either children cgroups and/or using tasks. So we don't
775 * need a special hack to ensure that top_cgroup cannot be deleted.
777 * The task_lock() exception
779 * The need for this exception arises from the action of
780 * cgroup_attach_task(), which overwrites one task's cgroup pointer with
781 * another. It does so using cgroup_mutex, however there are
782 * several performance critical places that need to reference
783 * task->cgroup without the expense of grabbing a system global
784 * mutex. Therefore except as noted below, when dereferencing or, as
785 * in cgroup_attach_task(), modifying a task's cgroup pointer we use
786 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
787 * the task_struct routinely used for such matters.
789 * P.S. One more locking exception. RCU is used to guard the
790 * update of a tasks cgroup pointer by cgroup_attach_task()
794 * A couple of forward declarations required, due to cyclic reference loop:
795 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
796 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
800 static int cgroup_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
);
801 static int cgroup_rmdir(struct inode
*unused_dir
, struct dentry
*dentry
);
802 static int cgroup_populate_dir(struct cgroup
*cgrp
, unsigned long subsys_mask
);
803 static const struct inode_operations cgroup_dir_inode_operations
;
804 static const struct file_operations proc_cgroupstats_operations
;
806 static struct backing_dev_info cgroup_backing_dev_info
= {
808 .capabilities
= BDI_CAP_NO_ACCT_AND_WRITEBACK
,
811 static struct inode
*cgroup_new_inode(umode_t mode
, struct super_block
*sb
)
813 struct inode
*inode
= new_inode(sb
);
816 inode
->i_ino
= get_next_ino();
817 inode
->i_mode
= mode
;
818 inode
->i_uid
= current_fsuid();
819 inode
->i_gid
= current_fsgid();
820 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
821 inode
->i_mapping
->backing_dev_info
= &cgroup_backing_dev_info
;
826 static struct cgroup_name
*cgroup_alloc_name(struct dentry
*dentry
)
828 struct cgroup_name
*name
;
830 name
= kmalloc(sizeof(*name
) + dentry
->d_name
.len
+ 1, GFP_KERNEL
);
833 strcpy(name
->name
, dentry
->d_name
.name
);
837 static void cgroup_free_fn(struct work_struct
*work
)
839 struct cgroup
*cgrp
= container_of(work
, struct cgroup
, destroy_work
);
841 mutex_lock(&cgroup_mutex
);
842 cgrp
->root
->number_of_cgroups
--;
843 mutex_unlock(&cgroup_mutex
);
846 * We get a ref to the parent's dentry, and put the ref when
847 * this cgroup is being freed, so it's guaranteed that the
848 * parent won't be destroyed before its children.
850 dput(cgrp
->parent
->dentry
);
853 * Drop the active superblock reference that we took when we
854 * created the cgroup. This will free cgrp->root, if we are
855 * holding the last reference to @sb.
857 deactivate_super(cgrp
->root
->sb
);
859 cgroup_pidlist_destroy_all(cgrp
);
861 simple_xattrs_free(&cgrp
->xattrs
);
863 kfree(rcu_dereference_raw(cgrp
->name
));
867 static void cgroup_free_rcu(struct rcu_head
*head
)
869 struct cgroup
*cgrp
= container_of(head
, struct cgroup
, rcu_head
);
871 INIT_WORK(&cgrp
->destroy_work
, cgroup_free_fn
);
872 queue_work(cgroup_destroy_wq
, &cgrp
->destroy_work
);
875 static void cgroup_diput(struct dentry
*dentry
, struct inode
*inode
)
877 /* is dentry a directory ? if so, kfree() associated cgroup */
878 if (S_ISDIR(inode
->i_mode
)) {
879 struct cgroup
*cgrp
= dentry
->d_fsdata
;
881 BUG_ON(!(cgroup_is_dead(cgrp
)));
884 * XXX: cgrp->id is only used to look up css's. As cgroup
885 * and css's lifetimes will be decoupled, it should be made
886 * per-subsystem and moved to css->id so that lookups are
887 * successful until the target css is released.
889 mutex_lock(&cgroup_mutex
);
890 idr_remove(&cgrp
->root
->cgroup_idr
, cgrp
->id
);
891 mutex_unlock(&cgroup_mutex
);
894 call_rcu(&cgrp
->rcu_head
, cgroup_free_rcu
);
896 struct cfent
*cfe
= __d_cfe(dentry
);
897 struct cgroup
*cgrp
= dentry
->d_parent
->d_fsdata
;
899 WARN_ONCE(!list_empty(&cfe
->node
) &&
900 cgrp
!= &cgrp
->root
->top_cgroup
,
901 "cfe still linked for %s\n", cfe
->type
->name
);
902 simple_xattrs_free(&cfe
->xattrs
);
908 static void remove_dir(struct dentry
*d
)
910 struct dentry
*parent
= dget(d
->d_parent
);
913 simple_rmdir(parent
->d_inode
, d
);
917 static void cgroup_rm_file(struct cgroup
*cgrp
, const struct cftype
*cft
)
921 lockdep_assert_held(&cgrp
->dentry
->d_inode
->i_mutex
);
922 lockdep_assert_held(&cgroup_mutex
);
925 * If we're doing cleanup due to failure of cgroup_create(),
926 * the corresponding @cfe may not exist.
928 list_for_each_entry(cfe
, &cgrp
->files
, node
) {
929 struct dentry
*d
= cfe
->dentry
;
931 if (cft
&& cfe
->type
!= cft
)
936 simple_unlink(cgrp
->dentry
->d_inode
, d
);
937 list_del_init(&cfe
->node
);
945 * cgroup_clear_dir - remove subsys files in a cgroup directory
946 * @cgrp: target cgroup
947 * @subsys_mask: mask of the subsystem ids whose files should be removed
949 static void cgroup_clear_dir(struct cgroup
*cgrp
, unsigned long subsys_mask
)
951 struct cgroup_subsys
*ss
;
954 for_each_subsys(ss
, i
) {
955 struct cftype_set
*set
;
957 if (!test_bit(i
, &subsys_mask
))
959 list_for_each_entry(set
, &ss
->cftsets
, node
)
960 cgroup_addrm_files(cgrp
, set
->cfts
, false);
965 * NOTE : the dentry must have been dget()'ed
967 static void cgroup_d_remove_dir(struct dentry
*dentry
)
969 struct dentry
*parent
;
971 parent
= dentry
->d_parent
;
972 spin_lock(&parent
->d_lock
);
973 spin_lock_nested(&dentry
->d_lock
, DENTRY_D_LOCK_NESTED
);
974 list_del_init(&dentry
->d_u
.d_child
);
975 spin_unlock(&dentry
->d_lock
);
976 spin_unlock(&parent
->d_lock
);
981 * Call with cgroup_mutex held. Drops reference counts on modules, including
982 * any duplicate ones that parse_cgroupfs_options took. If this function
983 * returns an error, no reference counts are touched.
985 static int rebind_subsystems(struct cgroupfs_root
*root
,
986 unsigned long added_mask
, unsigned removed_mask
)
988 struct cgroup
*cgrp
= &root
->top_cgroup
;
989 struct cgroup_subsys
*ss
;
990 unsigned long pinned
= 0;
993 BUG_ON(!mutex_is_locked(&cgroup_mutex
));
994 BUG_ON(!mutex_is_locked(&cgroup_root_mutex
));
996 /* Check that any added subsystems are currently free */
997 for_each_subsys(ss
, i
) {
998 if (!(added_mask
& (1 << i
)))
1001 /* is the subsystem mounted elsewhere? */
1002 if (ss
->root
!= &cgroup_dummy_root
) {
1007 /* pin the module */
1008 if (!try_module_get(ss
->module
)) {
1015 /* subsys could be missing if unloaded between parsing and here */
1016 if (added_mask
!= pinned
) {
1021 ret
= cgroup_populate_dir(cgrp
, added_mask
);
1026 * Nothing can fail from this point on. Remove files for the
1027 * removed subsystems and rebind each subsystem.
1029 cgroup_clear_dir(cgrp
, removed_mask
);
1031 for_each_subsys(ss
, i
) {
1032 unsigned long bit
= 1UL << i
;
1034 if (bit
& added_mask
) {
1035 /* We're binding this subsystem to this hierarchy */
1036 BUG_ON(cgroup_css(cgrp
, ss
));
1037 BUG_ON(!cgroup_css(cgroup_dummy_top
, ss
));
1038 BUG_ON(cgroup_css(cgroup_dummy_top
, ss
)->cgroup
!= cgroup_dummy_top
);
1040 rcu_assign_pointer(cgrp
->subsys
[i
],
1041 cgroup_css(cgroup_dummy_top
, ss
));
1042 cgroup_css(cgrp
, ss
)->cgroup
= cgrp
;
1046 ss
->bind(cgroup_css(cgrp
, ss
));
1048 /* refcount was already taken, and we're keeping it */
1049 root
->subsys_mask
|= bit
;
1050 } else if (bit
& removed_mask
) {
1051 /* We're removing this subsystem */
1052 BUG_ON(cgroup_css(cgrp
, ss
) != cgroup_css(cgroup_dummy_top
, ss
));
1053 BUG_ON(cgroup_css(cgrp
, ss
)->cgroup
!= cgrp
);
1056 ss
->bind(cgroup_css(cgroup_dummy_top
, ss
));
1058 cgroup_css(cgroup_dummy_top
, ss
)->cgroup
= cgroup_dummy_top
;
1059 RCU_INIT_POINTER(cgrp
->subsys
[i
], NULL
);
1061 cgroup_subsys
[i
]->root
= &cgroup_dummy_root
;
1063 /* subsystem is now free - drop reference on module */
1064 module_put(ss
->module
);
1065 root
->subsys_mask
&= ~bit
;
1070 * Mark @root has finished binding subsystems. @root->subsys_mask
1071 * now matches the bound subsystems.
1073 root
->flags
|= CGRP_ROOT_SUBSYS_BOUND
;
1078 for_each_subsys(ss
, i
)
1079 if (pinned
& (1 << i
))
1080 module_put(ss
->module
);
1084 static int cgroup_show_options(struct seq_file
*seq
, struct dentry
*dentry
)
1086 struct cgroupfs_root
*root
= dentry
->d_sb
->s_fs_info
;
1087 struct cgroup_subsys
*ss
;
1090 mutex_lock(&cgroup_root_mutex
);
1091 for_each_subsys(ss
, ssid
)
1092 if (root
->subsys_mask
& (1 << ssid
))
1093 seq_printf(seq
, ",%s", ss
->name
);
1094 if (root
->flags
& CGRP_ROOT_SANE_BEHAVIOR
)
1095 seq_puts(seq
, ",sane_behavior");
1096 if (root
->flags
& CGRP_ROOT_NOPREFIX
)
1097 seq_puts(seq
, ",noprefix");
1098 if (root
->flags
& CGRP_ROOT_XATTR
)
1099 seq_puts(seq
, ",xattr");
1100 if (strlen(root
->release_agent_path
))
1101 seq_printf(seq
, ",release_agent=%s", root
->release_agent_path
);
1102 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN
, &root
->top_cgroup
.flags
))
1103 seq_puts(seq
, ",clone_children");
1104 if (strlen(root
->name
))
1105 seq_printf(seq
, ",name=%s", root
->name
);
1106 mutex_unlock(&cgroup_root_mutex
);
1110 struct cgroup_sb_opts
{
1111 unsigned long subsys_mask
;
1112 unsigned long flags
;
1113 char *release_agent
;
1114 bool cpuset_clone_children
;
1116 /* User explicitly requested empty subsystem */
1119 struct cgroupfs_root
*new_root
;
1124 * Convert a hierarchy specifier into a bitmask of subsystems and
1125 * flags. Call with cgroup_mutex held to protect the cgroup_subsys[]
1126 * array. This function takes refcounts on subsystems to be used, unless it
1127 * returns error, in which case no refcounts are taken.
1129 static int parse_cgroupfs_options(char *data
, struct cgroup_sb_opts
*opts
)
1131 char *token
, *o
= data
;
1132 bool all_ss
= false, one_ss
= false;
1133 unsigned long mask
= (unsigned long)-1;
1134 struct cgroup_subsys
*ss
;
1137 BUG_ON(!mutex_is_locked(&cgroup_mutex
));
1139 #ifdef CONFIG_CPUSETS
1140 mask
= ~(1UL << cpuset_subsys_id
);
1143 memset(opts
, 0, sizeof(*opts
));
1145 while ((token
= strsep(&o
, ",")) != NULL
) {
1148 if (!strcmp(token
, "none")) {
1149 /* Explicitly have no subsystems */
1153 if (!strcmp(token
, "all")) {
1154 /* Mutually exclusive option 'all' + subsystem name */
1160 if (!strcmp(token
, "__DEVEL__sane_behavior")) {
1161 opts
->flags
|= CGRP_ROOT_SANE_BEHAVIOR
;
1164 if (!strcmp(token
, "noprefix")) {
1165 opts
->flags
|= CGRP_ROOT_NOPREFIX
;
1168 if (!strcmp(token
, "clone_children")) {
1169 opts
->cpuset_clone_children
= true;
1172 if (!strcmp(token
, "xattr")) {
1173 opts
->flags
|= CGRP_ROOT_XATTR
;
1176 if (!strncmp(token
, "release_agent=", 14)) {
1177 /* Specifying two release agents is forbidden */
1178 if (opts
->release_agent
)
1180 opts
->release_agent
=
1181 kstrndup(token
+ 14, PATH_MAX
- 1, GFP_KERNEL
);
1182 if (!opts
->release_agent
)
1186 if (!strncmp(token
, "name=", 5)) {
1187 const char *name
= token
+ 5;
1188 /* Can't specify an empty name */
1191 /* Must match [\w.-]+ */
1192 for (i
= 0; i
< strlen(name
); i
++) {
1196 if ((c
== '.') || (c
== '-') || (c
== '_'))
1200 /* Specifying two names is forbidden */
1203 opts
->name
= kstrndup(name
,
1204 MAX_CGROUP_ROOT_NAMELEN
- 1,
1212 for_each_subsys(ss
, i
) {
1213 if (strcmp(token
, ss
->name
))
1218 /* Mutually exclusive option 'all' + subsystem name */
1221 set_bit(i
, &opts
->subsys_mask
);
1226 if (i
== CGROUP_SUBSYS_COUNT
)
1231 * If the 'all' option was specified select all the subsystems,
1232 * otherwise if 'none', 'name=' and a subsystem name options
1233 * were not specified, let's default to 'all'
1235 if (all_ss
|| (!one_ss
&& !opts
->none
&& !opts
->name
))
1236 for_each_subsys(ss
, i
)
1238 set_bit(i
, &opts
->subsys_mask
);
1240 /* Consistency checks */
1242 if (opts
->flags
& CGRP_ROOT_SANE_BEHAVIOR
) {
1243 pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
1245 if (opts
->flags
& CGRP_ROOT_NOPREFIX
) {
1246 pr_err("cgroup: sane_behavior: noprefix is not allowed\n");
1250 if (opts
->cpuset_clone_children
) {
1251 pr_err("cgroup: sane_behavior: clone_children is not allowed\n");
1257 * Option noprefix was introduced just for backward compatibility
1258 * with the old cpuset, so we allow noprefix only if mounting just
1259 * the cpuset subsystem.
1261 if ((opts
->flags
& CGRP_ROOT_NOPREFIX
) && (opts
->subsys_mask
& mask
))
1265 /* Can't specify "none" and some subsystems */
1266 if (opts
->subsys_mask
&& opts
->none
)
1270 * We either have to specify by name or by subsystems. (So all
1271 * empty hierarchies must have a name).
1273 if (!opts
->subsys_mask
&& !opts
->name
)
1279 static int cgroup_remount(struct super_block
*sb
, int *flags
, char *data
)
1282 struct cgroupfs_root
*root
= sb
->s_fs_info
;
1283 struct cgroup
*cgrp
= &root
->top_cgroup
;
1284 struct cgroup_sb_opts opts
;
1285 unsigned long added_mask
, removed_mask
;
1287 if (root
->flags
& CGRP_ROOT_SANE_BEHAVIOR
) {
1288 pr_err("cgroup: sane_behavior: remount is not allowed\n");
1292 mutex_lock(&cgrp
->dentry
->d_inode
->i_mutex
);
1293 mutex_lock(&cgroup_mutex
);
1294 mutex_lock(&cgroup_root_mutex
);
1296 /* See what subsystems are wanted */
1297 ret
= parse_cgroupfs_options(data
, &opts
);
1301 if (opts
.subsys_mask
!= root
->subsys_mask
|| opts
.release_agent
)
1302 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1303 task_tgid_nr(current
), current
->comm
);
1305 added_mask
= opts
.subsys_mask
& ~root
->subsys_mask
;
1306 removed_mask
= root
->subsys_mask
& ~opts
.subsys_mask
;
1308 /* Don't allow flags or name to change at remount */
1309 if (((opts
.flags
^ root
->flags
) & CGRP_ROOT_OPTION_MASK
) ||
1310 (opts
.name
&& strcmp(opts
.name
, root
->name
))) {
1311 pr_err("cgroup: option or name mismatch, new: 0x%lx \"%s\", old: 0x%lx \"%s\"\n",
1312 opts
.flags
& CGRP_ROOT_OPTION_MASK
, opts
.name
?: "",
1313 root
->flags
& CGRP_ROOT_OPTION_MASK
, root
->name
);
1318 /* remounting is not allowed for populated hierarchies */
1319 if (root
->number_of_cgroups
> 1) {
1324 ret
= rebind_subsystems(root
, added_mask
, removed_mask
);
1328 if (opts
.release_agent
)
1329 strcpy(root
->release_agent_path
, opts
.release_agent
);
1331 kfree(opts
.release_agent
);
1333 mutex_unlock(&cgroup_root_mutex
);
1334 mutex_unlock(&cgroup_mutex
);
1335 mutex_unlock(&cgrp
->dentry
->d_inode
->i_mutex
);
1339 static const struct super_operations cgroup_ops
= {
1340 .statfs
= simple_statfs
,
1341 .drop_inode
= generic_delete_inode
,
1342 .show_options
= cgroup_show_options
,
1343 .remount_fs
= cgroup_remount
,
1346 static void init_cgroup_housekeeping(struct cgroup
*cgrp
)
1348 INIT_LIST_HEAD(&cgrp
->sibling
);
1349 INIT_LIST_HEAD(&cgrp
->children
);
1350 INIT_LIST_HEAD(&cgrp
->files
);
1351 INIT_LIST_HEAD(&cgrp
->cset_links
);
1352 INIT_LIST_HEAD(&cgrp
->release_list
);
1353 INIT_LIST_HEAD(&cgrp
->pidlists
);
1354 mutex_init(&cgrp
->pidlist_mutex
);
1355 cgrp
->dummy_css
.cgroup
= cgrp
;
1356 simple_xattrs_init(&cgrp
->xattrs
);
1359 static void init_cgroup_root(struct cgroupfs_root
*root
)
1361 struct cgroup
*cgrp
= &root
->top_cgroup
;
1363 INIT_LIST_HEAD(&root
->root_list
);
1364 root
->number_of_cgroups
= 1;
1366 RCU_INIT_POINTER(cgrp
->name
, &root_cgroup_name
);
1367 init_cgroup_housekeeping(cgrp
);
1368 idr_init(&root
->cgroup_idr
);
1371 static int cgroup_init_root_id(struct cgroupfs_root
*root
, int start
, int end
)
1375 lockdep_assert_held(&cgroup_mutex
);
1376 lockdep_assert_held(&cgroup_root_mutex
);
1378 id
= idr_alloc_cyclic(&cgroup_hierarchy_idr
, root
, start
, end
,
1383 root
->hierarchy_id
= id
;
1387 static void cgroup_exit_root_id(struct cgroupfs_root
*root
)
1389 lockdep_assert_held(&cgroup_mutex
);
1390 lockdep_assert_held(&cgroup_root_mutex
);
1392 if (root
->hierarchy_id
) {
1393 idr_remove(&cgroup_hierarchy_idr
, root
->hierarchy_id
);
1394 root
->hierarchy_id
= 0;
1398 static int cgroup_test_super(struct super_block
*sb
, void *data
)
1400 struct cgroup_sb_opts
*opts
= data
;
1401 struct cgroupfs_root
*root
= sb
->s_fs_info
;
1403 /* If we asked for a name then it must match */
1404 if (opts
->name
&& strcmp(opts
->name
, root
->name
))
1408 * If we asked for subsystems (or explicitly for no
1409 * subsystems) then they must match
1411 if ((opts
->subsys_mask
|| opts
->none
)
1412 && (opts
->subsys_mask
!= root
->subsys_mask
))
1418 static struct cgroupfs_root
*cgroup_root_from_opts(struct cgroup_sb_opts
*opts
)
1420 struct cgroupfs_root
*root
;
1422 if (!opts
->subsys_mask
&& !opts
->none
)
1425 root
= kzalloc(sizeof(*root
), GFP_KERNEL
);
1427 return ERR_PTR(-ENOMEM
);
1429 init_cgroup_root(root
);
1432 * We need to set @root->subsys_mask now so that @root can be
1433 * matched by cgroup_test_super() before it finishes
1434 * initialization; otherwise, competing mounts with the same
1435 * options may try to bind the same subsystems instead of waiting
1436 * for the first one leading to unexpected mount errors.
1437 * SUBSYS_BOUND will be set once actual binding is complete.
1439 root
->subsys_mask
= opts
->subsys_mask
;
1440 root
->flags
= opts
->flags
;
1441 if (opts
->release_agent
)
1442 strcpy(root
->release_agent_path
, opts
->release_agent
);
1444 strcpy(root
->name
, opts
->name
);
1445 if (opts
->cpuset_clone_children
)
1446 set_bit(CGRP_CPUSET_CLONE_CHILDREN
, &root
->top_cgroup
.flags
);
1450 static void cgroup_free_root(struct cgroupfs_root
*root
)
1453 /* hierarhcy ID shoulid already have been released */
1454 WARN_ON_ONCE(root
->hierarchy_id
);
1456 idr_destroy(&root
->cgroup_idr
);
1461 static int cgroup_set_super(struct super_block
*sb
, void *data
)
1464 struct cgroup_sb_opts
*opts
= data
;
1466 /* If we don't have a new root, we can't set up a new sb */
1467 if (!opts
->new_root
)
1470 BUG_ON(!opts
->subsys_mask
&& !opts
->none
);
1472 ret
= set_anon_super(sb
, NULL
);
1476 sb
->s_fs_info
= opts
->new_root
;
1477 opts
->new_root
->sb
= sb
;
1479 sb
->s_blocksize
= PAGE_CACHE_SIZE
;
1480 sb
->s_blocksize_bits
= PAGE_CACHE_SHIFT
;
1481 sb
->s_magic
= CGROUP_SUPER_MAGIC
;
1482 sb
->s_op
= &cgroup_ops
;
1487 static int cgroup_get_rootdir(struct super_block
*sb
)
1489 static const struct dentry_operations cgroup_dops
= {
1490 .d_iput
= cgroup_diput
,
1491 .d_delete
= always_delete_dentry
,
1494 struct inode
*inode
=
1495 cgroup_new_inode(S_IFDIR
| S_IRUGO
| S_IXUGO
| S_IWUSR
, sb
);
1500 inode
->i_fop
= &simple_dir_operations
;
1501 inode
->i_op
= &cgroup_dir_inode_operations
;
1502 /* directories start off with i_nlink == 2 (for "." entry) */
1504 sb
->s_root
= d_make_root(inode
);
1507 /* for everything else we want ->d_op set */
1508 sb
->s_d_op
= &cgroup_dops
;
1512 static struct dentry
*cgroup_mount(struct file_system_type
*fs_type
,
1513 int flags
, const char *unused_dev_name
,
1516 struct cgroup_sb_opts opts
;
1517 struct cgroupfs_root
*root
;
1519 struct super_block
*sb
;
1520 struct cgroupfs_root
*new_root
;
1521 struct list_head tmp_links
;
1522 struct inode
*inode
;
1523 const struct cred
*cred
;
1525 /* First find the desired set of subsystems */
1526 mutex_lock(&cgroup_mutex
);
1527 ret
= parse_cgroupfs_options(data
, &opts
);
1528 mutex_unlock(&cgroup_mutex
);
1533 * Allocate a new cgroup root. We may not need it if we're
1534 * reusing an existing hierarchy.
1536 new_root
= cgroup_root_from_opts(&opts
);
1537 if (IS_ERR(new_root
)) {
1538 ret
= PTR_ERR(new_root
);
1541 opts
.new_root
= new_root
;
1543 /* Locate an existing or new sb for this hierarchy */
1544 sb
= sget(fs_type
, cgroup_test_super
, cgroup_set_super
, 0, &opts
);
1547 cgroup_free_root(opts
.new_root
);
1551 root
= sb
->s_fs_info
;
1553 if (root
== opts
.new_root
) {
1554 /* We used the new root structure, so this is a new hierarchy */
1555 struct cgroup
*root_cgrp
= &root
->top_cgroup
;
1556 struct cgroupfs_root
*existing_root
;
1558 struct css_set
*cset
;
1560 BUG_ON(sb
->s_root
!= NULL
);
1562 ret
= cgroup_get_rootdir(sb
);
1564 goto drop_new_super
;
1565 inode
= sb
->s_root
->d_inode
;
1567 mutex_lock(&inode
->i_mutex
);
1568 mutex_lock(&cgroup_mutex
);
1569 mutex_lock(&cgroup_root_mutex
);
1571 ret
= idr_alloc(&root
->cgroup_idr
, root_cgrp
, 0, 1, GFP_KERNEL
);
1574 root_cgrp
->id
= ret
;
1576 /* Check for name clashes with existing mounts */
1578 if (strlen(root
->name
))
1579 for_each_active_root(existing_root
)
1580 if (!strcmp(existing_root
->name
, root
->name
))
1584 * We're accessing css_set_count without locking
1585 * css_set_lock here, but that's OK - it can only be
1586 * increased by someone holding cgroup_lock, and
1587 * that's us. The worst that can happen is that we
1588 * have some link structures left over
1590 ret
= allocate_cgrp_cset_links(css_set_count
, &tmp_links
);
1594 /* ID 0 is reserved for dummy root, 1 for unified hierarchy */
1595 ret
= cgroup_init_root_id(root
, 2, 0);
1599 sb
->s_root
->d_fsdata
= root_cgrp
;
1600 root_cgrp
->dentry
= sb
->s_root
;
1603 * We're inside get_sb() and will call lookup_one_len() to
1604 * create the root files, which doesn't work if SELinux is
1605 * in use. The following cred dancing somehow works around
1606 * it. See 2ce9738ba ("cgroupfs: use init_cred when
1607 * populating new cgroupfs mount") for more details.
1609 cred
= override_creds(&init_cred
);
1611 ret
= cgroup_addrm_files(root_cgrp
, cgroup_base_files
, true);
1615 ret
= rebind_subsystems(root
, root
->subsys_mask
, 0);
1622 * There must be no failure case after here, since rebinding
1623 * takes care of subsystems' refcounts, which are explicitly
1624 * dropped in the failure exit path.
1627 list_add(&root
->root_list
, &cgroup_roots
);
1628 cgroup_root_count
++;
1630 /* Link the top cgroup in this hierarchy into all
1631 * the css_set objects */
1632 write_lock(&css_set_lock
);
1633 hash_for_each(css_set_table
, i
, cset
, hlist
)
1634 link_css_set(&tmp_links
, cset
, root_cgrp
);
1635 write_unlock(&css_set_lock
);
1637 free_cgrp_cset_links(&tmp_links
);
1639 BUG_ON(!list_empty(&root_cgrp
->children
));
1640 BUG_ON(root
->number_of_cgroups
!= 1);
1642 mutex_unlock(&cgroup_root_mutex
);
1643 mutex_unlock(&cgroup_mutex
);
1644 mutex_unlock(&inode
->i_mutex
);
1647 * We re-used an existing hierarchy - the new root (if
1648 * any) is not needed
1650 cgroup_free_root(opts
.new_root
);
1652 if ((root
->flags
^ opts
.flags
) & CGRP_ROOT_OPTION_MASK
) {
1653 if ((root
->flags
| opts
.flags
) & CGRP_ROOT_SANE_BEHAVIOR
) {
1654 pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
1656 goto drop_new_super
;
1658 pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n");
1663 kfree(opts
.release_agent
);
1665 return dget(sb
->s_root
);
1668 free_cgrp_cset_links(&tmp_links
);
1669 cgroup_addrm_files(&root
->top_cgroup
, cgroup_base_files
, false);
1672 cgroup_exit_root_id(root
);
1673 mutex_unlock(&cgroup_root_mutex
);
1674 mutex_unlock(&cgroup_mutex
);
1675 mutex_unlock(&inode
->i_mutex
);
1677 deactivate_locked_super(sb
);
1679 kfree(opts
.release_agent
);
1681 return ERR_PTR(ret
);
1684 static void cgroup_kill_sb(struct super_block
*sb
)
1686 struct cgroupfs_root
*root
= sb
->s_fs_info
;
1687 struct cgroup
*cgrp
= &root
->top_cgroup
;
1688 struct cgrp_cset_link
*link
, *tmp_link
;
1693 BUG_ON(root
->number_of_cgroups
!= 1);
1694 BUG_ON(!list_empty(&cgrp
->children
));
1696 mutex_lock(&cgrp
->dentry
->d_inode
->i_mutex
);
1697 mutex_lock(&cgroup_mutex
);
1698 mutex_lock(&cgroup_root_mutex
);
1700 /* Rebind all subsystems back to the default hierarchy */
1701 if (root
->flags
& CGRP_ROOT_SUBSYS_BOUND
) {
1702 ret
= rebind_subsystems(root
, 0, root
->subsys_mask
);
1703 /* Shouldn't be able to fail ... */
1708 * Release all the links from cset_links to this hierarchy's
1711 write_lock(&css_set_lock
);
1713 list_for_each_entry_safe(link
, tmp_link
, &cgrp
->cset_links
, cset_link
) {
1714 list_del(&link
->cset_link
);
1715 list_del(&link
->cgrp_link
);
1718 write_unlock(&css_set_lock
);
1720 if (!list_empty(&root
->root_list
)) {
1721 list_del(&root
->root_list
);
1722 cgroup_root_count
--;
1725 cgroup_exit_root_id(root
);
1727 mutex_unlock(&cgroup_root_mutex
);
1728 mutex_unlock(&cgroup_mutex
);
1729 mutex_unlock(&cgrp
->dentry
->d_inode
->i_mutex
);
1731 simple_xattrs_free(&cgrp
->xattrs
);
1733 kill_litter_super(sb
);
1734 cgroup_free_root(root
);
1737 static struct file_system_type cgroup_fs_type
= {
1739 .mount
= cgroup_mount
,
1740 .kill_sb
= cgroup_kill_sb
,
1743 static struct kobject
*cgroup_kobj
;
1746 * cgroup_path - generate the path of a cgroup
1747 * @cgrp: the cgroup in question
1748 * @buf: the buffer to write the path into
1749 * @buflen: the length of the buffer
1751 * Writes path of cgroup into buf. Returns 0 on success, -errno on error.
1753 * We can't generate cgroup path using dentry->d_name, as accessing
1754 * dentry->name must be protected by irq-unsafe dentry->d_lock or parent
1755 * inode's i_mutex, while on the other hand cgroup_path() can be called
1756 * with some irq-safe spinlocks held.
1758 int cgroup_path(const struct cgroup
*cgrp
, char *buf
, int buflen
)
1760 int ret
= -ENAMETOOLONG
;
1763 if (!cgrp
->parent
) {
1764 if (strlcpy(buf
, "/", buflen
) >= buflen
)
1765 return -ENAMETOOLONG
;
1769 start
= buf
+ buflen
- 1;
1774 const char *name
= cgroup_name(cgrp
);
1778 if ((start
-= len
) < buf
)
1780 memcpy(start
, name
, len
);
1786 cgrp
= cgrp
->parent
;
1787 } while (cgrp
->parent
);
1789 memmove(buf
, start
, buf
+ buflen
- start
);
1794 EXPORT_SYMBOL_GPL(cgroup_path
);
1797 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
1798 * @task: target task
1799 * @buf: the buffer to write the path into
1800 * @buflen: the length of the buffer
1802 * Determine @task's cgroup on the first (the one with the lowest non-zero
1803 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
1804 * function grabs cgroup_mutex and shouldn't be used inside locks used by
1805 * cgroup controller callbacks.
1807 * Returns 0 on success, fails with -%ENAMETOOLONG if @buflen is too short.
1809 int task_cgroup_path(struct task_struct
*task
, char *buf
, size_t buflen
)
1811 struct cgroupfs_root
*root
;
1812 struct cgroup
*cgrp
;
1813 int hierarchy_id
= 1, ret
= 0;
1816 return -ENAMETOOLONG
;
1818 mutex_lock(&cgroup_mutex
);
1820 root
= idr_get_next(&cgroup_hierarchy_idr
, &hierarchy_id
);
1823 cgrp
= task_cgroup_from_root(task
, root
);
1824 ret
= cgroup_path(cgrp
, buf
, buflen
);
1826 /* if no hierarchy exists, everyone is in "/" */
1827 memcpy(buf
, "/", 2);
1830 mutex_unlock(&cgroup_mutex
);
1833 EXPORT_SYMBOL_GPL(task_cgroup_path
);
1836 * Control Group taskset
1838 struct task_and_cgroup
{
1839 struct task_struct
*task
;
1840 struct cgroup
*cgrp
;
1841 struct css_set
*cset
;
1844 struct cgroup_taskset
{
1845 struct task_and_cgroup single
;
1846 struct flex_array
*tc_array
;
1849 struct cgroup
*cur_cgrp
;
1853 * cgroup_taskset_first - reset taskset and return the first task
1854 * @tset: taskset of interest
1856 * @tset iteration is initialized and the first task is returned.
1858 struct task_struct
*cgroup_taskset_first(struct cgroup_taskset
*tset
)
1860 if (tset
->tc_array
) {
1862 return cgroup_taskset_next(tset
);
1864 tset
->cur_cgrp
= tset
->single
.cgrp
;
1865 return tset
->single
.task
;
1868 EXPORT_SYMBOL_GPL(cgroup_taskset_first
);
1871 * cgroup_taskset_next - iterate to the next task in taskset
1872 * @tset: taskset of interest
1874 * Return the next task in @tset. Iteration must have been initialized
1875 * with cgroup_taskset_first().
1877 struct task_struct
*cgroup_taskset_next(struct cgroup_taskset
*tset
)
1879 struct task_and_cgroup
*tc
;
1881 if (!tset
->tc_array
|| tset
->idx
>= tset
->tc_array_len
)
1884 tc
= flex_array_get(tset
->tc_array
, tset
->idx
++);
1885 tset
->cur_cgrp
= tc
->cgrp
;
1888 EXPORT_SYMBOL_GPL(cgroup_taskset_next
);
1891 * cgroup_taskset_cur_css - return the matching css for the current task
1892 * @tset: taskset of interest
1893 * @subsys_id: the ID of the target subsystem
1895 * Return the css for the current (last returned) task of @tset for
1896 * subsystem specified by @subsys_id. This function must be preceded by
1897 * either cgroup_taskset_first() or cgroup_taskset_next().
1899 struct cgroup_subsys_state
*cgroup_taskset_cur_css(struct cgroup_taskset
*tset
,
1902 return cgroup_css(tset
->cur_cgrp
, cgroup_subsys
[subsys_id
]);
1904 EXPORT_SYMBOL_GPL(cgroup_taskset_cur_css
);
1907 * cgroup_taskset_size - return the number of tasks in taskset
1908 * @tset: taskset of interest
1910 int cgroup_taskset_size(struct cgroup_taskset
*tset
)
1912 return tset
->tc_array
? tset
->tc_array_len
: 1;
1914 EXPORT_SYMBOL_GPL(cgroup_taskset_size
);
1918 * cgroup_task_migrate - move a task from one cgroup to another.
1920 * Must be called with cgroup_mutex and threadgroup locked.
1922 static void cgroup_task_migrate(struct cgroup
*old_cgrp
,
1923 struct task_struct
*tsk
,
1924 struct css_set
*new_cset
)
1926 struct css_set
*old_cset
;
1929 * We are synchronized through threadgroup_lock() against PF_EXITING
1930 * setting such that we can't race against cgroup_exit() changing the
1931 * css_set to init_css_set and dropping the old one.
1933 WARN_ON_ONCE(tsk
->flags
& PF_EXITING
);
1934 old_cset
= task_css_set(tsk
);
1937 rcu_assign_pointer(tsk
->cgroups
, new_cset
);
1940 /* Update the css_set linked lists if we're using them */
1941 write_lock(&css_set_lock
);
1942 if (!list_empty(&tsk
->cg_list
))
1943 list_move(&tsk
->cg_list
, &new_cset
->tasks
);
1944 write_unlock(&css_set_lock
);
1947 * We just gained a reference on old_cset by taking it from the
1948 * task. As trading it for new_cset is protected by cgroup_mutex,
1949 * we're safe to drop it here; it will be freed under RCU.
1951 set_bit(CGRP_RELEASABLE
, &old_cgrp
->flags
);
1952 put_css_set(old_cset
);
1956 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
1957 * @cgrp: the cgroup to attach to
1958 * @tsk: the task or the leader of the threadgroup to be attached
1959 * @threadgroup: attach the whole threadgroup?
1961 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
1962 * task_lock of @tsk or each thread in the threadgroup individually in turn.
1964 static int cgroup_attach_task(struct cgroup
*cgrp
, struct task_struct
*tsk
,
1967 int retval
, i
, group_size
;
1968 struct cgroupfs_root
*root
= cgrp
->root
;
1969 struct cgroup_subsys_state
*css
, *failed_css
= NULL
;
1970 /* threadgroup list cursor and array */
1971 struct task_struct
*leader
= tsk
;
1972 struct task_and_cgroup
*tc
;
1973 struct flex_array
*group
;
1974 struct cgroup_taskset tset
= { };
1977 * step 0: in order to do expensive, possibly blocking operations for
1978 * every thread, we cannot iterate the thread group list, since it needs
1979 * rcu or tasklist locked. instead, build an array of all threads in the
1980 * group - group_rwsem prevents new threads from appearing, and if
1981 * threads exit, this will just be an over-estimate.
1984 group_size
= get_nr_threads(tsk
);
1987 /* flex_array supports very large thread-groups better than kmalloc. */
1988 group
= flex_array_alloc(sizeof(*tc
), group_size
, GFP_KERNEL
);
1991 /* pre-allocate to guarantee space while iterating in rcu read-side. */
1992 retval
= flex_array_prealloc(group
, 0, group_size
, GFP_KERNEL
);
1994 goto out_free_group_list
;
1998 * Prevent freeing of tasks while we take a snapshot. Tasks that are
1999 * already PF_EXITING could be freed from underneath us unless we
2000 * take an rcu_read_lock.
2004 struct task_and_cgroup ent
;
2006 /* @tsk either already exited or can't exit until the end */
2007 if (tsk
->flags
& PF_EXITING
)
2010 /* as per above, nr_threads may decrease, but not increase. */
2011 BUG_ON(i
>= group_size
);
2013 ent
.cgrp
= task_cgroup_from_root(tsk
, root
);
2014 /* nothing to do if this task is already in the cgroup */
2015 if (ent
.cgrp
== cgrp
)
2018 * saying GFP_ATOMIC has no effect here because we did prealloc
2019 * earlier, but it's good form to communicate our expectations.
2021 retval
= flex_array_put(group
, i
, &ent
, GFP_ATOMIC
);
2022 BUG_ON(retval
!= 0);
2027 } while_each_thread(leader
, tsk
);
2029 /* remember the number of threads in the array for later. */
2031 tset
.tc_array
= group
;
2032 tset
.tc_array_len
= group_size
;
2034 /* methods shouldn't be called if no task is actually migrating */
2037 goto out_free_group_list
;
2040 * step 1: check that we can legitimately attach to the cgroup.
2042 for_each_css(css
, i
, cgrp
) {
2043 if (css
->ss
->can_attach
) {
2044 retval
= css
->ss
->can_attach(css
, &tset
);
2047 goto out_cancel_attach
;
2053 * step 2: make sure css_sets exist for all threads to be migrated.
2054 * we use find_css_set, which allocates a new one if necessary.
2056 for (i
= 0; i
< group_size
; i
++) {
2057 struct css_set
*old_cset
;
2059 tc
= flex_array_get(group
, i
);
2060 old_cset
= task_css_set(tc
->task
);
2061 tc
->cset
= find_css_set(old_cset
, cgrp
);
2064 goto out_put_css_set_refs
;
2069 * step 3: now that we're guaranteed success wrt the css_sets,
2070 * proceed to move all tasks to the new cgroup. There are no
2071 * failure cases after here, so this is the commit point.
2073 for (i
= 0; i
< group_size
; i
++) {
2074 tc
= flex_array_get(group
, i
);
2075 cgroup_task_migrate(tc
->cgrp
, tc
->task
, tc
->cset
);
2077 /* nothing is sensitive to fork() after this point. */
2080 * step 4: do subsystem attach callbacks.
2082 for_each_css(css
, i
, cgrp
)
2083 if (css
->ss
->attach
)
2084 css
->ss
->attach(css
, &tset
);
2087 * step 5: success! and cleanup
2090 out_put_css_set_refs
:
2092 for (i
= 0; i
< group_size
; i
++) {
2093 tc
= flex_array_get(group
, i
);
2096 put_css_set(tc
->cset
);
2101 for_each_css(css
, i
, cgrp
) {
2102 if (css
== failed_css
)
2104 if (css
->ss
->cancel_attach
)
2105 css
->ss
->cancel_attach(css
, &tset
);
2108 out_free_group_list
:
2109 flex_array_free(group
);
2114 * Find the task_struct of the task to attach by vpid and pass it along to the
2115 * function to attach either it or all tasks in its threadgroup. Will lock
2116 * cgroup_mutex and threadgroup; may take task_lock of task.
2118 static int attach_task_by_pid(struct cgroup
*cgrp
, u64 pid
, bool threadgroup
)
2120 struct task_struct
*tsk
;
2121 const struct cred
*cred
= current_cred(), *tcred
;
2124 if (!cgroup_lock_live_group(cgrp
))
2130 tsk
= find_task_by_vpid(pid
);
2134 goto out_unlock_cgroup
;
2137 * even if we're attaching all tasks in the thread group, we
2138 * only need to check permissions on one of them.
2140 tcred
= __task_cred(tsk
);
2141 if (!uid_eq(cred
->euid
, GLOBAL_ROOT_UID
) &&
2142 !uid_eq(cred
->euid
, tcred
->uid
) &&
2143 !uid_eq(cred
->euid
, tcred
->suid
)) {
2146 goto out_unlock_cgroup
;
2152 tsk
= tsk
->group_leader
;
2155 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
2156 * trapped in a cpuset, or RT worker may be born in a cgroup
2157 * with no rt_runtime allocated. Just say no.
2159 if (tsk
== kthreadd_task
|| (tsk
->flags
& PF_NO_SETAFFINITY
)) {
2162 goto out_unlock_cgroup
;
2165 get_task_struct(tsk
);
2168 threadgroup_lock(tsk
);
2170 if (!thread_group_leader(tsk
)) {
2172 * a race with de_thread from another thread's exec()
2173 * may strip us of our leadership, if this happens,
2174 * there is no choice but to throw this task away and
2175 * try again; this is
2176 * "double-double-toil-and-trouble-check locking".
2178 threadgroup_unlock(tsk
);
2179 put_task_struct(tsk
);
2180 goto retry_find_task
;
2184 ret
= cgroup_attach_task(cgrp
, tsk
, threadgroup
);
2186 threadgroup_unlock(tsk
);
2188 put_task_struct(tsk
);
2190 mutex_unlock(&cgroup_mutex
);
2195 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2196 * @from: attach to all cgroups of a given task
2197 * @tsk: the task to be attached
2199 int cgroup_attach_task_all(struct task_struct
*from
, struct task_struct
*tsk
)
2201 struct cgroupfs_root
*root
;
2204 mutex_lock(&cgroup_mutex
);
2205 for_each_active_root(root
) {
2206 struct cgroup
*from_cgrp
= task_cgroup_from_root(from
, root
);
2208 retval
= cgroup_attach_task(from_cgrp
, tsk
, false);
2212 mutex_unlock(&cgroup_mutex
);
2216 EXPORT_SYMBOL_GPL(cgroup_attach_task_all
);
2218 static int cgroup_tasks_write(struct cgroup_subsys_state
*css
,
2219 struct cftype
*cft
, u64 pid
)
2221 return attach_task_by_pid(css
->cgroup
, pid
, false);
2224 static int cgroup_procs_write(struct cgroup_subsys_state
*css
,
2225 struct cftype
*cft
, u64 tgid
)
2227 return attach_task_by_pid(css
->cgroup
, tgid
, true);
2230 static int cgroup_release_agent_write(struct cgroup_subsys_state
*css
,
2231 struct cftype
*cft
, const char *buffer
)
2233 BUILD_BUG_ON(sizeof(css
->cgroup
->root
->release_agent_path
) < PATH_MAX
);
2234 if (strlen(buffer
) >= PATH_MAX
)
2236 if (!cgroup_lock_live_group(css
->cgroup
))
2238 mutex_lock(&cgroup_root_mutex
);
2239 strcpy(css
->cgroup
->root
->release_agent_path
, buffer
);
2240 mutex_unlock(&cgroup_root_mutex
);
2241 mutex_unlock(&cgroup_mutex
);
2245 static int cgroup_release_agent_show(struct seq_file
*seq
, void *v
)
2247 struct cgroup
*cgrp
= seq_css(seq
)->cgroup
;
2249 if (!cgroup_lock_live_group(cgrp
))
2251 seq_puts(seq
, cgrp
->root
->release_agent_path
);
2252 seq_putc(seq
, '\n');
2253 mutex_unlock(&cgroup_mutex
);
2257 static int cgroup_sane_behavior_show(struct seq_file
*seq
, void *v
)
2259 struct cgroup
*cgrp
= seq_css(seq
)->cgroup
;
2261 seq_printf(seq
, "%d\n", cgroup_sane_behavior(cgrp
));
2265 /* A buffer size big enough for numbers or short strings */
2266 #define CGROUP_LOCAL_BUFFER_SIZE 64
2268 static ssize_t
cgroup_file_write(struct file
*file
, const char __user
*userbuf
,
2269 size_t nbytes
, loff_t
*ppos
)
2271 struct cfent
*cfe
= __d_cfe(file
->f_dentry
);
2272 struct cftype
*cft
= __d_cft(file
->f_dentry
);
2273 struct cgroup_subsys_state
*css
= cfe
->css
;
2274 size_t max_bytes
= cft
->max_write_len
?: CGROUP_LOCAL_BUFFER_SIZE
- 1;
2278 if (nbytes
>= max_bytes
)
2281 buf
= kmalloc(nbytes
+ 1, GFP_KERNEL
);
2285 if (copy_from_user(buf
, userbuf
, nbytes
)) {
2292 if (cft
->write_string
) {
2293 ret
= cft
->write_string(css
, cft
, strstrip(buf
));
2294 } else if (cft
->write_u64
) {
2295 unsigned long long v
;
2296 ret
= kstrtoull(buf
, 0, &v
);
2298 ret
= cft
->write_u64(css
, cft
, v
);
2299 } else if (cft
->write_s64
) {
2301 ret
= kstrtoll(buf
, 0, &v
);
2303 ret
= cft
->write_s64(css
, cft
, v
);
2304 } else if (cft
->trigger
) {
2305 ret
= cft
->trigger(css
, (unsigned int)cft
->private);
2311 return ret
?: nbytes
;
2315 * seqfile ops/methods for returning structured data. Currently just
2316 * supports string->u64 maps, but can be extended in future.
2319 static void *cgroup_seqfile_start(struct seq_file
*seq
, loff_t
*ppos
)
2321 struct cftype
*cft
= seq_cft(seq
);
2323 if (cft
->seq_start
) {
2324 return cft
->seq_start(seq
, ppos
);
2327 * The same behavior and code as single_open(). Returns
2328 * !NULL if pos is at the beginning; otherwise, NULL.
2330 return NULL
+ !*ppos
;
2334 static void *cgroup_seqfile_next(struct seq_file
*seq
, void *v
, loff_t
*ppos
)
2336 struct cftype
*cft
= seq_cft(seq
);
2338 if (cft
->seq_next
) {
2339 return cft
->seq_next(seq
, v
, ppos
);
2342 * The same behavior and code as single_open(), always
2343 * terminate after the initial read.
2350 static void cgroup_seqfile_stop(struct seq_file
*seq
, void *v
)
2352 struct cftype
*cft
= seq_cft(seq
);
2355 cft
->seq_stop(seq
, v
);
2358 static int cgroup_seqfile_show(struct seq_file
*m
, void *arg
)
2360 struct cftype
*cft
= seq_cft(m
);
2361 struct cgroup_subsys_state
*css
= seq_css(m
);
2364 return cft
->seq_show(m
, arg
);
2367 seq_printf(m
, "%llu\n", cft
->read_u64(css
, cft
));
2368 else if (cft
->read_s64
)
2369 seq_printf(m
, "%lld\n", cft
->read_s64(css
, cft
));
2375 static struct seq_operations cgroup_seq_operations
= {
2376 .start
= cgroup_seqfile_start
,
2377 .next
= cgroup_seqfile_next
,
2378 .stop
= cgroup_seqfile_stop
,
2379 .show
= cgroup_seqfile_show
,
2382 static int cgroup_file_open(struct inode
*inode
, struct file
*file
)
2384 struct cfent
*cfe
= __d_cfe(file
->f_dentry
);
2385 struct cftype
*cft
= __d_cft(file
->f_dentry
);
2386 struct cgroup
*cgrp
= __d_cgrp(cfe
->dentry
->d_parent
);
2387 struct cgroup_subsys_state
*css
;
2388 struct cgroup_open_file
*of
;
2391 err
= generic_file_open(inode
, file
);
2396 * If the file belongs to a subsystem, pin the css. Will be
2397 * unpinned either on open failure or release. This ensures that
2398 * @css stays alive for all file operations.
2401 css
= cgroup_css(cgrp
, cft
->ss
);
2402 if (cft
->ss
&& !css_tryget(css
))
2410 * @cfe->css is used by read/write/close to determine the
2411 * associated css. @file->private_data would be a better place but
2412 * that's already used by seqfile. Multiple accessors may use it
2413 * simultaneously which is okay as the association never changes.
2415 WARN_ON_ONCE(cfe
->css
&& cfe
->css
!= css
);
2418 of
= __seq_open_private(file
, &cgroup_seq_operations
,
2419 sizeof(struct cgroup_open_file
));
2430 static int cgroup_file_release(struct inode
*inode
, struct file
*file
)
2432 struct cfent
*cfe
= __d_cfe(file
->f_dentry
);
2433 struct cgroup_subsys_state
*css
= cfe
->css
;
2437 return seq_release_private(inode
, file
);
2441 * cgroup_rename - Only allow simple rename of directories in place.
2443 static int cgroup_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
2444 struct inode
*new_dir
, struct dentry
*new_dentry
)
2447 struct cgroup_name
*name
, *old_name
;
2448 struct cgroup
*cgrp
;
2451 * It's convinient to use parent dir's i_mutex to protected
2454 lockdep_assert_held(&old_dir
->i_mutex
);
2456 if (!S_ISDIR(old_dentry
->d_inode
->i_mode
))
2458 if (new_dentry
->d_inode
)
2460 if (old_dir
!= new_dir
)
2463 cgrp
= __d_cgrp(old_dentry
);
2466 * This isn't a proper migration and its usefulness is very
2467 * limited. Disallow if sane_behavior.
2469 if (cgroup_sane_behavior(cgrp
))
2472 name
= cgroup_alloc_name(new_dentry
);
2476 ret
= simple_rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
2482 old_name
= rcu_dereference_protected(cgrp
->name
, true);
2483 rcu_assign_pointer(cgrp
->name
, name
);
2485 kfree_rcu(old_name
, rcu_head
);
2489 static struct simple_xattrs
*__d_xattrs(struct dentry
*dentry
)
2491 if (S_ISDIR(dentry
->d_inode
->i_mode
))
2492 return &__d_cgrp(dentry
)->xattrs
;
2494 return &__d_cfe(dentry
)->xattrs
;
2497 static inline int xattr_enabled(struct dentry
*dentry
)
2499 struct cgroupfs_root
*root
= dentry
->d_sb
->s_fs_info
;
2500 return root
->flags
& CGRP_ROOT_XATTR
;
2503 static bool is_valid_xattr(const char *name
)
2505 if (!strncmp(name
, XATTR_TRUSTED_PREFIX
, XATTR_TRUSTED_PREFIX_LEN
) ||
2506 !strncmp(name
, XATTR_SECURITY_PREFIX
, XATTR_SECURITY_PREFIX_LEN
))
2511 static int cgroup_setxattr(struct dentry
*dentry
, const char *name
,
2512 const void *val
, size_t size
, int flags
)
2514 if (!xattr_enabled(dentry
))
2516 if (!is_valid_xattr(name
))
2518 return simple_xattr_set(__d_xattrs(dentry
), name
, val
, size
, flags
);
2521 static int cgroup_removexattr(struct dentry
*dentry
, const char *name
)
2523 if (!xattr_enabled(dentry
))
2525 if (!is_valid_xattr(name
))
2527 return simple_xattr_remove(__d_xattrs(dentry
), name
);
2530 static ssize_t
cgroup_getxattr(struct dentry
*dentry
, const char *name
,
2531 void *buf
, size_t size
)
2533 if (!xattr_enabled(dentry
))
2535 if (!is_valid_xattr(name
))
2537 return simple_xattr_get(__d_xattrs(dentry
), name
, buf
, size
);
2540 static ssize_t
cgroup_listxattr(struct dentry
*dentry
, char *buf
, size_t size
)
2542 if (!xattr_enabled(dentry
))
2544 return simple_xattr_list(__d_xattrs(dentry
), buf
, size
);
2547 static const struct file_operations cgroup_file_operations
= {
2549 .write
= cgroup_file_write
,
2550 .llseek
= generic_file_llseek
,
2551 .open
= cgroup_file_open
,
2552 .release
= cgroup_file_release
,
2555 static const struct inode_operations cgroup_file_inode_operations
= {
2556 .setxattr
= cgroup_setxattr
,
2557 .getxattr
= cgroup_getxattr
,
2558 .listxattr
= cgroup_listxattr
,
2559 .removexattr
= cgroup_removexattr
,
2562 static const struct inode_operations cgroup_dir_inode_operations
= {
2563 .lookup
= simple_lookup
,
2564 .mkdir
= cgroup_mkdir
,
2565 .rmdir
= cgroup_rmdir
,
2566 .rename
= cgroup_rename
,
2567 .setxattr
= cgroup_setxattr
,
2568 .getxattr
= cgroup_getxattr
,
2569 .listxattr
= cgroup_listxattr
,
2570 .removexattr
= cgroup_removexattr
,
2573 static int cgroup_create_file(struct dentry
*dentry
, umode_t mode
,
2574 struct super_block
*sb
)
2576 struct inode
*inode
;
2580 if (dentry
->d_inode
)
2583 inode
= cgroup_new_inode(mode
, sb
);
2587 if (S_ISDIR(mode
)) {
2588 inode
->i_op
= &cgroup_dir_inode_operations
;
2589 inode
->i_fop
= &simple_dir_operations
;
2591 /* start off with i_nlink == 2 (for "." entry) */
2593 inc_nlink(dentry
->d_parent
->d_inode
);
2596 * Control reaches here with cgroup_mutex held.
2597 * @inode->i_mutex should nest outside cgroup_mutex but we
2598 * want to populate it immediately without releasing
2599 * cgroup_mutex. As @inode isn't visible to anyone else
2600 * yet, trylock will always succeed without affecting
2603 WARN_ON_ONCE(!mutex_trylock(&inode
->i_mutex
));
2604 } else if (S_ISREG(mode
)) {
2606 inode
->i_fop
= &cgroup_file_operations
;
2607 inode
->i_op
= &cgroup_file_inode_operations
;
2609 d_instantiate(dentry
, inode
);
2610 dget(dentry
); /* Extra count - pin the dentry in core */
2615 * cgroup_file_mode - deduce file mode of a control file
2616 * @cft: the control file in question
2618 * returns cft->mode if ->mode is not 0
2619 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2620 * returns S_IRUGO if it has only a read handler
2621 * returns S_IWUSR if it has only a write hander
2623 static umode_t
cgroup_file_mode(const struct cftype
*cft
)
2630 if (cft
->read_u64
|| cft
->read_s64
|| cft
->seq_show
)
2633 if (cft
->write_u64
|| cft
->write_s64
|| cft
->write_string
||
2640 static int cgroup_add_file(struct cgroup
*cgrp
, struct cftype
*cft
)
2642 struct dentry
*dir
= cgrp
->dentry
;
2643 struct cgroup
*parent
= __d_cgrp(dir
);
2644 struct dentry
*dentry
;
2648 char name
[MAX_CGROUP_TYPE_NAMELEN
+ MAX_CFTYPE_NAME
+ 2] = { 0 };
2650 if (cft
->ss
&& !(cft
->flags
& CFTYPE_NO_PREFIX
) &&
2651 !(cgrp
->root
->flags
& CGRP_ROOT_NOPREFIX
)) {
2652 strcpy(name
, cft
->ss
->name
);
2655 strcat(name
, cft
->name
);
2657 BUG_ON(!mutex_is_locked(&dir
->d_inode
->i_mutex
));
2659 cfe
= kzalloc(sizeof(*cfe
), GFP_KERNEL
);
2663 dentry
= lookup_one_len(name
, dir
, strlen(name
));
2664 if (IS_ERR(dentry
)) {
2665 error
= PTR_ERR(dentry
);
2669 cfe
->type
= (void *)cft
;
2670 cfe
->dentry
= dentry
;
2671 dentry
->d_fsdata
= cfe
;
2672 simple_xattrs_init(&cfe
->xattrs
);
2674 mode
= cgroup_file_mode(cft
);
2675 error
= cgroup_create_file(dentry
, mode
| S_IFREG
, cgrp
->root
->sb
);
2677 list_add_tail(&cfe
->node
, &parent
->files
);
2687 * cgroup_addrm_files - add or remove files to a cgroup directory
2688 * @cgrp: the target cgroup
2689 * @cfts: array of cftypes to be added
2690 * @is_add: whether to add or remove
2692 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
2693 * For removals, this function never fails. If addition fails, this
2694 * function doesn't remove files already added. The caller is responsible
2697 static int cgroup_addrm_files(struct cgroup
*cgrp
, struct cftype cfts
[],
2703 lockdep_assert_held(&cgrp
->dentry
->d_inode
->i_mutex
);
2704 lockdep_assert_held(&cgroup_mutex
);
2706 for (cft
= cfts
; cft
->name
[0] != '\0'; cft
++) {
2707 /* does cft->flags tell us to skip this file on @cgrp? */
2708 if ((cft
->flags
& CFTYPE_INSANE
) && cgroup_sane_behavior(cgrp
))
2710 if ((cft
->flags
& CFTYPE_NOT_ON_ROOT
) && !cgrp
->parent
)
2712 if ((cft
->flags
& CFTYPE_ONLY_ON_ROOT
) && cgrp
->parent
)
2716 ret
= cgroup_add_file(cgrp
, cft
);
2718 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
2723 cgroup_rm_file(cgrp
, cft
);
2729 static void cgroup_cfts_prepare(void)
2730 __acquires(&cgroup_mutex
)
2733 * Thanks to the entanglement with vfs inode locking, we can't walk
2734 * the existing cgroups under cgroup_mutex and create files.
2735 * Instead, we use css_for_each_descendant_pre() and drop RCU read
2736 * lock before calling cgroup_addrm_files().
2738 mutex_lock(&cgroup_mutex
);
2741 static int cgroup_cfts_commit(struct cftype
*cfts
, bool is_add
)
2742 __releases(&cgroup_mutex
)
2745 struct cgroup_subsys
*ss
= cfts
[0].ss
;
2746 struct cgroup
*root
= &ss
->root
->top_cgroup
;
2747 struct super_block
*sb
= ss
->root
->sb
;
2748 struct dentry
*prev
= NULL
;
2749 struct inode
*inode
;
2750 struct cgroup_subsys_state
*css
;
2754 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
2755 if (!cfts
|| ss
->root
== &cgroup_dummy_root
||
2756 !atomic_inc_not_zero(&sb
->s_active
)) {
2757 mutex_unlock(&cgroup_mutex
);
2762 * All cgroups which are created after we drop cgroup_mutex will
2763 * have the updated set of files, so we only need to update the
2764 * cgroups created before the current @cgroup_serial_nr_next.
2766 update_before
= cgroup_serial_nr_next
;
2768 /* add/rm files for all cgroups created before */
2769 css_for_each_descendant_pre(css
, cgroup_css(root
, ss
)) {
2770 struct cgroup
*cgrp
= css
->cgroup
;
2772 if (cgroup_is_dead(cgrp
))
2775 inode
= cgrp
->dentry
->d_inode
;
2778 prev
= cgrp
->dentry
;
2780 mutex_unlock(&cgroup_mutex
);
2781 mutex_lock(&inode
->i_mutex
);
2782 mutex_lock(&cgroup_mutex
);
2783 if (cgrp
->serial_nr
< update_before
&& !cgroup_is_dead(cgrp
))
2784 ret
= cgroup_addrm_files(cgrp
, cfts
, is_add
);
2785 mutex_unlock(&inode
->i_mutex
);
2789 mutex_unlock(&cgroup_mutex
);
2791 deactivate_super(sb
);
2796 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2797 * @ss: target cgroup subsystem
2798 * @cfts: zero-length name terminated array of cftypes
2800 * Register @cfts to @ss. Files described by @cfts are created for all
2801 * existing cgroups to which @ss is attached and all future cgroups will
2802 * have them too. This function can be called anytime whether @ss is
2805 * Returns 0 on successful registration, -errno on failure. Note that this
2806 * function currently returns 0 as long as @cfts registration is successful
2807 * even if some file creation attempts on existing cgroups fail.
2809 int cgroup_add_cftypes(struct cgroup_subsys
*ss
, struct cftype
*cfts
)
2811 struct cftype_set
*set
;
2815 set
= kzalloc(sizeof(*set
), GFP_KERNEL
);
2819 for (cft
= cfts
; cft
->name
[0] != '\0'; cft
++)
2822 cgroup_cfts_prepare();
2824 list_add_tail(&set
->node
, &ss
->cftsets
);
2825 ret
= cgroup_cfts_commit(cfts
, true);
2827 cgroup_rm_cftypes(cfts
);
2830 EXPORT_SYMBOL_GPL(cgroup_add_cftypes
);
2833 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
2834 * @cfts: zero-length name terminated array of cftypes
2836 * Unregister @cfts. Files described by @cfts are removed from all
2837 * existing cgroups and all future cgroups won't have them either. This
2838 * function can be called anytime whether @cfts' subsys is attached or not.
2840 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
2843 int cgroup_rm_cftypes(struct cftype
*cfts
)
2845 struct cftype_set
*set
;
2847 if (!cfts
|| !cfts
[0].ss
)
2850 cgroup_cfts_prepare();
2852 list_for_each_entry(set
, &cfts
[0].ss
->cftsets
, node
) {
2853 if (set
->cfts
== cfts
) {
2854 list_del(&set
->node
);
2856 cgroup_cfts_commit(cfts
, false);
2861 cgroup_cfts_commit(NULL
, false);
2866 * cgroup_task_count - count the number of tasks in a cgroup.
2867 * @cgrp: the cgroup in question
2869 * Return the number of tasks in the cgroup.
2871 int cgroup_task_count(const struct cgroup
*cgrp
)
2874 struct cgrp_cset_link
*link
;
2876 read_lock(&css_set_lock
);
2877 list_for_each_entry(link
, &cgrp
->cset_links
, cset_link
)
2878 count
+= atomic_read(&link
->cset
->refcount
);
2879 read_unlock(&css_set_lock
);
2884 * To reduce the fork() overhead for systems that are not actually using
2885 * their cgroups capability, we don't maintain the lists running through
2886 * each css_set to its tasks until we see the list actually used - in other
2887 * words after the first call to css_task_iter_start().
2889 static void cgroup_enable_task_cg_lists(void)
2891 struct task_struct
*p
, *g
;
2892 write_lock(&css_set_lock
);
2893 use_task_css_set_links
= 1;
2895 * We need tasklist_lock because RCU is not safe against
2896 * while_each_thread(). Besides, a forking task that has passed
2897 * cgroup_post_fork() without seeing use_task_css_set_links = 1
2898 * is not guaranteed to have its child immediately visible in the
2899 * tasklist if we walk through it with RCU.
2901 read_lock(&tasklist_lock
);
2902 do_each_thread(g
, p
) {
2905 * We should check if the process is exiting, otherwise
2906 * it will race with cgroup_exit() in that the list
2907 * entry won't be deleted though the process has exited.
2909 if (!(p
->flags
& PF_EXITING
) && list_empty(&p
->cg_list
))
2910 list_add(&p
->cg_list
, &task_css_set(p
)->tasks
);
2912 } while_each_thread(g
, p
);
2913 read_unlock(&tasklist_lock
);
2914 write_unlock(&css_set_lock
);
2918 * css_next_child - find the next child of a given css
2919 * @pos_css: the current position (%NULL to initiate traversal)
2920 * @parent_css: css whose children to walk
2922 * This function returns the next child of @parent_css and should be called
2923 * under either cgroup_mutex or RCU read lock. The only requirement is
2924 * that @parent_css and @pos_css are accessible. The next sibling is
2925 * guaranteed to be returned regardless of their states.
2927 struct cgroup_subsys_state
*
2928 css_next_child(struct cgroup_subsys_state
*pos_css
,
2929 struct cgroup_subsys_state
*parent_css
)
2931 struct cgroup
*pos
= pos_css
? pos_css
->cgroup
: NULL
;
2932 struct cgroup
*cgrp
= parent_css
->cgroup
;
2933 struct cgroup
*next
;
2935 cgroup_assert_mutex_or_rcu_locked();
2938 * @pos could already have been removed. Once a cgroup is removed,
2939 * its ->sibling.next is no longer updated when its next sibling
2940 * changes. As CGRP_DEAD assertion is serialized and happens
2941 * before the cgroup is taken off the ->sibling list, if we see it
2942 * unasserted, it's guaranteed that the next sibling hasn't
2943 * finished its grace period even if it's already removed, and thus
2944 * safe to dereference from this RCU critical section. If
2945 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
2946 * to be visible as %true here.
2948 * If @pos is dead, its next pointer can't be dereferenced;
2949 * however, as each cgroup is given a monotonically increasing
2950 * unique serial number and always appended to the sibling list,
2951 * the next one can be found by walking the parent's children until
2952 * we see a cgroup with higher serial number than @pos's. While
2953 * this path can be slower, it's taken only when either the current
2954 * cgroup is removed or iteration and removal race.
2957 next
= list_entry_rcu(cgrp
->children
.next
, struct cgroup
, sibling
);
2958 } else if (likely(!cgroup_is_dead(pos
))) {
2959 next
= list_entry_rcu(pos
->sibling
.next
, struct cgroup
, sibling
);
2961 list_for_each_entry_rcu(next
, &cgrp
->children
, sibling
)
2962 if (next
->serial_nr
> pos
->serial_nr
)
2966 if (&next
->sibling
== &cgrp
->children
)
2969 return cgroup_css(next
, parent_css
->ss
);
2971 EXPORT_SYMBOL_GPL(css_next_child
);
2974 * css_next_descendant_pre - find the next descendant for pre-order walk
2975 * @pos: the current position (%NULL to initiate traversal)
2976 * @root: css whose descendants to walk
2978 * To be used by css_for_each_descendant_pre(). Find the next descendant
2979 * to visit for pre-order traversal of @root's descendants. @root is
2980 * included in the iteration and the first node to be visited.
2982 * While this function requires cgroup_mutex or RCU read locking, it
2983 * doesn't require the whole traversal to be contained in a single critical
2984 * section. This function will return the correct next descendant as long
2985 * as both @pos and @root are accessible and @pos is a descendant of @root.
2987 struct cgroup_subsys_state
*
2988 css_next_descendant_pre(struct cgroup_subsys_state
*pos
,
2989 struct cgroup_subsys_state
*root
)
2991 struct cgroup_subsys_state
*next
;
2993 cgroup_assert_mutex_or_rcu_locked();
2995 /* if first iteration, visit @root */
2999 /* visit the first child if exists */
3000 next
= css_next_child(NULL
, pos
);
3004 /* no child, visit my or the closest ancestor's next sibling */
3005 while (pos
!= root
) {
3006 next
= css_next_child(pos
, css_parent(pos
));
3009 pos
= css_parent(pos
);
3014 EXPORT_SYMBOL_GPL(css_next_descendant_pre
);
3017 * css_rightmost_descendant - return the rightmost descendant of a css
3018 * @pos: css of interest
3020 * Return the rightmost descendant of @pos. If there's no descendant, @pos
3021 * is returned. This can be used during pre-order traversal to skip
3024 * While this function requires cgroup_mutex or RCU read locking, it
3025 * doesn't require the whole traversal to be contained in a single critical
3026 * section. This function will return the correct rightmost descendant as
3027 * long as @pos is accessible.
3029 struct cgroup_subsys_state
*
3030 css_rightmost_descendant(struct cgroup_subsys_state
*pos
)
3032 struct cgroup_subsys_state
*last
, *tmp
;
3034 cgroup_assert_mutex_or_rcu_locked();
3038 /* ->prev isn't RCU safe, walk ->next till the end */
3040 css_for_each_child(tmp
, last
)
3046 EXPORT_SYMBOL_GPL(css_rightmost_descendant
);
3048 static struct cgroup_subsys_state
*
3049 css_leftmost_descendant(struct cgroup_subsys_state
*pos
)
3051 struct cgroup_subsys_state
*last
;
3055 pos
= css_next_child(NULL
, pos
);
3062 * css_next_descendant_post - find the next descendant for post-order walk
3063 * @pos: the current position (%NULL to initiate traversal)
3064 * @root: css whose descendants to walk
3066 * To be used by css_for_each_descendant_post(). Find the next descendant
3067 * to visit for post-order traversal of @root's descendants. @root is
3068 * included in the iteration and the last node to be visited.
3070 * While this function requires cgroup_mutex or RCU read locking, it
3071 * doesn't require the whole traversal to be contained in a single critical
3072 * section. This function will return the correct next descendant as long
3073 * as both @pos and @cgroup are accessible and @pos is a descendant of
3076 struct cgroup_subsys_state
*
3077 css_next_descendant_post(struct cgroup_subsys_state
*pos
,
3078 struct cgroup_subsys_state
*root
)
3080 struct cgroup_subsys_state
*next
;
3082 cgroup_assert_mutex_or_rcu_locked();
3084 /* if first iteration, visit leftmost descendant which may be @root */
3086 return css_leftmost_descendant(root
);
3088 /* if we visited @root, we're done */
3092 /* if there's an unvisited sibling, visit its leftmost descendant */
3093 next
= css_next_child(pos
, css_parent(pos
));
3095 return css_leftmost_descendant(next
);
3097 /* no sibling left, visit parent */
3098 return css_parent(pos
);
3100 EXPORT_SYMBOL_GPL(css_next_descendant_post
);
3103 * css_advance_task_iter - advance a task itererator to the next css_set
3104 * @it: the iterator to advance
3106 * Advance @it to the next css_set to walk.
3108 static void css_advance_task_iter(struct css_task_iter
*it
)
3110 struct list_head
*l
= it
->cset_link
;
3111 struct cgrp_cset_link
*link
;
3112 struct css_set
*cset
;
3114 /* Advance to the next non-empty css_set */
3117 if (l
== &it
->origin_css
->cgroup
->cset_links
) {
3118 it
->cset_link
= NULL
;
3121 link
= list_entry(l
, struct cgrp_cset_link
, cset_link
);
3123 } while (list_empty(&cset
->tasks
));
3125 it
->task
= cset
->tasks
.next
;
3129 * css_task_iter_start - initiate task iteration
3130 * @css: the css to walk tasks of
3131 * @it: the task iterator to use
3133 * Initiate iteration through the tasks of @css. The caller can call
3134 * css_task_iter_next() to walk through the tasks until the function
3135 * returns NULL. On completion of iteration, css_task_iter_end() must be
3138 * Note that this function acquires a lock which is released when the
3139 * iteration finishes. The caller can't sleep while iteration is in
3142 void css_task_iter_start(struct cgroup_subsys_state
*css
,
3143 struct css_task_iter
*it
)
3144 __acquires(css_set_lock
)
3147 * The first time anyone tries to iterate across a css, we need to
3148 * enable the list linking each css_set to its tasks, and fix up
3149 * all existing tasks.
3151 if (!use_task_css_set_links
)
3152 cgroup_enable_task_cg_lists();
3154 read_lock(&css_set_lock
);
3156 it
->origin_css
= css
;
3157 it
->cset_link
= &css
->cgroup
->cset_links
;
3159 css_advance_task_iter(it
);
3163 * css_task_iter_next - return the next task for the iterator
3164 * @it: the task iterator being iterated
3166 * The "next" function for task iteration. @it should have been
3167 * initialized via css_task_iter_start(). Returns NULL when the iteration
3170 struct task_struct
*css_task_iter_next(struct css_task_iter
*it
)
3172 struct task_struct
*res
;
3173 struct list_head
*l
= it
->task
;
3174 struct cgrp_cset_link
*link
;
3176 /* If the iterator cg is NULL, we have no tasks */
3179 res
= list_entry(l
, struct task_struct
, cg_list
);
3180 /* Advance iterator to find next entry */
3182 link
= list_entry(it
->cset_link
, struct cgrp_cset_link
, cset_link
);
3183 if (l
== &link
->cset
->tasks
) {
3185 * We reached the end of this task list - move on to the
3186 * next cgrp_cset_link.
3188 css_advance_task_iter(it
);
3196 * css_task_iter_end - finish task iteration
3197 * @it: the task iterator to finish
3199 * Finish task iteration started by css_task_iter_start().
3201 void css_task_iter_end(struct css_task_iter
*it
)
3202 __releases(css_set_lock
)
3204 read_unlock(&css_set_lock
);
3207 static inline int started_after_time(struct task_struct
*t1
,
3208 struct timespec
*time
,
3209 struct task_struct
*t2
)
3211 int start_diff
= timespec_compare(&t1
->start_time
, time
);
3212 if (start_diff
> 0) {
3214 } else if (start_diff
< 0) {
3218 * Arbitrarily, if two processes started at the same
3219 * time, we'll say that the lower pointer value
3220 * started first. Note that t2 may have exited by now
3221 * so this may not be a valid pointer any longer, but
3222 * that's fine - it still serves to distinguish
3223 * between two tasks started (effectively) simultaneously.
3230 * This function is a callback from heap_insert() and is used to order
3232 * In this case we order the heap in descending task start time.
3234 static inline int started_after(void *p1
, void *p2
)
3236 struct task_struct
*t1
= p1
;
3237 struct task_struct
*t2
= p2
;
3238 return started_after_time(t1
, &t2
->start_time
, t2
);
3242 * css_scan_tasks - iterate though all the tasks in a css
3243 * @css: the css to iterate tasks of
3244 * @test: optional test callback
3245 * @process: process callback
3246 * @data: data passed to @test and @process
3247 * @heap: optional pre-allocated heap used for task iteration
3249 * Iterate through all the tasks in @css, calling @test for each, and if it
3250 * returns %true, call @process for it also.
3252 * @test may be NULL, meaning always true (select all tasks), which
3253 * effectively duplicates css_task_iter_{start,next,end}() but does not
3254 * lock css_set_lock for the call to @process.
3256 * It is guaranteed that @process will act on every task that is a member
3257 * of @css for the duration of this call. This function may or may not
3258 * call @process for tasks that exit or move to a different css during the
3259 * call, or are forked or move into the css during the call.
3261 * Note that @test may be called with locks held, and may in some
3262 * situations be called multiple times for the same task, so it should be
3265 * If @heap is non-NULL, a heap has been pre-allocated and will be used for
3266 * heap operations (and its "gt" member will be overwritten), else a
3267 * temporary heap will be used (allocation of which may cause this function
3270 int css_scan_tasks(struct cgroup_subsys_state
*css
,
3271 bool (*test
)(struct task_struct
*, void *),
3272 void (*process
)(struct task_struct
*, void *),
3273 void *data
, struct ptr_heap
*heap
)
3276 struct css_task_iter it
;
3277 struct task_struct
*p
, *dropped
;
3278 /* Never dereference latest_task, since it's not refcounted */
3279 struct task_struct
*latest_task
= NULL
;
3280 struct ptr_heap tmp_heap
;
3281 struct timespec latest_time
= { 0, 0 };
3284 /* The caller supplied our heap and pre-allocated its memory */
3285 heap
->gt
= &started_after
;
3287 /* We need to allocate our own heap memory */
3289 retval
= heap_init(heap
, PAGE_SIZE
, GFP_KERNEL
, &started_after
);
3291 /* cannot allocate the heap */
3297 * Scan tasks in the css, using the @test callback to determine
3298 * which are of interest, and invoking @process callback on the
3299 * ones which need an update. Since we don't want to hold any
3300 * locks during the task updates, gather tasks to be processed in a
3301 * heap structure. The heap is sorted by descending task start
3302 * time. If the statically-sized heap fills up, we overflow tasks
3303 * that started later, and in future iterations only consider tasks
3304 * that started after the latest task in the previous pass. This
3305 * guarantees forward progress and that we don't miss any tasks.
3308 css_task_iter_start(css
, &it
);
3309 while ((p
= css_task_iter_next(&it
))) {
3311 * Only affect tasks that qualify per the caller's callback,
3312 * if he provided one
3314 if (test
&& !test(p
, data
))
3317 * Only process tasks that started after the last task
3320 if (!started_after_time(p
, &latest_time
, latest_task
))
3322 dropped
= heap_insert(heap
, p
);
3323 if (dropped
== NULL
) {
3325 * The new task was inserted; the heap wasn't
3329 } else if (dropped
!= p
) {
3331 * The new task was inserted, and pushed out a
3335 put_task_struct(dropped
);
3338 * Else the new task was newer than anything already in
3339 * the heap and wasn't inserted
3342 css_task_iter_end(&it
);
3345 for (i
= 0; i
< heap
->size
; i
++) {
3346 struct task_struct
*q
= heap
->ptrs
[i
];
3348 latest_time
= q
->start_time
;
3351 /* Process the task per the caller's callback */
3356 * If we had to process any tasks at all, scan again
3357 * in case some of them were in the middle of forking
3358 * children that didn't get processed.
3359 * Not the most efficient way to do it, but it avoids
3360 * having to take callback_mutex in the fork path
3364 if (heap
== &tmp_heap
)
3365 heap_free(&tmp_heap
);
3369 static void cgroup_transfer_one_task(struct task_struct
*task
, void *data
)
3371 struct cgroup
*new_cgroup
= data
;
3373 mutex_lock(&cgroup_mutex
);
3374 cgroup_attach_task(new_cgroup
, task
, false);
3375 mutex_unlock(&cgroup_mutex
);
3379 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3380 * @to: cgroup to which the tasks will be moved
3381 * @from: cgroup in which the tasks currently reside
3383 int cgroup_transfer_tasks(struct cgroup
*to
, struct cgroup
*from
)
3385 return css_scan_tasks(&from
->dummy_css
, NULL
, cgroup_transfer_one_task
,
3390 * Stuff for reading the 'tasks'/'procs' files.
3392 * Reading this file can return large amounts of data if a cgroup has
3393 * *lots* of attached tasks. So it may need several calls to read(),
3394 * but we cannot guarantee that the information we produce is correct
3395 * unless we produce it entirely atomically.
3399 /* which pidlist file are we talking about? */
3400 enum cgroup_filetype
{
3406 * A pidlist is a list of pids that virtually represents the contents of one
3407 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3408 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3411 struct cgroup_pidlist
{
3413 * used to find which pidlist is wanted. doesn't change as long as
3414 * this particular list stays in the list.
3416 struct { enum cgroup_filetype type
; struct pid_namespace
*ns
; } key
;
3419 /* how many elements the above list has */
3421 /* each of these stored in a list by its cgroup */
3422 struct list_head links
;
3423 /* pointer to the cgroup we belong to, for list removal purposes */
3424 struct cgroup
*owner
;
3425 /* for delayed destruction */
3426 struct delayed_work destroy_dwork
;
3430 * The following two functions "fix" the issue where there are more pids
3431 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3432 * TODO: replace with a kernel-wide solution to this problem
3434 #define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3435 static void *pidlist_allocate(int count
)
3437 if (PIDLIST_TOO_LARGE(count
))
3438 return vmalloc(count
* sizeof(pid_t
));
3440 return kmalloc(count
* sizeof(pid_t
), GFP_KERNEL
);
3443 static void pidlist_free(void *p
)
3445 if (is_vmalloc_addr(p
))
3452 * Used to destroy all pidlists lingering waiting for destroy timer. None
3453 * should be left afterwards.
3455 static void cgroup_pidlist_destroy_all(struct cgroup
*cgrp
)
3457 struct cgroup_pidlist
*l
, *tmp_l
;
3459 mutex_lock(&cgrp
->pidlist_mutex
);
3460 list_for_each_entry_safe(l
, tmp_l
, &cgrp
->pidlists
, links
)
3461 mod_delayed_work(cgroup_pidlist_destroy_wq
, &l
->destroy_dwork
, 0);
3462 mutex_unlock(&cgrp
->pidlist_mutex
);
3464 flush_workqueue(cgroup_pidlist_destroy_wq
);
3465 BUG_ON(!list_empty(&cgrp
->pidlists
));
3468 static void cgroup_pidlist_destroy_work_fn(struct work_struct
*work
)
3470 struct delayed_work
*dwork
= to_delayed_work(work
);
3471 struct cgroup_pidlist
*l
= container_of(dwork
, struct cgroup_pidlist
,
3473 struct cgroup_pidlist
*tofree
= NULL
;
3475 mutex_lock(&l
->owner
->pidlist_mutex
);
3478 * Destroy iff we didn't get queued again. The state won't change
3479 * as destroy_dwork can only be queued while locked.
3481 if (!delayed_work_pending(dwork
)) {
3482 list_del(&l
->links
);
3483 pidlist_free(l
->list
);
3484 put_pid_ns(l
->key
.ns
);
3488 mutex_unlock(&l
->owner
->pidlist_mutex
);
3493 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
3494 * Returns the number of unique elements.
3496 static int pidlist_uniq(pid_t
*list
, int length
)
3501 * we presume the 0th element is unique, so i starts at 1. trivial
3502 * edge cases first; no work needs to be done for either
3504 if (length
== 0 || length
== 1)
3506 /* src and dest walk down the list; dest counts unique elements */
3507 for (src
= 1; src
< length
; src
++) {
3508 /* find next unique element */
3509 while (list
[src
] == list
[src
-1]) {
3514 /* dest always points to where the next unique element goes */
3515 list
[dest
] = list
[src
];
3523 * The two pid files - task and cgroup.procs - guaranteed that the result
3524 * is sorted, which forced this whole pidlist fiasco. As pid order is
3525 * different per namespace, each namespace needs differently sorted list,
3526 * making it impossible to use, for example, single rbtree of member tasks
3527 * sorted by task pointer. As pidlists can be fairly large, allocating one
3528 * per open file is dangerous, so cgroup had to implement shared pool of
3529 * pidlists keyed by cgroup and namespace.
3531 * All this extra complexity was caused by the original implementation
3532 * committing to an entirely unnecessary property. In the long term, we
3533 * want to do away with it. Explicitly scramble sort order if
3534 * sane_behavior so that no such expectation exists in the new interface.
3536 * Scrambling is done by swapping every two consecutive bits, which is
3537 * non-identity one-to-one mapping which disturbs sort order sufficiently.
3539 static pid_t
pid_fry(pid_t pid
)
3541 unsigned a
= pid
& 0x55555555;
3542 unsigned b
= pid
& 0xAAAAAAAA;
3544 return (a
<< 1) | (b
>> 1);
3547 static pid_t
cgroup_pid_fry(struct cgroup
*cgrp
, pid_t pid
)
3549 if (cgroup_sane_behavior(cgrp
))
3550 return pid_fry(pid
);
3555 static int cmppid(const void *a
, const void *b
)
3557 return *(pid_t
*)a
- *(pid_t
*)b
;
3560 static int fried_cmppid(const void *a
, const void *b
)
3562 return pid_fry(*(pid_t
*)a
) - pid_fry(*(pid_t
*)b
);
3565 static struct cgroup_pidlist
*cgroup_pidlist_find(struct cgroup
*cgrp
,
3566 enum cgroup_filetype type
)
3568 struct cgroup_pidlist
*l
;
3569 /* don't need task_nsproxy() if we're looking at ourself */
3570 struct pid_namespace
*ns
= task_active_pid_ns(current
);
3572 lockdep_assert_held(&cgrp
->pidlist_mutex
);
3574 list_for_each_entry(l
, &cgrp
->pidlists
, links
)
3575 if (l
->key
.type
== type
&& l
->key
.ns
== ns
)
3581 * find the appropriate pidlist for our purpose (given procs vs tasks)
3582 * returns with the lock on that pidlist already held, and takes care
3583 * of the use count, or returns NULL with no locks held if we're out of
3586 static struct cgroup_pidlist
*cgroup_pidlist_find_create(struct cgroup
*cgrp
,
3587 enum cgroup_filetype type
)
3589 struct cgroup_pidlist
*l
;
3591 lockdep_assert_held(&cgrp
->pidlist_mutex
);
3593 l
= cgroup_pidlist_find(cgrp
, type
);
3597 /* entry not found; create a new one */
3598 l
= kzalloc(sizeof(struct cgroup_pidlist
), GFP_KERNEL
);
3602 INIT_DELAYED_WORK(&l
->destroy_dwork
, cgroup_pidlist_destroy_work_fn
);
3604 /* don't need task_nsproxy() if we're looking at ourself */
3605 l
->key
.ns
= get_pid_ns(task_active_pid_ns(current
));
3607 list_add(&l
->links
, &cgrp
->pidlists
);
3612 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3614 static int pidlist_array_load(struct cgroup
*cgrp
, enum cgroup_filetype type
,
3615 struct cgroup_pidlist
**lp
)
3619 int pid
, n
= 0; /* used for populating the array */
3620 struct css_task_iter it
;
3621 struct task_struct
*tsk
;
3622 struct cgroup_pidlist
*l
;
3624 lockdep_assert_held(&cgrp
->pidlist_mutex
);
3627 * If cgroup gets more users after we read count, we won't have
3628 * enough space - tough. This race is indistinguishable to the
3629 * caller from the case that the additional cgroup users didn't
3630 * show up until sometime later on.
3632 length
= cgroup_task_count(cgrp
);
3633 array
= pidlist_allocate(length
);
3636 /* now, populate the array */
3637 css_task_iter_start(&cgrp
->dummy_css
, &it
);
3638 while ((tsk
= css_task_iter_next(&it
))) {
3639 if (unlikely(n
== length
))
3641 /* get tgid or pid for procs or tasks file respectively */
3642 if (type
== CGROUP_FILE_PROCS
)
3643 pid
= task_tgid_vnr(tsk
);
3645 pid
= task_pid_vnr(tsk
);
3646 if (pid
> 0) /* make sure to only use valid results */
3649 css_task_iter_end(&it
);
3651 /* now sort & (if procs) strip out duplicates */
3652 if (cgroup_sane_behavior(cgrp
))
3653 sort(array
, length
, sizeof(pid_t
), fried_cmppid
, NULL
);
3655 sort(array
, length
, sizeof(pid_t
), cmppid
, NULL
);
3656 if (type
== CGROUP_FILE_PROCS
)
3657 length
= pidlist_uniq(array
, length
);
3659 l
= cgroup_pidlist_find_create(cgrp
, type
);
3661 mutex_unlock(&cgrp
->pidlist_mutex
);
3662 pidlist_free(array
);
3666 /* store array, freeing old if necessary */
3667 pidlist_free(l
->list
);
3675 * cgroupstats_build - build and fill cgroupstats
3676 * @stats: cgroupstats to fill information into
3677 * @dentry: A dentry entry belonging to the cgroup for which stats have
3680 * Build and fill cgroupstats so that taskstats can export it to user
3683 int cgroupstats_build(struct cgroupstats
*stats
, struct dentry
*dentry
)
3686 struct cgroup
*cgrp
;
3687 struct css_task_iter it
;
3688 struct task_struct
*tsk
;
3691 * Validate dentry by checking the superblock operations,
3692 * and make sure it's a directory.
3694 if (dentry
->d_sb
->s_op
!= &cgroup_ops
||
3695 !S_ISDIR(dentry
->d_inode
->i_mode
))
3699 cgrp
= dentry
->d_fsdata
;
3701 css_task_iter_start(&cgrp
->dummy_css
, &it
);
3702 while ((tsk
= css_task_iter_next(&it
))) {
3703 switch (tsk
->state
) {
3705 stats
->nr_running
++;
3707 case TASK_INTERRUPTIBLE
:
3708 stats
->nr_sleeping
++;
3710 case TASK_UNINTERRUPTIBLE
:
3711 stats
->nr_uninterruptible
++;
3714 stats
->nr_stopped
++;
3717 if (delayacct_is_task_waiting_on_io(tsk
))
3718 stats
->nr_io_wait
++;
3722 css_task_iter_end(&it
);
3730 * seq_file methods for the tasks/procs files. The seq_file position is the
3731 * next pid to display; the seq_file iterator is a pointer to the pid
3732 * in the cgroup->l->list array.
3735 static void *cgroup_pidlist_start(struct seq_file
*s
, loff_t
*pos
)
3738 * Initially we receive a position value that corresponds to
3739 * one more than the last pid shown (or 0 on the first call or
3740 * after a seek to the start). Use a binary-search to find the
3741 * next pid to display, if any
3743 struct cgroup_open_file
*of
= s
->private;
3744 struct cgroup
*cgrp
= seq_css(s
)->cgroup
;
3745 struct cgroup_pidlist
*l
;
3746 enum cgroup_filetype type
= seq_cft(s
)->private;
3747 int index
= 0, pid
= *pos
;
3750 mutex_lock(&cgrp
->pidlist_mutex
);
3753 * !NULL @of->priv indicates that this isn't the first start()
3754 * after open. If the matching pidlist is around, we can use that.
3755 * Look for it. Note that @of->priv can't be used directly. It
3756 * could already have been destroyed.
3759 of
->priv
= cgroup_pidlist_find(cgrp
, type
);
3762 * Either this is the first start() after open or the matching
3763 * pidlist has been destroyed inbetween. Create a new one.
3766 ret
= pidlist_array_load(cgrp
, type
,
3767 (struct cgroup_pidlist
**)&of
->priv
);
3769 return ERR_PTR(ret
);
3774 int end
= l
->length
;
3776 while (index
< end
) {
3777 int mid
= (index
+ end
) / 2;
3778 if (cgroup_pid_fry(cgrp
, l
->list
[mid
]) == pid
) {
3781 } else if (cgroup_pid_fry(cgrp
, l
->list
[mid
]) <= pid
)
3787 /* If we're off the end of the array, we're done */
3788 if (index
>= l
->length
)
3790 /* Update the abstract position to be the actual pid that we found */
3791 iter
= l
->list
+ index
;
3792 *pos
= cgroup_pid_fry(cgrp
, *iter
);
3796 static void cgroup_pidlist_stop(struct seq_file
*s
, void *v
)
3798 struct cgroup_open_file
*of
= s
->private;
3799 struct cgroup_pidlist
*l
= of
->priv
;
3802 mod_delayed_work(cgroup_pidlist_destroy_wq
, &l
->destroy_dwork
,
3803 CGROUP_PIDLIST_DESTROY_DELAY
);
3804 mutex_unlock(&seq_css(s
)->cgroup
->pidlist_mutex
);
3807 static void *cgroup_pidlist_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
3809 struct cgroup_open_file
*of
= s
->private;
3810 struct cgroup_pidlist
*l
= of
->priv
;
3812 pid_t
*end
= l
->list
+ l
->length
;
3814 * Advance to the next pid in the array. If this goes off the
3821 *pos
= cgroup_pid_fry(seq_css(s
)->cgroup
, *p
);
3826 static int cgroup_pidlist_show(struct seq_file
*s
, void *v
)
3828 return seq_printf(s
, "%d\n", *(int *)v
);
3832 * seq_operations functions for iterating on pidlists through seq_file -
3833 * independent of whether it's tasks or procs
3835 static const struct seq_operations cgroup_pidlist_seq_operations
= {
3836 .start
= cgroup_pidlist_start
,
3837 .stop
= cgroup_pidlist_stop
,
3838 .next
= cgroup_pidlist_next
,
3839 .show
= cgroup_pidlist_show
,
3842 static u64
cgroup_read_notify_on_release(struct cgroup_subsys_state
*css
,
3845 return notify_on_release(css
->cgroup
);
3848 static int cgroup_write_notify_on_release(struct cgroup_subsys_state
*css
,
3849 struct cftype
*cft
, u64 val
)
3851 clear_bit(CGRP_RELEASABLE
, &css
->cgroup
->flags
);
3853 set_bit(CGRP_NOTIFY_ON_RELEASE
, &css
->cgroup
->flags
);
3855 clear_bit(CGRP_NOTIFY_ON_RELEASE
, &css
->cgroup
->flags
);
3860 * When dput() is called asynchronously, if umount has been done and
3861 * then deactivate_super() in cgroup_free_fn() kills the superblock,
3862 * there's a small window that vfs will see the root dentry with non-zero
3863 * refcnt and trigger BUG().
3865 * That's why we hold a reference before dput() and drop it right after.
3867 static void cgroup_dput(struct cgroup
*cgrp
)
3869 struct super_block
*sb
= cgrp
->root
->sb
;
3871 atomic_inc(&sb
->s_active
);
3873 deactivate_super(sb
);
3876 static u64
cgroup_clone_children_read(struct cgroup_subsys_state
*css
,
3879 return test_bit(CGRP_CPUSET_CLONE_CHILDREN
, &css
->cgroup
->flags
);
3882 static int cgroup_clone_children_write(struct cgroup_subsys_state
*css
,
3883 struct cftype
*cft
, u64 val
)
3886 set_bit(CGRP_CPUSET_CLONE_CHILDREN
, &css
->cgroup
->flags
);
3888 clear_bit(CGRP_CPUSET_CLONE_CHILDREN
, &css
->cgroup
->flags
);
3892 static struct cftype cgroup_base_files
[] = {
3894 .name
= "cgroup.procs",
3895 .seq_start
= cgroup_pidlist_start
,
3896 .seq_next
= cgroup_pidlist_next
,
3897 .seq_stop
= cgroup_pidlist_stop
,
3898 .seq_show
= cgroup_pidlist_show
,
3899 .private = CGROUP_FILE_PROCS
,
3900 .write_u64
= cgroup_procs_write
,
3901 .mode
= S_IRUGO
| S_IWUSR
,
3904 .name
= "cgroup.clone_children",
3905 .flags
= CFTYPE_INSANE
,
3906 .read_u64
= cgroup_clone_children_read
,
3907 .write_u64
= cgroup_clone_children_write
,
3910 .name
= "cgroup.sane_behavior",
3911 .flags
= CFTYPE_ONLY_ON_ROOT
,
3912 .seq_show
= cgroup_sane_behavior_show
,
3916 * Historical crazy stuff. These don't have "cgroup." prefix and
3917 * don't exist if sane_behavior. If you're depending on these, be
3918 * prepared to be burned.
3922 .flags
= CFTYPE_INSANE
, /* use "procs" instead */
3923 .seq_start
= cgroup_pidlist_start
,
3924 .seq_next
= cgroup_pidlist_next
,
3925 .seq_stop
= cgroup_pidlist_stop
,
3926 .seq_show
= cgroup_pidlist_show
,
3927 .private = CGROUP_FILE_TASKS
,
3928 .write_u64
= cgroup_tasks_write
,
3929 .mode
= S_IRUGO
| S_IWUSR
,
3932 .name
= "notify_on_release",
3933 .flags
= CFTYPE_INSANE
,
3934 .read_u64
= cgroup_read_notify_on_release
,
3935 .write_u64
= cgroup_write_notify_on_release
,
3938 .name
= "release_agent",
3939 .flags
= CFTYPE_INSANE
| CFTYPE_ONLY_ON_ROOT
,
3940 .seq_show
= cgroup_release_agent_show
,
3941 .write_string
= cgroup_release_agent_write
,
3942 .max_write_len
= PATH_MAX
,
3948 * cgroup_populate_dir - create subsys files in a cgroup directory
3949 * @cgrp: target cgroup
3950 * @subsys_mask: mask of the subsystem ids whose files should be added
3952 * On failure, no file is added.
3954 static int cgroup_populate_dir(struct cgroup
*cgrp
, unsigned long subsys_mask
)
3956 struct cgroup_subsys
*ss
;
3959 /* process cftsets of each subsystem */
3960 for_each_subsys(ss
, i
) {
3961 struct cftype_set
*set
;
3963 if (!test_bit(i
, &subsys_mask
))
3966 list_for_each_entry(set
, &ss
->cftsets
, node
) {
3967 ret
= cgroup_addrm_files(cgrp
, set
->cfts
, true);
3974 cgroup_clear_dir(cgrp
, subsys_mask
);
3979 * css destruction is four-stage process.
3981 * 1. Destruction starts. Killing of the percpu_ref is initiated.
3982 * Implemented in kill_css().
3984 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
3985 * and thus css_tryget() is guaranteed to fail, the css can be offlined
3986 * by invoking offline_css(). After offlining, the base ref is put.
3987 * Implemented in css_killed_work_fn().
3989 * 3. When the percpu_ref reaches zero, the only possible remaining
3990 * accessors are inside RCU read sections. css_release() schedules the
3993 * 4. After the grace period, the css can be freed. Implemented in
3994 * css_free_work_fn().
3996 * It is actually hairier because both step 2 and 4 require process context
3997 * and thus involve punting to css->destroy_work adding two additional
3998 * steps to the already complex sequence.
4000 static void css_free_work_fn(struct work_struct
*work
)
4002 struct cgroup_subsys_state
*css
=
4003 container_of(work
, struct cgroup_subsys_state
, destroy_work
);
4004 struct cgroup
*cgrp
= css
->cgroup
;
4007 css_put(css
->parent
);
4009 css
->ss
->css_free(css
);
4013 static void css_free_rcu_fn(struct rcu_head
*rcu_head
)
4015 struct cgroup_subsys_state
*css
=
4016 container_of(rcu_head
, struct cgroup_subsys_state
, rcu_head
);
4019 * css holds an extra ref to @cgrp->dentry which is put on the last
4020 * css_put(). dput() requires process context which we don't have.
4022 INIT_WORK(&css
->destroy_work
, css_free_work_fn
);
4023 queue_work(cgroup_destroy_wq
, &css
->destroy_work
);
4026 static void css_release(struct percpu_ref
*ref
)
4028 struct cgroup_subsys_state
*css
=
4029 container_of(ref
, struct cgroup_subsys_state
, refcnt
);
4031 rcu_assign_pointer(css
->cgroup
->subsys
[css
->ss
->subsys_id
], NULL
);
4032 call_rcu(&css
->rcu_head
, css_free_rcu_fn
);
4035 static void init_css(struct cgroup_subsys_state
*css
, struct cgroup_subsys
*ss
,
4036 struct cgroup
*cgrp
)
4043 css
->parent
= cgroup_css(cgrp
->parent
, ss
);
4045 css
->flags
|= CSS_ROOT
;
4047 BUG_ON(cgroup_css(cgrp
, ss
));
4050 /* invoke ->css_online() on a new CSS and mark it online if successful */
4051 static int online_css(struct cgroup_subsys_state
*css
)
4053 struct cgroup_subsys
*ss
= css
->ss
;
4056 lockdep_assert_held(&cgroup_mutex
);
4059 ret
= ss
->css_online(css
);
4061 css
->flags
|= CSS_ONLINE
;
4062 css
->cgroup
->nr_css
++;
4063 rcu_assign_pointer(css
->cgroup
->subsys
[ss
->subsys_id
], css
);
4068 /* if the CSS is online, invoke ->css_offline() on it and mark it offline */
4069 static void offline_css(struct cgroup_subsys_state
*css
)
4071 struct cgroup_subsys
*ss
= css
->ss
;
4073 lockdep_assert_held(&cgroup_mutex
);
4075 if (!(css
->flags
& CSS_ONLINE
))
4078 if (ss
->css_offline
)
4079 ss
->css_offline(css
);
4081 css
->flags
&= ~CSS_ONLINE
;
4082 css
->cgroup
->nr_css
--;
4083 RCU_INIT_POINTER(css
->cgroup
->subsys
[ss
->subsys_id
], css
);
4087 * create_css - create a cgroup_subsys_state
4088 * @cgrp: the cgroup new css will be associated with
4089 * @ss: the subsys of new css
4091 * Create a new css associated with @cgrp - @ss pair. On success, the new
4092 * css is online and installed in @cgrp with all interface files created.
4093 * Returns 0 on success, -errno on failure.
4095 static int create_css(struct cgroup
*cgrp
, struct cgroup_subsys
*ss
)
4097 struct cgroup
*parent
= cgrp
->parent
;
4098 struct cgroup_subsys_state
*css
;
4101 lockdep_assert_held(&cgrp
->dentry
->d_inode
->i_mutex
);
4102 lockdep_assert_held(&cgroup_mutex
);
4104 css
= ss
->css_alloc(cgroup_css(parent
, ss
));
4106 return PTR_ERR(css
);
4108 err
= percpu_ref_init(&css
->refcnt
, css_release
);
4112 init_css(css
, ss
, cgrp
);
4114 err
= cgroup_populate_dir(cgrp
, 1 << ss
->subsys_id
);
4118 err
= online_css(css
);
4123 css_get(css
->parent
);
4125 if (ss
->broken_hierarchy
&& !ss
->warned_broken_hierarchy
&&
4127 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",
4128 current
->comm
, current
->pid
, ss
->name
);
4129 if (!strcmp(ss
->name
, "memory"))
4130 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4131 ss
->warned_broken_hierarchy
= true;
4137 percpu_ref_cancel_init(&css
->refcnt
);
4143 * cgroup_create - create a cgroup
4144 * @parent: cgroup that will be parent of the new cgroup
4145 * @dentry: dentry of the new cgroup
4146 * @mode: mode to set on new inode
4148 * Must be called with the mutex on the parent inode held
4150 static long cgroup_create(struct cgroup
*parent
, struct dentry
*dentry
,
4153 struct cgroup
*cgrp
;
4154 struct cgroup_name
*name
;
4155 struct cgroupfs_root
*root
= parent
->root
;
4157 struct cgroup_subsys
*ss
;
4158 struct super_block
*sb
= root
->sb
;
4160 /* allocate the cgroup and its ID, 0 is reserved for the root */
4161 cgrp
= kzalloc(sizeof(*cgrp
), GFP_KERNEL
);
4165 name
= cgroup_alloc_name(dentry
);
4170 rcu_assign_pointer(cgrp
->name
, name
);
4173 * Only live parents can have children. Note that the liveliness
4174 * check isn't strictly necessary because cgroup_mkdir() and
4175 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4176 * anyway so that locking is contained inside cgroup proper and we
4177 * don't get nasty surprises if we ever grow another caller.
4179 if (!cgroup_lock_live_group(parent
)) {
4185 * Temporarily set the pointer to NULL, so idr_find() won't return
4186 * a half-baked cgroup.
4188 cgrp
->id
= idr_alloc(&root
->cgroup_idr
, NULL
, 1, 0, GFP_KERNEL
);
4194 /* Grab a reference on the superblock so the hierarchy doesn't
4195 * get deleted on unmount if there are child cgroups. This
4196 * can be done outside cgroup_mutex, since the sb can't
4197 * disappear while someone has an open control file on the
4199 atomic_inc(&sb
->s_active
);
4201 init_cgroup_housekeeping(cgrp
);
4203 dentry
->d_fsdata
= cgrp
;
4204 cgrp
->dentry
= dentry
;
4206 cgrp
->parent
= parent
;
4207 cgrp
->dummy_css
.parent
= &parent
->dummy_css
;
4208 cgrp
->root
= parent
->root
;
4210 if (notify_on_release(parent
))
4211 set_bit(CGRP_NOTIFY_ON_RELEASE
, &cgrp
->flags
);
4213 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN
, &parent
->flags
))
4214 set_bit(CGRP_CPUSET_CLONE_CHILDREN
, &cgrp
->flags
);
4217 * Create directory. cgroup_create_file() returns with the new
4218 * directory locked on success so that it can be populated without
4219 * dropping cgroup_mutex.
4221 err
= cgroup_create_file(dentry
, S_IFDIR
| mode
, sb
);
4224 lockdep_assert_held(&dentry
->d_inode
->i_mutex
);
4226 cgrp
->serial_nr
= cgroup_serial_nr_next
++;
4228 /* allocation complete, commit to creation */
4229 list_add_tail_rcu(&cgrp
->sibling
, &cgrp
->parent
->children
);
4230 root
->number_of_cgroups
++;
4232 /* hold a ref to the parent's dentry */
4233 dget(parent
->dentry
);
4236 * @cgrp is now fully operational. If something fails after this
4237 * point, it'll be released via the normal destruction path.
4239 idr_replace(&root
->cgroup_idr
, cgrp
, cgrp
->id
);
4241 err
= cgroup_addrm_files(cgrp
, cgroup_base_files
, true);
4245 /* let's create and online css's */
4246 for_each_subsys(ss
, ssid
) {
4247 if (root
->subsys_mask
& (1 << ssid
)) {
4248 err
= create_css(cgrp
, ss
);
4254 mutex_unlock(&cgroup_mutex
);
4255 mutex_unlock(&cgrp
->dentry
->d_inode
->i_mutex
);
4260 idr_remove(&root
->cgroup_idr
, cgrp
->id
);
4261 /* Release the reference count that we took on the superblock */
4262 deactivate_super(sb
);
4264 mutex_unlock(&cgroup_mutex
);
4266 kfree(rcu_dereference_raw(cgrp
->name
));
4272 cgroup_destroy_locked(cgrp
);
4273 mutex_unlock(&cgroup_mutex
);
4274 mutex_unlock(&dentry
->d_inode
->i_mutex
);
4278 static int cgroup_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
4280 struct cgroup
*c_parent
= dentry
->d_parent
->d_fsdata
;
4282 /* the vfs holds inode->i_mutex already */
4283 return cgroup_create(c_parent
, dentry
, mode
| S_IFDIR
);
4287 * This is called when the refcnt of a css is confirmed to be killed.
4288 * css_tryget() is now guaranteed to fail.
4290 static void css_killed_work_fn(struct work_struct
*work
)
4292 struct cgroup_subsys_state
*css
=
4293 container_of(work
, struct cgroup_subsys_state
, destroy_work
);
4294 struct cgroup
*cgrp
= css
->cgroup
;
4296 mutex_lock(&cgroup_mutex
);
4299 * css_tryget() is guaranteed to fail now. Tell subsystems to
4300 * initate destruction.
4305 * If @cgrp is marked dead, it's waiting for refs of all css's to
4306 * be disabled before proceeding to the second phase of cgroup
4307 * destruction. If we are the last one, kick it off.
4309 if (!cgrp
->nr_css
&& cgroup_is_dead(cgrp
))
4310 cgroup_destroy_css_killed(cgrp
);
4312 mutex_unlock(&cgroup_mutex
);
4315 * Put the css refs from kill_css(). Each css holds an extra
4316 * reference to the cgroup's dentry and cgroup removal proceeds
4317 * regardless of css refs. On the last put of each css, whenever
4318 * that may be, the extra dentry ref is put so that dentry
4319 * destruction happens only after all css's are released.
4324 /* css kill confirmation processing requires process context, bounce */
4325 static void css_killed_ref_fn(struct percpu_ref
*ref
)
4327 struct cgroup_subsys_state
*css
=
4328 container_of(ref
, struct cgroup_subsys_state
, refcnt
);
4330 INIT_WORK(&css
->destroy_work
, css_killed_work_fn
);
4331 queue_work(cgroup_destroy_wq
, &css
->destroy_work
);
4335 * kill_css - destroy a css
4336 * @css: css to destroy
4338 * This function initiates destruction of @css by removing cgroup interface
4339 * files and putting its base reference. ->css_offline() will be invoked
4340 * asynchronously once css_tryget() is guaranteed to fail and when the
4341 * reference count reaches zero, @css will be released.
4343 static void kill_css(struct cgroup_subsys_state
*css
)
4345 cgroup_clear_dir(css
->cgroup
, 1 << css
->ss
->subsys_id
);
4348 * Killing would put the base ref, but we need to keep it alive
4349 * until after ->css_offline().
4354 * cgroup core guarantees that, by the time ->css_offline() is
4355 * invoked, no new css reference will be given out via
4356 * css_tryget(). We can't simply call percpu_ref_kill() and
4357 * proceed to offlining css's because percpu_ref_kill() doesn't
4358 * guarantee that the ref is seen as killed on all CPUs on return.
4360 * Use percpu_ref_kill_and_confirm() to get notifications as each
4361 * css is confirmed to be seen as killed on all CPUs.
4363 percpu_ref_kill_and_confirm(&css
->refcnt
, css_killed_ref_fn
);
4367 * cgroup_destroy_locked - the first stage of cgroup destruction
4368 * @cgrp: cgroup to be destroyed
4370 * css's make use of percpu refcnts whose killing latency shouldn't be
4371 * exposed to userland and are RCU protected. Also, cgroup core needs to
4372 * guarantee that css_tryget() won't succeed by the time ->css_offline() is
4373 * invoked. To satisfy all the requirements, destruction is implemented in
4374 * the following two steps.
4376 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4377 * userland visible parts and start killing the percpu refcnts of
4378 * css's. Set up so that the next stage will be kicked off once all
4379 * the percpu refcnts are confirmed to be killed.
4381 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4382 * rest of destruction. Once all cgroup references are gone, the
4383 * cgroup is RCU-freed.
4385 * This function implements s1. After this step, @cgrp is gone as far as
4386 * the userland is concerned and a new cgroup with the same name may be
4387 * created. As cgroup doesn't care about the names internally, this
4388 * doesn't cause any problem.
4390 static int cgroup_destroy_locked(struct cgroup
*cgrp
)
4391 __releases(&cgroup_mutex
) __acquires(&cgroup_mutex
)
4393 struct dentry
*d
= cgrp
->dentry
;
4394 struct cgroup_subsys_state
*css
;
4395 struct cgroup
*child
;
4399 lockdep_assert_held(&d
->d_inode
->i_mutex
);
4400 lockdep_assert_held(&cgroup_mutex
);
4403 * css_set_lock synchronizes access to ->cset_links and prevents
4404 * @cgrp from being removed while __put_css_set() is in progress.
4406 read_lock(&css_set_lock
);
4407 empty
= list_empty(&cgrp
->cset_links
);
4408 read_unlock(&css_set_lock
);
4413 * Make sure there's no live children. We can't test ->children
4414 * emptiness as dead children linger on it while being destroyed;
4415 * otherwise, "rmdir parent/child parent" may fail with -EBUSY.
4419 list_for_each_entry_rcu(child
, &cgrp
->children
, sibling
) {
4420 empty
= cgroup_is_dead(child
);
4429 * Initiate massacre of all css's. cgroup_destroy_css_killed()
4430 * will be invoked to perform the rest of destruction once the
4431 * percpu refs of all css's are confirmed to be killed.
4433 for_each_css(css
, ssid
, cgrp
)
4437 * Mark @cgrp dead. This prevents further task migration and child
4438 * creation by disabling cgroup_lock_live_group(). Note that
4439 * CGRP_DEAD assertion is depended upon by css_next_child() to
4440 * resume iteration after dropping RCU read lock. See
4441 * css_next_child() for details.
4443 set_bit(CGRP_DEAD
, &cgrp
->flags
);
4445 /* CGRP_DEAD is set, remove from ->release_list for the last time */
4446 raw_spin_lock(&release_list_lock
);
4447 if (!list_empty(&cgrp
->release_list
))
4448 list_del_init(&cgrp
->release_list
);
4449 raw_spin_unlock(&release_list_lock
);
4452 * If @cgrp has css's attached, the second stage of cgroup
4453 * destruction is kicked off from css_killed_work_fn() after the
4454 * refs of all attached css's are killed. If @cgrp doesn't have
4455 * any css, we kick it off here.
4458 cgroup_destroy_css_killed(cgrp
);
4461 * Clear the base files and remove @cgrp directory. The removal
4462 * puts the base ref but we aren't quite done with @cgrp yet, so
4465 cgroup_addrm_files(cgrp
, cgroup_base_files
, false);
4467 cgroup_d_remove_dir(d
);
4473 * cgroup_destroy_css_killed - the second step of cgroup destruction
4474 * @work: cgroup->destroy_free_work
4476 * This function is invoked from a work item for a cgroup which is being
4477 * destroyed after all css's are offlined and performs the rest of
4478 * destruction. This is the second step of destruction described in the
4479 * comment above cgroup_destroy_locked().
4481 static void cgroup_destroy_css_killed(struct cgroup
*cgrp
)
4483 struct cgroup
*parent
= cgrp
->parent
;
4484 struct dentry
*d
= cgrp
->dentry
;
4486 lockdep_assert_held(&cgroup_mutex
);
4488 /* delete this cgroup from parent->children */
4489 list_del_rcu(&cgrp
->sibling
);
4493 set_bit(CGRP_RELEASABLE
, &parent
->flags
);
4494 check_for_release(parent
);
4497 static int cgroup_rmdir(struct inode
*unused_dir
, struct dentry
*dentry
)
4501 mutex_lock(&cgroup_mutex
);
4502 ret
= cgroup_destroy_locked(dentry
->d_fsdata
);
4503 mutex_unlock(&cgroup_mutex
);
4508 static void __init_or_module
cgroup_init_cftsets(struct cgroup_subsys
*ss
)
4510 INIT_LIST_HEAD(&ss
->cftsets
);
4513 * base_cftset is embedded in subsys itself, no need to worry about
4516 if (ss
->base_cftypes
) {
4519 for (cft
= ss
->base_cftypes
; cft
->name
[0] != '\0'; cft
++)
4522 ss
->base_cftset
.cfts
= ss
->base_cftypes
;
4523 list_add_tail(&ss
->base_cftset
.node
, &ss
->cftsets
);
4527 static void __init
cgroup_init_subsys(struct cgroup_subsys
*ss
)
4529 struct cgroup_subsys_state
*css
;
4531 printk(KERN_INFO
"Initializing cgroup subsys %s\n", ss
->name
);
4533 mutex_lock(&cgroup_mutex
);
4535 /* init base cftset */
4536 cgroup_init_cftsets(ss
);
4538 /* Create the top cgroup state for this subsystem */
4539 ss
->root
= &cgroup_dummy_root
;
4540 css
= ss
->css_alloc(cgroup_css(cgroup_dummy_top
, ss
));
4541 /* We don't handle early failures gracefully */
4542 BUG_ON(IS_ERR(css
));
4543 init_css(css
, ss
, cgroup_dummy_top
);
4545 /* Update the init_css_set to contain a subsys
4546 * pointer to this state - since the subsystem is
4547 * newly registered, all tasks and hence the
4548 * init_css_set is in the subsystem's top cgroup. */
4549 init_css_set
.subsys
[ss
->subsys_id
] = css
;
4551 need_forkexit_callback
|= ss
->fork
|| ss
->exit
;
4553 /* At system boot, before all subsystems have been
4554 * registered, no tasks have been forked, so we don't
4555 * need to invoke fork callbacks here. */
4556 BUG_ON(!list_empty(&init_task
.tasks
));
4558 BUG_ON(online_css(css
));
4560 mutex_unlock(&cgroup_mutex
);
4562 /* this function shouldn't be used with modular subsystems, since they
4563 * need to register a subsys_id, among other things */
4568 * cgroup_load_subsys: load and register a modular subsystem at runtime
4569 * @ss: the subsystem to load
4571 * This function should be called in a modular subsystem's initcall. If the
4572 * subsystem is built as a module, it will be assigned a new subsys_id and set
4573 * up for use. If the subsystem is built-in anyway, work is delegated to the
4574 * simpler cgroup_init_subsys.
4576 int __init_or_module
cgroup_load_subsys(struct cgroup_subsys
*ss
)
4578 struct cgroup_subsys_state
*css
;
4580 struct hlist_node
*tmp
;
4581 struct css_set
*cset
;
4584 /* check name and function validity */
4585 if (ss
->name
== NULL
|| strlen(ss
->name
) > MAX_CGROUP_TYPE_NAMELEN
||
4586 ss
->css_alloc
== NULL
|| ss
->css_free
== NULL
)
4590 * we don't support callbacks in modular subsystems. this check is
4591 * before the ss->module check for consistency; a subsystem that could
4592 * be a module should still have no callbacks even if the user isn't
4593 * compiling it as one.
4595 if (ss
->fork
|| ss
->exit
)
4599 * an optionally modular subsystem is built-in: we want to do nothing,
4600 * since cgroup_init_subsys will have already taken care of it.
4602 if (ss
->module
== NULL
) {
4603 /* a sanity check */
4604 BUG_ON(cgroup_subsys
[ss
->subsys_id
] != ss
);
4608 /* init base cftset */
4609 cgroup_init_cftsets(ss
);
4611 mutex_lock(&cgroup_mutex
);
4612 mutex_lock(&cgroup_root_mutex
);
4613 cgroup_subsys
[ss
->subsys_id
] = ss
;
4616 * no ss->css_alloc seems to need anything important in the ss
4617 * struct, so this can happen first (i.e. before the dummy root
4620 css
= ss
->css_alloc(cgroup_css(cgroup_dummy_top
, ss
));
4622 /* failure case - need to deassign the cgroup_subsys[] slot. */
4623 cgroup_subsys
[ss
->subsys_id
] = NULL
;
4624 mutex_unlock(&cgroup_root_mutex
);
4625 mutex_unlock(&cgroup_mutex
);
4626 return PTR_ERR(css
);
4629 ss
->root
= &cgroup_dummy_root
;
4631 /* our new subsystem will be attached to the dummy hierarchy. */
4632 init_css(css
, ss
, cgroup_dummy_top
);
4635 * Now we need to entangle the css into the existing css_sets. unlike
4636 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4637 * will need a new pointer to it; done by iterating the css_set_table.
4638 * furthermore, modifying the existing css_sets will corrupt the hash
4639 * table state, so each changed css_set will need its hash recomputed.
4640 * this is all done under the css_set_lock.
4642 write_lock(&css_set_lock
);
4643 hash_for_each_safe(css_set_table
, i
, tmp
, cset
, hlist
) {
4644 /* skip entries that we already rehashed */
4645 if (cset
->subsys
[ss
->subsys_id
])
4647 /* remove existing entry */
4648 hash_del(&cset
->hlist
);
4650 cset
->subsys
[ss
->subsys_id
] = css
;
4651 /* recompute hash and restore entry */
4652 key
= css_set_hash(cset
->subsys
);
4653 hash_add(css_set_table
, &cset
->hlist
, key
);
4655 write_unlock(&css_set_lock
);
4657 ret
= online_css(css
);
4664 mutex_unlock(&cgroup_root_mutex
);
4665 mutex_unlock(&cgroup_mutex
);
4669 mutex_unlock(&cgroup_root_mutex
);
4670 mutex_unlock(&cgroup_mutex
);
4671 /* @ss can't be mounted here as try_module_get() would fail */
4672 cgroup_unload_subsys(ss
);
4675 EXPORT_SYMBOL_GPL(cgroup_load_subsys
);
4678 * cgroup_unload_subsys: unload a modular subsystem
4679 * @ss: the subsystem to unload
4681 * This function should be called in a modular subsystem's exitcall. When this
4682 * function is invoked, the refcount on the subsystem's module will be 0, so
4683 * the subsystem will not be attached to any hierarchy.
4685 void cgroup_unload_subsys(struct cgroup_subsys
*ss
)
4687 struct cgrp_cset_link
*link
;
4688 struct cgroup_subsys_state
*css
;
4690 BUG_ON(ss
->module
== NULL
);
4693 * we shouldn't be called if the subsystem is in use, and the use of
4694 * try_module_get() in rebind_subsystems() should ensure that it
4695 * doesn't start being used while we're killing it off.
4697 BUG_ON(ss
->root
!= &cgroup_dummy_root
);
4699 mutex_lock(&cgroup_mutex
);
4700 mutex_lock(&cgroup_root_mutex
);
4702 css
= cgroup_css(cgroup_dummy_top
, ss
);
4706 /* deassign the subsys_id */
4707 cgroup_subsys
[ss
->subsys_id
] = NULL
;
4710 * disentangle the css from all css_sets attached to the dummy
4711 * top. as in loading, we need to pay our respects to the hashtable
4714 write_lock(&css_set_lock
);
4715 list_for_each_entry(link
, &cgroup_dummy_top
->cset_links
, cset_link
) {
4716 struct css_set
*cset
= link
->cset
;
4719 hash_del(&cset
->hlist
);
4720 cset
->subsys
[ss
->subsys_id
] = NULL
;
4721 key
= css_set_hash(cset
->subsys
);
4722 hash_add(css_set_table
, &cset
->hlist
, key
);
4724 write_unlock(&css_set_lock
);
4727 * remove subsystem's css from the cgroup_dummy_top and free it -
4728 * need to free before marking as null because ss->css_free needs
4729 * the cgrp->subsys pointer to find their state.
4733 RCU_INIT_POINTER(cgroup_dummy_top
->subsys
[ss
->subsys_id
], NULL
);
4735 mutex_unlock(&cgroup_root_mutex
);
4736 mutex_unlock(&cgroup_mutex
);
4738 EXPORT_SYMBOL_GPL(cgroup_unload_subsys
);
4741 * cgroup_init_early - cgroup initialization at system boot
4743 * Initialize cgroups at system boot, and initialize any
4744 * subsystems that request early init.
4746 int __init
cgroup_init_early(void)
4748 struct cgroup_subsys
*ss
;
4751 atomic_set(&init_css_set
.refcount
, 1);
4752 INIT_LIST_HEAD(&init_css_set
.cgrp_links
);
4753 INIT_LIST_HEAD(&init_css_set
.tasks
);
4754 INIT_HLIST_NODE(&init_css_set
.hlist
);
4756 init_cgroup_root(&cgroup_dummy_root
);
4757 cgroup_root_count
= 1;
4758 RCU_INIT_POINTER(init_task
.cgroups
, &init_css_set
);
4760 init_cgrp_cset_link
.cset
= &init_css_set
;
4761 init_cgrp_cset_link
.cgrp
= cgroup_dummy_top
;
4762 list_add(&init_cgrp_cset_link
.cset_link
, &cgroup_dummy_top
->cset_links
);
4763 list_add(&init_cgrp_cset_link
.cgrp_link
, &init_css_set
.cgrp_links
);
4765 /* at bootup time, we don't worry about modular subsystems */
4766 for_each_builtin_subsys(ss
, i
) {
4768 BUG_ON(strlen(ss
->name
) > MAX_CGROUP_TYPE_NAMELEN
);
4769 BUG_ON(!ss
->css_alloc
);
4770 BUG_ON(!ss
->css_free
);
4771 if (ss
->subsys_id
!= i
) {
4772 printk(KERN_ERR
"cgroup: Subsys %s id == %d\n",
4773 ss
->name
, ss
->subsys_id
);
4778 cgroup_init_subsys(ss
);
4784 * cgroup_init - cgroup initialization
4786 * Register cgroup filesystem and /proc file, and initialize
4787 * any subsystems that didn't request early init.
4789 int __init
cgroup_init(void)
4791 struct cgroup_subsys
*ss
;
4795 err
= bdi_init(&cgroup_backing_dev_info
);
4799 for_each_builtin_subsys(ss
, i
) {
4800 if (!ss
->early_init
)
4801 cgroup_init_subsys(ss
);
4804 /* allocate id for the dummy hierarchy */
4805 mutex_lock(&cgroup_mutex
);
4806 mutex_lock(&cgroup_root_mutex
);
4808 /* Add init_css_set to the hash table */
4809 key
= css_set_hash(init_css_set
.subsys
);
4810 hash_add(css_set_table
, &init_css_set
.hlist
, key
);
4812 BUG_ON(cgroup_init_root_id(&cgroup_dummy_root
, 0, 1));
4814 err
= idr_alloc(&cgroup_dummy_root
.cgroup_idr
, cgroup_dummy_top
,
4818 mutex_unlock(&cgroup_root_mutex
);
4819 mutex_unlock(&cgroup_mutex
);
4821 cgroup_kobj
= kobject_create_and_add("cgroup", fs_kobj
);
4827 err
= register_filesystem(&cgroup_fs_type
);
4829 kobject_put(cgroup_kobj
);
4833 proc_create("cgroups", 0, NULL
, &proc_cgroupstats_operations
);
4837 bdi_destroy(&cgroup_backing_dev_info
);
4842 static int __init
cgroup_wq_init(void)
4845 * There isn't much point in executing destruction path in
4846 * parallel. Good chunk is serialized with cgroup_mutex anyway.
4848 * XXX: Must be ordered to make sure parent is offlined after
4849 * children. The ordering requirement is for memcg where a
4850 * parent's offline may wait for a child's leading to deadlock. In
4851 * the long term, this should be fixed from memcg side.
4853 * We would prefer to do this in cgroup_init() above, but that
4854 * is called before init_workqueues(): so leave this until after.
4856 cgroup_destroy_wq
= alloc_ordered_workqueue("cgroup_destroy", 0);
4857 BUG_ON(!cgroup_destroy_wq
);
4860 * Used to destroy pidlists and separate to serve as flush domain.
4861 * Cap @max_active to 1 too.
4863 cgroup_pidlist_destroy_wq
= alloc_workqueue("cgroup_pidlist_destroy",
4865 BUG_ON(!cgroup_pidlist_destroy_wq
);
4869 core_initcall(cgroup_wq_init
);
4872 * proc_cgroup_show()
4873 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4874 * - Used for /proc/<pid>/cgroup.
4875 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4876 * doesn't really matter if tsk->cgroup changes after we read it,
4877 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
4878 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4879 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4880 * cgroup to top_cgroup.
4883 /* TODO: Use a proper seq_file iterator */
4884 int proc_cgroup_show(struct seq_file
*m
, void *v
)
4887 struct task_struct
*tsk
;
4890 struct cgroupfs_root
*root
;
4893 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
4899 tsk
= get_pid_task(pid
, PIDTYPE_PID
);
4905 mutex_lock(&cgroup_mutex
);
4907 for_each_active_root(root
) {
4908 struct cgroup_subsys
*ss
;
4909 struct cgroup
*cgrp
;
4910 int ssid
, count
= 0;
4912 seq_printf(m
, "%d:", root
->hierarchy_id
);
4913 for_each_subsys(ss
, ssid
)
4914 if (root
->subsys_mask
& (1 << ssid
))
4915 seq_printf(m
, "%s%s", count
++ ? "," : "", ss
->name
);
4916 if (strlen(root
->name
))
4917 seq_printf(m
, "%sname=%s", count
? "," : "",
4920 cgrp
= task_cgroup_from_root(tsk
, root
);
4921 retval
= cgroup_path(cgrp
, buf
, PAGE_SIZE
);
4929 mutex_unlock(&cgroup_mutex
);
4930 put_task_struct(tsk
);
4937 /* Display information about each subsystem and each hierarchy */
4938 static int proc_cgroupstats_show(struct seq_file
*m
, void *v
)
4940 struct cgroup_subsys
*ss
;
4943 seq_puts(m
, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
4945 * ideally we don't want subsystems moving around while we do this.
4946 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4947 * subsys/hierarchy state.
4949 mutex_lock(&cgroup_mutex
);
4951 for_each_subsys(ss
, i
)
4952 seq_printf(m
, "%s\t%d\t%d\t%d\n",
4953 ss
->name
, ss
->root
->hierarchy_id
,
4954 ss
->root
->number_of_cgroups
, !ss
->disabled
);
4956 mutex_unlock(&cgroup_mutex
);
4960 static int cgroupstats_open(struct inode
*inode
, struct file
*file
)
4962 return single_open(file
, proc_cgroupstats_show
, NULL
);
4965 static const struct file_operations proc_cgroupstats_operations
= {
4966 .open
= cgroupstats_open
,
4968 .llseek
= seq_lseek
,
4969 .release
= single_release
,
4973 * cgroup_fork - attach newly forked task to its parents cgroup.
4974 * @child: pointer to task_struct of forking parent process.
4976 * Description: A task inherits its parent's cgroup at fork().
4978 * A pointer to the shared css_set was automatically copied in
4979 * fork.c by dup_task_struct(). However, we ignore that copy, since
4980 * it was not made under the protection of RCU or cgroup_mutex, so
4981 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
4982 * have already changed current->cgroups, allowing the previously
4983 * referenced cgroup group to be removed and freed.
4985 * At the point that cgroup_fork() is called, 'current' is the parent
4986 * task, and the passed argument 'child' points to the child task.
4988 void cgroup_fork(struct task_struct
*child
)
4991 get_css_set(task_css_set(current
));
4992 child
->cgroups
= current
->cgroups
;
4993 task_unlock(current
);
4994 INIT_LIST_HEAD(&child
->cg_list
);
4998 * cgroup_post_fork - called on a new task after adding it to the task list
4999 * @child: the task in question
5001 * Adds the task to the list running through its css_set if necessary and
5002 * call the subsystem fork() callbacks. Has to be after the task is
5003 * visible on the task list in case we race with the first call to
5004 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
5007 void cgroup_post_fork(struct task_struct
*child
)
5009 struct cgroup_subsys
*ss
;
5013 * use_task_css_set_links is set to 1 before we walk the tasklist
5014 * under the tasklist_lock and we read it here after we added the child
5015 * to the tasklist under the tasklist_lock as well. If the child wasn't
5016 * yet in the tasklist when we walked through it from
5017 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
5018 * should be visible now due to the paired locking and barriers implied
5019 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
5020 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
5023 if (use_task_css_set_links
) {
5024 write_lock(&css_set_lock
);
5026 if (list_empty(&child
->cg_list
))
5027 list_add(&child
->cg_list
, &task_css_set(child
)->tasks
);
5029 write_unlock(&css_set_lock
);
5033 * Call ss->fork(). This must happen after @child is linked on
5034 * css_set; otherwise, @child might change state between ->fork()
5035 * and addition to css_set.
5037 if (need_forkexit_callback
) {
5039 * fork/exit callbacks are supported only for builtin
5040 * subsystems, and the builtin section of the subsys
5041 * array is immutable, so we don't need to lock the
5042 * subsys array here. On the other hand, modular section
5043 * of the array can be freed at module unload, so we
5046 for_each_builtin_subsys(ss
, i
)
5053 * cgroup_exit - detach cgroup from exiting task
5054 * @tsk: pointer to task_struct of exiting process
5055 * @run_callback: run exit callbacks?
5057 * Description: Detach cgroup from @tsk and release it.
5059 * Note that cgroups marked notify_on_release force every task in
5060 * them to take the global cgroup_mutex mutex when exiting.
5061 * This could impact scaling on very large systems. Be reluctant to
5062 * use notify_on_release cgroups where very high task exit scaling
5063 * is required on large systems.
5065 * the_top_cgroup_hack:
5067 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
5069 * We call cgroup_exit() while the task is still competent to
5070 * handle notify_on_release(), then leave the task attached to the
5071 * root cgroup in each hierarchy for the remainder of its exit.
5073 * To do this properly, we would increment the reference count on
5074 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
5075 * code we would add a second cgroup function call, to drop that
5076 * reference. This would just create an unnecessary hot spot on
5077 * the top_cgroup reference count, to no avail.
5079 * Normally, holding a reference to a cgroup without bumping its
5080 * count is unsafe. The cgroup could go away, or someone could
5081 * attach us to a different cgroup, decrementing the count on
5082 * the first cgroup that we never incremented. But in this case,
5083 * top_cgroup isn't going away, and either task has PF_EXITING set,
5084 * which wards off any cgroup_attach_task() attempts, or task is a failed
5085 * fork, never visible to cgroup_attach_task.
5087 void cgroup_exit(struct task_struct
*tsk
, int run_callbacks
)
5089 struct cgroup_subsys
*ss
;
5090 struct css_set
*cset
;
5094 * Unlink from the css_set task list if necessary.
5095 * Optimistically check cg_list before taking
5098 if (!list_empty(&tsk
->cg_list
)) {
5099 write_lock(&css_set_lock
);
5100 if (!list_empty(&tsk
->cg_list
))
5101 list_del_init(&tsk
->cg_list
);
5102 write_unlock(&css_set_lock
);
5105 /* Reassign the task to the init_css_set. */
5107 cset
= task_css_set(tsk
);
5108 RCU_INIT_POINTER(tsk
->cgroups
, &init_css_set
);
5110 if (run_callbacks
&& need_forkexit_callback
) {
5112 * fork/exit callbacks are supported only for builtin
5113 * subsystems, see cgroup_post_fork() for details.
5115 for_each_builtin_subsys(ss
, i
) {
5117 struct cgroup_subsys_state
*old_css
= cset
->subsys
[i
];
5118 struct cgroup_subsys_state
*css
= task_css(tsk
, i
);
5120 ss
->exit(css
, old_css
, tsk
);
5126 put_css_set_taskexit(cset
);
5129 static void check_for_release(struct cgroup
*cgrp
)
5131 if (cgroup_is_releasable(cgrp
) &&
5132 list_empty(&cgrp
->cset_links
) && list_empty(&cgrp
->children
)) {
5134 * Control Group is currently removeable. If it's not
5135 * already queued for a userspace notification, queue
5138 int need_schedule_work
= 0;
5140 raw_spin_lock(&release_list_lock
);
5141 if (!cgroup_is_dead(cgrp
) &&
5142 list_empty(&cgrp
->release_list
)) {
5143 list_add(&cgrp
->release_list
, &release_list
);
5144 need_schedule_work
= 1;
5146 raw_spin_unlock(&release_list_lock
);
5147 if (need_schedule_work
)
5148 schedule_work(&release_agent_work
);
5153 * Notify userspace when a cgroup is released, by running the
5154 * configured release agent with the name of the cgroup (path
5155 * relative to the root of cgroup file system) as the argument.
5157 * Most likely, this user command will try to rmdir this cgroup.
5159 * This races with the possibility that some other task will be
5160 * attached to this cgroup before it is removed, or that some other
5161 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5162 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5163 * unused, and this cgroup will be reprieved from its death sentence,
5164 * to continue to serve a useful existence. Next time it's released,
5165 * we will get notified again, if it still has 'notify_on_release' set.
5167 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5168 * means only wait until the task is successfully execve()'d. The
5169 * separate release agent task is forked by call_usermodehelper(),
5170 * then control in this thread returns here, without waiting for the
5171 * release agent task. We don't bother to wait because the caller of
5172 * this routine has no use for the exit status of the release agent
5173 * task, so no sense holding our caller up for that.
5175 static void cgroup_release_agent(struct work_struct
*work
)
5177 BUG_ON(work
!= &release_agent_work
);
5178 mutex_lock(&cgroup_mutex
);
5179 raw_spin_lock(&release_list_lock
);
5180 while (!list_empty(&release_list
)) {
5181 char *argv
[3], *envp
[3];
5183 char *pathbuf
= NULL
, *agentbuf
= NULL
;
5184 struct cgroup
*cgrp
= list_entry(release_list
.next
,
5187 list_del_init(&cgrp
->release_list
);
5188 raw_spin_unlock(&release_list_lock
);
5189 pathbuf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
5192 if (cgroup_path(cgrp
, pathbuf
, PAGE_SIZE
) < 0)
5194 agentbuf
= kstrdup(cgrp
->root
->release_agent_path
, GFP_KERNEL
);
5199 argv
[i
++] = agentbuf
;
5200 argv
[i
++] = pathbuf
;
5204 /* minimal command environment */
5205 envp
[i
++] = "HOME=/";
5206 envp
[i
++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5209 /* Drop the lock while we invoke the usermode helper,
5210 * since the exec could involve hitting disk and hence
5211 * be a slow process */
5212 mutex_unlock(&cgroup_mutex
);
5213 call_usermodehelper(argv
[0], argv
, envp
, UMH_WAIT_EXEC
);
5214 mutex_lock(&cgroup_mutex
);
5218 raw_spin_lock(&release_list_lock
);
5220 raw_spin_unlock(&release_list_lock
);
5221 mutex_unlock(&cgroup_mutex
);
5224 static int __init
cgroup_disable(char *str
)
5226 struct cgroup_subsys
*ss
;
5230 while ((token
= strsep(&str
, ",")) != NULL
) {
5235 * cgroup_disable, being at boot time, can't know about
5236 * module subsystems, so we don't worry about them.
5238 for_each_builtin_subsys(ss
, i
) {
5239 if (!strcmp(token
, ss
->name
)) {
5241 printk(KERN_INFO
"Disabling %s control group"
5242 " subsystem\n", ss
->name
);
5249 __setup("cgroup_disable=", cgroup_disable
);
5252 * css_from_dir - get corresponding css from the dentry of a cgroup dir
5253 * @dentry: directory dentry of interest
5254 * @ss: subsystem of interest
5256 * Must be called under cgroup_mutex or RCU read lock. The caller is
5257 * responsible for pinning the returned css if it needs to be accessed
5258 * outside the critical section.
5260 struct cgroup_subsys_state
*css_from_dir(struct dentry
*dentry
,
5261 struct cgroup_subsys
*ss
)
5263 struct cgroup
*cgrp
;
5265 cgroup_assert_mutex_or_rcu_locked();
5267 /* is @dentry a cgroup dir? */
5268 if (!dentry
->d_inode
||
5269 dentry
->d_inode
->i_op
!= &cgroup_dir_inode_operations
)
5270 return ERR_PTR(-EBADF
);
5272 cgrp
= __d_cgrp(dentry
);
5273 return cgroup_css(cgrp
, ss
) ?: ERR_PTR(-ENOENT
);
5277 * css_from_id - lookup css by id
5278 * @id: the cgroup id
5279 * @ss: cgroup subsys to be looked into
5281 * Returns the css if there's valid one with @id, otherwise returns NULL.
5282 * Should be called under rcu_read_lock().
5284 struct cgroup_subsys_state
*css_from_id(int id
, struct cgroup_subsys
*ss
)
5286 struct cgroup
*cgrp
;
5288 cgroup_assert_mutex_or_rcu_locked();
5290 cgrp
= idr_find(&ss
->root
->cgroup_idr
, id
);
5292 return cgroup_css(cgrp
, ss
);
5296 #ifdef CONFIG_CGROUP_DEBUG
5297 static struct cgroup_subsys_state
*
5298 debug_css_alloc(struct cgroup_subsys_state
*parent_css
)
5300 struct cgroup_subsys_state
*css
= kzalloc(sizeof(*css
), GFP_KERNEL
);
5303 return ERR_PTR(-ENOMEM
);
5308 static void debug_css_free(struct cgroup_subsys_state
*css
)
5313 static u64
debug_taskcount_read(struct cgroup_subsys_state
*css
,
5316 return cgroup_task_count(css
->cgroup
);
5319 static u64
current_css_set_read(struct cgroup_subsys_state
*css
,
5322 return (u64
)(unsigned long)current
->cgroups
;
5325 static u64
current_css_set_refcount_read(struct cgroup_subsys_state
*css
,
5331 count
= atomic_read(&task_css_set(current
)->refcount
);
5336 static int current_css_set_cg_links_read(struct seq_file
*seq
, void *v
)
5338 struct cgrp_cset_link
*link
;
5339 struct css_set
*cset
;
5341 read_lock(&css_set_lock
);
5343 cset
= rcu_dereference(current
->cgroups
);
5344 list_for_each_entry(link
, &cset
->cgrp_links
, cgrp_link
) {
5345 struct cgroup
*c
= link
->cgrp
;
5349 name
= c
->dentry
->d_name
.name
;
5352 seq_printf(seq
, "Root %d group %s\n",
5353 c
->root
->hierarchy_id
, name
);
5356 read_unlock(&css_set_lock
);
5360 #define MAX_TASKS_SHOWN_PER_CSS 25
5361 static int cgroup_css_links_read(struct seq_file
*seq
, void *v
)
5363 struct cgroup_subsys_state
*css
= seq_css(seq
);
5364 struct cgrp_cset_link
*link
;
5366 read_lock(&css_set_lock
);
5367 list_for_each_entry(link
, &css
->cgroup
->cset_links
, cset_link
) {
5368 struct css_set
*cset
= link
->cset
;
5369 struct task_struct
*task
;
5371 seq_printf(seq
, "css_set %p\n", cset
);
5372 list_for_each_entry(task
, &cset
->tasks
, cg_list
) {
5373 if (count
++ > MAX_TASKS_SHOWN_PER_CSS
) {
5374 seq_puts(seq
, " ...\n");
5377 seq_printf(seq
, " task %d\n",
5378 task_pid_vnr(task
));
5382 read_unlock(&css_set_lock
);
5386 static u64
releasable_read(struct cgroup_subsys_state
*css
, struct cftype
*cft
)
5388 return test_bit(CGRP_RELEASABLE
, &css
->cgroup
->flags
);
5391 static struct cftype debug_files
[] = {
5393 .name
= "taskcount",
5394 .read_u64
= debug_taskcount_read
,
5398 .name
= "current_css_set",
5399 .read_u64
= current_css_set_read
,
5403 .name
= "current_css_set_refcount",
5404 .read_u64
= current_css_set_refcount_read
,
5408 .name
= "current_css_set_cg_links",
5409 .seq_show
= current_css_set_cg_links_read
,
5413 .name
= "cgroup_css_links",
5414 .seq_show
= cgroup_css_links_read
,
5418 .name
= "releasable",
5419 .read_u64
= releasable_read
,
5425 struct cgroup_subsys debug_subsys
= {
5427 .css_alloc
= debug_css_alloc
,
5428 .css_free
= debug_css_free
,
5429 .subsys_id
= debug_subsys_id
,
5430 .base_cftypes
= debug_files
,
5432 #endif /* CONFIG_CGROUP_DEBUG */