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>
34 #include <linux/init_task.h>
35 #include <linux/kernel.h>
36 #include <linux/list.h>
38 #include <linux/mutex.h>
39 #include <linux/mount.h>
40 #include <linux/pagemap.h>
41 #include <linux/proc_fs.h>
42 #include <linux/rcupdate.h>
43 #include <linux/sched.h>
44 #include <linux/backing-dev.h>
45 #include <linux/seq_file.h>
46 #include <linux/slab.h>
47 #include <linux/magic.h>
48 #include <linux/spinlock.h>
49 #include <linux/string.h>
50 #include <linux/sort.h>
51 #include <linux/kmod.h>
52 #include <linux/module.h>
53 #include <linux/delayacct.h>
54 #include <linux/cgroupstats.h>
55 #include <linux/hash.h>
56 #include <linux/namei.h>
57 #include <linux/pid_namespace.h>
58 #include <linux/idr.h>
59 #include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
60 #include <linux/eventfd.h>
61 #include <linux/poll.h>
62 #include <linux/flex_array.h> /* used in cgroup_attach_proc */
63 #include <linux/kthread.h>
65 #include <linux/atomic.h>
67 /* css deactivation bias, makes css->refcnt negative to deny new trygets */
68 #define CSS_DEACT_BIAS INT_MIN
71 * cgroup_mutex is the master lock. Any modification to cgroup or its
72 * hierarchy must be performed while holding it.
74 * cgroup_root_mutex nests inside cgroup_mutex and should be held to modify
75 * cgroupfs_root of any cgroup hierarchy - subsys list, flags,
76 * release_agent_path and so on. Modifying requires both cgroup_mutex and
77 * cgroup_root_mutex. Readers can acquire either of the two. This is to
78 * break the following locking order cycle.
80 * A. cgroup_mutex -> cred_guard_mutex -> s_type->i_mutex_key -> namespace_sem
81 * B. namespace_sem -> cgroup_mutex
83 * B happens only through cgroup_show_options() and using cgroup_root_mutex
86 static DEFINE_MUTEX(cgroup_mutex
);
87 static DEFINE_MUTEX(cgroup_root_mutex
);
90 * Generate an array of cgroup subsystem pointers. At boot time, this is
91 * populated with the built in subsystems, and modular subsystems are
92 * registered after that. The mutable section of this array is protected by
95 #define SUBSYS(_x) [_x ## _subsys_id] = &_x ## _subsys,
96 #define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option)
97 static struct cgroup_subsys
*subsys
[CGROUP_SUBSYS_COUNT
] = {
98 #include <linux/cgroup_subsys.h>
101 #define MAX_CGROUP_ROOT_NAMELEN 64
104 * A cgroupfs_root represents the root of a cgroup hierarchy,
105 * and may be associated with a superblock to form an active
108 struct cgroupfs_root
{
109 struct super_block
*sb
;
112 * The bitmask of subsystems intended to be attached to this
115 unsigned long subsys_mask
;
117 /* Unique id for this hierarchy. */
120 /* The bitmask of subsystems currently attached to this hierarchy */
121 unsigned long actual_subsys_mask
;
123 /* A list running through the attached subsystems */
124 struct list_head subsys_list
;
126 /* The root cgroup for this hierarchy */
127 struct cgroup top_cgroup
;
129 /* Tracks how many cgroups are currently defined in hierarchy.*/
130 int number_of_cgroups
;
132 /* A list running through the active hierarchies */
133 struct list_head root_list
;
135 /* All cgroups on this root, cgroup_mutex protected */
136 struct list_head allcg_list
;
138 /* Hierarchy-specific flags */
141 /* IDs for cgroups in this hierarchy */
142 struct ida cgroup_ida
;
144 /* The path to use for release notifications. */
145 char release_agent_path
[PATH_MAX
];
147 /* The name for this hierarchy - may be empty */
148 char name
[MAX_CGROUP_ROOT_NAMELEN
];
152 * The "rootnode" hierarchy is the "dummy hierarchy", reserved for the
153 * subsystems that are otherwise unattached - it never has more than a
154 * single cgroup, and all tasks are part of that cgroup.
156 static struct cgroupfs_root rootnode
;
159 * cgroupfs file entry, pointed to from leaf dentry->d_fsdata.
162 struct list_head node
;
163 struct dentry
*dentry
;
168 * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when
169 * cgroup_subsys->use_id != 0.
171 #define CSS_ID_MAX (65535)
174 * The css to which this ID points. This pointer is set to valid value
175 * after cgroup is populated. If cgroup is removed, this will be NULL.
176 * This pointer is expected to be RCU-safe because destroy()
177 * is called after synchronize_rcu(). But for safe use, css_tryget()
178 * should be used for avoiding race.
180 struct cgroup_subsys_state __rcu
*css
;
186 * Depth in hierarchy which this ID belongs to.
188 unsigned short depth
;
190 * ID is freed by RCU. (and lookup routine is RCU safe.)
192 struct rcu_head rcu_head
;
194 * Hierarchy of CSS ID belongs to.
196 unsigned short stack
[0]; /* Array of Length (depth+1) */
200 * cgroup_event represents events which userspace want to receive.
202 struct cgroup_event
{
204 * Cgroup which the event belongs to.
208 * Control file which the event associated.
212 * eventfd to signal userspace about the event.
214 struct eventfd_ctx
*eventfd
;
216 * Each of these stored in a list by the cgroup.
218 struct list_head list
;
220 * All fields below needed to unregister event when
221 * userspace closes eventfd.
224 wait_queue_head_t
*wqh
;
226 struct work_struct remove
;
229 /* The list of hierarchy roots */
231 static LIST_HEAD(roots
);
232 static int root_count
;
234 static DEFINE_IDA(hierarchy_ida
);
235 static int next_hierarchy_id
;
236 static DEFINE_SPINLOCK(hierarchy_id_lock
);
238 /* dummytop is a shorthand for the dummy hierarchy's top cgroup */
239 #define dummytop (&rootnode.top_cgroup)
241 /* This flag indicates whether tasks in the fork and exit paths should
242 * check for fork/exit handlers to call. This avoids us having to do
243 * extra work in the fork/exit path if none of the subsystems need to
246 static int need_forkexit_callback __read_mostly
;
248 static int cgroup_destroy_locked(struct cgroup
*cgrp
);
250 #ifdef CONFIG_PROVE_LOCKING
251 int cgroup_lock_is_held(void)
253 return lockdep_is_held(&cgroup_mutex
);
255 #else /* #ifdef CONFIG_PROVE_LOCKING */
256 int cgroup_lock_is_held(void)
258 return mutex_is_locked(&cgroup_mutex
);
260 #endif /* #else #ifdef CONFIG_PROVE_LOCKING */
262 EXPORT_SYMBOL_GPL(cgroup_lock_is_held
);
264 static int css_unbias_refcnt(int refcnt
)
266 return refcnt
>= 0 ? refcnt
: refcnt
- CSS_DEACT_BIAS
;
269 /* the current nr of refs, always >= 0 whether @css is deactivated or not */
270 static int css_refcnt(struct cgroup_subsys_state
*css
)
272 int v
= atomic_read(&css
->refcnt
);
274 return css_unbias_refcnt(v
);
277 /* convenient tests for these bits */
278 inline int cgroup_is_removed(const struct cgroup
*cgrp
)
280 return test_bit(CGRP_REMOVED
, &cgrp
->flags
);
283 /* bits in struct cgroupfs_root flags field */
285 ROOT_NOPREFIX
, /* mounted subsystems have no named prefix */
286 ROOT_XATTR
, /* supports extended attributes */
289 static int cgroup_is_releasable(const struct cgroup
*cgrp
)
292 (1 << CGRP_RELEASABLE
) |
293 (1 << CGRP_NOTIFY_ON_RELEASE
);
294 return (cgrp
->flags
& bits
) == bits
;
297 static int notify_on_release(const struct cgroup
*cgrp
)
299 return test_bit(CGRP_NOTIFY_ON_RELEASE
, &cgrp
->flags
);
303 * for_each_subsys() allows you to iterate on each subsystem attached to
304 * an active hierarchy
306 #define for_each_subsys(_root, _ss) \
307 list_for_each_entry(_ss, &_root->subsys_list, sibling)
309 /* for_each_active_root() allows you to iterate across the active hierarchies */
310 #define for_each_active_root(_root) \
311 list_for_each_entry(_root, &roots, root_list)
313 static inline struct cgroup
*__d_cgrp(struct dentry
*dentry
)
315 return dentry
->d_fsdata
;
318 static inline struct cfent
*__d_cfe(struct dentry
*dentry
)
320 return dentry
->d_fsdata
;
323 static inline struct cftype
*__d_cft(struct dentry
*dentry
)
325 return __d_cfe(dentry
)->type
;
328 /* the list of cgroups eligible for automatic release. Protected by
329 * release_list_lock */
330 static LIST_HEAD(release_list
);
331 static DEFINE_RAW_SPINLOCK(release_list_lock
);
332 static void cgroup_release_agent(struct work_struct
*work
);
333 static DECLARE_WORK(release_agent_work
, cgroup_release_agent
);
334 static void check_for_release(struct cgroup
*cgrp
);
336 /* Link structure for associating css_set objects with cgroups */
337 struct cg_cgroup_link
{
339 * List running through cg_cgroup_links associated with a
340 * cgroup, anchored on cgroup->css_sets
342 struct list_head cgrp_link_list
;
345 * List running through cg_cgroup_links pointing at a
346 * single css_set object, anchored on css_set->cg_links
348 struct list_head cg_link_list
;
352 /* The default css_set - used by init and its children prior to any
353 * hierarchies being mounted. It contains a pointer to the root state
354 * for each subsystem. Also used to anchor the list of css_sets. Not
355 * reference-counted, to improve performance when child cgroups
356 * haven't been created.
359 static struct css_set init_css_set
;
360 static struct cg_cgroup_link init_css_set_link
;
362 static int cgroup_init_idr(struct cgroup_subsys
*ss
,
363 struct cgroup_subsys_state
*css
);
365 /* css_set_lock protects the list of css_set objects, and the
366 * chain of tasks off each css_set. Nests outside task->alloc_lock
367 * due to cgroup_iter_start() */
368 static DEFINE_RWLOCK(css_set_lock
);
369 static int css_set_count
;
372 * hash table for cgroup groups. This improves the performance to find
373 * an existing css_set. This hash doesn't (currently) take into
374 * account cgroups in empty hierarchies.
376 #define CSS_SET_HASH_BITS 7
377 #define CSS_SET_TABLE_SIZE (1 << CSS_SET_HASH_BITS)
378 static struct hlist_head css_set_table
[CSS_SET_TABLE_SIZE
];
380 static struct hlist_head
*css_set_hash(struct cgroup_subsys_state
*css
[])
384 unsigned long tmp
= 0UL;
386 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++)
387 tmp
+= (unsigned long)css
[i
];
388 tmp
= (tmp
>> 16) ^ tmp
;
390 index
= hash_long(tmp
, CSS_SET_HASH_BITS
);
392 return &css_set_table
[index
];
395 /* We don't maintain the lists running through each css_set to its
396 * task until after the first call to cgroup_iter_start(). This
397 * reduces the fork()/exit() overhead for people who have cgroups
398 * compiled into their kernel but not actually in use */
399 static int use_task_css_set_links __read_mostly
;
401 static void __put_css_set(struct css_set
*cg
, int taskexit
)
403 struct cg_cgroup_link
*link
;
404 struct cg_cgroup_link
*saved_link
;
406 * Ensure that the refcount doesn't hit zero while any readers
407 * can see it. Similar to atomic_dec_and_lock(), but for an
410 if (atomic_add_unless(&cg
->refcount
, -1, 1))
412 write_lock(&css_set_lock
);
413 if (!atomic_dec_and_test(&cg
->refcount
)) {
414 write_unlock(&css_set_lock
);
418 /* This css_set is dead. unlink it and release cgroup refcounts */
419 hlist_del(&cg
->hlist
);
422 list_for_each_entry_safe(link
, saved_link
, &cg
->cg_links
,
424 struct cgroup
*cgrp
= link
->cgrp
;
425 list_del(&link
->cg_link_list
);
426 list_del(&link
->cgrp_link_list
);
427 if (atomic_dec_and_test(&cgrp
->count
) &&
428 notify_on_release(cgrp
)) {
430 set_bit(CGRP_RELEASABLE
, &cgrp
->flags
);
431 check_for_release(cgrp
);
437 write_unlock(&css_set_lock
);
438 kfree_rcu(cg
, rcu_head
);
442 * refcounted get/put for css_set objects
444 static inline void get_css_set(struct css_set
*cg
)
446 atomic_inc(&cg
->refcount
);
449 static inline void put_css_set(struct css_set
*cg
)
451 __put_css_set(cg
, 0);
454 static inline void put_css_set_taskexit(struct css_set
*cg
)
456 __put_css_set(cg
, 1);
460 * compare_css_sets - helper function for find_existing_css_set().
461 * @cg: candidate css_set being tested
462 * @old_cg: existing css_set for a task
463 * @new_cgrp: cgroup that's being entered by the task
464 * @template: desired set of css pointers in css_set (pre-calculated)
466 * Returns true if "cg" matches "old_cg" except for the hierarchy
467 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
469 static bool compare_css_sets(struct css_set
*cg
,
470 struct css_set
*old_cg
,
471 struct cgroup
*new_cgrp
,
472 struct cgroup_subsys_state
*template[])
474 struct list_head
*l1
, *l2
;
476 if (memcmp(template, cg
->subsys
, sizeof(cg
->subsys
))) {
477 /* Not all subsystems matched */
482 * Compare cgroup pointers in order to distinguish between
483 * different cgroups in heirarchies with no subsystems. We
484 * could get by with just this check alone (and skip the
485 * memcmp above) but on most setups the memcmp check will
486 * avoid the need for this more expensive check on almost all
491 l2
= &old_cg
->cg_links
;
493 struct cg_cgroup_link
*cgl1
, *cgl2
;
494 struct cgroup
*cg1
, *cg2
;
498 /* See if we reached the end - both lists are equal length. */
499 if (l1
== &cg
->cg_links
) {
500 BUG_ON(l2
!= &old_cg
->cg_links
);
503 BUG_ON(l2
== &old_cg
->cg_links
);
505 /* Locate the cgroups associated with these links. */
506 cgl1
= list_entry(l1
, struct cg_cgroup_link
, cg_link_list
);
507 cgl2
= list_entry(l2
, struct cg_cgroup_link
, cg_link_list
);
510 /* Hierarchies should be linked in the same order. */
511 BUG_ON(cg1
->root
!= cg2
->root
);
514 * If this hierarchy is the hierarchy of the cgroup
515 * that's changing, then we need to check that this
516 * css_set points to the new cgroup; if it's any other
517 * hierarchy, then this css_set should point to the
518 * same cgroup as the old css_set.
520 if (cg1
->root
== new_cgrp
->root
) {
532 * find_existing_css_set() is a helper for
533 * find_css_set(), and checks to see whether an existing
534 * css_set is suitable.
536 * oldcg: the cgroup group that we're using before the cgroup
539 * cgrp: the cgroup that we're moving into
541 * template: location in which to build the desired set of subsystem
542 * state objects for the new cgroup group
544 static struct css_set
*find_existing_css_set(
545 struct css_set
*oldcg
,
547 struct cgroup_subsys_state
*template[])
550 struct cgroupfs_root
*root
= cgrp
->root
;
551 struct hlist_head
*hhead
;
552 struct hlist_node
*node
;
556 * Build the set of subsystem state objects that we want to see in the
557 * new css_set. while subsystems can change globally, the entries here
558 * won't change, so no need for locking.
560 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
561 if (root
->subsys_mask
& (1UL << i
)) {
562 /* Subsystem is in this hierarchy. So we want
563 * the subsystem state from the new
565 template[i
] = cgrp
->subsys
[i
];
567 /* Subsystem is not in this hierarchy, so we
568 * don't want to change the subsystem state */
569 template[i
] = oldcg
->subsys
[i
];
573 hhead
= css_set_hash(template);
574 hlist_for_each_entry(cg
, node
, hhead
, hlist
) {
575 if (!compare_css_sets(cg
, oldcg
, cgrp
, template))
578 /* This css_set matches what we need */
582 /* No existing cgroup group matched */
586 static void free_cg_links(struct list_head
*tmp
)
588 struct cg_cgroup_link
*link
;
589 struct cg_cgroup_link
*saved_link
;
591 list_for_each_entry_safe(link
, saved_link
, tmp
, cgrp_link_list
) {
592 list_del(&link
->cgrp_link_list
);
598 * allocate_cg_links() allocates "count" cg_cgroup_link structures
599 * and chains them on tmp through their cgrp_link_list fields. Returns 0 on
600 * success or a negative error
602 static int allocate_cg_links(int count
, struct list_head
*tmp
)
604 struct cg_cgroup_link
*link
;
607 for (i
= 0; i
< count
; i
++) {
608 link
= kmalloc(sizeof(*link
), GFP_KERNEL
);
613 list_add(&link
->cgrp_link_list
, tmp
);
619 * link_css_set - a helper function to link a css_set to a cgroup
620 * @tmp_cg_links: cg_cgroup_link objects allocated by allocate_cg_links()
621 * @cg: the css_set to be linked
622 * @cgrp: the destination cgroup
624 static void link_css_set(struct list_head
*tmp_cg_links
,
625 struct css_set
*cg
, struct cgroup
*cgrp
)
627 struct cg_cgroup_link
*link
;
629 BUG_ON(list_empty(tmp_cg_links
));
630 link
= list_first_entry(tmp_cg_links
, struct cg_cgroup_link
,
634 atomic_inc(&cgrp
->count
);
635 list_move(&link
->cgrp_link_list
, &cgrp
->css_sets
);
637 * Always add links to the tail of the list so that the list
638 * is sorted by order of hierarchy creation
640 list_add_tail(&link
->cg_link_list
, &cg
->cg_links
);
644 * find_css_set() takes an existing cgroup group and a
645 * cgroup object, and returns a css_set object that's
646 * equivalent to the old group, but with the given cgroup
647 * substituted into the appropriate hierarchy. Must be called with
650 static struct css_set
*find_css_set(
651 struct css_set
*oldcg
, struct cgroup
*cgrp
)
654 struct cgroup_subsys_state
*template[CGROUP_SUBSYS_COUNT
];
656 struct list_head tmp_cg_links
;
658 struct hlist_head
*hhead
;
659 struct cg_cgroup_link
*link
;
661 /* First see if we already have a cgroup group that matches
663 read_lock(&css_set_lock
);
664 res
= find_existing_css_set(oldcg
, cgrp
, template);
667 read_unlock(&css_set_lock
);
672 res
= kmalloc(sizeof(*res
), GFP_KERNEL
);
676 /* Allocate all the cg_cgroup_link objects that we'll need */
677 if (allocate_cg_links(root_count
, &tmp_cg_links
) < 0) {
682 atomic_set(&res
->refcount
, 1);
683 INIT_LIST_HEAD(&res
->cg_links
);
684 INIT_LIST_HEAD(&res
->tasks
);
685 INIT_HLIST_NODE(&res
->hlist
);
687 /* Copy the set of subsystem state objects generated in
688 * find_existing_css_set() */
689 memcpy(res
->subsys
, template, sizeof(res
->subsys
));
691 write_lock(&css_set_lock
);
692 /* Add reference counts and links from the new css_set. */
693 list_for_each_entry(link
, &oldcg
->cg_links
, cg_link_list
) {
694 struct cgroup
*c
= link
->cgrp
;
695 if (c
->root
== cgrp
->root
)
697 link_css_set(&tmp_cg_links
, res
, c
);
700 BUG_ON(!list_empty(&tmp_cg_links
));
704 /* Add this cgroup group to the hash table */
705 hhead
= css_set_hash(res
->subsys
);
706 hlist_add_head(&res
->hlist
, hhead
);
708 write_unlock(&css_set_lock
);
714 * Return the cgroup for "task" from the given hierarchy. Must be
715 * called with cgroup_mutex held.
717 static struct cgroup
*task_cgroup_from_root(struct task_struct
*task
,
718 struct cgroupfs_root
*root
)
721 struct cgroup
*res
= NULL
;
723 BUG_ON(!mutex_is_locked(&cgroup_mutex
));
724 read_lock(&css_set_lock
);
726 * No need to lock the task - since we hold cgroup_mutex the
727 * task can't change groups, so the only thing that can happen
728 * is that it exits and its css is set back to init_css_set.
731 if (css
== &init_css_set
) {
732 res
= &root
->top_cgroup
;
734 struct cg_cgroup_link
*link
;
735 list_for_each_entry(link
, &css
->cg_links
, cg_link_list
) {
736 struct cgroup
*c
= link
->cgrp
;
737 if (c
->root
== root
) {
743 read_unlock(&css_set_lock
);
749 * There is one global cgroup mutex. We also require taking
750 * task_lock() when dereferencing a task's cgroup subsys pointers.
751 * See "The task_lock() exception", at the end of this comment.
753 * A task must hold cgroup_mutex to modify cgroups.
755 * Any task can increment and decrement the count field without lock.
756 * So in general, code holding cgroup_mutex can't rely on the count
757 * field not changing. However, if the count goes to zero, then only
758 * cgroup_attach_task() can increment it again. Because a count of zero
759 * means that no tasks are currently attached, therefore there is no
760 * way a task attached to that cgroup can fork (the other way to
761 * increment the count). So code holding cgroup_mutex can safely
762 * assume that if the count is zero, it will stay zero. Similarly, if
763 * a task holds cgroup_mutex on a cgroup with zero count, it
764 * knows that the cgroup won't be removed, as cgroup_rmdir()
767 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
768 * (usually) take cgroup_mutex. These are the two most performance
769 * critical pieces of code here. The exception occurs on cgroup_exit(),
770 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
771 * is taken, and if the cgroup count is zero, a usermode call made
772 * to the release agent with the name of the cgroup (path relative to
773 * the root of cgroup file system) as the argument.
775 * A cgroup can only be deleted if both its 'count' of using tasks
776 * is zero, and its list of 'children' cgroups is empty. Since all
777 * tasks in the system use _some_ cgroup, and since there is always at
778 * least one task in the system (init, pid == 1), therefore, top_cgroup
779 * always has either children cgroups and/or using tasks. So we don't
780 * need a special hack to ensure that top_cgroup cannot be deleted.
782 * The task_lock() exception
784 * The need for this exception arises from the action of
785 * cgroup_attach_task(), which overwrites one task's cgroup pointer with
786 * another. It does so using cgroup_mutex, however there are
787 * several performance critical places that need to reference
788 * task->cgroup without the expense of grabbing a system global
789 * mutex. Therefore except as noted below, when dereferencing or, as
790 * in cgroup_attach_task(), modifying a task's cgroup pointer we use
791 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
792 * the task_struct routinely used for such matters.
794 * P.S. One more locking exception. RCU is used to guard the
795 * update of a tasks cgroup pointer by cgroup_attach_task()
799 * cgroup_lock - lock out any changes to cgroup structures
802 void cgroup_lock(void)
804 mutex_lock(&cgroup_mutex
);
806 EXPORT_SYMBOL_GPL(cgroup_lock
);
809 * cgroup_unlock - release lock on cgroup changes
811 * Undo the lock taken in a previous cgroup_lock() call.
813 void cgroup_unlock(void)
815 mutex_unlock(&cgroup_mutex
);
817 EXPORT_SYMBOL_GPL(cgroup_unlock
);
820 * A couple of forward declarations required, due to cyclic reference loop:
821 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
822 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
826 static int cgroup_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
);
827 static struct dentry
*cgroup_lookup(struct inode
*, struct dentry
*, unsigned int);
828 static int cgroup_rmdir(struct inode
*unused_dir
, struct dentry
*dentry
);
829 static int cgroup_populate_dir(struct cgroup
*cgrp
, bool base_files
,
830 unsigned long subsys_mask
);
831 static const struct inode_operations cgroup_dir_inode_operations
;
832 static const struct file_operations proc_cgroupstats_operations
;
834 static struct backing_dev_info cgroup_backing_dev_info
= {
836 .capabilities
= BDI_CAP_NO_ACCT_AND_WRITEBACK
,
839 static int alloc_css_id(struct cgroup_subsys
*ss
,
840 struct cgroup
*parent
, struct cgroup
*child
);
842 static struct inode
*cgroup_new_inode(umode_t mode
, struct super_block
*sb
)
844 struct inode
*inode
= new_inode(sb
);
847 inode
->i_ino
= get_next_ino();
848 inode
->i_mode
= mode
;
849 inode
->i_uid
= current_fsuid();
850 inode
->i_gid
= current_fsgid();
851 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
852 inode
->i_mapping
->backing_dev_info
= &cgroup_backing_dev_info
;
857 static void cgroup_diput(struct dentry
*dentry
, struct inode
*inode
)
859 /* is dentry a directory ? if so, kfree() associated cgroup */
860 if (S_ISDIR(inode
->i_mode
)) {
861 struct cgroup
*cgrp
= dentry
->d_fsdata
;
862 struct cgroup_subsys
*ss
;
863 BUG_ON(!(cgroup_is_removed(cgrp
)));
864 /* It's possible for external users to be holding css
865 * reference counts on a cgroup; css_put() needs to
866 * be able to access the cgroup after decrementing
867 * the reference count in order to know if it needs to
868 * queue the cgroup to be handled by the release
872 mutex_lock(&cgroup_mutex
);
874 * Release the subsystem state objects.
876 for_each_subsys(cgrp
->root
, ss
)
879 cgrp
->root
->number_of_cgroups
--;
880 mutex_unlock(&cgroup_mutex
);
883 * Drop the active superblock reference that we took when we
886 deactivate_super(cgrp
->root
->sb
);
889 * if we're getting rid of the cgroup, refcount should ensure
890 * that there are no pidlists left.
892 BUG_ON(!list_empty(&cgrp
->pidlists
));
894 simple_xattrs_free(&cgrp
->xattrs
);
896 ida_simple_remove(&cgrp
->root
->cgroup_ida
, cgrp
->id
);
897 kfree_rcu(cgrp
, rcu_head
);
899 struct cfent
*cfe
= __d_cfe(dentry
);
900 struct cgroup
*cgrp
= dentry
->d_parent
->d_fsdata
;
901 struct cftype
*cft
= cfe
->type
;
903 WARN_ONCE(!list_empty(&cfe
->node
) &&
904 cgrp
!= &cgrp
->root
->top_cgroup
,
905 "cfe still linked for %s\n", cfe
->type
->name
);
907 simple_xattrs_free(&cft
->xattrs
);
912 static int cgroup_delete(const struct dentry
*d
)
917 static void remove_dir(struct dentry
*d
)
919 struct dentry
*parent
= dget(d
->d_parent
);
922 simple_rmdir(parent
->d_inode
, d
);
926 static int cgroup_rm_file(struct cgroup
*cgrp
, const struct cftype
*cft
)
930 lockdep_assert_held(&cgrp
->dentry
->d_inode
->i_mutex
);
931 lockdep_assert_held(&cgroup_mutex
);
933 list_for_each_entry(cfe
, &cgrp
->files
, node
) {
934 struct dentry
*d
= cfe
->dentry
;
936 if (cft
&& cfe
->type
!= cft
)
941 simple_unlink(cgrp
->dentry
->d_inode
, d
);
942 list_del_init(&cfe
->node
);
951 * cgroup_clear_directory - selective removal of base and subsystem files
952 * @dir: directory containing the files
953 * @base_files: true if the base files should be removed
954 * @subsys_mask: mask of the subsystem ids whose files should be removed
956 static void cgroup_clear_directory(struct dentry
*dir
, bool base_files
,
957 unsigned long subsys_mask
)
959 struct cgroup
*cgrp
= __d_cgrp(dir
);
960 struct cgroup_subsys
*ss
;
962 for_each_subsys(cgrp
->root
, ss
) {
963 struct cftype_set
*set
;
964 if (!test_bit(ss
->subsys_id
, &subsys_mask
))
966 list_for_each_entry(set
, &ss
->cftsets
, node
)
967 cgroup_rm_file(cgrp
, set
->cfts
);
970 while (!list_empty(&cgrp
->files
))
971 cgroup_rm_file(cgrp
, NULL
);
976 * NOTE : the dentry must have been dget()'ed
978 static void cgroup_d_remove_dir(struct dentry
*dentry
)
980 struct dentry
*parent
;
981 struct cgroupfs_root
*root
= dentry
->d_sb
->s_fs_info
;
983 cgroup_clear_directory(dentry
, true, root
->subsys_mask
);
985 parent
= dentry
->d_parent
;
986 spin_lock(&parent
->d_lock
);
987 spin_lock_nested(&dentry
->d_lock
, DENTRY_D_LOCK_NESTED
);
988 list_del_init(&dentry
->d_u
.d_child
);
989 spin_unlock(&dentry
->d_lock
);
990 spin_unlock(&parent
->d_lock
);
995 * Call with cgroup_mutex held. Drops reference counts on modules, including
996 * any duplicate ones that parse_cgroupfs_options took. If this function
997 * returns an error, no reference counts are touched.
999 static int rebind_subsystems(struct cgroupfs_root
*root
,
1000 unsigned long final_subsys_mask
)
1002 unsigned long added_mask
, removed_mask
;
1003 struct cgroup
*cgrp
= &root
->top_cgroup
;
1006 BUG_ON(!mutex_is_locked(&cgroup_mutex
));
1007 BUG_ON(!mutex_is_locked(&cgroup_root_mutex
));
1009 removed_mask
= root
->actual_subsys_mask
& ~final_subsys_mask
;
1010 added_mask
= final_subsys_mask
& ~root
->actual_subsys_mask
;
1011 /* Check that any added subsystems are currently free */
1012 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
1013 unsigned long bit
= 1UL << i
;
1014 struct cgroup_subsys
*ss
= subsys
[i
];
1015 if (!(bit
& added_mask
))
1018 * Nobody should tell us to do a subsys that doesn't exist:
1019 * parse_cgroupfs_options should catch that case and refcounts
1020 * ensure that subsystems won't disappear once selected.
1023 if (ss
->root
!= &rootnode
) {
1024 /* Subsystem isn't free */
1029 /* Currently we don't handle adding/removing subsystems when
1030 * any child cgroups exist. This is theoretically supportable
1031 * but involves complex error handling, so it's being left until
1033 if (root
->number_of_cgroups
> 1)
1036 /* Process each subsystem */
1037 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
1038 struct cgroup_subsys
*ss
= subsys
[i
];
1039 unsigned long bit
= 1UL << i
;
1040 if (bit
& added_mask
) {
1041 /* We're binding this subsystem to this hierarchy */
1043 BUG_ON(cgrp
->subsys
[i
]);
1044 BUG_ON(!dummytop
->subsys
[i
]);
1045 BUG_ON(dummytop
->subsys
[i
]->cgroup
!= dummytop
);
1046 cgrp
->subsys
[i
] = dummytop
->subsys
[i
];
1047 cgrp
->subsys
[i
]->cgroup
= cgrp
;
1048 list_move(&ss
->sibling
, &root
->subsys_list
);
1052 /* refcount was already taken, and we're keeping it */
1053 } else if (bit
& removed_mask
) {
1054 /* We're removing this subsystem */
1056 BUG_ON(cgrp
->subsys
[i
] != dummytop
->subsys
[i
]);
1057 BUG_ON(cgrp
->subsys
[i
]->cgroup
!= cgrp
);
1060 dummytop
->subsys
[i
]->cgroup
= dummytop
;
1061 cgrp
->subsys
[i
] = NULL
;
1062 subsys
[i
]->root
= &rootnode
;
1063 list_move(&ss
->sibling
, &rootnode
.subsys_list
);
1064 /* subsystem is now free - drop reference on module */
1065 module_put(ss
->module
);
1066 } else if (bit
& final_subsys_mask
) {
1067 /* Subsystem state should already exist */
1069 BUG_ON(!cgrp
->subsys
[i
]);
1071 * a refcount was taken, but we already had one, so
1072 * drop the extra reference.
1074 module_put(ss
->module
);
1075 #ifdef CONFIG_MODULE_UNLOAD
1076 BUG_ON(ss
->module
&& !module_refcount(ss
->module
));
1079 /* Subsystem state shouldn't exist */
1080 BUG_ON(cgrp
->subsys
[i
]);
1083 root
->subsys_mask
= root
->actual_subsys_mask
= final_subsys_mask
;
1089 static int cgroup_show_options(struct seq_file
*seq
, struct dentry
*dentry
)
1091 struct cgroupfs_root
*root
= dentry
->d_sb
->s_fs_info
;
1092 struct cgroup_subsys
*ss
;
1094 mutex_lock(&cgroup_root_mutex
);
1095 for_each_subsys(root
, ss
)
1096 seq_printf(seq
, ",%s", ss
->name
);
1097 if (test_bit(ROOT_NOPREFIX
, &root
->flags
))
1098 seq_puts(seq
, ",noprefix");
1099 if (test_bit(ROOT_XATTR
, &root
->flags
))
1100 seq_puts(seq
, ",xattr");
1101 if (strlen(root
->release_agent_path
))
1102 seq_printf(seq
, ",release_agent=%s", root
->release_agent_path
);
1103 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN
, &root
->top_cgroup
.flags
))
1104 seq_puts(seq
, ",clone_children");
1105 if (strlen(root
->name
))
1106 seq_printf(seq
, ",name=%s", root
->name
);
1107 mutex_unlock(&cgroup_root_mutex
);
1111 struct cgroup_sb_opts
{
1112 unsigned long subsys_mask
;
1113 unsigned long flags
;
1114 char *release_agent
;
1115 bool cpuset_clone_children
;
1117 /* User explicitly requested empty subsystem */
1120 struct cgroupfs_root
*new_root
;
1125 * Convert a hierarchy specifier into a bitmask of subsystems and flags. Call
1126 * with cgroup_mutex held to protect the subsys[] array. This function takes
1127 * refcounts on subsystems to be used, unless it returns error, in which case
1128 * no refcounts are taken.
1130 static int parse_cgroupfs_options(char *data
, struct cgroup_sb_opts
*opts
)
1132 char *token
, *o
= data
;
1133 bool all_ss
= false, one_ss
= false;
1134 unsigned long mask
= (unsigned long)-1;
1136 bool module_pin_failed
= false;
1138 BUG_ON(!mutex_is_locked(&cgroup_mutex
));
1140 #ifdef CONFIG_CPUSETS
1141 mask
= ~(1UL << cpuset_subsys_id
);
1144 memset(opts
, 0, sizeof(*opts
));
1146 while ((token
= strsep(&o
, ",")) != NULL
) {
1149 if (!strcmp(token
, "none")) {
1150 /* Explicitly have no subsystems */
1154 if (!strcmp(token
, "all")) {
1155 /* Mutually exclusive option 'all' + subsystem name */
1161 if (!strcmp(token
, "noprefix")) {
1162 set_bit(ROOT_NOPREFIX
, &opts
->flags
);
1165 if (!strcmp(token
, "clone_children")) {
1166 opts
->cpuset_clone_children
= true;
1169 if (!strcmp(token
, "xattr")) {
1170 set_bit(ROOT_XATTR
, &opts
->flags
);
1173 if (!strncmp(token
, "release_agent=", 14)) {
1174 /* Specifying two release agents is forbidden */
1175 if (opts
->release_agent
)
1177 opts
->release_agent
=
1178 kstrndup(token
+ 14, PATH_MAX
- 1, GFP_KERNEL
);
1179 if (!opts
->release_agent
)
1183 if (!strncmp(token
, "name=", 5)) {
1184 const char *name
= token
+ 5;
1185 /* Can't specify an empty name */
1188 /* Must match [\w.-]+ */
1189 for (i
= 0; i
< strlen(name
); i
++) {
1193 if ((c
== '.') || (c
== '-') || (c
== '_'))
1197 /* Specifying two names is forbidden */
1200 opts
->name
= kstrndup(name
,
1201 MAX_CGROUP_ROOT_NAMELEN
- 1,
1209 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
1210 struct cgroup_subsys
*ss
= subsys
[i
];
1213 if (strcmp(token
, ss
->name
))
1218 /* Mutually exclusive option 'all' + subsystem name */
1221 set_bit(i
, &opts
->subsys_mask
);
1226 if (i
== CGROUP_SUBSYS_COUNT
)
1231 * If the 'all' option was specified select all the subsystems,
1232 * otherwise if 'none', 'name=' and a subsystem name options
1233 * were not specified, let's default to 'all'
1235 if (all_ss
|| (!one_ss
&& !opts
->none
&& !opts
->name
)) {
1236 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
1237 struct cgroup_subsys
*ss
= subsys
[i
];
1242 set_bit(i
, &opts
->subsys_mask
);
1246 /* Consistency checks */
1249 * Option noprefix was introduced just for backward compatibility
1250 * with the old cpuset, so we allow noprefix only if mounting just
1251 * the cpuset subsystem.
1253 if (test_bit(ROOT_NOPREFIX
, &opts
->flags
) &&
1254 (opts
->subsys_mask
& mask
))
1258 /* Can't specify "none" and some subsystems */
1259 if (opts
->subsys_mask
&& opts
->none
)
1263 * We either have to specify by name or by subsystems. (So all
1264 * empty hierarchies must have a name).
1266 if (!opts
->subsys_mask
&& !opts
->name
)
1270 * Grab references on all the modules we'll need, so the subsystems
1271 * don't dance around before rebind_subsystems attaches them. This may
1272 * take duplicate reference counts on a subsystem that's already used,
1273 * but rebind_subsystems handles this case.
1275 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
1276 unsigned long bit
= 1UL << i
;
1278 if (!(bit
& opts
->subsys_mask
))
1280 if (!try_module_get(subsys
[i
]->module
)) {
1281 module_pin_failed
= true;
1285 if (module_pin_failed
) {
1287 * oops, one of the modules was going away. this means that we
1288 * raced with a module_delete call, and to the user this is
1289 * essentially a "subsystem doesn't exist" case.
1291 for (i
--; i
>= 0; i
--) {
1292 /* drop refcounts only on the ones we took */
1293 unsigned long bit
= 1UL << i
;
1295 if (!(bit
& opts
->subsys_mask
))
1297 module_put(subsys
[i
]->module
);
1305 static void drop_parsed_module_refcounts(unsigned long subsys_mask
)
1308 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
1309 unsigned long bit
= 1UL << i
;
1311 if (!(bit
& subsys_mask
))
1313 module_put(subsys
[i
]->module
);
1317 static int cgroup_remount(struct super_block
*sb
, int *flags
, char *data
)
1320 struct cgroupfs_root
*root
= sb
->s_fs_info
;
1321 struct cgroup
*cgrp
= &root
->top_cgroup
;
1322 struct cgroup_sb_opts opts
;
1323 unsigned long added_mask
, removed_mask
;
1325 mutex_lock(&cgrp
->dentry
->d_inode
->i_mutex
);
1326 mutex_lock(&cgroup_mutex
);
1327 mutex_lock(&cgroup_root_mutex
);
1329 /* See what subsystems are wanted */
1330 ret
= parse_cgroupfs_options(data
, &opts
);
1334 /* See feature-removal-schedule.txt */
1335 if (opts
.subsys_mask
!= root
->actual_subsys_mask
|| opts
.release_agent
)
1336 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1337 task_tgid_nr(current
), current
->comm
);
1339 added_mask
= opts
.subsys_mask
& ~root
->subsys_mask
;
1340 removed_mask
= root
->subsys_mask
& ~opts
.subsys_mask
;
1342 /* Don't allow flags or name to change at remount */
1343 if (opts
.flags
!= root
->flags
||
1344 (opts
.name
&& strcmp(opts
.name
, root
->name
))) {
1346 drop_parsed_module_refcounts(opts
.subsys_mask
);
1350 ret
= rebind_subsystems(root
, opts
.subsys_mask
);
1352 drop_parsed_module_refcounts(opts
.subsys_mask
);
1356 /* clear out any existing files and repopulate subsystem files */
1357 cgroup_clear_directory(cgrp
->dentry
, false, removed_mask
);
1358 /* re-populate subsystem files */
1359 cgroup_populate_dir(cgrp
, false, added_mask
);
1361 if (opts
.release_agent
)
1362 strcpy(root
->release_agent_path
, opts
.release_agent
);
1364 kfree(opts
.release_agent
);
1366 mutex_unlock(&cgroup_root_mutex
);
1367 mutex_unlock(&cgroup_mutex
);
1368 mutex_unlock(&cgrp
->dentry
->d_inode
->i_mutex
);
1372 static const struct super_operations cgroup_ops
= {
1373 .statfs
= simple_statfs
,
1374 .drop_inode
= generic_delete_inode
,
1375 .show_options
= cgroup_show_options
,
1376 .remount_fs
= cgroup_remount
,
1379 static void init_cgroup_housekeeping(struct cgroup
*cgrp
)
1381 INIT_LIST_HEAD(&cgrp
->sibling
);
1382 INIT_LIST_HEAD(&cgrp
->children
);
1383 INIT_LIST_HEAD(&cgrp
->files
);
1384 INIT_LIST_HEAD(&cgrp
->css_sets
);
1385 INIT_LIST_HEAD(&cgrp
->allcg_node
);
1386 INIT_LIST_HEAD(&cgrp
->release_list
);
1387 INIT_LIST_HEAD(&cgrp
->pidlists
);
1388 mutex_init(&cgrp
->pidlist_mutex
);
1389 INIT_LIST_HEAD(&cgrp
->event_list
);
1390 spin_lock_init(&cgrp
->event_list_lock
);
1391 simple_xattrs_init(&cgrp
->xattrs
);
1394 static void init_cgroup_root(struct cgroupfs_root
*root
)
1396 struct cgroup
*cgrp
= &root
->top_cgroup
;
1398 INIT_LIST_HEAD(&root
->subsys_list
);
1399 INIT_LIST_HEAD(&root
->root_list
);
1400 INIT_LIST_HEAD(&root
->allcg_list
);
1401 root
->number_of_cgroups
= 1;
1403 cgrp
->top_cgroup
= cgrp
;
1404 list_add_tail(&cgrp
->allcg_node
, &root
->allcg_list
);
1405 init_cgroup_housekeeping(cgrp
);
1408 static bool init_root_id(struct cgroupfs_root
*root
)
1413 if (!ida_pre_get(&hierarchy_ida
, GFP_KERNEL
))
1415 spin_lock(&hierarchy_id_lock
);
1416 /* Try to allocate the next unused ID */
1417 ret
= ida_get_new_above(&hierarchy_ida
, next_hierarchy_id
,
1418 &root
->hierarchy_id
);
1420 /* Try again starting from 0 */
1421 ret
= ida_get_new(&hierarchy_ida
, &root
->hierarchy_id
);
1423 next_hierarchy_id
= root
->hierarchy_id
+ 1;
1424 } else if (ret
!= -EAGAIN
) {
1425 /* Can only get here if the 31-bit IDR is full ... */
1428 spin_unlock(&hierarchy_id_lock
);
1433 static int cgroup_test_super(struct super_block
*sb
, void *data
)
1435 struct cgroup_sb_opts
*opts
= data
;
1436 struct cgroupfs_root
*root
= sb
->s_fs_info
;
1438 /* If we asked for a name then it must match */
1439 if (opts
->name
&& strcmp(opts
->name
, root
->name
))
1443 * If we asked for subsystems (or explicitly for no
1444 * subsystems) then they must match
1446 if ((opts
->subsys_mask
|| opts
->none
)
1447 && (opts
->subsys_mask
!= root
->subsys_mask
))
1453 static struct cgroupfs_root
*cgroup_root_from_opts(struct cgroup_sb_opts
*opts
)
1455 struct cgroupfs_root
*root
;
1457 if (!opts
->subsys_mask
&& !opts
->none
)
1460 root
= kzalloc(sizeof(*root
), GFP_KERNEL
);
1462 return ERR_PTR(-ENOMEM
);
1464 if (!init_root_id(root
)) {
1466 return ERR_PTR(-ENOMEM
);
1468 init_cgroup_root(root
);
1470 root
->subsys_mask
= opts
->subsys_mask
;
1471 root
->flags
= opts
->flags
;
1472 ida_init(&root
->cgroup_ida
);
1473 if (opts
->release_agent
)
1474 strcpy(root
->release_agent_path
, opts
->release_agent
);
1476 strcpy(root
->name
, opts
->name
);
1477 if (opts
->cpuset_clone_children
)
1478 set_bit(CGRP_CPUSET_CLONE_CHILDREN
, &root
->top_cgroup
.flags
);
1482 static void cgroup_drop_root(struct cgroupfs_root
*root
)
1487 BUG_ON(!root
->hierarchy_id
);
1488 spin_lock(&hierarchy_id_lock
);
1489 ida_remove(&hierarchy_ida
, root
->hierarchy_id
);
1490 spin_unlock(&hierarchy_id_lock
);
1491 ida_destroy(&root
->cgroup_ida
);
1495 static int cgroup_set_super(struct super_block
*sb
, void *data
)
1498 struct cgroup_sb_opts
*opts
= data
;
1500 /* If we don't have a new root, we can't set up a new sb */
1501 if (!opts
->new_root
)
1504 BUG_ON(!opts
->subsys_mask
&& !opts
->none
);
1506 ret
= set_anon_super(sb
, NULL
);
1510 sb
->s_fs_info
= opts
->new_root
;
1511 opts
->new_root
->sb
= sb
;
1513 sb
->s_blocksize
= PAGE_CACHE_SIZE
;
1514 sb
->s_blocksize_bits
= PAGE_CACHE_SHIFT
;
1515 sb
->s_magic
= CGROUP_SUPER_MAGIC
;
1516 sb
->s_op
= &cgroup_ops
;
1521 static int cgroup_get_rootdir(struct super_block
*sb
)
1523 static const struct dentry_operations cgroup_dops
= {
1524 .d_iput
= cgroup_diput
,
1525 .d_delete
= cgroup_delete
,
1528 struct inode
*inode
=
1529 cgroup_new_inode(S_IFDIR
| S_IRUGO
| S_IXUGO
| S_IWUSR
, sb
);
1534 inode
->i_fop
= &simple_dir_operations
;
1535 inode
->i_op
= &cgroup_dir_inode_operations
;
1536 /* directories start off with i_nlink == 2 (for "." entry) */
1538 sb
->s_root
= d_make_root(inode
);
1541 /* for everything else we want ->d_op set */
1542 sb
->s_d_op
= &cgroup_dops
;
1546 static struct dentry
*cgroup_mount(struct file_system_type
*fs_type
,
1547 int flags
, const char *unused_dev_name
,
1550 struct cgroup_sb_opts opts
;
1551 struct cgroupfs_root
*root
;
1553 struct super_block
*sb
;
1554 struct cgroupfs_root
*new_root
;
1555 struct inode
*inode
;
1557 /* First find the desired set of subsystems */
1558 mutex_lock(&cgroup_mutex
);
1559 ret
= parse_cgroupfs_options(data
, &opts
);
1560 mutex_unlock(&cgroup_mutex
);
1565 * Allocate a new cgroup root. We may not need it if we're
1566 * reusing an existing hierarchy.
1568 new_root
= cgroup_root_from_opts(&opts
);
1569 if (IS_ERR(new_root
)) {
1570 ret
= PTR_ERR(new_root
);
1573 opts
.new_root
= new_root
;
1575 /* Locate an existing or new sb for this hierarchy */
1576 sb
= sget(fs_type
, cgroup_test_super
, cgroup_set_super
, 0, &opts
);
1579 cgroup_drop_root(opts
.new_root
);
1583 root
= sb
->s_fs_info
;
1585 if (root
== opts
.new_root
) {
1586 /* We used the new root structure, so this is a new hierarchy */
1587 struct list_head tmp_cg_links
;
1588 struct cgroup
*root_cgrp
= &root
->top_cgroup
;
1589 struct cgroupfs_root
*existing_root
;
1590 const struct cred
*cred
;
1593 BUG_ON(sb
->s_root
!= NULL
);
1595 ret
= cgroup_get_rootdir(sb
);
1597 goto drop_new_super
;
1598 inode
= sb
->s_root
->d_inode
;
1600 mutex_lock(&inode
->i_mutex
);
1601 mutex_lock(&cgroup_mutex
);
1602 mutex_lock(&cgroup_root_mutex
);
1604 /* Check for name clashes with existing mounts */
1606 if (strlen(root
->name
))
1607 for_each_active_root(existing_root
)
1608 if (!strcmp(existing_root
->name
, root
->name
))
1612 * We're accessing css_set_count without locking
1613 * css_set_lock here, but that's OK - it can only be
1614 * increased by someone holding cgroup_lock, and
1615 * that's us. The worst that can happen is that we
1616 * have some link structures left over
1618 ret
= allocate_cg_links(css_set_count
, &tmp_cg_links
);
1622 ret
= rebind_subsystems(root
, root
->subsys_mask
);
1623 if (ret
== -EBUSY
) {
1624 free_cg_links(&tmp_cg_links
);
1628 * There must be no failure case after here, since rebinding
1629 * takes care of subsystems' refcounts, which are explicitly
1630 * dropped in the failure exit path.
1633 /* EBUSY should be the only error here */
1636 list_add(&root
->root_list
, &roots
);
1639 sb
->s_root
->d_fsdata
= root_cgrp
;
1640 root
->top_cgroup
.dentry
= sb
->s_root
;
1642 /* Link the top cgroup in this hierarchy into all
1643 * the css_set objects */
1644 write_lock(&css_set_lock
);
1645 for (i
= 0; i
< CSS_SET_TABLE_SIZE
; i
++) {
1646 struct hlist_head
*hhead
= &css_set_table
[i
];
1647 struct hlist_node
*node
;
1650 hlist_for_each_entry(cg
, node
, hhead
, hlist
)
1651 link_css_set(&tmp_cg_links
, cg
, root_cgrp
);
1653 write_unlock(&css_set_lock
);
1655 free_cg_links(&tmp_cg_links
);
1657 BUG_ON(!list_empty(&root_cgrp
->children
));
1658 BUG_ON(root
->number_of_cgroups
!= 1);
1660 cred
= override_creds(&init_cred
);
1661 cgroup_populate_dir(root_cgrp
, true, root
->subsys_mask
);
1663 mutex_unlock(&cgroup_root_mutex
);
1664 mutex_unlock(&cgroup_mutex
);
1665 mutex_unlock(&inode
->i_mutex
);
1668 * We re-used an existing hierarchy - the new root (if
1669 * any) is not needed
1671 cgroup_drop_root(opts
.new_root
);
1672 /* no subsys rebinding, so refcounts don't change */
1673 drop_parsed_module_refcounts(opts
.subsys_mask
);
1676 kfree(opts
.release_agent
);
1678 return dget(sb
->s_root
);
1681 mutex_unlock(&cgroup_root_mutex
);
1682 mutex_unlock(&cgroup_mutex
);
1683 mutex_unlock(&inode
->i_mutex
);
1685 deactivate_locked_super(sb
);
1687 drop_parsed_module_refcounts(opts
.subsys_mask
);
1689 kfree(opts
.release_agent
);
1691 return ERR_PTR(ret
);
1694 static void cgroup_kill_sb(struct super_block
*sb
) {
1695 struct cgroupfs_root
*root
= sb
->s_fs_info
;
1696 struct cgroup
*cgrp
= &root
->top_cgroup
;
1698 struct cg_cgroup_link
*link
;
1699 struct cg_cgroup_link
*saved_link
;
1703 BUG_ON(root
->number_of_cgroups
!= 1);
1704 BUG_ON(!list_empty(&cgrp
->children
));
1706 mutex_lock(&cgroup_mutex
);
1707 mutex_lock(&cgroup_root_mutex
);
1709 /* Rebind all subsystems back to the default hierarchy */
1710 ret
= rebind_subsystems(root
, 0);
1711 /* Shouldn't be able to fail ... */
1715 * Release all the links from css_sets to this hierarchy's
1718 write_lock(&css_set_lock
);
1720 list_for_each_entry_safe(link
, saved_link
, &cgrp
->css_sets
,
1722 list_del(&link
->cg_link_list
);
1723 list_del(&link
->cgrp_link_list
);
1726 write_unlock(&css_set_lock
);
1728 if (!list_empty(&root
->root_list
)) {
1729 list_del(&root
->root_list
);
1733 mutex_unlock(&cgroup_root_mutex
);
1734 mutex_unlock(&cgroup_mutex
);
1736 simple_xattrs_free(&cgrp
->xattrs
);
1738 kill_litter_super(sb
);
1739 cgroup_drop_root(root
);
1742 static struct file_system_type cgroup_fs_type
= {
1744 .mount
= cgroup_mount
,
1745 .kill_sb
= cgroup_kill_sb
,
1748 static struct kobject
*cgroup_kobj
;
1751 * cgroup_path - generate the path of a cgroup
1752 * @cgrp: the cgroup in question
1753 * @buf: the buffer to write the path into
1754 * @buflen: the length of the buffer
1756 * Called with cgroup_mutex held or else with an RCU-protected cgroup
1757 * reference. Writes path of cgroup into buf. Returns 0 on success,
1760 int cgroup_path(const struct cgroup
*cgrp
, char *buf
, int buflen
)
1762 struct dentry
*dentry
= cgrp
->dentry
;
1765 rcu_lockdep_assert(rcu_read_lock_held() || cgroup_lock_is_held(),
1766 "cgroup_path() called without proper locking");
1768 if (!dentry
|| cgrp
== dummytop
) {
1770 * Inactive subsystems have no dentry for their root
1777 start
= buf
+ buflen
- 1;
1781 int len
= dentry
->d_name
.len
;
1783 if ((start
-= len
) < buf
)
1784 return -ENAMETOOLONG
;
1785 memcpy(start
, dentry
->d_name
.name
, len
);
1786 cgrp
= cgrp
->parent
;
1790 dentry
= cgrp
->dentry
;
1794 return -ENAMETOOLONG
;
1797 memmove(buf
, start
, buf
+ buflen
- start
);
1800 EXPORT_SYMBOL_GPL(cgroup_path
);
1803 * Control Group taskset
1805 struct task_and_cgroup
{
1806 struct task_struct
*task
;
1807 struct cgroup
*cgrp
;
1811 struct cgroup_taskset
{
1812 struct task_and_cgroup single
;
1813 struct flex_array
*tc_array
;
1816 struct cgroup
*cur_cgrp
;
1820 * cgroup_taskset_first - reset taskset and return the first task
1821 * @tset: taskset of interest
1823 * @tset iteration is initialized and the first task is returned.
1825 struct task_struct
*cgroup_taskset_first(struct cgroup_taskset
*tset
)
1827 if (tset
->tc_array
) {
1829 return cgroup_taskset_next(tset
);
1831 tset
->cur_cgrp
= tset
->single
.cgrp
;
1832 return tset
->single
.task
;
1835 EXPORT_SYMBOL_GPL(cgroup_taskset_first
);
1838 * cgroup_taskset_next - iterate to the next task in taskset
1839 * @tset: taskset of interest
1841 * Return the next task in @tset. Iteration must have been initialized
1842 * with cgroup_taskset_first().
1844 struct task_struct
*cgroup_taskset_next(struct cgroup_taskset
*tset
)
1846 struct task_and_cgroup
*tc
;
1848 if (!tset
->tc_array
|| tset
->idx
>= tset
->tc_array_len
)
1851 tc
= flex_array_get(tset
->tc_array
, tset
->idx
++);
1852 tset
->cur_cgrp
= tc
->cgrp
;
1855 EXPORT_SYMBOL_GPL(cgroup_taskset_next
);
1858 * cgroup_taskset_cur_cgroup - return the matching cgroup for the current task
1859 * @tset: taskset of interest
1861 * Return the cgroup for the current (last returned) task of @tset. This
1862 * function must be preceded by either cgroup_taskset_first() or
1863 * cgroup_taskset_next().
1865 struct cgroup
*cgroup_taskset_cur_cgroup(struct cgroup_taskset
*tset
)
1867 return tset
->cur_cgrp
;
1869 EXPORT_SYMBOL_GPL(cgroup_taskset_cur_cgroup
);
1872 * cgroup_taskset_size - return the number of tasks in taskset
1873 * @tset: taskset of interest
1875 int cgroup_taskset_size(struct cgroup_taskset
*tset
)
1877 return tset
->tc_array
? tset
->tc_array_len
: 1;
1879 EXPORT_SYMBOL_GPL(cgroup_taskset_size
);
1883 * cgroup_task_migrate - move a task from one cgroup to another.
1885 * Must be called with cgroup_mutex and threadgroup locked.
1887 static void cgroup_task_migrate(struct cgroup
*cgrp
, struct cgroup
*oldcgrp
,
1888 struct task_struct
*tsk
, struct css_set
*newcg
)
1890 struct css_set
*oldcg
;
1893 * We are synchronized through threadgroup_lock() against PF_EXITING
1894 * setting such that we can't race against cgroup_exit() changing the
1895 * css_set to init_css_set and dropping the old one.
1897 WARN_ON_ONCE(tsk
->flags
& PF_EXITING
);
1898 oldcg
= tsk
->cgroups
;
1901 rcu_assign_pointer(tsk
->cgroups
, newcg
);
1904 /* Update the css_set linked lists if we're using them */
1905 write_lock(&css_set_lock
);
1906 if (!list_empty(&tsk
->cg_list
))
1907 list_move(&tsk
->cg_list
, &newcg
->tasks
);
1908 write_unlock(&css_set_lock
);
1911 * We just gained a reference on oldcg by taking it from the task. As
1912 * trading it for newcg is protected by cgroup_mutex, we're safe to drop
1913 * it here; it will be freed under RCU.
1915 set_bit(CGRP_RELEASABLE
, &oldcgrp
->flags
);
1920 * cgroup_attach_task - attach task 'tsk' to cgroup 'cgrp'
1921 * @cgrp: the cgroup the task is attaching to
1922 * @tsk: the task to be attached
1924 * Call with cgroup_mutex and threadgroup locked. May take task_lock of
1927 int cgroup_attach_task(struct cgroup
*cgrp
, struct task_struct
*tsk
)
1930 struct cgroup_subsys
*ss
, *failed_ss
= NULL
;
1931 struct cgroup
*oldcgrp
;
1932 struct cgroupfs_root
*root
= cgrp
->root
;
1933 struct cgroup_taskset tset
= { };
1934 struct css_set
*newcg
;
1936 /* @tsk either already exited or can't exit until the end */
1937 if (tsk
->flags
& PF_EXITING
)
1940 /* Nothing to do if the task is already in that cgroup */
1941 oldcgrp
= task_cgroup_from_root(tsk
, root
);
1942 if (cgrp
== oldcgrp
)
1945 tset
.single
.task
= tsk
;
1946 tset
.single
.cgrp
= oldcgrp
;
1948 for_each_subsys(root
, ss
) {
1949 if (ss
->can_attach
) {
1950 retval
= ss
->can_attach(cgrp
, &tset
);
1953 * Remember on which subsystem the can_attach()
1954 * failed, so that we only call cancel_attach()
1955 * against the subsystems whose can_attach()
1956 * succeeded. (See below)
1964 newcg
= find_css_set(tsk
->cgroups
, cgrp
);
1970 cgroup_task_migrate(cgrp
, oldcgrp
, tsk
, newcg
);
1972 for_each_subsys(root
, ss
) {
1974 ss
->attach(cgrp
, &tset
);
1980 for_each_subsys(root
, ss
) {
1981 if (ss
== failed_ss
)
1983 * This subsystem was the one that failed the
1984 * can_attach() check earlier, so we don't need
1985 * to call cancel_attach() against it or any
1986 * remaining subsystems.
1989 if (ss
->cancel_attach
)
1990 ss
->cancel_attach(cgrp
, &tset
);
1997 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
1998 * @from: attach to all cgroups of a given task
1999 * @tsk: the task to be attached
2001 int cgroup_attach_task_all(struct task_struct
*from
, struct task_struct
*tsk
)
2003 struct cgroupfs_root
*root
;
2007 for_each_active_root(root
) {
2008 struct cgroup
*from_cg
= task_cgroup_from_root(from
, root
);
2010 retval
= cgroup_attach_task(from_cg
, tsk
);
2018 EXPORT_SYMBOL_GPL(cgroup_attach_task_all
);
2021 * cgroup_attach_proc - attach all threads in a threadgroup to a cgroup
2022 * @cgrp: the cgroup to attach to
2023 * @leader: the threadgroup leader task_struct of the group to be attached
2025 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
2026 * task_lock of each thread in leader's threadgroup individually in turn.
2028 static int cgroup_attach_proc(struct cgroup
*cgrp
, struct task_struct
*leader
)
2030 int retval
, i
, group_size
;
2031 struct cgroup_subsys
*ss
, *failed_ss
= NULL
;
2032 /* guaranteed to be initialized later, but the compiler needs this */
2033 struct cgroupfs_root
*root
= cgrp
->root
;
2034 /* threadgroup list cursor and array */
2035 struct task_struct
*tsk
;
2036 struct task_and_cgroup
*tc
;
2037 struct flex_array
*group
;
2038 struct cgroup_taskset tset
= { };
2041 * step 0: in order to do expensive, possibly blocking operations for
2042 * every thread, we cannot iterate the thread group list, since it needs
2043 * rcu or tasklist locked. instead, build an array of all threads in the
2044 * group - group_rwsem prevents new threads from appearing, and if
2045 * threads exit, this will just be an over-estimate.
2047 group_size
= get_nr_threads(leader
);
2048 /* flex_array supports very large thread-groups better than kmalloc. */
2049 group
= flex_array_alloc(sizeof(*tc
), group_size
, GFP_KERNEL
);
2052 /* pre-allocate to guarantee space while iterating in rcu read-side. */
2053 retval
= flex_array_prealloc(group
, 0, group_size
- 1, GFP_KERNEL
);
2055 goto out_free_group_list
;
2060 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2061 * already PF_EXITING could be freed from underneath us unless we
2062 * take an rcu_read_lock.
2066 struct task_and_cgroup ent
;
2068 /* @tsk either already exited or can't exit until the end */
2069 if (tsk
->flags
& PF_EXITING
)
2072 /* as per above, nr_threads may decrease, but not increase. */
2073 BUG_ON(i
>= group_size
);
2075 ent
.cgrp
= task_cgroup_from_root(tsk
, root
);
2076 /* nothing to do if this task is already in the cgroup */
2077 if (ent
.cgrp
== cgrp
)
2080 * saying GFP_ATOMIC has no effect here because we did prealloc
2081 * earlier, but it's good form to communicate our expectations.
2083 retval
= flex_array_put(group
, i
, &ent
, GFP_ATOMIC
);
2084 BUG_ON(retval
!= 0);
2086 } while_each_thread(leader
, tsk
);
2088 /* remember the number of threads in the array for later. */
2090 tset
.tc_array
= group
;
2091 tset
.tc_array_len
= group_size
;
2093 /* methods shouldn't be called if no task is actually migrating */
2096 goto out_free_group_list
;
2099 * step 1: check that we can legitimately attach to the cgroup.
2101 for_each_subsys(root
, ss
) {
2102 if (ss
->can_attach
) {
2103 retval
= ss
->can_attach(cgrp
, &tset
);
2106 goto out_cancel_attach
;
2112 * step 2: make sure css_sets exist for all threads to be migrated.
2113 * we use find_css_set, which allocates a new one if necessary.
2115 for (i
= 0; i
< group_size
; i
++) {
2116 tc
= flex_array_get(group
, i
);
2117 tc
->cg
= find_css_set(tc
->task
->cgroups
, cgrp
);
2120 goto out_put_css_set_refs
;
2125 * step 3: now that we're guaranteed success wrt the css_sets,
2126 * proceed to move all tasks to the new cgroup. There are no
2127 * failure cases after here, so this is the commit point.
2129 for (i
= 0; i
< group_size
; i
++) {
2130 tc
= flex_array_get(group
, i
);
2131 cgroup_task_migrate(cgrp
, tc
->cgrp
, tc
->task
, tc
->cg
);
2133 /* nothing is sensitive to fork() after this point. */
2136 * step 4: do subsystem attach callbacks.
2138 for_each_subsys(root
, ss
) {
2140 ss
->attach(cgrp
, &tset
);
2144 * step 5: success! and cleanup
2148 out_put_css_set_refs
:
2150 for (i
= 0; i
< group_size
; i
++) {
2151 tc
= flex_array_get(group
, i
);
2154 put_css_set(tc
->cg
);
2159 for_each_subsys(root
, ss
) {
2160 if (ss
== failed_ss
)
2162 if (ss
->cancel_attach
)
2163 ss
->cancel_attach(cgrp
, &tset
);
2166 out_free_group_list
:
2167 flex_array_free(group
);
2172 * Find the task_struct of the task to attach by vpid and pass it along to the
2173 * function to attach either it or all tasks in its threadgroup. Will lock
2174 * cgroup_mutex and threadgroup; may take task_lock of task.
2176 static int attach_task_by_pid(struct cgroup
*cgrp
, u64 pid
, bool threadgroup
)
2178 struct task_struct
*tsk
;
2179 const struct cred
*cred
= current_cred(), *tcred
;
2182 if (!cgroup_lock_live_group(cgrp
))
2188 tsk
= find_task_by_vpid(pid
);
2192 goto out_unlock_cgroup
;
2195 * even if we're attaching all tasks in the thread group, we
2196 * only need to check permissions on one of them.
2198 tcred
= __task_cred(tsk
);
2199 if (!uid_eq(cred
->euid
, GLOBAL_ROOT_UID
) &&
2200 !uid_eq(cred
->euid
, tcred
->uid
) &&
2201 !uid_eq(cred
->euid
, tcred
->suid
)) {
2204 goto out_unlock_cgroup
;
2210 tsk
= tsk
->group_leader
;
2213 * Workqueue threads may acquire PF_THREAD_BOUND and become
2214 * trapped in a cpuset, or RT worker may be born in a cgroup
2215 * with no rt_runtime allocated. Just say no.
2217 if (tsk
== kthreadd_task
|| (tsk
->flags
& PF_THREAD_BOUND
)) {
2220 goto out_unlock_cgroup
;
2223 get_task_struct(tsk
);
2226 threadgroup_lock(tsk
);
2228 if (!thread_group_leader(tsk
)) {
2230 * a race with de_thread from another thread's exec()
2231 * may strip us of our leadership, if this happens,
2232 * there is no choice but to throw this task away and
2233 * try again; this is
2234 * "double-double-toil-and-trouble-check locking".
2236 threadgroup_unlock(tsk
);
2237 put_task_struct(tsk
);
2238 goto retry_find_task
;
2240 ret
= cgroup_attach_proc(cgrp
, tsk
);
2242 ret
= cgroup_attach_task(cgrp
, tsk
);
2243 threadgroup_unlock(tsk
);
2245 put_task_struct(tsk
);
2251 static int cgroup_tasks_write(struct cgroup
*cgrp
, struct cftype
*cft
, u64 pid
)
2253 return attach_task_by_pid(cgrp
, pid
, false);
2256 static int cgroup_procs_write(struct cgroup
*cgrp
, struct cftype
*cft
, u64 tgid
)
2258 return attach_task_by_pid(cgrp
, tgid
, true);
2262 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
2263 * @cgrp: the cgroup to be checked for liveness
2265 * On success, returns true; the lock should be later released with
2266 * cgroup_unlock(). On failure returns false with no lock held.
2268 bool cgroup_lock_live_group(struct cgroup
*cgrp
)
2270 mutex_lock(&cgroup_mutex
);
2271 if (cgroup_is_removed(cgrp
)) {
2272 mutex_unlock(&cgroup_mutex
);
2277 EXPORT_SYMBOL_GPL(cgroup_lock_live_group
);
2279 static int cgroup_release_agent_write(struct cgroup
*cgrp
, struct cftype
*cft
,
2282 BUILD_BUG_ON(sizeof(cgrp
->root
->release_agent_path
) < PATH_MAX
);
2283 if (strlen(buffer
) >= PATH_MAX
)
2285 if (!cgroup_lock_live_group(cgrp
))
2287 mutex_lock(&cgroup_root_mutex
);
2288 strcpy(cgrp
->root
->release_agent_path
, buffer
);
2289 mutex_unlock(&cgroup_root_mutex
);
2294 static int cgroup_release_agent_show(struct cgroup
*cgrp
, struct cftype
*cft
,
2295 struct seq_file
*seq
)
2297 if (!cgroup_lock_live_group(cgrp
))
2299 seq_puts(seq
, cgrp
->root
->release_agent_path
);
2300 seq_putc(seq
, '\n');
2305 /* A buffer size big enough for numbers or short strings */
2306 #define CGROUP_LOCAL_BUFFER_SIZE 64
2308 static ssize_t
cgroup_write_X64(struct cgroup
*cgrp
, struct cftype
*cft
,
2310 const char __user
*userbuf
,
2311 size_t nbytes
, loff_t
*unused_ppos
)
2313 char buffer
[CGROUP_LOCAL_BUFFER_SIZE
];
2319 if (nbytes
>= sizeof(buffer
))
2321 if (copy_from_user(buffer
, userbuf
, nbytes
))
2324 buffer
[nbytes
] = 0; /* nul-terminate */
2325 if (cft
->write_u64
) {
2326 u64 val
= simple_strtoull(strstrip(buffer
), &end
, 0);
2329 retval
= cft
->write_u64(cgrp
, cft
, val
);
2331 s64 val
= simple_strtoll(strstrip(buffer
), &end
, 0);
2334 retval
= cft
->write_s64(cgrp
, cft
, val
);
2341 static ssize_t
cgroup_write_string(struct cgroup
*cgrp
, struct cftype
*cft
,
2343 const char __user
*userbuf
,
2344 size_t nbytes
, loff_t
*unused_ppos
)
2346 char local_buffer
[CGROUP_LOCAL_BUFFER_SIZE
];
2348 size_t max_bytes
= cft
->max_write_len
;
2349 char *buffer
= local_buffer
;
2352 max_bytes
= sizeof(local_buffer
) - 1;
2353 if (nbytes
>= max_bytes
)
2355 /* Allocate a dynamic buffer if we need one */
2356 if (nbytes
>= sizeof(local_buffer
)) {
2357 buffer
= kmalloc(nbytes
+ 1, GFP_KERNEL
);
2361 if (nbytes
&& copy_from_user(buffer
, userbuf
, nbytes
)) {
2366 buffer
[nbytes
] = 0; /* nul-terminate */
2367 retval
= cft
->write_string(cgrp
, cft
, strstrip(buffer
));
2371 if (buffer
!= local_buffer
)
2376 static ssize_t
cgroup_file_write(struct file
*file
, const char __user
*buf
,
2377 size_t nbytes
, loff_t
*ppos
)
2379 struct cftype
*cft
= __d_cft(file
->f_dentry
);
2380 struct cgroup
*cgrp
= __d_cgrp(file
->f_dentry
->d_parent
);
2382 if (cgroup_is_removed(cgrp
))
2385 return cft
->write(cgrp
, cft
, file
, buf
, nbytes
, ppos
);
2386 if (cft
->write_u64
|| cft
->write_s64
)
2387 return cgroup_write_X64(cgrp
, cft
, file
, buf
, nbytes
, ppos
);
2388 if (cft
->write_string
)
2389 return cgroup_write_string(cgrp
, cft
, file
, buf
, nbytes
, ppos
);
2391 int ret
= cft
->trigger(cgrp
, (unsigned int)cft
->private);
2392 return ret
? ret
: nbytes
;
2397 static ssize_t
cgroup_read_u64(struct cgroup
*cgrp
, struct cftype
*cft
,
2399 char __user
*buf
, size_t nbytes
,
2402 char tmp
[CGROUP_LOCAL_BUFFER_SIZE
];
2403 u64 val
= cft
->read_u64(cgrp
, cft
);
2404 int len
= sprintf(tmp
, "%llu\n", (unsigned long long) val
);
2406 return simple_read_from_buffer(buf
, nbytes
, ppos
, tmp
, len
);
2409 static ssize_t
cgroup_read_s64(struct cgroup
*cgrp
, struct cftype
*cft
,
2411 char __user
*buf
, size_t nbytes
,
2414 char tmp
[CGROUP_LOCAL_BUFFER_SIZE
];
2415 s64 val
= cft
->read_s64(cgrp
, cft
);
2416 int len
= sprintf(tmp
, "%lld\n", (long long) val
);
2418 return simple_read_from_buffer(buf
, nbytes
, ppos
, tmp
, len
);
2421 static ssize_t
cgroup_file_read(struct file
*file
, char __user
*buf
,
2422 size_t nbytes
, loff_t
*ppos
)
2424 struct cftype
*cft
= __d_cft(file
->f_dentry
);
2425 struct cgroup
*cgrp
= __d_cgrp(file
->f_dentry
->d_parent
);
2427 if (cgroup_is_removed(cgrp
))
2431 return cft
->read(cgrp
, cft
, file
, buf
, nbytes
, ppos
);
2433 return cgroup_read_u64(cgrp
, cft
, file
, buf
, nbytes
, ppos
);
2435 return cgroup_read_s64(cgrp
, cft
, file
, buf
, nbytes
, ppos
);
2440 * seqfile ops/methods for returning structured data. Currently just
2441 * supports string->u64 maps, but can be extended in future.
2444 struct cgroup_seqfile_state
{
2446 struct cgroup
*cgroup
;
2449 static int cgroup_map_add(struct cgroup_map_cb
*cb
, const char *key
, u64 value
)
2451 struct seq_file
*sf
= cb
->state
;
2452 return seq_printf(sf
, "%s %llu\n", key
, (unsigned long long)value
);
2455 static int cgroup_seqfile_show(struct seq_file
*m
, void *arg
)
2457 struct cgroup_seqfile_state
*state
= m
->private;
2458 struct cftype
*cft
= state
->cft
;
2459 if (cft
->read_map
) {
2460 struct cgroup_map_cb cb
= {
2461 .fill
= cgroup_map_add
,
2464 return cft
->read_map(state
->cgroup
, cft
, &cb
);
2466 return cft
->read_seq_string(state
->cgroup
, cft
, m
);
2469 static int cgroup_seqfile_release(struct inode
*inode
, struct file
*file
)
2471 struct seq_file
*seq
= file
->private_data
;
2472 kfree(seq
->private);
2473 return single_release(inode
, file
);
2476 static const struct file_operations cgroup_seqfile_operations
= {
2478 .write
= cgroup_file_write
,
2479 .llseek
= seq_lseek
,
2480 .release
= cgroup_seqfile_release
,
2483 static int cgroup_file_open(struct inode
*inode
, struct file
*file
)
2488 err
= generic_file_open(inode
, file
);
2491 cft
= __d_cft(file
->f_dentry
);
2493 if (cft
->read_map
|| cft
->read_seq_string
) {
2494 struct cgroup_seqfile_state
*state
=
2495 kzalloc(sizeof(*state
), GFP_USER
);
2499 state
->cgroup
= __d_cgrp(file
->f_dentry
->d_parent
);
2500 file
->f_op
= &cgroup_seqfile_operations
;
2501 err
= single_open(file
, cgroup_seqfile_show
, state
);
2504 } else if (cft
->open
)
2505 err
= cft
->open(inode
, file
);
2512 static int cgroup_file_release(struct inode
*inode
, struct file
*file
)
2514 struct cftype
*cft
= __d_cft(file
->f_dentry
);
2516 return cft
->release(inode
, file
);
2521 * cgroup_rename - Only allow simple rename of directories in place.
2523 static int cgroup_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
2524 struct inode
*new_dir
, struct dentry
*new_dentry
)
2526 if (!S_ISDIR(old_dentry
->d_inode
->i_mode
))
2528 if (new_dentry
->d_inode
)
2530 if (old_dir
!= new_dir
)
2532 return simple_rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
2535 static struct simple_xattrs
*__d_xattrs(struct dentry
*dentry
)
2537 if (S_ISDIR(dentry
->d_inode
->i_mode
))
2538 return &__d_cgrp(dentry
)->xattrs
;
2540 return &__d_cft(dentry
)->xattrs
;
2543 static inline int xattr_enabled(struct dentry
*dentry
)
2545 struct cgroupfs_root
*root
= dentry
->d_sb
->s_fs_info
;
2546 return test_bit(ROOT_XATTR
, &root
->flags
);
2549 static bool is_valid_xattr(const char *name
)
2551 if (!strncmp(name
, XATTR_TRUSTED_PREFIX
, XATTR_TRUSTED_PREFIX_LEN
) ||
2552 !strncmp(name
, XATTR_SECURITY_PREFIX
, XATTR_SECURITY_PREFIX_LEN
))
2557 static int cgroup_setxattr(struct dentry
*dentry
, const char *name
,
2558 const void *val
, size_t size
, int flags
)
2560 if (!xattr_enabled(dentry
))
2562 if (!is_valid_xattr(name
))
2564 return simple_xattr_set(__d_xattrs(dentry
), name
, val
, size
, flags
);
2567 static int cgroup_removexattr(struct dentry
*dentry
, const char *name
)
2569 if (!xattr_enabled(dentry
))
2571 if (!is_valid_xattr(name
))
2573 return simple_xattr_remove(__d_xattrs(dentry
), name
);
2576 static ssize_t
cgroup_getxattr(struct dentry
*dentry
, const char *name
,
2577 void *buf
, size_t size
)
2579 if (!xattr_enabled(dentry
))
2581 if (!is_valid_xattr(name
))
2583 return simple_xattr_get(__d_xattrs(dentry
), name
, buf
, size
);
2586 static ssize_t
cgroup_listxattr(struct dentry
*dentry
, char *buf
, size_t size
)
2588 if (!xattr_enabled(dentry
))
2590 return simple_xattr_list(__d_xattrs(dentry
), buf
, size
);
2593 static const struct file_operations cgroup_file_operations
= {
2594 .read
= cgroup_file_read
,
2595 .write
= cgroup_file_write
,
2596 .llseek
= generic_file_llseek
,
2597 .open
= cgroup_file_open
,
2598 .release
= cgroup_file_release
,
2601 static const struct inode_operations cgroup_file_inode_operations
= {
2602 .setxattr
= cgroup_setxattr
,
2603 .getxattr
= cgroup_getxattr
,
2604 .listxattr
= cgroup_listxattr
,
2605 .removexattr
= cgroup_removexattr
,
2608 static const struct inode_operations cgroup_dir_inode_operations
= {
2609 .lookup
= cgroup_lookup
,
2610 .mkdir
= cgroup_mkdir
,
2611 .rmdir
= cgroup_rmdir
,
2612 .rename
= cgroup_rename
,
2613 .setxattr
= cgroup_setxattr
,
2614 .getxattr
= cgroup_getxattr
,
2615 .listxattr
= cgroup_listxattr
,
2616 .removexattr
= cgroup_removexattr
,
2619 static struct dentry
*cgroup_lookup(struct inode
*dir
, struct dentry
*dentry
, unsigned int flags
)
2621 if (dentry
->d_name
.len
> NAME_MAX
)
2622 return ERR_PTR(-ENAMETOOLONG
);
2623 d_add(dentry
, NULL
);
2628 * Check if a file is a control file
2630 static inline struct cftype
*__file_cft(struct file
*file
)
2632 if (file
->f_dentry
->d_inode
->i_fop
!= &cgroup_file_operations
)
2633 return ERR_PTR(-EINVAL
);
2634 return __d_cft(file
->f_dentry
);
2637 static int cgroup_create_file(struct dentry
*dentry
, umode_t mode
,
2638 struct super_block
*sb
)
2640 struct inode
*inode
;
2644 if (dentry
->d_inode
)
2647 inode
= cgroup_new_inode(mode
, sb
);
2651 if (S_ISDIR(mode
)) {
2652 inode
->i_op
= &cgroup_dir_inode_operations
;
2653 inode
->i_fop
= &simple_dir_operations
;
2655 /* start off with i_nlink == 2 (for "." entry) */
2657 inc_nlink(dentry
->d_parent
->d_inode
);
2660 * Control reaches here with cgroup_mutex held.
2661 * @inode->i_mutex should nest outside cgroup_mutex but we
2662 * want to populate it immediately without releasing
2663 * cgroup_mutex. As @inode isn't visible to anyone else
2664 * yet, trylock will always succeed without affecting
2667 WARN_ON_ONCE(!mutex_trylock(&inode
->i_mutex
));
2668 } else if (S_ISREG(mode
)) {
2670 inode
->i_fop
= &cgroup_file_operations
;
2671 inode
->i_op
= &cgroup_file_inode_operations
;
2673 d_instantiate(dentry
, inode
);
2674 dget(dentry
); /* Extra count - pin the dentry in core */
2679 * cgroup_file_mode - deduce file mode of a control file
2680 * @cft: the control file in question
2682 * returns cft->mode if ->mode is not 0
2683 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2684 * returns S_IRUGO if it has only a read handler
2685 * returns S_IWUSR if it has only a write hander
2687 static umode_t
cgroup_file_mode(const struct cftype
*cft
)
2694 if (cft
->read
|| cft
->read_u64
|| cft
->read_s64
||
2695 cft
->read_map
|| cft
->read_seq_string
)
2698 if (cft
->write
|| cft
->write_u64
|| cft
->write_s64
||
2699 cft
->write_string
|| cft
->trigger
)
2705 static int cgroup_add_file(struct cgroup
*cgrp
, struct cgroup_subsys
*subsys
,
2708 struct dentry
*dir
= cgrp
->dentry
;
2709 struct cgroup
*parent
= __d_cgrp(dir
);
2710 struct dentry
*dentry
;
2714 char name
[MAX_CGROUP_TYPE_NAMELEN
+ MAX_CFTYPE_NAME
+ 2] = { 0 };
2716 simple_xattrs_init(&cft
->xattrs
);
2718 /* does @cft->flags tell us to skip creation on @cgrp? */
2719 if ((cft
->flags
& CFTYPE_NOT_ON_ROOT
) && !cgrp
->parent
)
2721 if ((cft
->flags
& CFTYPE_ONLY_ON_ROOT
) && cgrp
->parent
)
2724 if (subsys
&& !test_bit(ROOT_NOPREFIX
, &cgrp
->root
->flags
)) {
2725 strcpy(name
, subsys
->name
);
2728 strcat(name
, cft
->name
);
2730 BUG_ON(!mutex_is_locked(&dir
->d_inode
->i_mutex
));
2732 cfe
= kzalloc(sizeof(*cfe
), GFP_KERNEL
);
2736 dentry
= lookup_one_len(name
, dir
, strlen(name
));
2737 if (IS_ERR(dentry
)) {
2738 error
= PTR_ERR(dentry
);
2742 mode
= cgroup_file_mode(cft
);
2743 error
= cgroup_create_file(dentry
, mode
| S_IFREG
, cgrp
->root
->sb
);
2745 cfe
->type
= (void *)cft
;
2746 cfe
->dentry
= dentry
;
2747 dentry
->d_fsdata
= cfe
;
2748 list_add_tail(&cfe
->node
, &parent
->files
);
2757 static int cgroup_addrm_files(struct cgroup
*cgrp
, struct cgroup_subsys
*subsys
,
2758 struct cftype cfts
[], bool is_add
)
2763 for (cft
= cfts
; cft
->name
[0] != '\0'; cft
++) {
2765 err
= cgroup_add_file(cgrp
, subsys
, cft
);
2767 err
= cgroup_rm_file(cgrp
, cft
);
2769 pr_warning("cgroup_addrm_files: failed to %s %s, err=%d\n",
2770 is_add
? "add" : "remove", cft
->name
, err
);
2777 static DEFINE_MUTEX(cgroup_cft_mutex
);
2779 static void cgroup_cfts_prepare(void)
2780 __acquires(&cgroup_cft_mutex
) __acquires(&cgroup_mutex
)
2783 * Thanks to the entanglement with vfs inode locking, we can't walk
2784 * the existing cgroups under cgroup_mutex and create files.
2785 * Instead, we increment reference on all cgroups and build list of
2786 * them using @cgrp->cft_q_node. Grab cgroup_cft_mutex to ensure
2787 * exclusive access to the field.
2789 mutex_lock(&cgroup_cft_mutex
);
2790 mutex_lock(&cgroup_mutex
);
2793 static void cgroup_cfts_commit(struct cgroup_subsys
*ss
,
2794 struct cftype
*cfts
, bool is_add
)
2795 __releases(&cgroup_mutex
) __releases(&cgroup_cft_mutex
)
2798 struct cgroup
*cgrp
, *n
;
2800 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
2801 if (cfts
&& ss
->root
!= &rootnode
) {
2802 list_for_each_entry(cgrp
, &ss
->root
->allcg_list
, allcg_node
) {
2804 list_add_tail(&cgrp
->cft_q_node
, &pending
);
2808 mutex_unlock(&cgroup_mutex
);
2811 * All new cgroups will see @cfts update on @ss->cftsets. Add/rm
2812 * files for all cgroups which were created before.
2814 list_for_each_entry_safe(cgrp
, n
, &pending
, cft_q_node
) {
2815 struct inode
*inode
= cgrp
->dentry
->d_inode
;
2817 mutex_lock(&inode
->i_mutex
);
2818 mutex_lock(&cgroup_mutex
);
2819 if (!cgroup_is_removed(cgrp
))
2820 cgroup_addrm_files(cgrp
, ss
, cfts
, is_add
);
2821 mutex_unlock(&cgroup_mutex
);
2822 mutex_unlock(&inode
->i_mutex
);
2824 list_del_init(&cgrp
->cft_q_node
);
2828 mutex_unlock(&cgroup_cft_mutex
);
2832 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2833 * @ss: target cgroup subsystem
2834 * @cfts: zero-length name terminated array of cftypes
2836 * Register @cfts to @ss. Files described by @cfts are created for all
2837 * existing cgroups to which @ss is attached and all future cgroups will
2838 * have them too. This function can be called anytime whether @ss is
2841 * Returns 0 on successful registration, -errno on failure. Note that this
2842 * function currently returns 0 as long as @cfts registration is successful
2843 * even if some file creation attempts on existing cgroups fail.
2845 int cgroup_add_cftypes(struct cgroup_subsys
*ss
, struct cftype
*cfts
)
2847 struct cftype_set
*set
;
2849 set
= kzalloc(sizeof(*set
), GFP_KERNEL
);
2853 cgroup_cfts_prepare();
2855 list_add_tail(&set
->node
, &ss
->cftsets
);
2856 cgroup_cfts_commit(ss
, cfts
, true);
2860 EXPORT_SYMBOL_GPL(cgroup_add_cftypes
);
2863 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
2864 * @ss: target cgroup subsystem
2865 * @cfts: zero-length name terminated array of cftypes
2867 * Unregister @cfts from @ss. Files described by @cfts are removed from
2868 * all existing cgroups to which @ss is attached and all future cgroups
2869 * won't have them either. This function can be called anytime whether @ss
2870 * is attached or not.
2872 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
2873 * registered with @ss.
2875 int cgroup_rm_cftypes(struct cgroup_subsys
*ss
, struct cftype
*cfts
)
2877 struct cftype_set
*set
;
2879 cgroup_cfts_prepare();
2881 list_for_each_entry(set
, &ss
->cftsets
, node
) {
2882 if (set
->cfts
== cfts
) {
2883 list_del_init(&set
->node
);
2884 cgroup_cfts_commit(ss
, cfts
, false);
2889 cgroup_cfts_commit(ss
, NULL
, false);
2894 * cgroup_task_count - count the number of tasks in a cgroup.
2895 * @cgrp: the cgroup in question
2897 * Return the number of tasks in the cgroup.
2899 int cgroup_task_count(const struct cgroup
*cgrp
)
2902 struct cg_cgroup_link
*link
;
2904 read_lock(&css_set_lock
);
2905 list_for_each_entry(link
, &cgrp
->css_sets
, cgrp_link_list
) {
2906 count
+= atomic_read(&link
->cg
->refcount
);
2908 read_unlock(&css_set_lock
);
2913 * Advance a list_head iterator. The iterator should be positioned at
2914 * the start of a css_set
2916 static void cgroup_advance_iter(struct cgroup
*cgrp
,
2917 struct cgroup_iter
*it
)
2919 struct list_head
*l
= it
->cg_link
;
2920 struct cg_cgroup_link
*link
;
2923 /* Advance to the next non-empty css_set */
2926 if (l
== &cgrp
->css_sets
) {
2930 link
= list_entry(l
, struct cg_cgroup_link
, cgrp_link_list
);
2932 } while (list_empty(&cg
->tasks
));
2934 it
->task
= cg
->tasks
.next
;
2938 * To reduce the fork() overhead for systems that are not actually
2939 * using their cgroups capability, we don't maintain the lists running
2940 * through each css_set to its tasks until we see the list actually
2941 * used - in other words after the first call to cgroup_iter_start().
2943 static void cgroup_enable_task_cg_lists(void)
2945 struct task_struct
*p
, *g
;
2946 write_lock(&css_set_lock
);
2947 use_task_css_set_links
= 1;
2949 * We need tasklist_lock because RCU is not safe against
2950 * while_each_thread(). Besides, a forking task that has passed
2951 * cgroup_post_fork() without seeing use_task_css_set_links = 1
2952 * is not guaranteed to have its child immediately visible in the
2953 * tasklist if we walk through it with RCU.
2955 read_lock(&tasklist_lock
);
2956 do_each_thread(g
, p
) {
2959 * We should check if the process is exiting, otherwise
2960 * it will race with cgroup_exit() in that the list
2961 * entry won't be deleted though the process has exited.
2963 if (!(p
->flags
& PF_EXITING
) && list_empty(&p
->cg_list
))
2964 list_add(&p
->cg_list
, &p
->cgroups
->tasks
);
2966 } while_each_thread(g
, p
);
2967 read_unlock(&tasklist_lock
);
2968 write_unlock(&css_set_lock
);
2972 * cgroup_next_descendant_pre - find the next descendant for pre-order walk
2973 * @pos: the current position (%NULL to initiate traversal)
2974 * @cgroup: cgroup whose descendants to walk
2976 * To be used by cgroup_for_each_descendant_pre(). Find the next
2977 * descendant to visit for pre-order traversal of @cgroup's descendants.
2979 struct cgroup
*cgroup_next_descendant_pre(struct cgroup
*pos
,
2980 struct cgroup
*cgroup
)
2982 struct cgroup
*next
;
2984 WARN_ON_ONCE(!rcu_read_lock_held());
2986 /* if first iteration, pretend we just visited @cgroup */
2988 if (list_empty(&cgroup
->children
))
2993 /* visit the first child if exists */
2994 next
= list_first_or_null_rcu(&pos
->children
, struct cgroup
, sibling
);
2998 /* no child, visit my or the closest ancestor's next sibling */
3000 next
= list_entry_rcu(pos
->sibling
.next
, struct cgroup
,
3002 if (&next
->sibling
!= &pos
->parent
->children
)
3006 } while (pos
!= cgroup
);
3010 EXPORT_SYMBOL_GPL(cgroup_next_descendant_pre
);
3012 static struct cgroup
*cgroup_leftmost_descendant(struct cgroup
*pos
)
3014 struct cgroup
*last
;
3018 pos
= list_first_or_null_rcu(&pos
->children
, struct cgroup
,
3026 * cgroup_next_descendant_post - find the next descendant for post-order walk
3027 * @pos: the current position (%NULL to initiate traversal)
3028 * @cgroup: cgroup whose descendants to walk
3030 * To be used by cgroup_for_each_descendant_post(). Find the next
3031 * descendant to visit for post-order traversal of @cgroup's descendants.
3033 struct cgroup
*cgroup_next_descendant_post(struct cgroup
*pos
,
3034 struct cgroup
*cgroup
)
3036 struct cgroup
*next
;
3038 WARN_ON_ONCE(!rcu_read_lock_held());
3040 /* if first iteration, visit the leftmost descendant */
3042 next
= cgroup_leftmost_descendant(cgroup
);
3043 return next
!= cgroup
? next
: NULL
;
3046 /* if there's an unvisited sibling, visit its leftmost descendant */
3047 next
= list_entry_rcu(pos
->sibling
.next
, struct cgroup
, sibling
);
3048 if (&next
->sibling
!= &pos
->parent
->children
)
3049 return cgroup_leftmost_descendant(next
);
3051 /* no sibling left, visit parent */
3053 return next
!= cgroup
? next
: NULL
;
3055 EXPORT_SYMBOL_GPL(cgroup_next_descendant_post
);
3057 void cgroup_iter_start(struct cgroup
*cgrp
, struct cgroup_iter
*it
)
3058 __acquires(css_set_lock
)
3061 * The first time anyone tries to iterate across a cgroup,
3062 * we need to enable the list linking each css_set to its
3063 * tasks, and fix up all existing tasks.
3065 if (!use_task_css_set_links
)
3066 cgroup_enable_task_cg_lists();
3068 read_lock(&css_set_lock
);
3069 it
->cg_link
= &cgrp
->css_sets
;
3070 cgroup_advance_iter(cgrp
, it
);
3073 struct task_struct
*cgroup_iter_next(struct cgroup
*cgrp
,
3074 struct cgroup_iter
*it
)
3076 struct task_struct
*res
;
3077 struct list_head
*l
= it
->task
;
3078 struct cg_cgroup_link
*link
;
3080 /* If the iterator cg is NULL, we have no tasks */
3083 res
= list_entry(l
, struct task_struct
, cg_list
);
3084 /* Advance iterator to find next entry */
3086 link
= list_entry(it
->cg_link
, struct cg_cgroup_link
, cgrp_link_list
);
3087 if (l
== &link
->cg
->tasks
) {
3088 /* We reached the end of this task list - move on to
3089 * the next cg_cgroup_link */
3090 cgroup_advance_iter(cgrp
, it
);
3097 void cgroup_iter_end(struct cgroup
*cgrp
, struct cgroup_iter
*it
)
3098 __releases(css_set_lock
)
3100 read_unlock(&css_set_lock
);
3103 static inline int started_after_time(struct task_struct
*t1
,
3104 struct timespec
*time
,
3105 struct task_struct
*t2
)
3107 int start_diff
= timespec_compare(&t1
->start_time
, time
);
3108 if (start_diff
> 0) {
3110 } else if (start_diff
< 0) {
3114 * Arbitrarily, if two processes started at the same
3115 * time, we'll say that the lower pointer value
3116 * started first. Note that t2 may have exited by now
3117 * so this may not be a valid pointer any longer, but
3118 * that's fine - it still serves to distinguish
3119 * between two tasks started (effectively) simultaneously.
3126 * This function is a callback from heap_insert() and is used to order
3128 * In this case we order the heap in descending task start time.
3130 static inline int started_after(void *p1
, void *p2
)
3132 struct task_struct
*t1
= p1
;
3133 struct task_struct
*t2
= p2
;
3134 return started_after_time(t1
, &t2
->start_time
, t2
);
3138 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
3139 * @scan: struct cgroup_scanner containing arguments for the scan
3141 * Arguments include pointers to callback functions test_task() and
3143 * Iterate through all the tasks in a cgroup, calling test_task() for each,
3144 * and if it returns true, call process_task() for it also.
3145 * The test_task pointer may be NULL, meaning always true (select all tasks).
3146 * Effectively duplicates cgroup_iter_{start,next,end}()
3147 * but does not lock css_set_lock for the call to process_task().
3148 * The struct cgroup_scanner may be embedded in any structure of the caller's
3150 * It is guaranteed that process_task() will act on every task that
3151 * is a member of the cgroup for the duration of this call. This
3152 * function may or may not call process_task() for tasks that exit
3153 * or move to a different cgroup during the call, or are forked or
3154 * move into the cgroup during the call.
3156 * Note that test_task() may be called with locks held, and may in some
3157 * situations be called multiple times for the same task, so it should
3159 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
3160 * pre-allocated and will be used for heap operations (and its "gt" member will
3161 * be overwritten), else a temporary heap will be used (allocation of which
3162 * may cause this function to fail).
3164 int cgroup_scan_tasks(struct cgroup_scanner
*scan
)
3167 struct cgroup_iter it
;
3168 struct task_struct
*p
, *dropped
;
3169 /* Never dereference latest_task, since it's not refcounted */
3170 struct task_struct
*latest_task
= NULL
;
3171 struct ptr_heap tmp_heap
;
3172 struct ptr_heap
*heap
;
3173 struct timespec latest_time
= { 0, 0 };
3176 /* The caller supplied our heap and pre-allocated its memory */
3178 heap
->gt
= &started_after
;
3180 /* We need to allocate our own heap memory */
3182 retval
= heap_init(heap
, PAGE_SIZE
, GFP_KERNEL
, &started_after
);
3184 /* cannot allocate the heap */
3190 * Scan tasks in the cgroup, using the scanner's "test_task" callback
3191 * to determine which are of interest, and using the scanner's
3192 * "process_task" callback to process any of them that need an update.
3193 * Since we don't want to hold any locks during the task updates,
3194 * gather tasks to be processed in a heap structure.
3195 * The heap is sorted by descending task start time.
3196 * If the statically-sized heap fills up, we overflow tasks that
3197 * started later, and in future iterations only consider tasks that
3198 * started after the latest task in the previous pass. This
3199 * guarantees forward progress and that we don't miss any tasks.
3202 cgroup_iter_start(scan
->cg
, &it
);
3203 while ((p
= cgroup_iter_next(scan
->cg
, &it
))) {
3205 * Only affect tasks that qualify per the caller's callback,
3206 * if he provided one
3208 if (scan
->test_task
&& !scan
->test_task(p
, scan
))
3211 * Only process tasks that started after the last task
3214 if (!started_after_time(p
, &latest_time
, latest_task
))
3216 dropped
= heap_insert(heap
, p
);
3217 if (dropped
== NULL
) {
3219 * The new task was inserted; the heap wasn't
3223 } else if (dropped
!= p
) {
3225 * The new task was inserted, and pushed out a
3229 put_task_struct(dropped
);
3232 * Else the new task was newer than anything already in
3233 * the heap and wasn't inserted
3236 cgroup_iter_end(scan
->cg
, &it
);
3239 for (i
= 0; i
< heap
->size
; i
++) {
3240 struct task_struct
*q
= heap
->ptrs
[i
];
3242 latest_time
= q
->start_time
;
3245 /* Process the task per the caller's callback */
3246 scan
->process_task(q
, scan
);
3250 * If we had to process any tasks at all, scan again
3251 * in case some of them were in the middle of forking
3252 * children that didn't get processed.
3253 * Not the most efficient way to do it, but it avoids
3254 * having to take callback_mutex in the fork path
3258 if (heap
== &tmp_heap
)
3259 heap_free(&tmp_heap
);
3264 * Stuff for reading the 'tasks'/'procs' files.
3266 * Reading this file can return large amounts of data if a cgroup has
3267 * *lots* of attached tasks. So it may need several calls to read(),
3268 * but we cannot guarantee that the information we produce is correct
3269 * unless we produce it entirely atomically.
3273 /* which pidlist file are we talking about? */
3274 enum cgroup_filetype
{
3280 * A pidlist is a list of pids that virtually represents the contents of one
3281 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3282 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3285 struct cgroup_pidlist
{
3287 * used to find which pidlist is wanted. doesn't change as long as
3288 * this particular list stays in the list.
3290 struct { enum cgroup_filetype type
; struct pid_namespace
*ns
; } key
;
3293 /* how many elements the above list has */
3295 /* how many files are using the current array */
3297 /* each of these stored in a list by its cgroup */
3298 struct list_head links
;
3299 /* pointer to the cgroup we belong to, for list removal purposes */
3300 struct cgroup
*owner
;
3301 /* protects the other fields */
3302 struct rw_semaphore mutex
;
3306 * The following two functions "fix" the issue where there are more pids
3307 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3308 * TODO: replace with a kernel-wide solution to this problem
3310 #define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3311 static void *pidlist_allocate(int count
)
3313 if (PIDLIST_TOO_LARGE(count
))
3314 return vmalloc(count
* sizeof(pid_t
));
3316 return kmalloc(count
* sizeof(pid_t
), GFP_KERNEL
);
3318 static void pidlist_free(void *p
)
3320 if (is_vmalloc_addr(p
))
3325 static void *pidlist_resize(void *p
, int newcount
)
3328 /* note: if new alloc fails, old p will still be valid either way */
3329 if (is_vmalloc_addr(p
)) {
3330 newlist
= vmalloc(newcount
* sizeof(pid_t
));
3333 memcpy(newlist
, p
, newcount
* sizeof(pid_t
));
3336 newlist
= krealloc(p
, newcount
* sizeof(pid_t
), GFP_KERNEL
);
3342 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
3343 * If the new stripped list is sufficiently smaller and there's enough memory
3344 * to allocate a new buffer, will let go of the unneeded memory. Returns the
3345 * number of unique elements.
3347 /* is the size difference enough that we should re-allocate the array? */
3348 #define PIDLIST_REALLOC_DIFFERENCE(old, new) ((old) - PAGE_SIZE >= (new))
3349 static int pidlist_uniq(pid_t
**p
, int length
)
3356 * we presume the 0th element is unique, so i starts at 1. trivial
3357 * edge cases first; no work needs to be done for either
3359 if (length
== 0 || length
== 1)
3361 /* src and dest walk down the list; dest counts unique elements */
3362 for (src
= 1; src
< length
; src
++) {
3363 /* find next unique element */
3364 while (list
[src
] == list
[src
-1]) {
3369 /* dest always points to where the next unique element goes */
3370 list
[dest
] = list
[src
];
3375 * if the length difference is large enough, we want to allocate a
3376 * smaller buffer to save memory. if this fails due to out of memory,
3377 * we'll just stay with what we've got.
3379 if (PIDLIST_REALLOC_DIFFERENCE(length
, dest
)) {
3380 newlist
= pidlist_resize(list
, dest
);
3387 static int cmppid(const void *a
, const void *b
)
3389 return *(pid_t
*)a
- *(pid_t
*)b
;
3393 * find the appropriate pidlist for our purpose (given procs vs tasks)
3394 * returns with the lock on that pidlist already held, and takes care
3395 * of the use count, or returns NULL with no locks held if we're out of
3398 static struct cgroup_pidlist
*cgroup_pidlist_find(struct cgroup
*cgrp
,
3399 enum cgroup_filetype type
)
3401 struct cgroup_pidlist
*l
;
3402 /* don't need task_nsproxy() if we're looking at ourself */
3403 struct pid_namespace
*ns
= current
->nsproxy
->pid_ns
;
3406 * We can't drop the pidlist_mutex before taking the l->mutex in case
3407 * the last ref-holder is trying to remove l from the list at the same
3408 * time. Holding the pidlist_mutex precludes somebody taking whichever
3409 * list we find out from under us - compare release_pid_array().
3411 mutex_lock(&cgrp
->pidlist_mutex
);
3412 list_for_each_entry(l
, &cgrp
->pidlists
, links
) {
3413 if (l
->key
.type
== type
&& l
->key
.ns
== ns
) {
3414 /* make sure l doesn't vanish out from under us */
3415 down_write(&l
->mutex
);
3416 mutex_unlock(&cgrp
->pidlist_mutex
);
3420 /* entry not found; create a new one */
3421 l
= kmalloc(sizeof(struct cgroup_pidlist
), GFP_KERNEL
);
3423 mutex_unlock(&cgrp
->pidlist_mutex
);
3426 init_rwsem(&l
->mutex
);
3427 down_write(&l
->mutex
);
3429 l
->key
.ns
= get_pid_ns(ns
);
3430 l
->use_count
= 0; /* don't increment here */
3433 list_add(&l
->links
, &cgrp
->pidlists
);
3434 mutex_unlock(&cgrp
->pidlist_mutex
);
3439 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3441 static int pidlist_array_load(struct cgroup
*cgrp
, enum cgroup_filetype type
,
3442 struct cgroup_pidlist
**lp
)
3446 int pid
, n
= 0; /* used for populating the array */
3447 struct cgroup_iter it
;
3448 struct task_struct
*tsk
;
3449 struct cgroup_pidlist
*l
;
3452 * If cgroup gets more users after we read count, we won't have
3453 * enough space - tough. This race is indistinguishable to the
3454 * caller from the case that the additional cgroup users didn't
3455 * show up until sometime later on.
3457 length
= cgroup_task_count(cgrp
);
3458 array
= pidlist_allocate(length
);
3461 /* now, populate the array */
3462 cgroup_iter_start(cgrp
, &it
);
3463 while ((tsk
= cgroup_iter_next(cgrp
, &it
))) {
3464 if (unlikely(n
== length
))
3466 /* get tgid or pid for procs or tasks file respectively */
3467 if (type
== CGROUP_FILE_PROCS
)
3468 pid
= task_tgid_vnr(tsk
);
3470 pid
= task_pid_vnr(tsk
);
3471 if (pid
> 0) /* make sure to only use valid results */
3474 cgroup_iter_end(cgrp
, &it
);
3476 /* now sort & (if procs) strip out duplicates */
3477 sort(array
, length
, sizeof(pid_t
), cmppid
, NULL
);
3478 if (type
== CGROUP_FILE_PROCS
)
3479 length
= pidlist_uniq(&array
, length
);
3480 l
= cgroup_pidlist_find(cgrp
, type
);
3482 pidlist_free(array
);
3485 /* store array, freeing old if necessary - lock already held */
3486 pidlist_free(l
->list
);
3490 up_write(&l
->mutex
);
3496 * cgroupstats_build - build and fill cgroupstats
3497 * @stats: cgroupstats to fill information into
3498 * @dentry: A dentry entry belonging to the cgroup for which stats have
3501 * Build and fill cgroupstats so that taskstats can export it to user
3504 int cgroupstats_build(struct cgroupstats
*stats
, struct dentry
*dentry
)
3507 struct cgroup
*cgrp
;
3508 struct cgroup_iter it
;
3509 struct task_struct
*tsk
;
3512 * Validate dentry by checking the superblock operations,
3513 * and make sure it's a directory.
3515 if (dentry
->d_sb
->s_op
!= &cgroup_ops
||
3516 !S_ISDIR(dentry
->d_inode
->i_mode
))
3520 cgrp
= dentry
->d_fsdata
;
3522 cgroup_iter_start(cgrp
, &it
);
3523 while ((tsk
= cgroup_iter_next(cgrp
, &it
))) {
3524 switch (tsk
->state
) {
3526 stats
->nr_running
++;
3528 case TASK_INTERRUPTIBLE
:
3529 stats
->nr_sleeping
++;
3531 case TASK_UNINTERRUPTIBLE
:
3532 stats
->nr_uninterruptible
++;
3535 stats
->nr_stopped
++;
3538 if (delayacct_is_task_waiting_on_io(tsk
))
3539 stats
->nr_io_wait
++;
3543 cgroup_iter_end(cgrp
, &it
);
3551 * seq_file methods for the tasks/procs files. The seq_file position is the
3552 * next pid to display; the seq_file iterator is a pointer to the pid
3553 * in the cgroup->l->list array.
3556 static void *cgroup_pidlist_start(struct seq_file
*s
, loff_t
*pos
)
3559 * Initially we receive a position value that corresponds to
3560 * one more than the last pid shown (or 0 on the first call or
3561 * after a seek to the start). Use a binary-search to find the
3562 * next pid to display, if any
3564 struct cgroup_pidlist
*l
= s
->private;
3565 int index
= 0, pid
= *pos
;
3568 down_read(&l
->mutex
);
3570 int end
= l
->length
;
3572 while (index
< end
) {
3573 int mid
= (index
+ end
) / 2;
3574 if (l
->list
[mid
] == pid
) {
3577 } else if (l
->list
[mid
] <= pid
)
3583 /* If we're off the end of the array, we're done */
3584 if (index
>= l
->length
)
3586 /* Update the abstract position to be the actual pid that we found */
3587 iter
= l
->list
+ index
;
3592 static void cgroup_pidlist_stop(struct seq_file
*s
, void *v
)
3594 struct cgroup_pidlist
*l
= s
->private;
3598 static void *cgroup_pidlist_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
3600 struct cgroup_pidlist
*l
= s
->private;
3602 pid_t
*end
= l
->list
+ l
->length
;
3604 * Advance to the next pid in the array. If this goes off the
3616 static int cgroup_pidlist_show(struct seq_file
*s
, void *v
)
3618 return seq_printf(s
, "%d\n", *(int *)v
);
3622 * seq_operations functions for iterating on pidlists through seq_file -
3623 * independent of whether it's tasks or procs
3625 static const struct seq_operations cgroup_pidlist_seq_operations
= {
3626 .start
= cgroup_pidlist_start
,
3627 .stop
= cgroup_pidlist_stop
,
3628 .next
= cgroup_pidlist_next
,
3629 .show
= cgroup_pidlist_show
,
3632 static void cgroup_release_pid_array(struct cgroup_pidlist
*l
)
3635 * the case where we're the last user of this particular pidlist will
3636 * have us remove it from the cgroup's list, which entails taking the
3637 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3638 * pidlist_mutex, we have to take pidlist_mutex first.
3640 mutex_lock(&l
->owner
->pidlist_mutex
);
3641 down_write(&l
->mutex
);
3642 BUG_ON(!l
->use_count
);
3643 if (!--l
->use_count
) {
3644 /* we're the last user if refcount is 0; remove and free */
3645 list_del(&l
->links
);
3646 mutex_unlock(&l
->owner
->pidlist_mutex
);
3647 pidlist_free(l
->list
);
3648 put_pid_ns(l
->key
.ns
);
3649 up_write(&l
->mutex
);
3653 mutex_unlock(&l
->owner
->pidlist_mutex
);
3654 up_write(&l
->mutex
);
3657 static int cgroup_pidlist_release(struct inode
*inode
, struct file
*file
)
3659 struct cgroup_pidlist
*l
;
3660 if (!(file
->f_mode
& FMODE_READ
))
3663 * the seq_file will only be initialized if the file was opened for
3664 * reading; hence we check if it's not null only in that case.
3666 l
= ((struct seq_file
*)file
->private_data
)->private;
3667 cgroup_release_pid_array(l
);
3668 return seq_release(inode
, file
);
3671 static const struct file_operations cgroup_pidlist_operations
= {
3673 .llseek
= seq_lseek
,
3674 .write
= cgroup_file_write
,
3675 .release
= cgroup_pidlist_release
,
3679 * The following functions handle opens on a file that displays a pidlist
3680 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3683 /* helper function for the two below it */
3684 static int cgroup_pidlist_open(struct file
*file
, enum cgroup_filetype type
)
3686 struct cgroup
*cgrp
= __d_cgrp(file
->f_dentry
->d_parent
);
3687 struct cgroup_pidlist
*l
;
3690 /* Nothing to do for write-only files */
3691 if (!(file
->f_mode
& FMODE_READ
))
3694 /* have the array populated */
3695 retval
= pidlist_array_load(cgrp
, type
, &l
);
3698 /* configure file information */
3699 file
->f_op
= &cgroup_pidlist_operations
;
3701 retval
= seq_open(file
, &cgroup_pidlist_seq_operations
);
3703 cgroup_release_pid_array(l
);
3706 ((struct seq_file
*)file
->private_data
)->private = l
;
3709 static int cgroup_tasks_open(struct inode
*unused
, struct file
*file
)
3711 return cgroup_pidlist_open(file
, CGROUP_FILE_TASKS
);
3713 static int cgroup_procs_open(struct inode
*unused
, struct file
*file
)
3715 return cgroup_pidlist_open(file
, CGROUP_FILE_PROCS
);
3718 static u64
cgroup_read_notify_on_release(struct cgroup
*cgrp
,
3721 return notify_on_release(cgrp
);
3724 static int cgroup_write_notify_on_release(struct cgroup
*cgrp
,
3728 clear_bit(CGRP_RELEASABLE
, &cgrp
->flags
);
3730 set_bit(CGRP_NOTIFY_ON_RELEASE
, &cgrp
->flags
);
3732 clear_bit(CGRP_NOTIFY_ON_RELEASE
, &cgrp
->flags
);
3737 * Unregister event and free resources.
3739 * Gets called from workqueue.
3741 static void cgroup_event_remove(struct work_struct
*work
)
3743 struct cgroup_event
*event
= container_of(work
, struct cgroup_event
,
3745 struct cgroup
*cgrp
= event
->cgrp
;
3747 event
->cft
->unregister_event(cgrp
, event
->cft
, event
->eventfd
);
3749 eventfd_ctx_put(event
->eventfd
);
3755 * Gets called on POLLHUP on eventfd when user closes it.
3757 * Called with wqh->lock held and interrupts disabled.
3759 static int cgroup_event_wake(wait_queue_t
*wait
, unsigned mode
,
3760 int sync
, void *key
)
3762 struct cgroup_event
*event
= container_of(wait
,
3763 struct cgroup_event
, wait
);
3764 struct cgroup
*cgrp
= event
->cgrp
;
3765 unsigned long flags
= (unsigned long)key
;
3767 if (flags
& POLLHUP
) {
3768 __remove_wait_queue(event
->wqh
, &event
->wait
);
3769 spin_lock(&cgrp
->event_list_lock
);
3770 list_del(&event
->list
);
3771 spin_unlock(&cgrp
->event_list_lock
);
3773 * We are in atomic context, but cgroup_event_remove() may
3774 * sleep, so we have to call it in workqueue.
3776 schedule_work(&event
->remove
);
3782 static void cgroup_event_ptable_queue_proc(struct file
*file
,
3783 wait_queue_head_t
*wqh
, poll_table
*pt
)
3785 struct cgroup_event
*event
= container_of(pt
,
3786 struct cgroup_event
, pt
);
3789 add_wait_queue(wqh
, &event
->wait
);
3793 * Parse input and register new cgroup event handler.
3795 * Input must be in format '<event_fd> <control_fd> <args>'.
3796 * Interpretation of args is defined by control file implementation.
3798 static int cgroup_write_event_control(struct cgroup
*cgrp
, struct cftype
*cft
,
3801 struct cgroup_event
*event
= NULL
;
3802 unsigned int efd
, cfd
;
3803 struct file
*efile
= NULL
;
3804 struct file
*cfile
= NULL
;
3808 efd
= simple_strtoul(buffer
, &endp
, 10);
3813 cfd
= simple_strtoul(buffer
, &endp
, 10);
3814 if ((*endp
!= ' ') && (*endp
!= '\0'))
3818 event
= kzalloc(sizeof(*event
), GFP_KERNEL
);
3822 INIT_LIST_HEAD(&event
->list
);
3823 init_poll_funcptr(&event
->pt
, cgroup_event_ptable_queue_proc
);
3824 init_waitqueue_func_entry(&event
->wait
, cgroup_event_wake
);
3825 INIT_WORK(&event
->remove
, cgroup_event_remove
);
3827 efile
= eventfd_fget(efd
);
3828 if (IS_ERR(efile
)) {
3829 ret
= PTR_ERR(efile
);
3833 event
->eventfd
= eventfd_ctx_fileget(efile
);
3834 if (IS_ERR(event
->eventfd
)) {
3835 ret
= PTR_ERR(event
->eventfd
);
3845 /* the process need read permission on control file */
3846 /* AV: shouldn't we check that it's been opened for read instead? */
3847 ret
= inode_permission(cfile
->f_path
.dentry
->d_inode
, MAY_READ
);
3851 event
->cft
= __file_cft(cfile
);
3852 if (IS_ERR(event
->cft
)) {
3853 ret
= PTR_ERR(event
->cft
);
3857 if (!event
->cft
->register_event
|| !event
->cft
->unregister_event
) {
3862 ret
= event
->cft
->register_event(cgrp
, event
->cft
,
3863 event
->eventfd
, buffer
);
3867 if (efile
->f_op
->poll(efile
, &event
->pt
) & POLLHUP
) {
3868 event
->cft
->unregister_event(cgrp
, event
->cft
, event
->eventfd
);
3874 * Events should be removed after rmdir of cgroup directory, but before
3875 * destroying subsystem state objects. Let's take reference to cgroup
3876 * directory dentry to do that.
3880 spin_lock(&cgrp
->event_list_lock
);
3881 list_add(&event
->list
, &cgrp
->event_list
);
3882 spin_unlock(&cgrp
->event_list_lock
);
3893 if (event
&& event
->eventfd
&& !IS_ERR(event
->eventfd
))
3894 eventfd_ctx_put(event
->eventfd
);
3896 if (!IS_ERR_OR_NULL(efile
))
3904 static u64
cgroup_clone_children_read(struct cgroup
*cgrp
,
3907 return test_bit(CGRP_CPUSET_CLONE_CHILDREN
, &cgrp
->flags
);
3910 static int cgroup_clone_children_write(struct cgroup
*cgrp
,
3915 set_bit(CGRP_CPUSET_CLONE_CHILDREN
, &cgrp
->flags
);
3917 clear_bit(CGRP_CPUSET_CLONE_CHILDREN
, &cgrp
->flags
);
3922 * for the common functions, 'private' gives the type of file
3924 /* for hysterical raisins, we can't put this on the older files */
3925 #define CGROUP_FILE_GENERIC_PREFIX "cgroup."
3926 static struct cftype files
[] = {
3929 .open
= cgroup_tasks_open
,
3930 .write_u64
= cgroup_tasks_write
,
3931 .release
= cgroup_pidlist_release
,
3932 .mode
= S_IRUGO
| S_IWUSR
,
3935 .name
= CGROUP_FILE_GENERIC_PREFIX
"procs",
3936 .open
= cgroup_procs_open
,
3937 .write_u64
= cgroup_procs_write
,
3938 .release
= cgroup_pidlist_release
,
3939 .mode
= S_IRUGO
| S_IWUSR
,
3942 .name
= "notify_on_release",
3943 .read_u64
= cgroup_read_notify_on_release
,
3944 .write_u64
= cgroup_write_notify_on_release
,
3947 .name
= CGROUP_FILE_GENERIC_PREFIX
"event_control",
3948 .write_string
= cgroup_write_event_control
,
3952 .name
= "cgroup.clone_children",
3953 .read_u64
= cgroup_clone_children_read
,
3954 .write_u64
= cgroup_clone_children_write
,
3957 .name
= "release_agent",
3958 .flags
= CFTYPE_ONLY_ON_ROOT
,
3959 .read_seq_string
= cgroup_release_agent_show
,
3960 .write_string
= cgroup_release_agent_write
,
3961 .max_write_len
= PATH_MAX
,
3967 * cgroup_populate_dir - selectively creation of files in a directory
3968 * @cgrp: target cgroup
3969 * @base_files: true if the base files should be added
3970 * @subsys_mask: mask of the subsystem ids whose files should be added
3972 static int cgroup_populate_dir(struct cgroup
*cgrp
, bool base_files
,
3973 unsigned long subsys_mask
)
3976 struct cgroup_subsys
*ss
;
3979 err
= cgroup_addrm_files(cgrp
, NULL
, files
, true);
3984 /* process cftsets of each subsystem */
3985 for_each_subsys(cgrp
->root
, ss
) {
3986 struct cftype_set
*set
;
3987 if (!test_bit(ss
->subsys_id
, &subsys_mask
))
3990 list_for_each_entry(set
, &ss
->cftsets
, node
)
3991 cgroup_addrm_files(cgrp
, ss
, set
->cfts
, true);
3994 /* This cgroup is ready now */
3995 for_each_subsys(cgrp
->root
, ss
) {
3996 struct cgroup_subsys_state
*css
= cgrp
->subsys
[ss
->subsys_id
];
3998 * Update id->css pointer and make this css visible from
3999 * CSS ID functions. This pointer will be dereferened
4000 * from RCU-read-side without locks.
4003 rcu_assign_pointer(css
->id
->css
, css
);
4009 static void css_dput_fn(struct work_struct
*work
)
4011 struct cgroup_subsys_state
*css
=
4012 container_of(work
, struct cgroup_subsys_state
, dput_work
);
4013 struct dentry
*dentry
= css
->cgroup
->dentry
;
4014 struct super_block
*sb
= dentry
->d_sb
;
4016 atomic_inc(&sb
->s_active
);
4018 deactivate_super(sb
);
4021 static void init_cgroup_css(struct cgroup_subsys_state
*css
,
4022 struct cgroup_subsys
*ss
,
4023 struct cgroup
*cgrp
)
4026 atomic_set(&css
->refcnt
, 1);
4029 if (cgrp
== dummytop
)
4030 css
->flags
|= CSS_ROOT
;
4031 BUG_ON(cgrp
->subsys
[ss
->subsys_id
]);
4032 cgrp
->subsys
[ss
->subsys_id
] = css
;
4035 * css holds an extra ref to @cgrp->dentry which is put on the last
4036 * css_put(). dput() requires process context, which css_put() may
4037 * be called without. @css->dput_work will be used to invoke
4038 * dput() asynchronously from css_put().
4040 INIT_WORK(&css
->dput_work
, css_dput_fn
);
4043 /* invoke ->post_create() on a new CSS and mark it online if successful */
4044 static int online_css(struct cgroup_subsys
*ss
, struct cgroup
*cgrp
)
4048 lockdep_assert_held(&cgroup_mutex
);
4051 ret
= ss
->css_online(cgrp
);
4053 cgrp
->subsys
[ss
->subsys_id
]->flags
|= CSS_ONLINE
;
4057 /* if the CSS is online, invoke ->pre_destory() on it and mark it offline */
4058 static void offline_css(struct cgroup_subsys
*ss
, struct cgroup
*cgrp
)
4059 __releases(&cgroup_mutex
) __acquires(&cgroup_mutex
)
4061 struct cgroup_subsys_state
*css
= cgrp
->subsys
[ss
->subsys_id
];
4063 lockdep_assert_held(&cgroup_mutex
);
4065 if (!(css
->flags
& CSS_ONLINE
))
4069 * css_offline() should be called with cgroup_mutex unlocked. See
4070 * 3fa59dfbc3 ("cgroup: fix potential deadlock in pre_destroy") for
4071 * details. This temporary unlocking should go away once
4072 * cgroup_mutex is unexported from controllers.
4074 if (ss
->css_offline
) {
4075 mutex_unlock(&cgroup_mutex
);
4076 ss
->css_offline(cgrp
);
4077 mutex_lock(&cgroup_mutex
);
4080 cgrp
->subsys
[ss
->subsys_id
]->flags
&= ~CSS_ONLINE
;
4084 * cgroup_create - create a cgroup
4085 * @parent: cgroup that will be parent of the new cgroup
4086 * @dentry: dentry of the new cgroup
4087 * @mode: mode to set on new inode
4089 * Must be called with the mutex on the parent inode held
4091 static long cgroup_create(struct cgroup
*parent
, struct dentry
*dentry
,
4094 struct cgroup
*cgrp
;
4095 struct cgroupfs_root
*root
= parent
->root
;
4097 struct cgroup_subsys
*ss
;
4098 struct super_block
*sb
= root
->sb
;
4100 /* allocate the cgroup and its ID, 0 is reserved for the root */
4101 cgrp
= kzalloc(sizeof(*cgrp
), GFP_KERNEL
);
4105 cgrp
->id
= ida_simple_get(&root
->cgroup_ida
, 1, 0, GFP_KERNEL
);
4110 * Only live parents can have children. Note that the liveliness
4111 * check isn't strictly necessary because cgroup_mkdir() and
4112 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4113 * anyway so that locking is contained inside cgroup proper and we
4114 * don't get nasty surprises if we ever grow another caller.
4116 if (!cgroup_lock_live_group(parent
)) {
4121 /* Grab a reference on the superblock so the hierarchy doesn't
4122 * get deleted on unmount if there are child cgroups. This
4123 * can be done outside cgroup_mutex, since the sb can't
4124 * disappear while someone has an open control file on the
4126 atomic_inc(&sb
->s_active
);
4128 init_cgroup_housekeeping(cgrp
);
4130 cgrp
->parent
= parent
;
4131 cgrp
->root
= parent
->root
;
4132 cgrp
->top_cgroup
= parent
->top_cgroup
;
4134 if (notify_on_release(parent
))
4135 set_bit(CGRP_NOTIFY_ON_RELEASE
, &cgrp
->flags
);
4137 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN
, &parent
->flags
))
4138 set_bit(CGRP_CPUSET_CLONE_CHILDREN
, &cgrp
->flags
);
4140 for_each_subsys(root
, ss
) {
4141 struct cgroup_subsys_state
*css
;
4143 css
= ss
->css_alloc(cgrp
);
4148 init_cgroup_css(css
, ss
, cgrp
);
4150 err
= alloc_css_id(ss
, parent
, cgrp
);
4155 if (ss
->broken_hierarchy
&& !ss
->warned_broken_hierarchy
&&
4157 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",
4158 current
->comm
, current
->pid
, ss
->name
);
4159 if (!strcmp(ss
->name
, "memory"))
4160 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4161 ss
->warned_broken_hierarchy
= true;
4166 * Create directory. cgroup_create_file() returns with the new
4167 * directory locked on success so that it can be populated without
4168 * dropping cgroup_mutex.
4170 err
= cgroup_create_file(dentry
, S_IFDIR
| mode
, sb
);
4173 lockdep_assert_held(&dentry
->d_inode
->i_mutex
);
4175 /* allocation complete, commit to creation */
4176 dentry
->d_fsdata
= cgrp
;
4177 cgrp
->dentry
= dentry
;
4178 list_add_tail(&cgrp
->allcg_node
, &root
->allcg_list
);
4179 list_add_tail_rcu(&cgrp
->sibling
, &cgrp
->parent
->children
);
4180 root
->number_of_cgroups
++;
4182 /* each css holds a ref to the cgroup's dentry */
4183 for_each_subsys(root
, ss
)
4186 /* creation succeeded, notify subsystems */
4187 for_each_subsys(root
, ss
) {
4188 err
= online_css(ss
, cgrp
);
4193 err
= cgroup_populate_dir(cgrp
, true, root
->subsys_mask
);
4197 mutex_unlock(&cgroup_mutex
);
4198 mutex_unlock(&cgrp
->dentry
->d_inode
->i_mutex
);
4203 for_each_subsys(root
, ss
) {
4204 if (cgrp
->subsys
[ss
->subsys_id
])
4207 mutex_unlock(&cgroup_mutex
);
4208 /* Release the reference count that we took on the superblock */
4209 deactivate_super(sb
);
4211 ida_simple_remove(&root
->cgroup_ida
, cgrp
->id
);
4217 cgroup_destroy_locked(cgrp
);
4218 mutex_unlock(&cgroup_mutex
);
4219 mutex_unlock(&dentry
->d_inode
->i_mutex
);
4223 static int cgroup_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
4225 struct cgroup
*c_parent
= dentry
->d_parent
->d_fsdata
;
4227 /* the vfs holds inode->i_mutex already */
4228 return cgroup_create(c_parent
, dentry
, mode
| S_IFDIR
);
4232 * Check the reference count on each subsystem. Since we already
4233 * established that there are no tasks in the cgroup, if the css refcount
4234 * is also 1, then there should be no outstanding references, so the
4235 * subsystem is safe to destroy. We scan across all subsystems rather than
4236 * using the per-hierarchy linked list of mounted subsystems since we can
4237 * be called via check_for_release() with no synchronization other than
4238 * RCU, and the subsystem linked list isn't RCU-safe.
4240 static int cgroup_has_css_refs(struct cgroup
*cgrp
)
4245 * We won't need to lock the subsys array, because the subsystems
4246 * we're concerned about aren't going anywhere since our cgroup root
4247 * has a reference on them.
4249 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
4250 struct cgroup_subsys
*ss
= subsys
[i
];
4251 struct cgroup_subsys_state
*css
;
4253 /* Skip subsystems not present or not in this hierarchy */
4254 if (ss
== NULL
|| ss
->root
!= cgrp
->root
)
4257 css
= cgrp
->subsys
[ss
->subsys_id
];
4259 * When called from check_for_release() it's possible
4260 * that by this point the cgroup has been removed
4261 * and the css deleted. But a false-positive doesn't
4262 * matter, since it can only happen if the cgroup
4263 * has been deleted and hence no longer needs the
4264 * release agent to be called anyway.
4266 if (css
&& css_refcnt(css
) > 1)
4272 static int cgroup_destroy_locked(struct cgroup
*cgrp
)
4273 __releases(&cgroup_mutex
) __acquires(&cgroup_mutex
)
4275 struct dentry
*d
= cgrp
->dentry
;
4276 struct cgroup
*parent
= cgrp
->parent
;
4278 struct cgroup_event
*event
, *tmp
;
4279 struct cgroup_subsys
*ss
;
4281 lockdep_assert_held(&d
->d_inode
->i_mutex
);
4282 lockdep_assert_held(&cgroup_mutex
);
4284 if (atomic_read(&cgrp
->count
) || !list_empty(&cgrp
->children
))
4288 * Block new css_tryget() by deactivating refcnt and mark @cgrp
4289 * removed. This makes future css_tryget() and child creation
4290 * attempts fail thus maintaining the removal conditions verified
4293 for_each_subsys(cgrp
->root
, ss
) {
4294 struct cgroup_subsys_state
*css
= cgrp
->subsys
[ss
->subsys_id
];
4296 WARN_ON(atomic_read(&css
->refcnt
) < 0);
4297 atomic_add(CSS_DEACT_BIAS
, &css
->refcnt
);
4299 set_bit(CGRP_REMOVED
, &cgrp
->flags
);
4301 /* tell subsystems to initate destruction */
4302 for_each_subsys(cgrp
->root
, ss
)
4303 offline_css(ss
, cgrp
);
4306 * Put all the base refs. Each css holds an extra reference to the
4307 * cgroup's dentry and cgroup removal proceeds regardless of css
4308 * refs. On the last put of each css, whenever that may be, the
4309 * extra dentry ref is put so that dentry destruction happens only
4310 * after all css's are released.
4312 for_each_subsys(cgrp
->root
, ss
)
4313 css_put(cgrp
->subsys
[ss
->subsys_id
]);
4315 raw_spin_lock(&release_list_lock
);
4316 if (!list_empty(&cgrp
->release_list
))
4317 list_del_init(&cgrp
->release_list
);
4318 raw_spin_unlock(&release_list_lock
);
4320 /* delete this cgroup from parent->children */
4321 list_del_rcu(&cgrp
->sibling
);
4322 list_del_init(&cgrp
->allcg_node
);
4325 cgroup_d_remove_dir(d
);
4328 set_bit(CGRP_RELEASABLE
, &parent
->flags
);
4329 check_for_release(parent
);
4332 * Unregister events and notify userspace.
4333 * Notify userspace about cgroup removing only after rmdir of cgroup
4334 * directory to avoid race between userspace and kernelspace
4336 spin_lock(&cgrp
->event_list_lock
);
4337 list_for_each_entry_safe(event
, tmp
, &cgrp
->event_list
, list
) {
4338 list_del(&event
->list
);
4339 remove_wait_queue(event
->wqh
, &event
->wait
);
4340 eventfd_signal(event
->eventfd
, 1);
4341 schedule_work(&event
->remove
);
4343 spin_unlock(&cgrp
->event_list_lock
);
4348 static int cgroup_rmdir(struct inode
*unused_dir
, struct dentry
*dentry
)
4352 mutex_lock(&cgroup_mutex
);
4353 ret
= cgroup_destroy_locked(dentry
->d_fsdata
);
4354 mutex_unlock(&cgroup_mutex
);
4359 static void __init_or_module
cgroup_init_cftsets(struct cgroup_subsys
*ss
)
4361 INIT_LIST_HEAD(&ss
->cftsets
);
4364 * base_cftset is embedded in subsys itself, no need to worry about
4367 if (ss
->base_cftypes
) {
4368 ss
->base_cftset
.cfts
= ss
->base_cftypes
;
4369 list_add_tail(&ss
->base_cftset
.node
, &ss
->cftsets
);
4373 static void __init
cgroup_init_subsys(struct cgroup_subsys
*ss
)
4375 struct cgroup_subsys_state
*css
;
4377 printk(KERN_INFO
"Initializing cgroup subsys %s\n", ss
->name
);
4379 mutex_lock(&cgroup_mutex
);
4381 /* init base cftset */
4382 cgroup_init_cftsets(ss
);
4384 /* Create the top cgroup state for this subsystem */
4385 list_add(&ss
->sibling
, &rootnode
.subsys_list
);
4386 ss
->root
= &rootnode
;
4387 css
= ss
->css_alloc(dummytop
);
4388 /* We don't handle early failures gracefully */
4389 BUG_ON(IS_ERR(css
));
4390 init_cgroup_css(css
, ss
, dummytop
);
4392 /* Update the init_css_set to contain a subsys
4393 * pointer to this state - since the subsystem is
4394 * newly registered, all tasks and hence the
4395 * init_css_set is in the subsystem's top cgroup. */
4396 init_css_set
.subsys
[ss
->subsys_id
] = css
;
4398 need_forkexit_callback
|= ss
->fork
|| ss
->exit
;
4400 /* At system boot, before all subsystems have been
4401 * registered, no tasks have been forked, so we don't
4402 * need to invoke fork callbacks here. */
4403 BUG_ON(!list_empty(&init_task
.tasks
));
4406 BUG_ON(online_css(ss
, dummytop
));
4408 mutex_unlock(&cgroup_mutex
);
4410 /* this function shouldn't be used with modular subsystems, since they
4411 * need to register a subsys_id, among other things */
4416 * cgroup_load_subsys: load and register a modular subsystem at runtime
4417 * @ss: the subsystem to load
4419 * This function should be called in a modular subsystem's initcall. If the
4420 * subsystem is built as a module, it will be assigned a new subsys_id and set
4421 * up for use. If the subsystem is built-in anyway, work is delegated to the
4422 * simpler cgroup_init_subsys.
4424 int __init_or_module
cgroup_load_subsys(struct cgroup_subsys
*ss
)
4426 struct cgroup_subsys_state
*css
;
4429 /* check name and function validity */
4430 if (ss
->name
== NULL
|| strlen(ss
->name
) > MAX_CGROUP_TYPE_NAMELEN
||
4431 ss
->css_alloc
== NULL
|| ss
->css_free
== NULL
)
4435 * we don't support callbacks in modular subsystems. this check is
4436 * before the ss->module check for consistency; a subsystem that could
4437 * be a module should still have no callbacks even if the user isn't
4438 * compiling it as one.
4440 if (ss
->fork
|| ss
->exit
)
4444 * an optionally modular subsystem is built-in: we want to do nothing,
4445 * since cgroup_init_subsys will have already taken care of it.
4447 if (ss
->module
== NULL
) {
4448 /* a sanity check */
4449 BUG_ON(subsys
[ss
->subsys_id
] != ss
);
4453 /* init base cftset */
4454 cgroup_init_cftsets(ss
);
4456 mutex_lock(&cgroup_mutex
);
4457 subsys
[ss
->subsys_id
] = ss
;
4460 * no ss->css_alloc seems to need anything important in the ss
4461 * struct, so this can happen first (i.e. before the rootnode
4464 css
= ss
->css_alloc(dummytop
);
4466 /* failure case - need to deassign the subsys[] slot. */
4467 subsys
[ss
->subsys_id
] = NULL
;
4468 mutex_unlock(&cgroup_mutex
);
4469 return PTR_ERR(css
);
4472 list_add(&ss
->sibling
, &rootnode
.subsys_list
);
4473 ss
->root
= &rootnode
;
4475 /* our new subsystem will be attached to the dummy hierarchy. */
4476 init_cgroup_css(css
, ss
, dummytop
);
4477 /* init_idr must be after init_cgroup_css because it sets css->id. */
4479 ret
= cgroup_init_idr(ss
, css
);
4485 * Now we need to entangle the css into the existing css_sets. unlike
4486 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4487 * will need a new pointer to it; done by iterating the css_set_table.
4488 * furthermore, modifying the existing css_sets will corrupt the hash
4489 * table state, so each changed css_set will need its hash recomputed.
4490 * this is all done under the css_set_lock.
4492 write_lock(&css_set_lock
);
4493 for (i
= 0; i
< CSS_SET_TABLE_SIZE
; i
++) {
4495 struct hlist_node
*node
, *tmp
;
4496 struct hlist_head
*bucket
= &css_set_table
[i
], *new_bucket
;
4498 hlist_for_each_entry_safe(cg
, node
, tmp
, bucket
, hlist
) {
4499 /* skip entries that we already rehashed */
4500 if (cg
->subsys
[ss
->subsys_id
])
4502 /* remove existing entry */
4503 hlist_del(&cg
->hlist
);
4505 cg
->subsys
[ss
->subsys_id
] = css
;
4506 /* recompute hash and restore entry */
4507 new_bucket
= css_set_hash(cg
->subsys
);
4508 hlist_add_head(&cg
->hlist
, new_bucket
);
4511 write_unlock(&css_set_lock
);
4514 ret
= online_css(ss
, dummytop
);
4519 mutex_unlock(&cgroup_mutex
);
4523 mutex_unlock(&cgroup_mutex
);
4524 /* @ss can't be mounted here as try_module_get() would fail */
4525 cgroup_unload_subsys(ss
);
4528 EXPORT_SYMBOL_GPL(cgroup_load_subsys
);
4531 * cgroup_unload_subsys: unload a modular subsystem
4532 * @ss: the subsystem to unload
4534 * This function should be called in a modular subsystem's exitcall. When this
4535 * function is invoked, the refcount on the subsystem's module will be 0, so
4536 * the subsystem will not be attached to any hierarchy.
4538 void cgroup_unload_subsys(struct cgroup_subsys
*ss
)
4540 struct cg_cgroup_link
*link
;
4541 struct hlist_head
*hhead
;
4543 BUG_ON(ss
->module
== NULL
);
4546 * we shouldn't be called if the subsystem is in use, and the use of
4547 * try_module_get in parse_cgroupfs_options should ensure that it
4548 * doesn't start being used while we're killing it off.
4550 BUG_ON(ss
->root
!= &rootnode
);
4552 mutex_lock(&cgroup_mutex
);
4554 offline_css(ss
, dummytop
);
4558 idr_remove_all(&ss
->idr
);
4559 idr_destroy(&ss
->idr
);
4562 /* deassign the subsys_id */
4563 subsys
[ss
->subsys_id
] = NULL
;
4565 /* remove subsystem from rootnode's list of subsystems */
4566 list_del_init(&ss
->sibling
);
4569 * disentangle the css from all css_sets attached to the dummytop. as
4570 * in loading, we need to pay our respects to the hashtable gods.
4572 write_lock(&css_set_lock
);
4573 list_for_each_entry(link
, &dummytop
->css_sets
, cgrp_link_list
) {
4574 struct css_set
*cg
= link
->cg
;
4576 hlist_del(&cg
->hlist
);
4577 cg
->subsys
[ss
->subsys_id
] = NULL
;
4578 hhead
= css_set_hash(cg
->subsys
);
4579 hlist_add_head(&cg
->hlist
, hhead
);
4581 write_unlock(&css_set_lock
);
4584 * remove subsystem's css from the dummytop and free it - need to
4585 * free before marking as null because ss->css_free needs the
4586 * cgrp->subsys pointer to find their state. note that this also
4587 * takes care of freeing the css_id.
4589 ss
->css_free(dummytop
);
4590 dummytop
->subsys
[ss
->subsys_id
] = NULL
;
4592 mutex_unlock(&cgroup_mutex
);
4594 EXPORT_SYMBOL_GPL(cgroup_unload_subsys
);
4597 * cgroup_init_early - cgroup initialization at system boot
4599 * Initialize cgroups at system boot, and initialize any
4600 * subsystems that request early init.
4602 int __init
cgroup_init_early(void)
4605 atomic_set(&init_css_set
.refcount
, 1);
4606 INIT_LIST_HEAD(&init_css_set
.cg_links
);
4607 INIT_LIST_HEAD(&init_css_set
.tasks
);
4608 INIT_HLIST_NODE(&init_css_set
.hlist
);
4610 init_cgroup_root(&rootnode
);
4612 init_task
.cgroups
= &init_css_set
;
4614 init_css_set_link
.cg
= &init_css_set
;
4615 init_css_set_link
.cgrp
= dummytop
;
4616 list_add(&init_css_set_link
.cgrp_link_list
,
4617 &rootnode
.top_cgroup
.css_sets
);
4618 list_add(&init_css_set_link
.cg_link_list
,
4619 &init_css_set
.cg_links
);
4621 for (i
= 0; i
< CSS_SET_TABLE_SIZE
; i
++)
4622 INIT_HLIST_HEAD(&css_set_table
[i
]);
4624 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
4625 struct cgroup_subsys
*ss
= subsys
[i
];
4627 /* at bootup time, we don't worry about modular subsystems */
4628 if (!ss
|| ss
->module
)
4632 BUG_ON(strlen(ss
->name
) > MAX_CGROUP_TYPE_NAMELEN
);
4633 BUG_ON(!ss
->css_alloc
);
4634 BUG_ON(!ss
->css_free
);
4635 if (ss
->subsys_id
!= i
) {
4636 printk(KERN_ERR
"cgroup: Subsys %s id == %d\n",
4637 ss
->name
, ss
->subsys_id
);
4642 cgroup_init_subsys(ss
);
4648 * cgroup_init - cgroup initialization
4650 * Register cgroup filesystem and /proc file, and initialize
4651 * any subsystems that didn't request early init.
4653 int __init
cgroup_init(void)
4657 struct hlist_head
*hhead
;
4659 err
= bdi_init(&cgroup_backing_dev_info
);
4663 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
4664 struct cgroup_subsys
*ss
= subsys
[i
];
4666 /* at bootup time, we don't worry about modular subsystems */
4667 if (!ss
|| ss
->module
)
4669 if (!ss
->early_init
)
4670 cgroup_init_subsys(ss
);
4672 cgroup_init_idr(ss
, init_css_set
.subsys
[ss
->subsys_id
]);
4675 /* Add init_css_set to the hash table */
4676 hhead
= css_set_hash(init_css_set
.subsys
);
4677 hlist_add_head(&init_css_set
.hlist
, hhead
);
4678 BUG_ON(!init_root_id(&rootnode
));
4680 cgroup_kobj
= kobject_create_and_add("cgroup", fs_kobj
);
4686 err
= register_filesystem(&cgroup_fs_type
);
4688 kobject_put(cgroup_kobj
);
4692 proc_create("cgroups", 0, NULL
, &proc_cgroupstats_operations
);
4696 bdi_destroy(&cgroup_backing_dev_info
);
4702 * proc_cgroup_show()
4703 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4704 * - Used for /proc/<pid>/cgroup.
4705 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4706 * doesn't really matter if tsk->cgroup changes after we read it,
4707 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
4708 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4709 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4710 * cgroup to top_cgroup.
4713 /* TODO: Use a proper seq_file iterator */
4714 static int proc_cgroup_show(struct seq_file
*m
, void *v
)
4717 struct task_struct
*tsk
;
4720 struct cgroupfs_root
*root
;
4723 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
4729 tsk
= get_pid_task(pid
, PIDTYPE_PID
);
4735 mutex_lock(&cgroup_mutex
);
4737 for_each_active_root(root
) {
4738 struct cgroup_subsys
*ss
;
4739 struct cgroup
*cgrp
;
4742 seq_printf(m
, "%d:", root
->hierarchy_id
);
4743 for_each_subsys(root
, ss
)
4744 seq_printf(m
, "%s%s", count
++ ? "," : "", ss
->name
);
4745 if (strlen(root
->name
))
4746 seq_printf(m
, "%sname=%s", count
? "," : "",
4749 cgrp
= task_cgroup_from_root(tsk
, root
);
4750 retval
= cgroup_path(cgrp
, buf
, PAGE_SIZE
);
4758 mutex_unlock(&cgroup_mutex
);
4759 put_task_struct(tsk
);
4766 static int cgroup_open(struct inode
*inode
, struct file
*file
)
4768 struct pid
*pid
= PROC_I(inode
)->pid
;
4769 return single_open(file
, proc_cgroup_show
, pid
);
4772 const struct file_operations proc_cgroup_operations
= {
4773 .open
= cgroup_open
,
4775 .llseek
= seq_lseek
,
4776 .release
= single_release
,
4779 /* Display information about each subsystem and each hierarchy */
4780 static int proc_cgroupstats_show(struct seq_file
*m
, void *v
)
4784 seq_puts(m
, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
4786 * ideally we don't want subsystems moving around while we do this.
4787 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4788 * subsys/hierarchy state.
4790 mutex_lock(&cgroup_mutex
);
4791 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
4792 struct cgroup_subsys
*ss
= subsys
[i
];
4795 seq_printf(m
, "%s\t%d\t%d\t%d\n",
4796 ss
->name
, ss
->root
->hierarchy_id
,
4797 ss
->root
->number_of_cgroups
, !ss
->disabled
);
4799 mutex_unlock(&cgroup_mutex
);
4803 static int cgroupstats_open(struct inode
*inode
, struct file
*file
)
4805 return single_open(file
, proc_cgroupstats_show
, NULL
);
4808 static const struct file_operations proc_cgroupstats_operations
= {
4809 .open
= cgroupstats_open
,
4811 .llseek
= seq_lseek
,
4812 .release
= single_release
,
4816 * cgroup_fork - attach newly forked task to its parents cgroup.
4817 * @child: pointer to task_struct of forking parent process.
4819 * Description: A task inherits its parent's cgroup at fork().
4821 * A pointer to the shared css_set was automatically copied in
4822 * fork.c by dup_task_struct(). However, we ignore that copy, since
4823 * it was not made under the protection of RCU or cgroup_mutex, so
4824 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
4825 * have already changed current->cgroups, allowing the previously
4826 * referenced cgroup group to be removed and freed.
4828 * At the point that cgroup_fork() is called, 'current' is the parent
4829 * task, and the passed argument 'child' points to the child task.
4831 void cgroup_fork(struct task_struct
*child
)
4834 child
->cgroups
= current
->cgroups
;
4835 get_css_set(child
->cgroups
);
4836 task_unlock(current
);
4837 INIT_LIST_HEAD(&child
->cg_list
);
4841 * cgroup_post_fork - called on a new task after adding it to the task list
4842 * @child: the task in question
4844 * Adds the task to the list running through its css_set if necessary and
4845 * call the subsystem fork() callbacks. Has to be after the task is
4846 * visible on the task list in case we race with the first call to
4847 * cgroup_iter_start() - to guarantee that the new task ends up on its
4850 void cgroup_post_fork(struct task_struct
*child
)
4855 * use_task_css_set_links is set to 1 before we walk the tasklist
4856 * under the tasklist_lock and we read it here after we added the child
4857 * to the tasklist under the tasklist_lock as well. If the child wasn't
4858 * yet in the tasklist when we walked through it from
4859 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
4860 * should be visible now due to the paired locking and barriers implied
4861 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
4862 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
4865 if (use_task_css_set_links
) {
4866 write_lock(&css_set_lock
);
4868 if (list_empty(&child
->cg_list
))
4869 list_add(&child
->cg_list
, &child
->cgroups
->tasks
);
4871 write_unlock(&css_set_lock
);
4875 * Call ss->fork(). This must happen after @child is linked on
4876 * css_set; otherwise, @child might change state between ->fork()
4877 * and addition to css_set.
4879 if (need_forkexit_callback
) {
4880 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
4881 struct cgroup_subsys
*ss
= subsys
[i
];
4884 * fork/exit callbacks are supported only for
4885 * builtin subsystems and we don't need further
4886 * synchronization as they never go away.
4888 if (!ss
|| ss
->module
)
4898 * cgroup_exit - detach cgroup from exiting task
4899 * @tsk: pointer to task_struct of exiting process
4900 * @run_callback: run exit callbacks?
4902 * Description: Detach cgroup from @tsk and release it.
4904 * Note that cgroups marked notify_on_release force every task in
4905 * them to take the global cgroup_mutex mutex when exiting.
4906 * This could impact scaling on very large systems. Be reluctant to
4907 * use notify_on_release cgroups where very high task exit scaling
4908 * is required on large systems.
4910 * the_top_cgroup_hack:
4912 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
4914 * We call cgroup_exit() while the task is still competent to
4915 * handle notify_on_release(), then leave the task attached to the
4916 * root cgroup in each hierarchy for the remainder of its exit.
4918 * To do this properly, we would increment the reference count on
4919 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
4920 * code we would add a second cgroup function call, to drop that
4921 * reference. This would just create an unnecessary hot spot on
4922 * the top_cgroup reference count, to no avail.
4924 * Normally, holding a reference to a cgroup without bumping its
4925 * count is unsafe. The cgroup could go away, or someone could
4926 * attach us to a different cgroup, decrementing the count on
4927 * the first cgroup that we never incremented. But in this case,
4928 * top_cgroup isn't going away, and either task has PF_EXITING set,
4929 * which wards off any cgroup_attach_task() attempts, or task is a failed
4930 * fork, never visible to cgroup_attach_task.
4932 void cgroup_exit(struct task_struct
*tsk
, int run_callbacks
)
4938 * Unlink from the css_set task list if necessary.
4939 * Optimistically check cg_list before taking
4942 if (!list_empty(&tsk
->cg_list
)) {
4943 write_lock(&css_set_lock
);
4944 if (!list_empty(&tsk
->cg_list
))
4945 list_del_init(&tsk
->cg_list
);
4946 write_unlock(&css_set_lock
);
4949 /* Reassign the task to the init_css_set. */
4952 tsk
->cgroups
= &init_css_set
;
4954 if (run_callbacks
&& need_forkexit_callback
) {
4955 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
4956 struct cgroup_subsys
*ss
= subsys
[i
];
4958 /* modular subsystems can't use callbacks */
4959 if (!ss
|| ss
->module
)
4963 struct cgroup
*old_cgrp
=
4964 rcu_dereference_raw(cg
->subsys
[i
])->cgroup
;
4965 struct cgroup
*cgrp
= task_cgroup(tsk
, i
);
4966 ss
->exit(cgrp
, old_cgrp
, tsk
);
4973 put_css_set_taskexit(cg
);
4977 * cgroup_is_descendant - see if @cgrp is a descendant of @task's cgrp
4978 * @cgrp: the cgroup in question
4979 * @task: the task in question
4981 * See if @cgrp is a descendant of @task's cgroup in the appropriate
4984 * If we are sending in dummytop, then presumably we are creating
4985 * the top cgroup in the subsystem.
4987 * Called only by the ns (nsproxy) cgroup.
4989 int cgroup_is_descendant(const struct cgroup
*cgrp
, struct task_struct
*task
)
4992 struct cgroup
*target
;
4994 if (cgrp
== dummytop
)
4997 target
= task_cgroup_from_root(task
, cgrp
->root
);
4998 while (cgrp
!= target
&& cgrp
!= cgrp
->top_cgroup
)
4999 cgrp
= cgrp
->parent
;
5000 ret
= (cgrp
== target
);
5004 static void check_for_release(struct cgroup
*cgrp
)
5006 /* All of these checks rely on RCU to keep the cgroup
5007 * structure alive */
5008 if (cgroup_is_releasable(cgrp
) && !atomic_read(&cgrp
->count
)
5009 && list_empty(&cgrp
->children
) && !cgroup_has_css_refs(cgrp
)) {
5010 /* Control Group is currently removeable. If it's not
5011 * already queued for a userspace notification, queue
5013 int need_schedule_work
= 0;
5014 raw_spin_lock(&release_list_lock
);
5015 if (!cgroup_is_removed(cgrp
) &&
5016 list_empty(&cgrp
->release_list
)) {
5017 list_add(&cgrp
->release_list
, &release_list
);
5018 need_schedule_work
= 1;
5020 raw_spin_unlock(&release_list_lock
);
5021 if (need_schedule_work
)
5022 schedule_work(&release_agent_work
);
5026 /* Caller must verify that the css is not for root cgroup */
5027 bool __css_tryget(struct cgroup_subsys_state
*css
)
5032 v
= css_refcnt(css
);
5033 t
= atomic_cmpxchg(&css
->refcnt
, v
, v
+ 1);
5041 EXPORT_SYMBOL_GPL(__css_tryget
);
5043 /* Caller must verify that the css is not for root cgroup */
5044 void __css_put(struct cgroup_subsys_state
*css
)
5046 struct cgroup
*cgrp
= css
->cgroup
;
5050 v
= css_unbias_refcnt(atomic_dec_return(&css
->refcnt
));
5054 if (notify_on_release(cgrp
)) {
5055 set_bit(CGRP_RELEASABLE
, &cgrp
->flags
);
5056 check_for_release(cgrp
);
5060 schedule_work(&css
->dput_work
);
5065 EXPORT_SYMBOL_GPL(__css_put
);
5068 * Notify userspace when a cgroup is released, by running the
5069 * configured release agent with the name of the cgroup (path
5070 * relative to the root of cgroup file system) as the argument.
5072 * Most likely, this user command will try to rmdir this cgroup.
5074 * This races with the possibility that some other task will be
5075 * attached to this cgroup before it is removed, or that some other
5076 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5077 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5078 * unused, and this cgroup will be reprieved from its death sentence,
5079 * to continue to serve a useful existence. Next time it's released,
5080 * we will get notified again, if it still has 'notify_on_release' set.
5082 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5083 * means only wait until the task is successfully execve()'d. The
5084 * separate release agent task is forked by call_usermodehelper(),
5085 * then control in this thread returns here, without waiting for the
5086 * release agent task. We don't bother to wait because the caller of
5087 * this routine has no use for the exit status of the release agent
5088 * task, so no sense holding our caller up for that.
5090 static void cgroup_release_agent(struct work_struct
*work
)
5092 BUG_ON(work
!= &release_agent_work
);
5093 mutex_lock(&cgroup_mutex
);
5094 raw_spin_lock(&release_list_lock
);
5095 while (!list_empty(&release_list
)) {
5096 char *argv
[3], *envp
[3];
5098 char *pathbuf
= NULL
, *agentbuf
= NULL
;
5099 struct cgroup
*cgrp
= list_entry(release_list
.next
,
5102 list_del_init(&cgrp
->release_list
);
5103 raw_spin_unlock(&release_list_lock
);
5104 pathbuf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
5107 if (cgroup_path(cgrp
, pathbuf
, PAGE_SIZE
) < 0)
5109 agentbuf
= kstrdup(cgrp
->root
->release_agent_path
, GFP_KERNEL
);
5114 argv
[i
++] = agentbuf
;
5115 argv
[i
++] = pathbuf
;
5119 /* minimal command environment */
5120 envp
[i
++] = "HOME=/";
5121 envp
[i
++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5124 /* Drop the lock while we invoke the usermode helper,
5125 * since the exec could involve hitting disk and hence
5126 * be a slow process */
5127 mutex_unlock(&cgroup_mutex
);
5128 call_usermodehelper(argv
[0], argv
, envp
, UMH_WAIT_EXEC
);
5129 mutex_lock(&cgroup_mutex
);
5133 raw_spin_lock(&release_list_lock
);
5135 raw_spin_unlock(&release_list_lock
);
5136 mutex_unlock(&cgroup_mutex
);
5139 static int __init
cgroup_disable(char *str
)
5144 while ((token
= strsep(&str
, ",")) != NULL
) {
5147 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
5148 struct cgroup_subsys
*ss
= subsys
[i
];
5151 * cgroup_disable, being at boot time, can't
5152 * know about module subsystems, so we don't
5155 if (!ss
|| ss
->module
)
5158 if (!strcmp(token
, ss
->name
)) {
5160 printk(KERN_INFO
"Disabling %s control group"
5161 " subsystem\n", ss
->name
);
5168 __setup("cgroup_disable=", cgroup_disable
);
5171 * Functons for CSS ID.
5175 *To get ID other than 0, this should be called when !cgroup_is_removed().
5177 unsigned short css_id(struct cgroup_subsys_state
*css
)
5179 struct css_id
*cssid
;
5182 * This css_id() can return correct value when somone has refcnt
5183 * on this or this is under rcu_read_lock(). Once css->id is allocated,
5184 * it's unchanged until freed.
5186 cssid
= rcu_dereference_check(css
->id
, css_refcnt(css
));
5192 EXPORT_SYMBOL_GPL(css_id
);
5194 unsigned short css_depth(struct cgroup_subsys_state
*css
)
5196 struct css_id
*cssid
;
5198 cssid
= rcu_dereference_check(css
->id
, css_refcnt(css
));
5201 return cssid
->depth
;
5204 EXPORT_SYMBOL_GPL(css_depth
);
5207 * css_is_ancestor - test "root" css is an ancestor of "child"
5208 * @child: the css to be tested.
5209 * @root: the css supporsed to be an ancestor of the child.
5211 * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
5212 * this function reads css->id, the caller must hold rcu_read_lock().
5213 * But, considering usual usage, the csses should be valid objects after test.
5214 * Assuming that the caller will do some action to the child if this returns
5215 * returns true, the caller must take "child";s reference count.
5216 * If "child" is valid object and this returns true, "root" is valid, too.
5219 bool css_is_ancestor(struct cgroup_subsys_state
*child
,
5220 const struct cgroup_subsys_state
*root
)
5222 struct css_id
*child_id
;
5223 struct css_id
*root_id
;
5225 child_id
= rcu_dereference(child
->id
);
5228 root_id
= rcu_dereference(root
->id
);
5231 if (child_id
->depth
< root_id
->depth
)
5233 if (child_id
->stack
[root_id
->depth
] != root_id
->id
)
5238 void free_css_id(struct cgroup_subsys
*ss
, struct cgroup_subsys_state
*css
)
5240 struct css_id
*id
= css
->id
;
5241 /* When this is called before css_id initialization, id can be NULL */
5245 BUG_ON(!ss
->use_id
);
5247 rcu_assign_pointer(id
->css
, NULL
);
5248 rcu_assign_pointer(css
->id
, NULL
);
5249 spin_lock(&ss
->id_lock
);
5250 idr_remove(&ss
->idr
, id
->id
);
5251 spin_unlock(&ss
->id_lock
);
5252 kfree_rcu(id
, rcu_head
);
5254 EXPORT_SYMBOL_GPL(free_css_id
);
5257 * This is called by init or create(). Then, calls to this function are
5258 * always serialized (By cgroup_mutex() at create()).
5261 static struct css_id
*get_new_cssid(struct cgroup_subsys
*ss
, int depth
)
5263 struct css_id
*newid
;
5264 int myid
, error
, size
;
5266 BUG_ON(!ss
->use_id
);
5268 size
= sizeof(*newid
) + sizeof(unsigned short) * (depth
+ 1);
5269 newid
= kzalloc(size
, GFP_KERNEL
);
5271 return ERR_PTR(-ENOMEM
);
5273 if (unlikely(!idr_pre_get(&ss
->idr
, GFP_KERNEL
))) {
5277 spin_lock(&ss
->id_lock
);
5278 /* Don't use 0. allocates an ID of 1-65535 */
5279 error
= idr_get_new_above(&ss
->idr
, newid
, 1, &myid
);
5280 spin_unlock(&ss
->id_lock
);
5282 /* Returns error when there are no free spaces for new ID.*/
5287 if (myid
> CSS_ID_MAX
)
5291 newid
->depth
= depth
;
5295 spin_lock(&ss
->id_lock
);
5296 idr_remove(&ss
->idr
, myid
);
5297 spin_unlock(&ss
->id_lock
);
5300 return ERR_PTR(error
);
5304 static int __init_or_module
cgroup_init_idr(struct cgroup_subsys
*ss
,
5305 struct cgroup_subsys_state
*rootcss
)
5307 struct css_id
*newid
;
5309 spin_lock_init(&ss
->id_lock
);
5312 newid
= get_new_cssid(ss
, 0);
5314 return PTR_ERR(newid
);
5316 newid
->stack
[0] = newid
->id
;
5317 newid
->css
= rootcss
;
5318 rootcss
->id
= newid
;
5322 static int alloc_css_id(struct cgroup_subsys
*ss
, struct cgroup
*parent
,
5323 struct cgroup
*child
)
5325 int subsys_id
, i
, depth
= 0;
5326 struct cgroup_subsys_state
*parent_css
, *child_css
;
5327 struct css_id
*child_id
, *parent_id
;
5329 subsys_id
= ss
->subsys_id
;
5330 parent_css
= parent
->subsys
[subsys_id
];
5331 child_css
= child
->subsys
[subsys_id
];
5332 parent_id
= parent_css
->id
;
5333 depth
= parent_id
->depth
+ 1;
5335 child_id
= get_new_cssid(ss
, depth
);
5336 if (IS_ERR(child_id
))
5337 return PTR_ERR(child_id
);
5339 for (i
= 0; i
< depth
; i
++)
5340 child_id
->stack
[i
] = parent_id
->stack
[i
];
5341 child_id
->stack
[depth
] = child_id
->id
;
5343 * child_id->css pointer will be set after this cgroup is available
5344 * see cgroup_populate_dir()
5346 rcu_assign_pointer(child_css
->id
, child_id
);
5352 * css_lookup - lookup css by id
5353 * @ss: cgroup subsys to be looked into.
5356 * Returns pointer to cgroup_subsys_state if there is valid one with id.
5357 * NULL if not. Should be called under rcu_read_lock()
5359 struct cgroup_subsys_state
*css_lookup(struct cgroup_subsys
*ss
, int id
)
5361 struct css_id
*cssid
= NULL
;
5363 BUG_ON(!ss
->use_id
);
5364 cssid
= idr_find(&ss
->idr
, id
);
5366 if (unlikely(!cssid
))
5369 return rcu_dereference(cssid
->css
);
5371 EXPORT_SYMBOL_GPL(css_lookup
);
5374 * css_get_next - lookup next cgroup under specified hierarchy.
5375 * @ss: pointer to subsystem
5376 * @id: current position of iteration.
5377 * @root: pointer to css. search tree under this.
5378 * @foundid: position of found object.
5380 * Search next css under the specified hierarchy of rootid. Calling under
5381 * rcu_read_lock() is necessary. Returns NULL if it reaches the end.
5383 struct cgroup_subsys_state
*
5384 css_get_next(struct cgroup_subsys
*ss
, int id
,
5385 struct cgroup_subsys_state
*root
, int *foundid
)
5387 struct cgroup_subsys_state
*ret
= NULL
;
5390 int rootid
= css_id(root
);
5391 int depth
= css_depth(root
);
5396 BUG_ON(!ss
->use_id
);
5397 WARN_ON_ONCE(!rcu_read_lock_held());
5399 /* fill start point for scan */
5403 * scan next entry from bitmap(tree), tmpid is updated after
5406 tmp
= idr_get_next(&ss
->idr
, &tmpid
);
5409 if (tmp
->depth
>= depth
&& tmp
->stack
[depth
] == rootid
) {
5410 ret
= rcu_dereference(tmp
->css
);
5416 /* continue to scan from next id */
5423 * get corresponding css from file open on cgroupfs directory
5425 struct cgroup_subsys_state
*cgroup_css_from_dir(struct file
*f
, int id
)
5427 struct cgroup
*cgrp
;
5428 struct inode
*inode
;
5429 struct cgroup_subsys_state
*css
;
5431 inode
= f
->f_dentry
->d_inode
;
5432 /* check in cgroup filesystem dir */
5433 if (inode
->i_op
!= &cgroup_dir_inode_operations
)
5434 return ERR_PTR(-EBADF
);
5436 if (id
< 0 || id
>= CGROUP_SUBSYS_COUNT
)
5437 return ERR_PTR(-EINVAL
);
5440 cgrp
= __d_cgrp(f
->f_dentry
);
5441 css
= cgrp
->subsys
[id
];
5442 return css
? css
: ERR_PTR(-ENOENT
);
5445 #ifdef CONFIG_CGROUP_DEBUG
5446 static struct cgroup_subsys_state
*debug_css_alloc(struct cgroup
*cont
)
5448 struct cgroup_subsys_state
*css
= kzalloc(sizeof(*css
), GFP_KERNEL
);
5451 return ERR_PTR(-ENOMEM
);
5456 static void debug_css_free(struct cgroup
*cont
)
5458 kfree(cont
->subsys
[debug_subsys_id
]);
5461 static u64
cgroup_refcount_read(struct cgroup
*cont
, struct cftype
*cft
)
5463 return atomic_read(&cont
->count
);
5466 static u64
debug_taskcount_read(struct cgroup
*cont
, struct cftype
*cft
)
5468 return cgroup_task_count(cont
);
5471 static u64
current_css_set_read(struct cgroup
*cont
, struct cftype
*cft
)
5473 return (u64
)(unsigned long)current
->cgroups
;
5476 static u64
current_css_set_refcount_read(struct cgroup
*cont
,
5482 count
= atomic_read(¤t
->cgroups
->refcount
);
5487 static int current_css_set_cg_links_read(struct cgroup
*cont
,
5489 struct seq_file
*seq
)
5491 struct cg_cgroup_link
*link
;
5494 read_lock(&css_set_lock
);
5496 cg
= rcu_dereference(current
->cgroups
);
5497 list_for_each_entry(link
, &cg
->cg_links
, cg_link_list
) {
5498 struct cgroup
*c
= link
->cgrp
;
5502 name
= c
->dentry
->d_name
.name
;
5505 seq_printf(seq
, "Root %d group %s\n",
5506 c
->root
->hierarchy_id
, name
);
5509 read_unlock(&css_set_lock
);
5513 #define MAX_TASKS_SHOWN_PER_CSS 25
5514 static int cgroup_css_links_read(struct cgroup
*cont
,
5516 struct seq_file
*seq
)
5518 struct cg_cgroup_link
*link
;
5520 read_lock(&css_set_lock
);
5521 list_for_each_entry(link
, &cont
->css_sets
, cgrp_link_list
) {
5522 struct css_set
*cg
= link
->cg
;
5523 struct task_struct
*task
;
5525 seq_printf(seq
, "css_set %p\n", cg
);
5526 list_for_each_entry(task
, &cg
->tasks
, cg_list
) {
5527 if (count
++ > MAX_TASKS_SHOWN_PER_CSS
) {
5528 seq_puts(seq
, " ...\n");
5531 seq_printf(seq
, " task %d\n",
5532 task_pid_vnr(task
));
5536 read_unlock(&css_set_lock
);
5540 static u64
releasable_read(struct cgroup
*cgrp
, struct cftype
*cft
)
5542 return test_bit(CGRP_RELEASABLE
, &cgrp
->flags
);
5545 static struct cftype debug_files
[] = {
5547 .name
= "cgroup_refcount",
5548 .read_u64
= cgroup_refcount_read
,
5551 .name
= "taskcount",
5552 .read_u64
= debug_taskcount_read
,
5556 .name
= "current_css_set",
5557 .read_u64
= current_css_set_read
,
5561 .name
= "current_css_set_refcount",
5562 .read_u64
= current_css_set_refcount_read
,
5566 .name
= "current_css_set_cg_links",
5567 .read_seq_string
= current_css_set_cg_links_read
,
5571 .name
= "cgroup_css_links",
5572 .read_seq_string
= cgroup_css_links_read
,
5576 .name
= "releasable",
5577 .read_u64
= releasable_read
,
5583 struct cgroup_subsys debug_subsys
= {
5585 .css_alloc
= debug_css_alloc
,
5586 .css_free
= debug_css_free
,
5587 .subsys_id
= debug_subsys_id
,
5588 .base_cftypes
= debug_files
,
5590 #endif /* CONFIG_CGROUP_DEBUG */