2 * Generic process-grouping system.
4 * Based originally on the cpuset system, extracted by Paul Menage
5 * Copyright (C) 2006 Google, Inc
7 * Notifications support
8 * Copyright (C) 2009 Nokia Corporation
9 * Author: Kirill A. Shutemov
11 * Copyright notices from the original cpuset code:
12 * --------------------------------------------------
13 * Copyright (C) 2003 BULL SA.
14 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
16 * Portions derived from Patrick Mochel's sysfs code.
17 * sysfs is Copyright (c) 2001-3 Patrick Mochel
19 * 2003-10-10 Written by Simon Derr.
20 * 2003-10-22 Updates by Stephen Hemminger.
21 * 2004 May-July Rework by Paul Jackson.
22 * ---------------------------------------------------
24 * This file is subject to the terms and conditions of the GNU General Public
25 * License. See the file COPYING in the main directory of the Linux
26 * distribution for more details.
29 #include <linux/cgroup.h>
30 #include <linux/cred.h>
31 #include <linux/ctype.h>
32 #include <linux/errno.h>
33 #include <linux/init_task.h>
34 #include <linux/kernel.h>
35 #include <linux/list.h>
37 #include <linux/mutex.h>
38 #include <linux/mount.h>
39 #include <linux/pagemap.h>
40 #include <linux/proc_fs.h>
41 #include <linux/rcupdate.h>
42 #include <linux/sched.h>
43 #include <linux/backing-dev.h>
44 #include <linux/seq_file.h>
45 #include <linux/slab.h>
46 #include <linux/magic.h>
47 #include <linux/spinlock.h>
48 #include <linux/string.h>
49 #include <linux/sort.h>
50 #include <linux/kmod.h>
51 #include <linux/module.h>
52 #include <linux/delayacct.h>
53 #include <linux/cgroupstats.h>
54 #include <linux/hashtable.h>
55 #include <linux/namei.h>
56 #include <linux/pid_namespace.h>
57 #include <linux/idr.h>
58 #include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
59 #include <linux/eventfd.h>
60 #include <linux/poll.h>
61 #include <linux/flex_array.h> /* used in cgroup_attach_task */
62 #include <linux/kthread.h>
64 #include <linux/atomic.h>
67 * cgroup_mutex is the master lock. Any modification to cgroup or its
68 * hierarchy must be performed while holding it.
70 * cgroup_root_mutex nests inside cgroup_mutex and should be held to modify
71 * cgroupfs_root of any cgroup hierarchy - subsys list, flags,
72 * release_agent_path and so on. Modifying requires both cgroup_mutex and
73 * cgroup_root_mutex. Readers can acquire either of the two. This is to
74 * break the following locking order cycle.
76 * A. cgroup_mutex -> cred_guard_mutex -> s_type->i_mutex_key -> namespace_sem
77 * B. namespace_sem -> cgroup_mutex
79 * B happens only through cgroup_show_options() and using cgroup_root_mutex
82 #ifdef CONFIG_PROVE_RCU
83 DEFINE_MUTEX(cgroup_mutex
);
84 EXPORT_SYMBOL_GPL(cgroup_mutex
); /* only for task_subsys_state_check() */
86 static DEFINE_MUTEX(cgroup_mutex
);
89 static DEFINE_MUTEX(cgroup_root_mutex
);
92 * Generate an array of cgroup subsystem pointers. At boot time, this is
93 * populated with the built in subsystems, and modular subsystems are
94 * registered after that. The mutable section of this array is protected by
97 #define SUBSYS(_x) [_x ## _subsys_id] = &_x ## _subsys,
98 #define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option)
99 static struct cgroup_subsys
*cgroup_subsys
[CGROUP_SUBSYS_COUNT
] = {
100 #include <linux/cgroup_subsys.h>
104 * The dummy hierarchy, reserved for the subsystems that are otherwise
105 * unattached - it never has more than a single cgroup, and all tasks are
106 * part of that cgroup.
108 static struct cgroupfs_root cgroup_dummy_root
;
110 /* dummy_top is a shorthand for the dummy hierarchy's top cgroup */
111 static struct cgroup
* const cgroup_dummy_top
= &cgroup_dummy_root
.top_cgroup
;
114 * cgroupfs file entry, pointed to from leaf dentry->d_fsdata.
117 struct list_head node
;
118 struct dentry
*dentry
;
122 struct simple_xattrs xattrs
;
126 * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when
127 * cgroup_subsys->use_id != 0.
129 #define CSS_ID_MAX (65535)
132 * The css to which this ID points. This pointer is set to valid value
133 * after cgroup is populated. If cgroup is removed, this will be NULL.
134 * This pointer is expected to be RCU-safe because destroy()
135 * is called after synchronize_rcu(). But for safe use, css_tryget()
136 * should be used for avoiding race.
138 struct cgroup_subsys_state __rcu
*css
;
144 * Depth in hierarchy which this ID belongs to.
146 unsigned short depth
;
148 * ID is freed by RCU. (and lookup routine is RCU safe.)
150 struct rcu_head rcu_head
;
152 * Hierarchy of CSS ID belongs to.
154 unsigned short stack
[0]; /* Array of Length (depth+1) */
158 * cgroup_event represents events which userspace want to receive.
160 struct cgroup_event
{
162 * Cgroup which the event belongs to.
166 * Control file which the event associated.
170 * eventfd to signal userspace about the event.
172 struct eventfd_ctx
*eventfd
;
174 * Each of these stored in a list by the cgroup.
176 struct list_head list
;
178 * All fields below needed to unregister event when
179 * userspace closes eventfd.
182 wait_queue_head_t
*wqh
;
184 struct work_struct remove
;
187 /* The list of hierarchy roots */
189 static LIST_HEAD(cgroup_roots
);
190 static int cgroup_root_count
;
193 * Hierarchy ID allocation and mapping. It follows the same exclusion
194 * rules as other root ops - both cgroup_mutex and cgroup_root_mutex for
195 * writes, either for reads.
197 static DEFINE_IDR(cgroup_hierarchy_idr
);
199 static struct cgroup_name root_cgroup_name
= { .name
= "/" };
202 * Assign a monotonically increasing serial number to cgroups. It
203 * guarantees cgroups with bigger numbers are newer than those with smaller
204 * numbers. Also, as cgroups are always appended to the parent's
205 * ->children list, it guarantees that sibling cgroups are always sorted in
206 * the ascending serial number order on the list. Protected by
209 static u64 cgroup_serial_nr_next
= 1;
211 /* This flag indicates whether tasks in the fork and exit paths should
212 * check for fork/exit handlers to call. This avoids us having to do
213 * extra work in the fork/exit path if none of the subsystems need to
216 static int need_forkexit_callback __read_mostly
;
218 static void cgroup_offline_fn(struct work_struct
*work
);
219 static int cgroup_destroy_locked(struct cgroup
*cgrp
);
220 static int cgroup_addrm_files(struct cgroup
*cgrp
, struct cgroup_subsys
*subsys
,
221 struct cftype cfts
[], bool is_add
);
223 /* convenient tests for these bits */
224 static inline bool cgroup_is_dead(const struct cgroup
*cgrp
)
226 return test_bit(CGRP_DEAD
, &cgrp
->flags
);
230 * cgroup_is_descendant - test ancestry
231 * @cgrp: the cgroup to be tested
232 * @ancestor: possible ancestor of @cgrp
234 * Test whether @cgrp is a descendant of @ancestor. It also returns %true
235 * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
236 * and @ancestor are accessible.
238 bool cgroup_is_descendant(struct cgroup
*cgrp
, struct cgroup
*ancestor
)
241 if (cgrp
== ancestor
)
247 EXPORT_SYMBOL_GPL(cgroup_is_descendant
);
249 static int cgroup_is_releasable(const struct cgroup
*cgrp
)
252 (1 << CGRP_RELEASABLE
) |
253 (1 << CGRP_NOTIFY_ON_RELEASE
);
254 return (cgrp
->flags
& bits
) == bits
;
257 static int notify_on_release(const struct cgroup
*cgrp
)
259 return test_bit(CGRP_NOTIFY_ON_RELEASE
, &cgrp
->flags
);
263 * for_each_subsys - iterate all loaded cgroup subsystems
264 * @ss: the iteration cursor
265 * @i: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
267 * Should be called under cgroup_mutex.
269 #define for_each_subsys(ss, i) \
270 for ((i) = 0; (i) < CGROUP_SUBSYS_COUNT; (i)++) \
271 if (({ lockdep_assert_held(&cgroup_mutex); \
272 !((ss) = cgroup_subsys[i]); })) { } \
276 * for_each_builtin_subsys - iterate all built-in cgroup subsystems
277 * @ss: the iteration cursor
278 * @i: the index of @ss, CGROUP_BUILTIN_SUBSYS_COUNT after reaching the end
280 * Bulit-in subsystems are always present and iteration itself doesn't
281 * require any synchronization.
283 #define for_each_builtin_subsys(ss, i) \
284 for ((i) = 0; (i) < CGROUP_BUILTIN_SUBSYS_COUNT && \
285 (((ss) = cgroup_subsys[i]) || true); (i)++)
287 /* iterate each subsystem attached to a hierarchy */
288 #define for_each_root_subsys(root, ss) \
289 list_for_each_entry((ss), &(root)->subsys_list, sibling)
291 /* iterate across the active hierarchies */
292 #define for_each_active_root(root) \
293 list_for_each_entry((root), &cgroup_roots, root_list)
295 static inline struct cgroup
*__d_cgrp(struct dentry
*dentry
)
297 return dentry
->d_fsdata
;
300 static inline struct cfent
*__d_cfe(struct dentry
*dentry
)
302 return dentry
->d_fsdata
;
305 static inline struct cftype
*__d_cft(struct dentry
*dentry
)
307 return __d_cfe(dentry
)->type
;
311 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
312 * @cgrp: the cgroup to be checked for liveness
314 * On success, returns true; the mutex should be later unlocked. On
315 * failure returns false with no lock held.
317 static bool cgroup_lock_live_group(struct cgroup
*cgrp
)
319 mutex_lock(&cgroup_mutex
);
320 if (cgroup_is_dead(cgrp
)) {
321 mutex_unlock(&cgroup_mutex
);
327 /* the list of cgroups eligible for automatic release. Protected by
328 * release_list_lock */
329 static LIST_HEAD(release_list
);
330 static DEFINE_RAW_SPINLOCK(release_list_lock
);
331 static void cgroup_release_agent(struct work_struct
*work
);
332 static DECLARE_WORK(release_agent_work
, cgroup_release_agent
);
333 static void check_for_release(struct cgroup
*cgrp
);
336 * A cgroup can be associated with multiple css_sets as different tasks may
337 * belong to different cgroups on different hierarchies. In the other
338 * direction, a css_set is naturally associated with multiple cgroups.
339 * This M:N relationship is represented by the following link structure
340 * which exists for each association and allows traversing the associations
343 struct cgrp_cset_link
{
344 /* the cgroup and css_set this link associates */
346 struct css_set
*cset
;
348 /* list of cgrp_cset_links anchored at cgrp->cset_links */
349 struct list_head cset_link
;
351 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
352 struct list_head cgrp_link
;
355 /* The default css_set - used by init and its children prior to any
356 * hierarchies being mounted. It contains a pointer to the root state
357 * for each subsystem. Also used to anchor the list of css_sets. Not
358 * reference-counted, to improve performance when child cgroups
359 * haven't been created.
362 static struct css_set init_css_set
;
363 static struct cgrp_cset_link init_cgrp_cset_link
;
365 static int cgroup_init_idr(struct cgroup_subsys
*ss
,
366 struct cgroup_subsys_state
*css
);
368 /* css_set_lock protects the list of css_set objects, and the
369 * chain of tasks off each css_set. Nests outside task->alloc_lock
370 * due to cgroup_iter_start() */
371 static DEFINE_RWLOCK(css_set_lock
);
372 static int css_set_count
;
375 * hash table for cgroup groups. This improves the performance to find
376 * an existing css_set. This hash doesn't (currently) take into
377 * account cgroups in empty hierarchies.
379 #define CSS_SET_HASH_BITS 7
380 static DEFINE_HASHTABLE(css_set_table
, CSS_SET_HASH_BITS
);
382 static unsigned long css_set_hash(struct cgroup_subsys_state
*css
[])
384 unsigned long key
= 0UL;
385 struct cgroup_subsys
*ss
;
388 for_each_subsys(ss
, i
)
389 key
+= (unsigned long)css
[i
];
390 key
= (key
>> 16) ^ key
;
395 /* We don't maintain the lists running through each css_set to its
396 * task until after the first call to cgroup_iter_start(). This
397 * reduces the fork()/exit() overhead for people who have cgroups
398 * compiled into their kernel but not actually in use */
399 static int use_task_css_set_links __read_mostly
;
401 static void __put_css_set(struct css_set
*cset
, int taskexit
)
403 struct cgrp_cset_link
*link
, *tmp_link
;
406 * Ensure that the refcount doesn't hit zero while any readers
407 * can see it. Similar to atomic_dec_and_lock(), but for an
410 if (atomic_add_unless(&cset
->refcount
, -1, 1))
412 write_lock(&css_set_lock
);
413 if (!atomic_dec_and_test(&cset
->refcount
)) {
414 write_unlock(&css_set_lock
);
418 /* This css_set is dead. unlink it and release cgroup refcounts */
419 hash_del(&cset
->hlist
);
422 list_for_each_entry_safe(link
, tmp_link
, &cset
->cgrp_links
, cgrp_link
) {
423 struct cgroup
*cgrp
= link
->cgrp
;
425 list_del(&link
->cset_link
);
426 list_del(&link
->cgrp_link
);
428 /* @cgrp can't go away while we're holding css_set_lock */
429 if (list_empty(&cgrp
->cset_links
) && notify_on_release(cgrp
)) {
431 set_bit(CGRP_RELEASABLE
, &cgrp
->flags
);
432 check_for_release(cgrp
);
438 write_unlock(&css_set_lock
);
439 kfree_rcu(cset
, rcu_head
);
443 * refcounted get/put for css_set objects
445 static inline void get_css_set(struct css_set
*cset
)
447 atomic_inc(&cset
->refcount
);
450 static inline void put_css_set(struct css_set
*cset
)
452 __put_css_set(cset
, 0);
455 static inline void put_css_set_taskexit(struct css_set
*cset
)
457 __put_css_set(cset
, 1);
461 * compare_css_sets - helper function for find_existing_css_set().
462 * @cset: candidate css_set being tested
463 * @old_cset: existing css_set for a task
464 * @new_cgrp: cgroup that's being entered by the task
465 * @template: desired set of css pointers in css_set (pre-calculated)
467 * Returns true if "cg" matches "old_cg" except for the hierarchy
468 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
470 static bool compare_css_sets(struct css_set
*cset
,
471 struct css_set
*old_cset
,
472 struct cgroup
*new_cgrp
,
473 struct cgroup_subsys_state
*template[])
475 struct list_head
*l1
, *l2
;
477 if (memcmp(template, cset
->subsys
, sizeof(cset
->subsys
))) {
478 /* Not all subsystems matched */
483 * Compare cgroup pointers in order to distinguish between
484 * different cgroups in heirarchies with no subsystems. We
485 * could get by with just this check alone (and skip the
486 * memcmp above) but on most setups the memcmp check will
487 * avoid the need for this more expensive check on almost all
491 l1
= &cset
->cgrp_links
;
492 l2
= &old_cset
->cgrp_links
;
494 struct cgrp_cset_link
*link1
, *link2
;
495 struct cgroup
*cgrp1
, *cgrp2
;
499 /* See if we reached the end - both lists are equal length. */
500 if (l1
== &cset
->cgrp_links
) {
501 BUG_ON(l2
!= &old_cset
->cgrp_links
);
504 BUG_ON(l2
== &old_cset
->cgrp_links
);
506 /* Locate the cgroups associated with these links. */
507 link1
= list_entry(l1
, struct cgrp_cset_link
, cgrp_link
);
508 link2
= list_entry(l2
, struct cgrp_cset_link
, cgrp_link
);
511 /* Hierarchies should be linked in the same order. */
512 BUG_ON(cgrp1
->root
!= cgrp2
->root
);
515 * If this hierarchy is the hierarchy of the cgroup
516 * that's changing, then we need to check that this
517 * css_set points to the new cgroup; if it's any other
518 * hierarchy, then this css_set should point to the
519 * same cgroup as the old css_set.
521 if (cgrp1
->root
== new_cgrp
->root
) {
522 if (cgrp1
!= new_cgrp
)
533 * find_existing_css_set - init css array and find the matching css_set
534 * @old_cset: the css_set that we're using before the cgroup transition
535 * @cgrp: the cgroup that we're moving into
536 * @template: out param for the new set of csses, should be clear on entry
538 static struct css_set
*find_existing_css_set(struct css_set
*old_cset
,
540 struct cgroup_subsys_state
*template[])
542 struct cgroupfs_root
*root
= cgrp
->root
;
543 struct cgroup_subsys
*ss
;
544 struct css_set
*cset
;
549 * Build the set of subsystem state objects that we want to see in the
550 * new css_set. while subsystems can change globally, the entries here
551 * won't change, so no need for locking.
553 for_each_subsys(ss
, i
) {
554 if (root
->subsys_mask
& (1UL << i
)) {
555 /* Subsystem is in this hierarchy. So we want
556 * the subsystem state from the new
558 template[i
] = cgrp
->subsys
[i
];
560 /* Subsystem is not in this hierarchy, so we
561 * don't want to change the subsystem state */
562 template[i
] = old_cset
->subsys
[i
];
566 key
= css_set_hash(template);
567 hash_for_each_possible(css_set_table
, cset
, hlist
, key
) {
568 if (!compare_css_sets(cset
, old_cset
, cgrp
, template))
571 /* This css_set matches what we need */
575 /* No existing cgroup group matched */
579 static void free_cgrp_cset_links(struct list_head
*links_to_free
)
581 struct cgrp_cset_link
*link
, *tmp_link
;
583 list_for_each_entry_safe(link
, tmp_link
, links_to_free
, cset_link
) {
584 list_del(&link
->cset_link
);
590 * allocate_cgrp_cset_links - allocate cgrp_cset_links
591 * @count: the number of links to allocate
592 * @tmp_links: list_head the allocated links are put on
594 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
595 * through ->cset_link. Returns 0 on success or -errno.
597 static int allocate_cgrp_cset_links(int count
, struct list_head
*tmp_links
)
599 struct cgrp_cset_link
*link
;
602 INIT_LIST_HEAD(tmp_links
);
604 for (i
= 0; i
< count
; i
++) {
605 link
= kzalloc(sizeof(*link
), GFP_KERNEL
);
607 free_cgrp_cset_links(tmp_links
);
610 list_add(&link
->cset_link
, tmp_links
);
616 * link_css_set - a helper function to link a css_set to a cgroup
617 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
618 * @cset: the css_set to be linked
619 * @cgrp: the destination cgroup
621 static void link_css_set(struct list_head
*tmp_links
, struct css_set
*cset
,
624 struct cgrp_cset_link
*link
;
626 BUG_ON(list_empty(tmp_links
));
627 link
= list_first_entry(tmp_links
, struct cgrp_cset_link
, cset_link
);
630 list_move(&link
->cset_link
, &cgrp
->cset_links
);
632 * Always add links to the tail of the list so that the list
633 * is sorted by order of hierarchy creation
635 list_add_tail(&link
->cgrp_link
, &cset
->cgrp_links
);
639 * find_css_set - return a new css_set with one cgroup updated
640 * @old_cset: the baseline css_set
641 * @cgrp: the cgroup to be updated
643 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
644 * substituted into the appropriate hierarchy.
646 static struct css_set
*find_css_set(struct css_set
*old_cset
,
649 struct cgroup_subsys_state
*template[CGROUP_SUBSYS_COUNT
] = { };
650 struct css_set
*cset
;
651 struct list_head tmp_links
;
652 struct cgrp_cset_link
*link
;
655 lockdep_assert_held(&cgroup_mutex
);
657 /* First see if we already have a cgroup group that matches
659 read_lock(&css_set_lock
);
660 cset
= find_existing_css_set(old_cset
, cgrp
, template);
663 read_unlock(&css_set_lock
);
668 cset
= kzalloc(sizeof(*cset
), GFP_KERNEL
);
672 /* Allocate all the cgrp_cset_link objects that we'll need */
673 if (allocate_cgrp_cset_links(cgroup_root_count
, &tmp_links
) < 0) {
678 atomic_set(&cset
->refcount
, 1);
679 INIT_LIST_HEAD(&cset
->cgrp_links
);
680 INIT_LIST_HEAD(&cset
->tasks
);
681 INIT_HLIST_NODE(&cset
->hlist
);
683 /* Copy the set of subsystem state objects generated in
684 * find_existing_css_set() */
685 memcpy(cset
->subsys
, template, sizeof(cset
->subsys
));
687 write_lock(&css_set_lock
);
688 /* Add reference counts and links from the new css_set. */
689 list_for_each_entry(link
, &old_cset
->cgrp_links
, cgrp_link
) {
690 struct cgroup
*c
= link
->cgrp
;
692 if (c
->root
== cgrp
->root
)
694 link_css_set(&tmp_links
, cset
, c
);
697 BUG_ON(!list_empty(&tmp_links
));
701 /* Add this cgroup group to the hash table */
702 key
= css_set_hash(cset
->subsys
);
703 hash_add(css_set_table
, &cset
->hlist
, key
);
705 write_unlock(&css_set_lock
);
711 * Return the cgroup for "task" from the given hierarchy. Must be
712 * called with cgroup_mutex held.
714 static struct cgroup
*task_cgroup_from_root(struct task_struct
*task
,
715 struct cgroupfs_root
*root
)
717 struct css_set
*cset
;
718 struct cgroup
*res
= NULL
;
720 BUG_ON(!mutex_is_locked(&cgroup_mutex
));
721 read_lock(&css_set_lock
);
723 * No need to lock the task - since we hold cgroup_mutex the
724 * task can't change groups, so the only thing that can happen
725 * is that it exits and its css is set back to init_css_set.
727 cset
= task_css_set(task
);
728 if (cset
== &init_css_set
) {
729 res
= &root
->top_cgroup
;
731 struct cgrp_cset_link
*link
;
733 list_for_each_entry(link
, &cset
->cgrp_links
, cgrp_link
) {
734 struct cgroup
*c
= link
->cgrp
;
736 if (c
->root
== root
) {
742 read_unlock(&css_set_lock
);
748 * There is one global cgroup mutex. We also require taking
749 * task_lock() when dereferencing a task's cgroup subsys pointers.
750 * See "The task_lock() exception", at the end of this comment.
752 * A task must hold cgroup_mutex to modify cgroups.
754 * Any task can increment and decrement the count field without lock.
755 * So in general, code holding cgroup_mutex can't rely on the count
756 * field not changing. However, if the count goes to zero, then only
757 * cgroup_attach_task() can increment it again. Because a count of zero
758 * means that no tasks are currently attached, therefore there is no
759 * way a task attached to that cgroup can fork (the other way to
760 * increment the count). So code holding cgroup_mutex can safely
761 * assume that if the count is zero, it will stay zero. Similarly, if
762 * a task holds cgroup_mutex on a cgroup with zero count, it
763 * knows that the cgroup won't be removed, as cgroup_rmdir()
766 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
767 * (usually) take cgroup_mutex. These are the two most performance
768 * critical pieces of code here. The exception occurs on cgroup_exit(),
769 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
770 * is taken, and if the cgroup count is zero, a usermode call made
771 * to the release agent with the name of the cgroup (path relative to
772 * the root of cgroup file system) as the argument.
774 * A cgroup can only be deleted if both its 'count' of using tasks
775 * is zero, and its list of 'children' cgroups is empty. Since all
776 * tasks in the system use _some_ cgroup, and since there is always at
777 * least one task in the system (init, pid == 1), therefore, top_cgroup
778 * always has either children cgroups and/or using tasks. So we don't
779 * need a special hack to ensure that top_cgroup cannot be deleted.
781 * The task_lock() exception
783 * The need for this exception arises from the action of
784 * cgroup_attach_task(), which overwrites one task's cgroup pointer with
785 * another. It does so using cgroup_mutex, however there are
786 * several performance critical places that need to reference
787 * task->cgroup without the expense of grabbing a system global
788 * mutex. Therefore except as noted below, when dereferencing or, as
789 * in cgroup_attach_task(), modifying a task's cgroup pointer we use
790 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
791 * the task_struct routinely used for such matters.
793 * P.S. One more locking exception. RCU is used to guard the
794 * update of a tasks cgroup pointer by cgroup_attach_task()
798 * A couple of forward declarations required, due to cyclic reference loop:
799 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
800 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
804 static int cgroup_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
);
805 static struct dentry
*cgroup_lookup(struct inode
*, struct dentry
*, unsigned int);
806 static int cgroup_rmdir(struct inode
*unused_dir
, struct dentry
*dentry
);
807 static int cgroup_populate_dir(struct cgroup
*cgrp
, bool base_files
,
808 unsigned long subsys_mask
);
809 static const struct inode_operations cgroup_dir_inode_operations
;
810 static const struct file_operations proc_cgroupstats_operations
;
812 static struct backing_dev_info cgroup_backing_dev_info
= {
814 .capabilities
= BDI_CAP_NO_ACCT_AND_WRITEBACK
,
817 static int alloc_css_id(struct cgroup_subsys
*ss
,
818 struct cgroup
*parent
, struct cgroup
*child
);
820 static struct inode
*cgroup_new_inode(umode_t mode
, struct super_block
*sb
)
822 struct inode
*inode
= new_inode(sb
);
825 inode
->i_ino
= get_next_ino();
826 inode
->i_mode
= mode
;
827 inode
->i_uid
= current_fsuid();
828 inode
->i_gid
= current_fsgid();
829 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
830 inode
->i_mapping
->backing_dev_info
= &cgroup_backing_dev_info
;
835 static struct cgroup_name
*cgroup_alloc_name(struct dentry
*dentry
)
837 struct cgroup_name
*name
;
839 name
= kmalloc(sizeof(*name
) + dentry
->d_name
.len
+ 1, GFP_KERNEL
);
842 strcpy(name
->name
, dentry
->d_name
.name
);
846 static void cgroup_free_fn(struct work_struct
*work
)
848 struct cgroup
*cgrp
= container_of(work
, struct cgroup
, destroy_work
);
849 struct cgroup_subsys
*ss
;
851 mutex_lock(&cgroup_mutex
);
853 * Release the subsystem state objects.
855 for_each_root_subsys(cgrp
->root
, ss
)
858 cgrp
->root
->number_of_cgroups
--;
859 mutex_unlock(&cgroup_mutex
);
862 * We get a ref to the parent's dentry, and put the ref when
863 * this cgroup is being freed, so it's guaranteed that the
864 * parent won't be destroyed before its children.
866 dput(cgrp
->parent
->dentry
);
868 ida_simple_remove(&cgrp
->root
->cgroup_ida
, cgrp
->id
);
871 * Drop the active superblock reference that we took when we
872 * created the cgroup. This will free cgrp->root, if we are
873 * holding the last reference to @sb.
875 deactivate_super(cgrp
->root
->sb
);
878 * if we're getting rid of the cgroup, refcount should ensure
879 * that there are no pidlists left.
881 BUG_ON(!list_empty(&cgrp
->pidlists
));
883 simple_xattrs_free(&cgrp
->xattrs
);
885 kfree(rcu_dereference_raw(cgrp
->name
));
889 static void cgroup_free_rcu(struct rcu_head
*head
)
891 struct cgroup
*cgrp
= container_of(head
, struct cgroup
, rcu_head
);
893 INIT_WORK(&cgrp
->destroy_work
, cgroup_free_fn
);
894 schedule_work(&cgrp
->destroy_work
);
897 static void cgroup_diput(struct dentry
*dentry
, struct inode
*inode
)
899 /* is dentry a directory ? if so, kfree() associated cgroup */
900 if (S_ISDIR(inode
->i_mode
)) {
901 struct cgroup
*cgrp
= dentry
->d_fsdata
;
903 BUG_ON(!(cgroup_is_dead(cgrp
)));
904 call_rcu(&cgrp
->rcu_head
, cgroup_free_rcu
);
906 struct cfent
*cfe
= __d_cfe(dentry
);
907 struct cgroup
*cgrp
= dentry
->d_parent
->d_fsdata
;
909 WARN_ONCE(!list_empty(&cfe
->node
) &&
910 cgrp
!= &cgrp
->root
->top_cgroup
,
911 "cfe still linked for %s\n", cfe
->type
->name
);
912 simple_xattrs_free(&cfe
->xattrs
);
918 static int cgroup_delete(const struct dentry
*d
)
923 static void remove_dir(struct dentry
*d
)
925 struct dentry
*parent
= dget(d
->d_parent
);
928 simple_rmdir(parent
->d_inode
, d
);
932 static void cgroup_rm_file(struct cgroup
*cgrp
, const struct cftype
*cft
)
936 lockdep_assert_held(&cgrp
->dentry
->d_inode
->i_mutex
);
937 lockdep_assert_held(&cgroup_mutex
);
940 * If we're doing cleanup due to failure of cgroup_create(),
941 * the corresponding @cfe may not exist.
943 list_for_each_entry(cfe
, &cgrp
->files
, node
) {
944 struct dentry
*d
= cfe
->dentry
;
946 if (cft
&& cfe
->type
!= cft
)
951 simple_unlink(cgrp
->dentry
->d_inode
, d
);
952 list_del_init(&cfe
->node
);
960 * cgroup_clear_dir - selective removal of base and subsystem files
961 * @cgrp: target cgroup
962 * @base_files: true if the base files should be removed
963 * @subsys_mask: mask of the subsystem ids whose files should be removed
965 static void cgroup_clear_dir(struct cgroup
*cgrp
, bool base_files
,
966 unsigned long subsys_mask
)
968 struct cgroup_subsys
*ss
;
970 for_each_root_subsys(cgrp
->root
, ss
) {
971 struct cftype_set
*set
;
972 if (!test_bit(ss
->subsys_id
, &subsys_mask
))
974 list_for_each_entry(set
, &ss
->cftsets
, node
)
975 cgroup_addrm_files(cgrp
, NULL
, set
->cfts
, false);
978 while (!list_empty(&cgrp
->files
))
979 cgroup_rm_file(cgrp
, NULL
);
984 * NOTE : the dentry must have been dget()'ed
986 static void cgroup_d_remove_dir(struct dentry
*dentry
)
988 struct dentry
*parent
;
990 parent
= dentry
->d_parent
;
991 spin_lock(&parent
->d_lock
);
992 spin_lock_nested(&dentry
->d_lock
, DENTRY_D_LOCK_NESTED
);
993 list_del_init(&dentry
->d_u
.d_child
);
994 spin_unlock(&dentry
->d_lock
);
995 spin_unlock(&parent
->d_lock
);
1000 * Call with cgroup_mutex held. Drops reference counts on modules, including
1001 * any duplicate ones that parse_cgroupfs_options took. If this function
1002 * returns an error, no reference counts are touched.
1004 static int rebind_subsystems(struct cgroupfs_root
*root
,
1005 unsigned long added_mask
, unsigned removed_mask
)
1007 struct cgroup
*cgrp
= &root
->top_cgroup
;
1008 struct cgroup_subsys
*ss
;
1011 BUG_ON(!mutex_is_locked(&cgroup_mutex
));
1012 BUG_ON(!mutex_is_locked(&cgroup_root_mutex
));
1014 /* Check that any added subsystems are currently free */
1015 for_each_subsys(ss
, i
) {
1016 unsigned long bit
= 1UL << i
;
1018 if (!(bit
& added_mask
))
1021 if (ss
->root
!= &cgroup_dummy_root
) {
1022 /* Subsystem isn't free */
1027 /* Currently we don't handle adding/removing subsystems when
1028 * any child cgroups exist. This is theoretically supportable
1029 * but involves complex error handling, so it's being left until
1031 if (root
->number_of_cgroups
> 1)
1034 /* Process each subsystem */
1035 for_each_subsys(ss
, i
) {
1036 unsigned long bit
= 1UL << i
;
1038 if (bit
& added_mask
) {
1039 /* We're binding this subsystem to this hierarchy */
1040 BUG_ON(cgrp
->subsys
[i
]);
1041 BUG_ON(!cgroup_dummy_top
->subsys
[i
]);
1042 BUG_ON(cgroup_dummy_top
->subsys
[i
]->cgroup
!= cgroup_dummy_top
);
1044 cgrp
->subsys
[i
] = cgroup_dummy_top
->subsys
[i
];
1045 cgrp
->subsys
[i
]->cgroup
= cgrp
;
1046 list_move(&ss
->sibling
, &root
->subsys_list
);
1051 /* refcount was already taken, and we're keeping it */
1052 root
->subsys_mask
|= bit
;
1053 } else if (bit
& removed_mask
) {
1054 /* We're removing this subsystem */
1055 BUG_ON(cgrp
->subsys
[i
] != cgroup_dummy_top
->subsys
[i
]);
1056 BUG_ON(cgrp
->subsys
[i
]->cgroup
!= cgrp
);
1059 ss
->bind(cgroup_dummy_top
);
1060 cgroup_dummy_top
->subsys
[i
]->cgroup
= cgroup_dummy_top
;
1061 cgrp
->subsys
[i
] = NULL
;
1062 cgroup_subsys
[i
]->root
= &cgroup_dummy_root
;
1063 list_move(&ss
->sibling
, &cgroup_dummy_root
.subsys_list
);
1065 /* subsystem is now free - drop reference on module */
1066 module_put(ss
->module
);
1067 root
->subsys_mask
&= ~bit
;
1068 } else if (bit
& root
->subsys_mask
) {
1069 /* Subsystem state should already exist */
1070 BUG_ON(!cgrp
->subsys
[i
]);
1072 * a refcount was taken, but we already had one, so
1073 * drop the extra reference.
1075 module_put(ss
->module
);
1076 #ifdef CONFIG_MODULE_UNLOAD
1077 BUG_ON(ss
->module
&& !module_refcount(ss
->module
));
1080 /* Subsystem state shouldn't exist */
1081 BUG_ON(cgrp
->subsys
[i
]);
1086 * Mark @root has finished binding subsystems. @root->subsys_mask
1087 * now matches the bound subsystems.
1089 root
->flags
|= CGRP_ROOT_SUBSYS_BOUND
;
1094 static int cgroup_show_options(struct seq_file
*seq
, struct dentry
*dentry
)
1096 struct cgroupfs_root
*root
= dentry
->d_sb
->s_fs_info
;
1097 struct cgroup_subsys
*ss
;
1099 mutex_lock(&cgroup_root_mutex
);
1100 for_each_root_subsys(root
, ss
)
1101 seq_printf(seq
, ",%s", ss
->name
);
1102 if (root
->flags
& CGRP_ROOT_SANE_BEHAVIOR
)
1103 seq_puts(seq
, ",sane_behavior");
1104 if (root
->flags
& CGRP_ROOT_NOPREFIX
)
1105 seq_puts(seq
, ",noprefix");
1106 if (root
->flags
& CGRP_ROOT_XATTR
)
1107 seq_puts(seq
, ",xattr");
1108 if (strlen(root
->release_agent_path
))
1109 seq_printf(seq
, ",release_agent=%s", root
->release_agent_path
);
1110 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN
, &root
->top_cgroup
.flags
))
1111 seq_puts(seq
, ",clone_children");
1112 if (strlen(root
->name
))
1113 seq_printf(seq
, ",name=%s", root
->name
);
1114 mutex_unlock(&cgroup_root_mutex
);
1118 struct cgroup_sb_opts
{
1119 unsigned long subsys_mask
;
1120 unsigned long flags
;
1121 char *release_agent
;
1122 bool cpuset_clone_children
;
1124 /* User explicitly requested empty subsystem */
1127 struct cgroupfs_root
*new_root
;
1132 * Convert a hierarchy specifier into a bitmask of subsystems and
1133 * flags. Call with cgroup_mutex held to protect the cgroup_subsys[]
1134 * array. This function takes refcounts on subsystems to be used, unless it
1135 * returns error, in which case no refcounts are taken.
1137 static int parse_cgroupfs_options(char *data
, struct cgroup_sb_opts
*opts
)
1139 char *token
, *o
= data
;
1140 bool all_ss
= false, one_ss
= false;
1141 unsigned long mask
= (unsigned long)-1;
1142 bool module_pin_failed
= false;
1143 struct cgroup_subsys
*ss
;
1146 BUG_ON(!mutex_is_locked(&cgroup_mutex
));
1148 #ifdef CONFIG_CPUSETS
1149 mask
= ~(1UL << cpuset_subsys_id
);
1152 memset(opts
, 0, sizeof(*opts
));
1154 while ((token
= strsep(&o
, ",")) != NULL
) {
1157 if (!strcmp(token
, "none")) {
1158 /* Explicitly have no subsystems */
1162 if (!strcmp(token
, "all")) {
1163 /* Mutually exclusive option 'all' + subsystem name */
1169 if (!strcmp(token
, "__DEVEL__sane_behavior")) {
1170 opts
->flags
|= CGRP_ROOT_SANE_BEHAVIOR
;
1173 if (!strcmp(token
, "noprefix")) {
1174 opts
->flags
|= CGRP_ROOT_NOPREFIX
;
1177 if (!strcmp(token
, "clone_children")) {
1178 opts
->cpuset_clone_children
= true;
1181 if (!strcmp(token
, "xattr")) {
1182 opts
->flags
|= CGRP_ROOT_XATTR
;
1185 if (!strncmp(token
, "release_agent=", 14)) {
1186 /* Specifying two release agents is forbidden */
1187 if (opts
->release_agent
)
1189 opts
->release_agent
=
1190 kstrndup(token
+ 14, PATH_MAX
- 1, GFP_KERNEL
);
1191 if (!opts
->release_agent
)
1195 if (!strncmp(token
, "name=", 5)) {
1196 const char *name
= token
+ 5;
1197 /* Can't specify an empty name */
1200 /* Must match [\w.-]+ */
1201 for (i
= 0; i
< strlen(name
); i
++) {
1205 if ((c
== '.') || (c
== '-') || (c
== '_'))
1209 /* Specifying two names is forbidden */
1212 opts
->name
= kstrndup(name
,
1213 MAX_CGROUP_ROOT_NAMELEN
- 1,
1221 for_each_subsys(ss
, i
) {
1222 if (strcmp(token
, ss
->name
))
1227 /* Mutually exclusive option 'all' + subsystem name */
1230 set_bit(i
, &opts
->subsys_mask
);
1235 if (i
== CGROUP_SUBSYS_COUNT
)
1240 * If the 'all' option was specified select all the subsystems,
1241 * otherwise if 'none', 'name=' and a subsystem name options
1242 * were not specified, let's default to 'all'
1244 if (all_ss
|| (!one_ss
&& !opts
->none
&& !opts
->name
))
1245 for_each_subsys(ss
, i
)
1247 set_bit(i
, &opts
->subsys_mask
);
1249 /* Consistency checks */
1251 if (opts
->flags
& CGRP_ROOT_SANE_BEHAVIOR
) {
1252 pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
1254 if (opts
->flags
& CGRP_ROOT_NOPREFIX
) {
1255 pr_err("cgroup: sane_behavior: noprefix is not allowed\n");
1259 if (opts
->cpuset_clone_children
) {
1260 pr_err("cgroup: sane_behavior: clone_children is not allowed\n");
1266 * Option noprefix was introduced just for backward compatibility
1267 * with the old cpuset, so we allow noprefix only if mounting just
1268 * the cpuset subsystem.
1270 if ((opts
->flags
& CGRP_ROOT_NOPREFIX
) && (opts
->subsys_mask
& mask
))
1274 /* Can't specify "none" and some subsystems */
1275 if (opts
->subsys_mask
&& opts
->none
)
1279 * We either have to specify by name or by subsystems. (So all
1280 * empty hierarchies must have a name).
1282 if (!opts
->subsys_mask
&& !opts
->name
)
1286 * Grab references on all the modules we'll need, so the subsystems
1287 * don't dance around before rebind_subsystems attaches them. This may
1288 * take duplicate reference counts on a subsystem that's already used,
1289 * but rebind_subsystems handles this case.
1291 for_each_subsys(ss
, i
) {
1292 if (!(opts
->subsys_mask
& (1UL << i
)))
1294 if (!try_module_get(cgroup_subsys
[i
]->module
)) {
1295 module_pin_failed
= true;
1299 if (module_pin_failed
) {
1301 * oops, one of the modules was going away. this means that we
1302 * raced with a module_delete call, and to the user this is
1303 * essentially a "subsystem doesn't exist" case.
1305 for (i
--; i
>= 0; i
--) {
1306 /* drop refcounts only on the ones we took */
1307 unsigned long bit
= 1UL << i
;
1309 if (!(bit
& opts
->subsys_mask
))
1311 module_put(cgroup_subsys
[i
]->module
);
1319 static void drop_parsed_module_refcounts(unsigned long subsys_mask
)
1321 struct cgroup_subsys
*ss
;
1324 mutex_lock(&cgroup_mutex
);
1325 for_each_subsys(ss
, i
)
1326 if (subsys_mask
& (1UL << i
))
1327 module_put(cgroup_subsys
[i
]->module
);
1328 mutex_unlock(&cgroup_mutex
);
1331 static int cgroup_remount(struct super_block
*sb
, int *flags
, char *data
)
1334 struct cgroupfs_root
*root
= sb
->s_fs_info
;
1335 struct cgroup
*cgrp
= &root
->top_cgroup
;
1336 struct cgroup_sb_opts opts
;
1337 unsigned long added_mask
, removed_mask
;
1339 if (root
->flags
& CGRP_ROOT_SANE_BEHAVIOR
) {
1340 pr_err("cgroup: sane_behavior: remount is not allowed\n");
1344 mutex_lock(&cgrp
->dentry
->d_inode
->i_mutex
);
1345 mutex_lock(&cgroup_mutex
);
1346 mutex_lock(&cgroup_root_mutex
);
1348 /* See what subsystems are wanted */
1349 ret
= parse_cgroupfs_options(data
, &opts
);
1353 if (opts
.subsys_mask
!= root
->subsys_mask
|| opts
.release_agent
)
1354 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1355 task_tgid_nr(current
), current
->comm
);
1357 added_mask
= opts
.subsys_mask
& ~root
->subsys_mask
;
1358 removed_mask
= root
->subsys_mask
& ~opts
.subsys_mask
;
1360 /* Don't allow flags or name to change at remount */
1361 if (((opts
.flags
^ root
->flags
) & CGRP_ROOT_OPTION_MASK
) ||
1362 (opts
.name
&& strcmp(opts
.name
, root
->name
))) {
1363 pr_err("cgroup: option or name mismatch, new: 0x%lx \"%s\", old: 0x%lx \"%s\"\n",
1364 opts
.flags
& CGRP_ROOT_OPTION_MASK
, opts
.name
?: "",
1365 root
->flags
& CGRP_ROOT_OPTION_MASK
, root
->name
);
1371 * Clear out the files of subsystems that should be removed, do
1372 * this before rebind_subsystems, since rebind_subsystems may
1373 * change this hierarchy's subsys_list.
1375 cgroup_clear_dir(cgrp
, false, removed_mask
);
1377 ret
= rebind_subsystems(root
, added_mask
, removed_mask
);
1379 /* rebind_subsystems failed, re-populate the removed files */
1380 cgroup_populate_dir(cgrp
, false, removed_mask
);
1384 /* re-populate subsystem files */
1385 cgroup_populate_dir(cgrp
, false, added_mask
);
1387 if (opts
.release_agent
)
1388 strcpy(root
->release_agent_path
, opts
.release_agent
);
1390 kfree(opts
.release_agent
);
1392 mutex_unlock(&cgroup_root_mutex
);
1393 mutex_unlock(&cgroup_mutex
);
1394 mutex_unlock(&cgrp
->dentry
->d_inode
->i_mutex
);
1396 drop_parsed_module_refcounts(opts
.subsys_mask
);
1400 static const struct super_operations cgroup_ops
= {
1401 .statfs
= simple_statfs
,
1402 .drop_inode
= generic_delete_inode
,
1403 .show_options
= cgroup_show_options
,
1404 .remount_fs
= cgroup_remount
,
1407 static void init_cgroup_housekeeping(struct cgroup
*cgrp
)
1409 INIT_LIST_HEAD(&cgrp
->sibling
);
1410 INIT_LIST_HEAD(&cgrp
->children
);
1411 INIT_LIST_HEAD(&cgrp
->files
);
1412 INIT_LIST_HEAD(&cgrp
->cset_links
);
1413 INIT_LIST_HEAD(&cgrp
->release_list
);
1414 INIT_LIST_HEAD(&cgrp
->pidlists
);
1415 mutex_init(&cgrp
->pidlist_mutex
);
1416 INIT_LIST_HEAD(&cgrp
->event_list
);
1417 spin_lock_init(&cgrp
->event_list_lock
);
1418 simple_xattrs_init(&cgrp
->xattrs
);
1421 static void init_cgroup_root(struct cgroupfs_root
*root
)
1423 struct cgroup
*cgrp
= &root
->top_cgroup
;
1425 INIT_LIST_HEAD(&root
->subsys_list
);
1426 INIT_LIST_HEAD(&root
->root_list
);
1427 root
->number_of_cgroups
= 1;
1429 RCU_INIT_POINTER(cgrp
->name
, &root_cgroup_name
);
1430 init_cgroup_housekeeping(cgrp
);
1433 static int cgroup_init_root_id(struct cgroupfs_root
*root
, int start
, int end
)
1437 lockdep_assert_held(&cgroup_mutex
);
1438 lockdep_assert_held(&cgroup_root_mutex
);
1440 id
= idr_alloc_cyclic(&cgroup_hierarchy_idr
, root
, start
, end
,
1445 root
->hierarchy_id
= id
;
1449 static void cgroup_exit_root_id(struct cgroupfs_root
*root
)
1451 lockdep_assert_held(&cgroup_mutex
);
1452 lockdep_assert_held(&cgroup_root_mutex
);
1454 if (root
->hierarchy_id
) {
1455 idr_remove(&cgroup_hierarchy_idr
, root
->hierarchy_id
);
1456 root
->hierarchy_id
= 0;
1460 static int cgroup_test_super(struct super_block
*sb
, void *data
)
1462 struct cgroup_sb_opts
*opts
= data
;
1463 struct cgroupfs_root
*root
= sb
->s_fs_info
;
1465 /* If we asked for a name then it must match */
1466 if (opts
->name
&& strcmp(opts
->name
, root
->name
))
1470 * If we asked for subsystems (or explicitly for no
1471 * subsystems) then they must match
1473 if ((opts
->subsys_mask
|| opts
->none
)
1474 && (opts
->subsys_mask
!= root
->subsys_mask
))
1480 static struct cgroupfs_root
*cgroup_root_from_opts(struct cgroup_sb_opts
*opts
)
1482 struct cgroupfs_root
*root
;
1484 if (!opts
->subsys_mask
&& !opts
->none
)
1487 root
= kzalloc(sizeof(*root
), GFP_KERNEL
);
1489 return ERR_PTR(-ENOMEM
);
1491 init_cgroup_root(root
);
1494 * We need to set @root->subsys_mask now so that @root can be
1495 * matched by cgroup_test_super() before it finishes
1496 * initialization; otherwise, competing mounts with the same
1497 * options may try to bind the same subsystems instead of waiting
1498 * for the first one leading to unexpected mount errors.
1499 * SUBSYS_BOUND will be set once actual binding is complete.
1501 root
->subsys_mask
= opts
->subsys_mask
;
1502 root
->flags
= opts
->flags
;
1503 ida_init(&root
->cgroup_ida
);
1504 if (opts
->release_agent
)
1505 strcpy(root
->release_agent_path
, opts
->release_agent
);
1507 strcpy(root
->name
, opts
->name
);
1508 if (opts
->cpuset_clone_children
)
1509 set_bit(CGRP_CPUSET_CLONE_CHILDREN
, &root
->top_cgroup
.flags
);
1513 static void cgroup_free_root(struct cgroupfs_root
*root
)
1516 /* hierarhcy ID shoulid already have been released */
1517 WARN_ON_ONCE(root
->hierarchy_id
);
1519 ida_destroy(&root
->cgroup_ida
);
1524 static int cgroup_set_super(struct super_block
*sb
, void *data
)
1527 struct cgroup_sb_opts
*opts
= data
;
1529 /* If we don't have a new root, we can't set up a new sb */
1530 if (!opts
->new_root
)
1533 BUG_ON(!opts
->subsys_mask
&& !opts
->none
);
1535 ret
= set_anon_super(sb
, NULL
);
1539 sb
->s_fs_info
= opts
->new_root
;
1540 opts
->new_root
->sb
= sb
;
1542 sb
->s_blocksize
= PAGE_CACHE_SIZE
;
1543 sb
->s_blocksize_bits
= PAGE_CACHE_SHIFT
;
1544 sb
->s_magic
= CGROUP_SUPER_MAGIC
;
1545 sb
->s_op
= &cgroup_ops
;
1550 static int cgroup_get_rootdir(struct super_block
*sb
)
1552 static const struct dentry_operations cgroup_dops
= {
1553 .d_iput
= cgroup_diput
,
1554 .d_delete
= cgroup_delete
,
1557 struct inode
*inode
=
1558 cgroup_new_inode(S_IFDIR
| S_IRUGO
| S_IXUGO
| S_IWUSR
, sb
);
1563 inode
->i_fop
= &simple_dir_operations
;
1564 inode
->i_op
= &cgroup_dir_inode_operations
;
1565 /* directories start off with i_nlink == 2 (for "." entry) */
1567 sb
->s_root
= d_make_root(inode
);
1570 /* for everything else we want ->d_op set */
1571 sb
->s_d_op
= &cgroup_dops
;
1575 static struct dentry
*cgroup_mount(struct file_system_type
*fs_type
,
1576 int flags
, const char *unused_dev_name
,
1579 struct cgroup_sb_opts opts
;
1580 struct cgroupfs_root
*root
;
1582 struct super_block
*sb
;
1583 struct cgroupfs_root
*new_root
;
1584 struct inode
*inode
;
1586 /* First find the desired set of subsystems */
1587 mutex_lock(&cgroup_mutex
);
1588 ret
= parse_cgroupfs_options(data
, &opts
);
1589 mutex_unlock(&cgroup_mutex
);
1594 * Allocate a new cgroup root. We may not need it if we're
1595 * reusing an existing hierarchy.
1597 new_root
= cgroup_root_from_opts(&opts
);
1598 if (IS_ERR(new_root
)) {
1599 ret
= PTR_ERR(new_root
);
1602 opts
.new_root
= new_root
;
1604 /* Locate an existing or new sb for this hierarchy */
1605 sb
= sget(fs_type
, cgroup_test_super
, cgroup_set_super
, 0, &opts
);
1608 cgroup_free_root(opts
.new_root
);
1612 root
= sb
->s_fs_info
;
1614 if (root
== opts
.new_root
) {
1615 /* We used the new root structure, so this is a new hierarchy */
1616 struct list_head tmp_links
;
1617 struct cgroup
*root_cgrp
= &root
->top_cgroup
;
1618 struct cgroupfs_root
*existing_root
;
1619 const struct cred
*cred
;
1621 struct css_set
*cset
;
1623 BUG_ON(sb
->s_root
!= NULL
);
1625 ret
= cgroup_get_rootdir(sb
);
1627 goto drop_new_super
;
1628 inode
= sb
->s_root
->d_inode
;
1630 mutex_lock(&inode
->i_mutex
);
1631 mutex_lock(&cgroup_mutex
);
1632 mutex_lock(&cgroup_root_mutex
);
1634 /* Check for name clashes with existing mounts */
1636 if (strlen(root
->name
))
1637 for_each_active_root(existing_root
)
1638 if (!strcmp(existing_root
->name
, root
->name
))
1642 * We're accessing css_set_count without locking
1643 * css_set_lock here, but that's OK - it can only be
1644 * increased by someone holding cgroup_lock, and
1645 * that's us. The worst that can happen is that we
1646 * have some link structures left over
1648 ret
= allocate_cgrp_cset_links(css_set_count
, &tmp_links
);
1652 /* ID 0 is reserved for dummy root, 1 for unified hierarchy */
1653 ret
= cgroup_init_root_id(root
, 2, 0);
1657 ret
= rebind_subsystems(root
, root
->subsys_mask
, 0);
1658 if (ret
== -EBUSY
) {
1659 free_cgrp_cset_links(&tmp_links
);
1663 * There must be no failure case after here, since rebinding
1664 * takes care of subsystems' refcounts, which are explicitly
1665 * dropped in the failure exit path.
1668 /* EBUSY should be the only error here */
1671 list_add(&root
->root_list
, &cgroup_roots
);
1672 cgroup_root_count
++;
1674 sb
->s_root
->d_fsdata
= root_cgrp
;
1675 root
->top_cgroup
.dentry
= sb
->s_root
;
1677 /* Link the top cgroup in this hierarchy into all
1678 * the css_set objects */
1679 write_lock(&css_set_lock
);
1680 hash_for_each(css_set_table
, i
, cset
, hlist
)
1681 link_css_set(&tmp_links
, cset
, root_cgrp
);
1682 write_unlock(&css_set_lock
);
1684 free_cgrp_cset_links(&tmp_links
);
1686 BUG_ON(!list_empty(&root_cgrp
->children
));
1687 BUG_ON(root
->number_of_cgroups
!= 1);
1689 cred
= override_creds(&init_cred
);
1690 cgroup_populate_dir(root_cgrp
, true, root
->subsys_mask
);
1692 mutex_unlock(&cgroup_root_mutex
);
1693 mutex_unlock(&cgroup_mutex
);
1694 mutex_unlock(&inode
->i_mutex
);
1697 * We re-used an existing hierarchy - the new root (if
1698 * any) is not needed
1700 cgroup_free_root(opts
.new_root
);
1702 if ((root
->flags
^ opts
.flags
) & CGRP_ROOT_OPTION_MASK
) {
1703 if ((root
->flags
| opts
.flags
) & CGRP_ROOT_SANE_BEHAVIOR
) {
1704 pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
1706 goto drop_new_super
;
1708 pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n");
1712 /* no subsys rebinding, so refcounts don't change */
1713 drop_parsed_module_refcounts(opts
.subsys_mask
);
1716 kfree(opts
.release_agent
);
1718 return dget(sb
->s_root
);
1721 cgroup_exit_root_id(root
);
1722 mutex_unlock(&cgroup_root_mutex
);
1723 mutex_unlock(&cgroup_mutex
);
1724 mutex_unlock(&inode
->i_mutex
);
1726 deactivate_locked_super(sb
);
1728 drop_parsed_module_refcounts(opts
.subsys_mask
);
1730 kfree(opts
.release_agent
);
1732 return ERR_PTR(ret
);
1735 static void cgroup_kill_sb(struct super_block
*sb
) {
1736 struct cgroupfs_root
*root
= sb
->s_fs_info
;
1737 struct cgroup
*cgrp
= &root
->top_cgroup
;
1738 struct cgrp_cset_link
*link
, *tmp_link
;
1743 BUG_ON(root
->number_of_cgroups
!= 1);
1744 BUG_ON(!list_empty(&cgrp
->children
));
1746 mutex_lock(&cgroup_mutex
);
1747 mutex_lock(&cgroup_root_mutex
);
1749 /* Rebind all subsystems back to the default hierarchy */
1750 if (root
->flags
& CGRP_ROOT_SUBSYS_BOUND
) {
1751 ret
= rebind_subsystems(root
, 0, root
->subsys_mask
);
1752 /* Shouldn't be able to fail ... */
1757 * Release all the links from cset_links to this hierarchy's
1760 write_lock(&css_set_lock
);
1762 list_for_each_entry_safe(link
, tmp_link
, &cgrp
->cset_links
, cset_link
) {
1763 list_del(&link
->cset_link
);
1764 list_del(&link
->cgrp_link
);
1767 write_unlock(&css_set_lock
);
1769 if (!list_empty(&root
->root_list
)) {
1770 list_del(&root
->root_list
);
1771 cgroup_root_count
--;
1774 cgroup_exit_root_id(root
);
1776 mutex_unlock(&cgroup_root_mutex
);
1777 mutex_unlock(&cgroup_mutex
);
1779 simple_xattrs_free(&cgrp
->xattrs
);
1781 kill_litter_super(sb
);
1782 cgroup_free_root(root
);
1785 static struct file_system_type cgroup_fs_type
= {
1787 .mount
= cgroup_mount
,
1788 .kill_sb
= cgroup_kill_sb
,
1791 static struct kobject
*cgroup_kobj
;
1794 * cgroup_path - generate the path of a cgroup
1795 * @cgrp: the cgroup in question
1796 * @buf: the buffer to write the path into
1797 * @buflen: the length of the buffer
1799 * Writes path of cgroup into buf. Returns 0 on success, -errno on error.
1801 * We can't generate cgroup path using dentry->d_name, as accessing
1802 * dentry->name must be protected by irq-unsafe dentry->d_lock or parent
1803 * inode's i_mutex, while on the other hand cgroup_path() can be called
1804 * with some irq-safe spinlocks held.
1806 int cgroup_path(const struct cgroup
*cgrp
, char *buf
, int buflen
)
1808 int ret
= -ENAMETOOLONG
;
1811 if (!cgrp
->parent
) {
1812 if (strlcpy(buf
, "/", buflen
) >= buflen
)
1813 return -ENAMETOOLONG
;
1817 start
= buf
+ buflen
- 1;
1822 const char *name
= cgroup_name(cgrp
);
1826 if ((start
-= len
) < buf
)
1828 memcpy(start
, name
, len
);
1834 cgrp
= cgrp
->parent
;
1835 } while (cgrp
->parent
);
1837 memmove(buf
, start
, buf
+ buflen
- start
);
1842 EXPORT_SYMBOL_GPL(cgroup_path
);
1845 * task_cgroup_path_from_hierarchy - cgroup path of a task on a hierarchy
1846 * @task: target task
1847 * @hierarchy_id: the hierarchy to look up @task's cgroup from
1848 * @buf: the buffer to write the path into
1849 * @buflen: the length of the buffer
1851 * Determine @task's cgroup on the hierarchy specified by @hierarchy_id and
1852 * copy its path into @buf. This function grabs cgroup_mutex and shouldn't
1853 * be used inside locks used by cgroup controller callbacks.
1855 int task_cgroup_path_from_hierarchy(struct task_struct
*task
, int hierarchy_id
,
1856 char *buf
, size_t buflen
)
1858 struct cgroupfs_root
*root
;
1859 struct cgroup
*cgrp
= NULL
;
1862 mutex_lock(&cgroup_mutex
);
1864 root
= idr_find(&cgroup_hierarchy_idr
, hierarchy_id
);
1866 cgrp
= task_cgroup_from_root(task
, root
);
1867 ret
= cgroup_path(cgrp
, buf
, buflen
);
1870 mutex_unlock(&cgroup_mutex
);
1874 EXPORT_SYMBOL_GPL(task_cgroup_path_from_hierarchy
);
1877 * Control Group taskset
1879 struct task_and_cgroup
{
1880 struct task_struct
*task
;
1881 struct cgroup
*cgrp
;
1885 struct cgroup_taskset
{
1886 struct task_and_cgroup single
;
1887 struct flex_array
*tc_array
;
1890 struct cgroup
*cur_cgrp
;
1894 * cgroup_taskset_first - reset taskset and return the first task
1895 * @tset: taskset of interest
1897 * @tset iteration is initialized and the first task is returned.
1899 struct task_struct
*cgroup_taskset_first(struct cgroup_taskset
*tset
)
1901 if (tset
->tc_array
) {
1903 return cgroup_taskset_next(tset
);
1905 tset
->cur_cgrp
= tset
->single
.cgrp
;
1906 return tset
->single
.task
;
1909 EXPORT_SYMBOL_GPL(cgroup_taskset_first
);
1912 * cgroup_taskset_next - iterate to the next task in taskset
1913 * @tset: taskset of interest
1915 * Return the next task in @tset. Iteration must have been initialized
1916 * with cgroup_taskset_first().
1918 struct task_struct
*cgroup_taskset_next(struct cgroup_taskset
*tset
)
1920 struct task_and_cgroup
*tc
;
1922 if (!tset
->tc_array
|| tset
->idx
>= tset
->tc_array_len
)
1925 tc
= flex_array_get(tset
->tc_array
, tset
->idx
++);
1926 tset
->cur_cgrp
= tc
->cgrp
;
1929 EXPORT_SYMBOL_GPL(cgroup_taskset_next
);
1932 * cgroup_taskset_cur_cgroup - return the matching cgroup for the current task
1933 * @tset: taskset of interest
1935 * Return the cgroup for the current (last returned) task of @tset. This
1936 * function must be preceded by either cgroup_taskset_first() or
1937 * cgroup_taskset_next().
1939 struct cgroup
*cgroup_taskset_cur_cgroup(struct cgroup_taskset
*tset
)
1941 return tset
->cur_cgrp
;
1943 EXPORT_SYMBOL_GPL(cgroup_taskset_cur_cgroup
);
1946 * cgroup_taskset_size - return the number of tasks in taskset
1947 * @tset: taskset of interest
1949 int cgroup_taskset_size(struct cgroup_taskset
*tset
)
1951 return tset
->tc_array
? tset
->tc_array_len
: 1;
1953 EXPORT_SYMBOL_GPL(cgroup_taskset_size
);
1957 * cgroup_task_migrate - move a task from one cgroup to another.
1959 * Must be called with cgroup_mutex and threadgroup locked.
1961 static void cgroup_task_migrate(struct cgroup
*old_cgrp
,
1962 struct task_struct
*tsk
,
1963 struct css_set
*new_cset
)
1965 struct css_set
*old_cset
;
1968 * We are synchronized through threadgroup_lock() against PF_EXITING
1969 * setting such that we can't race against cgroup_exit() changing the
1970 * css_set to init_css_set and dropping the old one.
1972 WARN_ON_ONCE(tsk
->flags
& PF_EXITING
);
1973 old_cset
= task_css_set(tsk
);
1976 rcu_assign_pointer(tsk
->cgroups
, new_cset
);
1979 /* Update the css_set linked lists if we're using them */
1980 write_lock(&css_set_lock
);
1981 if (!list_empty(&tsk
->cg_list
))
1982 list_move(&tsk
->cg_list
, &new_cset
->tasks
);
1983 write_unlock(&css_set_lock
);
1986 * We just gained a reference on old_cset by taking it from the
1987 * task. As trading it for new_cset is protected by cgroup_mutex,
1988 * we're safe to drop it here; it will be freed under RCU.
1990 set_bit(CGRP_RELEASABLE
, &old_cgrp
->flags
);
1991 put_css_set(old_cset
);
1995 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
1996 * @cgrp: the cgroup to attach to
1997 * @tsk: the task or the leader of the threadgroup to be attached
1998 * @threadgroup: attach the whole threadgroup?
2000 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
2001 * task_lock of @tsk or each thread in the threadgroup individually in turn.
2003 static int cgroup_attach_task(struct cgroup
*cgrp
, struct task_struct
*tsk
,
2006 int retval
, i
, group_size
;
2007 struct cgroup_subsys
*ss
, *failed_ss
= NULL
;
2008 struct cgroupfs_root
*root
= cgrp
->root
;
2009 /* threadgroup list cursor and array */
2010 struct task_struct
*leader
= tsk
;
2011 struct task_and_cgroup
*tc
;
2012 struct flex_array
*group
;
2013 struct cgroup_taskset tset
= { };
2016 * step 0: in order to do expensive, possibly blocking operations for
2017 * every thread, we cannot iterate the thread group list, since it needs
2018 * rcu or tasklist locked. instead, build an array of all threads in the
2019 * group - group_rwsem prevents new threads from appearing, and if
2020 * threads exit, this will just be an over-estimate.
2023 group_size
= get_nr_threads(tsk
);
2026 /* flex_array supports very large thread-groups better than kmalloc. */
2027 group
= flex_array_alloc(sizeof(*tc
), group_size
, GFP_KERNEL
);
2030 /* pre-allocate to guarantee space while iterating in rcu read-side. */
2031 retval
= flex_array_prealloc(group
, 0, group_size
, GFP_KERNEL
);
2033 goto out_free_group_list
;
2037 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2038 * already PF_EXITING could be freed from underneath us unless we
2039 * take an rcu_read_lock.
2043 struct task_and_cgroup ent
;
2045 /* @tsk either already exited or can't exit until the end */
2046 if (tsk
->flags
& PF_EXITING
)
2049 /* as per above, nr_threads may decrease, but not increase. */
2050 BUG_ON(i
>= group_size
);
2052 ent
.cgrp
= task_cgroup_from_root(tsk
, root
);
2053 /* nothing to do if this task is already in the cgroup */
2054 if (ent
.cgrp
== cgrp
)
2057 * saying GFP_ATOMIC has no effect here because we did prealloc
2058 * earlier, but it's good form to communicate our expectations.
2060 retval
= flex_array_put(group
, i
, &ent
, GFP_ATOMIC
);
2061 BUG_ON(retval
!= 0);
2066 } while_each_thread(leader
, tsk
);
2068 /* remember the number of threads in the array for later. */
2070 tset
.tc_array
= group
;
2071 tset
.tc_array_len
= group_size
;
2073 /* methods shouldn't be called if no task is actually migrating */
2076 goto out_free_group_list
;
2079 * step 1: check that we can legitimately attach to the cgroup.
2081 for_each_root_subsys(root
, ss
) {
2082 if (ss
->can_attach
) {
2083 retval
= ss
->can_attach(cgrp
, &tset
);
2086 goto out_cancel_attach
;
2092 * step 2: make sure css_sets exist for all threads to be migrated.
2093 * we use find_css_set, which allocates a new one if necessary.
2095 for (i
= 0; i
< group_size
; i
++) {
2096 struct css_set
*old_cset
;
2098 tc
= flex_array_get(group
, i
);
2099 old_cset
= task_css_set(tc
->task
);
2100 tc
->cg
= find_css_set(old_cset
, cgrp
);
2103 goto out_put_css_set_refs
;
2108 * step 3: now that we're guaranteed success wrt the css_sets,
2109 * proceed to move all tasks to the new cgroup. There are no
2110 * failure cases after here, so this is the commit point.
2112 for (i
= 0; i
< group_size
; i
++) {
2113 tc
= flex_array_get(group
, i
);
2114 cgroup_task_migrate(tc
->cgrp
, tc
->task
, tc
->cg
);
2116 /* nothing is sensitive to fork() after this point. */
2119 * step 4: do subsystem attach callbacks.
2121 for_each_root_subsys(root
, ss
) {
2123 ss
->attach(cgrp
, &tset
);
2127 * step 5: success! and cleanup
2130 out_put_css_set_refs
:
2132 for (i
= 0; i
< group_size
; i
++) {
2133 tc
= flex_array_get(group
, i
);
2136 put_css_set(tc
->cg
);
2141 for_each_root_subsys(root
, ss
) {
2142 if (ss
== failed_ss
)
2144 if (ss
->cancel_attach
)
2145 ss
->cancel_attach(cgrp
, &tset
);
2148 out_free_group_list
:
2149 flex_array_free(group
);
2154 * Find the task_struct of the task to attach by vpid and pass it along to the
2155 * function to attach either it or all tasks in its threadgroup. Will lock
2156 * cgroup_mutex and threadgroup; may take task_lock of task.
2158 static int attach_task_by_pid(struct cgroup
*cgrp
, u64 pid
, bool threadgroup
)
2160 struct task_struct
*tsk
;
2161 const struct cred
*cred
= current_cred(), *tcred
;
2164 if (!cgroup_lock_live_group(cgrp
))
2170 tsk
= find_task_by_vpid(pid
);
2174 goto out_unlock_cgroup
;
2177 * even if we're attaching all tasks in the thread group, we
2178 * only need to check permissions on one of them.
2180 tcred
= __task_cred(tsk
);
2181 if (!uid_eq(cred
->euid
, GLOBAL_ROOT_UID
) &&
2182 !uid_eq(cred
->euid
, tcred
->uid
) &&
2183 !uid_eq(cred
->euid
, tcred
->suid
)) {
2186 goto out_unlock_cgroup
;
2192 tsk
= tsk
->group_leader
;
2195 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
2196 * trapped in a cpuset, or RT worker may be born in a cgroup
2197 * with no rt_runtime allocated. Just say no.
2199 if (tsk
== kthreadd_task
|| (tsk
->flags
& PF_NO_SETAFFINITY
)) {
2202 goto out_unlock_cgroup
;
2205 get_task_struct(tsk
);
2208 threadgroup_lock(tsk
);
2210 if (!thread_group_leader(tsk
)) {
2212 * a race with de_thread from another thread's exec()
2213 * may strip us of our leadership, if this happens,
2214 * there is no choice but to throw this task away and
2215 * try again; this is
2216 * "double-double-toil-and-trouble-check locking".
2218 threadgroup_unlock(tsk
);
2219 put_task_struct(tsk
);
2220 goto retry_find_task
;
2224 ret
= cgroup_attach_task(cgrp
, tsk
, threadgroup
);
2226 threadgroup_unlock(tsk
);
2228 put_task_struct(tsk
);
2230 mutex_unlock(&cgroup_mutex
);
2235 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2236 * @from: attach to all cgroups of a given task
2237 * @tsk: the task to be attached
2239 int cgroup_attach_task_all(struct task_struct
*from
, struct task_struct
*tsk
)
2241 struct cgroupfs_root
*root
;
2244 mutex_lock(&cgroup_mutex
);
2245 for_each_active_root(root
) {
2246 struct cgroup
*from_cg
= task_cgroup_from_root(from
, root
);
2248 retval
= cgroup_attach_task(from_cg
, tsk
, false);
2252 mutex_unlock(&cgroup_mutex
);
2256 EXPORT_SYMBOL_GPL(cgroup_attach_task_all
);
2258 static int cgroup_tasks_write(struct cgroup
*cgrp
, struct cftype
*cft
, u64 pid
)
2260 return attach_task_by_pid(cgrp
, pid
, false);
2263 static int cgroup_procs_write(struct cgroup
*cgrp
, struct cftype
*cft
, u64 tgid
)
2265 return attach_task_by_pid(cgrp
, tgid
, true);
2268 static int cgroup_release_agent_write(struct cgroup
*cgrp
, struct cftype
*cft
,
2271 BUILD_BUG_ON(sizeof(cgrp
->root
->release_agent_path
) < PATH_MAX
);
2272 if (strlen(buffer
) >= PATH_MAX
)
2274 if (!cgroup_lock_live_group(cgrp
))
2276 mutex_lock(&cgroup_root_mutex
);
2277 strcpy(cgrp
->root
->release_agent_path
, buffer
);
2278 mutex_unlock(&cgroup_root_mutex
);
2279 mutex_unlock(&cgroup_mutex
);
2283 static int cgroup_release_agent_show(struct cgroup
*cgrp
, struct cftype
*cft
,
2284 struct seq_file
*seq
)
2286 if (!cgroup_lock_live_group(cgrp
))
2288 seq_puts(seq
, cgrp
->root
->release_agent_path
);
2289 seq_putc(seq
, '\n');
2290 mutex_unlock(&cgroup_mutex
);
2294 static int cgroup_sane_behavior_show(struct cgroup
*cgrp
, struct cftype
*cft
,
2295 struct seq_file
*seq
)
2297 seq_printf(seq
, "%d\n", cgroup_sane_behavior(cgrp
));
2301 /* A buffer size big enough for numbers or short strings */
2302 #define CGROUP_LOCAL_BUFFER_SIZE 64
2304 static ssize_t
cgroup_write_X64(struct cgroup
*cgrp
, struct cftype
*cft
,
2306 const char __user
*userbuf
,
2307 size_t nbytes
, loff_t
*unused_ppos
)
2309 char buffer
[CGROUP_LOCAL_BUFFER_SIZE
];
2315 if (nbytes
>= sizeof(buffer
))
2317 if (copy_from_user(buffer
, userbuf
, nbytes
))
2320 buffer
[nbytes
] = 0; /* nul-terminate */
2321 if (cft
->write_u64
) {
2322 u64 val
= simple_strtoull(strstrip(buffer
), &end
, 0);
2325 retval
= cft
->write_u64(cgrp
, cft
, val
);
2327 s64 val
= simple_strtoll(strstrip(buffer
), &end
, 0);
2330 retval
= cft
->write_s64(cgrp
, cft
, val
);
2337 static ssize_t
cgroup_write_string(struct cgroup
*cgrp
, struct cftype
*cft
,
2339 const char __user
*userbuf
,
2340 size_t nbytes
, loff_t
*unused_ppos
)
2342 char local_buffer
[CGROUP_LOCAL_BUFFER_SIZE
];
2344 size_t max_bytes
= cft
->max_write_len
;
2345 char *buffer
= local_buffer
;
2348 max_bytes
= sizeof(local_buffer
) - 1;
2349 if (nbytes
>= max_bytes
)
2351 /* Allocate a dynamic buffer if we need one */
2352 if (nbytes
>= sizeof(local_buffer
)) {
2353 buffer
= kmalloc(nbytes
+ 1, GFP_KERNEL
);
2357 if (nbytes
&& copy_from_user(buffer
, userbuf
, nbytes
)) {
2362 buffer
[nbytes
] = 0; /* nul-terminate */
2363 retval
= cft
->write_string(cgrp
, cft
, strstrip(buffer
));
2367 if (buffer
!= local_buffer
)
2372 static ssize_t
cgroup_file_write(struct file
*file
, const char __user
*buf
,
2373 size_t nbytes
, loff_t
*ppos
)
2375 struct cftype
*cft
= __d_cft(file
->f_dentry
);
2376 struct cgroup
*cgrp
= __d_cgrp(file
->f_dentry
->d_parent
);
2378 if (cgroup_is_dead(cgrp
))
2381 return cft
->write(cgrp
, cft
, file
, buf
, nbytes
, ppos
);
2382 if (cft
->write_u64
|| cft
->write_s64
)
2383 return cgroup_write_X64(cgrp
, cft
, file
, buf
, nbytes
, ppos
);
2384 if (cft
->write_string
)
2385 return cgroup_write_string(cgrp
, cft
, file
, buf
, nbytes
, ppos
);
2387 int ret
= cft
->trigger(cgrp
, (unsigned int)cft
->private);
2388 return ret
? ret
: nbytes
;
2393 static ssize_t
cgroup_read_u64(struct cgroup
*cgrp
, struct cftype
*cft
,
2395 char __user
*buf
, size_t nbytes
,
2398 char tmp
[CGROUP_LOCAL_BUFFER_SIZE
];
2399 u64 val
= cft
->read_u64(cgrp
, cft
);
2400 int len
= sprintf(tmp
, "%llu\n", (unsigned long long) val
);
2402 return simple_read_from_buffer(buf
, nbytes
, ppos
, tmp
, len
);
2405 static ssize_t
cgroup_read_s64(struct cgroup
*cgrp
, struct cftype
*cft
,
2407 char __user
*buf
, size_t nbytes
,
2410 char tmp
[CGROUP_LOCAL_BUFFER_SIZE
];
2411 s64 val
= cft
->read_s64(cgrp
, cft
);
2412 int len
= sprintf(tmp
, "%lld\n", (long long) val
);
2414 return simple_read_from_buffer(buf
, nbytes
, ppos
, tmp
, len
);
2417 static ssize_t
cgroup_file_read(struct file
*file
, char __user
*buf
,
2418 size_t nbytes
, loff_t
*ppos
)
2420 struct cftype
*cft
= __d_cft(file
->f_dentry
);
2421 struct cgroup
*cgrp
= __d_cgrp(file
->f_dentry
->d_parent
);
2423 if (cgroup_is_dead(cgrp
))
2427 return cft
->read(cgrp
, cft
, file
, buf
, nbytes
, ppos
);
2429 return cgroup_read_u64(cgrp
, cft
, file
, buf
, nbytes
, ppos
);
2431 return cgroup_read_s64(cgrp
, cft
, file
, buf
, nbytes
, ppos
);
2436 * seqfile ops/methods for returning structured data. Currently just
2437 * supports string->u64 maps, but can be extended in future.
2440 struct cgroup_seqfile_state
{
2442 struct cgroup
*cgroup
;
2445 static int cgroup_map_add(struct cgroup_map_cb
*cb
, const char *key
, u64 value
)
2447 struct seq_file
*sf
= cb
->state
;
2448 return seq_printf(sf
, "%s %llu\n", key
, (unsigned long long)value
);
2451 static int cgroup_seqfile_show(struct seq_file
*m
, void *arg
)
2453 struct cgroup_seqfile_state
*state
= m
->private;
2454 struct cftype
*cft
= state
->cft
;
2455 if (cft
->read_map
) {
2456 struct cgroup_map_cb cb
= {
2457 .fill
= cgroup_map_add
,
2460 return cft
->read_map(state
->cgroup
, cft
, &cb
);
2462 return cft
->read_seq_string(state
->cgroup
, cft
, m
);
2465 static int cgroup_seqfile_release(struct inode
*inode
, struct file
*file
)
2467 struct seq_file
*seq
= file
->private_data
;
2468 kfree(seq
->private);
2469 return single_release(inode
, file
);
2472 static const struct file_operations cgroup_seqfile_operations
= {
2474 .write
= cgroup_file_write
,
2475 .llseek
= seq_lseek
,
2476 .release
= cgroup_seqfile_release
,
2479 static int cgroup_file_open(struct inode
*inode
, struct file
*file
)
2484 err
= generic_file_open(inode
, file
);
2487 cft
= __d_cft(file
->f_dentry
);
2489 if (cft
->read_map
|| cft
->read_seq_string
) {
2490 struct cgroup_seqfile_state
*state
;
2492 state
= kzalloc(sizeof(*state
), GFP_USER
);
2497 state
->cgroup
= __d_cgrp(file
->f_dentry
->d_parent
);
2498 file
->f_op
= &cgroup_seqfile_operations
;
2499 err
= single_open(file
, cgroup_seqfile_show
, state
);
2502 } else if (cft
->open
)
2503 err
= cft
->open(inode
, file
);
2510 static int cgroup_file_release(struct inode
*inode
, struct file
*file
)
2512 struct cftype
*cft
= __d_cft(file
->f_dentry
);
2514 return cft
->release(inode
, file
);
2519 * cgroup_rename - Only allow simple rename of directories in place.
2521 static int cgroup_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
2522 struct inode
*new_dir
, struct dentry
*new_dentry
)
2525 struct cgroup_name
*name
, *old_name
;
2526 struct cgroup
*cgrp
;
2529 * It's convinient to use parent dir's i_mutex to protected
2532 lockdep_assert_held(&old_dir
->i_mutex
);
2534 if (!S_ISDIR(old_dentry
->d_inode
->i_mode
))
2536 if (new_dentry
->d_inode
)
2538 if (old_dir
!= new_dir
)
2541 cgrp
= __d_cgrp(old_dentry
);
2544 * This isn't a proper migration and its usefulness is very
2545 * limited. Disallow if sane_behavior.
2547 if (cgroup_sane_behavior(cgrp
))
2550 name
= cgroup_alloc_name(new_dentry
);
2554 ret
= simple_rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
2560 old_name
= rcu_dereference_protected(cgrp
->name
, true);
2561 rcu_assign_pointer(cgrp
->name
, name
);
2563 kfree_rcu(old_name
, rcu_head
);
2567 static struct simple_xattrs
*__d_xattrs(struct dentry
*dentry
)
2569 if (S_ISDIR(dentry
->d_inode
->i_mode
))
2570 return &__d_cgrp(dentry
)->xattrs
;
2572 return &__d_cfe(dentry
)->xattrs
;
2575 static inline int xattr_enabled(struct dentry
*dentry
)
2577 struct cgroupfs_root
*root
= dentry
->d_sb
->s_fs_info
;
2578 return root
->flags
& CGRP_ROOT_XATTR
;
2581 static bool is_valid_xattr(const char *name
)
2583 if (!strncmp(name
, XATTR_TRUSTED_PREFIX
, XATTR_TRUSTED_PREFIX_LEN
) ||
2584 !strncmp(name
, XATTR_SECURITY_PREFIX
, XATTR_SECURITY_PREFIX_LEN
))
2589 static int cgroup_setxattr(struct dentry
*dentry
, const char *name
,
2590 const void *val
, size_t size
, int flags
)
2592 if (!xattr_enabled(dentry
))
2594 if (!is_valid_xattr(name
))
2596 return simple_xattr_set(__d_xattrs(dentry
), name
, val
, size
, flags
);
2599 static int cgroup_removexattr(struct dentry
*dentry
, const char *name
)
2601 if (!xattr_enabled(dentry
))
2603 if (!is_valid_xattr(name
))
2605 return simple_xattr_remove(__d_xattrs(dentry
), name
);
2608 static ssize_t
cgroup_getxattr(struct dentry
*dentry
, const char *name
,
2609 void *buf
, size_t size
)
2611 if (!xattr_enabled(dentry
))
2613 if (!is_valid_xattr(name
))
2615 return simple_xattr_get(__d_xattrs(dentry
), name
, buf
, size
);
2618 static ssize_t
cgroup_listxattr(struct dentry
*dentry
, char *buf
, size_t size
)
2620 if (!xattr_enabled(dentry
))
2622 return simple_xattr_list(__d_xattrs(dentry
), buf
, size
);
2625 static const struct file_operations cgroup_file_operations
= {
2626 .read
= cgroup_file_read
,
2627 .write
= cgroup_file_write
,
2628 .llseek
= generic_file_llseek
,
2629 .open
= cgroup_file_open
,
2630 .release
= cgroup_file_release
,
2633 static const struct inode_operations cgroup_file_inode_operations
= {
2634 .setxattr
= cgroup_setxattr
,
2635 .getxattr
= cgroup_getxattr
,
2636 .listxattr
= cgroup_listxattr
,
2637 .removexattr
= cgroup_removexattr
,
2640 static const struct inode_operations cgroup_dir_inode_operations
= {
2641 .lookup
= cgroup_lookup
,
2642 .mkdir
= cgroup_mkdir
,
2643 .rmdir
= cgroup_rmdir
,
2644 .rename
= cgroup_rename
,
2645 .setxattr
= cgroup_setxattr
,
2646 .getxattr
= cgroup_getxattr
,
2647 .listxattr
= cgroup_listxattr
,
2648 .removexattr
= cgroup_removexattr
,
2651 static struct dentry
*cgroup_lookup(struct inode
*dir
, struct dentry
*dentry
, unsigned int flags
)
2653 if (dentry
->d_name
.len
> NAME_MAX
)
2654 return ERR_PTR(-ENAMETOOLONG
);
2655 d_add(dentry
, NULL
);
2660 * Check if a file is a control file
2662 static inline struct cftype
*__file_cft(struct file
*file
)
2664 if (file_inode(file
)->i_fop
!= &cgroup_file_operations
)
2665 return ERR_PTR(-EINVAL
);
2666 return __d_cft(file
->f_dentry
);
2669 static int cgroup_create_file(struct dentry
*dentry
, umode_t mode
,
2670 struct super_block
*sb
)
2672 struct inode
*inode
;
2676 if (dentry
->d_inode
)
2679 inode
= cgroup_new_inode(mode
, sb
);
2683 if (S_ISDIR(mode
)) {
2684 inode
->i_op
= &cgroup_dir_inode_operations
;
2685 inode
->i_fop
= &simple_dir_operations
;
2687 /* start off with i_nlink == 2 (for "." entry) */
2689 inc_nlink(dentry
->d_parent
->d_inode
);
2692 * Control reaches here with cgroup_mutex held.
2693 * @inode->i_mutex should nest outside cgroup_mutex but we
2694 * want to populate it immediately without releasing
2695 * cgroup_mutex. As @inode isn't visible to anyone else
2696 * yet, trylock will always succeed without affecting
2699 WARN_ON_ONCE(!mutex_trylock(&inode
->i_mutex
));
2700 } else if (S_ISREG(mode
)) {
2702 inode
->i_fop
= &cgroup_file_operations
;
2703 inode
->i_op
= &cgroup_file_inode_operations
;
2705 d_instantiate(dentry
, inode
);
2706 dget(dentry
); /* Extra count - pin the dentry in core */
2711 * cgroup_file_mode - deduce file mode of a control file
2712 * @cft: the control file in question
2714 * returns cft->mode if ->mode is not 0
2715 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2716 * returns S_IRUGO if it has only a read handler
2717 * returns S_IWUSR if it has only a write hander
2719 static umode_t
cgroup_file_mode(const struct cftype
*cft
)
2726 if (cft
->read
|| cft
->read_u64
|| cft
->read_s64
||
2727 cft
->read_map
|| cft
->read_seq_string
)
2730 if (cft
->write
|| cft
->write_u64
|| cft
->write_s64
||
2731 cft
->write_string
|| cft
->trigger
)
2737 static int cgroup_add_file(struct cgroup
*cgrp
, struct cgroup_subsys
*subsys
,
2740 struct dentry
*dir
= cgrp
->dentry
;
2741 struct cgroup
*parent
= __d_cgrp(dir
);
2742 struct dentry
*dentry
;
2746 char name
[MAX_CGROUP_TYPE_NAMELEN
+ MAX_CFTYPE_NAME
+ 2] = { 0 };
2748 if (subsys
&& !(cgrp
->root
->flags
& CGRP_ROOT_NOPREFIX
)) {
2749 strcpy(name
, subsys
->name
);
2752 strcat(name
, cft
->name
);
2754 BUG_ON(!mutex_is_locked(&dir
->d_inode
->i_mutex
));
2756 cfe
= kzalloc(sizeof(*cfe
), GFP_KERNEL
);
2760 dentry
= lookup_one_len(name
, dir
, strlen(name
));
2761 if (IS_ERR(dentry
)) {
2762 error
= PTR_ERR(dentry
);
2766 cfe
->type
= (void *)cft
;
2767 cfe
->dentry
= dentry
;
2768 dentry
->d_fsdata
= cfe
;
2769 simple_xattrs_init(&cfe
->xattrs
);
2771 mode
= cgroup_file_mode(cft
);
2772 error
= cgroup_create_file(dentry
, mode
| S_IFREG
, cgrp
->root
->sb
);
2774 list_add_tail(&cfe
->node
, &parent
->files
);
2783 static int cgroup_addrm_files(struct cgroup
*cgrp
, struct cgroup_subsys
*subsys
,
2784 struct cftype cfts
[], bool is_add
)
2789 for (cft
= cfts
; cft
->name
[0] != '\0'; cft
++) {
2790 /* does cft->flags tell us to skip this file on @cgrp? */
2791 if ((cft
->flags
& CFTYPE_INSANE
) && cgroup_sane_behavior(cgrp
))
2793 if ((cft
->flags
& CFTYPE_NOT_ON_ROOT
) && !cgrp
->parent
)
2795 if ((cft
->flags
& CFTYPE_ONLY_ON_ROOT
) && cgrp
->parent
)
2799 err
= cgroup_add_file(cgrp
, subsys
, cft
);
2801 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
2805 cgroup_rm_file(cgrp
, cft
);
2811 static void cgroup_cfts_prepare(void)
2812 __acquires(&cgroup_mutex
)
2815 * Thanks to the entanglement with vfs inode locking, we can't walk
2816 * the existing cgroups under cgroup_mutex and create files.
2817 * Instead, we use cgroup_for_each_descendant_pre() and drop RCU
2818 * read lock before calling cgroup_addrm_files().
2820 mutex_lock(&cgroup_mutex
);
2823 static void cgroup_cfts_commit(struct cgroup_subsys
*ss
,
2824 struct cftype
*cfts
, bool is_add
)
2825 __releases(&cgroup_mutex
)
2828 struct cgroup
*cgrp
, *root
= &ss
->root
->top_cgroup
;
2829 struct super_block
*sb
= ss
->root
->sb
;
2830 struct dentry
*prev
= NULL
;
2831 struct inode
*inode
;
2834 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
2835 if (!cfts
|| ss
->root
== &cgroup_dummy_root
||
2836 !atomic_inc_not_zero(&sb
->s_active
)) {
2837 mutex_unlock(&cgroup_mutex
);
2842 * All cgroups which are created after we drop cgroup_mutex will
2843 * have the updated set of files, so we only need to update the
2844 * cgroups created before the current @cgroup_serial_nr_next.
2846 update_before
= cgroup_serial_nr_next
;
2848 mutex_unlock(&cgroup_mutex
);
2850 /* @root always needs to be updated */
2851 inode
= root
->dentry
->d_inode
;
2852 mutex_lock(&inode
->i_mutex
);
2853 mutex_lock(&cgroup_mutex
);
2854 cgroup_addrm_files(root
, ss
, cfts
, is_add
);
2855 mutex_unlock(&cgroup_mutex
);
2856 mutex_unlock(&inode
->i_mutex
);
2858 /* add/rm files for all cgroups created before */
2860 cgroup_for_each_descendant_pre(cgrp
, root
) {
2861 if (cgroup_is_dead(cgrp
))
2864 inode
= cgrp
->dentry
->d_inode
;
2869 prev
= cgrp
->dentry
;
2871 mutex_lock(&inode
->i_mutex
);
2872 mutex_lock(&cgroup_mutex
);
2873 if (cgrp
->serial_nr
< update_before
&& !cgroup_is_dead(cgrp
))
2874 cgroup_addrm_files(cgrp
, ss
, cfts
, is_add
);
2875 mutex_unlock(&cgroup_mutex
);
2876 mutex_unlock(&inode
->i_mutex
);
2882 deactivate_super(sb
);
2886 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2887 * @ss: target cgroup subsystem
2888 * @cfts: zero-length name terminated array of cftypes
2890 * Register @cfts to @ss. Files described by @cfts are created for all
2891 * existing cgroups to which @ss is attached and all future cgroups will
2892 * have them too. This function can be called anytime whether @ss is
2895 * Returns 0 on successful registration, -errno on failure. Note that this
2896 * function currently returns 0 as long as @cfts registration is successful
2897 * even if some file creation attempts on existing cgroups fail.
2899 int cgroup_add_cftypes(struct cgroup_subsys
*ss
, struct cftype
*cfts
)
2901 struct cftype_set
*set
;
2903 set
= kzalloc(sizeof(*set
), GFP_KERNEL
);
2907 cgroup_cfts_prepare();
2909 list_add_tail(&set
->node
, &ss
->cftsets
);
2910 cgroup_cfts_commit(ss
, cfts
, true);
2914 EXPORT_SYMBOL_GPL(cgroup_add_cftypes
);
2917 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
2918 * @ss: target cgroup subsystem
2919 * @cfts: zero-length name terminated array of cftypes
2921 * Unregister @cfts from @ss. Files described by @cfts are removed from
2922 * all existing cgroups to which @ss is attached and all future cgroups
2923 * won't have them either. This function can be called anytime whether @ss
2924 * is attached or not.
2926 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
2927 * registered with @ss.
2929 int cgroup_rm_cftypes(struct cgroup_subsys
*ss
, struct cftype
*cfts
)
2931 struct cftype_set
*set
;
2933 cgroup_cfts_prepare();
2935 list_for_each_entry(set
, &ss
->cftsets
, node
) {
2936 if (set
->cfts
== cfts
) {
2937 list_del(&set
->node
);
2939 cgroup_cfts_commit(ss
, cfts
, false);
2944 cgroup_cfts_commit(ss
, NULL
, false);
2949 * cgroup_task_count - count the number of tasks in a cgroup.
2950 * @cgrp: the cgroup in question
2952 * Return the number of tasks in the cgroup.
2954 int cgroup_task_count(const struct cgroup
*cgrp
)
2957 struct cgrp_cset_link
*link
;
2959 read_lock(&css_set_lock
);
2960 list_for_each_entry(link
, &cgrp
->cset_links
, cset_link
)
2961 count
+= atomic_read(&link
->cset
->refcount
);
2962 read_unlock(&css_set_lock
);
2967 * Advance a list_head iterator. The iterator should be positioned at
2968 * the start of a css_set
2970 static void cgroup_advance_iter(struct cgroup
*cgrp
, struct cgroup_iter
*it
)
2972 struct list_head
*l
= it
->cset_link
;
2973 struct cgrp_cset_link
*link
;
2974 struct css_set
*cset
;
2976 /* Advance to the next non-empty css_set */
2979 if (l
== &cgrp
->cset_links
) {
2980 it
->cset_link
= NULL
;
2983 link
= list_entry(l
, struct cgrp_cset_link
, cset_link
);
2985 } while (list_empty(&cset
->tasks
));
2987 it
->task
= cset
->tasks
.next
;
2991 * To reduce the fork() overhead for systems that are not actually
2992 * using their cgroups capability, we don't maintain the lists running
2993 * through each css_set to its tasks until we see the list actually
2994 * used - in other words after the first call to cgroup_iter_start().
2996 static void cgroup_enable_task_cg_lists(void)
2998 struct task_struct
*p
, *g
;
2999 write_lock(&css_set_lock
);
3000 use_task_css_set_links
= 1;
3002 * We need tasklist_lock because RCU is not safe against
3003 * while_each_thread(). Besides, a forking task that has passed
3004 * cgroup_post_fork() without seeing use_task_css_set_links = 1
3005 * is not guaranteed to have its child immediately visible in the
3006 * tasklist if we walk through it with RCU.
3008 read_lock(&tasklist_lock
);
3009 do_each_thread(g
, p
) {
3012 * We should check if the process is exiting, otherwise
3013 * it will race with cgroup_exit() in that the list
3014 * entry won't be deleted though the process has exited.
3016 if (!(p
->flags
& PF_EXITING
) && list_empty(&p
->cg_list
))
3017 list_add(&p
->cg_list
, &task_css_set(p
)->tasks
);
3019 } while_each_thread(g
, p
);
3020 read_unlock(&tasklist_lock
);
3021 write_unlock(&css_set_lock
);
3025 * cgroup_next_sibling - find the next sibling of a given cgroup
3026 * @pos: the current cgroup
3028 * This function returns the next sibling of @pos and should be called
3029 * under RCU read lock. The only requirement is that @pos is accessible.
3030 * The next sibling is guaranteed to be returned regardless of @pos's
3033 struct cgroup
*cgroup_next_sibling(struct cgroup
*pos
)
3035 struct cgroup
*next
;
3037 WARN_ON_ONCE(!rcu_read_lock_held());
3040 * @pos could already have been removed. Once a cgroup is removed,
3041 * its ->sibling.next is no longer updated when its next sibling
3042 * changes. As CGRP_DEAD assertion is serialized and happens
3043 * before the cgroup is taken off the ->sibling list, if we see it
3044 * unasserted, it's guaranteed that the next sibling hasn't
3045 * finished its grace period even if it's already removed, and thus
3046 * safe to dereference from this RCU critical section. If
3047 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
3048 * to be visible as %true here.
3050 if (likely(!cgroup_is_dead(pos
))) {
3051 next
= list_entry_rcu(pos
->sibling
.next
, struct cgroup
, sibling
);
3052 if (&next
->sibling
!= &pos
->parent
->children
)
3058 * Can't dereference the next pointer. Each cgroup is given a
3059 * monotonically increasing unique serial number and always
3060 * appended to the sibling list, so the next one can be found by
3061 * walking the parent's children until we see a cgroup with higher
3062 * serial number than @pos's.
3064 * While this path can be slow, it's taken only when either the
3065 * current cgroup is removed or iteration and removal race.
3067 list_for_each_entry_rcu(next
, &pos
->parent
->children
, sibling
)
3068 if (next
->serial_nr
> pos
->serial_nr
)
3072 EXPORT_SYMBOL_GPL(cgroup_next_sibling
);
3075 * cgroup_next_descendant_pre - find the next descendant for pre-order walk
3076 * @pos: the current position (%NULL to initiate traversal)
3077 * @cgroup: cgroup whose descendants to walk
3079 * To be used by cgroup_for_each_descendant_pre(). Find the next
3080 * descendant to visit for pre-order traversal of @cgroup's descendants.
3082 * While this function requires RCU read locking, it doesn't require the
3083 * whole traversal to be contained in a single RCU critical section. This
3084 * function will return the correct next descendant as long as both @pos
3085 * and @cgroup are accessible and @pos is a descendant of @cgroup.
3087 struct cgroup
*cgroup_next_descendant_pre(struct cgroup
*pos
,
3088 struct cgroup
*cgroup
)
3090 struct cgroup
*next
;
3092 WARN_ON_ONCE(!rcu_read_lock_held());
3094 /* if first iteration, pretend we just visited @cgroup */
3098 /* visit the first child if exists */
3099 next
= list_first_or_null_rcu(&pos
->children
, struct cgroup
, sibling
);
3103 /* no child, visit my or the closest ancestor's next sibling */
3104 while (pos
!= cgroup
) {
3105 next
= cgroup_next_sibling(pos
);
3113 EXPORT_SYMBOL_GPL(cgroup_next_descendant_pre
);
3116 * cgroup_rightmost_descendant - return the rightmost descendant of a cgroup
3117 * @pos: cgroup of interest
3119 * Return the rightmost descendant of @pos. If there's no descendant,
3120 * @pos is returned. This can be used during pre-order traversal to skip
3123 * While this function requires RCU read locking, it doesn't require the
3124 * whole traversal to be contained in a single RCU critical section. This
3125 * function will return the correct rightmost descendant as long as @pos is
3128 struct cgroup
*cgroup_rightmost_descendant(struct cgroup
*pos
)
3130 struct cgroup
*last
, *tmp
;
3132 WARN_ON_ONCE(!rcu_read_lock_held());
3136 /* ->prev isn't RCU safe, walk ->next till the end */
3138 list_for_each_entry_rcu(tmp
, &last
->children
, sibling
)
3144 EXPORT_SYMBOL_GPL(cgroup_rightmost_descendant
);
3146 static struct cgroup
*cgroup_leftmost_descendant(struct cgroup
*pos
)
3148 struct cgroup
*last
;
3152 pos
= list_first_or_null_rcu(&pos
->children
, struct cgroup
,
3160 * cgroup_next_descendant_post - find the next descendant for post-order walk
3161 * @pos: the current position (%NULL to initiate traversal)
3162 * @cgroup: cgroup whose descendants to walk
3164 * To be used by cgroup_for_each_descendant_post(). Find the next
3165 * descendant to visit for post-order traversal of @cgroup's descendants.
3167 * While this function requires RCU read locking, it doesn't require the
3168 * whole traversal to be contained in a single RCU critical section. This
3169 * function will return the correct next descendant as long as both @pos
3170 * and @cgroup are accessible and @pos is a descendant of @cgroup.
3172 struct cgroup
*cgroup_next_descendant_post(struct cgroup
*pos
,
3173 struct cgroup
*cgroup
)
3175 struct cgroup
*next
;
3177 WARN_ON_ONCE(!rcu_read_lock_held());
3179 /* if first iteration, visit the leftmost descendant */
3181 next
= cgroup_leftmost_descendant(cgroup
);
3182 return next
!= cgroup
? next
: NULL
;
3185 /* if there's an unvisited sibling, visit its leftmost descendant */
3186 next
= cgroup_next_sibling(pos
);
3188 return cgroup_leftmost_descendant(next
);
3190 /* no sibling left, visit parent */
3192 return next
!= cgroup
? next
: NULL
;
3194 EXPORT_SYMBOL_GPL(cgroup_next_descendant_post
);
3196 void cgroup_iter_start(struct cgroup
*cgrp
, struct cgroup_iter
*it
)
3197 __acquires(css_set_lock
)
3200 * The first time anyone tries to iterate across a cgroup,
3201 * we need to enable the list linking each css_set to its
3202 * tasks, and fix up all existing tasks.
3204 if (!use_task_css_set_links
)
3205 cgroup_enable_task_cg_lists();
3207 read_lock(&css_set_lock
);
3208 it
->cset_link
= &cgrp
->cset_links
;
3209 cgroup_advance_iter(cgrp
, it
);
3212 struct task_struct
*cgroup_iter_next(struct cgroup
*cgrp
,
3213 struct cgroup_iter
*it
)
3215 struct task_struct
*res
;
3216 struct list_head
*l
= it
->task
;
3217 struct cgrp_cset_link
*link
;
3219 /* If the iterator cg is NULL, we have no tasks */
3222 res
= list_entry(l
, struct task_struct
, cg_list
);
3223 /* Advance iterator to find next entry */
3225 link
= list_entry(it
->cset_link
, struct cgrp_cset_link
, cset_link
);
3226 if (l
== &link
->cset
->tasks
) {
3227 /* We reached the end of this task list - move on to
3228 * the next cg_cgroup_link */
3229 cgroup_advance_iter(cgrp
, it
);
3236 void cgroup_iter_end(struct cgroup
*cgrp
, struct cgroup_iter
*it
)
3237 __releases(css_set_lock
)
3239 read_unlock(&css_set_lock
);
3242 static inline int started_after_time(struct task_struct
*t1
,
3243 struct timespec
*time
,
3244 struct task_struct
*t2
)
3246 int start_diff
= timespec_compare(&t1
->start_time
, time
);
3247 if (start_diff
> 0) {
3249 } else if (start_diff
< 0) {
3253 * Arbitrarily, if two processes started at the same
3254 * time, we'll say that the lower pointer value
3255 * started first. Note that t2 may have exited by now
3256 * so this may not be a valid pointer any longer, but
3257 * that's fine - it still serves to distinguish
3258 * between two tasks started (effectively) simultaneously.
3265 * This function is a callback from heap_insert() and is used to order
3267 * In this case we order the heap in descending task start time.
3269 static inline int started_after(void *p1
, void *p2
)
3271 struct task_struct
*t1
= p1
;
3272 struct task_struct
*t2
= p2
;
3273 return started_after_time(t1
, &t2
->start_time
, t2
);
3277 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
3278 * @scan: struct cgroup_scanner containing arguments for the scan
3280 * Arguments include pointers to callback functions test_task() and
3282 * Iterate through all the tasks in a cgroup, calling test_task() for each,
3283 * and if it returns true, call process_task() for it also.
3284 * The test_task pointer may be NULL, meaning always true (select all tasks).
3285 * Effectively duplicates cgroup_iter_{start,next,end}()
3286 * but does not lock css_set_lock for the call to process_task().
3287 * The struct cgroup_scanner may be embedded in any structure of the caller's
3289 * It is guaranteed that process_task() will act on every task that
3290 * is a member of the cgroup for the duration of this call. This
3291 * function may or may not call process_task() for tasks that exit
3292 * or move to a different cgroup during the call, or are forked or
3293 * move into the cgroup during the call.
3295 * Note that test_task() may be called with locks held, and may in some
3296 * situations be called multiple times for the same task, so it should
3298 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
3299 * pre-allocated and will be used for heap operations (and its "gt" member will
3300 * be overwritten), else a temporary heap will be used (allocation of which
3301 * may cause this function to fail).
3303 int cgroup_scan_tasks(struct cgroup_scanner
*scan
)
3306 struct cgroup_iter it
;
3307 struct task_struct
*p
, *dropped
;
3308 /* Never dereference latest_task, since it's not refcounted */
3309 struct task_struct
*latest_task
= NULL
;
3310 struct ptr_heap tmp_heap
;
3311 struct ptr_heap
*heap
;
3312 struct timespec latest_time
= { 0, 0 };
3315 /* The caller supplied our heap and pre-allocated its memory */
3317 heap
->gt
= &started_after
;
3319 /* We need to allocate our own heap memory */
3321 retval
= heap_init(heap
, PAGE_SIZE
, GFP_KERNEL
, &started_after
);
3323 /* cannot allocate the heap */
3329 * Scan tasks in the cgroup, using the scanner's "test_task" callback
3330 * to determine which are of interest, and using the scanner's
3331 * "process_task" callback to process any of them that need an update.
3332 * Since we don't want to hold any locks during the task updates,
3333 * gather tasks to be processed in a heap structure.
3334 * The heap is sorted by descending task start time.
3335 * If the statically-sized heap fills up, we overflow tasks that
3336 * started later, and in future iterations only consider tasks that
3337 * started after the latest task in the previous pass. This
3338 * guarantees forward progress and that we don't miss any tasks.
3341 cgroup_iter_start(scan
->cg
, &it
);
3342 while ((p
= cgroup_iter_next(scan
->cg
, &it
))) {
3344 * Only affect tasks that qualify per the caller's callback,
3345 * if he provided one
3347 if (scan
->test_task
&& !scan
->test_task(p
, scan
))
3350 * Only process tasks that started after the last task
3353 if (!started_after_time(p
, &latest_time
, latest_task
))
3355 dropped
= heap_insert(heap
, p
);
3356 if (dropped
== NULL
) {
3358 * The new task was inserted; the heap wasn't
3362 } else if (dropped
!= p
) {
3364 * The new task was inserted, and pushed out a
3368 put_task_struct(dropped
);
3371 * Else the new task was newer than anything already in
3372 * the heap and wasn't inserted
3375 cgroup_iter_end(scan
->cg
, &it
);
3378 for (i
= 0; i
< heap
->size
; i
++) {
3379 struct task_struct
*q
= heap
->ptrs
[i
];
3381 latest_time
= q
->start_time
;
3384 /* Process the task per the caller's callback */
3385 scan
->process_task(q
, scan
);
3389 * If we had to process any tasks at all, scan again
3390 * in case some of them were in the middle of forking
3391 * children that didn't get processed.
3392 * Not the most efficient way to do it, but it avoids
3393 * having to take callback_mutex in the fork path
3397 if (heap
== &tmp_heap
)
3398 heap_free(&tmp_heap
);
3402 static void cgroup_transfer_one_task(struct task_struct
*task
,
3403 struct cgroup_scanner
*scan
)
3405 struct cgroup
*new_cgroup
= scan
->data
;
3407 mutex_lock(&cgroup_mutex
);
3408 cgroup_attach_task(new_cgroup
, task
, false);
3409 mutex_unlock(&cgroup_mutex
);
3413 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3414 * @to: cgroup to which the tasks will be moved
3415 * @from: cgroup in which the tasks currently reside
3417 int cgroup_transfer_tasks(struct cgroup
*to
, struct cgroup
*from
)
3419 struct cgroup_scanner scan
;
3422 scan
.test_task
= NULL
; /* select all tasks in cgroup */
3423 scan
.process_task
= cgroup_transfer_one_task
;
3427 return cgroup_scan_tasks(&scan
);
3431 * Stuff for reading the 'tasks'/'procs' files.
3433 * Reading this file can return large amounts of data if a cgroup has
3434 * *lots* of attached tasks. So it may need several calls to read(),
3435 * but we cannot guarantee that the information we produce is correct
3436 * unless we produce it entirely atomically.
3440 /* which pidlist file are we talking about? */
3441 enum cgroup_filetype
{
3447 * A pidlist is a list of pids that virtually represents the contents of one
3448 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3449 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3452 struct cgroup_pidlist
{
3454 * used to find which pidlist is wanted. doesn't change as long as
3455 * this particular list stays in the list.
3457 struct { enum cgroup_filetype type
; struct pid_namespace
*ns
; } key
;
3460 /* how many elements the above list has */
3462 /* how many files are using the current array */
3464 /* each of these stored in a list by its cgroup */
3465 struct list_head links
;
3466 /* pointer to the cgroup we belong to, for list removal purposes */
3467 struct cgroup
*owner
;
3468 /* protects the other fields */
3469 struct rw_semaphore mutex
;
3473 * The following two functions "fix" the issue where there are more pids
3474 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3475 * TODO: replace with a kernel-wide solution to this problem
3477 #define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3478 static void *pidlist_allocate(int count
)
3480 if (PIDLIST_TOO_LARGE(count
))
3481 return vmalloc(count
* sizeof(pid_t
));
3483 return kmalloc(count
* sizeof(pid_t
), GFP_KERNEL
);
3485 static void pidlist_free(void *p
)
3487 if (is_vmalloc_addr(p
))
3494 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
3495 * Returns the number of unique elements.
3497 static int pidlist_uniq(pid_t
*list
, int length
)
3502 * we presume the 0th element is unique, so i starts at 1. trivial
3503 * edge cases first; no work needs to be done for either
3505 if (length
== 0 || length
== 1)
3507 /* src and dest walk down the list; dest counts unique elements */
3508 for (src
= 1; src
< length
; src
++) {
3509 /* find next unique element */
3510 while (list
[src
] == list
[src
-1]) {
3515 /* dest always points to where the next unique element goes */
3516 list
[dest
] = list
[src
];
3523 static int cmppid(const void *a
, const void *b
)
3525 return *(pid_t
*)a
- *(pid_t
*)b
;
3529 * find the appropriate pidlist for our purpose (given procs vs tasks)
3530 * returns with the lock on that pidlist already held, and takes care
3531 * of the use count, or returns NULL with no locks held if we're out of
3534 static struct cgroup_pidlist
*cgroup_pidlist_find(struct cgroup
*cgrp
,
3535 enum cgroup_filetype type
)
3537 struct cgroup_pidlist
*l
;
3538 /* don't need task_nsproxy() if we're looking at ourself */
3539 struct pid_namespace
*ns
= task_active_pid_ns(current
);
3542 * We can't drop the pidlist_mutex before taking the l->mutex in case
3543 * the last ref-holder is trying to remove l from the list at the same
3544 * time. Holding the pidlist_mutex precludes somebody taking whichever
3545 * list we find out from under us - compare release_pid_array().
3547 mutex_lock(&cgrp
->pidlist_mutex
);
3548 list_for_each_entry(l
, &cgrp
->pidlists
, links
) {
3549 if (l
->key
.type
== type
&& l
->key
.ns
== ns
) {
3550 /* make sure l doesn't vanish out from under us */
3551 down_write(&l
->mutex
);
3552 mutex_unlock(&cgrp
->pidlist_mutex
);
3556 /* entry not found; create a new one */
3557 l
= kzalloc(sizeof(struct cgroup_pidlist
), GFP_KERNEL
);
3559 mutex_unlock(&cgrp
->pidlist_mutex
);
3562 init_rwsem(&l
->mutex
);
3563 down_write(&l
->mutex
);
3565 l
->key
.ns
= get_pid_ns(ns
);
3567 list_add(&l
->links
, &cgrp
->pidlists
);
3568 mutex_unlock(&cgrp
->pidlist_mutex
);
3573 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3575 static int pidlist_array_load(struct cgroup
*cgrp
, enum cgroup_filetype type
,
3576 struct cgroup_pidlist
**lp
)
3580 int pid
, n
= 0; /* used for populating the array */
3581 struct cgroup_iter it
;
3582 struct task_struct
*tsk
;
3583 struct cgroup_pidlist
*l
;
3586 * If cgroup gets more users after we read count, we won't have
3587 * enough space - tough. This race is indistinguishable to the
3588 * caller from the case that the additional cgroup users didn't
3589 * show up until sometime later on.
3591 length
= cgroup_task_count(cgrp
);
3592 array
= pidlist_allocate(length
);
3595 /* now, populate the array */
3596 cgroup_iter_start(cgrp
, &it
);
3597 while ((tsk
= cgroup_iter_next(cgrp
, &it
))) {
3598 if (unlikely(n
== length
))
3600 /* get tgid or pid for procs or tasks file respectively */
3601 if (type
== CGROUP_FILE_PROCS
)
3602 pid
= task_tgid_vnr(tsk
);
3604 pid
= task_pid_vnr(tsk
);
3605 if (pid
> 0) /* make sure to only use valid results */
3608 cgroup_iter_end(cgrp
, &it
);
3610 /* now sort & (if procs) strip out duplicates */
3611 sort(array
, length
, sizeof(pid_t
), cmppid
, NULL
);
3612 if (type
== CGROUP_FILE_PROCS
)
3613 length
= pidlist_uniq(array
, length
);
3614 l
= cgroup_pidlist_find(cgrp
, type
);
3616 pidlist_free(array
);
3619 /* store array, freeing old if necessary - lock already held */
3620 pidlist_free(l
->list
);
3624 up_write(&l
->mutex
);
3630 * cgroupstats_build - build and fill cgroupstats
3631 * @stats: cgroupstats to fill information into
3632 * @dentry: A dentry entry belonging to the cgroup for which stats have
3635 * Build and fill cgroupstats so that taskstats can export it to user
3638 int cgroupstats_build(struct cgroupstats
*stats
, struct dentry
*dentry
)
3641 struct cgroup
*cgrp
;
3642 struct cgroup_iter it
;
3643 struct task_struct
*tsk
;
3646 * Validate dentry by checking the superblock operations,
3647 * and make sure it's a directory.
3649 if (dentry
->d_sb
->s_op
!= &cgroup_ops
||
3650 !S_ISDIR(dentry
->d_inode
->i_mode
))
3654 cgrp
= dentry
->d_fsdata
;
3656 cgroup_iter_start(cgrp
, &it
);
3657 while ((tsk
= cgroup_iter_next(cgrp
, &it
))) {
3658 switch (tsk
->state
) {
3660 stats
->nr_running
++;
3662 case TASK_INTERRUPTIBLE
:
3663 stats
->nr_sleeping
++;
3665 case TASK_UNINTERRUPTIBLE
:
3666 stats
->nr_uninterruptible
++;
3669 stats
->nr_stopped
++;
3672 if (delayacct_is_task_waiting_on_io(tsk
))
3673 stats
->nr_io_wait
++;
3677 cgroup_iter_end(cgrp
, &it
);
3685 * seq_file methods for the tasks/procs files. The seq_file position is the
3686 * next pid to display; the seq_file iterator is a pointer to the pid
3687 * in the cgroup->l->list array.
3690 static void *cgroup_pidlist_start(struct seq_file
*s
, loff_t
*pos
)
3693 * Initially we receive a position value that corresponds to
3694 * one more than the last pid shown (or 0 on the first call or
3695 * after a seek to the start). Use a binary-search to find the
3696 * next pid to display, if any
3698 struct cgroup_pidlist
*l
= s
->private;
3699 int index
= 0, pid
= *pos
;
3702 down_read(&l
->mutex
);
3704 int end
= l
->length
;
3706 while (index
< end
) {
3707 int mid
= (index
+ end
) / 2;
3708 if (l
->list
[mid
] == pid
) {
3711 } else if (l
->list
[mid
] <= pid
)
3717 /* If we're off the end of the array, we're done */
3718 if (index
>= l
->length
)
3720 /* Update the abstract position to be the actual pid that we found */
3721 iter
= l
->list
+ index
;
3726 static void cgroup_pidlist_stop(struct seq_file
*s
, void *v
)
3728 struct cgroup_pidlist
*l
= s
->private;
3732 static void *cgroup_pidlist_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
3734 struct cgroup_pidlist
*l
= s
->private;
3736 pid_t
*end
= l
->list
+ l
->length
;
3738 * Advance to the next pid in the array. If this goes off the
3750 static int cgroup_pidlist_show(struct seq_file
*s
, void *v
)
3752 return seq_printf(s
, "%d\n", *(int *)v
);
3756 * seq_operations functions for iterating on pidlists through seq_file -
3757 * independent of whether it's tasks or procs
3759 static const struct seq_operations cgroup_pidlist_seq_operations
= {
3760 .start
= cgroup_pidlist_start
,
3761 .stop
= cgroup_pidlist_stop
,
3762 .next
= cgroup_pidlist_next
,
3763 .show
= cgroup_pidlist_show
,
3766 static void cgroup_release_pid_array(struct cgroup_pidlist
*l
)
3769 * the case where we're the last user of this particular pidlist will
3770 * have us remove it from the cgroup's list, which entails taking the
3771 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3772 * pidlist_mutex, we have to take pidlist_mutex first.
3774 mutex_lock(&l
->owner
->pidlist_mutex
);
3775 down_write(&l
->mutex
);
3776 BUG_ON(!l
->use_count
);
3777 if (!--l
->use_count
) {
3778 /* we're the last user if refcount is 0; remove and free */
3779 list_del(&l
->links
);
3780 mutex_unlock(&l
->owner
->pidlist_mutex
);
3781 pidlist_free(l
->list
);
3782 put_pid_ns(l
->key
.ns
);
3783 up_write(&l
->mutex
);
3787 mutex_unlock(&l
->owner
->pidlist_mutex
);
3788 up_write(&l
->mutex
);
3791 static int cgroup_pidlist_release(struct inode
*inode
, struct file
*file
)
3793 struct cgroup_pidlist
*l
;
3794 if (!(file
->f_mode
& FMODE_READ
))
3797 * the seq_file will only be initialized if the file was opened for
3798 * reading; hence we check if it's not null only in that case.
3800 l
= ((struct seq_file
*)file
->private_data
)->private;
3801 cgroup_release_pid_array(l
);
3802 return seq_release(inode
, file
);
3805 static const struct file_operations cgroup_pidlist_operations
= {
3807 .llseek
= seq_lseek
,
3808 .write
= cgroup_file_write
,
3809 .release
= cgroup_pidlist_release
,
3813 * The following functions handle opens on a file that displays a pidlist
3814 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3817 /* helper function for the two below it */
3818 static int cgroup_pidlist_open(struct file
*file
, enum cgroup_filetype type
)
3820 struct cgroup
*cgrp
= __d_cgrp(file
->f_dentry
->d_parent
);
3821 struct cgroup_pidlist
*l
;
3824 /* Nothing to do for write-only files */
3825 if (!(file
->f_mode
& FMODE_READ
))
3828 /* have the array populated */
3829 retval
= pidlist_array_load(cgrp
, type
, &l
);
3832 /* configure file information */
3833 file
->f_op
= &cgroup_pidlist_operations
;
3835 retval
= seq_open(file
, &cgroup_pidlist_seq_operations
);
3837 cgroup_release_pid_array(l
);
3840 ((struct seq_file
*)file
->private_data
)->private = l
;
3843 static int cgroup_tasks_open(struct inode
*unused
, struct file
*file
)
3845 return cgroup_pidlist_open(file
, CGROUP_FILE_TASKS
);
3847 static int cgroup_procs_open(struct inode
*unused
, struct file
*file
)
3849 return cgroup_pidlist_open(file
, CGROUP_FILE_PROCS
);
3852 static u64
cgroup_read_notify_on_release(struct cgroup
*cgrp
,
3855 return notify_on_release(cgrp
);
3858 static int cgroup_write_notify_on_release(struct cgroup
*cgrp
,
3862 clear_bit(CGRP_RELEASABLE
, &cgrp
->flags
);
3864 set_bit(CGRP_NOTIFY_ON_RELEASE
, &cgrp
->flags
);
3866 clear_bit(CGRP_NOTIFY_ON_RELEASE
, &cgrp
->flags
);
3871 * When dput() is called asynchronously, if umount has been done and
3872 * then deactivate_super() in cgroup_free_fn() kills the superblock,
3873 * there's a small window that vfs will see the root dentry with non-zero
3874 * refcnt and trigger BUG().
3876 * That's why we hold a reference before dput() and drop it right after.
3878 static void cgroup_dput(struct cgroup
*cgrp
)
3880 struct super_block
*sb
= cgrp
->root
->sb
;
3882 atomic_inc(&sb
->s_active
);
3884 deactivate_super(sb
);
3888 * Unregister event and free resources.
3890 * Gets called from workqueue.
3892 static void cgroup_event_remove(struct work_struct
*work
)
3894 struct cgroup_event
*event
= container_of(work
, struct cgroup_event
,
3896 struct cgroup
*cgrp
= event
->cgrp
;
3898 remove_wait_queue(event
->wqh
, &event
->wait
);
3900 event
->cft
->unregister_event(cgrp
, event
->cft
, event
->eventfd
);
3902 /* Notify userspace the event is going away. */
3903 eventfd_signal(event
->eventfd
, 1);
3905 eventfd_ctx_put(event
->eventfd
);
3911 * Gets called on POLLHUP on eventfd when user closes it.
3913 * Called with wqh->lock held and interrupts disabled.
3915 static int cgroup_event_wake(wait_queue_t
*wait
, unsigned mode
,
3916 int sync
, void *key
)
3918 struct cgroup_event
*event
= container_of(wait
,
3919 struct cgroup_event
, wait
);
3920 struct cgroup
*cgrp
= event
->cgrp
;
3921 unsigned long flags
= (unsigned long)key
;
3923 if (flags
& POLLHUP
) {
3925 * If the event has been detached at cgroup removal, we
3926 * can simply return knowing the other side will cleanup
3929 * We can't race against event freeing since the other
3930 * side will require wqh->lock via remove_wait_queue(),
3933 spin_lock(&cgrp
->event_list_lock
);
3934 if (!list_empty(&event
->list
)) {
3935 list_del_init(&event
->list
);
3937 * We are in atomic context, but cgroup_event_remove()
3938 * may sleep, so we have to call it in workqueue.
3940 schedule_work(&event
->remove
);
3942 spin_unlock(&cgrp
->event_list_lock
);
3948 static void cgroup_event_ptable_queue_proc(struct file
*file
,
3949 wait_queue_head_t
*wqh
, poll_table
*pt
)
3951 struct cgroup_event
*event
= container_of(pt
,
3952 struct cgroup_event
, pt
);
3955 add_wait_queue(wqh
, &event
->wait
);
3959 * Parse input and register new cgroup event handler.
3961 * Input must be in format '<event_fd> <control_fd> <args>'.
3962 * Interpretation of args is defined by control file implementation.
3964 static int cgroup_write_event_control(struct cgroup
*cgrp
, struct cftype
*cft
,
3967 struct cgroup_event
*event
= NULL
;
3968 struct cgroup
*cgrp_cfile
;
3969 unsigned int efd
, cfd
;
3970 struct file
*efile
= NULL
;
3971 struct file
*cfile
= NULL
;
3975 efd
= simple_strtoul(buffer
, &endp
, 10);
3980 cfd
= simple_strtoul(buffer
, &endp
, 10);
3981 if ((*endp
!= ' ') && (*endp
!= '\0'))
3985 event
= kzalloc(sizeof(*event
), GFP_KERNEL
);
3989 INIT_LIST_HEAD(&event
->list
);
3990 init_poll_funcptr(&event
->pt
, cgroup_event_ptable_queue_proc
);
3991 init_waitqueue_func_entry(&event
->wait
, cgroup_event_wake
);
3992 INIT_WORK(&event
->remove
, cgroup_event_remove
);
3994 efile
= eventfd_fget(efd
);
3995 if (IS_ERR(efile
)) {
3996 ret
= PTR_ERR(efile
);
4000 event
->eventfd
= eventfd_ctx_fileget(efile
);
4001 if (IS_ERR(event
->eventfd
)) {
4002 ret
= PTR_ERR(event
->eventfd
);
4012 /* the process need read permission on control file */
4013 /* AV: shouldn't we check that it's been opened for read instead? */
4014 ret
= inode_permission(file_inode(cfile
), MAY_READ
);
4018 event
->cft
= __file_cft(cfile
);
4019 if (IS_ERR(event
->cft
)) {
4020 ret
= PTR_ERR(event
->cft
);
4025 * The file to be monitored must be in the same cgroup as
4026 * cgroup.event_control is.
4028 cgrp_cfile
= __d_cgrp(cfile
->f_dentry
->d_parent
);
4029 if (cgrp_cfile
!= cgrp
) {
4034 if (!event
->cft
->register_event
|| !event
->cft
->unregister_event
) {
4039 ret
= event
->cft
->register_event(cgrp
, event
->cft
,
4040 event
->eventfd
, buffer
);
4044 efile
->f_op
->poll(efile
, &event
->pt
);
4047 * Events should be removed after rmdir of cgroup directory, but before
4048 * destroying subsystem state objects. Let's take reference to cgroup
4049 * directory dentry to do that.
4053 spin_lock(&cgrp
->event_list_lock
);
4054 list_add(&event
->list
, &cgrp
->event_list
);
4055 spin_unlock(&cgrp
->event_list_lock
);
4066 if (event
&& event
->eventfd
&& !IS_ERR(event
->eventfd
))
4067 eventfd_ctx_put(event
->eventfd
);
4069 if (!IS_ERR_OR_NULL(efile
))
4077 static u64
cgroup_clone_children_read(struct cgroup
*cgrp
,
4080 return test_bit(CGRP_CPUSET_CLONE_CHILDREN
, &cgrp
->flags
);
4083 static int cgroup_clone_children_write(struct cgroup
*cgrp
,
4088 set_bit(CGRP_CPUSET_CLONE_CHILDREN
, &cgrp
->flags
);
4090 clear_bit(CGRP_CPUSET_CLONE_CHILDREN
, &cgrp
->flags
);
4094 static struct cftype cgroup_base_files
[] = {
4096 .name
= "cgroup.procs",
4097 .open
= cgroup_procs_open
,
4098 .write_u64
= cgroup_procs_write
,
4099 .release
= cgroup_pidlist_release
,
4100 .mode
= S_IRUGO
| S_IWUSR
,
4103 .name
= "cgroup.event_control",
4104 .write_string
= cgroup_write_event_control
,
4108 .name
= "cgroup.clone_children",
4109 .flags
= CFTYPE_INSANE
,
4110 .read_u64
= cgroup_clone_children_read
,
4111 .write_u64
= cgroup_clone_children_write
,
4114 .name
= "cgroup.sane_behavior",
4115 .flags
= CFTYPE_ONLY_ON_ROOT
,
4116 .read_seq_string
= cgroup_sane_behavior_show
,
4120 * Historical crazy stuff. These don't have "cgroup." prefix and
4121 * don't exist if sane_behavior. If you're depending on these, be
4122 * prepared to be burned.
4126 .flags
= CFTYPE_INSANE
, /* use "procs" instead */
4127 .open
= cgroup_tasks_open
,
4128 .write_u64
= cgroup_tasks_write
,
4129 .release
= cgroup_pidlist_release
,
4130 .mode
= S_IRUGO
| S_IWUSR
,
4133 .name
= "notify_on_release",
4134 .flags
= CFTYPE_INSANE
,
4135 .read_u64
= cgroup_read_notify_on_release
,
4136 .write_u64
= cgroup_write_notify_on_release
,
4139 .name
= "release_agent",
4140 .flags
= CFTYPE_INSANE
| CFTYPE_ONLY_ON_ROOT
,
4141 .read_seq_string
= cgroup_release_agent_show
,
4142 .write_string
= cgroup_release_agent_write
,
4143 .max_write_len
= PATH_MAX
,
4149 * cgroup_populate_dir - selectively creation of files in a directory
4150 * @cgrp: target cgroup
4151 * @base_files: true if the base files should be added
4152 * @subsys_mask: mask of the subsystem ids whose files should be added
4154 static int cgroup_populate_dir(struct cgroup
*cgrp
, bool base_files
,
4155 unsigned long subsys_mask
)
4158 struct cgroup_subsys
*ss
;
4161 err
= cgroup_addrm_files(cgrp
, NULL
, cgroup_base_files
, true);
4166 /* process cftsets of each subsystem */
4167 for_each_root_subsys(cgrp
->root
, ss
) {
4168 struct cftype_set
*set
;
4169 if (!test_bit(ss
->subsys_id
, &subsys_mask
))
4172 list_for_each_entry(set
, &ss
->cftsets
, node
)
4173 cgroup_addrm_files(cgrp
, ss
, set
->cfts
, true);
4176 /* This cgroup is ready now */
4177 for_each_root_subsys(cgrp
->root
, ss
) {
4178 struct cgroup_subsys_state
*css
= cgrp
->subsys
[ss
->subsys_id
];
4179 struct css_id
*id
= rcu_dereference_protected(css
->id
, true);
4182 * Update id->css pointer and make this css visible from
4183 * CSS ID functions. This pointer will be dereferened
4184 * from RCU-read-side without locks.
4187 rcu_assign_pointer(id
->css
, css
);
4193 static void css_dput_fn(struct work_struct
*work
)
4195 struct cgroup_subsys_state
*css
=
4196 container_of(work
, struct cgroup_subsys_state
, dput_work
);
4198 cgroup_dput(css
->cgroup
);
4201 static void css_release(struct percpu_ref
*ref
)
4203 struct cgroup_subsys_state
*css
=
4204 container_of(ref
, struct cgroup_subsys_state
, refcnt
);
4206 schedule_work(&css
->dput_work
);
4209 static void init_cgroup_css(struct cgroup_subsys_state
*css
,
4210 struct cgroup_subsys
*ss
,
4211 struct cgroup
*cgrp
)
4216 if (cgrp
== cgroup_dummy_top
)
4217 css
->flags
|= CSS_ROOT
;
4218 BUG_ON(cgrp
->subsys
[ss
->subsys_id
]);
4219 cgrp
->subsys
[ss
->subsys_id
] = css
;
4222 * css holds an extra ref to @cgrp->dentry which is put on the last
4223 * css_put(). dput() requires process context, which css_put() may
4224 * be called without. @css->dput_work will be used to invoke
4225 * dput() asynchronously from css_put().
4227 INIT_WORK(&css
->dput_work
, css_dput_fn
);
4230 /* invoke ->post_create() on a new CSS and mark it online if successful */
4231 static int online_css(struct cgroup_subsys
*ss
, struct cgroup
*cgrp
)
4235 lockdep_assert_held(&cgroup_mutex
);
4238 ret
= ss
->css_online(cgrp
);
4240 cgrp
->subsys
[ss
->subsys_id
]->flags
|= CSS_ONLINE
;
4244 /* if the CSS is online, invoke ->pre_destory() on it and mark it offline */
4245 static void offline_css(struct cgroup_subsys
*ss
, struct cgroup
*cgrp
)
4246 __releases(&cgroup_mutex
) __acquires(&cgroup_mutex
)
4248 struct cgroup_subsys_state
*css
= cgrp
->subsys
[ss
->subsys_id
];
4250 lockdep_assert_held(&cgroup_mutex
);
4252 if (!(css
->flags
& CSS_ONLINE
))
4255 if (ss
->css_offline
)
4256 ss
->css_offline(cgrp
);
4258 cgrp
->subsys
[ss
->subsys_id
]->flags
&= ~CSS_ONLINE
;
4262 * cgroup_create - create a cgroup
4263 * @parent: cgroup that will be parent of the new cgroup
4264 * @dentry: dentry of the new cgroup
4265 * @mode: mode to set on new inode
4267 * Must be called with the mutex on the parent inode held
4269 static long cgroup_create(struct cgroup
*parent
, struct dentry
*dentry
,
4272 struct cgroup
*cgrp
;
4273 struct cgroup_name
*name
;
4274 struct cgroupfs_root
*root
= parent
->root
;
4276 struct cgroup_subsys
*ss
;
4277 struct super_block
*sb
= root
->sb
;
4279 /* allocate the cgroup and its ID, 0 is reserved for the root */
4280 cgrp
= kzalloc(sizeof(*cgrp
), GFP_KERNEL
);
4284 name
= cgroup_alloc_name(dentry
);
4287 rcu_assign_pointer(cgrp
->name
, name
);
4289 cgrp
->id
= ida_simple_get(&root
->cgroup_ida
, 1, 0, GFP_KERNEL
);
4294 * Only live parents can have children. Note that the liveliness
4295 * check isn't strictly necessary because cgroup_mkdir() and
4296 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4297 * anyway so that locking is contained inside cgroup proper and we
4298 * don't get nasty surprises if we ever grow another caller.
4300 if (!cgroup_lock_live_group(parent
)) {
4305 /* Grab a reference on the superblock so the hierarchy doesn't
4306 * get deleted on unmount if there are child cgroups. This
4307 * can be done outside cgroup_mutex, since the sb can't
4308 * disappear while someone has an open control file on the
4310 atomic_inc(&sb
->s_active
);
4312 init_cgroup_housekeeping(cgrp
);
4314 dentry
->d_fsdata
= cgrp
;
4315 cgrp
->dentry
= dentry
;
4317 cgrp
->parent
= parent
;
4318 cgrp
->root
= parent
->root
;
4320 if (notify_on_release(parent
))
4321 set_bit(CGRP_NOTIFY_ON_RELEASE
, &cgrp
->flags
);
4323 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN
, &parent
->flags
))
4324 set_bit(CGRP_CPUSET_CLONE_CHILDREN
, &cgrp
->flags
);
4326 for_each_root_subsys(root
, ss
) {
4327 struct cgroup_subsys_state
*css
;
4329 css
= ss
->css_alloc(cgrp
);
4335 err
= percpu_ref_init(&css
->refcnt
, css_release
);
4339 init_cgroup_css(css
, ss
, cgrp
);
4342 err
= alloc_css_id(ss
, parent
, cgrp
);
4349 * Create directory. cgroup_create_file() returns with the new
4350 * directory locked on success so that it can be populated without
4351 * dropping cgroup_mutex.
4353 err
= cgroup_create_file(dentry
, S_IFDIR
| mode
, sb
);
4356 lockdep_assert_held(&dentry
->d_inode
->i_mutex
);
4358 cgrp
->serial_nr
= cgroup_serial_nr_next
++;
4360 /* allocation complete, commit to creation */
4361 list_add_tail_rcu(&cgrp
->sibling
, &cgrp
->parent
->children
);
4362 root
->number_of_cgroups
++;
4364 /* each css holds a ref to the cgroup's dentry */
4365 for_each_root_subsys(root
, ss
)
4368 /* hold a ref to the parent's dentry */
4369 dget(parent
->dentry
);
4371 /* creation succeeded, notify subsystems */
4372 for_each_root_subsys(root
, ss
) {
4373 err
= online_css(ss
, cgrp
);
4377 if (ss
->broken_hierarchy
&& !ss
->warned_broken_hierarchy
&&
4379 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",
4380 current
->comm
, current
->pid
, ss
->name
);
4381 if (!strcmp(ss
->name
, "memory"))
4382 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4383 ss
->warned_broken_hierarchy
= true;
4387 err
= cgroup_populate_dir(cgrp
, true, root
->subsys_mask
);
4391 mutex_unlock(&cgroup_mutex
);
4392 mutex_unlock(&cgrp
->dentry
->d_inode
->i_mutex
);
4397 for_each_root_subsys(root
, ss
) {
4398 struct cgroup_subsys_state
*css
= cgrp
->subsys
[ss
->subsys_id
];
4401 percpu_ref_cancel_init(&css
->refcnt
);
4405 mutex_unlock(&cgroup_mutex
);
4406 /* Release the reference count that we took on the superblock */
4407 deactivate_super(sb
);
4409 ida_simple_remove(&root
->cgroup_ida
, cgrp
->id
);
4411 kfree(rcu_dereference_raw(cgrp
->name
));
4417 cgroup_destroy_locked(cgrp
);
4418 mutex_unlock(&cgroup_mutex
);
4419 mutex_unlock(&dentry
->d_inode
->i_mutex
);
4423 static int cgroup_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
4425 struct cgroup
*c_parent
= dentry
->d_parent
->d_fsdata
;
4427 /* the vfs holds inode->i_mutex already */
4428 return cgroup_create(c_parent
, dentry
, mode
| S_IFDIR
);
4431 static void cgroup_css_killed(struct cgroup
*cgrp
)
4433 if (!atomic_dec_and_test(&cgrp
->css_kill_cnt
))
4436 /* percpu ref's of all css's are killed, kick off the next step */
4437 INIT_WORK(&cgrp
->destroy_work
, cgroup_offline_fn
);
4438 schedule_work(&cgrp
->destroy_work
);
4441 static void css_ref_killed_fn(struct percpu_ref
*ref
)
4443 struct cgroup_subsys_state
*css
=
4444 container_of(ref
, struct cgroup_subsys_state
, refcnt
);
4446 cgroup_css_killed(css
->cgroup
);
4450 * cgroup_destroy_locked - the first stage of cgroup destruction
4451 * @cgrp: cgroup to be destroyed
4453 * css's make use of percpu refcnts whose killing latency shouldn't be
4454 * exposed to userland and are RCU protected. Also, cgroup core needs to
4455 * guarantee that css_tryget() won't succeed by the time ->css_offline() is
4456 * invoked. To satisfy all the requirements, destruction is implemented in
4457 * the following two steps.
4459 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4460 * userland visible parts and start killing the percpu refcnts of
4461 * css's. Set up so that the next stage will be kicked off once all
4462 * the percpu refcnts are confirmed to be killed.
4464 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4465 * rest of destruction. Once all cgroup references are gone, the
4466 * cgroup is RCU-freed.
4468 * This function implements s1. After this step, @cgrp is gone as far as
4469 * the userland is concerned and a new cgroup with the same name may be
4470 * created. As cgroup doesn't care about the names internally, this
4471 * doesn't cause any problem.
4473 static int cgroup_destroy_locked(struct cgroup
*cgrp
)
4474 __releases(&cgroup_mutex
) __acquires(&cgroup_mutex
)
4476 struct dentry
*d
= cgrp
->dentry
;
4477 struct cgroup_event
*event
, *tmp
;
4478 struct cgroup_subsys
*ss
;
4481 lockdep_assert_held(&d
->d_inode
->i_mutex
);
4482 lockdep_assert_held(&cgroup_mutex
);
4485 * css_set_lock synchronizes access to ->cset_links and prevents
4486 * @cgrp from being removed while __put_css_set() is in progress.
4488 read_lock(&css_set_lock
);
4489 empty
= list_empty(&cgrp
->cset_links
) && list_empty(&cgrp
->children
);
4490 read_unlock(&css_set_lock
);
4495 * Block new css_tryget() by killing css refcnts. cgroup core
4496 * guarantees that, by the time ->css_offline() is invoked, no new
4497 * css reference will be given out via css_tryget(). We can't
4498 * simply call percpu_ref_kill() and proceed to offlining css's
4499 * because percpu_ref_kill() doesn't guarantee that the ref is seen
4500 * as killed on all CPUs on return.
4502 * Use percpu_ref_kill_and_confirm() to get notifications as each
4503 * css is confirmed to be seen as killed on all CPUs. The
4504 * notification callback keeps track of the number of css's to be
4505 * killed and schedules cgroup_offline_fn() to perform the rest of
4506 * destruction once the percpu refs of all css's are confirmed to
4509 atomic_set(&cgrp
->css_kill_cnt
, 1);
4510 for_each_root_subsys(cgrp
->root
, ss
) {
4511 struct cgroup_subsys_state
*css
= cgrp
->subsys
[ss
->subsys_id
];
4514 * Killing would put the base ref, but we need to keep it
4515 * alive until after ->css_offline.
4517 percpu_ref_get(&css
->refcnt
);
4519 atomic_inc(&cgrp
->css_kill_cnt
);
4520 percpu_ref_kill_and_confirm(&css
->refcnt
, css_ref_killed_fn
);
4522 cgroup_css_killed(cgrp
);
4525 * Mark @cgrp dead. This prevents further task migration and child
4526 * creation by disabling cgroup_lock_live_group(). Note that
4527 * CGRP_DEAD assertion is depended upon by cgroup_next_sibling() to
4528 * resume iteration after dropping RCU read lock. See
4529 * cgroup_next_sibling() for details.
4531 set_bit(CGRP_DEAD
, &cgrp
->flags
);
4533 /* CGRP_DEAD is set, remove from ->release_list for the last time */
4534 raw_spin_lock(&release_list_lock
);
4535 if (!list_empty(&cgrp
->release_list
))
4536 list_del_init(&cgrp
->release_list
);
4537 raw_spin_unlock(&release_list_lock
);
4540 * Clear and remove @cgrp directory. The removal puts the base ref
4541 * but we aren't quite done with @cgrp yet, so hold onto it.
4543 cgroup_clear_dir(cgrp
, true, cgrp
->root
->subsys_mask
);
4545 cgroup_d_remove_dir(d
);
4548 * Unregister events and notify userspace.
4549 * Notify userspace about cgroup removing only after rmdir of cgroup
4550 * directory to avoid race between userspace and kernelspace.
4552 spin_lock(&cgrp
->event_list_lock
);
4553 list_for_each_entry_safe(event
, tmp
, &cgrp
->event_list
, list
) {
4554 list_del_init(&event
->list
);
4555 schedule_work(&event
->remove
);
4557 spin_unlock(&cgrp
->event_list_lock
);
4563 * cgroup_offline_fn - the second step of cgroup destruction
4564 * @work: cgroup->destroy_free_work
4566 * This function is invoked from a work item for a cgroup which is being
4567 * destroyed after the percpu refcnts of all css's are guaranteed to be
4568 * seen as killed on all CPUs, and performs the rest of destruction. This
4569 * is the second step of destruction described in the comment above
4570 * cgroup_destroy_locked().
4572 static void cgroup_offline_fn(struct work_struct
*work
)
4574 struct cgroup
*cgrp
= container_of(work
, struct cgroup
, destroy_work
);
4575 struct cgroup
*parent
= cgrp
->parent
;
4576 struct dentry
*d
= cgrp
->dentry
;
4577 struct cgroup_subsys
*ss
;
4579 mutex_lock(&cgroup_mutex
);
4582 * css_tryget() is guaranteed to fail now. Tell subsystems to
4583 * initate destruction.
4585 for_each_root_subsys(cgrp
->root
, ss
)
4586 offline_css(ss
, cgrp
);
4589 * Put the css refs from cgroup_destroy_locked(). Each css holds
4590 * an extra reference to the cgroup's dentry and cgroup removal
4591 * proceeds regardless of css refs. On the last put of each css,
4592 * whenever that may be, the extra dentry ref is put so that dentry
4593 * destruction happens only after all css's are released.
4595 for_each_root_subsys(cgrp
->root
, ss
)
4596 css_put(cgrp
->subsys
[ss
->subsys_id
]);
4598 /* delete this cgroup from parent->children */
4599 list_del_rcu(&cgrp
->sibling
);
4603 set_bit(CGRP_RELEASABLE
, &parent
->flags
);
4604 check_for_release(parent
);
4606 mutex_unlock(&cgroup_mutex
);
4609 static int cgroup_rmdir(struct inode
*unused_dir
, struct dentry
*dentry
)
4613 mutex_lock(&cgroup_mutex
);
4614 ret
= cgroup_destroy_locked(dentry
->d_fsdata
);
4615 mutex_unlock(&cgroup_mutex
);
4620 static void __init_or_module
cgroup_init_cftsets(struct cgroup_subsys
*ss
)
4622 INIT_LIST_HEAD(&ss
->cftsets
);
4625 * base_cftset is embedded in subsys itself, no need to worry about
4628 if (ss
->base_cftypes
) {
4629 ss
->base_cftset
.cfts
= ss
->base_cftypes
;
4630 list_add_tail(&ss
->base_cftset
.node
, &ss
->cftsets
);
4634 static void __init
cgroup_init_subsys(struct cgroup_subsys
*ss
)
4636 struct cgroup_subsys_state
*css
;
4638 printk(KERN_INFO
"Initializing cgroup subsys %s\n", ss
->name
);
4640 mutex_lock(&cgroup_mutex
);
4642 /* init base cftset */
4643 cgroup_init_cftsets(ss
);
4645 /* Create the top cgroup state for this subsystem */
4646 list_add(&ss
->sibling
, &cgroup_dummy_root
.subsys_list
);
4647 ss
->root
= &cgroup_dummy_root
;
4648 css
= ss
->css_alloc(cgroup_dummy_top
);
4649 /* We don't handle early failures gracefully */
4650 BUG_ON(IS_ERR(css
));
4651 init_cgroup_css(css
, ss
, cgroup_dummy_top
);
4653 /* Update the init_css_set to contain a subsys
4654 * pointer to this state - since the subsystem is
4655 * newly registered, all tasks and hence the
4656 * init_css_set is in the subsystem's top cgroup. */
4657 init_css_set
.subsys
[ss
->subsys_id
] = css
;
4659 need_forkexit_callback
|= ss
->fork
|| ss
->exit
;
4661 /* At system boot, before all subsystems have been
4662 * registered, no tasks have been forked, so we don't
4663 * need to invoke fork callbacks here. */
4664 BUG_ON(!list_empty(&init_task
.tasks
));
4666 BUG_ON(online_css(ss
, cgroup_dummy_top
));
4668 mutex_unlock(&cgroup_mutex
);
4670 /* this function shouldn't be used with modular subsystems, since they
4671 * need to register a subsys_id, among other things */
4676 * cgroup_load_subsys: load and register a modular subsystem at runtime
4677 * @ss: the subsystem to load
4679 * This function should be called in a modular subsystem's initcall. If the
4680 * subsystem is built as a module, it will be assigned a new subsys_id and set
4681 * up for use. If the subsystem is built-in anyway, work is delegated to the
4682 * simpler cgroup_init_subsys.
4684 int __init_or_module
cgroup_load_subsys(struct cgroup_subsys
*ss
)
4686 struct cgroup_subsys_state
*css
;
4688 struct hlist_node
*tmp
;
4689 struct css_set
*cset
;
4692 /* check name and function validity */
4693 if (ss
->name
== NULL
|| strlen(ss
->name
) > MAX_CGROUP_TYPE_NAMELEN
||
4694 ss
->css_alloc
== NULL
|| ss
->css_free
== NULL
)
4698 * we don't support callbacks in modular subsystems. this check is
4699 * before the ss->module check for consistency; a subsystem that could
4700 * be a module should still have no callbacks even if the user isn't
4701 * compiling it as one.
4703 if (ss
->fork
|| ss
->exit
)
4707 * an optionally modular subsystem is built-in: we want to do nothing,
4708 * since cgroup_init_subsys will have already taken care of it.
4710 if (ss
->module
== NULL
) {
4711 /* a sanity check */
4712 BUG_ON(cgroup_subsys
[ss
->subsys_id
] != ss
);
4716 /* init base cftset */
4717 cgroup_init_cftsets(ss
);
4719 mutex_lock(&cgroup_mutex
);
4720 cgroup_subsys
[ss
->subsys_id
] = ss
;
4723 * no ss->css_alloc seems to need anything important in the ss
4724 * struct, so this can happen first (i.e. before the dummy root
4727 css
= ss
->css_alloc(cgroup_dummy_top
);
4729 /* failure case - need to deassign the cgroup_subsys[] slot. */
4730 cgroup_subsys
[ss
->subsys_id
] = NULL
;
4731 mutex_unlock(&cgroup_mutex
);
4732 return PTR_ERR(css
);
4735 list_add(&ss
->sibling
, &cgroup_dummy_root
.subsys_list
);
4736 ss
->root
= &cgroup_dummy_root
;
4738 /* our new subsystem will be attached to the dummy hierarchy. */
4739 init_cgroup_css(css
, ss
, cgroup_dummy_top
);
4740 /* init_idr must be after init_cgroup_css because it sets css->id. */
4742 ret
= cgroup_init_idr(ss
, css
);
4748 * Now we need to entangle the css into the existing css_sets. unlike
4749 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4750 * will need a new pointer to it; done by iterating the css_set_table.
4751 * furthermore, modifying the existing css_sets will corrupt the hash
4752 * table state, so each changed css_set will need its hash recomputed.
4753 * this is all done under the css_set_lock.
4755 write_lock(&css_set_lock
);
4756 hash_for_each_safe(css_set_table
, i
, tmp
, cset
, hlist
) {
4757 /* skip entries that we already rehashed */
4758 if (cset
->subsys
[ss
->subsys_id
])
4760 /* remove existing entry */
4761 hash_del(&cset
->hlist
);
4763 cset
->subsys
[ss
->subsys_id
] = css
;
4764 /* recompute hash and restore entry */
4765 key
= css_set_hash(cset
->subsys
);
4766 hash_add(css_set_table
, &cset
->hlist
, key
);
4768 write_unlock(&css_set_lock
);
4770 ret
= online_css(ss
, cgroup_dummy_top
);
4775 mutex_unlock(&cgroup_mutex
);
4779 mutex_unlock(&cgroup_mutex
);
4780 /* @ss can't be mounted here as try_module_get() would fail */
4781 cgroup_unload_subsys(ss
);
4784 EXPORT_SYMBOL_GPL(cgroup_load_subsys
);
4787 * cgroup_unload_subsys: unload a modular subsystem
4788 * @ss: the subsystem to unload
4790 * This function should be called in a modular subsystem's exitcall. When this
4791 * function is invoked, the refcount on the subsystem's module will be 0, so
4792 * the subsystem will not be attached to any hierarchy.
4794 void cgroup_unload_subsys(struct cgroup_subsys
*ss
)
4796 struct cgrp_cset_link
*link
;
4798 BUG_ON(ss
->module
== NULL
);
4801 * we shouldn't be called if the subsystem is in use, and the use of
4802 * try_module_get in parse_cgroupfs_options should ensure that it
4803 * doesn't start being used while we're killing it off.
4805 BUG_ON(ss
->root
!= &cgroup_dummy_root
);
4807 mutex_lock(&cgroup_mutex
);
4809 offline_css(ss
, cgroup_dummy_top
);
4812 idr_destroy(&ss
->idr
);
4814 /* deassign the subsys_id */
4815 cgroup_subsys
[ss
->subsys_id
] = NULL
;
4817 /* remove subsystem from the dummy root's list of subsystems */
4818 list_del_init(&ss
->sibling
);
4821 * disentangle the css from all css_sets attached to the dummy
4822 * top. as in loading, we need to pay our respects to the hashtable
4825 write_lock(&css_set_lock
);
4826 list_for_each_entry(link
, &cgroup_dummy_top
->cset_links
, cset_link
) {
4827 struct css_set
*cset
= link
->cset
;
4830 hash_del(&cset
->hlist
);
4831 cset
->subsys
[ss
->subsys_id
] = NULL
;
4832 key
= css_set_hash(cset
->subsys
);
4833 hash_add(css_set_table
, &cset
->hlist
, key
);
4835 write_unlock(&css_set_lock
);
4838 * remove subsystem's css from the cgroup_dummy_top and free it -
4839 * need to free before marking as null because ss->css_free needs
4840 * the cgrp->subsys pointer to find their state. note that this
4841 * also takes care of freeing the css_id.
4843 ss
->css_free(cgroup_dummy_top
);
4844 cgroup_dummy_top
->subsys
[ss
->subsys_id
] = NULL
;
4846 mutex_unlock(&cgroup_mutex
);
4848 EXPORT_SYMBOL_GPL(cgroup_unload_subsys
);
4851 * cgroup_init_early - cgroup initialization at system boot
4853 * Initialize cgroups at system boot, and initialize any
4854 * subsystems that request early init.
4856 int __init
cgroup_init_early(void)
4858 struct cgroup_subsys
*ss
;
4861 atomic_set(&init_css_set
.refcount
, 1);
4862 INIT_LIST_HEAD(&init_css_set
.cgrp_links
);
4863 INIT_LIST_HEAD(&init_css_set
.tasks
);
4864 INIT_HLIST_NODE(&init_css_set
.hlist
);
4866 init_cgroup_root(&cgroup_dummy_root
);
4867 cgroup_root_count
= 1;
4868 RCU_INIT_POINTER(init_task
.cgroups
, &init_css_set
);
4870 init_cgrp_cset_link
.cset
= &init_css_set
;
4871 init_cgrp_cset_link
.cgrp
= cgroup_dummy_top
;
4872 list_add(&init_cgrp_cset_link
.cset_link
, &cgroup_dummy_top
->cset_links
);
4873 list_add(&init_cgrp_cset_link
.cgrp_link
, &init_css_set
.cgrp_links
);
4875 /* at bootup time, we don't worry about modular subsystems */
4876 for_each_builtin_subsys(ss
, i
) {
4878 BUG_ON(strlen(ss
->name
) > MAX_CGROUP_TYPE_NAMELEN
);
4879 BUG_ON(!ss
->css_alloc
);
4880 BUG_ON(!ss
->css_free
);
4881 if (ss
->subsys_id
!= i
) {
4882 printk(KERN_ERR
"cgroup: Subsys %s id == %d\n",
4883 ss
->name
, ss
->subsys_id
);
4888 cgroup_init_subsys(ss
);
4894 * cgroup_init - cgroup initialization
4896 * Register cgroup filesystem and /proc file, and initialize
4897 * any subsystems that didn't request early init.
4899 int __init
cgroup_init(void)
4901 struct cgroup_subsys
*ss
;
4905 err
= bdi_init(&cgroup_backing_dev_info
);
4909 for_each_builtin_subsys(ss
, i
) {
4910 if (!ss
->early_init
)
4911 cgroup_init_subsys(ss
);
4913 cgroup_init_idr(ss
, init_css_set
.subsys
[ss
->subsys_id
]);
4916 /* allocate id for the dummy hierarchy */
4917 mutex_lock(&cgroup_mutex
);
4918 mutex_lock(&cgroup_root_mutex
);
4920 /* Add init_css_set to the hash table */
4921 key
= css_set_hash(init_css_set
.subsys
);
4922 hash_add(css_set_table
, &init_css_set
.hlist
, key
);
4924 BUG_ON(cgroup_init_root_id(&cgroup_dummy_root
, 0, 1));
4926 mutex_unlock(&cgroup_root_mutex
);
4927 mutex_unlock(&cgroup_mutex
);
4929 cgroup_kobj
= kobject_create_and_add("cgroup", fs_kobj
);
4935 err
= register_filesystem(&cgroup_fs_type
);
4937 kobject_put(cgroup_kobj
);
4941 proc_create("cgroups", 0, NULL
, &proc_cgroupstats_operations
);
4945 bdi_destroy(&cgroup_backing_dev_info
);
4951 * proc_cgroup_show()
4952 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4953 * - Used for /proc/<pid>/cgroup.
4954 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4955 * doesn't really matter if tsk->cgroup changes after we read it,
4956 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
4957 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4958 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4959 * cgroup to top_cgroup.
4962 /* TODO: Use a proper seq_file iterator */
4963 int proc_cgroup_show(struct seq_file
*m
, void *v
)
4966 struct task_struct
*tsk
;
4969 struct cgroupfs_root
*root
;
4972 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
4978 tsk
= get_pid_task(pid
, PIDTYPE_PID
);
4984 mutex_lock(&cgroup_mutex
);
4986 for_each_active_root(root
) {
4987 struct cgroup_subsys
*ss
;
4988 struct cgroup
*cgrp
;
4991 seq_printf(m
, "%d:", root
->hierarchy_id
);
4992 for_each_root_subsys(root
, ss
)
4993 seq_printf(m
, "%s%s", count
++ ? "," : "", ss
->name
);
4994 if (strlen(root
->name
))
4995 seq_printf(m
, "%sname=%s", count
? "," : "",
4998 cgrp
= task_cgroup_from_root(tsk
, root
);
4999 retval
= cgroup_path(cgrp
, buf
, PAGE_SIZE
);
5007 mutex_unlock(&cgroup_mutex
);
5008 put_task_struct(tsk
);
5015 /* Display information about each subsystem and each hierarchy */
5016 static int proc_cgroupstats_show(struct seq_file
*m
, void *v
)
5018 struct cgroup_subsys
*ss
;
5021 seq_puts(m
, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
5023 * ideally we don't want subsystems moving around while we do this.
5024 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
5025 * subsys/hierarchy state.
5027 mutex_lock(&cgroup_mutex
);
5029 for_each_subsys(ss
, i
)
5030 seq_printf(m
, "%s\t%d\t%d\t%d\n",
5031 ss
->name
, ss
->root
->hierarchy_id
,
5032 ss
->root
->number_of_cgroups
, !ss
->disabled
);
5034 mutex_unlock(&cgroup_mutex
);
5038 static int cgroupstats_open(struct inode
*inode
, struct file
*file
)
5040 return single_open(file
, proc_cgroupstats_show
, NULL
);
5043 static const struct file_operations proc_cgroupstats_operations
= {
5044 .open
= cgroupstats_open
,
5046 .llseek
= seq_lseek
,
5047 .release
= single_release
,
5051 * cgroup_fork - attach newly forked task to its parents cgroup.
5052 * @child: pointer to task_struct of forking parent process.
5054 * Description: A task inherits its parent's cgroup at fork().
5056 * A pointer to the shared css_set was automatically copied in
5057 * fork.c by dup_task_struct(). However, we ignore that copy, since
5058 * it was not made under the protection of RCU or cgroup_mutex, so
5059 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
5060 * have already changed current->cgroups, allowing the previously
5061 * referenced cgroup group to be removed and freed.
5063 * At the point that cgroup_fork() is called, 'current' is the parent
5064 * task, and the passed argument 'child' points to the child task.
5066 void cgroup_fork(struct task_struct
*child
)
5069 get_css_set(task_css_set(current
));
5070 child
->cgroups
= current
->cgroups
;
5071 task_unlock(current
);
5072 INIT_LIST_HEAD(&child
->cg_list
);
5076 * cgroup_post_fork - called on a new task after adding it to the task list
5077 * @child: the task in question
5079 * Adds the task to the list running through its css_set if necessary and
5080 * call the subsystem fork() callbacks. Has to be after the task is
5081 * visible on the task list in case we race with the first call to
5082 * cgroup_iter_start() - to guarantee that the new task ends up on its
5085 void cgroup_post_fork(struct task_struct
*child
)
5087 struct cgroup_subsys
*ss
;
5091 * use_task_css_set_links is set to 1 before we walk the tasklist
5092 * under the tasklist_lock and we read it here after we added the child
5093 * to the tasklist under the tasklist_lock as well. If the child wasn't
5094 * yet in the tasklist when we walked through it from
5095 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
5096 * should be visible now due to the paired locking and barriers implied
5097 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
5098 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
5101 if (use_task_css_set_links
) {
5102 write_lock(&css_set_lock
);
5104 if (list_empty(&child
->cg_list
))
5105 list_add(&child
->cg_list
, &task_css_set(child
)->tasks
);
5107 write_unlock(&css_set_lock
);
5111 * Call ss->fork(). This must happen after @child is linked on
5112 * css_set; otherwise, @child might change state between ->fork()
5113 * and addition to css_set.
5115 if (need_forkexit_callback
) {
5117 * fork/exit callbacks are supported only for builtin
5118 * subsystems, and the builtin section of the subsys
5119 * array is immutable, so we don't need to lock the
5120 * subsys array here. On the other hand, modular section
5121 * of the array can be freed at module unload, so we
5124 for_each_builtin_subsys(ss
, i
)
5131 * cgroup_exit - detach cgroup from exiting task
5132 * @tsk: pointer to task_struct of exiting process
5133 * @run_callback: run exit callbacks?
5135 * Description: Detach cgroup from @tsk and release it.
5137 * Note that cgroups marked notify_on_release force every task in
5138 * them to take the global cgroup_mutex mutex when exiting.
5139 * This could impact scaling on very large systems. Be reluctant to
5140 * use notify_on_release cgroups where very high task exit scaling
5141 * is required on large systems.
5143 * the_top_cgroup_hack:
5145 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
5147 * We call cgroup_exit() while the task is still competent to
5148 * handle notify_on_release(), then leave the task attached to the
5149 * root cgroup in each hierarchy for the remainder of its exit.
5151 * To do this properly, we would increment the reference count on
5152 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
5153 * code we would add a second cgroup function call, to drop that
5154 * reference. This would just create an unnecessary hot spot on
5155 * the top_cgroup reference count, to no avail.
5157 * Normally, holding a reference to a cgroup without bumping its
5158 * count is unsafe. The cgroup could go away, or someone could
5159 * attach us to a different cgroup, decrementing the count on
5160 * the first cgroup that we never incremented. But in this case,
5161 * top_cgroup isn't going away, and either task has PF_EXITING set,
5162 * which wards off any cgroup_attach_task() attempts, or task is a failed
5163 * fork, never visible to cgroup_attach_task.
5165 void cgroup_exit(struct task_struct
*tsk
, int run_callbacks
)
5167 struct cgroup_subsys
*ss
;
5168 struct css_set
*cset
;
5172 * Unlink from the css_set task list if necessary.
5173 * Optimistically check cg_list before taking
5176 if (!list_empty(&tsk
->cg_list
)) {
5177 write_lock(&css_set_lock
);
5178 if (!list_empty(&tsk
->cg_list
))
5179 list_del_init(&tsk
->cg_list
);
5180 write_unlock(&css_set_lock
);
5183 /* Reassign the task to the init_css_set. */
5185 cset
= task_css_set(tsk
);
5186 RCU_INIT_POINTER(tsk
->cgroups
, &init_css_set
);
5188 if (run_callbacks
&& need_forkexit_callback
) {
5190 * fork/exit callbacks are supported only for builtin
5191 * subsystems, see cgroup_post_fork() for details.
5193 for_each_builtin_subsys(ss
, i
) {
5195 struct cgroup
*old_cgrp
= cset
->subsys
[i
]->cgroup
;
5196 struct cgroup
*cgrp
= task_cgroup(tsk
, i
);
5198 ss
->exit(cgrp
, old_cgrp
, tsk
);
5204 put_css_set_taskexit(cset
);
5207 static void check_for_release(struct cgroup
*cgrp
)
5209 if (cgroup_is_releasable(cgrp
) &&
5210 list_empty(&cgrp
->cset_links
) && list_empty(&cgrp
->children
)) {
5212 * Control Group is currently removeable. If it's not
5213 * already queued for a userspace notification, queue
5216 int need_schedule_work
= 0;
5218 raw_spin_lock(&release_list_lock
);
5219 if (!cgroup_is_dead(cgrp
) &&
5220 list_empty(&cgrp
->release_list
)) {
5221 list_add(&cgrp
->release_list
, &release_list
);
5222 need_schedule_work
= 1;
5224 raw_spin_unlock(&release_list_lock
);
5225 if (need_schedule_work
)
5226 schedule_work(&release_agent_work
);
5231 * Notify userspace when a cgroup is released, by running the
5232 * configured release agent with the name of the cgroup (path
5233 * relative to the root of cgroup file system) as the argument.
5235 * Most likely, this user command will try to rmdir this cgroup.
5237 * This races with the possibility that some other task will be
5238 * attached to this cgroup before it is removed, or that some other
5239 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5240 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5241 * unused, and this cgroup will be reprieved from its death sentence,
5242 * to continue to serve a useful existence. Next time it's released,
5243 * we will get notified again, if it still has 'notify_on_release' set.
5245 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5246 * means only wait until the task is successfully execve()'d. The
5247 * separate release agent task is forked by call_usermodehelper(),
5248 * then control in this thread returns here, without waiting for the
5249 * release agent task. We don't bother to wait because the caller of
5250 * this routine has no use for the exit status of the release agent
5251 * task, so no sense holding our caller up for that.
5253 static void cgroup_release_agent(struct work_struct
*work
)
5255 BUG_ON(work
!= &release_agent_work
);
5256 mutex_lock(&cgroup_mutex
);
5257 raw_spin_lock(&release_list_lock
);
5258 while (!list_empty(&release_list
)) {
5259 char *argv
[3], *envp
[3];
5261 char *pathbuf
= NULL
, *agentbuf
= NULL
;
5262 struct cgroup
*cgrp
= list_entry(release_list
.next
,
5265 list_del_init(&cgrp
->release_list
);
5266 raw_spin_unlock(&release_list_lock
);
5267 pathbuf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
5270 if (cgroup_path(cgrp
, pathbuf
, PAGE_SIZE
) < 0)
5272 agentbuf
= kstrdup(cgrp
->root
->release_agent_path
, GFP_KERNEL
);
5277 argv
[i
++] = agentbuf
;
5278 argv
[i
++] = pathbuf
;
5282 /* minimal command environment */
5283 envp
[i
++] = "HOME=/";
5284 envp
[i
++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5287 /* Drop the lock while we invoke the usermode helper,
5288 * since the exec could involve hitting disk and hence
5289 * be a slow process */
5290 mutex_unlock(&cgroup_mutex
);
5291 call_usermodehelper(argv
[0], argv
, envp
, UMH_WAIT_EXEC
);
5292 mutex_lock(&cgroup_mutex
);
5296 raw_spin_lock(&release_list_lock
);
5298 raw_spin_unlock(&release_list_lock
);
5299 mutex_unlock(&cgroup_mutex
);
5302 static int __init
cgroup_disable(char *str
)
5304 struct cgroup_subsys
*ss
;
5308 while ((token
= strsep(&str
, ",")) != NULL
) {
5313 * cgroup_disable, being at boot time, can't know about
5314 * module subsystems, so we don't worry about them.
5316 for_each_builtin_subsys(ss
, i
) {
5317 if (!strcmp(token
, ss
->name
)) {
5319 printk(KERN_INFO
"Disabling %s control group"
5320 " subsystem\n", ss
->name
);
5327 __setup("cgroup_disable=", cgroup_disable
);
5330 * Functons for CSS ID.
5333 /* to get ID other than 0, this should be called when !cgroup_is_dead() */
5334 unsigned short css_id(struct cgroup_subsys_state
*css
)
5336 struct css_id
*cssid
;
5339 * This css_id() can return correct value when somone has refcnt
5340 * on this or this is under rcu_read_lock(). Once css->id is allocated,
5341 * it's unchanged until freed.
5343 cssid
= rcu_dereference_raw(css
->id
);
5349 EXPORT_SYMBOL_GPL(css_id
);
5352 * css_is_ancestor - test "root" css is an ancestor of "child"
5353 * @child: the css to be tested.
5354 * @root: the css supporsed to be an ancestor of the child.
5356 * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
5357 * this function reads css->id, the caller must hold rcu_read_lock().
5358 * But, considering usual usage, the csses should be valid objects after test.
5359 * Assuming that the caller will do some action to the child if this returns
5360 * returns true, the caller must take "child";s reference count.
5361 * If "child" is valid object and this returns true, "root" is valid, too.
5364 bool css_is_ancestor(struct cgroup_subsys_state
*child
,
5365 const struct cgroup_subsys_state
*root
)
5367 struct css_id
*child_id
;
5368 struct css_id
*root_id
;
5370 child_id
= rcu_dereference(child
->id
);
5373 root_id
= rcu_dereference(root
->id
);
5376 if (child_id
->depth
< root_id
->depth
)
5378 if (child_id
->stack
[root_id
->depth
] != root_id
->id
)
5383 void free_css_id(struct cgroup_subsys
*ss
, struct cgroup_subsys_state
*css
)
5385 struct css_id
*id
= rcu_dereference_protected(css
->id
, true);
5387 /* When this is called before css_id initialization, id can be NULL */
5391 BUG_ON(!ss
->use_id
);
5393 rcu_assign_pointer(id
->css
, NULL
);
5394 rcu_assign_pointer(css
->id
, NULL
);
5395 spin_lock(&ss
->id_lock
);
5396 idr_remove(&ss
->idr
, id
->id
);
5397 spin_unlock(&ss
->id_lock
);
5398 kfree_rcu(id
, rcu_head
);
5400 EXPORT_SYMBOL_GPL(free_css_id
);
5403 * This is called by init or create(). Then, calls to this function are
5404 * always serialized (By cgroup_mutex() at create()).
5407 static struct css_id
*get_new_cssid(struct cgroup_subsys
*ss
, int depth
)
5409 struct css_id
*newid
;
5412 BUG_ON(!ss
->use_id
);
5414 size
= sizeof(*newid
) + sizeof(unsigned short) * (depth
+ 1);
5415 newid
= kzalloc(size
, GFP_KERNEL
);
5417 return ERR_PTR(-ENOMEM
);
5419 idr_preload(GFP_KERNEL
);
5420 spin_lock(&ss
->id_lock
);
5421 /* Don't use 0. allocates an ID of 1-65535 */
5422 ret
= idr_alloc(&ss
->idr
, newid
, 1, CSS_ID_MAX
+ 1, GFP_NOWAIT
);
5423 spin_unlock(&ss
->id_lock
);
5426 /* Returns error when there are no free spaces for new ID.*/
5431 newid
->depth
= depth
;
5435 return ERR_PTR(ret
);
5439 static int __init_or_module
cgroup_init_idr(struct cgroup_subsys
*ss
,
5440 struct cgroup_subsys_state
*rootcss
)
5442 struct css_id
*newid
;
5444 spin_lock_init(&ss
->id_lock
);
5447 newid
= get_new_cssid(ss
, 0);
5449 return PTR_ERR(newid
);
5451 newid
->stack
[0] = newid
->id
;
5452 RCU_INIT_POINTER(newid
->css
, rootcss
);
5453 RCU_INIT_POINTER(rootcss
->id
, newid
);
5457 static int alloc_css_id(struct cgroup_subsys
*ss
, struct cgroup
*parent
,
5458 struct cgroup
*child
)
5460 int subsys_id
, i
, depth
= 0;
5461 struct cgroup_subsys_state
*parent_css
, *child_css
;
5462 struct css_id
*child_id
, *parent_id
;
5464 subsys_id
= ss
->subsys_id
;
5465 parent_css
= parent
->subsys
[subsys_id
];
5466 child_css
= child
->subsys
[subsys_id
];
5467 parent_id
= rcu_dereference_protected(parent_css
->id
, true);
5468 depth
= parent_id
->depth
+ 1;
5470 child_id
= get_new_cssid(ss
, depth
);
5471 if (IS_ERR(child_id
))
5472 return PTR_ERR(child_id
);
5474 for (i
= 0; i
< depth
; i
++)
5475 child_id
->stack
[i
] = parent_id
->stack
[i
];
5476 child_id
->stack
[depth
] = child_id
->id
;
5478 * child_id->css pointer will be set after this cgroup is available
5479 * see cgroup_populate_dir()
5481 rcu_assign_pointer(child_css
->id
, child_id
);
5487 * css_lookup - lookup css by id
5488 * @ss: cgroup subsys to be looked into.
5491 * Returns pointer to cgroup_subsys_state if there is valid one with id.
5492 * NULL if not. Should be called under rcu_read_lock()
5494 struct cgroup_subsys_state
*css_lookup(struct cgroup_subsys
*ss
, int id
)
5496 struct css_id
*cssid
= NULL
;
5498 BUG_ON(!ss
->use_id
);
5499 cssid
= idr_find(&ss
->idr
, id
);
5501 if (unlikely(!cssid
))
5504 return rcu_dereference(cssid
->css
);
5506 EXPORT_SYMBOL_GPL(css_lookup
);
5509 * get corresponding css from file open on cgroupfs directory
5511 struct cgroup_subsys_state
*cgroup_css_from_dir(struct file
*f
, int id
)
5513 struct cgroup
*cgrp
;
5514 struct inode
*inode
;
5515 struct cgroup_subsys_state
*css
;
5517 inode
= file_inode(f
);
5518 /* check in cgroup filesystem dir */
5519 if (inode
->i_op
!= &cgroup_dir_inode_operations
)
5520 return ERR_PTR(-EBADF
);
5522 if (id
< 0 || id
>= CGROUP_SUBSYS_COUNT
)
5523 return ERR_PTR(-EINVAL
);
5526 cgrp
= __d_cgrp(f
->f_dentry
);
5527 css
= cgrp
->subsys
[id
];
5528 return css
? css
: ERR_PTR(-ENOENT
);
5531 #ifdef CONFIG_CGROUP_DEBUG
5532 static struct cgroup_subsys_state
*debug_css_alloc(struct cgroup
*cgrp
)
5534 struct cgroup_subsys_state
*css
= kzalloc(sizeof(*css
), GFP_KERNEL
);
5537 return ERR_PTR(-ENOMEM
);
5542 static void debug_css_free(struct cgroup
*cgrp
)
5544 kfree(cgrp
->subsys
[debug_subsys_id
]);
5547 static u64
debug_taskcount_read(struct cgroup
*cgrp
, struct cftype
*cft
)
5549 return cgroup_task_count(cgrp
);
5552 static u64
current_css_set_read(struct cgroup
*cgrp
, struct cftype
*cft
)
5554 return (u64
)(unsigned long)current
->cgroups
;
5557 static u64
current_css_set_refcount_read(struct cgroup
*cgrp
,
5563 count
= atomic_read(&task_css_set(current
)->refcount
);
5568 static int current_css_set_cg_links_read(struct cgroup
*cgrp
,
5570 struct seq_file
*seq
)
5572 struct cgrp_cset_link
*link
;
5573 struct css_set
*cset
;
5575 read_lock(&css_set_lock
);
5577 cset
= rcu_dereference(current
->cgroups
);
5578 list_for_each_entry(link
, &cset
->cgrp_links
, cgrp_link
) {
5579 struct cgroup
*c
= link
->cgrp
;
5583 name
= c
->dentry
->d_name
.name
;
5586 seq_printf(seq
, "Root %d group %s\n",
5587 c
->root
->hierarchy_id
, name
);
5590 read_unlock(&css_set_lock
);
5594 #define MAX_TASKS_SHOWN_PER_CSS 25
5595 static int cgroup_css_links_read(struct cgroup
*cgrp
,
5597 struct seq_file
*seq
)
5599 struct cgrp_cset_link
*link
;
5601 read_lock(&css_set_lock
);
5602 list_for_each_entry(link
, &cgrp
->cset_links
, cset_link
) {
5603 struct css_set
*cset
= link
->cset
;
5604 struct task_struct
*task
;
5606 seq_printf(seq
, "css_set %p\n", cset
);
5607 list_for_each_entry(task
, &cset
->tasks
, cg_list
) {
5608 if (count
++ > MAX_TASKS_SHOWN_PER_CSS
) {
5609 seq_puts(seq
, " ...\n");
5612 seq_printf(seq
, " task %d\n",
5613 task_pid_vnr(task
));
5617 read_unlock(&css_set_lock
);
5621 static u64
releasable_read(struct cgroup
*cgrp
, struct cftype
*cft
)
5623 return test_bit(CGRP_RELEASABLE
, &cgrp
->flags
);
5626 static struct cftype debug_files
[] = {
5628 .name
= "taskcount",
5629 .read_u64
= debug_taskcount_read
,
5633 .name
= "current_css_set",
5634 .read_u64
= current_css_set_read
,
5638 .name
= "current_css_set_refcount",
5639 .read_u64
= current_css_set_refcount_read
,
5643 .name
= "current_css_set_cg_links",
5644 .read_seq_string
= current_css_set_cg_links_read
,
5648 .name
= "cgroup_css_links",
5649 .read_seq_string
= cgroup_css_links_read
,
5653 .name
= "releasable",
5654 .read_u64
= releasable_read
,
5660 struct cgroup_subsys debug_subsys
= {
5662 .css_alloc
= debug_css_alloc
,
5663 .css_free
= debug_css_free
,
5664 .subsys_id
= debug_subsys_id
,
5665 .base_cftypes
= debug_files
,
5667 #endif /* CONFIG_CGROUP_DEBUG */