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/errno.h>
28 #include <linux/kernel.h>
29 #include <linux/list.h>
31 #include <linux/mutex.h>
32 #include <linux/mount.h>
33 #include <linux/pagemap.h>
34 #include <linux/proc_fs.h>
35 #include <linux/rcupdate.h>
36 #include <linux/sched.h>
37 #include <linux/backing-dev.h>
38 #include <linux/seq_file.h>
39 #include <linux/slab.h>
40 #include <linux/magic.h>
41 #include <linux/spinlock.h>
42 #include <linux/string.h>
43 #include <linux/sort.h>
44 #include <linux/kmod.h>
45 #include <linux/delayacct.h>
46 #include <linux/cgroupstats.h>
47 #include <linux/hash.h>
49 #include <asm/atomic.h>
51 static DEFINE_MUTEX(cgroup_mutex
);
53 /* Generate an array of cgroup subsystem pointers */
54 #define SUBSYS(_x) &_x ## _subsys,
56 static struct cgroup_subsys
*subsys
[] = {
57 #include <linux/cgroup_subsys.h>
61 * A cgroupfs_root represents the root of a cgroup hierarchy,
62 * and may be associated with a superblock to form an active
65 struct cgroupfs_root
{
66 struct super_block
*sb
;
69 * The bitmask of subsystems intended to be attached to this
72 unsigned long subsys_bits
;
74 /* The bitmask of subsystems currently attached to this hierarchy */
75 unsigned long actual_subsys_bits
;
77 /* A list running through the attached subsystems */
78 struct list_head subsys_list
;
80 /* The root cgroup for this hierarchy */
81 struct cgroup top_cgroup
;
83 /* Tracks how many cgroups are currently defined in hierarchy.*/
84 int number_of_cgroups
;
86 /* A list running through the mounted hierarchies */
87 struct list_head root_list
;
89 /* Hierarchy-specific flags */
92 /* The path to use for release notifications. No locking
93 * between setting and use - so if userspace updates this
94 * while child cgroups exist, you could miss a
95 * notification. We ensure that it's always a valid
96 * NUL-terminated string */
97 char release_agent_path
[PATH_MAX
];
102 * The "rootnode" hierarchy is the "dummy hierarchy", reserved for the
103 * subsystems that are otherwise unattached - it never has more than a
104 * single cgroup, and all tasks are part of that cgroup.
106 static struct cgroupfs_root rootnode
;
108 /* The list of hierarchy roots */
110 static LIST_HEAD(roots
);
111 static int root_count
;
113 /* dummytop is a shorthand for the dummy hierarchy's top cgroup */
114 #define dummytop (&rootnode.top_cgroup)
116 /* This flag indicates whether tasks in the fork and exit paths should
117 * check for fork/exit handlers to call. This avoids us having to do
118 * extra work in the fork/exit path if none of the subsystems need to
121 static int need_forkexit_callback
;
123 /* convenient tests for these bits */
124 inline int cgroup_is_removed(const struct cgroup
*cgrp
)
126 return test_bit(CGRP_REMOVED
, &cgrp
->flags
);
129 /* bits in struct cgroupfs_root flags field */
131 ROOT_NOPREFIX
, /* mounted subsystems have no named prefix */
134 static int cgroup_is_releasable(const struct cgroup
*cgrp
)
137 (1 << CGRP_RELEASABLE
) |
138 (1 << CGRP_NOTIFY_ON_RELEASE
);
139 return (cgrp
->flags
& bits
) == bits
;
142 static int notify_on_release(const struct cgroup
*cgrp
)
144 return test_bit(CGRP_NOTIFY_ON_RELEASE
, &cgrp
->flags
);
148 * for_each_subsys() allows you to iterate on each subsystem attached to
149 * an active hierarchy
151 #define for_each_subsys(_root, _ss) \
152 list_for_each_entry(_ss, &_root->subsys_list, sibling)
154 /* for_each_root() allows you to iterate across the active hierarchies */
155 #define for_each_root(_root) \
156 list_for_each_entry(_root, &roots, root_list)
158 /* the list of cgroups eligible for automatic release. Protected by
159 * release_list_lock */
160 static LIST_HEAD(release_list
);
161 static DEFINE_SPINLOCK(release_list_lock
);
162 static void cgroup_release_agent(struct work_struct
*work
);
163 static DECLARE_WORK(release_agent_work
, cgroup_release_agent
);
164 static void check_for_release(struct cgroup
*cgrp
);
166 /* Link structure for associating css_set objects with cgroups */
167 struct cg_cgroup_link
{
169 * List running through cg_cgroup_links associated with a
170 * cgroup, anchored on cgroup->css_sets
172 struct list_head cgrp_link_list
;
174 * List running through cg_cgroup_links pointing at a
175 * single css_set object, anchored on css_set->cg_links
177 struct list_head cg_link_list
;
181 /* The default css_set - used by init and its children prior to any
182 * hierarchies being mounted. It contains a pointer to the root state
183 * for each subsystem. Also used to anchor the list of css_sets. Not
184 * reference-counted, to improve performance when child cgroups
185 * haven't been created.
188 static struct css_set init_css_set
;
189 static struct cg_cgroup_link init_css_set_link
;
191 /* css_set_lock protects the list of css_set objects, and the
192 * chain of tasks off each css_set. Nests outside task->alloc_lock
193 * due to cgroup_iter_start() */
194 static DEFINE_RWLOCK(css_set_lock
);
195 static int css_set_count
;
197 /* hash table for cgroup groups. This improves the performance to
198 * find an existing css_set */
199 #define CSS_SET_HASH_BITS 7
200 #define CSS_SET_TABLE_SIZE (1 << CSS_SET_HASH_BITS)
201 static struct hlist_head css_set_table
[CSS_SET_TABLE_SIZE
];
203 static struct hlist_head
*css_set_hash(struct cgroup_subsys_state
*css
[])
207 unsigned long tmp
= 0UL;
209 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++)
210 tmp
+= (unsigned long)css
[i
];
211 tmp
= (tmp
>> 16) ^ tmp
;
213 index
= hash_long(tmp
, CSS_SET_HASH_BITS
);
215 return &css_set_table
[index
];
218 /* We don't maintain the lists running through each css_set to its
219 * task until after the first call to cgroup_iter_start(). This
220 * reduces the fork()/exit() overhead for people who have cgroups
221 * compiled into their kernel but not actually in use */
222 static int use_task_css_set_links
;
224 /* When we create or destroy a css_set, the operation simply
225 * takes/releases a reference count on all the cgroups referenced
226 * by subsystems in this css_set. This can end up multiple-counting
227 * some cgroups, but that's OK - the ref-count is just a
228 * busy/not-busy indicator; ensuring that we only count each cgroup
229 * once would require taking a global lock to ensure that no
230 * subsystems moved between hierarchies while we were doing so.
232 * Possible TODO: decide at boot time based on the number of
233 * registered subsystems and the number of CPUs or NUMA nodes whether
234 * it's better for performance to ref-count every subsystem, or to
235 * take a global lock and only add one ref count to each hierarchy.
239 * unlink a css_set from the list and free it
241 static void unlink_css_set(struct css_set
*cg
)
243 write_lock(&css_set_lock
);
244 hlist_del(&cg
->hlist
);
247 while (!list_empty(&cg
->cg_links
)) {
248 struct cg_cgroup_link
*link
;
249 link
= list_entry(cg
->cg_links
.next
,
250 struct cg_cgroup_link
, cg_link_list
);
251 list_del(&link
->cg_link_list
);
252 list_del(&link
->cgrp_link_list
);
255 write_unlock(&css_set_lock
);
258 static void __release_css_set(struct kref
*k
, int taskexit
)
261 struct css_set
*cg
= container_of(k
, struct css_set
, ref
);
266 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
267 struct cgroup
*cgrp
= cg
->subsys
[i
]->cgroup
;
268 if (atomic_dec_and_test(&cgrp
->count
) &&
269 notify_on_release(cgrp
)) {
271 set_bit(CGRP_RELEASABLE
, &cgrp
->flags
);
272 check_for_release(cgrp
);
279 static void release_css_set(struct kref
*k
)
281 __release_css_set(k
, 0);
284 static void release_css_set_taskexit(struct kref
*k
)
286 __release_css_set(k
, 1);
290 * refcounted get/put for css_set objects
292 static inline void get_css_set(struct css_set
*cg
)
297 static inline void put_css_set(struct css_set
*cg
)
299 kref_put(&cg
->ref
, release_css_set
);
302 static inline void put_css_set_taskexit(struct css_set
*cg
)
304 kref_put(&cg
->ref
, release_css_set_taskexit
);
308 * find_existing_css_set() is a helper for
309 * find_css_set(), and checks to see whether an existing
310 * css_set is suitable.
312 * oldcg: the cgroup group that we're using before the cgroup
315 * cgrp: the cgroup that we're moving into
317 * template: location in which to build the desired set of subsystem
318 * state objects for the new cgroup group
320 static struct css_set
*find_existing_css_set(
321 struct css_set
*oldcg
,
323 struct cgroup_subsys_state
*template[])
326 struct cgroupfs_root
*root
= cgrp
->root
;
327 struct hlist_head
*hhead
;
328 struct hlist_node
*node
;
331 /* Built the set of subsystem state objects that we want to
332 * see in the new css_set */
333 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
334 if (root
->subsys_bits
& (1UL << i
)) {
335 /* Subsystem is in this hierarchy. So we want
336 * the subsystem state from the new
338 template[i
] = cgrp
->subsys
[i
];
340 /* Subsystem is not in this hierarchy, so we
341 * don't want to change the subsystem state */
342 template[i
] = oldcg
->subsys
[i
];
346 hhead
= css_set_hash(template);
347 hlist_for_each_entry(cg
, node
, hhead
, hlist
) {
348 if (!memcmp(template, cg
->subsys
, sizeof(cg
->subsys
))) {
349 /* All subsystems matched */
354 /* No existing cgroup group matched */
359 * allocate_cg_links() allocates "count" cg_cgroup_link structures
360 * and chains them on tmp through their cgrp_link_list fields. Returns 0 on
361 * success or a negative error
363 static int allocate_cg_links(int count
, struct list_head
*tmp
)
365 struct cg_cgroup_link
*link
;
368 for (i
= 0; i
< count
; i
++) {
369 link
= kmalloc(sizeof(*link
), GFP_KERNEL
);
371 while (!list_empty(tmp
)) {
372 link
= list_entry(tmp
->next
,
373 struct cg_cgroup_link
,
375 list_del(&link
->cgrp_link_list
);
380 list_add(&link
->cgrp_link_list
, tmp
);
385 static void free_cg_links(struct list_head
*tmp
)
387 while (!list_empty(tmp
)) {
388 struct cg_cgroup_link
*link
;
389 link
= list_entry(tmp
->next
,
390 struct cg_cgroup_link
,
392 list_del(&link
->cgrp_link_list
);
398 * find_css_set() takes an existing cgroup group and a
399 * cgroup object, and returns a css_set object that's
400 * equivalent to the old group, but with the given cgroup
401 * substituted into the appropriate hierarchy. Must be called with
404 static struct css_set
*find_css_set(
405 struct css_set
*oldcg
, struct cgroup
*cgrp
)
408 struct cgroup_subsys_state
*template[CGROUP_SUBSYS_COUNT
];
411 struct list_head tmp_cg_links
;
412 struct cg_cgroup_link
*link
;
414 struct hlist_head
*hhead
;
416 /* First see if we already have a cgroup group that matches
418 write_lock(&css_set_lock
);
419 res
= find_existing_css_set(oldcg
, cgrp
, template);
422 write_unlock(&css_set_lock
);
427 res
= kmalloc(sizeof(*res
), GFP_KERNEL
);
431 /* Allocate all the cg_cgroup_link objects that we'll need */
432 if (allocate_cg_links(root_count
, &tmp_cg_links
) < 0) {
437 kref_init(&res
->ref
);
438 INIT_LIST_HEAD(&res
->cg_links
);
439 INIT_LIST_HEAD(&res
->tasks
);
440 INIT_HLIST_NODE(&res
->hlist
);
442 /* Copy the set of subsystem state objects generated in
443 * find_existing_css_set() */
444 memcpy(res
->subsys
, template, sizeof(res
->subsys
));
446 write_lock(&css_set_lock
);
447 /* Add reference counts and links from the new css_set. */
448 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
449 struct cgroup
*cgrp
= res
->subsys
[i
]->cgroup
;
450 struct cgroup_subsys
*ss
= subsys
[i
];
451 atomic_inc(&cgrp
->count
);
453 * We want to add a link once per cgroup, so we
454 * only do it for the first subsystem in each
457 if (ss
->root
->subsys_list
.next
== &ss
->sibling
) {
458 BUG_ON(list_empty(&tmp_cg_links
));
459 link
= list_entry(tmp_cg_links
.next
,
460 struct cg_cgroup_link
,
462 list_del(&link
->cgrp_link_list
);
463 list_add(&link
->cgrp_link_list
, &cgrp
->css_sets
);
465 list_add(&link
->cg_link_list
, &res
->cg_links
);
468 if (list_empty(&rootnode
.subsys_list
)) {
469 link
= list_entry(tmp_cg_links
.next
,
470 struct cg_cgroup_link
,
472 list_del(&link
->cgrp_link_list
);
473 list_add(&link
->cgrp_link_list
, &dummytop
->css_sets
);
475 list_add(&link
->cg_link_list
, &res
->cg_links
);
478 BUG_ON(!list_empty(&tmp_cg_links
));
480 /* Link this cgroup group into the list */
481 list_add(&res
->list
, &init_css_set
.list
);
484 /* Add this cgroup group to the hash table */
485 hhead
= css_set_hash(res
->subsys
);
486 hlist_add_head(&res
->hlist
, hhead
);
488 write_unlock(&css_set_lock
);
494 * There is one global cgroup mutex. We also require taking
495 * task_lock() when dereferencing a task's cgroup subsys pointers.
496 * See "The task_lock() exception", at the end of this comment.
498 * A task must hold cgroup_mutex to modify cgroups.
500 * Any task can increment and decrement the count field without lock.
501 * So in general, code holding cgroup_mutex can't rely on the count
502 * field not changing. However, if the count goes to zero, then only
503 * cgroup_attach_task() can increment it again. Because a count of zero
504 * means that no tasks are currently attached, therefore there is no
505 * way a task attached to that cgroup can fork (the other way to
506 * increment the count). So code holding cgroup_mutex can safely
507 * assume that if the count is zero, it will stay zero. Similarly, if
508 * a task holds cgroup_mutex on a cgroup with zero count, it
509 * knows that the cgroup won't be removed, as cgroup_rmdir()
512 * The cgroup_common_file_write handler for operations that modify
513 * the cgroup hierarchy holds cgroup_mutex across the entire operation,
514 * single threading all such cgroup modifications across the system.
516 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
517 * (usually) take cgroup_mutex. These are the two most performance
518 * critical pieces of code here. The exception occurs on cgroup_exit(),
519 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
520 * is taken, and if the cgroup count is zero, a usermode call made
521 * to the release agent with the name of the cgroup (path relative to
522 * the root of cgroup file system) as the argument.
524 * A cgroup can only be deleted if both its 'count' of using tasks
525 * is zero, and its list of 'children' cgroups is empty. Since all
526 * tasks in the system use _some_ cgroup, and since there is always at
527 * least one task in the system (init, pid == 1), therefore, top_cgroup
528 * always has either children cgroups and/or using tasks. So we don't
529 * need a special hack to ensure that top_cgroup cannot be deleted.
531 * The task_lock() exception
533 * The need for this exception arises from the action of
534 * cgroup_attach_task(), which overwrites one tasks cgroup pointer with
535 * another. It does so using cgroup_mutex, however there are
536 * several performance critical places that need to reference
537 * task->cgroup without the expense of grabbing a system global
538 * mutex. Therefore except as noted below, when dereferencing or, as
539 * in cgroup_attach_task(), modifying a task'ss cgroup pointer we use
540 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
541 * the task_struct routinely used for such matters.
543 * P.S. One more locking exception. RCU is used to guard the
544 * update of a tasks cgroup pointer by cgroup_attach_task()
548 * cgroup_lock - lock out any changes to cgroup structures
551 void cgroup_lock(void)
553 mutex_lock(&cgroup_mutex
);
557 * cgroup_unlock - release lock on cgroup changes
559 * Undo the lock taken in a previous cgroup_lock() call.
561 void cgroup_unlock(void)
563 mutex_unlock(&cgroup_mutex
);
567 * A couple of forward declarations required, due to cyclic reference loop:
568 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
569 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
573 static int cgroup_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
);
574 static int cgroup_rmdir(struct inode
*unused_dir
, struct dentry
*dentry
);
575 static int cgroup_populate_dir(struct cgroup
*cgrp
);
576 static struct inode_operations cgroup_dir_inode_operations
;
577 static struct file_operations proc_cgroupstats_operations
;
579 static struct backing_dev_info cgroup_backing_dev_info
= {
580 .capabilities
= BDI_CAP_NO_ACCT_DIRTY
| BDI_CAP_NO_WRITEBACK
,
583 static struct inode
*cgroup_new_inode(mode_t mode
, struct super_block
*sb
)
585 struct inode
*inode
= new_inode(sb
);
588 inode
->i_mode
= mode
;
589 inode
->i_uid
= current
->fsuid
;
590 inode
->i_gid
= current
->fsgid
;
592 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
593 inode
->i_mapping
->backing_dev_info
= &cgroup_backing_dev_info
;
599 * Call subsys's pre_destroy handler.
600 * This is called before css refcnt check.
602 static void cgroup_call_pre_destroy(struct cgroup
*cgrp
)
604 struct cgroup_subsys
*ss
;
605 for_each_subsys(cgrp
->root
, ss
)
606 if (ss
->pre_destroy
&& cgrp
->subsys
[ss
->subsys_id
])
607 ss
->pre_destroy(ss
, cgrp
);
611 static void cgroup_diput(struct dentry
*dentry
, struct inode
*inode
)
613 /* is dentry a directory ? if so, kfree() associated cgroup */
614 if (S_ISDIR(inode
->i_mode
)) {
615 struct cgroup
*cgrp
= dentry
->d_fsdata
;
616 struct cgroup_subsys
*ss
;
617 BUG_ON(!(cgroup_is_removed(cgrp
)));
618 /* It's possible for external users to be holding css
619 * reference counts on a cgroup; css_put() needs to
620 * be able to access the cgroup after decrementing
621 * the reference count in order to know if it needs to
622 * queue the cgroup to be handled by the release
626 mutex_lock(&cgroup_mutex
);
628 * Release the subsystem state objects.
630 for_each_subsys(cgrp
->root
, ss
) {
631 if (cgrp
->subsys
[ss
->subsys_id
])
632 ss
->destroy(ss
, cgrp
);
635 cgrp
->root
->number_of_cgroups
--;
636 mutex_unlock(&cgroup_mutex
);
638 /* Drop the active superblock reference that we took when we
639 * created the cgroup */
640 deactivate_super(cgrp
->root
->sb
);
647 static void remove_dir(struct dentry
*d
)
649 struct dentry
*parent
= dget(d
->d_parent
);
652 simple_rmdir(parent
->d_inode
, d
);
656 static void cgroup_clear_directory(struct dentry
*dentry
)
658 struct list_head
*node
;
660 BUG_ON(!mutex_is_locked(&dentry
->d_inode
->i_mutex
));
661 spin_lock(&dcache_lock
);
662 node
= dentry
->d_subdirs
.next
;
663 while (node
!= &dentry
->d_subdirs
) {
664 struct dentry
*d
= list_entry(node
, struct dentry
, d_u
.d_child
);
667 /* This should never be called on a cgroup
668 * directory with child cgroups */
669 BUG_ON(d
->d_inode
->i_mode
& S_IFDIR
);
671 spin_unlock(&dcache_lock
);
673 simple_unlink(dentry
->d_inode
, d
);
675 spin_lock(&dcache_lock
);
677 node
= dentry
->d_subdirs
.next
;
679 spin_unlock(&dcache_lock
);
683 * NOTE : the dentry must have been dget()'ed
685 static void cgroup_d_remove_dir(struct dentry
*dentry
)
687 cgroup_clear_directory(dentry
);
689 spin_lock(&dcache_lock
);
690 list_del_init(&dentry
->d_u
.d_child
);
691 spin_unlock(&dcache_lock
);
695 static int rebind_subsystems(struct cgroupfs_root
*root
,
696 unsigned long final_bits
)
698 unsigned long added_bits
, removed_bits
;
699 struct cgroup
*cgrp
= &root
->top_cgroup
;
702 removed_bits
= root
->actual_subsys_bits
& ~final_bits
;
703 added_bits
= final_bits
& ~root
->actual_subsys_bits
;
704 /* Check that any added subsystems are currently free */
705 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
706 unsigned long bit
= 1UL << i
;
707 struct cgroup_subsys
*ss
= subsys
[i
];
708 if (!(bit
& added_bits
))
710 if (ss
->root
!= &rootnode
) {
711 /* Subsystem isn't free */
716 /* Currently we don't handle adding/removing subsystems when
717 * any child cgroups exist. This is theoretically supportable
718 * but involves complex error handling, so it's being left until
720 if (!list_empty(&cgrp
->children
))
723 /* Process each subsystem */
724 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
725 struct cgroup_subsys
*ss
= subsys
[i
];
726 unsigned long bit
= 1UL << i
;
727 if (bit
& added_bits
) {
728 /* We're binding this subsystem to this hierarchy */
729 BUG_ON(cgrp
->subsys
[i
]);
730 BUG_ON(!dummytop
->subsys
[i
]);
731 BUG_ON(dummytop
->subsys
[i
]->cgroup
!= dummytop
);
732 cgrp
->subsys
[i
] = dummytop
->subsys
[i
];
733 cgrp
->subsys
[i
]->cgroup
= cgrp
;
734 list_add(&ss
->sibling
, &root
->subsys_list
);
735 rcu_assign_pointer(ss
->root
, root
);
739 } else if (bit
& removed_bits
) {
740 /* We're removing this subsystem */
741 BUG_ON(cgrp
->subsys
[i
] != dummytop
->subsys
[i
]);
742 BUG_ON(cgrp
->subsys
[i
]->cgroup
!= cgrp
);
744 ss
->bind(ss
, dummytop
);
745 dummytop
->subsys
[i
]->cgroup
= dummytop
;
746 cgrp
->subsys
[i
] = NULL
;
747 rcu_assign_pointer(subsys
[i
]->root
, &rootnode
);
748 list_del(&ss
->sibling
);
749 } else if (bit
& final_bits
) {
750 /* Subsystem state should already exist */
751 BUG_ON(!cgrp
->subsys
[i
]);
753 /* Subsystem state shouldn't exist */
754 BUG_ON(cgrp
->subsys
[i
]);
757 root
->subsys_bits
= root
->actual_subsys_bits
= final_bits
;
763 static int cgroup_show_options(struct seq_file
*seq
, struct vfsmount
*vfs
)
765 struct cgroupfs_root
*root
= vfs
->mnt_sb
->s_fs_info
;
766 struct cgroup_subsys
*ss
;
768 mutex_lock(&cgroup_mutex
);
769 for_each_subsys(root
, ss
)
770 seq_printf(seq
, ",%s", ss
->name
);
771 if (test_bit(ROOT_NOPREFIX
, &root
->flags
))
772 seq_puts(seq
, ",noprefix");
773 if (strlen(root
->release_agent_path
))
774 seq_printf(seq
, ",release_agent=%s", root
->release_agent_path
);
775 mutex_unlock(&cgroup_mutex
);
779 struct cgroup_sb_opts
{
780 unsigned long subsys_bits
;
785 /* Convert a hierarchy specifier into a bitmask of subsystems and
787 static int parse_cgroupfs_options(char *data
,
788 struct cgroup_sb_opts
*opts
)
790 char *token
, *o
= data
?: "all";
792 opts
->subsys_bits
= 0;
794 opts
->release_agent
= NULL
;
796 while ((token
= strsep(&o
, ",")) != NULL
) {
799 if (!strcmp(token
, "all")) {
800 /* Add all non-disabled subsystems */
802 opts
->subsys_bits
= 0;
803 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
804 struct cgroup_subsys
*ss
= subsys
[i
];
806 opts
->subsys_bits
|= 1ul << i
;
808 } else if (!strcmp(token
, "noprefix")) {
809 set_bit(ROOT_NOPREFIX
, &opts
->flags
);
810 } else if (!strncmp(token
, "release_agent=", 14)) {
811 /* Specifying two release agents is forbidden */
812 if (opts
->release_agent
)
814 opts
->release_agent
= kzalloc(PATH_MAX
, GFP_KERNEL
);
815 if (!opts
->release_agent
)
817 strncpy(opts
->release_agent
, token
+ 14, PATH_MAX
- 1);
818 opts
->release_agent
[PATH_MAX
- 1] = 0;
820 struct cgroup_subsys
*ss
;
822 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
824 if (!strcmp(token
, ss
->name
)) {
826 set_bit(i
, &opts
->subsys_bits
);
830 if (i
== CGROUP_SUBSYS_COUNT
)
835 /* We can't have an empty hierarchy */
836 if (!opts
->subsys_bits
)
842 static int cgroup_remount(struct super_block
*sb
, int *flags
, char *data
)
845 struct cgroupfs_root
*root
= sb
->s_fs_info
;
846 struct cgroup
*cgrp
= &root
->top_cgroup
;
847 struct cgroup_sb_opts opts
;
849 mutex_lock(&cgrp
->dentry
->d_inode
->i_mutex
);
850 mutex_lock(&cgroup_mutex
);
852 /* See what subsystems are wanted */
853 ret
= parse_cgroupfs_options(data
, &opts
);
857 /* Don't allow flags to change at remount */
858 if (opts
.flags
!= root
->flags
) {
863 ret
= rebind_subsystems(root
, opts
.subsys_bits
);
865 /* (re)populate subsystem files */
867 cgroup_populate_dir(cgrp
);
869 if (opts
.release_agent
)
870 strcpy(root
->release_agent_path
, opts
.release_agent
);
872 if (opts
.release_agent
)
873 kfree(opts
.release_agent
);
874 mutex_unlock(&cgroup_mutex
);
875 mutex_unlock(&cgrp
->dentry
->d_inode
->i_mutex
);
879 static struct super_operations cgroup_ops
= {
880 .statfs
= simple_statfs
,
881 .drop_inode
= generic_delete_inode
,
882 .show_options
= cgroup_show_options
,
883 .remount_fs
= cgroup_remount
,
886 static void init_cgroup_root(struct cgroupfs_root
*root
)
888 struct cgroup
*cgrp
= &root
->top_cgroup
;
889 INIT_LIST_HEAD(&root
->subsys_list
);
890 INIT_LIST_HEAD(&root
->root_list
);
891 root
->number_of_cgroups
= 1;
893 cgrp
->top_cgroup
= cgrp
;
894 INIT_LIST_HEAD(&cgrp
->sibling
);
895 INIT_LIST_HEAD(&cgrp
->children
);
896 INIT_LIST_HEAD(&cgrp
->css_sets
);
897 INIT_LIST_HEAD(&cgrp
->release_list
);
900 static int cgroup_test_super(struct super_block
*sb
, void *data
)
902 struct cgroupfs_root
*new = data
;
903 struct cgroupfs_root
*root
= sb
->s_fs_info
;
905 /* First check subsystems */
906 if (new->subsys_bits
!= root
->subsys_bits
)
909 /* Next check flags */
910 if (new->flags
!= root
->flags
)
916 static int cgroup_set_super(struct super_block
*sb
, void *data
)
919 struct cgroupfs_root
*root
= data
;
921 ret
= set_anon_super(sb
, NULL
);
925 sb
->s_fs_info
= root
;
928 sb
->s_blocksize
= PAGE_CACHE_SIZE
;
929 sb
->s_blocksize_bits
= PAGE_CACHE_SHIFT
;
930 sb
->s_magic
= CGROUP_SUPER_MAGIC
;
931 sb
->s_op
= &cgroup_ops
;
936 static int cgroup_get_rootdir(struct super_block
*sb
)
938 struct inode
*inode
=
939 cgroup_new_inode(S_IFDIR
| S_IRUGO
| S_IXUGO
| S_IWUSR
, sb
);
940 struct dentry
*dentry
;
945 inode
->i_fop
= &simple_dir_operations
;
946 inode
->i_op
= &cgroup_dir_inode_operations
;
947 /* directories start off with i_nlink == 2 (for "." entry) */
949 dentry
= d_alloc_root(inode
);
958 static int cgroup_get_sb(struct file_system_type
*fs_type
,
959 int flags
, const char *unused_dev_name
,
960 void *data
, struct vfsmount
*mnt
)
962 struct cgroup_sb_opts opts
;
964 struct super_block
*sb
;
965 struct cgroupfs_root
*root
;
966 struct list_head tmp_cg_links
, *l
;
967 INIT_LIST_HEAD(&tmp_cg_links
);
969 /* First find the desired set of subsystems */
970 ret
= parse_cgroupfs_options(data
, &opts
);
972 if (opts
.release_agent
)
973 kfree(opts
.release_agent
);
977 root
= kzalloc(sizeof(*root
), GFP_KERNEL
);
979 if (opts
.release_agent
)
980 kfree(opts
.release_agent
);
984 init_cgroup_root(root
);
985 root
->subsys_bits
= opts
.subsys_bits
;
986 root
->flags
= opts
.flags
;
987 if (opts
.release_agent
) {
988 strcpy(root
->release_agent_path
, opts
.release_agent
);
989 kfree(opts
.release_agent
);
992 sb
= sget(fs_type
, cgroup_test_super
, cgroup_set_super
, root
);
999 if (sb
->s_fs_info
!= root
) {
1000 /* Reusing an existing superblock */
1001 BUG_ON(sb
->s_root
== NULL
);
1005 /* New superblock */
1006 struct cgroup
*cgrp
= &root
->top_cgroup
;
1007 struct inode
*inode
;
1009 BUG_ON(sb
->s_root
!= NULL
);
1011 ret
= cgroup_get_rootdir(sb
);
1013 goto drop_new_super
;
1014 inode
= sb
->s_root
->d_inode
;
1016 mutex_lock(&inode
->i_mutex
);
1017 mutex_lock(&cgroup_mutex
);
1020 * We're accessing css_set_count without locking
1021 * css_set_lock here, but that's OK - it can only be
1022 * increased by someone holding cgroup_lock, and
1023 * that's us. The worst that can happen is that we
1024 * have some link structures left over
1026 ret
= allocate_cg_links(css_set_count
, &tmp_cg_links
);
1028 mutex_unlock(&cgroup_mutex
);
1029 mutex_unlock(&inode
->i_mutex
);
1030 goto drop_new_super
;
1033 ret
= rebind_subsystems(root
, root
->subsys_bits
);
1034 if (ret
== -EBUSY
) {
1035 mutex_unlock(&cgroup_mutex
);
1036 mutex_unlock(&inode
->i_mutex
);
1037 goto drop_new_super
;
1040 /* EBUSY should be the only error here */
1043 list_add(&root
->root_list
, &roots
);
1046 sb
->s_root
->d_fsdata
= &root
->top_cgroup
;
1047 root
->top_cgroup
.dentry
= sb
->s_root
;
1049 /* Link the top cgroup in this hierarchy into all
1050 * the css_set objects */
1051 write_lock(&css_set_lock
);
1052 l
= &init_css_set
.list
;
1055 struct cg_cgroup_link
*link
;
1056 cg
= list_entry(l
, struct css_set
, list
);
1057 BUG_ON(list_empty(&tmp_cg_links
));
1058 link
= list_entry(tmp_cg_links
.next
,
1059 struct cg_cgroup_link
,
1061 list_del(&link
->cgrp_link_list
);
1063 list_add(&link
->cgrp_link_list
,
1064 &root
->top_cgroup
.css_sets
);
1065 list_add(&link
->cg_link_list
, &cg
->cg_links
);
1067 } while (l
!= &init_css_set
.list
);
1068 write_unlock(&css_set_lock
);
1070 free_cg_links(&tmp_cg_links
);
1072 BUG_ON(!list_empty(&cgrp
->sibling
));
1073 BUG_ON(!list_empty(&cgrp
->children
));
1074 BUG_ON(root
->number_of_cgroups
!= 1);
1076 cgroup_populate_dir(cgrp
);
1077 mutex_unlock(&inode
->i_mutex
);
1078 mutex_unlock(&cgroup_mutex
);
1081 return simple_set_mnt(mnt
, sb
);
1084 up_write(&sb
->s_umount
);
1085 deactivate_super(sb
);
1086 free_cg_links(&tmp_cg_links
);
1090 static void cgroup_kill_sb(struct super_block
*sb
) {
1091 struct cgroupfs_root
*root
= sb
->s_fs_info
;
1092 struct cgroup
*cgrp
= &root
->top_cgroup
;
1097 BUG_ON(root
->number_of_cgroups
!= 1);
1098 BUG_ON(!list_empty(&cgrp
->children
));
1099 BUG_ON(!list_empty(&cgrp
->sibling
));
1101 mutex_lock(&cgroup_mutex
);
1103 /* Rebind all subsystems back to the default hierarchy */
1104 ret
= rebind_subsystems(root
, 0);
1105 /* Shouldn't be able to fail ... */
1109 * Release all the links from css_sets to this hierarchy's
1112 write_lock(&css_set_lock
);
1113 while (!list_empty(&cgrp
->css_sets
)) {
1114 struct cg_cgroup_link
*link
;
1115 link
= list_entry(cgrp
->css_sets
.next
,
1116 struct cg_cgroup_link
, cgrp_link_list
);
1117 list_del(&link
->cg_link_list
);
1118 list_del(&link
->cgrp_link_list
);
1121 write_unlock(&css_set_lock
);
1123 if (!list_empty(&root
->root_list
)) {
1124 list_del(&root
->root_list
);
1127 mutex_unlock(&cgroup_mutex
);
1130 kill_litter_super(sb
);
1133 static struct file_system_type cgroup_fs_type
= {
1135 .get_sb
= cgroup_get_sb
,
1136 .kill_sb
= cgroup_kill_sb
,
1139 static inline struct cgroup
*__d_cgrp(struct dentry
*dentry
)
1141 return dentry
->d_fsdata
;
1144 static inline struct cftype
*__d_cft(struct dentry
*dentry
)
1146 return dentry
->d_fsdata
;
1150 * cgroup_path - generate the path of a cgroup
1151 * @cgrp: the cgroup in question
1152 * @buf: the buffer to write the path into
1153 * @buflen: the length of the buffer
1155 * Called with cgroup_mutex held. Writes path of cgroup into buf.
1156 * Returns 0 on success, -errno on error.
1158 int cgroup_path(const struct cgroup
*cgrp
, char *buf
, int buflen
)
1162 if (cgrp
== dummytop
) {
1164 * Inactive subsystems have no dentry for their root
1171 start
= buf
+ buflen
;
1175 int len
= cgrp
->dentry
->d_name
.len
;
1176 if ((start
-= len
) < buf
)
1177 return -ENAMETOOLONG
;
1178 memcpy(start
, cgrp
->dentry
->d_name
.name
, len
);
1179 cgrp
= cgrp
->parent
;
1185 return -ENAMETOOLONG
;
1188 memmove(buf
, start
, buf
+ buflen
- start
);
1193 * Return the first subsystem attached to a cgroup's hierarchy, and
1197 static void get_first_subsys(const struct cgroup
*cgrp
,
1198 struct cgroup_subsys_state
**css
, int *subsys_id
)
1200 const struct cgroupfs_root
*root
= cgrp
->root
;
1201 const struct cgroup_subsys
*test_ss
;
1202 BUG_ON(list_empty(&root
->subsys_list
));
1203 test_ss
= list_entry(root
->subsys_list
.next
,
1204 struct cgroup_subsys
, sibling
);
1206 *css
= cgrp
->subsys
[test_ss
->subsys_id
];
1210 *subsys_id
= test_ss
->subsys_id
;
1214 * cgroup_attach_task - attach task 'tsk' to cgroup 'cgrp'
1215 * @cgrp: the cgroup the task is attaching to
1216 * @tsk: the task to be attached
1218 * Call holding cgroup_mutex. May take task_lock of
1219 * the task 'tsk' during call.
1221 int cgroup_attach_task(struct cgroup
*cgrp
, struct task_struct
*tsk
)
1224 struct cgroup_subsys
*ss
;
1225 struct cgroup
*oldcgrp
;
1226 struct css_set
*cg
= tsk
->cgroups
;
1227 struct css_set
*newcg
;
1228 struct cgroupfs_root
*root
= cgrp
->root
;
1231 get_first_subsys(cgrp
, NULL
, &subsys_id
);
1233 /* Nothing to do if the task is already in that cgroup */
1234 oldcgrp
= task_cgroup(tsk
, subsys_id
);
1235 if (cgrp
== oldcgrp
)
1238 for_each_subsys(root
, ss
) {
1239 if (ss
->can_attach
) {
1240 retval
= ss
->can_attach(ss
, cgrp
, tsk
);
1247 * Locate or allocate a new css_set for this task,
1248 * based on its final set of cgroups
1250 newcg
= find_css_set(cg
, cgrp
);
1255 if (tsk
->flags
& PF_EXITING
) {
1260 rcu_assign_pointer(tsk
->cgroups
, newcg
);
1263 /* Update the css_set linked lists if we're using them */
1264 write_lock(&css_set_lock
);
1265 if (!list_empty(&tsk
->cg_list
)) {
1266 list_del(&tsk
->cg_list
);
1267 list_add(&tsk
->cg_list
, &newcg
->tasks
);
1269 write_unlock(&css_set_lock
);
1271 for_each_subsys(root
, ss
) {
1273 ss
->attach(ss
, cgrp
, oldcgrp
, tsk
);
1275 set_bit(CGRP_RELEASABLE
, &oldcgrp
->flags
);
1282 * Attach task with pid 'pid' to cgroup 'cgrp'. Call with
1283 * cgroup_mutex, may take task_lock of task
1285 static int attach_task_by_pid(struct cgroup
*cgrp
, char *pidbuf
)
1288 struct task_struct
*tsk
;
1291 if (sscanf(pidbuf
, "%d", &pid
) != 1)
1296 tsk
= find_task_by_vpid(pid
);
1297 if (!tsk
|| tsk
->flags
& PF_EXITING
) {
1301 get_task_struct(tsk
);
1304 if ((current
->euid
) && (current
->euid
!= tsk
->uid
)
1305 && (current
->euid
!= tsk
->suid
)) {
1306 put_task_struct(tsk
);
1311 get_task_struct(tsk
);
1314 ret
= cgroup_attach_task(cgrp
, tsk
);
1315 put_task_struct(tsk
);
1319 /* The various types of files and directories in a cgroup file system */
1320 enum cgroup_filetype
{
1324 FILE_NOTIFY_ON_RELEASE
,
1328 static ssize_t
cgroup_write_X64(struct cgroup
*cgrp
, struct cftype
*cft
,
1330 const char __user
*userbuf
,
1331 size_t nbytes
, loff_t
*unused_ppos
)
1339 if (nbytes
>= sizeof(buffer
))
1341 if (copy_from_user(buffer
, userbuf
, nbytes
))
1344 buffer
[nbytes
] = 0; /* nul-terminate */
1346 if (cft
->write_u64
) {
1347 u64 val
= simple_strtoull(buffer
, &end
, 0);
1350 retval
= cft
->write_u64(cgrp
, cft
, val
);
1352 s64 val
= simple_strtoll(buffer
, &end
, 0);
1355 retval
= cft
->write_s64(cgrp
, cft
, val
);
1362 static ssize_t
cgroup_common_file_write(struct cgroup
*cgrp
,
1365 const char __user
*userbuf
,
1366 size_t nbytes
, loff_t
*unused_ppos
)
1368 enum cgroup_filetype type
= cft
->private;
1372 if (nbytes
>= PATH_MAX
)
1375 /* +1 for nul-terminator */
1376 buffer
= kmalloc(nbytes
+ 1, GFP_KERNEL
);
1380 if (copy_from_user(buffer
, userbuf
, nbytes
)) {
1384 buffer
[nbytes
] = 0; /* nul-terminate */
1385 strstrip(buffer
); /* strip -just- trailing whitespace */
1387 mutex_lock(&cgroup_mutex
);
1390 * This was already checked for in cgroup_file_write(), but
1391 * check again now we're holding cgroup_mutex.
1393 if (cgroup_is_removed(cgrp
)) {
1400 retval
= attach_task_by_pid(cgrp
, buffer
);
1402 case FILE_NOTIFY_ON_RELEASE
:
1403 clear_bit(CGRP_RELEASABLE
, &cgrp
->flags
);
1404 if (simple_strtoul(buffer
, NULL
, 10) != 0)
1405 set_bit(CGRP_NOTIFY_ON_RELEASE
, &cgrp
->flags
);
1407 clear_bit(CGRP_NOTIFY_ON_RELEASE
, &cgrp
->flags
);
1409 case FILE_RELEASE_AGENT
:
1410 BUILD_BUG_ON(sizeof(cgrp
->root
->release_agent_path
) < PATH_MAX
);
1411 strcpy(cgrp
->root
->release_agent_path
, buffer
);
1421 mutex_unlock(&cgroup_mutex
);
1427 static ssize_t
cgroup_file_write(struct file
*file
, const char __user
*buf
,
1428 size_t nbytes
, loff_t
*ppos
)
1430 struct cftype
*cft
= __d_cft(file
->f_dentry
);
1431 struct cgroup
*cgrp
= __d_cgrp(file
->f_dentry
->d_parent
);
1433 if (!cft
|| cgroup_is_removed(cgrp
))
1436 return cft
->write(cgrp
, cft
, file
, buf
, nbytes
, ppos
);
1437 if (cft
->write_u64
|| cft
->write_s64
)
1438 return cgroup_write_X64(cgrp
, cft
, file
, buf
, nbytes
, ppos
);
1440 int ret
= cft
->trigger(cgrp
, (unsigned int)cft
->private);
1441 return ret
? ret
: nbytes
;
1446 static ssize_t
cgroup_read_u64(struct cgroup
*cgrp
, struct cftype
*cft
,
1448 char __user
*buf
, size_t nbytes
,
1452 u64 val
= cft
->read_u64(cgrp
, cft
);
1453 int len
= sprintf(tmp
, "%llu\n", (unsigned long long) val
);
1455 return simple_read_from_buffer(buf
, nbytes
, ppos
, tmp
, len
);
1458 static ssize_t
cgroup_read_s64(struct cgroup
*cgrp
, struct cftype
*cft
,
1460 char __user
*buf
, size_t nbytes
,
1464 s64 val
= cft
->read_s64(cgrp
, cft
);
1465 int len
= sprintf(tmp
, "%lld\n", (long long) val
);
1467 return simple_read_from_buffer(buf
, nbytes
, ppos
, tmp
, len
);
1470 static ssize_t
cgroup_common_file_read(struct cgroup
*cgrp
,
1474 size_t nbytes
, loff_t
*ppos
)
1476 enum cgroup_filetype type
= cft
->private;
1481 if (!(page
= (char *)__get_free_page(GFP_KERNEL
)))
1487 case FILE_RELEASE_AGENT
:
1489 struct cgroupfs_root
*root
;
1491 mutex_lock(&cgroup_mutex
);
1493 n
= strnlen(root
->release_agent_path
,
1494 sizeof(root
->release_agent_path
));
1495 n
= min(n
, (size_t) PAGE_SIZE
);
1496 strncpy(s
, root
->release_agent_path
, n
);
1497 mutex_unlock(&cgroup_mutex
);
1507 retval
= simple_read_from_buffer(buf
, nbytes
, ppos
, page
, s
- page
);
1509 free_page((unsigned long)page
);
1513 static ssize_t
cgroup_file_read(struct file
*file
, char __user
*buf
,
1514 size_t nbytes
, loff_t
*ppos
)
1516 struct cftype
*cft
= __d_cft(file
->f_dentry
);
1517 struct cgroup
*cgrp
= __d_cgrp(file
->f_dentry
->d_parent
);
1519 if (!cft
|| cgroup_is_removed(cgrp
))
1523 return cft
->read(cgrp
, cft
, file
, buf
, nbytes
, ppos
);
1525 return cgroup_read_u64(cgrp
, cft
, file
, buf
, nbytes
, ppos
);
1527 return cgroup_read_s64(cgrp
, cft
, file
, buf
, nbytes
, ppos
);
1532 * seqfile ops/methods for returning structured data. Currently just
1533 * supports string->u64 maps, but can be extended in future.
1536 struct cgroup_seqfile_state
{
1538 struct cgroup
*cgroup
;
1541 static int cgroup_map_add(struct cgroup_map_cb
*cb
, const char *key
, u64 value
)
1543 struct seq_file
*sf
= cb
->state
;
1544 return seq_printf(sf
, "%s %llu\n", key
, (unsigned long long)value
);
1547 static int cgroup_seqfile_show(struct seq_file
*m
, void *arg
)
1549 struct cgroup_seqfile_state
*state
= m
->private;
1550 struct cftype
*cft
= state
->cft
;
1551 struct cgroup_map_cb cb
= {
1552 .fill
= cgroup_map_add
,
1555 return cft
->read_map(state
->cgroup
, cft
, &cb
);
1558 int cgroup_seqfile_release(struct inode
*inode
, struct file
*file
)
1560 struct seq_file
*seq
= file
->private_data
;
1561 kfree(seq
->private);
1562 return single_release(inode
, file
);
1565 static struct file_operations cgroup_seqfile_operations
= {
1567 .llseek
= seq_lseek
,
1568 .release
= cgroup_seqfile_release
,
1571 static int cgroup_file_open(struct inode
*inode
, struct file
*file
)
1576 err
= generic_file_open(inode
, file
);
1580 cft
= __d_cft(file
->f_dentry
);
1583 if (cft
->read_map
) {
1584 struct cgroup_seqfile_state
*state
=
1585 kzalloc(sizeof(*state
), GFP_USER
);
1589 state
->cgroup
= __d_cgrp(file
->f_dentry
->d_parent
);
1590 file
->f_op
= &cgroup_seqfile_operations
;
1591 err
= single_open(file
, cgroup_seqfile_show
, state
);
1594 } else if (cft
->open
)
1595 err
= cft
->open(inode
, file
);
1602 static int cgroup_file_release(struct inode
*inode
, struct file
*file
)
1604 struct cftype
*cft
= __d_cft(file
->f_dentry
);
1606 return cft
->release(inode
, file
);
1611 * cgroup_rename - Only allow simple rename of directories in place.
1613 static int cgroup_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
1614 struct inode
*new_dir
, struct dentry
*new_dentry
)
1616 if (!S_ISDIR(old_dentry
->d_inode
->i_mode
))
1618 if (new_dentry
->d_inode
)
1620 if (old_dir
!= new_dir
)
1622 return simple_rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
1625 static struct file_operations cgroup_file_operations
= {
1626 .read
= cgroup_file_read
,
1627 .write
= cgroup_file_write
,
1628 .llseek
= generic_file_llseek
,
1629 .open
= cgroup_file_open
,
1630 .release
= cgroup_file_release
,
1633 static struct inode_operations cgroup_dir_inode_operations
= {
1634 .lookup
= simple_lookup
,
1635 .mkdir
= cgroup_mkdir
,
1636 .rmdir
= cgroup_rmdir
,
1637 .rename
= cgroup_rename
,
1640 static int cgroup_create_file(struct dentry
*dentry
, int mode
,
1641 struct super_block
*sb
)
1643 static struct dentry_operations cgroup_dops
= {
1644 .d_iput
= cgroup_diput
,
1647 struct inode
*inode
;
1651 if (dentry
->d_inode
)
1654 inode
= cgroup_new_inode(mode
, sb
);
1658 if (S_ISDIR(mode
)) {
1659 inode
->i_op
= &cgroup_dir_inode_operations
;
1660 inode
->i_fop
= &simple_dir_operations
;
1662 /* start off with i_nlink == 2 (for "." entry) */
1665 /* start with the directory inode held, so that we can
1666 * populate it without racing with another mkdir */
1667 mutex_lock_nested(&inode
->i_mutex
, I_MUTEX_CHILD
);
1668 } else if (S_ISREG(mode
)) {
1670 inode
->i_fop
= &cgroup_file_operations
;
1672 dentry
->d_op
= &cgroup_dops
;
1673 d_instantiate(dentry
, inode
);
1674 dget(dentry
); /* Extra count - pin the dentry in core */
1679 * cgroup_create_dir - create a directory for an object.
1680 * @cgrp: the cgroup we create the directory for. It must have a valid
1681 * ->parent field. And we are going to fill its ->dentry field.
1682 * @dentry: dentry of the new cgroup
1683 * @mode: mode to set on new directory.
1685 static int cgroup_create_dir(struct cgroup
*cgrp
, struct dentry
*dentry
,
1688 struct dentry
*parent
;
1691 parent
= cgrp
->parent
->dentry
;
1692 error
= cgroup_create_file(dentry
, S_IFDIR
| mode
, cgrp
->root
->sb
);
1694 dentry
->d_fsdata
= cgrp
;
1695 inc_nlink(parent
->d_inode
);
1696 cgrp
->dentry
= dentry
;
1704 int cgroup_add_file(struct cgroup
*cgrp
,
1705 struct cgroup_subsys
*subsys
,
1706 const struct cftype
*cft
)
1708 struct dentry
*dir
= cgrp
->dentry
;
1709 struct dentry
*dentry
;
1712 char name
[MAX_CGROUP_TYPE_NAMELEN
+ MAX_CFTYPE_NAME
+ 2] = { 0 };
1713 if (subsys
&& !test_bit(ROOT_NOPREFIX
, &cgrp
->root
->flags
)) {
1714 strcpy(name
, subsys
->name
);
1717 strcat(name
, cft
->name
);
1718 BUG_ON(!mutex_is_locked(&dir
->d_inode
->i_mutex
));
1719 dentry
= lookup_one_len(name
, dir
, strlen(name
));
1720 if (!IS_ERR(dentry
)) {
1721 error
= cgroup_create_file(dentry
, 0644 | S_IFREG
,
1724 dentry
->d_fsdata
= (void *)cft
;
1727 error
= PTR_ERR(dentry
);
1731 int cgroup_add_files(struct cgroup
*cgrp
,
1732 struct cgroup_subsys
*subsys
,
1733 const struct cftype cft
[],
1737 for (i
= 0; i
< count
; i
++) {
1738 err
= cgroup_add_file(cgrp
, subsys
, &cft
[i
]);
1746 * cgroup_task_count - count the number of tasks in a cgroup.
1747 * @cgrp: the cgroup in question
1749 * Return the number of tasks in the cgroup.
1751 int cgroup_task_count(const struct cgroup
*cgrp
)
1754 struct list_head
*l
;
1756 read_lock(&css_set_lock
);
1757 l
= cgrp
->css_sets
.next
;
1758 while (l
!= &cgrp
->css_sets
) {
1759 struct cg_cgroup_link
*link
=
1760 list_entry(l
, struct cg_cgroup_link
, cgrp_link_list
);
1761 count
+= atomic_read(&link
->cg
->ref
.refcount
);
1764 read_unlock(&css_set_lock
);
1769 * Advance a list_head iterator. The iterator should be positioned at
1770 * the start of a css_set
1772 static void cgroup_advance_iter(struct cgroup
*cgrp
,
1773 struct cgroup_iter
*it
)
1775 struct list_head
*l
= it
->cg_link
;
1776 struct cg_cgroup_link
*link
;
1779 /* Advance to the next non-empty css_set */
1782 if (l
== &cgrp
->css_sets
) {
1786 link
= list_entry(l
, struct cg_cgroup_link
, cgrp_link_list
);
1788 } while (list_empty(&cg
->tasks
));
1790 it
->task
= cg
->tasks
.next
;
1794 * To reduce the fork() overhead for systems that are not actually
1795 * using their cgroups capability, we don't maintain the lists running
1796 * through each css_set to its tasks until we see the list actually
1797 * used - in other words after the first call to cgroup_iter_start().
1799 * The tasklist_lock is not held here, as do_each_thread() and
1800 * while_each_thread() are protected by RCU.
1802 static void cgroup_enable_task_cg_lists(void)
1804 struct task_struct
*p
, *g
;
1805 write_lock(&css_set_lock
);
1806 use_task_css_set_links
= 1;
1807 do_each_thread(g
, p
) {
1810 * We should check if the process is exiting, otherwise
1811 * it will race with cgroup_exit() in that the list
1812 * entry won't be deleted though the process has exited.
1814 if (!(p
->flags
& PF_EXITING
) && list_empty(&p
->cg_list
))
1815 list_add(&p
->cg_list
, &p
->cgroups
->tasks
);
1817 } while_each_thread(g
, p
);
1818 write_unlock(&css_set_lock
);
1821 void cgroup_iter_start(struct cgroup
*cgrp
, struct cgroup_iter
*it
)
1824 * The first time anyone tries to iterate across a cgroup,
1825 * we need to enable the list linking each css_set to its
1826 * tasks, and fix up all existing tasks.
1828 if (!use_task_css_set_links
)
1829 cgroup_enable_task_cg_lists();
1831 read_lock(&css_set_lock
);
1832 it
->cg_link
= &cgrp
->css_sets
;
1833 cgroup_advance_iter(cgrp
, it
);
1836 struct task_struct
*cgroup_iter_next(struct cgroup
*cgrp
,
1837 struct cgroup_iter
*it
)
1839 struct task_struct
*res
;
1840 struct list_head
*l
= it
->task
;
1842 /* If the iterator cg is NULL, we have no tasks */
1845 res
= list_entry(l
, struct task_struct
, cg_list
);
1846 /* Advance iterator to find next entry */
1848 if (l
== &res
->cgroups
->tasks
) {
1849 /* We reached the end of this task list - move on to
1850 * the next cg_cgroup_link */
1851 cgroup_advance_iter(cgrp
, it
);
1858 void cgroup_iter_end(struct cgroup
*cgrp
, struct cgroup_iter
*it
)
1860 read_unlock(&css_set_lock
);
1863 static inline int started_after_time(struct task_struct
*t1
,
1864 struct timespec
*time
,
1865 struct task_struct
*t2
)
1867 int start_diff
= timespec_compare(&t1
->start_time
, time
);
1868 if (start_diff
> 0) {
1870 } else if (start_diff
< 0) {
1874 * Arbitrarily, if two processes started at the same
1875 * time, we'll say that the lower pointer value
1876 * started first. Note that t2 may have exited by now
1877 * so this may not be a valid pointer any longer, but
1878 * that's fine - it still serves to distinguish
1879 * between two tasks started (effectively) simultaneously.
1886 * This function is a callback from heap_insert() and is used to order
1888 * In this case we order the heap in descending task start time.
1890 static inline int started_after(void *p1
, void *p2
)
1892 struct task_struct
*t1
= p1
;
1893 struct task_struct
*t2
= p2
;
1894 return started_after_time(t1
, &t2
->start_time
, t2
);
1898 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
1899 * @scan: struct cgroup_scanner containing arguments for the scan
1901 * Arguments include pointers to callback functions test_task() and
1903 * Iterate through all the tasks in a cgroup, calling test_task() for each,
1904 * and if it returns true, call process_task() for it also.
1905 * The test_task pointer may be NULL, meaning always true (select all tasks).
1906 * Effectively duplicates cgroup_iter_{start,next,end}()
1907 * but does not lock css_set_lock for the call to process_task().
1908 * The struct cgroup_scanner may be embedded in any structure of the caller's
1910 * It is guaranteed that process_task() will act on every task that
1911 * is a member of the cgroup for the duration of this call. This
1912 * function may or may not call process_task() for tasks that exit
1913 * or move to a different cgroup during the call, or are forked or
1914 * move into the cgroup during the call.
1916 * Note that test_task() may be called with locks held, and may in some
1917 * situations be called multiple times for the same task, so it should
1919 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
1920 * pre-allocated and will be used for heap operations (and its "gt" member will
1921 * be overwritten), else a temporary heap will be used (allocation of which
1922 * may cause this function to fail).
1924 int cgroup_scan_tasks(struct cgroup_scanner
*scan
)
1927 struct cgroup_iter it
;
1928 struct task_struct
*p
, *dropped
;
1929 /* Never dereference latest_task, since it's not refcounted */
1930 struct task_struct
*latest_task
= NULL
;
1931 struct ptr_heap tmp_heap
;
1932 struct ptr_heap
*heap
;
1933 struct timespec latest_time
= { 0, 0 };
1936 /* The caller supplied our heap and pre-allocated its memory */
1938 heap
->gt
= &started_after
;
1940 /* We need to allocate our own heap memory */
1942 retval
= heap_init(heap
, PAGE_SIZE
, GFP_KERNEL
, &started_after
);
1944 /* cannot allocate the heap */
1950 * Scan tasks in the cgroup, using the scanner's "test_task" callback
1951 * to determine which are of interest, and using the scanner's
1952 * "process_task" callback to process any of them that need an update.
1953 * Since we don't want to hold any locks during the task updates,
1954 * gather tasks to be processed in a heap structure.
1955 * The heap is sorted by descending task start time.
1956 * If the statically-sized heap fills up, we overflow tasks that
1957 * started later, and in future iterations only consider tasks that
1958 * started after the latest task in the previous pass. This
1959 * guarantees forward progress and that we don't miss any tasks.
1962 cgroup_iter_start(scan
->cg
, &it
);
1963 while ((p
= cgroup_iter_next(scan
->cg
, &it
))) {
1965 * Only affect tasks that qualify per the caller's callback,
1966 * if he provided one
1968 if (scan
->test_task
&& !scan
->test_task(p
, scan
))
1971 * Only process tasks that started after the last task
1974 if (!started_after_time(p
, &latest_time
, latest_task
))
1976 dropped
= heap_insert(heap
, p
);
1977 if (dropped
== NULL
) {
1979 * The new task was inserted; the heap wasn't
1983 } else if (dropped
!= p
) {
1985 * The new task was inserted, and pushed out a
1989 put_task_struct(dropped
);
1992 * Else the new task was newer than anything already in
1993 * the heap and wasn't inserted
1996 cgroup_iter_end(scan
->cg
, &it
);
1999 for (i
= 0; i
< heap
->size
; i
++) {
2000 struct task_struct
*q
= heap
->ptrs
[i
];
2002 latest_time
= q
->start_time
;
2005 /* Process the task per the caller's callback */
2006 scan
->process_task(q
, scan
);
2010 * If we had to process any tasks at all, scan again
2011 * in case some of them were in the middle of forking
2012 * children that didn't get processed.
2013 * Not the most efficient way to do it, but it avoids
2014 * having to take callback_mutex in the fork path
2018 if (heap
== &tmp_heap
)
2019 heap_free(&tmp_heap
);
2024 * Stuff for reading the 'tasks' file.
2026 * Reading this file can return large amounts of data if a cgroup has
2027 * *lots* of attached tasks. So it may need several calls to read(),
2028 * but we cannot guarantee that the information we produce is correct
2029 * unless we produce it entirely atomically.
2031 * Upon tasks file open(), a struct ctr_struct is allocated, that
2032 * will have a pointer to an array (also allocated here). The struct
2033 * ctr_struct * is stored in file->private_data. Its resources will
2034 * be freed by release() when the file is closed. The array is used
2035 * to sprintf the PIDs and then used by read().
2043 * Load into 'pidarray' up to 'npids' of the tasks using cgroup
2044 * 'cgrp'. Return actual number of pids loaded. No need to
2045 * task_lock(p) when reading out p->cgroup, since we're in an RCU
2046 * read section, so the css_set can't go away, and is
2047 * immutable after creation.
2049 static int pid_array_load(pid_t
*pidarray
, int npids
, struct cgroup
*cgrp
)
2052 struct cgroup_iter it
;
2053 struct task_struct
*tsk
;
2054 cgroup_iter_start(cgrp
, &it
);
2055 while ((tsk
= cgroup_iter_next(cgrp
, &it
))) {
2056 if (unlikely(n
== npids
))
2058 pidarray
[n
++] = task_pid_vnr(tsk
);
2060 cgroup_iter_end(cgrp
, &it
);
2065 * cgroupstats_build - build and fill cgroupstats
2066 * @stats: cgroupstats to fill information into
2067 * @dentry: A dentry entry belonging to the cgroup for which stats have
2070 * Build and fill cgroupstats so that taskstats can export it to user
2073 int cgroupstats_build(struct cgroupstats
*stats
, struct dentry
*dentry
)
2076 struct cgroup
*cgrp
;
2077 struct cgroup_iter it
;
2078 struct task_struct
*tsk
;
2080 * Validate dentry by checking the superblock operations
2082 if (dentry
->d_sb
->s_op
!= &cgroup_ops
)
2086 cgrp
= dentry
->d_fsdata
;
2089 cgroup_iter_start(cgrp
, &it
);
2090 while ((tsk
= cgroup_iter_next(cgrp
, &it
))) {
2091 switch (tsk
->state
) {
2093 stats
->nr_running
++;
2095 case TASK_INTERRUPTIBLE
:
2096 stats
->nr_sleeping
++;
2098 case TASK_UNINTERRUPTIBLE
:
2099 stats
->nr_uninterruptible
++;
2102 stats
->nr_stopped
++;
2105 if (delayacct_is_task_waiting_on_io(tsk
))
2106 stats
->nr_io_wait
++;
2110 cgroup_iter_end(cgrp
, &it
);
2117 static int cmppid(const void *a
, const void *b
)
2119 return *(pid_t
*)a
- *(pid_t
*)b
;
2123 * Convert array 'a' of 'npids' pid_t's to a string of newline separated
2124 * decimal pids in 'buf'. Don't write more than 'sz' chars, but return
2125 * count 'cnt' of how many chars would be written if buf were large enough.
2127 static int pid_array_to_buf(char *buf
, int sz
, pid_t
*a
, int npids
)
2132 for (i
= 0; i
< npids
; i
++)
2133 cnt
+= snprintf(buf
+ cnt
, max(sz
- cnt
, 0), "%d\n", a
[i
]);
2138 * Handle an open on 'tasks' file. Prepare a buffer listing the
2139 * process id's of tasks currently attached to the cgroup being opened.
2141 * Does not require any specific cgroup mutexes, and does not take any.
2143 static int cgroup_tasks_open(struct inode
*unused
, struct file
*file
)
2145 struct cgroup
*cgrp
= __d_cgrp(file
->f_dentry
->d_parent
);
2146 struct ctr_struct
*ctr
;
2151 if (!(file
->f_mode
& FMODE_READ
))
2154 ctr
= kmalloc(sizeof(*ctr
), GFP_KERNEL
);
2159 * If cgroup gets more users after we read count, we won't have
2160 * enough space - tough. This race is indistinguishable to the
2161 * caller from the case that the additional cgroup users didn't
2162 * show up until sometime later on.
2164 npids
= cgroup_task_count(cgrp
);
2166 pidarray
= kmalloc(npids
* sizeof(pid_t
), GFP_KERNEL
);
2170 npids
= pid_array_load(pidarray
, npids
, cgrp
);
2171 sort(pidarray
, npids
, sizeof(pid_t
), cmppid
, NULL
);
2173 /* Call pid_array_to_buf() twice, first just to get bufsz */
2174 ctr
->bufsz
= pid_array_to_buf(&c
, sizeof(c
), pidarray
, npids
) + 1;
2175 ctr
->buf
= kmalloc(ctr
->bufsz
, GFP_KERNEL
);
2178 ctr
->bufsz
= pid_array_to_buf(ctr
->buf
, ctr
->bufsz
, pidarray
, npids
);
2185 file
->private_data
= ctr
;
2196 static ssize_t
cgroup_tasks_read(struct cgroup
*cgrp
,
2198 struct file
*file
, char __user
*buf
,
2199 size_t nbytes
, loff_t
*ppos
)
2201 struct ctr_struct
*ctr
= file
->private_data
;
2203 return simple_read_from_buffer(buf
, nbytes
, ppos
, ctr
->buf
, ctr
->bufsz
);
2206 static int cgroup_tasks_release(struct inode
*unused_inode
,
2209 struct ctr_struct
*ctr
;
2211 if (file
->f_mode
& FMODE_READ
) {
2212 ctr
= file
->private_data
;
2219 static u64
cgroup_read_notify_on_release(struct cgroup
*cgrp
,
2222 return notify_on_release(cgrp
);
2226 * for the common functions, 'private' gives the type of file
2228 static struct cftype files
[] = {
2231 .open
= cgroup_tasks_open
,
2232 .read
= cgroup_tasks_read
,
2233 .write
= cgroup_common_file_write
,
2234 .release
= cgroup_tasks_release
,
2235 .private = FILE_TASKLIST
,
2239 .name
= "notify_on_release",
2240 .read_u64
= cgroup_read_notify_on_release
,
2241 .write
= cgroup_common_file_write
,
2242 .private = FILE_NOTIFY_ON_RELEASE
,
2246 static struct cftype cft_release_agent
= {
2247 .name
= "release_agent",
2248 .read
= cgroup_common_file_read
,
2249 .write
= cgroup_common_file_write
,
2250 .private = FILE_RELEASE_AGENT
,
2253 static int cgroup_populate_dir(struct cgroup
*cgrp
)
2256 struct cgroup_subsys
*ss
;
2258 /* First clear out any existing files */
2259 cgroup_clear_directory(cgrp
->dentry
);
2261 err
= cgroup_add_files(cgrp
, NULL
, files
, ARRAY_SIZE(files
));
2265 if (cgrp
== cgrp
->top_cgroup
) {
2266 if ((err
= cgroup_add_file(cgrp
, NULL
, &cft_release_agent
)) < 0)
2270 for_each_subsys(cgrp
->root
, ss
) {
2271 if (ss
->populate
&& (err
= ss
->populate(ss
, cgrp
)) < 0)
2278 static void init_cgroup_css(struct cgroup_subsys_state
*css
,
2279 struct cgroup_subsys
*ss
,
2280 struct cgroup
*cgrp
)
2283 atomic_set(&css
->refcnt
, 0);
2285 if (cgrp
== dummytop
)
2286 set_bit(CSS_ROOT
, &css
->flags
);
2287 BUG_ON(cgrp
->subsys
[ss
->subsys_id
]);
2288 cgrp
->subsys
[ss
->subsys_id
] = css
;
2292 * cgroup_create - create a cgroup
2293 * @parent: cgroup that will be parent of the new cgroup
2294 * @dentry: dentry of the new cgroup
2295 * @mode: mode to set on new inode
2297 * Must be called with the mutex on the parent inode held
2299 static long cgroup_create(struct cgroup
*parent
, struct dentry
*dentry
,
2302 struct cgroup
*cgrp
;
2303 struct cgroupfs_root
*root
= parent
->root
;
2305 struct cgroup_subsys
*ss
;
2306 struct super_block
*sb
= root
->sb
;
2308 cgrp
= kzalloc(sizeof(*cgrp
), GFP_KERNEL
);
2312 /* Grab a reference on the superblock so the hierarchy doesn't
2313 * get deleted on unmount if there are child cgroups. This
2314 * can be done outside cgroup_mutex, since the sb can't
2315 * disappear while someone has an open control file on the
2317 atomic_inc(&sb
->s_active
);
2319 mutex_lock(&cgroup_mutex
);
2321 INIT_LIST_HEAD(&cgrp
->sibling
);
2322 INIT_LIST_HEAD(&cgrp
->children
);
2323 INIT_LIST_HEAD(&cgrp
->css_sets
);
2324 INIT_LIST_HEAD(&cgrp
->release_list
);
2326 cgrp
->parent
= parent
;
2327 cgrp
->root
= parent
->root
;
2328 cgrp
->top_cgroup
= parent
->top_cgroup
;
2330 if (notify_on_release(parent
))
2331 set_bit(CGRP_NOTIFY_ON_RELEASE
, &cgrp
->flags
);
2333 for_each_subsys(root
, ss
) {
2334 struct cgroup_subsys_state
*css
= ss
->create(ss
, cgrp
);
2339 init_cgroup_css(css
, ss
, cgrp
);
2342 list_add(&cgrp
->sibling
, &cgrp
->parent
->children
);
2343 root
->number_of_cgroups
++;
2345 err
= cgroup_create_dir(cgrp
, dentry
, mode
);
2349 /* The cgroup directory was pre-locked for us */
2350 BUG_ON(!mutex_is_locked(&cgrp
->dentry
->d_inode
->i_mutex
));
2352 err
= cgroup_populate_dir(cgrp
);
2353 /* If err < 0, we have a half-filled directory - oh well ;) */
2355 mutex_unlock(&cgroup_mutex
);
2356 mutex_unlock(&cgrp
->dentry
->d_inode
->i_mutex
);
2362 list_del(&cgrp
->sibling
);
2363 root
->number_of_cgroups
--;
2367 for_each_subsys(root
, ss
) {
2368 if (cgrp
->subsys
[ss
->subsys_id
])
2369 ss
->destroy(ss
, cgrp
);
2372 mutex_unlock(&cgroup_mutex
);
2374 /* Release the reference count that we took on the superblock */
2375 deactivate_super(sb
);
2381 static int cgroup_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
2383 struct cgroup
*c_parent
= dentry
->d_parent
->d_fsdata
;
2385 /* the vfs holds inode->i_mutex already */
2386 return cgroup_create(c_parent
, dentry
, mode
| S_IFDIR
);
2389 static inline int cgroup_has_css_refs(struct cgroup
*cgrp
)
2391 /* Check the reference count on each subsystem. Since we
2392 * already established that there are no tasks in the
2393 * cgroup, if the css refcount is also 0, then there should
2394 * be no outstanding references, so the subsystem is safe to
2395 * destroy. We scan across all subsystems rather than using
2396 * the per-hierarchy linked list of mounted subsystems since
2397 * we can be called via check_for_release() with no
2398 * synchronization other than RCU, and the subsystem linked
2399 * list isn't RCU-safe */
2401 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
2402 struct cgroup_subsys
*ss
= subsys
[i
];
2403 struct cgroup_subsys_state
*css
;
2404 /* Skip subsystems not in this hierarchy */
2405 if (ss
->root
!= cgrp
->root
)
2407 css
= cgrp
->subsys
[ss
->subsys_id
];
2408 /* When called from check_for_release() it's possible
2409 * that by this point the cgroup has been removed
2410 * and the css deleted. But a false-positive doesn't
2411 * matter, since it can only happen if the cgroup
2412 * has been deleted and hence no longer needs the
2413 * release agent to be called anyway. */
2414 if (css
&& atomic_read(&css
->refcnt
))
2420 static int cgroup_rmdir(struct inode
*unused_dir
, struct dentry
*dentry
)
2422 struct cgroup
*cgrp
= dentry
->d_fsdata
;
2424 struct cgroup
*parent
;
2425 struct super_block
*sb
;
2426 struct cgroupfs_root
*root
;
2428 /* the vfs holds both inode->i_mutex already */
2430 mutex_lock(&cgroup_mutex
);
2431 if (atomic_read(&cgrp
->count
) != 0) {
2432 mutex_unlock(&cgroup_mutex
);
2435 if (!list_empty(&cgrp
->children
)) {
2436 mutex_unlock(&cgroup_mutex
);
2440 parent
= cgrp
->parent
;
2445 * Call pre_destroy handlers of subsys. Notify subsystems
2446 * that rmdir() request comes.
2448 cgroup_call_pre_destroy(cgrp
);
2450 if (cgroup_has_css_refs(cgrp
)) {
2451 mutex_unlock(&cgroup_mutex
);
2455 spin_lock(&release_list_lock
);
2456 set_bit(CGRP_REMOVED
, &cgrp
->flags
);
2457 if (!list_empty(&cgrp
->release_list
))
2458 list_del(&cgrp
->release_list
);
2459 spin_unlock(&release_list_lock
);
2460 /* delete my sibling from parent->children */
2461 list_del(&cgrp
->sibling
);
2462 spin_lock(&cgrp
->dentry
->d_lock
);
2463 d
= dget(cgrp
->dentry
);
2464 cgrp
->dentry
= NULL
;
2465 spin_unlock(&d
->d_lock
);
2467 cgroup_d_remove_dir(d
);
2470 set_bit(CGRP_RELEASABLE
, &parent
->flags
);
2471 check_for_release(parent
);
2473 mutex_unlock(&cgroup_mutex
);
2477 static void __init
cgroup_init_subsys(struct cgroup_subsys
*ss
)
2479 struct cgroup_subsys_state
*css
;
2480 struct list_head
*l
;
2482 printk(KERN_INFO
"Initializing cgroup subsys %s\n", ss
->name
);
2484 /* Create the top cgroup state for this subsystem */
2485 ss
->root
= &rootnode
;
2486 css
= ss
->create(ss
, dummytop
);
2487 /* We don't handle early failures gracefully */
2488 BUG_ON(IS_ERR(css
));
2489 init_cgroup_css(css
, ss
, dummytop
);
2491 /* Update all cgroup groups to contain a subsys
2492 * pointer to this state - since the subsystem is
2493 * newly registered, all tasks and hence all cgroup
2494 * groups are in the subsystem's top cgroup. */
2495 write_lock(&css_set_lock
);
2496 l
= &init_css_set
.list
;
2498 struct css_set
*cg
=
2499 list_entry(l
, struct css_set
, list
);
2500 cg
->subsys
[ss
->subsys_id
] = dummytop
->subsys
[ss
->subsys_id
];
2502 } while (l
!= &init_css_set
.list
);
2503 write_unlock(&css_set_lock
);
2505 /* If this subsystem requested that it be notified with fork
2506 * events, we should send it one now for every process in the
2509 struct task_struct
*g
, *p
;
2511 read_lock(&tasklist_lock
);
2512 do_each_thread(g
, p
) {
2514 } while_each_thread(g
, p
);
2515 read_unlock(&tasklist_lock
);
2518 need_forkexit_callback
|= ss
->fork
|| ss
->exit
;
2524 * cgroup_init_early - cgroup initialization at system boot
2526 * Initialize cgroups at system boot, and initialize any
2527 * subsystems that request early init.
2529 int __init
cgroup_init_early(void)
2532 kref_init(&init_css_set
.ref
);
2533 kref_get(&init_css_set
.ref
);
2534 INIT_LIST_HEAD(&init_css_set
.list
);
2535 INIT_LIST_HEAD(&init_css_set
.cg_links
);
2536 INIT_LIST_HEAD(&init_css_set
.tasks
);
2537 INIT_HLIST_NODE(&init_css_set
.hlist
);
2539 init_cgroup_root(&rootnode
);
2540 list_add(&rootnode
.root_list
, &roots
);
2542 init_task
.cgroups
= &init_css_set
;
2544 init_css_set_link
.cg
= &init_css_set
;
2545 list_add(&init_css_set_link
.cgrp_link_list
,
2546 &rootnode
.top_cgroup
.css_sets
);
2547 list_add(&init_css_set_link
.cg_link_list
,
2548 &init_css_set
.cg_links
);
2550 for (i
= 0; i
< CSS_SET_TABLE_SIZE
; i
++)
2551 INIT_HLIST_HEAD(&css_set_table
[i
]);
2553 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
2554 struct cgroup_subsys
*ss
= subsys
[i
];
2557 BUG_ON(strlen(ss
->name
) > MAX_CGROUP_TYPE_NAMELEN
);
2558 BUG_ON(!ss
->create
);
2559 BUG_ON(!ss
->destroy
);
2560 if (ss
->subsys_id
!= i
) {
2561 printk(KERN_ERR
"cgroup: Subsys %s id == %d\n",
2562 ss
->name
, ss
->subsys_id
);
2567 cgroup_init_subsys(ss
);
2573 * cgroup_init - cgroup initialization
2575 * Register cgroup filesystem and /proc file, and initialize
2576 * any subsystems that didn't request early init.
2578 int __init
cgroup_init(void)
2582 struct hlist_head
*hhead
;
2584 err
= bdi_init(&cgroup_backing_dev_info
);
2588 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
2589 struct cgroup_subsys
*ss
= subsys
[i
];
2590 if (!ss
->early_init
)
2591 cgroup_init_subsys(ss
);
2594 /* Add init_css_set to the hash table */
2595 hhead
= css_set_hash(init_css_set
.subsys
);
2596 hlist_add_head(&init_css_set
.hlist
, hhead
);
2598 err
= register_filesystem(&cgroup_fs_type
);
2602 proc_create("cgroups", 0, NULL
, &proc_cgroupstats_operations
);
2606 bdi_destroy(&cgroup_backing_dev_info
);
2612 * proc_cgroup_show()
2613 * - Print task's cgroup paths into seq_file, one line for each hierarchy
2614 * - Used for /proc/<pid>/cgroup.
2615 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
2616 * doesn't really matter if tsk->cgroup changes after we read it,
2617 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
2618 * anyway. No need to check that tsk->cgroup != NULL, thanks to
2619 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
2620 * cgroup to top_cgroup.
2623 /* TODO: Use a proper seq_file iterator */
2624 static int proc_cgroup_show(struct seq_file
*m
, void *v
)
2627 struct task_struct
*tsk
;
2630 struct cgroupfs_root
*root
;
2633 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
2639 tsk
= get_pid_task(pid
, PIDTYPE_PID
);
2645 mutex_lock(&cgroup_mutex
);
2647 for_each_root(root
) {
2648 struct cgroup_subsys
*ss
;
2649 struct cgroup
*cgrp
;
2653 /* Skip this hierarchy if it has no active subsystems */
2654 if (!root
->actual_subsys_bits
)
2656 seq_printf(m
, "%lu:", root
->subsys_bits
);
2657 for_each_subsys(root
, ss
)
2658 seq_printf(m
, "%s%s", count
++ ? "," : "", ss
->name
);
2660 get_first_subsys(&root
->top_cgroup
, NULL
, &subsys_id
);
2661 cgrp
= task_cgroup(tsk
, subsys_id
);
2662 retval
= cgroup_path(cgrp
, buf
, PAGE_SIZE
);
2670 mutex_unlock(&cgroup_mutex
);
2671 put_task_struct(tsk
);
2678 static int cgroup_open(struct inode
*inode
, struct file
*file
)
2680 struct pid
*pid
= PROC_I(inode
)->pid
;
2681 return single_open(file
, proc_cgroup_show
, pid
);
2684 struct file_operations proc_cgroup_operations
= {
2685 .open
= cgroup_open
,
2687 .llseek
= seq_lseek
,
2688 .release
= single_release
,
2691 /* Display information about each subsystem and each hierarchy */
2692 static int proc_cgroupstats_show(struct seq_file
*m
, void *v
)
2696 seq_puts(m
, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
2697 mutex_lock(&cgroup_mutex
);
2698 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
2699 struct cgroup_subsys
*ss
= subsys
[i
];
2700 seq_printf(m
, "%s\t%lu\t%d\t%d\n",
2701 ss
->name
, ss
->root
->subsys_bits
,
2702 ss
->root
->number_of_cgroups
, !ss
->disabled
);
2704 mutex_unlock(&cgroup_mutex
);
2708 static int cgroupstats_open(struct inode
*inode
, struct file
*file
)
2710 return single_open(file
, proc_cgroupstats_show
, NULL
);
2713 static struct file_operations proc_cgroupstats_operations
= {
2714 .open
= cgroupstats_open
,
2716 .llseek
= seq_lseek
,
2717 .release
= single_release
,
2721 * cgroup_fork - attach newly forked task to its parents cgroup.
2722 * @child: pointer to task_struct of forking parent process.
2724 * Description: A task inherits its parent's cgroup at fork().
2726 * A pointer to the shared css_set was automatically copied in
2727 * fork.c by dup_task_struct(). However, we ignore that copy, since
2728 * it was not made under the protection of RCU or cgroup_mutex, so
2729 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
2730 * have already changed current->cgroups, allowing the previously
2731 * referenced cgroup group to be removed and freed.
2733 * At the point that cgroup_fork() is called, 'current' is the parent
2734 * task, and the passed argument 'child' points to the child task.
2736 void cgroup_fork(struct task_struct
*child
)
2739 child
->cgroups
= current
->cgroups
;
2740 get_css_set(child
->cgroups
);
2741 task_unlock(current
);
2742 INIT_LIST_HEAD(&child
->cg_list
);
2746 * cgroup_fork_callbacks - run fork callbacks
2747 * @child: the new task
2749 * Called on a new task very soon before adding it to the
2750 * tasklist. No need to take any locks since no-one can
2751 * be operating on this task.
2753 void cgroup_fork_callbacks(struct task_struct
*child
)
2755 if (need_forkexit_callback
) {
2757 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
2758 struct cgroup_subsys
*ss
= subsys
[i
];
2760 ss
->fork(ss
, child
);
2766 * cgroup_post_fork - called on a new task after adding it to the task list
2767 * @child: the task in question
2769 * Adds the task to the list running through its css_set if necessary.
2770 * Has to be after the task is visible on the task list in case we race
2771 * with the first call to cgroup_iter_start() - to guarantee that the
2772 * new task ends up on its list.
2774 void cgroup_post_fork(struct task_struct
*child
)
2776 if (use_task_css_set_links
) {
2777 write_lock(&css_set_lock
);
2778 if (list_empty(&child
->cg_list
))
2779 list_add(&child
->cg_list
, &child
->cgroups
->tasks
);
2780 write_unlock(&css_set_lock
);
2784 * cgroup_exit - detach cgroup from exiting task
2785 * @tsk: pointer to task_struct of exiting process
2786 * @run_callback: run exit callbacks?
2788 * Description: Detach cgroup from @tsk and release it.
2790 * Note that cgroups marked notify_on_release force every task in
2791 * them to take the global cgroup_mutex mutex when exiting.
2792 * This could impact scaling on very large systems. Be reluctant to
2793 * use notify_on_release cgroups where very high task exit scaling
2794 * is required on large systems.
2796 * the_top_cgroup_hack:
2798 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
2800 * We call cgroup_exit() while the task is still competent to
2801 * handle notify_on_release(), then leave the task attached to the
2802 * root cgroup in each hierarchy for the remainder of its exit.
2804 * To do this properly, we would increment the reference count on
2805 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
2806 * code we would add a second cgroup function call, to drop that
2807 * reference. This would just create an unnecessary hot spot on
2808 * the top_cgroup reference count, to no avail.
2810 * Normally, holding a reference to a cgroup without bumping its
2811 * count is unsafe. The cgroup could go away, or someone could
2812 * attach us to a different cgroup, decrementing the count on
2813 * the first cgroup that we never incremented. But in this case,
2814 * top_cgroup isn't going away, and either task has PF_EXITING set,
2815 * which wards off any cgroup_attach_task() attempts, or task is a failed
2816 * fork, never visible to cgroup_attach_task.
2818 void cgroup_exit(struct task_struct
*tsk
, int run_callbacks
)
2823 if (run_callbacks
&& need_forkexit_callback
) {
2824 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
2825 struct cgroup_subsys
*ss
= subsys
[i
];
2832 * Unlink from the css_set task list if necessary.
2833 * Optimistically check cg_list before taking
2836 if (!list_empty(&tsk
->cg_list
)) {
2837 write_lock(&css_set_lock
);
2838 if (!list_empty(&tsk
->cg_list
))
2839 list_del(&tsk
->cg_list
);
2840 write_unlock(&css_set_lock
);
2843 /* Reassign the task to the init_css_set. */
2846 tsk
->cgroups
= &init_css_set
;
2849 put_css_set_taskexit(cg
);
2853 * cgroup_clone - clone the cgroup the given subsystem is attached to
2854 * @tsk: the task to be moved
2855 * @subsys: the given subsystem
2857 * Duplicate the current cgroup in the hierarchy that the given
2858 * subsystem is attached to, and move this task into the new
2861 int cgroup_clone(struct task_struct
*tsk
, struct cgroup_subsys
*subsys
)
2863 struct dentry
*dentry
;
2865 char nodename
[MAX_CGROUP_TYPE_NAMELEN
];
2866 struct cgroup
*parent
, *child
;
2867 struct inode
*inode
;
2869 struct cgroupfs_root
*root
;
2870 struct cgroup_subsys
*ss
;
2872 /* We shouldn't be called by an unregistered subsystem */
2873 BUG_ON(!subsys
->active
);
2875 /* First figure out what hierarchy and cgroup we're dealing
2876 * with, and pin them so we can drop cgroup_mutex */
2877 mutex_lock(&cgroup_mutex
);
2879 root
= subsys
->root
;
2880 if (root
== &rootnode
) {
2882 "Not cloning cgroup for unused subsystem %s\n",
2884 mutex_unlock(&cgroup_mutex
);
2888 parent
= task_cgroup(tsk
, subsys
->subsys_id
);
2890 snprintf(nodename
, MAX_CGROUP_TYPE_NAMELEN
, "node_%d", tsk
->pid
);
2892 /* Pin the hierarchy */
2893 atomic_inc(&parent
->root
->sb
->s_active
);
2895 /* Keep the cgroup alive */
2897 mutex_unlock(&cgroup_mutex
);
2899 /* Now do the VFS work to create a cgroup */
2900 inode
= parent
->dentry
->d_inode
;
2902 /* Hold the parent directory mutex across this operation to
2903 * stop anyone else deleting the new cgroup */
2904 mutex_lock(&inode
->i_mutex
);
2905 dentry
= lookup_one_len(nodename
, parent
->dentry
, strlen(nodename
));
2906 if (IS_ERR(dentry
)) {
2908 "cgroup: Couldn't allocate dentry for %s: %ld\n", nodename
,
2910 ret
= PTR_ERR(dentry
);
2914 /* Create the cgroup directory, which also creates the cgroup */
2915 ret
= vfs_mkdir(inode
, dentry
, S_IFDIR
| 0755);
2916 child
= __d_cgrp(dentry
);
2920 "Failed to create cgroup %s: %d\n", nodename
,
2927 "Couldn't find new cgroup %s\n", nodename
);
2932 /* The cgroup now exists. Retake cgroup_mutex and check
2933 * that we're still in the same state that we thought we
2935 mutex_lock(&cgroup_mutex
);
2936 if ((root
!= subsys
->root
) ||
2937 (parent
!= task_cgroup(tsk
, subsys
->subsys_id
))) {
2938 /* Aargh, we raced ... */
2939 mutex_unlock(&inode
->i_mutex
);
2942 deactivate_super(parent
->root
->sb
);
2943 /* The cgroup is still accessible in the VFS, but
2944 * we're not going to try to rmdir() it at this
2947 "Race in cgroup_clone() - leaking cgroup %s\n",
2952 /* do any required auto-setup */
2953 for_each_subsys(root
, ss
) {
2955 ss
->post_clone(ss
, child
);
2958 /* All seems fine. Finish by moving the task into the new cgroup */
2959 ret
= cgroup_attach_task(child
, tsk
);
2960 mutex_unlock(&cgroup_mutex
);
2963 mutex_unlock(&inode
->i_mutex
);
2965 mutex_lock(&cgroup_mutex
);
2967 mutex_unlock(&cgroup_mutex
);
2968 deactivate_super(parent
->root
->sb
);
2973 * cgroup_is_descendant - see if @cgrp is a descendant of current task's cgrp
2974 * @cgrp: the cgroup in question
2976 * See if @cgrp is a descendant of the current task's cgroup in
2977 * the appropriate hierarchy.
2979 * If we are sending in dummytop, then presumably we are creating
2980 * the top cgroup in the subsystem.
2982 * Called only by the ns (nsproxy) cgroup.
2984 int cgroup_is_descendant(const struct cgroup
*cgrp
)
2987 struct cgroup
*target
;
2990 if (cgrp
== dummytop
)
2993 get_first_subsys(cgrp
, NULL
, &subsys_id
);
2994 target
= task_cgroup(current
, subsys_id
);
2995 while (cgrp
!= target
&& cgrp
!= cgrp
->top_cgroup
)
2996 cgrp
= cgrp
->parent
;
2997 ret
= (cgrp
== target
);
3001 static void check_for_release(struct cgroup
*cgrp
)
3003 /* All of these checks rely on RCU to keep the cgroup
3004 * structure alive */
3005 if (cgroup_is_releasable(cgrp
) && !atomic_read(&cgrp
->count
)
3006 && list_empty(&cgrp
->children
) && !cgroup_has_css_refs(cgrp
)) {
3007 /* Control Group is currently removeable. If it's not
3008 * already queued for a userspace notification, queue
3010 int need_schedule_work
= 0;
3011 spin_lock(&release_list_lock
);
3012 if (!cgroup_is_removed(cgrp
) &&
3013 list_empty(&cgrp
->release_list
)) {
3014 list_add(&cgrp
->release_list
, &release_list
);
3015 need_schedule_work
= 1;
3017 spin_unlock(&release_list_lock
);
3018 if (need_schedule_work
)
3019 schedule_work(&release_agent_work
);
3023 void __css_put(struct cgroup_subsys_state
*css
)
3025 struct cgroup
*cgrp
= css
->cgroup
;
3027 if (atomic_dec_and_test(&css
->refcnt
) && notify_on_release(cgrp
)) {
3028 set_bit(CGRP_RELEASABLE
, &cgrp
->flags
);
3029 check_for_release(cgrp
);
3035 * Notify userspace when a cgroup is released, by running the
3036 * configured release agent with the name of the cgroup (path
3037 * relative to the root of cgroup file system) as the argument.
3039 * Most likely, this user command will try to rmdir this cgroup.
3041 * This races with the possibility that some other task will be
3042 * attached to this cgroup before it is removed, or that some other
3043 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
3044 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
3045 * unused, and this cgroup will be reprieved from its death sentence,
3046 * to continue to serve a useful existence. Next time it's released,
3047 * we will get notified again, if it still has 'notify_on_release' set.
3049 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
3050 * means only wait until the task is successfully execve()'d. The
3051 * separate release agent task is forked by call_usermodehelper(),
3052 * then control in this thread returns here, without waiting for the
3053 * release agent task. We don't bother to wait because the caller of
3054 * this routine has no use for the exit status of the release agent
3055 * task, so no sense holding our caller up for that.
3057 static void cgroup_release_agent(struct work_struct
*work
)
3059 BUG_ON(work
!= &release_agent_work
);
3060 mutex_lock(&cgroup_mutex
);
3061 spin_lock(&release_list_lock
);
3062 while (!list_empty(&release_list
)) {
3063 char *argv
[3], *envp
[3];
3066 struct cgroup
*cgrp
= list_entry(release_list
.next
,
3069 list_del_init(&cgrp
->release_list
);
3070 spin_unlock(&release_list_lock
);
3071 pathbuf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
3073 spin_lock(&release_list_lock
);
3077 if (cgroup_path(cgrp
, pathbuf
, PAGE_SIZE
) < 0) {
3079 spin_lock(&release_list_lock
);
3084 argv
[i
++] = cgrp
->root
->release_agent_path
;
3085 argv
[i
++] = (char *)pathbuf
;
3089 /* minimal command environment */
3090 envp
[i
++] = "HOME=/";
3091 envp
[i
++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
3094 /* Drop the lock while we invoke the usermode helper,
3095 * since the exec could involve hitting disk and hence
3096 * be a slow process */
3097 mutex_unlock(&cgroup_mutex
);
3098 call_usermodehelper(argv
[0], argv
, envp
, UMH_WAIT_EXEC
);
3100 mutex_lock(&cgroup_mutex
);
3101 spin_lock(&release_list_lock
);
3103 spin_unlock(&release_list_lock
);
3104 mutex_unlock(&cgroup_mutex
);
3107 static int __init
cgroup_disable(char *str
)
3112 while ((token
= strsep(&str
, ",")) != NULL
) {
3116 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
3117 struct cgroup_subsys
*ss
= subsys
[i
];
3119 if (!strcmp(token
, ss
->name
)) {
3121 printk(KERN_INFO
"Disabling %s control group"
3122 " subsystem\n", ss
->name
);
3129 __setup("cgroup_disable=", cgroup_disable
);