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>
63 #include <linux/file.h>
65 #include <linux/atomic.h>
68 * cgroup_mutex is the master lock. Any modification to cgroup or its
69 * hierarchy must be performed while holding it.
71 * cgroup_root_mutex nests inside cgroup_mutex and should be held to modify
72 * cgroupfs_root of any cgroup hierarchy - subsys list, flags,
73 * release_agent_path and so on. Modifying requires both cgroup_mutex and
74 * cgroup_root_mutex. Readers can acquire either of the two. This is to
75 * break the following locking order cycle.
77 * A. cgroup_mutex -> cred_guard_mutex -> s_type->i_mutex_key -> namespace_sem
78 * B. namespace_sem -> cgroup_mutex
80 * B happens only through cgroup_show_options() and using cgroup_root_mutex
83 #ifdef CONFIG_PROVE_RCU
84 DEFINE_MUTEX(cgroup_mutex
);
85 EXPORT_SYMBOL_GPL(cgroup_mutex
); /* only for lockdep */
87 static DEFINE_MUTEX(cgroup_mutex
);
90 static DEFINE_MUTEX(cgroup_root_mutex
);
93 * cgroup destruction makes heavy use of work items and there can be a lot
94 * of concurrent destructions. Use a separate workqueue so that cgroup
95 * destruction work items don't end up filling up max_active of system_wq
96 * which may lead to deadlock.
98 static struct workqueue_struct
*cgroup_destroy_wq
;
101 * Generate an array of cgroup subsystem pointers. At boot time, this is
102 * populated with the built in subsystems, and modular subsystems are
103 * registered after that. The mutable section of this array is protected by
106 #define SUBSYS(_x) [_x ## _subsys_id] = &_x ## _subsys,
107 #define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option)
108 static struct cgroup_subsys
*cgroup_subsys
[CGROUP_SUBSYS_COUNT
] = {
109 #include <linux/cgroup_subsys.h>
113 * The dummy hierarchy, reserved for the subsystems that are otherwise
114 * unattached - it never has more than a single cgroup, and all tasks are
115 * part of that cgroup.
117 static struct cgroupfs_root cgroup_dummy_root
;
119 /* dummy_top is a shorthand for the dummy hierarchy's top cgroup */
120 static struct cgroup
* const cgroup_dummy_top
= &cgroup_dummy_root
.top_cgroup
;
123 * cgroupfs file entry, pointed to from leaf dentry->d_fsdata.
126 struct list_head node
;
127 struct dentry
*dentry
;
129 struct cgroup_subsys_state
*css
;
132 struct simple_xattrs xattrs
;
136 * cgroup_event represents events which userspace want to receive.
138 struct cgroup_event
{
140 * css which the event belongs to.
142 struct cgroup_subsys_state
*css
;
144 * Control file which the event associated.
148 * eventfd to signal userspace about the event.
150 struct eventfd_ctx
*eventfd
;
152 * Each of these stored in a list by the cgroup.
154 struct list_head list
;
156 * All fields below needed to unregister event when
157 * userspace closes eventfd.
160 wait_queue_head_t
*wqh
;
162 struct work_struct remove
;
165 /* The list of hierarchy roots */
167 static LIST_HEAD(cgroup_roots
);
168 static int cgroup_root_count
;
171 * Hierarchy ID allocation and mapping. It follows the same exclusion
172 * rules as other root ops - both cgroup_mutex and cgroup_root_mutex for
173 * writes, either for reads.
175 static DEFINE_IDR(cgroup_hierarchy_idr
);
177 static struct cgroup_name root_cgroup_name
= { .name
= "/" };
180 * Assign a monotonically increasing serial number to cgroups. It
181 * guarantees cgroups with bigger numbers are newer than those with smaller
182 * numbers. Also, as cgroups are always appended to the parent's
183 * ->children list, it guarantees that sibling cgroups are always sorted in
184 * the ascending serial number order on the list. Protected by
187 static u64 cgroup_serial_nr_next
= 1;
189 /* This flag indicates whether tasks in the fork and exit paths should
190 * check for fork/exit handlers to call. This avoids us having to do
191 * extra work in the fork/exit path if none of the subsystems need to
194 static int need_forkexit_callback __read_mostly
;
196 static struct cftype cgroup_base_files
[];
198 static void cgroup_destroy_css_killed(struct cgroup
*cgrp
);
199 static int cgroup_destroy_locked(struct cgroup
*cgrp
);
200 static int cgroup_addrm_files(struct cgroup
*cgrp
, struct cftype cfts
[],
202 static int cgroup_file_release(struct inode
*inode
, struct file
*file
);
205 * cgroup_css - obtain a cgroup's css for the specified subsystem
206 * @cgrp: the cgroup of interest
207 * @ss: the subsystem of interest (%NULL returns the dummy_css)
209 * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This
210 * function must be called either under cgroup_mutex or rcu_read_lock() and
211 * the caller is responsible for pinning the returned css if it wants to
212 * keep accessing it outside the said locks. This function may return
213 * %NULL if @cgrp doesn't have @subsys_id enabled.
215 static struct cgroup_subsys_state
*cgroup_css(struct cgroup
*cgrp
,
216 struct cgroup_subsys
*ss
)
219 return rcu_dereference_check(cgrp
->subsys
[ss
->subsys_id
],
220 lockdep_is_held(&cgroup_mutex
));
222 return &cgrp
->dummy_css
;
225 /* convenient tests for these bits */
226 static inline bool cgroup_is_dead(const struct cgroup
*cgrp
)
228 return test_bit(CGRP_DEAD
, &cgrp
->flags
);
232 * cgroup_is_descendant - test ancestry
233 * @cgrp: the cgroup to be tested
234 * @ancestor: possible ancestor of @cgrp
236 * Test whether @cgrp is a descendant of @ancestor. It also returns %true
237 * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
238 * and @ancestor are accessible.
240 bool cgroup_is_descendant(struct cgroup
*cgrp
, struct cgroup
*ancestor
)
243 if (cgrp
== ancestor
)
249 EXPORT_SYMBOL_GPL(cgroup_is_descendant
);
251 static int cgroup_is_releasable(const struct cgroup
*cgrp
)
254 (1 << CGRP_RELEASABLE
) |
255 (1 << CGRP_NOTIFY_ON_RELEASE
);
256 return (cgrp
->flags
& bits
) == bits
;
259 static int notify_on_release(const struct cgroup
*cgrp
)
261 return test_bit(CGRP_NOTIFY_ON_RELEASE
, &cgrp
->flags
);
265 * for_each_subsys - iterate all loaded cgroup subsystems
266 * @ss: the iteration cursor
267 * @i: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
269 * Should be called under cgroup_mutex.
271 #define for_each_subsys(ss, i) \
272 for ((i) = 0; (i) < CGROUP_SUBSYS_COUNT; (i)++) \
273 if (({ lockdep_assert_held(&cgroup_mutex); \
274 !((ss) = cgroup_subsys[i]); })) { } \
278 * for_each_builtin_subsys - iterate all built-in cgroup subsystems
279 * @ss: the iteration cursor
280 * @i: the index of @ss, CGROUP_BUILTIN_SUBSYS_COUNT after reaching the end
282 * Bulit-in subsystems are always present and iteration itself doesn't
283 * require any synchronization.
285 #define for_each_builtin_subsys(ss, i) \
286 for ((i) = 0; (i) < CGROUP_BUILTIN_SUBSYS_COUNT && \
287 (((ss) = cgroup_subsys[i]) || true); (i)++)
289 /* iterate each subsystem attached to a hierarchy */
290 #define for_each_root_subsys(root, ss) \
291 list_for_each_entry((ss), &(root)->subsys_list, sibling)
293 /* iterate across the active hierarchies */
294 #define for_each_active_root(root) \
295 list_for_each_entry((root), &cgroup_roots, root_list)
297 static inline struct cgroup
*__d_cgrp(struct dentry
*dentry
)
299 return dentry
->d_fsdata
;
302 static inline struct cfent
*__d_cfe(struct dentry
*dentry
)
304 return dentry
->d_fsdata
;
307 static inline struct cftype
*__d_cft(struct dentry
*dentry
)
309 return __d_cfe(dentry
)->type
;
313 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
314 * @cgrp: the cgroup to be checked for liveness
316 * On success, returns true; the mutex should be later unlocked. On
317 * failure returns false with no lock held.
319 static bool cgroup_lock_live_group(struct cgroup
*cgrp
)
321 mutex_lock(&cgroup_mutex
);
322 if (cgroup_is_dead(cgrp
)) {
323 mutex_unlock(&cgroup_mutex
);
329 /* the list of cgroups eligible for automatic release. Protected by
330 * release_list_lock */
331 static LIST_HEAD(release_list
);
332 static DEFINE_RAW_SPINLOCK(release_list_lock
);
333 static void cgroup_release_agent(struct work_struct
*work
);
334 static DECLARE_WORK(release_agent_work
, cgroup_release_agent
);
335 static void check_for_release(struct cgroup
*cgrp
);
338 * A cgroup can be associated with multiple css_sets as different tasks may
339 * belong to different cgroups on different hierarchies. In the other
340 * direction, a css_set is naturally associated with multiple cgroups.
341 * This M:N relationship is represented by the following link structure
342 * which exists for each association and allows traversing the associations
345 struct cgrp_cset_link
{
346 /* the cgroup and css_set this link associates */
348 struct css_set
*cset
;
350 /* list of cgrp_cset_links anchored at cgrp->cset_links */
351 struct list_head cset_link
;
353 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
354 struct list_head cgrp_link
;
357 /* The default css_set - used by init and its children prior to any
358 * hierarchies being mounted. It contains a pointer to the root state
359 * for each subsystem. Also used to anchor the list of css_sets. Not
360 * reference-counted, to improve performance when child cgroups
361 * haven't been created.
364 static struct css_set init_css_set
;
365 static struct cgrp_cset_link init_cgrp_cset_link
;
368 * css_set_lock protects the list of css_set objects, and the chain of
369 * tasks off each css_set. Nests outside task->alloc_lock due to
370 * css_task_iter_start().
372 static DEFINE_RWLOCK(css_set_lock
);
373 static int css_set_count
;
376 * hash table for cgroup groups. This improves the performance to find
377 * an existing css_set. This hash doesn't (currently) take into
378 * account cgroups in empty hierarchies.
380 #define CSS_SET_HASH_BITS 7
381 static DEFINE_HASHTABLE(css_set_table
, CSS_SET_HASH_BITS
);
383 static unsigned long css_set_hash(struct cgroup_subsys_state
*css
[])
385 unsigned long key
= 0UL;
386 struct cgroup_subsys
*ss
;
389 for_each_subsys(ss
, i
)
390 key
+= (unsigned long)css
[i
];
391 key
= (key
>> 16) ^ key
;
397 * We don't maintain the lists running through each css_set to its task
398 * until after the first call to css_task_iter_start(). This reduces the
399 * fork()/exit() overhead for people who have cgroups compiled into their
400 * kernel but not actually in use.
402 static int use_task_css_set_links __read_mostly
;
404 static void __put_css_set(struct css_set
*cset
, int taskexit
)
406 struct cgrp_cset_link
*link
, *tmp_link
;
409 * Ensure that the refcount doesn't hit zero while any readers
410 * can see it. Similar to atomic_dec_and_lock(), but for an
413 if (atomic_add_unless(&cset
->refcount
, -1, 1))
415 write_lock(&css_set_lock
);
416 if (!atomic_dec_and_test(&cset
->refcount
)) {
417 write_unlock(&css_set_lock
);
421 /* This css_set is dead. unlink it and release cgroup refcounts */
422 hash_del(&cset
->hlist
);
425 list_for_each_entry_safe(link
, tmp_link
, &cset
->cgrp_links
, cgrp_link
) {
426 struct cgroup
*cgrp
= link
->cgrp
;
428 list_del(&link
->cset_link
);
429 list_del(&link
->cgrp_link
);
431 /* @cgrp can't go away while we're holding css_set_lock */
432 if (list_empty(&cgrp
->cset_links
) && notify_on_release(cgrp
)) {
434 set_bit(CGRP_RELEASABLE
, &cgrp
->flags
);
435 check_for_release(cgrp
);
441 write_unlock(&css_set_lock
);
442 kfree_rcu(cset
, rcu_head
);
446 * refcounted get/put for css_set objects
448 static inline void get_css_set(struct css_set
*cset
)
450 atomic_inc(&cset
->refcount
);
453 static inline void put_css_set(struct css_set
*cset
)
455 __put_css_set(cset
, 0);
458 static inline void put_css_set_taskexit(struct css_set
*cset
)
460 __put_css_set(cset
, 1);
464 * compare_css_sets - helper function for find_existing_css_set().
465 * @cset: candidate css_set being tested
466 * @old_cset: existing css_set for a task
467 * @new_cgrp: cgroup that's being entered by the task
468 * @template: desired set of css pointers in css_set (pre-calculated)
470 * Returns true if "cset" matches "old_cset" except for the hierarchy
471 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
473 static bool compare_css_sets(struct css_set
*cset
,
474 struct css_set
*old_cset
,
475 struct cgroup
*new_cgrp
,
476 struct cgroup_subsys_state
*template[])
478 struct list_head
*l1
, *l2
;
480 if (memcmp(template, cset
->subsys
, sizeof(cset
->subsys
))) {
481 /* Not all subsystems matched */
486 * Compare cgroup pointers in order to distinguish between
487 * different cgroups in heirarchies with no subsystems. We
488 * could get by with just this check alone (and skip the
489 * memcmp above) but on most setups the memcmp check will
490 * avoid the need for this more expensive check on almost all
494 l1
= &cset
->cgrp_links
;
495 l2
= &old_cset
->cgrp_links
;
497 struct cgrp_cset_link
*link1
, *link2
;
498 struct cgroup
*cgrp1
, *cgrp2
;
502 /* See if we reached the end - both lists are equal length. */
503 if (l1
== &cset
->cgrp_links
) {
504 BUG_ON(l2
!= &old_cset
->cgrp_links
);
507 BUG_ON(l2
== &old_cset
->cgrp_links
);
509 /* Locate the cgroups associated with these links. */
510 link1
= list_entry(l1
, struct cgrp_cset_link
, cgrp_link
);
511 link2
= list_entry(l2
, struct cgrp_cset_link
, cgrp_link
);
514 /* Hierarchies should be linked in the same order. */
515 BUG_ON(cgrp1
->root
!= cgrp2
->root
);
518 * If this hierarchy is the hierarchy of the cgroup
519 * that's changing, then we need to check that this
520 * css_set points to the new cgroup; if it's any other
521 * hierarchy, then this css_set should point to the
522 * same cgroup as the old css_set.
524 if (cgrp1
->root
== new_cgrp
->root
) {
525 if (cgrp1
!= new_cgrp
)
536 * find_existing_css_set - init css array and find the matching css_set
537 * @old_cset: the css_set that we're using before the cgroup transition
538 * @cgrp: the cgroup that we're moving into
539 * @template: out param for the new set of csses, should be clear on entry
541 static struct css_set
*find_existing_css_set(struct css_set
*old_cset
,
543 struct cgroup_subsys_state
*template[])
545 struct cgroupfs_root
*root
= cgrp
->root
;
546 struct cgroup_subsys
*ss
;
547 struct css_set
*cset
;
552 * Build the set of subsystem state objects that we want to see in the
553 * new css_set. while subsystems can change globally, the entries here
554 * won't change, so no need for locking.
556 for_each_subsys(ss
, i
) {
557 if (root
->subsys_mask
& (1UL << i
)) {
558 /* Subsystem is in this hierarchy. So we want
559 * the subsystem state from the new
561 template[i
] = cgroup_css(cgrp
, ss
);
563 /* Subsystem is not in this hierarchy, so we
564 * don't want to change the subsystem state */
565 template[i
] = old_cset
->subsys
[i
];
569 key
= css_set_hash(template);
570 hash_for_each_possible(css_set_table
, cset
, hlist
, key
) {
571 if (!compare_css_sets(cset
, old_cset
, cgrp
, template))
574 /* This css_set matches what we need */
578 /* No existing cgroup group matched */
582 static void free_cgrp_cset_links(struct list_head
*links_to_free
)
584 struct cgrp_cset_link
*link
, *tmp_link
;
586 list_for_each_entry_safe(link
, tmp_link
, links_to_free
, cset_link
) {
587 list_del(&link
->cset_link
);
593 * allocate_cgrp_cset_links - allocate cgrp_cset_links
594 * @count: the number of links to allocate
595 * @tmp_links: list_head the allocated links are put on
597 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
598 * through ->cset_link. Returns 0 on success or -errno.
600 static int allocate_cgrp_cset_links(int count
, struct list_head
*tmp_links
)
602 struct cgrp_cset_link
*link
;
605 INIT_LIST_HEAD(tmp_links
);
607 for (i
= 0; i
< count
; i
++) {
608 link
= kzalloc(sizeof(*link
), GFP_KERNEL
);
610 free_cgrp_cset_links(tmp_links
);
613 list_add(&link
->cset_link
, tmp_links
);
619 * link_css_set - a helper function to link a css_set to a cgroup
620 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
621 * @cset: the css_set to be linked
622 * @cgrp: the destination cgroup
624 static void link_css_set(struct list_head
*tmp_links
, struct css_set
*cset
,
627 struct cgrp_cset_link
*link
;
629 BUG_ON(list_empty(tmp_links
));
630 link
= list_first_entry(tmp_links
, struct cgrp_cset_link
, cset_link
);
633 list_move(&link
->cset_link
, &cgrp
->cset_links
);
635 * Always add links to the tail of the list so that the list
636 * is sorted by order of hierarchy creation
638 list_add_tail(&link
->cgrp_link
, &cset
->cgrp_links
);
642 * find_css_set - return a new css_set with one cgroup updated
643 * @old_cset: the baseline css_set
644 * @cgrp: the cgroup to be updated
646 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
647 * substituted into the appropriate hierarchy.
649 static struct css_set
*find_css_set(struct css_set
*old_cset
,
652 struct cgroup_subsys_state
*template[CGROUP_SUBSYS_COUNT
] = { };
653 struct css_set
*cset
;
654 struct list_head tmp_links
;
655 struct cgrp_cset_link
*link
;
658 lockdep_assert_held(&cgroup_mutex
);
660 /* First see if we already have a cgroup group that matches
662 read_lock(&css_set_lock
);
663 cset
= find_existing_css_set(old_cset
, cgrp
, template);
666 read_unlock(&css_set_lock
);
671 cset
= kzalloc(sizeof(*cset
), GFP_KERNEL
);
675 /* Allocate all the cgrp_cset_link objects that we'll need */
676 if (allocate_cgrp_cset_links(cgroup_root_count
, &tmp_links
) < 0) {
681 atomic_set(&cset
->refcount
, 1);
682 INIT_LIST_HEAD(&cset
->cgrp_links
);
683 INIT_LIST_HEAD(&cset
->tasks
);
684 INIT_HLIST_NODE(&cset
->hlist
);
686 /* Copy the set of subsystem state objects generated in
687 * find_existing_css_set() */
688 memcpy(cset
->subsys
, template, sizeof(cset
->subsys
));
690 write_lock(&css_set_lock
);
691 /* Add reference counts and links from the new css_set. */
692 list_for_each_entry(link
, &old_cset
->cgrp_links
, cgrp_link
) {
693 struct cgroup
*c
= link
->cgrp
;
695 if (c
->root
== cgrp
->root
)
697 link_css_set(&tmp_links
, cset
, c
);
700 BUG_ON(!list_empty(&tmp_links
));
704 /* Add this cgroup group to the hash table */
705 key
= css_set_hash(cset
->subsys
);
706 hash_add(css_set_table
, &cset
->hlist
, key
);
708 write_unlock(&css_set_lock
);
714 * Return the cgroup for "task" from the given hierarchy. Must be
715 * called with cgroup_mutex held.
717 static struct cgroup
*task_cgroup_from_root(struct task_struct
*task
,
718 struct cgroupfs_root
*root
)
720 struct css_set
*cset
;
721 struct cgroup
*res
= NULL
;
723 BUG_ON(!mutex_is_locked(&cgroup_mutex
));
724 read_lock(&css_set_lock
);
726 * No need to lock the task - since we hold cgroup_mutex the
727 * task can't change groups, so the only thing that can happen
728 * is that it exits and its css is set back to init_css_set.
730 cset
= task_css_set(task
);
731 if (cset
== &init_css_set
) {
732 res
= &root
->top_cgroup
;
734 struct cgrp_cset_link
*link
;
736 list_for_each_entry(link
, &cset
->cgrp_links
, cgrp_link
) {
737 struct cgroup
*c
= link
->cgrp
;
739 if (c
->root
== root
) {
745 read_unlock(&css_set_lock
);
751 * There is one global cgroup mutex. We also require taking
752 * task_lock() when dereferencing a task's cgroup subsys pointers.
753 * See "The task_lock() exception", at the end of this comment.
755 * A task must hold cgroup_mutex to modify cgroups.
757 * Any task can increment and decrement the count field without lock.
758 * So in general, code holding cgroup_mutex can't rely on the count
759 * field not changing. However, if the count goes to zero, then only
760 * cgroup_attach_task() can increment it again. Because a count of zero
761 * means that no tasks are currently attached, therefore there is no
762 * way a task attached to that cgroup can fork (the other way to
763 * increment the count). So code holding cgroup_mutex can safely
764 * assume that if the count is zero, it will stay zero. Similarly, if
765 * a task holds cgroup_mutex on a cgroup with zero count, it
766 * knows that the cgroup won't be removed, as cgroup_rmdir()
769 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
770 * (usually) take cgroup_mutex. These are the two most performance
771 * critical pieces of code here. The exception occurs on cgroup_exit(),
772 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
773 * is taken, and if the cgroup count is zero, a usermode call made
774 * to the release agent with the name of the cgroup (path relative to
775 * the root of cgroup file system) as the argument.
777 * A cgroup can only be deleted if both its 'count' of using tasks
778 * is zero, and its list of 'children' cgroups is empty. Since all
779 * tasks in the system use _some_ cgroup, and since there is always at
780 * least one task in the system (init, pid == 1), therefore, top_cgroup
781 * always has either children cgroups and/or using tasks. So we don't
782 * need a special hack to ensure that top_cgroup cannot be deleted.
784 * The task_lock() exception
786 * The need for this exception arises from the action of
787 * cgroup_attach_task(), which overwrites one task's cgroup pointer with
788 * another. It does so using cgroup_mutex, however there are
789 * several performance critical places that need to reference
790 * task->cgroup without the expense of grabbing a system global
791 * mutex. Therefore except as noted below, when dereferencing or, as
792 * in cgroup_attach_task(), modifying a task's cgroup pointer we use
793 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
794 * the task_struct routinely used for such matters.
796 * P.S. One more locking exception. RCU is used to guard the
797 * update of a tasks cgroup pointer by cgroup_attach_task()
801 * A couple of forward declarations required, due to cyclic reference loop:
802 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
803 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
807 static int cgroup_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
);
808 static int cgroup_rmdir(struct inode
*unused_dir
, struct dentry
*dentry
);
809 static int cgroup_populate_dir(struct cgroup
*cgrp
, unsigned long subsys_mask
);
810 static const struct inode_operations cgroup_dir_inode_operations
;
811 static const struct file_operations proc_cgroupstats_operations
;
813 static struct backing_dev_info cgroup_backing_dev_info
= {
815 .capabilities
= BDI_CAP_NO_ACCT_AND_WRITEBACK
,
818 static struct inode
*cgroup_new_inode(umode_t mode
, struct super_block
*sb
)
820 struct inode
*inode
= new_inode(sb
);
823 inode
->i_ino
= get_next_ino();
824 inode
->i_mode
= mode
;
825 inode
->i_uid
= current_fsuid();
826 inode
->i_gid
= current_fsgid();
827 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
828 inode
->i_mapping
->backing_dev_info
= &cgroup_backing_dev_info
;
833 static struct cgroup_name
*cgroup_alloc_name(struct dentry
*dentry
)
835 struct cgroup_name
*name
;
837 name
= kmalloc(sizeof(*name
) + dentry
->d_name
.len
+ 1, GFP_KERNEL
);
840 strcpy(name
->name
, dentry
->d_name
.name
);
844 static void cgroup_free_fn(struct work_struct
*work
)
846 struct cgroup
*cgrp
= container_of(work
, struct cgroup
, destroy_work
);
848 mutex_lock(&cgroup_mutex
);
849 cgrp
->root
->number_of_cgroups
--;
850 mutex_unlock(&cgroup_mutex
);
853 * We get a ref to the parent's dentry, and put the ref when
854 * this cgroup is being freed, so it's guaranteed that the
855 * parent won't be destroyed before its children.
857 dput(cgrp
->parent
->dentry
);
860 * Drop the active superblock reference that we took when we
861 * created the cgroup. This will free cgrp->root, if we are
862 * holding the last reference to @sb.
864 deactivate_super(cgrp
->root
->sb
);
867 * if we're getting rid of the cgroup, refcount should ensure
868 * that there are no pidlists left.
870 BUG_ON(!list_empty(&cgrp
->pidlists
));
872 simple_xattrs_free(&cgrp
->xattrs
);
874 kfree(rcu_dereference_raw(cgrp
->name
));
878 static void cgroup_free_rcu(struct rcu_head
*head
)
880 struct cgroup
*cgrp
= container_of(head
, struct cgroup
, rcu_head
);
882 INIT_WORK(&cgrp
->destroy_work
, cgroup_free_fn
);
883 queue_work(cgroup_destroy_wq
, &cgrp
->destroy_work
);
886 static void cgroup_diput(struct dentry
*dentry
, struct inode
*inode
)
888 /* is dentry a directory ? if so, kfree() associated cgroup */
889 if (S_ISDIR(inode
->i_mode
)) {
890 struct cgroup
*cgrp
= dentry
->d_fsdata
;
892 BUG_ON(!(cgroup_is_dead(cgrp
)));
893 call_rcu(&cgrp
->rcu_head
, cgroup_free_rcu
);
895 struct cfent
*cfe
= __d_cfe(dentry
);
896 struct cgroup
*cgrp
= dentry
->d_parent
->d_fsdata
;
898 WARN_ONCE(!list_empty(&cfe
->node
) &&
899 cgrp
!= &cgrp
->root
->top_cgroup
,
900 "cfe still linked for %s\n", cfe
->type
->name
);
901 simple_xattrs_free(&cfe
->xattrs
);
907 static void remove_dir(struct dentry
*d
)
909 struct dentry
*parent
= dget(d
->d_parent
);
912 simple_rmdir(parent
->d_inode
, d
);
916 static void cgroup_rm_file(struct cgroup
*cgrp
, const struct cftype
*cft
)
920 lockdep_assert_held(&cgrp
->dentry
->d_inode
->i_mutex
);
921 lockdep_assert_held(&cgroup_mutex
);
924 * If we're doing cleanup due to failure of cgroup_create(),
925 * the corresponding @cfe may not exist.
927 list_for_each_entry(cfe
, &cgrp
->files
, node
) {
928 struct dentry
*d
= cfe
->dentry
;
930 if (cft
&& cfe
->type
!= cft
)
935 simple_unlink(cgrp
->dentry
->d_inode
, d
);
936 list_del_init(&cfe
->node
);
944 * cgroup_clear_dir - remove subsys files in a cgroup directory
945 * @cgrp: target cgroup
946 * @subsys_mask: mask of the subsystem ids whose files should be removed
948 static void cgroup_clear_dir(struct cgroup
*cgrp
, unsigned long subsys_mask
)
950 struct cgroup_subsys
*ss
;
953 for_each_subsys(ss
, i
) {
954 struct cftype_set
*set
;
956 if (!test_bit(i
, &subsys_mask
))
958 list_for_each_entry(set
, &ss
->cftsets
, node
)
959 cgroup_addrm_files(cgrp
, set
->cfts
, false);
964 * NOTE : the dentry must have been dget()'ed
966 static void cgroup_d_remove_dir(struct dentry
*dentry
)
968 struct dentry
*parent
;
970 parent
= dentry
->d_parent
;
971 spin_lock(&parent
->d_lock
);
972 spin_lock_nested(&dentry
->d_lock
, DENTRY_D_LOCK_NESTED
);
973 list_del_init(&dentry
->d_u
.d_child
);
974 spin_unlock(&dentry
->d_lock
);
975 spin_unlock(&parent
->d_lock
);
980 * Call with cgroup_mutex held. Drops reference counts on modules, including
981 * any duplicate ones that parse_cgroupfs_options took. If this function
982 * returns an error, no reference counts are touched.
984 static int rebind_subsystems(struct cgroupfs_root
*root
,
985 unsigned long added_mask
, unsigned removed_mask
)
987 struct cgroup
*cgrp
= &root
->top_cgroup
;
988 struct cgroup_subsys
*ss
;
989 unsigned long pinned
= 0;
992 BUG_ON(!mutex_is_locked(&cgroup_mutex
));
993 BUG_ON(!mutex_is_locked(&cgroup_root_mutex
));
995 /* Check that any added subsystems are currently free */
996 for_each_subsys(ss
, i
) {
997 if (!(added_mask
& (1 << i
)))
1000 /* is the subsystem mounted elsewhere? */
1001 if (ss
->root
!= &cgroup_dummy_root
) {
1006 /* pin the module */
1007 if (!try_module_get(ss
->module
)) {
1014 /* subsys could be missing if unloaded between parsing and here */
1015 if (added_mask
!= pinned
) {
1020 ret
= cgroup_populate_dir(cgrp
, added_mask
);
1025 * Nothing can fail from this point on. Remove files for the
1026 * removed subsystems and rebind each subsystem.
1028 cgroup_clear_dir(cgrp
, removed_mask
);
1030 for_each_subsys(ss
, i
) {
1031 unsigned long bit
= 1UL << i
;
1033 if (bit
& added_mask
) {
1034 /* We're binding this subsystem to this hierarchy */
1035 BUG_ON(cgroup_css(cgrp
, ss
));
1036 BUG_ON(!cgroup_css(cgroup_dummy_top
, ss
));
1037 BUG_ON(cgroup_css(cgroup_dummy_top
, ss
)->cgroup
!= cgroup_dummy_top
);
1039 rcu_assign_pointer(cgrp
->subsys
[i
],
1040 cgroup_css(cgroup_dummy_top
, ss
));
1041 cgroup_css(cgrp
, ss
)->cgroup
= cgrp
;
1043 list_move(&ss
->sibling
, &root
->subsys_list
);
1046 ss
->bind(cgroup_css(cgrp
, ss
));
1048 /* refcount was already taken, and we're keeping it */
1049 root
->subsys_mask
|= bit
;
1050 } else if (bit
& removed_mask
) {
1051 /* We're removing this subsystem */
1052 BUG_ON(cgroup_css(cgrp
, ss
) != cgroup_css(cgroup_dummy_top
, ss
));
1053 BUG_ON(cgroup_css(cgrp
, ss
)->cgroup
!= cgrp
);
1056 ss
->bind(cgroup_css(cgroup_dummy_top
, ss
));
1058 cgroup_css(cgroup_dummy_top
, ss
)->cgroup
= cgroup_dummy_top
;
1059 RCU_INIT_POINTER(cgrp
->subsys
[i
], NULL
);
1061 cgroup_subsys
[i
]->root
= &cgroup_dummy_root
;
1062 list_move(&ss
->sibling
, &cgroup_dummy_root
.subsys_list
);
1064 /* subsystem is now free - drop reference on module */
1065 module_put(ss
->module
);
1066 root
->subsys_mask
&= ~bit
;
1071 * Mark @root has finished binding subsystems. @root->subsys_mask
1072 * now matches the bound subsystems.
1074 root
->flags
|= CGRP_ROOT_SUBSYS_BOUND
;
1079 for_each_subsys(ss
, i
)
1080 if (pinned
& (1 << i
))
1081 module_put(ss
->module
);
1085 static int cgroup_show_options(struct seq_file
*seq
, struct dentry
*dentry
)
1087 struct cgroupfs_root
*root
= dentry
->d_sb
->s_fs_info
;
1088 struct cgroup_subsys
*ss
;
1090 mutex_lock(&cgroup_root_mutex
);
1091 for_each_root_subsys(root
, ss
)
1092 seq_printf(seq
, ",%s", ss
->name
);
1093 if (root
->flags
& CGRP_ROOT_SANE_BEHAVIOR
)
1094 seq_puts(seq
, ",sane_behavior");
1095 if (root
->flags
& CGRP_ROOT_NOPREFIX
)
1096 seq_puts(seq
, ",noprefix");
1097 if (root
->flags
& CGRP_ROOT_XATTR
)
1098 seq_puts(seq
, ",xattr");
1099 if (strlen(root
->release_agent_path
))
1100 seq_printf(seq
, ",release_agent=%s", root
->release_agent_path
);
1101 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN
, &root
->top_cgroup
.flags
))
1102 seq_puts(seq
, ",clone_children");
1103 if (strlen(root
->name
))
1104 seq_printf(seq
, ",name=%s", root
->name
);
1105 mutex_unlock(&cgroup_root_mutex
);
1109 struct cgroup_sb_opts
{
1110 unsigned long subsys_mask
;
1111 unsigned long flags
;
1112 char *release_agent
;
1113 bool cpuset_clone_children
;
1115 /* User explicitly requested empty subsystem */
1118 struct cgroupfs_root
*new_root
;
1123 * Convert a hierarchy specifier into a bitmask of subsystems and
1124 * flags. Call with cgroup_mutex held to protect the cgroup_subsys[]
1125 * array. This function takes refcounts on subsystems to be used, unless it
1126 * returns error, in which case no refcounts are taken.
1128 static int parse_cgroupfs_options(char *data
, struct cgroup_sb_opts
*opts
)
1130 char *token
, *o
= data
;
1131 bool all_ss
= false, one_ss
= false;
1132 unsigned long mask
= (unsigned long)-1;
1133 struct cgroup_subsys
*ss
;
1136 BUG_ON(!mutex_is_locked(&cgroup_mutex
));
1138 #ifdef CONFIG_CPUSETS
1139 mask
= ~(1UL << cpuset_subsys_id
);
1142 memset(opts
, 0, sizeof(*opts
));
1144 while ((token
= strsep(&o
, ",")) != NULL
) {
1147 if (!strcmp(token
, "none")) {
1148 /* Explicitly have no subsystems */
1152 if (!strcmp(token
, "all")) {
1153 /* Mutually exclusive option 'all' + subsystem name */
1159 if (!strcmp(token
, "__DEVEL__sane_behavior")) {
1160 opts
->flags
|= CGRP_ROOT_SANE_BEHAVIOR
;
1163 if (!strcmp(token
, "noprefix")) {
1164 opts
->flags
|= CGRP_ROOT_NOPREFIX
;
1167 if (!strcmp(token
, "clone_children")) {
1168 opts
->cpuset_clone_children
= true;
1171 if (!strcmp(token
, "xattr")) {
1172 opts
->flags
|= CGRP_ROOT_XATTR
;
1175 if (!strncmp(token
, "release_agent=", 14)) {
1176 /* Specifying two release agents is forbidden */
1177 if (opts
->release_agent
)
1179 opts
->release_agent
=
1180 kstrndup(token
+ 14, PATH_MAX
- 1, GFP_KERNEL
);
1181 if (!opts
->release_agent
)
1185 if (!strncmp(token
, "name=", 5)) {
1186 const char *name
= token
+ 5;
1187 /* Can't specify an empty name */
1190 /* Must match [\w.-]+ */
1191 for (i
= 0; i
< strlen(name
); i
++) {
1195 if ((c
== '.') || (c
== '-') || (c
== '_'))
1199 /* Specifying two names is forbidden */
1202 opts
->name
= kstrndup(name
,
1203 MAX_CGROUP_ROOT_NAMELEN
- 1,
1211 for_each_subsys(ss
, i
) {
1212 if (strcmp(token
, ss
->name
))
1217 /* Mutually exclusive option 'all' + subsystem name */
1220 set_bit(i
, &opts
->subsys_mask
);
1225 if (i
== CGROUP_SUBSYS_COUNT
)
1230 * If the 'all' option was specified select all the subsystems,
1231 * otherwise if 'none', 'name=' and a subsystem name options
1232 * were not specified, let's default to 'all'
1234 if (all_ss
|| (!one_ss
&& !opts
->none
&& !opts
->name
))
1235 for_each_subsys(ss
, i
)
1237 set_bit(i
, &opts
->subsys_mask
);
1239 /* Consistency checks */
1241 if (opts
->flags
& CGRP_ROOT_SANE_BEHAVIOR
) {
1242 pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
1244 if (opts
->flags
& CGRP_ROOT_NOPREFIX
) {
1245 pr_err("cgroup: sane_behavior: noprefix is not allowed\n");
1249 if (opts
->cpuset_clone_children
) {
1250 pr_err("cgroup: sane_behavior: clone_children is not allowed\n");
1256 * Option noprefix was introduced just for backward compatibility
1257 * with the old cpuset, so we allow noprefix only if mounting just
1258 * the cpuset subsystem.
1260 if ((opts
->flags
& CGRP_ROOT_NOPREFIX
) && (opts
->subsys_mask
& mask
))
1264 /* Can't specify "none" and some subsystems */
1265 if (opts
->subsys_mask
&& opts
->none
)
1269 * We either have to specify by name or by subsystems. (So all
1270 * empty hierarchies must have a name).
1272 if (!opts
->subsys_mask
&& !opts
->name
)
1278 static int cgroup_remount(struct super_block
*sb
, int *flags
, char *data
)
1281 struct cgroupfs_root
*root
= sb
->s_fs_info
;
1282 struct cgroup
*cgrp
= &root
->top_cgroup
;
1283 struct cgroup_sb_opts opts
;
1284 unsigned long added_mask
, removed_mask
;
1286 if (root
->flags
& CGRP_ROOT_SANE_BEHAVIOR
) {
1287 pr_err("cgroup: sane_behavior: remount is not allowed\n");
1291 mutex_lock(&cgrp
->dentry
->d_inode
->i_mutex
);
1292 mutex_lock(&cgroup_mutex
);
1293 mutex_lock(&cgroup_root_mutex
);
1295 /* See what subsystems are wanted */
1296 ret
= parse_cgroupfs_options(data
, &opts
);
1300 if (opts
.subsys_mask
!= root
->subsys_mask
|| opts
.release_agent
)
1301 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1302 task_tgid_nr(current
), current
->comm
);
1304 added_mask
= opts
.subsys_mask
& ~root
->subsys_mask
;
1305 removed_mask
= root
->subsys_mask
& ~opts
.subsys_mask
;
1307 /* Don't allow flags or name to change at remount */
1308 if (((opts
.flags
^ root
->flags
) & CGRP_ROOT_OPTION_MASK
) ||
1309 (opts
.name
&& strcmp(opts
.name
, root
->name
))) {
1310 pr_err("cgroup: option or name mismatch, new: 0x%lx \"%s\", old: 0x%lx \"%s\"\n",
1311 opts
.flags
& CGRP_ROOT_OPTION_MASK
, opts
.name
?: "",
1312 root
->flags
& CGRP_ROOT_OPTION_MASK
, root
->name
);
1317 /* remounting is not allowed for populated hierarchies */
1318 if (root
->number_of_cgroups
> 1) {
1323 ret
= rebind_subsystems(root
, added_mask
, removed_mask
);
1327 if (opts
.release_agent
)
1328 strcpy(root
->release_agent_path
, opts
.release_agent
);
1330 kfree(opts
.release_agent
);
1332 mutex_unlock(&cgroup_root_mutex
);
1333 mutex_unlock(&cgroup_mutex
);
1334 mutex_unlock(&cgrp
->dentry
->d_inode
->i_mutex
);
1338 static const struct super_operations cgroup_ops
= {
1339 .statfs
= simple_statfs
,
1340 .drop_inode
= generic_delete_inode
,
1341 .show_options
= cgroup_show_options
,
1342 .remount_fs
= cgroup_remount
,
1345 static void init_cgroup_housekeeping(struct cgroup
*cgrp
)
1347 INIT_LIST_HEAD(&cgrp
->sibling
);
1348 INIT_LIST_HEAD(&cgrp
->children
);
1349 INIT_LIST_HEAD(&cgrp
->files
);
1350 INIT_LIST_HEAD(&cgrp
->cset_links
);
1351 INIT_LIST_HEAD(&cgrp
->release_list
);
1352 INIT_LIST_HEAD(&cgrp
->pidlists
);
1353 mutex_init(&cgrp
->pidlist_mutex
);
1354 cgrp
->dummy_css
.cgroup
= cgrp
;
1355 INIT_LIST_HEAD(&cgrp
->event_list
);
1356 spin_lock_init(&cgrp
->event_list_lock
);
1357 simple_xattrs_init(&cgrp
->xattrs
);
1360 static void init_cgroup_root(struct cgroupfs_root
*root
)
1362 struct cgroup
*cgrp
= &root
->top_cgroup
;
1364 INIT_LIST_HEAD(&root
->subsys_list
);
1365 INIT_LIST_HEAD(&root
->root_list
);
1366 root
->number_of_cgroups
= 1;
1368 RCU_INIT_POINTER(cgrp
->name
, &root_cgroup_name
);
1369 init_cgroup_housekeeping(cgrp
);
1370 idr_init(&root
->cgroup_idr
);
1373 static int cgroup_init_root_id(struct cgroupfs_root
*root
, int start
, int end
)
1377 lockdep_assert_held(&cgroup_mutex
);
1378 lockdep_assert_held(&cgroup_root_mutex
);
1380 id
= idr_alloc_cyclic(&cgroup_hierarchy_idr
, root
, start
, end
,
1385 root
->hierarchy_id
= id
;
1389 static void cgroup_exit_root_id(struct cgroupfs_root
*root
)
1391 lockdep_assert_held(&cgroup_mutex
);
1392 lockdep_assert_held(&cgroup_root_mutex
);
1394 if (root
->hierarchy_id
) {
1395 idr_remove(&cgroup_hierarchy_idr
, root
->hierarchy_id
);
1396 root
->hierarchy_id
= 0;
1400 static int cgroup_test_super(struct super_block
*sb
, void *data
)
1402 struct cgroup_sb_opts
*opts
= data
;
1403 struct cgroupfs_root
*root
= sb
->s_fs_info
;
1405 /* If we asked for a name then it must match */
1406 if (opts
->name
&& strcmp(opts
->name
, root
->name
))
1410 * If we asked for subsystems (or explicitly for no
1411 * subsystems) then they must match
1413 if ((opts
->subsys_mask
|| opts
->none
)
1414 && (opts
->subsys_mask
!= root
->subsys_mask
))
1420 static struct cgroupfs_root
*cgroup_root_from_opts(struct cgroup_sb_opts
*opts
)
1422 struct cgroupfs_root
*root
;
1424 if (!opts
->subsys_mask
&& !opts
->none
)
1427 root
= kzalloc(sizeof(*root
), GFP_KERNEL
);
1429 return ERR_PTR(-ENOMEM
);
1431 init_cgroup_root(root
);
1434 * We need to set @root->subsys_mask now so that @root can be
1435 * matched by cgroup_test_super() before it finishes
1436 * initialization; otherwise, competing mounts with the same
1437 * options may try to bind the same subsystems instead of waiting
1438 * for the first one leading to unexpected mount errors.
1439 * SUBSYS_BOUND will be set once actual binding is complete.
1441 root
->subsys_mask
= opts
->subsys_mask
;
1442 root
->flags
= opts
->flags
;
1443 if (opts
->release_agent
)
1444 strcpy(root
->release_agent_path
, opts
->release_agent
);
1446 strcpy(root
->name
, opts
->name
);
1447 if (opts
->cpuset_clone_children
)
1448 set_bit(CGRP_CPUSET_CLONE_CHILDREN
, &root
->top_cgroup
.flags
);
1452 static void cgroup_free_root(struct cgroupfs_root
*root
)
1455 /* hierarhcy ID shoulid already have been released */
1456 WARN_ON_ONCE(root
->hierarchy_id
);
1458 idr_destroy(&root
->cgroup_idr
);
1463 static int cgroup_set_super(struct super_block
*sb
, void *data
)
1466 struct cgroup_sb_opts
*opts
= data
;
1468 /* If we don't have a new root, we can't set up a new sb */
1469 if (!opts
->new_root
)
1472 BUG_ON(!opts
->subsys_mask
&& !opts
->none
);
1474 ret
= set_anon_super(sb
, NULL
);
1478 sb
->s_fs_info
= opts
->new_root
;
1479 opts
->new_root
->sb
= sb
;
1481 sb
->s_blocksize
= PAGE_CACHE_SIZE
;
1482 sb
->s_blocksize_bits
= PAGE_CACHE_SHIFT
;
1483 sb
->s_magic
= CGROUP_SUPER_MAGIC
;
1484 sb
->s_op
= &cgroup_ops
;
1489 static int cgroup_get_rootdir(struct super_block
*sb
)
1491 static const struct dentry_operations cgroup_dops
= {
1492 .d_iput
= cgroup_diput
,
1493 .d_delete
= always_delete_dentry
,
1496 struct inode
*inode
=
1497 cgroup_new_inode(S_IFDIR
| S_IRUGO
| S_IXUGO
| S_IWUSR
, sb
);
1502 inode
->i_fop
= &simple_dir_operations
;
1503 inode
->i_op
= &cgroup_dir_inode_operations
;
1504 /* directories start off with i_nlink == 2 (for "." entry) */
1506 sb
->s_root
= d_make_root(inode
);
1509 /* for everything else we want ->d_op set */
1510 sb
->s_d_op
= &cgroup_dops
;
1514 static struct dentry
*cgroup_mount(struct file_system_type
*fs_type
,
1515 int flags
, const char *unused_dev_name
,
1518 struct cgroup_sb_opts opts
;
1519 struct cgroupfs_root
*root
;
1521 struct super_block
*sb
;
1522 struct cgroupfs_root
*new_root
;
1523 struct list_head tmp_links
;
1524 struct inode
*inode
;
1525 const struct cred
*cred
;
1527 /* First find the desired set of subsystems */
1528 mutex_lock(&cgroup_mutex
);
1529 ret
= parse_cgroupfs_options(data
, &opts
);
1530 mutex_unlock(&cgroup_mutex
);
1535 * Allocate a new cgroup root. We may not need it if we're
1536 * reusing an existing hierarchy.
1538 new_root
= cgroup_root_from_opts(&opts
);
1539 if (IS_ERR(new_root
)) {
1540 ret
= PTR_ERR(new_root
);
1543 opts
.new_root
= new_root
;
1545 /* Locate an existing or new sb for this hierarchy */
1546 sb
= sget(fs_type
, cgroup_test_super
, cgroup_set_super
, 0, &opts
);
1549 cgroup_free_root(opts
.new_root
);
1553 root
= sb
->s_fs_info
;
1555 if (root
== opts
.new_root
) {
1556 /* We used the new root structure, so this is a new hierarchy */
1557 struct cgroup
*root_cgrp
= &root
->top_cgroup
;
1558 struct cgroupfs_root
*existing_root
;
1560 struct css_set
*cset
;
1562 BUG_ON(sb
->s_root
!= NULL
);
1564 ret
= cgroup_get_rootdir(sb
);
1566 goto drop_new_super
;
1567 inode
= sb
->s_root
->d_inode
;
1569 mutex_lock(&inode
->i_mutex
);
1570 mutex_lock(&cgroup_mutex
);
1571 mutex_lock(&cgroup_root_mutex
);
1573 root_cgrp
->id
= idr_alloc(&root
->cgroup_idr
, root_cgrp
,
1575 if (root_cgrp
->id
< 0)
1578 /* Check for name clashes with existing mounts */
1580 if (strlen(root
->name
))
1581 for_each_active_root(existing_root
)
1582 if (!strcmp(existing_root
->name
, root
->name
))
1586 * We're accessing css_set_count without locking
1587 * css_set_lock here, but that's OK - it can only be
1588 * increased by someone holding cgroup_lock, and
1589 * that's us. The worst that can happen is that we
1590 * have some link structures left over
1592 ret
= allocate_cgrp_cset_links(css_set_count
, &tmp_links
);
1596 /* ID 0 is reserved for dummy root, 1 for unified hierarchy */
1597 ret
= cgroup_init_root_id(root
, 2, 0);
1601 sb
->s_root
->d_fsdata
= root_cgrp
;
1602 root_cgrp
->dentry
= sb
->s_root
;
1605 * We're inside get_sb() and will call lookup_one_len() to
1606 * create the root files, which doesn't work if SELinux is
1607 * in use. The following cred dancing somehow works around
1608 * it. See 2ce9738ba ("cgroupfs: use init_cred when
1609 * populating new cgroupfs mount") for more details.
1611 cred
= override_creds(&init_cred
);
1613 ret
= cgroup_addrm_files(root_cgrp
, cgroup_base_files
, true);
1617 ret
= rebind_subsystems(root
, root
->subsys_mask
, 0);
1624 * There must be no failure case after here, since rebinding
1625 * takes care of subsystems' refcounts, which are explicitly
1626 * dropped in the failure exit path.
1629 list_add(&root
->root_list
, &cgroup_roots
);
1630 cgroup_root_count
++;
1632 /* Link the top cgroup in this hierarchy into all
1633 * the css_set objects */
1634 write_lock(&css_set_lock
);
1635 hash_for_each(css_set_table
, i
, cset
, hlist
)
1636 link_css_set(&tmp_links
, cset
, root_cgrp
);
1637 write_unlock(&css_set_lock
);
1639 free_cgrp_cset_links(&tmp_links
);
1641 BUG_ON(!list_empty(&root_cgrp
->children
));
1642 BUG_ON(root
->number_of_cgroups
!= 1);
1644 mutex_unlock(&cgroup_root_mutex
);
1645 mutex_unlock(&cgroup_mutex
);
1646 mutex_unlock(&inode
->i_mutex
);
1649 * We re-used an existing hierarchy - the new root (if
1650 * any) is not needed
1652 cgroup_free_root(opts
.new_root
);
1654 if ((root
->flags
^ opts
.flags
) & CGRP_ROOT_OPTION_MASK
) {
1655 if ((root
->flags
| opts
.flags
) & CGRP_ROOT_SANE_BEHAVIOR
) {
1656 pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
1658 goto drop_new_super
;
1660 pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n");
1665 kfree(opts
.release_agent
);
1667 return dget(sb
->s_root
);
1670 free_cgrp_cset_links(&tmp_links
);
1671 cgroup_addrm_files(&root
->top_cgroup
, cgroup_base_files
, false);
1674 cgroup_exit_root_id(root
);
1675 mutex_unlock(&cgroup_root_mutex
);
1676 mutex_unlock(&cgroup_mutex
);
1677 mutex_unlock(&inode
->i_mutex
);
1679 deactivate_locked_super(sb
);
1681 kfree(opts
.release_agent
);
1683 return ERR_PTR(ret
);
1686 static void cgroup_kill_sb(struct super_block
*sb
) {
1687 struct cgroupfs_root
*root
= sb
->s_fs_info
;
1688 struct cgroup
*cgrp
= &root
->top_cgroup
;
1689 struct cgrp_cset_link
*link
, *tmp_link
;
1694 BUG_ON(root
->number_of_cgroups
!= 1);
1695 BUG_ON(!list_empty(&cgrp
->children
));
1697 mutex_lock(&cgrp
->dentry
->d_inode
->i_mutex
);
1698 mutex_lock(&cgroup_mutex
);
1699 mutex_lock(&cgroup_root_mutex
);
1701 /* Rebind all subsystems back to the default hierarchy */
1702 if (root
->flags
& CGRP_ROOT_SUBSYS_BOUND
) {
1703 ret
= rebind_subsystems(root
, 0, root
->subsys_mask
);
1704 /* Shouldn't be able to fail ... */
1709 * Release all the links from cset_links to this hierarchy's
1712 write_lock(&css_set_lock
);
1714 list_for_each_entry_safe(link
, tmp_link
, &cgrp
->cset_links
, cset_link
) {
1715 list_del(&link
->cset_link
);
1716 list_del(&link
->cgrp_link
);
1719 write_unlock(&css_set_lock
);
1721 if (!list_empty(&root
->root_list
)) {
1722 list_del(&root
->root_list
);
1723 cgroup_root_count
--;
1726 cgroup_exit_root_id(root
);
1728 mutex_unlock(&cgroup_root_mutex
);
1729 mutex_unlock(&cgroup_mutex
);
1730 mutex_unlock(&cgrp
->dentry
->d_inode
->i_mutex
);
1732 simple_xattrs_free(&cgrp
->xattrs
);
1734 kill_litter_super(sb
);
1735 cgroup_free_root(root
);
1738 static struct file_system_type cgroup_fs_type
= {
1740 .mount
= cgroup_mount
,
1741 .kill_sb
= cgroup_kill_sb
,
1744 static struct kobject
*cgroup_kobj
;
1747 * cgroup_path - generate the path of a cgroup
1748 * @cgrp: the cgroup in question
1749 * @buf: the buffer to write the path into
1750 * @buflen: the length of the buffer
1752 * Writes path of cgroup into buf. Returns 0 on success, -errno on error.
1754 * We can't generate cgroup path using dentry->d_name, as accessing
1755 * dentry->name must be protected by irq-unsafe dentry->d_lock or parent
1756 * inode's i_mutex, while on the other hand cgroup_path() can be called
1757 * with some irq-safe spinlocks held.
1759 int cgroup_path(const struct cgroup
*cgrp
, char *buf
, int buflen
)
1761 int ret
= -ENAMETOOLONG
;
1764 if (!cgrp
->parent
) {
1765 if (strlcpy(buf
, "/", buflen
) >= buflen
)
1766 return -ENAMETOOLONG
;
1770 start
= buf
+ buflen
- 1;
1775 const char *name
= cgroup_name(cgrp
);
1779 if ((start
-= len
) < buf
)
1781 memcpy(start
, name
, len
);
1787 cgrp
= cgrp
->parent
;
1788 } while (cgrp
->parent
);
1790 memmove(buf
, start
, buf
+ buflen
- start
);
1795 EXPORT_SYMBOL_GPL(cgroup_path
);
1798 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
1799 * @task: target task
1800 * @buf: the buffer to write the path into
1801 * @buflen: the length of the buffer
1803 * Determine @task's cgroup on the first (the one with the lowest non-zero
1804 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
1805 * function grabs cgroup_mutex and shouldn't be used inside locks used by
1806 * cgroup controller callbacks.
1808 * Returns 0 on success, fails with -%ENAMETOOLONG if @buflen is too short.
1810 int task_cgroup_path(struct task_struct
*task
, char *buf
, size_t buflen
)
1812 struct cgroupfs_root
*root
;
1813 struct cgroup
*cgrp
;
1814 int hierarchy_id
= 1, ret
= 0;
1817 return -ENAMETOOLONG
;
1819 mutex_lock(&cgroup_mutex
);
1821 root
= idr_get_next(&cgroup_hierarchy_idr
, &hierarchy_id
);
1824 cgrp
= task_cgroup_from_root(task
, root
);
1825 ret
= cgroup_path(cgrp
, buf
, buflen
);
1827 /* if no hierarchy exists, everyone is in "/" */
1828 memcpy(buf
, "/", 2);
1831 mutex_unlock(&cgroup_mutex
);
1834 EXPORT_SYMBOL_GPL(task_cgroup_path
);
1837 * Control Group taskset
1839 struct task_and_cgroup
{
1840 struct task_struct
*task
;
1841 struct cgroup
*cgrp
;
1842 struct css_set
*cset
;
1845 struct cgroup_taskset
{
1846 struct task_and_cgroup single
;
1847 struct flex_array
*tc_array
;
1850 struct cgroup
*cur_cgrp
;
1854 * cgroup_taskset_first - reset taskset and return the first task
1855 * @tset: taskset of interest
1857 * @tset iteration is initialized and the first task is returned.
1859 struct task_struct
*cgroup_taskset_first(struct cgroup_taskset
*tset
)
1861 if (tset
->tc_array
) {
1863 return cgroup_taskset_next(tset
);
1865 tset
->cur_cgrp
= tset
->single
.cgrp
;
1866 return tset
->single
.task
;
1869 EXPORT_SYMBOL_GPL(cgroup_taskset_first
);
1872 * cgroup_taskset_next - iterate to the next task in taskset
1873 * @tset: taskset of interest
1875 * Return the next task in @tset. Iteration must have been initialized
1876 * with cgroup_taskset_first().
1878 struct task_struct
*cgroup_taskset_next(struct cgroup_taskset
*tset
)
1880 struct task_and_cgroup
*tc
;
1882 if (!tset
->tc_array
|| tset
->idx
>= tset
->tc_array_len
)
1885 tc
= flex_array_get(tset
->tc_array
, tset
->idx
++);
1886 tset
->cur_cgrp
= tc
->cgrp
;
1889 EXPORT_SYMBOL_GPL(cgroup_taskset_next
);
1892 * cgroup_taskset_cur_css - return the matching css for the current task
1893 * @tset: taskset of interest
1894 * @subsys_id: the ID of the target subsystem
1896 * Return the css for the current (last returned) task of @tset for
1897 * subsystem specified by @subsys_id. This function must be preceded by
1898 * either cgroup_taskset_first() or cgroup_taskset_next().
1900 struct cgroup_subsys_state
*cgroup_taskset_cur_css(struct cgroup_taskset
*tset
,
1903 return cgroup_css(tset
->cur_cgrp
, cgroup_subsys
[subsys_id
]);
1905 EXPORT_SYMBOL_GPL(cgroup_taskset_cur_css
);
1908 * cgroup_taskset_size - return the number of tasks in taskset
1909 * @tset: taskset of interest
1911 int cgroup_taskset_size(struct cgroup_taskset
*tset
)
1913 return tset
->tc_array
? tset
->tc_array_len
: 1;
1915 EXPORT_SYMBOL_GPL(cgroup_taskset_size
);
1919 * cgroup_task_migrate - move a task from one cgroup to another.
1921 * Must be called with cgroup_mutex and threadgroup locked.
1923 static void cgroup_task_migrate(struct cgroup
*old_cgrp
,
1924 struct task_struct
*tsk
,
1925 struct css_set
*new_cset
)
1927 struct css_set
*old_cset
;
1930 * We are synchronized through threadgroup_lock() against PF_EXITING
1931 * setting such that we can't race against cgroup_exit() changing the
1932 * css_set to init_css_set and dropping the old one.
1934 WARN_ON_ONCE(tsk
->flags
& PF_EXITING
);
1935 old_cset
= task_css_set(tsk
);
1938 rcu_assign_pointer(tsk
->cgroups
, new_cset
);
1941 /* Update the css_set linked lists if we're using them */
1942 write_lock(&css_set_lock
);
1943 if (!list_empty(&tsk
->cg_list
))
1944 list_move(&tsk
->cg_list
, &new_cset
->tasks
);
1945 write_unlock(&css_set_lock
);
1948 * We just gained a reference on old_cset by taking it from the
1949 * task. As trading it for new_cset is protected by cgroup_mutex,
1950 * we're safe to drop it here; it will be freed under RCU.
1952 set_bit(CGRP_RELEASABLE
, &old_cgrp
->flags
);
1953 put_css_set(old_cset
);
1957 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
1958 * @cgrp: the cgroup to attach to
1959 * @tsk: the task or the leader of the threadgroup to be attached
1960 * @threadgroup: attach the whole threadgroup?
1962 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
1963 * task_lock of @tsk or each thread in the threadgroup individually in turn.
1965 static int cgroup_attach_task(struct cgroup
*cgrp
, struct task_struct
*tsk
,
1968 int retval
, i
, group_size
;
1969 struct cgroup_subsys
*ss
, *failed_ss
= NULL
;
1970 struct cgroupfs_root
*root
= cgrp
->root
;
1971 /* threadgroup list cursor and array */
1972 struct task_struct
*leader
= tsk
;
1973 struct task_and_cgroup
*tc
;
1974 struct flex_array
*group
;
1975 struct cgroup_taskset tset
= { };
1978 * step 0: in order to do expensive, possibly blocking operations for
1979 * every thread, we cannot iterate the thread group list, since it needs
1980 * rcu or tasklist locked. instead, build an array of all threads in the
1981 * group - group_rwsem prevents new threads from appearing, and if
1982 * threads exit, this will just be an over-estimate.
1985 group_size
= get_nr_threads(tsk
);
1988 /* flex_array supports very large thread-groups better than kmalloc. */
1989 group
= flex_array_alloc(sizeof(*tc
), group_size
, GFP_KERNEL
);
1992 /* pre-allocate to guarantee space while iterating in rcu read-side. */
1993 retval
= flex_array_prealloc(group
, 0, group_size
, GFP_KERNEL
);
1995 goto out_free_group_list
;
1999 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2000 * already PF_EXITING could be freed from underneath us unless we
2001 * take an rcu_read_lock.
2005 struct task_and_cgroup ent
;
2007 /* @tsk either already exited or can't exit until the end */
2008 if (tsk
->flags
& PF_EXITING
)
2011 /* as per above, nr_threads may decrease, but not increase. */
2012 BUG_ON(i
>= group_size
);
2014 ent
.cgrp
= task_cgroup_from_root(tsk
, root
);
2015 /* nothing to do if this task is already in the cgroup */
2016 if (ent
.cgrp
== cgrp
)
2019 * saying GFP_ATOMIC has no effect here because we did prealloc
2020 * earlier, but it's good form to communicate our expectations.
2022 retval
= flex_array_put(group
, i
, &ent
, GFP_ATOMIC
);
2023 BUG_ON(retval
!= 0);
2028 } while_each_thread(leader
, tsk
);
2030 /* remember the number of threads in the array for later. */
2032 tset
.tc_array
= group
;
2033 tset
.tc_array_len
= group_size
;
2035 /* methods shouldn't be called if no task is actually migrating */
2038 goto out_free_group_list
;
2041 * step 1: check that we can legitimately attach to the cgroup.
2043 for_each_root_subsys(root
, ss
) {
2044 struct cgroup_subsys_state
*css
= cgroup_css(cgrp
, ss
);
2046 if (ss
->can_attach
) {
2047 retval
= ss
->can_attach(css
, &tset
);
2050 goto out_cancel_attach
;
2056 * step 2: make sure css_sets exist for all threads to be migrated.
2057 * we use find_css_set, which allocates a new one if necessary.
2059 for (i
= 0; i
< group_size
; i
++) {
2060 struct css_set
*old_cset
;
2062 tc
= flex_array_get(group
, i
);
2063 old_cset
= task_css_set(tc
->task
);
2064 tc
->cset
= find_css_set(old_cset
, cgrp
);
2067 goto out_put_css_set_refs
;
2072 * step 3: now that we're guaranteed success wrt the css_sets,
2073 * proceed to move all tasks to the new cgroup. There are no
2074 * failure cases after here, so this is the commit point.
2076 for (i
= 0; i
< group_size
; i
++) {
2077 tc
= flex_array_get(group
, i
);
2078 cgroup_task_migrate(tc
->cgrp
, tc
->task
, tc
->cset
);
2080 /* nothing is sensitive to fork() after this point. */
2083 * step 4: do subsystem attach callbacks.
2085 for_each_root_subsys(root
, ss
) {
2086 struct cgroup_subsys_state
*css
= cgroup_css(cgrp
, ss
);
2089 ss
->attach(css
, &tset
);
2093 * step 5: success! and cleanup
2096 out_put_css_set_refs
:
2098 for (i
= 0; i
< group_size
; i
++) {
2099 tc
= flex_array_get(group
, i
);
2102 put_css_set(tc
->cset
);
2107 for_each_root_subsys(root
, ss
) {
2108 struct cgroup_subsys_state
*css
= cgroup_css(cgrp
, ss
);
2110 if (ss
== failed_ss
)
2112 if (ss
->cancel_attach
)
2113 ss
->cancel_attach(css
, &tset
);
2116 out_free_group_list
:
2117 flex_array_free(group
);
2122 * Find the task_struct of the task to attach by vpid and pass it along to the
2123 * function to attach either it or all tasks in its threadgroup. Will lock
2124 * cgroup_mutex and threadgroup; may take task_lock of task.
2126 static int attach_task_by_pid(struct cgroup
*cgrp
, u64 pid
, bool threadgroup
)
2128 struct task_struct
*tsk
;
2129 const struct cred
*cred
= current_cred(), *tcred
;
2132 if (!cgroup_lock_live_group(cgrp
))
2138 tsk
= find_task_by_vpid(pid
);
2142 goto out_unlock_cgroup
;
2145 * even if we're attaching all tasks in the thread group, we
2146 * only need to check permissions on one of them.
2148 tcred
= __task_cred(tsk
);
2149 if (!uid_eq(cred
->euid
, GLOBAL_ROOT_UID
) &&
2150 !uid_eq(cred
->euid
, tcred
->uid
) &&
2151 !uid_eq(cred
->euid
, tcred
->suid
)) {
2154 goto out_unlock_cgroup
;
2160 tsk
= tsk
->group_leader
;
2163 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
2164 * trapped in a cpuset, or RT worker may be born in a cgroup
2165 * with no rt_runtime allocated. Just say no.
2167 if (tsk
== kthreadd_task
|| (tsk
->flags
& PF_NO_SETAFFINITY
)) {
2170 goto out_unlock_cgroup
;
2173 get_task_struct(tsk
);
2176 threadgroup_lock(tsk
);
2178 if (!thread_group_leader(tsk
)) {
2180 * a race with de_thread from another thread's exec()
2181 * may strip us of our leadership, if this happens,
2182 * there is no choice but to throw this task away and
2183 * try again; this is
2184 * "double-double-toil-and-trouble-check locking".
2186 threadgroup_unlock(tsk
);
2187 put_task_struct(tsk
);
2188 goto retry_find_task
;
2192 ret
= cgroup_attach_task(cgrp
, tsk
, threadgroup
);
2194 threadgroup_unlock(tsk
);
2196 put_task_struct(tsk
);
2198 mutex_unlock(&cgroup_mutex
);
2203 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2204 * @from: attach to all cgroups of a given task
2205 * @tsk: the task to be attached
2207 int cgroup_attach_task_all(struct task_struct
*from
, struct task_struct
*tsk
)
2209 struct cgroupfs_root
*root
;
2212 mutex_lock(&cgroup_mutex
);
2213 for_each_active_root(root
) {
2214 struct cgroup
*from_cgrp
= task_cgroup_from_root(from
, root
);
2216 retval
= cgroup_attach_task(from_cgrp
, tsk
, false);
2220 mutex_unlock(&cgroup_mutex
);
2224 EXPORT_SYMBOL_GPL(cgroup_attach_task_all
);
2226 static int cgroup_tasks_write(struct cgroup_subsys_state
*css
,
2227 struct cftype
*cft
, u64 pid
)
2229 return attach_task_by_pid(css
->cgroup
, pid
, false);
2232 static int cgroup_procs_write(struct cgroup_subsys_state
*css
,
2233 struct cftype
*cft
, u64 tgid
)
2235 return attach_task_by_pid(css
->cgroup
, tgid
, true);
2238 static int cgroup_release_agent_write(struct cgroup_subsys_state
*css
,
2239 struct cftype
*cft
, const char *buffer
)
2241 BUILD_BUG_ON(sizeof(css
->cgroup
->root
->release_agent_path
) < PATH_MAX
);
2242 if (strlen(buffer
) >= PATH_MAX
)
2244 if (!cgroup_lock_live_group(css
->cgroup
))
2246 mutex_lock(&cgroup_root_mutex
);
2247 strcpy(css
->cgroup
->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_subsys_state
*css
,
2254 struct cftype
*cft
, struct seq_file
*seq
)
2256 struct cgroup
*cgrp
= css
->cgroup
;
2258 if (!cgroup_lock_live_group(cgrp
))
2260 seq_puts(seq
, cgrp
->root
->release_agent_path
);
2261 seq_putc(seq
, '\n');
2262 mutex_unlock(&cgroup_mutex
);
2266 static int cgroup_sane_behavior_show(struct cgroup_subsys_state
*css
,
2267 struct cftype
*cft
, struct seq_file
*seq
)
2269 seq_printf(seq
, "%d\n", cgroup_sane_behavior(css
->cgroup
));
2273 /* A buffer size big enough for numbers or short strings */
2274 #define CGROUP_LOCAL_BUFFER_SIZE 64
2276 static ssize_t
cgroup_write_X64(struct cgroup_subsys_state
*css
,
2277 struct cftype
*cft
, struct file
*file
,
2278 const char __user
*userbuf
, size_t nbytes
,
2279 loff_t
*unused_ppos
)
2281 char buffer
[CGROUP_LOCAL_BUFFER_SIZE
];
2287 if (nbytes
>= sizeof(buffer
))
2289 if (copy_from_user(buffer
, userbuf
, nbytes
))
2292 buffer
[nbytes
] = 0; /* nul-terminate */
2293 if (cft
->write_u64
) {
2294 u64 val
= simple_strtoull(strstrip(buffer
), &end
, 0);
2297 retval
= cft
->write_u64(css
, cft
, val
);
2299 s64 val
= simple_strtoll(strstrip(buffer
), &end
, 0);
2302 retval
= cft
->write_s64(css
, cft
, val
);
2309 static ssize_t
cgroup_write_string(struct cgroup_subsys_state
*css
,
2310 struct cftype
*cft
, struct file
*file
,
2311 const char __user
*userbuf
, size_t nbytes
,
2312 loff_t
*unused_ppos
)
2314 char local_buffer
[CGROUP_LOCAL_BUFFER_SIZE
];
2316 size_t max_bytes
= cft
->max_write_len
;
2317 char *buffer
= local_buffer
;
2320 max_bytes
= sizeof(local_buffer
) - 1;
2321 if (nbytes
>= max_bytes
)
2323 /* Allocate a dynamic buffer if we need one */
2324 if (nbytes
>= sizeof(local_buffer
)) {
2325 buffer
= kmalloc(nbytes
+ 1, GFP_KERNEL
);
2329 if (nbytes
&& copy_from_user(buffer
, userbuf
, nbytes
)) {
2334 buffer
[nbytes
] = 0; /* nul-terminate */
2335 retval
= cft
->write_string(css
, cft
, strstrip(buffer
));
2339 if (buffer
!= local_buffer
)
2344 static ssize_t
cgroup_file_write(struct file
*file
, const char __user
*buf
,
2345 size_t nbytes
, loff_t
*ppos
)
2347 struct cfent
*cfe
= __d_cfe(file
->f_dentry
);
2348 struct cftype
*cft
= __d_cft(file
->f_dentry
);
2349 struct cgroup_subsys_state
*css
= cfe
->css
;
2352 return cft
->write(css
, cft
, file
, buf
, nbytes
, ppos
);
2353 if (cft
->write_u64
|| cft
->write_s64
)
2354 return cgroup_write_X64(css
, cft
, file
, buf
, nbytes
, ppos
);
2355 if (cft
->write_string
)
2356 return cgroup_write_string(css
, cft
, file
, buf
, nbytes
, ppos
);
2358 int ret
= cft
->trigger(css
, (unsigned int)cft
->private);
2359 return ret
? ret
: nbytes
;
2364 static ssize_t
cgroup_read_u64(struct cgroup_subsys_state
*css
,
2365 struct cftype
*cft
, struct file
*file
,
2366 char __user
*buf
, size_t nbytes
, loff_t
*ppos
)
2368 char tmp
[CGROUP_LOCAL_BUFFER_SIZE
];
2369 u64 val
= cft
->read_u64(css
, 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_subsys_state
*css
,
2376 struct cftype
*cft
, struct file
*file
,
2377 char __user
*buf
, size_t nbytes
, loff_t
*ppos
)
2379 char tmp
[CGROUP_LOCAL_BUFFER_SIZE
];
2380 s64 val
= cft
->read_s64(css
, cft
);
2381 int len
= sprintf(tmp
, "%lld\n", (long long) val
);
2383 return simple_read_from_buffer(buf
, nbytes
, ppos
, tmp
, len
);
2386 static ssize_t
cgroup_file_read(struct file
*file
, char __user
*buf
,
2387 size_t nbytes
, loff_t
*ppos
)
2389 struct cfent
*cfe
= __d_cfe(file
->f_dentry
);
2390 struct cftype
*cft
= __d_cft(file
->f_dentry
);
2391 struct cgroup_subsys_state
*css
= cfe
->css
;
2394 return cft
->read(css
, cft
, file
, buf
, nbytes
, ppos
);
2396 return cgroup_read_u64(css
, cft
, file
, buf
, nbytes
, ppos
);
2398 return cgroup_read_s64(css
, cft
, file
, buf
, nbytes
, ppos
);
2403 * seqfile ops/methods for returning structured data. Currently just
2404 * supports string->u64 maps, but can be extended in future.
2407 static int cgroup_map_add(struct cgroup_map_cb
*cb
, const char *key
, u64 value
)
2409 struct seq_file
*sf
= cb
->state
;
2410 return seq_printf(sf
, "%s %llu\n", key
, (unsigned long long)value
);
2413 static int cgroup_seqfile_show(struct seq_file
*m
, void *arg
)
2415 struct cfent
*cfe
= m
->private;
2416 struct cftype
*cft
= cfe
->type
;
2417 struct cgroup_subsys_state
*css
= cfe
->css
;
2419 if (cft
->read_map
) {
2420 struct cgroup_map_cb cb
= {
2421 .fill
= cgroup_map_add
,
2424 return cft
->read_map(css
, cft
, &cb
);
2426 return cft
->read_seq_string(css
, cft
, m
);
2429 static const struct file_operations cgroup_seqfile_operations
= {
2431 .write
= cgroup_file_write
,
2432 .llseek
= seq_lseek
,
2433 .release
= cgroup_file_release
,
2436 static int cgroup_file_open(struct inode
*inode
, struct file
*file
)
2438 struct cfent
*cfe
= __d_cfe(file
->f_dentry
);
2439 struct cftype
*cft
= __d_cft(file
->f_dentry
);
2440 struct cgroup
*cgrp
= __d_cgrp(cfe
->dentry
->d_parent
);
2441 struct cgroup_subsys_state
*css
;
2444 err
= generic_file_open(inode
, file
);
2449 * If the file belongs to a subsystem, pin the css. Will be
2450 * unpinned either on open failure or release. This ensures that
2451 * @css stays alive for all file operations.
2454 css
= cgroup_css(cgrp
, cft
->ss
);
2455 if (cft
->ss
&& !css_tryget(css
))
2463 * @cfe->css is used by read/write/close to determine the
2464 * associated css. @file->private_data would be a better place but
2465 * that's already used by seqfile. Multiple accessors may use it
2466 * simultaneously which is okay as the association never changes.
2468 WARN_ON_ONCE(cfe
->css
&& cfe
->css
!= css
);
2471 if (cft
->read_map
|| cft
->read_seq_string
) {
2472 file
->f_op
= &cgroup_seqfile_operations
;
2473 err
= single_open(file
, cgroup_seqfile_show
, cfe
);
2474 } else if (cft
->open
) {
2475 err
= cft
->open(inode
, file
);
2483 static int cgroup_file_release(struct inode
*inode
, struct file
*file
)
2485 struct cfent
*cfe
= __d_cfe(file
->f_dentry
);
2486 struct cftype
*cft
= __d_cft(file
->f_dentry
);
2487 struct cgroup_subsys_state
*css
= cfe
->css
;
2491 ret
= cft
->release(inode
, file
);
2494 if (file
->f_op
== &cgroup_seqfile_operations
)
2495 single_release(inode
, file
);
2500 * cgroup_rename - Only allow simple rename of directories in place.
2502 static int cgroup_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
2503 struct inode
*new_dir
, struct dentry
*new_dentry
)
2506 struct cgroup_name
*name
, *old_name
;
2507 struct cgroup
*cgrp
;
2510 * It's convinient to use parent dir's i_mutex to protected
2513 lockdep_assert_held(&old_dir
->i_mutex
);
2515 if (!S_ISDIR(old_dentry
->d_inode
->i_mode
))
2517 if (new_dentry
->d_inode
)
2519 if (old_dir
!= new_dir
)
2522 cgrp
= __d_cgrp(old_dentry
);
2525 * This isn't a proper migration and its usefulness is very
2526 * limited. Disallow if sane_behavior.
2528 if (cgroup_sane_behavior(cgrp
))
2531 name
= cgroup_alloc_name(new_dentry
);
2535 ret
= simple_rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
2541 old_name
= rcu_dereference_protected(cgrp
->name
, true);
2542 rcu_assign_pointer(cgrp
->name
, name
);
2544 kfree_rcu(old_name
, rcu_head
);
2548 static struct simple_xattrs
*__d_xattrs(struct dentry
*dentry
)
2550 if (S_ISDIR(dentry
->d_inode
->i_mode
))
2551 return &__d_cgrp(dentry
)->xattrs
;
2553 return &__d_cfe(dentry
)->xattrs
;
2556 static inline int xattr_enabled(struct dentry
*dentry
)
2558 struct cgroupfs_root
*root
= dentry
->d_sb
->s_fs_info
;
2559 return root
->flags
& CGRP_ROOT_XATTR
;
2562 static bool is_valid_xattr(const char *name
)
2564 if (!strncmp(name
, XATTR_TRUSTED_PREFIX
, XATTR_TRUSTED_PREFIX_LEN
) ||
2565 !strncmp(name
, XATTR_SECURITY_PREFIX
, XATTR_SECURITY_PREFIX_LEN
))
2570 static int cgroup_setxattr(struct dentry
*dentry
, const char *name
,
2571 const void *val
, size_t size
, int flags
)
2573 if (!xattr_enabled(dentry
))
2575 if (!is_valid_xattr(name
))
2577 return simple_xattr_set(__d_xattrs(dentry
), name
, val
, size
, flags
);
2580 static int cgroup_removexattr(struct dentry
*dentry
, const char *name
)
2582 if (!xattr_enabled(dentry
))
2584 if (!is_valid_xattr(name
))
2586 return simple_xattr_remove(__d_xattrs(dentry
), name
);
2589 static ssize_t
cgroup_getxattr(struct dentry
*dentry
, const char *name
,
2590 void *buf
, size_t size
)
2592 if (!xattr_enabled(dentry
))
2594 if (!is_valid_xattr(name
))
2596 return simple_xattr_get(__d_xattrs(dentry
), name
, buf
, size
);
2599 static ssize_t
cgroup_listxattr(struct dentry
*dentry
, char *buf
, size_t size
)
2601 if (!xattr_enabled(dentry
))
2603 return simple_xattr_list(__d_xattrs(dentry
), buf
, size
);
2606 static const struct file_operations cgroup_file_operations
= {
2607 .read
= cgroup_file_read
,
2608 .write
= cgroup_file_write
,
2609 .llseek
= generic_file_llseek
,
2610 .open
= cgroup_file_open
,
2611 .release
= cgroup_file_release
,
2614 static const struct inode_operations cgroup_file_inode_operations
= {
2615 .setxattr
= cgroup_setxattr
,
2616 .getxattr
= cgroup_getxattr
,
2617 .listxattr
= cgroup_listxattr
,
2618 .removexattr
= cgroup_removexattr
,
2621 static const struct inode_operations cgroup_dir_inode_operations
= {
2622 .lookup
= simple_lookup
,
2623 .mkdir
= cgroup_mkdir
,
2624 .rmdir
= cgroup_rmdir
,
2625 .rename
= cgroup_rename
,
2626 .setxattr
= cgroup_setxattr
,
2627 .getxattr
= cgroup_getxattr
,
2628 .listxattr
= cgroup_listxattr
,
2629 .removexattr
= cgroup_removexattr
,
2633 * Check if a file is a control file
2635 static inline struct cftype
*__file_cft(struct file
*file
)
2637 if (file_inode(file
)->i_fop
!= &cgroup_file_operations
)
2638 return ERR_PTR(-EINVAL
);
2639 return __d_cft(file
->f_dentry
);
2642 static int cgroup_create_file(struct dentry
*dentry
, umode_t mode
,
2643 struct super_block
*sb
)
2645 struct inode
*inode
;
2649 if (dentry
->d_inode
)
2652 inode
= cgroup_new_inode(mode
, sb
);
2656 if (S_ISDIR(mode
)) {
2657 inode
->i_op
= &cgroup_dir_inode_operations
;
2658 inode
->i_fop
= &simple_dir_operations
;
2660 /* start off with i_nlink == 2 (for "." entry) */
2662 inc_nlink(dentry
->d_parent
->d_inode
);
2665 * Control reaches here with cgroup_mutex held.
2666 * @inode->i_mutex should nest outside cgroup_mutex but we
2667 * want to populate it immediately without releasing
2668 * cgroup_mutex. As @inode isn't visible to anyone else
2669 * yet, trylock will always succeed without affecting
2672 WARN_ON_ONCE(!mutex_trylock(&inode
->i_mutex
));
2673 } else if (S_ISREG(mode
)) {
2675 inode
->i_fop
= &cgroup_file_operations
;
2676 inode
->i_op
= &cgroup_file_inode_operations
;
2678 d_instantiate(dentry
, inode
);
2679 dget(dentry
); /* Extra count - pin the dentry in core */
2684 * cgroup_file_mode - deduce file mode of a control file
2685 * @cft: the control file in question
2687 * returns cft->mode if ->mode is not 0
2688 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2689 * returns S_IRUGO if it has only a read handler
2690 * returns S_IWUSR if it has only a write hander
2692 static umode_t
cgroup_file_mode(const struct cftype
*cft
)
2699 if (cft
->read
|| cft
->read_u64
|| cft
->read_s64
||
2700 cft
->read_map
|| cft
->read_seq_string
)
2703 if (cft
->write
|| cft
->write_u64
|| cft
->write_s64
||
2704 cft
->write_string
|| cft
->trigger
)
2710 static int cgroup_add_file(struct cgroup
*cgrp
, struct cftype
*cft
)
2712 struct dentry
*dir
= cgrp
->dentry
;
2713 struct cgroup
*parent
= __d_cgrp(dir
);
2714 struct dentry
*dentry
;
2718 char name
[MAX_CGROUP_TYPE_NAMELEN
+ MAX_CFTYPE_NAME
+ 2] = { 0 };
2720 if (cft
->ss
&& !(cft
->flags
& CFTYPE_NO_PREFIX
) &&
2721 !(cgrp
->root
->flags
& CGRP_ROOT_NOPREFIX
)) {
2722 strcpy(name
, cft
->ss
->name
);
2725 strcat(name
, cft
->name
);
2727 BUG_ON(!mutex_is_locked(&dir
->d_inode
->i_mutex
));
2729 cfe
= kzalloc(sizeof(*cfe
), GFP_KERNEL
);
2733 dentry
= lookup_one_len(name
, dir
, strlen(name
));
2734 if (IS_ERR(dentry
)) {
2735 error
= PTR_ERR(dentry
);
2739 cfe
->type
= (void *)cft
;
2740 cfe
->dentry
= dentry
;
2741 dentry
->d_fsdata
= cfe
;
2742 simple_xattrs_init(&cfe
->xattrs
);
2744 mode
= cgroup_file_mode(cft
);
2745 error
= cgroup_create_file(dentry
, mode
| S_IFREG
, cgrp
->root
->sb
);
2747 list_add_tail(&cfe
->node
, &parent
->files
);
2757 * cgroup_addrm_files - add or remove files to a cgroup directory
2758 * @cgrp: the target cgroup
2759 * @cfts: array of cftypes to be added
2760 * @is_add: whether to add or remove
2762 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
2763 * For removals, this function never fails. If addition fails, this
2764 * function doesn't remove files already added. The caller is responsible
2767 static int cgroup_addrm_files(struct cgroup
*cgrp
, struct cftype cfts
[],
2773 lockdep_assert_held(&cgrp
->dentry
->d_inode
->i_mutex
);
2774 lockdep_assert_held(&cgroup_mutex
);
2776 for (cft
= cfts
; cft
->name
[0] != '\0'; cft
++) {
2777 /* does cft->flags tell us to skip this file on @cgrp? */
2778 if ((cft
->flags
& CFTYPE_INSANE
) && cgroup_sane_behavior(cgrp
))
2780 if ((cft
->flags
& CFTYPE_NOT_ON_ROOT
) && !cgrp
->parent
)
2782 if ((cft
->flags
& CFTYPE_ONLY_ON_ROOT
) && cgrp
->parent
)
2786 ret
= cgroup_add_file(cgrp
, cft
);
2788 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
2793 cgroup_rm_file(cgrp
, cft
);
2799 static void cgroup_cfts_prepare(void)
2800 __acquires(&cgroup_mutex
)
2803 * Thanks to the entanglement with vfs inode locking, we can't walk
2804 * the existing cgroups under cgroup_mutex and create files.
2805 * Instead, we use css_for_each_descendant_pre() and drop RCU read
2806 * lock before calling cgroup_addrm_files().
2808 mutex_lock(&cgroup_mutex
);
2811 static int cgroup_cfts_commit(struct cftype
*cfts
, bool is_add
)
2812 __releases(&cgroup_mutex
)
2815 struct cgroup_subsys
*ss
= cfts
[0].ss
;
2816 struct cgroup
*root
= &ss
->root
->top_cgroup
;
2817 struct super_block
*sb
= ss
->root
->sb
;
2818 struct dentry
*prev
= NULL
;
2819 struct inode
*inode
;
2820 struct cgroup_subsys_state
*css
;
2824 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
2825 if (!cfts
|| ss
->root
== &cgroup_dummy_root
||
2826 !atomic_inc_not_zero(&sb
->s_active
)) {
2827 mutex_unlock(&cgroup_mutex
);
2832 * All cgroups which are created after we drop cgroup_mutex will
2833 * have the updated set of files, so we only need to update the
2834 * cgroups created before the current @cgroup_serial_nr_next.
2836 update_before
= cgroup_serial_nr_next
;
2838 mutex_unlock(&cgroup_mutex
);
2840 /* add/rm files for all cgroups created before */
2842 css_for_each_descendant_pre(css
, cgroup_css(root
, ss
)) {
2843 struct cgroup
*cgrp
= css
->cgroup
;
2845 if (cgroup_is_dead(cgrp
))
2848 inode
= cgrp
->dentry
->d_inode
;
2853 prev
= cgrp
->dentry
;
2855 mutex_lock(&inode
->i_mutex
);
2856 mutex_lock(&cgroup_mutex
);
2857 if (cgrp
->serial_nr
< update_before
&& !cgroup_is_dead(cgrp
))
2858 ret
= cgroup_addrm_files(cgrp
, cfts
, is_add
);
2859 mutex_unlock(&cgroup_mutex
);
2860 mutex_unlock(&inode
->i_mutex
);
2868 deactivate_super(sb
);
2873 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2874 * @ss: target cgroup subsystem
2875 * @cfts: zero-length name terminated array of cftypes
2877 * Register @cfts to @ss. Files described by @cfts are created for all
2878 * existing cgroups to which @ss is attached and all future cgroups will
2879 * have them too. This function can be called anytime whether @ss is
2882 * Returns 0 on successful registration, -errno on failure. Note that this
2883 * function currently returns 0 as long as @cfts registration is successful
2884 * even if some file creation attempts on existing cgroups fail.
2886 int cgroup_add_cftypes(struct cgroup_subsys
*ss
, struct cftype
*cfts
)
2888 struct cftype_set
*set
;
2892 set
= kzalloc(sizeof(*set
), GFP_KERNEL
);
2896 for (cft
= cfts
; cft
->name
[0] != '\0'; cft
++)
2899 cgroup_cfts_prepare();
2901 list_add_tail(&set
->node
, &ss
->cftsets
);
2902 ret
= cgroup_cfts_commit(cfts
, true);
2904 cgroup_rm_cftypes(cfts
);
2907 EXPORT_SYMBOL_GPL(cgroup_add_cftypes
);
2910 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
2911 * @cfts: zero-length name terminated array of cftypes
2913 * Unregister @cfts. Files described by @cfts are removed from all
2914 * existing cgroups and all future cgroups won't have them either. This
2915 * function can be called anytime whether @cfts' subsys is attached or not.
2917 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
2920 int cgroup_rm_cftypes(struct cftype
*cfts
)
2922 struct cftype_set
*set
;
2924 if (!cfts
|| !cfts
[0].ss
)
2927 cgroup_cfts_prepare();
2929 list_for_each_entry(set
, &cfts
[0].ss
->cftsets
, node
) {
2930 if (set
->cfts
== cfts
) {
2931 list_del(&set
->node
);
2933 cgroup_cfts_commit(cfts
, false);
2938 cgroup_cfts_commit(NULL
, false);
2943 * cgroup_task_count - count the number of tasks in a cgroup.
2944 * @cgrp: the cgroup in question
2946 * Return the number of tasks in the cgroup.
2948 int cgroup_task_count(const struct cgroup
*cgrp
)
2951 struct cgrp_cset_link
*link
;
2953 read_lock(&css_set_lock
);
2954 list_for_each_entry(link
, &cgrp
->cset_links
, cset_link
)
2955 count
+= atomic_read(&link
->cset
->refcount
);
2956 read_unlock(&css_set_lock
);
2961 * To reduce the fork() overhead for systems that are not actually using
2962 * their cgroups capability, we don't maintain the lists running through
2963 * each css_set to its tasks until we see the list actually used - in other
2964 * words after the first call to css_task_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
, &task_css_set(p
)->tasks
);
2989 } while_each_thread(g
, p
);
2990 read_unlock(&tasklist_lock
);
2991 write_unlock(&css_set_lock
);
2995 * css_next_child - find the next child of a given css
2996 * @pos_css: the current position (%NULL to initiate traversal)
2997 * @parent_css: css whose children to walk
2999 * This function returns the next child of @parent_css and should be called
3000 * under RCU read lock. The only requirement is that @parent_css and
3001 * @pos_css are accessible. The next sibling is guaranteed to be returned
3002 * regardless of their states.
3004 struct cgroup_subsys_state
*
3005 css_next_child(struct cgroup_subsys_state
*pos_css
,
3006 struct cgroup_subsys_state
*parent_css
)
3008 struct cgroup
*pos
= pos_css
? pos_css
->cgroup
: NULL
;
3009 struct cgroup
*cgrp
= parent_css
->cgroup
;
3010 struct cgroup
*next
;
3012 WARN_ON_ONCE(!rcu_read_lock_held());
3015 * @pos could already have been removed. Once a cgroup is removed,
3016 * its ->sibling.next is no longer updated when its next sibling
3017 * changes. As CGRP_DEAD assertion is serialized and happens
3018 * before the cgroup is taken off the ->sibling list, if we see it
3019 * unasserted, it's guaranteed that the next sibling hasn't
3020 * finished its grace period even if it's already removed, and thus
3021 * safe to dereference from this RCU critical section. If
3022 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
3023 * to be visible as %true here.
3025 * If @pos is dead, its next pointer can't be dereferenced;
3026 * however, as each cgroup is given a monotonically increasing
3027 * unique serial number and always appended to the sibling list,
3028 * the next one can be found by walking the parent's children until
3029 * we see a cgroup with higher serial number than @pos's. While
3030 * this path can be slower, it's taken only when either the current
3031 * cgroup is removed or iteration and removal race.
3034 next
= list_entry_rcu(cgrp
->children
.next
, struct cgroup
, sibling
);
3035 } else if (likely(!cgroup_is_dead(pos
))) {
3036 next
= list_entry_rcu(pos
->sibling
.next
, struct cgroup
, sibling
);
3038 list_for_each_entry_rcu(next
, &cgrp
->children
, sibling
)
3039 if (next
->serial_nr
> pos
->serial_nr
)
3043 if (&next
->sibling
== &cgrp
->children
)
3046 return cgroup_css(next
, parent_css
->ss
);
3048 EXPORT_SYMBOL_GPL(css_next_child
);
3051 * css_next_descendant_pre - find the next descendant for pre-order walk
3052 * @pos: the current position (%NULL to initiate traversal)
3053 * @root: css whose descendants to walk
3055 * To be used by css_for_each_descendant_pre(). Find the next descendant
3056 * to visit for pre-order traversal of @root's descendants. @root is
3057 * included in the iteration and the first node to be visited.
3059 * While this function requires RCU read locking, it doesn't require the
3060 * whole traversal to be contained in a single RCU critical section. This
3061 * function will return the correct next descendant as long as both @pos
3062 * and @root are accessible and @pos is a descendant of @root.
3064 struct cgroup_subsys_state
*
3065 css_next_descendant_pre(struct cgroup_subsys_state
*pos
,
3066 struct cgroup_subsys_state
*root
)
3068 struct cgroup_subsys_state
*next
;
3070 WARN_ON_ONCE(!rcu_read_lock_held());
3072 /* if first iteration, visit @root */
3076 /* visit the first child if exists */
3077 next
= css_next_child(NULL
, pos
);
3081 /* no child, visit my or the closest ancestor's next sibling */
3082 while (pos
!= root
) {
3083 next
= css_next_child(pos
, css_parent(pos
));
3086 pos
= css_parent(pos
);
3091 EXPORT_SYMBOL_GPL(css_next_descendant_pre
);
3094 * css_rightmost_descendant - return the rightmost descendant of a css
3095 * @pos: css of interest
3097 * Return the rightmost descendant of @pos. If there's no descendant, @pos
3098 * is returned. This can be used during pre-order traversal to skip
3101 * While this function requires RCU read locking, it doesn't require the
3102 * whole traversal to be contained in a single RCU critical section. This
3103 * function will return the correct rightmost descendant as long as @pos is
3106 struct cgroup_subsys_state
*
3107 css_rightmost_descendant(struct cgroup_subsys_state
*pos
)
3109 struct cgroup_subsys_state
*last
, *tmp
;
3111 WARN_ON_ONCE(!rcu_read_lock_held());
3115 /* ->prev isn't RCU safe, walk ->next till the end */
3117 css_for_each_child(tmp
, last
)
3123 EXPORT_SYMBOL_GPL(css_rightmost_descendant
);
3125 static struct cgroup_subsys_state
*
3126 css_leftmost_descendant(struct cgroup_subsys_state
*pos
)
3128 struct cgroup_subsys_state
*last
;
3132 pos
= css_next_child(NULL
, pos
);
3139 * css_next_descendant_post - find the next descendant for post-order walk
3140 * @pos: the current position (%NULL to initiate traversal)
3141 * @root: css whose descendants to walk
3143 * To be used by css_for_each_descendant_post(). Find the next descendant
3144 * to visit for post-order traversal of @root's descendants. @root is
3145 * included in the iteration and the last node to be visited.
3147 * While this function requires RCU read locking, it doesn't require the
3148 * whole traversal to be contained in a single RCU critical section. This
3149 * function will return the correct next descendant as long as both @pos
3150 * and @cgroup are accessible and @pos is a descendant of @cgroup.
3152 struct cgroup_subsys_state
*
3153 css_next_descendant_post(struct cgroup_subsys_state
*pos
,
3154 struct cgroup_subsys_state
*root
)
3156 struct cgroup_subsys_state
*next
;
3158 WARN_ON_ONCE(!rcu_read_lock_held());
3160 /* if first iteration, visit leftmost descendant which may be @root */
3162 return css_leftmost_descendant(root
);
3164 /* if we visited @root, we're done */
3168 /* if there's an unvisited sibling, visit its leftmost descendant */
3169 next
= css_next_child(pos
, css_parent(pos
));
3171 return css_leftmost_descendant(next
);
3173 /* no sibling left, visit parent */
3174 return css_parent(pos
);
3176 EXPORT_SYMBOL_GPL(css_next_descendant_post
);
3179 * css_advance_task_iter - advance a task itererator to the next css_set
3180 * @it: the iterator to advance
3182 * Advance @it to the next css_set to walk.
3184 static void css_advance_task_iter(struct css_task_iter
*it
)
3186 struct list_head
*l
= it
->cset_link
;
3187 struct cgrp_cset_link
*link
;
3188 struct css_set
*cset
;
3190 /* Advance to the next non-empty css_set */
3193 if (l
== &it
->origin_css
->cgroup
->cset_links
) {
3194 it
->cset_link
= NULL
;
3197 link
= list_entry(l
, struct cgrp_cset_link
, cset_link
);
3199 } while (list_empty(&cset
->tasks
));
3201 it
->task
= cset
->tasks
.next
;
3205 * css_task_iter_start - initiate task iteration
3206 * @css: the css to walk tasks of
3207 * @it: the task iterator to use
3209 * Initiate iteration through the tasks of @css. The caller can call
3210 * css_task_iter_next() to walk through the tasks until the function
3211 * returns NULL. On completion of iteration, css_task_iter_end() must be
3214 * Note that this function acquires a lock which is released when the
3215 * iteration finishes. The caller can't sleep while iteration is in
3218 void css_task_iter_start(struct cgroup_subsys_state
*css
,
3219 struct css_task_iter
*it
)
3220 __acquires(css_set_lock
)
3223 * The first time anyone tries to iterate across a css, we need to
3224 * enable the list linking each css_set to its tasks, and fix up
3225 * all existing tasks.
3227 if (!use_task_css_set_links
)
3228 cgroup_enable_task_cg_lists();
3230 read_lock(&css_set_lock
);
3232 it
->origin_css
= css
;
3233 it
->cset_link
= &css
->cgroup
->cset_links
;
3235 css_advance_task_iter(it
);
3239 * css_task_iter_next - return the next task for the iterator
3240 * @it: the task iterator being iterated
3242 * The "next" function for task iteration. @it should have been
3243 * initialized via css_task_iter_start(). Returns NULL when the iteration
3246 struct task_struct
*css_task_iter_next(struct css_task_iter
*it
)
3248 struct task_struct
*res
;
3249 struct list_head
*l
= it
->task
;
3250 struct cgrp_cset_link
*link
;
3252 /* If the iterator cg is NULL, we have no tasks */
3255 res
= list_entry(l
, struct task_struct
, cg_list
);
3256 /* Advance iterator to find next entry */
3258 link
= list_entry(it
->cset_link
, struct cgrp_cset_link
, cset_link
);
3259 if (l
== &link
->cset
->tasks
) {
3261 * We reached the end of this task list - move on to the
3262 * next cgrp_cset_link.
3264 css_advance_task_iter(it
);
3272 * css_task_iter_end - finish task iteration
3273 * @it: the task iterator to finish
3275 * Finish task iteration started by css_task_iter_start().
3277 void css_task_iter_end(struct css_task_iter
*it
)
3278 __releases(css_set_lock
)
3280 read_unlock(&css_set_lock
);
3283 static inline int started_after_time(struct task_struct
*t1
,
3284 struct timespec
*time
,
3285 struct task_struct
*t2
)
3287 int start_diff
= timespec_compare(&t1
->start_time
, time
);
3288 if (start_diff
> 0) {
3290 } else if (start_diff
< 0) {
3294 * Arbitrarily, if two processes started at the same
3295 * time, we'll say that the lower pointer value
3296 * started first. Note that t2 may have exited by now
3297 * so this may not be a valid pointer any longer, but
3298 * that's fine - it still serves to distinguish
3299 * between two tasks started (effectively) simultaneously.
3306 * This function is a callback from heap_insert() and is used to order
3308 * In this case we order the heap in descending task start time.
3310 static inline int started_after(void *p1
, void *p2
)
3312 struct task_struct
*t1
= p1
;
3313 struct task_struct
*t2
= p2
;
3314 return started_after_time(t1
, &t2
->start_time
, t2
);
3318 * css_scan_tasks - iterate though all the tasks in a css
3319 * @css: the css to iterate tasks of
3320 * @test: optional test callback
3321 * @process: process callback
3322 * @data: data passed to @test and @process
3323 * @heap: optional pre-allocated heap used for task iteration
3325 * Iterate through all the tasks in @css, calling @test for each, and if it
3326 * returns %true, call @process for it also.
3328 * @test may be NULL, meaning always true (select all tasks), which
3329 * effectively duplicates css_task_iter_{start,next,end}() but does not
3330 * lock css_set_lock for the call to @process.
3332 * It is guaranteed that @process will act on every task that is a member
3333 * of @css for the duration of this call. This function may or may not
3334 * call @process for tasks that exit or move to a different css during the
3335 * call, or are forked or move into the css during the call.
3337 * Note that @test may be called with locks held, and may in some
3338 * situations be called multiple times for the same task, so it should be
3341 * If @heap is non-NULL, a heap has been pre-allocated and will be used for
3342 * heap operations (and its "gt" member will be overwritten), else a
3343 * temporary heap will be used (allocation of which may cause this function
3346 int css_scan_tasks(struct cgroup_subsys_state
*css
,
3347 bool (*test
)(struct task_struct
*, void *),
3348 void (*process
)(struct task_struct
*, void *),
3349 void *data
, struct ptr_heap
*heap
)
3352 struct css_task_iter it
;
3353 struct task_struct
*p
, *dropped
;
3354 /* Never dereference latest_task, since it's not refcounted */
3355 struct task_struct
*latest_task
= NULL
;
3356 struct ptr_heap tmp_heap
;
3357 struct timespec latest_time
= { 0, 0 };
3360 /* The caller supplied our heap and pre-allocated its memory */
3361 heap
->gt
= &started_after
;
3363 /* We need to allocate our own heap memory */
3365 retval
= heap_init(heap
, PAGE_SIZE
, GFP_KERNEL
, &started_after
);
3367 /* cannot allocate the heap */
3373 * Scan tasks in the css, using the @test callback to determine
3374 * which are of interest, and invoking @process callback on the
3375 * ones which need an update. Since we don't want to hold any
3376 * locks during the task updates, gather tasks to be processed in a
3377 * heap structure. The heap is sorted by descending task start
3378 * time. If the statically-sized heap fills up, we overflow tasks
3379 * that started later, and in future iterations only consider tasks
3380 * that started after the latest task in the previous pass. This
3381 * guarantees forward progress and that we don't miss any tasks.
3384 css_task_iter_start(css
, &it
);
3385 while ((p
= css_task_iter_next(&it
))) {
3387 * Only affect tasks that qualify per the caller's callback,
3388 * if he provided one
3390 if (test
&& !test(p
, data
))
3393 * Only process tasks that started after the last task
3396 if (!started_after_time(p
, &latest_time
, latest_task
))
3398 dropped
= heap_insert(heap
, p
);
3399 if (dropped
== NULL
) {
3401 * The new task was inserted; the heap wasn't
3405 } else if (dropped
!= p
) {
3407 * The new task was inserted, and pushed out a
3411 put_task_struct(dropped
);
3414 * Else the new task was newer than anything already in
3415 * the heap and wasn't inserted
3418 css_task_iter_end(&it
);
3421 for (i
= 0; i
< heap
->size
; i
++) {
3422 struct task_struct
*q
= heap
->ptrs
[i
];
3424 latest_time
= q
->start_time
;
3427 /* Process the task per the caller's callback */
3432 * If we had to process any tasks at all, scan again
3433 * in case some of them were in the middle of forking
3434 * children that didn't get processed.
3435 * Not the most efficient way to do it, but it avoids
3436 * having to take callback_mutex in the fork path
3440 if (heap
== &tmp_heap
)
3441 heap_free(&tmp_heap
);
3445 static void cgroup_transfer_one_task(struct task_struct
*task
, void *data
)
3447 struct cgroup
*new_cgroup
= data
;
3449 mutex_lock(&cgroup_mutex
);
3450 cgroup_attach_task(new_cgroup
, task
, false);
3451 mutex_unlock(&cgroup_mutex
);
3455 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3456 * @to: cgroup to which the tasks will be moved
3457 * @from: cgroup in which the tasks currently reside
3459 int cgroup_transfer_tasks(struct cgroup
*to
, struct cgroup
*from
)
3461 return css_scan_tasks(&from
->dummy_css
, NULL
, cgroup_transfer_one_task
,
3466 * Stuff for reading the 'tasks'/'procs' files.
3468 * Reading this file can return large amounts of data if a cgroup has
3469 * *lots* of attached tasks. So it may need several calls to read(),
3470 * but we cannot guarantee that the information we produce is correct
3471 * unless we produce it entirely atomically.
3475 /* which pidlist file are we talking about? */
3476 enum cgroup_filetype
{
3482 * A pidlist is a list of pids that virtually represents the contents of one
3483 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3484 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3487 struct cgroup_pidlist
{
3489 * used to find which pidlist is wanted. doesn't change as long as
3490 * this particular list stays in the list.
3492 struct { enum cgroup_filetype type
; struct pid_namespace
*ns
; } key
;
3495 /* how many elements the above list has */
3497 /* how many files are using the current array */
3499 /* each of these stored in a list by its cgroup */
3500 struct list_head links
;
3501 /* pointer to the cgroup we belong to, for list removal purposes */
3502 struct cgroup
*owner
;
3503 /* protects the other fields */
3504 struct rw_semaphore rwsem
;
3508 * The following two functions "fix" the issue where there are more pids
3509 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3510 * TODO: replace with a kernel-wide solution to this problem
3512 #define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3513 static void *pidlist_allocate(int count
)
3515 if (PIDLIST_TOO_LARGE(count
))
3516 return vmalloc(count
* sizeof(pid_t
));
3518 return kmalloc(count
* sizeof(pid_t
), GFP_KERNEL
);
3520 static void pidlist_free(void *p
)
3522 if (is_vmalloc_addr(p
))
3529 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
3530 * Returns the number of unique elements.
3532 static int pidlist_uniq(pid_t
*list
, int length
)
3537 * we presume the 0th element is unique, so i starts at 1. trivial
3538 * edge cases first; no work needs to be done for either
3540 if (length
== 0 || length
== 1)
3542 /* src and dest walk down the list; dest counts unique elements */
3543 for (src
= 1; src
< length
; src
++) {
3544 /* find next unique element */
3545 while (list
[src
] == list
[src
-1]) {
3550 /* dest always points to where the next unique element goes */
3551 list
[dest
] = list
[src
];
3558 static int cmppid(const void *a
, const void *b
)
3560 return *(pid_t
*)a
- *(pid_t
*)b
;
3564 * find the appropriate pidlist for our purpose (given procs vs tasks)
3565 * returns with the lock on that pidlist already held, and takes care
3566 * of the use count, or returns NULL with no locks held if we're out of
3569 static struct cgroup_pidlist
*cgroup_pidlist_find(struct cgroup
*cgrp
,
3570 enum cgroup_filetype type
)
3572 struct cgroup_pidlist
*l
;
3573 /* don't need task_nsproxy() if we're looking at ourself */
3574 struct pid_namespace
*ns
= task_active_pid_ns(current
);
3577 * We can't drop the pidlist_mutex before taking the l->rwsem in case
3578 * the last ref-holder is trying to remove l from the list at the same
3579 * time. Holding the pidlist_mutex precludes somebody taking whichever
3580 * list we find out from under us - compare release_pid_array().
3582 mutex_lock(&cgrp
->pidlist_mutex
);
3583 list_for_each_entry(l
, &cgrp
->pidlists
, links
) {
3584 if (l
->key
.type
== type
&& l
->key
.ns
== ns
) {
3585 /* make sure l doesn't vanish out from under us */
3586 down_write(&l
->rwsem
);
3587 mutex_unlock(&cgrp
->pidlist_mutex
);
3591 /* entry not found; create a new one */
3592 l
= kzalloc(sizeof(struct cgroup_pidlist
), GFP_KERNEL
);
3594 mutex_unlock(&cgrp
->pidlist_mutex
);
3597 init_rwsem(&l
->rwsem
);
3598 down_write(&l
->rwsem
);
3600 l
->key
.ns
= get_pid_ns(ns
);
3602 list_add(&l
->links
, &cgrp
->pidlists
);
3603 mutex_unlock(&cgrp
->pidlist_mutex
);
3608 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3610 static int pidlist_array_load(struct cgroup
*cgrp
, enum cgroup_filetype type
,
3611 struct cgroup_pidlist
**lp
)
3615 int pid
, n
= 0; /* used for populating the array */
3616 struct css_task_iter it
;
3617 struct task_struct
*tsk
;
3618 struct cgroup_pidlist
*l
;
3621 * If cgroup gets more users after we read count, we won't have
3622 * enough space - tough. This race is indistinguishable to the
3623 * caller from the case that the additional cgroup users didn't
3624 * show up until sometime later on.
3626 length
= cgroup_task_count(cgrp
);
3627 array
= pidlist_allocate(length
);
3630 /* now, populate the array */
3631 css_task_iter_start(&cgrp
->dummy_css
, &it
);
3632 while ((tsk
= css_task_iter_next(&it
))) {
3633 if (unlikely(n
== length
))
3635 /* get tgid or pid for procs or tasks file respectively */
3636 if (type
== CGROUP_FILE_PROCS
)
3637 pid
= task_tgid_vnr(tsk
);
3639 pid
= task_pid_vnr(tsk
);
3640 if (pid
> 0) /* make sure to only use valid results */
3643 css_task_iter_end(&it
);
3645 /* now sort & (if procs) strip out duplicates */
3646 sort(array
, length
, sizeof(pid_t
), cmppid
, NULL
);
3647 if (type
== CGROUP_FILE_PROCS
)
3648 length
= pidlist_uniq(array
, length
);
3649 l
= cgroup_pidlist_find(cgrp
, type
);
3651 pidlist_free(array
);
3654 /* store array, freeing old if necessary - lock already held */
3655 pidlist_free(l
->list
);
3659 up_write(&l
->rwsem
);
3665 * cgroupstats_build - build and fill cgroupstats
3666 * @stats: cgroupstats to fill information into
3667 * @dentry: A dentry entry belonging to the cgroup for which stats have
3670 * Build and fill cgroupstats so that taskstats can export it to user
3673 int cgroupstats_build(struct cgroupstats
*stats
, struct dentry
*dentry
)
3676 struct cgroup
*cgrp
;
3677 struct css_task_iter it
;
3678 struct task_struct
*tsk
;
3681 * Validate dentry by checking the superblock operations,
3682 * and make sure it's a directory.
3684 if (dentry
->d_sb
->s_op
!= &cgroup_ops
||
3685 !S_ISDIR(dentry
->d_inode
->i_mode
))
3689 cgrp
= dentry
->d_fsdata
;
3691 css_task_iter_start(&cgrp
->dummy_css
, &it
);
3692 while ((tsk
= css_task_iter_next(&it
))) {
3693 switch (tsk
->state
) {
3695 stats
->nr_running
++;
3697 case TASK_INTERRUPTIBLE
:
3698 stats
->nr_sleeping
++;
3700 case TASK_UNINTERRUPTIBLE
:
3701 stats
->nr_uninterruptible
++;
3704 stats
->nr_stopped
++;
3707 if (delayacct_is_task_waiting_on_io(tsk
))
3708 stats
->nr_io_wait
++;
3712 css_task_iter_end(&it
);
3720 * seq_file methods for the tasks/procs files. The seq_file position is the
3721 * next pid to display; the seq_file iterator is a pointer to the pid
3722 * in the cgroup->l->list array.
3725 static void *cgroup_pidlist_start(struct seq_file
*s
, loff_t
*pos
)
3728 * Initially we receive a position value that corresponds to
3729 * one more than the last pid shown (or 0 on the first call or
3730 * after a seek to the start). Use a binary-search to find the
3731 * next pid to display, if any
3733 struct cgroup_pidlist
*l
= s
->private;
3734 int index
= 0, pid
= *pos
;
3737 down_read(&l
->rwsem
);
3739 int end
= l
->length
;
3741 while (index
< end
) {
3742 int mid
= (index
+ end
) / 2;
3743 if (l
->list
[mid
] == pid
) {
3746 } else if (l
->list
[mid
] <= pid
)
3752 /* If we're off the end of the array, we're done */
3753 if (index
>= l
->length
)
3755 /* Update the abstract position to be the actual pid that we found */
3756 iter
= l
->list
+ index
;
3761 static void cgroup_pidlist_stop(struct seq_file
*s
, void *v
)
3763 struct cgroup_pidlist
*l
= s
->private;
3767 static void *cgroup_pidlist_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
3769 struct cgroup_pidlist
*l
= s
->private;
3771 pid_t
*end
= l
->list
+ l
->length
;
3773 * Advance to the next pid in the array. If this goes off the
3785 static int cgroup_pidlist_show(struct seq_file
*s
, void *v
)
3787 return seq_printf(s
, "%d\n", *(int *)v
);
3791 * seq_operations functions for iterating on pidlists through seq_file -
3792 * independent of whether it's tasks or procs
3794 static const struct seq_operations cgroup_pidlist_seq_operations
= {
3795 .start
= cgroup_pidlist_start
,
3796 .stop
= cgroup_pidlist_stop
,
3797 .next
= cgroup_pidlist_next
,
3798 .show
= cgroup_pidlist_show
,
3801 static void cgroup_release_pid_array(struct cgroup_pidlist
*l
)
3804 * the case where we're the last user of this particular pidlist will
3805 * have us remove it from the cgroup's list, which entails taking the
3806 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3807 * pidlist_mutex, we have to take pidlist_mutex first.
3809 mutex_lock(&l
->owner
->pidlist_mutex
);
3810 down_write(&l
->rwsem
);
3811 BUG_ON(!l
->use_count
);
3812 if (!--l
->use_count
) {
3813 /* we're the last user if refcount is 0; remove and free */
3814 list_del(&l
->links
);
3815 mutex_unlock(&l
->owner
->pidlist_mutex
);
3816 pidlist_free(l
->list
);
3817 put_pid_ns(l
->key
.ns
);
3818 up_write(&l
->rwsem
);
3822 mutex_unlock(&l
->owner
->pidlist_mutex
);
3823 up_write(&l
->rwsem
);
3826 static int cgroup_pidlist_release(struct inode
*inode
, struct file
*file
)
3828 struct cgroup_pidlist
*l
;
3829 if (!(file
->f_mode
& FMODE_READ
))
3832 * the seq_file will only be initialized if the file was opened for
3833 * reading; hence we check if it's not null only in that case.
3835 l
= ((struct seq_file
*)file
->private_data
)->private;
3836 cgroup_release_pid_array(l
);
3837 return seq_release(inode
, file
);
3840 static const struct file_operations cgroup_pidlist_operations
= {
3842 .llseek
= seq_lseek
,
3843 .write
= cgroup_file_write
,
3844 .release
= cgroup_pidlist_release
,
3848 * The following functions handle opens on a file that displays a pidlist
3849 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3852 /* helper function for the two below it */
3853 static int cgroup_pidlist_open(struct file
*file
, enum cgroup_filetype type
)
3855 struct cgroup
*cgrp
= __d_cgrp(file
->f_dentry
->d_parent
);
3856 struct cgroup_pidlist
*l
;
3859 /* Nothing to do for write-only files */
3860 if (!(file
->f_mode
& FMODE_READ
))
3863 /* have the array populated */
3864 retval
= pidlist_array_load(cgrp
, type
, &l
);
3867 /* configure file information */
3868 file
->f_op
= &cgroup_pidlist_operations
;
3870 retval
= seq_open(file
, &cgroup_pidlist_seq_operations
);
3872 cgroup_release_pid_array(l
);
3875 ((struct seq_file
*)file
->private_data
)->private = l
;
3878 static int cgroup_tasks_open(struct inode
*unused
, struct file
*file
)
3880 return cgroup_pidlist_open(file
, CGROUP_FILE_TASKS
);
3882 static int cgroup_procs_open(struct inode
*unused
, struct file
*file
)
3884 return cgroup_pidlist_open(file
, CGROUP_FILE_PROCS
);
3887 static u64
cgroup_read_notify_on_release(struct cgroup_subsys_state
*css
,
3890 return notify_on_release(css
->cgroup
);
3893 static int cgroup_write_notify_on_release(struct cgroup_subsys_state
*css
,
3894 struct cftype
*cft
, u64 val
)
3896 clear_bit(CGRP_RELEASABLE
, &css
->cgroup
->flags
);
3898 set_bit(CGRP_NOTIFY_ON_RELEASE
, &css
->cgroup
->flags
);
3900 clear_bit(CGRP_NOTIFY_ON_RELEASE
, &css
->cgroup
->flags
);
3905 * When dput() is called asynchronously, if umount has been done and
3906 * then deactivate_super() in cgroup_free_fn() kills the superblock,
3907 * there's a small window that vfs will see the root dentry with non-zero
3908 * refcnt and trigger BUG().
3910 * That's why we hold a reference before dput() and drop it right after.
3912 static void cgroup_dput(struct cgroup
*cgrp
)
3914 struct super_block
*sb
= cgrp
->root
->sb
;
3916 atomic_inc(&sb
->s_active
);
3918 deactivate_super(sb
);
3922 * Unregister event and free resources.
3924 * Gets called from workqueue.
3926 static void cgroup_event_remove(struct work_struct
*work
)
3928 struct cgroup_event
*event
= container_of(work
, struct cgroup_event
,
3930 struct cgroup_subsys_state
*css
= event
->css
;
3932 remove_wait_queue(event
->wqh
, &event
->wait
);
3934 event
->cft
->unregister_event(css
, event
->cft
, event
->eventfd
);
3936 /* Notify userspace the event is going away. */
3937 eventfd_signal(event
->eventfd
, 1);
3939 eventfd_ctx_put(event
->eventfd
);
3945 * Gets called on POLLHUP on eventfd when user closes it.
3947 * Called with wqh->lock held and interrupts disabled.
3949 static int cgroup_event_wake(wait_queue_t
*wait
, unsigned mode
,
3950 int sync
, void *key
)
3952 struct cgroup_event
*event
= container_of(wait
,
3953 struct cgroup_event
, wait
);
3954 struct cgroup
*cgrp
= event
->css
->cgroup
;
3955 unsigned long flags
= (unsigned long)key
;
3957 if (flags
& POLLHUP
) {
3959 * If the event has been detached at cgroup removal, we
3960 * can simply return knowing the other side will cleanup
3963 * We can't race against event freeing since the other
3964 * side will require wqh->lock via remove_wait_queue(),
3967 spin_lock(&cgrp
->event_list_lock
);
3968 if (!list_empty(&event
->list
)) {
3969 list_del_init(&event
->list
);
3971 * We are in atomic context, but cgroup_event_remove()
3972 * may sleep, so we have to call it in workqueue.
3974 schedule_work(&event
->remove
);
3976 spin_unlock(&cgrp
->event_list_lock
);
3982 static void cgroup_event_ptable_queue_proc(struct file
*file
,
3983 wait_queue_head_t
*wqh
, poll_table
*pt
)
3985 struct cgroup_event
*event
= container_of(pt
,
3986 struct cgroup_event
, pt
);
3989 add_wait_queue(wqh
, &event
->wait
);
3993 * Parse input and register new cgroup event handler.
3995 * Input must be in format '<event_fd> <control_fd> <args>'.
3996 * Interpretation of args is defined by control file implementation.
3998 static int cgroup_write_event_control(struct cgroup_subsys_state
*dummy_css
,
3999 struct cftype
*cft
, const char *buffer
)
4001 struct cgroup
*cgrp
= dummy_css
->cgroup
;
4002 struct cgroup_event
*event
;
4003 struct cgroup_subsys_state
*cfile_css
;
4004 unsigned int efd
, cfd
;
4010 efd
= simple_strtoul(buffer
, &endp
, 10);
4015 cfd
= simple_strtoul(buffer
, &endp
, 10);
4016 if ((*endp
!= ' ') && (*endp
!= '\0'))
4020 event
= kzalloc(sizeof(*event
), GFP_KERNEL
);
4024 INIT_LIST_HEAD(&event
->list
);
4025 init_poll_funcptr(&event
->pt
, cgroup_event_ptable_queue_proc
);
4026 init_waitqueue_func_entry(&event
->wait
, cgroup_event_wake
);
4027 INIT_WORK(&event
->remove
, cgroup_event_remove
);
4035 event
->eventfd
= eventfd_ctx_fileget(efile
.file
);
4036 if (IS_ERR(event
->eventfd
)) {
4037 ret
= PTR_ERR(event
->eventfd
);
4044 goto out_put_eventfd
;
4047 /* the process need read permission on control file */
4048 /* AV: shouldn't we check that it's been opened for read instead? */
4049 ret
= inode_permission(file_inode(cfile
.file
), MAY_READ
);
4053 event
->cft
= __file_cft(cfile
.file
);
4054 if (IS_ERR(event
->cft
)) {
4055 ret
= PTR_ERR(event
->cft
);
4059 if (!event
->cft
->ss
) {
4065 * Determine the css of @cfile, verify it belongs to the same
4066 * cgroup as cgroup.event_control, and associate @event with it.
4067 * Remaining events are automatically removed on cgroup destruction
4068 * but the removal is asynchronous, so take an extra ref.
4073 event
->css
= cgroup_css(cgrp
, event
->cft
->ss
);
4074 cfile_css
= css_from_dir(cfile
.file
->f_dentry
->d_parent
, event
->cft
->ss
);
4075 if (event
->css
&& event
->css
== cfile_css
&& css_tryget(event
->css
))
4082 if (!event
->cft
->register_event
|| !event
->cft
->unregister_event
) {
4087 ret
= event
->cft
->register_event(event
->css
, event
->cft
,
4088 event
->eventfd
, buffer
);
4092 efile
.file
->f_op
->poll(efile
.file
, &event
->pt
);
4094 spin_lock(&cgrp
->event_list_lock
);
4095 list_add(&event
->list
, &cgrp
->event_list
);
4096 spin_unlock(&cgrp
->event_list_lock
);
4104 css_put(event
->css
);
4108 eventfd_ctx_put(event
->eventfd
);
4117 static u64
cgroup_clone_children_read(struct cgroup_subsys_state
*css
,
4120 return test_bit(CGRP_CPUSET_CLONE_CHILDREN
, &css
->cgroup
->flags
);
4123 static int cgroup_clone_children_write(struct cgroup_subsys_state
*css
,
4124 struct cftype
*cft
, u64 val
)
4127 set_bit(CGRP_CPUSET_CLONE_CHILDREN
, &css
->cgroup
->flags
);
4129 clear_bit(CGRP_CPUSET_CLONE_CHILDREN
, &css
->cgroup
->flags
);
4133 static struct cftype cgroup_base_files
[] = {
4135 .name
= "cgroup.procs",
4136 .open
= cgroup_procs_open
,
4137 .write_u64
= cgroup_procs_write
,
4138 .release
= cgroup_pidlist_release
,
4139 .mode
= S_IRUGO
| S_IWUSR
,
4142 .name
= "cgroup.event_control",
4143 .write_string
= cgroup_write_event_control
,
4147 .name
= "cgroup.clone_children",
4148 .flags
= CFTYPE_INSANE
,
4149 .read_u64
= cgroup_clone_children_read
,
4150 .write_u64
= cgroup_clone_children_write
,
4153 .name
= "cgroup.sane_behavior",
4154 .flags
= CFTYPE_ONLY_ON_ROOT
,
4155 .read_seq_string
= cgroup_sane_behavior_show
,
4159 * Historical crazy stuff. These don't have "cgroup." prefix and
4160 * don't exist if sane_behavior. If you're depending on these, be
4161 * prepared to be burned.
4165 .flags
= CFTYPE_INSANE
, /* use "procs" instead */
4166 .open
= cgroup_tasks_open
,
4167 .write_u64
= cgroup_tasks_write
,
4168 .release
= cgroup_pidlist_release
,
4169 .mode
= S_IRUGO
| S_IWUSR
,
4172 .name
= "notify_on_release",
4173 .flags
= CFTYPE_INSANE
,
4174 .read_u64
= cgroup_read_notify_on_release
,
4175 .write_u64
= cgroup_write_notify_on_release
,
4178 .name
= "release_agent",
4179 .flags
= CFTYPE_INSANE
| CFTYPE_ONLY_ON_ROOT
,
4180 .read_seq_string
= cgroup_release_agent_show
,
4181 .write_string
= cgroup_release_agent_write
,
4182 .max_write_len
= PATH_MAX
,
4188 * cgroup_populate_dir - create subsys files in a cgroup directory
4189 * @cgrp: target cgroup
4190 * @subsys_mask: mask of the subsystem ids whose files should be added
4192 * On failure, no file is added.
4194 static int cgroup_populate_dir(struct cgroup
*cgrp
, unsigned long subsys_mask
)
4196 struct cgroup_subsys
*ss
;
4199 /* process cftsets of each subsystem */
4200 for_each_subsys(ss
, i
) {
4201 struct cftype_set
*set
;
4203 if (!test_bit(i
, &subsys_mask
))
4206 list_for_each_entry(set
, &ss
->cftsets
, node
) {
4207 ret
= cgroup_addrm_files(cgrp
, set
->cfts
, true);
4214 cgroup_clear_dir(cgrp
, subsys_mask
);
4219 * css destruction is four-stage process.
4221 * 1. Destruction starts. Killing of the percpu_ref is initiated.
4222 * Implemented in kill_css().
4224 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
4225 * and thus css_tryget() is guaranteed to fail, the css can be offlined
4226 * by invoking offline_css(). After offlining, the base ref is put.
4227 * Implemented in css_killed_work_fn().
4229 * 3. When the percpu_ref reaches zero, the only possible remaining
4230 * accessors are inside RCU read sections. css_release() schedules the
4233 * 4. After the grace period, the css can be freed. Implemented in
4234 * css_free_work_fn().
4236 * It is actually hairier because both step 2 and 4 require process context
4237 * and thus involve punting to css->destroy_work adding two additional
4238 * steps to the already complex sequence.
4240 static void css_free_work_fn(struct work_struct
*work
)
4242 struct cgroup_subsys_state
*css
=
4243 container_of(work
, struct cgroup_subsys_state
, destroy_work
);
4244 struct cgroup
*cgrp
= css
->cgroup
;
4247 css_put(css
->parent
);
4249 css
->ss
->css_free(css
);
4253 static void css_free_rcu_fn(struct rcu_head
*rcu_head
)
4255 struct cgroup_subsys_state
*css
=
4256 container_of(rcu_head
, struct cgroup_subsys_state
, rcu_head
);
4259 * css holds an extra ref to @cgrp->dentry which is put on the last
4260 * css_put(). dput() requires process context which we don't have.
4262 INIT_WORK(&css
->destroy_work
, css_free_work_fn
);
4263 queue_work(cgroup_destroy_wq
, &css
->destroy_work
);
4266 static void css_release(struct percpu_ref
*ref
)
4268 struct cgroup_subsys_state
*css
=
4269 container_of(ref
, struct cgroup_subsys_state
, refcnt
);
4271 call_rcu(&css
->rcu_head
, css_free_rcu_fn
);
4274 static void init_css(struct cgroup_subsys_state
*css
, struct cgroup_subsys
*ss
,
4275 struct cgroup
*cgrp
)
4282 css
->parent
= cgroup_css(cgrp
->parent
, ss
);
4284 css
->flags
|= CSS_ROOT
;
4286 BUG_ON(cgroup_css(cgrp
, ss
));
4289 /* invoke ->css_online() on a new CSS and mark it online if successful */
4290 static int online_css(struct cgroup_subsys_state
*css
)
4292 struct cgroup_subsys
*ss
= css
->ss
;
4295 lockdep_assert_held(&cgroup_mutex
);
4298 ret
= ss
->css_online(css
);
4300 css
->flags
|= CSS_ONLINE
;
4301 css
->cgroup
->nr_css
++;
4302 rcu_assign_pointer(css
->cgroup
->subsys
[ss
->subsys_id
], css
);
4307 /* if the CSS is online, invoke ->css_offline() on it and mark it offline */
4308 static void offline_css(struct cgroup_subsys_state
*css
)
4310 struct cgroup_subsys
*ss
= css
->ss
;
4312 lockdep_assert_held(&cgroup_mutex
);
4314 if (!(css
->flags
& CSS_ONLINE
))
4317 if (ss
->css_offline
)
4318 ss
->css_offline(css
);
4320 css
->flags
&= ~CSS_ONLINE
;
4321 css
->cgroup
->nr_css
--;
4322 RCU_INIT_POINTER(css
->cgroup
->subsys
[ss
->subsys_id
], css
);
4326 * cgroup_create - create a cgroup
4327 * @parent: cgroup that will be parent of the new cgroup
4328 * @dentry: dentry of the new cgroup
4329 * @mode: mode to set on new inode
4331 * Must be called with the mutex on the parent inode held
4333 static long cgroup_create(struct cgroup
*parent
, struct dentry
*dentry
,
4336 struct cgroup_subsys_state
*css_ar
[CGROUP_SUBSYS_COUNT
] = { };
4337 struct cgroup
*cgrp
;
4338 struct cgroup_name
*name
;
4339 struct cgroupfs_root
*root
= parent
->root
;
4341 struct cgroup_subsys
*ss
;
4342 struct super_block
*sb
= root
->sb
;
4344 /* allocate the cgroup and its ID, 0 is reserved for the root */
4345 cgrp
= kzalloc(sizeof(*cgrp
), GFP_KERNEL
);
4349 name
= cgroup_alloc_name(dentry
);
4352 rcu_assign_pointer(cgrp
->name
, name
);
4355 * Temporarily set the pointer to NULL, so idr_find() won't return
4356 * a half-baked cgroup.
4358 cgrp
->id
= idr_alloc(&root
->cgroup_idr
, NULL
, 1, 0, GFP_KERNEL
);
4363 * Only live parents can have children. Note that the liveliness
4364 * check isn't strictly necessary because cgroup_mkdir() and
4365 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4366 * anyway so that locking is contained inside cgroup proper and we
4367 * don't get nasty surprises if we ever grow another caller.
4369 if (!cgroup_lock_live_group(parent
)) {
4374 /* Grab a reference on the superblock so the hierarchy doesn't
4375 * get deleted on unmount if there are child cgroups. This
4376 * can be done outside cgroup_mutex, since the sb can't
4377 * disappear while someone has an open control file on the
4379 atomic_inc(&sb
->s_active
);
4381 init_cgroup_housekeeping(cgrp
);
4383 dentry
->d_fsdata
= cgrp
;
4384 cgrp
->dentry
= dentry
;
4386 cgrp
->parent
= parent
;
4387 cgrp
->dummy_css
.parent
= &parent
->dummy_css
;
4388 cgrp
->root
= parent
->root
;
4390 if (notify_on_release(parent
))
4391 set_bit(CGRP_NOTIFY_ON_RELEASE
, &cgrp
->flags
);
4393 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN
, &parent
->flags
))
4394 set_bit(CGRP_CPUSET_CLONE_CHILDREN
, &cgrp
->flags
);
4396 for_each_root_subsys(root
, ss
) {
4397 struct cgroup_subsys_state
*css
;
4399 css
= ss
->css_alloc(cgroup_css(parent
, ss
));
4404 css_ar
[ss
->subsys_id
] = css
;
4406 err
= percpu_ref_init(&css
->refcnt
, css_release
);
4410 init_css(css
, ss
, cgrp
);
4414 * Create directory. cgroup_create_file() returns with the new
4415 * directory locked on success so that it can be populated without
4416 * dropping cgroup_mutex.
4418 err
= cgroup_create_file(dentry
, S_IFDIR
| mode
, sb
);
4421 lockdep_assert_held(&dentry
->d_inode
->i_mutex
);
4423 cgrp
->serial_nr
= cgroup_serial_nr_next
++;
4425 /* allocation complete, commit to creation */
4426 list_add_tail_rcu(&cgrp
->sibling
, &cgrp
->parent
->children
);
4427 root
->number_of_cgroups
++;
4429 /* each css holds a ref to the cgroup's dentry and the parent css */
4430 for_each_root_subsys(root
, ss
) {
4431 struct cgroup_subsys_state
*css
= css_ar
[ss
->subsys_id
];
4434 css_get(css
->parent
);
4437 /* hold a ref to the parent's dentry */
4438 dget(parent
->dentry
);
4440 /* creation succeeded, notify subsystems */
4441 for_each_root_subsys(root
, ss
) {
4442 struct cgroup_subsys_state
*css
= css_ar
[ss
->subsys_id
];
4444 err
= online_css(css
);
4448 if (ss
->broken_hierarchy
&& !ss
->warned_broken_hierarchy
&&
4450 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",
4451 current
->comm
, current
->pid
, ss
->name
);
4452 if (!strcmp(ss
->name
, "memory"))
4453 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4454 ss
->warned_broken_hierarchy
= true;
4458 idr_replace(&root
->cgroup_idr
, cgrp
, cgrp
->id
);
4460 err
= cgroup_addrm_files(cgrp
, cgroup_base_files
, true);
4464 err
= cgroup_populate_dir(cgrp
, root
->subsys_mask
);
4468 mutex_unlock(&cgroup_mutex
);
4469 mutex_unlock(&cgrp
->dentry
->d_inode
->i_mutex
);
4474 for_each_root_subsys(root
, ss
) {
4475 struct cgroup_subsys_state
*css
= css_ar
[ss
->subsys_id
];
4478 percpu_ref_cancel_init(&css
->refcnt
);
4482 mutex_unlock(&cgroup_mutex
);
4483 /* Release the reference count that we took on the superblock */
4484 deactivate_super(sb
);
4486 idr_remove(&root
->cgroup_idr
, cgrp
->id
);
4488 kfree(rcu_dereference_raw(cgrp
->name
));
4494 cgroup_destroy_locked(cgrp
);
4495 mutex_unlock(&cgroup_mutex
);
4496 mutex_unlock(&dentry
->d_inode
->i_mutex
);
4500 static int cgroup_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
4502 struct cgroup
*c_parent
= dentry
->d_parent
->d_fsdata
;
4504 /* the vfs holds inode->i_mutex already */
4505 return cgroup_create(c_parent
, dentry
, mode
| S_IFDIR
);
4509 * This is called when the refcnt of a css is confirmed to be killed.
4510 * css_tryget() is now guaranteed to fail.
4512 static void css_killed_work_fn(struct work_struct
*work
)
4514 struct cgroup_subsys_state
*css
=
4515 container_of(work
, struct cgroup_subsys_state
, destroy_work
);
4516 struct cgroup
*cgrp
= css
->cgroup
;
4518 mutex_lock(&cgroup_mutex
);
4521 * css_tryget() is guaranteed to fail now. Tell subsystems to
4522 * initate destruction.
4527 * If @cgrp is marked dead, it's waiting for refs of all css's to
4528 * be disabled before proceeding to the second phase of cgroup
4529 * destruction. If we are the last one, kick it off.
4531 if (!cgrp
->nr_css
&& cgroup_is_dead(cgrp
))
4532 cgroup_destroy_css_killed(cgrp
);
4534 mutex_unlock(&cgroup_mutex
);
4537 * Put the css refs from kill_css(). Each css holds an extra
4538 * reference to the cgroup's dentry and cgroup removal proceeds
4539 * regardless of css refs. On the last put of each css, whenever
4540 * that may be, the extra dentry ref is put so that dentry
4541 * destruction happens only after all css's are released.
4546 /* css kill confirmation processing requires process context, bounce */
4547 static void css_killed_ref_fn(struct percpu_ref
*ref
)
4549 struct cgroup_subsys_state
*css
=
4550 container_of(ref
, struct cgroup_subsys_state
, refcnt
);
4552 INIT_WORK(&css
->destroy_work
, css_killed_work_fn
);
4553 queue_work(cgroup_destroy_wq
, &css
->destroy_work
);
4557 * kill_css - destroy a css
4558 * @css: css to destroy
4560 * This function initiates destruction of @css by removing cgroup interface
4561 * files and putting its base reference. ->css_offline() will be invoked
4562 * asynchronously once css_tryget() is guaranteed to fail and when the
4563 * reference count reaches zero, @css will be released.
4565 static void kill_css(struct cgroup_subsys_state
*css
)
4567 cgroup_clear_dir(css
->cgroup
, 1 << css
->ss
->subsys_id
);
4570 * Killing would put the base ref, but we need to keep it alive
4571 * until after ->css_offline().
4576 * cgroup core guarantees that, by the time ->css_offline() is
4577 * invoked, no new css reference will be given out via
4578 * css_tryget(). We can't simply call percpu_ref_kill() and
4579 * proceed to offlining css's because percpu_ref_kill() doesn't
4580 * guarantee that the ref is seen as killed on all CPUs on return.
4582 * Use percpu_ref_kill_and_confirm() to get notifications as each
4583 * css is confirmed to be seen as killed on all CPUs.
4585 percpu_ref_kill_and_confirm(&css
->refcnt
, css_killed_ref_fn
);
4589 * cgroup_destroy_locked - the first stage of cgroup destruction
4590 * @cgrp: cgroup to be destroyed
4592 * css's make use of percpu refcnts whose killing latency shouldn't be
4593 * exposed to userland and are RCU protected. Also, cgroup core needs to
4594 * guarantee that css_tryget() won't succeed by the time ->css_offline() is
4595 * invoked. To satisfy all the requirements, destruction is implemented in
4596 * the following two steps.
4598 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4599 * userland visible parts and start killing the percpu refcnts of
4600 * css's. Set up so that the next stage will be kicked off once all
4601 * the percpu refcnts are confirmed to be killed.
4603 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4604 * rest of destruction. Once all cgroup references are gone, the
4605 * cgroup is RCU-freed.
4607 * This function implements s1. After this step, @cgrp is gone as far as
4608 * the userland is concerned and a new cgroup with the same name may be
4609 * created. As cgroup doesn't care about the names internally, this
4610 * doesn't cause any problem.
4612 static int cgroup_destroy_locked(struct cgroup
*cgrp
)
4613 __releases(&cgroup_mutex
) __acquires(&cgroup_mutex
)
4615 struct dentry
*d
= cgrp
->dentry
;
4616 struct cgroup_event
*event
, *tmp
;
4617 struct cgroup_subsys
*ss
;
4618 struct cgroup
*child
;
4621 lockdep_assert_held(&d
->d_inode
->i_mutex
);
4622 lockdep_assert_held(&cgroup_mutex
);
4625 * css_set_lock synchronizes access to ->cset_links and prevents
4626 * @cgrp from being removed while __put_css_set() is in progress.
4628 read_lock(&css_set_lock
);
4629 empty
= list_empty(&cgrp
->cset_links
);
4630 read_unlock(&css_set_lock
);
4635 * Make sure there's no live children. We can't test ->children
4636 * emptiness as dead children linger on it while being destroyed;
4637 * otherwise, "rmdir parent/child parent" may fail with -EBUSY.
4641 list_for_each_entry_rcu(child
, &cgrp
->children
, sibling
) {
4642 empty
= cgroup_is_dead(child
);
4651 * Initiate massacre of all css's. cgroup_destroy_css_killed()
4652 * will be invoked to perform the rest of destruction once the
4653 * percpu refs of all css's are confirmed to be killed.
4655 for_each_root_subsys(cgrp
->root
, ss
)
4656 kill_css(cgroup_css(cgrp
, ss
));
4659 * Mark @cgrp dead. This prevents further task migration and child
4660 * creation by disabling cgroup_lock_live_group(). Note that
4661 * CGRP_DEAD assertion is depended upon by css_next_child() to
4662 * resume iteration after dropping RCU read lock. See
4663 * css_next_child() for details.
4665 set_bit(CGRP_DEAD
, &cgrp
->flags
);
4667 /* CGRP_DEAD is set, remove from ->release_list for the last time */
4668 raw_spin_lock(&release_list_lock
);
4669 if (!list_empty(&cgrp
->release_list
))
4670 list_del_init(&cgrp
->release_list
);
4671 raw_spin_unlock(&release_list_lock
);
4674 * If @cgrp has css's attached, the second stage of cgroup
4675 * destruction is kicked off from css_killed_work_fn() after the
4676 * refs of all attached css's are killed. If @cgrp doesn't have
4677 * any css, we kick it off here.
4680 cgroup_destroy_css_killed(cgrp
);
4683 * Clear the base files and remove @cgrp directory. The removal
4684 * puts the base ref but we aren't quite done with @cgrp yet, so
4687 cgroup_addrm_files(cgrp
, cgroup_base_files
, false);
4689 cgroup_d_remove_dir(d
);
4692 * Unregister events and notify userspace.
4693 * Notify userspace about cgroup removing only after rmdir of cgroup
4694 * directory to avoid race between userspace and kernelspace.
4696 spin_lock(&cgrp
->event_list_lock
);
4697 list_for_each_entry_safe(event
, tmp
, &cgrp
->event_list
, list
) {
4698 list_del_init(&event
->list
);
4699 schedule_work(&event
->remove
);
4701 spin_unlock(&cgrp
->event_list_lock
);
4707 * cgroup_destroy_css_killed - the second step of cgroup destruction
4708 * @work: cgroup->destroy_free_work
4710 * This function is invoked from a work item for a cgroup which is being
4711 * destroyed after all css's are offlined and performs the rest of
4712 * destruction. This is the second step of destruction described in the
4713 * comment above cgroup_destroy_locked().
4715 static void cgroup_destroy_css_killed(struct cgroup
*cgrp
)
4717 struct cgroup
*parent
= cgrp
->parent
;
4718 struct dentry
*d
= cgrp
->dentry
;
4720 lockdep_assert_held(&cgroup_mutex
);
4722 /* delete this cgroup from parent->children */
4723 list_del_rcu(&cgrp
->sibling
);
4726 * We should remove the cgroup object from idr before its grace
4727 * period starts, so we won't be looking up a cgroup while the
4728 * cgroup is being freed.
4730 idr_remove(&cgrp
->root
->cgroup_idr
, cgrp
->id
);
4735 set_bit(CGRP_RELEASABLE
, &parent
->flags
);
4736 check_for_release(parent
);
4739 static int cgroup_rmdir(struct inode
*unused_dir
, struct dentry
*dentry
)
4743 mutex_lock(&cgroup_mutex
);
4744 ret
= cgroup_destroy_locked(dentry
->d_fsdata
);
4745 mutex_unlock(&cgroup_mutex
);
4750 static void __init_or_module
cgroup_init_cftsets(struct cgroup_subsys
*ss
)
4752 INIT_LIST_HEAD(&ss
->cftsets
);
4755 * base_cftset is embedded in subsys itself, no need to worry about
4758 if (ss
->base_cftypes
) {
4761 for (cft
= ss
->base_cftypes
; cft
->name
[0] != '\0'; cft
++)
4764 ss
->base_cftset
.cfts
= ss
->base_cftypes
;
4765 list_add_tail(&ss
->base_cftset
.node
, &ss
->cftsets
);
4769 static void __init
cgroup_init_subsys(struct cgroup_subsys
*ss
)
4771 struct cgroup_subsys_state
*css
;
4773 printk(KERN_INFO
"Initializing cgroup subsys %s\n", ss
->name
);
4775 mutex_lock(&cgroup_mutex
);
4777 /* init base cftset */
4778 cgroup_init_cftsets(ss
);
4780 /* Create the top cgroup state for this subsystem */
4781 list_add(&ss
->sibling
, &cgroup_dummy_root
.subsys_list
);
4782 ss
->root
= &cgroup_dummy_root
;
4783 css
= ss
->css_alloc(cgroup_css(cgroup_dummy_top
, ss
));
4784 /* We don't handle early failures gracefully */
4785 BUG_ON(IS_ERR(css
));
4786 init_css(css
, ss
, cgroup_dummy_top
);
4788 /* Update the init_css_set to contain a subsys
4789 * pointer to this state - since the subsystem is
4790 * newly registered, all tasks and hence the
4791 * init_css_set is in the subsystem's top cgroup. */
4792 init_css_set
.subsys
[ss
->subsys_id
] = css
;
4794 need_forkexit_callback
|= ss
->fork
|| ss
->exit
;
4796 /* At system boot, before all subsystems have been
4797 * registered, no tasks have been forked, so we don't
4798 * need to invoke fork callbacks here. */
4799 BUG_ON(!list_empty(&init_task
.tasks
));
4801 BUG_ON(online_css(css
));
4803 mutex_unlock(&cgroup_mutex
);
4805 /* this function shouldn't be used with modular subsystems, since they
4806 * need to register a subsys_id, among other things */
4811 * cgroup_load_subsys: load and register a modular subsystem at runtime
4812 * @ss: the subsystem to load
4814 * This function should be called in a modular subsystem's initcall. If the
4815 * subsystem is built as a module, it will be assigned a new subsys_id and set
4816 * up for use. If the subsystem is built-in anyway, work is delegated to the
4817 * simpler cgroup_init_subsys.
4819 int __init_or_module
cgroup_load_subsys(struct cgroup_subsys
*ss
)
4821 struct cgroup_subsys_state
*css
;
4823 struct hlist_node
*tmp
;
4824 struct css_set
*cset
;
4827 /* check name and function validity */
4828 if (ss
->name
== NULL
|| strlen(ss
->name
) > MAX_CGROUP_TYPE_NAMELEN
||
4829 ss
->css_alloc
== NULL
|| ss
->css_free
== NULL
)
4833 * we don't support callbacks in modular subsystems. this check is
4834 * before the ss->module check for consistency; a subsystem that could
4835 * be a module should still have no callbacks even if the user isn't
4836 * compiling it as one.
4838 if (ss
->fork
|| ss
->exit
)
4842 * an optionally modular subsystem is built-in: we want to do nothing,
4843 * since cgroup_init_subsys will have already taken care of it.
4845 if (ss
->module
== NULL
) {
4846 /* a sanity check */
4847 BUG_ON(cgroup_subsys
[ss
->subsys_id
] != ss
);
4851 /* init base cftset */
4852 cgroup_init_cftsets(ss
);
4854 mutex_lock(&cgroup_mutex
);
4855 cgroup_subsys
[ss
->subsys_id
] = ss
;
4858 * no ss->css_alloc seems to need anything important in the ss
4859 * struct, so this can happen first (i.e. before the dummy root
4862 css
= ss
->css_alloc(cgroup_css(cgroup_dummy_top
, ss
));
4864 /* failure case - need to deassign the cgroup_subsys[] slot. */
4865 cgroup_subsys
[ss
->subsys_id
] = NULL
;
4866 mutex_unlock(&cgroup_mutex
);
4867 return PTR_ERR(css
);
4870 list_add(&ss
->sibling
, &cgroup_dummy_root
.subsys_list
);
4871 ss
->root
= &cgroup_dummy_root
;
4873 /* our new subsystem will be attached to the dummy hierarchy. */
4874 init_css(css
, ss
, cgroup_dummy_top
);
4877 * Now we need to entangle the css into the existing css_sets. unlike
4878 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4879 * will need a new pointer to it; done by iterating the css_set_table.
4880 * furthermore, modifying the existing css_sets will corrupt the hash
4881 * table state, so each changed css_set will need its hash recomputed.
4882 * this is all done under the css_set_lock.
4884 write_lock(&css_set_lock
);
4885 hash_for_each_safe(css_set_table
, i
, tmp
, cset
, hlist
) {
4886 /* skip entries that we already rehashed */
4887 if (cset
->subsys
[ss
->subsys_id
])
4889 /* remove existing entry */
4890 hash_del(&cset
->hlist
);
4892 cset
->subsys
[ss
->subsys_id
] = css
;
4893 /* recompute hash and restore entry */
4894 key
= css_set_hash(cset
->subsys
);
4895 hash_add(css_set_table
, &cset
->hlist
, key
);
4897 write_unlock(&css_set_lock
);
4899 ret
= online_css(css
);
4904 mutex_unlock(&cgroup_mutex
);
4908 mutex_unlock(&cgroup_mutex
);
4909 /* @ss can't be mounted here as try_module_get() would fail */
4910 cgroup_unload_subsys(ss
);
4913 EXPORT_SYMBOL_GPL(cgroup_load_subsys
);
4916 * cgroup_unload_subsys: unload a modular subsystem
4917 * @ss: the subsystem to unload
4919 * This function should be called in a modular subsystem's exitcall. When this
4920 * function is invoked, the refcount on the subsystem's module will be 0, so
4921 * the subsystem will not be attached to any hierarchy.
4923 void cgroup_unload_subsys(struct cgroup_subsys
*ss
)
4925 struct cgrp_cset_link
*link
;
4927 BUG_ON(ss
->module
== NULL
);
4930 * we shouldn't be called if the subsystem is in use, and the use of
4931 * try_module_get() in rebind_subsystems() should ensure that it
4932 * doesn't start being used while we're killing it off.
4934 BUG_ON(ss
->root
!= &cgroup_dummy_root
);
4936 mutex_lock(&cgroup_mutex
);
4938 offline_css(cgroup_css(cgroup_dummy_top
, ss
));
4940 /* deassign the subsys_id */
4941 cgroup_subsys
[ss
->subsys_id
] = NULL
;
4943 /* remove subsystem from the dummy root's list of subsystems */
4944 list_del_init(&ss
->sibling
);
4947 * disentangle the css from all css_sets attached to the dummy
4948 * top. as in loading, we need to pay our respects to the hashtable
4951 write_lock(&css_set_lock
);
4952 list_for_each_entry(link
, &cgroup_dummy_top
->cset_links
, cset_link
) {
4953 struct css_set
*cset
= link
->cset
;
4956 hash_del(&cset
->hlist
);
4957 cset
->subsys
[ss
->subsys_id
] = NULL
;
4958 key
= css_set_hash(cset
->subsys
);
4959 hash_add(css_set_table
, &cset
->hlist
, key
);
4961 write_unlock(&css_set_lock
);
4964 * remove subsystem's css from the cgroup_dummy_top and free it -
4965 * need to free before marking as null because ss->css_free needs
4966 * the cgrp->subsys pointer to find their state.
4968 ss
->css_free(cgroup_css(cgroup_dummy_top
, ss
));
4969 RCU_INIT_POINTER(cgroup_dummy_top
->subsys
[ss
->subsys_id
], NULL
);
4971 mutex_unlock(&cgroup_mutex
);
4973 EXPORT_SYMBOL_GPL(cgroup_unload_subsys
);
4976 * cgroup_init_early - cgroup initialization at system boot
4978 * Initialize cgroups at system boot, and initialize any
4979 * subsystems that request early init.
4981 int __init
cgroup_init_early(void)
4983 struct cgroup_subsys
*ss
;
4986 atomic_set(&init_css_set
.refcount
, 1);
4987 INIT_LIST_HEAD(&init_css_set
.cgrp_links
);
4988 INIT_LIST_HEAD(&init_css_set
.tasks
);
4989 INIT_HLIST_NODE(&init_css_set
.hlist
);
4991 init_cgroup_root(&cgroup_dummy_root
);
4992 cgroup_root_count
= 1;
4993 RCU_INIT_POINTER(init_task
.cgroups
, &init_css_set
);
4995 init_cgrp_cset_link
.cset
= &init_css_set
;
4996 init_cgrp_cset_link
.cgrp
= cgroup_dummy_top
;
4997 list_add(&init_cgrp_cset_link
.cset_link
, &cgroup_dummy_top
->cset_links
);
4998 list_add(&init_cgrp_cset_link
.cgrp_link
, &init_css_set
.cgrp_links
);
5000 /* at bootup time, we don't worry about modular subsystems */
5001 for_each_builtin_subsys(ss
, i
) {
5003 BUG_ON(strlen(ss
->name
) > MAX_CGROUP_TYPE_NAMELEN
);
5004 BUG_ON(!ss
->css_alloc
);
5005 BUG_ON(!ss
->css_free
);
5006 if (ss
->subsys_id
!= i
) {
5007 printk(KERN_ERR
"cgroup: Subsys %s id == %d\n",
5008 ss
->name
, ss
->subsys_id
);
5013 cgroup_init_subsys(ss
);
5019 * cgroup_init - cgroup initialization
5021 * Register cgroup filesystem and /proc file, and initialize
5022 * any subsystems that didn't request early init.
5024 int __init
cgroup_init(void)
5026 struct cgroup_subsys
*ss
;
5030 err
= bdi_init(&cgroup_backing_dev_info
);
5034 for_each_builtin_subsys(ss
, i
) {
5035 if (!ss
->early_init
)
5036 cgroup_init_subsys(ss
);
5039 /* allocate id for the dummy hierarchy */
5040 mutex_lock(&cgroup_mutex
);
5041 mutex_lock(&cgroup_root_mutex
);
5043 /* Add init_css_set to the hash table */
5044 key
= css_set_hash(init_css_set
.subsys
);
5045 hash_add(css_set_table
, &init_css_set
.hlist
, key
);
5047 BUG_ON(cgroup_init_root_id(&cgroup_dummy_root
, 0, 1));
5049 err
= idr_alloc(&cgroup_dummy_root
.cgroup_idr
, cgroup_dummy_top
,
5053 mutex_unlock(&cgroup_root_mutex
);
5054 mutex_unlock(&cgroup_mutex
);
5056 cgroup_kobj
= kobject_create_and_add("cgroup", fs_kobj
);
5062 err
= register_filesystem(&cgroup_fs_type
);
5064 kobject_put(cgroup_kobj
);
5068 proc_create("cgroups", 0, NULL
, &proc_cgroupstats_operations
);
5072 bdi_destroy(&cgroup_backing_dev_info
);
5077 static int __init
cgroup_wq_init(void)
5080 * There isn't much point in executing destruction path in
5081 * parallel. Good chunk is serialized with cgroup_mutex anyway.
5082 * Use 1 for @max_active.
5084 * We would prefer to do this in cgroup_init() above, but that
5085 * is called before init_workqueues(): so leave this until after.
5087 cgroup_destroy_wq
= alloc_workqueue("cgroup_destroy", 0, 1);
5088 BUG_ON(!cgroup_destroy_wq
);
5091 core_initcall(cgroup_wq_init
);
5094 * proc_cgroup_show()
5095 * - Print task's cgroup paths into seq_file, one line for each hierarchy
5096 * - Used for /proc/<pid>/cgroup.
5097 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
5098 * doesn't really matter if tsk->cgroup changes after we read it,
5099 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
5100 * anyway. No need to check that tsk->cgroup != NULL, thanks to
5101 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
5102 * cgroup to top_cgroup.
5105 /* TODO: Use a proper seq_file iterator */
5106 int proc_cgroup_show(struct seq_file
*m
, void *v
)
5109 struct task_struct
*tsk
;
5112 struct cgroupfs_root
*root
;
5115 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
5121 tsk
= get_pid_task(pid
, PIDTYPE_PID
);
5127 mutex_lock(&cgroup_mutex
);
5129 for_each_active_root(root
) {
5130 struct cgroup_subsys
*ss
;
5131 struct cgroup
*cgrp
;
5134 seq_printf(m
, "%d:", root
->hierarchy_id
);
5135 for_each_root_subsys(root
, ss
)
5136 seq_printf(m
, "%s%s", count
++ ? "," : "", ss
->name
);
5137 if (strlen(root
->name
))
5138 seq_printf(m
, "%sname=%s", count
? "," : "",
5141 cgrp
= task_cgroup_from_root(tsk
, root
);
5142 retval
= cgroup_path(cgrp
, buf
, PAGE_SIZE
);
5150 mutex_unlock(&cgroup_mutex
);
5151 put_task_struct(tsk
);
5158 /* Display information about each subsystem and each hierarchy */
5159 static int proc_cgroupstats_show(struct seq_file
*m
, void *v
)
5161 struct cgroup_subsys
*ss
;
5164 seq_puts(m
, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
5166 * ideally we don't want subsystems moving around while we do this.
5167 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
5168 * subsys/hierarchy state.
5170 mutex_lock(&cgroup_mutex
);
5172 for_each_subsys(ss
, i
)
5173 seq_printf(m
, "%s\t%d\t%d\t%d\n",
5174 ss
->name
, ss
->root
->hierarchy_id
,
5175 ss
->root
->number_of_cgroups
, !ss
->disabled
);
5177 mutex_unlock(&cgroup_mutex
);
5181 static int cgroupstats_open(struct inode
*inode
, struct file
*file
)
5183 return single_open(file
, proc_cgroupstats_show
, NULL
);
5186 static const struct file_operations proc_cgroupstats_operations
= {
5187 .open
= cgroupstats_open
,
5189 .llseek
= seq_lseek
,
5190 .release
= single_release
,
5194 * cgroup_fork - attach newly forked task to its parents cgroup.
5195 * @child: pointer to task_struct of forking parent process.
5197 * Description: A task inherits its parent's cgroup at fork().
5199 * A pointer to the shared css_set was automatically copied in
5200 * fork.c by dup_task_struct(). However, we ignore that copy, since
5201 * it was not made under the protection of RCU or cgroup_mutex, so
5202 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
5203 * have already changed current->cgroups, allowing the previously
5204 * referenced cgroup group to be removed and freed.
5206 * At the point that cgroup_fork() is called, 'current' is the parent
5207 * task, and the passed argument 'child' points to the child task.
5209 void cgroup_fork(struct task_struct
*child
)
5212 get_css_set(task_css_set(current
));
5213 child
->cgroups
= current
->cgroups
;
5214 task_unlock(current
);
5215 INIT_LIST_HEAD(&child
->cg_list
);
5219 * cgroup_post_fork - called on a new task after adding it to the task list
5220 * @child: the task in question
5222 * Adds the task to the list running through its css_set if necessary and
5223 * call the subsystem fork() callbacks. Has to be after the task is
5224 * visible on the task list in case we race with the first call to
5225 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
5228 void cgroup_post_fork(struct task_struct
*child
)
5230 struct cgroup_subsys
*ss
;
5234 * use_task_css_set_links is set to 1 before we walk the tasklist
5235 * under the tasklist_lock and we read it here after we added the child
5236 * to the tasklist under the tasklist_lock as well. If the child wasn't
5237 * yet in the tasklist when we walked through it from
5238 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
5239 * should be visible now due to the paired locking and barriers implied
5240 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
5241 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
5244 if (use_task_css_set_links
) {
5245 write_lock(&css_set_lock
);
5247 if (list_empty(&child
->cg_list
))
5248 list_add(&child
->cg_list
, &task_css_set(child
)->tasks
);
5250 write_unlock(&css_set_lock
);
5254 * Call ss->fork(). This must happen after @child is linked on
5255 * css_set; otherwise, @child might change state between ->fork()
5256 * and addition to css_set.
5258 if (need_forkexit_callback
) {
5260 * fork/exit callbacks are supported only for builtin
5261 * subsystems, and the builtin section of the subsys
5262 * array is immutable, so we don't need to lock the
5263 * subsys array here. On the other hand, modular section
5264 * of the array can be freed at module unload, so we
5267 for_each_builtin_subsys(ss
, i
)
5274 * cgroup_exit - detach cgroup from exiting task
5275 * @tsk: pointer to task_struct of exiting process
5276 * @run_callback: run exit callbacks?
5278 * Description: Detach cgroup from @tsk and release it.
5280 * Note that cgroups marked notify_on_release force every task in
5281 * them to take the global cgroup_mutex mutex when exiting.
5282 * This could impact scaling on very large systems. Be reluctant to
5283 * use notify_on_release cgroups where very high task exit scaling
5284 * is required on large systems.
5286 * the_top_cgroup_hack:
5288 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
5290 * We call cgroup_exit() while the task is still competent to
5291 * handle notify_on_release(), then leave the task attached to the
5292 * root cgroup in each hierarchy for the remainder of its exit.
5294 * To do this properly, we would increment the reference count on
5295 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
5296 * code we would add a second cgroup function call, to drop that
5297 * reference. This would just create an unnecessary hot spot on
5298 * the top_cgroup reference count, to no avail.
5300 * Normally, holding a reference to a cgroup without bumping its
5301 * count is unsafe. The cgroup could go away, or someone could
5302 * attach us to a different cgroup, decrementing the count on
5303 * the first cgroup that we never incremented. But in this case,
5304 * top_cgroup isn't going away, and either task has PF_EXITING set,
5305 * which wards off any cgroup_attach_task() attempts, or task is a failed
5306 * fork, never visible to cgroup_attach_task.
5308 void cgroup_exit(struct task_struct
*tsk
, int run_callbacks
)
5310 struct cgroup_subsys
*ss
;
5311 struct css_set
*cset
;
5315 * Unlink from the css_set task list if necessary.
5316 * Optimistically check cg_list before taking
5319 if (!list_empty(&tsk
->cg_list
)) {
5320 write_lock(&css_set_lock
);
5321 if (!list_empty(&tsk
->cg_list
))
5322 list_del_init(&tsk
->cg_list
);
5323 write_unlock(&css_set_lock
);
5326 /* Reassign the task to the init_css_set. */
5328 cset
= task_css_set(tsk
);
5329 RCU_INIT_POINTER(tsk
->cgroups
, &init_css_set
);
5331 if (run_callbacks
&& need_forkexit_callback
) {
5333 * fork/exit callbacks are supported only for builtin
5334 * subsystems, see cgroup_post_fork() for details.
5336 for_each_builtin_subsys(ss
, i
) {
5338 struct cgroup_subsys_state
*old_css
= cset
->subsys
[i
];
5339 struct cgroup_subsys_state
*css
= task_css(tsk
, i
);
5341 ss
->exit(css
, old_css
, tsk
);
5347 put_css_set_taskexit(cset
);
5350 static void check_for_release(struct cgroup
*cgrp
)
5352 if (cgroup_is_releasable(cgrp
) &&
5353 list_empty(&cgrp
->cset_links
) && list_empty(&cgrp
->children
)) {
5355 * Control Group is currently removeable. If it's not
5356 * already queued for a userspace notification, queue
5359 int need_schedule_work
= 0;
5361 raw_spin_lock(&release_list_lock
);
5362 if (!cgroup_is_dead(cgrp
) &&
5363 list_empty(&cgrp
->release_list
)) {
5364 list_add(&cgrp
->release_list
, &release_list
);
5365 need_schedule_work
= 1;
5367 raw_spin_unlock(&release_list_lock
);
5368 if (need_schedule_work
)
5369 schedule_work(&release_agent_work
);
5374 * Notify userspace when a cgroup is released, by running the
5375 * configured release agent with the name of the cgroup (path
5376 * relative to the root of cgroup file system) as the argument.
5378 * Most likely, this user command will try to rmdir this cgroup.
5380 * This races with the possibility that some other task will be
5381 * attached to this cgroup before it is removed, or that some other
5382 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5383 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5384 * unused, and this cgroup will be reprieved from its death sentence,
5385 * to continue to serve a useful existence. Next time it's released,
5386 * we will get notified again, if it still has 'notify_on_release' set.
5388 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5389 * means only wait until the task is successfully execve()'d. The
5390 * separate release agent task is forked by call_usermodehelper(),
5391 * then control in this thread returns here, without waiting for the
5392 * release agent task. We don't bother to wait because the caller of
5393 * this routine has no use for the exit status of the release agent
5394 * task, so no sense holding our caller up for that.
5396 static void cgroup_release_agent(struct work_struct
*work
)
5398 BUG_ON(work
!= &release_agent_work
);
5399 mutex_lock(&cgroup_mutex
);
5400 raw_spin_lock(&release_list_lock
);
5401 while (!list_empty(&release_list
)) {
5402 char *argv
[3], *envp
[3];
5404 char *pathbuf
= NULL
, *agentbuf
= NULL
;
5405 struct cgroup
*cgrp
= list_entry(release_list
.next
,
5408 list_del_init(&cgrp
->release_list
);
5409 raw_spin_unlock(&release_list_lock
);
5410 pathbuf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
5413 if (cgroup_path(cgrp
, pathbuf
, PAGE_SIZE
) < 0)
5415 agentbuf
= kstrdup(cgrp
->root
->release_agent_path
, GFP_KERNEL
);
5420 argv
[i
++] = agentbuf
;
5421 argv
[i
++] = pathbuf
;
5425 /* minimal command environment */
5426 envp
[i
++] = "HOME=/";
5427 envp
[i
++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5430 /* Drop the lock while we invoke the usermode helper,
5431 * since the exec could involve hitting disk and hence
5432 * be a slow process */
5433 mutex_unlock(&cgroup_mutex
);
5434 call_usermodehelper(argv
[0], argv
, envp
, UMH_WAIT_EXEC
);
5435 mutex_lock(&cgroup_mutex
);
5439 raw_spin_lock(&release_list_lock
);
5441 raw_spin_unlock(&release_list_lock
);
5442 mutex_unlock(&cgroup_mutex
);
5445 static int __init
cgroup_disable(char *str
)
5447 struct cgroup_subsys
*ss
;
5451 while ((token
= strsep(&str
, ",")) != NULL
) {
5456 * cgroup_disable, being at boot time, can't know about
5457 * module subsystems, so we don't worry about them.
5459 for_each_builtin_subsys(ss
, i
) {
5460 if (!strcmp(token
, ss
->name
)) {
5462 printk(KERN_INFO
"Disabling %s control group"
5463 " subsystem\n", ss
->name
);
5470 __setup("cgroup_disable=", cgroup_disable
);
5473 * css_from_dir - get corresponding css from the dentry of a cgroup dir
5474 * @dentry: directory dentry of interest
5475 * @ss: subsystem of interest
5477 * Must be called under RCU read lock. The caller is responsible for
5478 * pinning the returned css if it needs to be accessed outside the RCU
5481 struct cgroup_subsys_state
*css_from_dir(struct dentry
*dentry
,
5482 struct cgroup_subsys
*ss
)
5484 struct cgroup
*cgrp
;
5486 WARN_ON_ONCE(!rcu_read_lock_held());
5488 /* is @dentry a cgroup dir? */
5489 if (!dentry
->d_inode
||
5490 dentry
->d_inode
->i_op
!= &cgroup_dir_inode_operations
)
5491 return ERR_PTR(-EBADF
);
5493 cgrp
= __d_cgrp(dentry
);
5494 return cgroup_css(cgrp
, ss
) ?: ERR_PTR(-ENOENT
);
5498 * css_from_id - lookup css by id
5499 * @id: the cgroup id
5500 * @ss: cgroup subsys to be looked into
5502 * Returns the css if there's valid one with @id, otherwise returns NULL.
5503 * Should be called under rcu_read_lock().
5505 struct cgroup_subsys_state
*css_from_id(int id
, struct cgroup_subsys
*ss
)
5507 struct cgroup
*cgrp
;
5509 rcu_lockdep_assert(rcu_read_lock_held() ||
5510 lockdep_is_held(&cgroup_mutex
),
5511 "css_from_id() needs proper protection");
5513 cgrp
= idr_find(&ss
->root
->cgroup_idr
, id
);
5515 return cgroup_css(cgrp
, ss
);
5519 #ifdef CONFIG_CGROUP_DEBUG
5520 static struct cgroup_subsys_state
*
5521 debug_css_alloc(struct cgroup_subsys_state
*parent_css
)
5523 struct cgroup_subsys_state
*css
= kzalloc(sizeof(*css
), GFP_KERNEL
);
5526 return ERR_PTR(-ENOMEM
);
5531 static void debug_css_free(struct cgroup_subsys_state
*css
)
5536 static u64
debug_taskcount_read(struct cgroup_subsys_state
*css
,
5539 return cgroup_task_count(css
->cgroup
);
5542 static u64
current_css_set_read(struct cgroup_subsys_state
*css
,
5545 return (u64
)(unsigned long)current
->cgroups
;
5548 static u64
current_css_set_refcount_read(struct cgroup_subsys_state
*css
,
5554 count
= atomic_read(&task_css_set(current
)->refcount
);
5559 static int current_css_set_cg_links_read(struct cgroup_subsys_state
*css
,
5561 struct seq_file
*seq
)
5563 struct cgrp_cset_link
*link
;
5564 struct css_set
*cset
;
5566 read_lock(&css_set_lock
);
5568 cset
= rcu_dereference(current
->cgroups
);
5569 list_for_each_entry(link
, &cset
->cgrp_links
, cgrp_link
) {
5570 struct cgroup
*c
= link
->cgrp
;
5574 name
= c
->dentry
->d_name
.name
;
5577 seq_printf(seq
, "Root %d group %s\n",
5578 c
->root
->hierarchy_id
, name
);
5581 read_unlock(&css_set_lock
);
5585 #define MAX_TASKS_SHOWN_PER_CSS 25
5586 static int cgroup_css_links_read(struct cgroup_subsys_state
*css
,
5587 struct cftype
*cft
, struct seq_file
*seq
)
5589 struct cgrp_cset_link
*link
;
5591 read_lock(&css_set_lock
);
5592 list_for_each_entry(link
, &css
->cgroup
->cset_links
, cset_link
) {
5593 struct css_set
*cset
= link
->cset
;
5594 struct task_struct
*task
;
5596 seq_printf(seq
, "css_set %p\n", cset
);
5597 list_for_each_entry(task
, &cset
->tasks
, cg_list
) {
5598 if (count
++ > MAX_TASKS_SHOWN_PER_CSS
) {
5599 seq_puts(seq
, " ...\n");
5602 seq_printf(seq
, " task %d\n",
5603 task_pid_vnr(task
));
5607 read_unlock(&css_set_lock
);
5611 static u64
releasable_read(struct cgroup_subsys_state
*css
, struct cftype
*cft
)
5613 return test_bit(CGRP_RELEASABLE
, &css
->cgroup
->flags
);
5616 static struct cftype debug_files
[] = {
5618 .name
= "taskcount",
5619 .read_u64
= debug_taskcount_read
,
5623 .name
= "current_css_set",
5624 .read_u64
= current_css_set_read
,
5628 .name
= "current_css_set_refcount",
5629 .read_u64
= current_css_set_refcount_read
,
5633 .name
= "current_css_set_cg_links",
5634 .read_seq_string
= current_css_set_cg_links_read
,
5638 .name
= "cgroup_css_links",
5639 .read_seq_string
= cgroup_css_links_read
,
5643 .name
= "releasable",
5644 .read_u64
= releasable_read
,
5650 struct cgroup_subsys debug_subsys
= {
5652 .css_alloc
= debug_css_alloc
,
5653 .css_free
= debug_css_free
,
5654 .subsys_id
= debug_subsys_id
,
5655 .base_cftypes
= debug_files
,
5657 #endif /* CONFIG_CGROUP_DEBUG */