2 * Generic process-grouping system.
4 * Based originally on the cpuset system, extracted by Paul Menage
5 * Copyright (C) 2006 Google, Inc
7 * Copyright notices from the original cpuset code:
8 * --------------------------------------------------
9 * Copyright (C) 2003 BULL SA.
10 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
12 * Portions derived from Patrick Mochel's sysfs code.
13 * sysfs is Copyright (c) 2001-3 Patrick Mochel
15 * 2003-10-10 Written by Simon Derr.
16 * 2003-10-22 Updates by Stephen Hemminger.
17 * 2004 May-July Rework by Paul Jackson.
18 * ---------------------------------------------------
20 * This file is subject to the terms and conditions of the GNU General Public
21 * License. See the file COPYING in the main directory of the Linux
22 * distribution for more details.
25 #include <linux/cgroup.h>
26 #include <linux/ctype.h>
27 #include <linux/errno.h>
29 #include <linux/kernel.h>
30 #include <linux/list.h>
32 #include <linux/mutex.h>
33 #include <linux/mount.h>
34 #include <linux/pagemap.h>
35 #include <linux/proc_fs.h>
36 #include <linux/rcupdate.h>
37 #include <linux/sched.h>
38 #include <linux/backing-dev.h>
39 #include <linux/seq_file.h>
40 #include <linux/slab.h>
41 #include <linux/magic.h>
42 #include <linux/spinlock.h>
43 #include <linux/string.h>
44 #include <linux/sort.h>
45 #include <linux/kmod.h>
46 #include <linux/delayacct.h>
47 #include <linux/cgroupstats.h>
48 #include <linux/hash.h>
49 #include <linux/namei.h>
50 #include <linux/smp_lock.h>
51 #include <linux/pid_namespace.h>
52 #include <linux/idr.h>
53 #include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
55 #include <asm/atomic.h>
57 static DEFINE_MUTEX(cgroup_mutex
);
59 /* Generate an array of cgroup subsystem pointers */
60 #define SUBSYS(_x) &_x ## _subsys,
62 static struct cgroup_subsys
*subsys
[] = {
63 #include <linux/cgroup_subsys.h>
66 #define MAX_CGROUP_ROOT_NAMELEN 64
69 * A cgroupfs_root represents the root of a cgroup hierarchy,
70 * and may be associated with a superblock to form an active
73 struct cgroupfs_root
{
74 struct super_block
*sb
;
77 * The bitmask of subsystems intended to be attached to this
80 unsigned long subsys_bits
;
82 /* Unique id for this hierarchy. */
85 /* The bitmask of subsystems currently attached to this hierarchy */
86 unsigned long actual_subsys_bits
;
88 /* A list running through the attached subsystems */
89 struct list_head subsys_list
;
91 /* The root cgroup for this hierarchy */
92 struct cgroup top_cgroup
;
94 /* Tracks how many cgroups are currently defined in hierarchy.*/
95 int number_of_cgroups
;
97 /* A list running through the active hierarchies */
98 struct list_head root_list
;
100 /* Hierarchy-specific flags */
103 /* The path to use for release notifications. */
104 char release_agent_path
[PATH_MAX
];
106 /* The name for this hierarchy - may be empty */
107 char name
[MAX_CGROUP_ROOT_NAMELEN
];
111 * The "rootnode" hierarchy is the "dummy hierarchy", reserved for the
112 * subsystems that are otherwise unattached - it never has more than a
113 * single cgroup, and all tasks are part of that cgroup.
115 static struct cgroupfs_root rootnode
;
118 * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when
119 * cgroup_subsys->use_id != 0.
121 #define CSS_ID_MAX (65535)
124 * The css to which this ID points. This pointer is set to valid value
125 * after cgroup is populated. If cgroup is removed, this will be NULL.
126 * This pointer is expected to be RCU-safe because destroy()
127 * is called after synchronize_rcu(). But for safe use, css_is_removed()
128 * css_tryget() should be used for avoiding race.
130 struct cgroup_subsys_state
*css
;
136 * Depth in hierarchy which this ID belongs to.
138 unsigned short depth
;
140 * ID is freed by RCU. (and lookup routine is RCU safe.)
142 struct rcu_head rcu_head
;
144 * Hierarchy of CSS ID belongs to.
146 unsigned short stack
[0]; /* Array of Length (depth+1) */
150 /* The list of hierarchy roots */
152 static LIST_HEAD(roots
);
153 static int root_count
;
155 static DEFINE_IDA(hierarchy_ida
);
156 static int next_hierarchy_id
;
157 static DEFINE_SPINLOCK(hierarchy_id_lock
);
159 /* dummytop is a shorthand for the dummy hierarchy's top cgroup */
160 #define dummytop (&rootnode.top_cgroup)
162 /* This flag indicates whether tasks in the fork and exit paths should
163 * check for fork/exit handlers to call. This avoids us having to do
164 * extra work in the fork/exit path if none of the subsystems need to
167 static int need_forkexit_callback __read_mostly
;
169 #ifdef CONFIG_PROVE_LOCKING
170 int cgroup_lock_is_held(void)
172 return lockdep_is_held(&cgroup_mutex
);
174 #else /* #ifdef CONFIG_PROVE_LOCKING */
175 int cgroup_lock_is_held(void)
177 return mutex_is_locked(&cgroup_mutex
);
179 #endif /* #else #ifdef CONFIG_PROVE_LOCKING */
181 EXPORT_SYMBOL_GPL(cgroup_lock_is_held
);
183 /* convenient tests for these bits */
184 inline int cgroup_is_removed(const struct cgroup
*cgrp
)
186 return test_bit(CGRP_REMOVED
, &cgrp
->flags
);
189 /* bits in struct cgroupfs_root flags field */
191 ROOT_NOPREFIX
, /* mounted subsystems have no named prefix */
194 static int cgroup_is_releasable(const struct cgroup
*cgrp
)
197 (1 << CGRP_RELEASABLE
) |
198 (1 << CGRP_NOTIFY_ON_RELEASE
);
199 return (cgrp
->flags
& bits
) == bits
;
202 static int notify_on_release(const struct cgroup
*cgrp
)
204 return test_bit(CGRP_NOTIFY_ON_RELEASE
, &cgrp
->flags
);
208 * for_each_subsys() allows you to iterate on each subsystem attached to
209 * an active hierarchy
211 #define for_each_subsys(_root, _ss) \
212 list_for_each_entry(_ss, &_root->subsys_list, sibling)
214 /* for_each_active_root() allows you to iterate across the active hierarchies */
215 #define for_each_active_root(_root) \
216 list_for_each_entry(_root, &roots, root_list)
218 /* the list of cgroups eligible for automatic release. Protected by
219 * release_list_lock */
220 static LIST_HEAD(release_list
);
221 static DEFINE_SPINLOCK(release_list_lock
);
222 static void cgroup_release_agent(struct work_struct
*work
);
223 static DECLARE_WORK(release_agent_work
, cgroup_release_agent
);
224 static void check_for_release(struct cgroup
*cgrp
);
226 /* Link structure for associating css_set objects with cgroups */
227 struct cg_cgroup_link
{
229 * List running through cg_cgroup_links associated with a
230 * cgroup, anchored on cgroup->css_sets
232 struct list_head cgrp_link_list
;
235 * List running through cg_cgroup_links pointing at a
236 * single css_set object, anchored on css_set->cg_links
238 struct list_head cg_link_list
;
242 /* The default css_set - used by init and its children prior to any
243 * hierarchies being mounted. It contains a pointer to the root state
244 * for each subsystem. Also used to anchor the list of css_sets. Not
245 * reference-counted, to improve performance when child cgroups
246 * haven't been created.
249 static struct css_set init_css_set
;
250 static struct cg_cgroup_link init_css_set_link
;
252 static int cgroup_subsys_init_idr(struct cgroup_subsys
*ss
);
254 /* css_set_lock protects the list of css_set objects, and the
255 * chain of tasks off each css_set. Nests outside task->alloc_lock
256 * due to cgroup_iter_start() */
257 static DEFINE_RWLOCK(css_set_lock
);
258 static int css_set_count
;
261 * hash table for cgroup groups. This improves the performance to find
262 * an existing css_set. This hash doesn't (currently) take into
263 * account cgroups in empty hierarchies.
265 #define CSS_SET_HASH_BITS 7
266 #define CSS_SET_TABLE_SIZE (1 << CSS_SET_HASH_BITS)
267 static struct hlist_head css_set_table
[CSS_SET_TABLE_SIZE
];
269 static struct hlist_head
*css_set_hash(struct cgroup_subsys_state
*css
[])
273 unsigned long tmp
= 0UL;
275 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++)
276 tmp
+= (unsigned long)css
[i
];
277 tmp
= (tmp
>> 16) ^ tmp
;
279 index
= hash_long(tmp
, CSS_SET_HASH_BITS
);
281 return &css_set_table
[index
];
284 static void free_css_set_rcu(struct rcu_head
*obj
)
286 struct css_set
*cg
= container_of(obj
, struct css_set
, rcu_head
);
290 /* We don't maintain the lists running through each css_set to its
291 * task until after the first call to cgroup_iter_start(). This
292 * reduces the fork()/exit() overhead for people who have cgroups
293 * compiled into their kernel but not actually in use */
294 static int use_task_css_set_links __read_mostly
;
296 static void __put_css_set(struct css_set
*cg
, int taskexit
)
298 struct cg_cgroup_link
*link
;
299 struct cg_cgroup_link
*saved_link
;
301 * Ensure that the refcount doesn't hit zero while any readers
302 * can see it. Similar to atomic_dec_and_lock(), but for an
305 if (atomic_add_unless(&cg
->refcount
, -1, 1))
307 write_lock(&css_set_lock
);
308 if (!atomic_dec_and_test(&cg
->refcount
)) {
309 write_unlock(&css_set_lock
);
313 /* This css_set is dead. unlink it and release cgroup refcounts */
314 hlist_del(&cg
->hlist
);
317 list_for_each_entry_safe(link
, saved_link
, &cg
->cg_links
,
319 struct cgroup
*cgrp
= link
->cgrp
;
320 list_del(&link
->cg_link_list
);
321 list_del(&link
->cgrp_link_list
);
322 if (atomic_dec_and_test(&cgrp
->count
) &&
323 notify_on_release(cgrp
)) {
325 set_bit(CGRP_RELEASABLE
, &cgrp
->flags
);
326 check_for_release(cgrp
);
332 write_unlock(&css_set_lock
);
333 call_rcu(&cg
->rcu_head
, free_css_set_rcu
);
337 * refcounted get/put for css_set objects
339 static inline void get_css_set(struct css_set
*cg
)
341 atomic_inc(&cg
->refcount
);
344 static inline void put_css_set(struct css_set
*cg
)
346 __put_css_set(cg
, 0);
349 static inline void put_css_set_taskexit(struct css_set
*cg
)
351 __put_css_set(cg
, 1);
355 * compare_css_sets - helper function for find_existing_css_set().
356 * @cg: candidate css_set being tested
357 * @old_cg: existing css_set for a task
358 * @new_cgrp: cgroup that's being entered by the task
359 * @template: desired set of css pointers in css_set (pre-calculated)
361 * Returns true if "cg" matches "old_cg" except for the hierarchy
362 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
364 static bool compare_css_sets(struct css_set
*cg
,
365 struct css_set
*old_cg
,
366 struct cgroup
*new_cgrp
,
367 struct cgroup_subsys_state
*template[])
369 struct list_head
*l1
, *l2
;
371 if (memcmp(template, cg
->subsys
, sizeof(cg
->subsys
))) {
372 /* Not all subsystems matched */
377 * Compare cgroup pointers in order to distinguish between
378 * different cgroups in heirarchies with no subsystems. We
379 * could get by with just this check alone (and skip the
380 * memcmp above) but on most setups the memcmp check will
381 * avoid the need for this more expensive check on almost all
386 l2
= &old_cg
->cg_links
;
388 struct cg_cgroup_link
*cgl1
, *cgl2
;
389 struct cgroup
*cg1
, *cg2
;
393 /* See if we reached the end - both lists are equal length. */
394 if (l1
== &cg
->cg_links
) {
395 BUG_ON(l2
!= &old_cg
->cg_links
);
398 BUG_ON(l2
== &old_cg
->cg_links
);
400 /* Locate the cgroups associated with these links. */
401 cgl1
= list_entry(l1
, struct cg_cgroup_link
, cg_link_list
);
402 cgl2
= list_entry(l2
, struct cg_cgroup_link
, cg_link_list
);
405 /* Hierarchies should be linked in the same order. */
406 BUG_ON(cg1
->root
!= cg2
->root
);
409 * If this hierarchy is the hierarchy of the cgroup
410 * that's changing, then we need to check that this
411 * css_set points to the new cgroup; if it's any other
412 * hierarchy, then this css_set should point to the
413 * same cgroup as the old css_set.
415 if (cg1
->root
== new_cgrp
->root
) {
427 * find_existing_css_set() is a helper for
428 * find_css_set(), and checks to see whether an existing
429 * css_set is suitable.
431 * oldcg: the cgroup group that we're using before the cgroup
434 * cgrp: the cgroup that we're moving into
436 * template: location in which to build the desired set of subsystem
437 * state objects for the new cgroup group
439 static struct css_set
*find_existing_css_set(
440 struct css_set
*oldcg
,
442 struct cgroup_subsys_state
*template[])
445 struct cgroupfs_root
*root
= cgrp
->root
;
446 struct hlist_head
*hhead
;
447 struct hlist_node
*node
;
450 /* Built the set of subsystem state objects that we want to
451 * see in the new css_set */
452 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
453 if (root
->subsys_bits
& (1UL << i
)) {
454 /* Subsystem is in this hierarchy. So we want
455 * the subsystem state from the new
457 template[i
] = cgrp
->subsys
[i
];
459 /* Subsystem is not in this hierarchy, so we
460 * don't want to change the subsystem state */
461 template[i
] = oldcg
->subsys
[i
];
465 hhead
= css_set_hash(template);
466 hlist_for_each_entry(cg
, node
, hhead
, hlist
) {
467 if (!compare_css_sets(cg
, oldcg
, cgrp
, template))
470 /* This css_set matches what we need */
474 /* No existing cgroup group matched */
478 static void free_cg_links(struct list_head
*tmp
)
480 struct cg_cgroup_link
*link
;
481 struct cg_cgroup_link
*saved_link
;
483 list_for_each_entry_safe(link
, saved_link
, tmp
, cgrp_link_list
) {
484 list_del(&link
->cgrp_link_list
);
490 * allocate_cg_links() allocates "count" cg_cgroup_link structures
491 * and chains them on tmp through their cgrp_link_list fields. Returns 0 on
492 * success or a negative error
494 static int allocate_cg_links(int count
, struct list_head
*tmp
)
496 struct cg_cgroup_link
*link
;
499 for (i
= 0; i
< count
; i
++) {
500 link
= kmalloc(sizeof(*link
), GFP_KERNEL
);
505 list_add(&link
->cgrp_link_list
, tmp
);
511 * link_css_set - a helper function to link a css_set to a cgroup
512 * @tmp_cg_links: cg_cgroup_link objects allocated by allocate_cg_links()
513 * @cg: the css_set to be linked
514 * @cgrp: the destination cgroup
516 static void link_css_set(struct list_head
*tmp_cg_links
,
517 struct css_set
*cg
, struct cgroup
*cgrp
)
519 struct cg_cgroup_link
*link
;
521 BUG_ON(list_empty(tmp_cg_links
));
522 link
= list_first_entry(tmp_cg_links
, struct cg_cgroup_link
,
526 atomic_inc(&cgrp
->count
);
527 list_move(&link
->cgrp_link_list
, &cgrp
->css_sets
);
529 * Always add links to the tail of the list so that the list
530 * is sorted by order of hierarchy creation
532 list_add_tail(&link
->cg_link_list
, &cg
->cg_links
);
536 * find_css_set() takes an existing cgroup group and a
537 * cgroup object, and returns a css_set object that's
538 * equivalent to the old group, but with the given cgroup
539 * substituted into the appropriate hierarchy. Must be called with
542 static struct css_set
*find_css_set(
543 struct css_set
*oldcg
, struct cgroup
*cgrp
)
546 struct cgroup_subsys_state
*template[CGROUP_SUBSYS_COUNT
];
548 struct list_head tmp_cg_links
;
550 struct hlist_head
*hhead
;
551 struct cg_cgroup_link
*link
;
553 /* First see if we already have a cgroup group that matches
555 read_lock(&css_set_lock
);
556 res
= find_existing_css_set(oldcg
, cgrp
, template);
559 read_unlock(&css_set_lock
);
564 res
= kmalloc(sizeof(*res
), GFP_KERNEL
);
568 /* Allocate all the cg_cgroup_link objects that we'll need */
569 if (allocate_cg_links(root_count
, &tmp_cg_links
) < 0) {
574 atomic_set(&res
->refcount
, 1);
575 INIT_LIST_HEAD(&res
->cg_links
);
576 INIT_LIST_HEAD(&res
->tasks
);
577 INIT_HLIST_NODE(&res
->hlist
);
579 /* Copy the set of subsystem state objects generated in
580 * find_existing_css_set() */
581 memcpy(res
->subsys
, template, sizeof(res
->subsys
));
583 write_lock(&css_set_lock
);
584 /* Add reference counts and links from the new css_set. */
585 list_for_each_entry(link
, &oldcg
->cg_links
, cg_link_list
) {
586 struct cgroup
*c
= link
->cgrp
;
587 if (c
->root
== cgrp
->root
)
589 link_css_set(&tmp_cg_links
, res
, c
);
592 BUG_ON(!list_empty(&tmp_cg_links
));
596 /* Add this cgroup group to the hash table */
597 hhead
= css_set_hash(res
->subsys
);
598 hlist_add_head(&res
->hlist
, hhead
);
600 write_unlock(&css_set_lock
);
606 * Return the cgroup for "task" from the given hierarchy. Must be
607 * called with cgroup_mutex held.
609 static struct cgroup
*task_cgroup_from_root(struct task_struct
*task
,
610 struct cgroupfs_root
*root
)
613 struct cgroup
*res
= NULL
;
615 BUG_ON(!mutex_is_locked(&cgroup_mutex
));
616 read_lock(&css_set_lock
);
618 * No need to lock the task - since we hold cgroup_mutex the
619 * task can't change groups, so the only thing that can happen
620 * is that it exits and its css is set back to init_css_set.
623 if (css
== &init_css_set
) {
624 res
= &root
->top_cgroup
;
626 struct cg_cgroup_link
*link
;
627 list_for_each_entry(link
, &css
->cg_links
, cg_link_list
) {
628 struct cgroup
*c
= link
->cgrp
;
629 if (c
->root
== root
) {
635 read_unlock(&css_set_lock
);
641 * There is one global cgroup mutex. We also require taking
642 * task_lock() when dereferencing a task's cgroup subsys pointers.
643 * See "The task_lock() exception", at the end of this comment.
645 * A task must hold cgroup_mutex to modify cgroups.
647 * Any task can increment and decrement the count field without lock.
648 * So in general, code holding cgroup_mutex can't rely on the count
649 * field not changing. However, if the count goes to zero, then only
650 * cgroup_attach_task() can increment it again. Because a count of zero
651 * means that no tasks are currently attached, therefore there is no
652 * way a task attached to that cgroup can fork (the other way to
653 * increment the count). So code holding cgroup_mutex can safely
654 * assume that if the count is zero, it will stay zero. Similarly, if
655 * a task holds cgroup_mutex on a cgroup with zero count, it
656 * knows that the cgroup won't be removed, as cgroup_rmdir()
659 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
660 * (usually) take cgroup_mutex. These are the two most performance
661 * critical pieces of code here. The exception occurs on cgroup_exit(),
662 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
663 * is taken, and if the cgroup count is zero, a usermode call made
664 * to the release agent with the name of the cgroup (path relative to
665 * the root of cgroup file system) as the argument.
667 * A cgroup can only be deleted if both its 'count' of using tasks
668 * is zero, and its list of 'children' cgroups is empty. Since all
669 * tasks in the system use _some_ cgroup, and since there is always at
670 * least one task in the system (init, pid == 1), therefore, top_cgroup
671 * always has either children cgroups and/or using tasks. So we don't
672 * need a special hack to ensure that top_cgroup cannot be deleted.
674 * The task_lock() exception
676 * The need for this exception arises from the action of
677 * cgroup_attach_task(), which overwrites one tasks cgroup pointer with
678 * another. It does so using cgroup_mutex, however there are
679 * several performance critical places that need to reference
680 * task->cgroup without the expense of grabbing a system global
681 * mutex. Therefore except as noted below, when dereferencing or, as
682 * in cgroup_attach_task(), modifying a task'ss cgroup pointer we use
683 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
684 * the task_struct routinely used for such matters.
686 * P.S. One more locking exception. RCU is used to guard the
687 * update of a tasks cgroup pointer by cgroup_attach_task()
691 * cgroup_lock - lock out any changes to cgroup structures
694 void cgroup_lock(void)
696 mutex_lock(&cgroup_mutex
);
700 * cgroup_unlock - release lock on cgroup changes
702 * Undo the lock taken in a previous cgroup_lock() call.
704 void cgroup_unlock(void)
706 mutex_unlock(&cgroup_mutex
);
710 * A couple of forward declarations required, due to cyclic reference loop:
711 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
712 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
716 static int cgroup_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
);
717 static int cgroup_rmdir(struct inode
*unused_dir
, struct dentry
*dentry
);
718 static int cgroup_populate_dir(struct cgroup
*cgrp
);
719 static const struct inode_operations cgroup_dir_inode_operations
;
720 static const struct file_operations proc_cgroupstats_operations
;
722 static struct backing_dev_info cgroup_backing_dev_info
= {
724 .capabilities
= BDI_CAP_NO_ACCT_AND_WRITEBACK
,
727 static int alloc_css_id(struct cgroup_subsys
*ss
,
728 struct cgroup
*parent
, struct cgroup
*child
);
730 static struct inode
*cgroup_new_inode(mode_t mode
, struct super_block
*sb
)
732 struct inode
*inode
= new_inode(sb
);
735 inode
->i_mode
= mode
;
736 inode
->i_uid
= current_fsuid();
737 inode
->i_gid
= current_fsgid();
738 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
739 inode
->i_mapping
->backing_dev_info
= &cgroup_backing_dev_info
;
745 * Call subsys's pre_destroy handler.
746 * This is called before css refcnt check.
748 static int cgroup_call_pre_destroy(struct cgroup
*cgrp
)
750 struct cgroup_subsys
*ss
;
753 for_each_subsys(cgrp
->root
, ss
)
754 if (ss
->pre_destroy
) {
755 ret
= ss
->pre_destroy(ss
, cgrp
);
762 static void free_cgroup_rcu(struct rcu_head
*obj
)
764 struct cgroup
*cgrp
= container_of(obj
, struct cgroup
, rcu_head
);
769 static void cgroup_diput(struct dentry
*dentry
, struct inode
*inode
)
771 /* is dentry a directory ? if so, kfree() associated cgroup */
772 if (S_ISDIR(inode
->i_mode
)) {
773 struct cgroup
*cgrp
= dentry
->d_fsdata
;
774 struct cgroup_subsys
*ss
;
775 BUG_ON(!(cgroup_is_removed(cgrp
)));
776 /* It's possible for external users to be holding css
777 * reference counts on a cgroup; css_put() needs to
778 * be able to access the cgroup after decrementing
779 * the reference count in order to know if it needs to
780 * queue the cgroup to be handled by the release
784 mutex_lock(&cgroup_mutex
);
786 * Release the subsystem state objects.
788 for_each_subsys(cgrp
->root
, ss
)
789 ss
->destroy(ss
, cgrp
);
791 cgrp
->root
->number_of_cgroups
--;
792 mutex_unlock(&cgroup_mutex
);
795 * Drop the active superblock reference that we took when we
798 deactivate_super(cgrp
->root
->sb
);
801 * if we're getting rid of the cgroup, refcount should ensure
802 * that there are no pidlists left.
804 BUG_ON(!list_empty(&cgrp
->pidlists
));
806 call_rcu(&cgrp
->rcu_head
, free_cgroup_rcu
);
811 static void remove_dir(struct dentry
*d
)
813 struct dentry
*parent
= dget(d
->d_parent
);
816 simple_rmdir(parent
->d_inode
, d
);
820 static void cgroup_clear_directory(struct dentry
*dentry
)
822 struct list_head
*node
;
824 BUG_ON(!mutex_is_locked(&dentry
->d_inode
->i_mutex
));
825 spin_lock(&dcache_lock
);
826 node
= dentry
->d_subdirs
.next
;
827 while (node
!= &dentry
->d_subdirs
) {
828 struct dentry
*d
= list_entry(node
, struct dentry
, d_u
.d_child
);
831 /* This should never be called on a cgroup
832 * directory with child cgroups */
833 BUG_ON(d
->d_inode
->i_mode
& S_IFDIR
);
835 spin_unlock(&dcache_lock
);
837 simple_unlink(dentry
->d_inode
, d
);
839 spin_lock(&dcache_lock
);
841 node
= dentry
->d_subdirs
.next
;
843 spin_unlock(&dcache_lock
);
847 * NOTE : the dentry must have been dget()'ed
849 static void cgroup_d_remove_dir(struct dentry
*dentry
)
851 cgroup_clear_directory(dentry
);
853 spin_lock(&dcache_lock
);
854 list_del_init(&dentry
->d_u
.d_child
);
855 spin_unlock(&dcache_lock
);
860 * A queue for waiters to do rmdir() cgroup. A tasks will sleep when
861 * cgroup->count == 0 && list_empty(&cgroup->children) && subsys has some
862 * reference to css->refcnt. In general, this refcnt is expected to goes down
865 * CGRP_WAIT_ON_RMDIR flag is set under cgroup's inode->i_mutex;
867 DECLARE_WAIT_QUEUE_HEAD(cgroup_rmdir_waitq
);
869 static void cgroup_wakeup_rmdir_waiter(struct cgroup
*cgrp
)
871 if (unlikely(test_and_clear_bit(CGRP_WAIT_ON_RMDIR
, &cgrp
->flags
)))
872 wake_up_all(&cgroup_rmdir_waitq
);
875 void cgroup_exclude_rmdir(struct cgroup_subsys_state
*css
)
880 void cgroup_release_and_wakeup_rmdir(struct cgroup_subsys_state
*css
)
882 cgroup_wakeup_rmdir_waiter(css
->cgroup
);
887 static int rebind_subsystems(struct cgroupfs_root
*root
,
888 unsigned long final_bits
)
890 unsigned long added_bits
, removed_bits
;
891 struct cgroup
*cgrp
= &root
->top_cgroup
;
894 removed_bits
= root
->actual_subsys_bits
& ~final_bits
;
895 added_bits
= final_bits
& ~root
->actual_subsys_bits
;
896 /* Check that any added subsystems are currently free */
897 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
898 unsigned long bit
= 1UL << i
;
899 struct cgroup_subsys
*ss
= subsys
[i
];
900 if (!(bit
& added_bits
))
902 if (ss
->root
!= &rootnode
) {
903 /* Subsystem isn't free */
908 /* Currently we don't handle adding/removing subsystems when
909 * any child cgroups exist. This is theoretically supportable
910 * but involves complex error handling, so it's being left until
912 if (root
->number_of_cgroups
> 1)
915 /* Process each subsystem */
916 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
917 struct cgroup_subsys
*ss
= subsys
[i
];
918 unsigned long bit
= 1UL << i
;
919 if (bit
& added_bits
) {
920 /* We're binding this subsystem to this hierarchy */
921 BUG_ON(cgrp
->subsys
[i
]);
922 BUG_ON(!dummytop
->subsys
[i
]);
923 BUG_ON(dummytop
->subsys
[i
]->cgroup
!= dummytop
);
924 mutex_lock(&ss
->hierarchy_mutex
);
925 cgrp
->subsys
[i
] = dummytop
->subsys
[i
];
926 cgrp
->subsys
[i
]->cgroup
= cgrp
;
927 list_move(&ss
->sibling
, &root
->subsys_list
);
931 mutex_unlock(&ss
->hierarchy_mutex
);
932 } else if (bit
& removed_bits
) {
933 /* We're removing this subsystem */
934 BUG_ON(cgrp
->subsys
[i
] != dummytop
->subsys
[i
]);
935 BUG_ON(cgrp
->subsys
[i
]->cgroup
!= cgrp
);
936 mutex_lock(&ss
->hierarchy_mutex
);
938 ss
->bind(ss
, dummytop
);
939 dummytop
->subsys
[i
]->cgroup
= dummytop
;
940 cgrp
->subsys
[i
] = NULL
;
941 subsys
[i
]->root
= &rootnode
;
942 list_move(&ss
->sibling
, &rootnode
.subsys_list
);
943 mutex_unlock(&ss
->hierarchy_mutex
);
944 } else if (bit
& final_bits
) {
945 /* Subsystem state should already exist */
946 BUG_ON(!cgrp
->subsys
[i
]);
948 /* Subsystem state shouldn't exist */
949 BUG_ON(cgrp
->subsys
[i
]);
952 root
->subsys_bits
= root
->actual_subsys_bits
= final_bits
;
958 static int cgroup_show_options(struct seq_file
*seq
, struct vfsmount
*vfs
)
960 struct cgroupfs_root
*root
= vfs
->mnt_sb
->s_fs_info
;
961 struct cgroup_subsys
*ss
;
963 mutex_lock(&cgroup_mutex
);
964 for_each_subsys(root
, ss
)
965 seq_printf(seq
, ",%s", ss
->name
);
966 if (test_bit(ROOT_NOPREFIX
, &root
->flags
))
967 seq_puts(seq
, ",noprefix");
968 if (strlen(root
->release_agent_path
))
969 seq_printf(seq
, ",release_agent=%s", root
->release_agent_path
);
970 if (strlen(root
->name
))
971 seq_printf(seq
, ",name=%s", root
->name
);
972 mutex_unlock(&cgroup_mutex
);
976 struct cgroup_sb_opts
{
977 unsigned long subsys_bits
;
981 /* User explicitly requested empty subsystem */
984 struct cgroupfs_root
*new_root
;
988 /* Convert a hierarchy specifier into a bitmask of subsystems and
990 static int parse_cgroupfs_options(char *data
,
991 struct cgroup_sb_opts
*opts
)
993 char *token
, *o
= data
?: "all";
994 unsigned long mask
= (unsigned long)-1;
996 #ifdef CONFIG_CPUSETS
997 mask
= ~(1UL << cpuset_subsys_id
);
1000 memset(opts
, 0, sizeof(*opts
));
1002 while ((token
= strsep(&o
, ",")) != NULL
) {
1005 if (!strcmp(token
, "all")) {
1006 /* Add all non-disabled subsystems */
1008 opts
->subsys_bits
= 0;
1009 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
1010 struct cgroup_subsys
*ss
= subsys
[i
];
1012 opts
->subsys_bits
|= 1ul << i
;
1014 } else if (!strcmp(token
, "none")) {
1015 /* Explicitly have no subsystems */
1017 } else if (!strcmp(token
, "noprefix")) {
1018 set_bit(ROOT_NOPREFIX
, &opts
->flags
);
1019 } else if (!strncmp(token
, "release_agent=", 14)) {
1020 /* Specifying two release agents is forbidden */
1021 if (opts
->release_agent
)
1023 opts
->release_agent
=
1024 kstrndup(token
+ 14, PATH_MAX
, GFP_KERNEL
);
1025 if (!opts
->release_agent
)
1027 } else if (!strncmp(token
, "name=", 5)) {
1029 const char *name
= token
+ 5;
1030 /* Can't specify an empty name */
1033 /* Must match [\w.-]+ */
1034 for (i
= 0; i
< strlen(name
); i
++) {
1038 if ((c
== '.') || (c
== '-') || (c
== '_'))
1042 /* Specifying two names is forbidden */
1045 opts
->name
= kstrndup(name
,
1046 MAX_CGROUP_ROOT_NAMELEN
,
1051 struct cgroup_subsys
*ss
;
1053 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
1055 if (!strcmp(token
, ss
->name
)) {
1057 set_bit(i
, &opts
->subsys_bits
);
1061 if (i
== CGROUP_SUBSYS_COUNT
)
1066 /* Consistency checks */
1069 * Option noprefix was introduced just for backward compatibility
1070 * with the old cpuset, so we allow noprefix only if mounting just
1071 * the cpuset subsystem.
1073 if (test_bit(ROOT_NOPREFIX
, &opts
->flags
) &&
1074 (opts
->subsys_bits
& mask
))
1078 /* Can't specify "none" and some subsystems */
1079 if (opts
->subsys_bits
&& opts
->none
)
1083 * We either have to specify by name or by subsystems. (So all
1084 * empty hierarchies must have a name).
1086 if (!opts
->subsys_bits
&& !opts
->name
)
1092 static int cgroup_remount(struct super_block
*sb
, int *flags
, char *data
)
1095 struct cgroupfs_root
*root
= sb
->s_fs_info
;
1096 struct cgroup
*cgrp
= &root
->top_cgroup
;
1097 struct cgroup_sb_opts opts
;
1100 mutex_lock(&cgrp
->dentry
->d_inode
->i_mutex
);
1101 mutex_lock(&cgroup_mutex
);
1103 /* See what subsystems are wanted */
1104 ret
= parse_cgroupfs_options(data
, &opts
);
1108 /* Don't allow flags to change at remount */
1109 if (opts
.flags
!= root
->flags
) {
1114 /* Don't allow name to change at remount */
1115 if (opts
.name
&& strcmp(opts
.name
, root
->name
)) {
1120 ret
= rebind_subsystems(root
, opts
.subsys_bits
);
1124 /* (re)populate subsystem files */
1125 cgroup_populate_dir(cgrp
);
1127 if (opts
.release_agent
)
1128 strcpy(root
->release_agent_path
, opts
.release_agent
);
1130 kfree(opts
.release_agent
);
1132 mutex_unlock(&cgroup_mutex
);
1133 mutex_unlock(&cgrp
->dentry
->d_inode
->i_mutex
);
1138 static const struct super_operations cgroup_ops
= {
1139 .statfs
= simple_statfs
,
1140 .drop_inode
= generic_delete_inode
,
1141 .show_options
= cgroup_show_options
,
1142 .remount_fs
= cgroup_remount
,
1145 static void init_cgroup_housekeeping(struct cgroup
*cgrp
)
1147 INIT_LIST_HEAD(&cgrp
->sibling
);
1148 INIT_LIST_HEAD(&cgrp
->children
);
1149 INIT_LIST_HEAD(&cgrp
->css_sets
);
1150 INIT_LIST_HEAD(&cgrp
->release_list
);
1151 INIT_LIST_HEAD(&cgrp
->pidlists
);
1152 mutex_init(&cgrp
->pidlist_mutex
);
1155 static void init_cgroup_root(struct cgroupfs_root
*root
)
1157 struct cgroup
*cgrp
= &root
->top_cgroup
;
1158 INIT_LIST_HEAD(&root
->subsys_list
);
1159 INIT_LIST_HEAD(&root
->root_list
);
1160 root
->number_of_cgroups
= 1;
1162 cgrp
->top_cgroup
= cgrp
;
1163 init_cgroup_housekeeping(cgrp
);
1166 static bool init_root_id(struct cgroupfs_root
*root
)
1171 if (!ida_pre_get(&hierarchy_ida
, GFP_KERNEL
))
1173 spin_lock(&hierarchy_id_lock
);
1174 /* Try to allocate the next unused ID */
1175 ret
= ida_get_new_above(&hierarchy_ida
, next_hierarchy_id
,
1176 &root
->hierarchy_id
);
1178 /* Try again starting from 0 */
1179 ret
= ida_get_new(&hierarchy_ida
, &root
->hierarchy_id
);
1181 next_hierarchy_id
= root
->hierarchy_id
+ 1;
1182 } else if (ret
!= -EAGAIN
) {
1183 /* Can only get here if the 31-bit IDR is full ... */
1186 spin_unlock(&hierarchy_id_lock
);
1191 static int cgroup_test_super(struct super_block
*sb
, void *data
)
1193 struct cgroup_sb_opts
*opts
= data
;
1194 struct cgroupfs_root
*root
= sb
->s_fs_info
;
1196 /* If we asked for a name then it must match */
1197 if (opts
->name
&& strcmp(opts
->name
, root
->name
))
1201 * If we asked for subsystems (or explicitly for no
1202 * subsystems) then they must match
1204 if ((opts
->subsys_bits
|| opts
->none
)
1205 && (opts
->subsys_bits
!= root
->subsys_bits
))
1211 static struct cgroupfs_root
*cgroup_root_from_opts(struct cgroup_sb_opts
*opts
)
1213 struct cgroupfs_root
*root
;
1215 if (!opts
->subsys_bits
&& !opts
->none
)
1218 root
= kzalloc(sizeof(*root
), GFP_KERNEL
);
1220 return ERR_PTR(-ENOMEM
);
1222 if (!init_root_id(root
)) {
1224 return ERR_PTR(-ENOMEM
);
1226 init_cgroup_root(root
);
1228 root
->subsys_bits
= opts
->subsys_bits
;
1229 root
->flags
= opts
->flags
;
1230 if (opts
->release_agent
)
1231 strcpy(root
->release_agent_path
, opts
->release_agent
);
1233 strcpy(root
->name
, opts
->name
);
1237 static void cgroup_drop_root(struct cgroupfs_root
*root
)
1242 BUG_ON(!root
->hierarchy_id
);
1243 spin_lock(&hierarchy_id_lock
);
1244 ida_remove(&hierarchy_ida
, root
->hierarchy_id
);
1245 spin_unlock(&hierarchy_id_lock
);
1249 static int cgroup_set_super(struct super_block
*sb
, void *data
)
1252 struct cgroup_sb_opts
*opts
= data
;
1254 /* If we don't have a new root, we can't set up a new sb */
1255 if (!opts
->new_root
)
1258 BUG_ON(!opts
->subsys_bits
&& !opts
->none
);
1260 ret
= set_anon_super(sb
, NULL
);
1264 sb
->s_fs_info
= opts
->new_root
;
1265 opts
->new_root
->sb
= sb
;
1267 sb
->s_blocksize
= PAGE_CACHE_SIZE
;
1268 sb
->s_blocksize_bits
= PAGE_CACHE_SHIFT
;
1269 sb
->s_magic
= CGROUP_SUPER_MAGIC
;
1270 sb
->s_op
= &cgroup_ops
;
1275 static int cgroup_get_rootdir(struct super_block
*sb
)
1277 struct inode
*inode
=
1278 cgroup_new_inode(S_IFDIR
| S_IRUGO
| S_IXUGO
| S_IWUSR
, sb
);
1279 struct dentry
*dentry
;
1284 inode
->i_fop
= &simple_dir_operations
;
1285 inode
->i_op
= &cgroup_dir_inode_operations
;
1286 /* directories start off with i_nlink == 2 (for "." entry) */
1288 dentry
= d_alloc_root(inode
);
1293 sb
->s_root
= dentry
;
1297 static int cgroup_get_sb(struct file_system_type
*fs_type
,
1298 int flags
, const char *unused_dev_name
,
1299 void *data
, struct vfsmount
*mnt
)
1301 struct cgroup_sb_opts opts
;
1302 struct cgroupfs_root
*root
;
1304 struct super_block
*sb
;
1305 struct cgroupfs_root
*new_root
;
1307 /* First find the desired set of subsystems */
1308 ret
= parse_cgroupfs_options(data
, &opts
);
1313 * Allocate a new cgroup root. We may not need it if we're
1314 * reusing an existing hierarchy.
1316 new_root
= cgroup_root_from_opts(&opts
);
1317 if (IS_ERR(new_root
)) {
1318 ret
= PTR_ERR(new_root
);
1321 opts
.new_root
= new_root
;
1323 /* Locate an existing or new sb for this hierarchy */
1324 sb
= sget(fs_type
, cgroup_test_super
, cgroup_set_super
, &opts
);
1327 cgroup_drop_root(opts
.new_root
);
1331 root
= sb
->s_fs_info
;
1333 if (root
== opts
.new_root
) {
1334 /* We used the new root structure, so this is a new hierarchy */
1335 struct list_head tmp_cg_links
;
1336 struct cgroup
*root_cgrp
= &root
->top_cgroup
;
1337 struct inode
*inode
;
1338 struct cgroupfs_root
*existing_root
;
1341 BUG_ON(sb
->s_root
!= NULL
);
1343 ret
= cgroup_get_rootdir(sb
);
1345 goto drop_new_super
;
1346 inode
= sb
->s_root
->d_inode
;
1348 mutex_lock(&inode
->i_mutex
);
1349 mutex_lock(&cgroup_mutex
);
1351 if (strlen(root
->name
)) {
1352 /* Check for name clashes with existing mounts */
1353 for_each_active_root(existing_root
) {
1354 if (!strcmp(existing_root
->name
, root
->name
)) {
1356 mutex_unlock(&cgroup_mutex
);
1357 mutex_unlock(&inode
->i_mutex
);
1358 goto drop_new_super
;
1364 * We're accessing css_set_count without locking
1365 * css_set_lock here, but that's OK - it can only be
1366 * increased by someone holding cgroup_lock, and
1367 * that's us. The worst that can happen is that we
1368 * have some link structures left over
1370 ret
= allocate_cg_links(css_set_count
, &tmp_cg_links
);
1372 mutex_unlock(&cgroup_mutex
);
1373 mutex_unlock(&inode
->i_mutex
);
1374 goto drop_new_super
;
1377 ret
= rebind_subsystems(root
, root
->subsys_bits
);
1378 if (ret
== -EBUSY
) {
1379 mutex_unlock(&cgroup_mutex
);
1380 mutex_unlock(&inode
->i_mutex
);
1381 free_cg_links(&tmp_cg_links
);
1382 goto drop_new_super
;
1385 /* EBUSY should be the only error here */
1388 list_add(&root
->root_list
, &roots
);
1391 sb
->s_root
->d_fsdata
= root_cgrp
;
1392 root
->top_cgroup
.dentry
= sb
->s_root
;
1394 /* Link the top cgroup in this hierarchy into all
1395 * the css_set objects */
1396 write_lock(&css_set_lock
);
1397 for (i
= 0; i
< CSS_SET_TABLE_SIZE
; i
++) {
1398 struct hlist_head
*hhead
= &css_set_table
[i
];
1399 struct hlist_node
*node
;
1402 hlist_for_each_entry(cg
, node
, hhead
, hlist
)
1403 link_css_set(&tmp_cg_links
, cg
, root_cgrp
);
1405 write_unlock(&css_set_lock
);
1407 free_cg_links(&tmp_cg_links
);
1409 BUG_ON(!list_empty(&root_cgrp
->sibling
));
1410 BUG_ON(!list_empty(&root_cgrp
->children
));
1411 BUG_ON(root
->number_of_cgroups
!= 1);
1413 cgroup_populate_dir(root_cgrp
);
1414 mutex_unlock(&cgroup_mutex
);
1415 mutex_unlock(&inode
->i_mutex
);
1418 * We re-used an existing hierarchy - the new root (if
1419 * any) is not needed
1421 cgroup_drop_root(opts
.new_root
);
1424 simple_set_mnt(mnt
, sb
);
1425 kfree(opts
.release_agent
);
1430 deactivate_locked_super(sb
);
1432 kfree(opts
.release_agent
);
1438 static void cgroup_kill_sb(struct super_block
*sb
) {
1439 struct cgroupfs_root
*root
= sb
->s_fs_info
;
1440 struct cgroup
*cgrp
= &root
->top_cgroup
;
1442 struct cg_cgroup_link
*link
;
1443 struct cg_cgroup_link
*saved_link
;
1447 BUG_ON(root
->number_of_cgroups
!= 1);
1448 BUG_ON(!list_empty(&cgrp
->children
));
1449 BUG_ON(!list_empty(&cgrp
->sibling
));
1451 mutex_lock(&cgroup_mutex
);
1453 /* Rebind all subsystems back to the default hierarchy */
1454 ret
= rebind_subsystems(root
, 0);
1455 /* Shouldn't be able to fail ... */
1459 * Release all the links from css_sets to this hierarchy's
1462 write_lock(&css_set_lock
);
1464 list_for_each_entry_safe(link
, saved_link
, &cgrp
->css_sets
,
1466 list_del(&link
->cg_link_list
);
1467 list_del(&link
->cgrp_link_list
);
1470 write_unlock(&css_set_lock
);
1472 if (!list_empty(&root
->root_list
)) {
1473 list_del(&root
->root_list
);
1477 mutex_unlock(&cgroup_mutex
);
1479 kill_litter_super(sb
);
1480 cgroup_drop_root(root
);
1483 static struct file_system_type cgroup_fs_type
= {
1485 .get_sb
= cgroup_get_sb
,
1486 .kill_sb
= cgroup_kill_sb
,
1489 static inline struct cgroup
*__d_cgrp(struct dentry
*dentry
)
1491 return dentry
->d_fsdata
;
1494 static inline struct cftype
*__d_cft(struct dentry
*dentry
)
1496 return dentry
->d_fsdata
;
1500 * cgroup_path - generate the path of a cgroup
1501 * @cgrp: the cgroup in question
1502 * @buf: the buffer to write the path into
1503 * @buflen: the length of the buffer
1505 * Called with cgroup_mutex held or else with an RCU-protected cgroup
1506 * reference. Writes path of cgroup into buf. Returns 0 on success,
1509 int cgroup_path(const struct cgroup
*cgrp
, char *buf
, int buflen
)
1512 struct dentry
*dentry
= rcu_dereference(cgrp
->dentry
);
1514 if (!dentry
|| cgrp
== dummytop
) {
1516 * Inactive subsystems have no dentry for their root
1523 start
= buf
+ buflen
;
1527 int len
= dentry
->d_name
.len
;
1528 if ((start
-= len
) < buf
)
1529 return -ENAMETOOLONG
;
1530 memcpy(start
, cgrp
->dentry
->d_name
.name
, len
);
1531 cgrp
= cgrp
->parent
;
1534 dentry
= rcu_dereference(cgrp
->dentry
);
1538 return -ENAMETOOLONG
;
1541 memmove(buf
, start
, buf
+ buflen
- start
);
1546 * cgroup_attach_task - attach task 'tsk' to cgroup 'cgrp'
1547 * @cgrp: the cgroup the task is attaching to
1548 * @tsk: the task to be attached
1550 * Call holding cgroup_mutex. May take task_lock of
1551 * the task 'tsk' during call.
1553 int cgroup_attach_task(struct cgroup
*cgrp
, struct task_struct
*tsk
)
1556 struct cgroup_subsys
*ss
;
1557 struct cgroup
*oldcgrp
;
1559 struct css_set
*newcg
;
1560 struct cgroupfs_root
*root
= cgrp
->root
;
1562 /* Nothing to do if the task is already in that cgroup */
1563 oldcgrp
= task_cgroup_from_root(tsk
, root
);
1564 if (cgrp
== oldcgrp
)
1567 for_each_subsys(root
, ss
) {
1568 if (ss
->can_attach
) {
1569 retval
= ss
->can_attach(ss
, cgrp
, tsk
, false);
1580 * Locate or allocate a new css_set for this task,
1581 * based on its final set of cgroups
1583 newcg
= find_css_set(cg
, cgrp
);
1589 if (tsk
->flags
& PF_EXITING
) {
1594 rcu_assign_pointer(tsk
->cgroups
, newcg
);
1597 /* Update the css_set linked lists if we're using them */
1598 write_lock(&css_set_lock
);
1599 if (!list_empty(&tsk
->cg_list
)) {
1600 list_del(&tsk
->cg_list
);
1601 list_add(&tsk
->cg_list
, &newcg
->tasks
);
1603 write_unlock(&css_set_lock
);
1605 for_each_subsys(root
, ss
) {
1607 ss
->attach(ss
, cgrp
, oldcgrp
, tsk
, false);
1609 set_bit(CGRP_RELEASABLE
, &oldcgrp
->flags
);
1614 * wake up rmdir() waiter. the rmdir should fail since the cgroup
1615 * is no longer empty.
1617 cgroup_wakeup_rmdir_waiter(cgrp
);
1622 * Attach task with pid 'pid' to cgroup 'cgrp'. Call with cgroup_mutex
1623 * held. May take task_lock of task
1625 static int attach_task_by_pid(struct cgroup
*cgrp
, u64 pid
)
1627 struct task_struct
*tsk
;
1628 const struct cred
*cred
= current_cred(), *tcred
;
1633 tsk
= find_task_by_vpid(pid
);
1634 if (!tsk
|| tsk
->flags
& PF_EXITING
) {
1639 tcred
= __task_cred(tsk
);
1641 cred
->euid
!= tcred
->uid
&&
1642 cred
->euid
!= tcred
->suid
) {
1646 get_task_struct(tsk
);
1650 get_task_struct(tsk
);
1653 ret
= cgroup_attach_task(cgrp
, tsk
);
1654 put_task_struct(tsk
);
1658 static int cgroup_tasks_write(struct cgroup
*cgrp
, struct cftype
*cft
, u64 pid
)
1661 if (!cgroup_lock_live_group(cgrp
))
1663 ret
= attach_task_by_pid(cgrp
, pid
);
1669 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
1670 * @cgrp: the cgroup to be checked for liveness
1672 * On success, returns true; the lock should be later released with
1673 * cgroup_unlock(). On failure returns false with no lock held.
1675 bool cgroup_lock_live_group(struct cgroup
*cgrp
)
1677 mutex_lock(&cgroup_mutex
);
1678 if (cgroup_is_removed(cgrp
)) {
1679 mutex_unlock(&cgroup_mutex
);
1685 static int cgroup_release_agent_write(struct cgroup
*cgrp
, struct cftype
*cft
,
1688 BUILD_BUG_ON(sizeof(cgrp
->root
->release_agent_path
) < PATH_MAX
);
1689 if (!cgroup_lock_live_group(cgrp
))
1691 strcpy(cgrp
->root
->release_agent_path
, buffer
);
1696 static int cgroup_release_agent_show(struct cgroup
*cgrp
, struct cftype
*cft
,
1697 struct seq_file
*seq
)
1699 if (!cgroup_lock_live_group(cgrp
))
1701 seq_puts(seq
, cgrp
->root
->release_agent_path
);
1702 seq_putc(seq
, '\n');
1707 /* A buffer size big enough for numbers or short strings */
1708 #define CGROUP_LOCAL_BUFFER_SIZE 64
1710 static ssize_t
cgroup_write_X64(struct cgroup
*cgrp
, struct cftype
*cft
,
1712 const char __user
*userbuf
,
1713 size_t nbytes
, loff_t
*unused_ppos
)
1715 char buffer
[CGROUP_LOCAL_BUFFER_SIZE
];
1721 if (nbytes
>= sizeof(buffer
))
1723 if (copy_from_user(buffer
, userbuf
, nbytes
))
1726 buffer
[nbytes
] = 0; /* nul-terminate */
1727 if (cft
->write_u64
) {
1728 u64 val
= simple_strtoull(strstrip(buffer
), &end
, 0);
1731 retval
= cft
->write_u64(cgrp
, cft
, val
);
1733 s64 val
= simple_strtoll(strstrip(buffer
), &end
, 0);
1736 retval
= cft
->write_s64(cgrp
, cft
, val
);
1743 static ssize_t
cgroup_write_string(struct cgroup
*cgrp
, struct cftype
*cft
,
1745 const char __user
*userbuf
,
1746 size_t nbytes
, loff_t
*unused_ppos
)
1748 char local_buffer
[CGROUP_LOCAL_BUFFER_SIZE
];
1750 size_t max_bytes
= cft
->max_write_len
;
1751 char *buffer
= local_buffer
;
1754 max_bytes
= sizeof(local_buffer
) - 1;
1755 if (nbytes
>= max_bytes
)
1757 /* Allocate a dynamic buffer if we need one */
1758 if (nbytes
>= sizeof(local_buffer
)) {
1759 buffer
= kmalloc(nbytes
+ 1, GFP_KERNEL
);
1763 if (nbytes
&& copy_from_user(buffer
, userbuf
, nbytes
)) {
1768 buffer
[nbytes
] = 0; /* nul-terminate */
1769 retval
= cft
->write_string(cgrp
, cft
, strstrip(buffer
));
1773 if (buffer
!= local_buffer
)
1778 static ssize_t
cgroup_file_write(struct file
*file
, const char __user
*buf
,
1779 size_t nbytes
, loff_t
*ppos
)
1781 struct cftype
*cft
= __d_cft(file
->f_dentry
);
1782 struct cgroup
*cgrp
= __d_cgrp(file
->f_dentry
->d_parent
);
1784 if (cgroup_is_removed(cgrp
))
1787 return cft
->write(cgrp
, cft
, file
, buf
, nbytes
, ppos
);
1788 if (cft
->write_u64
|| cft
->write_s64
)
1789 return cgroup_write_X64(cgrp
, cft
, file
, buf
, nbytes
, ppos
);
1790 if (cft
->write_string
)
1791 return cgroup_write_string(cgrp
, cft
, file
, buf
, nbytes
, ppos
);
1793 int ret
= cft
->trigger(cgrp
, (unsigned int)cft
->private);
1794 return ret
? ret
: nbytes
;
1799 static ssize_t
cgroup_read_u64(struct cgroup
*cgrp
, struct cftype
*cft
,
1801 char __user
*buf
, size_t nbytes
,
1804 char tmp
[CGROUP_LOCAL_BUFFER_SIZE
];
1805 u64 val
= cft
->read_u64(cgrp
, cft
);
1806 int len
= sprintf(tmp
, "%llu\n", (unsigned long long) val
);
1808 return simple_read_from_buffer(buf
, nbytes
, ppos
, tmp
, len
);
1811 static ssize_t
cgroup_read_s64(struct cgroup
*cgrp
, struct cftype
*cft
,
1813 char __user
*buf
, size_t nbytes
,
1816 char tmp
[CGROUP_LOCAL_BUFFER_SIZE
];
1817 s64 val
= cft
->read_s64(cgrp
, cft
);
1818 int len
= sprintf(tmp
, "%lld\n", (long long) val
);
1820 return simple_read_from_buffer(buf
, nbytes
, ppos
, tmp
, len
);
1823 static ssize_t
cgroup_file_read(struct file
*file
, char __user
*buf
,
1824 size_t nbytes
, loff_t
*ppos
)
1826 struct cftype
*cft
= __d_cft(file
->f_dentry
);
1827 struct cgroup
*cgrp
= __d_cgrp(file
->f_dentry
->d_parent
);
1829 if (cgroup_is_removed(cgrp
))
1833 return cft
->read(cgrp
, cft
, file
, buf
, nbytes
, ppos
);
1835 return cgroup_read_u64(cgrp
, cft
, file
, buf
, nbytes
, ppos
);
1837 return cgroup_read_s64(cgrp
, cft
, file
, buf
, nbytes
, ppos
);
1842 * seqfile ops/methods for returning structured data. Currently just
1843 * supports string->u64 maps, but can be extended in future.
1846 struct cgroup_seqfile_state
{
1848 struct cgroup
*cgroup
;
1851 static int cgroup_map_add(struct cgroup_map_cb
*cb
, const char *key
, u64 value
)
1853 struct seq_file
*sf
= cb
->state
;
1854 return seq_printf(sf
, "%s %llu\n", key
, (unsigned long long)value
);
1857 static int cgroup_seqfile_show(struct seq_file
*m
, void *arg
)
1859 struct cgroup_seqfile_state
*state
= m
->private;
1860 struct cftype
*cft
= state
->cft
;
1861 if (cft
->read_map
) {
1862 struct cgroup_map_cb cb
= {
1863 .fill
= cgroup_map_add
,
1866 return cft
->read_map(state
->cgroup
, cft
, &cb
);
1868 return cft
->read_seq_string(state
->cgroup
, cft
, m
);
1871 static int cgroup_seqfile_release(struct inode
*inode
, struct file
*file
)
1873 struct seq_file
*seq
= file
->private_data
;
1874 kfree(seq
->private);
1875 return single_release(inode
, file
);
1878 static const struct file_operations cgroup_seqfile_operations
= {
1880 .write
= cgroup_file_write
,
1881 .llseek
= seq_lseek
,
1882 .release
= cgroup_seqfile_release
,
1885 static int cgroup_file_open(struct inode
*inode
, struct file
*file
)
1890 err
= generic_file_open(inode
, file
);
1893 cft
= __d_cft(file
->f_dentry
);
1895 if (cft
->read_map
|| cft
->read_seq_string
) {
1896 struct cgroup_seqfile_state
*state
=
1897 kzalloc(sizeof(*state
), GFP_USER
);
1901 state
->cgroup
= __d_cgrp(file
->f_dentry
->d_parent
);
1902 file
->f_op
= &cgroup_seqfile_operations
;
1903 err
= single_open(file
, cgroup_seqfile_show
, state
);
1906 } else if (cft
->open
)
1907 err
= cft
->open(inode
, file
);
1914 static int cgroup_file_release(struct inode
*inode
, struct file
*file
)
1916 struct cftype
*cft
= __d_cft(file
->f_dentry
);
1918 return cft
->release(inode
, file
);
1923 * cgroup_rename - Only allow simple rename of directories in place.
1925 static int cgroup_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
1926 struct inode
*new_dir
, struct dentry
*new_dentry
)
1928 if (!S_ISDIR(old_dentry
->d_inode
->i_mode
))
1930 if (new_dentry
->d_inode
)
1932 if (old_dir
!= new_dir
)
1934 return simple_rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
1937 static const struct file_operations cgroup_file_operations
= {
1938 .read
= cgroup_file_read
,
1939 .write
= cgroup_file_write
,
1940 .llseek
= generic_file_llseek
,
1941 .open
= cgroup_file_open
,
1942 .release
= cgroup_file_release
,
1945 static const struct inode_operations cgroup_dir_inode_operations
= {
1946 .lookup
= simple_lookup
,
1947 .mkdir
= cgroup_mkdir
,
1948 .rmdir
= cgroup_rmdir
,
1949 .rename
= cgroup_rename
,
1952 static int cgroup_create_file(struct dentry
*dentry
, mode_t mode
,
1953 struct super_block
*sb
)
1955 static const struct dentry_operations cgroup_dops
= {
1956 .d_iput
= cgroup_diput
,
1959 struct inode
*inode
;
1963 if (dentry
->d_inode
)
1966 inode
= cgroup_new_inode(mode
, sb
);
1970 if (S_ISDIR(mode
)) {
1971 inode
->i_op
= &cgroup_dir_inode_operations
;
1972 inode
->i_fop
= &simple_dir_operations
;
1974 /* start off with i_nlink == 2 (for "." entry) */
1977 /* start with the directory inode held, so that we can
1978 * populate it without racing with another mkdir */
1979 mutex_lock_nested(&inode
->i_mutex
, I_MUTEX_CHILD
);
1980 } else if (S_ISREG(mode
)) {
1982 inode
->i_fop
= &cgroup_file_operations
;
1984 dentry
->d_op
= &cgroup_dops
;
1985 d_instantiate(dentry
, inode
);
1986 dget(dentry
); /* Extra count - pin the dentry in core */
1991 * cgroup_create_dir - create a directory for an object.
1992 * @cgrp: the cgroup we create the directory for. It must have a valid
1993 * ->parent field. And we are going to fill its ->dentry field.
1994 * @dentry: dentry of the new cgroup
1995 * @mode: mode to set on new directory.
1997 static int cgroup_create_dir(struct cgroup
*cgrp
, struct dentry
*dentry
,
2000 struct dentry
*parent
;
2003 parent
= cgrp
->parent
->dentry
;
2004 error
= cgroup_create_file(dentry
, S_IFDIR
| mode
, cgrp
->root
->sb
);
2006 dentry
->d_fsdata
= cgrp
;
2007 inc_nlink(parent
->d_inode
);
2008 rcu_assign_pointer(cgrp
->dentry
, dentry
);
2017 * cgroup_file_mode - deduce file mode of a control file
2018 * @cft: the control file in question
2020 * returns cft->mode if ->mode is not 0
2021 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2022 * returns S_IRUGO if it has only a read handler
2023 * returns S_IWUSR if it has only a write hander
2025 static mode_t
cgroup_file_mode(const struct cftype
*cft
)
2032 if (cft
->read
|| cft
->read_u64
|| cft
->read_s64
||
2033 cft
->read_map
|| cft
->read_seq_string
)
2036 if (cft
->write
|| cft
->write_u64
|| cft
->write_s64
||
2037 cft
->write_string
|| cft
->trigger
)
2043 int cgroup_add_file(struct cgroup
*cgrp
,
2044 struct cgroup_subsys
*subsys
,
2045 const struct cftype
*cft
)
2047 struct dentry
*dir
= cgrp
->dentry
;
2048 struct dentry
*dentry
;
2052 char name
[MAX_CGROUP_TYPE_NAMELEN
+ MAX_CFTYPE_NAME
+ 2] = { 0 };
2053 if (subsys
&& !test_bit(ROOT_NOPREFIX
, &cgrp
->root
->flags
)) {
2054 strcpy(name
, subsys
->name
);
2057 strcat(name
, cft
->name
);
2058 BUG_ON(!mutex_is_locked(&dir
->d_inode
->i_mutex
));
2059 dentry
= lookup_one_len(name
, dir
, strlen(name
));
2060 if (!IS_ERR(dentry
)) {
2061 mode
= cgroup_file_mode(cft
);
2062 error
= cgroup_create_file(dentry
, mode
| S_IFREG
,
2065 dentry
->d_fsdata
= (void *)cft
;
2068 error
= PTR_ERR(dentry
);
2072 int cgroup_add_files(struct cgroup
*cgrp
,
2073 struct cgroup_subsys
*subsys
,
2074 const struct cftype cft
[],
2078 for (i
= 0; i
< count
; i
++) {
2079 err
= cgroup_add_file(cgrp
, subsys
, &cft
[i
]);
2087 * cgroup_task_count - count the number of tasks in a cgroup.
2088 * @cgrp: the cgroup in question
2090 * Return the number of tasks in the cgroup.
2092 int cgroup_task_count(const struct cgroup
*cgrp
)
2095 struct cg_cgroup_link
*link
;
2097 read_lock(&css_set_lock
);
2098 list_for_each_entry(link
, &cgrp
->css_sets
, cgrp_link_list
) {
2099 count
+= atomic_read(&link
->cg
->refcount
);
2101 read_unlock(&css_set_lock
);
2106 * Advance a list_head iterator. The iterator should be positioned at
2107 * the start of a css_set
2109 static void cgroup_advance_iter(struct cgroup
*cgrp
,
2110 struct cgroup_iter
*it
)
2112 struct list_head
*l
= it
->cg_link
;
2113 struct cg_cgroup_link
*link
;
2116 /* Advance to the next non-empty css_set */
2119 if (l
== &cgrp
->css_sets
) {
2123 link
= list_entry(l
, struct cg_cgroup_link
, cgrp_link_list
);
2125 } while (list_empty(&cg
->tasks
));
2127 it
->task
= cg
->tasks
.next
;
2131 * To reduce the fork() overhead for systems that are not actually
2132 * using their cgroups capability, we don't maintain the lists running
2133 * through each css_set to its tasks until we see the list actually
2134 * used - in other words after the first call to cgroup_iter_start().
2136 * The tasklist_lock is not held here, as do_each_thread() and
2137 * while_each_thread() are protected by RCU.
2139 static void cgroup_enable_task_cg_lists(void)
2141 struct task_struct
*p
, *g
;
2142 write_lock(&css_set_lock
);
2143 use_task_css_set_links
= 1;
2144 do_each_thread(g
, p
) {
2147 * We should check if the process is exiting, otherwise
2148 * it will race with cgroup_exit() in that the list
2149 * entry won't be deleted though the process has exited.
2151 if (!(p
->flags
& PF_EXITING
) && list_empty(&p
->cg_list
))
2152 list_add(&p
->cg_list
, &p
->cgroups
->tasks
);
2154 } while_each_thread(g
, p
);
2155 write_unlock(&css_set_lock
);
2158 void cgroup_iter_start(struct cgroup
*cgrp
, struct cgroup_iter
*it
)
2161 * The first time anyone tries to iterate across a cgroup,
2162 * we need to enable the list linking each css_set to its
2163 * tasks, and fix up all existing tasks.
2165 if (!use_task_css_set_links
)
2166 cgroup_enable_task_cg_lists();
2168 read_lock(&css_set_lock
);
2169 it
->cg_link
= &cgrp
->css_sets
;
2170 cgroup_advance_iter(cgrp
, it
);
2173 struct task_struct
*cgroup_iter_next(struct cgroup
*cgrp
,
2174 struct cgroup_iter
*it
)
2176 struct task_struct
*res
;
2177 struct list_head
*l
= it
->task
;
2178 struct cg_cgroup_link
*link
;
2180 /* If the iterator cg is NULL, we have no tasks */
2183 res
= list_entry(l
, struct task_struct
, cg_list
);
2184 /* Advance iterator to find next entry */
2186 link
= list_entry(it
->cg_link
, struct cg_cgroup_link
, cgrp_link_list
);
2187 if (l
== &link
->cg
->tasks
) {
2188 /* We reached the end of this task list - move on to
2189 * the next cg_cgroup_link */
2190 cgroup_advance_iter(cgrp
, it
);
2197 void cgroup_iter_end(struct cgroup
*cgrp
, struct cgroup_iter
*it
)
2199 read_unlock(&css_set_lock
);
2202 static inline int started_after_time(struct task_struct
*t1
,
2203 struct timespec
*time
,
2204 struct task_struct
*t2
)
2206 int start_diff
= timespec_compare(&t1
->start_time
, time
);
2207 if (start_diff
> 0) {
2209 } else if (start_diff
< 0) {
2213 * Arbitrarily, if two processes started at the same
2214 * time, we'll say that the lower pointer value
2215 * started first. Note that t2 may have exited by now
2216 * so this may not be a valid pointer any longer, but
2217 * that's fine - it still serves to distinguish
2218 * between two tasks started (effectively) simultaneously.
2225 * This function is a callback from heap_insert() and is used to order
2227 * In this case we order the heap in descending task start time.
2229 static inline int started_after(void *p1
, void *p2
)
2231 struct task_struct
*t1
= p1
;
2232 struct task_struct
*t2
= p2
;
2233 return started_after_time(t1
, &t2
->start_time
, t2
);
2237 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
2238 * @scan: struct cgroup_scanner containing arguments for the scan
2240 * Arguments include pointers to callback functions test_task() and
2242 * Iterate through all the tasks in a cgroup, calling test_task() for each,
2243 * and if it returns true, call process_task() for it also.
2244 * The test_task pointer may be NULL, meaning always true (select all tasks).
2245 * Effectively duplicates cgroup_iter_{start,next,end}()
2246 * but does not lock css_set_lock for the call to process_task().
2247 * The struct cgroup_scanner may be embedded in any structure of the caller's
2249 * It is guaranteed that process_task() will act on every task that
2250 * is a member of the cgroup for the duration of this call. This
2251 * function may or may not call process_task() for tasks that exit
2252 * or move to a different cgroup during the call, or are forked or
2253 * move into the cgroup during the call.
2255 * Note that test_task() may be called with locks held, and may in some
2256 * situations be called multiple times for the same task, so it should
2258 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
2259 * pre-allocated and will be used for heap operations (and its "gt" member will
2260 * be overwritten), else a temporary heap will be used (allocation of which
2261 * may cause this function to fail).
2263 int cgroup_scan_tasks(struct cgroup_scanner
*scan
)
2266 struct cgroup_iter it
;
2267 struct task_struct
*p
, *dropped
;
2268 /* Never dereference latest_task, since it's not refcounted */
2269 struct task_struct
*latest_task
= NULL
;
2270 struct ptr_heap tmp_heap
;
2271 struct ptr_heap
*heap
;
2272 struct timespec latest_time
= { 0, 0 };
2275 /* The caller supplied our heap and pre-allocated its memory */
2277 heap
->gt
= &started_after
;
2279 /* We need to allocate our own heap memory */
2281 retval
= heap_init(heap
, PAGE_SIZE
, GFP_KERNEL
, &started_after
);
2283 /* cannot allocate the heap */
2289 * Scan tasks in the cgroup, using the scanner's "test_task" callback
2290 * to determine which are of interest, and using the scanner's
2291 * "process_task" callback to process any of them that need an update.
2292 * Since we don't want to hold any locks during the task updates,
2293 * gather tasks to be processed in a heap structure.
2294 * The heap is sorted by descending task start time.
2295 * If the statically-sized heap fills up, we overflow tasks that
2296 * started later, and in future iterations only consider tasks that
2297 * started after the latest task in the previous pass. This
2298 * guarantees forward progress and that we don't miss any tasks.
2301 cgroup_iter_start(scan
->cg
, &it
);
2302 while ((p
= cgroup_iter_next(scan
->cg
, &it
))) {
2304 * Only affect tasks that qualify per the caller's callback,
2305 * if he provided one
2307 if (scan
->test_task
&& !scan
->test_task(p
, scan
))
2310 * Only process tasks that started after the last task
2313 if (!started_after_time(p
, &latest_time
, latest_task
))
2315 dropped
= heap_insert(heap
, p
);
2316 if (dropped
== NULL
) {
2318 * The new task was inserted; the heap wasn't
2322 } else if (dropped
!= p
) {
2324 * The new task was inserted, and pushed out a
2328 put_task_struct(dropped
);
2331 * Else the new task was newer than anything already in
2332 * the heap and wasn't inserted
2335 cgroup_iter_end(scan
->cg
, &it
);
2338 for (i
= 0; i
< heap
->size
; i
++) {
2339 struct task_struct
*q
= heap
->ptrs
[i
];
2341 latest_time
= q
->start_time
;
2344 /* Process the task per the caller's callback */
2345 scan
->process_task(q
, scan
);
2349 * If we had to process any tasks at all, scan again
2350 * in case some of them were in the middle of forking
2351 * children that didn't get processed.
2352 * Not the most efficient way to do it, but it avoids
2353 * having to take callback_mutex in the fork path
2357 if (heap
== &tmp_heap
)
2358 heap_free(&tmp_heap
);
2363 * Stuff for reading the 'tasks'/'procs' files.
2365 * Reading this file can return large amounts of data if a cgroup has
2366 * *lots* of attached tasks. So it may need several calls to read(),
2367 * but we cannot guarantee that the information we produce is correct
2368 * unless we produce it entirely atomically.
2373 * The following two functions "fix" the issue where there are more pids
2374 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
2375 * TODO: replace with a kernel-wide solution to this problem
2377 #define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
2378 static void *pidlist_allocate(int count
)
2380 if (PIDLIST_TOO_LARGE(count
))
2381 return vmalloc(count
* sizeof(pid_t
));
2383 return kmalloc(count
* sizeof(pid_t
), GFP_KERNEL
);
2385 static void pidlist_free(void *p
)
2387 if (is_vmalloc_addr(p
))
2392 static void *pidlist_resize(void *p
, int newcount
)
2395 /* note: if new alloc fails, old p will still be valid either way */
2396 if (is_vmalloc_addr(p
)) {
2397 newlist
= vmalloc(newcount
* sizeof(pid_t
));
2400 memcpy(newlist
, p
, newcount
* sizeof(pid_t
));
2403 newlist
= krealloc(p
, newcount
* sizeof(pid_t
), GFP_KERNEL
);
2409 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
2410 * If the new stripped list is sufficiently smaller and there's enough memory
2411 * to allocate a new buffer, will let go of the unneeded memory. Returns the
2412 * number of unique elements.
2414 /* is the size difference enough that we should re-allocate the array? */
2415 #define PIDLIST_REALLOC_DIFFERENCE(old, new) ((old) - PAGE_SIZE >= (new))
2416 static int pidlist_uniq(pid_t
**p
, int length
)
2423 * we presume the 0th element is unique, so i starts at 1. trivial
2424 * edge cases first; no work needs to be done for either
2426 if (length
== 0 || length
== 1)
2428 /* src and dest walk down the list; dest counts unique elements */
2429 for (src
= 1; src
< length
; src
++) {
2430 /* find next unique element */
2431 while (list
[src
] == list
[src
-1]) {
2436 /* dest always points to where the next unique element goes */
2437 list
[dest
] = list
[src
];
2442 * if the length difference is large enough, we want to allocate a
2443 * smaller buffer to save memory. if this fails due to out of memory,
2444 * we'll just stay with what we've got.
2446 if (PIDLIST_REALLOC_DIFFERENCE(length
, dest
)) {
2447 newlist
= pidlist_resize(list
, dest
);
2454 static int cmppid(const void *a
, const void *b
)
2456 return *(pid_t
*)a
- *(pid_t
*)b
;
2460 * find the appropriate pidlist for our purpose (given procs vs tasks)
2461 * returns with the lock on that pidlist already held, and takes care
2462 * of the use count, or returns NULL with no locks held if we're out of
2465 static struct cgroup_pidlist
*cgroup_pidlist_find(struct cgroup
*cgrp
,
2466 enum cgroup_filetype type
)
2468 struct cgroup_pidlist
*l
;
2469 /* don't need task_nsproxy() if we're looking at ourself */
2470 struct pid_namespace
*ns
= get_pid_ns(current
->nsproxy
->pid_ns
);
2472 * We can't drop the pidlist_mutex before taking the l->mutex in case
2473 * the last ref-holder is trying to remove l from the list at the same
2474 * time. Holding the pidlist_mutex precludes somebody taking whichever
2475 * list we find out from under us - compare release_pid_array().
2477 mutex_lock(&cgrp
->pidlist_mutex
);
2478 list_for_each_entry(l
, &cgrp
->pidlists
, links
) {
2479 if (l
->key
.type
== type
&& l
->key
.ns
== ns
) {
2480 /* found a matching list - drop the extra refcount */
2482 /* make sure l doesn't vanish out from under us */
2483 down_write(&l
->mutex
);
2484 mutex_unlock(&cgrp
->pidlist_mutex
);
2488 /* entry not found; create a new one */
2489 l
= kmalloc(sizeof(struct cgroup_pidlist
), GFP_KERNEL
);
2491 mutex_unlock(&cgrp
->pidlist_mutex
);
2495 init_rwsem(&l
->mutex
);
2496 down_write(&l
->mutex
);
2499 l
->use_count
= 0; /* don't increment here */
2502 list_add(&l
->links
, &cgrp
->pidlists
);
2503 mutex_unlock(&cgrp
->pidlist_mutex
);
2508 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
2510 static int pidlist_array_load(struct cgroup
*cgrp
, enum cgroup_filetype type
,
2511 struct cgroup_pidlist
**lp
)
2515 int pid
, n
= 0; /* used for populating the array */
2516 struct cgroup_iter it
;
2517 struct task_struct
*tsk
;
2518 struct cgroup_pidlist
*l
;
2521 * If cgroup gets more users after we read count, we won't have
2522 * enough space - tough. This race is indistinguishable to the
2523 * caller from the case that the additional cgroup users didn't
2524 * show up until sometime later on.
2526 length
= cgroup_task_count(cgrp
);
2527 array
= pidlist_allocate(length
);
2530 /* now, populate the array */
2531 cgroup_iter_start(cgrp
, &it
);
2532 while ((tsk
= cgroup_iter_next(cgrp
, &it
))) {
2533 if (unlikely(n
== length
))
2535 /* get tgid or pid for procs or tasks file respectively */
2536 if (type
== CGROUP_FILE_PROCS
)
2537 pid
= task_tgid_vnr(tsk
);
2539 pid
= task_pid_vnr(tsk
);
2540 if (pid
> 0) /* make sure to only use valid results */
2543 cgroup_iter_end(cgrp
, &it
);
2545 /* now sort & (if procs) strip out duplicates */
2546 sort(array
, length
, sizeof(pid_t
), cmppid
, NULL
);
2547 if (type
== CGROUP_FILE_PROCS
)
2548 length
= pidlist_uniq(&array
, length
);
2549 l
= cgroup_pidlist_find(cgrp
, type
);
2551 pidlist_free(array
);
2554 /* store array, freeing old if necessary - lock already held */
2555 pidlist_free(l
->list
);
2559 up_write(&l
->mutex
);
2565 * cgroupstats_build - build and fill cgroupstats
2566 * @stats: cgroupstats to fill information into
2567 * @dentry: A dentry entry belonging to the cgroup for which stats have
2570 * Build and fill cgroupstats so that taskstats can export it to user
2573 int cgroupstats_build(struct cgroupstats
*stats
, struct dentry
*dentry
)
2576 struct cgroup
*cgrp
;
2577 struct cgroup_iter it
;
2578 struct task_struct
*tsk
;
2581 * Validate dentry by checking the superblock operations,
2582 * and make sure it's a directory.
2584 if (dentry
->d_sb
->s_op
!= &cgroup_ops
||
2585 !S_ISDIR(dentry
->d_inode
->i_mode
))
2589 cgrp
= dentry
->d_fsdata
;
2591 cgroup_iter_start(cgrp
, &it
);
2592 while ((tsk
= cgroup_iter_next(cgrp
, &it
))) {
2593 switch (tsk
->state
) {
2595 stats
->nr_running
++;
2597 case TASK_INTERRUPTIBLE
:
2598 stats
->nr_sleeping
++;
2600 case TASK_UNINTERRUPTIBLE
:
2601 stats
->nr_uninterruptible
++;
2604 stats
->nr_stopped
++;
2607 if (delayacct_is_task_waiting_on_io(tsk
))
2608 stats
->nr_io_wait
++;
2612 cgroup_iter_end(cgrp
, &it
);
2620 * seq_file methods for the tasks/procs files. The seq_file position is the
2621 * next pid to display; the seq_file iterator is a pointer to the pid
2622 * in the cgroup->l->list array.
2625 static void *cgroup_pidlist_start(struct seq_file
*s
, loff_t
*pos
)
2628 * Initially we receive a position value that corresponds to
2629 * one more than the last pid shown (or 0 on the first call or
2630 * after a seek to the start). Use a binary-search to find the
2631 * next pid to display, if any
2633 struct cgroup_pidlist
*l
= s
->private;
2634 int index
= 0, pid
= *pos
;
2637 down_read(&l
->mutex
);
2639 int end
= l
->length
;
2641 while (index
< end
) {
2642 int mid
= (index
+ end
) / 2;
2643 if (l
->list
[mid
] == pid
) {
2646 } else if (l
->list
[mid
] <= pid
)
2652 /* If we're off the end of the array, we're done */
2653 if (index
>= l
->length
)
2655 /* Update the abstract position to be the actual pid that we found */
2656 iter
= l
->list
+ index
;
2661 static void cgroup_pidlist_stop(struct seq_file
*s
, void *v
)
2663 struct cgroup_pidlist
*l
= s
->private;
2667 static void *cgroup_pidlist_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
2669 struct cgroup_pidlist
*l
= s
->private;
2671 pid_t
*end
= l
->list
+ l
->length
;
2673 * Advance to the next pid in the array. If this goes off the
2685 static int cgroup_pidlist_show(struct seq_file
*s
, void *v
)
2687 return seq_printf(s
, "%d\n", *(int *)v
);
2691 * seq_operations functions for iterating on pidlists through seq_file -
2692 * independent of whether it's tasks or procs
2694 static const struct seq_operations cgroup_pidlist_seq_operations
= {
2695 .start
= cgroup_pidlist_start
,
2696 .stop
= cgroup_pidlist_stop
,
2697 .next
= cgroup_pidlist_next
,
2698 .show
= cgroup_pidlist_show
,
2701 static void cgroup_release_pid_array(struct cgroup_pidlist
*l
)
2704 * the case where we're the last user of this particular pidlist will
2705 * have us remove it from the cgroup's list, which entails taking the
2706 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
2707 * pidlist_mutex, we have to take pidlist_mutex first.
2709 mutex_lock(&l
->owner
->pidlist_mutex
);
2710 down_write(&l
->mutex
);
2711 BUG_ON(!l
->use_count
);
2712 if (!--l
->use_count
) {
2713 /* we're the last user if refcount is 0; remove and free */
2714 list_del(&l
->links
);
2715 mutex_unlock(&l
->owner
->pidlist_mutex
);
2716 pidlist_free(l
->list
);
2717 put_pid_ns(l
->key
.ns
);
2718 up_write(&l
->mutex
);
2722 mutex_unlock(&l
->owner
->pidlist_mutex
);
2723 up_write(&l
->mutex
);
2726 static int cgroup_pidlist_release(struct inode
*inode
, struct file
*file
)
2728 struct cgroup_pidlist
*l
;
2729 if (!(file
->f_mode
& FMODE_READ
))
2732 * the seq_file will only be initialized if the file was opened for
2733 * reading; hence we check if it's not null only in that case.
2735 l
= ((struct seq_file
*)file
->private_data
)->private;
2736 cgroup_release_pid_array(l
);
2737 return seq_release(inode
, file
);
2740 static const struct file_operations cgroup_pidlist_operations
= {
2742 .llseek
= seq_lseek
,
2743 .write
= cgroup_file_write
,
2744 .release
= cgroup_pidlist_release
,
2748 * The following functions handle opens on a file that displays a pidlist
2749 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
2752 /* helper function for the two below it */
2753 static int cgroup_pidlist_open(struct file
*file
, enum cgroup_filetype type
)
2755 struct cgroup
*cgrp
= __d_cgrp(file
->f_dentry
->d_parent
);
2756 struct cgroup_pidlist
*l
;
2759 /* Nothing to do for write-only files */
2760 if (!(file
->f_mode
& FMODE_READ
))
2763 /* have the array populated */
2764 retval
= pidlist_array_load(cgrp
, type
, &l
);
2767 /* configure file information */
2768 file
->f_op
= &cgroup_pidlist_operations
;
2770 retval
= seq_open(file
, &cgroup_pidlist_seq_operations
);
2772 cgroup_release_pid_array(l
);
2775 ((struct seq_file
*)file
->private_data
)->private = l
;
2778 static int cgroup_tasks_open(struct inode
*unused
, struct file
*file
)
2780 return cgroup_pidlist_open(file
, CGROUP_FILE_TASKS
);
2782 static int cgroup_procs_open(struct inode
*unused
, struct file
*file
)
2784 return cgroup_pidlist_open(file
, CGROUP_FILE_PROCS
);
2787 static u64
cgroup_read_notify_on_release(struct cgroup
*cgrp
,
2790 return notify_on_release(cgrp
);
2793 static int cgroup_write_notify_on_release(struct cgroup
*cgrp
,
2797 clear_bit(CGRP_RELEASABLE
, &cgrp
->flags
);
2799 set_bit(CGRP_NOTIFY_ON_RELEASE
, &cgrp
->flags
);
2801 clear_bit(CGRP_NOTIFY_ON_RELEASE
, &cgrp
->flags
);
2806 * for the common functions, 'private' gives the type of file
2808 /* for hysterical raisins, we can't put this on the older files */
2809 #define CGROUP_FILE_GENERIC_PREFIX "cgroup."
2810 static struct cftype files
[] = {
2813 .open
= cgroup_tasks_open
,
2814 .write_u64
= cgroup_tasks_write
,
2815 .release
= cgroup_pidlist_release
,
2816 .mode
= S_IRUGO
| S_IWUSR
,
2819 .name
= CGROUP_FILE_GENERIC_PREFIX
"procs",
2820 .open
= cgroup_procs_open
,
2821 /* .write_u64 = cgroup_procs_write, TODO */
2822 .release
= cgroup_pidlist_release
,
2826 .name
= "notify_on_release",
2827 .read_u64
= cgroup_read_notify_on_release
,
2828 .write_u64
= cgroup_write_notify_on_release
,
2832 static struct cftype cft_release_agent
= {
2833 .name
= "release_agent",
2834 .read_seq_string
= cgroup_release_agent_show
,
2835 .write_string
= cgroup_release_agent_write
,
2836 .max_write_len
= PATH_MAX
,
2839 static int cgroup_populate_dir(struct cgroup
*cgrp
)
2842 struct cgroup_subsys
*ss
;
2844 /* First clear out any existing files */
2845 cgroup_clear_directory(cgrp
->dentry
);
2847 err
= cgroup_add_files(cgrp
, NULL
, files
, ARRAY_SIZE(files
));
2851 if (cgrp
== cgrp
->top_cgroup
) {
2852 if ((err
= cgroup_add_file(cgrp
, NULL
, &cft_release_agent
)) < 0)
2856 for_each_subsys(cgrp
->root
, ss
) {
2857 if (ss
->populate
&& (err
= ss
->populate(ss
, cgrp
)) < 0)
2860 /* This cgroup is ready now */
2861 for_each_subsys(cgrp
->root
, ss
) {
2862 struct cgroup_subsys_state
*css
= cgrp
->subsys
[ss
->subsys_id
];
2864 * Update id->css pointer and make this css visible from
2865 * CSS ID functions. This pointer will be dereferened
2866 * from RCU-read-side without locks.
2869 rcu_assign_pointer(css
->id
->css
, css
);
2875 static void init_cgroup_css(struct cgroup_subsys_state
*css
,
2876 struct cgroup_subsys
*ss
,
2877 struct cgroup
*cgrp
)
2880 atomic_set(&css
->refcnt
, 1);
2883 if (cgrp
== dummytop
)
2884 set_bit(CSS_ROOT
, &css
->flags
);
2885 BUG_ON(cgrp
->subsys
[ss
->subsys_id
]);
2886 cgrp
->subsys
[ss
->subsys_id
] = css
;
2889 static void cgroup_lock_hierarchy(struct cgroupfs_root
*root
)
2891 /* We need to take each hierarchy_mutex in a consistent order */
2894 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
2895 struct cgroup_subsys
*ss
= subsys
[i
];
2896 if (ss
->root
== root
)
2897 mutex_lock(&ss
->hierarchy_mutex
);
2901 static void cgroup_unlock_hierarchy(struct cgroupfs_root
*root
)
2905 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
2906 struct cgroup_subsys
*ss
= subsys
[i
];
2907 if (ss
->root
== root
)
2908 mutex_unlock(&ss
->hierarchy_mutex
);
2913 * cgroup_create - create a cgroup
2914 * @parent: cgroup that will be parent of the new cgroup
2915 * @dentry: dentry of the new cgroup
2916 * @mode: mode to set on new inode
2918 * Must be called with the mutex on the parent inode held
2920 static long cgroup_create(struct cgroup
*parent
, struct dentry
*dentry
,
2923 struct cgroup
*cgrp
;
2924 struct cgroupfs_root
*root
= parent
->root
;
2926 struct cgroup_subsys
*ss
;
2927 struct super_block
*sb
= root
->sb
;
2929 cgrp
= kzalloc(sizeof(*cgrp
), GFP_KERNEL
);
2933 /* Grab a reference on the superblock so the hierarchy doesn't
2934 * get deleted on unmount if there are child cgroups. This
2935 * can be done outside cgroup_mutex, since the sb can't
2936 * disappear while someone has an open control file on the
2938 atomic_inc(&sb
->s_active
);
2940 mutex_lock(&cgroup_mutex
);
2942 init_cgroup_housekeeping(cgrp
);
2944 cgrp
->parent
= parent
;
2945 cgrp
->root
= parent
->root
;
2946 cgrp
->top_cgroup
= parent
->top_cgroup
;
2948 if (notify_on_release(parent
))
2949 set_bit(CGRP_NOTIFY_ON_RELEASE
, &cgrp
->flags
);
2951 for_each_subsys(root
, ss
) {
2952 struct cgroup_subsys_state
*css
= ss
->create(ss
, cgrp
);
2958 init_cgroup_css(css
, ss
, cgrp
);
2960 err
= alloc_css_id(ss
, parent
, cgrp
);
2964 /* At error, ->destroy() callback has to free assigned ID. */
2967 cgroup_lock_hierarchy(root
);
2968 list_add(&cgrp
->sibling
, &cgrp
->parent
->children
);
2969 cgroup_unlock_hierarchy(root
);
2970 root
->number_of_cgroups
++;
2972 err
= cgroup_create_dir(cgrp
, dentry
, mode
);
2976 /* The cgroup directory was pre-locked for us */
2977 BUG_ON(!mutex_is_locked(&cgrp
->dentry
->d_inode
->i_mutex
));
2979 err
= cgroup_populate_dir(cgrp
);
2980 /* If err < 0, we have a half-filled directory - oh well ;) */
2982 mutex_unlock(&cgroup_mutex
);
2983 mutex_unlock(&cgrp
->dentry
->d_inode
->i_mutex
);
2989 cgroup_lock_hierarchy(root
);
2990 list_del(&cgrp
->sibling
);
2991 cgroup_unlock_hierarchy(root
);
2992 root
->number_of_cgroups
--;
2996 for_each_subsys(root
, ss
) {
2997 if (cgrp
->subsys
[ss
->subsys_id
])
2998 ss
->destroy(ss
, cgrp
);
3001 mutex_unlock(&cgroup_mutex
);
3003 /* Release the reference count that we took on the superblock */
3004 deactivate_super(sb
);
3010 static int cgroup_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
3012 struct cgroup
*c_parent
= dentry
->d_parent
->d_fsdata
;
3014 /* the vfs holds inode->i_mutex already */
3015 return cgroup_create(c_parent
, dentry
, mode
| S_IFDIR
);
3018 static int cgroup_has_css_refs(struct cgroup
*cgrp
)
3020 /* Check the reference count on each subsystem. Since we
3021 * already established that there are no tasks in the
3022 * cgroup, if the css refcount is also 1, then there should
3023 * be no outstanding references, so the subsystem is safe to
3024 * destroy. We scan across all subsystems rather than using
3025 * the per-hierarchy linked list of mounted subsystems since
3026 * we can be called via check_for_release() with no
3027 * synchronization other than RCU, and the subsystem linked
3028 * list isn't RCU-safe */
3030 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
3031 struct cgroup_subsys
*ss
= subsys
[i
];
3032 struct cgroup_subsys_state
*css
;
3033 /* Skip subsystems not in this hierarchy */
3034 if (ss
->root
!= cgrp
->root
)
3036 css
= cgrp
->subsys
[ss
->subsys_id
];
3037 /* When called from check_for_release() it's possible
3038 * that by this point the cgroup has been removed
3039 * and the css deleted. But a false-positive doesn't
3040 * matter, since it can only happen if the cgroup
3041 * has been deleted and hence no longer needs the
3042 * release agent to be called anyway. */
3043 if (css
&& (atomic_read(&css
->refcnt
) > 1))
3050 * Atomically mark all (or else none) of the cgroup's CSS objects as
3051 * CSS_REMOVED. Return true on success, or false if the cgroup has
3052 * busy subsystems. Call with cgroup_mutex held
3055 static int cgroup_clear_css_refs(struct cgroup
*cgrp
)
3057 struct cgroup_subsys
*ss
;
3058 unsigned long flags
;
3059 bool failed
= false;
3060 local_irq_save(flags
);
3061 for_each_subsys(cgrp
->root
, ss
) {
3062 struct cgroup_subsys_state
*css
= cgrp
->subsys
[ss
->subsys_id
];
3065 /* We can only remove a CSS with a refcnt==1 */
3066 refcnt
= atomic_read(&css
->refcnt
);
3073 * Drop the refcnt to 0 while we check other
3074 * subsystems. This will cause any racing
3075 * css_tryget() to spin until we set the
3076 * CSS_REMOVED bits or abort
3078 if (atomic_cmpxchg(&css
->refcnt
, refcnt
, 0) == refcnt
)
3084 for_each_subsys(cgrp
->root
, ss
) {
3085 struct cgroup_subsys_state
*css
= cgrp
->subsys
[ss
->subsys_id
];
3088 * Restore old refcnt if we previously managed
3089 * to clear it from 1 to 0
3091 if (!atomic_read(&css
->refcnt
))
3092 atomic_set(&css
->refcnt
, 1);
3094 /* Commit the fact that the CSS is removed */
3095 set_bit(CSS_REMOVED
, &css
->flags
);
3098 local_irq_restore(flags
);
3102 static int cgroup_rmdir(struct inode
*unused_dir
, struct dentry
*dentry
)
3104 struct cgroup
*cgrp
= dentry
->d_fsdata
;
3106 struct cgroup
*parent
;
3110 /* the vfs holds both inode->i_mutex already */
3112 mutex_lock(&cgroup_mutex
);
3113 if (atomic_read(&cgrp
->count
) != 0) {
3114 mutex_unlock(&cgroup_mutex
);
3117 if (!list_empty(&cgrp
->children
)) {
3118 mutex_unlock(&cgroup_mutex
);
3121 mutex_unlock(&cgroup_mutex
);
3124 * In general, subsystem has no css->refcnt after pre_destroy(). But
3125 * in racy cases, subsystem may have to get css->refcnt after
3126 * pre_destroy() and it makes rmdir return with -EBUSY. This sometimes
3127 * make rmdir return -EBUSY too often. To avoid that, we use waitqueue
3128 * for cgroup's rmdir. CGRP_WAIT_ON_RMDIR is for synchronizing rmdir
3129 * and subsystem's reference count handling. Please see css_get/put
3130 * and css_tryget() and cgroup_wakeup_rmdir_waiter() implementation.
3132 set_bit(CGRP_WAIT_ON_RMDIR
, &cgrp
->flags
);
3135 * Call pre_destroy handlers of subsys. Notify subsystems
3136 * that rmdir() request comes.
3138 ret
= cgroup_call_pre_destroy(cgrp
);
3140 clear_bit(CGRP_WAIT_ON_RMDIR
, &cgrp
->flags
);
3144 mutex_lock(&cgroup_mutex
);
3145 parent
= cgrp
->parent
;
3146 if (atomic_read(&cgrp
->count
) || !list_empty(&cgrp
->children
)) {
3147 clear_bit(CGRP_WAIT_ON_RMDIR
, &cgrp
->flags
);
3148 mutex_unlock(&cgroup_mutex
);
3151 prepare_to_wait(&cgroup_rmdir_waitq
, &wait
, TASK_INTERRUPTIBLE
);
3152 if (!cgroup_clear_css_refs(cgrp
)) {
3153 mutex_unlock(&cgroup_mutex
);
3155 * Because someone may call cgroup_wakeup_rmdir_waiter() before
3156 * prepare_to_wait(), we need to check this flag.
3158 if (test_bit(CGRP_WAIT_ON_RMDIR
, &cgrp
->flags
))
3160 finish_wait(&cgroup_rmdir_waitq
, &wait
);
3161 clear_bit(CGRP_WAIT_ON_RMDIR
, &cgrp
->flags
);
3162 if (signal_pending(current
))
3166 /* NO css_tryget() can success after here. */
3167 finish_wait(&cgroup_rmdir_waitq
, &wait
);
3168 clear_bit(CGRP_WAIT_ON_RMDIR
, &cgrp
->flags
);
3170 spin_lock(&release_list_lock
);
3171 set_bit(CGRP_REMOVED
, &cgrp
->flags
);
3172 if (!list_empty(&cgrp
->release_list
))
3173 list_del(&cgrp
->release_list
);
3174 spin_unlock(&release_list_lock
);
3176 cgroup_lock_hierarchy(cgrp
->root
);
3177 /* delete this cgroup from parent->children */
3178 list_del(&cgrp
->sibling
);
3179 cgroup_unlock_hierarchy(cgrp
->root
);
3181 spin_lock(&cgrp
->dentry
->d_lock
);
3182 d
= dget(cgrp
->dentry
);
3183 spin_unlock(&d
->d_lock
);
3185 cgroup_d_remove_dir(d
);
3188 set_bit(CGRP_RELEASABLE
, &parent
->flags
);
3189 check_for_release(parent
);
3191 mutex_unlock(&cgroup_mutex
);
3195 static void __init
cgroup_init_subsys(struct cgroup_subsys
*ss
)
3197 struct cgroup_subsys_state
*css
;
3199 printk(KERN_INFO
"Initializing cgroup subsys %s\n", ss
->name
);
3201 /* Create the top cgroup state for this subsystem */
3202 list_add(&ss
->sibling
, &rootnode
.subsys_list
);
3203 ss
->root
= &rootnode
;
3204 css
= ss
->create(ss
, dummytop
);
3205 /* We don't handle early failures gracefully */
3206 BUG_ON(IS_ERR(css
));
3207 init_cgroup_css(css
, ss
, dummytop
);
3209 /* Update the init_css_set to contain a subsys
3210 * pointer to this state - since the subsystem is
3211 * newly registered, all tasks and hence the
3212 * init_css_set is in the subsystem's top cgroup. */
3213 init_css_set
.subsys
[ss
->subsys_id
] = dummytop
->subsys
[ss
->subsys_id
];
3215 need_forkexit_callback
|= ss
->fork
|| ss
->exit
;
3217 /* At system boot, before all subsystems have been
3218 * registered, no tasks have been forked, so we don't
3219 * need to invoke fork callbacks here. */
3220 BUG_ON(!list_empty(&init_task
.tasks
));
3222 mutex_init(&ss
->hierarchy_mutex
);
3223 lockdep_set_class(&ss
->hierarchy_mutex
, &ss
->subsys_key
);
3228 * cgroup_init_early - cgroup initialization at system boot
3230 * Initialize cgroups at system boot, and initialize any
3231 * subsystems that request early init.
3233 int __init
cgroup_init_early(void)
3236 atomic_set(&init_css_set
.refcount
, 1);
3237 INIT_LIST_HEAD(&init_css_set
.cg_links
);
3238 INIT_LIST_HEAD(&init_css_set
.tasks
);
3239 INIT_HLIST_NODE(&init_css_set
.hlist
);
3241 init_cgroup_root(&rootnode
);
3243 init_task
.cgroups
= &init_css_set
;
3245 init_css_set_link
.cg
= &init_css_set
;
3246 init_css_set_link
.cgrp
= dummytop
;
3247 list_add(&init_css_set_link
.cgrp_link_list
,
3248 &rootnode
.top_cgroup
.css_sets
);
3249 list_add(&init_css_set_link
.cg_link_list
,
3250 &init_css_set
.cg_links
);
3252 for (i
= 0; i
< CSS_SET_TABLE_SIZE
; i
++)
3253 INIT_HLIST_HEAD(&css_set_table
[i
]);
3255 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
3256 struct cgroup_subsys
*ss
= subsys
[i
];
3259 BUG_ON(strlen(ss
->name
) > MAX_CGROUP_TYPE_NAMELEN
);
3260 BUG_ON(!ss
->create
);
3261 BUG_ON(!ss
->destroy
);
3262 if (ss
->subsys_id
!= i
) {
3263 printk(KERN_ERR
"cgroup: Subsys %s id == %d\n",
3264 ss
->name
, ss
->subsys_id
);
3269 cgroup_init_subsys(ss
);
3275 * cgroup_init - cgroup initialization
3277 * Register cgroup filesystem and /proc file, and initialize
3278 * any subsystems that didn't request early init.
3280 int __init
cgroup_init(void)
3284 struct hlist_head
*hhead
;
3286 err
= bdi_init(&cgroup_backing_dev_info
);
3290 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
3291 struct cgroup_subsys
*ss
= subsys
[i
];
3292 if (!ss
->early_init
)
3293 cgroup_init_subsys(ss
);
3295 cgroup_subsys_init_idr(ss
);
3298 /* Add init_css_set to the hash table */
3299 hhead
= css_set_hash(init_css_set
.subsys
);
3300 hlist_add_head(&init_css_set
.hlist
, hhead
);
3301 BUG_ON(!init_root_id(&rootnode
));
3302 err
= register_filesystem(&cgroup_fs_type
);
3306 proc_create("cgroups", 0, NULL
, &proc_cgroupstats_operations
);
3310 bdi_destroy(&cgroup_backing_dev_info
);
3316 * proc_cgroup_show()
3317 * - Print task's cgroup paths into seq_file, one line for each hierarchy
3318 * - Used for /proc/<pid>/cgroup.
3319 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
3320 * doesn't really matter if tsk->cgroup changes after we read it,
3321 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
3322 * anyway. No need to check that tsk->cgroup != NULL, thanks to
3323 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
3324 * cgroup to top_cgroup.
3327 /* TODO: Use a proper seq_file iterator */
3328 static int proc_cgroup_show(struct seq_file
*m
, void *v
)
3331 struct task_struct
*tsk
;
3334 struct cgroupfs_root
*root
;
3337 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
3343 tsk
= get_pid_task(pid
, PIDTYPE_PID
);
3349 mutex_lock(&cgroup_mutex
);
3351 for_each_active_root(root
) {
3352 struct cgroup_subsys
*ss
;
3353 struct cgroup
*cgrp
;
3356 seq_printf(m
, "%d:", root
->hierarchy_id
);
3357 for_each_subsys(root
, ss
)
3358 seq_printf(m
, "%s%s", count
++ ? "," : "", ss
->name
);
3359 if (strlen(root
->name
))
3360 seq_printf(m
, "%sname=%s", count
? "," : "",
3363 cgrp
= task_cgroup_from_root(tsk
, root
);
3364 retval
= cgroup_path(cgrp
, buf
, PAGE_SIZE
);
3372 mutex_unlock(&cgroup_mutex
);
3373 put_task_struct(tsk
);
3380 static int cgroup_open(struct inode
*inode
, struct file
*file
)
3382 struct pid
*pid
= PROC_I(inode
)->pid
;
3383 return single_open(file
, proc_cgroup_show
, pid
);
3386 const struct file_operations proc_cgroup_operations
= {
3387 .open
= cgroup_open
,
3389 .llseek
= seq_lseek
,
3390 .release
= single_release
,
3393 /* Display information about each subsystem and each hierarchy */
3394 static int proc_cgroupstats_show(struct seq_file
*m
, void *v
)
3398 seq_puts(m
, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
3399 mutex_lock(&cgroup_mutex
);
3400 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
3401 struct cgroup_subsys
*ss
= subsys
[i
];
3402 seq_printf(m
, "%s\t%d\t%d\t%d\n",
3403 ss
->name
, ss
->root
->hierarchy_id
,
3404 ss
->root
->number_of_cgroups
, !ss
->disabled
);
3406 mutex_unlock(&cgroup_mutex
);
3410 static int cgroupstats_open(struct inode
*inode
, struct file
*file
)
3412 return single_open(file
, proc_cgroupstats_show
, NULL
);
3415 static const struct file_operations proc_cgroupstats_operations
= {
3416 .open
= cgroupstats_open
,
3418 .llseek
= seq_lseek
,
3419 .release
= single_release
,
3423 * cgroup_fork - attach newly forked task to its parents cgroup.
3424 * @child: pointer to task_struct of forking parent process.
3426 * Description: A task inherits its parent's cgroup at fork().
3428 * A pointer to the shared css_set was automatically copied in
3429 * fork.c by dup_task_struct(). However, we ignore that copy, since
3430 * it was not made under the protection of RCU or cgroup_mutex, so
3431 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
3432 * have already changed current->cgroups, allowing the previously
3433 * referenced cgroup group to be removed and freed.
3435 * At the point that cgroup_fork() is called, 'current' is the parent
3436 * task, and the passed argument 'child' points to the child task.
3438 void cgroup_fork(struct task_struct
*child
)
3441 child
->cgroups
= current
->cgroups
;
3442 get_css_set(child
->cgroups
);
3443 task_unlock(current
);
3444 INIT_LIST_HEAD(&child
->cg_list
);
3448 * cgroup_fork_callbacks - run fork callbacks
3449 * @child: the new task
3451 * Called on a new task very soon before adding it to the
3452 * tasklist. No need to take any locks since no-one can
3453 * be operating on this task.
3455 void cgroup_fork_callbacks(struct task_struct
*child
)
3457 if (need_forkexit_callback
) {
3459 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
3460 struct cgroup_subsys
*ss
= subsys
[i
];
3462 ss
->fork(ss
, child
);
3468 * cgroup_post_fork - called on a new task after adding it to the task list
3469 * @child: the task in question
3471 * Adds the task to the list running through its css_set if necessary.
3472 * Has to be after the task is visible on the task list in case we race
3473 * with the first call to cgroup_iter_start() - to guarantee that the
3474 * new task ends up on its list.
3476 void cgroup_post_fork(struct task_struct
*child
)
3478 if (use_task_css_set_links
) {
3479 write_lock(&css_set_lock
);
3481 if (list_empty(&child
->cg_list
))
3482 list_add(&child
->cg_list
, &child
->cgroups
->tasks
);
3484 write_unlock(&css_set_lock
);
3488 * cgroup_exit - detach cgroup from exiting task
3489 * @tsk: pointer to task_struct of exiting process
3490 * @run_callback: run exit callbacks?
3492 * Description: Detach cgroup from @tsk and release it.
3494 * Note that cgroups marked notify_on_release force every task in
3495 * them to take the global cgroup_mutex mutex when exiting.
3496 * This could impact scaling on very large systems. Be reluctant to
3497 * use notify_on_release cgroups where very high task exit scaling
3498 * is required on large systems.
3500 * the_top_cgroup_hack:
3502 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
3504 * We call cgroup_exit() while the task is still competent to
3505 * handle notify_on_release(), then leave the task attached to the
3506 * root cgroup in each hierarchy for the remainder of its exit.
3508 * To do this properly, we would increment the reference count on
3509 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
3510 * code we would add a second cgroup function call, to drop that
3511 * reference. This would just create an unnecessary hot spot on
3512 * the top_cgroup reference count, to no avail.
3514 * Normally, holding a reference to a cgroup without bumping its
3515 * count is unsafe. The cgroup could go away, or someone could
3516 * attach us to a different cgroup, decrementing the count on
3517 * the first cgroup that we never incremented. But in this case,
3518 * top_cgroup isn't going away, and either task has PF_EXITING set,
3519 * which wards off any cgroup_attach_task() attempts, or task is a failed
3520 * fork, never visible to cgroup_attach_task.
3522 void cgroup_exit(struct task_struct
*tsk
, int run_callbacks
)
3527 if (run_callbacks
&& need_forkexit_callback
) {
3528 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
3529 struct cgroup_subsys
*ss
= subsys
[i
];
3536 * Unlink from the css_set task list if necessary.
3537 * Optimistically check cg_list before taking
3540 if (!list_empty(&tsk
->cg_list
)) {
3541 write_lock(&css_set_lock
);
3542 if (!list_empty(&tsk
->cg_list
))
3543 list_del(&tsk
->cg_list
);
3544 write_unlock(&css_set_lock
);
3547 /* Reassign the task to the init_css_set. */
3550 tsk
->cgroups
= &init_css_set
;
3553 put_css_set_taskexit(cg
);
3557 * cgroup_clone - clone the cgroup the given subsystem is attached to
3558 * @tsk: the task to be moved
3559 * @subsys: the given subsystem
3560 * @nodename: the name for the new cgroup
3562 * Duplicate the current cgroup in the hierarchy that the given
3563 * subsystem is attached to, and move this task into the new
3566 int cgroup_clone(struct task_struct
*tsk
, struct cgroup_subsys
*subsys
,
3569 struct dentry
*dentry
;
3571 struct cgroup
*parent
, *child
;
3572 struct inode
*inode
;
3574 struct cgroupfs_root
*root
;
3575 struct cgroup_subsys
*ss
;
3577 /* We shouldn't be called by an unregistered subsystem */
3578 BUG_ON(!subsys
->active
);
3580 /* First figure out what hierarchy and cgroup we're dealing
3581 * with, and pin them so we can drop cgroup_mutex */
3582 mutex_lock(&cgroup_mutex
);
3584 root
= subsys
->root
;
3585 if (root
== &rootnode
) {
3586 mutex_unlock(&cgroup_mutex
);
3590 /* Pin the hierarchy */
3591 if (!atomic_inc_not_zero(&root
->sb
->s_active
)) {
3592 /* We race with the final deactivate_super() */
3593 mutex_unlock(&cgroup_mutex
);
3597 /* Keep the cgroup alive */
3599 parent
= task_cgroup(tsk
, subsys
->subsys_id
);
3604 mutex_unlock(&cgroup_mutex
);
3606 /* Now do the VFS work to create a cgroup */
3607 inode
= parent
->dentry
->d_inode
;
3609 /* Hold the parent directory mutex across this operation to
3610 * stop anyone else deleting the new cgroup */
3611 mutex_lock(&inode
->i_mutex
);
3612 dentry
= lookup_one_len(nodename
, parent
->dentry
, strlen(nodename
));
3613 if (IS_ERR(dentry
)) {
3615 "cgroup: Couldn't allocate dentry for %s: %ld\n", nodename
,
3617 ret
= PTR_ERR(dentry
);
3621 /* Create the cgroup directory, which also creates the cgroup */
3622 ret
= vfs_mkdir(inode
, dentry
, 0755);
3623 child
= __d_cgrp(dentry
);
3627 "Failed to create cgroup %s: %d\n", nodename
,
3632 /* The cgroup now exists. Retake cgroup_mutex and check
3633 * that we're still in the same state that we thought we
3635 mutex_lock(&cgroup_mutex
);
3636 if ((root
!= subsys
->root
) ||
3637 (parent
!= task_cgroup(tsk
, subsys
->subsys_id
))) {
3638 /* Aargh, we raced ... */
3639 mutex_unlock(&inode
->i_mutex
);
3642 deactivate_super(root
->sb
);
3643 /* The cgroup is still accessible in the VFS, but
3644 * we're not going to try to rmdir() it at this
3647 "Race in cgroup_clone() - leaking cgroup %s\n",
3652 /* do any required auto-setup */
3653 for_each_subsys(root
, ss
) {
3655 ss
->post_clone(ss
, child
);
3658 /* All seems fine. Finish by moving the task into the new cgroup */
3659 ret
= cgroup_attach_task(child
, tsk
);
3660 mutex_unlock(&cgroup_mutex
);
3663 mutex_unlock(&inode
->i_mutex
);
3665 mutex_lock(&cgroup_mutex
);
3667 mutex_unlock(&cgroup_mutex
);
3668 deactivate_super(root
->sb
);
3673 * cgroup_is_descendant - see if @cgrp is a descendant of @task's cgrp
3674 * @cgrp: the cgroup in question
3675 * @task: the task in question
3677 * See if @cgrp is a descendant of @task's cgroup in the appropriate
3680 * If we are sending in dummytop, then presumably we are creating
3681 * the top cgroup in the subsystem.
3683 * Called only by the ns (nsproxy) cgroup.
3685 int cgroup_is_descendant(const struct cgroup
*cgrp
, struct task_struct
*task
)
3688 struct cgroup
*target
;
3690 if (cgrp
== dummytop
)
3693 target
= task_cgroup_from_root(task
, cgrp
->root
);
3694 while (cgrp
!= target
&& cgrp
!= cgrp
->top_cgroup
)
3695 cgrp
= cgrp
->parent
;
3696 ret
= (cgrp
== target
);
3700 static void check_for_release(struct cgroup
*cgrp
)
3702 /* All of these checks rely on RCU to keep the cgroup
3703 * structure alive */
3704 if (cgroup_is_releasable(cgrp
) && !atomic_read(&cgrp
->count
)
3705 && list_empty(&cgrp
->children
) && !cgroup_has_css_refs(cgrp
)) {
3706 /* Control Group is currently removeable. If it's not
3707 * already queued for a userspace notification, queue
3709 int need_schedule_work
= 0;
3710 spin_lock(&release_list_lock
);
3711 if (!cgroup_is_removed(cgrp
) &&
3712 list_empty(&cgrp
->release_list
)) {
3713 list_add(&cgrp
->release_list
, &release_list
);
3714 need_schedule_work
= 1;
3716 spin_unlock(&release_list_lock
);
3717 if (need_schedule_work
)
3718 schedule_work(&release_agent_work
);
3722 void __css_put(struct cgroup_subsys_state
*css
)
3724 struct cgroup
*cgrp
= css
->cgroup
;
3727 val
= atomic_dec_return(&css
->refcnt
);
3729 if (notify_on_release(cgrp
)) {
3730 set_bit(CGRP_RELEASABLE
, &cgrp
->flags
);
3731 check_for_release(cgrp
);
3733 cgroup_wakeup_rmdir_waiter(cgrp
);
3736 WARN_ON_ONCE(val
< 1);
3740 * Notify userspace when a cgroup is released, by running the
3741 * configured release agent with the name of the cgroup (path
3742 * relative to the root of cgroup file system) as the argument.
3744 * Most likely, this user command will try to rmdir this cgroup.
3746 * This races with the possibility that some other task will be
3747 * attached to this cgroup before it is removed, or that some other
3748 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
3749 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
3750 * unused, and this cgroup will be reprieved from its death sentence,
3751 * to continue to serve a useful existence. Next time it's released,
3752 * we will get notified again, if it still has 'notify_on_release' set.
3754 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
3755 * means only wait until the task is successfully execve()'d. The
3756 * separate release agent task is forked by call_usermodehelper(),
3757 * then control in this thread returns here, without waiting for the
3758 * release agent task. We don't bother to wait because the caller of
3759 * this routine has no use for the exit status of the release agent
3760 * task, so no sense holding our caller up for that.
3762 static void cgroup_release_agent(struct work_struct
*work
)
3764 BUG_ON(work
!= &release_agent_work
);
3765 mutex_lock(&cgroup_mutex
);
3766 spin_lock(&release_list_lock
);
3767 while (!list_empty(&release_list
)) {
3768 char *argv
[3], *envp
[3];
3770 char *pathbuf
= NULL
, *agentbuf
= NULL
;
3771 struct cgroup
*cgrp
= list_entry(release_list
.next
,
3774 list_del_init(&cgrp
->release_list
);
3775 spin_unlock(&release_list_lock
);
3776 pathbuf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
3779 if (cgroup_path(cgrp
, pathbuf
, PAGE_SIZE
) < 0)
3781 agentbuf
= kstrdup(cgrp
->root
->release_agent_path
, GFP_KERNEL
);
3786 argv
[i
++] = agentbuf
;
3787 argv
[i
++] = pathbuf
;
3791 /* minimal command environment */
3792 envp
[i
++] = "HOME=/";
3793 envp
[i
++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
3796 /* Drop the lock while we invoke the usermode helper,
3797 * since the exec could involve hitting disk and hence
3798 * be a slow process */
3799 mutex_unlock(&cgroup_mutex
);
3800 call_usermodehelper(argv
[0], argv
, envp
, UMH_WAIT_EXEC
);
3801 mutex_lock(&cgroup_mutex
);
3805 spin_lock(&release_list_lock
);
3807 spin_unlock(&release_list_lock
);
3808 mutex_unlock(&cgroup_mutex
);
3811 static int __init
cgroup_disable(char *str
)
3816 while ((token
= strsep(&str
, ",")) != NULL
) {
3820 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
3821 struct cgroup_subsys
*ss
= subsys
[i
];
3823 if (!strcmp(token
, ss
->name
)) {
3825 printk(KERN_INFO
"Disabling %s control group"
3826 " subsystem\n", ss
->name
);
3833 __setup("cgroup_disable=", cgroup_disable
);
3836 * Functons for CSS ID.
3840 *To get ID other than 0, this should be called when !cgroup_is_removed().
3842 unsigned short css_id(struct cgroup_subsys_state
*css
)
3844 struct css_id
*cssid
= rcu_dereference(css
->id
);
3851 unsigned short css_depth(struct cgroup_subsys_state
*css
)
3853 struct css_id
*cssid
= rcu_dereference(css
->id
);
3856 return cssid
->depth
;
3860 bool css_is_ancestor(struct cgroup_subsys_state
*child
,
3861 const struct cgroup_subsys_state
*root
)
3863 struct css_id
*child_id
= rcu_dereference(child
->id
);
3864 struct css_id
*root_id
= rcu_dereference(root
->id
);
3866 if (!child_id
|| !root_id
|| (child_id
->depth
< root_id
->depth
))
3868 return child_id
->stack
[root_id
->depth
] == root_id
->id
;
3871 static void __free_css_id_cb(struct rcu_head
*head
)
3875 id
= container_of(head
, struct css_id
, rcu_head
);
3879 void free_css_id(struct cgroup_subsys
*ss
, struct cgroup_subsys_state
*css
)
3881 struct css_id
*id
= css
->id
;
3882 /* When this is called before css_id initialization, id can be NULL */
3886 BUG_ON(!ss
->use_id
);
3888 rcu_assign_pointer(id
->css
, NULL
);
3889 rcu_assign_pointer(css
->id
, NULL
);
3890 spin_lock(&ss
->id_lock
);
3891 idr_remove(&ss
->idr
, id
->id
);
3892 spin_unlock(&ss
->id_lock
);
3893 call_rcu(&id
->rcu_head
, __free_css_id_cb
);
3897 * This is called by init or create(). Then, calls to this function are
3898 * always serialized (By cgroup_mutex() at create()).
3901 static struct css_id
*get_new_cssid(struct cgroup_subsys
*ss
, int depth
)
3903 struct css_id
*newid
;
3904 int myid
, error
, size
;
3906 BUG_ON(!ss
->use_id
);
3908 size
= sizeof(*newid
) + sizeof(unsigned short) * (depth
+ 1);
3909 newid
= kzalloc(size
, GFP_KERNEL
);
3911 return ERR_PTR(-ENOMEM
);
3913 if (unlikely(!idr_pre_get(&ss
->idr
, GFP_KERNEL
))) {
3917 spin_lock(&ss
->id_lock
);
3918 /* Don't use 0. allocates an ID of 1-65535 */
3919 error
= idr_get_new_above(&ss
->idr
, newid
, 1, &myid
);
3920 spin_unlock(&ss
->id_lock
);
3922 /* Returns error when there are no free spaces for new ID.*/
3927 if (myid
> CSS_ID_MAX
)
3931 newid
->depth
= depth
;
3935 spin_lock(&ss
->id_lock
);
3936 idr_remove(&ss
->idr
, myid
);
3937 spin_unlock(&ss
->id_lock
);
3940 return ERR_PTR(error
);
3944 static int __init
cgroup_subsys_init_idr(struct cgroup_subsys
*ss
)
3946 struct css_id
*newid
;
3947 struct cgroup_subsys_state
*rootcss
;
3949 spin_lock_init(&ss
->id_lock
);
3952 rootcss
= init_css_set
.subsys
[ss
->subsys_id
];
3953 newid
= get_new_cssid(ss
, 0);
3955 return PTR_ERR(newid
);
3957 newid
->stack
[0] = newid
->id
;
3958 newid
->css
= rootcss
;
3959 rootcss
->id
= newid
;
3963 static int alloc_css_id(struct cgroup_subsys
*ss
, struct cgroup
*parent
,
3964 struct cgroup
*child
)
3966 int subsys_id
, i
, depth
= 0;
3967 struct cgroup_subsys_state
*parent_css
, *child_css
;
3968 struct css_id
*child_id
, *parent_id
= NULL
;
3970 subsys_id
= ss
->subsys_id
;
3971 parent_css
= parent
->subsys
[subsys_id
];
3972 child_css
= child
->subsys
[subsys_id
];
3973 depth
= css_depth(parent_css
) + 1;
3974 parent_id
= parent_css
->id
;
3976 child_id
= get_new_cssid(ss
, depth
);
3977 if (IS_ERR(child_id
))
3978 return PTR_ERR(child_id
);
3980 for (i
= 0; i
< depth
; i
++)
3981 child_id
->stack
[i
] = parent_id
->stack
[i
];
3982 child_id
->stack
[depth
] = child_id
->id
;
3984 * child_id->css pointer will be set after this cgroup is available
3985 * see cgroup_populate_dir()
3987 rcu_assign_pointer(child_css
->id
, child_id
);
3993 * css_lookup - lookup css by id
3994 * @ss: cgroup subsys to be looked into.
3997 * Returns pointer to cgroup_subsys_state if there is valid one with id.
3998 * NULL if not. Should be called under rcu_read_lock()
4000 struct cgroup_subsys_state
*css_lookup(struct cgroup_subsys
*ss
, int id
)
4002 struct css_id
*cssid
= NULL
;
4004 BUG_ON(!ss
->use_id
);
4005 cssid
= idr_find(&ss
->idr
, id
);
4007 if (unlikely(!cssid
))
4010 return rcu_dereference(cssid
->css
);
4014 * css_get_next - lookup next cgroup under specified hierarchy.
4015 * @ss: pointer to subsystem
4016 * @id: current position of iteration.
4017 * @root: pointer to css. search tree under this.
4018 * @foundid: position of found object.
4020 * Search next css under the specified hierarchy of rootid. Calling under
4021 * rcu_read_lock() is necessary. Returns NULL if it reaches the end.
4023 struct cgroup_subsys_state
*
4024 css_get_next(struct cgroup_subsys
*ss
, int id
,
4025 struct cgroup_subsys_state
*root
, int *foundid
)
4027 struct cgroup_subsys_state
*ret
= NULL
;
4030 int rootid
= css_id(root
);
4031 int depth
= css_depth(root
);
4036 BUG_ON(!ss
->use_id
);
4037 /* fill start point for scan */
4041 * scan next entry from bitmap(tree), tmpid is updated after
4044 spin_lock(&ss
->id_lock
);
4045 tmp
= idr_get_next(&ss
->idr
, &tmpid
);
4046 spin_unlock(&ss
->id_lock
);
4050 if (tmp
->depth
>= depth
&& tmp
->stack
[depth
] == rootid
) {
4051 ret
= rcu_dereference(tmp
->css
);
4057 /* continue to scan from next id */
4063 #ifdef CONFIG_CGROUP_DEBUG
4064 static struct cgroup_subsys_state
*debug_create(struct cgroup_subsys
*ss
,
4065 struct cgroup
*cont
)
4067 struct cgroup_subsys_state
*css
= kzalloc(sizeof(*css
), GFP_KERNEL
);
4070 return ERR_PTR(-ENOMEM
);
4075 static void debug_destroy(struct cgroup_subsys
*ss
, struct cgroup
*cont
)
4077 kfree(cont
->subsys
[debug_subsys_id
]);
4080 static u64
cgroup_refcount_read(struct cgroup
*cont
, struct cftype
*cft
)
4082 return atomic_read(&cont
->count
);
4085 static u64
debug_taskcount_read(struct cgroup
*cont
, struct cftype
*cft
)
4087 return cgroup_task_count(cont
);
4090 static u64
current_css_set_read(struct cgroup
*cont
, struct cftype
*cft
)
4092 return (u64
)(unsigned long)current
->cgroups
;
4095 static u64
current_css_set_refcount_read(struct cgroup
*cont
,
4101 count
= atomic_read(¤t
->cgroups
->refcount
);
4106 static int current_css_set_cg_links_read(struct cgroup
*cont
,
4108 struct seq_file
*seq
)
4110 struct cg_cgroup_link
*link
;
4113 read_lock(&css_set_lock
);
4115 cg
= rcu_dereference(current
->cgroups
);
4116 list_for_each_entry(link
, &cg
->cg_links
, cg_link_list
) {
4117 struct cgroup
*c
= link
->cgrp
;
4121 name
= c
->dentry
->d_name
.name
;
4124 seq_printf(seq
, "Root %d group %s\n",
4125 c
->root
->hierarchy_id
, name
);
4128 read_unlock(&css_set_lock
);
4132 #define MAX_TASKS_SHOWN_PER_CSS 25
4133 static int cgroup_css_links_read(struct cgroup
*cont
,
4135 struct seq_file
*seq
)
4137 struct cg_cgroup_link
*link
;
4139 read_lock(&css_set_lock
);
4140 list_for_each_entry(link
, &cont
->css_sets
, cgrp_link_list
) {
4141 struct css_set
*cg
= link
->cg
;
4142 struct task_struct
*task
;
4144 seq_printf(seq
, "css_set %p\n", cg
);
4145 list_for_each_entry(task
, &cg
->tasks
, cg_list
) {
4146 if (count
++ > MAX_TASKS_SHOWN_PER_CSS
) {
4147 seq_puts(seq
, " ...\n");
4150 seq_printf(seq
, " task %d\n",
4151 task_pid_vnr(task
));
4155 read_unlock(&css_set_lock
);
4159 static u64
releasable_read(struct cgroup
*cgrp
, struct cftype
*cft
)
4161 return test_bit(CGRP_RELEASABLE
, &cgrp
->flags
);
4164 static struct cftype debug_files
[] = {
4166 .name
= "cgroup_refcount",
4167 .read_u64
= cgroup_refcount_read
,
4170 .name
= "taskcount",
4171 .read_u64
= debug_taskcount_read
,
4175 .name
= "current_css_set",
4176 .read_u64
= current_css_set_read
,
4180 .name
= "current_css_set_refcount",
4181 .read_u64
= current_css_set_refcount_read
,
4185 .name
= "current_css_set_cg_links",
4186 .read_seq_string
= current_css_set_cg_links_read
,
4190 .name
= "cgroup_css_links",
4191 .read_seq_string
= cgroup_css_links_read
,
4195 .name
= "releasable",
4196 .read_u64
= releasable_read
,
4200 static int debug_populate(struct cgroup_subsys
*ss
, struct cgroup
*cont
)
4202 return cgroup_add_files(cont
, ss
, debug_files
,
4203 ARRAY_SIZE(debug_files
));
4206 struct cgroup_subsys debug_subsys
= {
4208 .create
= debug_create
,
4209 .destroy
= debug_destroy
,
4210 .populate
= debug_populate
,
4211 .subsys_id
= debug_subsys_id
,
4213 #endif /* CONFIG_CGROUP_DEBUG */