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
);
262 /* iterate each subsystem attached to a hierarchy */
263 #define for_each_root_subsys(root, ss) \
264 list_for_each_entry((ss), &(root)->subsys_list, sibling)
266 /* iterate across the active hierarchies */
267 #define for_each_active_root(root) \
268 list_for_each_entry((root), &cgroup_roots, root_list)
270 static inline struct cgroup
*__d_cgrp(struct dentry
*dentry
)
272 return dentry
->d_fsdata
;
275 static inline struct cfent
*__d_cfe(struct dentry
*dentry
)
277 return dentry
->d_fsdata
;
280 static inline struct cftype
*__d_cft(struct dentry
*dentry
)
282 return __d_cfe(dentry
)->type
;
286 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
287 * @cgrp: the cgroup to be checked for liveness
289 * On success, returns true; the mutex should be later unlocked. On
290 * failure returns false with no lock held.
292 static bool cgroup_lock_live_group(struct cgroup
*cgrp
)
294 mutex_lock(&cgroup_mutex
);
295 if (cgroup_is_dead(cgrp
)) {
296 mutex_unlock(&cgroup_mutex
);
302 /* the list of cgroups eligible for automatic release. Protected by
303 * release_list_lock */
304 static LIST_HEAD(release_list
);
305 static DEFINE_RAW_SPINLOCK(release_list_lock
);
306 static void cgroup_release_agent(struct work_struct
*work
);
307 static DECLARE_WORK(release_agent_work
, cgroup_release_agent
);
308 static void check_for_release(struct cgroup
*cgrp
);
311 * A cgroup can be associated with multiple css_sets as different tasks may
312 * belong to different cgroups on different hierarchies. In the other
313 * direction, a css_set is naturally associated with multiple cgroups.
314 * This M:N relationship is represented by the following link structure
315 * which exists for each association and allows traversing the associations
318 struct cgrp_cset_link
{
319 /* the cgroup and css_set this link associates */
321 struct css_set
*cset
;
323 /* list of cgrp_cset_links anchored at cgrp->cset_links */
324 struct list_head cset_link
;
326 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
327 struct list_head cgrp_link
;
330 /* The default css_set - used by init and its children prior to any
331 * hierarchies being mounted. It contains a pointer to the root state
332 * for each subsystem. Also used to anchor the list of css_sets. Not
333 * reference-counted, to improve performance when child cgroups
334 * haven't been created.
337 static struct css_set init_css_set
;
338 static struct cgrp_cset_link init_cgrp_cset_link
;
340 static int cgroup_init_idr(struct cgroup_subsys
*ss
,
341 struct cgroup_subsys_state
*css
);
343 /* css_set_lock protects the list of css_set objects, and the
344 * chain of tasks off each css_set. Nests outside task->alloc_lock
345 * due to cgroup_iter_start() */
346 static DEFINE_RWLOCK(css_set_lock
);
347 static int css_set_count
;
350 * hash table for cgroup groups. This improves the performance to find
351 * an existing css_set. This hash doesn't (currently) take into
352 * account cgroups in empty hierarchies.
354 #define CSS_SET_HASH_BITS 7
355 static DEFINE_HASHTABLE(css_set_table
, CSS_SET_HASH_BITS
);
357 static unsigned long css_set_hash(struct cgroup_subsys_state
*css
[])
360 unsigned long key
= 0UL;
362 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++)
363 key
+= (unsigned long)css
[i
];
364 key
= (key
>> 16) ^ key
;
369 /* We don't maintain the lists running through each css_set to its
370 * task until after the first call to cgroup_iter_start(). This
371 * reduces the fork()/exit() overhead for people who have cgroups
372 * compiled into their kernel but not actually in use */
373 static int use_task_css_set_links __read_mostly
;
375 static void __put_css_set(struct css_set
*cset
, int taskexit
)
377 struct cgrp_cset_link
*link
, *tmp_link
;
380 * Ensure that the refcount doesn't hit zero while any readers
381 * can see it. Similar to atomic_dec_and_lock(), but for an
384 if (atomic_add_unless(&cset
->refcount
, -1, 1))
386 write_lock(&css_set_lock
);
387 if (!atomic_dec_and_test(&cset
->refcount
)) {
388 write_unlock(&css_set_lock
);
392 /* This css_set is dead. unlink it and release cgroup refcounts */
393 hash_del(&cset
->hlist
);
396 list_for_each_entry_safe(link
, tmp_link
, &cset
->cgrp_links
, cgrp_link
) {
397 struct cgroup
*cgrp
= link
->cgrp
;
399 list_del(&link
->cset_link
);
400 list_del(&link
->cgrp_link
);
402 /* @cgrp can't go away while we're holding css_set_lock */
403 if (list_empty(&cgrp
->cset_links
) && notify_on_release(cgrp
)) {
405 set_bit(CGRP_RELEASABLE
, &cgrp
->flags
);
406 check_for_release(cgrp
);
412 write_unlock(&css_set_lock
);
413 kfree_rcu(cset
, rcu_head
);
417 * refcounted get/put for css_set objects
419 static inline void get_css_set(struct css_set
*cset
)
421 atomic_inc(&cset
->refcount
);
424 static inline void put_css_set(struct css_set
*cset
)
426 __put_css_set(cset
, 0);
429 static inline void put_css_set_taskexit(struct css_set
*cset
)
431 __put_css_set(cset
, 1);
435 * compare_css_sets - helper function for find_existing_css_set().
436 * @cset: candidate css_set being tested
437 * @old_cset: existing css_set for a task
438 * @new_cgrp: cgroup that's being entered by the task
439 * @template: desired set of css pointers in css_set (pre-calculated)
441 * Returns true if "cg" matches "old_cg" except for the hierarchy
442 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
444 static bool compare_css_sets(struct css_set
*cset
,
445 struct css_set
*old_cset
,
446 struct cgroup
*new_cgrp
,
447 struct cgroup_subsys_state
*template[])
449 struct list_head
*l1
, *l2
;
451 if (memcmp(template, cset
->subsys
, sizeof(cset
->subsys
))) {
452 /* Not all subsystems matched */
457 * Compare cgroup pointers in order to distinguish between
458 * different cgroups in heirarchies with no subsystems. We
459 * could get by with just this check alone (and skip the
460 * memcmp above) but on most setups the memcmp check will
461 * avoid the need for this more expensive check on almost all
465 l1
= &cset
->cgrp_links
;
466 l2
= &old_cset
->cgrp_links
;
468 struct cgrp_cset_link
*link1
, *link2
;
469 struct cgroup
*cgrp1
, *cgrp2
;
473 /* See if we reached the end - both lists are equal length. */
474 if (l1
== &cset
->cgrp_links
) {
475 BUG_ON(l2
!= &old_cset
->cgrp_links
);
478 BUG_ON(l2
== &old_cset
->cgrp_links
);
480 /* Locate the cgroups associated with these links. */
481 link1
= list_entry(l1
, struct cgrp_cset_link
, cgrp_link
);
482 link2
= list_entry(l2
, struct cgrp_cset_link
, cgrp_link
);
485 /* Hierarchies should be linked in the same order. */
486 BUG_ON(cgrp1
->root
!= cgrp2
->root
);
489 * If this hierarchy is the hierarchy of the cgroup
490 * that's changing, then we need to check that this
491 * css_set points to the new cgroup; if it's any other
492 * hierarchy, then this css_set should point to the
493 * same cgroup as the old css_set.
495 if (cgrp1
->root
== new_cgrp
->root
) {
496 if (cgrp1
!= new_cgrp
)
507 * find_existing_css_set - init css array and find the matching css_set
508 * @old_cset: the css_set that we're using before the cgroup transition
509 * @cgrp: the cgroup that we're moving into
510 * @template: out param for the new set of csses, should be clear on entry
512 static struct css_set
*find_existing_css_set(struct css_set
*old_cset
,
514 struct cgroup_subsys_state
*template[])
516 struct cgroupfs_root
*root
= cgrp
->root
;
517 struct css_set
*cset
;
522 * Build the set of subsystem state objects that we want to see in the
523 * new css_set. while subsystems can change globally, the entries here
524 * won't change, so no need for locking.
526 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
527 if (root
->subsys_mask
& (1UL << i
)) {
528 /* Subsystem is in this hierarchy. So we want
529 * the subsystem state from the new
531 template[i
] = cgrp
->subsys
[i
];
533 /* Subsystem is not in this hierarchy, so we
534 * don't want to change the subsystem state */
535 template[i
] = old_cset
->subsys
[i
];
539 key
= css_set_hash(template);
540 hash_for_each_possible(css_set_table
, cset
, hlist
, key
) {
541 if (!compare_css_sets(cset
, old_cset
, cgrp
, template))
544 /* This css_set matches what we need */
548 /* No existing cgroup group matched */
552 static void free_cgrp_cset_links(struct list_head
*links_to_free
)
554 struct cgrp_cset_link
*link
, *tmp_link
;
556 list_for_each_entry_safe(link
, tmp_link
, links_to_free
, cset_link
) {
557 list_del(&link
->cset_link
);
563 * allocate_cgrp_cset_links - allocate cgrp_cset_links
564 * @count: the number of links to allocate
565 * @tmp_links: list_head the allocated links are put on
567 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
568 * through ->cset_link. Returns 0 on success or -errno.
570 static int allocate_cgrp_cset_links(int count
, struct list_head
*tmp_links
)
572 struct cgrp_cset_link
*link
;
575 INIT_LIST_HEAD(tmp_links
);
577 for (i
= 0; i
< count
; i
++) {
578 link
= kzalloc(sizeof(*link
), GFP_KERNEL
);
580 free_cgrp_cset_links(tmp_links
);
583 list_add(&link
->cset_link
, tmp_links
);
589 * link_css_set - a helper function to link a css_set to a cgroup
590 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
591 * @cset: the css_set to be linked
592 * @cgrp: the destination cgroup
594 static void link_css_set(struct list_head
*tmp_links
, struct css_set
*cset
,
597 struct cgrp_cset_link
*link
;
599 BUG_ON(list_empty(tmp_links
));
600 link
= list_first_entry(tmp_links
, struct cgrp_cset_link
, cset_link
);
603 list_move(&link
->cset_link
, &cgrp
->cset_links
);
605 * Always add links to the tail of the list so that the list
606 * is sorted by order of hierarchy creation
608 list_add_tail(&link
->cgrp_link
, &cset
->cgrp_links
);
612 * find_css_set - return a new css_set with one cgroup updated
613 * @old_cset: the baseline css_set
614 * @cgrp: the cgroup to be updated
616 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
617 * substituted into the appropriate hierarchy.
619 static struct css_set
*find_css_set(struct css_set
*old_cset
,
622 struct cgroup_subsys_state
*template[CGROUP_SUBSYS_COUNT
] = { };
623 struct css_set
*cset
;
624 struct list_head tmp_links
;
625 struct cgrp_cset_link
*link
;
628 lockdep_assert_held(&cgroup_mutex
);
630 /* First see if we already have a cgroup group that matches
632 read_lock(&css_set_lock
);
633 cset
= find_existing_css_set(old_cset
, cgrp
, template);
636 read_unlock(&css_set_lock
);
641 cset
= kzalloc(sizeof(*cset
), GFP_KERNEL
);
645 /* Allocate all the cgrp_cset_link objects that we'll need */
646 if (allocate_cgrp_cset_links(cgroup_root_count
, &tmp_links
) < 0) {
651 atomic_set(&cset
->refcount
, 1);
652 INIT_LIST_HEAD(&cset
->cgrp_links
);
653 INIT_LIST_HEAD(&cset
->tasks
);
654 INIT_HLIST_NODE(&cset
->hlist
);
656 /* Copy the set of subsystem state objects generated in
657 * find_existing_css_set() */
658 memcpy(cset
->subsys
, template, sizeof(cset
->subsys
));
660 write_lock(&css_set_lock
);
661 /* Add reference counts and links from the new css_set. */
662 list_for_each_entry(link
, &old_cset
->cgrp_links
, cgrp_link
) {
663 struct cgroup
*c
= link
->cgrp
;
665 if (c
->root
== cgrp
->root
)
667 link_css_set(&tmp_links
, cset
, c
);
670 BUG_ON(!list_empty(&tmp_links
));
674 /* Add this cgroup group to the hash table */
675 key
= css_set_hash(cset
->subsys
);
676 hash_add(css_set_table
, &cset
->hlist
, key
);
678 write_unlock(&css_set_lock
);
684 * Return the cgroup for "task" from the given hierarchy. Must be
685 * called with cgroup_mutex held.
687 static struct cgroup
*task_cgroup_from_root(struct task_struct
*task
,
688 struct cgroupfs_root
*root
)
690 struct css_set
*cset
;
691 struct cgroup
*res
= NULL
;
693 BUG_ON(!mutex_is_locked(&cgroup_mutex
));
694 read_lock(&css_set_lock
);
696 * No need to lock the task - since we hold cgroup_mutex the
697 * task can't change groups, so the only thing that can happen
698 * is that it exits and its css is set back to init_css_set.
700 cset
= task
->cgroups
;
701 if (cset
== &init_css_set
) {
702 res
= &root
->top_cgroup
;
704 struct cgrp_cset_link
*link
;
706 list_for_each_entry(link
, &cset
->cgrp_links
, cgrp_link
) {
707 struct cgroup
*c
= link
->cgrp
;
709 if (c
->root
== root
) {
715 read_unlock(&css_set_lock
);
721 * There is one global cgroup mutex. We also require taking
722 * task_lock() when dereferencing a task's cgroup subsys pointers.
723 * See "The task_lock() exception", at the end of this comment.
725 * A task must hold cgroup_mutex to modify cgroups.
727 * Any task can increment and decrement the count field without lock.
728 * So in general, code holding cgroup_mutex can't rely on the count
729 * field not changing. However, if the count goes to zero, then only
730 * cgroup_attach_task() can increment it again. Because a count of zero
731 * means that no tasks are currently attached, therefore there is no
732 * way a task attached to that cgroup can fork (the other way to
733 * increment the count). So code holding cgroup_mutex can safely
734 * assume that if the count is zero, it will stay zero. Similarly, if
735 * a task holds cgroup_mutex on a cgroup with zero count, it
736 * knows that the cgroup won't be removed, as cgroup_rmdir()
739 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
740 * (usually) take cgroup_mutex. These are the two most performance
741 * critical pieces of code here. The exception occurs on cgroup_exit(),
742 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
743 * is taken, and if the cgroup count is zero, a usermode call made
744 * to the release agent with the name of the cgroup (path relative to
745 * the root of cgroup file system) as the argument.
747 * A cgroup can only be deleted if both its 'count' of using tasks
748 * is zero, and its list of 'children' cgroups is empty. Since all
749 * tasks in the system use _some_ cgroup, and since there is always at
750 * least one task in the system (init, pid == 1), therefore, top_cgroup
751 * always has either children cgroups and/or using tasks. So we don't
752 * need a special hack to ensure that top_cgroup cannot be deleted.
754 * The task_lock() exception
756 * The need for this exception arises from the action of
757 * cgroup_attach_task(), which overwrites one task's cgroup pointer with
758 * another. It does so using cgroup_mutex, however there are
759 * several performance critical places that need to reference
760 * task->cgroup without the expense of grabbing a system global
761 * mutex. Therefore except as noted below, when dereferencing or, as
762 * in cgroup_attach_task(), modifying a task's cgroup pointer we use
763 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
764 * the task_struct routinely used for such matters.
766 * P.S. One more locking exception. RCU is used to guard the
767 * update of a tasks cgroup pointer by cgroup_attach_task()
771 * A couple of forward declarations required, due to cyclic reference loop:
772 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
773 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
777 static int cgroup_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
);
778 static struct dentry
*cgroup_lookup(struct inode
*, struct dentry
*, unsigned int);
779 static int cgroup_rmdir(struct inode
*unused_dir
, struct dentry
*dentry
);
780 static int cgroup_populate_dir(struct cgroup
*cgrp
, bool base_files
,
781 unsigned long subsys_mask
);
782 static const struct inode_operations cgroup_dir_inode_operations
;
783 static const struct file_operations proc_cgroupstats_operations
;
785 static struct backing_dev_info cgroup_backing_dev_info
= {
787 .capabilities
= BDI_CAP_NO_ACCT_AND_WRITEBACK
,
790 static int alloc_css_id(struct cgroup_subsys
*ss
,
791 struct cgroup
*parent
, struct cgroup
*child
);
793 static struct inode
*cgroup_new_inode(umode_t mode
, struct super_block
*sb
)
795 struct inode
*inode
= new_inode(sb
);
798 inode
->i_ino
= get_next_ino();
799 inode
->i_mode
= mode
;
800 inode
->i_uid
= current_fsuid();
801 inode
->i_gid
= current_fsgid();
802 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
803 inode
->i_mapping
->backing_dev_info
= &cgroup_backing_dev_info
;
808 static struct cgroup_name
*cgroup_alloc_name(struct dentry
*dentry
)
810 struct cgroup_name
*name
;
812 name
= kmalloc(sizeof(*name
) + dentry
->d_name
.len
+ 1, GFP_KERNEL
);
815 strcpy(name
->name
, dentry
->d_name
.name
);
819 static void cgroup_free_fn(struct work_struct
*work
)
821 struct cgroup
*cgrp
= container_of(work
, struct cgroup
, destroy_work
);
822 struct cgroup_subsys
*ss
;
824 mutex_lock(&cgroup_mutex
);
826 * Release the subsystem state objects.
828 for_each_root_subsys(cgrp
->root
, ss
)
831 cgrp
->root
->number_of_cgroups
--;
832 mutex_unlock(&cgroup_mutex
);
835 * We get a ref to the parent's dentry, and put the ref when
836 * this cgroup is being freed, so it's guaranteed that the
837 * parent won't be destroyed before its children.
839 dput(cgrp
->parent
->dentry
);
841 ida_simple_remove(&cgrp
->root
->cgroup_ida
, cgrp
->id
);
844 * Drop the active superblock reference that we took when we
845 * created the cgroup. This will free cgrp->root, if we are
846 * holding the last reference to @sb.
848 deactivate_super(cgrp
->root
->sb
);
851 * if we're getting rid of the cgroup, refcount should ensure
852 * that there are no pidlists left.
854 BUG_ON(!list_empty(&cgrp
->pidlists
));
856 simple_xattrs_free(&cgrp
->xattrs
);
858 kfree(rcu_dereference_raw(cgrp
->name
));
862 static void cgroup_free_rcu(struct rcu_head
*head
)
864 struct cgroup
*cgrp
= container_of(head
, struct cgroup
, rcu_head
);
866 INIT_WORK(&cgrp
->destroy_work
, cgroup_free_fn
);
867 schedule_work(&cgrp
->destroy_work
);
870 static void cgroup_diput(struct dentry
*dentry
, struct inode
*inode
)
872 /* is dentry a directory ? if so, kfree() associated cgroup */
873 if (S_ISDIR(inode
->i_mode
)) {
874 struct cgroup
*cgrp
= dentry
->d_fsdata
;
876 BUG_ON(!(cgroup_is_dead(cgrp
)));
877 call_rcu(&cgrp
->rcu_head
, cgroup_free_rcu
);
879 struct cfent
*cfe
= __d_cfe(dentry
);
880 struct cgroup
*cgrp
= dentry
->d_parent
->d_fsdata
;
882 WARN_ONCE(!list_empty(&cfe
->node
) &&
883 cgrp
!= &cgrp
->root
->top_cgroup
,
884 "cfe still linked for %s\n", cfe
->type
->name
);
885 simple_xattrs_free(&cfe
->xattrs
);
891 static int cgroup_delete(const struct dentry
*d
)
896 static void remove_dir(struct dentry
*d
)
898 struct dentry
*parent
= dget(d
->d_parent
);
901 simple_rmdir(parent
->d_inode
, d
);
905 static void cgroup_rm_file(struct cgroup
*cgrp
, const struct cftype
*cft
)
909 lockdep_assert_held(&cgrp
->dentry
->d_inode
->i_mutex
);
910 lockdep_assert_held(&cgroup_mutex
);
913 * If we're doing cleanup due to failure of cgroup_create(),
914 * the corresponding @cfe may not exist.
916 list_for_each_entry(cfe
, &cgrp
->files
, node
) {
917 struct dentry
*d
= cfe
->dentry
;
919 if (cft
&& cfe
->type
!= cft
)
924 simple_unlink(cgrp
->dentry
->d_inode
, d
);
925 list_del_init(&cfe
->node
);
933 * cgroup_clear_directory - selective removal of base and subsystem files
934 * @dir: directory containing the files
935 * @base_files: true if the base files should be removed
936 * @subsys_mask: mask of the subsystem ids whose files should be removed
938 static void cgroup_clear_directory(struct dentry
*dir
, bool base_files
,
939 unsigned long subsys_mask
)
941 struct cgroup
*cgrp
= __d_cgrp(dir
);
942 struct cgroup_subsys
*ss
;
944 for_each_root_subsys(cgrp
->root
, ss
) {
945 struct cftype_set
*set
;
946 if (!test_bit(ss
->subsys_id
, &subsys_mask
))
948 list_for_each_entry(set
, &ss
->cftsets
, node
)
949 cgroup_addrm_files(cgrp
, NULL
, set
->cfts
, false);
952 while (!list_empty(&cgrp
->files
))
953 cgroup_rm_file(cgrp
, NULL
);
958 * NOTE : the dentry must have been dget()'ed
960 static void cgroup_d_remove_dir(struct dentry
*dentry
)
962 struct dentry
*parent
;
963 struct cgroupfs_root
*root
= dentry
->d_sb
->s_fs_info
;
965 cgroup_clear_directory(dentry
, true, root
->subsys_mask
);
967 parent
= dentry
->d_parent
;
968 spin_lock(&parent
->d_lock
);
969 spin_lock_nested(&dentry
->d_lock
, DENTRY_D_LOCK_NESTED
);
970 list_del_init(&dentry
->d_u
.d_child
);
971 spin_unlock(&dentry
->d_lock
);
972 spin_unlock(&parent
->d_lock
);
977 * Call with cgroup_mutex held. Drops reference counts on modules, including
978 * any duplicate ones that parse_cgroupfs_options took. If this function
979 * returns an error, no reference counts are touched.
981 static int rebind_subsystems(struct cgroupfs_root
*root
,
982 unsigned long added_mask
, unsigned removed_mask
)
984 struct cgroup
*cgrp
= &root
->top_cgroup
;
987 BUG_ON(!mutex_is_locked(&cgroup_mutex
));
988 BUG_ON(!mutex_is_locked(&cgroup_root_mutex
));
990 /* Check that any added subsystems are currently free */
991 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
992 unsigned long bit
= 1UL << i
;
993 struct cgroup_subsys
*ss
= cgroup_subsys
[i
];
994 if (!(bit
& added_mask
))
997 * Nobody should tell us to do a subsys that doesn't exist:
998 * parse_cgroupfs_options should catch that case and refcounts
999 * ensure that subsystems won't disappear once selected.
1002 if (ss
->root
!= &cgroup_dummy_root
) {
1003 /* Subsystem isn't free */
1008 /* Currently we don't handle adding/removing subsystems when
1009 * any child cgroups exist. This is theoretically supportable
1010 * but involves complex error handling, so it's being left until
1012 if (root
->number_of_cgroups
> 1)
1015 /* Process each subsystem */
1016 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
1017 struct cgroup_subsys
*ss
= cgroup_subsys
[i
];
1018 unsigned long bit
= 1UL << i
;
1019 if (bit
& added_mask
) {
1020 /* We're binding this subsystem to this hierarchy */
1022 BUG_ON(cgrp
->subsys
[i
]);
1023 BUG_ON(!cgroup_dummy_top
->subsys
[i
]);
1024 BUG_ON(cgroup_dummy_top
->subsys
[i
]->cgroup
!= cgroup_dummy_top
);
1026 cgrp
->subsys
[i
] = cgroup_dummy_top
->subsys
[i
];
1027 cgrp
->subsys
[i
]->cgroup
= cgrp
;
1028 list_move(&ss
->sibling
, &root
->subsys_list
);
1033 /* refcount was already taken, and we're keeping it */
1034 root
->subsys_mask
|= bit
;
1035 } else if (bit
& removed_mask
) {
1036 /* We're removing this subsystem */
1038 BUG_ON(cgrp
->subsys
[i
] != cgroup_dummy_top
->subsys
[i
]);
1039 BUG_ON(cgrp
->subsys
[i
]->cgroup
!= cgrp
);
1042 ss
->bind(cgroup_dummy_top
);
1043 cgroup_dummy_top
->subsys
[i
]->cgroup
= cgroup_dummy_top
;
1044 cgrp
->subsys
[i
] = NULL
;
1045 cgroup_subsys
[i
]->root
= &cgroup_dummy_root
;
1046 list_move(&ss
->sibling
, &cgroup_dummy_root
.subsys_list
);
1048 /* subsystem is now free - drop reference on module */
1049 module_put(ss
->module
);
1050 root
->subsys_mask
&= ~bit
;
1051 } else if (bit
& root
->subsys_mask
) {
1052 /* Subsystem state should already exist */
1054 BUG_ON(!cgrp
->subsys
[i
]);
1056 * a refcount was taken, but we already had one, so
1057 * drop the extra reference.
1059 module_put(ss
->module
);
1060 #ifdef CONFIG_MODULE_UNLOAD
1061 BUG_ON(ss
->module
&& !module_refcount(ss
->module
));
1064 /* Subsystem state shouldn't exist */
1065 BUG_ON(cgrp
->subsys
[i
]);
1072 static int cgroup_show_options(struct seq_file
*seq
, struct dentry
*dentry
)
1074 struct cgroupfs_root
*root
= dentry
->d_sb
->s_fs_info
;
1075 struct cgroup_subsys
*ss
;
1077 mutex_lock(&cgroup_root_mutex
);
1078 for_each_root_subsys(root
, ss
)
1079 seq_printf(seq
, ",%s", ss
->name
);
1080 if (root
->flags
& CGRP_ROOT_SANE_BEHAVIOR
)
1081 seq_puts(seq
, ",sane_behavior");
1082 if (root
->flags
& CGRP_ROOT_NOPREFIX
)
1083 seq_puts(seq
, ",noprefix");
1084 if (root
->flags
& CGRP_ROOT_XATTR
)
1085 seq_puts(seq
, ",xattr");
1086 if (strlen(root
->release_agent_path
))
1087 seq_printf(seq
, ",release_agent=%s", root
->release_agent_path
);
1088 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN
, &root
->top_cgroup
.flags
))
1089 seq_puts(seq
, ",clone_children");
1090 if (strlen(root
->name
))
1091 seq_printf(seq
, ",name=%s", root
->name
);
1092 mutex_unlock(&cgroup_root_mutex
);
1096 struct cgroup_sb_opts
{
1097 unsigned long subsys_mask
;
1098 unsigned long flags
;
1099 char *release_agent
;
1100 bool cpuset_clone_children
;
1102 /* User explicitly requested empty subsystem */
1105 struct cgroupfs_root
*new_root
;
1110 * Convert a hierarchy specifier into a bitmask of subsystems and
1111 * flags. Call with cgroup_mutex held to protect the cgroup_subsys[]
1112 * array. This function takes refcounts on subsystems to be used, unless it
1113 * returns error, in which case no refcounts are taken.
1115 static int parse_cgroupfs_options(char *data
, struct cgroup_sb_opts
*opts
)
1117 char *token
, *o
= data
;
1118 bool all_ss
= false, one_ss
= false;
1119 unsigned long mask
= (unsigned long)-1;
1121 bool module_pin_failed
= false;
1123 BUG_ON(!mutex_is_locked(&cgroup_mutex
));
1125 #ifdef CONFIG_CPUSETS
1126 mask
= ~(1UL << cpuset_subsys_id
);
1129 memset(opts
, 0, sizeof(*opts
));
1131 while ((token
= strsep(&o
, ",")) != NULL
) {
1134 if (!strcmp(token
, "none")) {
1135 /* Explicitly have no subsystems */
1139 if (!strcmp(token
, "all")) {
1140 /* Mutually exclusive option 'all' + subsystem name */
1146 if (!strcmp(token
, "__DEVEL__sane_behavior")) {
1147 opts
->flags
|= CGRP_ROOT_SANE_BEHAVIOR
;
1150 if (!strcmp(token
, "noprefix")) {
1151 opts
->flags
|= CGRP_ROOT_NOPREFIX
;
1154 if (!strcmp(token
, "clone_children")) {
1155 opts
->cpuset_clone_children
= true;
1158 if (!strcmp(token
, "xattr")) {
1159 opts
->flags
|= CGRP_ROOT_XATTR
;
1162 if (!strncmp(token
, "release_agent=", 14)) {
1163 /* Specifying two release agents is forbidden */
1164 if (opts
->release_agent
)
1166 opts
->release_agent
=
1167 kstrndup(token
+ 14, PATH_MAX
- 1, GFP_KERNEL
);
1168 if (!opts
->release_agent
)
1172 if (!strncmp(token
, "name=", 5)) {
1173 const char *name
= token
+ 5;
1174 /* Can't specify an empty name */
1177 /* Must match [\w.-]+ */
1178 for (i
= 0; i
< strlen(name
); i
++) {
1182 if ((c
== '.') || (c
== '-') || (c
== '_'))
1186 /* Specifying two names is forbidden */
1189 opts
->name
= kstrndup(name
,
1190 MAX_CGROUP_ROOT_NAMELEN
- 1,
1198 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
1199 struct cgroup_subsys
*ss
= cgroup_subsys
[i
];
1202 if (strcmp(token
, ss
->name
))
1207 /* Mutually exclusive option 'all' + subsystem name */
1210 set_bit(i
, &opts
->subsys_mask
);
1215 if (i
== CGROUP_SUBSYS_COUNT
)
1220 * If the 'all' option was specified select all the subsystems,
1221 * otherwise if 'none', 'name=' and a subsystem name options
1222 * were not specified, let's default to 'all'
1224 if (all_ss
|| (!one_ss
&& !opts
->none
&& !opts
->name
)) {
1225 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
1226 struct cgroup_subsys
*ss
= cgroup_subsys
[i
];
1231 set_bit(i
, &opts
->subsys_mask
);
1235 /* Consistency checks */
1237 if (opts
->flags
& CGRP_ROOT_SANE_BEHAVIOR
) {
1238 pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
1240 if (opts
->flags
& CGRP_ROOT_NOPREFIX
) {
1241 pr_err("cgroup: sane_behavior: noprefix is not allowed\n");
1245 if (opts
->cpuset_clone_children
) {
1246 pr_err("cgroup: sane_behavior: clone_children is not allowed\n");
1252 * Option noprefix was introduced just for backward compatibility
1253 * with the old cpuset, so we allow noprefix only if mounting just
1254 * the cpuset subsystem.
1256 if ((opts
->flags
& CGRP_ROOT_NOPREFIX
) && (opts
->subsys_mask
& mask
))
1260 /* Can't specify "none" and some subsystems */
1261 if (opts
->subsys_mask
&& opts
->none
)
1265 * We either have to specify by name or by subsystems. (So all
1266 * empty hierarchies must have a name).
1268 if (!opts
->subsys_mask
&& !opts
->name
)
1272 * Grab references on all the modules we'll need, so the subsystems
1273 * don't dance around before rebind_subsystems attaches them. This may
1274 * take duplicate reference counts on a subsystem that's already used,
1275 * but rebind_subsystems handles this case.
1277 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
1278 unsigned long bit
= 1UL << i
;
1280 if (!(bit
& opts
->subsys_mask
))
1282 if (!try_module_get(cgroup_subsys
[i
]->module
)) {
1283 module_pin_failed
= true;
1287 if (module_pin_failed
) {
1289 * oops, one of the modules was going away. this means that we
1290 * raced with a module_delete call, and to the user this is
1291 * essentially a "subsystem doesn't exist" case.
1293 for (i
--; i
>= 0; i
--) {
1294 /* drop refcounts only on the ones we took */
1295 unsigned long bit
= 1UL << i
;
1297 if (!(bit
& opts
->subsys_mask
))
1299 module_put(cgroup_subsys
[i
]->module
);
1307 static void drop_parsed_module_refcounts(unsigned long subsys_mask
)
1310 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
1311 unsigned long bit
= 1UL << i
;
1313 if (!(bit
& subsys_mask
))
1315 module_put(cgroup_subsys
[i
]->module
);
1319 static int cgroup_remount(struct super_block
*sb
, int *flags
, char *data
)
1322 struct cgroupfs_root
*root
= sb
->s_fs_info
;
1323 struct cgroup
*cgrp
= &root
->top_cgroup
;
1324 struct cgroup_sb_opts opts
;
1325 unsigned long added_mask
, removed_mask
;
1327 if (root
->flags
& CGRP_ROOT_SANE_BEHAVIOR
) {
1328 pr_err("cgroup: sane_behavior: remount is not allowed\n");
1332 mutex_lock(&cgrp
->dentry
->d_inode
->i_mutex
);
1333 mutex_lock(&cgroup_mutex
);
1334 mutex_lock(&cgroup_root_mutex
);
1336 /* See what subsystems are wanted */
1337 ret
= parse_cgroupfs_options(data
, &opts
);
1341 if (opts
.subsys_mask
!= root
->subsys_mask
|| opts
.release_agent
)
1342 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1343 task_tgid_nr(current
), current
->comm
);
1345 added_mask
= opts
.subsys_mask
& ~root
->subsys_mask
;
1346 removed_mask
= root
->subsys_mask
& ~opts
.subsys_mask
;
1348 /* Don't allow flags or name to change at remount */
1349 if (opts
.flags
!= root
->flags
||
1350 (opts
.name
&& strcmp(opts
.name
, root
->name
))) {
1352 drop_parsed_module_refcounts(opts
.subsys_mask
);
1357 * Clear out the files of subsystems that should be removed, do
1358 * this before rebind_subsystems, since rebind_subsystems may
1359 * change this hierarchy's subsys_list.
1361 cgroup_clear_directory(cgrp
->dentry
, false, removed_mask
);
1363 ret
= rebind_subsystems(root
, added_mask
, removed_mask
);
1365 /* rebind_subsystems failed, re-populate the removed files */
1366 cgroup_populate_dir(cgrp
, false, removed_mask
);
1367 drop_parsed_module_refcounts(opts
.subsys_mask
);
1371 /* re-populate subsystem files */
1372 cgroup_populate_dir(cgrp
, false, added_mask
);
1374 if (opts
.release_agent
)
1375 strcpy(root
->release_agent_path
, opts
.release_agent
);
1377 kfree(opts
.release_agent
);
1379 mutex_unlock(&cgroup_root_mutex
);
1380 mutex_unlock(&cgroup_mutex
);
1381 mutex_unlock(&cgrp
->dentry
->d_inode
->i_mutex
);
1385 static const struct super_operations cgroup_ops
= {
1386 .statfs
= simple_statfs
,
1387 .drop_inode
= generic_delete_inode
,
1388 .show_options
= cgroup_show_options
,
1389 .remount_fs
= cgroup_remount
,
1392 static void init_cgroup_housekeeping(struct cgroup
*cgrp
)
1394 INIT_LIST_HEAD(&cgrp
->sibling
);
1395 INIT_LIST_HEAD(&cgrp
->children
);
1396 INIT_LIST_HEAD(&cgrp
->files
);
1397 INIT_LIST_HEAD(&cgrp
->cset_links
);
1398 INIT_LIST_HEAD(&cgrp
->release_list
);
1399 INIT_LIST_HEAD(&cgrp
->pidlists
);
1400 mutex_init(&cgrp
->pidlist_mutex
);
1401 INIT_LIST_HEAD(&cgrp
->event_list
);
1402 spin_lock_init(&cgrp
->event_list_lock
);
1403 simple_xattrs_init(&cgrp
->xattrs
);
1406 static void init_cgroup_root(struct cgroupfs_root
*root
)
1408 struct cgroup
*cgrp
= &root
->top_cgroup
;
1410 INIT_LIST_HEAD(&root
->subsys_list
);
1411 INIT_LIST_HEAD(&root
->root_list
);
1412 root
->number_of_cgroups
= 1;
1414 cgrp
->name
= &root_cgroup_name
;
1415 init_cgroup_housekeeping(cgrp
);
1418 static int cgroup_init_root_id(struct cgroupfs_root
*root
)
1422 lockdep_assert_held(&cgroup_mutex
);
1423 lockdep_assert_held(&cgroup_root_mutex
);
1425 id
= idr_alloc_cyclic(&cgroup_hierarchy_idr
, root
, 2, 0, GFP_KERNEL
);
1429 root
->hierarchy_id
= id
;
1433 static void cgroup_exit_root_id(struct cgroupfs_root
*root
)
1435 lockdep_assert_held(&cgroup_mutex
);
1436 lockdep_assert_held(&cgroup_root_mutex
);
1438 if (root
->hierarchy_id
) {
1439 idr_remove(&cgroup_hierarchy_idr
, root
->hierarchy_id
);
1440 root
->hierarchy_id
= 0;
1444 static int cgroup_test_super(struct super_block
*sb
, void *data
)
1446 struct cgroup_sb_opts
*opts
= data
;
1447 struct cgroupfs_root
*root
= sb
->s_fs_info
;
1449 /* If we asked for a name then it must match */
1450 if (opts
->name
&& strcmp(opts
->name
, root
->name
))
1454 * If we asked for subsystems (or explicitly for no
1455 * subsystems) then they must match
1457 if ((opts
->subsys_mask
|| opts
->none
)
1458 && (opts
->subsys_mask
!= root
->subsys_mask
))
1464 static struct cgroupfs_root
*cgroup_root_from_opts(struct cgroup_sb_opts
*opts
)
1466 struct cgroupfs_root
*root
;
1468 if (!opts
->subsys_mask
&& !opts
->none
)
1471 root
= kzalloc(sizeof(*root
), GFP_KERNEL
);
1473 return ERR_PTR(-ENOMEM
);
1475 init_cgroup_root(root
);
1477 root
->subsys_mask
= opts
->subsys_mask
;
1478 root
->flags
= opts
->flags
;
1479 ida_init(&root
->cgroup_ida
);
1480 if (opts
->release_agent
)
1481 strcpy(root
->release_agent_path
, opts
->release_agent
);
1483 strcpy(root
->name
, opts
->name
);
1484 if (opts
->cpuset_clone_children
)
1485 set_bit(CGRP_CPUSET_CLONE_CHILDREN
, &root
->top_cgroup
.flags
);
1489 static void cgroup_free_root(struct cgroupfs_root
*root
)
1492 /* hierarhcy ID shoulid already have been released */
1493 WARN_ON_ONCE(root
->hierarchy_id
);
1495 ida_destroy(&root
->cgroup_ida
);
1500 static int cgroup_set_super(struct super_block
*sb
, void *data
)
1503 struct cgroup_sb_opts
*opts
= data
;
1505 /* If we don't have a new root, we can't set up a new sb */
1506 if (!opts
->new_root
)
1509 BUG_ON(!opts
->subsys_mask
&& !opts
->none
);
1511 ret
= set_anon_super(sb
, NULL
);
1515 sb
->s_fs_info
= opts
->new_root
;
1516 opts
->new_root
->sb
= sb
;
1518 sb
->s_blocksize
= PAGE_CACHE_SIZE
;
1519 sb
->s_blocksize_bits
= PAGE_CACHE_SHIFT
;
1520 sb
->s_magic
= CGROUP_SUPER_MAGIC
;
1521 sb
->s_op
= &cgroup_ops
;
1526 static int cgroup_get_rootdir(struct super_block
*sb
)
1528 static const struct dentry_operations cgroup_dops
= {
1529 .d_iput
= cgroup_diput
,
1530 .d_delete
= cgroup_delete
,
1533 struct inode
*inode
=
1534 cgroup_new_inode(S_IFDIR
| S_IRUGO
| S_IXUGO
| S_IWUSR
, sb
);
1539 inode
->i_fop
= &simple_dir_operations
;
1540 inode
->i_op
= &cgroup_dir_inode_operations
;
1541 /* directories start off with i_nlink == 2 (for "." entry) */
1543 sb
->s_root
= d_make_root(inode
);
1546 /* for everything else we want ->d_op set */
1547 sb
->s_d_op
= &cgroup_dops
;
1551 static struct dentry
*cgroup_mount(struct file_system_type
*fs_type
,
1552 int flags
, const char *unused_dev_name
,
1555 struct cgroup_sb_opts opts
;
1556 struct cgroupfs_root
*root
;
1558 struct super_block
*sb
;
1559 struct cgroupfs_root
*new_root
;
1560 struct inode
*inode
;
1562 /* First find the desired set of subsystems */
1563 mutex_lock(&cgroup_mutex
);
1564 ret
= parse_cgroupfs_options(data
, &opts
);
1565 mutex_unlock(&cgroup_mutex
);
1570 * Allocate a new cgroup root. We may not need it if we're
1571 * reusing an existing hierarchy.
1573 new_root
= cgroup_root_from_opts(&opts
);
1574 if (IS_ERR(new_root
)) {
1575 ret
= PTR_ERR(new_root
);
1578 opts
.new_root
= new_root
;
1580 /* Locate an existing or new sb for this hierarchy */
1581 sb
= sget(fs_type
, cgroup_test_super
, cgroup_set_super
, 0, &opts
);
1584 cgroup_free_root(opts
.new_root
);
1588 root
= sb
->s_fs_info
;
1590 if (root
== opts
.new_root
) {
1591 /* We used the new root structure, so this is a new hierarchy */
1592 struct list_head tmp_links
;
1593 struct cgroup
*root_cgrp
= &root
->top_cgroup
;
1594 struct cgroupfs_root
*existing_root
;
1595 const struct cred
*cred
;
1597 struct css_set
*cset
;
1599 BUG_ON(sb
->s_root
!= NULL
);
1601 ret
= cgroup_get_rootdir(sb
);
1603 goto drop_new_super
;
1604 inode
= sb
->s_root
->d_inode
;
1606 mutex_lock(&inode
->i_mutex
);
1607 mutex_lock(&cgroup_mutex
);
1608 mutex_lock(&cgroup_root_mutex
);
1610 /* Check for name clashes with existing mounts */
1612 if (strlen(root
->name
))
1613 for_each_active_root(existing_root
)
1614 if (!strcmp(existing_root
->name
, root
->name
))
1618 * We're accessing css_set_count without locking
1619 * css_set_lock here, but that's OK - it can only be
1620 * increased by someone holding cgroup_lock, and
1621 * that's us. The worst that can happen is that we
1622 * have some link structures left over
1624 ret
= allocate_cgrp_cset_links(css_set_count
, &tmp_links
);
1628 ret
= cgroup_init_root_id(root
);
1632 ret
= rebind_subsystems(root
, root
->subsys_mask
, 0);
1633 if (ret
== -EBUSY
) {
1634 free_cgrp_cset_links(&tmp_links
);
1638 * There must be no failure case after here, since rebinding
1639 * takes care of subsystems' refcounts, which are explicitly
1640 * dropped in the failure exit path.
1643 /* EBUSY should be the only error here */
1646 list_add(&root
->root_list
, &cgroup_roots
);
1647 cgroup_root_count
++;
1649 sb
->s_root
->d_fsdata
= root_cgrp
;
1650 root
->top_cgroup
.dentry
= sb
->s_root
;
1652 /* Link the top cgroup in this hierarchy into all
1653 * the css_set objects */
1654 write_lock(&css_set_lock
);
1655 hash_for_each(css_set_table
, i
, cset
, hlist
)
1656 link_css_set(&tmp_links
, cset
, root_cgrp
);
1657 write_unlock(&css_set_lock
);
1659 free_cgrp_cset_links(&tmp_links
);
1661 BUG_ON(!list_empty(&root_cgrp
->children
));
1662 BUG_ON(root
->number_of_cgroups
!= 1);
1664 cred
= override_creds(&init_cred
);
1665 cgroup_populate_dir(root_cgrp
, true, root
->subsys_mask
);
1667 mutex_unlock(&cgroup_root_mutex
);
1668 mutex_unlock(&cgroup_mutex
);
1669 mutex_unlock(&inode
->i_mutex
);
1672 * We re-used an existing hierarchy - the new root (if
1673 * any) is not needed
1675 cgroup_free_root(opts
.new_root
);
1677 if (root
->flags
!= opts
.flags
) {
1678 if ((root
->flags
| opts
.flags
) & CGRP_ROOT_SANE_BEHAVIOR
) {
1679 pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
1681 goto drop_new_super
;
1683 pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n");
1687 /* no subsys rebinding, so refcounts don't change */
1688 drop_parsed_module_refcounts(opts
.subsys_mask
);
1691 kfree(opts
.release_agent
);
1693 return dget(sb
->s_root
);
1696 cgroup_exit_root_id(root
);
1697 mutex_unlock(&cgroup_root_mutex
);
1698 mutex_unlock(&cgroup_mutex
);
1699 mutex_unlock(&inode
->i_mutex
);
1701 deactivate_locked_super(sb
);
1703 drop_parsed_module_refcounts(opts
.subsys_mask
);
1705 kfree(opts
.release_agent
);
1707 return ERR_PTR(ret
);
1710 static void cgroup_kill_sb(struct super_block
*sb
) {
1711 struct cgroupfs_root
*root
= sb
->s_fs_info
;
1712 struct cgroup
*cgrp
= &root
->top_cgroup
;
1713 struct cgrp_cset_link
*link
, *tmp_link
;
1718 BUG_ON(root
->number_of_cgroups
!= 1);
1719 BUG_ON(!list_empty(&cgrp
->children
));
1721 mutex_lock(&cgroup_mutex
);
1722 mutex_lock(&cgroup_root_mutex
);
1724 /* Rebind all subsystems back to the default hierarchy */
1725 ret
= rebind_subsystems(root
, 0, root
->subsys_mask
);
1726 /* Shouldn't be able to fail ... */
1730 * Release all the links from cset_links to this hierarchy's
1733 write_lock(&css_set_lock
);
1735 list_for_each_entry_safe(link
, tmp_link
, &cgrp
->cset_links
, cset_link
) {
1736 list_del(&link
->cset_link
);
1737 list_del(&link
->cgrp_link
);
1740 write_unlock(&css_set_lock
);
1742 if (!list_empty(&root
->root_list
)) {
1743 list_del(&root
->root_list
);
1744 cgroup_root_count
--;
1747 cgroup_exit_root_id(root
);
1749 mutex_unlock(&cgroup_root_mutex
);
1750 mutex_unlock(&cgroup_mutex
);
1752 simple_xattrs_free(&cgrp
->xattrs
);
1754 kill_litter_super(sb
);
1755 cgroup_free_root(root
);
1758 static struct file_system_type cgroup_fs_type
= {
1760 .mount
= cgroup_mount
,
1761 .kill_sb
= cgroup_kill_sb
,
1764 static struct kobject
*cgroup_kobj
;
1767 * cgroup_path - generate the path of a cgroup
1768 * @cgrp: the cgroup in question
1769 * @buf: the buffer to write the path into
1770 * @buflen: the length of the buffer
1772 * Writes path of cgroup into buf. Returns 0 on success, -errno on error.
1774 * We can't generate cgroup path using dentry->d_name, as accessing
1775 * dentry->name must be protected by irq-unsafe dentry->d_lock or parent
1776 * inode's i_mutex, while on the other hand cgroup_path() can be called
1777 * with some irq-safe spinlocks held.
1779 int cgroup_path(const struct cgroup
*cgrp
, char *buf
, int buflen
)
1781 int ret
= -ENAMETOOLONG
;
1784 if (!cgrp
->parent
) {
1785 if (strlcpy(buf
, "/", buflen
) >= buflen
)
1786 return -ENAMETOOLONG
;
1790 start
= buf
+ buflen
- 1;
1795 const char *name
= cgroup_name(cgrp
);
1799 if ((start
-= len
) < buf
)
1801 memcpy(start
, name
, len
);
1807 cgrp
= cgrp
->parent
;
1808 } while (cgrp
->parent
);
1810 memmove(buf
, start
, buf
+ buflen
- start
);
1815 EXPORT_SYMBOL_GPL(cgroup_path
);
1818 * task_cgroup_path_from_hierarchy - cgroup path of a task on a hierarchy
1819 * @task: target task
1820 * @hierarchy_id: the hierarchy to look up @task's cgroup from
1821 * @buf: the buffer to write the path into
1822 * @buflen: the length of the buffer
1824 * Determine @task's cgroup on the hierarchy specified by @hierarchy_id and
1825 * copy its path into @buf. This function grabs cgroup_mutex and shouldn't
1826 * be used inside locks used by cgroup controller callbacks.
1828 int task_cgroup_path_from_hierarchy(struct task_struct
*task
, int hierarchy_id
,
1829 char *buf
, size_t buflen
)
1831 struct cgroupfs_root
*root
;
1832 struct cgroup
*cgrp
= NULL
;
1835 mutex_lock(&cgroup_mutex
);
1837 root
= idr_find(&cgroup_hierarchy_idr
, hierarchy_id
);
1839 cgrp
= task_cgroup_from_root(task
, root
);
1840 ret
= cgroup_path(cgrp
, buf
, buflen
);
1843 mutex_unlock(&cgroup_mutex
);
1847 EXPORT_SYMBOL_GPL(task_cgroup_path_from_hierarchy
);
1850 * Control Group taskset
1852 struct task_and_cgroup
{
1853 struct task_struct
*task
;
1854 struct cgroup
*cgrp
;
1858 struct cgroup_taskset
{
1859 struct task_and_cgroup single
;
1860 struct flex_array
*tc_array
;
1863 struct cgroup
*cur_cgrp
;
1867 * cgroup_taskset_first - reset taskset and return the first task
1868 * @tset: taskset of interest
1870 * @tset iteration is initialized and the first task is returned.
1872 struct task_struct
*cgroup_taskset_first(struct cgroup_taskset
*tset
)
1874 if (tset
->tc_array
) {
1876 return cgroup_taskset_next(tset
);
1878 tset
->cur_cgrp
= tset
->single
.cgrp
;
1879 return tset
->single
.task
;
1882 EXPORT_SYMBOL_GPL(cgroup_taskset_first
);
1885 * cgroup_taskset_next - iterate to the next task in taskset
1886 * @tset: taskset of interest
1888 * Return the next task in @tset. Iteration must have been initialized
1889 * with cgroup_taskset_first().
1891 struct task_struct
*cgroup_taskset_next(struct cgroup_taskset
*tset
)
1893 struct task_and_cgroup
*tc
;
1895 if (!tset
->tc_array
|| tset
->idx
>= tset
->tc_array_len
)
1898 tc
= flex_array_get(tset
->tc_array
, tset
->idx
++);
1899 tset
->cur_cgrp
= tc
->cgrp
;
1902 EXPORT_SYMBOL_GPL(cgroup_taskset_next
);
1905 * cgroup_taskset_cur_cgroup - return the matching cgroup for the current task
1906 * @tset: taskset of interest
1908 * Return the cgroup for the current (last returned) task of @tset. This
1909 * function must be preceded by either cgroup_taskset_first() or
1910 * cgroup_taskset_next().
1912 struct cgroup
*cgroup_taskset_cur_cgroup(struct cgroup_taskset
*tset
)
1914 return tset
->cur_cgrp
;
1916 EXPORT_SYMBOL_GPL(cgroup_taskset_cur_cgroup
);
1919 * cgroup_taskset_size - return the number of tasks in taskset
1920 * @tset: taskset of interest
1922 int cgroup_taskset_size(struct cgroup_taskset
*tset
)
1924 return tset
->tc_array
? tset
->tc_array_len
: 1;
1926 EXPORT_SYMBOL_GPL(cgroup_taskset_size
);
1930 * cgroup_task_migrate - move a task from one cgroup to another.
1932 * Must be called with cgroup_mutex and threadgroup locked.
1934 static void cgroup_task_migrate(struct cgroup
*old_cgrp
,
1935 struct task_struct
*tsk
,
1936 struct css_set
*new_cset
)
1938 struct css_set
*old_cset
;
1941 * We are synchronized through threadgroup_lock() against PF_EXITING
1942 * setting such that we can't race against cgroup_exit() changing the
1943 * css_set to init_css_set and dropping the old one.
1945 WARN_ON_ONCE(tsk
->flags
& PF_EXITING
);
1946 old_cset
= tsk
->cgroups
;
1949 rcu_assign_pointer(tsk
->cgroups
, new_cset
);
1952 /* Update the css_set linked lists if we're using them */
1953 write_lock(&css_set_lock
);
1954 if (!list_empty(&tsk
->cg_list
))
1955 list_move(&tsk
->cg_list
, &new_cset
->tasks
);
1956 write_unlock(&css_set_lock
);
1959 * We just gained a reference on old_cset by taking it from the
1960 * task. As trading it for new_cset is protected by cgroup_mutex,
1961 * we're safe to drop it here; it will be freed under RCU.
1963 set_bit(CGRP_RELEASABLE
, &old_cgrp
->flags
);
1964 put_css_set(old_cset
);
1968 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
1969 * @cgrp: the cgroup to attach to
1970 * @tsk: the task or the leader of the threadgroup to be attached
1971 * @threadgroup: attach the whole threadgroup?
1973 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
1974 * task_lock of @tsk or each thread in the threadgroup individually in turn.
1976 static int cgroup_attach_task(struct cgroup
*cgrp
, struct task_struct
*tsk
,
1979 int retval
, i
, group_size
;
1980 struct cgroup_subsys
*ss
, *failed_ss
= NULL
;
1981 struct cgroupfs_root
*root
= cgrp
->root
;
1982 /* threadgroup list cursor and array */
1983 struct task_struct
*leader
= tsk
;
1984 struct task_and_cgroup
*tc
;
1985 struct flex_array
*group
;
1986 struct cgroup_taskset tset
= { };
1989 * step 0: in order to do expensive, possibly blocking operations for
1990 * every thread, we cannot iterate the thread group list, since it needs
1991 * rcu or tasklist locked. instead, build an array of all threads in the
1992 * group - group_rwsem prevents new threads from appearing, and if
1993 * threads exit, this will just be an over-estimate.
1996 group_size
= get_nr_threads(tsk
);
1999 /* flex_array supports very large thread-groups better than kmalloc. */
2000 group
= flex_array_alloc(sizeof(*tc
), group_size
, GFP_KERNEL
);
2003 /* pre-allocate to guarantee space while iterating in rcu read-side. */
2004 retval
= flex_array_prealloc(group
, 0, group_size
, GFP_KERNEL
);
2006 goto out_free_group_list
;
2010 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2011 * already PF_EXITING could be freed from underneath us unless we
2012 * take an rcu_read_lock.
2016 struct task_and_cgroup ent
;
2018 /* @tsk either already exited or can't exit until the end */
2019 if (tsk
->flags
& PF_EXITING
)
2022 /* as per above, nr_threads may decrease, but not increase. */
2023 BUG_ON(i
>= group_size
);
2025 ent
.cgrp
= task_cgroup_from_root(tsk
, root
);
2026 /* nothing to do if this task is already in the cgroup */
2027 if (ent
.cgrp
== cgrp
)
2030 * saying GFP_ATOMIC has no effect here because we did prealloc
2031 * earlier, but it's good form to communicate our expectations.
2033 retval
= flex_array_put(group
, i
, &ent
, GFP_ATOMIC
);
2034 BUG_ON(retval
!= 0);
2039 } while_each_thread(leader
, tsk
);
2041 /* remember the number of threads in the array for later. */
2043 tset
.tc_array
= group
;
2044 tset
.tc_array_len
= group_size
;
2046 /* methods shouldn't be called if no task is actually migrating */
2049 goto out_free_group_list
;
2052 * step 1: check that we can legitimately attach to the cgroup.
2054 for_each_root_subsys(root
, ss
) {
2055 if (ss
->can_attach
) {
2056 retval
= ss
->can_attach(cgrp
, &tset
);
2059 goto out_cancel_attach
;
2065 * step 2: make sure css_sets exist for all threads to be migrated.
2066 * we use find_css_set, which allocates a new one if necessary.
2068 for (i
= 0; i
< group_size
; i
++) {
2069 tc
= flex_array_get(group
, i
);
2070 tc
->cg
= find_css_set(tc
->task
->cgroups
, cgrp
);
2073 goto out_put_css_set_refs
;
2078 * step 3: now that we're guaranteed success wrt the css_sets,
2079 * proceed to move all tasks to the new cgroup. There are no
2080 * failure cases after here, so this is the commit point.
2082 for (i
= 0; i
< group_size
; i
++) {
2083 tc
= flex_array_get(group
, i
);
2084 cgroup_task_migrate(tc
->cgrp
, tc
->task
, tc
->cg
);
2086 /* nothing is sensitive to fork() after this point. */
2089 * step 4: do subsystem attach callbacks.
2091 for_each_root_subsys(root
, ss
) {
2093 ss
->attach(cgrp
, &tset
);
2097 * step 5: success! and cleanup
2100 out_put_css_set_refs
:
2102 for (i
= 0; i
< group_size
; i
++) {
2103 tc
= flex_array_get(group
, i
);
2106 put_css_set(tc
->cg
);
2111 for_each_root_subsys(root
, ss
) {
2112 if (ss
== failed_ss
)
2114 if (ss
->cancel_attach
)
2115 ss
->cancel_attach(cgrp
, &tset
);
2118 out_free_group_list
:
2119 flex_array_free(group
);
2124 * Find the task_struct of the task to attach by vpid and pass it along to the
2125 * function to attach either it or all tasks in its threadgroup. Will lock
2126 * cgroup_mutex and threadgroup; may take task_lock of task.
2128 static int attach_task_by_pid(struct cgroup
*cgrp
, u64 pid
, bool threadgroup
)
2130 struct task_struct
*tsk
;
2131 const struct cred
*cred
= current_cred(), *tcred
;
2134 if (!cgroup_lock_live_group(cgrp
))
2140 tsk
= find_task_by_vpid(pid
);
2144 goto out_unlock_cgroup
;
2147 * even if we're attaching all tasks in the thread group, we
2148 * only need to check permissions on one of them.
2150 tcred
= __task_cred(tsk
);
2151 if (!uid_eq(cred
->euid
, GLOBAL_ROOT_UID
) &&
2152 !uid_eq(cred
->euid
, tcred
->uid
) &&
2153 !uid_eq(cred
->euid
, tcred
->suid
)) {
2156 goto out_unlock_cgroup
;
2162 tsk
= tsk
->group_leader
;
2165 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
2166 * trapped in a cpuset, or RT worker may be born in a cgroup
2167 * with no rt_runtime allocated. Just say no.
2169 if (tsk
== kthreadd_task
|| (tsk
->flags
& PF_NO_SETAFFINITY
)) {
2172 goto out_unlock_cgroup
;
2175 get_task_struct(tsk
);
2178 threadgroup_lock(tsk
);
2180 if (!thread_group_leader(tsk
)) {
2182 * a race with de_thread from another thread's exec()
2183 * may strip us of our leadership, if this happens,
2184 * there is no choice but to throw this task away and
2185 * try again; this is
2186 * "double-double-toil-and-trouble-check locking".
2188 threadgroup_unlock(tsk
);
2189 put_task_struct(tsk
);
2190 goto retry_find_task
;
2194 ret
= cgroup_attach_task(cgrp
, tsk
, threadgroup
);
2196 threadgroup_unlock(tsk
);
2198 put_task_struct(tsk
);
2200 mutex_unlock(&cgroup_mutex
);
2205 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2206 * @from: attach to all cgroups of a given task
2207 * @tsk: the task to be attached
2209 int cgroup_attach_task_all(struct task_struct
*from
, struct task_struct
*tsk
)
2211 struct cgroupfs_root
*root
;
2214 mutex_lock(&cgroup_mutex
);
2215 for_each_active_root(root
) {
2216 struct cgroup
*from_cg
= task_cgroup_from_root(from
, root
);
2218 retval
= cgroup_attach_task(from_cg
, tsk
, false);
2222 mutex_unlock(&cgroup_mutex
);
2226 EXPORT_SYMBOL_GPL(cgroup_attach_task_all
);
2228 static int cgroup_tasks_write(struct cgroup
*cgrp
, struct cftype
*cft
, u64 pid
)
2230 return attach_task_by_pid(cgrp
, pid
, false);
2233 static int cgroup_procs_write(struct cgroup
*cgrp
, struct cftype
*cft
, u64 tgid
)
2235 return attach_task_by_pid(cgrp
, tgid
, true);
2238 static int cgroup_release_agent_write(struct cgroup
*cgrp
, struct cftype
*cft
,
2241 BUILD_BUG_ON(sizeof(cgrp
->root
->release_agent_path
) < PATH_MAX
);
2242 if (strlen(buffer
) >= PATH_MAX
)
2244 if (!cgroup_lock_live_group(cgrp
))
2246 mutex_lock(&cgroup_root_mutex
);
2247 strcpy(cgrp
->root
->release_agent_path
, buffer
);
2248 mutex_unlock(&cgroup_root_mutex
);
2249 mutex_unlock(&cgroup_mutex
);
2253 static int cgroup_release_agent_show(struct cgroup
*cgrp
, struct cftype
*cft
,
2254 struct seq_file
*seq
)
2256 if (!cgroup_lock_live_group(cgrp
))
2258 seq_puts(seq
, cgrp
->root
->release_agent_path
);
2259 seq_putc(seq
, '\n');
2260 mutex_unlock(&cgroup_mutex
);
2264 static int cgroup_sane_behavior_show(struct cgroup
*cgrp
, struct cftype
*cft
,
2265 struct seq_file
*seq
)
2267 seq_printf(seq
, "%d\n", cgroup_sane_behavior(cgrp
));
2271 /* A buffer size big enough for numbers or short strings */
2272 #define CGROUP_LOCAL_BUFFER_SIZE 64
2274 static ssize_t
cgroup_write_X64(struct cgroup
*cgrp
, struct cftype
*cft
,
2276 const char __user
*userbuf
,
2277 size_t nbytes
, loff_t
*unused_ppos
)
2279 char buffer
[CGROUP_LOCAL_BUFFER_SIZE
];
2285 if (nbytes
>= sizeof(buffer
))
2287 if (copy_from_user(buffer
, userbuf
, nbytes
))
2290 buffer
[nbytes
] = 0; /* nul-terminate */
2291 if (cft
->write_u64
) {
2292 u64 val
= simple_strtoull(strstrip(buffer
), &end
, 0);
2295 retval
= cft
->write_u64(cgrp
, cft
, val
);
2297 s64 val
= simple_strtoll(strstrip(buffer
), &end
, 0);
2300 retval
= cft
->write_s64(cgrp
, cft
, val
);
2307 static ssize_t
cgroup_write_string(struct cgroup
*cgrp
, struct cftype
*cft
,
2309 const char __user
*userbuf
,
2310 size_t nbytes
, loff_t
*unused_ppos
)
2312 char local_buffer
[CGROUP_LOCAL_BUFFER_SIZE
];
2314 size_t max_bytes
= cft
->max_write_len
;
2315 char *buffer
= local_buffer
;
2318 max_bytes
= sizeof(local_buffer
) - 1;
2319 if (nbytes
>= max_bytes
)
2321 /* Allocate a dynamic buffer if we need one */
2322 if (nbytes
>= sizeof(local_buffer
)) {
2323 buffer
= kmalloc(nbytes
+ 1, GFP_KERNEL
);
2327 if (nbytes
&& copy_from_user(buffer
, userbuf
, nbytes
)) {
2332 buffer
[nbytes
] = 0; /* nul-terminate */
2333 retval
= cft
->write_string(cgrp
, cft
, strstrip(buffer
));
2337 if (buffer
!= local_buffer
)
2342 static ssize_t
cgroup_file_write(struct file
*file
, const char __user
*buf
,
2343 size_t nbytes
, loff_t
*ppos
)
2345 struct cftype
*cft
= __d_cft(file
->f_dentry
);
2346 struct cgroup
*cgrp
= __d_cgrp(file
->f_dentry
->d_parent
);
2348 if (cgroup_is_dead(cgrp
))
2351 return cft
->write(cgrp
, cft
, file
, buf
, nbytes
, ppos
);
2352 if (cft
->write_u64
|| cft
->write_s64
)
2353 return cgroup_write_X64(cgrp
, cft
, file
, buf
, nbytes
, ppos
);
2354 if (cft
->write_string
)
2355 return cgroup_write_string(cgrp
, cft
, file
, buf
, nbytes
, ppos
);
2357 int ret
= cft
->trigger(cgrp
, (unsigned int)cft
->private);
2358 return ret
? ret
: nbytes
;
2363 static ssize_t
cgroup_read_u64(struct cgroup
*cgrp
, struct cftype
*cft
,
2365 char __user
*buf
, size_t nbytes
,
2368 char tmp
[CGROUP_LOCAL_BUFFER_SIZE
];
2369 u64 val
= cft
->read_u64(cgrp
, cft
);
2370 int len
= sprintf(tmp
, "%llu\n", (unsigned long long) val
);
2372 return simple_read_from_buffer(buf
, nbytes
, ppos
, tmp
, len
);
2375 static ssize_t
cgroup_read_s64(struct cgroup
*cgrp
, struct cftype
*cft
,
2377 char __user
*buf
, size_t nbytes
,
2380 char tmp
[CGROUP_LOCAL_BUFFER_SIZE
];
2381 s64 val
= cft
->read_s64(cgrp
, cft
);
2382 int len
= sprintf(tmp
, "%lld\n", (long long) val
);
2384 return simple_read_from_buffer(buf
, nbytes
, ppos
, tmp
, len
);
2387 static ssize_t
cgroup_file_read(struct file
*file
, char __user
*buf
,
2388 size_t nbytes
, loff_t
*ppos
)
2390 struct cftype
*cft
= __d_cft(file
->f_dentry
);
2391 struct cgroup
*cgrp
= __d_cgrp(file
->f_dentry
->d_parent
);
2393 if (cgroup_is_dead(cgrp
))
2397 return cft
->read(cgrp
, cft
, file
, buf
, nbytes
, ppos
);
2399 return cgroup_read_u64(cgrp
, cft
, file
, buf
, nbytes
, ppos
);
2401 return cgroup_read_s64(cgrp
, cft
, file
, buf
, nbytes
, ppos
);
2406 * seqfile ops/methods for returning structured data. Currently just
2407 * supports string->u64 maps, but can be extended in future.
2410 struct cgroup_seqfile_state
{
2412 struct cgroup
*cgroup
;
2415 static int cgroup_map_add(struct cgroup_map_cb
*cb
, const char *key
, u64 value
)
2417 struct seq_file
*sf
= cb
->state
;
2418 return seq_printf(sf
, "%s %llu\n", key
, (unsigned long long)value
);
2421 static int cgroup_seqfile_show(struct seq_file
*m
, void *arg
)
2423 struct cgroup_seqfile_state
*state
= m
->private;
2424 struct cftype
*cft
= state
->cft
;
2425 if (cft
->read_map
) {
2426 struct cgroup_map_cb cb
= {
2427 .fill
= cgroup_map_add
,
2430 return cft
->read_map(state
->cgroup
, cft
, &cb
);
2432 return cft
->read_seq_string(state
->cgroup
, cft
, m
);
2435 static int cgroup_seqfile_release(struct inode
*inode
, struct file
*file
)
2437 struct seq_file
*seq
= file
->private_data
;
2438 kfree(seq
->private);
2439 return single_release(inode
, file
);
2442 static const struct file_operations cgroup_seqfile_operations
= {
2444 .write
= cgroup_file_write
,
2445 .llseek
= seq_lseek
,
2446 .release
= cgroup_seqfile_release
,
2449 static int cgroup_file_open(struct inode
*inode
, struct file
*file
)
2454 err
= generic_file_open(inode
, file
);
2457 cft
= __d_cft(file
->f_dentry
);
2459 if (cft
->read_map
|| cft
->read_seq_string
) {
2460 struct cgroup_seqfile_state
*state
;
2462 state
= kzalloc(sizeof(*state
), GFP_USER
);
2467 state
->cgroup
= __d_cgrp(file
->f_dentry
->d_parent
);
2468 file
->f_op
= &cgroup_seqfile_operations
;
2469 err
= single_open(file
, cgroup_seqfile_show
, state
);
2472 } else if (cft
->open
)
2473 err
= cft
->open(inode
, file
);
2480 static int cgroup_file_release(struct inode
*inode
, struct file
*file
)
2482 struct cftype
*cft
= __d_cft(file
->f_dentry
);
2484 return cft
->release(inode
, file
);
2489 * cgroup_rename - Only allow simple rename of directories in place.
2491 static int cgroup_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
2492 struct inode
*new_dir
, struct dentry
*new_dentry
)
2495 struct cgroup_name
*name
, *old_name
;
2496 struct cgroup
*cgrp
;
2499 * It's convinient to use parent dir's i_mutex to protected
2502 lockdep_assert_held(&old_dir
->i_mutex
);
2504 if (!S_ISDIR(old_dentry
->d_inode
->i_mode
))
2506 if (new_dentry
->d_inode
)
2508 if (old_dir
!= new_dir
)
2511 cgrp
= __d_cgrp(old_dentry
);
2514 * This isn't a proper migration and its usefulness is very
2515 * limited. Disallow if sane_behavior.
2517 if (cgroup_sane_behavior(cgrp
))
2520 name
= cgroup_alloc_name(new_dentry
);
2524 ret
= simple_rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
2530 old_name
= cgrp
->name
;
2531 rcu_assign_pointer(cgrp
->name
, name
);
2533 kfree_rcu(old_name
, rcu_head
);
2537 static struct simple_xattrs
*__d_xattrs(struct dentry
*dentry
)
2539 if (S_ISDIR(dentry
->d_inode
->i_mode
))
2540 return &__d_cgrp(dentry
)->xattrs
;
2542 return &__d_cfe(dentry
)->xattrs
;
2545 static inline int xattr_enabled(struct dentry
*dentry
)
2547 struct cgroupfs_root
*root
= dentry
->d_sb
->s_fs_info
;
2548 return root
->flags
& CGRP_ROOT_XATTR
;
2551 static bool is_valid_xattr(const char *name
)
2553 if (!strncmp(name
, XATTR_TRUSTED_PREFIX
, XATTR_TRUSTED_PREFIX_LEN
) ||
2554 !strncmp(name
, XATTR_SECURITY_PREFIX
, XATTR_SECURITY_PREFIX_LEN
))
2559 static int cgroup_setxattr(struct dentry
*dentry
, const char *name
,
2560 const void *val
, size_t size
, int flags
)
2562 if (!xattr_enabled(dentry
))
2564 if (!is_valid_xattr(name
))
2566 return simple_xattr_set(__d_xattrs(dentry
), name
, val
, size
, flags
);
2569 static int cgroup_removexattr(struct dentry
*dentry
, const char *name
)
2571 if (!xattr_enabled(dentry
))
2573 if (!is_valid_xattr(name
))
2575 return simple_xattr_remove(__d_xattrs(dentry
), name
);
2578 static ssize_t
cgroup_getxattr(struct dentry
*dentry
, const char *name
,
2579 void *buf
, size_t size
)
2581 if (!xattr_enabled(dentry
))
2583 if (!is_valid_xattr(name
))
2585 return simple_xattr_get(__d_xattrs(dentry
), name
, buf
, size
);
2588 static ssize_t
cgroup_listxattr(struct dentry
*dentry
, char *buf
, size_t size
)
2590 if (!xattr_enabled(dentry
))
2592 return simple_xattr_list(__d_xattrs(dentry
), buf
, size
);
2595 static const struct file_operations cgroup_file_operations
= {
2596 .read
= cgroup_file_read
,
2597 .write
= cgroup_file_write
,
2598 .llseek
= generic_file_llseek
,
2599 .open
= cgroup_file_open
,
2600 .release
= cgroup_file_release
,
2603 static const struct inode_operations cgroup_file_inode_operations
= {
2604 .setxattr
= cgroup_setxattr
,
2605 .getxattr
= cgroup_getxattr
,
2606 .listxattr
= cgroup_listxattr
,
2607 .removexattr
= cgroup_removexattr
,
2610 static const struct inode_operations cgroup_dir_inode_operations
= {
2611 .lookup
= cgroup_lookup
,
2612 .mkdir
= cgroup_mkdir
,
2613 .rmdir
= cgroup_rmdir
,
2614 .rename
= cgroup_rename
,
2615 .setxattr
= cgroup_setxattr
,
2616 .getxattr
= cgroup_getxattr
,
2617 .listxattr
= cgroup_listxattr
,
2618 .removexattr
= cgroup_removexattr
,
2621 static struct dentry
*cgroup_lookup(struct inode
*dir
, struct dentry
*dentry
, unsigned int flags
)
2623 if (dentry
->d_name
.len
> NAME_MAX
)
2624 return ERR_PTR(-ENAMETOOLONG
);
2625 d_add(dentry
, NULL
);
2630 * Check if a file is a control file
2632 static inline struct cftype
*__file_cft(struct file
*file
)
2634 if (file_inode(file
)->i_fop
!= &cgroup_file_operations
)
2635 return ERR_PTR(-EINVAL
);
2636 return __d_cft(file
->f_dentry
);
2639 static int cgroup_create_file(struct dentry
*dentry
, umode_t mode
,
2640 struct super_block
*sb
)
2642 struct inode
*inode
;
2646 if (dentry
->d_inode
)
2649 inode
= cgroup_new_inode(mode
, sb
);
2653 if (S_ISDIR(mode
)) {
2654 inode
->i_op
= &cgroup_dir_inode_operations
;
2655 inode
->i_fop
= &simple_dir_operations
;
2657 /* start off with i_nlink == 2 (for "." entry) */
2659 inc_nlink(dentry
->d_parent
->d_inode
);
2662 * Control reaches here with cgroup_mutex held.
2663 * @inode->i_mutex should nest outside cgroup_mutex but we
2664 * want to populate it immediately without releasing
2665 * cgroup_mutex. As @inode isn't visible to anyone else
2666 * yet, trylock will always succeed without affecting
2669 WARN_ON_ONCE(!mutex_trylock(&inode
->i_mutex
));
2670 } else if (S_ISREG(mode
)) {
2672 inode
->i_fop
= &cgroup_file_operations
;
2673 inode
->i_op
= &cgroup_file_inode_operations
;
2675 d_instantiate(dentry
, inode
);
2676 dget(dentry
); /* Extra count - pin the dentry in core */
2681 * cgroup_file_mode - deduce file mode of a control file
2682 * @cft: the control file in question
2684 * returns cft->mode if ->mode is not 0
2685 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2686 * returns S_IRUGO if it has only a read handler
2687 * returns S_IWUSR if it has only a write hander
2689 static umode_t
cgroup_file_mode(const struct cftype
*cft
)
2696 if (cft
->read
|| cft
->read_u64
|| cft
->read_s64
||
2697 cft
->read_map
|| cft
->read_seq_string
)
2700 if (cft
->write
|| cft
->write_u64
|| cft
->write_s64
||
2701 cft
->write_string
|| cft
->trigger
)
2707 static int cgroup_add_file(struct cgroup
*cgrp
, struct cgroup_subsys
*subsys
,
2710 struct dentry
*dir
= cgrp
->dentry
;
2711 struct cgroup
*parent
= __d_cgrp(dir
);
2712 struct dentry
*dentry
;
2716 char name
[MAX_CGROUP_TYPE_NAMELEN
+ MAX_CFTYPE_NAME
+ 2] = { 0 };
2718 if (subsys
&& !(cgrp
->root
->flags
& CGRP_ROOT_NOPREFIX
)) {
2719 strcpy(name
, subsys
->name
);
2722 strcat(name
, cft
->name
);
2724 BUG_ON(!mutex_is_locked(&dir
->d_inode
->i_mutex
));
2726 cfe
= kzalloc(sizeof(*cfe
), GFP_KERNEL
);
2730 dentry
= lookup_one_len(name
, dir
, strlen(name
));
2731 if (IS_ERR(dentry
)) {
2732 error
= PTR_ERR(dentry
);
2736 cfe
->type
= (void *)cft
;
2737 cfe
->dentry
= dentry
;
2738 dentry
->d_fsdata
= cfe
;
2739 simple_xattrs_init(&cfe
->xattrs
);
2741 mode
= cgroup_file_mode(cft
);
2742 error
= cgroup_create_file(dentry
, mode
| S_IFREG
, cgrp
->root
->sb
);
2744 list_add_tail(&cfe
->node
, &parent
->files
);
2753 static int cgroup_addrm_files(struct cgroup
*cgrp
, struct cgroup_subsys
*subsys
,
2754 struct cftype cfts
[], bool is_add
)
2759 for (cft
= cfts
; cft
->name
[0] != '\0'; cft
++) {
2760 /* does cft->flags tell us to skip this file on @cgrp? */
2761 if ((cft
->flags
& CFTYPE_INSANE
) && cgroup_sane_behavior(cgrp
))
2763 if ((cft
->flags
& CFTYPE_NOT_ON_ROOT
) && !cgrp
->parent
)
2765 if ((cft
->flags
& CFTYPE_ONLY_ON_ROOT
) && cgrp
->parent
)
2769 err
= cgroup_add_file(cgrp
, subsys
, cft
);
2771 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
2775 cgroup_rm_file(cgrp
, cft
);
2781 static void cgroup_cfts_prepare(void)
2782 __acquires(&cgroup_mutex
)
2785 * Thanks to the entanglement with vfs inode locking, we can't walk
2786 * the existing cgroups under cgroup_mutex and create files.
2787 * Instead, we use cgroup_for_each_descendant_pre() and drop RCU
2788 * read lock before calling cgroup_addrm_files().
2790 mutex_lock(&cgroup_mutex
);
2793 static void cgroup_cfts_commit(struct cgroup_subsys
*ss
,
2794 struct cftype
*cfts
, bool is_add
)
2795 __releases(&cgroup_mutex
)
2798 struct cgroup
*cgrp
, *root
= &ss
->root
->top_cgroup
;
2799 struct super_block
*sb
= ss
->root
->sb
;
2800 struct dentry
*prev
= NULL
;
2801 struct inode
*inode
;
2804 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
2805 if (!cfts
|| ss
->root
== &cgroup_dummy_root
||
2806 !atomic_inc_not_zero(&sb
->s_active
)) {
2807 mutex_unlock(&cgroup_mutex
);
2812 * All cgroups which are created after we drop cgroup_mutex will
2813 * have the updated set of files, so we only need to update the
2814 * cgroups created before the current @cgroup_serial_nr_next.
2816 update_before
= cgroup_serial_nr_next
;
2818 mutex_unlock(&cgroup_mutex
);
2820 /* @root always needs to be updated */
2821 inode
= root
->dentry
->d_inode
;
2822 mutex_lock(&inode
->i_mutex
);
2823 mutex_lock(&cgroup_mutex
);
2824 cgroup_addrm_files(root
, ss
, cfts
, is_add
);
2825 mutex_unlock(&cgroup_mutex
);
2826 mutex_unlock(&inode
->i_mutex
);
2828 /* add/rm files for all cgroups created before */
2830 cgroup_for_each_descendant_pre(cgrp
, root
) {
2831 if (cgroup_is_dead(cgrp
))
2834 inode
= cgrp
->dentry
->d_inode
;
2839 prev
= cgrp
->dentry
;
2841 mutex_lock(&inode
->i_mutex
);
2842 mutex_lock(&cgroup_mutex
);
2843 if (cgrp
->serial_nr
< update_before
&& !cgroup_is_dead(cgrp
))
2844 cgroup_addrm_files(cgrp
, ss
, cfts
, is_add
);
2845 mutex_unlock(&cgroup_mutex
);
2846 mutex_unlock(&inode
->i_mutex
);
2852 deactivate_super(sb
);
2856 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2857 * @ss: target cgroup subsystem
2858 * @cfts: zero-length name terminated array of cftypes
2860 * Register @cfts to @ss. Files described by @cfts are created for all
2861 * existing cgroups to which @ss is attached and all future cgroups will
2862 * have them too. This function can be called anytime whether @ss is
2865 * Returns 0 on successful registration, -errno on failure. Note that this
2866 * function currently returns 0 as long as @cfts registration is successful
2867 * even if some file creation attempts on existing cgroups fail.
2869 int cgroup_add_cftypes(struct cgroup_subsys
*ss
, struct cftype
*cfts
)
2871 struct cftype_set
*set
;
2873 set
= kzalloc(sizeof(*set
), GFP_KERNEL
);
2877 cgroup_cfts_prepare();
2879 list_add_tail(&set
->node
, &ss
->cftsets
);
2880 cgroup_cfts_commit(ss
, cfts
, true);
2884 EXPORT_SYMBOL_GPL(cgroup_add_cftypes
);
2887 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
2888 * @ss: target cgroup subsystem
2889 * @cfts: zero-length name terminated array of cftypes
2891 * Unregister @cfts from @ss. Files described by @cfts are removed from
2892 * all existing cgroups to which @ss is attached and all future cgroups
2893 * won't have them either. This function can be called anytime whether @ss
2894 * is attached or not.
2896 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
2897 * registered with @ss.
2899 int cgroup_rm_cftypes(struct cgroup_subsys
*ss
, struct cftype
*cfts
)
2901 struct cftype_set
*set
;
2903 cgroup_cfts_prepare();
2905 list_for_each_entry(set
, &ss
->cftsets
, node
) {
2906 if (set
->cfts
== cfts
) {
2907 list_del(&set
->node
);
2909 cgroup_cfts_commit(ss
, cfts
, false);
2914 cgroup_cfts_commit(ss
, NULL
, false);
2919 * cgroup_task_count - count the number of tasks in a cgroup.
2920 * @cgrp: the cgroup in question
2922 * Return the number of tasks in the cgroup.
2924 int cgroup_task_count(const struct cgroup
*cgrp
)
2927 struct cgrp_cset_link
*link
;
2929 read_lock(&css_set_lock
);
2930 list_for_each_entry(link
, &cgrp
->cset_links
, cset_link
)
2931 count
+= atomic_read(&link
->cset
->refcount
);
2932 read_unlock(&css_set_lock
);
2937 * Advance a list_head iterator. The iterator should be positioned at
2938 * the start of a css_set
2940 static void cgroup_advance_iter(struct cgroup
*cgrp
, struct cgroup_iter
*it
)
2942 struct list_head
*l
= it
->cset_link
;
2943 struct cgrp_cset_link
*link
;
2944 struct css_set
*cset
;
2946 /* Advance to the next non-empty css_set */
2949 if (l
== &cgrp
->cset_links
) {
2950 it
->cset_link
= NULL
;
2953 link
= list_entry(l
, struct cgrp_cset_link
, cset_link
);
2955 } while (list_empty(&cset
->tasks
));
2957 it
->task
= cset
->tasks
.next
;
2961 * To reduce the fork() overhead for systems that are not actually
2962 * using their cgroups capability, we don't maintain the lists running
2963 * through each css_set to its tasks until we see the list actually
2964 * used - in other words after the first call to cgroup_iter_start().
2966 static void cgroup_enable_task_cg_lists(void)
2968 struct task_struct
*p
, *g
;
2969 write_lock(&css_set_lock
);
2970 use_task_css_set_links
= 1;
2972 * We need tasklist_lock because RCU is not safe against
2973 * while_each_thread(). Besides, a forking task that has passed
2974 * cgroup_post_fork() without seeing use_task_css_set_links = 1
2975 * is not guaranteed to have its child immediately visible in the
2976 * tasklist if we walk through it with RCU.
2978 read_lock(&tasklist_lock
);
2979 do_each_thread(g
, p
) {
2982 * We should check if the process is exiting, otherwise
2983 * it will race with cgroup_exit() in that the list
2984 * entry won't be deleted though the process has exited.
2986 if (!(p
->flags
& PF_EXITING
) && list_empty(&p
->cg_list
))
2987 list_add(&p
->cg_list
, &p
->cgroups
->tasks
);
2989 } while_each_thread(g
, p
);
2990 read_unlock(&tasklist_lock
);
2991 write_unlock(&css_set_lock
);
2995 * cgroup_next_sibling - find the next sibling of a given cgroup
2996 * @pos: the current cgroup
2998 * This function returns the next sibling of @pos and should be called
2999 * under RCU read lock. The only requirement is that @pos is accessible.
3000 * The next sibling is guaranteed to be returned regardless of @pos's
3003 struct cgroup
*cgroup_next_sibling(struct cgroup
*pos
)
3005 struct cgroup
*next
;
3007 WARN_ON_ONCE(!rcu_read_lock_held());
3010 * @pos could already have been removed. Once a cgroup is removed,
3011 * its ->sibling.next is no longer updated when its next sibling
3012 * changes. As CGRP_DEAD assertion is serialized and happens
3013 * before the cgroup is taken off the ->sibling list, if we see it
3014 * unasserted, it's guaranteed that the next sibling hasn't
3015 * finished its grace period even if it's already removed, and thus
3016 * safe to dereference from this RCU critical section. If
3017 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
3018 * to be visible as %true here.
3020 if (likely(!cgroup_is_dead(pos
))) {
3021 next
= list_entry_rcu(pos
->sibling
.next
, struct cgroup
, sibling
);
3022 if (&next
->sibling
!= &pos
->parent
->children
)
3028 * Can't dereference the next pointer. Each cgroup is given a
3029 * monotonically increasing unique serial number and always
3030 * appended to the sibling list, so the next one can be found by
3031 * walking the parent's children until we see a cgroup with higher
3032 * serial number than @pos's.
3034 * While this path can be slow, it's taken only when either the
3035 * current cgroup is removed or iteration and removal race.
3037 list_for_each_entry_rcu(next
, &pos
->parent
->children
, sibling
)
3038 if (next
->serial_nr
> pos
->serial_nr
)
3042 EXPORT_SYMBOL_GPL(cgroup_next_sibling
);
3045 * cgroup_next_descendant_pre - find the next descendant for pre-order walk
3046 * @pos: the current position (%NULL to initiate traversal)
3047 * @cgroup: cgroup whose descendants to walk
3049 * To be used by cgroup_for_each_descendant_pre(). Find the next
3050 * descendant to visit for pre-order traversal of @cgroup's descendants.
3052 * While this function requires RCU read locking, it doesn't require the
3053 * whole traversal to be contained in a single RCU critical section. This
3054 * function will return the correct next descendant as long as both @pos
3055 * and @cgroup are accessible and @pos is a descendant of @cgroup.
3057 struct cgroup
*cgroup_next_descendant_pre(struct cgroup
*pos
,
3058 struct cgroup
*cgroup
)
3060 struct cgroup
*next
;
3062 WARN_ON_ONCE(!rcu_read_lock_held());
3064 /* if first iteration, pretend we just visited @cgroup */
3068 /* visit the first child if exists */
3069 next
= list_first_or_null_rcu(&pos
->children
, struct cgroup
, sibling
);
3073 /* no child, visit my or the closest ancestor's next sibling */
3074 while (pos
!= cgroup
) {
3075 next
= cgroup_next_sibling(pos
);
3083 EXPORT_SYMBOL_GPL(cgroup_next_descendant_pre
);
3086 * cgroup_rightmost_descendant - return the rightmost descendant of a cgroup
3087 * @pos: cgroup of interest
3089 * Return the rightmost descendant of @pos. If there's no descendant,
3090 * @pos is returned. This can be used during pre-order traversal to skip
3093 * While this function requires RCU read locking, it doesn't require the
3094 * whole traversal to be contained in a single RCU critical section. This
3095 * function will return the correct rightmost descendant as long as @pos is
3098 struct cgroup
*cgroup_rightmost_descendant(struct cgroup
*pos
)
3100 struct cgroup
*last
, *tmp
;
3102 WARN_ON_ONCE(!rcu_read_lock_held());
3106 /* ->prev isn't RCU safe, walk ->next till the end */
3108 list_for_each_entry_rcu(tmp
, &last
->children
, sibling
)
3114 EXPORT_SYMBOL_GPL(cgroup_rightmost_descendant
);
3116 static struct cgroup
*cgroup_leftmost_descendant(struct cgroup
*pos
)
3118 struct cgroup
*last
;
3122 pos
= list_first_or_null_rcu(&pos
->children
, struct cgroup
,
3130 * cgroup_next_descendant_post - find the next descendant for post-order walk
3131 * @pos: the current position (%NULL to initiate traversal)
3132 * @cgroup: cgroup whose descendants to walk
3134 * To be used by cgroup_for_each_descendant_post(). Find the next
3135 * descendant to visit for post-order traversal of @cgroup's descendants.
3137 * While this function requires RCU read locking, it doesn't require the
3138 * whole traversal to be contained in a single RCU critical section. This
3139 * function will return the correct next descendant as long as both @pos
3140 * and @cgroup are accessible and @pos is a descendant of @cgroup.
3142 struct cgroup
*cgroup_next_descendant_post(struct cgroup
*pos
,
3143 struct cgroup
*cgroup
)
3145 struct cgroup
*next
;
3147 WARN_ON_ONCE(!rcu_read_lock_held());
3149 /* if first iteration, visit the leftmost descendant */
3151 next
= cgroup_leftmost_descendant(cgroup
);
3152 return next
!= cgroup
? next
: NULL
;
3155 /* if there's an unvisited sibling, visit its leftmost descendant */
3156 next
= cgroup_next_sibling(pos
);
3158 return cgroup_leftmost_descendant(next
);
3160 /* no sibling left, visit parent */
3162 return next
!= cgroup
? next
: NULL
;
3164 EXPORT_SYMBOL_GPL(cgroup_next_descendant_post
);
3166 void cgroup_iter_start(struct cgroup
*cgrp
, struct cgroup_iter
*it
)
3167 __acquires(css_set_lock
)
3170 * The first time anyone tries to iterate across a cgroup,
3171 * we need to enable the list linking each css_set to its
3172 * tasks, and fix up all existing tasks.
3174 if (!use_task_css_set_links
)
3175 cgroup_enable_task_cg_lists();
3177 read_lock(&css_set_lock
);
3178 it
->cset_link
= &cgrp
->cset_links
;
3179 cgroup_advance_iter(cgrp
, it
);
3182 struct task_struct
*cgroup_iter_next(struct cgroup
*cgrp
,
3183 struct cgroup_iter
*it
)
3185 struct task_struct
*res
;
3186 struct list_head
*l
= it
->task
;
3187 struct cgrp_cset_link
*link
;
3189 /* If the iterator cg is NULL, we have no tasks */
3192 res
= list_entry(l
, struct task_struct
, cg_list
);
3193 /* Advance iterator to find next entry */
3195 link
= list_entry(it
->cset_link
, struct cgrp_cset_link
, cset_link
);
3196 if (l
== &link
->cset
->tasks
) {
3197 /* We reached the end of this task list - move on to
3198 * the next cg_cgroup_link */
3199 cgroup_advance_iter(cgrp
, it
);
3206 void cgroup_iter_end(struct cgroup
*cgrp
, struct cgroup_iter
*it
)
3207 __releases(css_set_lock
)
3209 read_unlock(&css_set_lock
);
3212 static inline int started_after_time(struct task_struct
*t1
,
3213 struct timespec
*time
,
3214 struct task_struct
*t2
)
3216 int start_diff
= timespec_compare(&t1
->start_time
, time
);
3217 if (start_diff
> 0) {
3219 } else if (start_diff
< 0) {
3223 * Arbitrarily, if two processes started at the same
3224 * time, we'll say that the lower pointer value
3225 * started first. Note that t2 may have exited by now
3226 * so this may not be a valid pointer any longer, but
3227 * that's fine - it still serves to distinguish
3228 * between two tasks started (effectively) simultaneously.
3235 * This function is a callback from heap_insert() and is used to order
3237 * In this case we order the heap in descending task start time.
3239 static inline int started_after(void *p1
, void *p2
)
3241 struct task_struct
*t1
= p1
;
3242 struct task_struct
*t2
= p2
;
3243 return started_after_time(t1
, &t2
->start_time
, t2
);
3247 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
3248 * @scan: struct cgroup_scanner containing arguments for the scan
3250 * Arguments include pointers to callback functions test_task() and
3252 * Iterate through all the tasks in a cgroup, calling test_task() for each,
3253 * and if it returns true, call process_task() for it also.
3254 * The test_task pointer may be NULL, meaning always true (select all tasks).
3255 * Effectively duplicates cgroup_iter_{start,next,end}()
3256 * but does not lock css_set_lock for the call to process_task().
3257 * The struct cgroup_scanner may be embedded in any structure of the caller's
3259 * It is guaranteed that process_task() will act on every task that
3260 * is a member of the cgroup for the duration of this call. This
3261 * function may or may not call process_task() for tasks that exit
3262 * or move to a different cgroup during the call, or are forked or
3263 * move into the cgroup during the call.
3265 * Note that test_task() may be called with locks held, and may in some
3266 * situations be called multiple times for the same task, so it should
3268 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
3269 * pre-allocated and will be used for heap operations (and its "gt" member will
3270 * be overwritten), else a temporary heap will be used (allocation of which
3271 * may cause this function to fail).
3273 int cgroup_scan_tasks(struct cgroup_scanner
*scan
)
3276 struct cgroup_iter it
;
3277 struct task_struct
*p
, *dropped
;
3278 /* Never dereference latest_task, since it's not refcounted */
3279 struct task_struct
*latest_task
= NULL
;
3280 struct ptr_heap tmp_heap
;
3281 struct ptr_heap
*heap
;
3282 struct timespec latest_time
= { 0, 0 };
3285 /* The caller supplied our heap and pre-allocated its memory */
3287 heap
->gt
= &started_after
;
3289 /* We need to allocate our own heap memory */
3291 retval
= heap_init(heap
, PAGE_SIZE
, GFP_KERNEL
, &started_after
);
3293 /* cannot allocate the heap */
3299 * Scan tasks in the cgroup, using the scanner's "test_task" callback
3300 * to determine which are of interest, and using the scanner's
3301 * "process_task" callback to process any of them that need an update.
3302 * Since we don't want to hold any locks during the task updates,
3303 * gather tasks to be processed in a heap structure.
3304 * The heap is sorted by descending task start time.
3305 * If the statically-sized heap fills up, we overflow tasks that
3306 * started later, and in future iterations only consider tasks that
3307 * started after the latest task in the previous pass. This
3308 * guarantees forward progress and that we don't miss any tasks.
3311 cgroup_iter_start(scan
->cg
, &it
);
3312 while ((p
= cgroup_iter_next(scan
->cg
, &it
))) {
3314 * Only affect tasks that qualify per the caller's callback,
3315 * if he provided one
3317 if (scan
->test_task
&& !scan
->test_task(p
, scan
))
3320 * Only process tasks that started after the last task
3323 if (!started_after_time(p
, &latest_time
, latest_task
))
3325 dropped
= heap_insert(heap
, p
);
3326 if (dropped
== NULL
) {
3328 * The new task was inserted; the heap wasn't
3332 } else if (dropped
!= p
) {
3334 * The new task was inserted, and pushed out a
3338 put_task_struct(dropped
);
3341 * Else the new task was newer than anything already in
3342 * the heap and wasn't inserted
3345 cgroup_iter_end(scan
->cg
, &it
);
3348 for (i
= 0; i
< heap
->size
; i
++) {
3349 struct task_struct
*q
= heap
->ptrs
[i
];
3351 latest_time
= q
->start_time
;
3354 /* Process the task per the caller's callback */
3355 scan
->process_task(q
, scan
);
3359 * If we had to process any tasks at all, scan again
3360 * in case some of them were in the middle of forking
3361 * children that didn't get processed.
3362 * Not the most efficient way to do it, but it avoids
3363 * having to take callback_mutex in the fork path
3367 if (heap
== &tmp_heap
)
3368 heap_free(&tmp_heap
);
3372 static void cgroup_transfer_one_task(struct task_struct
*task
,
3373 struct cgroup_scanner
*scan
)
3375 struct cgroup
*new_cgroup
= scan
->data
;
3377 mutex_lock(&cgroup_mutex
);
3378 cgroup_attach_task(new_cgroup
, task
, false);
3379 mutex_unlock(&cgroup_mutex
);
3383 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3384 * @to: cgroup to which the tasks will be moved
3385 * @from: cgroup in which the tasks currently reside
3387 int cgroup_transfer_tasks(struct cgroup
*to
, struct cgroup
*from
)
3389 struct cgroup_scanner scan
;
3392 scan
.test_task
= NULL
; /* select all tasks in cgroup */
3393 scan
.process_task
= cgroup_transfer_one_task
;
3397 return cgroup_scan_tasks(&scan
);
3401 * Stuff for reading the 'tasks'/'procs' files.
3403 * Reading this file can return large amounts of data if a cgroup has
3404 * *lots* of attached tasks. So it may need several calls to read(),
3405 * but we cannot guarantee that the information we produce is correct
3406 * unless we produce it entirely atomically.
3410 /* which pidlist file are we talking about? */
3411 enum cgroup_filetype
{
3417 * A pidlist is a list of pids that virtually represents the contents of one
3418 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3419 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3422 struct cgroup_pidlist
{
3424 * used to find which pidlist is wanted. doesn't change as long as
3425 * this particular list stays in the list.
3427 struct { enum cgroup_filetype type
; struct pid_namespace
*ns
; } key
;
3430 /* how many elements the above list has */
3432 /* how many files are using the current array */
3434 /* each of these stored in a list by its cgroup */
3435 struct list_head links
;
3436 /* pointer to the cgroup we belong to, for list removal purposes */
3437 struct cgroup
*owner
;
3438 /* protects the other fields */
3439 struct rw_semaphore mutex
;
3443 * The following two functions "fix" the issue where there are more pids
3444 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3445 * TODO: replace with a kernel-wide solution to this problem
3447 #define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3448 static void *pidlist_allocate(int count
)
3450 if (PIDLIST_TOO_LARGE(count
))
3451 return vmalloc(count
* sizeof(pid_t
));
3453 return kmalloc(count
* sizeof(pid_t
), GFP_KERNEL
);
3455 static void pidlist_free(void *p
)
3457 if (is_vmalloc_addr(p
))
3464 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
3465 * Returns the number of unique elements.
3467 static int pidlist_uniq(pid_t
*list
, int length
)
3472 * we presume the 0th element is unique, so i starts at 1. trivial
3473 * edge cases first; no work needs to be done for either
3475 if (length
== 0 || length
== 1)
3477 /* src and dest walk down the list; dest counts unique elements */
3478 for (src
= 1; src
< length
; src
++) {
3479 /* find next unique element */
3480 while (list
[src
] == list
[src
-1]) {
3485 /* dest always points to where the next unique element goes */
3486 list
[dest
] = list
[src
];
3493 static int cmppid(const void *a
, const void *b
)
3495 return *(pid_t
*)a
- *(pid_t
*)b
;
3499 * find the appropriate pidlist for our purpose (given procs vs tasks)
3500 * returns with the lock on that pidlist already held, and takes care
3501 * of the use count, or returns NULL with no locks held if we're out of
3504 static struct cgroup_pidlist
*cgroup_pidlist_find(struct cgroup
*cgrp
,
3505 enum cgroup_filetype type
)
3507 struct cgroup_pidlist
*l
;
3508 /* don't need task_nsproxy() if we're looking at ourself */
3509 struct pid_namespace
*ns
= task_active_pid_ns(current
);
3512 * We can't drop the pidlist_mutex before taking the l->mutex in case
3513 * the last ref-holder is trying to remove l from the list at the same
3514 * time. Holding the pidlist_mutex precludes somebody taking whichever
3515 * list we find out from under us - compare release_pid_array().
3517 mutex_lock(&cgrp
->pidlist_mutex
);
3518 list_for_each_entry(l
, &cgrp
->pidlists
, links
) {
3519 if (l
->key
.type
== type
&& l
->key
.ns
== ns
) {
3520 /* make sure l doesn't vanish out from under us */
3521 down_write(&l
->mutex
);
3522 mutex_unlock(&cgrp
->pidlist_mutex
);
3526 /* entry not found; create a new one */
3527 l
= kzalloc(sizeof(struct cgroup_pidlist
), GFP_KERNEL
);
3529 mutex_unlock(&cgrp
->pidlist_mutex
);
3532 init_rwsem(&l
->mutex
);
3533 down_write(&l
->mutex
);
3535 l
->key
.ns
= get_pid_ns(ns
);
3537 list_add(&l
->links
, &cgrp
->pidlists
);
3538 mutex_unlock(&cgrp
->pidlist_mutex
);
3543 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3545 static int pidlist_array_load(struct cgroup
*cgrp
, enum cgroup_filetype type
,
3546 struct cgroup_pidlist
**lp
)
3550 int pid
, n
= 0; /* used for populating the array */
3551 struct cgroup_iter it
;
3552 struct task_struct
*tsk
;
3553 struct cgroup_pidlist
*l
;
3556 * If cgroup gets more users after we read count, we won't have
3557 * enough space - tough. This race is indistinguishable to the
3558 * caller from the case that the additional cgroup users didn't
3559 * show up until sometime later on.
3561 length
= cgroup_task_count(cgrp
);
3562 array
= pidlist_allocate(length
);
3565 /* now, populate the array */
3566 cgroup_iter_start(cgrp
, &it
);
3567 while ((tsk
= cgroup_iter_next(cgrp
, &it
))) {
3568 if (unlikely(n
== length
))
3570 /* get tgid or pid for procs or tasks file respectively */
3571 if (type
== CGROUP_FILE_PROCS
)
3572 pid
= task_tgid_vnr(tsk
);
3574 pid
= task_pid_vnr(tsk
);
3575 if (pid
> 0) /* make sure to only use valid results */
3578 cgroup_iter_end(cgrp
, &it
);
3580 /* now sort & (if procs) strip out duplicates */
3581 sort(array
, length
, sizeof(pid_t
), cmppid
, NULL
);
3582 if (type
== CGROUP_FILE_PROCS
)
3583 length
= pidlist_uniq(array
, length
);
3584 l
= cgroup_pidlist_find(cgrp
, type
);
3586 pidlist_free(array
);
3589 /* store array, freeing old if necessary - lock already held */
3590 pidlist_free(l
->list
);
3594 up_write(&l
->mutex
);
3600 * cgroupstats_build - build and fill cgroupstats
3601 * @stats: cgroupstats to fill information into
3602 * @dentry: A dentry entry belonging to the cgroup for which stats have
3605 * Build and fill cgroupstats so that taskstats can export it to user
3608 int cgroupstats_build(struct cgroupstats
*stats
, struct dentry
*dentry
)
3611 struct cgroup
*cgrp
;
3612 struct cgroup_iter it
;
3613 struct task_struct
*tsk
;
3616 * Validate dentry by checking the superblock operations,
3617 * and make sure it's a directory.
3619 if (dentry
->d_sb
->s_op
!= &cgroup_ops
||
3620 !S_ISDIR(dentry
->d_inode
->i_mode
))
3624 cgrp
= dentry
->d_fsdata
;
3626 cgroup_iter_start(cgrp
, &it
);
3627 while ((tsk
= cgroup_iter_next(cgrp
, &it
))) {
3628 switch (tsk
->state
) {
3630 stats
->nr_running
++;
3632 case TASK_INTERRUPTIBLE
:
3633 stats
->nr_sleeping
++;
3635 case TASK_UNINTERRUPTIBLE
:
3636 stats
->nr_uninterruptible
++;
3639 stats
->nr_stopped
++;
3642 if (delayacct_is_task_waiting_on_io(tsk
))
3643 stats
->nr_io_wait
++;
3647 cgroup_iter_end(cgrp
, &it
);
3655 * seq_file methods for the tasks/procs files. The seq_file position is the
3656 * next pid to display; the seq_file iterator is a pointer to the pid
3657 * in the cgroup->l->list array.
3660 static void *cgroup_pidlist_start(struct seq_file
*s
, loff_t
*pos
)
3663 * Initially we receive a position value that corresponds to
3664 * one more than the last pid shown (or 0 on the first call or
3665 * after a seek to the start). Use a binary-search to find the
3666 * next pid to display, if any
3668 struct cgroup_pidlist
*l
= s
->private;
3669 int index
= 0, pid
= *pos
;
3672 down_read(&l
->mutex
);
3674 int end
= l
->length
;
3676 while (index
< end
) {
3677 int mid
= (index
+ end
) / 2;
3678 if (l
->list
[mid
] == pid
) {
3681 } else if (l
->list
[mid
] <= pid
)
3687 /* If we're off the end of the array, we're done */
3688 if (index
>= l
->length
)
3690 /* Update the abstract position to be the actual pid that we found */
3691 iter
= l
->list
+ index
;
3696 static void cgroup_pidlist_stop(struct seq_file
*s
, void *v
)
3698 struct cgroup_pidlist
*l
= s
->private;
3702 static void *cgroup_pidlist_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
3704 struct cgroup_pidlist
*l
= s
->private;
3706 pid_t
*end
= l
->list
+ l
->length
;
3708 * Advance to the next pid in the array. If this goes off the
3720 static int cgroup_pidlist_show(struct seq_file
*s
, void *v
)
3722 return seq_printf(s
, "%d\n", *(int *)v
);
3726 * seq_operations functions for iterating on pidlists through seq_file -
3727 * independent of whether it's tasks or procs
3729 static const struct seq_operations cgroup_pidlist_seq_operations
= {
3730 .start
= cgroup_pidlist_start
,
3731 .stop
= cgroup_pidlist_stop
,
3732 .next
= cgroup_pidlist_next
,
3733 .show
= cgroup_pidlist_show
,
3736 static void cgroup_release_pid_array(struct cgroup_pidlist
*l
)
3739 * the case where we're the last user of this particular pidlist will
3740 * have us remove it from the cgroup's list, which entails taking the
3741 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3742 * pidlist_mutex, we have to take pidlist_mutex first.
3744 mutex_lock(&l
->owner
->pidlist_mutex
);
3745 down_write(&l
->mutex
);
3746 BUG_ON(!l
->use_count
);
3747 if (!--l
->use_count
) {
3748 /* we're the last user if refcount is 0; remove and free */
3749 list_del(&l
->links
);
3750 mutex_unlock(&l
->owner
->pidlist_mutex
);
3751 pidlist_free(l
->list
);
3752 put_pid_ns(l
->key
.ns
);
3753 up_write(&l
->mutex
);
3757 mutex_unlock(&l
->owner
->pidlist_mutex
);
3758 up_write(&l
->mutex
);
3761 static int cgroup_pidlist_release(struct inode
*inode
, struct file
*file
)
3763 struct cgroup_pidlist
*l
;
3764 if (!(file
->f_mode
& FMODE_READ
))
3767 * the seq_file will only be initialized if the file was opened for
3768 * reading; hence we check if it's not null only in that case.
3770 l
= ((struct seq_file
*)file
->private_data
)->private;
3771 cgroup_release_pid_array(l
);
3772 return seq_release(inode
, file
);
3775 static const struct file_operations cgroup_pidlist_operations
= {
3777 .llseek
= seq_lseek
,
3778 .write
= cgroup_file_write
,
3779 .release
= cgroup_pidlist_release
,
3783 * The following functions handle opens on a file that displays a pidlist
3784 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3787 /* helper function for the two below it */
3788 static int cgroup_pidlist_open(struct file
*file
, enum cgroup_filetype type
)
3790 struct cgroup
*cgrp
= __d_cgrp(file
->f_dentry
->d_parent
);
3791 struct cgroup_pidlist
*l
;
3794 /* Nothing to do for write-only files */
3795 if (!(file
->f_mode
& FMODE_READ
))
3798 /* have the array populated */
3799 retval
= pidlist_array_load(cgrp
, type
, &l
);
3802 /* configure file information */
3803 file
->f_op
= &cgroup_pidlist_operations
;
3805 retval
= seq_open(file
, &cgroup_pidlist_seq_operations
);
3807 cgroup_release_pid_array(l
);
3810 ((struct seq_file
*)file
->private_data
)->private = l
;
3813 static int cgroup_tasks_open(struct inode
*unused
, struct file
*file
)
3815 return cgroup_pidlist_open(file
, CGROUP_FILE_TASKS
);
3817 static int cgroup_procs_open(struct inode
*unused
, struct file
*file
)
3819 return cgroup_pidlist_open(file
, CGROUP_FILE_PROCS
);
3822 static u64
cgroup_read_notify_on_release(struct cgroup
*cgrp
,
3825 return notify_on_release(cgrp
);
3828 static int cgroup_write_notify_on_release(struct cgroup
*cgrp
,
3832 clear_bit(CGRP_RELEASABLE
, &cgrp
->flags
);
3834 set_bit(CGRP_NOTIFY_ON_RELEASE
, &cgrp
->flags
);
3836 clear_bit(CGRP_NOTIFY_ON_RELEASE
, &cgrp
->flags
);
3841 * When dput() is called asynchronously, if umount has been done and
3842 * then deactivate_super() in cgroup_free_fn() kills the superblock,
3843 * there's a small window that vfs will see the root dentry with non-zero
3844 * refcnt and trigger BUG().
3846 * That's why we hold a reference before dput() and drop it right after.
3848 static void cgroup_dput(struct cgroup
*cgrp
)
3850 struct super_block
*sb
= cgrp
->root
->sb
;
3852 atomic_inc(&sb
->s_active
);
3854 deactivate_super(sb
);
3858 * Unregister event and free resources.
3860 * Gets called from workqueue.
3862 static void cgroup_event_remove(struct work_struct
*work
)
3864 struct cgroup_event
*event
= container_of(work
, struct cgroup_event
,
3866 struct cgroup
*cgrp
= event
->cgrp
;
3868 remove_wait_queue(event
->wqh
, &event
->wait
);
3870 event
->cft
->unregister_event(cgrp
, event
->cft
, event
->eventfd
);
3872 /* Notify userspace the event is going away. */
3873 eventfd_signal(event
->eventfd
, 1);
3875 eventfd_ctx_put(event
->eventfd
);
3881 * Gets called on POLLHUP on eventfd when user closes it.
3883 * Called with wqh->lock held and interrupts disabled.
3885 static int cgroup_event_wake(wait_queue_t
*wait
, unsigned mode
,
3886 int sync
, void *key
)
3888 struct cgroup_event
*event
= container_of(wait
,
3889 struct cgroup_event
, wait
);
3890 struct cgroup
*cgrp
= event
->cgrp
;
3891 unsigned long flags
= (unsigned long)key
;
3893 if (flags
& POLLHUP
) {
3895 * If the event has been detached at cgroup removal, we
3896 * can simply return knowing the other side will cleanup
3899 * We can't race against event freeing since the other
3900 * side will require wqh->lock via remove_wait_queue(),
3903 spin_lock(&cgrp
->event_list_lock
);
3904 if (!list_empty(&event
->list
)) {
3905 list_del_init(&event
->list
);
3907 * We are in atomic context, but cgroup_event_remove()
3908 * may sleep, so we have to call it in workqueue.
3910 schedule_work(&event
->remove
);
3912 spin_unlock(&cgrp
->event_list_lock
);
3918 static void cgroup_event_ptable_queue_proc(struct file
*file
,
3919 wait_queue_head_t
*wqh
, poll_table
*pt
)
3921 struct cgroup_event
*event
= container_of(pt
,
3922 struct cgroup_event
, pt
);
3925 add_wait_queue(wqh
, &event
->wait
);
3929 * Parse input and register new cgroup event handler.
3931 * Input must be in format '<event_fd> <control_fd> <args>'.
3932 * Interpretation of args is defined by control file implementation.
3934 static int cgroup_write_event_control(struct cgroup
*cgrp
, struct cftype
*cft
,
3937 struct cgroup_event
*event
= NULL
;
3938 struct cgroup
*cgrp_cfile
;
3939 unsigned int efd
, cfd
;
3940 struct file
*efile
= NULL
;
3941 struct file
*cfile
= NULL
;
3945 efd
= simple_strtoul(buffer
, &endp
, 10);
3950 cfd
= simple_strtoul(buffer
, &endp
, 10);
3951 if ((*endp
!= ' ') && (*endp
!= '\0'))
3955 event
= kzalloc(sizeof(*event
), GFP_KERNEL
);
3959 INIT_LIST_HEAD(&event
->list
);
3960 init_poll_funcptr(&event
->pt
, cgroup_event_ptable_queue_proc
);
3961 init_waitqueue_func_entry(&event
->wait
, cgroup_event_wake
);
3962 INIT_WORK(&event
->remove
, cgroup_event_remove
);
3964 efile
= eventfd_fget(efd
);
3965 if (IS_ERR(efile
)) {
3966 ret
= PTR_ERR(efile
);
3970 event
->eventfd
= eventfd_ctx_fileget(efile
);
3971 if (IS_ERR(event
->eventfd
)) {
3972 ret
= PTR_ERR(event
->eventfd
);
3982 /* the process need read permission on control file */
3983 /* AV: shouldn't we check that it's been opened for read instead? */
3984 ret
= inode_permission(file_inode(cfile
), MAY_READ
);
3988 event
->cft
= __file_cft(cfile
);
3989 if (IS_ERR(event
->cft
)) {
3990 ret
= PTR_ERR(event
->cft
);
3995 * The file to be monitored must be in the same cgroup as
3996 * cgroup.event_control is.
3998 cgrp_cfile
= __d_cgrp(cfile
->f_dentry
->d_parent
);
3999 if (cgrp_cfile
!= cgrp
) {
4004 if (!event
->cft
->register_event
|| !event
->cft
->unregister_event
) {
4009 ret
= event
->cft
->register_event(cgrp
, event
->cft
,
4010 event
->eventfd
, buffer
);
4014 efile
->f_op
->poll(efile
, &event
->pt
);
4017 * Events should be removed after rmdir of cgroup directory, but before
4018 * destroying subsystem state objects. Let's take reference to cgroup
4019 * directory dentry to do that.
4023 spin_lock(&cgrp
->event_list_lock
);
4024 list_add(&event
->list
, &cgrp
->event_list
);
4025 spin_unlock(&cgrp
->event_list_lock
);
4036 if (event
&& event
->eventfd
&& !IS_ERR(event
->eventfd
))
4037 eventfd_ctx_put(event
->eventfd
);
4039 if (!IS_ERR_OR_NULL(efile
))
4047 static u64
cgroup_clone_children_read(struct cgroup
*cgrp
,
4050 return test_bit(CGRP_CPUSET_CLONE_CHILDREN
, &cgrp
->flags
);
4053 static int cgroup_clone_children_write(struct cgroup
*cgrp
,
4058 set_bit(CGRP_CPUSET_CLONE_CHILDREN
, &cgrp
->flags
);
4060 clear_bit(CGRP_CPUSET_CLONE_CHILDREN
, &cgrp
->flags
);
4064 static struct cftype cgroup_base_files
[] = {
4066 .name
= "cgroup.procs",
4067 .open
= cgroup_procs_open
,
4068 .write_u64
= cgroup_procs_write
,
4069 .release
= cgroup_pidlist_release
,
4070 .mode
= S_IRUGO
| S_IWUSR
,
4073 .name
= "cgroup.event_control",
4074 .write_string
= cgroup_write_event_control
,
4078 .name
= "cgroup.clone_children",
4079 .flags
= CFTYPE_INSANE
,
4080 .read_u64
= cgroup_clone_children_read
,
4081 .write_u64
= cgroup_clone_children_write
,
4084 .name
= "cgroup.sane_behavior",
4085 .flags
= CFTYPE_ONLY_ON_ROOT
,
4086 .read_seq_string
= cgroup_sane_behavior_show
,
4090 * Historical crazy stuff. These don't have "cgroup." prefix and
4091 * don't exist if sane_behavior. If you're depending on these, be
4092 * prepared to be burned.
4096 .flags
= CFTYPE_INSANE
, /* use "procs" instead */
4097 .open
= cgroup_tasks_open
,
4098 .write_u64
= cgroup_tasks_write
,
4099 .release
= cgroup_pidlist_release
,
4100 .mode
= S_IRUGO
| S_IWUSR
,
4103 .name
= "notify_on_release",
4104 .flags
= CFTYPE_INSANE
,
4105 .read_u64
= cgroup_read_notify_on_release
,
4106 .write_u64
= cgroup_write_notify_on_release
,
4109 .name
= "release_agent",
4110 .flags
= CFTYPE_INSANE
| CFTYPE_ONLY_ON_ROOT
,
4111 .read_seq_string
= cgroup_release_agent_show
,
4112 .write_string
= cgroup_release_agent_write
,
4113 .max_write_len
= PATH_MAX
,
4119 * cgroup_populate_dir - selectively creation of files in a directory
4120 * @cgrp: target cgroup
4121 * @base_files: true if the base files should be added
4122 * @subsys_mask: mask of the subsystem ids whose files should be added
4124 static int cgroup_populate_dir(struct cgroup
*cgrp
, bool base_files
,
4125 unsigned long subsys_mask
)
4128 struct cgroup_subsys
*ss
;
4131 err
= cgroup_addrm_files(cgrp
, NULL
, cgroup_base_files
, true);
4136 /* process cftsets of each subsystem */
4137 for_each_root_subsys(cgrp
->root
, ss
) {
4138 struct cftype_set
*set
;
4139 if (!test_bit(ss
->subsys_id
, &subsys_mask
))
4142 list_for_each_entry(set
, &ss
->cftsets
, node
)
4143 cgroup_addrm_files(cgrp
, ss
, set
->cfts
, true);
4146 /* This cgroup is ready now */
4147 for_each_root_subsys(cgrp
->root
, ss
) {
4148 struct cgroup_subsys_state
*css
= cgrp
->subsys
[ss
->subsys_id
];
4150 * Update id->css pointer and make this css visible from
4151 * CSS ID functions. This pointer will be dereferened
4152 * from RCU-read-side without locks.
4155 rcu_assign_pointer(css
->id
->css
, css
);
4161 static void css_dput_fn(struct work_struct
*work
)
4163 struct cgroup_subsys_state
*css
=
4164 container_of(work
, struct cgroup_subsys_state
, dput_work
);
4166 cgroup_dput(css
->cgroup
);
4169 static void css_release(struct percpu_ref
*ref
)
4171 struct cgroup_subsys_state
*css
=
4172 container_of(ref
, struct cgroup_subsys_state
, refcnt
);
4174 schedule_work(&css
->dput_work
);
4177 static void init_cgroup_css(struct cgroup_subsys_state
*css
,
4178 struct cgroup_subsys
*ss
,
4179 struct cgroup
*cgrp
)
4184 if (cgrp
== cgroup_dummy_top
)
4185 css
->flags
|= CSS_ROOT
;
4186 BUG_ON(cgrp
->subsys
[ss
->subsys_id
]);
4187 cgrp
->subsys
[ss
->subsys_id
] = css
;
4190 * css holds an extra ref to @cgrp->dentry which is put on the last
4191 * css_put(). dput() requires process context, which css_put() may
4192 * be called without. @css->dput_work will be used to invoke
4193 * dput() asynchronously from css_put().
4195 INIT_WORK(&css
->dput_work
, css_dput_fn
);
4198 /* invoke ->post_create() on a new CSS and mark it online if successful */
4199 static int online_css(struct cgroup_subsys
*ss
, struct cgroup
*cgrp
)
4203 lockdep_assert_held(&cgroup_mutex
);
4206 ret
= ss
->css_online(cgrp
);
4208 cgrp
->subsys
[ss
->subsys_id
]->flags
|= CSS_ONLINE
;
4212 /* if the CSS is online, invoke ->pre_destory() on it and mark it offline */
4213 static void offline_css(struct cgroup_subsys
*ss
, struct cgroup
*cgrp
)
4214 __releases(&cgroup_mutex
) __acquires(&cgroup_mutex
)
4216 struct cgroup_subsys_state
*css
= cgrp
->subsys
[ss
->subsys_id
];
4218 lockdep_assert_held(&cgroup_mutex
);
4220 if (!(css
->flags
& CSS_ONLINE
))
4223 if (ss
->css_offline
)
4224 ss
->css_offline(cgrp
);
4226 cgrp
->subsys
[ss
->subsys_id
]->flags
&= ~CSS_ONLINE
;
4230 * cgroup_create - create a cgroup
4231 * @parent: cgroup that will be parent of the new cgroup
4232 * @dentry: dentry of the new cgroup
4233 * @mode: mode to set on new inode
4235 * Must be called with the mutex on the parent inode held
4237 static long cgroup_create(struct cgroup
*parent
, struct dentry
*dentry
,
4240 struct cgroup
*cgrp
;
4241 struct cgroup_name
*name
;
4242 struct cgroupfs_root
*root
= parent
->root
;
4244 struct cgroup_subsys
*ss
;
4245 struct super_block
*sb
= root
->sb
;
4247 /* allocate the cgroup and its ID, 0 is reserved for the root */
4248 cgrp
= kzalloc(sizeof(*cgrp
), GFP_KERNEL
);
4252 name
= cgroup_alloc_name(dentry
);
4255 rcu_assign_pointer(cgrp
->name
, name
);
4257 cgrp
->id
= ida_simple_get(&root
->cgroup_ida
, 1, 0, GFP_KERNEL
);
4262 * Only live parents can have children. Note that the liveliness
4263 * check isn't strictly necessary because cgroup_mkdir() and
4264 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4265 * anyway so that locking is contained inside cgroup proper and we
4266 * don't get nasty surprises if we ever grow another caller.
4268 if (!cgroup_lock_live_group(parent
)) {
4273 /* Grab a reference on the superblock so the hierarchy doesn't
4274 * get deleted on unmount if there are child cgroups. This
4275 * can be done outside cgroup_mutex, since the sb can't
4276 * disappear while someone has an open control file on the
4278 atomic_inc(&sb
->s_active
);
4280 init_cgroup_housekeeping(cgrp
);
4282 dentry
->d_fsdata
= cgrp
;
4283 cgrp
->dentry
= dentry
;
4285 cgrp
->parent
= parent
;
4286 cgrp
->root
= parent
->root
;
4288 if (notify_on_release(parent
))
4289 set_bit(CGRP_NOTIFY_ON_RELEASE
, &cgrp
->flags
);
4291 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN
, &parent
->flags
))
4292 set_bit(CGRP_CPUSET_CLONE_CHILDREN
, &cgrp
->flags
);
4294 for_each_root_subsys(root
, ss
) {
4295 struct cgroup_subsys_state
*css
;
4297 css
= ss
->css_alloc(cgrp
);
4303 err
= percpu_ref_init(&css
->refcnt
, css_release
);
4307 init_cgroup_css(css
, ss
, cgrp
);
4310 err
= alloc_css_id(ss
, parent
, cgrp
);
4317 * Create directory. cgroup_create_file() returns with the new
4318 * directory locked on success so that it can be populated without
4319 * dropping cgroup_mutex.
4321 err
= cgroup_create_file(dentry
, S_IFDIR
| mode
, sb
);
4324 lockdep_assert_held(&dentry
->d_inode
->i_mutex
);
4326 cgrp
->serial_nr
= cgroup_serial_nr_next
++;
4328 /* allocation complete, commit to creation */
4329 list_add_tail_rcu(&cgrp
->sibling
, &cgrp
->parent
->children
);
4330 root
->number_of_cgroups
++;
4332 /* each css holds a ref to the cgroup's dentry */
4333 for_each_root_subsys(root
, ss
)
4336 /* hold a ref to the parent's dentry */
4337 dget(parent
->dentry
);
4339 /* creation succeeded, notify subsystems */
4340 for_each_root_subsys(root
, ss
) {
4341 err
= online_css(ss
, cgrp
);
4345 if (ss
->broken_hierarchy
&& !ss
->warned_broken_hierarchy
&&
4347 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",
4348 current
->comm
, current
->pid
, ss
->name
);
4349 if (!strcmp(ss
->name
, "memory"))
4350 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4351 ss
->warned_broken_hierarchy
= true;
4355 err
= cgroup_populate_dir(cgrp
, true, root
->subsys_mask
);
4359 mutex_unlock(&cgroup_mutex
);
4360 mutex_unlock(&cgrp
->dentry
->d_inode
->i_mutex
);
4365 for_each_root_subsys(root
, ss
) {
4366 struct cgroup_subsys_state
*css
= cgrp
->subsys
[ss
->subsys_id
];
4369 percpu_ref_cancel_init(&css
->refcnt
);
4373 mutex_unlock(&cgroup_mutex
);
4374 /* Release the reference count that we took on the superblock */
4375 deactivate_super(sb
);
4377 ida_simple_remove(&root
->cgroup_ida
, cgrp
->id
);
4379 kfree(rcu_dereference_raw(cgrp
->name
));
4385 cgroup_destroy_locked(cgrp
);
4386 mutex_unlock(&cgroup_mutex
);
4387 mutex_unlock(&dentry
->d_inode
->i_mutex
);
4391 static int cgroup_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
4393 struct cgroup
*c_parent
= dentry
->d_parent
->d_fsdata
;
4395 /* the vfs holds inode->i_mutex already */
4396 return cgroup_create(c_parent
, dentry
, mode
| S_IFDIR
);
4399 static void cgroup_css_killed(struct cgroup
*cgrp
)
4401 if (!atomic_dec_and_test(&cgrp
->css_kill_cnt
))
4404 /* percpu ref's of all css's are killed, kick off the next step */
4405 INIT_WORK(&cgrp
->destroy_work
, cgroup_offline_fn
);
4406 schedule_work(&cgrp
->destroy_work
);
4409 static void css_ref_killed_fn(struct percpu_ref
*ref
)
4411 struct cgroup_subsys_state
*css
=
4412 container_of(ref
, struct cgroup_subsys_state
, refcnt
);
4414 cgroup_css_killed(css
->cgroup
);
4418 * cgroup_destroy_locked - the first stage of cgroup destruction
4419 * @cgrp: cgroup to be destroyed
4421 * css's make use of percpu refcnts whose killing latency shouldn't be
4422 * exposed to userland and are RCU protected. Also, cgroup core needs to
4423 * guarantee that css_tryget() won't succeed by the time ->css_offline() is
4424 * invoked. To satisfy all the requirements, destruction is implemented in
4425 * the following two steps.
4427 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4428 * userland visible parts and start killing the percpu refcnts of
4429 * css's. Set up so that the next stage will be kicked off once all
4430 * the percpu refcnts are confirmed to be killed.
4432 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4433 * rest of destruction. Once all cgroup references are gone, the
4434 * cgroup is RCU-freed.
4436 * This function implements s1. After this step, @cgrp is gone as far as
4437 * the userland is concerned and a new cgroup with the same name may be
4438 * created. As cgroup doesn't care about the names internally, this
4439 * doesn't cause any problem.
4441 static int cgroup_destroy_locked(struct cgroup
*cgrp
)
4442 __releases(&cgroup_mutex
) __acquires(&cgroup_mutex
)
4444 struct dentry
*d
= cgrp
->dentry
;
4445 struct cgroup_event
*event
, *tmp
;
4446 struct cgroup_subsys
*ss
;
4449 lockdep_assert_held(&d
->d_inode
->i_mutex
);
4450 lockdep_assert_held(&cgroup_mutex
);
4453 * css_set_lock synchronizes access to ->cset_links and prevents
4454 * @cgrp from being removed while __put_css_set() is in progress.
4456 read_lock(&css_set_lock
);
4457 empty
= list_empty(&cgrp
->cset_links
) && list_empty(&cgrp
->children
);
4458 read_unlock(&css_set_lock
);
4463 * Block new css_tryget() by killing css refcnts. cgroup core
4464 * guarantees that, by the time ->css_offline() is invoked, no new
4465 * css reference will be given out via css_tryget(). We can't
4466 * simply call percpu_ref_kill() and proceed to offlining css's
4467 * because percpu_ref_kill() doesn't guarantee that the ref is seen
4468 * as killed on all CPUs on return.
4470 * Use percpu_ref_kill_and_confirm() to get notifications as each
4471 * css is confirmed to be seen as killed on all CPUs. The
4472 * notification callback keeps track of the number of css's to be
4473 * killed and schedules cgroup_offline_fn() to perform the rest of
4474 * destruction once the percpu refs of all css's are confirmed to
4477 atomic_set(&cgrp
->css_kill_cnt
, 1);
4478 for_each_root_subsys(cgrp
->root
, ss
) {
4479 struct cgroup_subsys_state
*css
= cgrp
->subsys
[ss
->subsys_id
];
4482 * Killing would put the base ref, but we need to keep it
4483 * alive until after ->css_offline.
4485 percpu_ref_get(&css
->refcnt
);
4487 atomic_inc(&cgrp
->css_kill_cnt
);
4488 percpu_ref_kill_and_confirm(&css
->refcnt
, css_ref_killed_fn
);
4490 cgroup_css_killed(cgrp
);
4493 * Mark @cgrp dead. This prevents further task migration and child
4494 * creation by disabling cgroup_lock_live_group(). Note that
4495 * CGRP_DEAD assertion is depended upon by cgroup_next_sibling() to
4496 * resume iteration after dropping RCU read lock. See
4497 * cgroup_next_sibling() for details.
4499 set_bit(CGRP_DEAD
, &cgrp
->flags
);
4501 /* CGRP_DEAD is set, remove from ->release_list for the last time */
4502 raw_spin_lock(&release_list_lock
);
4503 if (!list_empty(&cgrp
->release_list
))
4504 list_del_init(&cgrp
->release_list
);
4505 raw_spin_unlock(&release_list_lock
);
4508 * Remove @cgrp directory. The removal puts the base ref but we
4509 * aren't quite done with @cgrp yet, so hold onto it.
4512 cgroup_d_remove_dir(d
);
4515 * Unregister events and notify userspace.
4516 * Notify userspace about cgroup removing only after rmdir of cgroup
4517 * directory to avoid race between userspace and kernelspace.
4519 spin_lock(&cgrp
->event_list_lock
);
4520 list_for_each_entry_safe(event
, tmp
, &cgrp
->event_list
, list
) {
4521 list_del_init(&event
->list
);
4522 schedule_work(&event
->remove
);
4524 spin_unlock(&cgrp
->event_list_lock
);
4530 * cgroup_offline_fn - the second step of cgroup destruction
4531 * @work: cgroup->destroy_free_work
4533 * This function is invoked from a work item for a cgroup which is being
4534 * destroyed after the percpu refcnts of all css's are guaranteed to be
4535 * seen as killed on all CPUs, and performs the rest of destruction. This
4536 * is the second step of destruction described in the comment above
4537 * cgroup_destroy_locked().
4539 static void cgroup_offline_fn(struct work_struct
*work
)
4541 struct cgroup
*cgrp
= container_of(work
, struct cgroup
, destroy_work
);
4542 struct cgroup
*parent
= cgrp
->parent
;
4543 struct dentry
*d
= cgrp
->dentry
;
4544 struct cgroup_subsys
*ss
;
4546 mutex_lock(&cgroup_mutex
);
4549 * css_tryget() is guaranteed to fail now. Tell subsystems to
4550 * initate destruction.
4552 for_each_root_subsys(cgrp
->root
, ss
)
4553 offline_css(ss
, cgrp
);
4556 * Put the css refs from cgroup_destroy_locked(). Each css holds
4557 * an extra reference to the cgroup's dentry and cgroup removal
4558 * proceeds regardless of css refs. On the last put of each css,
4559 * whenever that may be, the extra dentry ref is put so that dentry
4560 * destruction happens only after all css's are released.
4562 for_each_root_subsys(cgrp
->root
, ss
)
4563 css_put(cgrp
->subsys
[ss
->subsys_id
]);
4565 /* delete this cgroup from parent->children */
4566 list_del_rcu(&cgrp
->sibling
);
4570 set_bit(CGRP_RELEASABLE
, &parent
->flags
);
4571 check_for_release(parent
);
4573 mutex_unlock(&cgroup_mutex
);
4576 static int cgroup_rmdir(struct inode
*unused_dir
, struct dentry
*dentry
)
4580 mutex_lock(&cgroup_mutex
);
4581 ret
= cgroup_destroy_locked(dentry
->d_fsdata
);
4582 mutex_unlock(&cgroup_mutex
);
4587 static void __init_or_module
cgroup_init_cftsets(struct cgroup_subsys
*ss
)
4589 INIT_LIST_HEAD(&ss
->cftsets
);
4592 * base_cftset is embedded in subsys itself, no need to worry about
4595 if (ss
->base_cftypes
) {
4596 ss
->base_cftset
.cfts
= ss
->base_cftypes
;
4597 list_add_tail(&ss
->base_cftset
.node
, &ss
->cftsets
);
4601 static void __init
cgroup_init_subsys(struct cgroup_subsys
*ss
)
4603 struct cgroup_subsys_state
*css
;
4605 printk(KERN_INFO
"Initializing cgroup subsys %s\n", ss
->name
);
4607 mutex_lock(&cgroup_mutex
);
4609 /* init base cftset */
4610 cgroup_init_cftsets(ss
);
4612 /* Create the top cgroup state for this subsystem */
4613 list_add(&ss
->sibling
, &cgroup_dummy_root
.subsys_list
);
4614 ss
->root
= &cgroup_dummy_root
;
4615 css
= ss
->css_alloc(cgroup_dummy_top
);
4616 /* We don't handle early failures gracefully */
4617 BUG_ON(IS_ERR(css
));
4618 init_cgroup_css(css
, ss
, cgroup_dummy_top
);
4620 /* Update the init_css_set to contain a subsys
4621 * pointer to this state - since the subsystem is
4622 * newly registered, all tasks and hence the
4623 * init_css_set is in the subsystem's top cgroup. */
4624 init_css_set
.subsys
[ss
->subsys_id
] = css
;
4626 need_forkexit_callback
|= ss
->fork
|| ss
->exit
;
4628 /* At system boot, before all subsystems have been
4629 * registered, no tasks have been forked, so we don't
4630 * need to invoke fork callbacks here. */
4631 BUG_ON(!list_empty(&init_task
.tasks
));
4633 BUG_ON(online_css(ss
, cgroup_dummy_top
));
4635 mutex_unlock(&cgroup_mutex
);
4637 /* this function shouldn't be used with modular subsystems, since they
4638 * need to register a subsys_id, among other things */
4643 * cgroup_load_subsys: load and register a modular subsystem at runtime
4644 * @ss: the subsystem to load
4646 * This function should be called in a modular subsystem's initcall. If the
4647 * subsystem is built as a module, it will be assigned a new subsys_id and set
4648 * up for use. If the subsystem is built-in anyway, work is delegated to the
4649 * simpler cgroup_init_subsys.
4651 int __init_or_module
cgroup_load_subsys(struct cgroup_subsys
*ss
)
4653 struct cgroup_subsys_state
*css
;
4655 struct hlist_node
*tmp
;
4656 struct css_set
*cset
;
4659 /* check name and function validity */
4660 if (ss
->name
== NULL
|| strlen(ss
->name
) > MAX_CGROUP_TYPE_NAMELEN
||
4661 ss
->css_alloc
== NULL
|| ss
->css_free
== NULL
)
4665 * we don't support callbacks in modular subsystems. this check is
4666 * before the ss->module check for consistency; a subsystem that could
4667 * be a module should still have no callbacks even if the user isn't
4668 * compiling it as one.
4670 if (ss
->fork
|| ss
->exit
)
4674 * an optionally modular subsystem is built-in: we want to do nothing,
4675 * since cgroup_init_subsys will have already taken care of it.
4677 if (ss
->module
== NULL
) {
4678 /* a sanity check */
4679 BUG_ON(cgroup_subsys
[ss
->subsys_id
] != ss
);
4683 /* init base cftset */
4684 cgroup_init_cftsets(ss
);
4686 mutex_lock(&cgroup_mutex
);
4687 cgroup_subsys
[ss
->subsys_id
] = ss
;
4690 * no ss->css_alloc seems to need anything important in the ss
4691 * struct, so this can happen first (i.e. before the dummy root
4694 css
= ss
->css_alloc(cgroup_dummy_top
);
4696 /* failure case - need to deassign the cgroup_subsys[] slot. */
4697 cgroup_subsys
[ss
->subsys_id
] = NULL
;
4698 mutex_unlock(&cgroup_mutex
);
4699 return PTR_ERR(css
);
4702 list_add(&ss
->sibling
, &cgroup_dummy_root
.subsys_list
);
4703 ss
->root
= &cgroup_dummy_root
;
4705 /* our new subsystem will be attached to the dummy hierarchy. */
4706 init_cgroup_css(css
, ss
, cgroup_dummy_top
);
4707 /* init_idr must be after init_cgroup_css because it sets css->id. */
4709 ret
= cgroup_init_idr(ss
, css
);
4715 * Now we need to entangle the css into the existing css_sets. unlike
4716 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4717 * will need a new pointer to it; done by iterating the css_set_table.
4718 * furthermore, modifying the existing css_sets will corrupt the hash
4719 * table state, so each changed css_set will need its hash recomputed.
4720 * this is all done under the css_set_lock.
4722 write_lock(&css_set_lock
);
4723 hash_for_each_safe(css_set_table
, i
, tmp
, cset
, hlist
) {
4724 /* skip entries that we already rehashed */
4725 if (cset
->subsys
[ss
->subsys_id
])
4727 /* remove existing entry */
4728 hash_del(&cset
->hlist
);
4730 cset
->subsys
[ss
->subsys_id
] = css
;
4731 /* recompute hash and restore entry */
4732 key
= css_set_hash(cset
->subsys
);
4733 hash_add(css_set_table
, &cset
->hlist
, key
);
4735 write_unlock(&css_set_lock
);
4737 ret
= online_css(ss
, cgroup_dummy_top
);
4742 mutex_unlock(&cgroup_mutex
);
4746 mutex_unlock(&cgroup_mutex
);
4747 /* @ss can't be mounted here as try_module_get() would fail */
4748 cgroup_unload_subsys(ss
);
4751 EXPORT_SYMBOL_GPL(cgroup_load_subsys
);
4754 * cgroup_unload_subsys: unload a modular subsystem
4755 * @ss: the subsystem to unload
4757 * This function should be called in a modular subsystem's exitcall. When this
4758 * function is invoked, the refcount on the subsystem's module will be 0, so
4759 * the subsystem will not be attached to any hierarchy.
4761 void cgroup_unload_subsys(struct cgroup_subsys
*ss
)
4763 struct cgrp_cset_link
*link
;
4765 BUG_ON(ss
->module
== NULL
);
4768 * we shouldn't be called if the subsystem is in use, and the use of
4769 * try_module_get in parse_cgroupfs_options should ensure that it
4770 * doesn't start being used while we're killing it off.
4772 BUG_ON(ss
->root
!= &cgroup_dummy_root
);
4774 mutex_lock(&cgroup_mutex
);
4776 offline_css(ss
, cgroup_dummy_top
);
4779 idr_destroy(&ss
->idr
);
4781 /* deassign the subsys_id */
4782 cgroup_subsys
[ss
->subsys_id
] = NULL
;
4784 /* remove subsystem from the dummy root's list of subsystems */
4785 list_del_init(&ss
->sibling
);
4788 * disentangle the css from all css_sets attached to the dummy
4789 * top. as in loading, we need to pay our respects to the hashtable
4792 write_lock(&css_set_lock
);
4793 list_for_each_entry(link
, &cgroup_dummy_top
->cset_links
, cset_link
) {
4794 struct css_set
*cset
= link
->cset
;
4797 hash_del(&cset
->hlist
);
4798 cset
->subsys
[ss
->subsys_id
] = NULL
;
4799 key
= css_set_hash(cset
->subsys
);
4800 hash_add(css_set_table
, &cset
->hlist
, key
);
4802 write_unlock(&css_set_lock
);
4805 * remove subsystem's css from the cgroup_dummy_top and free it -
4806 * need to free before marking as null because ss->css_free needs
4807 * the cgrp->subsys pointer to find their state. note that this
4808 * also takes care of freeing the css_id.
4810 ss
->css_free(cgroup_dummy_top
);
4811 cgroup_dummy_top
->subsys
[ss
->subsys_id
] = NULL
;
4813 mutex_unlock(&cgroup_mutex
);
4815 EXPORT_SYMBOL_GPL(cgroup_unload_subsys
);
4818 * cgroup_init_early - cgroup initialization at system boot
4820 * Initialize cgroups at system boot, and initialize any
4821 * subsystems that request early init.
4823 int __init
cgroup_init_early(void)
4826 atomic_set(&init_css_set
.refcount
, 1);
4827 INIT_LIST_HEAD(&init_css_set
.cgrp_links
);
4828 INIT_LIST_HEAD(&init_css_set
.tasks
);
4829 INIT_HLIST_NODE(&init_css_set
.hlist
);
4831 init_cgroup_root(&cgroup_dummy_root
);
4832 cgroup_root_count
= 1;
4833 init_task
.cgroups
= &init_css_set
;
4835 init_cgrp_cset_link
.cset
= &init_css_set
;
4836 init_cgrp_cset_link
.cgrp
= cgroup_dummy_top
;
4837 list_add(&init_cgrp_cset_link
.cset_link
, &cgroup_dummy_top
->cset_links
);
4838 list_add(&init_cgrp_cset_link
.cgrp_link
, &init_css_set
.cgrp_links
);
4840 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
4841 struct cgroup_subsys
*ss
= cgroup_subsys
[i
];
4843 /* at bootup time, we don't worry about modular subsystems */
4844 if (!ss
|| ss
->module
)
4848 BUG_ON(strlen(ss
->name
) > MAX_CGROUP_TYPE_NAMELEN
);
4849 BUG_ON(!ss
->css_alloc
);
4850 BUG_ON(!ss
->css_free
);
4851 if (ss
->subsys_id
!= i
) {
4852 printk(KERN_ERR
"cgroup: Subsys %s id == %d\n",
4853 ss
->name
, ss
->subsys_id
);
4858 cgroup_init_subsys(ss
);
4864 * cgroup_init - cgroup initialization
4866 * Register cgroup filesystem and /proc file, and initialize
4867 * any subsystems that didn't request early init.
4869 int __init
cgroup_init(void)
4875 err
= bdi_init(&cgroup_backing_dev_info
);
4879 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
4880 struct cgroup_subsys
*ss
= cgroup_subsys
[i
];
4882 /* at bootup time, we don't worry about modular subsystems */
4883 if (!ss
|| ss
->module
)
4885 if (!ss
->early_init
)
4886 cgroup_init_subsys(ss
);
4888 cgroup_init_idr(ss
, init_css_set
.subsys
[ss
->subsys_id
]);
4891 /* Add init_css_set to the hash table */
4892 key
= css_set_hash(init_css_set
.subsys
);
4893 hash_add(css_set_table
, &init_css_set
.hlist
, key
);
4895 /* allocate id for the dummy hierarchy */
4896 mutex_lock(&cgroup_mutex
);
4897 mutex_lock(&cgroup_root_mutex
);
4899 BUG_ON(cgroup_init_root_id(&cgroup_dummy_root
));
4901 mutex_unlock(&cgroup_root_mutex
);
4902 mutex_unlock(&cgroup_mutex
);
4904 cgroup_kobj
= kobject_create_and_add("cgroup", fs_kobj
);
4910 err
= register_filesystem(&cgroup_fs_type
);
4912 kobject_put(cgroup_kobj
);
4916 proc_create("cgroups", 0, NULL
, &proc_cgroupstats_operations
);
4920 bdi_destroy(&cgroup_backing_dev_info
);
4926 * proc_cgroup_show()
4927 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4928 * - Used for /proc/<pid>/cgroup.
4929 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4930 * doesn't really matter if tsk->cgroup changes after we read it,
4931 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
4932 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4933 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4934 * cgroup to top_cgroup.
4937 /* TODO: Use a proper seq_file iterator */
4938 int proc_cgroup_show(struct seq_file
*m
, void *v
)
4941 struct task_struct
*tsk
;
4944 struct cgroupfs_root
*root
;
4947 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
4953 tsk
= get_pid_task(pid
, PIDTYPE_PID
);
4959 mutex_lock(&cgroup_mutex
);
4961 for_each_active_root(root
) {
4962 struct cgroup_subsys
*ss
;
4963 struct cgroup
*cgrp
;
4966 seq_printf(m
, "%d:", root
->hierarchy_id
);
4967 for_each_root_subsys(root
, ss
)
4968 seq_printf(m
, "%s%s", count
++ ? "," : "", ss
->name
);
4969 if (strlen(root
->name
))
4970 seq_printf(m
, "%sname=%s", count
? "," : "",
4973 cgrp
= task_cgroup_from_root(tsk
, root
);
4974 retval
= cgroup_path(cgrp
, buf
, PAGE_SIZE
);
4982 mutex_unlock(&cgroup_mutex
);
4983 put_task_struct(tsk
);
4990 /* Display information about each subsystem and each hierarchy */
4991 static int proc_cgroupstats_show(struct seq_file
*m
, void *v
)
4995 seq_puts(m
, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
4997 * ideally we don't want subsystems moving around while we do this.
4998 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4999 * subsys/hierarchy state.
5001 mutex_lock(&cgroup_mutex
);
5002 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
5003 struct cgroup_subsys
*ss
= cgroup_subsys
[i
];
5006 seq_printf(m
, "%s\t%d\t%d\t%d\n",
5007 ss
->name
, ss
->root
->hierarchy_id
,
5008 ss
->root
->number_of_cgroups
, !ss
->disabled
);
5010 mutex_unlock(&cgroup_mutex
);
5014 static int cgroupstats_open(struct inode
*inode
, struct file
*file
)
5016 return single_open(file
, proc_cgroupstats_show
, NULL
);
5019 static const struct file_operations proc_cgroupstats_operations
= {
5020 .open
= cgroupstats_open
,
5022 .llseek
= seq_lseek
,
5023 .release
= single_release
,
5027 * cgroup_fork - attach newly forked task to its parents cgroup.
5028 * @child: pointer to task_struct of forking parent process.
5030 * Description: A task inherits its parent's cgroup at fork().
5032 * A pointer to the shared css_set was automatically copied in
5033 * fork.c by dup_task_struct(). However, we ignore that copy, since
5034 * it was not made under the protection of RCU or cgroup_mutex, so
5035 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
5036 * have already changed current->cgroups, allowing the previously
5037 * referenced cgroup group to be removed and freed.
5039 * At the point that cgroup_fork() is called, 'current' is the parent
5040 * task, and the passed argument 'child' points to the child task.
5042 void cgroup_fork(struct task_struct
*child
)
5045 child
->cgroups
= current
->cgroups
;
5046 get_css_set(child
->cgroups
);
5047 task_unlock(current
);
5048 INIT_LIST_HEAD(&child
->cg_list
);
5052 * cgroup_post_fork - called on a new task after adding it to the task list
5053 * @child: the task in question
5055 * Adds the task to the list running through its css_set if necessary and
5056 * call the subsystem fork() callbacks. Has to be after the task is
5057 * visible on the task list in case we race with the first call to
5058 * cgroup_iter_start() - to guarantee that the new task ends up on its
5061 void cgroup_post_fork(struct task_struct
*child
)
5066 * use_task_css_set_links is set to 1 before we walk the tasklist
5067 * under the tasklist_lock and we read it here after we added the child
5068 * to the tasklist under the tasklist_lock as well. If the child wasn't
5069 * yet in the tasklist when we walked through it from
5070 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
5071 * should be visible now due to the paired locking and barriers implied
5072 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
5073 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
5076 if (use_task_css_set_links
) {
5077 write_lock(&css_set_lock
);
5079 if (list_empty(&child
->cg_list
))
5080 list_add(&child
->cg_list
, &child
->cgroups
->tasks
);
5082 write_unlock(&css_set_lock
);
5086 * Call ss->fork(). This must happen after @child is linked on
5087 * css_set; otherwise, @child might change state between ->fork()
5088 * and addition to css_set.
5090 if (need_forkexit_callback
) {
5092 * fork/exit callbacks are supported only for builtin
5093 * subsystems, and the builtin section of the subsys
5094 * array is immutable, so we don't need to lock the
5095 * subsys array here. On the other hand, modular section
5096 * of the array can be freed at module unload, so we
5099 for (i
= 0; i
< CGROUP_BUILTIN_SUBSYS_COUNT
; i
++) {
5100 struct cgroup_subsys
*ss
= cgroup_subsys
[i
];
5109 * cgroup_exit - detach cgroup from exiting task
5110 * @tsk: pointer to task_struct of exiting process
5111 * @run_callback: run exit callbacks?
5113 * Description: Detach cgroup from @tsk and release it.
5115 * Note that cgroups marked notify_on_release force every task in
5116 * them to take the global cgroup_mutex mutex when exiting.
5117 * This could impact scaling on very large systems. Be reluctant to
5118 * use notify_on_release cgroups where very high task exit scaling
5119 * is required on large systems.
5121 * the_top_cgroup_hack:
5123 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
5125 * We call cgroup_exit() while the task is still competent to
5126 * handle notify_on_release(), then leave the task attached to the
5127 * root cgroup in each hierarchy for the remainder of its exit.
5129 * To do this properly, we would increment the reference count on
5130 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
5131 * code we would add a second cgroup function call, to drop that
5132 * reference. This would just create an unnecessary hot spot on
5133 * the top_cgroup reference count, to no avail.
5135 * Normally, holding a reference to a cgroup without bumping its
5136 * count is unsafe. The cgroup could go away, or someone could
5137 * attach us to a different cgroup, decrementing the count on
5138 * the first cgroup that we never incremented. But in this case,
5139 * top_cgroup isn't going away, and either task has PF_EXITING set,
5140 * which wards off any cgroup_attach_task() attempts, or task is a failed
5141 * fork, never visible to cgroup_attach_task.
5143 void cgroup_exit(struct task_struct
*tsk
, int run_callbacks
)
5145 struct css_set
*cset
;
5149 * Unlink from the css_set task list if necessary.
5150 * Optimistically check cg_list before taking
5153 if (!list_empty(&tsk
->cg_list
)) {
5154 write_lock(&css_set_lock
);
5155 if (!list_empty(&tsk
->cg_list
))
5156 list_del_init(&tsk
->cg_list
);
5157 write_unlock(&css_set_lock
);
5160 /* Reassign the task to the init_css_set. */
5162 cset
= tsk
->cgroups
;
5163 tsk
->cgroups
= &init_css_set
;
5165 if (run_callbacks
&& need_forkexit_callback
) {
5167 * fork/exit callbacks are supported only for builtin
5168 * subsystems, see cgroup_post_fork() for details.
5170 for (i
= 0; i
< CGROUP_BUILTIN_SUBSYS_COUNT
; i
++) {
5171 struct cgroup_subsys
*ss
= cgroup_subsys
[i
];
5174 struct cgroup
*old_cgrp
=
5175 rcu_dereference_raw(cset
->subsys
[i
])->cgroup
;
5176 struct cgroup
*cgrp
= task_cgroup(tsk
, i
);
5177 ss
->exit(cgrp
, old_cgrp
, tsk
);
5183 put_css_set_taskexit(cset
);
5186 static void check_for_release(struct cgroup
*cgrp
)
5188 if (cgroup_is_releasable(cgrp
) &&
5189 list_empty(&cgrp
->cset_links
) && list_empty(&cgrp
->children
)) {
5191 * Control Group is currently removeable. If it's not
5192 * already queued for a userspace notification, queue
5195 int need_schedule_work
= 0;
5197 raw_spin_lock(&release_list_lock
);
5198 if (!cgroup_is_dead(cgrp
) &&
5199 list_empty(&cgrp
->release_list
)) {
5200 list_add(&cgrp
->release_list
, &release_list
);
5201 need_schedule_work
= 1;
5203 raw_spin_unlock(&release_list_lock
);
5204 if (need_schedule_work
)
5205 schedule_work(&release_agent_work
);
5210 * Notify userspace when a cgroup is released, by running the
5211 * configured release agent with the name of the cgroup (path
5212 * relative to the root of cgroup file system) as the argument.
5214 * Most likely, this user command will try to rmdir this cgroup.
5216 * This races with the possibility that some other task will be
5217 * attached to this cgroup before it is removed, or that some other
5218 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5219 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5220 * unused, and this cgroup will be reprieved from its death sentence,
5221 * to continue to serve a useful existence. Next time it's released,
5222 * we will get notified again, if it still has 'notify_on_release' set.
5224 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5225 * means only wait until the task is successfully execve()'d. The
5226 * separate release agent task is forked by call_usermodehelper(),
5227 * then control in this thread returns here, without waiting for the
5228 * release agent task. We don't bother to wait because the caller of
5229 * this routine has no use for the exit status of the release agent
5230 * task, so no sense holding our caller up for that.
5232 static void cgroup_release_agent(struct work_struct
*work
)
5234 BUG_ON(work
!= &release_agent_work
);
5235 mutex_lock(&cgroup_mutex
);
5236 raw_spin_lock(&release_list_lock
);
5237 while (!list_empty(&release_list
)) {
5238 char *argv
[3], *envp
[3];
5240 char *pathbuf
= NULL
, *agentbuf
= NULL
;
5241 struct cgroup
*cgrp
= list_entry(release_list
.next
,
5244 list_del_init(&cgrp
->release_list
);
5245 raw_spin_unlock(&release_list_lock
);
5246 pathbuf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
5249 if (cgroup_path(cgrp
, pathbuf
, PAGE_SIZE
) < 0)
5251 agentbuf
= kstrdup(cgrp
->root
->release_agent_path
, GFP_KERNEL
);
5256 argv
[i
++] = agentbuf
;
5257 argv
[i
++] = pathbuf
;
5261 /* minimal command environment */
5262 envp
[i
++] = "HOME=/";
5263 envp
[i
++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5266 /* Drop the lock while we invoke the usermode helper,
5267 * since the exec could involve hitting disk and hence
5268 * be a slow process */
5269 mutex_unlock(&cgroup_mutex
);
5270 call_usermodehelper(argv
[0], argv
, envp
, UMH_WAIT_EXEC
);
5271 mutex_lock(&cgroup_mutex
);
5275 raw_spin_lock(&release_list_lock
);
5277 raw_spin_unlock(&release_list_lock
);
5278 mutex_unlock(&cgroup_mutex
);
5281 static int __init
cgroup_disable(char *str
)
5286 while ((token
= strsep(&str
, ",")) != NULL
) {
5289 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
5290 struct cgroup_subsys
*ss
= cgroup_subsys
[i
];
5293 * cgroup_disable, being at boot time, can't
5294 * know about module subsystems, so we don't
5297 if (!ss
|| ss
->module
)
5300 if (!strcmp(token
, ss
->name
)) {
5302 printk(KERN_INFO
"Disabling %s control group"
5303 " subsystem\n", ss
->name
);
5310 __setup("cgroup_disable=", cgroup_disable
);
5313 * Functons for CSS ID.
5316 /* to get ID other than 0, this should be called when !cgroup_is_dead() */
5317 unsigned short css_id(struct cgroup_subsys_state
*css
)
5319 struct css_id
*cssid
;
5322 * This css_id() can return correct value when somone has refcnt
5323 * on this or this is under rcu_read_lock(). Once css->id is allocated,
5324 * it's unchanged until freed.
5326 cssid
= rcu_dereference_raw(css
->id
);
5332 EXPORT_SYMBOL_GPL(css_id
);
5335 * css_is_ancestor - test "root" css is an ancestor of "child"
5336 * @child: the css to be tested.
5337 * @root: the css supporsed to be an ancestor of the child.
5339 * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
5340 * this function reads css->id, the caller must hold rcu_read_lock().
5341 * But, considering usual usage, the csses should be valid objects after test.
5342 * Assuming that the caller will do some action to the child if this returns
5343 * returns true, the caller must take "child";s reference count.
5344 * If "child" is valid object and this returns true, "root" is valid, too.
5347 bool css_is_ancestor(struct cgroup_subsys_state
*child
,
5348 const struct cgroup_subsys_state
*root
)
5350 struct css_id
*child_id
;
5351 struct css_id
*root_id
;
5353 child_id
= rcu_dereference(child
->id
);
5356 root_id
= rcu_dereference(root
->id
);
5359 if (child_id
->depth
< root_id
->depth
)
5361 if (child_id
->stack
[root_id
->depth
] != root_id
->id
)
5366 void free_css_id(struct cgroup_subsys
*ss
, struct cgroup_subsys_state
*css
)
5368 struct css_id
*id
= css
->id
;
5369 /* When this is called before css_id initialization, id can be NULL */
5373 BUG_ON(!ss
->use_id
);
5375 rcu_assign_pointer(id
->css
, NULL
);
5376 rcu_assign_pointer(css
->id
, NULL
);
5377 spin_lock(&ss
->id_lock
);
5378 idr_remove(&ss
->idr
, id
->id
);
5379 spin_unlock(&ss
->id_lock
);
5380 kfree_rcu(id
, rcu_head
);
5382 EXPORT_SYMBOL_GPL(free_css_id
);
5385 * This is called by init or create(). Then, calls to this function are
5386 * always serialized (By cgroup_mutex() at create()).
5389 static struct css_id
*get_new_cssid(struct cgroup_subsys
*ss
, int depth
)
5391 struct css_id
*newid
;
5394 BUG_ON(!ss
->use_id
);
5396 size
= sizeof(*newid
) + sizeof(unsigned short) * (depth
+ 1);
5397 newid
= kzalloc(size
, GFP_KERNEL
);
5399 return ERR_PTR(-ENOMEM
);
5401 idr_preload(GFP_KERNEL
);
5402 spin_lock(&ss
->id_lock
);
5403 /* Don't use 0. allocates an ID of 1-65535 */
5404 ret
= idr_alloc(&ss
->idr
, newid
, 1, CSS_ID_MAX
+ 1, GFP_NOWAIT
);
5405 spin_unlock(&ss
->id_lock
);
5408 /* Returns error when there are no free spaces for new ID.*/
5413 newid
->depth
= depth
;
5417 return ERR_PTR(ret
);
5421 static int __init_or_module
cgroup_init_idr(struct cgroup_subsys
*ss
,
5422 struct cgroup_subsys_state
*rootcss
)
5424 struct css_id
*newid
;
5426 spin_lock_init(&ss
->id_lock
);
5429 newid
= get_new_cssid(ss
, 0);
5431 return PTR_ERR(newid
);
5433 newid
->stack
[0] = newid
->id
;
5434 newid
->css
= rootcss
;
5435 rootcss
->id
= newid
;
5439 static int alloc_css_id(struct cgroup_subsys
*ss
, struct cgroup
*parent
,
5440 struct cgroup
*child
)
5442 int subsys_id
, i
, depth
= 0;
5443 struct cgroup_subsys_state
*parent_css
, *child_css
;
5444 struct css_id
*child_id
, *parent_id
;
5446 subsys_id
= ss
->subsys_id
;
5447 parent_css
= parent
->subsys
[subsys_id
];
5448 child_css
= child
->subsys
[subsys_id
];
5449 parent_id
= parent_css
->id
;
5450 depth
= parent_id
->depth
+ 1;
5452 child_id
= get_new_cssid(ss
, depth
);
5453 if (IS_ERR(child_id
))
5454 return PTR_ERR(child_id
);
5456 for (i
= 0; i
< depth
; i
++)
5457 child_id
->stack
[i
] = parent_id
->stack
[i
];
5458 child_id
->stack
[depth
] = child_id
->id
;
5460 * child_id->css pointer will be set after this cgroup is available
5461 * see cgroup_populate_dir()
5463 rcu_assign_pointer(child_css
->id
, child_id
);
5469 * css_lookup - lookup css by id
5470 * @ss: cgroup subsys to be looked into.
5473 * Returns pointer to cgroup_subsys_state if there is valid one with id.
5474 * NULL if not. Should be called under rcu_read_lock()
5476 struct cgroup_subsys_state
*css_lookup(struct cgroup_subsys
*ss
, int id
)
5478 struct css_id
*cssid
= NULL
;
5480 BUG_ON(!ss
->use_id
);
5481 cssid
= idr_find(&ss
->idr
, id
);
5483 if (unlikely(!cssid
))
5486 return rcu_dereference(cssid
->css
);
5488 EXPORT_SYMBOL_GPL(css_lookup
);
5491 * get corresponding css from file open on cgroupfs directory
5493 struct cgroup_subsys_state
*cgroup_css_from_dir(struct file
*f
, int id
)
5495 struct cgroup
*cgrp
;
5496 struct inode
*inode
;
5497 struct cgroup_subsys_state
*css
;
5499 inode
= file_inode(f
);
5500 /* check in cgroup filesystem dir */
5501 if (inode
->i_op
!= &cgroup_dir_inode_operations
)
5502 return ERR_PTR(-EBADF
);
5504 if (id
< 0 || id
>= CGROUP_SUBSYS_COUNT
)
5505 return ERR_PTR(-EINVAL
);
5508 cgrp
= __d_cgrp(f
->f_dentry
);
5509 css
= cgrp
->subsys
[id
];
5510 return css
? css
: ERR_PTR(-ENOENT
);
5513 #ifdef CONFIG_CGROUP_DEBUG
5514 static struct cgroup_subsys_state
*debug_css_alloc(struct cgroup
*cgrp
)
5516 struct cgroup_subsys_state
*css
= kzalloc(sizeof(*css
), GFP_KERNEL
);
5519 return ERR_PTR(-ENOMEM
);
5524 static void debug_css_free(struct cgroup
*cgrp
)
5526 kfree(cgrp
->subsys
[debug_subsys_id
]);
5529 static u64
debug_taskcount_read(struct cgroup
*cgrp
, struct cftype
*cft
)
5531 return cgroup_task_count(cgrp
);
5534 static u64
current_css_set_read(struct cgroup
*cgrp
, struct cftype
*cft
)
5536 return (u64
)(unsigned long)current
->cgroups
;
5539 static u64
current_css_set_refcount_read(struct cgroup
*cgrp
,
5545 count
= atomic_read(¤t
->cgroups
->refcount
);
5550 static int current_css_set_cg_links_read(struct cgroup
*cgrp
,
5552 struct seq_file
*seq
)
5554 struct cgrp_cset_link
*link
;
5555 struct css_set
*cset
;
5557 read_lock(&css_set_lock
);
5559 cset
= rcu_dereference(current
->cgroups
);
5560 list_for_each_entry(link
, &cset
->cgrp_links
, cgrp_link
) {
5561 struct cgroup
*c
= link
->cgrp
;
5565 name
= c
->dentry
->d_name
.name
;
5568 seq_printf(seq
, "Root %d group %s\n",
5569 c
->root
->hierarchy_id
, name
);
5572 read_unlock(&css_set_lock
);
5576 #define MAX_TASKS_SHOWN_PER_CSS 25
5577 static int cgroup_css_links_read(struct cgroup
*cgrp
,
5579 struct seq_file
*seq
)
5581 struct cgrp_cset_link
*link
;
5583 read_lock(&css_set_lock
);
5584 list_for_each_entry(link
, &cgrp
->cset_links
, cset_link
) {
5585 struct css_set
*cset
= link
->cset
;
5586 struct task_struct
*task
;
5588 seq_printf(seq
, "css_set %p\n", cset
);
5589 list_for_each_entry(task
, &cset
->tasks
, cg_list
) {
5590 if (count
++ > MAX_TASKS_SHOWN_PER_CSS
) {
5591 seq_puts(seq
, " ...\n");
5594 seq_printf(seq
, " task %d\n",
5595 task_pid_vnr(task
));
5599 read_unlock(&css_set_lock
);
5603 static u64
releasable_read(struct cgroup
*cgrp
, struct cftype
*cft
)
5605 return test_bit(CGRP_RELEASABLE
, &cgrp
->flags
);
5608 static struct cftype debug_files
[] = {
5610 .name
= "taskcount",
5611 .read_u64
= debug_taskcount_read
,
5615 .name
= "current_css_set",
5616 .read_u64
= current_css_set_read
,
5620 .name
= "current_css_set_refcount",
5621 .read_u64
= current_css_set_refcount_read
,
5625 .name
= "current_css_set_cg_links",
5626 .read_seq_string
= current_css_set_cg_links_read
,
5630 .name
= "cgroup_css_links",
5631 .read_seq_string
= cgroup_css_links_read
,
5635 .name
= "releasable",
5636 .read_u64
= releasable_read
,
5642 struct cgroup_subsys debug_subsys
= {
5644 .css_alloc
= debug_css_alloc
,
5645 .css_free
= debug_css_free
,
5646 .subsys_id
= debug_subsys_id
,
5647 .base_cftypes
= debug_files
,
5649 #endif /* CONFIG_CGROUP_DEBUG */