2 * Generic process-grouping system.
4 * Based originally on the cpuset system, extracted by Paul Menage
5 * Copyright (C) 2006 Google, Inc
7 * Notifications support
8 * Copyright (C) 2009 Nokia Corporation
9 * Author: Kirill A. Shutemov
11 * Copyright notices from the original cpuset code:
12 * --------------------------------------------------
13 * Copyright (C) 2003 BULL SA.
14 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
16 * Portions derived from Patrick Mochel's sysfs code.
17 * sysfs is Copyright (c) 2001-3 Patrick Mochel
19 * 2003-10-10 Written by Simon Derr.
20 * 2003-10-22 Updates by Stephen Hemminger.
21 * 2004 May-July Rework by Paul Jackson.
22 * ---------------------------------------------------
24 * This file is subject to the terms and conditions of the GNU General Public
25 * License. See the file COPYING in the main directory of the Linux
26 * distribution for more details.
29 #include <linux/cgroup.h>
30 #include <linux/cred.h>
31 #include <linux/ctype.h>
32 #include <linux/errno.h>
33 #include <linux/init_task.h>
34 #include <linux/kernel.h>
35 #include <linux/list.h>
37 #include <linux/mutex.h>
38 #include <linux/mount.h>
39 #include <linux/pagemap.h>
40 #include <linux/proc_fs.h>
41 #include <linux/rcupdate.h>
42 #include <linux/sched.h>
43 #include <linux/backing-dev.h>
44 #include <linux/seq_file.h>
45 #include <linux/slab.h>
46 #include <linux/magic.h>
47 #include <linux/spinlock.h>
48 #include <linux/string.h>
49 #include <linux/sort.h>
50 #include <linux/kmod.h>
51 #include <linux/module.h>
52 #include <linux/delayacct.h>
53 #include <linux/cgroupstats.h>
54 #include <linux/hashtable.h>
55 #include <linux/namei.h>
56 #include <linux/pid_namespace.h>
57 #include <linux/idr.h>
58 #include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
59 #include <linux/eventfd.h>
60 #include <linux/poll.h>
61 #include <linux/flex_array.h> /* used in cgroup_attach_task */
62 #include <linux/kthread.h>
64 #include <linux/atomic.h>
67 * cgroup_mutex is the master lock. Any modification to cgroup or its
68 * hierarchy must be performed while holding it.
70 * cgroup_root_mutex nests inside cgroup_mutex and should be held to modify
71 * cgroupfs_root of any cgroup hierarchy - subsys list, flags,
72 * release_agent_path and so on. Modifying requires both cgroup_mutex and
73 * cgroup_root_mutex. Readers can acquire either of the two. This is to
74 * break the following locking order cycle.
76 * A. cgroup_mutex -> cred_guard_mutex -> s_type->i_mutex_key -> namespace_sem
77 * B. namespace_sem -> cgroup_mutex
79 * B happens only through cgroup_show_options() and using cgroup_root_mutex
82 #ifdef CONFIG_PROVE_RCU
83 DEFINE_MUTEX(cgroup_mutex
);
84 EXPORT_SYMBOL_GPL(cgroup_mutex
); /* only for lockdep */
86 static DEFINE_MUTEX(cgroup_mutex
);
89 static DEFINE_MUTEX(cgroup_root_mutex
);
92 * Generate an array of cgroup subsystem pointers. At boot time, this is
93 * populated with the built in subsystems, and modular subsystems are
94 * registered after that. The mutable section of this array is protected by
97 #define SUBSYS(_x) [_x ## _subsys_id] = &_x ## _subsys,
98 #define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option)
99 static struct cgroup_subsys
*cgroup_subsys
[CGROUP_SUBSYS_COUNT
] = {
100 #include <linux/cgroup_subsys.h>
104 * The dummy hierarchy, reserved for the subsystems that are otherwise
105 * unattached - it never has more than a single cgroup, and all tasks are
106 * part of that cgroup.
108 static struct cgroupfs_root cgroup_dummy_root
;
110 /* dummy_top is a shorthand for the dummy hierarchy's top cgroup */
111 static struct cgroup
* const cgroup_dummy_top
= &cgroup_dummy_root
.top_cgroup
;
114 * cgroupfs file entry, pointed to from leaf dentry->d_fsdata.
117 struct list_head node
;
118 struct dentry
*dentry
;
122 struct simple_xattrs xattrs
;
126 * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when
127 * cgroup_subsys->use_id != 0.
129 #define CSS_ID_MAX (65535)
132 * The css to which this ID points. This pointer is set to valid value
133 * after cgroup is populated. If cgroup is removed, this will be NULL.
134 * This pointer is expected to be RCU-safe because destroy()
135 * is called after synchronize_rcu(). But for safe use, css_tryget()
136 * should be used for avoiding race.
138 struct cgroup_subsys_state __rcu
*css
;
144 * Depth in hierarchy which this ID belongs to.
146 unsigned short depth
;
148 * ID is freed by RCU. (and lookup routine is RCU safe.)
150 struct rcu_head rcu_head
;
152 * Hierarchy of CSS ID belongs to.
154 unsigned short stack
[0]; /* Array of Length (depth+1) */
158 * cgroup_event represents events which userspace want to receive.
160 struct cgroup_event
{
162 * Cgroup which the event belongs to.
166 * Control file which the event associated.
170 * eventfd to signal userspace about the event.
172 struct eventfd_ctx
*eventfd
;
174 * Each of these stored in a list by the cgroup.
176 struct list_head list
;
178 * All fields below needed to unregister event when
179 * userspace closes eventfd.
182 wait_queue_head_t
*wqh
;
184 struct work_struct remove
;
187 /* The list of hierarchy roots */
189 static LIST_HEAD(cgroup_roots
);
190 static int cgroup_root_count
;
193 * Hierarchy ID allocation and mapping. It follows the same exclusion
194 * rules as other root ops - both cgroup_mutex and cgroup_root_mutex for
195 * writes, either for reads.
197 static DEFINE_IDR(cgroup_hierarchy_idr
);
199 static struct cgroup_name root_cgroup_name
= { .name
= "/" };
202 * Assign a monotonically increasing serial number to cgroups. It
203 * guarantees cgroups with bigger numbers are newer than those with smaller
204 * numbers. Also, as cgroups are always appended to the parent's
205 * ->children list, it guarantees that sibling cgroups are always sorted in
206 * the ascending serial number order on the list. Protected by
209 static u64 cgroup_serial_nr_next
= 1;
211 /* This flag indicates whether tasks in the fork and exit paths should
212 * check for fork/exit handlers to call. This avoids us having to do
213 * extra work in the fork/exit path if none of the subsystems need to
216 static int need_forkexit_callback __read_mostly
;
218 static struct cftype cgroup_base_files
[];
220 static void cgroup_offline_fn(struct work_struct
*work
);
221 static int cgroup_destroy_locked(struct cgroup
*cgrp
);
222 static int cgroup_addrm_files(struct cgroup
*cgrp
, struct cftype cfts
[],
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
;
367 static int cgroup_init_idr(struct cgroup_subsys
*ss
,
368 struct cgroup_subsys_state
*css
);
371 * css_set_lock protects the list of css_set objects, and the chain of
372 * tasks off each css_set. Nests outside task->alloc_lock due to
373 * css_task_iter_start().
375 static DEFINE_RWLOCK(css_set_lock
);
376 static int css_set_count
;
379 * hash table for cgroup groups. This improves the performance to find
380 * an existing css_set. This hash doesn't (currently) take into
381 * account cgroups in empty hierarchies.
383 #define CSS_SET_HASH_BITS 7
384 static DEFINE_HASHTABLE(css_set_table
, CSS_SET_HASH_BITS
);
386 static unsigned long css_set_hash(struct cgroup_subsys_state
*css
[])
388 unsigned long key
= 0UL;
389 struct cgroup_subsys
*ss
;
392 for_each_subsys(ss
, i
)
393 key
+= (unsigned long)css
[i
];
394 key
= (key
>> 16) ^ key
;
400 * We don't maintain the lists running through each css_set to its task
401 * until after the first call to css_task_iter_start(). This reduces the
402 * fork()/exit() overhead for people who have cgroups compiled into their
403 * kernel but not actually in use.
405 static int use_task_css_set_links __read_mostly
;
407 static void __put_css_set(struct css_set
*cset
, int taskexit
)
409 struct cgrp_cset_link
*link
, *tmp_link
;
412 * Ensure that the refcount doesn't hit zero while any readers
413 * can see it. Similar to atomic_dec_and_lock(), but for an
416 if (atomic_add_unless(&cset
->refcount
, -1, 1))
418 write_lock(&css_set_lock
);
419 if (!atomic_dec_and_test(&cset
->refcount
)) {
420 write_unlock(&css_set_lock
);
424 /* This css_set is dead. unlink it and release cgroup refcounts */
425 hash_del(&cset
->hlist
);
428 list_for_each_entry_safe(link
, tmp_link
, &cset
->cgrp_links
, cgrp_link
) {
429 struct cgroup
*cgrp
= link
->cgrp
;
431 list_del(&link
->cset_link
);
432 list_del(&link
->cgrp_link
);
434 /* @cgrp can't go away while we're holding css_set_lock */
435 if (list_empty(&cgrp
->cset_links
) && notify_on_release(cgrp
)) {
437 set_bit(CGRP_RELEASABLE
, &cgrp
->flags
);
438 check_for_release(cgrp
);
444 write_unlock(&css_set_lock
);
445 kfree_rcu(cset
, rcu_head
);
449 * refcounted get/put for css_set objects
451 static inline void get_css_set(struct css_set
*cset
)
453 atomic_inc(&cset
->refcount
);
456 static inline void put_css_set(struct css_set
*cset
)
458 __put_css_set(cset
, 0);
461 static inline void put_css_set_taskexit(struct css_set
*cset
)
463 __put_css_set(cset
, 1);
467 * compare_css_sets - helper function for find_existing_css_set().
468 * @cset: candidate css_set being tested
469 * @old_cset: existing css_set for a task
470 * @new_cgrp: cgroup that's being entered by the task
471 * @template: desired set of css pointers in css_set (pre-calculated)
473 * Returns true if "cset" matches "old_cset" except for the hierarchy
474 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
476 static bool compare_css_sets(struct css_set
*cset
,
477 struct css_set
*old_cset
,
478 struct cgroup
*new_cgrp
,
479 struct cgroup_subsys_state
*template[])
481 struct list_head
*l1
, *l2
;
483 if (memcmp(template, cset
->subsys
, sizeof(cset
->subsys
))) {
484 /* Not all subsystems matched */
489 * Compare cgroup pointers in order to distinguish between
490 * different cgroups in heirarchies with no subsystems. We
491 * could get by with just this check alone (and skip the
492 * memcmp above) but on most setups the memcmp check will
493 * avoid the need for this more expensive check on almost all
497 l1
= &cset
->cgrp_links
;
498 l2
= &old_cset
->cgrp_links
;
500 struct cgrp_cset_link
*link1
, *link2
;
501 struct cgroup
*cgrp1
, *cgrp2
;
505 /* See if we reached the end - both lists are equal length. */
506 if (l1
== &cset
->cgrp_links
) {
507 BUG_ON(l2
!= &old_cset
->cgrp_links
);
510 BUG_ON(l2
== &old_cset
->cgrp_links
);
512 /* Locate the cgroups associated with these links. */
513 link1
= list_entry(l1
, struct cgrp_cset_link
, cgrp_link
);
514 link2
= list_entry(l2
, struct cgrp_cset_link
, cgrp_link
);
517 /* Hierarchies should be linked in the same order. */
518 BUG_ON(cgrp1
->root
!= cgrp2
->root
);
521 * If this hierarchy is the hierarchy of the cgroup
522 * that's changing, then we need to check that this
523 * css_set points to the new cgroup; if it's any other
524 * hierarchy, then this css_set should point to the
525 * same cgroup as the old css_set.
527 if (cgrp1
->root
== new_cgrp
->root
) {
528 if (cgrp1
!= new_cgrp
)
539 * find_existing_css_set - init css array and find the matching css_set
540 * @old_cset: the css_set that we're using before the cgroup transition
541 * @cgrp: the cgroup that we're moving into
542 * @template: out param for the new set of csses, should be clear on entry
544 static struct css_set
*find_existing_css_set(struct css_set
*old_cset
,
546 struct cgroup_subsys_state
*template[])
548 struct cgroupfs_root
*root
= cgrp
->root
;
549 struct cgroup_subsys
*ss
;
550 struct css_set
*cset
;
555 * Build the set of subsystem state objects that we want to see in the
556 * new css_set. while subsystems can change globally, the entries here
557 * won't change, so no need for locking.
559 for_each_subsys(ss
, i
) {
560 if (root
->subsys_mask
& (1UL << i
)) {
561 /* Subsystem is in this hierarchy. So we want
562 * the subsystem state from the new
564 template[i
] = cgrp
->subsys
[i
];
566 /* Subsystem is not in this hierarchy, so we
567 * don't want to change the subsystem state */
568 template[i
] = old_cset
->subsys
[i
];
572 key
= css_set_hash(template);
573 hash_for_each_possible(css_set_table
, cset
, hlist
, key
) {
574 if (!compare_css_sets(cset
, old_cset
, cgrp
, template))
577 /* This css_set matches what we need */
581 /* No existing cgroup group matched */
585 static void free_cgrp_cset_links(struct list_head
*links_to_free
)
587 struct cgrp_cset_link
*link
, *tmp_link
;
589 list_for_each_entry_safe(link
, tmp_link
, links_to_free
, cset_link
) {
590 list_del(&link
->cset_link
);
596 * allocate_cgrp_cset_links - allocate cgrp_cset_links
597 * @count: the number of links to allocate
598 * @tmp_links: list_head the allocated links are put on
600 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
601 * through ->cset_link. Returns 0 on success or -errno.
603 static int allocate_cgrp_cset_links(int count
, struct list_head
*tmp_links
)
605 struct cgrp_cset_link
*link
;
608 INIT_LIST_HEAD(tmp_links
);
610 for (i
= 0; i
< count
; i
++) {
611 link
= kzalloc(sizeof(*link
), GFP_KERNEL
);
613 free_cgrp_cset_links(tmp_links
);
616 list_add(&link
->cset_link
, tmp_links
);
622 * link_css_set - a helper function to link a css_set to a cgroup
623 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
624 * @cset: the css_set to be linked
625 * @cgrp: the destination cgroup
627 static void link_css_set(struct list_head
*tmp_links
, struct css_set
*cset
,
630 struct cgrp_cset_link
*link
;
632 BUG_ON(list_empty(tmp_links
));
633 link
= list_first_entry(tmp_links
, struct cgrp_cset_link
, cset_link
);
636 list_move(&link
->cset_link
, &cgrp
->cset_links
);
638 * Always add links to the tail of the list so that the list
639 * is sorted by order of hierarchy creation
641 list_add_tail(&link
->cgrp_link
, &cset
->cgrp_links
);
645 * find_css_set - return a new css_set with one cgroup updated
646 * @old_cset: the baseline css_set
647 * @cgrp: the cgroup to be updated
649 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
650 * substituted into the appropriate hierarchy.
652 static struct css_set
*find_css_set(struct css_set
*old_cset
,
655 struct cgroup_subsys_state
*template[CGROUP_SUBSYS_COUNT
] = { };
656 struct css_set
*cset
;
657 struct list_head tmp_links
;
658 struct cgrp_cset_link
*link
;
661 lockdep_assert_held(&cgroup_mutex
);
663 /* First see if we already have a cgroup group that matches
665 read_lock(&css_set_lock
);
666 cset
= find_existing_css_set(old_cset
, cgrp
, template);
669 read_unlock(&css_set_lock
);
674 cset
= kzalloc(sizeof(*cset
), GFP_KERNEL
);
678 /* Allocate all the cgrp_cset_link objects that we'll need */
679 if (allocate_cgrp_cset_links(cgroup_root_count
, &tmp_links
) < 0) {
684 atomic_set(&cset
->refcount
, 1);
685 INIT_LIST_HEAD(&cset
->cgrp_links
);
686 INIT_LIST_HEAD(&cset
->tasks
);
687 INIT_HLIST_NODE(&cset
->hlist
);
689 /* Copy the set of subsystem state objects generated in
690 * find_existing_css_set() */
691 memcpy(cset
->subsys
, template, sizeof(cset
->subsys
));
693 write_lock(&css_set_lock
);
694 /* Add reference counts and links from the new css_set. */
695 list_for_each_entry(link
, &old_cset
->cgrp_links
, cgrp_link
) {
696 struct cgroup
*c
= link
->cgrp
;
698 if (c
->root
== cgrp
->root
)
700 link_css_set(&tmp_links
, cset
, c
);
703 BUG_ON(!list_empty(&tmp_links
));
707 /* Add this cgroup group to the hash table */
708 key
= css_set_hash(cset
->subsys
);
709 hash_add(css_set_table
, &cset
->hlist
, key
);
711 write_unlock(&css_set_lock
);
717 * Return the cgroup for "task" from the given hierarchy. Must be
718 * called with cgroup_mutex held.
720 static struct cgroup
*task_cgroup_from_root(struct task_struct
*task
,
721 struct cgroupfs_root
*root
)
723 struct css_set
*cset
;
724 struct cgroup
*res
= NULL
;
726 BUG_ON(!mutex_is_locked(&cgroup_mutex
));
727 read_lock(&css_set_lock
);
729 * No need to lock the task - since we hold cgroup_mutex the
730 * task can't change groups, so the only thing that can happen
731 * is that it exits and its css is set back to init_css_set.
733 cset
= task_css_set(task
);
734 if (cset
== &init_css_set
) {
735 res
= &root
->top_cgroup
;
737 struct cgrp_cset_link
*link
;
739 list_for_each_entry(link
, &cset
->cgrp_links
, cgrp_link
) {
740 struct cgroup
*c
= link
->cgrp
;
742 if (c
->root
== root
) {
748 read_unlock(&css_set_lock
);
754 * There is one global cgroup mutex. We also require taking
755 * task_lock() when dereferencing a task's cgroup subsys pointers.
756 * See "The task_lock() exception", at the end of this comment.
758 * A task must hold cgroup_mutex to modify cgroups.
760 * Any task can increment and decrement the count field without lock.
761 * So in general, code holding cgroup_mutex can't rely on the count
762 * field not changing. However, if the count goes to zero, then only
763 * cgroup_attach_task() can increment it again. Because a count of zero
764 * means that no tasks are currently attached, therefore there is no
765 * way a task attached to that cgroup can fork (the other way to
766 * increment the count). So code holding cgroup_mutex can safely
767 * assume that if the count is zero, it will stay zero. Similarly, if
768 * a task holds cgroup_mutex on a cgroup with zero count, it
769 * knows that the cgroup won't be removed, as cgroup_rmdir()
772 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
773 * (usually) take cgroup_mutex. These are the two most performance
774 * critical pieces of code here. The exception occurs on cgroup_exit(),
775 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
776 * is taken, and if the cgroup count is zero, a usermode call made
777 * to the release agent with the name of the cgroup (path relative to
778 * the root of cgroup file system) as the argument.
780 * A cgroup can only be deleted if both its 'count' of using tasks
781 * is zero, and its list of 'children' cgroups is empty. Since all
782 * tasks in the system use _some_ cgroup, and since there is always at
783 * least one task in the system (init, pid == 1), therefore, top_cgroup
784 * always has either children cgroups and/or using tasks. So we don't
785 * need a special hack to ensure that top_cgroup cannot be deleted.
787 * The task_lock() exception
789 * The need for this exception arises from the action of
790 * cgroup_attach_task(), which overwrites one task's cgroup pointer with
791 * another. It does so using cgroup_mutex, however there are
792 * several performance critical places that need to reference
793 * task->cgroup without the expense of grabbing a system global
794 * mutex. Therefore except as noted below, when dereferencing or, as
795 * in cgroup_attach_task(), modifying a task's cgroup pointer we use
796 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
797 * the task_struct routinely used for such matters.
799 * P.S. One more locking exception. RCU is used to guard the
800 * update of a tasks cgroup pointer by cgroup_attach_task()
804 * A couple of forward declarations required, due to cyclic reference loop:
805 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
806 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
810 static int cgroup_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
);
811 static struct dentry
*cgroup_lookup(struct inode
*, struct dentry
*, unsigned int);
812 static int cgroup_rmdir(struct inode
*unused_dir
, struct dentry
*dentry
);
813 static int cgroup_populate_dir(struct cgroup
*cgrp
, unsigned long subsys_mask
);
814 static const struct inode_operations cgroup_dir_inode_operations
;
815 static const struct file_operations proc_cgroupstats_operations
;
817 static struct backing_dev_info cgroup_backing_dev_info
= {
819 .capabilities
= BDI_CAP_NO_ACCT_AND_WRITEBACK
,
822 static int alloc_css_id(struct cgroup_subsys
*ss
,
823 struct cgroup
*parent
, struct cgroup
*child
);
825 static struct inode
*cgroup_new_inode(umode_t mode
, struct super_block
*sb
)
827 struct inode
*inode
= new_inode(sb
);
830 inode
->i_ino
= get_next_ino();
831 inode
->i_mode
= mode
;
832 inode
->i_uid
= current_fsuid();
833 inode
->i_gid
= current_fsgid();
834 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
835 inode
->i_mapping
->backing_dev_info
= &cgroup_backing_dev_info
;
840 static struct cgroup_name
*cgroup_alloc_name(struct dentry
*dentry
)
842 struct cgroup_name
*name
;
844 name
= kmalloc(sizeof(*name
) + dentry
->d_name
.len
+ 1, GFP_KERNEL
);
847 strcpy(name
->name
, dentry
->d_name
.name
);
851 static void cgroup_free_fn(struct work_struct
*work
)
853 struct cgroup
*cgrp
= container_of(work
, struct cgroup
, destroy_work
);
854 struct cgroup_subsys
*ss
;
856 mutex_lock(&cgroup_mutex
);
858 * Release the subsystem state objects.
860 for_each_root_subsys(cgrp
->root
, ss
) {
861 struct cgroup_subsys_state
*css
= cgrp
->subsys
[ss
->subsys_id
];
866 cgrp
->root
->number_of_cgroups
--;
867 mutex_unlock(&cgroup_mutex
);
870 * We get a ref to the parent's dentry, and put the ref when
871 * this cgroup is being freed, so it's guaranteed that the
872 * parent won't be destroyed before its children.
874 dput(cgrp
->parent
->dentry
);
877 * Drop the active superblock reference that we took when we
878 * created the cgroup. This will free cgrp->root, if we are
879 * holding the last reference to @sb.
881 deactivate_super(cgrp
->root
->sb
);
884 * if we're getting rid of the cgroup, refcount should ensure
885 * that there are no pidlists left.
887 BUG_ON(!list_empty(&cgrp
->pidlists
));
889 simple_xattrs_free(&cgrp
->xattrs
);
891 kfree(rcu_dereference_raw(cgrp
->name
));
895 static void cgroup_free_rcu(struct rcu_head
*head
)
897 struct cgroup
*cgrp
= container_of(head
, struct cgroup
, rcu_head
);
899 INIT_WORK(&cgrp
->destroy_work
, cgroup_free_fn
);
900 schedule_work(&cgrp
->destroy_work
);
903 static void cgroup_diput(struct dentry
*dentry
, struct inode
*inode
)
905 /* is dentry a directory ? if so, kfree() associated cgroup */
906 if (S_ISDIR(inode
->i_mode
)) {
907 struct cgroup
*cgrp
= dentry
->d_fsdata
;
909 BUG_ON(!(cgroup_is_dead(cgrp
)));
910 call_rcu(&cgrp
->rcu_head
, cgroup_free_rcu
);
912 struct cfent
*cfe
= __d_cfe(dentry
);
913 struct cgroup
*cgrp
= dentry
->d_parent
->d_fsdata
;
915 WARN_ONCE(!list_empty(&cfe
->node
) &&
916 cgrp
!= &cgrp
->root
->top_cgroup
,
917 "cfe still linked for %s\n", cfe
->type
->name
);
918 simple_xattrs_free(&cfe
->xattrs
);
924 static int cgroup_delete(const struct dentry
*d
)
929 static void remove_dir(struct dentry
*d
)
931 struct dentry
*parent
= dget(d
->d_parent
);
934 simple_rmdir(parent
->d_inode
, d
);
938 static void cgroup_rm_file(struct cgroup
*cgrp
, const struct cftype
*cft
)
942 lockdep_assert_held(&cgrp
->dentry
->d_inode
->i_mutex
);
943 lockdep_assert_held(&cgroup_mutex
);
946 * If we're doing cleanup due to failure of cgroup_create(),
947 * the corresponding @cfe may not exist.
949 list_for_each_entry(cfe
, &cgrp
->files
, node
) {
950 struct dentry
*d
= cfe
->dentry
;
952 if (cft
&& cfe
->type
!= cft
)
957 simple_unlink(cgrp
->dentry
->d_inode
, d
);
958 list_del_init(&cfe
->node
);
966 * cgroup_clear_dir - remove subsys files in a cgroup directory
967 * @cgrp: target cgroup
968 * @subsys_mask: mask of the subsystem ids whose files should be removed
970 static void cgroup_clear_dir(struct cgroup
*cgrp
, unsigned long subsys_mask
)
972 struct cgroup_subsys
*ss
;
975 for_each_subsys(ss
, i
) {
976 struct cftype_set
*set
;
978 if (!test_bit(i
, &subsys_mask
))
980 list_for_each_entry(set
, &ss
->cftsets
, node
)
981 cgroup_addrm_files(cgrp
, set
->cfts
, false);
986 * NOTE : the dentry must have been dget()'ed
988 static void cgroup_d_remove_dir(struct dentry
*dentry
)
990 struct dentry
*parent
;
992 parent
= dentry
->d_parent
;
993 spin_lock(&parent
->d_lock
);
994 spin_lock_nested(&dentry
->d_lock
, DENTRY_D_LOCK_NESTED
);
995 list_del_init(&dentry
->d_u
.d_child
);
996 spin_unlock(&dentry
->d_lock
);
997 spin_unlock(&parent
->d_lock
);
1002 * Call with cgroup_mutex held. Drops reference counts on modules, including
1003 * any duplicate ones that parse_cgroupfs_options took. If this function
1004 * returns an error, no reference counts are touched.
1006 static int rebind_subsystems(struct cgroupfs_root
*root
,
1007 unsigned long added_mask
, unsigned removed_mask
)
1009 struct cgroup
*cgrp
= &root
->top_cgroup
;
1010 struct cgroup_subsys
*ss
;
1011 unsigned long pinned
= 0;
1014 BUG_ON(!mutex_is_locked(&cgroup_mutex
));
1015 BUG_ON(!mutex_is_locked(&cgroup_root_mutex
));
1017 /* Check that any added subsystems are currently free */
1018 for_each_subsys(ss
, i
) {
1019 if (!(added_mask
& (1 << i
)))
1022 /* is the subsystem mounted elsewhere? */
1023 if (ss
->root
!= &cgroup_dummy_root
) {
1028 /* pin the module */
1029 if (!try_module_get(ss
->module
)) {
1036 /* subsys could be missing if unloaded between parsing and here */
1037 if (added_mask
!= pinned
) {
1042 ret
= cgroup_populate_dir(cgrp
, added_mask
);
1047 * Nothing can fail from this point on. Remove files for the
1048 * removed subsystems and rebind each subsystem.
1050 cgroup_clear_dir(cgrp
, removed_mask
);
1052 for_each_subsys(ss
, i
) {
1053 unsigned long bit
= 1UL << i
;
1055 if (bit
& added_mask
) {
1056 /* We're binding this subsystem to this hierarchy */
1057 BUG_ON(cgrp
->subsys
[i
]);
1058 BUG_ON(!cgroup_dummy_top
->subsys
[i
]);
1059 BUG_ON(cgroup_dummy_top
->subsys
[i
]->cgroup
!= cgroup_dummy_top
);
1061 cgrp
->subsys
[i
] = cgroup_dummy_top
->subsys
[i
];
1062 cgrp
->subsys
[i
]->cgroup
= cgrp
;
1063 list_move(&ss
->sibling
, &root
->subsys_list
);
1066 ss
->bind(cgrp
->subsys
[i
]);
1068 /* refcount was already taken, and we're keeping it */
1069 root
->subsys_mask
|= bit
;
1070 } else if (bit
& removed_mask
) {
1071 /* We're removing this subsystem */
1072 BUG_ON(cgrp
->subsys
[i
] != cgroup_dummy_top
->subsys
[i
]);
1073 BUG_ON(cgrp
->subsys
[i
]->cgroup
!= cgrp
);
1076 ss
->bind(cgroup_dummy_top
->subsys
[i
]);
1077 cgroup_dummy_top
->subsys
[i
]->cgroup
= cgroup_dummy_top
;
1078 cgrp
->subsys
[i
] = NULL
;
1079 cgroup_subsys
[i
]->root
= &cgroup_dummy_root
;
1080 list_move(&ss
->sibling
, &cgroup_dummy_root
.subsys_list
);
1082 /* subsystem is now free - drop reference on module */
1083 module_put(ss
->module
);
1084 root
->subsys_mask
&= ~bit
;
1089 * Mark @root has finished binding subsystems. @root->subsys_mask
1090 * now matches the bound subsystems.
1092 root
->flags
|= CGRP_ROOT_SUBSYS_BOUND
;
1097 for_each_subsys(ss
, i
)
1098 if (pinned
& (1 << i
))
1099 module_put(ss
->module
);
1103 static int cgroup_show_options(struct seq_file
*seq
, struct dentry
*dentry
)
1105 struct cgroupfs_root
*root
= dentry
->d_sb
->s_fs_info
;
1106 struct cgroup_subsys
*ss
;
1108 mutex_lock(&cgroup_root_mutex
);
1109 for_each_root_subsys(root
, ss
)
1110 seq_printf(seq
, ",%s", ss
->name
);
1111 if (root
->flags
& CGRP_ROOT_SANE_BEHAVIOR
)
1112 seq_puts(seq
, ",sane_behavior");
1113 if (root
->flags
& CGRP_ROOT_NOPREFIX
)
1114 seq_puts(seq
, ",noprefix");
1115 if (root
->flags
& CGRP_ROOT_XATTR
)
1116 seq_puts(seq
, ",xattr");
1117 if (strlen(root
->release_agent_path
))
1118 seq_printf(seq
, ",release_agent=%s", root
->release_agent_path
);
1119 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN
, &root
->top_cgroup
.flags
))
1120 seq_puts(seq
, ",clone_children");
1121 if (strlen(root
->name
))
1122 seq_printf(seq
, ",name=%s", root
->name
);
1123 mutex_unlock(&cgroup_root_mutex
);
1127 struct cgroup_sb_opts
{
1128 unsigned long subsys_mask
;
1129 unsigned long flags
;
1130 char *release_agent
;
1131 bool cpuset_clone_children
;
1133 /* User explicitly requested empty subsystem */
1136 struct cgroupfs_root
*new_root
;
1141 * Convert a hierarchy specifier into a bitmask of subsystems and
1142 * flags. Call with cgroup_mutex held to protect the cgroup_subsys[]
1143 * array. This function takes refcounts on subsystems to be used, unless it
1144 * returns error, in which case no refcounts are taken.
1146 static int parse_cgroupfs_options(char *data
, struct cgroup_sb_opts
*opts
)
1148 char *token
, *o
= data
;
1149 bool all_ss
= false, one_ss
= false;
1150 unsigned long mask
= (unsigned long)-1;
1151 struct cgroup_subsys
*ss
;
1154 BUG_ON(!mutex_is_locked(&cgroup_mutex
));
1156 #ifdef CONFIG_CPUSETS
1157 mask
= ~(1UL << cpuset_subsys_id
);
1160 memset(opts
, 0, sizeof(*opts
));
1162 while ((token
= strsep(&o
, ",")) != NULL
) {
1165 if (!strcmp(token
, "none")) {
1166 /* Explicitly have no subsystems */
1170 if (!strcmp(token
, "all")) {
1171 /* Mutually exclusive option 'all' + subsystem name */
1177 if (!strcmp(token
, "__DEVEL__sane_behavior")) {
1178 opts
->flags
|= CGRP_ROOT_SANE_BEHAVIOR
;
1181 if (!strcmp(token
, "noprefix")) {
1182 opts
->flags
|= CGRP_ROOT_NOPREFIX
;
1185 if (!strcmp(token
, "clone_children")) {
1186 opts
->cpuset_clone_children
= true;
1189 if (!strcmp(token
, "xattr")) {
1190 opts
->flags
|= CGRP_ROOT_XATTR
;
1193 if (!strncmp(token
, "release_agent=", 14)) {
1194 /* Specifying two release agents is forbidden */
1195 if (opts
->release_agent
)
1197 opts
->release_agent
=
1198 kstrndup(token
+ 14, PATH_MAX
- 1, GFP_KERNEL
);
1199 if (!opts
->release_agent
)
1203 if (!strncmp(token
, "name=", 5)) {
1204 const char *name
= token
+ 5;
1205 /* Can't specify an empty name */
1208 /* Must match [\w.-]+ */
1209 for (i
= 0; i
< strlen(name
); i
++) {
1213 if ((c
== '.') || (c
== '-') || (c
== '_'))
1217 /* Specifying two names is forbidden */
1220 opts
->name
= kstrndup(name
,
1221 MAX_CGROUP_ROOT_NAMELEN
- 1,
1229 for_each_subsys(ss
, i
) {
1230 if (strcmp(token
, ss
->name
))
1235 /* Mutually exclusive option 'all' + subsystem name */
1238 set_bit(i
, &opts
->subsys_mask
);
1243 if (i
== CGROUP_SUBSYS_COUNT
)
1248 * If the 'all' option was specified select all the subsystems,
1249 * otherwise if 'none', 'name=' and a subsystem name options
1250 * were not specified, let's default to 'all'
1252 if (all_ss
|| (!one_ss
&& !opts
->none
&& !opts
->name
))
1253 for_each_subsys(ss
, i
)
1255 set_bit(i
, &opts
->subsys_mask
);
1257 /* Consistency checks */
1259 if (opts
->flags
& CGRP_ROOT_SANE_BEHAVIOR
) {
1260 pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
1262 if (opts
->flags
& CGRP_ROOT_NOPREFIX
) {
1263 pr_err("cgroup: sane_behavior: noprefix is not allowed\n");
1267 if (opts
->cpuset_clone_children
) {
1268 pr_err("cgroup: sane_behavior: clone_children is not allowed\n");
1274 * Option noprefix was introduced just for backward compatibility
1275 * with the old cpuset, so we allow noprefix only if mounting just
1276 * the cpuset subsystem.
1278 if ((opts
->flags
& CGRP_ROOT_NOPREFIX
) && (opts
->subsys_mask
& mask
))
1282 /* Can't specify "none" and some subsystems */
1283 if (opts
->subsys_mask
&& opts
->none
)
1287 * We either have to specify by name or by subsystems. (So all
1288 * empty hierarchies must have a name).
1290 if (!opts
->subsys_mask
&& !opts
->name
)
1296 static int cgroup_remount(struct super_block
*sb
, int *flags
, char *data
)
1299 struct cgroupfs_root
*root
= sb
->s_fs_info
;
1300 struct cgroup
*cgrp
= &root
->top_cgroup
;
1301 struct cgroup_sb_opts opts
;
1302 unsigned long added_mask
, removed_mask
;
1304 if (root
->flags
& CGRP_ROOT_SANE_BEHAVIOR
) {
1305 pr_err("cgroup: sane_behavior: remount is not allowed\n");
1309 mutex_lock(&cgrp
->dentry
->d_inode
->i_mutex
);
1310 mutex_lock(&cgroup_mutex
);
1311 mutex_lock(&cgroup_root_mutex
);
1313 /* See what subsystems are wanted */
1314 ret
= parse_cgroupfs_options(data
, &opts
);
1318 if (opts
.subsys_mask
!= root
->subsys_mask
|| opts
.release_agent
)
1319 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1320 task_tgid_nr(current
), current
->comm
);
1322 added_mask
= opts
.subsys_mask
& ~root
->subsys_mask
;
1323 removed_mask
= root
->subsys_mask
& ~opts
.subsys_mask
;
1325 /* Don't allow flags or name to change at remount */
1326 if (((opts
.flags
^ root
->flags
) & CGRP_ROOT_OPTION_MASK
) ||
1327 (opts
.name
&& strcmp(opts
.name
, root
->name
))) {
1328 pr_err("cgroup: option or name mismatch, new: 0x%lx \"%s\", old: 0x%lx \"%s\"\n",
1329 opts
.flags
& CGRP_ROOT_OPTION_MASK
, opts
.name
?: "",
1330 root
->flags
& CGRP_ROOT_OPTION_MASK
, root
->name
);
1335 /* remounting is not allowed for populated hierarchies */
1336 if (root
->number_of_cgroups
> 1) {
1341 ret
= rebind_subsystems(root
, added_mask
, removed_mask
);
1345 if (opts
.release_agent
)
1346 strcpy(root
->release_agent_path
, opts
.release_agent
);
1348 kfree(opts
.release_agent
);
1350 mutex_unlock(&cgroup_root_mutex
);
1351 mutex_unlock(&cgroup_mutex
);
1352 mutex_unlock(&cgrp
->dentry
->d_inode
->i_mutex
);
1356 static const struct super_operations cgroup_ops
= {
1357 .statfs
= simple_statfs
,
1358 .drop_inode
= generic_delete_inode
,
1359 .show_options
= cgroup_show_options
,
1360 .remount_fs
= cgroup_remount
,
1363 static void init_cgroup_housekeeping(struct cgroup
*cgrp
)
1365 INIT_LIST_HEAD(&cgrp
->sibling
);
1366 INIT_LIST_HEAD(&cgrp
->children
);
1367 INIT_LIST_HEAD(&cgrp
->files
);
1368 INIT_LIST_HEAD(&cgrp
->cset_links
);
1369 INIT_LIST_HEAD(&cgrp
->release_list
);
1370 INIT_LIST_HEAD(&cgrp
->pidlists
);
1371 mutex_init(&cgrp
->pidlist_mutex
);
1372 cgrp
->dummy_css
.cgroup
= cgrp
;
1373 INIT_LIST_HEAD(&cgrp
->event_list
);
1374 spin_lock_init(&cgrp
->event_list_lock
);
1375 simple_xattrs_init(&cgrp
->xattrs
);
1378 static void init_cgroup_root(struct cgroupfs_root
*root
)
1380 struct cgroup
*cgrp
= &root
->top_cgroup
;
1382 INIT_LIST_HEAD(&root
->subsys_list
);
1383 INIT_LIST_HEAD(&root
->root_list
);
1384 root
->number_of_cgroups
= 1;
1386 RCU_INIT_POINTER(cgrp
->name
, &root_cgroup_name
);
1387 init_cgroup_housekeeping(cgrp
);
1388 idr_init(&root
->cgroup_idr
);
1391 static int cgroup_init_root_id(struct cgroupfs_root
*root
, int start
, int end
)
1395 lockdep_assert_held(&cgroup_mutex
);
1396 lockdep_assert_held(&cgroup_root_mutex
);
1398 id
= idr_alloc_cyclic(&cgroup_hierarchy_idr
, root
, start
, end
,
1403 root
->hierarchy_id
= id
;
1407 static void cgroup_exit_root_id(struct cgroupfs_root
*root
)
1409 lockdep_assert_held(&cgroup_mutex
);
1410 lockdep_assert_held(&cgroup_root_mutex
);
1412 if (root
->hierarchy_id
) {
1413 idr_remove(&cgroup_hierarchy_idr
, root
->hierarchy_id
);
1414 root
->hierarchy_id
= 0;
1418 static int cgroup_test_super(struct super_block
*sb
, void *data
)
1420 struct cgroup_sb_opts
*opts
= data
;
1421 struct cgroupfs_root
*root
= sb
->s_fs_info
;
1423 /* If we asked for a name then it must match */
1424 if (opts
->name
&& strcmp(opts
->name
, root
->name
))
1428 * If we asked for subsystems (or explicitly for no
1429 * subsystems) then they must match
1431 if ((opts
->subsys_mask
|| opts
->none
)
1432 && (opts
->subsys_mask
!= root
->subsys_mask
))
1438 static struct cgroupfs_root
*cgroup_root_from_opts(struct cgroup_sb_opts
*opts
)
1440 struct cgroupfs_root
*root
;
1442 if (!opts
->subsys_mask
&& !opts
->none
)
1445 root
= kzalloc(sizeof(*root
), GFP_KERNEL
);
1447 return ERR_PTR(-ENOMEM
);
1449 init_cgroup_root(root
);
1452 * We need to set @root->subsys_mask now so that @root can be
1453 * matched by cgroup_test_super() before it finishes
1454 * initialization; otherwise, competing mounts with the same
1455 * options may try to bind the same subsystems instead of waiting
1456 * for the first one leading to unexpected mount errors.
1457 * SUBSYS_BOUND will be set once actual binding is complete.
1459 root
->subsys_mask
= opts
->subsys_mask
;
1460 root
->flags
= opts
->flags
;
1461 if (opts
->release_agent
)
1462 strcpy(root
->release_agent_path
, opts
->release_agent
);
1464 strcpy(root
->name
, opts
->name
);
1465 if (opts
->cpuset_clone_children
)
1466 set_bit(CGRP_CPUSET_CLONE_CHILDREN
, &root
->top_cgroup
.flags
);
1470 static void cgroup_free_root(struct cgroupfs_root
*root
)
1473 /* hierarhcy ID shoulid already have been released */
1474 WARN_ON_ONCE(root
->hierarchy_id
);
1476 idr_destroy(&root
->cgroup_idr
);
1481 static int cgroup_set_super(struct super_block
*sb
, void *data
)
1484 struct cgroup_sb_opts
*opts
= data
;
1486 /* If we don't have a new root, we can't set up a new sb */
1487 if (!opts
->new_root
)
1490 BUG_ON(!opts
->subsys_mask
&& !opts
->none
);
1492 ret
= set_anon_super(sb
, NULL
);
1496 sb
->s_fs_info
= opts
->new_root
;
1497 opts
->new_root
->sb
= sb
;
1499 sb
->s_blocksize
= PAGE_CACHE_SIZE
;
1500 sb
->s_blocksize_bits
= PAGE_CACHE_SHIFT
;
1501 sb
->s_magic
= CGROUP_SUPER_MAGIC
;
1502 sb
->s_op
= &cgroup_ops
;
1507 static int cgroup_get_rootdir(struct super_block
*sb
)
1509 static const struct dentry_operations cgroup_dops
= {
1510 .d_iput
= cgroup_diput
,
1511 .d_delete
= cgroup_delete
,
1514 struct inode
*inode
=
1515 cgroup_new_inode(S_IFDIR
| S_IRUGO
| S_IXUGO
| S_IWUSR
, sb
);
1520 inode
->i_fop
= &simple_dir_operations
;
1521 inode
->i_op
= &cgroup_dir_inode_operations
;
1522 /* directories start off with i_nlink == 2 (for "." entry) */
1524 sb
->s_root
= d_make_root(inode
);
1527 /* for everything else we want ->d_op set */
1528 sb
->s_d_op
= &cgroup_dops
;
1532 static struct dentry
*cgroup_mount(struct file_system_type
*fs_type
,
1533 int flags
, const char *unused_dev_name
,
1536 struct cgroup_sb_opts opts
;
1537 struct cgroupfs_root
*root
;
1539 struct super_block
*sb
;
1540 struct cgroupfs_root
*new_root
;
1541 struct list_head tmp_links
;
1542 struct inode
*inode
;
1543 const struct cred
*cred
;
1545 /* First find the desired set of subsystems */
1546 mutex_lock(&cgroup_mutex
);
1547 ret
= parse_cgroupfs_options(data
, &opts
);
1548 mutex_unlock(&cgroup_mutex
);
1553 * Allocate a new cgroup root. We may not need it if we're
1554 * reusing an existing hierarchy.
1556 new_root
= cgroup_root_from_opts(&opts
);
1557 if (IS_ERR(new_root
)) {
1558 ret
= PTR_ERR(new_root
);
1561 opts
.new_root
= new_root
;
1563 /* Locate an existing or new sb for this hierarchy */
1564 sb
= sget(fs_type
, cgroup_test_super
, cgroup_set_super
, 0, &opts
);
1567 cgroup_free_root(opts
.new_root
);
1571 root
= sb
->s_fs_info
;
1573 if (root
== opts
.new_root
) {
1574 /* We used the new root structure, so this is a new hierarchy */
1575 struct cgroup
*root_cgrp
= &root
->top_cgroup
;
1576 struct cgroupfs_root
*existing_root
;
1578 struct css_set
*cset
;
1580 BUG_ON(sb
->s_root
!= NULL
);
1582 ret
= cgroup_get_rootdir(sb
);
1584 goto drop_new_super
;
1585 inode
= sb
->s_root
->d_inode
;
1587 mutex_lock(&inode
->i_mutex
);
1588 mutex_lock(&cgroup_mutex
);
1589 mutex_lock(&cgroup_root_mutex
);
1591 root_cgrp
->id
= idr_alloc(&root
->cgroup_idr
, root_cgrp
,
1593 if (root_cgrp
->id
< 0)
1596 /* Check for name clashes with existing mounts */
1598 if (strlen(root
->name
))
1599 for_each_active_root(existing_root
)
1600 if (!strcmp(existing_root
->name
, root
->name
))
1604 * We're accessing css_set_count without locking
1605 * css_set_lock here, but that's OK - it can only be
1606 * increased by someone holding cgroup_lock, and
1607 * that's us. The worst that can happen is that we
1608 * have some link structures left over
1610 ret
= allocate_cgrp_cset_links(css_set_count
, &tmp_links
);
1614 /* ID 0 is reserved for dummy root, 1 for unified hierarchy */
1615 ret
= cgroup_init_root_id(root
, 2, 0);
1619 sb
->s_root
->d_fsdata
= root_cgrp
;
1620 root_cgrp
->dentry
= sb
->s_root
;
1623 * We're inside get_sb() and will call lookup_one_len() to
1624 * create the root files, which doesn't work if SELinux is
1625 * in use. The following cred dancing somehow works around
1626 * it. See 2ce9738ba ("cgroupfs: use init_cred when
1627 * populating new cgroupfs mount") for more details.
1629 cred
= override_creds(&init_cred
);
1631 ret
= cgroup_addrm_files(root_cgrp
, cgroup_base_files
, true);
1635 ret
= rebind_subsystems(root
, root
->subsys_mask
, 0);
1642 * There must be no failure case after here, since rebinding
1643 * takes care of subsystems' refcounts, which are explicitly
1644 * dropped in the failure exit path.
1647 list_add(&root
->root_list
, &cgroup_roots
);
1648 cgroup_root_count
++;
1650 /* Link the top cgroup in this hierarchy into all
1651 * the css_set objects */
1652 write_lock(&css_set_lock
);
1653 hash_for_each(css_set_table
, i
, cset
, hlist
)
1654 link_css_set(&tmp_links
, cset
, root_cgrp
);
1655 write_unlock(&css_set_lock
);
1657 free_cgrp_cset_links(&tmp_links
);
1659 BUG_ON(!list_empty(&root_cgrp
->children
));
1660 BUG_ON(root
->number_of_cgroups
!= 1);
1662 mutex_unlock(&cgroup_root_mutex
);
1663 mutex_unlock(&cgroup_mutex
);
1664 mutex_unlock(&inode
->i_mutex
);
1667 * We re-used an existing hierarchy - the new root (if
1668 * any) is not needed
1670 cgroup_free_root(opts
.new_root
);
1672 if ((root
->flags
^ opts
.flags
) & CGRP_ROOT_OPTION_MASK
) {
1673 if ((root
->flags
| opts
.flags
) & CGRP_ROOT_SANE_BEHAVIOR
) {
1674 pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
1676 goto drop_new_super
;
1678 pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n");
1683 kfree(opts
.release_agent
);
1685 return dget(sb
->s_root
);
1688 free_cgrp_cset_links(&tmp_links
);
1689 cgroup_addrm_files(&root
->top_cgroup
, cgroup_base_files
, false);
1692 cgroup_exit_root_id(root
);
1693 mutex_unlock(&cgroup_root_mutex
);
1694 mutex_unlock(&cgroup_mutex
);
1695 mutex_unlock(&inode
->i_mutex
);
1697 deactivate_locked_super(sb
);
1699 kfree(opts
.release_agent
);
1701 return ERR_PTR(ret
);
1704 static void cgroup_kill_sb(struct super_block
*sb
) {
1705 struct cgroupfs_root
*root
= sb
->s_fs_info
;
1706 struct cgroup
*cgrp
= &root
->top_cgroup
;
1707 struct cgrp_cset_link
*link
, *tmp_link
;
1712 BUG_ON(root
->number_of_cgroups
!= 1);
1713 BUG_ON(!list_empty(&cgrp
->children
));
1715 mutex_lock(&cgrp
->dentry
->d_inode
->i_mutex
);
1716 mutex_lock(&cgroup_mutex
);
1717 mutex_lock(&cgroup_root_mutex
);
1719 /* Rebind all subsystems back to the default hierarchy */
1720 if (root
->flags
& CGRP_ROOT_SUBSYS_BOUND
) {
1721 ret
= rebind_subsystems(root
, 0, root
->subsys_mask
);
1722 /* Shouldn't be able to fail ... */
1727 * Release all the links from cset_links to this hierarchy's
1730 write_lock(&css_set_lock
);
1732 list_for_each_entry_safe(link
, tmp_link
, &cgrp
->cset_links
, cset_link
) {
1733 list_del(&link
->cset_link
);
1734 list_del(&link
->cgrp_link
);
1737 write_unlock(&css_set_lock
);
1739 if (!list_empty(&root
->root_list
)) {
1740 list_del(&root
->root_list
);
1741 cgroup_root_count
--;
1744 cgroup_exit_root_id(root
);
1746 mutex_unlock(&cgroup_root_mutex
);
1747 mutex_unlock(&cgroup_mutex
);
1748 mutex_unlock(&cgrp
->dentry
->d_inode
->i_mutex
);
1750 simple_xattrs_free(&cgrp
->xattrs
);
1752 kill_litter_super(sb
);
1753 cgroup_free_root(root
);
1756 static struct file_system_type cgroup_fs_type
= {
1758 .mount
= cgroup_mount
,
1759 .kill_sb
= cgroup_kill_sb
,
1762 static struct kobject
*cgroup_kobj
;
1765 * cgroup_path - generate the path of a cgroup
1766 * @cgrp: the cgroup in question
1767 * @buf: the buffer to write the path into
1768 * @buflen: the length of the buffer
1770 * Writes path of cgroup into buf. Returns 0 on success, -errno on error.
1772 * We can't generate cgroup path using dentry->d_name, as accessing
1773 * dentry->name must be protected by irq-unsafe dentry->d_lock or parent
1774 * inode's i_mutex, while on the other hand cgroup_path() can be called
1775 * with some irq-safe spinlocks held.
1777 int cgroup_path(const struct cgroup
*cgrp
, char *buf
, int buflen
)
1779 int ret
= -ENAMETOOLONG
;
1782 if (!cgrp
->parent
) {
1783 if (strlcpy(buf
, "/", buflen
) >= buflen
)
1784 return -ENAMETOOLONG
;
1788 start
= buf
+ buflen
- 1;
1793 const char *name
= cgroup_name(cgrp
);
1797 if ((start
-= len
) < buf
)
1799 memcpy(start
, name
, len
);
1805 cgrp
= cgrp
->parent
;
1806 } while (cgrp
->parent
);
1808 memmove(buf
, start
, buf
+ buflen
- start
);
1813 EXPORT_SYMBOL_GPL(cgroup_path
);
1816 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
1817 * @task: target task
1818 * @buf: the buffer to write the path into
1819 * @buflen: the length of the buffer
1821 * Determine @task's cgroup on the first (the one with the lowest non-zero
1822 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
1823 * function grabs cgroup_mutex and shouldn't be used inside locks used by
1824 * cgroup controller callbacks.
1826 * Returns 0 on success, fails with -%ENAMETOOLONG if @buflen is too short.
1828 int task_cgroup_path(struct task_struct
*task
, char *buf
, size_t buflen
)
1830 struct cgroupfs_root
*root
;
1831 struct cgroup
*cgrp
;
1832 int hierarchy_id
= 1, ret
= 0;
1835 return -ENAMETOOLONG
;
1837 mutex_lock(&cgroup_mutex
);
1839 root
= idr_get_next(&cgroup_hierarchy_idr
, &hierarchy_id
);
1842 cgrp
= task_cgroup_from_root(task
, root
);
1843 ret
= cgroup_path(cgrp
, buf
, buflen
);
1845 /* if no hierarchy exists, everyone is in "/" */
1846 memcpy(buf
, "/", 2);
1849 mutex_unlock(&cgroup_mutex
);
1852 EXPORT_SYMBOL_GPL(task_cgroup_path
);
1855 * Control Group taskset
1857 struct task_and_cgroup
{
1858 struct task_struct
*task
;
1859 struct cgroup
*cgrp
;
1860 struct css_set
*cset
;
1863 struct cgroup_taskset
{
1864 struct task_and_cgroup single
;
1865 struct flex_array
*tc_array
;
1868 struct cgroup
*cur_cgrp
;
1872 * cgroup_taskset_first - reset taskset and return the first task
1873 * @tset: taskset of interest
1875 * @tset iteration is initialized and the first task is returned.
1877 struct task_struct
*cgroup_taskset_first(struct cgroup_taskset
*tset
)
1879 if (tset
->tc_array
) {
1881 return cgroup_taskset_next(tset
);
1883 tset
->cur_cgrp
= tset
->single
.cgrp
;
1884 return tset
->single
.task
;
1887 EXPORT_SYMBOL_GPL(cgroup_taskset_first
);
1890 * cgroup_taskset_next - iterate to the next task in taskset
1891 * @tset: taskset of interest
1893 * Return the next task in @tset. Iteration must have been initialized
1894 * with cgroup_taskset_first().
1896 struct task_struct
*cgroup_taskset_next(struct cgroup_taskset
*tset
)
1898 struct task_and_cgroup
*tc
;
1900 if (!tset
->tc_array
|| tset
->idx
>= tset
->tc_array_len
)
1903 tc
= flex_array_get(tset
->tc_array
, tset
->idx
++);
1904 tset
->cur_cgrp
= tc
->cgrp
;
1907 EXPORT_SYMBOL_GPL(cgroup_taskset_next
);
1910 * cgroup_taskset_cur_cgroup - return the matching cgroup for the current task
1911 * @tset: taskset of interest
1913 * Return the cgroup for the current (last returned) task of @tset. This
1914 * function must be preceded by either cgroup_taskset_first() or
1915 * cgroup_taskset_next().
1917 struct cgroup
*cgroup_taskset_cur_cgroup(struct cgroup_taskset
*tset
)
1919 return tset
->cur_cgrp
;
1921 EXPORT_SYMBOL_GPL(cgroup_taskset_cur_cgroup
);
1924 * cgroup_taskset_size - return the number of tasks in taskset
1925 * @tset: taskset of interest
1927 int cgroup_taskset_size(struct cgroup_taskset
*tset
)
1929 return tset
->tc_array
? tset
->tc_array_len
: 1;
1931 EXPORT_SYMBOL_GPL(cgroup_taskset_size
);
1935 * cgroup_task_migrate - move a task from one cgroup to another.
1937 * Must be called with cgroup_mutex and threadgroup locked.
1939 static void cgroup_task_migrate(struct cgroup
*old_cgrp
,
1940 struct task_struct
*tsk
,
1941 struct css_set
*new_cset
)
1943 struct css_set
*old_cset
;
1946 * We are synchronized through threadgroup_lock() against PF_EXITING
1947 * setting such that we can't race against cgroup_exit() changing the
1948 * css_set to init_css_set and dropping the old one.
1950 WARN_ON_ONCE(tsk
->flags
& PF_EXITING
);
1951 old_cset
= task_css_set(tsk
);
1954 rcu_assign_pointer(tsk
->cgroups
, new_cset
);
1957 /* Update the css_set linked lists if we're using them */
1958 write_lock(&css_set_lock
);
1959 if (!list_empty(&tsk
->cg_list
))
1960 list_move(&tsk
->cg_list
, &new_cset
->tasks
);
1961 write_unlock(&css_set_lock
);
1964 * We just gained a reference on old_cset by taking it from the
1965 * task. As trading it for new_cset is protected by cgroup_mutex,
1966 * we're safe to drop it here; it will be freed under RCU.
1968 set_bit(CGRP_RELEASABLE
, &old_cgrp
->flags
);
1969 put_css_set(old_cset
);
1973 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
1974 * @cgrp: the cgroup to attach to
1975 * @tsk: the task or the leader of the threadgroup to be attached
1976 * @threadgroup: attach the whole threadgroup?
1978 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
1979 * task_lock of @tsk or each thread in the threadgroup individually in turn.
1981 static int cgroup_attach_task(struct cgroup
*cgrp
, struct task_struct
*tsk
,
1984 int retval
, i
, group_size
;
1985 struct cgroup_subsys
*ss
, *failed_ss
= NULL
;
1986 struct cgroupfs_root
*root
= cgrp
->root
;
1987 /* threadgroup list cursor and array */
1988 struct task_struct
*leader
= tsk
;
1989 struct task_and_cgroup
*tc
;
1990 struct flex_array
*group
;
1991 struct cgroup_taskset tset
= { };
1994 * step 0: in order to do expensive, possibly blocking operations for
1995 * every thread, we cannot iterate the thread group list, since it needs
1996 * rcu or tasklist locked. instead, build an array of all threads in the
1997 * group - group_rwsem prevents new threads from appearing, and if
1998 * threads exit, this will just be an over-estimate.
2001 group_size
= get_nr_threads(tsk
);
2004 /* flex_array supports very large thread-groups better than kmalloc. */
2005 group
= flex_array_alloc(sizeof(*tc
), group_size
, GFP_KERNEL
);
2008 /* pre-allocate to guarantee space while iterating in rcu read-side. */
2009 retval
= flex_array_prealloc(group
, 0, group_size
, GFP_KERNEL
);
2011 goto out_free_group_list
;
2015 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2016 * already PF_EXITING could be freed from underneath us unless we
2017 * take an rcu_read_lock.
2021 struct task_and_cgroup ent
;
2023 /* @tsk either already exited or can't exit until the end */
2024 if (tsk
->flags
& PF_EXITING
)
2027 /* as per above, nr_threads may decrease, but not increase. */
2028 BUG_ON(i
>= group_size
);
2030 ent
.cgrp
= task_cgroup_from_root(tsk
, root
);
2031 /* nothing to do if this task is already in the cgroup */
2032 if (ent
.cgrp
== cgrp
)
2035 * saying GFP_ATOMIC has no effect here because we did prealloc
2036 * earlier, but it's good form to communicate our expectations.
2038 retval
= flex_array_put(group
, i
, &ent
, GFP_ATOMIC
);
2039 BUG_ON(retval
!= 0);
2044 } while_each_thread(leader
, tsk
);
2046 /* remember the number of threads in the array for later. */
2048 tset
.tc_array
= group
;
2049 tset
.tc_array_len
= group_size
;
2051 /* methods shouldn't be called if no task is actually migrating */
2054 goto out_free_group_list
;
2057 * step 1: check that we can legitimately attach to the cgroup.
2059 for_each_root_subsys(root
, ss
) {
2060 struct cgroup_subsys_state
*css
= cgrp
->subsys
[ss
->subsys_id
];
2062 if (ss
->can_attach
) {
2063 retval
= ss
->can_attach(css
, &tset
);
2066 goto out_cancel_attach
;
2072 * step 2: make sure css_sets exist for all threads to be migrated.
2073 * we use find_css_set, which allocates a new one if necessary.
2075 for (i
= 0; i
< group_size
; i
++) {
2076 struct css_set
*old_cset
;
2078 tc
= flex_array_get(group
, i
);
2079 old_cset
= task_css_set(tc
->task
);
2080 tc
->cset
= find_css_set(old_cset
, cgrp
);
2083 goto out_put_css_set_refs
;
2088 * step 3: now that we're guaranteed success wrt the css_sets,
2089 * proceed to move all tasks to the new cgroup. There are no
2090 * failure cases after here, so this is the commit point.
2092 for (i
= 0; i
< group_size
; i
++) {
2093 tc
= flex_array_get(group
, i
);
2094 cgroup_task_migrate(tc
->cgrp
, tc
->task
, tc
->cset
);
2096 /* nothing is sensitive to fork() after this point. */
2099 * step 4: do subsystem attach callbacks.
2101 for_each_root_subsys(root
, ss
) {
2102 struct cgroup_subsys_state
*css
= cgrp
->subsys
[ss
->subsys_id
];
2105 ss
->attach(css
, &tset
);
2109 * step 5: success! and cleanup
2112 out_put_css_set_refs
:
2114 for (i
= 0; i
< group_size
; i
++) {
2115 tc
= flex_array_get(group
, i
);
2118 put_css_set(tc
->cset
);
2123 for_each_root_subsys(root
, ss
) {
2124 struct cgroup_subsys_state
*css
= cgrp
->subsys
[ss
->subsys_id
];
2126 if (ss
== failed_ss
)
2128 if (ss
->cancel_attach
)
2129 ss
->cancel_attach(css
, &tset
);
2132 out_free_group_list
:
2133 flex_array_free(group
);
2138 * Find the task_struct of the task to attach by vpid and pass it along to the
2139 * function to attach either it or all tasks in its threadgroup. Will lock
2140 * cgroup_mutex and threadgroup; may take task_lock of task.
2142 static int attach_task_by_pid(struct cgroup
*cgrp
, u64 pid
, bool threadgroup
)
2144 struct task_struct
*tsk
;
2145 const struct cred
*cred
= current_cred(), *tcred
;
2148 if (!cgroup_lock_live_group(cgrp
))
2154 tsk
= find_task_by_vpid(pid
);
2158 goto out_unlock_cgroup
;
2161 * even if we're attaching all tasks in the thread group, we
2162 * only need to check permissions on one of them.
2164 tcred
= __task_cred(tsk
);
2165 if (!uid_eq(cred
->euid
, GLOBAL_ROOT_UID
) &&
2166 !uid_eq(cred
->euid
, tcred
->uid
) &&
2167 !uid_eq(cred
->euid
, tcred
->suid
)) {
2170 goto out_unlock_cgroup
;
2176 tsk
= tsk
->group_leader
;
2179 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
2180 * trapped in a cpuset, or RT worker may be born in a cgroup
2181 * with no rt_runtime allocated. Just say no.
2183 if (tsk
== kthreadd_task
|| (tsk
->flags
& PF_NO_SETAFFINITY
)) {
2186 goto out_unlock_cgroup
;
2189 get_task_struct(tsk
);
2192 threadgroup_lock(tsk
);
2194 if (!thread_group_leader(tsk
)) {
2196 * a race with de_thread from another thread's exec()
2197 * may strip us of our leadership, if this happens,
2198 * there is no choice but to throw this task away and
2199 * try again; this is
2200 * "double-double-toil-and-trouble-check locking".
2202 threadgroup_unlock(tsk
);
2203 put_task_struct(tsk
);
2204 goto retry_find_task
;
2208 ret
= cgroup_attach_task(cgrp
, tsk
, threadgroup
);
2210 threadgroup_unlock(tsk
);
2212 put_task_struct(tsk
);
2214 mutex_unlock(&cgroup_mutex
);
2219 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2220 * @from: attach to all cgroups of a given task
2221 * @tsk: the task to be attached
2223 int cgroup_attach_task_all(struct task_struct
*from
, struct task_struct
*tsk
)
2225 struct cgroupfs_root
*root
;
2228 mutex_lock(&cgroup_mutex
);
2229 for_each_active_root(root
) {
2230 struct cgroup
*from_cgrp
= task_cgroup_from_root(from
, root
);
2232 retval
= cgroup_attach_task(from_cgrp
, tsk
, false);
2236 mutex_unlock(&cgroup_mutex
);
2240 EXPORT_SYMBOL_GPL(cgroup_attach_task_all
);
2242 static int cgroup_tasks_write(struct cgroup_subsys_state
*css
,
2243 struct cftype
*cft
, u64 pid
)
2245 return attach_task_by_pid(css
->cgroup
, pid
, false);
2248 static int cgroup_procs_write(struct cgroup_subsys_state
*css
,
2249 struct cftype
*cft
, u64 tgid
)
2251 return attach_task_by_pid(css
->cgroup
, tgid
, true);
2254 static int cgroup_release_agent_write(struct cgroup_subsys_state
*css
,
2255 struct cftype
*cft
, const char *buffer
)
2257 BUILD_BUG_ON(sizeof(css
->cgroup
->root
->release_agent_path
) < PATH_MAX
);
2258 if (strlen(buffer
) >= PATH_MAX
)
2260 if (!cgroup_lock_live_group(css
->cgroup
))
2262 mutex_lock(&cgroup_root_mutex
);
2263 strcpy(css
->cgroup
->root
->release_agent_path
, buffer
);
2264 mutex_unlock(&cgroup_root_mutex
);
2265 mutex_unlock(&cgroup_mutex
);
2269 static int cgroup_release_agent_show(struct cgroup_subsys_state
*css
,
2270 struct cftype
*cft
, struct seq_file
*seq
)
2272 struct cgroup
*cgrp
= css
->cgroup
;
2274 if (!cgroup_lock_live_group(cgrp
))
2276 seq_puts(seq
, cgrp
->root
->release_agent_path
);
2277 seq_putc(seq
, '\n');
2278 mutex_unlock(&cgroup_mutex
);
2282 static int cgroup_sane_behavior_show(struct cgroup_subsys_state
*css
,
2283 struct cftype
*cft
, struct seq_file
*seq
)
2285 seq_printf(seq
, "%d\n", cgroup_sane_behavior(css
->cgroup
));
2289 /* return the css for the given cgroup file */
2290 static struct cgroup_subsys_state
*cgroup_file_css(struct cfent
*cfe
)
2292 struct cftype
*cft
= cfe
->type
;
2293 struct cgroup
*cgrp
= __d_cgrp(cfe
->dentry
->d_parent
);
2296 return cgrp
->subsys
[cft
->ss
->subsys_id
];
2297 return &cgrp
->dummy_css
;
2300 /* A buffer size big enough for numbers or short strings */
2301 #define CGROUP_LOCAL_BUFFER_SIZE 64
2303 static ssize_t
cgroup_write_X64(struct cgroup_subsys_state
*css
,
2304 struct cftype
*cft
, struct file
*file
,
2305 const char __user
*userbuf
, size_t nbytes
,
2306 loff_t
*unused_ppos
)
2308 char buffer
[CGROUP_LOCAL_BUFFER_SIZE
];
2314 if (nbytes
>= sizeof(buffer
))
2316 if (copy_from_user(buffer
, userbuf
, nbytes
))
2319 buffer
[nbytes
] = 0; /* nul-terminate */
2320 if (cft
->write_u64
) {
2321 u64 val
= simple_strtoull(strstrip(buffer
), &end
, 0);
2324 retval
= cft
->write_u64(css
, cft
, val
);
2326 s64 val
= simple_strtoll(strstrip(buffer
), &end
, 0);
2329 retval
= cft
->write_s64(css
, cft
, val
);
2336 static ssize_t
cgroup_write_string(struct cgroup_subsys_state
*css
,
2337 struct cftype
*cft
, struct file
*file
,
2338 const char __user
*userbuf
, size_t nbytes
,
2339 loff_t
*unused_ppos
)
2341 char local_buffer
[CGROUP_LOCAL_BUFFER_SIZE
];
2343 size_t max_bytes
= cft
->max_write_len
;
2344 char *buffer
= local_buffer
;
2347 max_bytes
= sizeof(local_buffer
) - 1;
2348 if (nbytes
>= max_bytes
)
2350 /* Allocate a dynamic buffer if we need one */
2351 if (nbytes
>= sizeof(local_buffer
)) {
2352 buffer
= kmalloc(nbytes
+ 1, GFP_KERNEL
);
2356 if (nbytes
&& copy_from_user(buffer
, userbuf
, nbytes
)) {
2361 buffer
[nbytes
] = 0; /* nul-terminate */
2362 retval
= cft
->write_string(css
, cft
, strstrip(buffer
));
2366 if (buffer
!= local_buffer
)
2371 static ssize_t
cgroup_file_write(struct file
*file
, const char __user
*buf
,
2372 size_t nbytes
, loff_t
*ppos
)
2374 struct cfent
*cfe
= __d_cfe(file
->f_dentry
);
2375 struct cftype
*cft
= __d_cft(file
->f_dentry
);
2376 struct cgroup_subsys_state
*css
= cgroup_file_css(cfe
);
2379 return cft
->write(css
, cft
, file
, buf
, nbytes
, ppos
);
2380 if (cft
->write_u64
|| cft
->write_s64
)
2381 return cgroup_write_X64(css
, cft
, file
, buf
, nbytes
, ppos
);
2382 if (cft
->write_string
)
2383 return cgroup_write_string(css
, cft
, file
, buf
, nbytes
, ppos
);
2385 int ret
= cft
->trigger(css
, (unsigned int)cft
->private);
2386 return ret
? ret
: nbytes
;
2391 static ssize_t
cgroup_read_u64(struct cgroup_subsys_state
*css
,
2392 struct cftype
*cft
, struct file
*file
,
2393 char __user
*buf
, size_t nbytes
, loff_t
*ppos
)
2395 char tmp
[CGROUP_LOCAL_BUFFER_SIZE
];
2396 u64 val
= cft
->read_u64(css
, cft
);
2397 int len
= sprintf(tmp
, "%llu\n", (unsigned long long) val
);
2399 return simple_read_from_buffer(buf
, nbytes
, ppos
, tmp
, len
);
2402 static ssize_t
cgroup_read_s64(struct cgroup_subsys_state
*css
,
2403 struct cftype
*cft
, struct file
*file
,
2404 char __user
*buf
, size_t nbytes
, loff_t
*ppos
)
2406 char tmp
[CGROUP_LOCAL_BUFFER_SIZE
];
2407 s64 val
= cft
->read_s64(css
, cft
);
2408 int len
= sprintf(tmp
, "%lld\n", (long long) val
);
2410 return simple_read_from_buffer(buf
, nbytes
, ppos
, tmp
, len
);
2413 static ssize_t
cgroup_file_read(struct file
*file
, char __user
*buf
,
2414 size_t nbytes
, loff_t
*ppos
)
2416 struct cfent
*cfe
= __d_cfe(file
->f_dentry
);
2417 struct cftype
*cft
= __d_cft(file
->f_dentry
);
2418 struct cgroup_subsys_state
*css
= cgroup_file_css(cfe
);
2421 return cft
->read(css
, cft
, file
, buf
, nbytes
, ppos
);
2423 return cgroup_read_u64(css
, cft
, file
, buf
, nbytes
, ppos
);
2425 return cgroup_read_s64(css
, cft
, file
, buf
, nbytes
, ppos
);
2430 * seqfile ops/methods for returning structured data. Currently just
2431 * supports string->u64 maps, but can be extended in future.
2434 static int cgroup_map_add(struct cgroup_map_cb
*cb
, const char *key
, u64 value
)
2436 struct seq_file
*sf
= cb
->state
;
2437 return seq_printf(sf
, "%s %llu\n", key
, (unsigned long long)value
);
2440 static int cgroup_seqfile_show(struct seq_file
*m
, void *arg
)
2442 struct cfent
*cfe
= m
->private;
2443 struct cftype
*cft
= cfe
->type
;
2444 struct cgroup_subsys_state
*css
= cgroup_file_css(cfe
);
2446 if (cft
->read_map
) {
2447 struct cgroup_map_cb cb
= {
2448 .fill
= cgroup_map_add
,
2451 return cft
->read_map(css
, cft
, &cb
);
2453 return cft
->read_seq_string(css
, cft
, m
);
2456 static const struct file_operations cgroup_seqfile_operations
= {
2458 .write
= cgroup_file_write
,
2459 .llseek
= seq_lseek
,
2460 .release
= single_release
,
2463 static int cgroup_file_open(struct inode
*inode
, struct file
*file
)
2465 struct cfent
*cfe
= __d_cfe(file
->f_dentry
);
2466 struct cftype
*cft
= __d_cft(file
->f_dentry
);
2467 struct cgroup_subsys_state
*css
= cgroup_file_css(cfe
);
2470 err
= generic_file_open(inode
, file
);
2475 * If the file belongs to a subsystem, pin the css. Will be
2476 * unpinned either on open failure or release. This ensures that
2477 * @css stays alive for all file operations.
2479 if (css
->ss
&& !css_tryget(css
))
2482 if (cft
->read_map
|| cft
->read_seq_string
) {
2483 file
->f_op
= &cgroup_seqfile_operations
;
2484 err
= single_open(file
, cgroup_seqfile_show
, cfe
);
2485 } else if (cft
->open
) {
2486 err
= cft
->open(inode
, file
);
2494 static int cgroup_file_release(struct inode
*inode
, struct file
*file
)
2496 struct cfent
*cfe
= __d_cfe(file
->f_dentry
);
2497 struct cftype
*cft
= __d_cft(file
->f_dentry
);
2498 struct cgroup_subsys_state
*css
= cgroup_file_css(cfe
);
2502 ret
= cft
->release(inode
, file
);
2509 * cgroup_rename - Only allow simple rename of directories in place.
2511 static int cgroup_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
2512 struct inode
*new_dir
, struct dentry
*new_dentry
)
2515 struct cgroup_name
*name
, *old_name
;
2516 struct cgroup
*cgrp
;
2519 * It's convinient to use parent dir's i_mutex to protected
2522 lockdep_assert_held(&old_dir
->i_mutex
);
2524 if (!S_ISDIR(old_dentry
->d_inode
->i_mode
))
2526 if (new_dentry
->d_inode
)
2528 if (old_dir
!= new_dir
)
2531 cgrp
= __d_cgrp(old_dentry
);
2534 * This isn't a proper migration and its usefulness is very
2535 * limited. Disallow if sane_behavior.
2537 if (cgroup_sane_behavior(cgrp
))
2540 name
= cgroup_alloc_name(new_dentry
);
2544 ret
= simple_rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
2550 old_name
= rcu_dereference_protected(cgrp
->name
, true);
2551 rcu_assign_pointer(cgrp
->name
, name
);
2553 kfree_rcu(old_name
, rcu_head
);
2557 static struct simple_xattrs
*__d_xattrs(struct dentry
*dentry
)
2559 if (S_ISDIR(dentry
->d_inode
->i_mode
))
2560 return &__d_cgrp(dentry
)->xattrs
;
2562 return &__d_cfe(dentry
)->xattrs
;
2565 static inline int xattr_enabled(struct dentry
*dentry
)
2567 struct cgroupfs_root
*root
= dentry
->d_sb
->s_fs_info
;
2568 return root
->flags
& CGRP_ROOT_XATTR
;
2571 static bool is_valid_xattr(const char *name
)
2573 if (!strncmp(name
, XATTR_TRUSTED_PREFIX
, XATTR_TRUSTED_PREFIX_LEN
) ||
2574 !strncmp(name
, XATTR_SECURITY_PREFIX
, XATTR_SECURITY_PREFIX_LEN
))
2579 static int cgroup_setxattr(struct dentry
*dentry
, const char *name
,
2580 const void *val
, size_t size
, int flags
)
2582 if (!xattr_enabled(dentry
))
2584 if (!is_valid_xattr(name
))
2586 return simple_xattr_set(__d_xattrs(dentry
), name
, val
, size
, flags
);
2589 static int cgroup_removexattr(struct dentry
*dentry
, const char *name
)
2591 if (!xattr_enabled(dentry
))
2593 if (!is_valid_xattr(name
))
2595 return simple_xattr_remove(__d_xattrs(dentry
), name
);
2598 static ssize_t
cgroup_getxattr(struct dentry
*dentry
, const char *name
,
2599 void *buf
, size_t size
)
2601 if (!xattr_enabled(dentry
))
2603 if (!is_valid_xattr(name
))
2605 return simple_xattr_get(__d_xattrs(dentry
), name
, buf
, size
);
2608 static ssize_t
cgroup_listxattr(struct dentry
*dentry
, char *buf
, size_t size
)
2610 if (!xattr_enabled(dentry
))
2612 return simple_xattr_list(__d_xattrs(dentry
), buf
, size
);
2615 static const struct file_operations cgroup_file_operations
= {
2616 .read
= cgroup_file_read
,
2617 .write
= cgroup_file_write
,
2618 .llseek
= generic_file_llseek
,
2619 .open
= cgroup_file_open
,
2620 .release
= cgroup_file_release
,
2623 static const struct inode_operations cgroup_file_inode_operations
= {
2624 .setxattr
= cgroup_setxattr
,
2625 .getxattr
= cgroup_getxattr
,
2626 .listxattr
= cgroup_listxattr
,
2627 .removexattr
= cgroup_removexattr
,
2630 static const struct inode_operations cgroup_dir_inode_operations
= {
2631 .lookup
= cgroup_lookup
,
2632 .mkdir
= cgroup_mkdir
,
2633 .rmdir
= cgroup_rmdir
,
2634 .rename
= cgroup_rename
,
2635 .setxattr
= cgroup_setxattr
,
2636 .getxattr
= cgroup_getxattr
,
2637 .listxattr
= cgroup_listxattr
,
2638 .removexattr
= cgroup_removexattr
,
2641 static struct dentry
*cgroup_lookup(struct inode
*dir
, struct dentry
*dentry
, unsigned int flags
)
2643 if (dentry
->d_name
.len
> NAME_MAX
)
2644 return ERR_PTR(-ENAMETOOLONG
);
2645 d_add(dentry
, NULL
);
2650 * Check if a file is a control file
2652 static inline struct cftype
*__file_cft(struct file
*file
)
2654 if (file_inode(file
)->i_fop
!= &cgroup_file_operations
)
2655 return ERR_PTR(-EINVAL
);
2656 return __d_cft(file
->f_dentry
);
2659 static int cgroup_create_file(struct dentry
*dentry
, umode_t mode
,
2660 struct super_block
*sb
)
2662 struct inode
*inode
;
2666 if (dentry
->d_inode
)
2669 inode
= cgroup_new_inode(mode
, sb
);
2673 if (S_ISDIR(mode
)) {
2674 inode
->i_op
= &cgroup_dir_inode_operations
;
2675 inode
->i_fop
= &simple_dir_operations
;
2677 /* start off with i_nlink == 2 (for "." entry) */
2679 inc_nlink(dentry
->d_parent
->d_inode
);
2682 * Control reaches here with cgroup_mutex held.
2683 * @inode->i_mutex should nest outside cgroup_mutex but we
2684 * want to populate it immediately without releasing
2685 * cgroup_mutex. As @inode isn't visible to anyone else
2686 * yet, trylock will always succeed without affecting
2689 WARN_ON_ONCE(!mutex_trylock(&inode
->i_mutex
));
2690 } else if (S_ISREG(mode
)) {
2692 inode
->i_fop
= &cgroup_file_operations
;
2693 inode
->i_op
= &cgroup_file_inode_operations
;
2695 d_instantiate(dentry
, inode
);
2696 dget(dentry
); /* Extra count - pin the dentry in core */
2701 * cgroup_file_mode - deduce file mode of a control file
2702 * @cft: the control file in question
2704 * returns cft->mode if ->mode is not 0
2705 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2706 * returns S_IRUGO if it has only a read handler
2707 * returns S_IWUSR if it has only a write hander
2709 static umode_t
cgroup_file_mode(const struct cftype
*cft
)
2716 if (cft
->read
|| cft
->read_u64
|| cft
->read_s64
||
2717 cft
->read_map
|| cft
->read_seq_string
)
2720 if (cft
->write
|| cft
->write_u64
|| cft
->write_s64
||
2721 cft
->write_string
|| cft
->trigger
)
2727 static int cgroup_add_file(struct cgroup
*cgrp
, struct cftype
*cft
)
2729 struct dentry
*dir
= cgrp
->dentry
;
2730 struct cgroup
*parent
= __d_cgrp(dir
);
2731 struct dentry
*dentry
;
2735 char name
[MAX_CGROUP_TYPE_NAMELEN
+ MAX_CFTYPE_NAME
+ 2] = { 0 };
2737 if (cft
->ss
&& !(cgrp
->root
->flags
& CGRP_ROOT_NOPREFIX
)) {
2738 strcpy(name
, cft
->ss
->name
);
2741 strcat(name
, cft
->name
);
2743 BUG_ON(!mutex_is_locked(&dir
->d_inode
->i_mutex
));
2745 cfe
= kzalloc(sizeof(*cfe
), GFP_KERNEL
);
2749 dentry
= lookup_one_len(name
, dir
, strlen(name
));
2750 if (IS_ERR(dentry
)) {
2751 error
= PTR_ERR(dentry
);
2755 cfe
->type
= (void *)cft
;
2756 cfe
->dentry
= dentry
;
2757 dentry
->d_fsdata
= cfe
;
2758 simple_xattrs_init(&cfe
->xattrs
);
2760 mode
= cgroup_file_mode(cft
);
2761 error
= cgroup_create_file(dentry
, mode
| S_IFREG
, cgrp
->root
->sb
);
2763 list_add_tail(&cfe
->node
, &parent
->files
);
2773 * cgroup_addrm_files - add or remove files to a cgroup directory
2774 * @cgrp: the target cgroup
2775 * @cfts: array of cftypes to be added
2776 * @is_add: whether to add or remove
2778 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
2779 * For removals, this function never fails. If addition fails, this
2780 * function doesn't remove files already added. The caller is responsible
2783 static int cgroup_addrm_files(struct cgroup
*cgrp
, struct cftype cfts
[],
2789 lockdep_assert_held(&cgrp
->dentry
->d_inode
->i_mutex
);
2790 lockdep_assert_held(&cgroup_mutex
);
2792 for (cft
= cfts
; cft
->name
[0] != '\0'; cft
++) {
2793 /* does cft->flags tell us to skip this file on @cgrp? */
2794 if ((cft
->flags
& CFTYPE_INSANE
) && cgroup_sane_behavior(cgrp
))
2796 if ((cft
->flags
& CFTYPE_NOT_ON_ROOT
) && !cgrp
->parent
)
2798 if ((cft
->flags
& CFTYPE_ONLY_ON_ROOT
) && cgrp
->parent
)
2802 ret
= cgroup_add_file(cgrp
, cft
);
2804 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
2809 cgroup_rm_file(cgrp
, cft
);
2815 static void cgroup_cfts_prepare(void)
2816 __acquires(&cgroup_mutex
)
2819 * Thanks to the entanglement with vfs inode locking, we can't walk
2820 * the existing cgroups under cgroup_mutex and create files.
2821 * Instead, we use css_for_each_descendant_pre() and drop RCU read
2822 * lock before calling cgroup_addrm_files().
2824 mutex_lock(&cgroup_mutex
);
2827 static int cgroup_cfts_commit(struct cftype
*cfts
, bool is_add
)
2828 __releases(&cgroup_mutex
)
2831 struct cgroup_subsys
*ss
= cfts
[0].ss
;
2832 struct cgroup
*root
= &ss
->root
->top_cgroup
;
2833 struct super_block
*sb
= ss
->root
->sb
;
2834 struct dentry
*prev
= NULL
;
2835 struct inode
*inode
;
2836 struct cgroup_subsys_state
*css
;
2840 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
2841 if (!cfts
|| ss
->root
== &cgroup_dummy_root
||
2842 !atomic_inc_not_zero(&sb
->s_active
)) {
2843 mutex_unlock(&cgroup_mutex
);
2848 * All cgroups which are created after we drop cgroup_mutex will
2849 * have the updated set of files, so we only need to update the
2850 * cgroups created before the current @cgroup_serial_nr_next.
2852 update_before
= cgroup_serial_nr_next
;
2854 mutex_unlock(&cgroup_mutex
);
2856 /* @root always needs to be updated */
2857 inode
= root
->dentry
->d_inode
;
2858 mutex_lock(&inode
->i_mutex
);
2859 mutex_lock(&cgroup_mutex
);
2860 ret
= cgroup_addrm_files(root
, cfts
, is_add
);
2861 mutex_unlock(&cgroup_mutex
);
2862 mutex_unlock(&inode
->i_mutex
);
2867 /* add/rm files for all cgroups created before */
2869 css_for_each_descendant_pre(css
, cgroup_css(root
, ss
->subsys_id
)) {
2870 struct cgroup
*cgrp
= css
->cgroup
;
2872 if (cgroup_is_dead(cgrp
))
2875 inode
= cgrp
->dentry
->d_inode
;
2880 prev
= cgrp
->dentry
;
2882 mutex_lock(&inode
->i_mutex
);
2883 mutex_lock(&cgroup_mutex
);
2884 if (cgrp
->serial_nr
< update_before
&& !cgroup_is_dead(cgrp
))
2885 ret
= cgroup_addrm_files(cgrp
, cfts
, is_add
);
2886 mutex_unlock(&cgroup_mutex
);
2887 mutex_unlock(&inode
->i_mutex
);
2896 deactivate_super(sb
);
2901 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2902 * @ss: target cgroup subsystem
2903 * @cfts: zero-length name terminated array of cftypes
2905 * Register @cfts to @ss. Files described by @cfts are created for all
2906 * existing cgroups to which @ss is attached and all future cgroups will
2907 * have them too. This function can be called anytime whether @ss is
2910 * Returns 0 on successful registration, -errno on failure. Note that this
2911 * function currently returns 0 as long as @cfts registration is successful
2912 * even if some file creation attempts on existing cgroups fail.
2914 int cgroup_add_cftypes(struct cgroup_subsys
*ss
, struct cftype
*cfts
)
2916 struct cftype_set
*set
;
2920 set
= kzalloc(sizeof(*set
), GFP_KERNEL
);
2924 for (cft
= cfts
; cft
->name
[0] != '\0'; cft
++)
2927 cgroup_cfts_prepare();
2929 list_add_tail(&set
->node
, &ss
->cftsets
);
2930 ret
= cgroup_cfts_commit(cfts
, true);
2932 cgroup_rm_cftypes(cfts
);
2935 EXPORT_SYMBOL_GPL(cgroup_add_cftypes
);
2938 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
2939 * @cfts: zero-length name terminated array of cftypes
2941 * Unregister @cfts. Files described by @cfts are removed from all
2942 * existing cgroups and all future cgroups won't have them either. This
2943 * function can be called anytime whether @cfts' subsys is attached or not.
2945 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
2948 int cgroup_rm_cftypes(struct cftype
*cfts
)
2950 struct cftype_set
*set
;
2952 if (!cfts
|| !cfts
[0].ss
)
2955 cgroup_cfts_prepare();
2957 list_for_each_entry(set
, &cfts
[0].ss
->cftsets
, node
) {
2958 if (set
->cfts
== cfts
) {
2959 list_del(&set
->node
);
2961 cgroup_cfts_commit(cfts
, false);
2966 cgroup_cfts_commit(NULL
, false);
2971 * cgroup_task_count - count the number of tasks in a cgroup.
2972 * @cgrp: the cgroup in question
2974 * Return the number of tasks in the cgroup.
2976 int cgroup_task_count(const struct cgroup
*cgrp
)
2979 struct cgrp_cset_link
*link
;
2981 read_lock(&css_set_lock
);
2982 list_for_each_entry(link
, &cgrp
->cset_links
, cset_link
)
2983 count
+= atomic_read(&link
->cset
->refcount
);
2984 read_unlock(&css_set_lock
);
2989 * To reduce the fork() overhead for systems that are not actually using
2990 * their cgroups capability, we don't maintain the lists running through
2991 * each css_set to its tasks until we see the list actually used - in other
2992 * words after the first call to css_task_iter_start().
2994 static void cgroup_enable_task_cg_lists(void)
2996 struct task_struct
*p
, *g
;
2997 write_lock(&css_set_lock
);
2998 use_task_css_set_links
= 1;
3000 * We need tasklist_lock because RCU is not safe against
3001 * while_each_thread(). Besides, a forking task that has passed
3002 * cgroup_post_fork() without seeing use_task_css_set_links = 1
3003 * is not guaranteed to have its child immediately visible in the
3004 * tasklist if we walk through it with RCU.
3006 read_lock(&tasklist_lock
);
3007 do_each_thread(g
, p
) {
3010 * We should check if the process is exiting, otherwise
3011 * it will race with cgroup_exit() in that the list
3012 * entry won't be deleted though the process has exited.
3014 if (!(p
->flags
& PF_EXITING
) && list_empty(&p
->cg_list
))
3015 list_add(&p
->cg_list
, &task_css_set(p
)->tasks
);
3017 } while_each_thread(g
, p
);
3018 read_unlock(&tasklist_lock
);
3019 write_unlock(&css_set_lock
);
3023 * css_next_child - find the next child of a given css
3024 * @pos_css: the current position (%NULL to initiate traversal)
3025 * @parent_css: css whose children to walk
3027 * This function returns the next child of @parent_css and should be called
3028 * under RCU read lock. The only requirement is that @parent_css and
3029 * @pos_css are accessible. The next sibling is guaranteed to be returned
3030 * regardless of their states.
3032 struct cgroup_subsys_state
*
3033 css_next_child(struct cgroup_subsys_state
*pos_css
,
3034 struct cgroup_subsys_state
*parent_css
)
3036 struct cgroup
*pos
= pos_css
? pos_css
->cgroup
: NULL
;
3037 struct cgroup
*cgrp
= parent_css
->cgroup
;
3038 struct cgroup
*next
;
3040 WARN_ON_ONCE(!rcu_read_lock_held());
3043 * @pos could already have been removed. Once a cgroup is removed,
3044 * its ->sibling.next is no longer updated when its next sibling
3045 * changes. As CGRP_DEAD assertion is serialized and happens
3046 * before the cgroup is taken off the ->sibling list, if we see it
3047 * unasserted, it's guaranteed that the next sibling hasn't
3048 * finished its grace period even if it's already removed, and thus
3049 * safe to dereference from this RCU critical section. If
3050 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
3051 * to be visible as %true here.
3053 * If @pos is dead, its next pointer can't be dereferenced;
3054 * however, as each cgroup is given a monotonically increasing
3055 * unique serial number and always appended to the sibling list,
3056 * the next one can be found by walking the parent's children until
3057 * we see a cgroup with higher serial number than @pos's. While
3058 * this path can be slower, it's taken only when either the current
3059 * cgroup is removed or iteration and removal race.
3062 next
= list_entry_rcu(cgrp
->children
.next
, struct cgroup
, sibling
);
3063 } else if (likely(!cgroup_is_dead(pos
))) {
3064 next
= list_entry_rcu(pos
->sibling
.next
, struct cgroup
, sibling
);
3066 list_for_each_entry_rcu(next
, &cgrp
->children
, sibling
)
3067 if (next
->serial_nr
> pos
->serial_nr
)
3071 if (&next
->sibling
== &cgrp
->children
)
3075 return cgroup_css(next
, parent_css
->ss
->subsys_id
);
3077 return &next
->dummy_css
;
3079 EXPORT_SYMBOL_GPL(css_next_child
);
3082 * css_next_descendant_pre - find the next descendant for pre-order walk
3083 * @pos: the current position (%NULL to initiate traversal)
3084 * @root: css whose descendants to walk
3086 * To be used by css_for_each_descendant_pre(). Find the next descendant
3087 * to visit for pre-order traversal of @root's descendants.
3089 * While this function requires RCU read locking, it doesn't require the
3090 * whole traversal to be contained in a single RCU critical section. This
3091 * function will return the correct next descendant as long as both @pos
3092 * and @root are accessible and @pos is a descendant of @root.
3094 struct cgroup_subsys_state
*
3095 css_next_descendant_pre(struct cgroup_subsys_state
*pos
,
3096 struct cgroup_subsys_state
*root
)
3098 struct cgroup_subsys_state
*next
;
3100 WARN_ON_ONCE(!rcu_read_lock_held());
3102 /* if first iteration, pretend we just visited @root */
3106 /* visit the first child if exists */
3107 next
= css_next_child(NULL
, pos
);
3111 /* no child, visit my or the closest ancestor's next sibling */
3112 while (pos
!= root
) {
3113 next
= css_next_child(pos
, css_parent(pos
));
3116 pos
= css_parent(pos
);
3121 EXPORT_SYMBOL_GPL(css_next_descendant_pre
);
3124 * css_rightmost_descendant - return the rightmost descendant of a css
3125 * @pos: css of interest
3127 * Return the rightmost descendant of @pos. If there's no descendant, @pos
3128 * is returned. This can be used during pre-order traversal to skip
3131 * While this function requires RCU read locking, it doesn't require the
3132 * whole traversal to be contained in a single RCU critical section. This
3133 * function will return the correct rightmost descendant as long as @pos is
3136 struct cgroup_subsys_state
*
3137 css_rightmost_descendant(struct cgroup_subsys_state
*pos
)
3139 struct cgroup_subsys_state
*last
, *tmp
;
3141 WARN_ON_ONCE(!rcu_read_lock_held());
3145 /* ->prev isn't RCU safe, walk ->next till the end */
3147 css_for_each_child(tmp
, last
)
3153 EXPORT_SYMBOL_GPL(css_rightmost_descendant
);
3155 static struct cgroup_subsys_state
*
3156 css_leftmost_descendant(struct cgroup_subsys_state
*pos
)
3158 struct cgroup_subsys_state
*last
;
3162 pos
= css_next_child(NULL
, pos
);
3169 * css_next_descendant_post - find the next descendant for post-order walk
3170 * @pos: the current position (%NULL to initiate traversal)
3171 * @root: css whose descendants to walk
3173 * To be used by css_for_each_descendant_post(). Find the next descendant
3174 * to visit for post-order traversal of @root's descendants.
3176 * While this function requires RCU read locking, it doesn't require the
3177 * whole traversal to be contained in a single RCU critical section. This
3178 * function will return the correct next descendant as long as both @pos
3179 * and @cgroup are accessible and @pos is a descendant of @cgroup.
3181 struct cgroup_subsys_state
*
3182 css_next_descendant_post(struct cgroup_subsys_state
*pos
,
3183 struct cgroup_subsys_state
*root
)
3185 struct cgroup_subsys_state
*next
;
3187 WARN_ON_ONCE(!rcu_read_lock_held());
3189 /* if first iteration, visit the leftmost descendant */
3191 next
= css_leftmost_descendant(root
);
3192 return next
!= root
? next
: NULL
;
3195 /* if there's an unvisited sibling, visit its leftmost descendant */
3196 next
= css_next_child(pos
, css_parent(pos
));
3198 return css_leftmost_descendant(next
);
3200 /* no sibling left, visit parent */
3201 next
= css_parent(pos
);
3202 return next
!= root
? next
: NULL
;
3204 EXPORT_SYMBOL_GPL(css_next_descendant_post
);
3207 * css_advance_task_iter - advance a task itererator to the next css_set
3208 * @it: the iterator to advance
3210 * Advance @it to the next css_set to walk.
3212 static void css_advance_task_iter(struct css_task_iter
*it
)
3214 struct list_head
*l
= it
->cset_link
;
3215 struct cgrp_cset_link
*link
;
3216 struct css_set
*cset
;
3218 /* Advance to the next non-empty css_set */
3221 if (l
== &it
->origin_css
->cgroup
->cset_links
) {
3222 it
->cset_link
= NULL
;
3225 link
= list_entry(l
, struct cgrp_cset_link
, cset_link
);
3227 } while (list_empty(&cset
->tasks
));
3229 it
->task
= cset
->tasks
.next
;
3233 * css_task_iter_start - initiate task iteration
3234 * @css: the css to walk tasks of
3235 * @it: the task iterator to use
3237 * Initiate iteration through the tasks of @css. The caller can call
3238 * css_task_iter_next() to walk through the tasks until the function
3239 * returns NULL. On completion of iteration, css_task_iter_end() must be
3242 * Note that this function acquires a lock which is released when the
3243 * iteration finishes. The caller can't sleep while iteration is in
3246 void css_task_iter_start(struct cgroup_subsys_state
*css
,
3247 struct css_task_iter
*it
)
3248 __acquires(css_set_lock
)
3251 * The first time anyone tries to iterate across a css, we need to
3252 * enable the list linking each css_set to its tasks, and fix up
3253 * all existing tasks.
3255 if (!use_task_css_set_links
)
3256 cgroup_enable_task_cg_lists();
3258 read_lock(&css_set_lock
);
3260 it
->origin_css
= css
;
3261 it
->cset_link
= &css
->cgroup
->cset_links
;
3263 css_advance_task_iter(it
);
3267 * css_task_iter_next - return the next task for the iterator
3268 * @it: the task iterator being iterated
3270 * The "next" function for task iteration. @it should have been
3271 * initialized via css_task_iter_start(). Returns NULL when the iteration
3274 struct task_struct
*css_task_iter_next(struct css_task_iter
*it
)
3276 struct task_struct
*res
;
3277 struct list_head
*l
= it
->task
;
3278 struct cgrp_cset_link
*link
;
3280 /* If the iterator cg is NULL, we have no tasks */
3283 res
= list_entry(l
, struct task_struct
, cg_list
);
3284 /* Advance iterator to find next entry */
3286 link
= list_entry(it
->cset_link
, struct cgrp_cset_link
, cset_link
);
3287 if (l
== &link
->cset
->tasks
) {
3289 * We reached the end of this task list - move on to the
3290 * next cgrp_cset_link.
3292 css_advance_task_iter(it
);
3300 * css_task_iter_end - finish task iteration
3301 * @it: the task iterator to finish
3303 * Finish task iteration started by css_task_iter_start().
3305 void css_task_iter_end(struct css_task_iter
*it
)
3306 __releases(css_set_lock
)
3308 read_unlock(&css_set_lock
);
3311 static inline int started_after_time(struct task_struct
*t1
,
3312 struct timespec
*time
,
3313 struct task_struct
*t2
)
3315 int start_diff
= timespec_compare(&t1
->start_time
, time
);
3316 if (start_diff
> 0) {
3318 } else if (start_diff
< 0) {
3322 * Arbitrarily, if two processes started at the same
3323 * time, we'll say that the lower pointer value
3324 * started first. Note that t2 may have exited by now
3325 * so this may not be a valid pointer any longer, but
3326 * that's fine - it still serves to distinguish
3327 * between two tasks started (effectively) simultaneously.
3334 * This function is a callback from heap_insert() and is used to order
3336 * In this case we order the heap in descending task start time.
3338 static inline int started_after(void *p1
, void *p2
)
3340 struct task_struct
*t1
= p1
;
3341 struct task_struct
*t2
= p2
;
3342 return started_after_time(t1
, &t2
->start_time
, t2
);
3346 * css_scan_tasks - iterate though all the tasks in a css
3347 * @css: the css to iterate tasks of
3348 * @test: optional test callback
3349 * @process: process callback
3350 * @data: data passed to @test and @process
3351 * @heap: optional pre-allocated heap used for task iteration
3353 * Iterate through all the tasks in @css, calling @test for each, and if it
3354 * returns %true, call @process for it also.
3356 * @test may be NULL, meaning always true (select all tasks), which
3357 * effectively duplicates css_task_iter_{start,next,end}() but does not
3358 * lock css_set_lock for the call to @process.
3360 * It is guaranteed that @process will act on every task that is a member
3361 * of @css for the duration of this call. This function may or may not
3362 * call @process for tasks that exit or move to a different css during the
3363 * call, or are forked or move into the css during the call.
3365 * Note that @test may be called with locks held, and may in some
3366 * situations be called multiple times for the same task, so it should be
3369 * If @heap is non-NULL, a heap has been pre-allocated and will be used for
3370 * heap operations (and its "gt" member will be overwritten), else a
3371 * temporary heap will be used (allocation of which may cause this function
3374 int css_scan_tasks(struct cgroup_subsys_state
*css
,
3375 bool (*test
)(struct task_struct
*, void *),
3376 void (*process
)(struct task_struct
*, void *),
3377 void *data
, struct ptr_heap
*heap
)
3380 struct css_task_iter it
;
3381 struct task_struct
*p
, *dropped
;
3382 /* Never dereference latest_task, since it's not refcounted */
3383 struct task_struct
*latest_task
= NULL
;
3384 struct ptr_heap tmp_heap
;
3385 struct timespec latest_time
= { 0, 0 };
3388 /* The caller supplied our heap and pre-allocated its memory */
3389 heap
->gt
= &started_after
;
3391 /* We need to allocate our own heap memory */
3393 retval
= heap_init(heap
, PAGE_SIZE
, GFP_KERNEL
, &started_after
);
3395 /* cannot allocate the heap */
3401 * Scan tasks in the css, using the @test callback to determine
3402 * which are of interest, and invoking @process callback on the
3403 * ones which need an update. Since we don't want to hold any
3404 * locks during the task updates, gather tasks to be processed in a
3405 * heap structure. The heap is sorted by descending task start
3406 * time. If the statically-sized heap fills up, we overflow tasks
3407 * that started later, and in future iterations only consider tasks
3408 * that started after the latest task in the previous pass. This
3409 * guarantees forward progress and that we don't miss any tasks.
3412 css_task_iter_start(css
, &it
);
3413 while ((p
= css_task_iter_next(&it
))) {
3415 * Only affect tasks that qualify per the caller's callback,
3416 * if he provided one
3418 if (test
&& !test(p
, data
))
3421 * Only process tasks that started after the last task
3424 if (!started_after_time(p
, &latest_time
, latest_task
))
3426 dropped
= heap_insert(heap
, p
);
3427 if (dropped
== NULL
) {
3429 * The new task was inserted; the heap wasn't
3433 } else if (dropped
!= p
) {
3435 * The new task was inserted, and pushed out a
3439 put_task_struct(dropped
);
3442 * Else the new task was newer than anything already in
3443 * the heap and wasn't inserted
3446 css_task_iter_end(&it
);
3449 for (i
= 0; i
< heap
->size
; i
++) {
3450 struct task_struct
*q
= heap
->ptrs
[i
];
3452 latest_time
= q
->start_time
;
3455 /* Process the task per the caller's callback */
3460 * If we had to process any tasks at all, scan again
3461 * in case some of them were in the middle of forking
3462 * children that didn't get processed.
3463 * Not the most efficient way to do it, but it avoids
3464 * having to take callback_mutex in the fork path
3468 if (heap
== &tmp_heap
)
3469 heap_free(&tmp_heap
);
3473 static void cgroup_transfer_one_task(struct task_struct
*task
, void *data
)
3475 struct cgroup
*new_cgroup
= data
;
3477 mutex_lock(&cgroup_mutex
);
3478 cgroup_attach_task(new_cgroup
, task
, false);
3479 mutex_unlock(&cgroup_mutex
);
3483 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3484 * @to: cgroup to which the tasks will be moved
3485 * @from: cgroup in which the tasks currently reside
3487 int cgroup_transfer_tasks(struct cgroup
*to
, struct cgroup
*from
)
3489 return css_scan_tasks(&from
->dummy_css
, NULL
, cgroup_transfer_one_task
,
3494 * Stuff for reading the 'tasks'/'procs' files.
3496 * Reading this file can return large amounts of data if a cgroup has
3497 * *lots* of attached tasks. So it may need several calls to read(),
3498 * but we cannot guarantee that the information we produce is correct
3499 * unless we produce it entirely atomically.
3503 /* which pidlist file are we talking about? */
3504 enum cgroup_filetype
{
3510 * A pidlist is a list of pids that virtually represents the contents of one
3511 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3512 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3515 struct cgroup_pidlist
{
3517 * used to find which pidlist is wanted. doesn't change as long as
3518 * this particular list stays in the list.
3520 struct { enum cgroup_filetype type
; struct pid_namespace
*ns
; } key
;
3523 /* how many elements the above list has */
3525 /* how many files are using the current array */
3527 /* each of these stored in a list by its cgroup */
3528 struct list_head links
;
3529 /* pointer to the cgroup we belong to, for list removal purposes */
3530 struct cgroup
*owner
;
3531 /* protects the other fields */
3532 struct rw_semaphore rwsem
;
3536 * The following two functions "fix" the issue where there are more pids
3537 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3538 * TODO: replace with a kernel-wide solution to this problem
3540 #define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3541 static void *pidlist_allocate(int count
)
3543 if (PIDLIST_TOO_LARGE(count
))
3544 return vmalloc(count
* sizeof(pid_t
));
3546 return kmalloc(count
* sizeof(pid_t
), GFP_KERNEL
);
3548 static void pidlist_free(void *p
)
3550 if (is_vmalloc_addr(p
))
3557 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
3558 * Returns the number of unique elements.
3560 static int pidlist_uniq(pid_t
*list
, int length
)
3565 * we presume the 0th element is unique, so i starts at 1. trivial
3566 * edge cases first; no work needs to be done for either
3568 if (length
== 0 || length
== 1)
3570 /* src and dest walk down the list; dest counts unique elements */
3571 for (src
= 1; src
< length
; src
++) {
3572 /* find next unique element */
3573 while (list
[src
] == list
[src
-1]) {
3578 /* dest always points to where the next unique element goes */
3579 list
[dest
] = list
[src
];
3586 static int cmppid(const void *a
, const void *b
)
3588 return *(pid_t
*)a
- *(pid_t
*)b
;
3592 * find the appropriate pidlist for our purpose (given procs vs tasks)
3593 * returns with the lock on that pidlist already held, and takes care
3594 * of the use count, or returns NULL with no locks held if we're out of
3597 static struct cgroup_pidlist
*cgroup_pidlist_find(struct cgroup
*cgrp
,
3598 enum cgroup_filetype type
)
3600 struct cgroup_pidlist
*l
;
3601 /* don't need task_nsproxy() if we're looking at ourself */
3602 struct pid_namespace
*ns
= task_active_pid_ns(current
);
3605 * We can't drop the pidlist_mutex before taking the l->rwsem in case
3606 * the last ref-holder is trying to remove l from the list at the same
3607 * time. Holding the pidlist_mutex precludes somebody taking whichever
3608 * list we find out from under us - compare release_pid_array().
3610 mutex_lock(&cgrp
->pidlist_mutex
);
3611 list_for_each_entry(l
, &cgrp
->pidlists
, links
) {
3612 if (l
->key
.type
== type
&& l
->key
.ns
== ns
) {
3613 /* make sure l doesn't vanish out from under us */
3614 down_write(&l
->rwsem
);
3615 mutex_unlock(&cgrp
->pidlist_mutex
);
3619 /* entry not found; create a new one */
3620 l
= kzalloc(sizeof(struct cgroup_pidlist
), GFP_KERNEL
);
3622 mutex_unlock(&cgrp
->pidlist_mutex
);
3625 init_rwsem(&l
->rwsem
);
3626 down_write(&l
->rwsem
);
3628 l
->key
.ns
= get_pid_ns(ns
);
3630 list_add(&l
->links
, &cgrp
->pidlists
);
3631 mutex_unlock(&cgrp
->pidlist_mutex
);
3636 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3638 static int pidlist_array_load(struct cgroup
*cgrp
, enum cgroup_filetype type
,
3639 struct cgroup_pidlist
**lp
)
3643 int pid
, n
= 0; /* used for populating the array */
3644 struct css_task_iter it
;
3645 struct task_struct
*tsk
;
3646 struct cgroup_pidlist
*l
;
3649 * If cgroup gets more users after we read count, we won't have
3650 * enough space - tough. This race is indistinguishable to the
3651 * caller from the case that the additional cgroup users didn't
3652 * show up until sometime later on.
3654 length
= cgroup_task_count(cgrp
);
3655 array
= pidlist_allocate(length
);
3658 /* now, populate the array */
3659 css_task_iter_start(&cgrp
->dummy_css
, &it
);
3660 while ((tsk
= css_task_iter_next(&it
))) {
3661 if (unlikely(n
== length
))
3663 /* get tgid or pid for procs or tasks file respectively */
3664 if (type
== CGROUP_FILE_PROCS
)
3665 pid
= task_tgid_vnr(tsk
);
3667 pid
= task_pid_vnr(tsk
);
3668 if (pid
> 0) /* make sure to only use valid results */
3671 css_task_iter_end(&it
);
3673 /* now sort & (if procs) strip out duplicates */
3674 sort(array
, length
, sizeof(pid_t
), cmppid
, NULL
);
3675 if (type
== CGROUP_FILE_PROCS
)
3676 length
= pidlist_uniq(array
, length
);
3677 l
= cgroup_pidlist_find(cgrp
, type
);
3679 pidlist_free(array
);
3682 /* store array, freeing old if necessary - lock already held */
3683 pidlist_free(l
->list
);
3687 up_write(&l
->rwsem
);
3693 * cgroupstats_build - build and fill cgroupstats
3694 * @stats: cgroupstats to fill information into
3695 * @dentry: A dentry entry belonging to the cgroup for which stats have
3698 * Build and fill cgroupstats so that taskstats can export it to user
3701 int cgroupstats_build(struct cgroupstats
*stats
, struct dentry
*dentry
)
3704 struct cgroup
*cgrp
;
3705 struct css_task_iter it
;
3706 struct task_struct
*tsk
;
3709 * Validate dentry by checking the superblock operations,
3710 * and make sure it's a directory.
3712 if (dentry
->d_sb
->s_op
!= &cgroup_ops
||
3713 !S_ISDIR(dentry
->d_inode
->i_mode
))
3717 cgrp
= dentry
->d_fsdata
;
3719 css_task_iter_start(&cgrp
->dummy_css
, &it
);
3720 while ((tsk
= css_task_iter_next(&it
))) {
3721 switch (tsk
->state
) {
3723 stats
->nr_running
++;
3725 case TASK_INTERRUPTIBLE
:
3726 stats
->nr_sleeping
++;
3728 case TASK_UNINTERRUPTIBLE
:
3729 stats
->nr_uninterruptible
++;
3732 stats
->nr_stopped
++;
3735 if (delayacct_is_task_waiting_on_io(tsk
))
3736 stats
->nr_io_wait
++;
3740 css_task_iter_end(&it
);
3748 * seq_file methods for the tasks/procs files. The seq_file position is the
3749 * next pid to display; the seq_file iterator is a pointer to the pid
3750 * in the cgroup->l->list array.
3753 static void *cgroup_pidlist_start(struct seq_file
*s
, loff_t
*pos
)
3756 * Initially we receive a position value that corresponds to
3757 * one more than the last pid shown (or 0 on the first call or
3758 * after a seek to the start). Use a binary-search to find the
3759 * next pid to display, if any
3761 struct cgroup_pidlist
*l
= s
->private;
3762 int index
= 0, pid
= *pos
;
3765 down_read(&l
->rwsem
);
3767 int end
= l
->length
;
3769 while (index
< end
) {
3770 int mid
= (index
+ end
) / 2;
3771 if (l
->list
[mid
] == pid
) {
3774 } else if (l
->list
[mid
] <= pid
)
3780 /* If we're off the end of the array, we're done */
3781 if (index
>= l
->length
)
3783 /* Update the abstract position to be the actual pid that we found */
3784 iter
= l
->list
+ index
;
3789 static void cgroup_pidlist_stop(struct seq_file
*s
, void *v
)
3791 struct cgroup_pidlist
*l
= s
->private;
3795 static void *cgroup_pidlist_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
3797 struct cgroup_pidlist
*l
= s
->private;
3799 pid_t
*end
= l
->list
+ l
->length
;
3801 * Advance to the next pid in the array. If this goes off the
3813 static int cgroup_pidlist_show(struct seq_file
*s
, void *v
)
3815 return seq_printf(s
, "%d\n", *(int *)v
);
3819 * seq_operations functions for iterating on pidlists through seq_file -
3820 * independent of whether it's tasks or procs
3822 static const struct seq_operations cgroup_pidlist_seq_operations
= {
3823 .start
= cgroup_pidlist_start
,
3824 .stop
= cgroup_pidlist_stop
,
3825 .next
= cgroup_pidlist_next
,
3826 .show
= cgroup_pidlist_show
,
3829 static void cgroup_release_pid_array(struct cgroup_pidlist
*l
)
3832 * the case where we're the last user of this particular pidlist will
3833 * have us remove it from the cgroup's list, which entails taking the
3834 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3835 * pidlist_mutex, we have to take pidlist_mutex first.
3837 mutex_lock(&l
->owner
->pidlist_mutex
);
3838 down_write(&l
->rwsem
);
3839 BUG_ON(!l
->use_count
);
3840 if (!--l
->use_count
) {
3841 /* we're the last user if refcount is 0; remove and free */
3842 list_del(&l
->links
);
3843 mutex_unlock(&l
->owner
->pidlist_mutex
);
3844 pidlist_free(l
->list
);
3845 put_pid_ns(l
->key
.ns
);
3846 up_write(&l
->rwsem
);
3850 mutex_unlock(&l
->owner
->pidlist_mutex
);
3851 up_write(&l
->rwsem
);
3854 static int cgroup_pidlist_release(struct inode
*inode
, struct file
*file
)
3856 struct cgroup_pidlist
*l
;
3857 if (!(file
->f_mode
& FMODE_READ
))
3860 * the seq_file will only be initialized if the file was opened for
3861 * reading; hence we check if it's not null only in that case.
3863 l
= ((struct seq_file
*)file
->private_data
)->private;
3864 cgroup_release_pid_array(l
);
3865 return seq_release(inode
, file
);
3868 static const struct file_operations cgroup_pidlist_operations
= {
3870 .llseek
= seq_lseek
,
3871 .write
= cgroup_file_write
,
3872 .release
= cgroup_pidlist_release
,
3876 * The following functions handle opens on a file that displays a pidlist
3877 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3880 /* helper function for the two below it */
3881 static int cgroup_pidlist_open(struct file
*file
, enum cgroup_filetype type
)
3883 struct cgroup
*cgrp
= __d_cgrp(file
->f_dentry
->d_parent
);
3884 struct cgroup_pidlist
*l
;
3887 /* Nothing to do for write-only files */
3888 if (!(file
->f_mode
& FMODE_READ
))
3891 /* have the array populated */
3892 retval
= pidlist_array_load(cgrp
, type
, &l
);
3895 /* configure file information */
3896 file
->f_op
= &cgroup_pidlist_operations
;
3898 retval
= seq_open(file
, &cgroup_pidlist_seq_operations
);
3900 cgroup_release_pid_array(l
);
3903 ((struct seq_file
*)file
->private_data
)->private = l
;
3906 static int cgroup_tasks_open(struct inode
*unused
, struct file
*file
)
3908 return cgroup_pidlist_open(file
, CGROUP_FILE_TASKS
);
3910 static int cgroup_procs_open(struct inode
*unused
, struct file
*file
)
3912 return cgroup_pidlist_open(file
, CGROUP_FILE_PROCS
);
3915 static u64
cgroup_read_notify_on_release(struct cgroup_subsys_state
*css
,
3918 return notify_on_release(css
->cgroup
);
3921 static int cgroup_write_notify_on_release(struct cgroup_subsys_state
*css
,
3922 struct cftype
*cft
, u64 val
)
3924 clear_bit(CGRP_RELEASABLE
, &css
->cgroup
->flags
);
3926 set_bit(CGRP_NOTIFY_ON_RELEASE
, &css
->cgroup
->flags
);
3928 clear_bit(CGRP_NOTIFY_ON_RELEASE
, &css
->cgroup
->flags
);
3933 * When dput() is called asynchronously, if umount has been done and
3934 * then deactivate_super() in cgroup_free_fn() kills the superblock,
3935 * there's a small window that vfs will see the root dentry with non-zero
3936 * refcnt and trigger BUG().
3938 * That's why we hold a reference before dput() and drop it right after.
3940 static void cgroup_dput(struct cgroup
*cgrp
)
3942 struct super_block
*sb
= cgrp
->root
->sb
;
3944 atomic_inc(&sb
->s_active
);
3946 deactivate_super(sb
);
3950 * Unregister event and free resources.
3952 * Gets called from workqueue.
3954 static void cgroup_event_remove(struct work_struct
*work
)
3956 struct cgroup_event
*event
= container_of(work
, struct cgroup_event
,
3958 struct cgroup
*cgrp
= event
->cgrp
;
3960 remove_wait_queue(event
->wqh
, &event
->wait
);
3962 event
->cft
->unregister_event(cgrp
, event
->cft
, event
->eventfd
);
3964 /* Notify userspace the event is going away. */
3965 eventfd_signal(event
->eventfd
, 1);
3967 eventfd_ctx_put(event
->eventfd
);
3973 * Gets called on POLLHUP on eventfd when user closes it.
3975 * Called with wqh->lock held and interrupts disabled.
3977 static int cgroup_event_wake(wait_queue_t
*wait
, unsigned mode
,
3978 int sync
, void *key
)
3980 struct cgroup_event
*event
= container_of(wait
,
3981 struct cgroup_event
, wait
);
3982 struct cgroup
*cgrp
= event
->cgrp
;
3983 unsigned long flags
= (unsigned long)key
;
3985 if (flags
& POLLHUP
) {
3987 * If the event has been detached at cgroup removal, we
3988 * can simply return knowing the other side will cleanup
3991 * We can't race against event freeing since the other
3992 * side will require wqh->lock via remove_wait_queue(),
3995 spin_lock(&cgrp
->event_list_lock
);
3996 if (!list_empty(&event
->list
)) {
3997 list_del_init(&event
->list
);
3999 * We are in atomic context, but cgroup_event_remove()
4000 * may sleep, so we have to call it in workqueue.
4002 schedule_work(&event
->remove
);
4004 spin_unlock(&cgrp
->event_list_lock
);
4010 static void cgroup_event_ptable_queue_proc(struct file
*file
,
4011 wait_queue_head_t
*wqh
, poll_table
*pt
)
4013 struct cgroup_event
*event
= container_of(pt
,
4014 struct cgroup_event
, pt
);
4017 add_wait_queue(wqh
, &event
->wait
);
4021 * Parse input and register new cgroup event handler.
4023 * Input must be in format '<event_fd> <control_fd> <args>'.
4024 * Interpretation of args is defined by control file implementation.
4026 static int cgroup_write_event_control(struct cgroup_subsys_state
*css
,
4027 struct cftype
*cft
, const char *buffer
)
4029 struct cgroup
*cgrp
= css
->cgroup
;
4030 struct cgroup_event
*event
;
4031 struct cgroup
*cgrp_cfile
;
4032 unsigned int efd
, cfd
;
4038 efd
= simple_strtoul(buffer
, &endp
, 10);
4043 cfd
= simple_strtoul(buffer
, &endp
, 10);
4044 if ((*endp
!= ' ') && (*endp
!= '\0'))
4048 event
= kzalloc(sizeof(*event
), GFP_KERNEL
);
4052 INIT_LIST_HEAD(&event
->list
);
4053 init_poll_funcptr(&event
->pt
, cgroup_event_ptable_queue_proc
);
4054 init_waitqueue_func_entry(&event
->wait
, cgroup_event_wake
);
4055 INIT_WORK(&event
->remove
, cgroup_event_remove
);
4057 efile
= eventfd_fget(efd
);
4058 if (IS_ERR(efile
)) {
4059 ret
= PTR_ERR(efile
);
4063 event
->eventfd
= eventfd_ctx_fileget(efile
);
4064 if (IS_ERR(event
->eventfd
)) {
4065 ret
= PTR_ERR(event
->eventfd
);
4072 goto out_put_eventfd
;
4075 /* the process need read permission on control file */
4076 /* AV: shouldn't we check that it's been opened for read instead? */
4077 ret
= inode_permission(file_inode(cfile
), MAY_READ
);
4081 event
->cft
= __file_cft(cfile
);
4082 if (IS_ERR(event
->cft
)) {
4083 ret
= PTR_ERR(event
->cft
);
4088 * The file to be monitored must be in the same cgroup as
4089 * cgroup.event_control is.
4091 cgrp_cfile
= __d_cgrp(cfile
->f_dentry
->d_parent
);
4092 if (cgrp_cfile
!= cgrp
) {
4097 if (!event
->cft
->register_event
|| !event
->cft
->unregister_event
) {
4102 ret
= event
->cft
->register_event(cgrp
, event
->cft
,
4103 event
->eventfd
, buffer
);
4107 efile
->f_op
->poll(efile
, &event
->pt
);
4110 * Events should be removed after rmdir of cgroup directory, but before
4111 * destroying subsystem state objects. Let's take reference to cgroup
4112 * directory dentry to do that.
4116 spin_lock(&cgrp
->event_list_lock
);
4117 list_add(&event
->list
, &cgrp
->event_list
);
4118 spin_unlock(&cgrp
->event_list_lock
);
4128 eventfd_ctx_put(event
->eventfd
);
4137 static u64
cgroup_clone_children_read(struct cgroup_subsys_state
*css
,
4140 return test_bit(CGRP_CPUSET_CLONE_CHILDREN
, &css
->cgroup
->flags
);
4143 static int cgroup_clone_children_write(struct cgroup_subsys_state
*css
,
4144 struct cftype
*cft
, u64 val
)
4147 set_bit(CGRP_CPUSET_CLONE_CHILDREN
, &css
->cgroup
->flags
);
4149 clear_bit(CGRP_CPUSET_CLONE_CHILDREN
, &css
->cgroup
->flags
);
4153 static struct cftype cgroup_base_files
[] = {
4155 .name
= "cgroup.procs",
4156 .open
= cgroup_procs_open
,
4157 .write_u64
= cgroup_procs_write
,
4158 .release
= cgroup_pidlist_release
,
4159 .mode
= S_IRUGO
| S_IWUSR
,
4162 .name
= "cgroup.event_control",
4163 .write_string
= cgroup_write_event_control
,
4167 .name
= "cgroup.clone_children",
4168 .flags
= CFTYPE_INSANE
,
4169 .read_u64
= cgroup_clone_children_read
,
4170 .write_u64
= cgroup_clone_children_write
,
4173 .name
= "cgroup.sane_behavior",
4174 .flags
= CFTYPE_ONLY_ON_ROOT
,
4175 .read_seq_string
= cgroup_sane_behavior_show
,
4179 * Historical crazy stuff. These don't have "cgroup." prefix and
4180 * don't exist if sane_behavior. If you're depending on these, be
4181 * prepared to be burned.
4185 .flags
= CFTYPE_INSANE
, /* use "procs" instead */
4186 .open
= cgroup_tasks_open
,
4187 .write_u64
= cgroup_tasks_write
,
4188 .release
= cgroup_pidlist_release
,
4189 .mode
= S_IRUGO
| S_IWUSR
,
4192 .name
= "notify_on_release",
4193 .flags
= CFTYPE_INSANE
,
4194 .read_u64
= cgroup_read_notify_on_release
,
4195 .write_u64
= cgroup_write_notify_on_release
,
4198 .name
= "release_agent",
4199 .flags
= CFTYPE_INSANE
| CFTYPE_ONLY_ON_ROOT
,
4200 .read_seq_string
= cgroup_release_agent_show
,
4201 .write_string
= cgroup_release_agent_write
,
4202 .max_write_len
= PATH_MAX
,
4208 * cgroup_populate_dir - create subsys files in a cgroup directory
4209 * @cgrp: target cgroup
4210 * @subsys_mask: mask of the subsystem ids whose files should be added
4212 * On failure, no file is added.
4214 static int cgroup_populate_dir(struct cgroup
*cgrp
, unsigned long subsys_mask
)
4216 struct cgroup_subsys
*ss
;
4219 /* process cftsets of each subsystem */
4220 for_each_subsys(ss
, i
) {
4221 struct cftype_set
*set
;
4223 if (!test_bit(i
, &subsys_mask
))
4226 list_for_each_entry(set
, &ss
->cftsets
, node
) {
4227 ret
= cgroup_addrm_files(cgrp
, set
->cfts
, true);
4233 /* This cgroup is ready now */
4234 for_each_root_subsys(cgrp
->root
, ss
) {
4235 struct cgroup_subsys_state
*css
= cgrp
->subsys
[ss
->subsys_id
];
4236 struct css_id
*id
= rcu_dereference_protected(css
->id
, true);
4239 * Update id->css pointer and make this css visible from
4240 * CSS ID functions. This pointer will be dereferened
4241 * from RCU-read-side without locks.
4244 rcu_assign_pointer(id
->css
, css
);
4249 cgroup_clear_dir(cgrp
, subsys_mask
);
4253 static void css_dput_fn(struct work_struct
*work
)
4255 struct cgroup_subsys_state
*css
=
4256 container_of(work
, struct cgroup_subsys_state
, dput_work
);
4258 cgroup_dput(css
->cgroup
);
4261 static void css_release(struct percpu_ref
*ref
)
4263 struct cgroup_subsys_state
*css
=
4264 container_of(ref
, struct cgroup_subsys_state
, refcnt
);
4266 schedule_work(&css
->dput_work
);
4269 static void init_cgroup_css(struct cgroup_subsys_state
*css
,
4270 struct cgroup_subsys
*ss
,
4271 struct cgroup
*cgrp
)
4277 if (cgrp
== cgroup_dummy_top
)
4278 css
->flags
|= CSS_ROOT
;
4279 BUG_ON(cgrp
->subsys
[ss
->subsys_id
]);
4280 cgrp
->subsys
[ss
->subsys_id
] = css
;
4283 * css holds an extra ref to @cgrp->dentry which is put on the last
4284 * css_put(). dput() requires process context, which css_put() may
4285 * be called without. @css->dput_work will be used to invoke
4286 * dput() asynchronously from css_put().
4288 INIT_WORK(&css
->dput_work
, css_dput_fn
);
4291 /* invoke ->css_online() on a new CSS and mark it online if successful */
4292 static int online_css(struct cgroup_subsys
*ss
, struct cgroup
*cgrp
)
4294 struct cgroup_subsys_state
*css
= cgrp
->subsys
[ss
->subsys_id
];
4297 lockdep_assert_held(&cgroup_mutex
);
4300 ret
= ss
->css_online(css
);
4302 css
->flags
|= CSS_ONLINE
;
4306 /* if the CSS is online, invoke ->css_offline() on it and mark it offline */
4307 static void offline_css(struct cgroup_subsys
*ss
, struct cgroup
*cgrp
)
4309 struct cgroup_subsys_state
*css
= cgrp
->subsys
[ss
->subsys_id
];
4311 lockdep_assert_held(&cgroup_mutex
);
4313 if (!(css
->flags
& CSS_ONLINE
))
4316 if (ss
->css_offline
)
4317 ss
->css_offline(css
);
4319 css
->flags
&= ~CSS_ONLINE
;
4323 * cgroup_create - create a cgroup
4324 * @parent: cgroup that will be parent of the new cgroup
4325 * @dentry: dentry of the new cgroup
4326 * @mode: mode to set on new inode
4328 * Must be called with the mutex on the parent inode held
4330 static long cgroup_create(struct cgroup
*parent
, struct dentry
*dentry
,
4333 struct cgroup
*cgrp
;
4334 struct cgroup_name
*name
;
4335 struct cgroupfs_root
*root
= parent
->root
;
4337 struct cgroup_subsys
*ss
;
4338 struct super_block
*sb
= root
->sb
;
4340 /* allocate the cgroup and its ID, 0 is reserved for the root */
4341 cgrp
= kzalloc(sizeof(*cgrp
), GFP_KERNEL
);
4345 name
= cgroup_alloc_name(dentry
);
4348 rcu_assign_pointer(cgrp
->name
, name
);
4351 * Temporarily set the pointer to NULL, so idr_find() won't return
4352 * a half-baked cgroup.
4354 cgrp
->id
= idr_alloc(&root
->cgroup_idr
, NULL
, 1, 0, GFP_KERNEL
);
4359 * Only live parents can have children. Note that the liveliness
4360 * check isn't strictly necessary because cgroup_mkdir() and
4361 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4362 * anyway so that locking is contained inside cgroup proper and we
4363 * don't get nasty surprises if we ever grow another caller.
4365 if (!cgroup_lock_live_group(parent
)) {
4370 /* Grab a reference on the superblock so the hierarchy doesn't
4371 * get deleted on unmount if there are child cgroups. This
4372 * can be done outside cgroup_mutex, since the sb can't
4373 * disappear while someone has an open control file on the
4375 atomic_inc(&sb
->s_active
);
4377 init_cgroup_housekeeping(cgrp
);
4379 dentry
->d_fsdata
= cgrp
;
4380 cgrp
->dentry
= dentry
;
4382 cgrp
->parent
= parent
;
4383 cgrp
->root
= parent
->root
;
4385 if (notify_on_release(parent
))
4386 set_bit(CGRP_NOTIFY_ON_RELEASE
, &cgrp
->flags
);
4388 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN
, &parent
->flags
))
4389 set_bit(CGRP_CPUSET_CLONE_CHILDREN
, &cgrp
->flags
);
4391 for_each_root_subsys(root
, ss
) {
4392 struct cgroup_subsys_state
*css
;
4394 css
= ss
->css_alloc(parent
->subsys
[ss
->subsys_id
]);
4400 err
= percpu_ref_init(&css
->refcnt
, css_release
);
4406 init_cgroup_css(css
, ss
, cgrp
);
4409 err
= alloc_css_id(ss
, parent
, cgrp
);
4416 * Create directory. cgroup_create_file() returns with the new
4417 * directory locked on success so that it can be populated without
4418 * dropping cgroup_mutex.
4420 err
= cgroup_create_file(dentry
, S_IFDIR
| mode
, sb
);
4423 lockdep_assert_held(&dentry
->d_inode
->i_mutex
);
4425 cgrp
->serial_nr
= cgroup_serial_nr_next
++;
4427 /* allocation complete, commit to creation */
4428 list_add_tail_rcu(&cgrp
->sibling
, &cgrp
->parent
->children
);
4429 root
->number_of_cgroups
++;
4431 /* each css holds a ref to the cgroup's dentry */
4432 for_each_root_subsys(root
, ss
)
4435 /* hold a ref to the parent's dentry */
4436 dget(parent
->dentry
);
4438 /* creation succeeded, notify subsystems */
4439 for_each_root_subsys(root
, ss
) {
4440 err
= online_css(ss
, cgrp
);
4444 if (ss
->broken_hierarchy
&& !ss
->warned_broken_hierarchy
&&
4446 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",
4447 current
->comm
, current
->pid
, ss
->name
);
4448 if (!strcmp(ss
->name
, "memory"))
4449 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4450 ss
->warned_broken_hierarchy
= true;
4454 idr_replace(&root
->cgroup_idr
, cgrp
, cgrp
->id
);
4456 err
= cgroup_addrm_files(cgrp
, cgroup_base_files
, true);
4460 err
= cgroup_populate_dir(cgrp
, root
->subsys_mask
);
4464 mutex_unlock(&cgroup_mutex
);
4465 mutex_unlock(&cgrp
->dentry
->d_inode
->i_mutex
);
4470 for_each_root_subsys(root
, ss
) {
4471 struct cgroup_subsys_state
*css
= cgrp
->subsys
[ss
->subsys_id
];
4474 percpu_ref_cancel_init(&css
->refcnt
);
4478 mutex_unlock(&cgroup_mutex
);
4479 /* Release the reference count that we took on the superblock */
4480 deactivate_super(sb
);
4482 idr_remove(&root
->cgroup_idr
, cgrp
->id
);
4484 kfree(rcu_dereference_raw(cgrp
->name
));
4490 cgroup_destroy_locked(cgrp
);
4491 mutex_unlock(&cgroup_mutex
);
4492 mutex_unlock(&dentry
->d_inode
->i_mutex
);
4496 static int cgroup_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
4498 struct cgroup
*c_parent
= dentry
->d_parent
->d_fsdata
;
4500 /* the vfs holds inode->i_mutex already */
4501 return cgroup_create(c_parent
, dentry
, mode
| S_IFDIR
);
4504 static void cgroup_css_killed(struct cgroup
*cgrp
)
4506 if (!atomic_dec_and_test(&cgrp
->css_kill_cnt
))
4509 /* percpu ref's of all css's are killed, kick off the next step */
4510 INIT_WORK(&cgrp
->destroy_work
, cgroup_offline_fn
);
4511 schedule_work(&cgrp
->destroy_work
);
4514 static void css_ref_killed_fn(struct percpu_ref
*ref
)
4516 struct cgroup_subsys_state
*css
=
4517 container_of(ref
, struct cgroup_subsys_state
, refcnt
);
4519 cgroup_css_killed(css
->cgroup
);
4523 * cgroup_destroy_locked - the first stage of cgroup destruction
4524 * @cgrp: cgroup to be destroyed
4526 * css's make use of percpu refcnts whose killing latency shouldn't be
4527 * exposed to userland and are RCU protected. Also, cgroup core needs to
4528 * guarantee that css_tryget() won't succeed by the time ->css_offline() is
4529 * invoked. To satisfy all the requirements, destruction is implemented in
4530 * the following two steps.
4532 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4533 * userland visible parts and start killing the percpu refcnts of
4534 * css's. Set up so that the next stage will be kicked off once all
4535 * the percpu refcnts are confirmed to be killed.
4537 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4538 * rest of destruction. Once all cgroup references are gone, the
4539 * cgroup is RCU-freed.
4541 * This function implements s1. After this step, @cgrp is gone as far as
4542 * the userland is concerned and a new cgroup with the same name may be
4543 * created. As cgroup doesn't care about the names internally, this
4544 * doesn't cause any problem.
4546 static int cgroup_destroy_locked(struct cgroup
*cgrp
)
4547 __releases(&cgroup_mutex
) __acquires(&cgroup_mutex
)
4549 struct dentry
*d
= cgrp
->dentry
;
4550 struct cgroup_event
*event
, *tmp
;
4551 struct cgroup_subsys
*ss
;
4554 lockdep_assert_held(&d
->d_inode
->i_mutex
);
4555 lockdep_assert_held(&cgroup_mutex
);
4558 * css_set_lock synchronizes access to ->cset_links and prevents
4559 * @cgrp from being removed while __put_css_set() is in progress.
4561 read_lock(&css_set_lock
);
4562 empty
= list_empty(&cgrp
->cset_links
) && list_empty(&cgrp
->children
);
4563 read_unlock(&css_set_lock
);
4568 * Block new css_tryget() by killing css refcnts. cgroup core
4569 * guarantees that, by the time ->css_offline() is invoked, no new
4570 * css reference will be given out via css_tryget(). We can't
4571 * simply call percpu_ref_kill() and proceed to offlining css's
4572 * because percpu_ref_kill() doesn't guarantee that the ref is seen
4573 * as killed on all CPUs on return.
4575 * Use percpu_ref_kill_and_confirm() to get notifications as each
4576 * css is confirmed to be seen as killed on all CPUs. The
4577 * notification callback keeps track of the number of css's to be
4578 * killed and schedules cgroup_offline_fn() to perform the rest of
4579 * destruction once the percpu refs of all css's are confirmed to
4582 atomic_set(&cgrp
->css_kill_cnt
, 1);
4583 for_each_root_subsys(cgrp
->root
, ss
) {
4584 struct cgroup_subsys_state
*css
= cgrp
->subsys
[ss
->subsys_id
];
4587 * Killing would put the base ref, but we need to keep it
4588 * alive until after ->css_offline.
4590 percpu_ref_get(&css
->refcnt
);
4592 atomic_inc(&cgrp
->css_kill_cnt
);
4593 percpu_ref_kill_and_confirm(&css
->refcnt
, css_ref_killed_fn
);
4595 cgroup_css_killed(cgrp
);
4598 * Mark @cgrp dead. This prevents further task migration and child
4599 * creation by disabling cgroup_lock_live_group(). Note that
4600 * CGRP_DEAD assertion is depended upon by css_next_child() to
4601 * resume iteration after dropping RCU read lock. See
4602 * css_next_child() for details.
4604 set_bit(CGRP_DEAD
, &cgrp
->flags
);
4606 /* CGRP_DEAD is set, remove from ->release_list for the last time */
4607 raw_spin_lock(&release_list_lock
);
4608 if (!list_empty(&cgrp
->release_list
))
4609 list_del_init(&cgrp
->release_list
);
4610 raw_spin_unlock(&release_list_lock
);
4613 * Clear and remove @cgrp directory. The removal puts the base ref
4614 * but we aren't quite done with @cgrp yet, so hold onto it.
4616 cgroup_clear_dir(cgrp
, cgrp
->root
->subsys_mask
);
4617 cgroup_addrm_files(cgrp
, cgroup_base_files
, false);
4619 cgroup_d_remove_dir(d
);
4622 * Unregister events and notify userspace.
4623 * Notify userspace about cgroup removing only after rmdir of cgroup
4624 * directory to avoid race between userspace and kernelspace.
4626 spin_lock(&cgrp
->event_list_lock
);
4627 list_for_each_entry_safe(event
, tmp
, &cgrp
->event_list
, list
) {
4628 list_del_init(&event
->list
);
4629 schedule_work(&event
->remove
);
4631 spin_unlock(&cgrp
->event_list_lock
);
4637 * cgroup_offline_fn - the second step of cgroup destruction
4638 * @work: cgroup->destroy_free_work
4640 * This function is invoked from a work item for a cgroup which is being
4641 * destroyed after the percpu refcnts of all css's are guaranteed to be
4642 * seen as killed on all CPUs, and performs the rest of destruction. This
4643 * is the second step of destruction described in the comment above
4644 * cgroup_destroy_locked().
4646 static void cgroup_offline_fn(struct work_struct
*work
)
4648 struct cgroup
*cgrp
= container_of(work
, struct cgroup
, destroy_work
);
4649 struct cgroup
*parent
= cgrp
->parent
;
4650 struct dentry
*d
= cgrp
->dentry
;
4651 struct cgroup_subsys
*ss
;
4653 mutex_lock(&cgroup_mutex
);
4656 * css_tryget() is guaranteed to fail now. Tell subsystems to
4657 * initate destruction.
4659 for_each_root_subsys(cgrp
->root
, ss
)
4660 offline_css(ss
, cgrp
);
4663 * Put the css refs from cgroup_destroy_locked(). Each css holds
4664 * an extra reference to the cgroup's dentry and cgroup removal
4665 * proceeds regardless of css refs. On the last put of each css,
4666 * whenever that may be, the extra dentry ref is put so that dentry
4667 * destruction happens only after all css's are released.
4669 for_each_root_subsys(cgrp
->root
, ss
)
4670 css_put(cgrp
->subsys
[ss
->subsys_id
]);
4672 /* delete this cgroup from parent->children */
4673 list_del_rcu(&cgrp
->sibling
);
4676 * We should remove the cgroup object from idr before its grace
4677 * period starts, so we won't be looking up a cgroup while the
4678 * cgroup is being freed.
4680 idr_remove(&cgrp
->root
->cgroup_idr
, cgrp
->id
);
4685 set_bit(CGRP_RELEASABLE
, &parent
->flags
);
4686 check_for_release(parent
);
4688 mutex_unlock(&cgroup_mutex
);
4691 static int cgroup_rmdir(struct inode
*unused_dir
, struct dentry
*dentry
)
4695 mutex_lock(&cgroup_mutex
);
4696 ret
= cgroup_destroy_locked(dentry
->d_fsdata
);
4697 mutex_unlock(&cgroup_mutex
);
4702 static void __init_or_module
cgroup_init_cftsets(struct cgroup_subsys
*ss
)
4704 INIT_LIST_HEAD(&ss
->cftsets
);
4707 * base_cftset is embedded in subsys itself, no need to worry about
4710 if (ss
->base_cftypes
) {
4713 for (cft
= ss
->base_cftypes
; cft
->name
[0] != '\0'; cft
++)
4716 ss
->base_cftset
.cfts
= ss
->base_cftypes
;
4717 list_add_tail(&ss
->base_cftset
.node
, &ss
->cftsets
);
4721 static void __init
cgroup_init_subsys(struct cgroup_subsys
*ss
)
4723 struct cgroup_subsys_state
*css
;
4725 printk(KERN_INFO
"Initializing cgroup subsys %s\n", ss
->name
);
4727 mutex_lock(&cgroup_mutex
);
4729 /* init base cftset */
4730 cgroup_init_cftsets(ss
);
4732 /* Create the top cgroup state for this subsystem */
4733 list_add(&ss
->sibling
, &cgroup_dummy_root
.subsys_list
);
4734 ss
->root
= &cgroup_dummy_root
;
4735 css
= ss
->css_alloc(cgroup_dummy_top
->subsys
[ss
->subsys_id
]);
4736 /* We don't handle early failures gracefully */
4737 BUG_ON(IS_ERR(css
));
4738 init_cgroup_css(css
, ss
, cgroup_dummy_top
);
4740 /* Update the init_css_set to contain a subsys
4741 * pointer to this state - since the subsystem is
4742 * newly registered, all tasks and hence the
4743 * init_css_set is in the subsystem's top cgroup. */
4744 init_css_set
.subsys
[ss
->subsys_id
] = css
;
4746 need_forkexit_callback
|= ss
->fork
|| ss
->exit
;
4748 /* At system boot, before all subsystems have been
4749 * registered, no tasks have been forked, so we don't
4750 * need to invoke fork callbacks here. */
4751 BUG_ON(!list_empty(&init_task
.tasks
));
4753 BUG_ON(online_css(ss
, cgroup_dummy_top
));
4755 mutex_unlock(&cgroup_mutex
);
4757 /* this function shouldn't be used with modular subsystems, since they
4758 * need to register a subsys_id, among other things */
4763 * cgroup_load_subsys: load and register a modular subsystem at runtime
4764 * @ss: the subsystem to load
4766 * This function should be called in a modular subsystem's initcall. If the
4767 * subsystem is built as a module, it will be assigned a new subsys_id and set
4768 * up for use. If the subsystem is built-in anyway, work is delegated to the
4769 * simpler cgroup_init_subsys.
4771 int __init_or_module
cgroup_load_subsys(struct cgroup_subsys
*ss
)
4773 struct cgroup_subsys_state
*css
;
4775 struct hlist_node
*tmp
;
4776 struct css_set
*cset
;
4779 /* check name and function validity */
4780 if (ss
->name
== NULL
|| strlen(ss
->name
) > MAX_CGROUP_TYPE_NAMELEN
||
4781 ss
->css_alloc
== NULL
|| ss
->css_free
== NULL
)
4785 * we don't support callbacks in modular subsystems. this check is
4786 * before the ss->module check for consistency; a subsystem that could
4787 * be a module should still have no callbacks even if the user isn't
4788 * compiling it as one.
4790 if (ss
->fork
|| ss
->exit
)
4794 * an optionally modular subsystem is built-in: we want to do nothing,
4795 * since cgroup_init_subsys will have already taken care of it.
4797 if (ss
->module
== NULL
) {
4798 /* a sanity check */
4799 BUG_ON(cgroup_subsys
[ss
->subsys_id
] != ss
);
4803 /* init base cftset */
4804 cgroup_init_cftsets(ss
);
4806 mutex_lock(&cgroup_mutex
);
4807 cgroup_subsys
[ss
->subsys_id
] = ss
;
4810 * no ss->css_alloc seems to need anything important in the ss
4811 * struct, so this can happen first (i.e. before the dummy root
4814 css
= ss
->css_alloc(cgroup_dummy_top
->subsys
[ss
->subsys_id
]);
4816 /* failure case - need to deassign the cgroup_subsys[] slot. */
4817 cgroup_subsys
[ss
->subsys_id
] = NULL
;
4818 mutex_unlock(&cgroup_mutex
);
4819 return PTR_ERR(css
);
4822 list_add(&ss
->sibling
, &cgroup_dummy_root
.subsys_list
);
4823 ss
->root
= &cgroup_dummy_root
;
4825 /* our new subsystem will be attached to the dummy hierarchy. */
4826 init_cgroup_css(css
, ss
, cgroup_dummy_top
);
4827 /* init_idr must be after init_cgroup_css because it sets css->id. */
4829 ret
= cgroup_init_idr(ss
, css
);
4835 * Now we need to entangle the css into the existing css_sets. unlike
4836 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4837 * will need a new pointer to it; done by iterating the css_set_table.
4838 * furthermore, modifying the existing css_sets will corrupt the hash
4839 * table state, so each changed css_set will need its hash recomputed.
4840 * this is all done under the css_set_lock.
4842 write_lock(&css_set_lock
);
4843 hash_for_each_safe(css_set_table
, i
, tmp
, cset
, hlist
) {
4844 /* skip entries that we already rehashed */
4845 if (cset
->subsys
[ss
->subsys_id
])
4847 /* remove existing entry */
4848 hash_del(&cset
->hlist
);
4850 cset
->subsys
[ss
->subsys_id
] = css
;
4851 /* recompute hash and restore entry */
4852 key
= css_set_hash(cset
->subsys
);
4853 hash_add(css_set_table
, &cset
->hlist
, key
);
4855 write_unlock(&css_set_lock
);
4857 ret
= online_css(ss
, cgroup_dummy_top
);
4862 mutex_unlock(&cgroup_mutex
);
4866 mutex_unlock(&cgroup_mutex
);
4867 /* @ss can't be mounted here as try_module_get() would fail */
4868 cgroup_unload_subsys(ss
);
4871 EXPORT_SYMBOL_GPL(cgroup_load_subsys
);
4874 * cgroup_unload_subsys: unload a modular subsystem
4875 * @ss: the subsystem to unload
4877 * This function should be called in a modular subsystem's exitcall. When this
4878 * function is invoked, the refcount on the subsystem's module will be 0, so
4879 * the subsystem will not be attached to any hierarchy.
4881 void cgroup_unload_subsys(struct cgroup_subsys
*ss
)
4883 struct cgrp_cset_link
*link
;
4885 BUG_ON(ss
->module
== NULL
);
4888 * we shouldn't be called if the subsystem is in use, and the use of
4889 * try_module_get() in rebind_subsystems() should ensure that it
4890 * doesn't start being used while we're killing it off.
4892 BUG_ON(ss
->root
!= &cgroup_dummy_root
);
4894 mutex_lock(&cgroup_mutex
);
4896 offline_css(ss
, cgroup_dummy_top
);
4899 idr_destroy(&ss
->idr
);
4901 /* deassign the subsys_id */
4902 cgroup_subsys
[ss
->subsys_id
] = NULL
;
4904 /* remove subsystem from the dummy root's list of subsystems */
4905 list_del_init(&ss
->sibling
);
4908 * disentangle the css from all css_sets attached to the dummy
4909 * top. as in loading, we need to pay our respects to the hashtable
4912 write_lock(&css_set_lock
);
4913 list_for_each_entry(link
, &cgroup_dummy_top
->cset_links
, cset_link
) {
4914 struct css_set
*cset
= link
->cset
;
4917 hash_del(&cset
->hlist
);
4918 cset
->subsys
[ss
->subsys_id
] = NULL
;
4919 key
= css_set_hash(cset
->subsys
);
4920 hash_add(css_set_table
, &cset
->hlist
, key
);
4922 write_unlock(&css_set_lock
);
4925 * remove subsystem's css from the cgroup_dummy_top and free it -
4926 * need to free before marking as null because ss->css_free needs
4927 * the cgrp->subsys pointer to find their state. note that this
4928 * also takes care of freeing the css_id.
4930 ss
->css_free(cgroup_dummy_top
->subsys
[ss
->subsys_id
]);
4931 cgroup_dummy_top
->subsys
[ss
->subsys_id
] = NULL
;
4933 mutex_unlock(&cgroup_mutex
);
4935 EXPORT_SYMBOL_GPL(cgroup_unload_subsys
);
4938 * cgroup_init_early - cgroup initialization at system boot
4940 * Initialize cgroups at system boot, and initialize any
4941 * subsystems that request early init.
4943 int __init
cgroup_init_early(void)
4945 struct cgroup_subsys
*ss
;
4948 atomic_set(&init_css_set
.refcount
, 1);
4949 INIT_LIST_HEAD(&init_css_set
.cgrp_links
);
4950 INIT_LIST_HEAD(&init_css_set
.tasks
);
4951 INIT_HLIST_NODE(&init_css_set
.hlist
);
4953 init_cgroup_root(&cgroup_dummy_root
);
4954 cgroup_root_count
= 1;
4955 RCU_INIT_POINTER(init_task
.cgroups
, &init_css_set
);
4957 init_cgrp_cset_link
.cset
= &init_css_set
;
4958 init_cgrp_cset_link
.cgrp
= cgroup_dummy_top
;
4959 list_add(&init_cgrp_cset_link
.cset_link
, &cgroup_dummy_top
->cset_links
);
4960 list_add(&init_cgrp_cset_link
.cgrp_link
, &init_css_set
.cgrp_links
);
4962 /* at bootup time, we don't worry about modular subsystems */
4963 for_each_builtin_subsys(ss
, i
) {
4965 BUG_ON(strlen(ss
->name
) > MAX_CGROUP_TYPE_NAMELEN
);
4966 BUG_ON(!ss
->css_alloc
);
4967 BUG_ON(!ss
->css_free
);
4968 if (ss
->subsys_id
!= i
) {
4969 printk(KERN_ERR
"cgroup: Subsys %s id == %d\n",
4970 ss
->name
, ss
->subsys_id
);
4975 cgroup_init_subsys(ss
);
4981 * cgroup_init - cgroup initialization
4983 * Register cgroup filesystem and /proc file, and initialize
4984 * any subsystems that didn't request early init.
4986 int __init
cgroup_init(void)
4988 struct cgroup_subsys
*ss
;
4992 err
= bdi_init(&cgroup_backing_dev_info
);
4996 for_each_builtin_subsys(ss
, i
) {
4997 if (!ss
->early_init
)
4998 cgroup_init_subsys(ss
);
5000 cgroup_init_idr(ss
, init_css_set
.subsys
[ss
->subsys_id
]);
5003 /* allocate id for the dummy hierarchy */
5004 mutex_lock(&cgroup_mutex
);
5005 mutex_lock(&cgroup_root_mutex
);
5007 /* Add init_css_set to the hash table */
5008 key
= css_set_hash(init_css_set
.subsys
);
5009 hash_add(css_set_table
, &init_css_set
.hlist
, key
);
5011 BUG_ON(cgroup_init_root_id(&cgroup_dummy_root
, 0, 1));
5013 err
= idr_alloc(&cgroup_dummy_root
.cgroup_idr
, cgroup_dummy_top
,
5017 mutex_unlock(&cgroup_root_mutex
);
5018 mutex_unlock(&cgroup_mutex
);
5020 cgroup_kobj
= kobject_create_and_add("cgroup", fs_kobj
);
5026 err
= register_filesystem(&cgroup_fs_type
);
5028 kobject_put(cgroup_kobj
);
5032 proc_create("cgroups", 0, NULL
, &proc_cgroupstats_operations
);
5036 bdi_destroy(&cgroup_backing_dev_info
);
5042 * proc_cgroup_show()
5043 * - Print task's cgroup paths into seq_file, one line for each hierarchy
5044 * - Used for /proc/<pid>/cgroup.
5045 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
5046 * doesn't really matter if tsk->cgroup changes after we read it,
5047 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
5048 * anyway. No need to check that tsk->cgroup != NULL, thanks to
5049 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
5050 * cgroup to top_cgroup.
5053 /* TODO: Use a proper seq_file iterator */
5054 int proc_cgroup_show(struct seq_file
*m
, void *v
)
5057 struct task_struct
*tsk
;
5060 struct cgroupfs_root
*root
;
5063 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
5069 tsk
= get_pid_task(pid
, PIDTYPE_PID
);
5075 mutex_lock(&cgroup_mutex
);
5077 for_each_active_root(root
) {
5078 struct cgroup_subsys
*ss
;
5079 struct cgroup
*cgrp
;
5082 seq_printf(m
, "%d:", root
->hierarchy_id
);
5083 for_each_root_subsys(root
, ss
)
5084 seq_printf(m
, "%s%s", count
++ ? "," : "", ss
->name
);
5085 if (strlen(root
->name
))
5086 seq_printf(m
, "%sname=%s", count
? "," : "",
5089 cgrp
= task_cgroup_from_root(tsk
, root
);
5090 retval
= cgroup_path(cgrp
, buf
, PAGE_SIZE
);
5098 mutex_unlock(&cgroup_mutex
);
5099 put_task_struct(tsk
);
5106 /* Display information about each subsystem and each hierarchy */
5107 static int proc_cgroupstats_show(struct seq_file
*m
, void *v
)
5109 struct cgroup_subsys
*ss
;
5112 seq_puts(m
, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
5114 * ideally we don't want subsystems moving around while we do this.
5115 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
5116 * subsys/hierarchy state.
5118 mutex_lock(&cgroup_mutex
);
5120 for_each_subsys(ss
, i
)
5121 seq_printf(m
, "%s\t%d\t%d\t%d\n",
5122 ss
->name
, ss
->root
->hierarchy_id
,
5123 ss
->root
->number_of_cgroups
, !ss
->disabled
);
5125 mutex_unlock(&cgroup_mutex
);
5129 static int cgroupstats_open(struct inode
*inode
, struct file
*file
)
5131 return single_open(file
, proc_cgroupstats_show
, NULL
);
5134 static const struct file_operations proc_cgroupstats_operations
= {
5135 .open
= cgroupstats_open
,
5137 .llseek
= seq_lseek
,
5138 .release
= single_release
,
5142 * cgroup_fork - attach newly forked task to its parents cgroup.
5143 * @child: pointer to task_struct of forking parent process.
5145 * Description: A task inherits its parent's cgroup at fork().
5147 * A pointer to the shared css_set was automatically copied in
5148 * fork.c by dup_task_struct(). However, we ignore that copy, since
5149 * it was not made under the protection of RCU or cgroup_mutex, so
5150 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
5151 * have already changed current->cgroups, allowing the previously
5152 * referenced cgroup group to be removed and freed.
5154 * At the point that cgroup_fork() is called, 'current' is the parent
5155 * task, and the passed argument 'child' points to the child task.
5157 void cgroup_fork(struct task_struct
*child
)
5160 get_css_set(task_css_set(current
));
5161 child
->cgroups
= current
->cgroups
;
5162 task_unlock(current
);
5163 INIT_LIST_HEAD(&child
->cg_list
);
5167 * cgroup_post_fork - called on a new task after adding it to the task list
5168 * @child: the task in question
5170 * Adds the task to the list running through its css_set if necessary and
5171 * call the subsystem fork() callbacks. Has to be after the task is
5172 * visible on the task list in case we race with the first call to
5173 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
5176 void cgroup_post_fork(struct task_struct
*child
)
5178 struct cgroup_subsys
*ss
;
5182 * use_task_css_set_links is set to 1 before we walk the tasklist
5183 * under the tasklist_lock and we read it here after we added the child
5184 * to the tasklist under the tasklist_lock as well. If the child wasn't
5185 * yet in the tasklist when we walked through it from
5186 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
5187 * should be visible now due to the paired locking and barriers implied
5188 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
5189 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
5192 if (use_task_css_set_links
) {
5193 write_lock(&css_set_lock
);
5195 if (list_empty(&child
->cg_list
))
5196 list_add(&child
->cg_list
, &task_css_set(child
)->tasks
);
5198 write_unlock(&css_set_lock
);
5202 * Call ss->fork(). This must happen after @child is linked on
5203 * css_set; otherwise, @child might change state between ->fork()
5204 * and addition to css_set.
5206 if (need_forkexit_callback
) {
5208 * fork/exit callbacks are supported only for builtin
5209 * subsystems, and the builtin section of the subsys
5210 * array is immutable, so we don't need to lock the
5211 * subsys array here. On the other hand, modular section
5212 * of the array can be freed at module unload, so we
5215 for_each_builtin_subsys(ss
, i
)
5222 * cgroup_exit - detach cgroup from exiting task
5223 * @tsk: pointer to task_struct of exiting process
5224 * @run_callback: run exit callbacks?
5226 * Description: Detach cgroup from @tsk and release it.
5228 * Note that cgroups marked notify_on_release force every task in
5229 * them to take the global cgroup_mutex mutex when exiting.
5230 * This could impact scaling on very large systems. Be reluctant to
5231 * use notify_on_release cgroups where very high task exit scaling
5232 * is required on large systems.
5234 * the_top_cgroup_hack:
5236 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
5238 * We call cgroup_exit() while the task is still competent to
5239 * handle notify_on_release(), then leave the task attached to the
5240 * root cgroup in each hierarchy for the remainder of its exit.
5242 * To do this properly, we would increment the reference count on
5243 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
5244 * code we would add a second cgroup function call, to drop that
5245 * reference. This would just create an unnecessary hot spot on
5246 * the top_cgroup reference count, to no avail.
5248 * Normally, holding a reference to a cgroup without bumping its
5249 * count is unsafe. The cgroup could go away, or someone could
5250 * attach us to a different cgroup, decrementing the count on
5251 * the first cgroup that we never incremented. But in this case,
5252 * top_cgroup isn't going away, and either task has PF_EXITING set,
5253 * which wards off any cgroup_attach_task() attempts, or task is a failed
5254 * fork, never visible to cgroup_attach_task.
5256 void cgroup_exit(struct task_struct
*tsk
, int run_callbacks
)
5258 struct cgroup_subsys
*ss
;
5259 struct css_set
*cset
;
5263 * Unlink from the css_set task list if necessary.
5264 * Optimistically check cg_list before taking
5267 if (!list_empty(&tsk
->cg_list
)) {
5268 write_lock(&css_set_lock
);
5269 if (!list_empty(&tsk
->cg_list
))
5270 list_del_init(&tsk
->cg_list
);
5271 write_unlock(&css_set_lock
);
5274 /* Reassign the task to the init_css_set. */
5276 cset
= task_css_set(tsk
);
5277 RCU_INIT_POINTER(tsk
->cgroups
, &init_css_set
);
5279 if (run_callbacks
&& need_forkexit_callback
) {
5281 * fork/exit callbacks are supported only for builtin
5282 * subsystems, see cgroup_post_fork() for details.
5284 for_each_builtin_subsys(ss
, i
) {
5286 struct cgroup_subsys_state
*old_css
= cset
->subsys
[i
];
5287 struct cgroup_subsys_state
*css
= task_css(tsk
, i
);
5289 ss
->exit(css
, old_css
, tsk
);
5295 put_css_set_taskexit(cset
);
5298 static void check_for_release(struct cgroup
*cgrp
)
5300 if (cgroup_is_releasable(cgrp
) &&
5301 list_empty(&cgrp
->cset_links
) && list_empty(&cgrp
->children
)) {
5303 * Control Group is currently removeable. If it's not
5304 * already queued for a userspace notification, queue
5307 int need_schedule_work
= 0;
5309 raw_spin_lock(&release_list_lock
);
5310 if (!cgroup_is_dead(cgrp
) &&
5311 list_empty(&cgrp
->release_list
)) {
5312 list_add(&cgrp
->release_list
, &release_list
);
5313 need_schedule_work
= 1;
5315 raw_spin_unlock(&release_list_lock
);
5316 if (need_schedule_work
)
5317 schedule_work(&release_agent_work
);
5322 * Notify userspace when a cgroup is released, by running the
5323 * configured release agent with the name of the cgroup (path
5324 * relative to the root of cgroup file system) as the argument.
5326 * Most likely, this user command will try to rmdir this cgroup.
5328 * This races with the possibility that some other task will be
5329 * attached to this cgroup before it is removed, or that some other
5330 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5331 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5332 * unused, and this cgroup will be reprieved from its death sentence,
5333 * to continue to serve a useful existence. Next time it's released,
5334 * we will get notified again, if it still has 'notify_on_release' set.
5336 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5337 * means only wait until the task is successfully execve()'d. The
5338 * separate release agent task is forked by call_usermodehelper(),
5339 * then control in this thread returns here, without waiting for the
5340 * release agent task. We don't bother to wait because the caller of
5341 * this routine has no use for the exit status of the release agent
5342 * task, so no sense holding our caller up for that.
5344 static void cgroup_release_agent(struct work_struct
*work
)
5346 BUG_ON(work
!= &release_agent_work
);
5347 mutex_lock(&cgroup_mutex
);
5348 raw_spin_lock(&release_list_lock
);
5349 while (!list_empty(&release_list
)) {
5350 char *argv
[3], *envp
[3];
5352 char *pathbuf
= NULL
, *agentbuf
= NULL
;
5353 struct cgroup
*cgrp
= list_entry(release_list
.next
,
5356 list_del_init(&cgrp
->release_list
);
5357 raw_spin_unlock(&release_list_lock
);
5358 pathbuf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
5361 if (cgroup_path(cgrp
, pathbuf
, PAGE_SIZE
) < 0)
5363 agentbuf
= kstrdup(cgrp
->root
->release_agent_path
, GFP_KERNEL
);
5368 argv
[i
++] = agentbuf
;
5369 argv
[i
++] = pathbuf
;
5373 /* minimal command environment */
5374 envp
[i
++] = "HOME=/";
5375 envp
[i
++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5378 /* Drop the lock while we invoke the usermode helper,
5379 * since the exec could involve hitting disk and hence
5380 * be a slow process */
5381 mutex_unlock(&cgroup_mutex
);
5382 call_usermodehelper(argv
[0], argv
, envp
, UMH_WAIT_EXEC
);
5383 mutex_lock(&cgroup_mutex
);
5387 raw_spin_lock(&release_list_lock
);
5389 raw_spin_unlock(&release_list_lock
);
5390 mutex_unlock(&cgroup_mutex
);
5393 static int __init
cgroup_disable(char *str
)
5395 struct cgroup_subsys
*ss
;
5399 while ((token
= strsep(&str
, ",")) != NULL
) {
5404 * cgroup_disable, being at boot time, can't know about
5405 * module subsystems, so we don't worry about them.
5407 for_each_builtin_subsys(ss
, i
) {
5408 if (!strcmp(token
, ss
->name
)) {
5410 printk(KERN_INFO
"Disabling %s control group"
5411 " subsystem\n", ss
->name
);
5418 __setup("cgroup_disable=", cgroup_disable
);
5421 * Functons for CSS ID.
5424 /* to get ID other than 0, this should be called when !cgroup_is_dead() */
5425 unsigned short css_id(struct cgroup_subsys_state
*css
)
5427 struct css_id
*cssid
;
5430 * This css_id() can return correct value when somone has refcnt
5431 * on this or this is under rcu_read_lock(). Once css->id is allocated,
5432 * it's unchanged until freed.
5434 cssid
= rcu_dereference_raw(css
->id
);
5440 EXPORT_SYMBOL_GPL(css_id
);
5443 * css_is_ancestor - test "root" css is an ancestor of "child"
5444 * @child: the css to be tested.
5445 * @root: the css supporsed to be an ancestor of the child.
5447 * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
5448 * this function reads css->id, the caller must hold rcu_read_lock().
5449 * But, considering usual usage, the csses should be valid objects after test.
5450 * Assuming that the caller will do some action to the child if this returns
5451 * returns true, the caller must take "child";s reference count.
5452 * If "child" is valid object and this returns true, "root" is valid, too.
5455 bool css_is_ancestor(struct cgroup_subsys_state
*child
,
5456 const struct cgroup_subsys_state
*root
)
5458 struct css_id
*child_id
;
5459 struct css_id
*root_id
;
5461 child_id
= rcu_dereference(child
->id
);
5464 root_id
= rcu_dereference(root
->id
);
5467 if (child_id
->depth
< root_id
->depth
)
5469 if (child_id
->stack
[root_id
->depth
] != root_id
->id
)
5474 void free_css_id(struct cgroup_subsys
*ss
, struct cgroup_subsys_state
*css
)
5476 struct css_id
*id
= rcu_dereference_protected(css
->id
, true);
5478 /* When this is called before css_id initialization, id can be NULL */
5482 BUG_ON(!ss
->use_id
);
5484 rcu_assign_pointer(id
->css
, NULL
);
5485 rcu_assign_pointer(css
->id
, NULL
);
5486 spin_lock(&ss
->id_lock
);
5487 idr_remove(&ss
->idr
, id
->id
);
5488 spin_unlock(&ss
->id_lock
);
5489 kfree_rcu(id
, rcu_head
);
5491 EXPORT_SYMBOL_GPL(free_css_id
);
5494 * This is called by init or create(). Then, calls to this function are
5495 * always serialized (By cgroup_mutex() at create()).
5498 static struct css_id
*get_new_cssid(struct cgroup_subsys
*ss
, int depth
)
5500 struct css_id
*newid
;
5503 BUG_ON(!ss
->use_id
);
5505 size
= sizeof(*newid
) + sizeof(unsigned short) * (depth
+ 1);
5506 newid
= kzalloc(size
, GFP_KERNEL
);
5508 return ERR_PTR(-ENOMEM
);
5510 idr_preload(GFP_KERNEL
);
5511 spin_lock(&ss
->id_lock
);
5512 /* Don't use 0. allocates an ID of 1-65535 */
5513 ret
= idr_alloc(&ss
->idr
, newid
, 1, CSS_ID_MAX
+ 1, GFP_NOWAIT
);
5514 spin_unlock(&ss
->id_lock
);
5517 /* Returns error when there are no free spaces for new ID.*/
5522 newid
->depth
= depth
;
5526 return ERR_PTR(ret
);
5530 static int __init_or_module
cgroup_init_idr(struct cgroup_subsys
*ss
,
5531 struct cgroup_subsys_state
*rootcss
)
5533 struct css_id
*newid
;
5535 spin_lock_init(&ss
->id_lock
);
5538 newid
= get_new_cssid(ss
, 0);
5540 return PTR_ERR(newid
);
5542 newid
->stack
[0] = newid
->id
;
5543 RCU_INIT_POINTER(newid
->css
, rootcss
);
5544 RCU_INIT_POINTER(rootcss
->id
, newid
);
5548 static int alloc_css_id(struct cgroup_subsys
*ss
, struct cgroup
*parent
,
5549 struct cgroup
*child
)
5551 int subsys_id
, i
, depth
= 0;
5552 struct cgroup_subsys_state
*parent_css
, *child_css
;
5553 struct css_id
*child_id
, *parent_id
;
5555 subsys_id
= ss
->subsys_id
;
5556 parent_css
= parent
->subsys
[subsys_id
];
5557 child_css
= child
->subsys
[subsys_id
];
5558 parent_id
= rcu_dereference_protected(parent_css
->id
, true);
5559 depth
= parent_id
->depth
+ 1;
5561 child_id
= get_new_cssid(ss
, depth
);
5562 if (IS_ERR(child_id
))
5563 return PTR_ERR(child_id
);
5565 for (i
= 0; i
< depth
; i
++)
5566 child_id
->stack
[i
] = parent_id
->stack
[i
];
5567 child_id
->stack
[depth
] = child_id
->id
;
5569 * child_id->css pointer will be set after this cgroup is available
5570 * see cgroup_populate_dir()
5572 rcu_assign_pointer(child_css
->id
, child_id
);
5578 * css_lookup - lookup css by id
5579 * @ss: cgroup subsys to be looked into.
5582 * Returns pointer to cgroup_subsys_state if there is valid one with id.
5583 * NULL if not. Should be called under rcu_read_lock()
5585 struct cgroup_subsys_state
*css_lookup(struct cgroup_subsys
*ss
, int id
)
5587 struct css_id
*cssid
= NULL
;
5589 BUG_ON(!ss
->use_id
);
5590 cssid
= idr_find(&ss
->idr
, id
);
5592 if (unlikely(!cssid
))
5595 return rcu_dereference(cssid
->css
);
5597 EXPORT_SYMBOL_GPL(css_lookup
);
5600 * get corresponding css from file open on cgroupfs directory
5602 struct cgroup_subsys_state
*cgroup_css_from_dir(struct file
*f
, int id
)
5604 struct cgroup
*cgrp
;
5605 struct inode
*inode
;
5606 struct cgroup_subsys_state
*css
;
5608 inode
= file_inode(f
);
5609 /* check in cgroup filesystem dir */
5610 if (inode
->i_op
!= &cgroup_dir_inode_operations
)
5611 return ERR_PTR(-EBADF
);
5613 if (id
< 0 || id
>= CGROUP_SUBSYS_COUNT
)
5614 return ERR_PTR(-EINVAL
);
5617 cgrp
= __d_cgrp(f
->f_dentry
);
5618 css
= cgrp
->subsys
[id
];
5619 return css
? css
: ERR_PTR(-ENOENT
);
5622 #ifdef CONFIG_CGROUP_DEBUG
5623 static struct cgroup_subsys_state
*
5624 debug_css_alloc(struct cgroup_subsys_state
*parent_css
)
5626 struct cgroup_subsys_state
*css
= kzalloc(sizeof(*css
), GFP_KERNEL
);
5629 return ERR_PTR(-ENOMEM
);
5634 static void debug_css_free(struct cgroup_subsys_state
*css
)
5639 static u64
debug_taskcount_read(struct cgroup_subsys_state
*css
,
5642 return cgroup_task_count(css
->cgroup
);
5645 static u64
current_css_set_read(struct cgroup_subsys_state
*css
,
5648 return (u64
)(unsigned long)current
->cgroups
;
5651 static u64
current_css_set_refcount_read(struct cgroup_subsys_state
*css
,
5657 count
= atomic_read(&task_css_set(current
)->refcount
);
5662 static int current_css_set_cg_links_read(struct cgroup_subsys_state
*css
,
5664 struct seq_file
*seq
)
5666 struct cgrp_cset_link
*link
;
5667 struct css_set
*cset
;
5669 read_lock(&css_set_lock
);
5671 cset
= rcu_dereference(current
->cgroups
);
5672 list_for_each_entry(link
, &cset
->cgrp_links
, cgrp_link
) {
5673 struct cgroup
*c
= link
->cgrp
;
5677 name
= c
->dentry
->d_name
.name
;
5680 seq_printf(seq
, "Root %d group %s\n",
5681 c
->root
->hierarchy_id
, name
);
5684 read_unlock(&css_set_lock
);
5688 #define MAX_TASKS_SHOWN_PER_CSS 25
5689 static int cgroup_css_links_read(struct cgroup_subsys_state
*css
,
5690 struct cftype
*cft
, struct seq_file
*seq
)
5692 struct cgrp_cset_link
*link
;
5694 read_lock(&css_set_lock
);
5695 list_for_each_entry(link
, &css
->cgroup
->cset_links
, cset_link
) {
5696 struct css_set
*cset
= link
->cset
;
5697 struct task_struct
*task
;
5699 seq_printf(seq
, "css_set %p\n", cset
);
5700 list_for_each_entry(task
, &cset
->tasks
, cg_list
) {
5701 if (count
++ > MAX_TASKS_SHOWN_PER_CSS
) {
5702 seq_puts(seq
, " ...\n");
5705 seq_printf(seq
, " task %d\n",
5706 task_pid_vnr(task
));
5710 read_unlock(&css_set_lock
);
5714 static u64
releasable_read(struct cgroup_subsys_state
*css
, struct cftype
*cft
)
5716 return test_bit(CGRP_RELEASABLE
, &css
->cgroup
->flags
);
5719 static struct cftype debug_files
[] = {
5721 .name
= "taskcount",
5722 .read_u64
= debug_taskcount_read
,
5726 .name
= "current_css_set",
5727 .read_u64
= current_css_set_read
,
5731 .name
= "current_css_set_refcount",
5732 .read_u64
= current_css_set_refcount_read
,
5736 .name
= "current_css_set_cg_links",
5737 .read_seq_string
= current_css_set_cg_links_read
,
5741 .name
= "cgroup_css_links",
5742 .read_seq_string
= cgroup_css_links_read
,
5746 .name
= "releasable",
5747 .read_u64
= releasable_read
,
5753 struct cgroup_subsys debug_subsys
= {
5755 .css_alloc
= debug_css_alloc
,
5756 .css_free
= debug_css_free
,
5757 .subsys_id
= debug_subsys_id
,
5758 .base_cftypes
= debug_files
,
5760 #endif /* CONFIG_CGROUP_DEBUG */