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>
53 #include <asm/atomic.h>
55 static DEFINE_MUTEX(cgroup_mutex
);
57 /* Generate an array of cgroup subsystem pointers */
58 #define SUBSYS(_x) &_x ## _subsys,
60 static struct cgroup_subsys
*subsys
[] = {
61 #include <linux/cgroup_subsys.h>
64 #define MAX_CGROUP_ROOT_NAMELEN 64
67 * A cgroupfs_root represents the root of a cgroup hierarchy,
68 * and may be associated with a superblock to form an active
71 struct cgroupfs_root
{
72 struct super_block
*sb
;
75 * The bitmask of subsystems intended to be attached to this
78 unsigned long subsys_bits
;
80 /* The bitmask of subsystems currently attached to this hierarchy */
81 unsigned long actual_subsys_bits
;
83 /* A list running through the attached subsystems */
84 struct list_head subsys_list
;
86 /* The root cgroup for this hierarchy */
87 struct cgroup top_cgroup
;
89 /* Tracks how many cgroups are currently defined in hierarchy.*/
90 int number_of_cgroups
;
92 /* A list running through the active hierarchies */
93 struct list_head root_list
;
95 /* Hierarchy-specific flags */
98 /* The path to use for release notifications. */
99 char release_agent_path
[PATH_MAX
];
101 /* The name for this hierarchy - may be empty */
102 char name
[MAX_CGROUP_ROOT_NAMELEN
];
106 * The "rootnode" hierarchy is the "dummy hierarchy", reserved for the
107 * subsystems that are otherwise unattached - it never has more than a
108 * single cgroup, and all tasks are part of that cgroup.
110 static struct cgroupfs_root rootnode
;
113 * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when
114 * cgroup_subsys->use_id != 0.
116 #define CSS_ID_MAX (65535)
119 * The css to which this ID points. This pointer is set to valid value
120 * after cgroup is populated. If cgroup is removed, this will be NULL.
121 * This pointer is expected to be RCU-safe because destroy()
122 * is called after synchronize_rcu(). But for safe use, css_is_removed()
123 * css_tryget() should be used for avoiding race.
125 struct cgroup_subsys_state
*css
;
131 * Depth in hierarchy which this ID belongs to.
133 unsigned short depth
;
135 * ID is freed by RCU. (and lookup routine is RCU safe.)
137 struct rcu_head rcu_head
;
139 * Hierarchy of CSS ID belongs to.
141 unsigned short stack
[0]; /* Array of Length (depth+1) */
145 /* The list of hierarchy roots */
147 static LIST_HEAD(roots
);
148 static int root_count
;
150 /* dummytop is a shorthand for the dummy hierarchy's top cgroup */
151 #define dummytop (&rootnode.top_cgroup)
153 /* This flag indicates whether tasks in the fork and exit paths should
154 * check for fork/exit handlers to call. This avoids us having to do
155 * extra work in the fork/exit path if none of the subsystems need to
158 static int need_forkexit_callback __read_mostly
;
160 /* convenient tests for these bits */
161 inline int cgroup_is_removed(const struct cgroup
*cgrp
)
163 return test_bit(CGRP_REMOVED
, &cgrp
->flags
);
166 /* bits in struct cgroupfs_root flags field */
168 ROOT_NOPREFIX
, /* mounted subsystems have no named prefix */
171 static int cgroup_is_releasable(const struct cgroup
*cgrp
)
174 (1 << CGRP_RELEASABLE
) |
175 (1 << CGRP_NOTIFY_ON_RELEASE
);
176 return (cgrp
->flags
& bits
) == bits
;
179 static int notify_on_release(const struct cgroup
*cgrp
)
181 return test_bit(CGRP_NOTIFY_ON_RELEASE
, &cgrp
->flags
);
185 * for_each_subsys() allows you to iterate on each subsystem attached to
186 * an active hierarchy
188 #define for_each_subsys(_root, _ss) \
189 list_for_each_entry(_ss, &_root->subsys_list, sibling)
191 /* for_each_active_root() allows you to iterate across the active hierarchies */
192 #define for_each_active_root(_root) \
193 list_for_each_entry(_root, &roots, root_list)
195 /* the list of cgroups eligible for automatic release. Protected by
196 * release_list_lock */
197 static LIST_HEAD(release_list
);
198 static DEFINE_SPINLOCK(release_list_lock
);
199 static void cgroup_release_agent(struct work_struct
*work
);
200 static DECLARE_WORK(release_agent_work
, cgroup_release_agent
);
201 static void check_for_release(struct cgroup
*cgrp
);
203 /* Link structure for associating css_set objects with cgroups */
204 struct cg_cgroup_link
{
206 * List running through cg_cgroup_links associated with a
207 * cgroup, anchored on cgroup->css_sets
209 struct list_head cgrp_link_list
;
211 * List running through cg_cgroup_links pointing at a
212 * single css_set object, anchored on css_set->cg_links
214 struct list_head cg_link_list
;
218 /* The default css_set - used by init and its children prior to any
219 * hierarchies being mounted. It contains a pointer to the root state
220 * for each subsystem. Also used to anchor the list of css_sets. Not
221 * reference-counted, to improve performance when child cgroups
222 * haven't been created.
225 static struct css_set init_css_set
;
226 static struct cg_cgroup_link init_css_set_link
;
228 static int cgroup_subsys_init_idr(struct cgroup_subsys
*ss
);
230 /* css_set_lock protects the list of css_set objects, and the
231 * chain of tasks off each css_set. Nests outside task->alloc_lock
232 * due to cgroup_iter_start() */
233 static DEFINE_RWLOCK(css_set_lock
);
234 static int css_set_count
;
236 /* hash table for cgroup groups. This improves the performance to
237 * find an existing css_set */
238 #define CSS_SET_HASH_BITS 7
239 #define CSS_SET_TABLE_SIZE (1 << CSS_SET_HASH_BITS)
240 static struct hlist_head css_set_table
[CSS_SET_TABLE_SIZE
];
242 static struct hlist_head
*css_set_hash(struct cgroup_subsys_state
*css
[])
246 unsigned long tmp
= 0UL;
248 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++)
249 tmp
+= (unsigned long)css
[i
];
250 tmp
= (tmp
>> 16) ^ tmp
;
252 index
= hash_long(tmp
, CSS_SET_HASH_BITS
);
254 return &css_set_table
[index
];
257 /* We don't maintain the lists running through each css_set to its
258 * task until after the first call to cgroup_iter_start(). This
259 * reduces the fork()/exit() overhead for people who have cgroups
260 * compiled into their kernel but not actually in use */
261 static int use_task_css_set_links __read_mostly
;
263 /* When we create or destroy a css_set, the operation simply
264 * takes/releases a reference count on all the cgroups referenced
265 * by subsystems in this css_set. This can end up multiple-counting
266 * some cgroups, but that's OK - the ref-count is just a
267 * busy/not-busy indicator; ensuring that we only count each cgroup
268 * once would require taking a global lock to ensure that no
269 * subsystems moved between hierarchies while we were doing so.
271 * Possible TODO: decide at boot time based on the number of
272 * registered subsystems and the number of CPUs or NUMA nodes whether
273 * it's better for performance to ref-count every subsystem, or to
274 * take a global lock and only add one ref count to each hierarchy.
278 * unlink a css_set from the list and free it
280 static void unlink_css_set(struct css_set
*cg
)
282 struct cg_cgroup_link
*link
;
283 struct cg_cgroup_link
*saved_link
;
285 hlist_del(&cg
->hlist
);
288 list_for_each_entry_safe(link
, saved_link
, &cg
->cg_links
,
290 list_del(&link
->cg_link_list
);
291 list_del(&link
->cgrp_link_list
);
296 static void __put_css_set(struct css_set
*cg
, int taskexit
)
300 * Ensure that the refcount doesn't hit zero while any readers
301 * can see it. Similar to atomic_dec_and_lock(), but for an
304 if (atomic_add_unless(&cg
->refcount
, -1, 1))
306 write_lock(&css_set_lock
);
307 if (!atomic_dec_and_test(&cg
->refcount
)) {
308 write_unlock(&css_set_lock
);
312 write_unlock(&css_set_lock
);
315 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
316 struct cgroup
*cgrp
= rcu_dereference(cg
->subsys
[i
]->cgroup
);
317 if (atomic_dec_and_test(&cgrp
->count
) &&
318 notify_on_release(cgrp
)) {
320 set_bit(CGRP_RELEASABLE
, &cgrp
->flags
);
321 check_for_release(cgrp
);
329 * refcounted get/put for css_set objects
331 static inline void get_css_set(struct css_set
*cg
)
333 atomic_inc(&cg
->refcount
);
336 static inline void put_css_set(struct css_set
*cg
)
338 __put_css_set(cg
, 0);
341 static inline void put_css_set_taskexit(struct css_set
*cg
)
343 __put_css_set(cg
, 1);
347 * find_existing_css_set() is a helper for
348 * find_css_set(), and checks to see whether an existing
349 * css_set is suitable.
351 * oldcg: the cgroup group that we're using before the cgroup
354 * cgrp: the cgroup that we're moving into
356 * template: location in which to build the desired set of subsystem
357 * state objects for the new cgroup group
359 static struct css_set
*find_existing_css_set(
360 struct css_set
*oldcg
,
362 struct cgroup_subsys_state
*template[])
365 struct cgroupfs_root
*root
= cgrp
->root
;
366 struct hlist_head
*hhead
;
367 struct hlist_node
*node
;
370 /* Built the set of subsystem state objects that we want to
371 * see in the new css_set */
372 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
373 if (root
->subsys_bits
& (1UL << i
)) {
374 /* Subsystem is in this hierarchy. So we want
375 * the subsystem state from the new
377 template[i
] = cgrp
->subsys
[i
];
379 /* Subsystem is not in this hierarchy, so we
380 * don't want to change the subsystem state */
381 template[i
] = oldcg
->subsys
[i
];
385 hhead
= css_set_hash(template);
386 hlist_for_each_entry(cg
, node
, hhead
, hlist
) {
387 if (!memcmp(template, cg
->subsys
, sizeof(cg
->subsys
))) {
388 /* All subsystems matched */
393 /* No existing cgroup group matched */
397 static void free_cg_links(struct list_head
*tmp
)
399 struct cg_cgroup_link
*link
;
400 struct cg_cgroup_link
*saved_link
;
402 list_for_each_entry_safe(link
, saved_link
, tmp
, cgrp_link_list
) {
403 list_del(&link
->cgrp_link_list
);
409 * allocate_cg_links() allocates "count" cg_cgroup_link structures
410 * and chains them on tmp through their cgrp_link_list fields. Returns 0 on
411 * success or a negative error
413 static int allocate_cg_links(int count
, struct list_head
*tmp
)
415 struct cg_cgroup_link
*link
;
418 for (i
= 0; i
< count
; i
++) {
419 link
= kmalloc(sizeof(*link
), GFP_KERNEL
);
424 list_add(&link
->cgrp_link_list
, tmp
);
430 * link_css_set - a helper function to link a css_set to a cgroup
431 * @tmp_cg_links: cg_cgroup_link objects allocated by allocate_cg_links()
432 * @cg: the css_set to be linked
433 * @cgrp: the destination cgroup
435 static void link_css_set(struct list_head
*tmp_cg_links
,
436 struct css_set
*cg
, struct cgroup
*cgrp
)
438 struct cg_cgroup_link
*link
;
440 BUG_ON(list_empty(tmp_cg_links
));
441 link
= list_first_entry(tmp_cg_links
, struct cg_cgroup_link
,
444 list_move(&link
->cgrp_link_list
, &cgrp
->css_sets
);
445 list_add(&link
->cg_link_list
, &cg
->cg_links
);
449 * find_css_set() takes an existing cgroup group and a
450 * cgroup object, and returns a css_set object that's
451 * equivalent to the old group, but with the given cgroup
452 * substituted into the appropriate hierarchy. Must be called with
455 static struct css_set
*find_css_set(
456 struct css_set
*oldcg
, struct cgroup
*cgrp
)
459 struct cgroup_subsys_state
*template[CGROUP_SUBSYS_COUNT
];
462 struct list_head tmp_cg_links
;
464 struct hlist_head
*hhead
;
466 /* First see if we already have a cgroup group that matches
468 read_lock(&css_set_lock
);
469 res
= find_existing_css_set(oldcg
, cgrp
, template);
472 read_unlock(&css_set_lock
);
477 res
= kmalloc(sizeof(*res
), GFP_KERNEL
);
481 /* Allocate all the cg_cgroup_link objects that we'll need */
482 if (allocate_cg_links(root_count
, &tmp_cg_links
) < 0) {
487 atomic_set(&res
->refcount
, 1);
488 INIT_LIST_HEAD(&res
->cg_links
);
489 INIT_LIST_HEAD(&res
->tasks
);
490 INIT_HLIST_NODE(&res
->hlist
);
492 /* Copy the set of subsystem state objects generated in
493 * find_existing_css_set() */
494 memcpy(res
->subsys
, template, sizeof(res
->subsys
));
496 write_lock(&css_set_lock
);
497 /* Add reference counts and links from the new css_set. */
498 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
499 struct cgroup
*cgrp
= res
->subsys
[i
]->cgroup
;
500 struct cgroup_subsys
*ss
= subsys
[i
];
501 atomic_inc(&cgrp
->count
);
503 * We want to add a link once per cgroup, so we
504 * only do it for the first subsystem in each
507 if (ss
->root
->subsys_list
.next
== &ss
->sibling
)
508 link_css_set(&tmp_cg_links
, res
, cgrp
);
510 if (list_empty(&rootnode
.subsys_list
))
511 link_css_set(&tmp_cg_links
, res
, dummytop
);
513 BUG_ON(!list_empty(&tmp_cg_links
));
517 /* Add this cgroup group to the hash table */
518 hhead
= css_set_hash(res
->subsys
);
519 hlist_add_head(&res
->hlist
, hhead
);
521 write_unlock(&css_set_lock
);
527 * There is one global cgroup mutex. We also require taking
528 * task_lock() when dereferencing a task's cgroup subsys pointers.
529 * See "The task_lock() exception", at the end of this comment.
531 * A task must hold cgroup_mutex to modify cgroups.
533 * Any task can increment and decrement the count field without lock.
534 * So in general, code holding cgroup_mutex can't rely on the count
535 * field not changing. However, if the count goes to zero, then only
536 * cgroup_attach_task() can increment it again. Because a count of zero
537 * means that no tasks are currently attached, therefore there is no
538 * way a task attached to that cgroup can fork (the other way to
539 * increment the count). So code holding cgroup_mutex can safely
540 * assume that if the count is zero, it will stay zero. Similarly, if
541 * a task holds cgroup_mutex on a cgroup with zero count, it
542 * knows that the cgroup won't be removed, as cgroup_rmdir()
545 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
546 * (usually) take cgroup_mutex. These are the two most performance
547 * critical pieces of code here. The exception occurs on cgroup_exit(),
548 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
549 * is taken, and if the cgroup count is zero, a usermode call made
550 * to the release agent with the name of the cgroup (path relative to
551 * the root of cgroup file system) as the argument.
553 * A cgroup can only be deleted if both its 'count' of using tasks
554 * is zero, and its list of 'children' cgroups is empty. Since all
555 * tasks in the system use _some_ cgroup, and since there is always at
556 * least one task in the system (init, pid == 1), therefore, top_cgroup
557 * always has either children cgroups and/or using tasks. So we don't
558 * need a special hack to ensure that top_cgroup cannot be deleted.
560 * The task_lock() exception
562 * The need for this exception arises from the action of
563 * cgroup_attach_task(), which overwrites one tasks cgroup pointer with
564 * another. It does so using cgroup_mutex, however there are
565 * several performance critical places that need to reference
566 * task->cgroup without the expense of grabbing a system global
567 * mutex. Therefore except as noted below, when dereferencing or, as
568 * in cgroup_attach_task(), modifying a task'ss cgroup pointer we use
569 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
570 * the task_struct routinely used for such matters.
572 * P.S. One more locking exception. RCU is used to guard the
573 * update of a tasks cgroup pointer by cgroup_attach_task()
577 * cgroup_lock - lock out any changes to cgroup structures
580 void cgroup_lock(void)
582 mutex_lock(&cgroup_mutex
);
586 * cgroup_unlock - release lock on cgroup changes
588 * Undo the lock taken in a previous cgroup_lock() call.
590 void cgroup_unlock(void)
592 mutex_unlock(&cgroup_mutex
);
596 * A couple of forward declarations required, due to cyclic reference loop:
597 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
598 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
602 static int cgroup_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
);
603 static int cgroup_rmdir(struct inode
*unused_dir
, struct dentry
*dentry
);
604 static int cgroup_populate_dir(struct cgroup
*cgrp
);
605 static const struct inode_operations cgroup_dir_inode_operations
;
606 static struct file_operations proc_cgroupstats_operations
;
608 static struct backing_dev_info cgroup_backing_dev_info
= {
610 .capabilities
= BDI_CAP_NO_ACCT_AND_WRITEBACK
,
613 static int alloc_css_id(struct cgroup_subsys
*ss
,
614 struct cgroup
*parent
, struct cgroup
*child
);
616 static struct inode
*cgroup_new_inode(mode_t mode
, struct super_block
*sb
)
618 struct inode
*inode
= new_inode(sb
);
621 inode
->i_mode
= mode
;
622 inode
->i_uid
= current_fsuid();
623 inode
->i_gid
= current_fsgid();
624 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
625 inode
->i_mapping
->backing_dev_info
= &cgroup_backing_dev_info
;
631 * Call subsys's pre_destroy handler.
632 * This is called before css refcnt check.
634 static int cgroup_call_pre_destroy(struct cgroup
*cgrp
)
636 struct cgroup_subsys
*ss
;
639 for_each_subsys(cgrp
->root
, ss
)
640 if (ss
->pre_destroy
) {
641 ret
= ss
->pre_destroy(ss
, cgrp
);
648 static void free_cgroup_rcu(struct rcu_head
*obj
)
650 struct cgroup
*cgrp
= container_of(obj
, struct cgroup
, rcu_head
);
655 static void cgroup_diput(struct dentry
*dentry
, struct inode
*inode
)
657 /* is dentry a directory ? if so, kfree() associated cgroup */
658 if (S_ISDIR(inode
->i_mode
)) {
659 struct cgroup
*cgrp
= dentry
->d_fsdata
;
660 struct cgroup_subsys
*ss
;
661 BUG_ON(!(cgroup_is_removed(cgrp
)));
662 /* It's possible for external users to be holding css
663 * reference counts on a cgroup; css_put() needs to
664 * be able to access the cgroup after decrementing
665 * the reference count in order to know if it needs to
666 * queue the cgroup to be handled by the release
670 mutex_lock(&cgroup_mutex
);
672 * Release the subsystem state objects.
674 for_each_subsys(cgrp
->root
, ss
)
675 ss
->destroy(ss
, cgrp
);
677 cgrp
->root
->number_of_cgroups
--;
678 mutex_unlock(&cgroup_mutex
);
681 * Drop the active superblock reference that we took when we
684 deactivate_super(cgrp
->root
->sb
);
686 call_rcu(&cgrp
->rcu_head
, free_cgroup_rcu
);
691 static void remove_dir(struct dentry
*d
)
693 struct dentry
*parent
= dget(d
->d_parent
);
696 simple_rmdir(parent
->d_inode
, d
);
700 static void cgroup_clear_directory(struct dentry
*dentry
)
702 struct list_head
*node
;
704 BUG_ON(!mutex_is_locked(&dentry
->d_inode
->i_mutex
));
705 spin_lock(&dcache_lock
);
706 node
= dentry
->d_subdirs
.next
;
707 while (node
!= &dentry
->d_subdirs
) {
708 struct dentry
*d
= list_entry(node
, struct dentry
, d_u
.d_child
);
711 /* This should never be called on a cgroup
712 * directory with child cgroups */
713 BUG_ON(d
->d_inode
->i_mode
& S_IFDIR
);
715 spin_unlock(&dcache_lock
);
717 simple_unlink(dentry
->d_inode
, d
);
719 spin_lock(&dcache_lock
);
721 node
= dentry
->d_subdirs
.next
;
723 spin_unlock(&dcache_lock
);
727 * NOTE : the dentry must have been dget()'ed
729 static void cgroup_d_remove_dir(struct dentry
*dentry
)
731 cgroup_clear_directory(dentry
);
733 spin_lock(&dcache_lock
);
734 list_del_init(&dentry
->d_u
.d_child
);
735 spin_unlock(&dcache_lock
);
740 * A queue for waiters to do rmdir() cgroup. A tasks will sleep when
741 * cgroup->count == 0 && list_empty(&cgroup->children) && subsys has some
742 * reference to css->refcnt. In general, this refcnt is expected to goes down
745 * CGRP_WAIT_ON_RMDIR flag is set under cgroup's inode->i_mutex;
747 DECLARE_WAIT_QUEUE_HEAD(cgroup_rmdir_waitq
);
749 static void cgroup_wakeup_rmdir_waiter(struct cgroup
*cgrp
)
751 if (unlikely(test_and_clear_bit(CGRP_WAIT_ON_RMDIR
, &cgrp
->flags
)))
752 wake_up_all(&cgroup_rmdir_waitq
);
755 void cgroup_exclude_rmdir(struct cgroup_subsys_state
*css
)
760 void cgroup_release_and_wakeup_rmdir(struct cgroup_subsys_state
*css
)
762 cgroup_wakeup_rmdir_waiter(css
->cgroup
);
767 static int rebind_subsystems(struct cgroupfs_root
*root
,
768 unsigned long final_bits
)
770 unsigned long added_bits
, removed_bits
;
771 struct cgroup
*cgrp
= &root
->top_cgroup
;
774 removed_bits
= root
->actual_subsys_bits
& ~final_bits
;
775 added_bits
= final_bits
& ~root
->actual_subsys_bits
;
776 /* Check that any added subsystems are currently free */
777 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
778 unsigned long bit
= 1UL << i
;
779 struct cgroup_subsys
*ss
= subsys
[i
];
780 if (!(bit
& added_bits
))
782 if (ss
->root
!= &rootnode
) {
783 /* Subsystem isn't free */
788 /* Currently we don't handle adding/removing subsystems when
789 * any child cgroups exist. This is theoretically supportable
790 * but involves complex error handling, so it's being left until
792 if (root
->number_of_cgroups
> 1)
795 /* Process each subsystem */
796 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
797 struct cgroup_subsys
*ss
= subsys
[i
];
798 unsigned long bit
= 1UL << i
;
799 if (bit
& added_bits
) {
800 /* We're binding this subsystem to this hierarchy */
801 BUG_ON(cgrp
->subsys
[i
]);
802 BUG_ON(!dummytop
->subsys
[i
]);
803 BUG_ON(dummytop
->subsys
[i
]->cgroup
!= dummytop
);
804 mutex_lock(&ss
->hierarchy_mutex
);
805 cgrp
->subsys
[i
] = dummytop
->subsys
[i
];
806 cgrp
->subsys
[i
]->cgroup
= cgrp
;
807 list_move(&ss
->sibling
, &root
->subsys_list
);
811 mutex_unlock(&ss
->hierarchy_mutex
);
812 } else if (bit
& removed_bits
) {
813 /* We're removing this subsystem */
814 BUG_ON(cgrp
->subsys
[i
] != dummytop
->subsys
[i
]);
815 BUG_ON(cgrp
->subsys
[i
]->cgroup
!= cgrp
);
816 mutex_lock(&ss
->hierarchy_mutex
);
818 ss
->bind(ss
, dummytop
);
819 dummytop
->subsys
[i
]->cgroup
= dummytop
;
820 cgrp
->subsys
[i
] = NULL
;
821 subsys
[i
]->root
= &rootnode
;
822 list_move(&ss
->sibling
, &rootnode
.subsys_list
);
823 mutex_unlock(&ss
->hierarchy_mutex
);
824 } else if (bit
& final_bits
) {
825 /* Subsystem state should already exist */
826 BUG_ON(!cgrp
->subsys
[i
]);
828 /* Subsystem state shouldn't exist */
829 BUG_ON(cgrp
->subsys
[i
]);
832 root
->subsys_bits
= root
->actual_subsys_bits
= final_bits
;
838 static int cgroup_show_options(struct seq_file
*seq
, struct vfsmount
*vfs
)
840 struct cgroupfs_root
*root
= vfs
->mnt_sb
->s_fs_info
;
841 struct cgroup_subsys
*ss
;
843 mutex_lock(&cgroup_mutex
);
844 for_each_subsys(root
, ss
)
845 seq_printf(seq
, ",%s", ss
->name
);
846 if (test_bit(ROOT_NOPREFIX
, &root
->flags
))
847 seq_puts(seq
, ",noprefix");
848 if (strlen(root
->release_agent_path
))
849 seq_printf(seq
, ",release_agent=%s", root
->release_agent_path
);
850 if (strlen(root
->name
))
851 seq_printf(seq
, ",name=%s", root
->name
);
852 mutex_unlock(&cgroup_mutex
);
856 struct cgroup_sb_opts
{
857 unsigned long subsys_bits
;
862 struct cgroupfs_root
*new_root
;
865 /* Convert a hierarchy specifier into a bitmask of subsystems and
867 static int parse_cgroupfs_options(char *data
,
868 struct cgroup_sb_opts
*opts
)
870 char *token
, *o
= data
?: "all";
871 unsigned long mask
= (unsigned long)-1;
873 #ifdef CONFIG_CPUSETS
874 mask
= ~(1UL << cpuset_subsys_id
);
877 memset(opts
, 0, sizeof(*opts
));
879 while ((token
= strsep(&o
, ",")) != NULL
) {
882 if (!strcmp(token
, "all")) {
883 /* Add all non-disabled subsystems */
885 opts
->subsys_bits
= 0;
886 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
887 struct cgroup_subsys
*ss
= subsys
[i
];
889 opts
->subsys_bits
|= 1ul << i
;
891 } else if (!strcmp(token
, "noprefix")) {
892 set_bit(ROOT_NOPREFIX
, &opts
->flags
);
893 } else if (!strncmp(token
, "release_agent=", 14)) {
894 /* Specifying two release agents is forbidden */
895 if (opts
->release_agent
)
897 opts
->release_agent
=
898 kstrndup(token
+ 14, PATH_MAX
, GFP_KERNEL
);
899 if (!opts
->release_agent
)
901 } else if (!strncmp(token
, "name=", 5)) {
903 const char *name
= token
+ 5;
904 /* Can't specify an empty name */
907 /* Must match [\w.-]+ */
908 for (i
= 0; i
< strlen(name
); i
++) {
912 if ((c
== '.') || (c
== '-') || (c
== '_'))
916 /* Specifying two names is forbidden */
919 opts
->name
= kstrndup(name
,
920 MAX_CGROUP_ROOT_NAMELEN
,
925 struct cgroup_subsys
*ss
;
927 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
929 if (!strcmp(token
, ss
->name
)) {
931 set_bit(i
, &opts
->subsys_bits
);
935 if (i
== CGROUP_SUBSYS_COUNT
)
941 * Option noprefix was introduced just for backward compatibility
942 * with the old cpuset, so we allow noprefix only if mounting just
943 * the cpuset subsystem.
945 if (test_bit(ROOT_NOPREFIX
, &opts
->flags
) &&
946 (opts
->subsys_bits
& mask
))
949 /* We can't have an empty hierarchy */
950 if (!opts
->subsys_bits
&& !opts
->name
)
956 static int cgroup_remount(struct super_block
*sb
, int *flags
, char *data
)
959 struct cgroupfs_root
*root
= sb
->s_fs_info
;
960 struct cgroup
*cgrp
= &root
->top_cgroup
;
961 struct cgroup_sb_opts opts
;
964 mutex_lock(&cgrp
->dentry
->d_inode
->i_mutex
);
965 mutex_lock(&cgroup_mutex
);
967 /* See what subsystems are wanted */
968 ret
= parse_cgroupfs_options(data
, &opts
);
972 /* Don't allow flags to change at remount */
973 if (opts
.flags
!= root
->flags
) {
978 /* Don't allow name to change at remount */
979 if (opts
.name
&& strcmp(opts
.name
, root
->name
)) {
984 ret
= rebind_subsystems(root
, opts
.subsys_bits
);
988 /* (re)populate subsystem files */
989 cgroup_populate_dir(cgrp
);
991 if (opts
.release_agent
)
992 strcpy(root
->release_agent_path
, opts
.release_agent
);
994 kfree(opts
.release_agent
);
996 mutex_unlock(&cgroup_mutex
);
997 mutex_unlock(&cgrp
->dentry
->d_inode
->i_mutex
);
1002 static const struct super_operations cgroup_ops
= {
1003 .statfs
= simple_statfs
,
1004 .drop_inode
= generic_delete_inode
,
1005 .show_options
= cgroup_show_options
,
1006 .remount_fs
= cgroup_remount
,
1009 static void init_cgroup_housekeeping(struct cgroup
*cgrp
)
1011 INIT_LIST_HEAD(&cgrp
->sibling
);
1012 INIT_LIST_HEAD(&cgrp
->children
);
1013 INIT_LIST_HEAD(&cgrp
->css_sets
);
1014 INIT_LIST_HEAD(&cgrp
->release_list
);
1015 INIT_LIST_HEAD(&cgrp
->pids_list
);
1016 init_rwsem(&cgrp
->pids_mutex
);
1019 static void init_cgroup_root(struct cgroupfs_root
*root
)
1021 struct cgroup
*cgrp
= &root
->top_cgroup
;
1022 INIT_LIST_HEAD(&root
->subsys_list
);
1023 INIT_LIST_HEAD(&root
->root_list
);
1024 root
->number_of_cgroups
= 1;
1026 cgrp
->top_cgroup
= cgrp
;
1027 init_cgroup_housekeeping(cgrp
);
1030 static int cgroup_test_super(struct super_block
*sb
, void *data
)
1032 struct cgroup_sb_opts
*opts
= data
;
1033 struct cgroupfs_root
*root
= sb
->s_fs_info
;
1035 /* If we asked for a name then it must match */
1036 if (opts
->name
&& strcmp(opts
->name
, root
->name
))
1039 /* If we asked for subsystems then they must match */
1040 if (opts
->subsys_bits
&& (opts
->subsys_bits
!= root
->subsys_bits
))
1046 static struct cgroupfs_root
*cgroup_root_from_opts(struct cgroup_sb_opts
*opts
)
1048 struct cgroupfs_root
*root
;
1050 /* Empty hierarchies aren't supported */
1051 if (!opts
->subsys_bits
)
1054 root
= kzalloc(sizeof(*root
), GFP_KERNEL
);
1056 return ERR_PTR(-ENOMEM
);
1058 init_cgroup_root(root
);
1059 root
->subsys_bits
= opts
->subsys_bits
;
1060 root
->flags
= opts
->flags
;
1061 if (opts
->release_agent
)
1062 strcpy(root
->release_agent_path
, opts
->release_agent
);
1064 strcpy(root
->name
, opts
->name
);
1068 static int cgroup_set_super(struct super_block
*sb
, void *data
)
1071 struct cgroup_sb_opts
*opts
= data
;
1073 /* If we don't have a new root, we can't set up a new sb */
1074 if (!opts
->new_root
)
1077 BUG_ON(!opts
->subsys_bits
);
1079 ret
= set_anon_super(sb
, NULL
);
1083 sb
->s_fs_info
= opts
->new_root
;
1084 opts
->new_root
->sb
= sb
;
1086 sb
->s_blocksize
= PAGE_CACHE_SIZE
;
1087 sb
->s_blocksize_bits
= PAGE_CACHE_SHIFT
;
1088 sb
->s_magic
= CGROUP_SUPER_MAGIC
;
1089 sb
->s_op
= &cgroup_ops
;
1094 static int cgroup_get_rootdir(struct super_block
*sb
)
1096 struct inode
*inode
=
1097 cgroup_new_inode(S_IFDIR
| S_IRUGO
| S_IXUGO
| S_IWUSR
, sb
);
1098 struct dentry
*dentry
;
1103 inode
->i_fop
= &simple_dir_operations
;
1104 inode
->i_op
= &cgroup_dir_inode_operations
;
1105 /* directories start off with i_nlink == 2 (for "." entry) */
1107 dentry
= d_alloc_root(inode
);
1112 sb
->s_root
= dentry
;
1116 static int cgroup_get_sb(struct file_system_type
*fs_type
,
1117 int flags
, const char *unused_dev_name
,
1118 void *data
, struct vfsmount
*mnt
)
1120 struct cgroup_sb_opts opts
;
1121 struct cgroupfs_root
*root
;
1123 struct super_block
*sb
;
1124 struct cgroupfs_root
*new_root
;
1126 /* First find the desired set of subsystems */
1127 ret
= parse_cgroupfs_options(data
, &opts
);
1132 * Allocate a new cgroup root. We may not need it if we're
1133 * reusing an existing hierarchy.
1135 new_root
= cgroup_root_from_opts(&opts
);
1136 if (IS_ERR(new_root
)) {
1137 ret
= PTR_ERR(new_root
);
1140 opts
.new_root
= new_root
;
1142 /* Locate an existing or new sb for this hierarchy */
1143 sb
= sget(fs_type
, cgroup_test_super
, cgroup_set_super
, &opts
);
1146 kfree(opts
.new_root
);
1150 root
= sb
->s_fs_info
;
1152 if (root
== opts
.new_root
) {
1153 /* We used the new root structure, so this is a new hierarchy */
1154 struct list_head tmp_cg_links
;
1155 struct cgroup
*root_cgrp
= &root
->top_cgroup
;
1156 struct inode
*inode
;
1157 struct cgroupfs_root
*existing_root
;
1160 BUG_ON(sb
->s_root
!= NULL
);
1162 ret
= cgroup_get_rootdir(sb
);
1164 goto drop_new_super
;
1165 inode
= sb
->s_root
->d_inode
;
1167 mutex_lock(&inode
->i_mutex
);
1168 mutex_lock(&cgroup_mutex
);
1170 if (strlen(root
->name
)) {
1171 /* Check for name clashes with existing mounts */
1172 for_each_active_root(existing_root
) {
1173 if (!strcmp(existing_root
->name
, root
->name
)) {
1175 mutex_unlock(&cgroup_mutex
);
1176 mutex_unlock(&inode
->i_mutex
);
1177 goto drop_new_super
;
1183 * We're accessing css_set_count without locking
1184 * css_set_lock here, but that's OK - it can only be
1185 * increased by someone holding cgroup_lock, and
1186 * that's us. The worst that can happen is that we
1187 * have some link structures left over
1189 ret
= allocate_cg_links(css_set_count
, &tmp_cg_links
);
1191 mutex_unlock(&cgroup_mutex
);
1192 mutex_unlock(&inode
->i_mutex
);
1193 goto drop_new_super
;
1196 ret
= rebind_subsystems(root
, root
->subsys_bits
);
1197 if (ret
== -EBUSY
) {
1198 mutex_unlock(&cgroup_mutex
);
1199 mutex_unlock(&inode
->i_mutex
);
1200 free_cg_links(&tmp_cg_links
);
1201 goto drop_new_super
;
1204 /* EBUSY should be the only error here */
1207 list_add(&root
->root_list
, &roots
);
1210 sb
->s_root
->d_fsdata
= root_cgrp
;
1211 root
->top_cgroup
.dentry
= sb
->s_root
;
1213 /* Link the top cgroup in this hierarchy into all
1214 * the css_set objects */
1215 write_lock(&css_set_lock
);
1216 for (i
= 0; i
< CSS_SET_TABLE_SIZE
; i
++) {
1217 struct hlist_head
*hhead
= &css_set_table
[i
];
1218 struct hlist_node
*node
;
1221 hlist_for_each_entry(cg
, node
, hhead
, hlist
)
1222 link_css_set(&tmp_cg_links
, cg
, root_cgrp
);
1224 write_unlock(&css_set_lock
);
1226 free_cg_links(&tmp_cg_links
);
1228 BUG_ON(!list_empty(&root_cgrp
->sibling
));
1229 BUG_ON(!list_empty(&root_cgrp
->children
));
1230 BUG_ON(root
->number_of_cgroups
!= 1);
1232 cgroup_populate_dir(root_cgrp
);
1233 mutex_unlock(&cgroup_mutex
);
1234 mutex_unlock(&inode
->i_mutex
);
1237 * We re-used an existing hierarchy - the new root (if
1238 * any) is not needed
1240 kfree(opts
.new_root
);
1243 simple_set_mnt(mnt
, sb
);
1244 kfree(opts
.release_agent
);
1249 deactivate_locked_super(sb
);
1251 kfree(opts
.release_agent
);
1257 static void cgroup_kill_sb(struct super_block
*sb
) {
1258 struct cgroupfs_root
*root
= sb
->s_fs_info
;
1259 struct cgroup
*cgrp
= &root
->top_cgroup
;
1261 struct cg_cgroup_link
*link
;
1262 struct cg_cgroup_link
*saved_link
;
1266 BUG_ON(root
->number_of_cgroups
!= 1);
1267 BUG_ON(!list_empty(&cgrp
->children
));
1268 BUG_ON(!list_empty(&cgrp
->sibling
));
1270 mutex_lock(&cgroup_mutex
);
1272 /* Rebind all subsystems back to the default hierarchy */
1273 ret
= rebind_subsystems(root
, 0);
1274 /* Shouldn't be able to fail ... */
1278 * Release all the links from css_sets to this hierarchy's
1281 write_lock(&css_set_lock
);
1283 list_for_each_entry_safe(link
, saved_link
, &cgrp
->css_sets
,
1285 list_del(&link
->cg_link_list
);
1286 list_del(&link
->cgrp_link_list
);
1289 write_unlock(&css_set_lock
);
1291 if (!list_empty(&root
->root_list
)) {
1292 list_del(&root
->root_list
);
1296 mutex_unlock(&cgroup_mutex
);
1298 kill_litter_super(sb
);
1302 static struct file_system_type cgroup_fs_type
= {
1304 .get_sb
= cgroup_get_sb
,
1305 .kill_sb
= cgroup_kill_sb
,
1308 static inline struct cgroup
*__d_cgrp(struct dentry
*dentry
)
1310 return dentry
->d_fsdata
;
1313 static inline struct cftype
*__d_cft(struct dentry
*dentry
)
1315 return dentry
->d_fsdata
;
1319 * cgroup_path - generate the path of a cgroup
1320 * @cgrp: the cgroup in question
1321 * @buf: the buffer to write the path into
1322 * @buflen: the length of the buffer
1324 * Called with cgroup_mutex held or else with an RCU-protected cgroup
1325 * reference. Writes path of cgroup into buf. Returns 0 on success,
1328 int cgroup_path(const struct cgroup
*cgrp
, char *buf
, int buflen
)
1331 struct dentry
*dentry
= rcu_dereference(cgrp
->dentry
);
1333 if (!dentry
|| cgrp
== dummytop
) {
1335 * Inactive subsystems have no dentry for their root
1342 start
= buf
+ buflen
;
1346 int len
= dentry
->d_name
.len
;
1347 if ((start
-= len
) < buf
)
1348 return -ENAMETOOLONG
;
1349 memcpy(start
, cgrp
->dentry
->d_name
.name
, len
);
1350 cgrp
= cgrp
->parent
;
1353 dentry
= rcu_dereference(cgrp
->dentry
);
1357 return -ENAMETOOLONG
;
1360 memmove(buf
, start
, buf
+ buflen
- start
);
1365 * Return the first subsystem attached to a cgroup's hierarchy, and
1369 static void get_first_subsys(const struct cgroup
*cgrp
,
1370 struct cgroup_subsys_state
**css
, int *subsys_id
)
1372 const struct cgroupfs_root
*root
= cgrp
->root
;
1373 const struct cgroup_subsys
*test_ss
;
1374 BUG_ON(list_empty(&root
->subsys_list
));
1375 test_ss
= list_entry(root
->subsys_list
.next
,
1376 struct cgroup_subsys
, sibling
);
1378 *css
= cgrp
->subsys
[test_ss
->subsys_id
];
1382 *subsys_id
= test_ss
->subsys_id
;
1386 * cgroup_attach_task - attach task 'tsk' to cgroup 'cgrp'
1387 * @cgrp: the cgroup the task is attaching to
1388 * @tsk: the task to be attached
1390 * Call holding cgroup_mutex. May take task_lock of
1391 * the task 'tsk' during call.
1393 int cgroup_attach_task(struct cgroup
*cgrp
, struct task_struct
*tsk
)
1396 struct cgroup_subsys
*ss
;
1397 struct cgroup
*oldcgrp
;
1399 struct css_set
*newcg
;
1400 struct cgroupfs_root
*root
= cgrp
->root
;
1403 get_first_subsys(cgrp
, NULL
, &subsys_id
);
1405 /* Nothing to do if the task is already in that cgroup */
1406 oldcgrp
= task_cgroup(tsk
, subsys_id
);
1407 if (cgrp
== oldcgrp
)
1410 for_each_subsys(root
, ss
) {
1411 if (ss
->can_attach
) {
1412 retval
= ss
->can_attach(ss
, cgrp
, tsk
);
1423 * Locate or allocate a new css_set for this task,
1424 * based on its final set of cgroups
1426 newcg
= find_css_set(cg
, cgrp
);
1432 if (tsk
->flags
& PF_EXITING
) {
1437 rcu_assign_pointer(tsk
->cgroups
, newcg
);
1440 /* Update the css_set linked lists if we're using them */
1441 write_lock(&css_set_lock
);
1442 if (!list_empty(&tsk
->cg_list
)) {
1443 list_del(&tsk
->cg_list
);
1444 list_add(&tsk
->cg_list
, &newcg
->tasks
);
1446 write_unlock(&css_set_lock
);
1448 for_each_subsys(root
, ss
) {
1450 ss
->attach(ss
, cgrp
, oldcgrp
, tsk
);
1452 set_bit(CGRP_RELEASABLE
, &oldcgrp
->flags
);
1457 * wake up rmdir() waiter. the rmdir should fail since the cgroup
1458 * is no longer empty.
1460 cgroup_wakeup_rmdir_waiter(cgrp
);
1465 * Attach task with pid 'pid' to cgroup 'cgrp'. Call with cgroup_mutex
1466 * held. May take task_lock of task
1468 static int attach_task_by_pid(struct cgroup
*cgrp
, u64 pid
)
1470 struct task_struct
*tsk
;
1471 const struct cred
*cred
= current_cred(), *tcred
;
1476 tsk
= find_task_by_vpid(pid
);
1477 if (!tsk
|| tsk
->flags
& PF_EXITING
) {
1482 tcred
= __task_cred(tsk
);
1484 cred
->euid
!= tcred
->uid
&&
1485 cred
->euid
!= tcred
->suid
) {
1489 get_task_struct(tsk
);
1493 get_task_struct(tsk
);
1496 ret
= cgroup_attach_task(cgrp
, tsk
);
1497 put_task_struct(tsk
);
1501 static int cgroup_tasks_write(struct cgroup
*cgrp
, struct cftype
*cft
, u64 pid
)
1504 if (!cgroup_lock_live_group(cgrp
))
1506 ret
= attach_task_by_pid(cgrp
, pid
);
1511 /* The various types of files and directories in a cgroup file system */
1512 enum cgroup_filetype
{
1516 FILE_NOTIFY_ON_RELEASE
,
1521 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
1522 * @cgrp: the cgroup to be checked for liveness
1524 * On success, returns true; the lock should be later released with
1525 * cgroup_unlock(). On failure returns false with no lock held.
1527 bool cgroup_lock_live_group(struct cgroup
*cgrp
)
1529 mutex_lock(&cgroup_mutex
);
1530 if (cgroup_is_removed(cgrp
)) {
1531 mutex_unlock(&cgroup_mutex
);
1537 static int cgroup_release_agent_write(struct cgroup
*cgrp
, struct cftype
*cft
,
1540 BUILD_BUG_ON(sizeof(cgrp
->root
->release_agent_path
) < PATH_MAX
);
1541 if (!cgroup_lock_live_group(cgrp
))
1543 strcpy(cgrp
->root
->release_agent_path
, buffer
);
1548 static int cgroup_release_agent_show(struct cgroup
*cgrp
, struct cftype
*cft
,
1549 struct seq_file
*seq
)
1551 if (!cgroup_lock_live_group(cgrp
))
1553 seq_puts(seq
, cgrp
->root
->release_agent_path
);
1554 seq_putc(seq
, '\n');
1559 /* A buffer size big enough for numbers or short strings */
1560 #define CGROUP_LOCAL_BUFFER_SIZE 64
1562 static ssize_t
cgroup_write_X64(struct cgroup
*cgrp
, struct cftype
*cft
,
1564 const char __user
*userbuf
,
1565 size_t nbytes
, loff_t
*unused_ppos
)
1567 char buffer
[CGROUP_LOCAL_BUFFER_SIZE
];
1573 if (nbytes
>= sizeof(buffer
))
1575 if (copy_from_user(buffer
, userbuf
, nbytes
))
1578 buffer
[nbytes
] = 0; /* nul-terminate */
1580 if (cft
->write_u64
) {
1581 u64 val
= simple_strtoull(buffer
, &end
, 0);
1584 retval
= cft
->write_u64(cgrp
, cft
, val
);
1586 s64 val
= simple_strtoll(buffer
, &end
, 0);
1589 retval
= cft
->write_s64(cgrp
, cft
, val
);
1596 static ssize_t
cgroup_write_string(struct cgroup
*cgrp
, struct cftype
*cft
,
1598 const char __user
*userbuf
,
1599 size_t nbytes
, loff_t
*unused_ppos
)
1601 char local_buffer
[CGROUP_LOCAL_BUFFER_SIZE
];
1603 size_t max_bytes
= cft
->max_write_len
;
1604 char *buffer
= local_buffer
;
1607 max_bytes
= sizeof(local_buffer
) - 1;
1608 if (nbytes
>= max_bytes
)
1610 /* Allocate a dynamic buffer if we need one */
1611 if (nbytes
>= sizeof(local_buffer
)) {
1612 buffer
= kmalloc(nbytes
+ 1, GFP_KERNEL
);
1616 if (nbytes
&& copy_from_user(buffer
, userbuf
, nbytes
)) {
1621 buffer
[nbytes
] = 0; /* nul-terminate */
1623 retval
= cft
->write_string(cgrp
, cft
, buffer
);
1627 if (buffer
!= local_buffer
)
1632 static ssize_t
cgroup_file_write(struct file
*file
, const char __user
*buf
,
1633 size_t nbytes
, loff_t
*ppos
)
1635 struct cftype
*cft
= __d_cft(file
->f_dentry
);
1636 struct cgroup
*cgrp
= __d_cgrp(file
->f_dentry
->d_parent
);
1638 if (cgroup_is_removed(cgrp
))
1641 return cft
->write(cgrp
, cft
, file
, buf
, nbytes
, ppos
);
1642 if (cft
->write_u64
|| cft
->write_s64
)
1643 return cgroup_write_X64(cgrp
, cft
, file
, buf
, nbytes
, ppos
);
1644 if (cft
->write_string
)
1645 return cgroup_write_string(cgrp
, cft
, file
, buf
, nbytes
, ppos
);
1647 int ret
= cft
->trigger(cgrp
, (unsigned int)cft
->private);
1648 return ret
? ret
: nbytes
;
1653 static ssize_t
cgroup_read_u64(struct cgroup
*cgrp
, struct cftype
*cft
,
1655 char __user
*buf
, size_t nbytes
,
1658 char tmp
[CGROUP_LOCAL_BUFFER_SIZE
];
1659 u64 val
= cft
->read_u64(cgrp
, cft
);
1660 int len
= sprintf(tmp
, "%llu\n", (unsigned long long) val
);
1662 return simple_read_from_buffer(buf
, nbytes
, ppos
, tmp
, len
);
1665 static ssize_t
cgroup_read_s64(struct cgroup
*cgrp
, struct cftype
*cft
,
1667 char __user
*buf
, size_t nbytes
,
1670 char tmp
[CGROUP_LOCAL_BUFFER_SIZE
];
1671 s64 val
= cft
->read_s64(cgrp
, cft
);
1672 int len
= sprintf(tmp
, "%lld\n", (long long) val
);
1674 return simple_read_from_buffer(buf
, nbytes
, ppos
, tmp
, len
);
1677 static ssize_t
cgroup_file_read(struct file
*file
, char __user
*buf
,
1678 size_t nbytes
, loff_t
*ppos
)
1680 struct cftype
*cft
= __d_cft(file
->f_dentry
);
1681 struct cgroup
*cgrp
= __d_cgrp(file
->f_dentry
->d_parent
);
1683 if (cgroup_is_removed(cgrp
))
1687 return cft
->read(cgrp
, cft
, file
, buf
, nbytes
, ppos
);
1689 return cgroup_read_u64(cgrp
, cft
, file
, buf
, nbytes
, ppos
);
1691 return cgroup_read_s64(cgrp
, cft
, file
, buf
, nbytes
, ppos
);
1696 * seqfile ops/methods for returning structured data. Currently just
1697 * supports string->u64 maps, but can be extended in future.
1700 struct cgroup_seqfile_state
{
1702 struct cgroup
*cgroup
;
1705 static int cgroup_map_add(struct cgroup_map_cb
*cb
, const char *key
, u64 value
)
1707 struct seq_file
*sf
= cb
->state
;
1708 return seq_printf(sf
, "%s %llu\n", key
, (unsigned long long)value
);
1711 static int cgroup_seqfile_show(struct seq_file
*m
, void *arg
)
1713 struct cgroup_seqfile_state
*state
= m
->private;
1714 struct cftype
*cft
= state
->cft
;
1715 if (cft
->read_map
) {
1716 struct cgroup_map_cb cb
= {
1717 .fill
= cgroup_map_add
,
1720 return cft
->read_map(state
->cgroup
, cft
, &cb
);
1722 return cft
->read_seq_string(state
->cgroup
, cft
, m
);
1725 static int cgroup_seqfile_release(struct inode
*inode
, struct file
*file
)
1727 struct seq_file
*seq
= file
->private_data
;
1728 kfree(seq
->private);
1729 return single_release(inode
, file
);
1732 static struct file_operations cgroup_seqfile_operations
= {
1734 .write
= cgroup_file_write
,
1735 .llseek
= seq_lseek
,
1736 .release
= cgroup_seqfile_release
,
1739 static int cgroup_file_open(struct inode
*inode
, struct file
*file
)
1744 err
= generic_file_open(inode
, file
);
1747 cft
= __d_cft(file
->f_dentry
);
1749 if (cft
->read_map
|| cft
->read_seq_string
) {
1750 struct cgroup_seqfile_state
*state
=
1751 kzalloc(sizeof(*state
), GFP_USER
);
1755 state
->cgroup
= __d_cgrp(file
->f_dentry
->d_parent
);
1756 file
->f_op
= &cgroup_seqfile_operations
;
1757 err
= single_open(file
, cgroup_seqfile_show
, state
);
1760 } else if (cft
->open
)
1761 err
= cft
->open(inode
, file
);
1768 static int cgroup_file_release(struct inode
*inode
, struct file
*file
)
1770 struct cftype
*cft
= __d_cft(file
->f_dentry
);
1772 return cft
->release(inode
, file
);
1777 * cgroup_rename - Only allow simple rename of directories in place.
1779 static int cgroup_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
1780 struct inode
*new_dir
, struct dentry
*new_dentry
)
1782 if (!S_ISDIR(old_dentry
->d_inode
->i_mode
))
1784 if (new_dentry
->d_inode
)
1786 if (old_dir
!= new_dir
)
1788 return simple_rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
1791 static struct file_operations cgroup_file_operations
= {
1792 .read
= cgroup_file_read
,
1793 .write
= cgroup_file_write
,
1794 .llseek
= generic_file_llseek
,
1795 .open
= cgroup_file_open
,
1796 .release
= cgroup_file_release
,
1799 static const struct inode_operations cgroup_dir_inode_operations
= {
1800 .lookup
= simple_lookup
,
1801 .mkdir
= cgroup_mkdir
,
1802 .rmdir
= cgroup_rmdir
,
1803 .rename
= cgroup_rename
,
1806 static int cgroup_create_file(struct dentry
*dentry
, mode_t mode
,
1807 struct super_block
*sb
)
1809 static const struct dentry_operations cgroup_dops
= {
1810 .d_iput
= cgroup_diput
,
1813 struct inode
*inode
;
1817 if (dentry
->d_inode
)
1820 inode
= cgroup_new_inode(mode
, sb
);
1824 if (S_ISDIR(mode
)) {
1825 inode
->i_op
= &cgroup_dir_inode_operations
;
1826 inode
->i_fop
= &simple_dir_operations
;
1828 /* start off with i_nlink == 2 (for "." entry) */
1831 /* start with the directory inode held, so that we can
1832 * populate it without racing with another mkdir */
1833 mutex_lock_nested(&inode
->i_mutex
, I_MUTEX_CHILD
);
1834 } else if (S_ISREG(mode
)) {
1836 inode
->i_fop
= &cgroup_file_operations
;
1838 dentry
->d_op
= &cgroup_dops
;
1839 d_instantiate(dentry
, inode
);
1840 dget(dentry
); /* Extra count - pin the dentry in core */
1845 * cgroup_create_dir - create a directory for an object.
1846 * @cgrp: the cgroup we create the directory for. It must have a valid
1847 * ->parent field. And we are going to fill its ->dentry field.
1848 * @dentry: dentry of the new cgroup
1849 * @mode: mode to set on new directory.
1851 static int cgroup_create_dir(struct cgroup
*cgrp
, struct dentry
*dentry
,
1854 struct dentry
*parent
;
1857 parent
= cgrp
->parent
->dentry
;
1858 error
= cgroup_create_file(dentry
, S_IFDIR
| mode
, cgrp
->root
->sb
);
1860 dentry
->d_fsdata
= cgrp
;
1861 inc_nlink(parent
->d_inode
);
1862 rcu_assign_pointer(cgrp
->dentry
, dentry
);
1871 * cgroup_file_mode - deduce file mode of a control file
1872 * @cft: the control file in question
1874 * returns cft->mode if ->mode is not 0
1875 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
1876 * returns S_IRUGO if it has only a read handler
1877 * returns S_IWUSR if it has only a write hander
1879 static mode_t
cgroup_file_mode(const struct cftype
*cft
)
1886 if (cft
->read
|| cft
->read_u64
|| cft
->read_s64
||
1887 cft
->read_map
|| cft
->read_seq_string
)
1890 if (cft
->write
|| cft
->write_u64
|| cft
->write_s64
||
1891 cft
->write_string
|| cft
->trigger
)
1897 int cgroup_add_file(struct cgroup
*cgrp
,
1898 struct cgroup_subsys
*subsys
,
1899 const struct cftype
*cft
)
1901 struct dentry
*dir
= cgrp
->dentry
;
1902 struct dentry
*dentry
;
1906 char name
[MAX_CGROUP_TYPE_NAMELEN
+ MAX_CFTYPE_NAME
+ 2] = { 0 };
1907 if (subsys
&& !test_bit(ROOT_NOPREFIX
, &cgrp
->root
->flags
)) {
1908 strcpy(name
, subsys
->name
);
1911 strcat(name
, cft
->name
);
1912 BUG_ON(!mutex_is_locked(&dir
->d_inode
->i_mutex
));
1913 dentry
= lookup_one_len(name
, dir
, strlen(name
));
1914 if (!IS_ERR(dentry
)) {
1915 mode
= cgroup_file_mode(cft
);
1916 error
= cgroup_create_file(dentry
, mode
| S_IFREG
,
1919 dentry
->d_fsdata
= (void *)cft
;
1922 error
= PTR_ERR(dentry
);
1926 int cgroup_add_files(struct cgroup
*cgrp
,
1927 struct cgroup_subsys
*subsys
,
1928 const struct cftype cft
[],
1932 for (i
= 0; i
< count
; i
++) {
1933 err
= cgroup_add_file(cgrp
, subsys
, &cft
[i
]);
1941 * cgroup_task_count - count the number of tasks in a cgroup.
1942 * @cgrp: the cgroup in question
1944 * Return the number of tasks in the cgroup.
1946 int cgroup_task_count(const struct cgroup
*cgrp
)
1949 struct cg_cgroup_link
*link
;
1951 read_lock(&css_set_lock
);
1952 list_for_each_entry(link
, &cgrp
->css_sets
, cgrp_link_list
) {
1953 count
+= atomic_read(&link
->cg
->refcount
);
1955 read_unlock(&css_set_lock
);
1960 * Advance a list_head iterator. The iterator should be positioned at
1961 * the start of a css_set
1963 static void cgroup_advance_iter(struct cgroup
*cgrp
,
1964 struct cgroup_iter
*it
)
1966 struct list_head
*l
= it
->cg_link
;
1967 struct cg_cgroup_link
*link
;
1970 /* Advance to the next non-empty css_set */
1973 if (l
== &cgrp
->css_sets
) {
1977 link
= list_entry(l
, struct cg_cgroup_link
, cgrp_link_list
);
1979 } while (list_empty(&cg
->tasks
));
1981 it
->task
= cg
->tasks
.next
;
1985 * To reduce the fork() overhead for systems that are not actually
1986 * using their cgroups capability, we don't maintain the lists running
1987 * through each css_set to its tasks until we see the list actually
1988 * used - in other words after the first call to cgroup_iter_start().
1990 * The tasklist_lock is not held here, as do_each_thread() and
1991 * while_each_thread() are protected by RCU.
1993 static void cgroup_enable_task_cg_lists(void)
1995 struct task_struct
*p
, *g
;
1996 write_lock(&css_set_lock
);
1997 use_task_css_set_links
= 1;
1998 do_each_thread(g
, p
) {
2001 * We should check if the process is exiting, otherwise
2002 * it will race with cgroup_exit() in that the list
2003 * entry won't be deleted though the process has exited.
2005 if (!(p
->flags
& PF_EXITING
) && list_empty(&p
->cg_list
))
2006 list_add(&p
->cg_list
, &p
->cgroups
->tasks
);
2008 } while_each_thread(g
, p
);
2009 write_unlock(&css_set_lock
);
2012 void cgroup_iter_start(struct cgroup
*cgrp
, struct cgroup_iter
*it
)
2015 * The first time anyone tries to iterate across a cgroup,
2016 * we need to enable the list linking each css_set to its
2017 * tasks, and fix up all existing tasks.
2019 if (!use_task_css_set_links
)
2020 cgroup_enable_task_cg_lists();
2022 read_lock(&css_set_lock
);
2023 it
->cg_link
= &cgrp
->css_sets
;
2024 cgroup_advance_iter(cgrp
, it
);
2027 struct task_struct
*cgroup_iter_next(struct cgroup
*cgrp
,
2028 struct cgroup_iter
*it
)
2030 struct task_struct
*res
;
2031 struct list_head
*l
= it
->task
;
2032 struct cg_cgroup_link
*link
;
2034 /* If the iterator cg is NULL, we have no tasks */
2037 res
= list_entry(l
, struct task_struct
, cg_list
);
2038 /* Advance iterator to find next entry */
2040 link
= list_entry(it
->cg_link
, struct cg_cgroup_link
, cgrp_link_list
);
2041 if (l
== &link
->cg
->tasks
) {
2042 /* We reached the end of this task list - move on to
2043 * the next cg_cgroup_link */
2044 cgroup_advance_iter(cgrp
, it
);
2051 void cgroup_iter_end(struct cgroup
*cgrp
, struct cgroup_iter
*it
)
2053 read_unlock(&css_set_lock
);
2056 static inline int started_after_time(struct task_struct
*t1
,
2057 struct timespec
*time
,
2058 struct task_struct
*t2
)
2060 int start_diff
= timespec_compare(&t1
->start_time
, time
);
2061 if (start_diff
> 0) {
2063 } else if (start_diff
< 0) {
2067 * Arbitrarily, if two processes started at the same
2068 * time, we'll say that the lower pointer value
2069 * started first. Note that t2 may have exited by now
2070 * so this may not be a valid pointer any longer, but
2071 * that's fine - it still serves to distinguish
2072 * between two tasks started (effectively) simultaneously.
2079 * This function is a callback from heap_insert() and is used to order
2081 * In this case we order the heap in descending task start time.
2083 static inline int started_after(void *p1
, void *p2
)
2085 struct task_struct
*t1
= p1
;
2086 struct task_struct
*t2
= p2
;
2087 return started_after_time(t1
, &t2
->start_time
, t2
);
2091 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
2092 * @scan: struct cgroup_scanner containing arguments for the scan
2094 * Arguments include pointers to callback functions test_task() and
2096 * Iterate through all the tasks in a cgroup, calling test_task() for each,
2097 * and if it returns true, call process_task() for it also.
2098 * The test_task pointer may be NULL, meaning always true (select all tasks).
2099 * Effectively duplicates cgroup_iter_{start,next,end}()
2100 * but does not lock css_set_lock for the call to process_task().
2101 * The struct cgroup_scanner may be embedded in any structure of the caller's
2103 * It is guaranteed that process_task() will act on every task that
2104 * is a member of the cgroup for the duration of this call. This
2105 * function may or may not call process_task() for tasks that exit
2106 * or move to a different cgroup during the call, or are forked or
2107 * move into the cgroup during the call.
2109 * Note that test_task() may be called with locks held, and may in some
2110 * situations be called multiple times for the same task, so it should
2112 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
2113 * pre-allocated and will be used for heap operations (and its "gt" member will
2114 * be overwritten), else a temporary heap will be used (allocation of which
2115 * may cause this function to fail).
2117 int cgroup_scan_tasks(struct cgroup_scanner
*scan
)
2120 struct cgroup_iter it
;
2121 struct task_struct
*p
, *dropped
;
2122 /* Never dereference latest_task, since it's not refcounted */
2123 struct task_struct
*latest_task
= NULL
;
2124 struct ptr_heap tmp_heap
;
2125 struct ptr_heap
*heap
;
2126 struct timespec latest_time
= { 0, 0 };
2129 /* The caller supplied our heap and pre-allocated its memory */
2131 heap
->gt
= &started_after
;
2133 /* We need to allocate our own heap memory */
2135 retval
= heap_init(heap
, PAGE_SIZE
, GFP_KERNEL
, &started_after
);
2137 /* cannot allocate the heap */
2143 * Scan tasks in the cgroup, using the scanner's "test_task" callback
2144 * to determine which are of interest, and using the scanner's
2145 * "process_task" callback to process any of them that need an update.
2146 * Since we don't want to hold any locks during the task updates,
2147 * gather tasks to be processed in a heap structure.
2148 * The heap is sorted by descending task start time.
2149 * If the statically-sized heap fills up, we overflow tasks that
2150 * started later, and in future iterations only consider tasks that
2151 * started after the latest task in the previous pass. This
2152 * guarantees forward progress and that we don't miss any tasks.
2155 cgroup_iter_start(scan
->cg
, &it
);
2156 while ((p
= cgroup_iter_next(scan
->cg
, &it
))) {
2158 * Only affect tasks that qualify per the caller's callback,
2159 * if he provided one
2161 if (scan
->test_task
&& !scan
->test_task(p
, scan
))
2164 * Only process tasks that started after the last task
2167 if (!started_after_time(p
, &latest_time
, latest_task
))
2169 dropped
= heap_insert(heap
, p
);
2170 if (dropped
== NULL
) {
2172 * The new task was inserted; the heap wasn't
2176 } else if (dropped
!= p
) {
2178 * The new task was inserted, and pushed out a
2182 put_task_struct(dropped
);
2185 * Else the new task was newer than anything already in
2186 * the heap and wasn't inserted
2189 cgroup_iter_end(scan
->cg
, &it
);
2192 for (i
= 0; i
< heap
->size
; i
++) {
2193 struct task_struct
*q
= heap
->ptrs
[i
];
2195 latest_time
= q
->start_time
;
2198 /* Process the task per the caller's callback */
2199 scan
->process_task(q
, scan
);
2203 * If we had to process any tasks at all, scan again
2204 * in case some of them were in the middle of forking
2205 * children that didn't get processed.
2206 * Not the most efficient way to do it, but it avoids
2207 * having to take callback_mutex in the fork path
2211 if (heap
== &tmp_heap
)
2212 heap_free(&tmp_heap
);
2217 * Stuff for reading the 'tasks' file.
2219 * Reading this file can return large amounts of data if a cgroup has
2220 * *lots* of attached tasks. So it may need several calls to read(),
2221 * but we cannot guarantee that the information we produce is correct
2222 * unless we produce it entirely atomically.
2227 * Load into 'pidarray' up to 'npids' of the tasks using cgroup
2228 * 'cgrp'. Return actual number of pids loaded. No need to
2229 * task_lock(p) when reading out p->cgroup, since we're in an RCU
2230 * read section, so the css_set can't go away, and is
2231 * immutable after creation.
2233 static int pid_array_load(pid_t
*pidarray
, int npids
, struct cgroup
*cgrp
)
2236 struct cgroup_iter it
;
2237 struct task_struct
*tsk
;
2238 cgroup_iter_start(cgrp
, &it
);
2239 while ((tsk
= cgroup_iter_next(cgrp
, &it
))) {
2240 if (unlikely(n
== npids
))
2242 pid
= task_pid_vnr(tsk
);
2244 pidarray
[n
++] = pid
;
2246 cgroup_iter_end(cgrp
, &it
);
2251 * cgroupstats_build - build and fill cgroupstats
2252 * @stats: cgroupstats to fill information into
2253 * @dentry: A dentry entry belonging to the cgroup for which stats have
2256 * Build and fill cgroupstats so that taskstats can export it to user
2259 int cgroupstats_build(struct cgroupstats
*stats
, struct dentry
*dentry
)
2262 struct cgroup
*cgrp
;
2263 struct cgroup_iter it
;
2264 struct task_struct
*tsk
;
2267 * Validate dentry by checking the superblock operations,
2268 * and make sure it's a directory.
2270 if (dentry
->d_sb
->s_op
!= &cgroup_ops
||
2271 !S_ISDIR(dentry
->d_inode
->i_mode
))
2275 cgrp
= dentry
->d_fsdata
;
2277 cgroup_iter_start(cgrp
, &it
);
2278 while ((tsk
= cgroup_iter_next(cgrp
, &it
))) {
2279 switch (tsk
->state
) {
2281 stats
->nr_running
++;
2283 case TASK_INTERRUPTIBLE
:
2284 stats
->nr_sleeping
++;
2286 case TASK_UNINTERRUPTIBLE
:
2287 stats
->nr_uninterruptible
++;
2290 stats
->nr_stopped
++;
2293 if (delayacct_is_task_waiting_on_io(tsk
))
2294 stats
->nr_io_wait
++;
2298 cgroup_iter_end(cgrp
, &it
);
2305 * Cache pids for all threads in the same pid namespace that are
2306 * opening the same "tasks" file.
2308 struct cgroup_pids
{
2309 /* The node in cgrp->pids_list */
2310 struct list_head list
;
2311 /* The cgroup those pids belong to */
2312 struct cgroup
*cgrp
;
2313 /* The namepsace those pids belong to */
2314 struct pid_namespace
*ns
;
2315 /* Array of process ids in the cgroup */
2317 /* How many files are using the this tasks_pids array */
2319 /* Length of the current tasks_pids array */
2323 static int cmppid(const void *a
, const void *b
)
2325 return *(pid_t
*)a
- *(pid_t
*)b
;
2329 * seq_file methods for the "tasks" file. The seq_file position is the
2330 * next pid to display; the seq_file iterator is a pointer to the pid
2331 * in the cgroup->tasks_pids array.
2334 static void *cgroup_tasks_start(struct seq_file
*s
, loff_t
*pos
)
2337 * Initially we receive a position value that corresponds to
2338 * one more than the last pid shown (or 0 on the first call or
2339 * after a seek to the start). Use a binary-search to find the
2340 * next pid to display, if any
2342 struct cgroup_pids
*cp
= s
->private;
2343 struct cgroup
*cgrp
= cp
->cgrp
;
2344 int index
= 0, pid
= *pos
;
2347 down_read(&cgrp
->pids_mutex
);
2349 int end
= cp
->length
;
2351 while (index
< end
) {
2352 int mid
= (index
+ end
) / 2;
2353 if (cp
->tasks_pids
[mid
] == pid
) {
2356 } else if (cp
->tasks_pids
[mid
] <= pid
)
2362 /* If we're off the end of the array, we're done */
2363 if (index
>= cp
->length
)
2365 /* Update the abstract position to be the actual pid that we found */
2366 iter
= cp
->tasks_pids
+ index
;
2371 static void cgroup_tasks_stop(struct seq_file
*s
, void *v
)
2373 struct cgroup_pids
*cp
= s
->private;
2374 struct cgroup
*cgrp
= cp
->cgrp
;
2375 up_read(&cgrp
->pids_mutex
);
2378 static void *cgroup_tasks_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
2380 struct cgroup_pids
*cp
= s
->private;
2382 int *end
= cp
->tasks_pids
+ cp
->length
;
2385 * Advance to the next pid in the array. If this goes off the
2397 static int cgroup_tasks_show(struct seq_file
*s
, void *v
)
2399 return seq_printf(s
, "%d\n", *(int *)v
);
2402 static const struct seq_operations cgroup_tasks_seq_operations
= {
2403 .start
= cgroup_tasks_start
,
2404 .stop
= cgroup_tasks_stop
,
2405 .next
= cgroup_tasks_next
,
2406 .show
= cgroup_tasks_show
,
2409 static void release_cgroup_pid_array(struct cgroup_pids
*cp
)
2411 struct cgroup
*cgrp
= cp
->cgrp
;
2413 down_write(&cgrp
->pids_mutex
);
2414 BUG_ON(!cp
->use_count
);
2415 if (!--cp
->use_count
) {
2416 list_del(&cp
->list
);
2418 kfree(cp
->tasks_pids
);
2421 up_write(&cgrp
->pids_mutex
);
2424 static int cgroup_tasks_release(struct inode
*inode
, struct file
*file
)
2426 struct seq_file
*seq
;
2427 struct cgroup_pids
*cp
;
2429 if (!(file
->f_mode
& FMODE_READ
))
2432 seq
= file
->private_data
;
2435 release_cgroup_pid_array(cp
);
2436 return seq_release(inode
, file
);
2439 static struct file_operations cgroup_tasks_operations
= {
2441 .llseek
= seq_lseek
,
2442 .write
= cgroup_file_write
,
2443 .release
= cgroup_tasks_release
,
2447 * Handle an open on 'tasks' file. Prepare an array containing the
2448 * process id's of tasks currently attached to the cgroup being opened.
2451 static int cgroup_tasks_open(struct inode
*unused
, struct file
*file
)
2453 struct cgroup
*cgrp
= __d_cgrp(file
->f_dentry
->d_parent
);
2454 struct pid_namespace
*ns
= current
->nsproxy
->pid_ns
;
2455 struct cgroup_pids
*cp
;
2460 /* Nothing to do for write-only files */
2461 if (!(file
->f_mode
& FMODE_READ
))
2465 * If cgroup gets more users after we read count, we won't have
2466 * enough space - tough. This race is indistinguishable to the
2467 * caller from the case that the additional cgroup users didn't
2468 * show up until sometime later on.
2470 npids
= cgroup_task_count(cgrp
);
2471 pidarray
= kmalloc(npids
* sizeof(pid_t
), GFP_KERNEL
);
2474 npids
= pid_array_load(pidarray
, npids
, cgrp
);
2475 sort(pidarray
, npids
, sizeof(pid_t
), cmppid
, NULL
);
2478 * Store the array in the cgroup, freeing the old
2479 * array if necessary
2481 down_write(&cgrp
->pids_mutex
);
2483 list_for_each_entry(cp
, &cgrp
->pids_list
, list
) {
2488 cp
= kzalloc(sizeof(*cp
), GFP_KERNEL
);
2490 up_write(&cgrp
->pids_mutex
);
2497 list_add(&cp
->list
, &cgrp
->pids_list
);
2499 kfree(cp
->tasks_pids
);
2500 cp
->tasks_pids
= pidarray
;
2503 up_write(&cgrp
->pids_mutex
);
2505 file
->f_op
= &cgroup_tasks_operations
;
2507 retval
= seq_open(file
, &cgroup_tasks_seq_operations
);
2509 release_cgroup_pid_array(cp
);
2512 ((struct seq_file
*)file
->private_data
)->private = cp
;
2516 static u64
cgroup_read_notify_on_release(struct cgroup
*cgrp
,
2519 return notify_on_release(cgrp
);
2522 static int cgroup_write_notify_on_release(struct cgroup
*cgrp
,
2526 clear_bit(CGRP_RELEASABLE
, &cgrp
->flags
);
2528 set_bit(CGRP_NOTIFY_ON_RELEASE
, &cgrp
->flags
);
2530 clear_bit(CGRP_NOTIFY_ON_RELEASE
, &cgrp
->flags
);
2535 * for the common functions, 'private' gives the type of file
2537 static struct cftype files
[] = {
2540 .open
= cgroup_tasks_open
,
2541 .write_u64
= cgroup_tasks_write
,
2542 .release
= cgroup_tasks_release
,
2543 .private = FILE_TASKLIST
,
2544 .mode
= S_IRUGO
| S_IWUSR
,
2548 .name
= "notify_on_release",
2549 .read_u64
= cgroup_read_notify_on_release
,
2550 .write_u64
= cgroup_write_notify_on_release
,
2551 .private = FILE_NOTIFY_ON_RELEASE
,
2555 static struct cftype cft_release_agent
= {
2556 .name
= "release_agent",
2557 .read_seq_string
= cgroup_release_agent_show
,
2558 .write_string
= cgroup_release_agent_write
,
2559 .max_write_len
= PATH_MAX
,
2560 .private = FILE_RELEASE_AGENT
,
2563 static int cgroup_populate_dir(struct cgroup
*cgrp
)
2566 struct cgroup_subsys
*ss
;
2568 /* First clear out any existing files */
2569 cgroup_clear_directory(cgrp
->dentry
);
2571 err
= cgroup_add_files(cgrp
, NULL
, files
, ARRAY_SIZE(files
));
2575 if (cgrp
== cgrp
->top_cgroup
) {
2576 if ((err
= cgroup_add_file(cgrp
, NULL
, &cft_release_agent
)) < 0)
2580 for_each_subsys(cgrp
->root
, ss
) {
2581 if (ss
->populate
&& (err
= ss
->populate(ss
, cgrp
)) < 0)
2584 /* This cgroup is ready now */
2585 for_each_subsys(cgrp
->root
, ss
) {
2586 struct cgroup_subsys_state
*css
= cgrp
->subsys
[ss
->subsys_id
];
2588 * Update id->css pointer and make this css visible from
2589 * CSS ID functions. This pointer will be dereferened
2590 * from RCU-read-side without locks.
2593 rcu_assign_pointer(css
->id
->css
, css
);
2599 static void init_cgroup_css(struct cgroup_subsys_state
*css
,
2600 struct cgroup_subsys
*ss
,
2601 struct cgroup
*cgrp
)
2604 atomic_set(&css
->refcnt
, 1);
2607 if (cgrp
== dummytop
)
2608 set_bit(CSS_ROOT
, &css
->flags
);
2609 BUG_ON(cgrp
->subsys
[ss
->subsys_id
]);
2610 cgrp
->subsys
[ss
->subsys_id
] = css
;
2613 static void cgroup_lock_hierarchy(struct cgroupfs_root
*root
)
2615 /* We need to take each hierarchy_mutex in a consistent order */
2618 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
2619 struct cgroup_subsys
*ss
= subsys
[i
];
2620 if (ss
->root
== root
)
2621 mutex_lock(&ss
->hierarchy_mutex
);
2625 static void cgroup_unlock_hierarchy(struct cgroupfs_root
*root
)
2629 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
2630 struct cgroup_subsys
*ss
= subsys
[i
];
2631 if (ss
->root
== root
)
2632 mutex_unlock(&ss
->hierarchy_mutex
);
2637 * cgroup_create - create a cgroup
2638 * @parent: cgroup that will be parent of the new cgroup
2639 * @dentry: dentry of the new cgroup
2640 * @mode: mode to set on new inode
2642 * Must be called with the mutex on the parent inode held
2644 static long cgroup_create(struct cgroup
*parent
, struct dentry
*dentry
,
2647 struct cgroup
*cgrp
;
2648 struct cgroupfs_root
*root
= parent
->root
;
2650 struct cgroup_subsys
*ss
;
2651 struct super_block
*sb
= root
->sb
;
2653 cgrp
= kzalloc(sizeof(*cgrp
), GFP_KERNEL
);
2657 /* Grab a reference on the superblock so the hierarchy doesn't
2658 * get deleted on unmount if there are child cgroups. This
2659 * can be done outside cgroup_mutex, since the sb can't
2660 * disappear while someone has an open control file on the
2662 atomic_inc(&sb
->s_active
);
2664 mutex_lock(&cgroup_mutex
);
2666 init_cgroup_housekeeping(cgrp
);
2668 cgrp
->parent
= parent
;
2669 cgrp
->root
= parent
->root
;
2670 cgrp
->top_cgroup
= parent
->top_cgroup
;
2672 if (notify_on_release(parent
))
2673 set_bit(CGRP_NOTIFY_ON_RELEASE
, &cgrp
->flags
);
2675 for_each_subsys(root
, ss
) {
2676 struct cgroup_subsys_state
*css
= ss
->create(ss
, cgrp
);
2681 init_cgroup_css(css
, ss
, cgrp
);
2683 if (alloc_css_id(ss
, parent
, cgrp
))
2685 /* At error, ->destroy() callback has to free assigned ID. */
2688 cgroup_lock_hierarchy(root
);
2689 list_add(&cgrp
->sibling
, &cgrp
->parent
->children
);
2690 cgroup_unlock_hierarchy(root
);
2691 root
->number_of_cgroups
++;
2693 err
= cgroup_create_dir(cgrp
, dentry
, mode
);
2697 /* The cgroup directory was pre-locked for us */
2698 BUG_ON(!mutex_is_locked(&cgrp
->dentry
->d_inode
->i_mutex
));
2700 err
= cgroup_populate_dir(cgrp
);
2701 /* If err < 0, we have a half-filled directory - oh well ;) */
2703 mutex_unlock(&cgroup_mutex
);
2704 mutex_unlock(&cgrp
->dentry
->d_inode
->i_mutex
);
2710 cgroup_lock_hierarchy(root
);
2711 list_del(&cgrp
->sibling
);
2712 cgroup_unlock_hierarchy(root
);
2713 root
->number_of_cgroups
--;
2717 for_each_subsys(root
, ss
) {
2718 if (cgrp
->subsys
[ss
->subsys_id
])
2719 ss
->destroy(ss
, cgrp
);
2722 mutex_unlock(&cgroup_mutex
);
2724 /* Release the reference count that we took on the superblock */
2725 deactivate_super(sb
);
2731 static int cgroup_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
2733 struct cgroup
*c_parent
= dentry
->d_parent
->d_fsdata
;
2735 /* the vfs holds inode->i_mutex already */
2736 return cgroup_create(c_parent
, dentry
, mode
| S_IFDIR
);
2739 static int cgroup_has_css_refs(struct cgroup
*cgrp
)
2741 /* Check the reference count on each subsystem. Since we
2742 * already established that there are no tasks in the
2743 * cgroup, if the css refcount is also 1, then there should
2744 * be no outstanding references, so the subsystem is safe to
2745 * destroy. We scan across all subsystems rather than using
2746 * the per-hierarchy linked list of mounted subsystems since
2747 * we can be called via check_for_release() with no
2748 * synchronization other than RCU, and the subsystem linked
2749 * list isn't RCU-safe */
2751 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
2752 struct cgroup_subsys
*ss
= subsys
[i
];
2753 struct cgroup_subsys_state
*css
;
2754 /* Skip subsystems not in this hierarchy */
2755 if (ss
->root
!= cgrp
->root
)
2757 css
= cgrp
->subsys
[ss
->subsys_id
];
2758 /* When called from check_for_release() it's possible
2759 * that by this point the cgroup has been removed
2760 * and the css deleted. But a false-positive doesn't
2761 * matter, since it can only happen if the cgroup
2762 * has been deleted and hence no longer needs the
2763 * release agent to be called anyway. */
2764 if (css
&& (atomic_read(&css
->refcnt
) > 1))
2771 * Atomically mark all (or else none) of the cgroup's CSS objects as
2772 * CSS_REMOVED. Return true on success, or false if the cgroup has
2773 * busy subsystems. Call with cgroup_mutex held
2776 static int cgroup_clear_css_refs(struct cgroup
*cgrp
)
2778 struct cgroup_subsys
*ss
;
2779 unsigned long flags
;
2780 bool failed
= false;
2781 local_irq_save(flags
);
2782 for_each_subsys(cgrp
->root
, ss
) {
2783 struct cgroup_subsys_state
*css
= cgrp
->subsys
[ss
->subsys_id
];
2786 /* We can only remove a CSS with a refcnt==1 */
2787 refcnt
= atomic_read(&css
->refcnt
);
2794 * Drop the refcnt to 0 while we check other
2795 * subsystems. This will cause any racing
2796 * css_tryget() to spin until we set the
2797 * CSS_REMOVED bits or abort
2799 if (atomic_cmpxchg(&css
->refcnt
, refcnt
, 0) == refcnt
)
2805 for_each_subsys(cgrp
->root
, ss
) {
2806 struct cgroup_subsys_state
*css
= cgrp
->subsys
[ss
->subsys_id
];
2809 * Restore old refcnt if we previously managed
2810 * to clear it from 1 to 0
2812 if (!atomic_read(&css
->refcnt
))
2813 atomic_set(&css
->refcnt
, 1);
2815 /* Commit the fact that the CSS is removed */
2816 set_bit(CSS_REMOVED
, &css
->flags
);
2819 local_irq_restore(flags
);
2823 static int cgroup_rmdir(struct inode
*unused_dir
, struct dentry
*dentry
)
2825 struct cgroup
*cgrp
= dentry
->d_fsdata
;
2827 struct cgroup
*parent
;
2831 /* the vfs holds both inode->i_mutex already */
2833 mutex_lock(&cgroup_mutex
);
2834 if (atomic_read(&cgrp
->count
) != 0) {
2835 mutex_unlock(&cgroup_mutex
);
2838 if (!list_empty(&cgrp
->children
)) {
2839 mutex_unlock(&cgroup_mutex
);
2842 mutex_unlock(&cgroup_mutex
);
2845 * In general, subsystem has no css->refcnt after pre_destroy(). But
2846 * in racy cases, subsystem may have to get css->refcnt after
2847 * pre_destroy() and it makes rmdir return with -EBUSY. This sometimes
2848 * make rmdir return -EBUSY too often. To avoid that, we use waitqueue
2849 * for cgroup's rmdir. CGRP_WAIT_ON_RMDIR is for synchronizing rmdir
2850 * and subsystem's reference count handling. Please see css_get/put
2851 * and css_tryget() and cgroup_wakeup_rmdir_waiter() implementation.
2853 set_bit(CGRP_WAIT_ON_RMDIR
, &cgrp
->flags
);
2856 * Call pre_destroy handlers of subsys. Notify subsystems
2857 * that rmdir() request comes.
2859 ret
= cgroup_call_pre_destroy(cgrp
);
2861 clear_bit(CGRP_WAIT_ON_RMDIR
, &cgrp
->flags
);
2865 mutex_lock(&cgroup_mutex
);
2866 parent
= cgrp
->parent
;
2867 if (atomic_read(&cgrp
->count
) || !list_empty(&cgrp
->children
)) {
2868 clear_bit(CGRP_WAIT_ON_RMDIR
, &cgrp
->flags
);
2869 mutex_unlock(&cgroup_mutex
);
2872 prepare_to_wait(&cgroup_rmdir_waitq
, &wait
, TASK_INTERRUPTIBLE
);
2873 if (!cgroup_clear_css_refs(cgrp
)) {
2874 mutex_unlock(&cgroup_mutex
);
2876 * Because someone may call cgroup_wakeup_rmdir_waiter() before
2877 * prepare_to_wait(), we need to check this flag.
2879 if (test_bit(CGRP_WAIT_ON_RMDIR
, &cgrp
->flags
))
2881 finish_wait(&cgroup_rmdir_waitq
, &wait
);
2882 clear_bit(CGRP_WAIT_ON_RMDIR
, &cgrp
->flags
);
2883 if (signal_pending(current
))
2887 /* NO css_tryget() can success after here. */
2888 finish_wait(&cgroup_rmdir_waitq
, &wait
);
2889 clear_bit(CGRP_WAIT_ON_RMDIR
, &cgrp
->flags
);
2891 spin_lock(&release_list_lock
);
2892 set_bit(CGRP_REMOVED
, &cgrp
->flags
);
2893 if (!list_empty(&cgrp
->release_list
))
2894 list_del(&cgrp
->release_list
);
2895 spin_unlock(&release_list_lock
);
2897 cgroup_lock_hierarchy(cgrp
->root
);
2898 /* delete this cgroup from parent->children */
2899 list_del(&cgrp
->sibling
);
2900 cgroup_unlock_hierarchy(cgrp
->root
);
2902 spin_lock(&cgrp
->dentry
->d_lock
);
2903 d
= dget(cgrp
->dentry
);
2904 spin_unlock(&d
->d_lock
);
2906 cgroup_d_remove_dir(d
);
2909 set_bit(CGRP_RELEASABLE
, &parent
->flags
);
2910 check_for_release(parent
);
2912 mutex_unlock(&cgroup_mutex
);
2916 static void __init
cgroup_init_subsys(struct cgroup_subsys
*ss
)
2918 struct cgroup_subsys_state
*css
;
2920 printk(KERN_INFO
"Initializing cgroup subsys %s\n", ss
->name
);
2922 /* Create the top cgroup state for this subsystem */
2923 list_add(&ss
->sibling
, &rootnode
.subsys_list
);
2924 ss
->root
= &rootnode
;
2925 css
= ss
->create(ss
, dummytop
);
2926 /* We don't handle early failures gracefully */
2927 BUG_ON(IS_ERR(css
));
2928 init_cgroup_css(css
, ss
, dummytop
);
2930 /* Update the init_css_set to contain a subsys
2931 * pointer to this state - since the subsystem is
2932 * newly registered, all tasks and hence the
2933 * init_css_set is in the subsystem's top cgroup. */
2934 init_css_set
.subsys
[ss
->subsys_id
] = dummytop
->subsys
[ss
->subsys_id
];
2936 need_forkexit_callback
|= ss
->fork
|| ss
->exit
;
2938 /* At system boot, before all subsystems have been
2939 * registered, no tasks have been forked, so we don't
2940 * need to invoke fork callbacks here. */
2941 BUG_ON(!list_empty(&init_task
.tasks
));
2943 mutex_init(&ss
->hierarchy_mutex
);
2944 lockdep_set_class(&ss
->hierarchy_mutex
, &ss
->subsys_key
);
2949 * cgroup_init_early - cgroup initialization at system boot
2951 * Initialize cgroups at system boot, and initialize any
2952 * subsystems that request early init.
2954 int __init
cgroup_init_early(void)
2957 atomic_set(&init_css_set
.refcount
, 1);
2958 INIT_LIST_HEAD(&init_css_set
.cg_links
);
2959 INIT_LIST_HEAD(&init_css_set
.tasks
);
2960 INIT_HLIST_NODE(&init_css_set
.hlist
);
2962 init_cgroup_root(&rootnode
);
2964 init_task
.cgroups
= &init_css_set
;
2966 init_css_set_link
.cg
= &init_css_set
;
2967 list_add(&init_css_set_link
.cgrp_link_list
,
2968 &rootnode
.top_cgroup
.css_sets
);
2969 list_add(&init_css_set_link
.cg_link_list
,
2970 &init_css_set
.cg_links
);
2972 for (i
= 0; i
< CSS_SET_TABLE_SIZE
; i
++)
2973 INIT_HLIST_HEAD(&css_set_table
[i
]);
2975 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
2976 struct cgroup_subsys
*ss
= subsys
[i
];
2979 BUG_ON(strlen(ss
->name
) > MAX_CGROUP_TYPE_NAMELEN
);
2980 BUG_ON(!ss
->create
);
2981 BUG_ON(!ss
->destroy
);
2982 if (ss
->subsys_id
!= i
) {
2983 printk(KERN_ERR
"cgroup: Subsys %s id == %d\n",
2984 ss
->name
, ss
->subsys_id
);
2989 cgroup_init_subsys(ss
);
2995 * cgroup_init - cgroup initialization
2997 * Register cgroup filesystem and /proc file, and initialize
2998 * any subsystems that didn't request early init.
3000 int __init
cgroup_init(void)
3004 struct hlist_head
*hhead
;
3006 err
= bdi_init(&cgroup_backing_dev_info
);
3010 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
3011 struct cgroup_subsys
*ss
= subsys
[i
];
3012 if (!ss
->early_init
)
3013 cgroup_init_subsys(ss
);
3015 cgroup_subsys_init_idr(ss
);
3018 /* Add init_css_set to the hash table */
3019 hhead
= css_set_hash(init_css_set
.subsys
);
3020 hlist_add_head(&init_css_set
.hlist
, hhead
);
3022 err
= register_filesystem(&cgroup_fs_type
);
3026 proc_create("cgroups", 0, NULL
, &proc_cgroupstats_operations
);
3030 bdi_destroy(&cgroup_backing_dev_info
);
3036 * proc_cgroup_show()
3037 * - Print task's cgroup paths into seq_file, one line for each hierarchy
3038 * - Used for /proc/<pid>/cgroup.
3039 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
3040 * doesn't really matter if tsk->cgroup changes after we read it,
3041 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
3042 * anyway. No need to check that tsk->cgroup != NULL, thanks to
3043 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
3044 * cgroup to top_cgroup.
3047 /* TODO: Use a proper seq_file iterator */
3048 static int proc_cgroup_show(struct seq_file
*m
, void *v
)
3051 struct task_struct
*tsk
;
3054 struct cgroupfs_root
*root
;
3057 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
3063 tsk
= get_pid_task(pid
, PIDTYPE_PID
);
3069 mutex_lock(&cgroup_mutex
);
3071 for_each_active_root(root
) {
3072 struct cgroup_subsys
*ss
;
3073 struct cgroup
*cgrp
;
3077 seq_printf(m
, "%lu:", root
->subsys_bits
);
3078 for_each_subsys(root
, ss
)
3079 seq_printf(m
, "%s%s", count
++ ? "," : "", ss
->name
);
3080 if (strlen(root
->name
))
3081 seq_printf(m
, "%sname=%s", count
? "," : "",
3084 get_first_subsys(&root
->top_cgroup
, NULL
, &subsys_id
);
3085 cgrp
= task_cgroup(tsk
, subsys_id
);
3086 retval
= cgroup_path(cgrp
, buf
, PAGE_SIZE
);
3094 mutex_unlock(&cgroup_mutex
);
3095 put_task_struct(tsk
);
3102 static int cgroup_open(struct inode
*inode
, struct file
*file
)
3104 struct pid
*pid
= PROC_I(inode
)->pid
;
3105 return single_open(file
, proc_cgroup_show
, pid
);
3108 struct file_operations proc_cgroup_operations
= {
3109 .open
= cgroup_open
,
3111 .llseek
= seq_lseek
,
3112 .release
= single_release
,
3115 /* Display information about each subsystem and each hierarchy */
3116 static int proc_cgroupstats_show(struct seq_file
*m
, void *v
)
3120 seq_puts(m
, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
3121 mutex_lock(&cgroup_mutex
);
3122 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
3123 struct cgroup_subsys
*ss
= subsys
[i
];
3124 seq_printf(m
, "%s\t%lu\t%d\t%d\n",
3125 ss
->name
, ss
->root
->subsys_bits
,
3126 ss
->root
->number_of_cgroups
, !ss
->disabled
);
3128 mutex_unlock(&cgroup_mutex
);
3132 static int cgroupstats_open(struct inode
*inode
, struct file
*file
)
3134 return single_open(file
, proc_cgroupstats_show
, NULL
);
3137 static struct file_operations proc_cgroupstats_operations
= {
3138 .open
= cgroupstats_open
,
3140 .llseek
= seq_lseek
,
3141 .release
= single_release
,
3145 * cgroup_fork - attach newly forked task to its parents cgroup.
3146 * @child: pointer to task_struct of forking parent process.
3148 * Description: A task inherits its parent's cgroup at fork().
3150 * A pointer to the shared css_set was automatically copied in
3151 * fork.c by dup_task_struct(). However, we ignore that copy, since
3152 * it was not made under the protection of RCU or cgroup_mutex, so
3153 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
3154 * have already changed current->cgroups, allowing the previously
3155 * referenced cgroup group to be removed and freed.
3157 * At the point that cgroup_fork() is called, 'current' is the parent
3158 * task, and the passed argument 'child' points to the child task.
3160 void cgroup_fork(struct task_struct
*child
)
3163 child
->cgroups
= current
->cgroups
;
3164 get_css_set(child
->cgroups
);
3165 task_unlock(current
);
3166 INIT_LIST_HEAD(&child
->cg_list
);
3170 * cgroup_fork_callbacks - run fork callbacks
3171 * @child: the new task
3173 * Called on a new task very soon before adding it to the
3174 * tasklist. No need to take any locks since no-one can
3175 * be operating on this task.
3177 void cgroup_fork_callbacks(struct task_struct
*child
)
3179 if (need_forkexit_callback
) {
3181 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
3182 struct cgroup_subsys
*ss
= subsys
[i
];
3184 ss
->fork(ss
, child
);
3190 * cgroup_post_fork - called on a new task after adding it to the task list
3191 * @child: the task in question
3193 * Adds the task to the list running through its css_set if necessary.
3194 * Has to be after the task is visible on the task list in case we race
3195 * with the first call to cgroup_iter_start() - to guarantee that the
3196 * new task ends up on its list.
3198 void cgroup_post_fork(struct task_struct
*child
)
3200 if (use_task_css_set_links
) {
3201 write_lock(&css_set_lock
);
3203 if (list_empty(&child
->cg_list
))
3204 list_add(&child
->cg_list
, &child
->cgroups
->tasks
);
3206 write_unlock(&css_set_lock
);
3210 * cgroup_exit - detach cgroup from exiting task
3211 * @tsk: pointer to task_struct of exiting process
3212 * @run_callback: run exit callbacks?
3214 * Description: Detach cgroup from @tsk and release it.
3216 * Note that cgroups marked notify_on_release force every task in
3217 * them to take the global cgroup_mutex mutex when exiting.
3218 * This could impact scaling on very large systems. Be reluctant to
3219 * use notify_on_release cgroups where very high task exit scaling
3220 * is required on large systems.
3222 * the_top_cgroup_hack:
3224 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
3226 * We call cgroup_exit() while the task is still competent to
3227 * handle notify_on_release(), then leave the task attached to the
3228 * root cgroup in each hierarchy for the remainder of its exit.
3230 * To do this properly, we would increment the reference count on
3231 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
3232 * code we would add a second cgroup function call, to drop that
3233 * reference. This would just create an unnecessary hot spot on
3234 * the top_cgroup reference count, to no avail.
3236 * Normally, holding a reference to a cgroup without bumping its
3237 * count is unsafe. The cgroup could go away, or someone could
3238 * attach us to a different cgroup, decrementing the count on
3239 * the first cgroup that we never incremented. But in this case,
3240 * top_cgroup isn't going away, and either task has PF_EXITING set,
3241 * which wards off any cgroup_attach_task() attempts, or task is a failed
3242 * fork, never visible to cgroup_attach_task.
3244 void cgroup_exit(struct task_struct
*tsk
, int run_callbacks
)
3249 if (run_callbacks
&& need_forkexit_callback
) {
3250 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
3251 struct cgroup_subsys
*ss
= subsys
[i
];
3258 * Unlink from the css_set task list if necessary.
3259 * Optimistically check cg_list before taking
3262 if (!list_empty(&tsk
->cg_list
)) {
3263 write_lock(&css_set_lock
);
3264 if (!list_empty(&tsk
->cg_list
))
3265 list_del(&tsk
->cg_list
);
3266 write_unlock(&css_set_lock
);
3269 /* Reassign the task to the init_css_set. */
3272 tsk
->cgroups
= &init_css_set
;
3275 put_css_set_taskexit(cg
);
3279 * cgroup_clone - clone the cgroup the given subsystem is attached to
3280 * @tsk: the task to be moved
3281 * @subsys: the given subsystem
3282 * @nodename: the name for the new cgroup
3284 * Duplicate the current cgroup in the hierarchy that the given
3285 * subsystem is attached to, and move this task into the new
3288 int cgroup_clone(struct task_struct
*tsk
, struct cgroup_subsys
*subsys
,
3291 struct dentry
*dentry
;
3293 struct cgroup
*parent
, *child
;
3294 struct inode
*inode
;
3296 struct cgroupfs_root
*root
;
3297 struct cgroup_subsys
*ss
;
3299 /* We shouldn't be called by an unregistered subsystem */
3300 BUG_ON(!subsys
->active
);
3302 /* First figure out what hierarchy and cgroup we're dealing
3303 * with, and pin them so we can drop cgroup_mutex */
3304 mutex_lock(&cgroup_mutex
);
3306 root
= subsys
->root
;
3307 if (root
== &rootnode
) {
3308 mutex_unlock(&cgroup_mutex
);
3312 /* Pin the hierarchy */
3313 if (!atomic_inc_not_zero(&root
->sb
->s_active
)) {
3314 /* We race with the final deactivate_super() */
3315 mutex_unlock(&cgroup_mutex
);
3319 /* Keep the cgroup alive */
3321 parent
= task_cgroup(tsk
, subsys
->subsys_id
);
3326 mutex_unlock(&cgroup_mutex
);
3328 /* Now do the VFS work to create a cgroup */
3329 inode
= parent
->dentry
->d_inode
;
3331 /* Hold the parent directory mutex across this operation to
3332 * stop anyone else deleting the new cgroup */
3333 mutex_lock(&inode
->i_mutex
);
3334 dentry
= lookup_one_len(nodename
, parent
->dentry
, strlen(nodename
));
3335 if (IS_ERR(dentry
)) {
3337 "cgroup: Couldn't allocate dentry for %s: %ld\n", nodename
,
3339 ret
= PTR_ERR(dentry
);
3343 /* Create the cgroup directory, which also creates the cgroup */
3344 ret
= vfs_mkdir(inode
, dentry
, 0755);
3345 child
= __d_cgrp(dentry
);
3349 "Failed to create cgroup %s: %d\n", nodename
,
3354 /* The cgroup now exists. Retake cgroup_mutex and check
3355 * that we're still in the same state that we thought we
3357 mutex_lock(&cgroup_mutex
);
3358 if ((root
!= subsys
->root
) ||
3359 (parent
!= task_cgroup(tsk
, subsys
->subsys_id
))) {
3360 /* Aargh, we raced ... */
3361 mutex_unlock(&inode
->i_mutex
);
3364 deactivate_super(root
->sb
);
3365 /* The cgroup is still accessible in the VFS, but
3366 * we're not going to try to rmdir() it at this
3369 "Race in cgroup_clone() - leaking cgroup %s\n",
3374 /* do any required auto-setup */
3375 for_each_subsys(root
, ss
) {
3377 ss
->post_clone(ss
, child
);
3380 /* All seems fine. Finish by moving the task into the new cgroup */
3381 ret
= cgroup_attach_task(child
, tsk
);
3382 mutex_unlock(&cgroup_mutex
);
3385 mutex_unlock(&inode
->i_mutex
);
3387 mutex_lock(&cgroup_mutex
);
3389 mutex_unlock(&cgroup_mutex
);
3390 deactivate_super(root
->sb
);
3395 * cgroup_is_descendant - see if @cgrp is a descendant of @task's cgrp
3396 * @cgrp: the cgroup in question
3397 * @task: the task in question
3399 * See if @cgrp is a descendant of @task's cgroup in the appropriate
3402 * If we are sending in dummytop, then presumably we are creating
3403 * the top cgroup in the subsystem.
3405 * Called only by the ns (nsproxy) cgroup.
3407 int cgroup_is_descendant(const struct cgroup
*cgrp
, struct task_struct
*task
)
3410 struct cgroup
*target
;
3413 if (cgrp
== dummytop
)
3416 get_first_subsys(cgrp
, NULL
, &subsys_id
);
3417 target
= task_cgroup(task
, subsys_id
);
3418 while (cgrp
!= target
&& cgrp
!= cgrp
->top_cgroup
)
3419 cgrp
= cgrp
->parent
;
3420 ret
= (cgrp
== target
);
3424 static void check_for_release(struct cgroup
*cgrp
)
3426 /* All of these checks rely on RCU to keep the cgroup
3427 * structure alive */
3428 if (cgroup_is_releasable(cgrp
) && !atomic_read(&cgrp
->count
)
3429 && list_empty(&cgrp
->children
) && !cgroup_has_css_refs(cgrp
)) {
3430 /* Control Group is currently removeable. If it's not
3431 * already queued for a userspace notification, queue
3433 int need_schedule_work
= 0;
3434 spin_lock(&release_list_lock
);
3435 if (!cgroup_is_removed(cgrp
) &&
3436 list_empty(&cgrp
->release_list
)) {
3437 list_add(&cgrp
->release_list
, &release_list
);
3438 need_schedule_work
= 1;
3440 spin_unlock(&release_list_lock
);
3441 if (need_schedule_work
)
3442 schedule_work(&release_agent_work
);
3446 void __css_put(struct cgroup_subsys_state
*css
)
3448 struct cgroup
*cgrp
= css
->cgroup
;
3450 if (atomic_dec_return(&css
->refcnt
) == 1) {
3451 if (notify_on_release(cgrp
)) {
3452 set_bit(CGRP_RELEASABLE
, &cgrp
->flags
);
3453 check_for_release(cgrp
);
3455 cgroup_wakeup_rmdir_waiter(cgrp
);
3461 * Notify userspace when a cgroup is released, by running the
3462 * configured release agent with the name of the cgroup (path
3463 * relative to the root of cgroup file system) as the argument.
3465 * Most likely, this user command will try to rmdir this cgroup.
3467 * This races with the possibility that some other task will be
3468 * attached to this cgroup before it is removed, or that some other
3469 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
3470 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
3471 * unused, and this cgroup will be reprieved from its death sentence,
3472 * to continue to serve a useful existence. Next time it's released,
3473 * we will get notified again, if it still has 'notify_on_release' set.
3475 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
3476 * means only wait until the task is successfully execve()'d. The
3477 * separate release agent task is forked by call_usermodehelper(),
3478 * then control in this thread returns here, without waiting for the
3479 * release agent task. We don't bother to wait because the caller of
3480 * this routine has no use for the exit status of the release agent
3481 * task, so no sense holding our caller up for that.
3483 static void cgroup_release_agent(struct work_struct
*work
)
3485 BUG_ON(work
!= &release_agent_work
);
3486 mutex_lock(&cgroup_mutex
);
3487 spin_lock(&release_list_lock
);
3488 while (!list_empty(&release_list
)) {
3489 char *argv
[3], *envp
[3];
3491 char *pathbuf
= NULL
, *agentbuf
= NULL
;
3492 struct cgroup
*cgrp
= list_entry(release_list
.next
,
3495 list_del_init(&cgrp
->release_list
);
3496 spin_unlock(&release_list_lock
);
3497 pathbuf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
3500 if (cgroup_path(cgrp
, pathbuf
, PAGE_SIZE
) < 0)
3502 agentbuf
= kstrdup(cgrp
->root
->release_agent_path
, GFP_KERNEL
);
3507 argv
[i
++] = agentbuf
;
3508 argv
[i
++] = pathbuf
;
3512 /* minimal command environment */
3513 envp
[i
++] = "HOME=/";
3514 envp
[i
++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
3517 /* Drop the lock while we invoke the usermode helper,
3518 * since the exec could involve hitting disk and hence
3519 * be a slow process */
3520 mutex_unlock(&cgroup_mutex
);
3521 call_usermodehelper(argv
[0], argv
, envp
, UMH_WAIT_EXEC
);
3522 mutex_lock(&cgroup_mutex
);
3526 spin_lock(&release_list_lock
);
3528 spin_unlock(&release_list_lock
);
3529 mutex_unlock(&cgroup_mutex
);
3532 static int __init
cgroup_disable(char *str
)
3537 while ((token
= strsep(&str
, ",")) != NULL
) {
3541 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
3542 struct cgroup_subsys
*ss
= subsys
[i
];
3544 if (!strcmp(token
, ss
->name
)) {
3546 printk(KERN_INFO
"Disabling %s control group"
3547 " subsystem\n", ss
->name
);
3554 __setup("cgroup_disable=", cgroup_disable
);
3557 * Functons for CSS ID.
3561 *To get ID other than 0, this should be called when !cgroup_is_removed().
3563 unsigned short css_id(struct cgroup_subsys_state
*css
)
3565 struct css_id
*cssid
= rcu_dereference(css
->id
);
3572 unsigned short css_depth(struct cgroup_subsys_state
*css
)
3574 struct css_id
*cssid
= rcu_dereference(css
->id
);
3577 return cssid
->depth
;
3581 bool css_is_ancestor(struct cgroup_subsys_state
*child
,
3582 const struct cgroup_subsys_state
*root
)
3584 struct css_id
*child_id
= rcu_dereference(child
->id
);
3585 struct css_id
*root_id
= rcu_dereference(root
->id
);
3587 if (!child_id
|| !root_id
|| (child_id
->depth
< root_id
->depth
))
3589 return child_id
->stack
[root_id
->depth
] == root_id
->id
;
3592 static void __free_css_id_cb(struct rcu_head
*head
)
3596 id
= container_of(head
, struct css_id
, rcu_head
);
3600 void free_css_id(struct cgroup_subsys
*ss
, struct cgroup_subsys_state
*css
)
3602 struct css_id
*id
= css
->id
;
3603 /* When this is called before css_id initialization, id can be NULL */
3607 BUG_ON(!ss
->use_id
);
3609 rcu_assign_pointer(id
->css
, NULL
);
3610 rcu_assign_pointer(css
->id
, NULL
);
3611 spin_lock(&ss
->id_lock
);
3612 idr_remove(&ss
->idr
, id
->id
);
3613 spin_unlock(&ss
->id_lock
);
3614 call_rcu(&id
->rcu_head
, __free_css_id_cb
);
3618 * This is called by init or create(). Then, calls to this function are
3619 * always serialized (By cgroup_mutex() at create()).
3622 static struct css_id
*get_new_cssid(struct cgroup_subsys
*ss
, int depth
)
3624 struct css_id
*newid
;
3625 int myid
, error
, size
;
3627 BUG_ON(!ss
->use_id
);
3629 size
= sizeof(*newid
) + sizeof(unsigned short) * (depth
+ 1);
3630 newid
= kzalloc(size
, GFP_KERNEL
);
3632 return ERR_PTR(-ENOMEM
);
3634 if (unlikely(!idr_pre_get(&ss
->idr
, GFP_KERNEL
))) {
3638 spin_lock(&ss
->id_lock
);
3639 /* Don't use 0. allocates an ID of 1-65535 */
3640 error
= idr_get_new_above(&ss
->idr
, newid
, 1, &myid
);
3641 spin_unlock(&ss
->id_lock
);
3643 /* Returns error when there are no free spaces for new ID.*/
3648 if (myid
> CSS_ID_MAX
)
3652 newid
->depth
= depth
;
3656 spin_lock(&ss
->id_lock
);
3657 idr_remove(&ss
->idr
, myid
);
3658 spin_unlock(&ss
->id_lock
);
3661 return ERR_PTR(error
);
3665 static int __init
cgroup_subsys_init_idr(struct cgroup_subsys
*ss
)
3667 struct css_id
*newid
;
3668 struct cgroup_subsys_state
*rootcss
;
3670 spin_lock_init(&ss
->id_lock
);
3673 rootcss
= init_css_set
.subsys
[ss
->subsys_id
];
3674 newid
= get_new_cssid(ss
, 0);
3676 return PTR_ERR(newid
);
3678 newid
->stack
[0] = newid
->id
;
3679 newid
->css
= rootcss
;
3680 rootcss
->id
= newid
;
3684 static int alloc_css_id(struct cgroup_subsys
*ss
, struct cgroup
*parent
,
3685 struct cgroup
*child
)
3687 int subsys_id
, i
, depth
= 0;
3688 struct cgroup_subsys_state
*parent_css
, *child_css
;
3689 struct css_id
*child_id
, *parent_id
= NULL
;
3691 subsys_id
= ss
->subsys_id
;
3692 parent_css
= parent
->subsys
[subsys_id
];
3693 child_css
= child
->subsys
[subsys_id
];
3694 depth
= css_depth(parent_css
) + 1;
3695 parent_id
= parent_css
->id
;
3697 child_id
= get_new_cssid(ss
, depth
);
3698 if (IS_ERR(child_id
))
3699 return PTR_ERR(child_id
);
3701 for (i
= 0; i
< depth
; i
++)
3702 child_id
->stack
[i
] = parent_id
->stack
[i
];
3703 child_id
->stack
[depth
] = child_id
->id
;
3705 * child_id->css pointer will be set after this cgroup is available
3706 * see cgroup_populate_dir()
3708 rcu_assign_pointer(child_css
->id
, child_id
);
3714 * css_lookup - lookup css by id
3715 * @ss: cgroup subsys to be looked into.
3718 * Returns pointer to cgroup_subsys_state if there is valid one with id.
3719 * NULL if not. Should be called under rcu_read_lock()
3721 struct cgroup_subsys_state
*css_lookup(struct cgroup_subsys
*ss
, int id
)
3723 struct css_id
*cssid
= NULL
;
3725 BUG_ON(!ss
->use_id
);
3726 cssid
= idr_find(&ss
->idr
, id
);
3728 if (unlikely(!cssid
))
3731 return rcu_dereference(cssid
->css
);
3735 * css_get_next - lookup next cgroup under specified hierarchy.
3736 * @ss: pointer to subsystem
3737 * @id: current position of iteration.
3738 * @root: pointer to css. search tree under this.
3739 * @foundid: position of found object.
3741 * Search next css under the specified hierarchy of rootid. Calling under
3742 * rcu_read_lock() is necessary. Returns NULL if it reaches the end.
3744 struct cgroup_subsys_state
*
3745 css_get_next(struct cgroup_subsys
*ss
, int id
,
3746 struct cgroup_subsys_state
*root
, int *foundid
)
3748 struct cgroup_subsys_state
*ret
= NULL
;
3751 int rootid
= css_id(root
);
3752 int depth
= css_depth(root
);
3757 BUG_ON(!ss
->use_id
);
3758 /* fill start point for scan */
3762 * scan next entry from bitmap(tree), tmpid is updated after
3765 spin_lock(&ss
->id_lock
);
3766 tmp
= idr_get_next(&ss
->idr
, &tmpid
);
3767 spin_unlock(&ss
->id_lock
);
3771 if (tmp
->depth
>= depth
&& tmp
->stack
[depth
] == rootid
) {
3772 ret
= rcu_dereference(tmp
->css
);
3778 /* continue to scan from next id */
3784 #ifdef CONFIG_CGROUP_DEBUG
3785 static struct cgroup_subsys_state
*debug_create(struct cgroup_subsys
*ss
,
3786 struct cgroup
*cont
)
3788 struct cgroup_subsys_state
*css
= kzalloc(sizeof(*css
), GFP_KERNEL
);
3791 return ERR_PTR(-ENOMEM
);
3796 static void debug_destroy(struct cgroup_subsys
*ss
, struct cgroup
*cont
)
3798 kfree(cont
->subsys
[debug_subsys_id
]);
3801 static u64
cgroup_refcount_read(struct cgroup
*cont
, struct cftype
*cft
)
3803 return atomic_read(&cont
->count
);
3806 static u64
debug_taskcount_read(struct cgroup
*cont
, struct cftype
*cft
)
3808 return cgroup_task_count(cont
);
3811 static u64
current_css_set_read(struct cgroup
*cont
, struct cftype
*cft
)
3813 return (u64
)(unsigned long)current
->cgroups
;
3816 static u64
current_css_set_refcount_read(struct cgroup
*cont
,
3822 count
= atomic_read(¤t
->cgroups
->refcount
);
3827 static u64
releasable_read(struct cgroup
*cgrp
, struct cftype
*cft
)
3829 return test_bit(CGRP_RELEASABLE
, &cgrp
->flags
);
3832 static struct cftype debug_files
[] = {
3834 .name
= "cgroup_refcount",
3835 .read_u64
= cgroup_refcount_read
,
3838 .name
= "taskcount",
3839 .read_u64
= debug_taskcount_read
,
3843 .name
= "current_css_set",
3844 .read_u64
= current_css_set_read
,
3848 .name
= "current_css_set_refcount",
3849 .read_u64
= current_css_set_refcount_read
,
3853 .name
= "releasable",
3854 .read_u64
= releasable_read
,
3858 static int debug_populate(struct cgroup_subsys
*ss
, struct cgroup
*cont
)
3860 return cgroup_add_files(cont
, ss
, debug_files
,
3861 ARRAY_SIZE(debug_files
));
3864 struct cgroup_subsys debug_subsys
= {
3866 .create
= debug_create
,
3867 .destroy
= debug_destroy
,
3868 .populate
= debug_populate
,
3869 .subsys_id
= debug_subsys_id
,
3871 #endif /* CONFIG_CGROUP_DEBUG */