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_directory - selective removal of base and subsystem files
961 * @dir: directory containing the files
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_directory(struct dentry
*dir
, bool base_files
,
966 unsigned long subsys_mask
)
968 struct cgroup
*cgrp
= __d_cgrp(dir
);
969 struct cgroup_subsys
*ss
;
971 for_each_root_subsys(cgrp
->root
, ss
) {
972 struct cftype_set
*set
;
973 if (!test_bit(ss
->subsys_id
, &subsys_mask
))
975 list_for_each_entry(set
, &ss
->cftsets
, node
)
976 cgroup_addrm_files(cgrp
, NULL
, set
->cfts
, false);
979 while (!list_empty(&cgrp
->files
))
980 cgroup_rm_file(cgrp
, NULL
);
985 * NOTE : the dentry must have been dget()'ed
987 static void cgroup_d_remove_dir(struct dentry
*dentry
)
989 struct dentry
*parent
;
990 struct cgroupfs_root
*root
= dentry
->d_sb
->s_fs_info
;
992 cgroup_clear_directory(dentry
, true, root
->subsys_mask
);
994 parent
= dentry
->d_parent
;
995 spin_lock(&parent
->d_lock
);
996 spin_lock_nested(&dentry
->d_lock
, DENTRY_D_LOCK_NESTED
);
997 list_del_init(&dentry
->d_u
.d_child
);
998 spin_unlock(&dentry
->d_lock
);
999 spin_unlock(&parent
->d_lock
);
1004 * Call with cgroup_mutex held. Drops reference counts on modules, including
1005 * any duplicate ones that parse_cgroupfs_options took. If this function
1006 * returns an error, no reference counts are touched.
1008 static int rebind_subsystems(struct cgroupfs_root
*root
,
1009 unsigned long added_mask
, unsigned removed_mask
)
1011 struct cgroup
*cgrp
= &root
->top_cgroup
;
1012 struct cgroup_subsys
*ss
;
1015 BUG_ON(!mutex_is_locked(&cgroup_mutex
));
1016 BUG_ON(!mutex_is_locked(&cgroup_root_mutex
));
1018 /* Check that any added subsystems are currently free */
1019 for_each_subsys(ss
, i
) {
1020 unsigned long bit
= 1UL << i
;
1022 if (!(bit
& added_mask
))
1025 if (ss
->root
!= &cgroup_dummy_root
) {
1026 /* Subsystem isn't free */
1031 /* Currently we don't handle adding/removing subsystems when
1032 * any child cgroups exist. This is theoretically supportable
1033 * but involves complex error handling, so it's being left until
1035 if (root
->number_of_cgroups
> 1)
1038 /* Process each subsystem */
1039 for_each_subsys(ss
, i
) {
1040 unsigned long bit
= 1UL << i
;
1042 if (bit
& added_mask
) {
1043 /* We're binding this subsystem to this hierarchy */
1044 BUG_ON(cgrp
->subsys
[i
]);
1045 BUG_ON(!cgroup_dummy_top
->subsys
[i
]);
1046 BUG_ON(cgroup_dummy_top
->subsys
[i
]->cgroup
!= cgroup_dummy_top
);
1048 cgrp
->subsys
[i
] = cgroup_dummy_top
->subsys
[i
];
1049 cgrp
->subsys
[i
]->cgroup
= cgrp
;
1050 list_move(&ss
->sibling
, &root
->subsys_list
);
1055 /* refcount was already taken, and we're keeping it */
1056 root
->subsys_mask
|= bit
;
1057 } else if (bit
& removed_mask
) {
1058 /* We're removing this subsystem */
1059 BUG_ON(cgrp
->subsys
[i
] != cgroup_dummy_top
->subsys
[i
]);
1060 BUG_ON(cgrp
->subsys
[i
]->cgroup
!= cgrp
);
1063 ss
->bind(cgroup_dummy_top
);
1064 cgroup_dummy_top
->subsys
[i
]->cgroup
= cgroup_dummy_top
;
1065 cgrp
->subsys
[i
] = NULL
;
1066 cgroup_subsys
[i
]->root
= &cgroup_dummy_root
;
1067 list_move(&ss
->sibling
, &cgroup_dummy_root
.subsys_list
);
1069 /* subsystem is now free - drop reference on module */
1070 module_put(ss
->module
);
1071 root
->subsys_mask
&= ~bit
;
1072 } else if (bit
& root
->subsys_mask
) {
1073 /* Subsystem state should already exist */
1074 BUG_ON(!cgrp
->subsys
[i
]);
1076 * a refcount was taken, but we already had one, so
1077 * drop the extra reference.
1079 module_put(ss
->module
);
1080 #ifdef CONFIG_MODULE_UNLOAD
1081 BUG_ON(ss
->module
&& !module_refcount(ss
->module
));
1084 /* Subsystem state shouldn't exist */
1085 BUG_ON(cgrp
->subsys
[i
]);
1090 * Mark @root has finished binding subsystems. @root->subsys_mask
1091 * now matches the bound subsystems.
1093 root
->flags
|= CGRP_ROOT_SUBSYS_BOUND
;
1098 static int cgroup_show_options(struct seq_file
*seq
, struct dentry
*dentry
)
1100 struct cgroupfs_root
*root
= dentry
->d_sb
->s_fs_info
;
1101 struct cgroup_subsys
*ss
;
1103 mutex_lock(&cgroup_root_mutex
);
1104 for_each_root_subsys(root
, ss
)
1105 seq_printf(seq
, ",%s", ss
->name
);
1106 if (root
->flags
& CGRP_ROOT_SANE_BEHAVIOR
)
1107 seq_puts(seq
, ",sane_behavior");
1108 if (root
->flags
& CGRP_ROOT_NOPREFIX
)
1109 seq_puts(seq
, ",noprefix");
1110 if (root
->flags
& CGRP_ROOT_XATTR
)
1111 seq_puts(seq
, ",xattr");
1112 if (strlen(root
->release_agent_path
))
1113 seq_printf(seq
, ",release_agent=%s", root
->release_agent_path
);
1114 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN
, &root
->top_cgroup
.flags
))
1115 seq_puts(seq
, ",clone_children");
1116 if (strlen(root
->name
))
1117 seq_printf(seq
, ",name=%s", root
->name
);
1118 mutex_unlock(&cgroup_root_mutex
);
1122 struct cgroup_sb_opts
{
1123 unsigned long subsys_mask
;
1124 unsigned long flags
;
1125 char *release_agent
;
1126 bool cpuset_clone_children
;
1128 /* User explicitly requested empty subsystem */
1131 struct cgroupfs_root
*new_root
;
1136 * Convert a hierarchy specifier into a bitmask of subsystems and
1137 * flags. Call with cgroup_mutex held to protect the cgroup_subsys[]
1138 * array. This function takes refcounts on subsystems to be used, unless it
1139 * returns error, in which case no refcounts are taken.
1141 static int parse_cgroupfs_options(char *data
, struct cgroup_sb_opts
*opts
)
1143 char *token
, *o
= data
;
1144 bool all_ss
= false, one_ss
= false;
1145 unsigned long mask
= (unsigned long)-1;
1146 bool module_pin_failed
= false;
1147 struct cgroup_subsys
*ss
;
1150 BUG_ON(!mutex_is_locked(&cgroup_mutex
));
1152 #ifdef CONFIG_CPUSETS
1153 mask
= ~(1UL << cpuset_subsys_id
);
1156 memset(opts
, 0, sizeof(*opts
));
1158 while ((token
= strsep(&o
, ",")) != NULL
) {
1161 if (!strcmp(token
, "none")) {
1162 /* Explicitly have no subsystems */
1166 if (!strcmp(token
, "all")) {
1167 /* Mutually exclusive option 'all' + subsystem name */
1173 if (!strcmp(token
, "__DEVEL__sane_behavior")) {
1174 opts
->flags
|= CGRP_ROOT_SANE_BEHAVIOR
;
1177 if (!strcmp(token
, "noprefix")) {
1178 opts
->flags
|= CGRP_ROOT_NOPREFIX
;
1181 if (!strcmp(token
, "clone_children")) {
1182 opts
->cpuset_clone_children
= true;
1185 if (!strcmp(token
, "xattr")) {
1186 opts
->flags
|= CGRP_ROOT_XATTR
;
1189 if (!strncmp(token
, "release_agent=", 14)) {
1190 /* Specifying two release agents is forbidden */
1191 if (opts
->release_agent
)
1193 opts
->release_agent
=
1194 kstrndup(token
+ 14, PATH_MAX
- 1, GFP_KERNEL
);
1195 if (!opts
->release_agent
)
1199 if (!strncmp(token
, "name=", 5)) {
1200 const char *name
= token
+ 5;
1201 /* Can't specify an empty name */
1204 /* Must match [\w.-]+ */
1205 for (i
= 0; i
< strlen(name
); i
++) {
1209 if ((c
== '.') || (c
== '-') || (c
== '_'))
1213 /* Specifying two names is forbidden */
1216 opts
->name
= kstrndup(name
,
1217 MAX_CGROUP_ROOT_NAMELEN
- 1,
1225 for_each_subsys(ss
, i
) {
1226 if (strcmp(token
, ss
->name
))
1231 /* Mutually exclusive option 'all' + subsystem name */
1234 set_bit(i
, &opts
->subsys_mask
);
1239 if (i
== CGROUP_SUBSYS_COUNT
)
1244 * If the 'all' option was specified select all the subsystems,
1245 * otherwise if 'none', 'name=' and a subsystem name options
1246 * were not specified, let's default to 'all'
1248 if (all_ss
|| (!one_ss
&& !opts
->none
&& !opts
->name
))
1249 for_each_subsys(ss
, i
)
1251 set_bit(i
, &opts
->subsys_mask
);
1253 /* Consistency checks */
1255 if (opts
->flags
& CGRP_ROOT_SANE_BEHAVIOR
) {
1256 pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
1258 if (opts
->flags
& CGRP_ROOT_NOPREFIX
) {
1259 pr_err("cgroup: sane_behavior: noprefix is not allowed\n");
1263 if (opts
->cpuset_clone_children
) {
1264 pr_err("cgroup: sane_behavior: clone_children is not allowed\n");
1270 * Option noprefix was introduced just for backward compatibility
1271 * with the old cpuset, so we allow noprefix only if mounting just
1272 * the cpuset subsystem.
1274 if ((opts
->flags
& CGRP_ROOT_NOPREFIX
) && (opts
->subsys_mask
& mask
))
1278 /* Can't specify "none" and some subsystems */
1279 if (opts
->subsys_mask
&& opts
->none
)
1283 * We either have to specify by name or by subsystems. (So all
1284 * empty hierarchies must have a name).
1286 if (!opts
->subsys_mask
&& !opts
->name
)
1290 * Grab references on all the modules we'll need, so the subsystems
1291 * don't dance around before rebind_subsystems attaches them. This may
1292 * take duplicate reference counts on a subsystem that's already used,
1293 * but rebind_subsystems handles this case.
1295 for_each_subsys(ss
, i
) {
1296 if (!(opts
->subsys_mask
& (1UL << i
)))
1298 if (!try_module_get(cgroup_subsys
[i
]->module
)) {
1299 module_pin_failed
= true;
1303 if (module_pin_failed
) {
1305 * oops, one of the modules was going away. this means that we
1306 * raced with a module_delete call, and to the user this is
1307 * essentially a "subsystem doesn't exist" case.
1309 for (i
--; i
>= 0; i
--) {
1310 /* drop refcounts only on the ones we took */
1311 unsigned long bit
= 1UL << i
;
1313 if (!(bit
& opts
->subsys_mask
))
1315 module_put(cgroup_subsys
[i
]->module
);
1323 static void drop_parsed_module_refcounts(unsigned long subsys_mask
)
1325 struct cgroup_subsys
*ss
;
1328 mutex_lock(&cgroup_mutex
);
1329 for_each_subsys(ss
, i
)
1330 if (subsys_mask
& (1UL << i
))
1331 module_put(cgroup_subsys
[i
]->module
);
1332 mutex_unlock(&cgroup_mutex
);
1335 static int cgroup_remount(struct super_block
*sb
, int *flags
, char *data
)
1338 struct cgroupfs_root
*root
= sb
->s_fs_info
;
1339 struct cgroup
*cgrp
= &root
->top_cgroup
;
1340 struct cgroup_sb_opts opts
;
1341 unsigned long added_mask
, removed_mask
;
1343 if (root
->flags
& CGRP_ROOT_SANE_BEHAVIOR
) {
1344 pr_err("cgroup: sane_behavior: remount is not allowed\n");
1348 mutex_lock(&cgrp
->dentry
->d_inode
->i_mutex
);
1349 mutex_lock(&cgroup_mutex
);
1350 mutex_lock(&cgroup_root_mutex
);
1352 /* See what subsystems are wanted */
1353 ret
= parse_cgroupfs_options(data
, &opts
);
1357 if (opts
.subsys_mask
!= root
->subsys_mask
|| opts
.release_agent
)
1358 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1359 task_tgid_nr(current
), current
->comm
);
1361 added_mask
= opts
.subsys_mask
& ~root
->subsys_mask
;
1362 removed_mask
= root
->subsys_mask
& ~opts
.subsys_mask
;
1364 /* Don't allow flags or name to change at remount */
1365 if (((opts
.flags
^ root
->flags
) & CGRP_ROOT_OPTION_MASK
) ||
1366 (opts
.name
&& strcmp(opts
.name
, root
->name
))) {
1367 pr_err("cgroup: option or name mismatch, new: 0x%lx \"%s\", old: 0x%lx \"%s\"\n",
1368 opts
.flags
& CGRP_ROOT_OPTION_MASK
, opts
.name
?: "",
1369 root
->flags
& CGRP_ROOT_OPTION_MASK
, root
->name
);
1375 * Clear out the files of subsystems that should be removed, do
1376 * this before rebind_subsystems, since rebind_subsystems may
1377 * change this hierarchy's subsys_list.
1379 cgroup_clear_directory(cgrp
->dentry
, false, removed_mask
);
1381 ret
= rebind_subsystems(root
, added_mask
, removed_mask
);
1383 /* rebind_subsystems failed, re-populate the removed files */
1384 cgroup_populate_dir(cgrp
, false, removed_mask
);
1388 /* re-populate subsystem files */
1389 cgroup_populate_dir(cgrp
, false, added_mask
);
1391 if (opts
.release_agent
)
1392 strcpy(root
->release_agent_path
, opts
.release_agent
);
1394 kfree(opts
.release_agent
);
1396 mutex_unlock(&cgroup_root_mutex
);
1397 mutex_unlock(&cgroup_mutex
);
1398 mutex_unlock(&cgrp
->dentry
->d_inode
->i_mutex
);
1400 drop_parsed_module_refcounts(opts
.subsys_mask
);
1404 static const struct super_operations cgroup_ops
= {
1405 .statfs
= simple_statfs
,
1406 .drop_inode
= generic_delete_inode
,
1407 .show_options
= cgroup_show_options
,
1408 .remount_fs
= cgroup_remount
,
1411 static void init_cgroup_housekeeping(struct cgroup
*cgrp
)
1413 INIT_LIST_HEAD(&cgrp
->sibling
);
1414 INIT_LIST_HEAD(&cgrp
->children
);
1415 INIT_LIST_HEAD(&cgrp
->files
);
1416 INIT_LIST_HEAD(&cgrp
->cset_links
);
1417 INIT_LIST_HEAD(&cgrp
->release_list
);
1418 INIT_LIST_HEAD(&cgrp
->pidlists
);
1419 mutex_init(&cgrp
->pidlist_mutex
);
1420 INIT_LIST_HEAD(&cgrp
->event_list
);
1421 spin_lock_init(&cgrp
->event_list_lock
);
1422 simple_xattrs_init(&cgrp
->xattrs
);
1425 static void init_cgroup_root(struct cgroupfs_root
*root
)
1427 struct cgroup
*cgrp
= &root
->top_cgroup
;
1429 INIT_LIST_HEAD(&root
->subsys_list
);
1430 INIT_LIST_HEAD(&root
->root_list
);
1431 root
->number_of_cgroups
= 1;
1433 RCU_INIT_POINTER(cgrp
->name
, &root_cgroup_name
);
1434 init_cgroup_housekeeping(cgrp
);
1437 static int cgroup_init_root_id(struct cgroupfs_root
*root
, int start
, int end
)
1441 lockdep_assert_held(&cgroup_mutex
);
1442 lockdep_assert_held(&cgroup_root_mutex
);
1444 id
= idr_alloc_cyclic(&cgroup_hierarchy_idr
, root
, start
, end
,
1449 root
->hierarchy_id
= id
;
1453 static void cgroup_exit_root_id(struct cgroupfs_root
*root
)
1455 lockdep_assert_held(&cgroup_mutex
);
1456 lockdep_assert_held(&cgroup_root_mutex
);
1458 if (root
->hierarchy_id
) {
1459 idr_remove(&cgroup_hierarchy_idr
, root
->hierarchy_id
);
1460 root
->hierarchy_id
= 0;
1464 static int cgroup_test_super(struct super_block
*sb
, void *data
)
1466 struct cgroup_sb_opts
*opts
= data
;
1467 struct cgroupfs_root
*root
= sb
->s_fs_info
;
1469 /* If we asked for a name then it must match */
1470 if (opts
->name
&& strcmp(opts
->name
, root
->name
))
1474 * If we asked for subsystems (or explicitly for no
1475 * subsystems) then they must match
1477 if ((opts
->subsys_mask
|| opts
->none
)
1478 && (opts
->subsys_mask
!= root
->subsys_mask
))
1484 static struct cgroupfs_root
*cgroup_root_from_opts(struct cgroup_sb_opts
*opts
)
1486 struct cgroupfs_root
*root
;
1488 if (!opts
->subsys_mask
&& !opts
->none
)
1491 root
= kzalloc(sizeof(*root
), GFP_KERNEL
);
1493 return ERR_PTR(-ENOMEM
);
1495 init_cgroup_root(root
);
1498 * We need to set @root->subsys_mask now so that @root can be
1499 * matched by cgroup_test_super() before it finishes
1500 * initialization; otherwise, competing mounts with the same
1501 * options may try to bind the same subsystems instead of waiting
1502 * for the first one leading to unexpected mount errors.
1503 * SUBSYS_BOUND will be set once actual binding is complete.
1505 root
->subsys_mask
= opts
->subsys_mask
;
1506 root
->flags
= opts
->flags
;
1507 ida_init(&root
->cgroup_ida
);
1508 if (opts
->release_agent
)
1509 strcpy(root
->release_agent_path
, opts
->release_agent
);
1511 strcpy(root
->name
, opts
->name
);
1512 if (opts
->cpuset_clone_children
)
1513 set_bit(CGRP_CPUSET_CLONE_CHILDREN
, &root
->top_cgroup
.flags
);
1517 static void cgroup_free_root(struct cgroupfs_root
*root
)
1520 /* hierarhcy ID shoulid already have been released */
1521 WARN_ON_ONCE(root
->hierarchy_id
);
1523 ida_destroy(&root
->cgroup_ida
);
1528 static int cgroup_set_super(struct super_block
*sb
, void *data
)
1531 struct cgroup_sb_opts
*opts
= data
;
1533 /* If we don't have a new root, we can't set up a new sb */
1534 if (!opts
->new_root
)
1537 BUG_ON(!opts
->subsys_mask
&& !opts
->none
);
1539 ret
= set_anon_super(sb
, NULL
);
1543 sb
->s_fs_info
= opts
->new_root
;
1544 opts
->new_root
->sb
= sb
;
1546 sb
->s_blocksize
= PAGE_CACHE_SIZE
;
1547 sb
->s_blocksize_bits
= PAGE_CACHE_SHIFT
;
1548 sb
->s_magic
= CGROUP_SUPER_MAGIC
;
1549 sb
->s_op
= &cgroup_ops
;
1554 static int cgroup_get_rootdir(struct super_block
*sb
)
1556 static const struct dentry_operations cgroup_dops
= {
1557 .d_iput
= cgroup_diput
,
1558 .d_delete
= cgroup_delete
,
1561 struct inode
*inode
=
1562 cgroup_new_inode(S_IFDIR
| S_IRUGO
| S_IXUGO
| S_IWUSR
, sb
);
1567 inode
->i_fop
= &simple_dir_operations
;
1568 inode
->i_op
= &cgroup_dir_inode_operations
;
1569 /* directories start off with i_nlink == 2 (for "." entry) */
1571 sb
->s_root
= d_make_root(inode
);
1574 /* for everything else we want ->d_op set */
1575 sb
->s_d_op
= &cgroup_dops
;
1579 static struct dentry
*cgroup_mount(struct file_system_type
*fs_type
,
1580 int flags
, const char *unused_dev_name
,
1583 struct cgroup_sb_opts opts
;
1584 struct cgroupfs_root
*root
;
1586 struct super_block
*sb
;
1587 struct cgroupfs_root
*new_root
;
1588 struct inode
*inode
;
1590 /* First find the desired set of subsystems */
1591 mutex_lock(&cgroup_mutex
);
1592 ret
= parse_cgroupfs_options(data
, &opts
);
1593 mutex_unlock(&cgroup_mutex
);
1598 * Allocate a new cgroup root. We may not need it if we're
1599 * reusing an existing hierarchy.
1601 new_root
= cgroup_root_from_opts(&opts
);
1602 if (IS_ERR(new_root
)) {
1603 ret
= PTR_ERR(new_root
);
1606 opts
.new_root
= new_root
;
1608 /* Locate an existing or new sb for this hierarchy */
1609 sb
= sget(fs_type
, cgroup_test_super
, cgroup_set_super
, 0, &opts
);
1612 cgroup_free_root(opts
.new_root
);
1616 root
= sb
->s_fs_info
;
1618 if (root
== opts
.new_root
) {
1619 /* We used the new root structure, so this is a new hierarchy */
1620 struct list_head tmp_links
;
1621 struct cgroup
*root_cgrp
= &root
->top_cgroup
;
1622 struct cgroupfs_root
*existing_root
;
1623 const struct cred
*cred
;
1625 struct css_set
*cset
;
1627 BUG_ON(sb
->s_root
!= NULL
);
1629 ret
= cgroup_get_rootdir(sb
);
1631 goto drop_new_super
;
1632 inode
= sb
->s_root
->d_inode
;
1634 mutex_lock(&inode
->i_mutex
);
1635 mutex_lock(&cgroup_mutex
);
1636 mutex_lock(&cgroup_root_mutex
);
1638 /* Check for name clashes with existing mounts */
1640 if (strlen(root
->name
))
1641 for_each_active_root(existing_root
)
1642 if (!strcmp(existing_root
->name
, root
->name
))
1646 * We're accessing css_set_count without locking
1647 * css_set_lock here, but that's OK - it can only be
1648 * increased by someone holding cgroup_lock, and
1649 * that's us. The worst that can happen is that we
1650 * have some link structures left over
1652 ret
= allocate_cgrp_cset_links(css_set_count
, &tmp_links
);
1656 /* ID 0 is reserved for dummy root, 1 for unified hierarchy */
1657 ret
= cgroup_init_root_id(root
, 2, 0);
1661 ret
= rebind_subsystems(root
, root
->subsys_mask
, 0);
1662 if (ret
== -EBUSY
) {
1663 free_cgrp_cset_links(&tmp_links
);
1667 * There must be no failure case after here, since rebinding
1668 * takes care of subsystems' refcounts, which are explicitly
1669 * dropped in the failure exit path.
1672 /* EBUSY should be the only error here */
1675 list_add(&root
->root_list
, &cgroup_roots
);
1676 cgroup_root_count
++;
1678 sb
->s_root
->d_fsdata
= root_cgrp
;
1679 root
->top_cgroup
.dentry
= sb
->s_root
;
1681 /* Link the top cgroup in this hierarchy into all
1682 * the css_set objects */
1683 write_lock(&css_set_lock
);
1684 hash_for_each(css_set_table
, i
, cset
, hlist
)
1685 link_css_set(&tmp_links
, cset
, root_cgrp
);
1686 write_unlock(&css_set_lock
);
1688 free_cgrp_cset_links(&tmp_links
);
1690 BUG_ON(!list_empty(&root_cgrp
->children
));
1691 BUG_ON(root
->number_of_cgroups
!= 1);
1693 cred
= override_creds(&init_cred
);
1694 cgroup_populate_dir(root_cgrp
, true, root
->subsys_mask
);
1696 mutex_unlock(&cgroup_root_mutex
);
1697 mutex_unlock(&cgroup_mutex
);
1698 mutex_unlock(&inode
->i_mutex
);
1701 * We re-used an existing hierarchy - the new root (if
1702 * any) is not needed
1704 cgroup_free_root(opts
.new_root
);
1706 if ((root
->flags
^ opts
.flags
) & CGRP_ROOT_OPTION_MASK
) {
1707 if ((root
->flags
| opts
.flags
) & CGRP_ROOT_SANE_BEHAVIOR
) {
1708 pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
1710 goto drop_new_super
;
1712 pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n");
1716 /* no subsys rebinding, so refcounts don't change */
1717 drop_parsed_module_refcounts(opts
.subsys_mask
);
1720 kfree(opts
.release_agent
);
1722 return dget(sb
->s_root
);
1725 cgroup_exit_root_id(root
);
1726 mutex_unlock(&cgroup_root_mutex
);
1727 mutex_unlock(&cgroup_mutex
);
1728 mutex_unlock(&inode
->i_mutex
);
1730 deactivate_locked_super(sb
);
1732 drop_parsed_module_refcounts(opts
.subsys_mask
);
1734 kfree(opts
.release_agent
);
1736 return ERR_PTR(ret
);
1739 static void cgroup_kill_sb(struct super_block
*sb
) {
1740 struct cgroupfs_root
*root
= sb
->s_fs_info
;
1741 struct cgroup
*cgrp
= &root
->top_cgroup
;
1742 struct cgrp_cset_link
*link
, *tmp_link
;
1747 BUG_ON(root
->number_of_cgroups
!= 1);
1748 BUG_ON(!list_empty(&cgrp
->children
));
1750 mutex_lock(&cgroup_mutex
);
1751 mutex_lock(&cgroup_root_mutex
);
1753 /* Rebind all subsystems back to the default hierarchy */
1754 if (root
->flags
& CGRP_ROOT_SUBSYS_BOUND
) {
1755 ret
= rebind_subsystems(root
, 0, root
->subsys_mask
);
1756 /* Shouldn't be able to fail ... */
1761 * Release all the links from cset_links to this hierarchy's
1764 write_lock(&css_set_lock
);
1766 list_for_each_entry_safe(link
, tmp_link
, &cgrp
->cset_links
, cset_link
) {
1767 list_del(&link
->cset_link
);
1768 list_del(&link
->cgrp_link
);
1771 write_unlock(&css_set_lock
);
1773 if (!list_empty(&root
->root_list
)) {
1774 list_del(&root
->root_list
);
1775 cgroup_root_count
--;
1778 cgroup_exit_root_id(root
);
1780 mutex_unlock(&cgroup_root_mutex
);
1781 mutex_unlock(&cgroup_mutex
);
1783 simple_xattrs_free(&cgrp
->xattrs
);
1785 kill_litter_super(sb
);
1786 cgroup_free_root(root
);
1789 static struct file_system_type cgroup_fs_type
= {
1791 .mount
= cgroup_mount
,
1792 .kill_sb
= cgroup_kill_sb
,
1795 static struct kobject
*cgroup_kobj
;
1798 * cgroup_path - generate the path of a cgroup
1799 * @cgrp: the cgroup in question
1800 * @buf: the buffer to write the path into
1801 * @buflen: the length of the buffer
1803 * Writes path of cgroup into buf. Returns 0 on success, -errno on error.
1805 * We can't generate cgroup path using dentry->d_name, as accessing
1806 * dentry->name must be protected by irq-unsafe dentry->d_lock or parent
1807 * inode's i_mutex, while on the other hand cgroup_path() can be called
1808 * with some irq-safe spinlocks held.
1810 int cgroup_path(const struct cgroup
*cgrp
, char *buf
, int buflen
)
1812 int ret
= -ENAMETOOLONG
;
1815 if (!cgrp
->parent
) {
1816 if (strlcpy(buf
, "/", buflen
) >= buflen
)
1817 return -ENAMETOOLONG
;
1821 start
= buf
+ buflen
- 1;
1826 const char *name
= cgroup_name(cgrp
);
1830 if ((start
-= len
) < buf
)
1832 memcpy(start
, name
, len
);
1838 cgrp
= cgrp
->parent
;
1839 } while (cgrp
->parent
);
1841 memmove(buf
, start
, buf
+ buflen
- start
);
1846 EXPORT_SYMBOL_GPL(cgroup_path
);
1849 * task_cgroup_path_from_hierarchy - cgroup path of a task on a hierarchy
1850 * @task: target task
1851 * @hierarchy_id: the hierarchy to look up @task's cgroup from
1852 * @buf: the buffer to write the path into
1853 * @buflen: the length of the buffer
1855 * Determine @task's cgroup on the hierarchy specified by @hierarchy_id and
1856 * copy its path into @buf. This function grabs cgroup_mutex and shouldn't
1857 * be used inside locks used by cgroup controller callbacks.
1859 int task_cgroup_path_from_hierarchy(struct task_struct
*task
, int hierarchy_id
,
1860 char *buf
, size_t buflen
)
1862 struct cgroupfs_root
*root
;
1863 struct cgroup
*cgrp
= NULL
;
1866 mutex_lock(&cgroup_mutex
);
1868 root
= idr_find(&cgroup_hierarchy_idr
, hierarchy_id
);
1870 cgrp
= task_cgroup_from_root(task
, root
);
1871 ret
= cgroup_path(cgrp
, buf
, buflen
);
1874 mutex_unlock(&cgroup_mutex
);
1878 EXPORT_SYMBOL_GPL(task_cgroup_path_from_hierarchy
);
1881 * Control Group taskset
1883 struct task_and_cgroup
{
1884 struct task_struct
*task
;
1885 struct cgroup
*cgrp
;
1889 struct cgroup_taskset
{
1890 struct task_and_cgroup single
;
1891 struct flex_array
*tc_array
;
1894 struct cgroup
*cur_cgrp
;
1898 * cgroup_taskset_first - reset taskset and return the first task
1899 * @tset: taskset of interest
1901 * @tset iteration is initialized and the first task is returned.
1903 struct task_struct
*cgroup_taskset_first(struct cgroup_taskset
*tset
)
1905 if (tset
->tc_array
) {
1907 return cgroup_taskset_next(tset
);
1909 tset
->cur_cgrp
= tset
->single
.cgrp
;
1910 return tset
->single
.task
;
1913 EXPORT_SYMBOL_GPL(cgroup_taskset_first
);
1916 * cgroup_taskset_next - iterate to the next task in taskset
1917 * @tset: taskset of interest
1919 * Return the next task in @tset. Iteration must have been initialized
1920 * with cgroup_taskset_first().
1922 struct task_struct
*cgroup_taskset_next(struct cgroup_taskset
*tset
)
1924 struct task_and_cgroup
*tc
;
1926 if (!tset
->tc_array
|| tset
->idx
>= tset
->tc_array_len
)
1929 tc
= flex_array_get(tset
->tc_array
, tset
->idx
++);
1930 tset
->cur_cgrp
= tc
->cgrp
;
1933 EXPORT_SYMBOL_GPL(cgroup_taskset_next
);
1936 * cgroup_taskset_cur_cgroup - return the matching cgroup for the current task
1937 * @tset: taskset of interest
1939 * Return the cgroup for the current (last returned) task of @tset. This
1940 * function must be preceded by either cgroup_taskset_first() or
1941 * cgroup_taskset_next().
1943 struct cgroup
*cgroup_taskset_cur_cgroup(struct cgroup_taskset
*tset
)
1945 return tset
->cur_cgrp
;
1947 EXPORT_SYMBOL_GPL(cgroup_taskset_cur_cgroup
);
1950 * cgroup_taskset_size - return the number of tasks in taskset
1951 * @tset: taskset of interest
1953 int cgroup_taskset_size(struct cgroup_taskset
*tset
)
1955 return tset
->tc_array
? tset
->tc_array_len
: 1;
1957 EXPORT_SYMBOL_GPL(cgroup_taskset_size
);
1961 * cgroup_task_migrate - move a task from one cgroup to another.
1963 * Must be called with cgroup_mutex and threadgroup locked.
1965 static void cgroup_task_migrate(struct cgroup
*old_cgrp
,
1966 struct task_struct
*tsk
,
1967 struct css_set
*new_cset
)
1969 struct css_set
*old_cset
;
1972 * We are synchronized through threadgroup_lock() against PF_EXITING
1973 * setting such that we can't race against cgroup_exit() changing the
1974 * css_set to init_css_set and dropping the old one.
1976 WARN_ON_ONCE(tsk
->flags
& PF_EXITING
);
1977 old_cset
= task_css_set(tsk
);
1980 rcu_assign_pointer(tsk
->cgroups
, new_cset
);
1983 /* Update the css_set linked lists if we're using them */
1984 write_lock(&css_set_lock
);
1985 if (!list_empty(&tsk
->cg_list
))
1986 list_move(&tsk
->cg_list
, &new_cset
->tasks
);
1987 write_unlock(&css_set_lock
);
1990 * We just gained a reference on old_cset by taking it from the
1991 * task. As trading it for new_cset is protected by cgroup_mutex,
1992 * we're safe to drop it here; it will be freed under RCU.
1994 set_bit(CGRP_RELEASABLE
, &old_cgrp
->flags
);
1995 put_css_set(old_cset
);
1999 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
2000 * @cgrp: the cgroup to attach to
2001 * @tsk: the task or the leader of the threadgroup to be attached
2002 * @threadgroup: attach the whole threadgroup?
2004 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
2005 * task_lock of @tsk or each thread in the threadgroup individually in turn.
2007 static int cgroup_attach_task(struct cgroup
*cgrp
, struct task_struct
*tsk
,
2010 int retval
, i
, group_size
;
2011 struct cgroup_subsys
*ss
, *failed_ss
= NULL
;
2012 struct cgroupfs_root
*root
= cgrp
->root
;
2013 /* threadgroup list cursor and array */
2014 struct task_struct
*leader
= tsk
;
2015 struct task_and_cgroup
*tc
;
2016 struct flex_array
*group
;
2017 struct cgroup_taskset tset
= { };
2020 * step 0: in order to do expensive, possibly blocking operations for
2021 * every thread, we cannot iterate the thread group list, since it needs
2022 * rcu or tasklist locked. instead, build an array of all threads in the
2023 * group - group_rwsem prevents new threads from appearing, and if
2024 * threads exit, this will just be an over-estimate.
2027 group_size
= get_nr_threads(tsk
);
2030 /* flex_array supports very large thread-groups better than kmalloc. */
2031 group
= flex_array_alloc(sizeof(*tc
), group_size
, GFP_KERNEL
);
2034 /* pre-allocate to guarantee space while iterating in rcu read-side. */
2035 retval
= flex_array_prealloc(group
, 0, group_size
, GFP_KERNEL
);
2037 goto out_free_group_list
;
2041 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2042 * already PF_EXITING could be freed from underneath us unless we
2043 * take an rcu_read_lock.
2047 struct task_and_cgroup ent
;
2049 /* @tsk either already exited or can't exit until the end */
2050 if (tsk
->flags
& PF_EXITING
)
2053 /* as per above, nr_threads may decrease, but not increase. */
2054 BUG_ON(i
>= group_size
);
2056 ent
.cgrp
= task_cgroup_from_root(tsk
, root
);
2057 /* nothing to do if this task is already in the cgroup */
2058 if (ent
.cgrp
== cgrp
)
2061 * saying GFP_ATOMIC has no effect here because we did prealloc
2062 * earlier, but it's good form to communicate our expectations.
2064 retval
= flex_array_put(group
, i
, &ent
, GFP_ATOMIC
);
2065 BUG_ON(retval
!= 0);
2070 } while_each_thread(leader
, tsk
);
2072 /* remember the number of threads in the array for later. */
2074 tset
.tc_array
= group
;
2075 tset
.tc_array_len
= group_size
;
2077 /* methods shouldn't be called if no task is actually migrating */
2080 goto out_free_group_list
;
2083 * step 1: check that we can legitimately attach to the cgroup.
2085 for_each_root_subsys(root
, ss
) {
2086 if (ss
->can_attach
) {
2087 retval
= ss
->can_attach(cgrp
, &tset
);
2090 goto out_cancel_attach
;
2096 * step 2: make sure css_sets exist for all threads to be migrated.
2097 * we use find_css_set, which allocates a new one if necessary.
2099 for (i
= 0; i
< group_size
; i
++) {
2100 struct css_set
*old_cset
;
2102 tc
= flex_array_get(group
, i
);
2103 old_cset
= task_css_set(tc
->task
);
2104 tc
->cg
= find_css_set(old_cset
, cgrp
);
2107 goto out_put_css_set_refs
;
2112 * step 3: now that we're guaranteed success wrt the css_sets,
2113 * proceed to move all tasks to the new cgroup. There are no
2114 * failure cases after here, so this is the commit point.
2116 for (i
= 0; i
< group_size
; i
++) {
2117 tc
= flex_array_get(group
, i
);
2118 cgroup_task_migrate(tc
->cgrp
, tc
->task
, tc
->cg
);
2120 /* nothing is sensitive to fork() after this point. */
2123 * step 4: do subsystem attach callbacks.
2125 for_each_root_subsys(root
, ss
) {
2127 ss
->attach(cgrp
, &tset
);
2131 * step 5: success! and cleanup
2134 out_put_css_set_refs
:
2136 for (i
= 0; i
< group_size
; i
++) {
2137 tc
= flex_array_get(group
, i
);
2140 put_css_set(tc
->cg
);
2145 for_each_root_subsys(root
, ss
) {
2146 if (ss
== failed_ss
)
2148 if (ss
->cancel_attach
)
2149 ss
->cancel_attach(cgrp
, &tset
);
2152 out_free_group_list
:
2153 flex_array_free(group
);
2158 * Find the task_struct of the task to attach by vpid and pass it along to the
2159 * function to attach either it or all tasks in its threadgroup. Will lock
2160 * cgroup_mutex and threadgroup; may take task_lock of task.
2162 static int attach_task_by_pid(struct cgroup
*cgrp
, u64 pid
, bool threadgroup
)
2164 struct task_struct
*tsk
;
2165 const struct cred
*cred
= current_cred(), *tcred
;
2168 if (!cgroup_lock_live_group(cgrp
))
2174 tsk
= find_task_by_vpid(pid
);
2178 goto out_unlock_cgroup
;
2181 * even if we're attaching all tasks in the thread group, we
2182 * only need to check permissions on one of them.
2184 tcred
= __task_cred(tsk
);
2185 if (!uid_eq(cred
->euid
, GLOBAL_ROOT_UID
) &&
2186 !uid_eq(cred
->euid
, tcred
->uid
) &&
2187 !uid_eq(cred
->euid
, tcred
->suid
)) {
2190 goto out_unlock_cgroup
;
2196 tsk
= tsk
->group_leader
;
2199 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
2200 * trapped in a cpuset, or RT worker may be born in a cgroup
2201 * with no rt_runtime allocated. Just say no.
2203 if (tsk
== kthreadd_task
|| (tsk
->flags
& PF_NO_SETAFFINITY
)) {
2206 goto out_unlock_cgroup
;
2209 get_task_struct(tsk
);
2212 threadgroup_lock(tsk
);
2214 if (!thread_group_leader(tsk
)) {
2216 * a race with de_thread from another thread's exec()
2217 * may strip us of our leadership, if this happens,
2218 * there is no choice but to throw this task away and
2219 * try again; this is
2220 * "double-double-toil-and-trouble-check locking".
2222 threadgroup_unlock(tsk
);
2223 put_task_struct(tsk
);
2224 goto retry_find_task
;
2228 ret
= cgroup_attach_task(cgrp
, tsk
, threadgroup
);
2230 threadgroup_unlock(tsk
);
2232 put_task_struct(tsk
);
2234 mutex_unlock(&cgroup_mutex
);
2239 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2240 * @from: attach to all cgroups of a given task
2241 * @tsk: the task to be attached
2243 int cgroup_attach_task_all(struct task_struct
*from
, struct task_struct
*tsk
)
2245 struct cgroupfs_root
*root
;
2248 mutex_lock(&cgroup_mutex
);
2249 for_each_active_root(root
) {
2250 struct cgroup
*from_cg
= task_cgroup_from_root(from
, root
);
2252 retval
= cgroup_attach_task(from_cg
, tsk
, false);
2256 mutex_unlock(&cgroup_mutex
);
2260 EXPORT_SYMBOL_GPL(cgroup_attach_task_all
);
2262 static int cgroup_tasks_write(struct cgroup
*cgrp
, struct cftype
*cft
, u64 pid
)
2264 return attach_task_by_pid(cgrp
, pid
, false);
2267 static int cgroup_procs_write(struct cgroup
*cgrp
, struct cftype
*cft
, u64 tgid
)
2269 return attach_task_by_pid(cgrp
, tgid
, true);
2272 static int cgroup_release_agent_write(struct cgroup
*cgrp
, struct cftype
*cft
,
2275 BUILD_BUG_ON(sizeof(cgrp
->root
->release_agent_path
) < PATH_MAX
);
2276 if (strlen(buffer
) >= PATH_MAX
)
2278 if (!cgroup_lock_live_group(cgrp
))
2280 mutex_lock(&cgroup_root_mutex
);
2281 strcpy(cgrp
->root
->release_agent_path
, buffer
);
2282 mutex_unlock(&cgroup_root_mutex
);
2283 mutex_unlock(&cgroup_mutex
);
2287 static int cgroup_release_agent_show(struct cgroup
*cgrp
, struct cftype
*cft
,
2288 struct seq_file
*seq
)
2290 if (!cgroup_lock_live_group(cgrp
))
2292 seq_puts(seq
, cgrp
->root
->release_agent_path
);
2293 seq_putc(seq
, '\n');
2294 mutex_unlock(&cgroup_mutex
);
2298 static int cgroup_sane_behavior_show(struct cgroup
*cgrp
, struct cftype
*cft
,
2299 struct seq_file
*seq
)
2301 seq_printf(seq
, "%d\n", cgroup_sane_behavior(cgrp
));
2305 /* A buffer size big enough for numbers or short strings */
2306 #define CGROUP_LOCAL_BUFFER_SIZE 64
2308 static ssize_t
cgroup_write_X64(struct cgroup
*cgrp
, struct cftype
*cft
,
2310 const char __user
*userbuf
,
2311 size_t nbytes
, loff_t
*unused_ppos
)
2313 char buffer
[CGROUP_LOCAL_BUFFER_SIZE
];
2319 if (nbytes
>= sizeof(buffer
))
2321 if (copy_from_user(buffer
, userbuf
, nbytes
))
2324 buffer
[nbytes
] = 0; /* nul-terminate */
2325 if (cft
->write_u64
) {
2326 u64 val
= simple_strtoull(strstrip(buffer
), &end
, 0);
2329 retval
= cft
->write_u64(cgrp
, cft
, val
);
2331 s64 val
= simple_strtoll(strstrip(buffer
), &end
, 0);
2334 retval
= cft
->write_s64(cgrp
, cft
, val
);
2341 static ssize_t
cgroup_write_string(struct cgroup
*cgrp
, struct cftype
*cft
,
2343 const char __user
*userbuf
,
2344 size_t nbytes
, loff_t
*unused_ppos
)
2346 char local_buffer
[CGROUP_LOCAL_BUFFER_SIZE
];
2348 size_t max_bytes
= cft
->max_write_len
;
2349 char *buffer
= local_buffer
;
2352 max_bytes
= sizeof(local_buffer
) - 1;
2353 if (nbytes
>= max_bytes
)
2355 /* Allocate a dynamic buffer if we need one */
2356 if (nbytes
>= sizeof(local_buffer
)) {
2357 buffer
= kmalloc(nbytes
+ 1, GFP_KERNEL
);
2361 if (nbytes
&& copy_from_user(buffer
, userbuf
, nbytes
)) {
2366 buffer
[nbytes
] = 0; /* nul-terminate */
2367 retval
= cft
->write_string(cgrp
, cft
, strstrip(buffer
));
2371 if (buffer
!= local_buffer
)
2376 static ssize_t
cgroup_file_write(struct file
*file
, const char __user
*buf
,
2377 size_t nbytes
, loff_t
*ppos
)
2379 struct cftype
*cft
= __d_cft(file
->f_dentry
);
2380 struct cgroup
*cgrp
= __d_cgrp(file
->f_dentry
->d_parent
);
2382 if (cgroup_is_dead(cgrp
))
2385 return cft
->write(cgrp
, cft
, file
, buf
, nbytes
, ppos
);
2386 if (cft
->write_u64
|| cft
->write_s64
)
2387 return cgroup_write_X64(cgrp
, cft
, file
, buf
, nbytes
, ppos
);
2388 if (cft
->write_string
)
2389 return cgroup_write_string(cgrp
, cft
, file
, buf
, nbytes
, ppos
);
2391 int ret
= cft
->trigger(cgrp
, (unsigned int)cft
->private);
2392 return ret
? ret
: nbytes
;
2397 static ssize_t
cgroup_read_u64(struct cgroup
*cgrp
, struct cftype
*cft
,
2399 char __user
*buf
, size_t nbytes
,
2402 char tmp
[CGROUP_LOCAL_BUFFER_SIZE
];
2403 u64 val
= cft
->read_u64(cgrp
, cft
);
2404 int len
= sprintf(tmp
, "%llu\n", (unsigned long long) val
);
2406 return simple_read_from_buffer(buf
, nbytes
, ppos
, tmp
, len
);
2409 static ssize_t
cgroup_read_s64(struct cgroup
*cgrp
, struct cftype
*cft
,
2411 char __user
*buf
, size_t nbytes
,
2414 char tmp
[CGROUP_LOCAL_BUFFER_SIZE
];
2415 s64 val
= cft
->read_s64(cgrp
, cft
);
2416 int len
= sprintf(tmp
, "%lld\n", (long long) val
);
2418 return simple_read_from_buffer(buf
, nbytes
, ppos
, tmp
, len
);
2421 static ssize_t
cgroup_file_read(struct file
*file
, char __user
*buf
,
2422 size_t nbytes
, loff_t
*ppos
)
2424 struct cftype
*cft
= __d_cft(file
->f_dentry
);
2425 struct cgroup
*cgrp
= __d_cgrp(file
->f_dentry
->d_parent
);
2427 if (cgroup_is_dead(cgrp
))
2431 return cft
->read(cgrp
, cft
, file
, buf
, nbytes
, ppos
);
2433 return cgroup_read_u64(cgrp
, cft
, file
, buf
, nbytes
, ppos
);
2435 return cgroup_read_s64(cgrp
, cft
, file
, buf
, nbytes
, ppos
);
2440 * seqfile ops/methods for returning structured data. Currently just
2441 * supports string->u64 maps, but can be extended in future.
2444 struct cgroup_seqfile_state
{
2446 struct cgroup
*cgroup
;
2449 static int cgroup_map_add(struct cgroup_map_cb
*cb
, const char *key
, u64 value
)
2451 struct seq_file
*sf
= cb
->state
;
2452 return seq_printf(sf
, "%s %llu\n", key
, (unsigned long long)value
);
2455 static int cgroup_seqfile_show(struct seq_file
*m
, void *arg
)
2457 struct cgroup_seqfile_state
*state
= m
->private;
2458 struct cftype
*cft
= state
->cft
;
2459 if (cft
->read_map
) {
2460 struct cgroup_map_cb cb
= {
2461 .fill
= cgroup_map_add
,
2464 return cft
->read_map(state
->cgroup
, cft
, &cb
);
2466 return cft
->read_seq_string(state
->cgroup
, cft
, m
);
2469 static int cgroup_seqfile_release(struct inode
*inode
, struct file
*file
)
2471 struct seq_file
*seq
= file
->private_data
;
2472 kfree(seq
->private);
2473 return single_release(inode
, file
);
2476 static const struct file_operations cgroup_seqfile_operations
= {
2478 .write
= cgroup_file_write
,
2479 .llseek
= seq_lseek
,
2480 .release
= cgroup_seqfile_release
,
2483 static int cgroup_file_open(struct inode
*inode
, struct file
*file
)
2488 err
= generic_file_open(inode
, file
);
2491 cft
= __d_cft(file
->f_dentry
);
2493 if (cft
->read_map
|| cft
->read_seq_string
) {
2494 struct cgroup_seqfile_state
*state
;
2496 state
= kzalloc(sizeof(*state
), GFP_USER
);
2501 state
->cgroup
= __d_cgrp(file
->f_dentry
->d_parent
);
2502 file
->f_op
= &cgroup_seqfile_operations
;
2503 err
= single_open(file
, cgroup_seqfile_show
, state
);
2506 } else if (cft
->open
)
2507 err
= cft
->open(inode
, file
);
2514 static int cgroup_file_release(struct inode
*inode
, struct file
*file
)
2516 struct cftype
*cft
= __d_cft(file
->f_dentry
);
2518 return cft
->release(inode
, file
);
2523 * cgroup_rename - Only allow simple rename of directories in place.
2525 static int cgroup_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
2526 struct inode
*new_dir
, struct dentry
*new_dentry
)
2529 struct cgroup_name
*name
, *old_name
;
2530 struct cgroup
*cgrp
;
2533 * It's convinient to use parent dir's i_mutex to protected
2536 lockdep_assert_held(&old_dir
->i_mutex
);
2538 if (!S_ISDIR(old_dentry
->d_inode
->i_mode
))
2540 if (new_dentry
->d_inode
)
2542 if (old_dir
!= new_dir
)
2545 cgrp
= __d_cgrp(old_dentry
);
2548 * This isn't a proper migration and its usefulness is very
2549 * limited. Disallow if sane_behavior.
2551 if (cgroup_sane_behavior(cgrp
))
2554 name
= cgroup_alloc_name(new_dentry
);
2558 ret
= simple_rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
2564 old_name
= rcu_dereference_protected(cgrp
->name
, true);
2565 rcu_assign_pointer(cgrp
->name
, name
);
2567 kfree_rcu(old_name
, rcu_head
);
2571 static struct simple_xattrs
*__d_xattrs(struct dentry
*dentry
)
2573 if (S_ISDIR(dentry
->d_inode
->i_mode
))
2574 return &__d_cgrp(dentry
)->xattrs
;
2576 return &__d_cfe(dentry
)->xattrs
;
2579 static inline int xattr_enabled(struct dentry
*dentry
)
2581 struct cgroupfs_root
*root
= dentry
->d_sb
->s_fs_info
;
2582 return root
->flags
& CGRP_ROOT_XATTR
;
2585 static bool is_valid_xattr(const char *name
)
2587 if (!strncmp(name
, XATTR_TRUSTED_PREFIX
, XATTR_TRUSTED_PREFIX_LEN
) ||
2588 !strncmp(name
, XATTR_SECURITY_PREFIX
, XATTR_SECURITY_PREFIX_LEN
))
2593 static int cgroup_setxattr(struct dentry
*dentry
, const char *name
,
2594 const void *val
, size_t size
, int flags
)
2596 if (!xattr_enabled(dentry
))
2598 if (!is_valid_xattr(name
))
2600 return simple_xattr_set(__d_xattrs(dentry
), name
, val
, size
, flags
);
2603 static int cgroup_removexattr(struct dentry
*dentry
, const char *name
)
2605 if (!xattr_enabled(dentry
))
2607 if (!is_valid_xattr(name
))
2609 return simple_xattr_remove(__d_xattrs(dentry
), name
);
2612 static ssize_t
cgroup_getxattr(struct dentry
*dentry
, const char *name
,
2613 void *buf
, size_t size
)
2615 if (!xattr_enabled(dentry
))
2617 if (!is_valid_xattr(name
))
2619 return simple_xattr_get(__d_xattrs(dentry
), name
, buf
, size
);
2622 static ssize_t
cgroup_listxattr(struct dentry
*dentry
, char *buf
, size_t size
)
2624 if (!xattr_enabled(dentry
))
2626 return simple_xattr_list(__d_xattrs(dentry
), buf
, size
);
2629 static const struct file_operations cgroup_file_operations
= {
2630 .read
= cgroup_file_read
,
2631 .write
= cgroup_file_write
,
2632 .llseek
= generic_file_llseek
,
2633 .open
= cgroup_file_open
,
2634 .release
= cgroup_file_release
,
2637 static const struct inode_operations cgroup_file_inode_operations
= {
2638 .setxattr
= cgroup_setxattr
,
2639 .getxattr
= cgroup_getxattr
,
2640 .listxattr
= cgroup_listxattr
,
2641 .removexattr
= cgroup_removexattr
,
2644 static const struct inode_operations cgroup_dir_inode_operations
= {
2645 .lookup
= cgroup_lookup
,
2646 .mkdir
= cgroup_mkdir
,
2647 .rmdir
= cgroup_rmdir
,
2648 .rename
= cgroup_rename
,
2649 .setxattr
= cgroup_setxattr
,
2650 .getxattr
= cgroup_getxattr
,
2651 .listxattr
= cgroup_listxattr
,
2652 .removexattr
= cgroup_removexattr
,
2655 static struct dentry
*cgroup_lookup(struct inode
*dir
, struct dentry
*dentry
, unsigned int flags
)
2657 if (dentry
->d_name
.len
> NAME_MAX
)
2658 return ERR_PTR(-ENAMETOOLONG
);
2659 d_add(dentry
, NULL
);
2664 * Check if a file is a control file
2666 static inline struct cftype
*__file_cft(struct file
*file
)
2668 if (file_inode(file
)->i_fop
!= &cgroup_file_operations
)
2669 return ERR_PTR(-EINVAL
);
2670 return __d_cft(file
->f_dentry
);
2673 static int cgroup_create_file(struct dentry
*dentry
, umode_t mode
,
2674 struct super_block
*sb
)
2676 struct inode
*inode
;
2680 if (dentry
->d_inode
)
2683 inode
= cgroup_new_inode(mode
, sb
);
2687 if (S_ISDIR(mode
)) {
2688 inode
->i_op
= &cgroup_dir_inode_operations
;
2689 inode
->i_fop
= &simple_dir_operations
;
2691 /* start off with i_nlink == 2 (for "." entry) */
2693 inc_nlink(dentry
->d_parent
->d_inode
);
2696 * Control reaches here with cgroup_mutex held.
2697 * @inode->i_mutex should nest outside cgroup_mutex but we
2698 * want to populate it immediately without releasing
2699 * cgroup_mutex. As @inode isn't visible to anyone else
2700 * yet, trylock will always succeed without affecting
2703 WARN_ON_ONCE(!mutex_trylock(&inode
->i_mutex
));
2704 } else if (S_ISREG(mode
)) {
2706 inode
->i_fop
= &cgroup_file_operations
;
2707 inode
->i_op
= &cgroup_file_inode_operations
;
2709 d_instantiate(dentry
, inode
);
2710 dget(dentry
); /* Extra count - pin the dentry in core */
2715 * cgroup_file_mode - deduce file mode of a control file
2716 * @cft: the control file in question
2718 * returns cft->mode if ->mode is not 0
2719 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2720 * returns S_IRUGO if it has only a read handler
2721 * returns S_IWUSR if it has only a write hander
2723 static umode_t
cgroup_file_mode(const struct cftype
*cft
)
2730 if (cft
->read
|| cft
->read_u64
|| cft
->read_s64
||
2731 cft
->read_map
|| cft
->read_seq_string
)
2734 if (cft
->write
|| cft
->write_u64
|| cft
->write_s64
||
2735 cft
->write_string
|| cft
->trigger
)
2741 static int cgroup_add_file(struct cgroup
*cgrp
, struct cgroup_subsys
*subsys
,
2744 struct dentry
*dir
= cgrp
->dentry
;
2745 struct cgroup
*parent
= __d_cgrp(dir
);
2746 struct dentry
*dentry
;
2750 char name
[MAX_CGROUP_TYPE_NAMELEN
+ MAX_CFTYPE_NAME
+ 2] = { 0 };
2752 if (subsys
&& !(cgrp
->root
->flags
& CGRP_ROOT_NOPREFIX
)) {
2753 strcpy(name
, subsys
->name
);
2756 strcat(name
, cft
->name
);
2758 BUG_ON(!mutex_is_locked(&dir
->d_inode
->i_mutex
));
2760 cfe
= kzalloc(sizeof(*cfe
), GFP_KERNEL
);
2764 dentry
= lookup_one_len(name
, dir
, strlen(name
));
2765 if (IS_ERR(dentry
)) {
2766 error
= PTR_ERR(dentry
);
2770 cfe
->type
= (void *)cft
;
2771 cfe
->dentry
= dentry
;
2772 dentry
->d_fsdata
= cfe
;
2773 simple_xattrs_init(&cfe
->xattrs
);
2775 mode
= cgroup_file_mode(cft
);
2776 error
= cgroup_create_file(dentry
, mode
| S_IFREG
, cgrp
->root
->sb
);
2778 list_add_tail(&cfe
->node
, &parent
->files
);
2787 static int cgroup_addrm_files(struct cgroup
*cgrp
, struct cgroup_subsys
*subsys
,
2788 struct cftype cfts
[], bool is_add
)
2793 for (cft
= cfts
; cft
->name
[0] != '\0'; cft
++) {
2794 /* does cft->flags tell us to skip this file on @cgrp? */
2795 if ((cft
->flags
& CFTYPE_INSANE
) && cgroup_sane_behavior(cgrp
))
2797 if ((cft
->flags
& CFTYPE_NOT_ON_ROOT
) && !cgrp
->parent
)
2799 if ((cft
->flags
& CFTYPE_ONLY_ON_ROOT
) && cgrp
->parent
)
2803 err
= cgroup_add_file(cgrp
, subsys
, cft
);
2805 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
2809 cgroup_rm_file(cgrp
, cft
);
2815 static void cgroup_cfts_prepare(void)
2816 __acquires(&cgroup_mutex
)
2819 * Thanks to the entanglement with vfs inode locking, we can't walk
2820 * the existing cgroups under cgroup_mutex and create files.
2821 * Instead, we use cgroup_for_each_descendant_pre() and drop RCU
2822 * read lock before calling cgroup_addrm_files().
2824 mutex_lock(&cgroup_mutex
);
2827 static void cgroup_cfts_commit(struct cgroup_subsys
*ss
,
2828 struct cftype
*cfts
, bool is_add
)
2829 __releases(&cgroup_mutex
)
2832 struct cgroup
*cgrp
, *root
= &ss
->root
->top_cgroup
;
2833 struct super_block
*sb
= ss
->root
->sb
;
2834 struct dentry
*prev
= NULL
;
2835 struct inode
*inode
;
2838 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
2839 if (!cfts
|| ss
->root
== &cgroup_dummy_root
||
2840 !atomic_inc_not_zero(&sb
->s_active
)) {
2841 mutex_unlock(&cgroup_mutex
);
2846 * All cgroups which are created after we drop cgroup_mutex will
2847 * have the updated set of files, so we only need to update the
2848 * cgroups created before the current @cgroup_serial_nr_next.
2850 update_before
= cgroup_serial_nr_next
;
2852 mutex_unlock(&cgroup_mutex
);
2854 /* @root always needs to be updated */
2855 inode
= root
->dentry
->d_inode
;
2856 mutex_lock(&inode
->i_mutex
);
2857 mutex_lock(&cgroup_mutex
);
2858 cgroup_addrm_files(root
, ss
, cfts
, is_add
);
2859 mutex_unlock(&cgroup_mutex
);
2860 mutex_unlock(&inode
->i_mutex
);
2862 /* add/rm files for all cgroups created before */
2864 cgroup_for_each_descendant_pre(cgrp
, root
) {
2865 if (cgroup_is_dead(cgrp
))
2868 inode
= cgrp
->dentry
->d_inode
;
2873 prev
= cgrp
->dentry
;
2875 mutex_lock(&inode
->i_mutex
);
2876 mutex_lock(&cgroup_mutex
);
2877 if (cgrp
->serial_nr
< update_before
&& !cgroup_is_dead(cgrp
))
2878 cgroup_addrm_files(cgrp
, ss
, cfts
, is_add
);
2879 mutex_unlock(&cgroup_mutex
);
2880 mutex_unlock(&inode
->i_mutex
);
2886 deactivate_super(sb
);
2890 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2891 * @ss: target cgroup subsystem
2892 * @cfts: zero-length name terminated array of cftypes
2894 * Register @cfts to @ss. Files described by @cfts are created for all
2895 * existing cgroups to which @ss is attached and all future cgroups will
2896 * have them too. This function can be called anytime whether @ss is
2899 * Returns 0 on successful registration, -errno on failure. Note that this
2900 * function currently returns 0 as long as @cfts registration is successful
2901 * even if some file creation attempts on existing cgroups fail.
2903 int cgroup_add_cftypes(struct cgroup_subsys
*ss
, struct cftype
*cfts
)
2905 struct cftype_set
*set
;
2907 set
= kzalloc(sizeof(*set
), GFP_KERNEL
);
2911 cgroup_cfts_prepare();
2913 list_add_tail(&set
->node
, &ss
->cftsets
);
2914 cgroup_cfts_commit(ss
, cfts
, true);
2918 EXPORT_SYMBOL_GPL(cgroup_add_cftypes
);
2921 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
2922 * @ss: target cgroup subsystem
2923 * @cfts: zero-length name terminated array of cftypes
2925 * Unregister @cfts from @ss. Files described by @cfts are removed from
2926 * all existing cgroups to which @ss is attached and all future cgroups
2927 * won't have them either. This function can be called anytime whether @ss
2928 * is attached or not.
2930 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
2931 * registered with @ss.
2933 int cgroup_rm_cftypes(struct cgroup_subsys
*ss
, struct cftype
*cfts
)
2935 struct cftype_set
*set
;
2937 cgroup_cfts_prepare();
2939 list_for_each_entry(set
, &ss
->cftsets
, node
) {
2940 if (set
->cfts
== cfts
) {
2941 list_del(&set
->node
);
2943 cgroup_cfts_commit(ss
, cfts
, false);
2948 cgroup_cfts_commit(ss
, NULL
, false);
2953 * cgroup_task_count - count the number of tasks in a cgroup.
2954 * @cgrp: the cgroup in question
2956 * Return the number of tasks in the cgroup.
2958 int cgroup_task_count(const struct cgroup
*cgrp
)
2961 struct cgrp_cset_link
*link
;
2963 read_lock(&css_set_lock
);
2964 list_for_each_entry(link
, &cgrp
->cset_links
, cset_link
)
2965 count
+= atomic_read(&link
->cset
->refcount
);
2966 read_unlock(&css_set_lock
);
2971 * Advance a list_head iterator. The iterator should be positioned at
2972 * the start of a css_set
2974 static void cgroup_advance_iter(struct cgroup
*cgrp
, struct cgroup_iter
*it
)
2976 struct list_head
*l
= it
->cset_link
;
2977 struct cgrp_cset_link
*link
;
2978 struct css_set
*cset
;
2980 /* Advance to the next non-empty css_set */
2983 if (l
== &cgrp
->cset_links
) {
2984 it
->cset_link
= NULL
;
2987 link
= list_entry(l
, struct cgrp_cset_link
, cset_link
);
2989 } while (list_empty(&cset
->tasks
));
2991 it
->task
= cset
->tasks
.next
;
2995 * To reduce the fork() overhead for systems that are not actually
2996 * using their cgroups capability, we don't maintain the lists running
2997 * through each css_set to its tasks until we see the list actually
2998 * used - in other words after the first call to cgroup_iter_start().
3000 static void cgroup_enable_task_cg_lists(void)
3002 struct task_struct
*p
, *g
;
3003 write_lock(&css_set_lock
);
3004 use_task_css_set_links
= 1;
3006 * We need tasklist_lock because RCU is not safe against
3007 * while_each_thread(). Besides, a forking task that has passed
3008 * cgroup_post_fork() without seeing use_task_css_set_links = 1
3009 * is not guaranteed to have its child immediately visible in the
3010 * tasklist if we walk through it with RCU.
3012 read_lock(&tasklist_lock
);
3013 do_each_thread(g
, p
) {
3016 * We should check if the process is exiting, otherwise
3017 * it will race with cgroup_exit() in that the list
3018 * entry won't be deleted though the process has exited.
3020 if (!(p
->flags
& PF_EXITING
) && list_empty(&p
->cg_list
))
3021 list_add(&p
->cg_list
, &task_css_set(p
)->tasks
);
3023 } while_each_thread(g
, p
);
3024 read_unlock(&tasklist_lock
);
3025 write_unlock(&css_set_lock
);
3029 * cgroup_next_sibling - find the next sibling of a given cgroup
3030 * @pos: the current cgroup
3032 * This function returns the next sibling of @pos and should be called
3033 * under RCU read lock. The only requirement is that @pos is accessible.
3034 * The next sibling is guaranteed to be returned regardless of @pos's
3037 struct cgroup
*cgroup_next_sibling(struct cgroup
*pos
)
3039 struct cgroup
*next
;
3041 WARN_ON_ONCE(!rcu_read_lock_held());
3044 * @pos could already have been removed. Once a cgroup is removed,
3045 * its ->sibling.next is no longer updated when its next sibling
3046 * changes. As CGRP_DEAD assertion is serialized and happens
3047 * before the cgroup is taken off the ->sibling list, if we see it
3048 * unasserted, it's guaranteed that the next sibling hasn't
3049 * finished its grace period even if it's already removed, and thus
3050 * safe to dereference from this RCU critical section. If
3051 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
3052 * to be visible as %true here.
3054 if (likely(!cgroup_is_dead(pos
))) {
3055 next
= list_entry_rcu(pos
->sibling
.next
, struct cgroup
, sibling
);
3056 if (&next
->sibling
!= &pos
->parent
->children
)
3062 * Can't dereference the next pointer. Each cgroup is given a
3063 * monotonically increasing unique serial number and always
3064 * appended to the sibling list, so the next one can be found by
3065 * walking the parent's children until we see a cgroup with higher
3066 * serial number than @pos's.
3068 * While this path can be slow, it's taken only when either the
3069 * current cgroup is removed or iteration and removal race.
3071 list_for_each_entry_rcu(next
, &pos
->parent
->children
, sibling
)
3072 if (next
->serial_nr
> pos
->serial_nr
)
3076 EXPORT_SYMBOL_GPL(cgroup_next_sibling
);
3079 * cgroup_next_descendant_pre - find the next descendant for pre-order walk
3080 * @pos: the current position (%NULL to initiate traversal)
3081 * @cgroup: cgroup whose descendants to walk
3083 * To be used by cgroup_for_each_descendant_pre(). Find the next
3084 * descendant to visit for pre-order traversal of @cgroup's descendants.
3086 * While this function requires RCU read locking, it doesn't require the
3087 * whole traversal to be contained in a single RCU critical section. This
3088 * function will return the correct next descendant as long as both @pos
3089 * and @cgroup are accessible and @pos is a descendant of @cgroup.
3091 struct cgroup
*cgroup_next_descendant_pre(struct cgroup
*pos
,
3092 struct cgroup
*cgroup
)
3094 struct cgroup
*next
;
3096 WARN_ON_ONCE(!rcu_read_lock_held());
3098 /* if first iteration, pretend we just visited @cgroup */
3102 /* visit the first child if exists */
3103 next
= list_first_or_null_rcu(&pos
->children
, struct cgroup
, sibling
);
3107 /* no child, visit my or the closest ancestor's next sibling */
3108 while (pos
!= cgroup
) {
3109 next
= cgroup_next_sibling(pos
);
3117 EXPORT_SYMBOL_GPL(cgroup_next_descendant_pre
);
3120 * cgroup_rightmost_descendant - return the rightmost descendant of a cgroup
3121 * @pos: cgroup of interest
3123 * Return the rightmost descendant of @pos. If there's no descendant,
3124 * @pos is returned. This can be used during pre-order traversal to skip
3127 * While this function requires RCU read locking, it doesn't require the
3128 * whole traversal to be contained in a single RCU critical section. This
3129 * function will return the correct rightmost descendant as long as @pos is
3132 struct cgroup
*cgroup_rightmost_descendant(struct cgroup
*pos
)
3134 struct cgroup
*last
, *tmp
;
3136 WARN_ON_ONCE(!rcu_read_lock_held());
3140 /* ->prev isn't RCU safe, walk ->next till the end */
3142 list_for_each_entry_rcu(tmp
, &last
->children
, sibling
)
3148 EXPORT_SYMBOL_GPL(cgroup_rightmost_descendant
);
3150 static struct cgroup
*cgroup_leftmost_descendant(struct cgroup
*pos
)
3152 struct cgroup
*last
;
3156 pos
= list_first_or_null_rcu(&pos
->children
, struct cgroup
,
3164 * cgroup_next_descendant_post - find the next descendant for post-order walk
3165 * @pos: the current position (%NULL to initiate traversal)
3166 * @cgroup: cgroup whose descendants to walk
3168 * To be used by cgroup_for_each_descendant_post(). Find the next
3169 * descendant to visit for post-order traversal of @cgroup's descendants.
3171 * While this function requires RCU read locking, it doesn't require the
3172 * whole traversal to be contained in a single RCU critical section. This
3173 * function will return the correct next descendant as long as both @pos
3174 * and @cgroup are accessible and @pos is a descendant of @cgroup.
3176 struct cgroup
*cgroup_next_descendant_post(struct cgroup
*pos
,
3177 struct cgroup
*cgroup
)
3179 struct cgroup
*next
;
3181 WARN_ON_ONCE(!rcu_read_lock_held());
3183 /* if first iteration, visit the leftmost descendant */
3185 next
= cgroup_leftmost_descendant(cgroup
);
3186 return next
!= cgroup
? next
: NULL
;
3189 /* if there's an unvisited sibling, visit its leftmost descendant */
3190 next
= cgroup_next_sibling(pos
);
3192 return cgroup_leftmost_descendant(next
);
3194 /* no sibling left, visit parent */
3196 return next
!= cgroup
? next
: NULL
;
3198 EXPORT_SYMBOL_GPL(cgroup_next_descendant_post
);
3200 void cgroup_iter_start(struct cgroup
*cgrp
, struct cgroup_iter
*it
)
3201 __acquires(css_set_lock
)
3204 * The first time anyone tries to iterate across a cgroup,
3205 * we need to enable the list linking each css_set to its
3206 * tasks, and fix up all existing tasks.
3208 if (!use_task_css_set_links
)
3209 cgroup_enable_task_cg_lists();
3211 read_lock(&css_set_lock
);
3212 it
->cset_link
= &cgrp
->cset_links
;
3213 cgroup_advance_iter(cgrp
, it
);
3216 struct task_struct
*cgroup_iter_next(struct cgroup
*cgrp
,
3217 struct cgroup_iter
*it
)
3219 struct task_struct
*res
;
3220 struct list_head
*l
= it
->task
;
3221 struct cgrp_cset_link
*link
;
3223 /* If the iterator cg is NULL, we have no tasks */
3226 res
= list_entry(l
, struct task_struct
, cg_list
);
3227 /* Advance iterator to find next entry */
3229 link
= list_entry(it
->cset_link
, struct cgrp_cset_link
, cset_link
);
3230 if (l
== &link
->cset
->tasks
) {
3231 /* We reached the end of this task list - move on to
3232 * the next cg_cgroup_link */
3233 cgroup_advance_iter(cgrp
, it
);
3240 void cgroup_iter_end(struct cgroup
*cgrp
, struct cgroup_iter
*it
)
3241 __releases(css_set_lock
)
3243 read_unlock(&css_set_lock
);
3246 static inline int started_after_time(struct task_struct
*t1
,
3247 struct timespec
*time
,
3248 struct task_struct
*t2
)
3250 int start_diff
= timespec_compare(&t1
->start_time
, time
);
3251 if (start_diff
> 0) {
3253 } else if (start_diff
< 0) {
3257 * Arbitrarily, if two processes started at the same
3258 * time, we'll say that the lower pointer value
3259 * started first. Note that t2 may have exited by now
3260 * so this may not be a valid pointer any longer, but
3261 * that's fine - it still serves to distinguish
3262 * between two tasks started (effectively) simultaneously.
3269 * This function is a callback from heap_insert() and is used to order
3271 * In this case we order the heap in descending task start time.
3273 static inline int started_after(void *p1
, void *p2
)
3275 struct task_struct
*t1
= p1
;
3276 struct task_struct
*t2
= p2
;
3277 return started_after_time(t1
, &t2
->start_time
, t2
);
3281 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
3282 * @scan: struct cgroup_scanner containing arguments for the scan
3284 * Arguments include pointers to callback functions test_task() and
3286 * Iterate through all the tasks in a cgroup, calling test_task() for each,
3287 * and if it returns true, call process_task() for it also.
3288 * The test_task pointer may be NULL, meaning always true (select all tasks).
3289 * Effectively duplicates cgroup_iter_{start,next,end}()
3290 * but does not lock css_set_lock for the call to process_task().
3291 * The struct cgroup_scanner may be embedded in any structure of the caller's
3293 * It is guaranteed that process_task() will act on every task that
3294 * is a member of the cgroup for the duration of this call. This
3295 * function may or may not call process_task() for tasks that exit
3296 * or move to a different cgroup during the call, or are forked or
3297 * move into the cgroup during the call.
3299 * Note that test_task() may be called with locks held, and may in some
3300 * situations be called multiple times for the same task, so it should
3302 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
3303 * pre-allocated and will be used for heap operations (and its "gt" member will
3304 * be overwritten), else a temporary heap will be used (allocation of which
3305 * may cause this function to fail).
3307 int cgroup_scan_tasks(struct cgroup_scanner
*scan
)
3310 struct cgroup_iter it
;
3311 struct task_struct
*p
, *dropped
;
3312 /* Never dereference latest_task, since it's not refcounted */
3313 struct task_struct
*latest_task
= NULL
;
3314 struct ptr_heap tmp_heap
;
3315 struct ptr_heap
*heap
;
3316 struct timespec latest_time
= { 0, 0 };
3319 /* The caller supplied our heap and pre-allocated its memory */
3321 heap
->gt
= &started_after
;
3323 /* We need to allocate our own heap memory */
3325 retval
= heap_init(heap
, PAGE_SIZE
, GFP_KERNEL
, &started_after
);
3327 /* cannot allocate the heap */
3333 * Scan tasks in the cgroup, using the scanner's "test_task" callback
3334 * to determine which are of interest, and using the scanner's
3335 * "process_task" callback to process any of them that need an update.
3336 * Since we don't want to hold any locks during the task updates,
3337 * gather tasks to be processed in a heap structure.
3338 * The heap is sorted by descending task start time.
3339 * If the statically-sized heap fills up, we overflow tasks that
3340 * started later, and in future iterations only consider tasks that
3341 * started after the latest task in the previous pass. This
3342 * guarantees forward progress and that we don't miss any tasks.
3345 cgroup_iter_start(scan
->cg
, &it
);
3346 while ((p
= cgroup_iter_next(scan
->cg
, &it
))) {
3348 * Only affect tasks that qualify per the caller's callback,
3349 * if he provided one
3351 if (scan
->test_task
&& !scan
->test_task(p
, scan
))
3354 * Only process tasks that started after the last task
3357 if (!started_after_time(p
, &latest_time
, latest_task
))
3359 dropped
= heap_insert(heap
, p
);
3360 if (dropped
== NULL
) {
3362 * The new task was inserted; the heap wasn't
3366 } else if (dropped
!= p
) {
3368 * The new task was inserted, and pushed out a
3372 put_task_struct(dropped
);
3375 * Else the new task was newer than anything already in
3376 * the heap and wasn't inserted
3379 cgroup_iter_end(scan
->cg
, &it
);
3382 for (i
= 0; i
< heap
->size
; i
++) {
3383 struct task_struct
*q
= heap
->ptrs
[i
];
3385 latest_time
= q
->start_time
;
3388 /* Process the task per the caller's callback */
3389 scan
->process_task(q
, scan
);
3393 * If we had to process any tasks at all, scan again
3394 * in case some of them were in the middle of forking
3395 * children that didn't get processed.
3396 * Not the most efficient way to do it, but it avoids
3397 * having to take callback_mutex in the fork path
3401 if (heap
== &tmp_heap
)
3402 heap_free(&tmp_heap
);
3406 static void cgroup_transfer_one_task(struct task_struct
*task
,
3407 struct cgroup_scanner
*scan
)
3409 struct cgroup
*new_cgroup
= scan
->data
;
3411 mutex_lock(&cgroup_mutex
);
3412 cgroup_attach_task(new_cgroup
, task
, false);
3413 mutex_unlock(&cgroup_mutex
);
3417 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3418 * @to: cgroup to which the tasks will be moved
3419 * @from: cgroup in which the tasks currently reside
3421 int cgroup_transfer_tasks(struct cgroup
*to
, struct cgroup
*from
)
3423 struct cgroup_scanner scan
;
3426 scan
.test_task
= NULL
; /* select all tasks in cgroup */
3427 scan
.process_task
= cgroup_transfer_one_task
;
3431 return cgroup_scan_tasks(&scan
);
3435 * Stuff for reading the 'tasks'/'procs' files.
3437 * Reading this file can return large amounts of data if a cgroup has
3438 * *lots* of attached tasks. So it may need several calls to read(),
3439 * but we cannot guarantee that the information we produce is correct
3440 * unless we produce it entirely atomically.
3444 /* which pidlist file are we talking about? */
3445 enum cgroup_filetype
{
3451 * A pidlist is a list of pids that virtually represents the contents of one
3452 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3453 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3456 struct cgroup_pidlist
{
3458 * used to find which pidlist is wanted. doesn't change as long as
3459 * this particular list stays in the list.
3461 struct { enum cgroup_filetype type
; struct pid_namespace
*ns
; } key
;
3464 /* how many elements the above list has */
3466 /* how many files are using the current array */
3468 /* each of these stored in a list by its cgroup */
3469 struct list_head links
;
3470 /* pointer to the cgroup we belong to, for list removal purposes */
3471 struct cgroup
*owner
;
3472 /* protects the other fields */
3473 struct rw_semaphore mutex
;
3477 * The following two functions "fix" the issue where there are more pids
3478 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3479 * TODO: replace with a kernel-wide solution to this problem
3481 #define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3482 static void *pidlist_allocate(int count
)
3484 if (PIDLIST_TOO_LARGE(count
))
3485 return vmalloc(count
* sizeof(pid_t
));
3487 return kmalloc(count
* sizeof(pid_t
), GFP_KERNEL
);
3489 static void pidlist_free(void *p
)
3491 if (is_vmalloc_addr(p
))
3498 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
3499 * Returns the number of unique elements.
3501 static int pidlist_uniq(pid_t
*list
, int length
)
3506 * we presume the 0th element is unique, so i starts at 1. trivial
3507 * edge cases first; no work needs to be done for either
3509 if (length
== 0 || length
== 1)
3511 /* src and dest walk down the list; dest counts unique elements */
3512 for (src
= 1; src
< length
; src
++) {
3513 /* find next unique element */
3514 while (list
[src
] == list
[src
-1]) {
3519 /* dest always points to where the next unique element goes */
3520 list
[dest
] = list
[src
];
3527 static int cmppid(const void *a
, const void *b
)
3529 return *(pid_t
*)a
- *(pid_t
*)b
;
3533 * find the appropriate pidlist for our purpose (given procs vs tasks)
3534 * returns with the lock on that pidlist already held, and takes care
3535 * of the use count, or returns NULL with no locks held if we're out of
3538 static struct cgroup_pidlist
*cgroup_pidlist_find(struct cgroup
*cgrp
,
3539 enum cgroup_filetype type
)
3541 struct cgroup_pidlist
*l
;
3542 /* don't need task_nsproxy() if we're looking at ourself */
3543 struct pid_namespace
*ns
= task_active_pid_ns(current
);
3546 * We can't drop the pidlist_mutex before taking the l->mutex in case
3547 * the last ref-holder is trying to remove l from the list at the same
3548 * time. Holding the pidlist_mutex precludes somebody taking whichever
3549 * list we find out from under us - compare release_pid_array().
3551 mutex_lock(&cgrp
->pidlist_mutex
);
3552 list_for_each_entry(l
, &cgrp
->pidlists
, links
) {
3553 if (l
->key
.type
== type
&& l
->key
.ns
== ns
) {
3554 /* make sure l doesn't vanish out from under us */
3555 down_write(&l
->mutex
);
3556 mutex_unlock(&cgrp
->pidlist_mutex
);
3560 /* entry not found; create a new one */
3561 l
= kzalloc(sizeof(struct cgroup_pidlist
), GFP_KERNEL
);
3563 mutex_unlock(&cgrp
->pidlist_mutex
);
3566 init_rwsem(&l
->mutex
);
3567 down_write(&l
->mutex
);
3569 l
->key
.ns
= get_pid_ns(ns
);
3571 list_add(&l
->links
, &cgrp
->pidlists
);
3572 mutex_unlock(&cgrp
->pidlist_mutex
);
3577 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3579 static int pidlist_array_load(struct cgroup
*cgrp
, enum cgroup_filetype type
,
3580 struct cgroup_pidlist
**lp
)
3584 int pid
, n
= 0; /* used for populating the array */
3585 struct cgroup_iter it
;
3586 struct task_struct
*tsk
;
3587 struct cgroup_pidlist
*l
;
3590 * If cgroup gets more users after we read count, we won't have
3591 * enough space - tough. This race is indistinguishable to the
3592 * caller from the case that the additional cgroup users didn't
3593 * show up until sometime later on.
3595 length
= cgroup_task_count(cgrp
);
3596 array
= pidlist_allocate(length
);
3599 /* now, populate the array */
3600 cgroup_iter_start(cgrp
, &it
);
3601 while ((tsk
= cgroup_iter_next(cgrp
, &it
))) {
3602 if (unlikely(n
== length
))
3604 /* get tgid or pid for procs or tasks file respectively */
3605 if (type
== CGROUP_FILE_PROCS
)
3606 pid
= task_tgid_vnr(tsk
);
3608 pid
= task_pid_vnr(tsk
);
3609 if (pid
> 0) /* make sure to only use valid results */
3612 cgroup_iter_end(cgrp
, &it
);
3614 /* now sort & (if procs) strip out duplicates */
3615 sort(array
, length
, sizeof(pid_t
), cmppid
, NULL
);
3616 if (type
== CGROUP_FILE_PROCS
)
3617 length
= pidlist_uniq(array
, length
);
3618 l
= cgroup_pidlist_find(cgrp
, type
);
3620 pidlist_free(array
);
3623 /* store array, freeing old if necessary - lock already held */
3624 pidlist_free(l
->list
);
3628 up_write(&l
->mutex
);
3634 * cgroupstats_build - build and fill cgroupstats
3635 * @stats: cgroupstats to fill information into
3636 * @dentry: A dentry entry belonging to the cgroup for which stats have
3639 * Build and fill cgroupstats so that taskstats can export it to user
3642 int cgroupstats_build(struct cgroupstats
*stats
, struct dentry
*dentry
)
3645 struct cgroup
*cgrp
;
3646 struct cgroup_iter it
;
3647 struct task_struct
*tsk
;
3650 * Validate dentry by checking the superblock operations,
3651 * and make sure it's a directory.
3653 if (dentry
->d_sb
->s_op
!= &cgroup_ops
||
3654 !S_ISDIR(dentry
->d_inode
->i_mode
))
3658 cgrp
= dentry
->d_fsdata
;
3660 cgroup_iter_start(cgrp
, &it
);
3661 while ((tsk
= cgroup_iter_next(cgrp
, &it
))) {
3662 switch (tsk
->state
) {
3664 stats
->nr_running
++;
3666 case TASK_INTERRUPTIBLE
:
3667 stats
->nr_sleeping
++;
3669 case TASK_UNINTERRUPTIBLE
:
3670 stats
->nr_uninterruptible
++;
3673 stats
->nr_stopped
++;
3676 if (delayacct_is_task_waiting_on_io(tsk
))
3677 stats
->nr_io_wait
++;
3681 cgroup_iter_end(cgrp
, &it
);
3689 * seq_file methods for the tasks/procs files. The seq_file position is the
3690 * next pid to display; the seq_file iterator is a pointer to the pid
3691 * in the cgroup->l->list array.
3694 static void *cgroup_pidlist_start(struct seq_file
*s
, loff_t
*pos
)
3697 * Initially we receive a position value that corresponds to
3698 * one more than the last pid shown (or 0 on the first call or
3699 * after a seek to the start). Use a binary-search to find the
3700 * next pid to display, if any
3702 struct cgroup_pidlist
*l
= s
->private;
3703 int index
= 0, pid
= *pos
;
3706 down_read(&l
->mutex
);
3708 int end
= l
->length
;
3710 while (index
< end
) {
3711 int mid
= (index
+ end
) / 2;
3712 if (l
->list
[mid
] == pid
) {
3715 } else if (l
->list
[mid
] <= pid
)
3721 /* If we're off the end of the array, we're done */
3722 if (index
>= l
->length
)
3724 /* Update the abstract position to be the actual pid that we found */
3725 iter
= l
->list
+ index
;
3730 static void cgroup_pidlist_stop(struct seq_file
*s
, void *v
)
3732 struct cgroup_pidlist
*l
= s
->private;
3736 static void *cgroup_pidlist_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
3738 struct cgroup_pidlist
*l
= s
->private;
3740 pid_t
*end
= l
->list
+ l
->length
;
3742 * Advance to the next pid in the array. If this goes off the
3754 static int cgroup_pidlist_show(struct seq_file
*s
, void *v
)
3756 return seq_printf(s
, "%d\n", *(int *)v
);
3760 * seq_operations functions for iterating on pidlists through seq_file -
3761 * independent of whether it's tasks or procs
3763 static const struct seq_operations cgroup_pidlist_seq_operations
= {
3764 .start
= cgroup_pidlist_start
,
3765 .stop
= cgroup_pidlist_stop
,
3766 .next
= cgroup_pidlist_next
,
3767 .show
= cgroup_pidlist_show
,
3770 static void cgroup_release_pid_array(struct cgroup_pidlist
*l
)
3773 * the case where we're the last user of this particular pidlist will
3774 * have us remove it from the cgroup's list, which entails taking the
3775 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3776 * pidlist_mutex, we have to take pidlist_mutex first.
3778 mutex_lock(&l
->owner
->pidlist_mutex
);
3779 down_write(&l
->mutex
);
3780 BUG_ON(!l
->use_count
);
3781 if (!--l
->use_count
) {
3782 /* we're the last user if refcount is 0; remove and free */
3783 list_del(&l
->links
);
3784 mutex_unlock(&l
->owner
->pidlist_mutex
);
3785 pidlist_free(l
->list
);
3786 put_pid_ns(l
->key
.ns
);
3787 up_write(&l
->mutex
);
3791 mutex_unlock(&l
->owner
->pidlist_mutex
);
3792 up_write(&l
->mutex
);
3795 static int cgroup_pidlist_release(struct inode
*inode
, struct file
*file
)
3797 struct cgroup_pidlist
*l
;
3798 if (!(file
->f_mode
& FMODE_READ
))
3801 * the seq_file will only be initialized if the file was opened for
3802 * reading; hence we check if it's not null only in that case.
3804 l
= ((struct seq_file
*)file
->private_data
)->private;
3805 cgroup_release_pid_array(l
);
3806 return seq_release(inode
, file
);
3809 static const struct file_operations cgroup_pidlist_operations
= {
3811 .llseek
= seq_lseek
,
3812 .write
= cgroup_file_write
,
3813 .release
= cgroup_pidlist_release
,
3817 * The following functions handle opens on a file that displays a pidlist
3818 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3821 /* helper function for the two below it */
3822 static int cgroup_pidlist_open(struct file
*file
, enum cgroup_filetype type
)
3824 struct cgroup
*cgrp
= __d_cgrp(file
->f_dentry
->d_parent
);
3825 struct cgroup_pidlist
*l
;
3828 /* Nothing to do for write-only files */
3829 if (!(file
->f_mode
& FMODE_READ
))
3832 /* have the array populated */
3833 retval
= pidlist_array_load(cgrp
, type
, &l
);
3836 /* configure file information */
3837 file
->f_op
= &cgroup_pidlist_operations
;
3839 retval
= seq_open(file
, &cgroup_pidlist_seq_operations
);
3841 cgroup_release_pid_array(l
);
3844 ((struct seq_file
*)file
->private_data
)->private = l
;
3847 static int cgroup_tasks_open(struct inode
*unused
, struct file
*file
)
3849 return cgroup_pidlist_open(file
, CGROUP_FILE_TASKS
);
3851 static int cgroup_procs_open(struct inode
*unused
, struct file
*file
)
3853 return cgroup_pidlist_open(file
, CGROUP_FILE_PROCS
);
3856 static u64
cgroup_read_notify_on_release(struct cgroup
*cgrp
,
3859 return notify_on_release(cgrp
);
3862 static int cgroup_write_notify_on_release(struct cgroup
*cgrp
,
3866 clear_bit(CGRP_RELEASABLE
, &cgrp
->flags
);
3868 set_bit(CGRP_NOTIFY_ON_RELEASE
, &cgrp
->flags
);
3870 clear_bit(CGRP_NOTIFY_ON_RELEASE
, &cgrp
->flags
);
3875 * When dput() is called asynchronously, if umount has been done and
3876 * then deactivate_super() in cgroup_free_fn() kills the superblock,
3877 * there's a small window that vfs will see the root dentry with non-zero
3878 * refcnt and trigger BUG().
3880 * That's why we hold a reference before dput() and drop it right after.
3882 static void cgroup_dput(struct cgroup
*cgrp
)
3884 struct super_block
*sb
= cgrp
->root
->sb
;
3886 atomic_inc(&sb
->s_active
);
3888 deactivate_super(sb
);
3892 * Unregister event and free resources.
3894 * Gets called from workqueue.
3896 static void cgroup_event_remove(struct work_struct
*work
)
3898 struct cgroup_event
*event
= container_of(work
, struct cgroup_event
,
3900 struct cgroup
*cgrp
= event
->cgrp
;
3902 remove_wait_queue(event
->wqh
, &event
->wait
);
3904 event
->cft
->unregister_event(cgrp
, event
->cft
, event
->eventfd
);
3906 /* Notify userspace the event is going away. */
3907 eventfd_signal(event
->eventfd
, 1);
3909 eventfd_ctx_put(event
->eventfd
);
3915 * Gets called on POLLHUP on eventfd when user closes it.
3917 * Called with wqh->lock held and interrupts disabled.
3919 static int cgroup_event_wake(wait_queue_t
*wait
, unsigned mode
,
3920 int sync
, void *key
)
3922 struct cgroup_event
*event
= container_of(wait
,
3923 struct cgroup_event
, wait
);
3924 struct cgroup
*cgrp
= event
->cgrp
;
3925 unsigned long flags
= (unsigned long)key
;
3927 if (flags
& POLLHUP
) {
3929 * If the event has been detached at cgroup removal, we
3930 * can simply return knowing the other side will cleanup
3933 * We can't race against event freeing since the other
3934 * side will require wqh->lock via remove_wait_queue(),
3937 spin_lock(&cgrp
->event_list_lock
);
3938 if (!list_empty(&event
->list
)) {
3939 list_del_init(&event
->list
);
3941 * We are in atomic context, but cgroup_event_remove()
3942 * may sleep, so we have to call it in workqueue.
3944 schedule_work(&event
->remove
);
3946 spin_unlock(&cgrp
->event_list_lock
);
3952 static void cgroup_event_ptable_queue_proc(struct file
*file
,
3953 wait_queue_head_t
*wqh
, poll_table
*pt
)
3955 struct cgroup_event
*event
= container_of(pt
,
3956 struct cgroup_event
, pt
);
3959 add_wait_queue(wqh
, &event
->wait
);
3963 * Parse input and register new cgroup event handler.
3965 * Input must be in format '<event_fd> <control_fd> <args>'.
3966 * Interpretation of args is defined by control file implementation.
3968 static int cgroup_write_event_control(struct cgroup
*cgrp
, struct cftype
*cft
,
3971 struct cgroup_event
*event
= NULL
;
3972 struct cgroup
*cgrp_cfile
;
3973 unsigned int efd
, cfd
;
3974 struct file
*efile
= NULL
;
3975 struct file
*cfile
= NULL
;
3979 efd
= simple_strtoul(buffer
, &endp
, 10);
3984 cfd
= simple_strtoul(buffer
, &endp
, 10);
3985 if ((*endp
!= ' ') && (*endp
!= '\0'))
3989 event
= kzalloc(sizeof(*event
), GFP_KERNEL
);
3993 INIT_LIST_HEAD(&event
->list
);
3994 init_poll_funcptr(&event
->pt
, cgroup_event_ptable_queue_proc
);
3995 init_waitqueue_func_entry(&event
->wait
, cgroup_event_wake
);
3996 INIT_WORK(&event
->remove
, cgroup_event_remove
);
3998 efile
= eventfd_fget(efd
);
3999 if (IS_ERR(efile
)) {
4000 ret
= PTR_ERR(efile
);
4004 event
->eventfd
= eventfd_ctx_fileget(efile
);
4005 if (IS_ERR(event
->eventfd
)) {
4006 ret
= PTR_ERR(event
->eventfd
);
4016 /* the process need read permission on control file */
4017 /* AV: shouldn't we check that it's been opened for read instead? */
4018 ret
= inode_permission(file_inode(cfile
), MAY_READ
);
4022 event
->cft
= __file_cft(cfile
);
4023 if (IS_ERR(event
->cft
)) {
4024 ret
= PTR_ERR(event
->cft
);
4029 * The file to be monitored must be in the same cgroup as
4030 * cgroup.event_control is.
4032 cgrp_cfile
= __d_cgrp(cfile
->f_dentry
->d_parent
);
4033 if (cgrp_cfile
!= cgrp
) {
4038 if (!event
->cft
->register_event
|| !event
->cft
->unregister_event
) {
4043 ret
= event
->cft
->register_event(cgrp
, event
->cft
,
4044 event
->eventfd
, buffer
);
4048 efile
->f_op
->poll(efile
, &event
->pt
);
4051 * Events should be removed after rmdir of cgroup directory, but before
4052 * destroying subsystem state objects. Let's take reference to cgroup
4053 * directory dentry to do that.
4057 spin_lock(&cgrp
->event_list_lock
);
4058 list_add(&event
->list
, &cgrp
->event_list
);
4059 spin_unlock(&cgrp
->event_list_lock
);
4070 if (event
&& event
->eventfd
&& !IS_ERR(event
->eventfd
))
4071 eventfd_ctx_put(event
->eventfd
);
4073 if (!IS_ERR_OR_NULL(efile
))
4081 static u64
cgroup_clone_children_read(struct cgroup
*cgrp
,
4084 return test_bit(CGRP_CPUSET_CLONE_CHILDREN
, &cgrp
->flags
);
4087 static int cgroup_clone_children_write(struct cgroup
*cgrp
,
4092 set_bit(CGRP_CPUSET_CLONE_CHILDREN
, &cgrp
->flags
);
4094 clear_bit(CGRP_CPUSET_CLONE_CHILDREN
, &cgrp
->flags
);
4098 static struct cftype cgroup_base_files
[] = {
4100 .name
= "cgroup.procs",
4101 .open
= cgroup_procs_open
,
4102 .write_u64
= cgroup_procs_write
,
4103 .release
= cgroup_pidlist_release
,
4104 .mode
= S_IRUGO
| S_IWUSR
,
4107 .name
= "cgroup.event_control",
4108 .write_string
= cgroup_write_event_control
,
4112 .name
= "cgroup.clone_children",
4113 .flags
= CFTYPE_INSANE
,
4114 .read_u64
= cgroup_clone_children_read
,
4115 .write_u64
= cgroup_clone_children_write
,
4118 .name
= "cgroup.sane_behavior",
4119 .flags
= CFTYPE_ONLY_ON_ROOT
,
4120 .read_seq_string
= cgroup_sane_behavior_show
,
4124 * Historical crazy stuff. These don't have "cgroup." prefix and
4125 * don't exist if sane_behavior. If you're depending on these, be
4126 * prepared to be burned.
4130 .flags
= CFTYPE_INSANE
, /* use "procs" instead */
4131 .open
= cgroup_tasks_open
,
4132 .write_u64
= cgroup_tasks_write
,
4133 .release
= cgroup_pidlist_release
,
4134 .mode
= S_IRUGO
| S_IWUSR
,
4137 .name
= "notify_on_release",
4138 .flags
= CFTYPE_INSANE
,
4139 .read_u64
= cgroup_read_notify_on_release
,
4140 .write_u64
= cgroup_write_notify_on_release
,
4143 .name
= "release_agent",
4144 .flags
= CFTYPE_INSANE
| CFTYPE_ONLY_ON_ROOT
,
4145 .read_seq_string
= cgroup_release_agent_show
,
4146 .write_string
= cgroup_release_agent_write
,
4147 .max_write_len
= PATH_MAX
,
4153 * cgroup_populate_dir - selectively creation of files in a directory
4154 * @cgrp: target cgroup
4155 * @base_files: true if the base files should be added
4156 * @subsys_mask: mask of the subsystem ids whose files should be added
4158 static int cgroup_populate_dir(struct cgroup
*cgrp
, bool base_files
,
4159 unsigned long subsys_mask
)
4162 struct cgroup_subsys
*ss
;
4165 err
= cgroup_addrm_files(cgrp
, NULL
, cgroup_base_files
, true);
4170 /* process cftsets of each subsystem */
4171 for_each_root_subsys(cgrp
->root
, ss
) {
4172 struct cftype_set
*set
;
4173 if (!test_bit(ss
->subsys_id
, &subsys_mask
))
4176 list_for_each_entry(set
, &ss
->cftsets
, node
)
4177 cgroup_addrm_files(cgrp
, ss
, set
->cfts
, true);
4180 /* This cgroup is ready now */
4181 for_each_root_subsys(cgrp
->root
, ss
) {
4182 struct cgroup_subsys_state
*css
= cgrp
->subsys
[ss
->subsys_id
];
4183 struct css_id
*id
= rcu_dereference_protected(css
->id
, true);
4186 * Update id->css pointer and make this css visible from
4187 * CSS ID functions. This pointer will be dereferened
4188 * from RCU-read-side without locks.
4191 rcu_assign_pointer(id
->css
, css
);
4197 static void css_dput_fn(struct work_struct
*work
)
4199 struct cgroup_subsys_state
*css
=
4200 container_of(work
, struct cgroup_subsys_state
, dput_work
);
4202 cgroup_dput(css
->cgroup
);
4205 static void css_release(struct percpu_ref
*ref
)
4207 struct cgroup_subsys_state
*css
=
4208 container_of(ref
, struct cgroup_subsys_state
, refcnt
);
4210 schedule_work(&css
->dput_work
);
4213 static void init_cgroup_css(struct cgroup_subsys_state
*css
,
4214 struct cgroup_subsys
*ss
,
4215 struct cgroup
*cgrp
)
4220 if (cgrp
== cgroup_dummy_top
)
4221 css
->flags
|= CSS_ROOT
;
4222 BUG_ON(cgrp
->subsys
[ss
->subsys_id
]);
4223 cgrp
->subsys
[ss
->subsys_id
] = css
;
4226 * css holds an extra ref to @cgrp->dentry which is put on the last
4227 * css_put(). dput() requires process context, which css_put() may
4228 * be called without. @css->dput_work will be used to invoke
4229 * dput() asynchronously from css_put().
4231 INIT_WORK(&css
->dput_work
, css_dput_fn
);
4234 /* invoke ->post_create() on a new CSS and mark it online if successful */
4235 static int online_css(struct cgroup_subsys
*ss
, struct cgroup
*cgrp
)
4239 lockdep_assert_held(&cgroup_mutex
);
4242 ret
= ss
->css_online(cgrp
);
4244 cgrp
->subsys
[ss
->subsys_id
]->flags
|= CSS_ONLINE
;
4248 /* if the CSS is online, invoke ->pre_destory() on it and mark it offline */
4249 static void offline_css(struct cgroup_subsys
*ss
, struct cgroup
*cgrp
)
4250 __releases(&cgroup_mutex
) __acquires(&cgroup_mutex
)
4252 struct cgroup_subsys_state
*css
= cgrp
->subsys
[ss
->subsys_id
];
4254 lockdep_assert_held(&cgroup_mutex
);
4256 if (!(css
->flags
& CSS_ONLINE
))
4259 if (ss
->css_offline
)
4260 ss
->css_offline(cgrp
);
4262 cgrp
->subsys
[ss
->subsys_id
]->flags
&= ~CSS_ONLINE
;
4266 * cgroup_create - create a cgroup
4267 * @parent: cgroup that will be parent of the new cgroup
4268 * @dentry: dentry of the new cgroup
4269 * @mode: mode to set on new inode
4271 * Must be called with the mutex on the parent inode held
4273 static long cgroup_create(struct cgroup
*parent
, struct dentry
*dentry
,
4276 struct cgroup
*cgrp
;
4277 struct cgroup_name
*name
;
4278 struct cgroupfs_root
*root
= parent
->root
;
4280 struct cgroup_subsys
*ss
;
4281 struct super_block
*sb
= root
->sb
;
4283 /* allocate the cgroup and its ID, 0 is reserved for the root */
4284 cgrp
= kzalloc(sizeof(*cgrp
), GFP_KERNEL
);
4288 name
= cgroup_alloc_name(dentry
);
4291 rcu_assign_pointer(cgrp
->name
, name
);
4293 cgrp
->id
= ida_simple_get(&root
->cgroup_ida
, 1, 0, GFP_KERNEL
);
4298 * Only live parents can have children. Note that the liveliness
4299 * check isn't strictly necessary because cgroup_mkdir() and
4300 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4301 * anyway so that locking is contained inside cgroup proper and we
4302 * don't get nasty surprises if we ever grow another caller.
4304 if (!cgroup_lock_live_group(parent
)) {
4309 /* Grab a reference on the superblock so the hierarchy doesn't
4310 * get deleted on unmount if there are child cgroups. This
4311 * can be done outside cgroup_mutex, since the sb can't
4312 * disappear while someone has an open control file on the
4314 atomic_inc(&sb
->s_active
);
4316 init_cgroup_housekeeping(cgrp
);
4318 dentry
->d_fsdata
= cgrp
;
4319 cgrp
->dentry
= dentry
;
4321 cgrp
->parent
= parent
;
4322 cgrp
->root
= parent
->root
;
4324 if (notify_on_release(parent
))
4325 set_bit(CGRP_NOTIFY_ON_RELEASE
, &cgrp
->flags
);
4327 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN
, &parent
->flags
))
4328 set_bit(CGRP_CPUSET_CLONE_CHILDREN
, &cgrp
->flags
);
4330 for_each_root_subsys(root
, ss
) {
4331 struct cgroup_subsys_state
*css
;
4333 css
= ss
->css_alloc(cgrp
);
4339 err
= percpu_ref_init(&css
->refcnt
, css_release
);
4343 init_cgroup_css(css
, ss
, cgrp
);
4346 err
= alloc_css_id(ss
, parent
, cgrp
);
4353 * Create directory. cgroup_create_file() returns with the new
4354 * directory locked on success so that it can be populated without
4355 * dropping cgroup_mutex.
4357 err
= cgroup_create_file(dentry
, S_IFDIR
| mode
, sb
);
4360 lockdep_assert_held(&dentry
->d_inode
->i_mutex
);
4362 cgrp
->serial_nr
= cgroup_serial_nr_next
++;
4364 /* allocation complete, commit to creation */
4365 list_add_tail_rcu(&cgrp
->sibling
, &cgrp
->parent
->children
);
4366 root
->number_of_cgroups
++;
4368 /* each css holds a ref to the cgroup's dentry */
4369 for_each_root_subsys(root
, ss
)
4372 /* hold a ref to the parent's dentry */
4373 dget(parent
->dentry
);
4375 /* creation succeeded, notify subsystems */
4376 for_each_root_subsys(root
, ss
) {
4377 err
= online_css(ss
, cgrp
);
4381 if (ss
->broken_hierarchy
&& !ss
->warned_broken_hierarchy
&&
4383 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",
4384 current
->comm
, current
->pid
, ss
->name
);
4385 if (!strcmp(ss
->name
, "memory"))
4386 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4387 ss
->warned_broken_hierarchy
= true;
4391 err
= cgroup_populate_dir(cgrp
, true, root
->subsys_mask
);
4395 mutex_unlock(&cgroup_mutex
);
4396 mutex_unlock(&cgrp
->dentry
->d_inode
->i_mutex
);
4401 for_each_root_subsys(root
, ss
) {
4402 struct cgroup_subsys_state
*css
= cgrp
->subsys
[ss
->subsys_id
];
4405 percpu_ref_cancel_init(&css
->refcnt
);
4409 mutex_unlock(&cgroup_mutex
);
4410 /* Release the reference count that we took on the superblock */
4411 deactivate_super(sb
);
4413 ida_simple_remove(&root
->cgroup_ida
, cgrp
->id
);
4415 kfree(rcu_dereference_raw(cgrp
->name
));
4421 cgroup_destroy_locked(cgrp
);
4422 mutex_unlock(&cgroup_mutex
);
4423 mutex_unlock(&dentry
->d_inode
->i_mutex
);
4427 static int cgroup_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
4429 struct cgroup
*c_parent
= dentry
->d_parent
->d_fsdata
;
4431 /* the vfs holds inode->i_mutex already */
4432 return cgroup_create(c_parent
, dentry
, mode
| S_IFDIR
);
4435 static void cgroup_css_killed(struct cgroup
*cgrp
)
4437 if (!atomic_dec_and_test(&cgrp
->css_kill_cnt
))
4440 /* percpu ref's of all css's are killed, kick off the next step */
4441 INIT_WORK(&cgrp
->destroy_work
, cgroup_offline_fn
);
4442 schedule_work(&cgrp
->destroy_work
);
4445 static void css_ref_killed_fn(struct percpu_ref
*ref
)
4447 struct cgroup_subsys_state
*css
=
4448 container_of(ref
, struct cgroup_subsys_state
, refcnt
);
4450 cgroup_css_killed(css
->cgroup
);
4454 * cgroup_destroy_locked - the first stage of cgroup destruction
4455 * @cgrp: cgroup to be destroyed
4457 * css's make use of percpu refcnts whose killing latency shouldn't be
4458 * exposed to userland and are RCU protected. Also, cgroup core needs to
4459 * guarantee that css_tryget() won't succeed by the time ->css_offline() is
4460 * invoked. To satisfy all the requirements, destruction is implemented in
4461 * the following two steps.
4463 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4464 * userland visible parts and start killing the percpu refcnts of
4465 * css's. Set up so that the next stage will be kicked off once all
4466 * the percpu refcnts are confirmed to be killed.
4468 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4469 * rest of destruction. Once all cgroup references are gone, the
4470 * cgroup is RCU-freed.
4472 * This function implements s1. After this step, @cgrp is gone as far as
4473 * the userland is concerned and a new cgroup with the same name may be
4474 * created. As cgroup doesn't care about the names internally, this
4475 * doesn't cause any problem.
4477 static int cgroup_destroy_locked(struct cgroup
*cgrp
)
4478 __releases(&cgroup_mutex
) __acquires(&cgroup_mutex
)
4480 struct dentry
*d
= cgrp
->dentry
;
4481 struct cgroup_event
*event
, *tmp
;
4482 struct cgroup_subsys
*ss
;
4485 lockdep_assert_held(&d
->d_inode
->i_mutex
);
4486 lockdep_assert_held(&cgroup_mutex
);
4489 * css_set_lock synchronizes access to ->cset_links and prevents
4490 * @cgrp from being removed while __put_css_set() is in progress.
4492 read_lock(&css_set_lock
);
4493 empty
= list_empty(&cgrp
->cset_links
) && list_empty(&cgrp
->children
);
4494 read_unlock(&css_set_lock
);
4499 * Block new css_tryget() by killing css refcnts. cgroup core
4500 * guarantees that, by the time ->css_offline() is invoked, no new
4501 * css reference will be given out via css_tryget(). We can't
4502 * simply call percpu_ref_kill() and proceed to offlining css's
4503 * because percpu_ref_kill() doesn't guarantee that the ref is seen
4504 * as killed on all CPUs on return.
4506 * Use percpu_ref_kill_and_confirm() to get notifications as each
4507 * css is confirmed to be seen as killed on all CPUs. The
4508 * notification callback keeps track of the number of css's to be
4509 * killed and schedules cgroup_offline_fn() to perform the rest of
4510 * destruction once the percpu refs of all css's are confirmed to
4513 atomic_set(&cgrp
->css_kill_cnt
, 1);
4514 for_each_root_subsys(cgrp
->root
, ss
) {
4515 struct cgroup_subsys_state
*css
= cgrp
->subsys
[ss
->subsys_id
];
4518 * Killing would put the base ref, but we need to keep it
4519 * alive until after ->css_offline.
4521 percpu_ref_get(&css
->refcnt
);
4523 atomic_inc(&cgrp
->css_kill_cnt
);
4524 percpu_ref_kill_and_confirm(&css
->refcnt
, css_ref_killed_fn
);
4526 cgroup_css_killed(cgrp
);
4529 * Mark @cgrp dead. This prevents further task migration and child
4530 * creation by disabling cgroup_lock_live_group(). Note that
4531 * CGRP_DEAD assertion is depended upon by cgroup_next_sibling() to
4532 * resume iteration after dropping RCU read lock. See
4533 * cgroup_next_sibling() for details.
4535 set_bit(CGRP_DEAD
, &cgrp
->flags
);
4537 /* CGRP_DEAD is set, remove from ->release_list for the last time */
4538 raw_spin_lock(&release_list_lock
);
4539 if (!list_empty(&cgrp
->release_list
))
4540 list_del_init(&cgrp
->release_list
);
4541 raw_spin_unlock(&release_list_lock
);
4544 * Remove @cgrp directory. The removal puts the base ref but we
4545 * aren't quite done with @cgrp yet, so hold onto it.
4548 cgroup_d_remove_dir(d
);
4551 * Unregister events and notify userspace.
4552 * Notify userspace about cgroup removing only after rmdir of cgroup
4553 * directory to avoid race between userspace and kernelspace.
4555 spin_lock(&cgrp
->event_list_lock
);
4556 list_for_each_entry_safe(event
, tmp
, &cgrp
->event_list
, list
) {
4557 list_del_init(&event
->list
);
4558 schedule_work(&event
->remove
);
4560 spin_unlock(&cgrp
->event_list_lock
);
4566 * cgroup_offline_fn - the second step of cgroup destruction
4567 * @work: cgroup->destroy_free_work
4569 * This function is invoked from a work item for a cgroup which is being
4570 * destroyed after the percpu refcnts of all css's are guaranteed to be
4571 * seen as killed on all CPUs, and performs the rest of destruction. This
4572 * is the second step of destruction described in the comment above
4573 * cgroup_destroy_locked().
4575 static void cgroup_offline_fn(struct work_struct
*work
)
4577 struct cgroup
*cgrp
= container_of(work
, struct cgroup
, destroy_work
);
4578 struct cgroup
*parent
= cgrp
->parent
;
4579 struct dentry
*d
= cgrp
->dentry
;
4580 struct cgroup_subsys
*ss
;
4582 mutex_lock(&cgroup_mutex
);
4585 * css_tryget() is guaranteed to fail now. Tell subsystems to
4586 * initate destruction.
4588 for_each_root_subsys(cgrp
->root
, ss
)
4589 offline_css(ss
, cgrp
);
4592 * Put the css refs from cgroup_destroy_locked(). Each css holds
4593 * an extra reference to the cgroup's dentry and cgroup removal
4594 * proceeds regardless of css refs. On the last put of each css,
4595 * whenever that may be, the extra dentry ref is put so that dentry
4596 * destruction happens only after all css's are released.
4598 for_each_root_subsys(cgrp
->root
, ss
)
4599 css_put(cgrp
->subsys
[ss
->subsys_id
]);
4601 /* delete this cgroup from parent->children */
4602 list_del_rcu(&cgrp
->sibling
);
4606 set_bit(CGRP_RELEASABLE
, &parent
->flags
);
4607 check_for_release(parent
);
4609 mutex_unlock(&cgroup_mutex
);
4612 static int cgroup_rmdir(struct inode
*unused_dir
, struct dentry
*dentry
)
4616 mutex_lock(&cgroup_mutex
);
4617 ret
= cgroup_destroy_locked(dentry
->d_fsdata
);
4618 mutex_unlock(&cgroup_mutex
);
4623 static void __init_or_module
cgroup_init_cftsets(struct cgroup_subsys
*ss
)
4625 INIT_LIST_HEAD(&ss
->cftsets
);
4628 * base_cftset is embedded in subsys itself, no need to worry about
4631 if (ss
->base_cftypes
) {
4632 ss
->base_cftset
.cfts
= ss
->base_cftypes
;
4633 list_add_tail(&ss
->base_cftset
.node
, &ss
->cftsets
);
4637 static void __init
cgroup_init_subsys(struct cgroup_subsys
*ss
)
4639 struct cgroup_subsys_state
*css
;
4641 printk(KERN_INFO
"Initializing cgroup subsys %s\n", ss
->name
);
4643 mutex_lock(&cgroup_mutex
);
4645 /* init base cftset */
4646 cgroup_init_cftsets(ss
);
4648 /* Create the top cgroup state for this subsystem */
4649 list_add(&ss
->sibling
, &cgroup_dummy_root
.subsys_list
);
4650 ss
->root
= &cgroup_dummy_root
;
4651 css
= ss
->css_alloc(cgroup_dummy_top
);
4652 /* We don't handle early failures gracefully */
4653 BUG_ON(IS_ERR(css
));
4654 init_cgroup_css(css
, ss
, cgroup_dummy_top
);
4656 /* Update the init_css_set to contain a subsys
4657 * pointer to this state - since the subsystem is
4658 * newly registered, all tasks and hence the
4659 * init_css_set is in the subsystem's top cgroup. */
4660 init_css_set
.subsys
[ss
->subsys_id
] = css
;
4662 need_forkexit_callback
|= ss
->fork
|| ss
->exit
;
4664 /* At system boot, before all subsystems have been
4665 * registered, no tasks have been forked, so we don't
4666 * need to invoke fork callbacks here. */
4667 BUG_ON(!list_empty(&init_task
.tasks
));
4669 BUG_ON(online_css(ss
, cgroup_dummy_top
));
4671 mutex_unlock(&cgroup_mutex
);
4673 /* this function shouldn't be used with modular subsystems, since they
4674 * need to register a subsys_id, among other things */
4679 * cgroup_load_subsys: load and register a modular subsystem at runtime
4680 * @ss: the subsystem to load
4682 * This function should be called in a modular subsystem's initcall. If the
4683 * subsystem is built as a module, it will be assigned a new subsys_id and set
4684 * up for use. If the subsystem is built-in anyway, work is delegated to the
4685 * simpler cgroup_init_subsys.
4687 int __init_or_module
cgroup_load_subsys(struct cgroup_subsys
*ss
)
4689 struct cgroup_subsys_state
*css
;
4691 struct hlist_node
*tmp
;
4692 struct css_set
*cset
;
4695 /* check name and function validity */
4696 if (ss
->name
== NULL
|| strlen(ss
->name
) > MAX_CGROUP_TYPE_NAMELEN
||
4697 ss
->css_alloc
== NULL
|| ss
->css_free
== NULL
)
4701 * we don't support callbacks in modular subsystems. this check is
4702 * before the ss->module check for consistency; a subsystem that could
4703 * be a module should still have no callbacks even if the user isn't
4704 * compiling it as one.
4706 if (ss
->fork
|| ss
->exit
)
4710 * an optionally modular subsystem is built-in: we want to do nothing,
4711 * since cgroup_init_subsys will have already taken care of it.
4713 if (ss
->module
== NULL
) {
4714 /* a sanity check */
4715 BUG_ON(cgroup_subsys
[ss
->subsys_id
] != ss
);
4719 /* init base cftset */
4720 cgroup_init_cftsets(ss
);
4722 mutex_lock(&cgroup_mutex
);
4723 cgroup_subsys
[ss
->subsys_id
] = ss
;
4726 * no ss->css_alloc seems to need anything important in the ss
4727 * struct, so this can happen first (i.e. before the dummy root
4730 css
= ss
->css_alloc(cgroup_dummy_top
);
4732 /* failure case - need to deassign the cgroup_subsys[] slot. */
4733 cgroup_subsys
[ss
->subsys_id
] = NULL
;
4734 mutex_unlock(&cgroup_mutex
);
4735 return PTR_ERR(css
);
4738 list_add(&ss
->sibling
, &cgroup_dummy_root
.subsys_list
);
4739 ss
->root
= &cgroup_dummy_root
;
4741 /* our new subsystem will be attached to the dummy hierarchy. */
4742 init_cgroup_css(css
, ss
, cgroup_dummy_top
);
4743 /* init_idr must be after init_cgroup_css because it sets css->id. */
4745 ret
= cgroup_init_idr(ss
, css
);
4751 * Now we need to entangle the css into the existing css_sets. unlike
4752 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4753 * will need a new pointer to it; done by iterating the css_set_table.
4754 * furthermore, modifying the existing css_sets will corrupt the hash
4755 * table state, so each changed css_set will need its hash recomputed.
4756 * this is all done under the css_set_lock.
4758 write_lock(&css_set_lock
);
4759 hash_for_each_safe(css_set_table
, i
, tmp
, cset
, hlist
) {
4760 /* skip entries that we already rehashed */
4761 if (cset
->subsys
[ss
->subsys_id
])
4763 /* remove existing entry */
4764 hash_del(&cset
->hlist
);
4766 cset
->subsys
[ss
->subsys_id
] = css
;
4767 /* recompute hash and restore entry */
4768 key
= css_set_hash(cset
->subsys
);
4769 hash_add(css_set_table
, &cset
->hlist
, key
);
4771 write_unlock(&css_set_lock
);
4773 ret
= online_css(ss
, cgroup_dummy_top
);
4778 mutex_unlock(&cgroup_mutex
);
4782 mutex_unlock(&cgroup_mutex
);
4783 /* @ss can't be mounted here as try_module_get() would fail */
4784 cgroup_unload_subsys(ss
);
4787 EXPORT_SYMBOL_GPL(cgroup_load_subsys
);
4790 * cgroup_unload_subsys: unload a modular subsystem
4791 * @ss: the subsystem to unload
4793 * This function should be called in a modular subsystem's exitcall. When this
4794 * function is invoked, the refcount on the subsystem's module will be 0, so
4795 * the subsystem will not be attached to any hierarchy.
4797 void cgroup_unload_subsys(struct cgroup_subsys
*ss
)
4799 struct cgrp_cset_link
*link
;
4801 BUG_ON(ss
->module
== NULL
);
4804 * we shouldn't be called if the subsystem is in use, and the use of
4805 * try_module_get in parse_cgroupfs_options should ensure that it
4806 * doesn't start being used while we're killing it off.
4808 BUG_ON(ss
->root
!= &cgroup_dummy_root
);
4810 mutex_lock(&cgroup_mutex
);
4812 offline_css(ss
, cgroup_dummy_top
);
4815 idr_destroy(&ss
->idr
);
4817 /* deassign the subsys_id */
4818 cgroup_subsys
[ss
->subsys_id
] = NULL
;
4820 /* remove subsystem from the dummy root's list of subsystems */
4821 list_del_init(&ss
->sibling
);
4824 * disentangle the css from all css_sets attached to the dummy
4825 * top. as in loading, we need to pay our respects to the hashtable
4828 write_lock(&css_set_lock
);
4829 list_for_each_entry(link
, &cgroup_dummy_top
->cset_links
, cset_link
) {
4830 struct css_set
*cset
= link
->cset
;
4833 hash_del(&cset
->hlist
);
4834 cset
->subsys
[ss
->subsys_id
] = NULL
;
4835 key
= css_set_hash(cset
->subsys
);
4836 hash_add(css_set_table
, &cset
->hlist
, key
);
4838 write_unlock(&css_set_lock
);
4841 * remove subsystem's css from the cgroup_dummy_top and free it -
4842 * need to free before marking as null because ss->css_free needs
4843 * the cgrp->subsys pointer to find their state. note that this
4844 * also takes care of freeing the css_id.
4846 ss
->css_free(cgroup_dummy_top
);
4847 cgroup_dummy_top
->subsys
[ss
->subsys_id
] = NULL
;
4849 mutex_unlock(&cgroup_mutex
);
4851 EXPORT_SYMBOL_GPL(cgroup_unload_subsys
);
4854 * cgroup_init_early - cgroup initialization at system boot
4856 * Initialize cgroups at system boot, and initialize any
4857 * subsystems that request early init.
4859 int __init
cgroup_init_early(void)
4861 struct cgroup_subsys
*ss
;
4864 atomic_set(&init_css_set
.refcount
, 1);
4865 INIT_LIST_HEAD(&init_css_set
.cgrp_links
);
4866 INIT_LIST_HEAD(&init_css_set
.tasks
);
4867 INIT_HLIST_NODE(&init_css_set
.hlist
);
4869 init_cgroup_root(&cgroup_dummy_root
);
4870 cgroup_root_count
= 1;
4871 RCU_INIT_POINTER(init_task
.cgroups
, &init_css_set
);
4873 init_cgrp_cset_link
.cset
= &init_css_set
;
4874 init_cgrp_cset_link
.cgrp
= cgroup_dummy_top
;
4875 list_add(&init_cgrp_cset_link
.cset_link
, &cgroup_dummy_top
->cset_links
);
4876 list_add(&init_cgrp_cset_link
.cgrp_link
, &init_css_set
.cgrp_links
);
4878 /* at bootup time, we don't worry about modular subsystems */
4879 for_each_builtin_subsys(ss
, i
) {
4881 BUG_ON(strlen(ss
->name
) > MAX_CGROUP_TYPE_NAMELEN
);
4882 BUG_ON(!ss
->css_alloc
);
4883 BUG_ON(!ss
->css_free
);
4884 if (ss
->subsys_id
!= i
) {
4885 printk(KERN_ERR
"cgroup: Subsys %s id == %d\n",
4886 ss
->name
, ss
->subsys_id
);
4891 cgroup_init_subsys(ss
);
4897 * cgroup_init - cgroup initialization
4899 * Register cgroup filesystem and /proc file, and initialize
4900 * any subsystems that didn't request early init.
4902 int __init
cgroup_init(void)
4904 struct cgroup_subsys
*ss
;
4908 err
= bdi_init(&cgroup_backing_dev_info
);
4912 for_each_builtin_subsys(ss
, i
) {
4913 if (!ss
->early_init
)
4914 cgroup_init_subsys(ss
);
4916 cgroup_init_idr(ss
, init_css_set
.subsys
[ss
->subsys_id
]);
4919 /* allocate id for the dummy hierarchy */
4920 mutex_lock(&cgroup_mutex
);
4921 mutex_lock(&cgroup_root_mutex
);
4923 /* Add init_css_set to the hash table */
4924 key
= css_set_hash(init_css_set
.subsys
);
4925 hash_add(css_set_table
, &init_css_set
.hlist
, key
);
4927 BUG_ON(cgroup_init_root_id(&cgroup_dummy_root
, 0, 1));
4929 mutex_unlock(&cgroup_root_mutex
);
4930 mutex_unlock(&cgroup_mutex
);
4932 cgroup_kobj
= kobject_create_and_add("cgroup", fs_kobj
);
4938 err
= register_filesystem(&cgroup_fs_type
);
4940 kobject_put(cgroup_kobj
);
4944 proc_create("cgroups", 0, NULL
, &proc_cgroupstats_operations
);
4948 bdi_destroy(&cgroup_backing_dev_info
);
4954 * proc_cgroup_show()
4955 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4956 * - Used for /proc/<pid>/cgroup.
4957 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4958 * doesn't really matter if tsk->cgroup changes after we read it,
4959 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
4960 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4961 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4962 * cgroup to top_cgroup.
4965 /* TODO: Use a proper seq_file iterator */
4966 int proc_cgroup_show(struct seq_file
*m
, void *v
)
4969 struct task_struct
*tsk
;
4972 struct cgroupfs_root
*root
;
4975 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
4981 tsk
= get_pid_task(pid
, PIDTYPE_PID
);
4987 mutex_lock(&cgroup_mutex
);
4989 for_each_active_root(root
) {
4990 struct cgroup_subsys
*ss
;
4991 struct cgroup
*cgrp
;
4994 seq_printf(m
, "%d:", root
->hierarchy_id
);
4995 for_each_root_subsys(root
, ss
)
4996 seq_printf(m
, "%s%s", count
++ ? "," : "", ss
->name
);
4997 if (strlen(root
->name
))
4998 seq_printf(m
, "%sname=%s", count
? "," : "",
5001 cgrp
= task_cgroup_from_root(tsk
, root
);
5002 retval
= cgroup_path(cgrp
, buf
, PAGE_SIZE
);
5010 mutex_unlock(&cgroup_mutex
);
5011 put_task_struct(tsk
);
5018 /* Display information about each subsystem and each hierarchy */
5019 static int proc_cgroupstats_show(struct seq_file
*m
, void *v
)
5021 struct cgroup_subsys
*ss
;
5024 seq_puts(m
, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
5026 * ideally we don't want subsystems moving around while we do this.
5027 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
5028 * subsys/hierarchy state.
5030 mutex_lock(&cgroup_mutex
);
5032 for_each_subsys(ss
, i
)
5033 seq_printf(m
, "%s\t%d\t%d\t%d\n",
5034 ss
->name
, ss
->root
->hierarchy_id
,
5035 ss
->root
->number_of_cgroups
, !ss
->disabled
);
5037 mutex_unlock(&cgroup_mutex
);
5041 static int cgroupstats_open(struct inode
*inode
, struct file
*file
)
5043 return single_open(file
, proc_cgroupstats_show
, NULL
);
5046 static const struct file_operations proc_cgroupstats_operations
= {
5047 .open
= cgroupstats_open
,
5049 .llseek
= seq_lseek
,
5050 .release
= single_release
,
5054 * cgroup_fork - attach newly forked task to its parents cgroup.
5055 * @child: pointer to task_struct of forking parent process.
5057 * Description: A task inherits its parent's cgroup at fork().
5059 * A pointer to the shared css_set was automatically copied in
5060 * fork.c by dup_task_struct(). However, we ignore that copy, since
5061 * it was not made under the protection of RCU or cgroup_mutex, so
5062 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
5063 * have already changed current->cgroups, allowing the previously
5064 * referenced cgroup group to be removed and freed.
5066 * At the point that cgroup_fork() is called, 'current' is the parent
5067 * task, and the passed argument 'child' points to the child task.
5069 void cgroup_fork(struct task_struct
*child
)
5072 get_css_set(task_css_set(current
));
5073 child
->cgroups
= current
->cgroups
;
5074 task_unlock(current
);
5075 INIT_LIST_HEAD(&child
->cg_list
);
5079 * cgroup_post_fork - called on a new task after adding it to the task list
5080 * @child: the task in question
5082 * Adds the task to the list running through its css_set if necessary and
5083 * call the subsystem fork() callbacks. Has to be after the task is
5084 * visible on the task list in case we race with the first call to
5085 * cgroup_iter_start() - to guarantee that the new task ends up on its
5088 void cgroup_post_fork(struct task_struct
*child
)
5090 struct cgroup_subsys
*ss
;
5094 * use_task_css_set_links is set to 1 before we walk the tasklist
5095 * under the tasklist_lock and we read it here after we added the child
5096 * to the tasklist under the tasklist_lock as well. If the child wasn't
5097 * yet in the tasklist when we walked through it from
5098 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
5099 * should be visible now due to the paired locking and barriers implied
5100 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
5101 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
5104 if (use_task_css_set_links
) {
5105 write_lock(&css_set_lock
);
5107 if (list_empty(&child
->cg_list
))
5108 list_add(&child
->cg_list
, &task_css_set(child
)->tasks
);
5110 write_unlock(&css_set_lock
);
5114 * Call ss->fork(). This must happen after @child is linked on
5115 * css_set; otherwise, @child might change state between ->fork()
5116 * and addition to css_set.
5118 if (need_forkexit_callback
) {
5120 * fork/exit callbacks are supported only for builtin
5121 * subsystems, and the builtin section of the subsys
5122 * array is immutable, so we don't need to lock the
5123 * subsys array here. On the other hand, modular section
5124 * of the array can be freed at module unload, so we
5127 for_each_builtin_subsys(ss
, i
)
5134 * cgroup_exit - detach cgroup from exiting task
5135 * @tsk: pointer to task_struct of exiting process
5136 * @run_callback: run exit callbacks?
5138 * Description: Detach cgroup from @tsk and release it.
5140 * Note that cgroups marked notify_on_release force every task in
5141 * them to take the global cgroup_mutex mutex when exiting.
5142 * This could impact scaling on very large systems. Be reluctant to
5143 * use notify_on_release cgroups where very high task exit scaling
5144 * is required on large systems.
5146 * the_top_cgroup_hack:
5148 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
5150 * We call cgroup_exit() while the task is still competent to
5151 * handle notify_on_release(), then leave the task attached to the
5152 * root cgroup in each hierarchy for the remainder of its exit.
5154 * To do this properly, we would increment the reference count on
5155 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
5156 * code we would add a second cgroup function call, to drop that
5157 * reference. This would just create an unnecessary hot spot on
5158 * the top_cgroup reference count, to no avail.
5160 * Normally, holding a reference to a cgroup without bumping its
5161 * count is unsafe. The cgroup could go away, or someone could
5162 * attach us to a different cgroup, decrementing the count on
5163 * the first cgroup that we never incremented. But in this case,
5164 * top_cgroup isn't going away, and either task has PF_EXITING set,
5165 * which wards off any cgroup_attach_task() attempts, or task is a failed
5166 * fork, never visible to cgroup_attach_task.
5168 void cgroup_exit(struct task_struct
*tsk
, int run_callbacks
)
5170 struct cgroup_subsys
*ss
;
5171 struct css_set
*cset
;
5175 * Unlink from the css_set task list if necessary.
5176 * Optimistically check cg_list before taking
5179 if (!list_empty(&tsk
->cg_list
)) {
5180 write_lock(&css_set_lock
);
5181 if (!list_empty(&tsk
->cg_list
))
5182 list_del_init(&tsk
->cg_list
);
5183 write_unlock(&css_set_lock
);
5186 /* Reassign the task to the init_css_set. */
5188 cset
= task_css_set(tsk
);
5189 RCU_INIT_POINTER(tsk
->cgroups
, &init_css_set
);
5191 if (run_callbacks
&& need_forkexit_callback
) {
5193 * fork/exit callbacks are supported only for builtin
5194 * subsystems, see cgroup_post_fork() for details.
5196 for_each_builtin_subsys(ss
, i
) {
5198 struct cgroup
*old_cgrp
= cset
->subsys
[i
]->cgroup
;
5199 struct cgroup
*cgrp
= task_cgroup(tsk
, i
);
5201 ss
->exit(cgrp
, old_cgrp
, tsk
);
5207 put_css_set_taskexit(cset
);
5210 static void check_for_release(struct cgroup
*cgrp
)
5212 if (cgroup_is_releasable(cgrp
) &&
5213 list_empty(&cgrp
->cset_links
) && list_empty(&cgrp
->children
)) {
5215 * Control Group is currently removeable. If it's not
5216 * already queued for a userspace notification, queue
5219 int need_schedule_work
= 0;
5221 raw_spin_lock(&release_list_lock
);
5222 if (!cgroup_is_dead(cgrp
) &&
5223 list_empty(&cgrp
->release_list
)) {
5224 list_add(&cgrp
->release_list
, &release_list
);
5225 need_schedule_work
= 1;
5227 raw_spin_unlock(&release_list_lock
);
5228 if (need_schedule_work
)
5229 schedule_work(&release_agent_work
);
5234 * Notify userspace when a cgroup is released, by running the
5235 * configured release agent with the name of the cgroup (path
5236 * relative to the root of cgroup file system) as the argument.
5238 * Most likely, this user command will try to rmdir this cgroup.
5240 * This races with the possibility that some other task will be
5241 * attached to this cgroup before it is removed, or that some other
5242 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5243 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5244 * unused, and this cgroup will be reprieved from its death sentence,
5245 * to continue to serve a useful existence. Next time it's released,
5246 * we will get notified again, if it still has 'notify_on_release' set.
5248 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5249 * means only wait until the task is successfully execve()'d. The
5250 * separate release agent task is forked by call_usermodehelper(),
5251 * then control in this thread returns here, without waiting for the
5252 * release agent task. We don't bother to wait because the caller of
5253 * this routine has no use for the exit status of the release agent
5254 * task, so no sense holding our caller up for that.
5256 static void cgroup_release_agent(struct work_struct
*work
)
5258 BUG_ON(work
!= &release_agent_work
);
5259 mutex_lock(&cgroup_mutex
);
5260 raw_spin_lock(&release_list_lock
);
5261 while (!list_empty(&release_list
)) {
5262 char *argv
[3], *envp
[3];
5264 char *pathbuf
= NULL
, *agentbuf
= NULL
;
5265 struct cgroup
*cgrp
= list_entry(release_list
.next
,
5268 list_del_init(&cgrp
->release_list
);
5269 raw_spin_unlock(&release_list_lock
);
5270 pathbuf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
5273 if (cgroup_path(cgrp
, pathbuf
, PAGE_SIZE
) < 0)
5275 agentbuf
= kstrdup(cgrp
->root
->release_agent_path
, GFP_KERNEL
);
5280 argv
[i
++] = agentbuf
;
5281 argv
[i
++] = pathbuf
;
5285 /* minimal command environment */
5286 envp
[i
++] = "HOME=/";
5287 envp
[i
++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5290 /* Drop the lock while we invoke the usermode helper,
5291 * since the exec could involve hitting disk and hence
5292 * be a slow process */
5293 mutex_unlock(&cgroup_mutex
);
5294 call_usermodehelper(argv
[0], argv
, envp
, UMH_WAIT_EXEC
);
5295 mutex_lock(&cgroup_mutex
);
5299 raw_spin_lock(&release_list_lock
);
5301 raw_spin_unlock(&release_list_lock
);
5302 mutex_unlock(&cgroup_mutex
);
5305 static int __init
cgroup_disable(char *str
)
5307 struct cgroup_subsys
*ss
;
5311 while ((token
= strsep(&str
, ",")) != NULL
) {
5316 * cgroup_disable, being at boot time, can't know about
5317 * module subsystems, so we don't worry about them.
5319 for_each_builtin_subsys(ss
, i
) {
5320 if (!strcmp(token
, ss
->name
)) {
5322 printk(KERN_INFO
"Disabling %s control group"
5323 " subsystem\n", ss
->name
);
5330 __setup("cgroup_disable=", cgroup_disable
);
5333 * Functons for CSS ID.
5336 /* to get ID other than 0, this should be called when !cgroup_is_dead() */
5337 unsigned short css_id(struct cgroup_subsys_state
*css
)
5339 struct css_id
*cssid
;
5342 * This css_id() can return correct value when somone has refcnt
5343 * on this or this is under rcu_read_lock(). Once css->id is allocated,
5344 * it's unchanged until freed.
5346 cssid
= rcu_dereference_raw(css
->id
);
5352 EXPORT_SYMBOL_GPL(css_id
);
5355 * css_is_ancestor - test "root" css is an ancestor of "child"
5356 * @child: the css to be tested.
5357 * @root: the css supporsed to be an ancestor of the child.
5359 * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
5360 * this function reads css->id, the caller must hold rcu_read_lock().
5361 * But, considering usual usage, the csses should be valid objects after test.
5362 * Assuming that the caller will do some action to the child if this returns
5363 * returns true, the caller must take "child";s reference count.
5364 * If "child" is valid object and this returns true, "root" is valid, too.
5367 bool css_is_ancestor(struct cgroup_subsys_state
*child
,
5368 const struct cgroup_subsys_state
*root
)
5370 struct css_id
*child_id
;
5371 struct css_id
*root_id
;
5373 child_id
= rcu_dereference(child
->id
);
5376 root_id
= rcu_dereference(root
->id
);
5379 if (child_id
->depth
< root_id
->depth
)
5381 if (child_id
->stack
[root_id
->depth
] != root_id
->id
)
5386 void free_css_id(struct cgroup_subsys
*ss
, struct cgroup_subsys_state
*css
)
5388 struct css_id
*id
= rcu_dereference_protected(css
->id
, true);
5390 /* When this is called before css_id initialization, id can be NULL */
5394 BUG_ON(!ss
->use_id
);
5396 rcu_assign_pointer(id
->css
, NULL
);
5397 rcu_assign_pointer(css
->id
, NULL
);
5398 spin_lock(&ss
->id_lock
);
5399 idr_remove(&ss
->idr
, id
->id
);
5400 spin_unlock(&ss
->id_lock
);
5401 kfree_rcu(id
, rcu_head
);
5403 EXPORT_SYMBOL_GPL(free_css_id
);
5406 * This is called by init or create(). Then, calls to this function are
5407 * always serialized (By cgroup_mutex() at create()).
5410 static struct css_id
*get_new_cssid(struct cgroup_subsys
*ss
, int depth
)
5412 struct css_id
*newid
;
5415 BUG_ON(!ss
->use_id
);
5417 size
= sizeof(*newid
) + sizeof(unsigned short) * (depth
+ 1);
5418 newid
= kzalloc(size
, GFP_KERNEL
);
5420 return ERR_PTR(-ENOMEM
);
5422 idr_preload(GFP_KERNEL
);
5423 spin_lock(&ss
->id_lock
);
5424 /* Don't use 0. allocates an ID of 1-65535 */
5425 ret
= idr_alloc(&ss
->idr
, newid
, 1, CSS_ID_MAX
+ 1, GFP_NOWAIT
);
5426 spin_unlock(&ss
->id_lock
);
5429 /* Returns error when there are no free spaces for new ID.*/
5434 newid
->depth
= depth
;
5438 return ERR_PTR(ret
);
5442 static int __init_or_module
cgroup_init_idr(struct cgroup_subsys
*ss
,
5443 struct cgroup_subsys_state
*rootcss
)
5445 struct css_id
*newid
;
5447 spin_lock_init(&ss
->id_lock
);
5450 newid
= get_new_cssid(ss
, 0);
5452 return PTR_ERR(newid
);
5454 newid
->stack
[0] = newid
->id
;
5455 RCU_INIT_POINTER(newid
->css
, rootcss
);
5456 RCU_INIT_POINTER(rootcss
->id
, newid
);
5460 static int alloc_css_id(struct cgroup_subsys
*ss
, struct cgroup
*parent
,
5461 struct cgroup
*child
)
5463 int subsys_id
, i
, depth
= 0;
5464 struct cgroup_subsys_state
*parent_css
, *child_css
;
5465 struct css_id
*child_id
, *parent_id
;
5467 subsys_id
= ss
->subsys_id
;
5468 parent_css
= parent
->subsys
[subsys_id
];
5469 child_css
= child
->subsys
[subsys_id
];
5470 parent_id
= rcu_dereference_protected(parent_css
->id
, true);
5471 depth
= parent_id
->depth
+ 1;
5473 child_id
= get_new_cssid(ss
, depth
);
5474 if (IS_ERR(child_id
))
5475 return PTR_ERR(child_id
);
5477 for (i
= 0; i
< depth
; i
++)
5478 child_id
->stack
[i
] = parent_id
->stack
[i
];
5479 child_id
->stack
[depth
] = child_id
->id
;
5481 * child_id->css pointer will be set after this cgroup is available
5482 * see cgroup_populate_dir()
5484 rcu_assign_pointer(child_css
->id
, child_id
);
5490 * css_lookup - lookup css by id
5491 * @ss: cgroup subsys to be looked into.
5494 * Returns pointer to cgroup_subsys_state if there is valid one with id.
5495 * NULL if not. Should be called under rcu_read_lock()
5497 struct cgroup_subsys_state
*css_lookup(struct cgroup_subsys
*ss
, int id
)
5499 struct css_id
*cssid
= NULL
;
5501 BUG_ON(!ss
->use_id
);
5502 cssid
= idr_find(&ss
->idr
, id
);
5504 if (unlikely(!cssid
))
5507 return rcu_dereference(cssid
->css
);
5509 EXPORT_SYMBOL_GPL(css_lookup
);
5512 * get corresponding css from file open on cgroupfs directory
5514 struct cgroup_subsys_state
*cgroup_css_from_dir(struct file
*f
, int id
)
5516 struct cgroup
*cgrp
;
5517 struct inode
*inode
;
5518 struct cgroup_subsys_state
*css
;
5520 inode
= file_inode(f
);
5521 /* check in cgroup filesystem dir */
5522 if (inode
->i_op
!= &cgroup_dir_inode_operations
)
5523 return ERR_PTR(-EBADF
);
5525 if (id
< 0 || id
>= CGROUP_SUBSYS_COUNT
)
5526 return ERR_PTR(-EINVAL
);
5529 cgrp
= __d_cgrp(f
->f_dentry
);
5530 css
= cgrp
->subsys
[id
];
5531 return css
? css
: ERR_PTR(-ENOENT
);
5534 #ifdef CONFIG_CGROUP_DEBUG
5535 static struct cgroup_subsys_state
*debug_css_alloc(struct cgroup
*cgrp
)
5537 struct cgroup_subsys_state
*css
= kzalloc(sizeof(*css
), GFP_KERNEL
);
5540 return ERR_PTR(-ENOMEM
);
5545 static void debug_css_free(struct cgroup
*cgrp
)
5547 kfree(cgrp
->subsys
[debug_subsys_id
]);
5550 static u64
debug_taskcount_read(struct cgroup
*cgrp
, struct cftype
*cft
)
5552 return cgroup_task_count(cgrp
);
5555 static u64
current_css_set_read(struct cgroup
*cgrp
, struct cftype
*cft
)
5557 return (u64
)(unsigned long)current
->cgroups
;
5560 static u64
current_css_set_refcount_read(struct cgroup
*cgrp
,
5566 count
= atomic_read(&task_css_set(current
)->refcount
);
5571 static int current_css_set_cg_links_read(struct cgroup
*cgrp
,
5573 struct seq_file
*seq
)
5575 struct cgrp_cset_link
*link
;
5576 struct css_set
*cset
;
5578 read_lock(&css_set_lock
);
5580 cset
= rcu_dereference(current
->cgroups
);
5581 list_for_each_entry(link
, &cset
->cgrp_links
, cgrp_link
) {
5582 struct cgroup
*c
= link
->cgrp
;
5586 name
= c
->dentry
->d_name
.name
;
5589 seq_printf(seq
, "Root %d group %s\n",
5590 c
->root
->hierarchy_id
, name
);
5593 read_unlock(&css_set_lock
);
5597 #define MAX_TASKS_SHOWN_PER_CSS 25
5598 static int cgroup_css_links_read(struct cgroup
*cgrp
,
5600 struct seq_file
*seq
)
5602 struct cgrp_cset_link
*link
;
5604 read_lock(&css_set_lock
);
5605 list_for_each_entry(link
, &cgrp
->cset_links
, cset_link
) {
5606 struct css_set
*cset
= link
->cset
;
5607 struct task_struct
*task
;
5609 seq_printf(seq
, "css_set %p\n", cset
);
5610 list_for_each_entry(task
, &cset
->tasks
, cg_list
) {
5611 if (count
++ > MAX_TASKS_SHOWN_PER_CSS
) {
5612 seq_puts(seq
, " ...\n");
5615 seq_printf(seq
, " task %d\n",
5616 task_pid_vnr(task
));
5620 read_unlock(&css_set_lock
);
5624 static u64
releasable_read(struct cgroup
*cgrp
, struct cftype
*cft
)
5626 return test_bit(CGRP_RELEASABLE
, &cgrp
->flags
);
5629 static struct cftype debug_files
[] = {
5631 .name
= "taskcount",
5632 .read_u64
= debug_taskcount_read
,
5636 .name
= "current_css_set",
5637 .read_u64
= current_css_set_read
,
5641 .name
= "current_css_set_refcount",
5642 .read_u64
= current_css_set_refcount_read
,
5646 .name
= "current_css_set_cg_links",
5647 .read_seq_string
= current_css_set_cg_links_read
,
5651 .name
= "cgroup_css_links",
5652 .read_seq_string
= cgroup_css_links_read
,
5656 .name
= "releasable",
5657 .read_u64
= releasable_read
,
5663 struct cgroup_subsys debug_subsys
= {
5665 .css_alloc
= debug_css_alloc
,
5666 .css_free
= debug_css_free
,
5667 .subsys_id
= debug_subsys_id
,
5668 .base_cftypes
= debug_files
,
5670 #endif /* CONFIG_CGROUP_DEBUG */