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/delayacct.h>
51 #include <linux/cgroupstats.h>
52 #include <linux/hashtable.h>
53 #include <linux/namei.h>
54 #include <linux/pid_namespace.h>
55 #include <linux/idr.h>
56 #include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
57 #include <linux/flex_array.h> /* used in cgroup_attach_task */
58 #include <linux/kthread.h>
60 #include <linux/atomic.h>
63 * pidlists linger the following amount before being destroyed. The goal
64 * is avoiding frequent destruction in the middle of consecutive read calls
65 * Expiring in the middle is a performance problem not a correctness one.
66 * 1 sec should be enough.
68 #define CGROUP_PIDLIST_DESTROY_DELAY HZ
70 #define CGROUP_FILE_NAME_MAX (MAX_CGROUP_TYPE_NAMELEN + \
74 * cgroup_tree_mutex nests above cgroup_mutex and protects cftypes, file
75 * creation/removal and hierarchy changing operations including cgroup
76 * creation, removal, css association and controller rebinding. This outer
77 * lock is needed mainly to resolve the circular dependency between kernfs
78 * active ref and cgroup_mutex. cgroup_tree_mutex nests above both.
80 static DEFINE_MUTEX(cgroup_tree_mutex
);
83 * cgroup_mutex is the master lock. Any modification to cgroup or its
84 * hierarchy must be performed while holding it.
86 #ifdef CONFIG_PROVE_RCU
87 DEFINE_MUTEX(cgroup_mutex
);
88 EXPORT_SYMBOL_GPL(cgroup_mutex
); /* only for lockdep */
90 static DEFINE_MUTEX(cgroup_mutex
);
94 * Protects cgroup_subsys->release_agent_path. Modifying it also requires
95 * cgroup_mutex. Reading requires either cgroup_mutex or this spinlock.
97 static DEFINE_SPINLOCK(release_agent_path_lock
);
99 #define cgroup_assert_mutexes_or_rcu_locked() \
100 rcu_lockdep_assert(rcu_read_lock_held() || \
101 lockdep_is_held(&cgroup_tree_mutex) || \
102 lockdep_is_held(&cgroup_mutex), \
103 "cgroup_[tree_]mutex or RCU read lock required");
106 * cgroup destruction makes heavy use of work items and there can be a lot
107 * of concurrent destructions. Use a separate workqueue so that cgroup
108 * destruction work items don't end up filling up max_active of system_wq
109 * which may lead to deadlock.
111 static struct workqueue_struct
*cgroup_destroy_wq
;
114 * pidlist destructions need to be flushed on cgroup destruction. Use a
115 * separate workqueue as flush domain.
117 static struct workqueue_struct
*cgroup_pidlist_destroy_wq
;
119 /* generate an array of cgroup subsystem pointers */
120 #define SUBSYS(_x) [_x ## _cgrp_id] = &_x ## _cgrp_subsys,
121 static struct cgroup_subsys
*cgroup_subsys
[] = {
122 #include <linux/cgroup_subsys.h>
126 /* array of cgroup subsystem names */
127 #define SUBSYS(_x) [_x ## _cgrp_id] = #_x,
128 static const char *cgroup_subsys_name
[] = {
129 #include <linux/cgroup_subsys.h>
134 * The dummy hierarchy, reserved for the subsystems that are otherwise
135 * unattached - it never has more than a single cgroup, and all tasks are
136 * part of that cgroup.
138 static struct cgroupfs_root cgroup_dummy_root
;
140 /* dummy_top is a shorthand for the dummy hierarchy's top cgroup */
141 static struct cgroup
* const cgroup_dummy_top
= &cgroup_dummy_root
.top_cgroup
;
143 /* The list of hierarchy roots */
145 static LIST_HEAD(cgroup_roots
);
146 static int cgroup_root_count
;
148 /* hierarchy ID allocation and mapping, protected by cgroup_mutex */
149 static DEFINE_IDR(cgroup_hierarchy_idr
);
151 static struct cgroup_name root_cgroup_name
= { .name
= "/" };
154 * Assign a monotonically increasing serial number to cgroups. It
155 * guarantees cgroups with bigger numbers are newer than those with smaller
156 * numbers. Also, as cgroups are always appended to the parent's
157 * ->children list, it guarantees that sibling cgroups are always sorted in
158 * the ascending serial number order on the list. Protected by
161 static u64 cgroup_serial_nr_next
= 1;
163 /* This flag indicates whether tasks in the fork and exit paths should
164 * check for fork/exit handlers to call. This avoids us having to do
165 * extra work in the fork/exit path if none of the subsystems need to
168 static int need_forkexit_callback __read_mostly
;
170 static struct cftype cgroup_base_files
[];
172 static void cgroup_put(struct cgroup
*cgrp
);
173 static void cgroup_destroy_css_killed(struct cgroup
*cgrp
);
174 static int cgroup_destroy_locked(struct cgroup
*cgrp
);
175 static int cgroup_addrm_files(struct cgroup
*cgrp
, struct cftype cfts
[],
177 static int cgroup_file_release(struct inode
*inode
, struct file
*file
);
178 static void cgroup_pidlist_destroy_all(struct cgroup
*cgrp
);
181 * cgroup_css - obtain a cgroup's css for the specified subsystem
182 * @cgrp: the cgroup of interest
183 * @ss: the subsystem of interest (%NULL returns the dummy_css)
185 * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This
186 * function must be called either under cgroup_mutex or rcu_read_lock() and
187 * the caller is responsible for pinning the returned css if it wants to
188 * keep accessing it outside the said locks. This function may return
189 * %NULL if @cgrp doesn't have @subsys_id enabled.
191 static struct cgroup_subsys_state
*cgroup_css(struct cgroup
*cgrp
,
192 struct cgroup_subsys
*ss
)
195 return rcu_dereference_check(cgrp
->subsys
[ss
->id
],
196 lockdep_is_held(&cgroup_tree_mutex
) ||
197 lockdep_is_held(&cgroup_mutex
));
199 return &cgrp
->dummy_css
;
202 /* convenient tests for these bits */
203 static inline bool cgroup_is_dead(const struct cgroup
*cgrp
)
205 return test_bit(CGRP_DEAD
, &cgrp
->flags
);
208 struct cgroup_subsys_state
*seq_css(struct seq_file
*seq
)
210 struct cgroup_open_file
*of
= seq
->private;
213 EXPORT_SYMBOL_GPL(seq_css
);
216 * cgroup_is_descendant - test ancestry
217 * @cgrp: the cgroup to be tested
218 * @ancestor: possible ancestor of @cgrp
220 * Test whether @cgrp is a descendant of @ancestor. It also returns %true
221 * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
222 * and @ancestor are accessible.
224 bool cgroup_is_descendant(struct cgroup
*cgrp
, struct cgroup
*ancestor
)
227 if (cgrp
== ancestor
)
233 EXPORT_SYMBOL_GPL(cgroup_is_descendant
);
235 static int cgroup_is_releasable(const struct cgroup
*cgrp
)
238 (1 << CGRP_RELEASABLE
) |
239 (1 << CGRP_NOTIFY_ON_RELEASE
);
240 return (cgrp
->flags
& bits
) == bits
;
243 static int notify_on_release(const struct cgroup
*cgrp
)
245 return test_bit(CGRP_NOTIFY_ON_RELEASE
, &cgrp
->flags
);
249 * for_each_css - iterate all css's of a cgroup
250 * @css: the iteration cursor
251 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
252 * @cgrp: the target cgroup to iterate css's of
254 * Should be called under cgroup_mutex.
256 #define for_each_css(css, ssid, cgrp) \
257 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
258 if (!((css) = rcu_dereference_check( \
259 (cgrp)->subsys[(ssid)], \
260 lockdep_is_held(&cgroup_tree_mutex) || \
261 lockdep_is_held(&cgroup_mutex)))) { } \
265 * for_each_subsys - iterate all enabled cgroup subsystems
266 * @ss: the iteration cursor
267 * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
269 #define for_each_subsys(ss, ssid) \
270 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT && \
271 (((ss) = cgroup_subsys[ssid]) || true); (ssid)++)
273 /* iterate across the active hierarchies */
274 #define for_each_active_root(root) \
275 list_for_each_entry((root), &cgroup_roots, root_list)
277 static inline struct cgroup
*__d_cgrp(struct dentry
*dentry
)
279 return dentry
->d_fsdata
;
282 static inline struct cfent
*__d_cfe(struct dentry
*dentry
)
284 return dentry
->d_fsdata
;
287 static inline struct cftype
*__d_cft(struct dentry
*dentry
)
289 return __d_cfe(dentry
)->type
;
293 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
294 * @cgrp: the cgroup to be checked for liveness
296 * On success, returns true; the mutex should be later unlocked. On
297 * failure returns false with no lock held.
299 static bool cgroup_lock_live_group(struct cgroup
*cgrp
)
301 mutex_lock(&cgroup_mutex
);
302 if (cgroup_is_dead(cgrp
)) {
303 mutex_unlock(&cgroup_mutex
);
309 /* the list of cgroups eligible for automatic release. Protected by
310 * release_list_lock */
311 static LIST_HEAD(release_list
);
312 static DEFINE_RAW_SPINLOCK(release_list_lock
);
313 static void cgroup_release_agent(struct work_struct
*work
);
314 static DECLARE_WORK(release_agent_work
, cgroup_release_agent
);
315 static void check_for_release(struct cgroup
*cgrp
);
318 * A cgroup can be associated with multiple css_sets as different tasks may
319 * belong to different cgroups on different hierarchies. In the other
320 * direction, a css_set is naturally associated with multiple cgroups.
321 * This M:N relationship is represented by the following link structure
322 * which exists for each association and allows traversing the associations
325 struct cgrp_cset_link
{
326 /* the cgroup and css_set this link associates */
328 struct css_set
*cset
;
330 /* list of cgrp_cset_links anchored at cgrp->cset_links */
331 struct list_head cset_link
;
333 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
334 struct list_head cgrp_link
;
337 /* The default css_set - used by init and its children prior to any
338 * hierarchies being mounted. It contains a pointer to the root state
339 * for each subsystem. Also used to anchor the list of css_sets. Not
340 * reference-counted, to improve performance when child cgroups
341 * haven't been created.
344 static struct css_set init_css_set
;
345 static struct cgrp_cset_link init_cgrp_cset_link
;
348 * css_set_lock protects the list of css_set objects, and the chain of
349 * tasks off each css_set. Nests outside task->alloc_lock due to
350 * css_task_iter_start().
352 static DEFINE_RWLOCK(css_set_lock
);
353 static int css_set_count
;
356 * hash table for cgroup groups. This improves the performance to find
357 * an existing css_set. This hash doesn't (currently) take into
358 * account cgroups in empty hierarchies.
360 #define CSS_SET_HASH_BITS 7
361 static DEFINE_HASHTABLE(css_set_table
, CSS_SET_HASH_BITS
);
363 static unsigned long css_set_hash(struct cgroup_subsys_state
*css
[])
365 unsigned long key
= 0UL;
366 struct cgroup_subsys
*ss
;
369 for_each_subsys(ss
, i
)
370 key
+= (unsigned long)css
[i
];
371 key
= (key
>> 16) ^ key
;
377 * We don't maintain the lists running through each css_set to its task
378 * until after the first call to css_task_iter_start(). This reduces the
379 * fork()/exit() overhead for people who have cgroups compiled into their
380 * kernel but not actually in use.
382 static int use_task_css_set_links __read_mostly
;
384 static void __put_css_set(struct css_set
*cset
, int taskexit
)
386 struct cgrp_cset_link
*link
, *tmp_link
;
389 * Ensure that the refcount doesn't hit zero while any readers
390 * can see it. Similar to atomic_dec_and_lock(), but for an
393 if (atomic_add_unless(&cset
->refcount
, -1, 1))
395 write_lock(&css_set_lock
);
396 if (!atomic_dec_and_test(&cset
->refcount
)) {
397 write_unlock(&css_set_lock
);
401 /* This css_set is dead. unlink it and release cgroup refcounts */
402 hash_del(&cset
->hlist
);
405 list_for_each_entry_safe(link
, tmp_link
, &cset
->cgrp_links
, cgrp_link
) {
406 struct cgroup
*cgrp
= link
->cgrp
;
408 list_del(&link
->cset_link
);
409 list_del(&link
->cgrp_link
);
411 /* @cgrp can't go away while we're holding css_set_lock */
412 if (list_empty(&cgrp
->cset_links
) && notify_on_release(cgrp
)) {
414 set_bit(CGRP_RELEASABLE
, &cgrp
->flags
);
415 check_for_release(cgrp
);
421 write_unlock(&css_set_lock
);
422 kfree_rcu(cset
, rcu_head
);
426 * refcounted get/put for css_set objects
428 static inline void get_css_set(struct css_set
*cset
)
430 atomic_inc(&cset
->refcount
);
433 static inline void put_css_set(struct css_set
*cset
)
435 __put_css_set(cset
, 0);
438 static inline void put_css_set_taskexit(struct css_set
*cset
)
440 __put_css_set(cset
, 1);
444 * compare_css_sets - helper function for find_existing_css_set().
445 * @cset: candidate css_set being tested
446 * @old_cset: existing css_set for a task
447 * @new_cgrp: cgroup that's being entered by the task
448 * @template: desired set of css pointers in css_set (pre-calculated)
450 * Returns true if "cset" matches "old_cset" except for the hierarchy
451 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
453 static bool compare_css_sets(struct css_set
*cset
,
454 struct css_set
*old_cset
,
455 struct cgroup
*new_cgrp
,
456 struct cgroup_subsys_state
*template[])
458 struct list_head
*l1
, *l2
;
460 if (memcmp(template, cset
->subsys
, sizeof(cset
->subsys
))) {
461 /* Not all subsystems matched */
466 * Compare cgroup pointers in order to distinguish between
467 * different cgroups in heirarchies with no subsystems. We
468 * could get by with just this check alone (and skip the
469 * memcmp above) but on most setups the memcmp check will
470 * avoid the need for this more expensive check on almost all
474 l1
= &cset
->cgrp_links
;
475 l2
= &old_cset
->cgrp_links
;
477 struct cgrp_cset_link
*link1
, *link2
;
478 struct cgroup
*cgrp1
, *cgrp2
;
482 /* See if we reached the end - both lists are equal length. */
483 if (l1
== &cset
->cgrp_links
) {
484 BUG_ON(l2
!= &old_cset
->cgrp_links
);
487 BUG_ON(l2
== &old_cset
->cgrp_links
);
489 /* Locate the cgroups associated with these links. */
490 link1
= list_entry(l1
, struct cgrp_cset_link
, cgrp_link
);
491 link2
= list_entry(l2
, struct cgrp_cset_link
, cgrp_link
);
494 /* Hierarchies should be linked in the same order. */
495 BUG_ON(cgrp1
->root
!= cgrp2
->root
);
498 * If this hierarchy is the hierarchy of the cgroup
499 * that's changing, then we need to check that this
500 * css_set points to the new cgroup; if it's any other
501 * hierarchy, then this css_set should point to the
502 * same cgroup as the old css_set.
504 if (cgrp1
->root
== new_cgrp
->root
) {
505 if (cgrp1
!= new_cgrp
)
516 * find_existing_css_set - init css array and find the matching css_set
517 * @old_cset: the css_set that we're using before the cgroup transition
518 * @cgrp: the cgroup that we're moving into
519 * @template: out param for the new set of csses, should be clear on entry
521 static struct css_set
*find_existing_css_set(struct css_set
*old_cset
,
523 struct cgroup_subsys_state
*template[])
525 struct cgroupfs_root
*root
= cgrp
->root
;
526 struct cgroup_subsys
*ss
;
527 struct css_set
*cset
;
532 * Build the set of subsystem state objects that we want to see in the
533 * new css_set. while subsystems can change globally, the entries here
534 * won't change, so no need for locking.
536 for_each_subsys(ss
, i
) {
537 if (root
->subsys_mask
& (1UL << i
)) {
538 /* Subsystem is in this hierarchy. So we want
539 * the subsystem state from the new
541 template[i
] = cgroup_css(cgrp
, ss
);
543 /* Subsystem is not in this hierarchy, so we
544 * don't want to change the subsystem state */
545 template[i
] = old_cset
->subsys
[i
];
549 key
= css_set_hash(template);
550 hash_for_each_possible(css_set_table
, cset
, hlist
, key
) {
551 if (!compare_css_sets(cset
, old_cset
, cgrp
, template))
554 /* This css_set matches what we need */
558 /* No existing cgroup group matched */
562 static void free_cgrp_cset_links(struct list_head
*links_to_free
)
564 struct cgrp_cset_link
*link
, *tmp_link
;
566 list_for_each_entry_safe(link
, tmp_link
, links_to_free
, cset_link
) {
567 list_del(&link
->cset_link
);
573 * allocate_cgrp_cset_links - allocate cgrp_cset_links
574 * @count: the number of links to allocate
575 * @tmp_links: list_head the allocated links are put on
577 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
578 * through ->cset_link. Returns 0 on success or -errno.
580 static int allocate_cgrp_cset_links(int count
, struct list_head
*tmp_links
)
582 struct cgrp_cset_link
*link
;
585 INIT_LIST_HEAD(tmp_links
);
587 for (i
= 0; i
< count
; i
++) {
588 link
= kzalloc(sizeof(*link
), GFP_KERNEL
);
590 free_cgrp_cset_links(tmp_links
);
593 list_add(&link
->cset_link
, tmp_links
);
599 * link_css_set - a helper function to link a css_set to a cgroup
600 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
601 * @cset: the css_set to be linked
602 * @cgrp: the destination cgroup
604 static void link_css_set(struct list_head
*tmp_links
, struct css_set
*cset
,
607 struct cgrp_cset_link
*link
;
609 BUG_ON(list_empty(tmp_links
));
610 link
= list_first_entry(tmp_links
, struct cgrp_cset_link
, cset_link
);
613 list_move(&link
->cset_link
, &cgrp
->cset_links
);
615 * Always add links to the tail of the list so that the list
616 * is sorted by order of hierarchy creation
618 list_add_tail(&link
->cgrp_link
, &cset
->cgrp_links
);
622 * find_css_set - return a new css_set with one cgroup updated
623 * @old_cset: the baseline css_set
624 * @cgrp: the cgroup to be updated
626 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
627 * substituted into the appropriate hierarchy.
629 static struct css_set
*find_css_set(struct css_set
*old_cset
,
632 struct cgroup_subsys_state
*template[CGROUP_SUBSYS_COUNT
] = { };
633 struct css_set
*cset
;
634 struct list_head tmp_links
;
635 struct cgrp_cset_link
*link
;
638 lockdep_assert_held(&cgroup_mutex
);
640 /* First see if we already have a cgroup group that matches
642 read_lock(&css_set_lock
);
643 cset
= find_existing_css_set(old_cset
, cgrp
, template);
646 read_unlock(&css_set_lock
);
651 cset
= kzalloc(sizeof(*cset
), GFP_KERNEL
);
655 /* Allocate all the cgrp_cset_link objects that we'll need */
656 if (allocate_cgrp_cset_links(cgroup_root_count
, &tmp_links
) < 0) {
661 atomic_set(&cset
->refcount
, 1);
662 INIT_LIST_HEAD(&cset
->cgrp_links
);
663 INIT_LIST_HEAD(&cset
->tasks
);
664 INIT_HLIST_NODE(&cset
->hlist
);
666 /* Copy the set of subsystem state objects generated in
667 * find_existing_css_set() */
668 memcpy(cset
->subsys
, template, sizeof(cset
->subsys
));
670 write_lock(&css_set_lock
);
671 /* Add reference counts and links from the new css_set. */
672 list_for_each_entry(link
, &old_cset
->cgrp_links
, cgrp_link
) {
673 struct cgroup
*c
= link
->cgrp
;
675 if (c
->root
== cgrp
->root
)
677 link_css_set(&tmp_links
, cset
, c
);
680 BUG_ON(!list_empty(&tmp_links
));
684 /* Add this cgroup group to the hash table */
685 key
= css_set_hash(cset
->subsys
);
686 hash_add(css_set_table
, &cset
->hlist
, key
);
688 write_unlock(&css_set_lock
);
693 static void cgroup_get_root(struct cgroupfs_root
*root
)
695 atomic_inc(&root
->sb
->s_active
);
698 static void cgroup_put_root(struct cgroupfs_root
*root
)
700 deactivate_super(root
->sb
);
704 * Return the cgroup for "task" from the given hierarchy. Must be
705 * called with cgroup_mutex held.
707 static struct cgroup
*task_cgroup_from_root(struct task_struct
*task
,
708 struct cgroupfs_root
*root
)
710 struct css_set
*cset
;
711 struct cgroup
*res
= NULL
;
713 BUG_ON(!mutex_is_locked(&cgroup_mutex
));
714 read_lock(&css_set_lock
);
716 * No need to lock the task - since we hold cgroup_mutex the
717 * task can't change groups, so the only thing that can happen
718 * is that it exits and its css is set back to init_css_set.
720 cset
= task_css_set(task
);
721 if (cset
== &init_css_set
) {
722 res
= &root
->top_cgroup
;
724 struct cgrp_cset_link
*link
;
726 list_for_each_entry(link
, &cset
->cgrp_links
, cgrp_link
) {
727 struct cgroup
*c
= link
->cgrp
;
729 if (c
->root
== root
) {
735 read_unlock(&css_set_lock
);
741 * There is one global cgroup mutex. We also require taking
742 * task_lock() when dereferencing a task's cgroup subsys pointers.
743 * See "The task_lock() exception", at the end of this comment.
745 * A task must hold cgroup_mutex to modify cgroups.
747 * Any task can increment and decrement the count field without lock.
748 * So in general, code holding cgroup_mutex can't rely on the count
749 * field not changing. However, if the count goes to zero, then only
750 * cgroup_attach_task() can increment it again. Because a count of zero
751 * means that no tasks are currently attached, therefore there is no
752 * way a task attached to that cgroup can fork (the other way to
753 * increment the count). So code holding cgroup_mutex can safely
754 * assume that if the count is zero, it will stay zero. Similarly, if
755 * a task holds cgroup_mutex on a cgroup with zero count, it
756 * knows that the cgroup won't be removed, as cgroup_rmdir()
759 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
760 * (usually) take cgroup_mutex. These are the two most performance
761 * critical pieces of code here. The exception occurs on cgroup_exit(),
762 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
763 * is taken, and if the cgroup count is zero, a usermode call made
764 * to the release agent with the name of the cgroup (path relative to
765 * the root of cgroup file system) as the argument.
767 * A cgroup can only be deleted if both its 'count' of using tasks
768 * is zero, and its list of 'children' cgroups is empty. Since all
769 * tasks in the system use _some_ cgroup, and since there is always at
770 * least one task in the system (init, pid == 1), therefore, top_cgroup
771 * always has either children cgroups and/or using tasks. So we don't
772 * need a special hack to ensure that top_cgroup cannot be deleted.
774 * The task_lock() exception
776 * The need for this exception arises from the action of
777 * cgroup_attach_task(), which overwrites one task's cgroup pointer with
778 * another. It does so using cgroup_mutex, however there are
779 * several performance critical places that need to reference
780 * task->cgroup without the expense of grabbing a system global
781 * mutex. Therefore except as noted below, when dereferencing or, as
782 * in cgroup_attach_task(), modifying a task's cgroup pointer we use
783 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
784 * the task_struct routinely used for such matters.
786 * P.S. One more locking exception. RCU is used to guard the
787 * update of a tasks cgroup pointer by cgroup_attach_task()
791 * A couple of forward declarations required, due to cyclic reference loop:
792 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
793 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
797 static int cgroup_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
);
798 static int cgroup_rmdir(struct inode
*unused_dir
, struct dentry
*dentry
);
799 static int cgroup_populate_dir(struct cgroup
*cgrp
, unsigned long subsys_mask
);
800 static const struct inode_operations cgroup_dir_inode_operations
;
801 static const struct file_operations proc_cgroupstats_operations
;
803 static struct backing_dev_info cgroup_backing_dev_info
= {
805 .capabilities
= BDI_CAP_NO_ACCT_AND_WRITEBACK
,
808 static struct inode
*cgroup_new_inode(umode_t mode
, struct super_block
*sb
)
810 struct inode
*inode
= new_inode(sb
);
814 /* ino 0 is reserved for dummy_root */
815 inode
->i_ino
= get_next_ino();
816 } while (!inode
->i_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(const char *name_str
)
828 struct cgroup_name
*name
;
830 name
= kmalloc(sizeof(*name
) + strlen(name_str
) + 1, GFP_KERNEL
);
833 strcpy(name
->name
, name_str
);
837 static char *cgroup_file_name(struct cgroup
*cgrp
, const struct cftype
*cft
,
840 if (cft
->ss
&& !(cft
->flags
& CFTYPE_NO_PREFIX
) &&
841 !(cgrp
->root
->flags
& CGRP_ROOT_NOPREFIX
))
842 snprintf(buf
, CGROUP_FILE_NAME_MAX
, "%s.%s",
843 cft
->ss
->name
, cft
->name
);
845 strncpy(buf
, cft
->name
, CGROUP_FILE_NAME_MAX
);
849 static void cgroup_free_fn(struct work_struct
*work
)
851 struct cgroup
*cgrp
= container_of(work
, struct cgroup
, destroy_work
);
853 mutex_lock(&cgroup_mutex
);
854 cgrp
->root
->number_of_cgroups
--;
855 mutex_unlock(&cgroup_mutex
);
858 * We get a ref to the parent, and put the ref when this cgroup is
859 * being freed, so it's guaranteed that the parent won't be
860 * destroyed before its children.
862 cgroup_put(cgrp
->parent
);
864 /* put the root reference that we took when we created the cgroup */
865 cgroup_put_root(cgrp
->root
);
867 cgroup_pidlist_destroy_all(cgrp
);
869 simple_xattrs_free(&cgrp
->xattrs
);
871 kfree(rcu_dereference_raw(cgrp
->name
));
875 static void cgroup_free_rcu(struct rcu_head
*head
)
877 struct cgroup
*cgrp
= container_of(head
, struct cgroup
, rcu_head
);
879 INIT_WORK(&cgrp
->destroy_work
, cgroup_free_fn
);
880 queue_work(cgroup_destroy_wq
, &cgrp
->destroy_work
);
883 static void cgroup_get(struct cgroup
*cgrp
)
888 static void cgroup_diput(struct dentry
*dentry
, struct inode
*inode
)
890 /* is dentry a directory ? if so, kfree() associated cgroup */
891 if (S_ISDIR(inode
->i_mode
)) {
892 struct cgroup
*cgrp
= dentry
->d_fsdata
;
894 BUG_ON(!(cgroup_is_dead(cgrp
)));
897 * XXX: cgrp->id is only used to look up css's. As cgroup
898 * and css's lifetimes will be decoupled, it should be made
899 * per-subsystem and moved to css->id so that lookups are
900 * successful until the target css is released.
902 mutex_lock(&cgroup_mutex
);
903 idr_remove(&cgrp
->root
->cgroup_idr
, cgrp
->id
);
904 mutex_unlock(&cgroup_mutex
);
907 call_rcu(&cgrp
->rcu_head
, cgroup_free_rcu
);
909 struct cfent
*cfe
= __d_cfe(dentry
);
910 struct cgroup
*cgrp
= dentry
->d_parent
->d_fsdata
;
912 WARN_ONCE(!list_empty(&cfe
->node
) &&
913 cgrp
!= &cgrp
->root
->top_cgroup
,
914 "cfe still linked for %s\n", cfe
->type
->name
);
915 simple_xattrs_free(&cfe
->xattrs
);
921 static void cgroup_put(struct cgroup
*cgrp
)
926 static void remove_dir(struct dentry
*d
)
928 struct dentry
*parent
= dget(d
->d_parent
);
931 simple_rmdir(parent
->d_inode
, d
);
935 static void cgroup_rm_file(struct cgroup
*cgrp
, const struct cftype
*cft
)
939 lockdep_assert_held(&cgrp
->dentry
->d_inode
->i_mutex
);
940 lockdep_assert_held(&cgroup_tree_mutex
);
943 * If we're doing cleanup due to failure of cgroup_create(),
944 * the corresponding @cfe may not exist.
946 list_for_each_entry(cfe
, &cgrp
->files
, node
) {
947 struct dentry
*d
= cfe
->dentry
;
949 if (cft
&& cfe
->type
!= cft
)
954 simple_unlink(cgrp
->dentry
->d_inode
, d
);
955 list_del_init(&cfe
->node
);
963 * cgroup_clear_dir - remove subsys files in a cgroup directory
964 * @cgrp: target cgroup
965 * @subsys_mask: mask of the subsystem ids whose files should be removed
967 static void cgroup_clear_dir(struct cgroup
*cgrp
, unsigned long subsys_mask
)
969 struct cgroup_subsys
*ss
;
972 for_each_subsys(ss
, i
) {
973 struct cftype_set
*set
;
975 if (!test_bit(i
, &subsys_mask
))
977 list_for_each_entry(set
, &ss
->cftsets
, node
)
978 cgroup_addrm_files(cgrp
, set
->cfts
, false);
983 * NOTE : the dentry must have been dget()'ed
985 static void cgroup_d_remove_dir(struct dentry
*dentry
)
987 struct dentry
*parent
;
989 parent
= dentry
->d_parent
;
990 spin_lock(&parent
->d_lock
);
991 spin_lock_nested(&dentry
->d_lock
, DENTRY_D_LOCK_NESTED
);
992 list_del_init(&dentry
->d_u
.d_child
);
993 spin_unlock(&dentry
->d_lock
);
994 spin_unlock(&parent
->d_lock
);
998 static int rebind_subsystems(struct cgroupfs_root
*root
,
999 unsigned long added_mask
, unsigned removed_mask
)
1001 struct cgroup
*cgrp
= &root
->top_cgroup
;
1002 struct cgroup_subsys
*ss
;
1005 lockdep_assert_held(&cgroup_tree_mutex
);
1006 lockdep_assert_held(&cgroup_mutex
);
1008 /* Check that any added subsystems are currently free */
1009 for_each_subsys(ss
, i
)
1010 if ((added_mask
& (1 << i
)) && ss
->root
!= &cgroup_dummy_root
)
1013 ret
= cgroup_populate_dir(cgrp
, added_mask
);
1018 * Nothing can fail from this point on. Remove files for the
1019 * removed subsystems and rebind each subsystem.
1021 mutex_unlock(&cgroup_mutex
);
1022 cgroup_clear_dir(cgrp
, removed_mask
);
1023 mutex_lock(&cgroup_mutex
);
1025 for_each_subsys(ss
, i
) {
1026 unsigned long bit
= 1UL << i
;
1028 if (bit
& added_mask
) {
1029 /* We're binding this subsystem to this hierarchy */
1030 BUG_ON(cgroup_css(cgrp
, ss
));
1031 BUG_ON(!cgroup_css(cgroup_dummy_top
, ss
));
1032 BUG_ON(cgroup_css(cgroup_dummy_top
, ss
)->cgroup
!= cgroup_dummy_top
);
1034 rcu_assign_pointer(cgrp
->subsys
[i
],
1035 cgroup_css(cgroup_dummy_top
, ss
));
1036 cgroup_css(cgrp
, ss
)->cgroup
= cgrp
;
1040 ss
->bind(cgroup_css(cgrp
, ss
));
1042 /* refcount was already taken, and we're keeping it */
1043 root
->subsys_mask
|= bit
;
1044 } else if (bit
& removed_mask
) {
1045 /* We're removing this subsystem */
1046 BUG_ON(cgroup_css(cgrp
, ss
) != cgroup_css(cgroup_dummy_top
, ss
));
1047 BUG_ON(cgroup_css(cgrp
, ss
)->cgroup
!= cgrp
);
1050 ss
->bind(cgroup_css(cgroup_dummy_top
, ss
));
1052 cgroup_css(cgroup_dummy_top
, ss
)->cgroup
= cgroup_dummy_top
;
1053 RCU_INIT_POINTER(cgrp
->subsys
[i
], NULL
);
1055 cgroup_subsys
[i
]->root
= &cgroup_dummy_root
;
1056 root
->subsys_mask
&= ~bit
;
1061 * Mark @root has finished binding subsystems. @root->subsys_mask
1062 * now matches the bound subsystems.
1064 root
->flags
|= CGRP_ROOT_SUBSYS_BOUND
;
1069 static int cgroup_show_options(struct seq_file
*seq
, struct dentry
*dentry
)
1071 struct cgroupfs_root
*root
= dentry
->d_sb
->s_fs_info
;
1072 struct cgroup_subsys
*ss
;
1075 for_each_subsys(ss
, ssid
)
1076 if (root
->subsys_mask
& (1 << ssid
))
1077 seq_printf(seq
, ",%s", ss
->name
);
1078 if (root
->flags
& CGRP_ROOT_SANE_BEHAVIOR
)
1079 seq_puts(seq
, ",sane_behavior");
1080 if (root
->flags
& CGRP_ROOT_NOPREFIX
)
1081 seq_puts(seq
, ",noprefix");
1082 if (root
->flags
& CGRP_ROOT_XATTR
)
1083 seq_puts(seq
, ",xattr");
1085 spin_lock(&release_agent_path_lock
);
1086 if (strlen(root
->release_agent_path
))
1087 seq_printf(seq
, ",release_agent=%s", root
->release_agent_path
);
1088 spin_unlock(&release_agent_path_lock
);
1090 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN
, &root
->top_cgroup
.flags
))
1091 seq_puts(seq
, ",clone_children");
1092 if (strlen(root
->name
))
1093 seq_printf(seq
, ",name=%s", root
->name
);
1097 struct cgroup_sb_opts
{
1098 unsigned long subsys_mask
;
1099 unsigned long flags
;
1100 char *release_agent
;
1101 bool cpuset_clone_children
;
1103 /* User explicitly requested empty subsystem */
1106 struct cgroupfs_root
*new_root
;
1111 * Convert a hierarchy specifier into a bitmask of subsystems and
1112 * flags. Call with cgroup_mutex held to protect the cgroup_subsys[]
1113 * array. This function takes refcounts on subsystems to be used, unless it
1114 * returns error, in which case no refcounts are taken.
1116 static int parse_cgroupfs_options(char *data
, struct cgroup_sb_opts
*opts
)
1118 char *token
, *o
= data
;
1119 bool all_ss
= false, one_ss
= false;
1120 unsigned long mask
= (unsigned long)-1;
1121 struct cgroup_subsys
*ss
;
1124 BUG_ON(!mutex_is_locked(&cgroup_mutex
));
1126 #ifdef CONFIG_CPUSETS
1127 mask
= ~(1UL << cpuset_cgrp_id
);
1130 memset(opts
, 0, sizeof(*opts
));
1132 while ((token
= strsep(&o
, ",")) != NULL
) {
1135 if (!strcmp(token
, "none")) {
1136 /* Explicitly have no subsystems */
1140 if (!strcmp(token
, "all")) {
1141 /* Mutually exclusive option 'all' + subsystem name */
1147 if (!strcmp(token
, "__DEVEL__sane_behavior")) {
1148 opts
->flags
|= CGRP_ROOT_SANE_BEHAVIOR
;
1151 if (!strcmp(token
, "noprefix")) {
1152 opts
->flags
|= CGRP_ROOT_NOPREFIX
;
1155 if (!strcmp(token
, "clone_children")) {
1156 opts
->cpuset_clone_children
= true;
1159 if (!strcmp(token
, "xattr")) {
1160 opts
->flags
|= CGRP_ROOT_XATTR
;
1163 if (!strncmp(token
, "release_agent=", 14)) {
1164 /* Specifying two release agents is forbidden */
1165 if (opts
->release_agent
)
1167 opts
->release_agent
=
1168 kstrndup(token
+ 14, PATH_MAX
- 1, GFP_KERNEL
);
1169 if (!opts
->release_agent
)
1173 if (!strncmp(token
, "name=", 5)) {
1174 const char *name
= token
+ 5;
1175 /* Can't specify an empty name */
1178 /* Must match [\w.-]+ */
1179 for (i
= 0; i
< strlen(name
); i
++) {
1183 if ((c
== '.') || (c
== '-') || (c
== '_'))
1187 /* Specifying two names is forbidden */
1190 opts
->name
= kstrndup(name
,
1191 MAX_CGROUP_ROOT_NAMELEN
- 1,
1199 for_each_subsys(ss
, i
) {
1200 if (strcmp(token
, ss
->name
))
1205 /* Mutually exclusive option 'all' + subsystem name */
1208 set_bit(i
, &opts
->subsys_mask
);
1213 if (i
== CGROUP_SUBSYS_COUNT
)
1218 * If the 'all' option was specified select all the subsystems,
1219 * otherwise if 'none', 'name=' and a subsystem name options
1220 * were not specified, let's default to 'all'
1222 if (all_ss
|| (!one_ss
&& !opts
->none
&& !opts
->name
))
1223 for_each_subsys(ss
, i
)
1225 set_bit(i
, &opts
->subsys_mask
);
1227 /* Consistency checks */
1229 if (opts
->flags
& CGRP_ROOT_SANE_BEHAVIOR
) {
1230 pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
1232 if (opts
->flags
& CGRP_ROOT_NOPREFIX
) {
1233 pr_err("cgroup: sane_behavior: noprefix is not allowed\n");
1237 if (opts
->cpuset_clone_children
) {
1238 pr_err("cgroup: sane_behavior: clone_children is not allowed\n");
1244 * Option noprefix was introduced just for backward compatibility
1245 * with the old cpuset, so we allow noprefix only if mounting just
1246 * the cpuset subsystem.
1248 if ((opts
->flags
& CGRP_ROOT_NOPREFIX
) && (opts
->subsys_mask
& mask
))
1252 /* Can't specify "none" and some subsystems */
1253 if (opts
->subsys_mask
&& opts
->none
)
1257 * We either have to specify by name or by subsystems. (So all
1258 * empty hierarchies must have a name).
1260 if (!opts
->subsys_mask
&& !opts
->name
)
1266 static int cgroup_remount(struct super_block
*sb
, int *flags
, char *data
)
1269 struct cgroupfs_root
*root
= sb
->s_fs_info
;
1270 struct cgroup
*cgrp
= &root
->top_cgroup
;
1271 struct cgroup_sb_opts opts
;
1272 unsigned long added_mask
, removed_mask
;
1274 if (root
->flags
& CGRP_ROOT_SANE_BEHAVIOR
) {
1275 pr_err("cgroup: sane_behavior: remount is not allowed\n");
1279 mutex_lock(&cgrp
->dentry
->d_inode
->i_mutex
);
1280 mutex_lock(&cgroup_tree_mutex
);
1281 mutex_lock(&cgroup_mutex
);
1283 /* See what subsystems are wanted */
1284 ret
= parse_cgroupfs_options(data
, &opts
);
1288 if (opts
.subsys_mask
!= root
->subsys_mask
|| opts
.release_agent
)
1289 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1290 task_tgid_nr(current
), current
->comm
);
1292 added_mask
= opts
.subsys_mask
& ~root
->subsys_mask
;
1293 removed_mask
= root
->subsys_mask
& ~opts
.subsys_mask
;
1295 /* Don't allow flags or name to change at remount */
1296 if (((opts
.flags
^ root
->flags
) & CGRP_ROOT_OPTION_MASK
) ||
1297 (opts
.name
&& strcmp(opts
.name
, root
->name
))) {
1298 pr_err("cgroup: option or name mismatch, new: 0x%lx \"%s\", old: 0x%lx \"%s\"\n",
1299 opts
.flags
& CGRP_ROOT_OPTION_MASK
, opts
.name
?: "",
1300 root
->flags
& CGRP_ROOT_OPTION_MASK
, root
->name
);
1305 /* remounting is not allowed for populated hierarchies */
1306 if (root
->number_of_cgroups
> 1) {
1311 ret
= rebind_subsystems(root
, added_mask
, removed_mask
);
1315 if (opts
.release_agent
) {
1316 spin_lock(&release_agent_path_lock
);
1317 strcpy(root
->release_agent_path
, opts
.release_agent
);
1318 spin_unlock(&release_agent_path_lock
);
1321 kfree(opts
.release_agent
);
1323 mutex_unlock(&cgroup_mutex
);
1324 mutex_unlock(&cgroup_tree_mutex
);
1325 mutex_unlock(&cgrp
->dentry
->d_inode
->i_mutex
);
1329 static const struct super_operations cgroup_ops
= {
1330 .statfs
= simple_statfs
,
1331 .drop_inode
= generic_delete_inode
,
1332 .show_options
= cgroup_show_options
,
1333 .remount_fs
= cgroup_remount
,
1336 static void init_cgroup_housekeeping(struct cgroup
*cgrp
)
1338 INIT_LIST_HEAD(&cgrp
->sibling
);
1339 INIT_LIST_HEAD(&cgrp
->children
);
1340 INIT_LIST_HEAD(&cgrp
->files
);
1341 INIT_LIST_HEAD(&cgrp
->cset_links
);
1342 INIT_LIST_HEAD(&cgrp
->release_list
);
1343 INIT_LIST_HEAD(&cgrp
->pidlists
);
1344 mutex_init(&cgrp
->pidlist_mutex
);
1345 cgrp
->dummy_css
.cgroup
= cgrp
;
1346 simple_xattrs_init(&cgrp
->xattrs
);
1349 static void init_cgroup_root(struct cgroupfs_root
*root
)
1351 struct cgroup
*cgrp
= &root
->top_cgroup
;
1353 INIT_LIST_HEAD(&root
->root_list
);
1354 root
->number_of_cgroups
= 1;
1356 RCU_INIT_POINTER(cgrp
->name
, &root_cgroup_name
);
1357 init_cgroup_housekeeping(cgrp
);
1358 idr_init(&root
->cgroup_idr
);
1361 static int cgroup_init_root_id(struct cgroupfs_root
*root
, int start
, int end
)
1365 lockdep_assert_held(&cgroup_mutex
);
1367 id
= idr_alloc_cyclic(&cgroup_hierarchy_idr
, root
, start
, end
,
1372 root
->hierarchy_id
= id
;
1376 static void cgroup_exit_root_id(struct cgroupfs_root
*root
)
1378 lockdep_assert_held(&cgroup_mutex
);
1380 if (root
->hierarchy_id
) {
1381 idr_remove(&cgroup_hierarchy_idr
, root
->hierarchy_id
);
1382 root
->hierarchy_id
= 0;
1386 static int cgroup_test_super(struct super_block
*sb
, void *data
)
1388 struct cgroup_sb_opts
*opts
= data
;
1389 struct cgroupfs_root
*root
= sb
->s_fs_info
;
1391 /* If we asked for a name then it must match */
1392 if (opts
->name
&& strcmp(opts
->name
, root
->name
))
1396 * If we asked for subsystems (or explicitly for no
1397 * subsystems) then they must match
1399 if ((opts
->subsys_mask
|| opts
->none
)
1400 && (opts
->subsys_mask
!= root
->subsys_mask
))
1406 static struct cgroupfs_root
*cgroup_root_from_opts(struct cgroup_sb_opts
*opts
)
1408 struct cgroupfs_root
*root
;
1410 if (!opts
->subsys_mask
&& !opts
->none
)
1413 root
= kzalloc(sizeof(*root
), GFP_KERNEL
);
1415 return ERR_PTR(-ENOMEM
);
1417 init_cgroup_root(root
);
1420 * We need to set @root->subsys_mask now so that @root can be
1421 * matched by cgroup_test_super() before it finishes
1422 * initialization; otherwise, competing mounts with the same
1423 * options may try to bind the same subsystems instead of waiting
1424 * for the first one leading to unexpected mount errors.
1425 * SUBSYS_BOUND will be set once actual binding is complete.
1427 root
->subsys_mask
= opts
->subsys_mask
;
1428 root
->flags
= opts
->flags
;
1429 if (opts
->release_agent
)
1430 strcpy(root
->release_agent_path
, opts
->release_agent
);
1432 strcpy(root
->name
, opts
->name
);
1433 if (opts
->cpuset_clone_children
)
1434 set_bit(CGRP_CPUSET_CLONE_CHILDREN
, &root
->top_cgroup
.flags
);
1438 static void cgroup_free_root(struct cgroupfs_root
*root
)
1441 /* hierarhcy ID shoulid already have been released */
1442 WARN_ON_ONCE(root
->hierarchy_id
);
1444 idr_destroy(&root
->cgroup_idr
);
1449 static int cgroup_set_super(struct super_block
*sb
, void *data
)
1452 struct cgroup_sb_opts
*opts
= data
;
1454 /* If we don't have a new root, we can't set up a new sb */
1455 if (!opts
->new_root
)
1458 BUG_ON(!opts
->subsys_mask
&& !opts
->none
);
1460 ret
= set_anon_super(sb
, NULL
);
1464 sb
->s_fs_info
= opts
->new_root
;
1465 opts
->new_root
->sb
= sb
;
1467 sb
->s_blocksize
= PAGE_CACHE_SIZE
;
1468 sb
->s_blocksize_bits
= PAGE_CACHE_SHIFT
;
1469 sb
->s_magic
= CGROUP_SUPER_MAGIC
;
1470 sb
->s_op
= &cgroup_ops
;
1475 static int cgroup_get_rootdir(struct super_block
*sb
)
1477 static const struct dentry_operations cgroup_dops
= {
1478 .d_iput
= cgroup_diput
,
1479 .d_delete
= always_delete_dentry
,
1482 struct inode
*inode
=
1483 cgroup_new_inode(S_IFDIR
| S_IRUGO
| S_IXUGO
| S_IWUSR
, sb
);
1488 inode
->i_fop
= &simple_dir_operations
;
1489 inode
->i_op
= &cgroup_dir_inode_operations
;
1490 /* directories start off with i_nlink == 2 (for "." entry) */
1492 sb
->s_root
= d_make_root(inode
);
1495 /* for everything else we want ->d_op set */
1496 sb
->s_d_op
= &cgroup_dops
;
1500 static int cgroup_setup_root(struct cgroupfs_root
*root
)
1502 LIST_HEAD(tmp_links
);
1503 struct super_block
*sb
= root
->sb
;
1504 struct cgroup
*root_cgrp
= &root
->top_cgroup
;
1505 struct cgroupfs_root
*existing_root
;
1506 struct css_set
*cset
;
1507 struct inode
*inode
;
1508 const struct cred
*cred
;
1511 lockdep_assert_held(&cgroup_tree_mutex
);
1512 lockdep_assert_held(&cgroup_mutex
);
1513 BUG_ON(sb
->s_root
!= NULL
);
1515 mutex_unlock(&cgroup_mutex
);
1516 mutex_unlock(&cgroup_tree_mutex
);
1518 ret
= cgroup_get_rootdir(sb
);
1520 mutex_lock(&cgroup_tree_mutex
);
1521 mutex_lock(&cgroup_mutex
);
1524 inode
= sb
->s_root
->d_inode
;
1526 mutex_lock(&inode
->i_mutex
);
1527 mutex_lock(&cgroup_tree_mutex
);
1528 mutex_lock(&cgroup_mutex
);
1530 ret
= idr_alloc(&root
->cgroup_idr
, root_cgrp
, 0, 1, GFP_KERNEL
);
1533 root_cgrp
->id
= ret
;
1535 /* check for name clashes with existing mounts */
1537 if (strlen(root
->name
))
1538 for_each_active_root(existing_root
)
1539 if (!strcmp(existing_root
->name
, root
->name
))
1543 * We're accessing css_set_count without locking css_set_lock here,
1544 * but that's OK - it can only be increased by someone holding
1545 * cgroup_lock, and that's us. The worst that can happen is that we
1546 * have some link structures left over
1548 ret
= allocate_cgrp_cset_links(css_set_count
, &tmp_links
);
1552 /* ID 0 is reserved for dummy root, 1 for unified hierarchy */
1553 ret
= cgroup_init_root_id(root
, 2, 0);
1557 sb
->s_root
->d_fsdata
= root_cgrp
;
1558 root_cgrp
->dentry
= sb
->s_root
;
1561 * We're inside get_sb() and will call lookup_one_len() to create
1562 * the root files, which doesn't work if SELinux is in use. The
1563 * following cred dancing somehow works around it. See 2ce9738ba
1564 * ("cgroupfs: use init_cred when populating new cgroupfs mount")
1567 cred
= override_creds(&init_cred
);
1569 ret
= cgroup_addrm_files(root_cgrp
, cgroup_base_files
, true);
1573 ret
= rebind_subsystems(root
, root
->subsys_mask
, 0);
1580 * There must be no failure case after here, since rebinding takes
1581 * care of subsystems' refcounts, which are explicitly dropped in
1582 * the failure exit path.
1584 list_add(&root
->root_list
, &cgroup_roots
);
1585 cgroup_root_count
++;
1588 * Link the top cgroup in this hierarchy into all the css_set
1591 write_lock(&css_set_lock
);
1592 hash_for_each(css_set_table
, i
, cset
, hlist
)
1593 link_css_set(&tmp_links
, cset
, root_cgrp
);
1594 write_unlock(&css_set_lock
);
1596 BUG_ON(!list_empty(&root_cgrp
->children
));
1597 BUG_ON(root
->number_of_cgroups
!= 1);
1603 cgroup_addrm_files(&root
->top_cgroup
, cgroup_base_files
, false);
1605 cgroup_exit_root_id(root
);
1607 mutex_unlock(&inode
->i_mutex
);
1608 free_cgrp_cset_links(&tmp_links
);
1612 static struct dentry
*cgroup_mount(struct file_system_type
*fs_type
,
1613 int flags
, const char *unused_dev_name
,
1616 struct super_block
*sb
= NULL
;
1617 struct cgroupfs_root
*root
= NULL
;
1618 struct cgroup_sb_opts opts
;
1619 struct cgroupfs_root
*new_root
;
1622 mutex_lock(&cgroup_tree_mutex
);
1623 mutex_lock(&cgroup_mutex
);
1625 /* First find the desired set of subsystems */
1626 ret
= parse_cgroupfs_options(data
, &opts
);
1631 * Allocate a new cgroup root. We may not need it if we're
1632 * reusing an existing hierarchy.
1634 new_root
= cgroup_root_from_opts(&opts
);
1635 if (IS_ERR(new_root
)) {
1636 ret
= PTR_ERR(new_root
);
1639 opts
.new_root
= new_root
;
1641 /* Locate an existing or new sb for this hierarchy */
1642 mutex_unlock(&cgroup_mutex
);
1643 mutex_unlock(&cgroup_tree_mutex
);
1644 sb
= sget(fs_type
, cgroup_test_super
, cgroup_set_super
, 0, &opts
);
1645 mutex_lock(&cgroup_tree_mutex
);
1646 mutex_lock(&cgroup_mutex
);
1649 cgroup_free_root(opts
.new_root
);
1653 root
= sb
->s_fs_info
;
1655 if (root
== opts
.new_root
) {
1656 ret
= cgroup_setup_root(root
);
1661 * We re-used an existing hierarchy - the new root (if
1662 * any) is not needed
1664 cgroup_free_root(opts
.new_root
);
1666 if ((root
->flags
^ opts
.flags
) & CGRP_ROOT_OPTION_MASK
) {
1667 if ((root
->flags
| opts
.flags
) & CGRP_ROOT_SANE_BEHAVIOR
) {
1668 pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
1672 pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n");
1679 mutex_unlock(&cgroup_mutex
);
1680 mutex_unlock(&cgroup_tree_mutex
);
1682 if (ret
&& !IS_ERR_OR_NULL(sb
))
1683 deactivate_locked_super(sb
);
1685 kfree(opts
.release_agent
);
1689 return dget(sb
->s_root
);
1691 return ERR_PTR(ret
);
1694 static void cgroup_kill_sb(struct super_block
*sb
)
1696 struct cgroupfs_root
*root
= sb
->s_fs_info
;
1697 struct cgroup
*cgrp
= &root
->top_cgroup
;
1698 struct cgrp_cset_link
*link
, *tmp_link
;
1703 BUG_ON(root
->number_of_cgroups
!= 1);
1704 BUG_ON(!list_empty(&cgrp
->children
));
1706 mutex_lock(&cgrp
->dentry
->d_inode
->i_mutex
);
1707 mutex_lock(&cgroup_tree_mutex
);
1708 mutex_lock(&cgroup_mutex
);
1710 /* Rebind all subsystems back to the default hierarchy */
1711 if (root
->flags
& CGRP_ROOT_SUBSYS_BOUND
) {
1712 ret
= rebind_subsystems(root
, 0, root
->subsys_mask
);
1713 /* Shouldn't be able to fail ... */
1718 * Release all the links from cset_links to this hierarchy's
1721 write_lock(&css_set_lock
);
1723 list_for_each_entry_safe(link
, tmp_link
, &cgrp
->cset_links
, cset_link
) {
1724 list_del(&link
->cset_link
);
1725 list_del(&link
->cgrp_link
);
1728 write_unlock(&css_set_lock
);
1730 if (!list_empty(&root
->root_list
)) {
1731 list_del(&root
->root_list
);
1732 cgroup_root_count
--;
1735 cgroup_exit_root_id(root
);
1737 mutex_unlock(&cgroup_mutex
);
1738 mutex_unlock(&cgroup_tree_mutex
);
1739 mutex_unlock(&cgrp
->dentry
->d_inode
->i_mutex
);
1741 simple_xattrs_free(&cgrp
->xattrs
);
1743 kill_litter_super(sb
);
1744 cgroup_free_root(root
);
1747 static struct file_system_type cgroup_fs_type
= {
1749 .mount
= cgroup_mount
,
1750 .kill_sb
= cgroup_kill_sb
,
1753 static struct kobject
*cgroup_kobj
;
1756 * cgroup_path - generate the path of a cgroup
1757 * @cgrp: the cgroup in question
1758 * @buf: the buffer to write the path into
1759 * @buflen: the length of the buffer
1761 * Writes path of cgroup into buf. Returns 0 on success, -errno on error.
1763 * We can't generate cgroup path using dentry->d_name, as accessing
1764 * dentry->name must be protected by irq-unsafe dentry->d_lock or parent
1765 * inode's i_mutex, while on the other hand cgroup_path() can be called
1766 * with some irq-safe spinlocks held.
1768 int cgroup_path(const struct cgroup
*cgrp
, char *buf
, int buflen
)
1770 int ret
= -ENAMETOOLONG
;
1773 if (!cgrp
->parent
) {
1774 if (strlcpy(buf
, "/", buflen
) >= buflen
)
1775 return -ENAMETOOLONG
;
1779 start
= buf
+ buflen
- 1;
1784 const char *name
= cgroup_name(cgrp
);
1788 if ((start
-= len
) < buf
)
1790 memcpy(start
, name
, len
);
1796 cgrp
= cgrp
->parent
;
1797 } while (cgrp
->parent
);
1799 memmove(buf
, start
, buf
+ buflen
- start
);
1804 EXPORT_SYMBOL_GPL(cgroup_path
);
1807 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
1808 * @task: target task
1809 * @buf: the buffer to write the path into
1810 * @buflen: the length of the buffer
1812 * Determine @task's cgroup on the first (the one with the lowest non-zero
1813 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
1814 * function grabs cgroup_mutex and shouldn't be used inside locks used by
1815 * cgroup controller callbacks.
1817 * Returns 0 on success, fails with -%ENAMETOOLONG if @buflen is too short.
1819 int task_cgroup_path(struct task_struct
*task
, char *buf
, size_t buflen
)
1821 struct cgroupfs_root
*root
;
1822 struct cgroup
*cgrp
;
1823 int hierarchy_id
= 1, ret
= 0;
1826 return -ENAMETOOLONG
;
1828 mutex_lock(&cgroup_mutex
);
1830 root
= idr_get_next(&cgroup_hierarchy_idr
, &hierarchy_id
);
1833 cgrp
= task_cgroup_from_root(task
, root
);
1834 ret
= cgroup_path(cgrp
, buf
, buflen
);
1836 /* if no hierarchy exists, everyone is in "/" */
1837 memcpy(buf
, "/", 2);
1840 mutex_unlock(&cgroup_mutex
);
1843 EXPORT_SYMBOL_GPL(task_cgroup_path
);
1846 * Control Group taskset
1848 struct task_and_cgroup
{
1849 struct task_struct
*task
;
1850 struct cgroup
*cgrp
;
1851 struct css_set
*cset
;
1854 struct cgroup_taskset
{
1855 struct task_and_cgroup single
;
1856 struct flex_array
*tc_array
;
1859 struct cgroup
*cur_cgrp
;
1863 * cgroup_taskset_first - reset taskset and return the first task
1864 * @tset: taskset of interest
1866 * @tset iteration is initialized and the first task is returned.
1868 struct task_struct
*cgroup_taskset_first(struct cgroup_taskset
*tset
)
1870 if (tset
->tc_array
) {
1872 return cgroup_taskset_next(tset
);
1874 tset
->cur_cgrp
= tset
->single
.cgrp
;
1875 return tset
->single
.task
;
1878 EXPORT_SYMBOL_GPL(cgroup_taskset_first
);
1881 * cgroup_taskset_next - iterate to the next task in taskset
1882 * @tset: taskset of interest
1884 * Return the next task in @tset. Iteration must have been initialized
1885 * with cgroup_taskset_first().
1887 struct task_struct
*cgroup_taskset_next(struct cgroup_taskset
*tset
)
1889 struct task_and_cgroup
*tc
;
1891 if (!tset
->tc_array
|| tset
->idx
>= tset
->tc_array_len
)
1894 tc
= flex_array_get(tset
->tc_array
, tset
->idx
++);
1895 tset
->cur_cgrp
= tc
->cgrp
;
1898 EXPORT_SYMBOL_GPL(cgroup_taskset_next
);
1901 * cgroup_taskset_cur_css - return the matching css for the current task
1902 * @tset: taskset of interest
1903 * @subsys_id: the ID of the target subsystem
1905 * Return the css for the current (last returned) task of @tset for
1906 * subsystem specified by @subsys_id. This function must be preceded by
1907 * either cgroup_taskset_first() or cgroup_taskset_next().
1909 struct cgroup_subsys_state
*cgroup_taskset_cur_css(struct cgroup_taskset
*tset
,
1912 return cgroup_css(tset
->cur_cgrp
, cgroup_subsys
[subsys_id
]);
1914 EXPORT_SYMBOL_GPL(cgroup_taskset_cur_css
);
1917 * cgroup_taskset_size - return the number of tasks in taskset
1918 * @tset: taskset of interest
1920 int cgroup_taskset_size(struct cgroup_taskset
*tset
)
1922 return tset
->tc_array
? tset
->tc_array_len
: 1;
1924 EXPORT_SYMBOL_GPL(cgroup_taskset_size
);
1928 * cgroup_task_migrate - move a task from one cgroup to another.
1930 * Must be called with cgroup_mutex and threadgroup locked.
1932 static void cgroup_task_migrate(struct cgroup
*old_cgrp
,
1933 struct task_struct
*tsk
,
1934 struct css_set
*new_cset
)
1936 struct css_set
*old_cset
;
1939 * We are synchronized through threadgroup_lock() against PF_EXITING
1940 * setting such that we can't race against cgroup_exit() changing the
1941 * css_set to init_css_set and dropping the old one.
1943 WARN_ON_ONCE(tsk
->flags
& PF_EXITING
);
1944 old_cset
= task_css_set(tsk
);
1947 rcu_assign_pointer(tsk
->cgroups
, new_cset
);
1950 /* Update the css_set linked lists if we're using them */
1951 write_lock(&css_set_lock
);
1952 if (!list_empty(&tsk
->cg_list
))
1953 list_move(&tsk
->cg_list
, &new_cset
->tasks
);
1954 write_unlock(&css_set_lock
);
1957 * We just gained a reference on old_cset by taking it from the
1958 * task. As trading it for new_cset is protected by cgroup_mutex,
1959 * we're safe to drop it here; it will be freed under RCU.
1961 set_bit(CGRP_RELEASABLE
, &old_cgrp
->flags
);
1962 put_css_set(old_cset
);
1966 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
1967 * @cgrp: the cgroup to attach to
1968 * @tsk: the task or the leader of the threadgroup to be attached
1969 * @threadgroup: attach the whole threadgroup?
1971 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
1972 * task_lock of @tsk or each thread in the threadgroup individually in turn.
1974 static int cgroup_attach_task(struct cgroup
*cgrp
, struct task_struct
*tsk
,
1977 int retval
, i
, group_size
;
1978 struct cgroupfs_root
*root
= cgrp
->root
;
1979 struct cgroup_subsys_state
*css
, *failed_css
= NULL
;
1980 /* threadgroup list cursor and array */
1981 struct task_struct
*leader
= tsk
;
1982 struct task_and_cgroup
*tc
;
1983 struct flex_array
*group
;
1984 struct cgroup_taskset tset
= { };
1987 * step 0: in order to do expensive, possibly blocking operations for
1988 * every thread, we cannot iterate the thread group list, since it needs
1989 * rcu or tasklist locked. instead, build an array of all threads in the
1990 * group - group_rwsem prevents new threads from appearing, and if
1991 * threads exit, this will just be an over-estimate.
1994 group_size
= get_nr_threads(tsk
);
1997 /* flex_array supports very large thread-groups better than kmalloc. */
1998 group
= flex_array_alloc(sizeof(*tc
), group_size
, GFP_KERNEL
);
2001 /* pre-allocate to guarantee space while iterating in rcu read-side. */
2002 retval
= flex_array_prealloc(group
, 0, group_size
, GFP_KERNEL
);
2004 goto out_free_group_list
;
2008 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2009 * already PF_EXITING could be freed from underneath us unless we
2010 * take an rcu_read_lock.
2014 struct task_and_cgroup ent
;
2016 /* @tsk either already exited or can't exit until the end */
2017 if (tsk
->flags
& PF_EXITING
)
2020 /* as per above, nr_threads may decrease, but not increase. */
2021 BUG_ON(i
>= group_size
);
2023 ent
.cgrp
= task_cgroup_from_root(tsk
, root
);
2024 /* nothing to do if this task is already in the cgroup */
2025 if (ent
.cgrp
== cgrp
)
2028 * saying GFP_ATOMIC has no effect here because we did prealloc
2029 * earlier, but it's good form to communicate our expectations.
2031 retval
= flex_array_put(group
, i
, &ent
, GFP_ATOMIC
);
2032 BUG_ON(retval
!= 0);
2037 } while_each_thread(leader
, tsk
);
2039 /* remember the number of threads in the array for later. */
2041 tset
.tc_array
= group
;
2042 tset
.tc_array_len
= group_size
;
2044 /* methods shouldn't be called if no task is actually migrating */
2047 goto out_free_group_list
;
2050 * step 1: check that we can legitimately attach to the cgroup.
2052 for_each_css(css
, i
, cgrp
) {
2053 if (css
->ss
->can_attach
) {
2054 retval
= css
->ss
->can_attach(css
, &tset
);
2057 goto out_cancel_attach
;
2063 * step 2: make sure css_sets exist for all threads to be migrated.
2064 * we use find_css_set, which allocates a new one if necessary.
2066 for (i
= 0; i
< group_size
; i
++) {
2067 struct css_set
*old_cset
;
2069 tc
= flex_array_get(group
, i
);
2070 old_cset
= task_css_set(tc
->task
);
2071 tc
->cset
= find_css_set(old_cset
, cgrp
);
2074 goto out_put_css_set_refs
;
2079 * step 3: now that we're guaranteed success wrt the css_sets,
2080 * proceed to move all tasks to the new cgroup. There are no
2081 * failure cases after here, so this is the commit point.
2083 for (i
= 0; i
< group_size
; i
++) {
2084 tc
= flex_array_get(group
, i
);
2085 cgroup_task_migrate(tc
->cgrp
, tc
->task
, tc
->cset
);
2087 /* nothing is sensitive to fork() after this point. */
2090 * step 4: do subsystem attach callbacks.
2092 for_each_css(css
, i
, cgrp
)
2093 if (css
->ss
->attach
)
2094 css
->ss
->attach(css
, &tset
);
2097 * step 5: success! and cleanup
2100 out_put_css_set_refs
:
2102 for (i
= 0; i
< group_size
; i
++) {
2103 tc
= flex_array_get(group
, i
);
2106 put_css_set(tc
->cset
);
2111 for_each_css(css
, i
, cgrp
) {
2112 if (css
== failed_css
)
2114 if (css
->ss
->cancel_attach
)
2115 css
->ss
->cancel_attach(css
, &tset
);
2118 out_free_group_list
:
2119 flex_array_free(group
);
2124 * Find the task_struct of the task to attach by vpid and pass it along to the
2125 * function to attach either it or all tasks in its threadgroup. Will lock
2126 * cgroup_mutex and threadgroup; may take task_lock of task.
2128 static int attach_task_by_pid(struct cgroup
*cgrp
, u64 pid
, bool threadgroup
)
2130 struct task_struct
*tsk
;
2131 const struct cred
*cred
= current_cred(), *tcred
;
2134 if (!cgroup_lock_live_group(cgrp
))
2140 tsk
= find_task_by_vpid(pid
);
2144 goto out_unlock_cgroup
;
2147 * even if we're attaching all tasks in the thread group, we
2148 * only need to check permissions on one of them.
2150 tcred
= __task_cred(tsk
);
2151 if (!uid_eq(cred
->euid
, GLOBAL_ROOT_UID
) &&
2152 !uid_eq(cred
->euid
, tcred
->uid
) &&
2153 !uid_eq(cred
->euid
, tcred
->suid
)) {
2156 goto out_unlock_cgroup
;
2162 tsk
= tsk
->group_leader
;
2165 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
2166 * trapped in a cpuset, or RT worker may be born in a cgroup
2167 * with no rt_runtime allocated. Just say no.
2169 if (tsk
== kthreadd_task
|| (tsk
->flags
& PF_NO_SETAFFINITY
)) {
2172 goto out_unlock_cgroup
;
2175 get_task_struct(tsk
);
2178 threadgroup_lock(tsk
);
2180 if (!thread_group_leader(tsk
)) {
2182 * a race with de_thread from another thread's exec()
2183 * may strip us of our leadership, if this happens,
2184 * there is no choice but to throw this task away and
2185 * try again; this is
2186 * "double-double-toil-and-trouble-check locking".
2188 threadgroup_unlock(tsk
);
2189 put_task_struct(tsk
);
2190 goto retry_find_task
;
2194 ret
= cgroup_attach_task(cgrp
, tsk
, threadgroup
);
2196 threadgroup_unlock(tsk
);
2198 put_task_struct(tsk
);
2200 mutex_unlock(&cgroup_mutex
);
2205 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2206 * @from: attach to all cgroups of a given task
2207 * @tsk: the task to be attached
2209 int cgroup_attach_task_all(struct task_struct
*from
, struct task_struct
*tsk
)
2211 struct cgroupfs_root
*root
;
2214 mutex_lock(&cgroup_mutex
);
2215 for_each_active_root(root
) {
2216 struct cgroup
*from_cgrp
= task_cgroup_from_root(from
, root
);
2218 retval
= cgroup_attach_task(from_cgrp
, tsk
, false);
2222 mutex_unlock(&cgroup_mutex
);
2226 EXPORT_SYMBOL_GPL(cgroup_attach_task_all
);
2228 static int cgroup_tasks_write(struct cgroup_subsys_state
*css
,
2229 struct cftype
*cft
, u64 pid
)
2231 return attach_task_by_pid(css
->cgroup
, pid
, false);
2234 static int cgroup_procs_write(struct cgroup_subsys_state
*css
,
2235 struct cftype
*cft
, u64 tgid
)
2237 return attach_task_by_pid(css
->cgroup
, tgid
, true);
2240 static int cgroup_release_agent_write(struct cgroup_subsys_state
*css
,
2241 struct cftype
*cft
, const char *buffer
)
2243 struct cgroupfs_root
*root
= css
->cgroup
->root
;
2245 BUILD_BUG_ON(sizeof(root
->release_agent_path
) < PATH_MAX
);
2246 if (!cgroup_lock_live_group(css
->cgroup
))
2248 spin_lock(&release_agent_path_lock
);
2249 strlcpy(root
->release_agent_path
, buffer
,
2250 sizeof(root
->release_agent_path
));
2251 spin_unlock(&release_agent_path_lock
);
2252 mutex_unlock(&cgroup_mutex
);
2256 static int cgroup_release_agent_show(struct seq_file
*seq
, void *v
)
2258 struct cgroup
*cgrp
= seq_css(seq
)->cgroup
;
2260 if (!cgroup_lock_live_group(cgrp
))
2262 seq_puts(seq
, cgrp
->root
->release_agent_path
);
2263 seq_putc(seq
, '\n');
2264 mutex_unlock(&cgroup_mutex
);
2268 static int cgroup_sane_behavior_show(struct seq_file
*seq
, void *v
)
2270 struct cgroup
*cgrp
= seq_css(seq
)->cgroup
;
2272 seq_printf(seq
, "%d\n", cgroup_sane_behavior(cgrp
));
2276 static ssize_t
cgroup_file_write(struct file
*file
, const char __user
*userbuf
,
2277 size_t nbytes
, loff_t
*ppos
)
2279 struct cfent
*cfe
= __d_cfe(file
->f_dentry
);
2280 struct cftype
*cft
= __d_cft(file
->f_dentry
);
2281 struct cgroup_subsys_state
*css
= cfe
->css
;
2282 size_t max_bytes
= max(cft
->max_write_len
, PAGE_SIZE
);
2286 if (nbytes
> max_bytes
)
2289 buf
= kmalloc(nbytes
+ 1, GFP_KERNEL
);
2293 if (copy_from_user(buf
, userbuf
, nbytes
)) {
2300 if (cft
->write_string
) {
2301 ret
= cft
->write_string(css
, cft
, strstrip(buf
));
2302 } else if (cft
->write_u64
) {
2303 unsigned long long v
;
2304 ret
= kstrtoull(buf
, 0, &v
);
2306 ret
= cft
->write_u64(css
, cft
, v
);
2307 } else if (cft
->write_s64
) {
2309 ret
= kstrtoll(buf
, 0, &v
);
2311 ret
= cft
->write_s64(css
, cft
, v
);
2312 } else if (cft
->trigger
) {
2313 ret
= cft
->trigger(css
, (unsigned int)cft
->private);
2319 return ret
?: nbytes
;
2323 * seqfile ops/methods for returning structured data. Currently just
2324 * supports string->u64 maps, but can be extended in future.
2327 static void *cgroup_seqfile_start(struct seq_file
*seq
, loff_t
*ppos
)
2329 struct cftype
*cft
= seq_cft(seq
);
2331 if (cft
->seq_start
) {
2332 return cft
->seq_start(seq
, ppos
);
2335 * The same behavior and code as single_open(). Returns
2336 * !NULL if pos is at the beginning; otherwise, NULL.
2338 return NULL
+ !*ppos
;
2342 static void *cgroup_seqfile_next(struct seq_file
*seq
, void *v
, loff_t
*ppos
)
2344 struct cftype
*cft
= seq_cft(seq
);
2346 if (cft
->seq_next
) {
2347 return cft
->seq_next(seq
, v
, ppos
);
2350 * The same behavior and code as single_open(), always
2351 * terminate after the initial read.
2358 static void cgroup_seqfile_stop(struct seq_file
*seq
, void *v
)
2360 struct cftype
*cft
= seq_cft(seq
);
2363 cft
->seq_stop(seq
, v
);
2366 static int cgroup_seqfile_show(struct seq_file
*m
, void *arg
)
2368 struct cftype
*cft
= seq_cft(m
);
2369 struct cgroup_subsys_state
*css
= seq_css(m
);
2372 return cft
->seq_show(m
, arg
);
2375 seq_printf(m
, "%llu\n", cft
->read_u64(css
, cft
));
2376 else if (cft
->read_s64
)
2377 seq_printf(m
, "%lld\n", cft
->read_s64(css
, cft
));
2383 static struct seq_operations cgroup_seq_operations
= {
2384 .start
= cgroup_seqfile_start
,
2385 .next
= cgroup_seqfile_next
,
2386 .stop
= cgroup_seqfile_stop
,
2387 .show
= cgroup_seqfile_show
,
2390 static int cgroup_file_open(struct inode
*inode
, struct file
*file
)
2392 struct cfent
*cfe
= __d_cfe(file
->f_dentry
);
2393 struct cftype
*cft
= __d_cft(file
->f_dentry
);
2394 struct cgroup
*cgrp
= __d_cgrp(cfe
->dentry
->d_parent
);
2395 struct cgroup_subsys_state
*css
;
2396 struct cgroup_open_file
*of
;
2399 err
= generic_file_open(inode
, file
);
2404 * If the file belongs to a subsystem, pin the css. Will be
2405 * unpinned either on open failure or release. This ensures that
2406 * @css stays alive for all file operations.
2409 css
= cgroup_css(cgrp
, cft
->ss
);
2410 if (cft
->ss
&& !css_tryget(css
))
2418 * @cfe->css is used by read/write/close to determine the
2419 * associated css. @file->private_data would be a better place but
2420 * that's already used by seqfile. Multiple accessors may use it
2421 * simultaneously which is okay as the association never changes.
2423 WARN_ON_ONCE(cfe
->css
&& cfe
->css
!= css
);
2426 of
= __seq_open_private(file
, &cgroup_seq_operations
,
2427 sizeof(struct cgroup_open_file
));
2438 static int cgroup_file_release(struct inode
*inode
, struct file
*file
)
2440 struct cfent
*cfe
= __d_cfe(file
->f_dentry
);
2441 struct cgroup_subsys_state
*css
= cfe
->css
;
2445 return seq_release_private(inode
, file
);
2449 * cgroup_rename - Only allow simple rename of directories in place.
2451 static int cgroup_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
2452 struct inode
*new_dir
, struct dentry
*new_dentry
)
2455 struct cgroup_name
*name
, *old_name
;
2456 struct cgroup
*cgrp
;
2459 * It's convinient to use parent dir's i_mutex to protected
2462 lockdep_assert_held(&old_dir
->i_mutex
);
2464 if (!S_ISDIR(old_dentry
->d_inode
->i_mode
))
2466 if (new_dentry
->d_inode
)
2468 if (old_dir
!= new_dir
)
2471 cgrp
= __d_cgrp(old_dentry
);
2474 * This isn't a proper migration and its usefulness is very
2475 * limited. Disallow if sane_behavior.
2477 if (cgroup_sane_behavior(cgrp
))
2480 name
= cgroup_alloc_name(new_dentry
->d_name
.name
);
2484 ret
= simple_rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
2490 old_name
= rcu_dereference_protected(cgrp
->name
, true);
2491 rcu_assign_pointer(cgrp
->name
, name
);
2493 kfree_rcu(old_name
, rcu_head
);
2497 static struct simple_xattrs
*__d_xattrs(struct dentry
*dentry
)
2499 if (S_ISDIR(dentry
->d_inode
->i_mode
))
2500 return &__d_cgrp(dentry
)->xattrs
;
2502 return &__d_cfe(dentry
)->xattrs
;
2505 static inline int xattr_enabled(struct dentry
*dentry
)
2507 struct cgroupfs_root
*root
= dentry
->d_sb
->s_fs_info
;
2508 return root
->flags
& CGRP_ROOT_XATTR
;
2511 static bool is_valid_xattr(const char *name
)
2513 if (!strncmp(name
, XATTR_TRUSTED_PREFIX
, XATTR_TRUSTED_PREFIX_LEN
) ||
2514 !strncmp(name
, XATTR_SECURITY_PREFIX
, XATTR_SECURITY_PREFIX_LEN
))
2519 static int cgroup_setxattr(struct dentry
*dentry
, const char *name
,
2520 const void *val
, size_t size
, int flags
)
2522 if (!xattr_enabled(dentry
))
2524 if (!is_valid_xattr(name
))
2526 return simple_xattr_set(__d_xattrs(dentry
), name
, val
, size
, flags
);
2529 static int cgroup_removexattr(struct dentry
*dentry
, const char *name
)
2531 if (!xattr_enabled(dentry
))
2533 if (!is_valid_xattr(name
))
2535 return simple_xattr_remove(__d_xattrs(dentry
), name
);
2538 static ssize_t
cgroup_getxattr(struct dentry
*dentry
, const char *name
,
2539 void *buf
, size_t size
)
2541 if (!xattr_enabled(dentry
))
2543 if (!is_valid_xattr(name
))
2545 return simple_xattr_get(__d_xattrs(dentry
), name
, buf
, size
);
2548 static ssize_t
cgroup_listxattr(struct dentry
*dentry
, char *buf
, size_t size
)
2550 if (!xattr_enabled(dentry
))
2552 return simple_xattr_list(__d_xattrs(dentry
), buf
, size
);
2555 static const struct file_operations cgroup_file_operations
= {
2557 .write
= cgroup_file_write
,
2558 .llseek
= generic_file_llseek
,
2559 .open
= cgroup_file_open
,
2560 .release
= cgroup_file_release
,
2563 static const struct inode_operations cgroup_file_inode_operations
= {
2564 .setxattr
= cgroup_setxattr
,
2565 .getxattr
= cgroup_getxattr
,
2566 .listxattr
= cgroup_listxattr
,
2567 .removexattr
= cgroup_removexattr
,
2570 static const struct inode_operations cgroup_dir_inode_operations
= {
2571 .lookup
= simple_lookup
,
2572 .mkdir
= cgroup_mkdir
,
2573 .rmdir
= cgroup_rmdir
,
2574 .rename
= cgroup_rename
,
2575 .setxattr
= cgroup_setxattr
,
2576 .getxattr
= cgroup_getxattr
,
2577 .listxattr
= cgroup_listxattr
,
2578 .removexattr
= cgroup_removexattr
,
2581 static int cgroup_create_file(struct dentry
*dentry
, umode_t mode
,
2582 struct super_block
*sb
)
2584 struct inode
*inode
;
2588 if (dentry
->d_inode
)
2591 inode
= cgroup_new_inode(mode
, sb
);
2595 if (S_ISDIR(mode
)) {
2596 inode
->i_op
= &cgroup_dir_inode_operations
;
2597 inode
->i_fop
= &simple_dir_operations
;
2599 /* start off with i_nlink == 2 (for "." entry) */
2601 inc_nlink(dentry
->d_parent
->d_inode
);
2604 * Control reaches here with cgroup_mutex held.
2605 * @inode->i_mutex should nest outside cgroup_mutex but we
2606 * want to populate it immediately without releasing
2607 * cgroup_mutex. As @inode isn't visible to anyone else
2608 * yet, trylock will always succeed without affecting
2611 WARN_ON_ONCE(!mutex_trylock(&inode
->i_mutex
));
2612 } else if (S_ISREG(mode
)) {
2614 inode
->i_fop
= &cgroup_file_operations
;
2615 inode
->i_op
= &cgroup_file_inode_operations
;
2617 d_instantiate(dentry
, inode
);
2618 dget(dentry
); /* Extra count - pin the dentry in core */
2623 * cgroup_file_mode - deduce file mode of a control file
2624 * @cft: the control file in question
2626 * returns cft->mode if ->mode is not 0
2627 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2628 * returns S_IRUGO if it has only a read handler
2629 * returns S_IWUSR if it has only a write hander
2631 static umode_t
cgroup_file_mode(const struct cftype
*cft
)
2638 if (cft
->read_u64
|| cft
->read_s64
|| cft
->seq_show
)
2641 if (cft
->write_u64
|| cft
->write_s64
|| cft
->write_string
||
2648 static int cgroup_add_file(struct cgroup
*cgrp
, struct cftype
*cft
)
2650 struct dentry
*dir
= cgrp
->dentry
;
2651 struct cgroup
*parent
= __d_cgrp(dir
);
2652 struct dentry
*dentry
;
2656 char name
[CGROUP_FILE_NAME_MAX
];
2658 BUG_ON(!mutex_is_locked(&dir
->d_inode
->i_mutex
));
2660 cfe
= kzalloc(sizeof(*cfe
), GFP_KERNEL
);
2664 cgroup_file_name(cgrp
, cft
, name
);
2665 dentry
= lookup_one_len(name
, dir
, strlen(name
));
2666 if (IS_ERR(dentry
)) {
2667 error
= PTR_ERR(dentry
);
2671 cfe
->type
= (void *)cft
;
2672 cfe
->dentry
= dentry
;
2673 dentry
->d_fsdata
= cfe
;
2674 simple_xattrs_init(&cfe
->xattrs
);
2676 mode
= cgroup_file_mode(cft
);
2677 error
= cgroup_create_file(dentry
, mode
| S_IFREG
, cgrp
->root
->sb
);
2679 list_add_tail(&cfe
->node
, &parent
->files
);
2689 * cgroup_addrm_files - add or remove files to a cgroup directory
2690 * @cgrp: the target cgroup
2691 * @cfts: array of cftypes to be added
2692 * @is_add: whether to add or remove
2694 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
2695 * For removals, this function never fails. If addition fails, this
2696 * function doesn't remove files already added. The caller is responsible
2699 static int cgroup_addrm_files(struct cgroup
*cgrp
, struct cftype cfts
[],
2705 lockdep_assert_held(&cgrp
->dentry
->d_inode
->i_mutex
);
2706 lockdep_assert_held(&cgroup_tree_mutex
);
2708 for (cft
= cfts
; cft
->name
[0] != '\0'; cft
++) {
2709 /* does cft->flags tell us to skip this file on @cgrp? */
2710 if ((cft
->flags
& CFTYPE_INSANE
) && cgroup_sane_behavior(cgrp
))
2712 if ((cft
->flags
& CFTYPE_NOT_ON_ROOT
) && !cgrp
->parent
)
2714 if ((cft
->flags
& CFTYPE_ONLY_ON_ROOT
) && cgrp
->parent
)
2718 ret
= cgroup_add_file(cgrp
, cft
);
2720 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
2725 cgroup_rm_file(cgrp
, cft
);
2731 static void cgroup_cfts_prepare(void)
2732 __acquires(&cgroup_mutex
)
2735 * Thanks to the entanglement with vfs inode locking, we can't walk
2736 * the existing cgroups under cgroup_mutex and create files.
2737 * Instead, we use css_for_each_descendant_pre() and drop RCU read
2738 * lock before calling cgroup_addrm_files().
2740 mutex_lock(&cgroup_tree_mutex
);
2741 mutex_lock(&cgroup_mutex
);
2744 static int cgroup_cfts_commit(struct cftype
*cfts
, bool is_add
)
2745 __releases(&cgroup_mutex
)
2748 struct cgroup_subsys
*ss
= cfts
[0].ss
;
2749 struct cgroup
*root
= &ss
->root
->top_cgroup
;
2750 struct super_block
*sb
= ss
->root
->sb
;
2751 struct cgroup
*prev
= NULL
;
2752 struct inode
*inode
;
2753 struct cgroup_subsys_state
*css
;
2757 mutex_unlock(&cgroup_mutex
);
2759 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
2760 if (!cfts
|| ss
->root
== &cgroup_dummy_root
||
2761 !atomic_inc_not_zero(&sb
->s_active
)) {
2762 mutex_unlock(&cgroup_tree_mutex
);
2767 * All cgroups which are created after we drop cgroup_mutex will
2768 * have the updated set of files, so we only need to update the
2769 * cgroups created before the current @cgroup_serial_nr_next.
2771 update_before
= cgroup_serial_nr_next
;
2773 /* add/rm files for all cgroups created before */
2774 css_for_each_descendant_pre(css
, cgroup_css(root
, ss
)) {
2775 struct cgroup
*cgrp
= css
->cgroup
;
2777 if (cgroup_is_dead(cgrp
))
2780 inode
= cgrp
->dentry
->d_inode
;
2786 mutex_unlock(&cgroup_tree_mutex
);
2787 mutex_lock(&inode
->i_mutex
);
2788 mutex_lock(&cgroup_tree_mutex
);
2789 if (cgrp
->serial_nr
< update_before
&& !cgroup_is_dead(cgrp
))
2790 ret
= cgroup_addrm_files(cgrp
, cfts
, is_add
);
2791 mutex_unlock(&inode
->i_mutex
);
2795 mutex_unlock(&cgroup_tree_mutex
);
2797 cgroup_put_root(ss
->root
);
2801 static void cgroup_exit_cftypes(struct cftype
*cfts
)
2805 for (cft
= cfts
; cft
->name
[0] != '\0'; cft
++)
2809 static void cgroup_init_cftypes(struct cgroup_subsys
*ss
, struct cftype
*cfts
)
2813 for (cft
= cfts
; cft
->name
[0] != '\0'; cft
++)
2818 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2819 * @ss: target cgroup subsystem
2820 * @cfts: zero-length name terminated array of cftypes
2822 * Register @cfts to @ss. Files described by @cfts are created for all
2823 * existing cgroups to which @ss is attached and all future cgroups will
2824 * have them too. This function can be called anytime whether @ss is
2827 * Returns 0 on successful registration, -errno on failure. Note that this
2828 * function currently returns 0 as long as @cfts registration is successful
2829 * even if some file creation attempts on existing cgroups fail.
2831 int cgroup_add_cftypes(struct cgroup_subsys
*ss
, struct cftype
*cfts
)
2833 struct cftype_set
*set
;
2836 set
= kzalloc(sizeof(*set
), GFP_KERNEL
);
2840 cgroup_init_cftypes(ss
, cfts
);
2842 cgroup_cfts_prepare();
2844 list_add_tail(&set
->node
, &ss
->cftsets
);
2845 ret
= cgroup_cfts_commit(cfts
, true);
2847 cgroup_rm_cftypes(cfts
);
2850 EXPORT_SYMBOL_GPL(cgroup_add_cftypes
);
2853 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
2854 * @cfts: zero-length name terminated array of cftypes
2856 * Unregister @cfts. Files described by @cfts are removed from all
2857 * existing cgroups and all future cgroups won't have them either. This
2858 * function can be called anytime whether @cfts' subsys is attached or not.
2860 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
2863 int cgroup_rm_cftypes(struct cftype
*cfts
)
2865 struct cftype
*found
= NULL
;
2866 struct cftype_set
*set
;
2868 if (!cfts
|| !cfts
[0].ss
)
2871 cgroup_cfts_prepare();
2873 list_for_each_entry(set
, &cfts
[0].ss
->cftsets
, node
) {
2874 if (set
->cfts
== cfts
) {
2875 list_del(&set
->node
);
2882 cgroup_cfts_commit(found
, false);
2883 cgroup_exit_cftypes(cfts
);
2884 return found
? 0 : -ENOENT
;
2888 * cgroup_task_count - count the number of tasks in a cgroup.
2889 * @cgrp: the cgroup in question
2891 * Return the number of tasks in the cgroup.
2893 int cgroup_task_count(const struct cgroup
*cgrp
)
2896 struct cgrp_cset_link
*link
;
2898 read_lock(&css_set_lock
);
2899 list_for_each_entry(link
, &cgrp
->cset_links
, cset_link
)
2900 count
+= atomic_read(&link
->cset
->refcount
);
2901 read_unlock(&css_set_lock
);
2906 * To reduce the fork() overhead for systems that are not actually using
2907 * their cgroups capability, we don't maintain the lists running through
2908 * each css_set to its tasks until we see the list actually used - in other
2909 * words after the first call to css_task_iter_start().
2911 static void cgroup_enable_task_cg_lists(void)
2913 struct task_struct
*p
, *g
;
2914 write_lock(&css_set_lock
);
2915 use_task_css_set_links
= 1;
2917 * We need tasklist_lock because RCU is not safe against
2918 * while_each_thread(). Besides, a forking task that has passed
2919 * cgroup_post_fork() without seeing use_task_css_set_links = 1
2920 * is not guaranteed to have its child immediately visible in the
2921 * tasklist if we walk through it with RCU.
2923 read_lock(&tasklist_lock
);
2924 do_each_thread(g
, p
) {
2927 * We should check if the process is exiting, otherwise
2928 * it will race with cgroup_exit() in that the list
2929 * entry won't be deleted though the process has exited.
2931 if (!(p
->flags
& PF_EXITING
) && list_empty(&p
->cg_list
))
2932 list_add(&p
->cg_list
, &task_css_set(p
)->tasks
);
2934 } while_each_thread(g
, p
);
2935 read_unlock(&tasklist_lock
);
2936 write_unlock(&css_set_lock
);
2940 * css_next_child - find the next child of a given css
2941 * @pos_css: the current position (%NULL to initiate traversal)
2942 * @parent_css: css whose children to walk
2944 * This function returns the next child of @parent_css and should be called
2945 * under either cgroup_mutex or RCU read lock. The only requirement is
2946 * that @parent_css and @pos_css are accessible. The next sibling is
2947 * guaranteed to be returned regardless of their states.
2949 struct cgroup_subsys_state
*
2950 css_next_child(struct cgroup_subsys_state
*pos_css
,
2951 struct cgroup_subsys_state
*parent_css
)
2953 struct cgroup
*pos
= pos_css
? pos_css
->cgroup
: NULL
;
2954 struct cgroup
*cgrp
= parent_css
->cgroup
;
2955 struct cgroup
*next
;
2957 cgroup_assert_mutexes_or_rcu_locked();
2960 * @pos could already have been removed. Once a cgroup is removed,
2961 * its ->sibling.next is no longer updated when its next sibling
2962 * changes. As CGRP_DEAD assertion is serialized and happens
2963 * before the cgroup is taken off the ->sibling list, if we see it
2964 * unasserted, it's guaranteed that the next sibling hasn't
2965 * finished its grace period even if it's already removed, and thus
2966 * safe to dereference from this RCU critical section. If
2967 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
2968 * to be visible as %true here.
2970 * If @pos is dead, its next pointer can't be dereferenced;
2971 * however, as each cgroup is given a monotonically increasing
2972 * unique serial number and always appended to the sibling list,
2973 * the next one can be found by walking the parent's children until
2974 * we see a cgroup with higher serial number than @pos's. While
2975 * this path can be slower, it's taken only when either the current
2976 * cgroup is removed or iteration and removal race.
2979 next
= list_entry_rcu(cgrp
->children
.next
, struct cgroup
, sibling
);
2980 } else if (likely(!cgroup_is_dead(pos
))) {
2981 next
= list_entry_rcu(pos
->sibling
.next
, struct cgroup
, sibling
);
2983 list_for_each_entry_rcu(next
, &cgrp
->children
, sibling
)
2984 if (next
->serial_nr
> pos
->serial_nr
)
2988 if (&next
->sibling
== &cgrp
->children
)
2991 return cgroup_css(next
, parent_css
->ss
);
2993 EXPORT_SYMBOL_GPL(css_next_child
);
2996 * css_next_descendant_pre - find the next descendant for pre-order walk
2997 * @pos: the current position (%NULL to initiate traversal)
2998 * @root: css whose descendants to walk
3000 * To be used by css_for_each_descendant_pre(). Find the next descendant
3001 * to visit for pre-order traversal of @root's descendants. @root is
3002 * included in the iteration and the first node to be visited.
3004 * While this function requires cgroup_mutex or RCU read locking, it
3005 * doesn't require the whole traversal to be contained in a single critical
3006 * section. This function will return the correct next descendant as long
3007 * as both @pos and @root are accessible and @pos is a descendant of @root.
3009 struct cgroup_subsys_state
*
3010 css_next_descendant_pre(struct cgroup_subsys_state
*pos
,
3011 struct cgroup_subsys_state
*root
)
3013 struct cgroup_subsys_state
*next
;
3015 cgroup_assert_mutexes_or_rcu_locked();
3017 /* if first iteration, visit @root */
3021 /* visit the first child if exists */
3022 next
= css_next_child(NULL
, pos
);
3026 /* no child, visit my or the closest ancestor's next sibling */
3027 while (pos
!= root
) {
3028 next
= css_next_child(pos
, css_parent(pos
));
3031 pos
= css_parent(pos
);
3036 EXPORT_SYMBOL_GPL(css_next_descendant_pre
);
3039 * css_rightmost_descendant - return the rightmost descendant of a css
3040 * @pos: css of interest
3042 * Return the rightmost descendant of @pos. If there's no descendant, @pos
3043 * is returned. This can be used during pre-order traversal to skip
3046 * While this function requires cgroup_mutex or RCU read locking, it
3047 * doesn't require the whole traversal to be contained in a single critical
3048 * section. This function will return the correct rightmost descendant as
3049 * long as @pos is accessible.
3051 struct cgroup_subsys_state
*
3052 css_rightmost_descendant(struct cgroup_subsys_state
*pos
)
3054 struct cgroup_subsys_state
*last
, *tmp
;
3056 cgroup_assert_mutexes_or_rcu_locked();
3060 /* ->prev isn't RCU safe, walk ->next till the end */
3062 css_for_each_child(tmp
, last
)
3068 EXPORT_SYMBOL_GPL(css_rightmost_descendant
);
3070 static struct cgroup_subsys_state
*
3071 css_leftmost_descendant(struct cgroup_subsys_state
*pos
)
3073 struct cgroup_subsys_state
*last
;
3077 pos
= css_next_child(NULL
, pos
);
3084 * css_next_descendant_post - find the next descendant for post-order walk
3085 * @pos: the current position (%NULL to initiate traversal)
3086 * @root: css whose descendants to walk
3088 * To be used by css_for_each_descendant_post(). Find the next descendant
3089 * to visit for post-order traversal of @root's descendants. @root is
3090 * included in the iteration and the last node to be visited.
3092 * While this function requires cgroup_mutex or RCU read locking, it
3093 * doesn't require the whole traversal to be contained in a single critical
3094 * section. This function will return the correct next descendant as long
3095 * as both @pos and @cgroup are accessible and @pos is a descendant of
3098 struct cgroup_subsys_state
*
3099 css_next_descendant_post(struct cgroup_subsys_state
*pos
,
3100 struct cgroup_subsys_state
*root
)
3102 struct cgroup_subsys_state
*next
;
3104 cgroup_assert_mutexes_or_rcu_locked();
3106 /* if first iteration, visit leftmost descendant which may be @root */
3108 return css_leftmost_descendant(root
);
3110 /* if we visited @root, we're done */
3114 /* if there's an unvisited sibling, visit its leftmost descendant */
3115 next
= css_next_child(pos
, css_parent(pos
));
3117 return css_leftmost_descendant(next
);
3119 /* no sibling left, visit parent */
3120 return css_parent(pos
);
3122 EXPORT_SYMBOL_GPL(css_next_descendant_post
);
3125 * css_advance_task_iter - advance a task itererator to the next css_set
3126 * @it: the iterator to advance
3128 * Advance @it to the next css_set to walk.
3130 static void css_advance_task_iter(struct css_task_iter
*it
)
3132 struct list_head
*l
= it
->cset_link
;
3133 struct cgrp_cset_link
*link
;
3134 struct css_set
*cset
;
3136 /* Advance to the next non-empty css_set */
3139 if (l
== &it
->origin_css
->cgroup
->cset_links
) {
3140 it
->cset_link
= NULL
;
3143 link
= list_entry(l
, struct cgrp_cset_link
, cset_link
);
3145 } while (list_empty(&cset
->tasks
));
3147 it
->task
= cset
->tasks
.next
;
3151 * css_task_iter_start - initiate task iteration
3152 * @css: the css to walk tasks of
3153 * @it: the task iterator to use
3155 * Initiate iteration through the tasks of @css. The caller can call
3156 * css_task_iter_next() to walk through the tasks until the function
3157 * returns NULL. On completion of iteration, css_task_iter_end() must be
3160 * Note that this function acquires a lock which is released when the
3161 * iteration finishes. The caller can't sleep while iteration is in
3164 void css_task_iter_start(struct cgroup_subsys_state
*css
,
3165 struct css_task_iter
*it
)
3166 __acquires(css_set_lock
)
3169 * The first time anyone tries to iterate across a css, we need to
3170 * enable the list linking each css_set to its tasks, and fix up
3171 * all existing tasks.
3173 if (!use_task_css_set_links
)
3174 cgroup_enable_task_cg_lists();
3176 read_lock(&css_set_lock
);
3178 it
->origin_css
= css
;
3179 it
->cset_link
= &css
->cgroup
->cset_links
;
3181 css_advance_task_iter(it
);
3185 * css_task_iter_next - return the next task for the iterator
3186 * @it: the task iterator being iterated
3188 * The "next" function for task iteration. @it should have been
3189 * initialized via css_task_iter_start(). Returns NULL when the iteration
3192 struct task_struct
*css_task_iter_next(struct css_task_iter
*it
)
3194 struct task_struct
*res
;
3195 struct list_head
*l
= it
->task
;
3196 struct cgrp_cset_link
*link
;
3198 /* If the iterator cg is NULL, we have no tasks */
3201 res
= list_entry(l
, struct task_struct
, cg_list
);
3202 /* Advance iterator to find next entry */
3204 link
= list_entry(it
->cset_link
, struct cgrp_cset_link
, cset_link
);
3205 if (l
== &link
->cset
->tasks
) {
3207 * We reached the end of this task list - move on to the
3208 * next cgrp_cset_link.
3210 css_advance_task_iter(it
);
3218 * css_task_iter_end - finish task iteration
3219 * @it: the task iterator to finish
3221 * Finish task iteration started by css_task_iter_start().
3223 void css_task_iter_end(struct css_task_iter
*it
)
3224 __releases(css_set_lock
)
3226 read_unlock(&css_set_lock
);
3229 static inline int started_after_time(struct task_struct
*t1
,
3230 struct timespec
*time
,
3231 struct task_struct
*t2
)
3233 int start_diff
= timespec_compare(&t1
->start_time
, time
);
3234 if (start_diff
> 0) {
3236 } else if (start_diff
< 0) {
3240 * Arbitrarily, if two processes started at the same
3241 * time, we'll say that the lower pointer value
3242 * started first. Note that t2 may have exited by now
3243 * so this may not be a valid pointer any longer, but
3244 * that's fine - it still serves to distinguish
3245 * between two tasks started (effectively) simultaneously.
3252 * This function is a callback from heap_insert() and is used to order
3254 * In this case we order the heap in descending task start time.
3256 static inline int started_after(void *p1
, void *p2
)
3258 struct task_struct
*t1
= p1
;
3259 struct task_struct
*t2
= p2
;
3260 return started_after_time(t1
, &t2
->start_time
, t2
);
3264 * css_scan_tasks - iterate though all the tasks in a css
3265 * @css: the css to iterate tasks of
3266 * @test: optional test callback
3267 * @process: process callback
3268 * @data: data passed to @test and @process
3269 * @heap: optional pre-allocated heap used for task iteration
3271 * Iterate through all the tasks in @css, calling @test for each, and if it
3272 * returns %true, call @process for it also.
3274 * @test may be NULL, meaning always true (select all tasks), which
3275 * effectively duplicates css_task_iter_{start,next,end}() but does not
3276 * lock css_set_lock for the call to @process.
3278 * It is guaranteed that @process will act on every task that is a member
3279 * of @css for the duration of this call. This function may or may not
3280 * call @process for tasks that exit or move to a different css during the
3281 * call, or are forked or move into the css during the call.
3283 * Note that @test may be called with locks held, and may in some
3284 * situations be called multiple times for the same task, so it should be
3287 * If @heap is non-NULL, a heap has been pre-allocated and will be used for
3288 * heap operations (and its "gt" member will be overwritten), else a
3289 * temporary heap will be used (allocation of which may cause this function
3292 int css_scan_tasks(struct cgroup_subsys_state
*css
,
3293 bool (*test
)(struct task_struct
*, void *),
3294 void (*process
)(struct task_struct
*, void *),
3295 void *data
, struct ptr_heap
*heap
)
3298 struct css_task_iter it
;
3299 struct task_struct
*p
, *dropped
;
3300 /* Never dereference latest_task, since it's not refcounted */
3301 struct task_struct
*latest_task
= NULL
;
3302 struct ptr_heap tmp_heap
;
3303 struct timespec latest_time
= { 0, 0 };
3306 /* The caller supplied our heap and pre-allocated its memory */
3307 heap
->gt
= &started_after
;
3309 /* We need to allocate our own heap memory */
3311 retval
= heap_init(heap
, PAGE_SIZE
, GFP_KERNEL
, &started_after
);
3313 /* cannot allocate the heap */
3319 * Scan tasks in the css, using the @test callback to determine
3320 * which are of interest, and invoking @process callback on the
3321 * ones which need an update. Since we don't want to hold any
3322 * locks during the task updates, gather tasks to be processed in a
3323 * heap structure. The heap is sorted by descending task start
3324 * time. If the statically-sized heap fills up, we overflow tasks
3325 * that started later, and in future iterations only consider tasks
3326 * that started after the latest task in the previous pass. This
3327 * guarantees forward progress and that we don't miss any tasks.
3330 css_task_iter_start(css
, &it
);
3331 while ((p
= css_task_iter_next(&it
))) {
3333 * Only affect tasks that qualify per the caller's callback,
3334 * if he provided one
3336 if (test
&& !test(p
, data
))
3339 * Only process tasks that started after the last task
3342 if (!started_after_time(p
, &latest_time
, latest_task
))
3344 dropped
= heap_insert(heap
, p
);
3345 if (dropped
== NULL
) {
3347 * The new task was inserted; the heap wasn't
3351 } else if (dropped
!= p
) {
3353 * The new task was inserted, and pushed out a
3357 put_task_struct(dropped
);
3360 * Else the new task was newer than anything already in
3361 * the heap and wasn't inserted
3364 css_task_iter_end(&it
);
3367 for (i
= 0; i
< heap
->size
; i
++) {
3368 struct task_struct
*q
= heap
->ptrs
[i
];
3370 latest_time
= q
->start_time
;
3373 /* Process the task per the caller's callback */
3378 * If we had to process any tasks at all, scan again
3379 * in case some of them were in the middle of forking
3380 * children that didn't get processed.
3381 * Not the most efficient way to do it, but it avoids
3382 * having to take callback_mutex in the fork path
3386 if (heap
== &tmp_heap
)
3387 heap_free(&tmp_heap
);
3391 static void cgroup_transfer_one_task(struct task_struct
*task
, void *data
)
3393 struct cgroup
*new_cgroup
= data
;
3395 mutex_lock(&cgroup_mutex
);
3396 cgroup_attach_task(new_cgroup
, task
, false);
3397 mutex_unlock(&cgroup_mutex
);
3401 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3402 * @to: cgroup to which the tasks will be moved
3403 * @from: cgroup in which the tasks currently reside
3405 int cgroup_transfer_tasks(struct cgroup
*to
, struct cgroup
*from
)
3407 return css_scan_tasks(&from
->dummy_css
, NULL
, cgroup_transfer_one_task
,
3412 * Stuff for reading the 'tasks'/'procs' files.
3414 * Reading this file can return large amounts of data if a cgroup has
3415 * *lots* of attached tasks. So it may need several calls to read(),
3416 * but we cannot guarantee that the information we produce is correct
3417 * unless we produce it entirely atomically.
3421 /* which pidlist file are we talking about? */
3422 enum cgroup_filetype
{
3428 * A pidlist is a list of pids that virtually represents the contents of one
3429 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3430 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3433 struct cgroup_pidlist
{
3435 * used to find which pidlist is wanted. doesn't change as long as
3436 * this particular list stays in the list.
3438 struct { enum cgroup_filetype type
; struct pid_namespace
*ns
; } key
;
3441 /* how many elements the above list has */
3443 /* each of these stored in a list by its cgroup */
3444 struct list_head links
;
3445 /* pointer to the cgroup we belong to, for list removal purposes */
3446 struct cgroup
*owner
;
3447 /* for delayed destruction */
3448 struct delayed_work destroy_dwork
;
3452 * The following two functions "fix" the issue where there are more pids
3453 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3454 * TODO: replace with a kernel-wide solution to this problem
3456 #define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3457 static void *pidlist_allocate(int count
)
3459 if (PIDLIST_TOO_LARGE(count
))
3460 return vmalloc(count
* sizeof(pid_t
));
3462 return kmalloc(count
* sizeof(pid_t
), GFP_KERNEL
);
3465 static void pidlist_free(void *p
)
3467 if (is_vmalloc_addr(p
))
3474 * Used to destroy all pidlists lingering waiting for destroy timer. None
3475 * should be left afterwards.
3477 static void cgroup_pidlist_destroy_all(struct cgroup
*cgrp
)
3479 struct cgroup_pidlist
*l
, *tmp_l
;
3481 mutex_lock(&cgrp
->pidlist_mutex
);
3482 list_for_each_entry_safe(l
, tmp_l
, &cgrp
->pidlists
, links
)
3483 mod_delayed_work(cgroup_pidlist_destroy_wq
, &l
->destroy_dwork
, 0);
3484 mutex_unlock(&cgrp
->pidlist_mutex
);
3486 flush_workqueue(cgroup_pidlist_destroy_wq
);
3487 BUG_ON(!list_empty(&cgrp
->pidlists
));
3490 static void cgroup_pidlist_destroy_work_fn(struct work_struct
*work
)
3492 struct delayed_work
*dwork
= to_delayed_work(work
);
3493 struct cgroup_pidlist
*l
= container_of(dwork
, struct cgroup_pidlist
,
3495 struct cgroup_pidlist
*tofree
= NULL
;
3497 mutex_lock(&l
->owner
->pidlist_mutex
);
3500 * Destroy iff we didn't get queued again. The state won't change
3501 * as destroy_dwork can only be queued while locked.
3503 if (!delayed_work_pending(dwork
)) {
3504 list_del(&l
->links
);
3505 pidlist_free(l
->list
);
3506 put_pid_ns(l
->key
.ns
);
3510 mutex_unlock(&l
->owner
->pidlist_mutex
);
3515 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
3516 * Returns the number of unique elements.
3518 static int pidlist_uniq(pid_t
*list
, int length
)
3523 * we presume the 0th element is unique, so i starts at 1. trivial
3524 * edge cases first; no work needs to be done for either
3526 if (length
== 0 || length
== 1)
3528 /* src and dest walk down the list; dest counts unique elements */
3529 for (src
= 1; src
< length
; src
++) {
3530 /* find next unique element */
3531 while (list
[src
] == list
[src
-1]) {
3536 /* dest always points to where the next unique element goes */
3537 list
[dest
] = list
[src
];
3545 * The two pid files - task and cgroup.procs - guaranteed that the result
3546 * is sorted, which forced this whole pidlist fiasco. As pid order is
3547 * different per namespace, each namespace needs differently sorted list,
3548 * making it impossible to use, for example, single rbtree of member tasks
3549 * sorted by task pointer. As pidlists can be fairly large, allocating one
3550 * per open file is dangerous, so cgroup had to implement shared pool of
3551 * pidlists keyed by cgroup and namespace.
3553 * All this extra complexity was caused by the original implementation
3554 * committing to an entirely unnecessary property. In the long term, we
3555 * want to do away with it. Explicitly scramble sort order if
3556 * sane_behavior so that no such expectation exists in the new interface.
3558 * Scrambling is done by swapping every two consecutive bits, which is
3559 * non-identity one-to-one mapping which disturbs sort order sufficiently.
3561 static pid_t
pid_fry(pid_t pid
)
3563 unsigned a
= pid
& 0x55555555;
3564 unsigned b
= pid
& 0xAAAAAAAA;
3566 return (a
<< 1) | (b
>> 1);
3569 static pid_t
cgroup_pid_fry(struct cgroup
*cgrp
, pid_t pid
)
3571 if (cgroup_sane_behavior(cgrp
))
3572 return pid_fry(pid
);
3577 static int cmppid(const void *a
, const void *b
)
3579 return *(pid_t
*)a
- *(pid_t
*)b
;
3582 static int fried_cmppid(const void *a
, const void *b
)
3584 return pid_fry(*(pid_t
*)a
) - pid_fry(*(pid_t
*)b
);
3587 static struct cgroup_pidlist
*cgroup_pidlist_find(struct cgroup
*cgrp
,
3588 enum cgroup_filetype type
)
3590 struct cgroup_pidlist
*l
;
3591 /* don't need task_nsproxy() if we're looking at ourself */
3592 struct pid_namespace
*ns
= task_active_pid_ns(current
);
3594 lockdep_assert_held(&cgrp
->pidlist_mutex
);
3596 list_for_each_entry(l
, &cgrp
->pidlists
, links
)
3597 if (l
->key
.type
== type
&& l
->key
.ns
== ns
)
3603 * find the appropriate pidlist for our purpose (given procs vs tasks)
3604 * returns with the lock on that pidlist already held, and takes care
3605 * of the use count, or returns NULL with no locks held if we're out of
3608 static struct cgroup_pidlist
*cgroup_pidlist_find_create(struct cgroup
*cgrp
,
3609 enum cgroup_filetype type
)
3611 struct cgroup_pidlist
*l
;
3613 lockdep_assert_held(&cgrp
->pidlist_mutex
);
3615 l
= cgroup_pidlist_find(cgrp
, type
);
3619 /* entry not found; create a new one */
3620 l
= kzalloc(sizeof(struct cgroup_pidlist
), GFP_KERNEL
);
3624 INIT_DELAYED_WORK(&l
->destroy_dwork
, cgroup_pidlist_destroy_work_fn
);
3626 /* don't need task_nsproxy() if we're looking at ourself */
3627 l
->key
.ns
= get_pid_ns(task_active_pid_ns(current
));
3629 list_add(&l
->links
, &cgrp
->pidlists
);
3634 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3636 static int pidlist_array_load(struct cgroup
*cgrp
, enum cgroup_filetype type
,
3637 struct cgroup_pidlist
**lp
)
3641 int pid
, n
= 0; /* used for populating the array */
3642 struct css_task_iter it
;
3643 struct task_struct
*tsk
;
3644 struct cgroup_pidlist
*l
;
3646 lockdep_assert_held(&cgrp
->pidlist_mutex
);
3649 * If cgroup gets more users after we read count, we won't have
3650 * enough space - tough. This race is indistinguishable to the
3651 * caller from the case that the additional cgroup users didn't
3652 * show up until sometime later on.
3654 length
= cgroup_task_count(cgrp
);
3655 array
= pidlist_allocate(length
);
3658 /* now, populate the array */
3659 css_task_iter_start(&cgrp
->dummy_css
, &it
);
3660 while ((tsk
= css_task_iter_next(&it
))) {
3661 if (unlikely(n
== length
))
3663 /* get tgid or pid for procs or tasks file respectively */
3664 if (type
== CGROUP_FILE_PROCS
)
3665 pid
= task_tgid_vnr(tsk
);
3667 pid
= task_pid_vnr(tsk
);
3668 if (pid
> 0) /* make sure to only use valid results */
3671 css_task_iter_end(&it
);
3673 /* now sort & (if procs) strip out duplicates */
3674 if (cgroup_sane_behavior(cgrp
))
3675 sort(array
, length
, sizeof(pid_t
), fried_cmppid
, NULL
);
3677 sort(array
, length
, sizeof(pid_t
), cmppid
, NULL
);
3678 if (type
== CGROUP_FILE_PROCS
)
3679 length
= pidlist_uniq(array
, length
);
3681 l
= cgroup_pidlist_find_create(cgrp
, type
);
3683 mutex_unlock(&cgrp
->pidlist_mutex
);
3684 pidlist_free(array
);
3688 /* store array, freeing old if necessary */
3689 pidlist_free(l
->list
);
3697 * cgroupstats_build - build and fill cgroupstats
3698 * @stats: cgroupstats to fill information into
3699 * @dentry: A dentry entry belonging to the cgroup for which stats have
3702 * Build and fill cgroupstats so that taskstats can export it to user
3705 int cgroupstats_build(struct cgroupstats
*stats
, struct dentry
*dentry
)
3708 struct cgroup
*cgrp
;
3709 struct css_task_iter it
;
3710 struct task_struct
*tsk
;
3713 * Validate dentry by checking the superblock operations,
3714 * and make sure it's a directory.
3716 if (dentry
->d_sb
->s_op
!= &cgroup_ops
||
3717 !S_ISDIR(dentry
->d_inode
->i_mode
))
3721 cgrp
= dentry
->d_fsdata
;
3723 css_task_iter_start(&cgrp
->dummy_css
, &it
);
3724 while ((tsk
= css_task_iter_next(&it
))) {
3725 switch (tsk
->state
) {
3727 stats
->nr_running
++;
3729 case TASK_INTERRUPTIBLE
:
3730 stats
->nr_sleeping
++;
3732 case TASK_UNINTERRUPTIBLE
:
3733 stats
->nr_uninterruptible
++;
3736 stats
->nr_stopped
++;
3739 if (delayacct_is_task_waiting_on_io(tsk
))
3740 stats
->nr_io_wait
++;
3744 css_task_iter_end(&it
);
3752 * seq_file methods for the tasks/procs files. The seq_file position is the
3753 * next pid to display; the seq_file iterator is a pointer to the pid
3754 * in the cgroup->l->list array.
3757 static void *cgroup_pidlist_start(struct seq_file
*s
, loff_t
*pos
)
3760 * Initially we receive a position value that corresponds to
3761 * one more than the last pid shown (or 0 on the first call or
3762 * after a seek to the start). Use a binary-search to find the
3763 * next pid to display, if any
3765 struct cgroup_open_file
*of
= s
->private;
3766 struct cgroup
*cgrp
= seq_css(s
)->cgroup
;
3767 struct cgroup_pidlist
*l
;
3768 enum cgroup_filetype type
= seq_cft(s
)->private;
3769 int index
= 0, pid
= *pos
;
3772 mutex_lock(&cgrp
->pidlist_mutex
);
3775 * !NULL @of->priv indicates that this isn't the first start()
3776 * after open. If the matching pidlist is around, we can use that.
3777 * Look for it. Note that @of->priv can't be used directly. It
3778 * could already have been destroyed.
3781 of
->priv
= cgroup_pidlist_find(cgrp
, type
);
3784 * Either this is the first start() after open or the matching
3785 * pidlist has been destroyed inbetween. Create a new one.
3788 ret
= pidlist_array_load(cgrp
, type
,
3789 (struct cgroup_pidlist
**)&of
->priv
);
3791 return ERR_PTR(ret
);
3796 int end
= l
->length
;
3798 while (index
< end
) {
3799 int mid
= (index
+ end
) / 2;
3800 if (cgroup_pid_fry(cgrp
, l
->list
[mid
]) == pid
) {
3803 } else if (cgroup_pid_fry(cgrp
, l
->list
[mid
]) <= pid
)
3809 /* If we're off the end of the array, we're done */
3810 if (index
>= l
->length
)
3812 /* Update the abstract position to be the actual pid that we found */
3813 iter
= l
->list
+ index
;
3814 *pos
= cgroup_pid_fry(cgrp
, *iter
);
3818 static void cgroup_pidlist_stop(struct seq_file
*s
, void *v
)
3820 struct cgroup_open_file
*of
= s
->private;
3821 struct cgroup_pidlist
*l
= of
->priv
;
3824 mod_delayed_work(cgroup_pidlist_destroy_wq
, &l
->destroy_dwork
,
3825 CGROUP_PIDLIST_DESTROY_DELAY
);
3826 mutex_unlock(&seq_css(s
)->cgroup
->pidlist_mutex
);
3829 static void *cgroup_pidlist_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
3831 struct cgroup_open_file
*of
= s
->private;
3832 struct cgroup_pidlist
*l
= of
->priv
;
3834 pid_t
*end
= l
->list
+ l
->length
;
3836 * Advance to the next pid in the array. If this goes off the
3843 *pos
= cgroup_pid_fry(seq_css(s
)->cgroup
, *p
);
3848 static int cgroup_pidlist_show(struct seq_file
*s
, void *v
)
3850 return seq_printf(s
, "%d\n", *(int *)v
);
3854 * seq_operations functions for iterating on pidlists through seq_file -
3855 * independent of whether it's tasks or procs
3857 static const struct seq_operations cgroup_pidlist_seq_operations
= {
3858 .start
= cgroup_pidlist_start
,
3859 .stop
= cgroup_pidlist_stop
,
3860 .next
= cgroup_pidlist_next
,
3861 .show
= cgroup_pidlist_show
,
3864 static u64
cgroup_read_notify_on_release(struct cgroup_subsys_state
*css
,
3867 return notify_on_release(css
->cgroup
);
3870 static int cgroup_write_notify_on_release(struct cgroup_subsys_state
*css
,
3871 struct cftype
*cft
, u64 val
)
3873 clear_bit(CGRP_RELEASABLE
, &css
->cgroup
->flags
);
3875 set_bit(CGRP_NOTIFY_ON_RELEASE
, &css
->cgroup
->flags
);
3877 clear_bit(CGRP_NOTIFY_ON_RELEASE
, &css
->cgroup
->flags
);
3882 * When dput() is called asynchronously, if umount has been done and
3883 * then deactivate_super() in cgroup_free_fn() kills the superblock,
3884 * there's a small window that vfs will see the root dentry with non-zero
3885 * refcnt and trigger BUG().
3887 * That's why we hold a reference before dput() and drop it right after.
3889 static void cgroup_dput(struct cgroup
*cgrp
)
3891 cgroup_get_root(cgrp
->root
);
3893 cgroup_put_root(cgrp
->root
);
3896 static u64
cgroup_clone_children_read(struct cgroup_subsys_state
*css
,
3899 return test_bit(CGRP_CPUSET_CLONE_CHILDREN
, &css
->cgroup
->flags
);
3902 static int cgroup_clone_children_write(struct cgroup_subsys_state
*css
,
3903 struct cftype
*cft
, u64 val
)
3906 set_bit(CGRP_CPUSET_CLONE_CHILDREN
, &css
->cgroup
->flags
);
3908 clear_bit(CGRP_CPUSET_CLONE_CHILDREN
, &css
->cgroup
->flags
);
3912 static struct cftype cgroup_base_files
[] = {
3914 .name
= "cgroup.procs",
3915 .seq_start
= cgroup_pidlist_start
,
3916 .seq_next
= cgroup_pidlist_next
,
3917 .seq_stop
= cgroup_pidlist_stop
,
3918 .seq_show
= cgroup_pidlist_show
,
3919 .private = CGROUP_FILE_PROCS
,
3920 .write_u64
= cgroup_procs_write
,
3921 .mode
= S_IRUGO
| S_IWUSR
,
3924 .name
= "cgroup.clone_children",
3925 .flags
= CFTYPE_INSANE
,
3926 .read_u64
= cgroup_clone_children_read
,
3927 .write_u64
= cgroup_clone_children_write
,
3930 .name
= "cgroup.sane_behavior",
3931 .flags
= CFTYPE_ONLY_ON_ROOT
,
3932 .seq_show
= cgroup_sane_behavior_show
,
3936 * Historical crazy stuff. These don't have "cgroup." prefix and
3937 * don't exist if sane_behavior. If you're depending on these, be
3938 * prepared to be burned.
3942 .flags
= CFTYPE_INSANE
, /* use "procs" instead */
3943 .seq_start
= cgroup_pidlist_start
,
3944 .seq_next
= cgroup_pidlist_next
,
3945 .seq_stop
= cgroup_pidlist_stop
,
3946 .seq_show
= cgroup_pidlist_show
,
3947 .private = CGROUP_FILE_TASKS
,
3948 .write_u64
= cgroup_tasks_write
,
3949 .mode
= S_IRUGO
| S_IWUSR
,
3952 .name
= "notify_on_release",
3953 .flags
= CFTYPE_INSANE
,
3954 .read_u64
= cgroup_read_notify_on_release
,
3955 .write_u64
= cgroup_write_notify_on_release
,
3958 .name
= "release_agent",
3959 .flags
= CFTYPE_INSANE
| CFTYPE_ONLY_ON_ROOT
,
3960 .seq_show
= cgroup_release_agent_show
,
3961 .write_string
= cgroup_release_agent_write
,
3962 .max_write_len
= PATH_MAX
- 1,
3968 * cgroup_populate_dir - create subsys files in a cgroup directory
3969 * @cgrp: target cgroup
3970 * @subsys_mask: mask of the subsystem ids whose files should be added
3972 * On failure, no file is added.
3974 static int cgroup_populate_dir(struct cgroup
*cgrp
, unsigned long subsys_mask
)
3976 struct cgroup_subsys
*ss
;
3979 /* process cftsets of each subsystem */
3980 for_each_subsys(ss
, i
) {
3981 struct cftype_set
*set
;
3983 if (!test_bit(i
, &subsys_mask
))
3986 list_for_each_entry(set
, &ss
->cftsets
, node
) {
3987 ret
= cgroup_addrm_files(cgrp
, set
->cfts
, true);
3994 cgroup_clear_dir(cgrp
, subsys_mask
);
3999 * css destruction is four-stage process.
4001 * 1. Destruction starts. Killing of the percpu_ref is initiated.
4002 * Implemented in kill_css().
4004 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
4005 * and thus css_tryget() is guaranteed to fail, the css can be offlined
4006 * by invoking offline_css(). After offlining, the base ref is put.
4007 * Implemented in css_killed_work_fn().
4009 * 3. When the percpu_ref reaches zero, the only possible remaining
4010 * accessors are inside RCU read sections. css_release() schedules the
4013 * 4. After the grace period, the css can be freed. Implemented in
4014 * css_free_work_fn().
4016 * It is actually hairier because both step 2 and 4 require process context
4017 * and thus involve punting to css->destroy_work adding two additional
4018 * steps to the already complex sequence.
4020 static void css_free_work_fn(struct work_struct
*work
)
4022 struct cgroup_subsys_state
*css
=
4023 container_of(work
, struct cgroup_subsys_state
, destroy_work
);
4024 struct cgroup
*cgrp
= css
->cgroup
;
4027 css_put(css
->parent
);
4029 css
->ss
->css_free(css
);
4033 static void css_free_rcu_fn(struct rcu_head
*rcu_head
)
4035 struct cgroup_subsys_state
*css
=
4036 container_of(rcu_head
, struct cgroup_subsys_state
, rcu_head
);
4039 * css holds an extra ref to @cgrp->dentry which is put on the last
4040 * css_put(). dput() requires process context which we don't have.
4042 INIT_WORK(&css
->destroy_work
, css_free_work_fn
);
4043 queue_work(cgroup_destroy_wq
, &css
->destroy_work
);
4046 static void css_release(struct percpu_ref
*ref
)
4048 struct cgroup_subsys_state
*css
=
4049 container_of(ref
, struct cgroup_subsys_state
, refcnt
);
4051 rcu_assign_pointer(css
->cgroup
->subsys
[css
->ss
->id
], NULL
);
4052 call_rcu(&css
->rcu_head
, css_free_rcu_fn
);
4055 static void init_css(struct cgroup_subsys_state
*css
, struct cgroup_subsys
*ss
,
4056 struct cgroup
*cgrp
)
4063 css
->parent
= cgroup_css(cgrp
->parent
, ss
);
4065 css
->flags
|= CSS_ROOT
;
4067 BUG_ON(cgroup_css(cgrp
, ss
));
4070 /* invoke ->css_online() on a new CSS and mark it online if successful */
4071 static int online_css(struct cgroup_subsys_state
*css
)
4073 struct cgroup_subsys
*ss
= css
->ss
;
4076 lockdep_assert_held(&cgroup_tree_mutex
);
4077 lockdep_assert_held(&cgroup_mutex
);
4080 ret
= ss
->css_online(css
);
4082 css
->flags
|= CSS_ONLINE
;
4083 css
->cgroup
->nr_css
++;
4084 rcu_assign_pointer(css
->cgroup
->subsys
[ss
->id
], css
);
4089 /* if the CSS is online, invoke ->css_offline() on it and mark it offline */
4090 static void offline_css(struct cgroup_subsys_state
*css
)
4092 struct cgroup_subsys
*ss
= css
->ss
;
4094 lockdep_assert_held(&cgroup_tree_mutex
);
4095 lockdep_assert_held(&cgroup_mutex
);
4097 if (!(css
->flags
& CSS_ONLINE
))
4100 if (ss
->css_offline
)
4101 ss
->css_offline(css
);
4103 css
->flags
&= ~CSS_ONLINE
;
4104 css
->cgroup
->nr_css
--;
4105 RCU_INIT_POINTER(css
->cgroup
->subsys
[ss
->id
], css
);
4109 * create_css - create a cgroup_subsys_state
4110 * @cgrp: the cgroup new css will be associated with
4111 * @ss: the subsys of new css
4113 * Create a new css associated with @cgrp - @ss pair. On success, the new
4114 * css is online and installed in @cgrp with all interface files created.
4115 * Returns 0 on success, -errno on failure.
4117 static int create_css(struct cgroup
*cgrp
, struct cgroup_subsys
*ss
)
4119 struct cgroup
*parent
= cgrp
->parent
;
4120 struct cgroup_subsys_state
*css
;
4123 lockdep_assert_held(&cgrp
->dentry
->d_inode
->i_mutex
);
4124 lockdep_assert_held(&cgroup_mutex
);
4126 css
= ss
->css_alloc(cgroup_css(parent
, ss
));
4128 return PTR_ERR(css
);
4130 err
= percpu_ref_init(&css
->refcnt
, css_release
);
4134 init_css(css
, ss
, cgrp
);
4136 err
= cgroup_populate_dir(cgrp
, 1 << ss
->id
);
4140 err
= online_css(css
);
4145 css_get(css
->parent
);
4147 if (ss
->broken_hierarchy
&& !ss
->warned_broken_hierarchy
&&
4149 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",
4150 current
->comm
, current
->pid
, ss
->name
);
4151 if (!strcmp(ss
->name
, "memory"))
4152 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4153 ss
->warned_broken_hierarchy
= true;
4159 percpu_ref_cancel_init(&css
->refcnt
);
4165 * cgroup_create - create a cgroup
4166 * @parent: cgroup that will be parent of the new cgroup
4167 * @dentry: dentry of the new cgroup
4168 * @mode: mode to set on new inode
4170 * Must be called with the mutex on the parent inode held
4172 static long cgroup_create(struct cgroup
*parent
, struct dentry
*dentry
,
4175 struct cgroup
*cgrp
;
4176 struct cgroup_name
*name
;
4177 struct cgroupfs_root
*root
= parent
->root
;
4179 struct cgroup_subsys
*ss
;
4180 struct super_block
*sb
= root
->sb
;
4182 /* allocate the cgroup and its ID, 0 is reserved for the root */
4183 cgrp
= kzalloc(sizeof(*cgrp
), GFP_KERNEL
);
4187 name
= cgroup_alloc_name(dentry
->d_name
.name
);
4192 rcu_assign_pointer(cgrp
->name
, name
);
4194 mutex_lock(&cgroup_tree_mutex
);
4197 * Only live parents can have children. Note that the liveliness
4198 * check isn't strictly necessary because cgroup_mkdir() and
4199 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4200 * anyway so that locking is contained inside cgroup proper and we
4201 * don't get nasty surprises if we ever grow another caller.
4203 if (!cgroup_lock_live_group(parent
)) {
4205 goto err_unlock_tree
;
4209 * Temporarily set the pointer to NULL, so idr_find() won't return
4210 * a half-baked cgroup.
4212 cgrp
->id
= idr_alloc(&root
->cgroup_idr
, NULL
, 1, 0, GFP_KERNEL
);
4218 /* Grab a reference on the superblock so the hierarchy doesn't
4219 * get deleted on unmount if there are child cgroups. This
4220 * can be done outside cgroup_mutex, since the sb can't
4221 * disappear while someone has an open control file on the
4223 cgroup_get_root(root
);
4225 init_cgroup_housekeeping(cgrp
);
4227 dentry
->d_fsdata
= cgrp
;
4228 cgrp
->dentry
= dentry
;
4230 cgrp
->parent
= parent
;
4231 cgrp
->dummy_css
.parent
= &parent
->dummy_css
;
4232 cgrp
->root
= parent
->root
;
4234 if (notify_on_release(parent
))
4235 set_bit(CGRP_NOTIFY_ON_RELEASE
, &cgrp
->flags
);
4237 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN
, &parent
->flags
))
4238 set_bit(CGRP_CPUSET_CLONE_CHILDREN
, &cgrp
->flags
);
4241 * Create directory. cgroup_create_file() returns with the new
4242 * directory locked on success so that it can be populated without
4243 * dropping cgroup_mutex.
4245 err
= cgroup_create_file(dentry
, S_IFDIR
| mode
, sb
);
4248 lockdep_assert_held(&dentry
->d_inode
->i_mutex
);
4250 cgrp
->serial_nr
= cgroup_serial_nr_next
++;
4252 /* allocation complete, commit to creation */
4253 list_add_tail_rcu(&cgrp
->sibling
, &cgrp
->parent
->children
);
4254 root
->number_of_cgroups
++;
4256 /* hold a ref to the parent's dentry */
4260 * @cgrp is now fully operational. If something fails after this
4261 * point, it'll be released via the normal destruction path.
4263 idr_replace(&root
->cgroup_idr
, cgrp
, cgrp
->id
);
4265 err
= cgroup_addrm_files(cgrp
, cgroup_base_files
, true);
4269 /* let's create and online css's */
4270 for_each_subsys(ss
, ssid
) {
4271 if (root
->subsys_mask
& (1 << ssid
)) {
4272 err
= create_css(cgrp
, ss
);
4278 mutex_unlock(&cgroup_mutex
);
4279 mutex_unlock(&cgroup_tree_mutex
);
4280 mutex_unlock(&cgrp
->dentry
->d_inode
->i_mutex
);
4285 idr_remove(&root
->cgroup_idr
, cgrp
->id
);
4286 /* Release the reference count that we took on the superblock */
4287 cgroup_put_root(root
);
4289 mutex_unlock(&cgroup_mutex
);
4291 mutex_unlock(&cgroup_tree_mutex
);
4292 kfree(rcu_dereference_raw(cgrp
->name
));
4298 cgroup_destroy_locked(cgrp
);
4299 mutex_unlock(&cgroup_mutex
);
4300 mutex_unlock(&cgroup_tree_mutex
);
4301 mutex_unlock(&dentry
->d_inode
->i_mutex
);
4305 static int cgroup_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
4307 struct cgroup
*c_parent
= dentry
->d_parent
->d_fsdata
;
4309 /* the vfs holds inode->i_mutex already */
4310 return cgroup_create(c_parent
, dentry
, mode
| S_IFDIR
);
4314 * This is called when the refcnt of a css is confirmed to be killed.
4315 * css_tryget() is now guaranteed to fail.
4317 static void css_killed_work_fn(struct work_struct
*work
)
4319 struct cgroup_subsys_state
*css
=
4320 container_of(work
, struct cgroup_subsys_state
, destroy_work
);
4321 struct cgroup
*cgrp
= css
->cgroup
;
4323 mutex_lock(&cgroup_tree_mutex
);
4324 mutex_lock(&cgroup_mutex
);
4327 * css_tryget() is guaranteed to fail now. Tell subsystems to
4328 * initate destruction.
4333 * If @cgrp is marked dead, it's waiting for refs of all css's to
4334 * be disabled before proceeding to the second phase of cgroup
4335 * destruction. If we are the last one, kick it off.
4337 if (!cgrp
->nr_css
&& cgroup_is_dead(cgrp
))
4338 cgroup_destroy_css_killed(cgrp
);
4340 mutex_unlock(&cgroup_mutex
);
4341 mutex_unlock(&cgroup_tree_mutex
);
4344 * Put the css refs from kill_css(). Each css holds an extra
4345 * reference to the cgroup's dentry and cgroup removal proceeds
4346 * regardless of css refs. On the last put of each css, whenever
4347 * that may be, the extra dentry ref is put so that dentry
4348 * destruction happens only after all css's are released.
4353 /* css kill confirmation processing requires process context, bounce */
4354 static void css_killed_ref_fn(struct percpu_ref
*ref
)
4356 struct cgroup_subsys_state
*css
=
4357 container_of(ref
, struct cgroup_subsys_state
, refcnt
);
4359 INIT_WORK(&css
->destroy_work
, css_killed_work_fn
);
4360 queue_work(cgroup_destroy_wq
, &css
->destroy_work
);
4364 * kill_css - destroy a css
4365 * @css: css to destroy
4367 * This function initiates destruction of @css by removing cgroup interface
4368 * files and putting its base reference. ->css_offline() will be invoked
4369 * asynchronously once css_tryget() is guaranteed to fail and when the
4370 * reference count reaches zero, @css will be released.
4372 static void kill_css(struct cgroup_subsys_state
*css
)
4374 cgroup_clear_dir(css
->cgroup
, 1 << css
->ss
->id
);
4377 * Killing would put the base ref, but we need to keep it alive
4378 * until after ->css_offline().
4383 * cgroup core guarantees that, by the time ->css_offline() is
4384 * invoked, no new css reference will be given out via
4385 * css_tryget(). We can't simply call percpu_ref_kill() and
4386 * proceed to offlining css's because percpu_ref_kill() doesn't
4387 * guarantee that the ref is seen as killed on all CPUs on return.
4389 * Use percpu_ref_kill_and_confirm() to get notifications as each
4390 * css is confirmed to be seen as killed on all CPUs.
4392 percpu_ref_kill_and_confirm(&css
->refcnt
, css_killed_ref_fn
);
4396 * cgroup_destroy_locked - the first stage of cgroup destruction
4397 * @cgrp: cgroup to be destroyed
4399 * css's make use of percpu refcnts whose killing latency shouldn't be
4400 * exposed to userland and are RCU protected. Also, cgroup core needs to
4401 * guarantee that css_tryget() won't succeed by the time ->css_offline() is
4402 * invoked. To satisfy all the requirements, destruction is implemented in
4403 * the following two steps.
4405 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4406 * userland visible parts and start killing the percpu refcnts of
4407 * css's. Set up so that the next stage will be kicked off once all
4408 * the percpu refcnts are confirmed to be killed.
4410 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4411 * rest of destruction. Once all cgroup references are gone, the
4412 * cgroup is RCU-freed.
4414 * This function implements s1. After this step, @cgrp is gone as far as
4415 * the userland is concerned and a new cgroup with the same name may be
4416 * created. As cgroup doesn't care about the names internally, this
4417 * doesn't cause any problem.
4419 static int cgroup_destroy_locked(struct cgroup
*cgrp
)
4420 __releases(&cgroup_mutex
) __acquires(&cgroup_mutex
)
4422 struct dentry
*d
= cgrp
->dentry
;
4423 struct cgroup_subsys_state
*css
;
4424 struct cgroup
*child
;
4428 lockdep_assert_held(&d
->d_inode
->i_mutex
);
4429 lockdep_assert_held(&cgroup_tree_mutex
);
4430 lockdep_assert_held(&cgroup_mutex
);
4433 * css_set_lock synchronizes access to ->cset_links and prevents
4434 * @cgrp from being removed while __put_css_set() is in progress.
4436 read_lock(&css_set_lock
);
4437 empty
= list_empty(&cgrp
->cset_links
);
4438 read_unlock(&css_set_lock
);
4443 * Make sure there's no live children. We can't test ->children
4444 * emptiness as dead children linger on it while being destroyed;
4445 * otherwise, "rmdir parent/child parent" may fail with -EBUSY.
4449 list_for_each_entry_rcu(child
, &cgrp
->children
, sibling
) {
4450 empty
= cgroup_is_dead(child
);
4459 * Initiate massacre of all css's. cgroup_destroy_css_killed()
4460 * will be invoked to perform the rest of destruction once the
4461 * percpu refs of all css's are confirmed to be killed. This
4462 * involves removing the subsystem's files, drop cgroup_mutex.
4464 mutex_unlock(&cgroup_mutex
);
4465 for_each_css(css
, ssid
, cgrp
)
4467 mutex_lock(&cgroup_mutex
);
4470 * Mark @cgrp dead. This prevents further task migration and child
4471 * creation by disabling cgroup_lock_live_group(). Note that
4472 * CGRP_DEAD assertion is depended upon by css_next_child() to
4473 * resume iteration after dropping RCU read lock. See
4474 * css_next_child() for details.
4476 set_bit(CGRP_DEAD
, &cgrp
->flags
);
4478 /* CGRP_DEAD is set, remove from ->release_list for the last time */
4479 raw_spin_lock(&release_list_lock
);
4480 if (!list_empty(&cgrp
->release_list
))
4481 list_del_init(&cgrp
->release_list
);
4482 raw_spin_unlock(&release_list_lock
);
4485 * If @cgrp has css's attached, the second stage of cgroup
4486 * destruction is kicked off from css_killed_work_fn() after the
4487 * refs of all attached css's are killed. If @cgrp doesn't have
4488 * any css, we kick it off here.
4491 cgroup_destroy_css_killed(cgrp
);
4494 * Clear the base files and remove @cgrp directory. The removal
4495 * puts the base ref but we aren't quite done with @cgrp yet, so
4498 mutex_unlock(&cgroup_mutex
);
4499 cgroup_addrm_files(cgrp
, cgroup_base_files
, false);
4501 cgroup_d_remove_dir(d
);
4502 mutex_lock(&cgroup_mutex
);
4508 * cgroup_destroy_css_killed - the second step of cgroup destruction
4509 * @work: cgroup->destroy_free_work
4511 * This function is invoked from a work item for a cgroup which is being
4512 * destroyed after all css's are offlined and performs the rest of
4513 * destruction. This is the second step of destruction described in the
4514 * comment above cgroup_destroy_locked().
4516 static void cgroup_destroy_css_killed(struct cgroup
*cgrp
)
4518 struct cgroup
*parent
= cgrp
->parent
;
4520 lockdep_assert_held(&cgroup_tree_mutex
);
4521 lockdep_assert_held(&cgroup_mutex
);
4523 /* delete this cgroup from parent->children */
4524 list_del_rcu(&cgrp
->sibling
);
4528 set_bit(CGRP_RELEASABLE
, &parent
->flags
);
4529 check_for_release(parent
);
4532 static int cgroup_rmdir(struct inode
*unused_dir
, struct dentry
*dentry
)
4536 mutex_lock(&cgroup_tree_mutex
);
4537 mutex_lock(&cgroup_mutex
);
4538 ret
= cgroup_destroy_locked(dentry
->d_fsdata
);
4539 mutex_unlock(&cgroup_mutex
);
4540 mutex_unlock(&cgroup_tree_mutex
);
4545 static void __init
cgroup_init_subsys(struct cgroup_subsys
*ss
)
4547 struct cgroup_subsys_state
*css
;
4549 printk(KERN_INFO
"Initializing cgroup subsys %s\n", ss
->name
);
4551 mutex_lock(&cgroup_tree_mutex
);
4552 mutex_lock(&cgroup_mutex
);
4554 INIT_LIST_HEAD(&ss
->cftsets
);
4556 /* Create the top cgroup state for this subsystem */
4557 ss
->root
= &cgroup_dummy_root
;
4558 css
= ss
->css_alloc(cgroup_css(cgroup_dummy_top
, ss
));
4559 /* We don't handle early failures gracefully */
4560 BUG_ON(IS_ERR(css
));
4561 init_css(css
, ss
, cgroup_dummy_top
);
4563 /* Update the init_css_set to contain a subsys
4564 * pointer to this state - since the subsystem is
4565 * newly registered, all tasks and hence the
4566 * init_css_set is in the subsystem's top cgroup. */
4567 init_css_set
.subsys
[ss
->id
] = css
;
4569 need_forkexit_callback
|= ss
->fork
|| ss
->exit
;
4571 /* At system boot, before all subsystems have been
4572 * registered, no tasks have been forked, so we don't
4573 * need to invoke fork callbacks here. */
4574 BUG_ON(!list_empty(&init_task
.tasks
));
4576 BUG_ON(online_css(css
));
4578 mutex_unlock(&cgroup_mutex
);
4579 mutex_unlock(&cgroup_tree_mutex
);
4583 * cgroup_init_early - cgroup initialization at system boot
4585 * Initialize cgroups at system boot, and initialize any
4586 * subsystems that request early init.
4588 int __init
cgroup_init_early(void)
4590 struct cgroup_subsys
*ss
;
4593 atomic_set(&init_css_set
.refcount
, 1);
4594 INIT_LIST_HEAD(&init_css_set
.cgrp_links
);
4595 INIT_LIST_HEAD(&init_css_set
.tasks
);
4596 INIT_HLIST_NODE(&init_css_set
.hlist
);
4598 init_cgroup_root(&cgroup_dummy_root
);
4599 cgroup_root_count
= 1;
4600 RCU_INIT_POINTER(init_task
.cgroups
, &init_css_set
);
4602 init_cgrp_cset_link
.cset
= &init_css_set
;
4603 init_cgrp_cset_link
.cgrp
= cgroup_dummy_top
;
4604 list_add(&init_cgrp_cset_link
.cset_link
, &cgroup_dummy_top
->cset_links
);
4605 list_add(&init_cgrp_cset_link
.cgrp_link
, &init_css_set
.cgrp_links
);
4607 for_each_subsys(ss
, i
) {
4608 WARN(!ss
->css_alloc
|| !ss
->css_free
|| ss
->name
|| ss
->id
,
4609 "invalid cgroup_subsys %d:%s css_alloc=%p css_free=%p name:id=%d:%s\n",
4610 i
, cgroup_subsys_name
[i
], ss
->css_alloc
, ss
->css_free
,
4612 WARN(strlen(cgroup_subsys_name
[i
]) > MAX_CGROUP_TYPE_NAMELEN
,
4613 "cgroup_subsys_name %s too long\n", cgroup_subsys_name
[i
]);
4616 ss
->name
= cgroup_subsys_name
[i
];
4619 cgroup_init_subsys(ss
);
4625 * cgroup_init - cgroup initialization
4627 * Register cgroup filesystem and /proc file, and initialize
4628 * any subsystems that didn't request early init.
4630 int __init
cgroup_init(void)
4632 struct cgroup_subsys
*ss
;
4636 err
= bdi_init(&cgroup_backing_dev_info
);
4640 cgroup_init_cftypes(NULL
, cgroup_base_files
);
4642 for_each_subsys(ss
, i
) {
4643 if (!ss
->early_init
)
4644 cgroup_init_subsys(ss
);
4647 * cftype registration needs kmalloc and can't be done
4648 * during early_init. Register base cftypes separately.
4650 if (ss
->base_cftypes
)
4651 WARN_ON(cgroup_add_cftypes(ss
, ss
->base_cftypes
));
4654 /* allocate id for the dummy hierarchy */
4655 mutex_lock(&cgroup_mutex
);
4657 /* Add init_css_set to the hash table */
4658 key
= css_set_hash(init_css_set
.subsys
);
4659 hash_add(css_set_table
, &init_css_set
.hlist
, key
);
4661 BUG_ON(cgroup_init_root_id(&cgroup_dummy_root
, 0, 1));
4663 err
= idr_alloc(&cgroup_dummy_root
.cgroup_idr
, cgroup_dummy_top
,
4667 mutex_unlock(&cgroup_mutex
);
4669 cgroup_kobj
= kobject_create_and_add("cgroup", fs_kobj
);
4675 err
= register_filesystem(&cgroup_fs_type
);
4677 kobject_put(cgroup_kobj
);
4681 proc_create("cgroups", 0, NULL
, &proc_cgroupstats_operations
);
4685 bdi_destroy(&cgroup_backing_dev_info
);
4690 static int __init
cgroup_wq_init(void)
4693 * There isn't much point in executing destruction path in
4694 * parallel. Good chunk is serialized with cgroup_mutex anyway.
4696 * XXX: Must be ordered to make sure parent is offlined after
4697 * children. The ordering requirement is for memcg where a
4698 * parent's offline may wait for a child's leading to deadlock. In
4699 * the long term, this should be fixed from memcg side.
4701 * We would prefer to do this in cgroup_init() above, but that
4702 * is called before init_workqueues(): so leave this until after.
4704 cgroup_destroy_wq
= alloc_ordered_workqueue("cgroup_destroy", 0);
4705 BUG_ON(!cgroup_destroy_wq
);
4708 * Used to destroy pidlists and separate to serve as flush domain.
4709 * Cap @max_active to 1 too.
4711 cgroup_pidlist_destroy_wq
= alloc_workqueue("cgroup_pidlist_destroy",
4713 BUG_ON(!cgroup_pidlist_destroy_wq
);
4717 core_initcall(cgroup_wq_init
);
4720 * proc_cgroup_show()
4721 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4722 * - Used for /proc/<pid>/cgroup.
4723 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4724 * doesn't really matter if tsk->cgroup changes after we read it,
4725 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
4726 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4727 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4728 * cgroup to top_cgroup.
4731 /* TODO: Use a proper seq_file iterator */
4732 int proc_cgroup_show(struct seq_file
*m
, void *v
)
4735 struct task_struct
*tsk
;
4738 struct cgroupfs_root
*root
;
4741 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
4747 tsk
= get_pid_task(pid
, PIDTYPE_PID
);
4753 mutex_lock(&cgroup_mutex
);
4755 for_each_active_root(root
) {
4756 struct cgroup_subsys
*ss
;
4757 struct cgroup
*cgrp
;
4758 int ssid
, count
= 0;
4760 seq_printf(m
, "%d:", root
->hierarchy_id
);
4761 for_each_subsys(ss
, ssid
)
4762 if (root
->subsys_mask
& (1 << ssid
))
4763 seq_printf(m
, "%s%s", count
++ ? "," : "", ss
->name
);
4764 if (strlen(root
->name
))
4765 seq_printf(m
, "%sname=%s", count
? "," : "",
4768 cgrp
= task_cgroup_from_root(tsk
, root
);
4769 retval
= cgroup_path(cgrp
, buf
, PAGE_SIZE
);
4777 mutex_unlock(&cgroup_mutex
);
4778 put_task_struct(tsk
);
4785 /* Display information about each subsystem and each hierarchy */
4786 static int proc_cgroupstats_show(struct seq_file
*m
, void *v
)
4788 struct cgroup_subsys
*ss
;
4791 seq_puts(m
, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
4793 * ideally we don't want subsystems moving around while we do this.
4794 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4795 * subsys/hierarchy state.
4797 mutex_lock(&cgroup_mutex
);
4799 for_each_subsys(ss
, i
)
4800 seq_printf(m
, "%s\t%d\t%d\t%d\n",
4801 ss
->name
, ss
->root
->hierarchy_id
,
4802 ss
->root
->number_of_cgroups
, !ss
->disabled
);
4804 mutex_unlock(&cgroup_mutex
);
4808 static int cgroupstats_open(struct inode
*inode
, struct file
*file
)
4810 return single_open(file
, proc_cgroupstats_show
, NULL
);
4813 static const struct file_operations proc_cgroupstats_operations
= {
4814 .open
= cgroupstats_open
,
4816 .llseek
= seq_lseek
,
4817 .release
= single_release
,
4821 * cgroup_fork - attach newly forked task to its parents cgroup.
4822 * @child: pointer to task_struct of forking parent process.
4824 * Description: A task inherits its parent's cgroup at fork().
4826 * A pointer to the shared css_set was automatically copied in
4827 * fork.c by dup_task_struct(). However, we ignore that copy, since
4828 * it was not made under the protection of RCU or cgroup_mutex, so
4829 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
4830 * have already changed current->cgroups, allowing the previously
4831 * referenced cgroup group to be removed and freed.
4833 * At the point that cgroup_fork() is called, 'current' is the parent
4834 * task, and the passed argument 'child' points to the child task.
4836 void cgroup_fork(struct task_struct
*child
)
4839 get_css_set(task_css_set(current
));
4840 child
->cgroups
= current
->cgroups
;
4841 task_unlock(current
);
4842 INIT_LIST_HEAD(&child
->cg_list
);
4846 * cgroup_post_fork - called on a new task after adding it to the task list
4847 * @child: the task in question
4849 * Adds the task to the list running through its css_set if necessary and
4850 * call the subsystem fork() callbacks. Has to be after the task is
4851 * visible on the task list in case we race with the first call to
4852 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
4855 void cgroup_post_fork(struct task_struct
*child
)
4857 struct cgroup_subsys
*ss
;
4861 * use_task_css_set_links is set to 1 before we walk the tasklist
4862 * under the tasklist_lock and we read it here after we added the child
4863 * to the tasklist under the tasklist_lock as well. If the child wasn't
4864 * yet in the tasklist when we walked through it from
4865 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
4866 * should be visible now due to the paired locking and barriers implied
4867 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
4868 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
4871 if (use_task_css_set_links
) {
4872 write_lock(&css_set_lock
);
4874 if (list_empty(&child
->cg_list
))
4875 list_add(&child
->cg_list
, &task_css_set(child
)->tasks
);
4877 write_unlock(&css_set_lock
);
4881 * Call ss->fork(). This must happen after @child is linked on
4882 * css_set; otherwise, @child might change state between ->fork()
4883 * and addition to css_set.
4885 if (need_forkexit_callback
) {
4886 for_each_subsys(ss
, i
)
4893 * cgroup_exit - detach cgroup from exiting task
4894 * @tsk: pointer to task_struct of exiting process
4895 * @run_callback: run exit callbacks?
4897 * Description: Detach cgroup from @tsk and release it.
4899 * Note that cgroups marked notify_on_release force every task in
4900 * them to take the global cgroup_mutex mutex when exiting.
4901 * This could impact scaling on very large systems. Be reluctant to
4902 * use notify_on_release cgroups where very high task exit scaling
4903 * is required on large systems.
4905 * the_top_cgroup_hack:
4907 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
4909 * We call cgroup_exit() while the task is still competent to
4910 * handle notify_on_release(), then leave the task attached to the
4911 * root cgroup in each hierarchy for the remainder of its exit.
4913 * To do this properly, we would increment the reference count on
4914 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
4915 * code we would add a second cgroup function call, to drop that
4916 * reference. This would just create an unnecessary hot spot on
4917 * the top_cgroup reference count, to no avail.
4919 * Normally, holding a reference to a cgroup without bumping its
4920 * count is unsafe. The cgroup could go away, or someone could
4921 * attach us to a different cgroup, decrementing the count on
4922 * the first cgroup that we never incremented. But in this case,
4923 * top_cgroup isn't going away, and either task has PF_EXITING set,
4924 * which wards off any cgroup_attach_task() attempts, or task is a failed
4925 * fork, never visible to cgroup_attach_task.
4927 void cgroup_exit(struct task_struct
*tsk
, int run_callbacks
)
4929 struct cgroup_subsys
*ss
;
4930 struct css_set
*cset
;
4934 * Unlink from the css_set task list if necessary.
4935 * Optimistically check cg_list before taking
4938 if (!list_empty(&tsk
->cg_list
)) {
4939 write_lock(&css_set_lock
);
4940 if (!list_empty(&tsk
->cg_list
))
4941 list_del_init(&tsk
->cg_list
);
4942 write_unlock(&css_set_lock
);
4945 /* Reassign the task to the init_css_set. */
4947 cset
= task_css_set(tsk
);
4948 RCU_INIT_POINTER(tsk
->cgroups
, &init_css_set
);
4950 if (run_callbacks
&& need_forkexit_callback
) {
4951 /* see cgroup_post_fork() for details */
4952 for_each_subsys(ss
, i
) {
4954 struct cgroup_subsys_state
*old_css
= cset
->subsys
[i
];
4955 struct cgroup_subsys_state
*css
= task_css(tsk
, i
);
4957 ss
->exit(css
, old_css
, tsk
);
4963 put_css_set_taskexit(cset
);
4966 static void check_for_release(struct cgroup
*cgrp
)
4968 if (cgroup_is_releasable(cgrp
) &&
4969 list_empty(&cgrp
->cset_links
) && list_empty(&cgrp
->children
)) {
4971 * Control Group is currently removeable. If it's not
4972 * already queued for a userspace notification, queue
4975 int need_schedule_work
= 0;
4977 raw_spin_lock(&release_list_lock
);
4978 if (!cgroup_is_dead(cgrp
) &&
4979 list_empty(&cgrp
->release_list
)) {
4980 list_add(&cgrp
->release_list
, &release_list
);
4981 need_schedule_work
= 1;
4983 raw_spin_unlock(&release_list_lock
);
4984 if (need_schedule_work
)
4985 schedule_work(&release_agent_work
);
4990 * Notify userspace when a cgroup is released, by running the
4991 * configured release agent with the name of the cgroup (path
4992 * relative to the root of cgroup file system) as the argument.
4994 * Most likely, this user command will try to rmdir this cgroup.
4996 * This races with the possibility that some other task will be
4997 * attached to this cgroup before it is removed, or that some other
4998 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
4999 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5000 * unused, and this cgroup will be reprieved from its death sentence,
5001 * to continue to serve a useful existence. Next time it's released,
5002 * we will get notified again, if it still has 'notify_on_release' set.
5004 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5005 * means only wait until the task is successfully execve()'d. The
5006 * separate release agent task is forked by call_usermodehelper(),
5007 * then control in this thread returns here, without waiting for the
5008 * release agent task. We don't bother to wait because the caller of
5009 * this routine has no use for the exit status of the release agent
5010 * task, so no sense holding our caller up for that.
5012 static void cgroup_release_agent(struct work_struct
*work
)
5014 BUG_ON(work
!= &release_agent_work
);
5015 mutex_lock(&cgroup_mutex
);
5016 raw_spin_lock(&release_list_lock
);
5017 while (!list_empty(&release_list
)) {
5018 char *argv
[3], *envp
[3];
5020 char *pathbuf
= NULL
, *agentbuf
= NULL
;
5021 struct cgroup
*cgrp
= list_entry(release_list
.next
,
5024 list_del_init(&cgrp
->release_list
);
5025 raw_spin_unlock(&release_list_lock
);
5026 pathbuf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
5029 if (cgroup_path(cgrp
, pathbuf
, PAGE_SIZE
) < 0)
5031 agentbuf
= kstrdup(cgrp
->root
->release_agent_path
, GFP_KERNEL
);
5036 argv
[i
++] = agentbuf
;
5037 argv
[i
++] = pathbuf
;
5041 /* minimal command environment */
5042 envp
[i
++] = "HOME=/";
5043 envp
[i
++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5046 /* Drop the lock while we invoke the usermode helper,
5047 * since the exec could involve hitting disk and hence
5048 * be a slow process */
5049 mutex_unlock(&cgroup_mutex
);
5050 call_usermodehelper(argv
[0], argv
, envp
, UMH_WAIT_EXEC
);
5051 mutex_lock(&cgroup_mutex
);
5055 raw_spin_lock(&release_list_lock
);
5057 raw_spin_unlock(&release_list_lock
);
5058 mutex_unlock(&cgroup_mutex
);
5061 static int __init
cgroup_disable(char *str
)
5063 struct cgroup_subsys
*ss
;
5067 while ((token
= strsep(&str
, ",")) != NULL
) {
5071 for_each_subsys(ss
, i
) {
5072 if (!strcmp(token
, ss
->name
)) {
5074 printk(KERN_INFO
"Disabling %s control group"
5075 " subsystem\n", ss
->name
);
5082 __setup("cgroup_disable=", cgroup_disable
);
5085 * css_tryget_from_dir - get corresponding css from the dentry of a cgroup dir
5086 * @dentry: directory dentry of interest
5087 * @ss: subsystem of interest
5089 * If @dentry is a directory for a cgroup which has @ss enabled on it, try
5090 * to get the corresponding css and return it. If such css doesn't exist
5091 * or can't be pinned, an ERR_PTR value is returned.
5093 struct cgroup_subsys_state
*css_tryget_from_dir(struct dentry
*dentry
,
5094 struct cgroup_subsys
*ss
)
5096 struct cgroup
*cgrp
;
5097 struct cgroup_subsys_state
*css
;
5099 /* is @dentry a cgroup dir? */
5100 if (!dentry
->d_inode
||
5101 dentry
->d_inode
->i_op
!= &cgroup_dir_inode_operations
)
5102 return ERR_PTR(-EBADF
);
5106 cgrp
= __d_cgrp(dentry
);
5107 css
= cgroup_css(cgrp
, ss
);
5109 if (!css
|| !css_tryget(css
))
5110 css
= ERR_PTR(-ENOENT
);
5117 * css_from_id - lookup css by id
5118 * @id: the cgroup id
5119 * @ss: cgroup subsys to be looked into
5121 * Returns the css if there's valid one with @id, otherwise returns NULL.
5122 * Should be called under rcu_read_lock().
5124 struct cgroup_subsys_state
*css_from_id(int id
, struct cgroup_subsys
*ss
)
5126 struct cgroup
*cgrp
;
5128 cgroup_assert_mutexes_or_rcu_locked();
5130 cgrp
= idr_find(&ss
->root
->cgroup_idr
, id
);
5132 return cgroup_css(cgrp
, ss
);
5136 #ifdef CONFIG_CGROUP_DEBUG
5137 static struct cgroup_subsys_state
*
5138 debug_css_alloc(struct cgroup_subsys_state
*parent_css
)
5140 struct cgroup_subsys_state
*css
= kzalloc(sizeof(*css
), GFP_KERNEL
);
5143 return ERR_PTR(-ENOMEM
);
5148 static void debug_css_free(struct cgroup_subsys_state
*css
)
5153 static u64
debug_taskcount_read(struct cgroup_subsys_state
*css
,
5156 return cgroup_task_count(css
->cgroup
);
5159 static u64
current_css_set_read(struct cgroup_subsys_state
*css
,
5162 return (u64
)(unsigned long)current
->cgroups
;
5165 static u64
current_css_set_refcount_read(struct cgroup_subsys_state
*css
,
5171 count
= atomic_read(&task_css_set(current
)->refcount
);
5176 static int current_css_set_cg_links_read(struct seq_file
*seq
, void *v
)
5178 struct cgrp_cset_link
*link
;
5179 struct css_set
*cset
;
5181 read_lock(&css_set_lock
);
5183 cset
= rcu_dereference(current
->cgroups
);
5184 list_for_each_entry(link
, &cset
->cgrp_links
, cgrp_link
) {
5185 struct cgroup
*c
= link
->cgrp
;
5186 const char *name
= "?";
5188 if (c
!= cgroup_dummy_top
)
5189 name
= cgroup_name(c
);
5191 seq_printf(seq
, "Root %d group %s\n",
5192 c
->root
->hierarchy_id
, name
);
5195 read_unlock(&css_set_lock
);
5199 #define MAX_TASKS_SHOWN_PER_CSS 25
5200 static int cgroup_css_links_read(struct seq_file
*seq
, void *v
)
5202 struct cgroup_subsys_state
*css
= seq_css(seq
);
5203 struct cgrp_cset_link
*link
;
5205 read_lock(&css_set_lock
);
5206 list_for_each_entry(link
, &css
->cgroup
->cset_links
, cset_link
) {
5207 struct css_set
*cset
= link
->cset
;
5208 struct task_struct
*task
;
5210 seq_printf(seq
, "css_set %p\n", cset
);
5211 list_for_each_entry(task
, &cset
->tasks
, cg_list
) {
5212 if (count
++ > MAX_TASKS_SHOWN_PER_CSS
) {
5213 seq_puts(seq
, " ...\n");
5216 seq_printf(seq
, " task %d\n",
5217 task_pid_vnr(task
));
5221 read_unlock(&css_set_lock
);
5225 static u64
releasable_read(struct cgroup_subsys_state
*css
, struct cftype
*cft
)
5227 return test_bit(CGRP_RELEASABLE
, &css
->cgroup
->flags
);
5230 static struct cftype debug_files
[] = {
5232 .name
= "taskcount",
5233 .read_u64
= debug_taskcount_read
,
5237 .name
= "current_css_set",
5238 .read_u64
= current_css_set_read
,
5242 .name
= "current_css_set_refcount",
5243 .read_u64
= current_css_set_refcount_read
,
5247 .name
= "current_css_set_cg_links",
5248 .seq_show
= current_css_set_cg_links_read
,
5252 .name
= "cgroup_css_links",
5253 .seq_show
= cgroup_css_links_read
,
5257 .name
= "releasable",
5258 .read_u64
= releasable_read
,
5264 struct cgroup_subsys debug_cgrp_subsys
= {
5265 .css_alloc
= debug_css_alloc
,
5266 .css_free
= debug_css_free
,
5267 .base_cftypes
= debug_files
,
5269 #endif /* CONFIG_CGROUP_DEBUG */