4 * Generic process-grouping system.
6 * Based originally on the cpuset system, extracted by Paul Menage
7 * Copyright (C) 2006 Google, Inc
9 * Copyright notices from the original cpuset code:
10 * --------------------------------------------------
11 * Copyright (C) 2003 BULL SA.
12 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
14 * Portions derived from Patrick Mochel's sysfs code.
15 * sysfs is Copyright (c) 2001-3 Patrick Mochel
17 * 2003-10-10 Written by Simon Derr.
18 * 2003-10-22 Updates by Stephen Hemminger.
19 * 2004 May-July Rework by Paul Jackson.
20 * ---------------------------------------------------
22 * This file is subject to the terms and conditions of the GNU General Public
23 * License. See the file COPYING in the main directory of the Linux
24 * distribution for more details.
27 #include <linux/cgroup.h>
28 #include <linux/errno.h>
30 #include <linux/kernel.h>
31 #include <linux/list.h>
33 #include <linux/mutex.h>
34 #include <linux/mount.h>
35 #include <linux/pagemap.h>
36 #include <linux/rcupdate.h>
37 #include <linux/sched.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 <asm/atomic.h>
46 /* Generate an array of cgroup subsystem pointers */
47 #define SUBSYS(_x) &_x ## _subsys,
49 static struct cgroup_subsys
*subsys
[] = {
50 #include <linux/cgroup_subsys.h>
54 * A cgroupfs_root represents the root of a cgroup hierarchy,
55 * and may be associated with a superblock to form an active
58 struct cgroupfs_root
{
59 struct super_block
*sb
;
62 * The bitmask of subsystems intended to be attached to this
65 unsigned long subsys_bits
;
67 /* The bitmask of subsystems currently attached to this hierarchy */
68 unsigned long actual_subsys_bits
;
70 /* A list running through the attached subsystems */
71 struct list_head subsys_list
;
73 /* The root cgroup for this hierarchy */
74 struct cgroup top_cgroup
;
76 /* Tracks how many cgroups are currently defined in hierarchy.*/
77 int number_of_cgroups
;
79 /* A list running through the mounted hierarchies */
80 struct list_head root_list
;
82 /* Hierarchy-specific flags */
88 * The "rootnode" hierarchy is the "dummy hierarchy", reserved for the
89 * subsystems that are otherwise unattached - it never has more than a
90 * single cgroup, and all tasks are part of that cgroup.
92 static struct cgroupfs_root rootnode
;
94 /* The list of hierarchy roots */
96 static LIST_HEAD(roots
);
98 /* dummytop is a shorthand for the dummy hierarchy's top cgroup */
99 #define dummytop (&rootnode.top_cgroup)
101 /* This flag indicates whether tasks in the fork and exit paths should
102 * take callback_mutex and check for fork/exit handlers to call. This
103 * avoids us having to do extra work in the fork/exit path if none of the
104 * subsystems need to be called.
106 static int need_forkexit_callback
;
108 /* bits in struct cgroup flags field */
113 /* convenient tests for these bits */
114 inline int cgroup_is_removed(const struct cgroup
*cont
)
116 return test_bit(CONT_REMOVED
, &cont
->flags
);
119 /* bits in struct cgroupfs_root flags field */
121 ROOT_NOPREFIX
, /* mounted subsystems have no named prefix */
125 * for_each_subsys() allows you to iterate on each subsystem attached to
126 * an active hierarchy
128 #define for_each_subsys(_root, _ss) \
129 list_for_each_entry(_ss, &_root->subsys_list, sibling)
131 /* for_each_root() allows you to iterate across the active hierarchies */
132 #define for_each_root(_root) \
133 list_for_each_entry(_root, &roots, root_list)
136 * There is one global cgroup mutex. We also require taking
137 * task_lock() when dereferencing a task's cgroup subsys pointers.
138 * See "The task_lock() exception", at the end of this comment.
140 * A task must hold cgroup_mutex to modify cgroups.
142 * Any task can increment and decrement the count field without lock.
143 * So in general, code holding cgroup_mutex can't rely on the count
144 * field not changing. However, if the count goes to zero, then only
145 * attach_task() can increment it again. Because a count of zero
146 * means that no tasks are currently attached, therefore there is no
147 * way a task attached to that cgroup can fork (the other way to
148 * increment the count). So code holding cgroup_mutex can safely
149 * assume that if the count is zero, it will stay zero. Similarly, if
150 * a task holds cgroup_mutex on a cgroup with zero count, it
151 * knows that the cgroup won't be removed, as cgroup_rmdir()
154 * The cgroup_common_file_write handler for operations that modify
155 * the cgroup hierarchy holds cgroup_mutex across the entire operation,
156 * single threading all such cgroup modifications across the system.
158 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
159 * (usually) take cgroup_mutex. These are the two most performance
160 * critical pieces of code here. The exception occurs on cgroup_exit(),
161 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
162 * is taken, and if the cgroup count is zero, a usermode call made
163 * to /sbin/cgroup_release_agent with the name of the cgroup (path
164 * relative to the root of cgroup file system) as the argument.
166 * A cgroup can only be deleted if both its 'count' of using tasks
167 * is zero, and its list of 'children' cgroups is empty. Since all
168 * tasks in the system use _some_ cgroup, and since there is always at
169 * least one task in the system (init, pid == 1), therefore, top_cgroup
170 * always has either children cgroups and/or using tasks. So we don't
171 * need a special hack to ensure that top_cgroup cannot be deleted.
173 * The task_lock() exception
175 * The need for this exception arises from the action of
176 * attach_task(), which overwrites one tasks cgroup pointer with
177 * another. It does so using cgroup_mutexe, however there are
178 * several performance critical places that need to reference
179 * task->cgroup without the expense of grabbing a system global
180 * mutex. Therefore except as noted below, when dereferencing or, as
181 * in attach_task(), modifying a task'ss cgroup pointer we use
182 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
183 * the task_struct routinely used for such matters.
185 * P.S. One more locking exception. RCU is used to guard the
186 * update of a tasks cgroup pointer by attach_task()
189 static DEFINE_MUTEX(cgroup_mutex
);
192 * cgroup_lock - lock out any changes to cgroup structures
196 void cgroup_lock(void)
198 mutex_lock(&cgroup_mutex
);
202 * cgroup_unlock - release lock on cgroup changes
204 * Undo the lock taken in a previous cgroup_lock() call.
207 void cgroup_unlock(void)
209 mutex_unlock(&cgroup_mutex
);
213 * A couple of forward declarations required, due to cyclic reference loop:
214 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
215 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
219 static int cgroup_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
);
220 static int cgroup_rmdir(struct inode
*unused_dir
, struct dentry
*dentry
);
221 static int cgroup_populate_dir(struct cgroup
*cont
);
222 static struct inode_operations cgroup_dir_inode_operations
;
224 static struct inode
*cgroup_new_inode(mode_t mode
, struct super_block
*sb
)
226 struct inode
*inode
= new_inode(sb
);
227 static struct backing_dev_info cgroup_backing_dev_info
= {
228 .capabilities
= BDI_CAP_NO_ACCT_DIRTY
| BDI_CAP_NO_WRITEBACK
,
232 inode
->i_mode
= mode
;
233 inode
->i_uid
= current
->fsuid
;
234 inode
->i_gid
= current
->fsgid
;
236 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
237 inode
->i_mapping
->backing_dev_info
= &cgroup_backing_dev_info
;
242 static void cgroup_diput(struct dentry
*dentry
, struct inode
*inode
)
244 /* is dentry a directory ? if so, kfree() associated cgroup */
245 if (S_ISDIR(inode
->i_mode
)) {
246 struct cgroup
*cont
= dentry
->d_fsdata
;
247 BUG_ON(!(cgroup_is_removed(cont
)));
253 static void remove_dir(struct dentry
*d
)
255 struct dentry
*parent
= dget(d
->d_parent
);
258 simple_rmdir(parent
->d_inode
, d
);
262 static void cgroup_clear_directory(struct dentry
*dentry
)
264 struct list_head
*node
;
266 BUG_ON(!mutex_is_locked(&dentry
->d_inode
->i_mutex
));
267 spin_lock(&dcache_lock
);
268 node
= dentry
->d_subdirs
.next
;
269 while (node
!= &dentry
->d_subdirs
) {
270 struct dentry
*d
= list_entry(node
, struct dentry
, d_u
.d_child
);
273 /* This should never be called on a cgroup
274 * directory with child cgroups */
275 BUG_ON(d
->d_inode
->i_mode
& S_IFDIR
);
277 spin_unlock(&dcache_lock
);
279 simple_unlink(dentry
->d_inode
, d
);
281 spin_lock(&dcache_lock
);
283 node
= dentry
->d_subdirs
.next
;
285 spin_unlock(&dcache_lock
);
289 * NOTE : the dentry must have been dget()'ed
291 static void cgroup_d_remove_dir(struct dentry
*dentry
)
293 cgroup_clear_directory(dentry
);
295 spin_lock(&dcache_lock
);
296 list_del_init(&dentry
->d_u
.d_child
);
297 spin_unlock(&dcache_lock
);
301 static int rebind_subsystems(struct cgroupfs_root
*root
,
302 unsigned long final_bits
)
304 unsigned long added_bits
, removed_bits
;
305 struct cgroup
*cont
= &root
->top_cgroup
;
308 removed_bits
= root
->actual_subsys_bits
& ~final_bits
;
309 added_bits
= final_bits
& ~root
->actual_subsys_bits
;
310 /* Check that any added subsystems are currently free */
311 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
312 unsigned long long bit
= 1ull << i
;
313 struct cgroup_subsys
*ss
= subsys
[i
];
314 if (!(bit
& added_bits
))
316 if (ss
->root
!= &rootnode
) {
317 /* Subsystem isn't free */
322 /* Currently we don't handle adding/removing subsystems when
323 * any child cgroups exist. This is theoretically supportable
324 * but involves complex error handling, so it's being left until
326 if (!list_empty(&cont
->children
))
329 /* Process each subsystem */
330 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
331 struct cgroup_subsys
*ss
= subsys
[i
];
332 unsigned long bit
= 1UL << i
;
333 if (bit
& added_bits
) {
334 /* We're binding this subsystem to this hierarchy */
335 BUG_ON(cont
->subsys
[i
]);
336 BUG_ON(!dummytop
->subsys
[i
]);
337 BUG_ON(dummytop
->subsys
[i
]->cgroup
!= dummytop
);
338 cont
->subsys
[i
] = dummytop
->subsys
[i
];
339 cont
->subsys
[i
]->cgroup
= cont
;
340 list_add(&ss
->sibling
, &root
->subsys_list
);
341 rcu_assign_pointer(ss
->root
, root
);
345 } else if (bit
& removed_bits
) {
346 /* We're removing this subsystem */
347 BUG_ON(cont
->subsys
[i
] != dummytop
->subsys
[i
]);
348 BUG_ON(cont
->subsys
[i
]->cgroup
!= cont
);
350 ss
->bind(ss
, dummytop
);
351 dummytop
->subsys
[i
]->cgroup
= dummytop
;
352 cont
->subsys
[i
] = NULL
;
353 rcu_assign_pointer(subsys
[i
]->root
, &rootnode
);
354 list_del(&ss
->sibling
);
355 } else if (bit
& final_bits
) {
356 /* Subsystem state should already exist */
357 BUG_ON(!cont
->subsys
[i
]);
359 /* Subsystem state shouldn't exist */
360 BUG_ON(cont
->subsys
[i
]);
363 root
->subsys_bits
= root
->actual_subsys_bits
= final_bits
;
369 static int cgroup_show_options(struct seq_file
*seq
, struct vfsmount
*vfs
)
371 struct cgroupfs_root
*root
= vfs
->mnt_sb
->s_fs_info
;
372 struct cgroup_subsys
*ss
;
374 mutex_lock(&cgroup_mutex
);
375 for_each_subsys(root
, ss
)
376 seq_printf(seq
, ",%s", ss
->name
);
377 if (test_bit(ROOT_NOPREFIX
, &root
->flags
))
378 seq_puts(seq
, ",noprefix");
379 mutex_unlock(&cgroup_mutex
);
383 struct cgroup_sb_opts
{
384 unsigned long subsys_bits
;
388 /* Convert a hierarchy specifier into a bitmask of subsystems and
390 static int parse_cgroupfs_options(char *data
,
391 struct cgroup_sb_opts
*opts
)
393 char *token
, *o
= data
?: "all";
395 opts
->subsys_bits
= 0;
398 while ((token
= strsep(&o
, ",")) != NULL
) {
401 if (!strcmp(token
, "all")) {
402 opts
->subsys_bits
= (1 << CGROUP_SUBSYS_COUNT
) - 1;
403 } else if (!strcmp(token
, "noprefix")) {
404 set_bit(ROOT_NOPREFIX
, &opts
->flags
);
406 struct cgroup_subsys
*ss
;
408 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
410 if (!strcmp(token
, ss
->name
)) {
411 set_bit(i
, &opts
->subsys_bits
);
415 if (i
== CGROUP_SUBSYS_COUNT
)
420 /* We can't have an empty hierarchy */
421 if (!opts
->subsys_bits
)
427 static int cgroup_remount(struct super_block
*sb
, int *flags
, char *data
)
430 struct cgroupfs_root
*root
= sb
->s_fs_info
;
431 struct cgroup
*cont
= &root
->top_cgroup
;
432 struct cgroup_sb_opts opts
;
434 mutex_lock(&cont
->dentry
->d_inode
->i_mutex
);
435 mutex_lock(&cgroup_mutex
);
437 /* See what subsystems are wanted */
438 ret
= parse_cgroupfs_options(data
, &opts
);
442 /* Don't allow flags to change at remount */
443 if (opts
.flags
!= root
->flags
) {
448 ret
= rebind_subsystems(root
, opts
.subsys_bits
);
450 /* (re)populate subsystem files */
452 cgroup_populate_dir(cont
);
455 mutex_unlock(&cgroup_mutex
);
456 mutex_unlock(&cont
->dentry
->d_inode
->i_mutex
);
460 static struct super_operations cgroup_ops
= {
461 .statfs
= simple_statfs
,
462 .drop_inode
= generic_delete_inode
,
463 .show_options
= cgroup_show_options
,
464 .remount_fs
= cgroup_remount
,
467 static void init_cgroup_root(struct cgroupfs_root
*root
)
469 struct cgroup
*cont
= &root
->top_cgroup
;
470 INIT_LIST_HEAD(&root
->subsys_list
);
471 INIT_LIST_HEAD(&root
->root_list
);
472 root
->number_of_cgroups
= 1;
474 cont
->top_cgroup
= cont
;
475 INIT_LIST_HEAD(&cont
->sibling
);
476 INIT_LIST_HEAD(&cont
->children
);
479 static int cgroup_test_super(struct super_block
*sb
, void *data
)
481 struct cgroupfs_root
*new = data
;
482 struct cgroupfs_root
*root
= sb
->s_fs_info
;
484 /* First check subsystems */
485 if (new->subsys_bits
!= root
->subsys_bits
)
488 /* Next check flags */
489 if (new->flags
!= root
->flags
)
495 static int cgroup_set_super(struct super_block
*sb
, void *data
)
498 struct cgroupfs_root
*root
= data
;
500 ret
= set_anon_super(sb
, NULL
);
504 sb
->s_fs_info
= root
;
507 sb
->s_blocksize
= PAGE_CACHE_SIZE
;
508 sb
->s_blocksize_bits
= PAGE_CACHE_SHIFT
;
509 sb
->s_magic
= CGROUP_SUPER_MAGIC
;
510 sb
->s_op
= &cgroup_ops
;
515 static int cgroup_get_rootdir(struct super_block
*sb
)
517 struct inode
*inode
=
518 cgroup_new_inode(S_IFDIR
| S_IRUGO
| S_IXUGO
| S_IWUSR
, sb
);
519 struct dentry
*dentry
;
524 inode
->i_op
= &simple_dir_inode_operations
;
525 inode
->i_fop
= &simple_dir_operations
;
526 inode
->i_op
= &cgroup_dir_inode_operations
;
527 /* directories start off with i_nlink == 2 (for "." entry) */
529 dentry
= d_alloc_root(inode
);
538 static int cgroup_get_sb(struct file_system_type
*fs_type
,
539 int flags
, const char *unused_dev_name
,
540 void *data
, struct vfsmount
*mnt
)
542 struct cgroup_sb_opts opts
;
544 struct super_block
*sb
;
545 struct cgroupfs_root
*root
;
547 /* First find the desired set of subsystems */
548 ret
= parse_cgroupfs_options(data
, &opts
);
552 root
= kzalloc(sizeof(*root
), GFP_KERNEL
);
556 init_cgroup_root(root
);
557 root
->subsys_bits
= opts
.subsys_bits
;
558 root
->flags
= opts
.flags
;
560 sb
= sget(fs_type
, cgroup_test_super
, cgroup_set_super
, root
);
567 if (sb
->s_fs_info
!= root
) {
568 /* Reusing an existing superblock */
569 BUG_ON(sb
->s_root
== NULL
);
574 struct cgroup
*cont
= &root
->top_cgroup
;
576 BUG_ON(sb
->s_root
!= NULL
);
578 ret
= cgroup_get_rootdir(sb
);
582 mutex_lock(&cgroup_mutex
);
584 ret
= rebind_subsystems(root
, root
->subsys_bits
);
586 mutex_unlock(&cgroup_mutex
);
590 /* EBUSY should be the only error here */
593 list_add(&root
->root_list
, &roots
);
595 sb
->s_root
->d_fsdata
= &root
->top_cgroup
;
596 root
->top_cgroup
.dentry
= sb
->s_root
;
598 BUG_ON(!list_empty(&cont
->sibling
));
599 BUG_ON(!list_empty(&cont
->children
));
600 BUG_ON(root
->number_of_cgroups
!= 1);
603 * I believe that it's safe to nest i_mutex inside
604 * cgroup_mutex in this case, since no-one else can
605 * be accessing this directory yet. But we still need
606 * to teach lockdep that this is the case - currently
607 * a cgroupfs remount triggers a lockdep warning
609 mutex_lock(&cont
->dentry
->d_inode
->i_mutex
);
610 cgroup_populate_dir(cont
);
611 mutex_unlock(&cont
->dentry
->d_inode
->i_mutex
);
612 mutex_unlock(&cgroup_mutex
);
615 return simple_set_mnt(mnt
, sb
);
618 up_write(&sb
->s_umount
);
619 deactivate_super(sb
);
623 static void cgroup_kill_sb(struct super_block
*sb
) {
624 struct cgroupfs_root
*root
= sb
->s_fs_info
;
625 struct cgroup
*cont
= &root
->top_cgroup
;
630 BUG_ON(root
->number_of_cgroups
!= 1);
631 BUG_ON(!list_empty(&cont
->children
));
632 BUG_ON(!list_empty(&cont
->sibling
));
634 mutex_lock(&cgroup_mutex
);
636 /* Rebind all subsystems back to the default hierarchy */
637 ret
= rebind_subsystems(root
, 0);
638 /* Shouldn't be able to fail ... */
641 if (!list_empty(&root
->root_list
))
642 list_del(&root
->root_list
);
643 mutex_unlock(&cgroup_mutex
);
646 kill_litter_super(sb
);
649 static struct file_system_type cgroup_fs_type
= {
651 .get_sb
= cgroup_get_sb
,
652 .kill_sb
= cgroup_kill_sb
,
655 static inline struct cgroup
*__d_cont(struct dentry
*dentry
)
657 return dentry
->d_fsdata
;
660 static inline struct cftype
*__d_cft(struct dentry
*dentry
)
662 return dentry
->d_fsdata
;
666 * Called with cgroup_mutex held. Writes path of cgroup into buf.
667 * Returns 0 on success, -errno on error.
669 int cgroup_path(const struct cgroup
*cont
, char *buf
, int buflen
)
673 if (cont
== dummytop
) {
675 * Inactive subsystems have no dentry for their root
682 start
= buf
+ buflen
;
686 int len
= cont
->dentry
->d_name
.len
;
687 if ((start
-= len
) < buf
)
688 return -ENAMETOOLONG
;
689 memcpy(start
, cont
->dentry
->d_name
.name
, len
);
696 return -ENAMETOOLONG
;
699 memmove(buf
, start
, buf
+ buflen
- start
);
704 * Return the first subsystem attached to a cgroup's hierarchy, and
708 static void get_first_subsys(const struct cgroup
*cont
,
709 struct cgroup_subsys_state
**css
, int *subsys_id
)
711 const struct cgroupfs_root
*root
= cont
->root
;
712 const struct cgroup_subsys
*test_ss
;
713 BUG_ON(list_empty(&root
->subsys_list
));
714 test_ss
= list_entry(root
->subsys_list
.next
,
715 struct cgroup_subsys
, sibling
);
717 *css
= cont
->subsys
[test_ss
->subsys_id
];
721 *subsys_id
= test_ss
->subsys_id
;
725 * Attach task 'tsk' to cgroup 'cont'
727 * Call holding cgroup_mutex. May take task_lock of
728 * the task 'pid' during call.
730 static int attach_task(struct cgroup
*cont
, struct task_struct
*tsk
)
733 struct cgroup_subsys
*ss
;
734 struct cgroup
*oldcont
;
735 struct css_set
*cg
= &tsk
->cgroups
;
736 struct cgroupfs_root
*root
= cont
->root
;
740 get_first_subsys(cont
, NULL
, &subsys_id
);
742 /* Nothing to do if the task is already in that cgroup */
743 oldcont
= task_cgroup(tsk
, subsys_id
);
747 for_each_subsys(root
, ss
) {
748 if (ss
->can_attach
) {
749 retval
= ss
->can_attach(ss
, cont
, tsk
);
757 if (tsk
->flags
& PF_EXITING
) {
761 /* Update the css_set pointers for the subsystems in this
763 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
764 if (root
->subsys_bits
& (1ull << i
)) {
765 /* Subsystem is in this hierarchy. So we want
766 * the subsystem state from the new
767 * cgroup. Transfer the refcount from the
769 atomic_inc(&cont
->count
);
770 atomic_dec(&cg
->subsys
[i
]->cgroup
->count
);
771 rcu_assign_pointer(cg
->subsys
[i
], cont
->subsys
[i
]);
776 for_each_subsys(root
, ss
) {
778 ss
->attach(ss
, cont
, oldcont
, tsk
);
787 * Attach task with pid 'pid' to cgroup 'cont'. Call with
788 * cgroup_mutex, may take task_lock of task
790 static int attach_task_by_pid(struct cgroup
*cont
, char *pidbuf
)
793 struct task_struct
*tsk
;
796 if (sscanf(pidbuf
, "%d", &pid
) != 1)
801 tsk
= find_task_by_pid(pid
);
802 if (!tsk
|| tsk
->flags
& PF_EXITING
) {
806 get_task_struct(tsk
);
809 if ((current
->euid
) && (current
->euid
!= tsk
->uid
)
810 && (current
->euid
!= tsk
->suid
)) {
811 put_task_struct(tsk
);
816 get_task_struct(tsk
);
819 ret
= attach_task(cont
, tsk
);
820 put_task_struct(tsk
);
824 /* The various types of files and directories in a cgroup file system */
826 enum cgroup_filetype
{
832 static ssize_t
cgroup_common_file_write(struct cgroup
*cont
,
835 const char __user
*userbuf
,
836 size_t nbytes
, loff_t
*unused_ppos
)
838 enum cgroup_filetype type
= cft
->private;
842 if (nbytes
>= PATH_MAX
)
845 /* +1 for nul-terminator */
846 buffer
= kmalloc(nbytes
+ 1, GFP_KERNEL
);
850 if (copy_from_user(buffer
, userbuf
, nbytes
)) {
854 buffer
[nbytes
] = 0; /* nul-terminate */
856 mutex_lock(&cgroup_mutex
);
858 if (cgroup_is_removed(cont
)) {
865 retval
= attach_task_by_pid(cont
, buffer
);
875 mutex_unlock(&cgroup_mutex
);
881 static ssize_t
cgroup_file_write(struct file
*file
, const char __user
*buf
,
882 size_t nbytes
, loff_t
*ppos
)
884 struct cftype
*cft
= __d_cft(file
->f_dentry
);
885 struct cgroup
*cont
= __d_cont(file
->f_dentry
->d_parent
);
892 return cft
->write(cont
, cft
, file
, buf
, nbytes
, ppos
);
895 static ssize_t
cgroup_read_uint(struct cgroup
*cont
, struct cftype
*cft
,
897 char __user
*buf
, size_t nbytes
,
901 u64 val
= cft
->read_uint(cont
, cft
);
902 int len
= sprintf(tmp
, "%llu\n", (unsigned long long) val
);
904 return simple_read_from_buffer(buf
, nbytes
, ppos
, tmp
, len
);
907 static ssize_t
cgroup_file_read(struct file
*file
, char __user
*buf
,
908 size_t nbytes
, loff_t
*ppos
)
910 struct cftype
*cft
= __d_cft(file
->f_dentry
);
911 struct cgroup
*cont
= __d_cont(file
->f_dentry
->d_parent
);
917 return cft
->read(cont
, cft
, file
, buf
, nbytes
, ppos
);
919 return cgroup_read_uint(cont
, cft
, file
, buf
, nbytes
, ppos
);
923 static int cgroup_file_open(struct inode
*inode
, struct file
*file
)
928 err
= generic_file_open(inode
, file
);
932 cft
= __d_cft(file
->f_dentry
);
936 err
= cft
->open(inode
, file
);
943 static int cgroup_file_release(struct inode
*inode
, struct file
*file
)
945 struct cftype
*cft
= __d_cft(file
->f_dentry
);
947 return cft
->release(inode
, file
);
952 * cgroup_rename - Only allow simple rename of directories in place.
954 static int cgroup_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
955 struct inode
*new_dir
, struct dentry
*new_dentry
)
957 if (!S_ISDIR(old_dentry
->d_inode
->i_mode
))
959 if (new_dentry
->d_inode
)
961 if (old_dir
!= new_dir
)
963 return simple_rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
966 static struct file_operations cgroup_file_operations
= {
967 .read
= cgroup_file_read
,
968 .write
= cgroup_file_write
,
969 .llseek
= generic_file_llseek
,
970 .open
= cgroup_file_open
,
971 .release
= cgroup_file_release
,
974 static struct inode_operations cgroup_dir_inode_operations
= {
975 .lookup
= simple_lookup
,
976 .mkdir
= cgroup_mkdir
,
977 .rmdir
= cgroup_rmdir
,
978 .rename
= cgroup_rename
,
981 static int cgroup_create_file(struct dentry
*dentry
, int mode
,
982 struct super_block
*sb
)
984 static struct dentry_operations cgroup_dops
= {
985 .d_iput
= cgroup_diput
,
995 inode
= cgroup_new_inode(mode
, sb
);
1000 inode
->i_op
= &cgroup_dir_inode_operations
;
1001 inode
->i_fop
= &simple_dir_operations
;
1003 /* start off with i_nlink == 2 (for "." entry) */
1006 /* start with the directory inode held, so that we can
1007 * populate it without racing with another mkdir */
1008 mutex_lock(&inode
->i_mutex
);
1009 } else if (S_ISREG(mode
)) {
1011 inode
->i_fop
= &cgroup_file_operations
;
1013 dentry
->d_op
= &cgroup_dops
;
1014 d_instantiate(dentry
, inode
);
1015 dget(dentry
); /* Extra count - pin the dentry in core */
1020 * cgroup_create_dir - create a directory for an object.
1021 * cont: the cgroup we create the directory for.
1022 * It must have a valid ->parent field
1023 * And we are going to fill its ->dentry field.
1024 * dentry: dentry of the new container
1025 * mode: mode to set on new directory.
1027 static int cgroup_create_dir(struct cgroup
*cont
, struct dentry
*dentry
,
1030 struct dentry
*parent
;
1033 parent
= cont
->parent
->dentry
;
1034 error
= cgroup_create_file(dentry
, S_IFDIR
| mode
, cont
->root
->sb
);
1036 dentry
->d_fsdata
= cont
;
1037 inc_nlink(parent
->d_inode
);
1038 cont
->dentry
= dentry
;
1046 int cgroup_add_file(struct cgroup
*cont
,
1047 struct cgroup_subsys
*subsys
,
1048 const struct cftype
*cft
)
1050 struct dentry
*dir
= cont
->dentry
;
1051 struct dentry
*dentry
;
1054 char name
[MAX_CGROUP_TYPE_NAMELEN
+ MAX_CFTYPE_NAME
+ 2] = { 0 };
1055 if (subsys
&& !test_bit(ROOT_NOPREFIX
, &cont
->root
->flags
)) {
1056 strcpy(name
, subsys
->name
);
1059 strcat(name
, cft
->name
);
1060 BUG_ON(!mutex_is_locked(&dir
->d_inode
->i_mutex
));
1061 dentry
= lookup_one_len(name
, dir
, strlen(name
));
1062 if (!IS_ERR(dentry
)) {
1063 error
= cgroup_create_file(dentry
, 0644 | S_IFREG
,
1066 dentry
->d_fsdata
= (void *)cft
;
1069 error
= PTR_ERR(dentry
);
1073 int cgroup_add_files(struct cgroup
*cont
,
1074 struct cgroup_subsys
*subsys
,
1075 const struct cftype cft
[],
1079 for (i
= 0; i
< count
; i
++) {
1080 err
= cgroup_add_file(cont
, subsys
, &cft
[i
]);
1087 /* Count the number of tasks in a cgroup. Could be made more
1088 * time-efficient but less space-efficient with more linked lists
1089 * running through each cgroup and the css_set structures that
1090 * referenced it. Must be called with tasklist_lock held for read or
1091 * write or in an rcu critical section.
1093 int __cgroup_task_count(const struct cgroup
*cont
)
1096 struct task_struct
*g
, *p
;
1097 struct cgroup_subsys_state
*css
;
1100 get_first_subsys(cont
, &css
, &subsys_id
);
1101 do_each_thread(g
, p
) {
1102 if (task_subsys_state(p
, subsys_id
) == css
)
1104 } while_each_thread(g
, p
);
1109 * Stuff for reading the 'tasks' file.
1111 * Reading this file can return large amounts of data if a cgroup has
1112 * *lots* of attached tasks. So it may need several calls to read(),
1113 * but we cannot guarantee that the information we produce is correct
1114 * unless we produce it entirely atomically.
1116 * Upon tasks file open(), a struct ctr_struct is allocated, that
1117 * will have a pointer to an array (also allocated here). The struct
1118 * ctr_struct * is stored in file->private_data. Its resources will
1119 * be freed by release() when the file is closed. The array is used
1120 * to sprintf the PIDs and then used by read().
1128 * Load into 'pidarray' up to 'npids' of the tasks using cgroup
1129 * 'cont'. Return actual number of pids loaded. No need to
1130 * task_lock(p) when reading out p->cgroup, since we're in an RCU
1131 * read section, so the css_set can't go away, and is
1132 * immutable after creation.
1134 static int pid_array_load(pid_t
*pidarray
, int npids
, struct cgroup
*cont
)
1137 struct task_struct
*g
, *p
;
1138 struct cgroup_subsys_state
*css
;
1141 get_first_subsys(cont
, &css
, &subsys_id
);
1143 do_each_thread(g
, p
) {
1144 if (task_subsys_state(p
, subsys_id
) == css
) {
1145 pidarray
[n
++] = pid_nr(task_pid(p
));
1146 if (unlikely(n
== npids
))
1149 } while_each_thread(g
, p
);
1156 static int cmppid(const void *a
, const void *b
)
1158 return *(pid_t
*)a
- *(pid_t
*)b
;
1162 * Convert array 'a' of 'npids' pid_t's to a string of newline separated
1163 * decimal pids in 'buf'. Don't write more than 'sz' chars, but return
1164 * count 'cnt' of how many chars would be written if buf were large enough.
1166 static int pid_array_to_buf(char *buf
, int sz
, pid_t
*a
, int npids
)
1171 for (i
= 0; i
< npids
; i
++)
1172 cnt
+= snprintf(buf
+ cnt
, max(sz
- cnt
, 0), "%d\n", a
[i
]);
1177 * Handle an open on 'tasks' file. Prepare a buffer listing the
1178 * process id's of tasks currently attached to the cgroup being opened.
1180 * Does not require any specific cgroup mutexes, and does not take any.
1182 static int cgroup_tasks_open(struct inode
*unused
, struct file
*file
)
1184 struct cgroup
*cont
= __d_cont(file
->f_dentry
->d_parent
);
1185 struct ctr_struct
*ctr
;
1190 if (!(file
->f_mode
& FMODE_READ
))
1193 ctr
= kmalloc(sizeof(*ctr
), GFP_KERNEL
);
1198 * If cgroup gets more users after we read count, we won't have
1199 * enough space - tough. This race is indistinguishable to the
1200 * caller from the case that the additional cgroup users didn't
1201 * show up until sometime later on.
1203 npids
= cgroup_task_count(cont
);
1205 pidarray
= kmalloc(npids
* sizeof(pid_t
), GFP_KERNEL
);
1209 npids
= pid_array_load(pidarray
, npids
, cont
);
1210 sort(pidarray
, npids
, sizeof(pid_t
), cmppid
, NULL
);
1212 /* Call pid_array_to_buf() twice, first just to get bufsz */
1213 ctr
->bufsz
= pid_array_to_buf(&c
, sizeof(c
), pidarray
, npids
) + 1;
1214 ctr
->buf
= kmalloc(ctr
->bufsz
, GFP_KERNEL
);
1217 ctr
->bufsz
= pid_array_to_buf(ctr
->buf
, ctr
->bufsz
, pidarray
, npids
);
1224 file
->private_data
= ctr
;
1235 static ssize_t
cgroup_tasks_read(struct cgroup
*cont
,
1237 struct file
*file
, char __user
*buf
,
1238 size_t nbytes
, loff_t
*ppos
)
1240 struct ctr_struct
*ctr
= file
->private_data
;
1242 return simple_read_from_buffer(buf
, nbytes
, ppos
, ctr
->buf
, ctr
->bufsz
);
1245 static int cgroup_tasks_release(struct inode
*unused_inode
,
1248 struct ctr_struct
*ctr
;
1250 if (file
->f_mode
& FMODE_READ
) {
1251 ctr
= file
->private_data
;
1259 * for the common functions, 'private' gives the type of file
1261 static struct cftype cft_tasks
= {
1263 .open
= cgroup_tasks_open
,
1264 .read
= cgroup_tasks_read
,
1265 .write
= cgroup_common_file_write
,
1266 .release
= cgroup_tasks_release
,
1267 .private = FILE_TASKLIST
,
1270 static int cgroup_populate_dir(struct cgroup
*cont
)
1273 struct cgroup_subsys
*ss
;
1275 /* First clear out any existing files */
1276 cgroup_clear_directory(cont
->dentry
);
1278 err
= cgroup_add_file(cont
, NULL
, &cft_tasks
);
1282 for_each_subsys(cont
->root
, ss
) {
1283 if (ss
->populate
&& (err
= ss
->populate(ss
, cont
)) < 0)
1290 static void init_cgroup_css(struct cgroup_subsys_state
*css
,
1291 struct cgroup_subsys
*ss
,
1292 struct cgroup
*cont
)
1295 atomic_set(&css
->refcnt
, 0);
1297 if (cont
== dummytop
)
1298 set_bit(CSS_ROOT
, &css
->flags
);
1299 BUG_ON(cont
->subsys
[ss
->subsys_id
]);
1300 cont
->subsys
[ss
->subsys_id
] = css
;
1304 * cgroup_create - create a cgroup
1305 * parent: cgroup that will be parent of the new cgroup.
1306 * name: name of the new cgroup. Will be strcpy'ed.
1307 * mode: mode to set on new inode
1309 * Must be called with the mutex on the parent inode held
1312 static long cgroup_create(struct cgroup
*parent
, struct dentry
*dentry
,
1315 struct cgroup
*cont
;
1316 struct cgroupfs_root
*root
= parent
->root
;
1318 struct cgroup_subsys
*ss
;
1319 struct super_block
*sb
= root
->sb
;
1321 cont
= kzalloc(sizeof(*cont
), GFP_KERNEL
);
1325 /* Grab a reference on the superblock so the hierarchy doesn't
1326 * get deleted on unmount if there are child cgroups. This
1327 * can be done outside cgroup_mutex, since the sb can't
1328 * disappear while someone has an open control file on the
1330 atomic_inc(&sb
->s_active
);
1332 mutex_lock(&cgroup_mutex
);
1335 INIT_LIST_HEAD(&cont
->sibling
);
1336 INIT_LIST_HEAD(&cont
->children
);
1338 cont
->parent
= parent
;
1339 cont
->root
= parent
->root
;
1340 cont
->top_cgroup
= parent
->top_cgroup
;
1342 for_each_subsys(root
, ss
) {
1343 struct cgroup_subsys_state
*css
= ss
->create(ss
, cont
);
1348 init_cgroup_css(css
, ss
, cont
);
1351 list_add(&cont
->sibling
, &cont
->parent
->children
);
1352 root
->number_of_cgroups
++;
1354 err
= cgroup_create_dir(cont
, dentry
, mode
);
1358 /* The cgroup directory was pre-locked for us */
1359 BUG_ON(!mutex_is_locked(&cont
->dentry
->d_inode
->i_mutex
));
1361 err
= cgroup_populate_dir(cont
);
1362 /* If err < 0, we have a half-filled directory - oh well ;) */
1364 mutex_unlock(&cgroup_mutex
);
1365 mutex_unlock(&cont
->dentry
->d_inode
->i_mutex
);
1371 list_del(&cont
->sibling
);
1372 root
->number_of_cgroups
--;
1376 for_each_subsys(root
, ss
) {
1377 if (cont
->subsys
[ss
->subsys_id
])
1378 ss
->destroy(ss
, cont
);
1381 mutex_unlock(&cgroup_mutex
);
1383 /* Release the reference count that we took on the superblock */
1384 deactivate_super(sb
);
1390 static int cgroup_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
1392 struct cgroup
*c_parent
= dentry
->d_parent
->d_fsdata
;
1394 /* the vfs holds inode->i_mutex already */
1395 return cgroup_create(c_parent
, dentry
, mode
| S_IFDIR
);
1398 static int cgroup_rmdir(struct inode
*unused_dir
, struct dentry
*dentry
)
1400 struct cgroup
*cont
= dentry
->d_fsdata
;
1402 struct cgroup
*parent
;
1403 struct cgroup_subsys
*ss
;
1404 struct super_block
*sb
;
1405 struct cgroupfs_root
*root
;
1408 /* the vfs holds both inode->i_mutex already */
1410 mutex_lock(&cgroup_mutex
);
1411 if (atomic_read(&cont
->count
) != 0) {
1412 mutex_unlock(&cgroup_mutex
);
1415 if (!list_empty(&cont
->children
)) {
1416 mutex_unlock(&cgroup_mutex
);
1420 parent
= cont
->parent
;
1424 /* Check the reference count on each subsystem. Since we
1425 * already established that there are no tasks in the
1426 * cgroup, if the css refcount is also 0, then there should
1427 * be no outstanding references, so the subsystem is safe to
1429 for_each_subsys(root
, ss
) {
1430 struct cgroup_subsys_state
*css
;
1431 css
= cont
->subsys
[ss
->subsys_id
];
1432 if (atomic_read(&css
->refcnt
)) {
1438 mutex_unlock(&cgroup_mutex
);
1442 for_each_subsys(root
, ss
) {
1443 if (cont
->subsys
[ss
->subsys_id
])
1444 ss
->destroy(ss
, cont
);
1447 set_bit(CONT_REMOVED
, &cont
->flags
);
1448 /* delete my sibling from parent->children */
1449 list_del(&cont
->sibling
);
1450 spin_lock(&cont
->dentry
->d_lock
);
1451 d
= dget(cont
->dentry
);
1452 cont
->dentry
= NULL
;
1453 spin_unlock(&d
->d_lock
);
1455 cgroup_d_remove_dir(d
);
1457 root
->number_of_cgroups
--;
1459 mutex_unlock(&cgroup_mutex
);
1460 /* Drop the active superblock reference that we took when we
1461 * created the cgroup */
1462 deactivate_super(sb
);
1466 static void cgroup_init_subsys(struct cgroup_subsys
*ss
)
1468 struct task_struct
*g
, *p
;
1469 struct cgroup_subsys_state
*css
;
1470 printk(KERN_ERR
"Initializing cgroup subsys %s\n", ss
->name
);
1472 /* Create the top cgroup state for this subsystem */
1473 ss
->root
= &rootnode
;
1474 css
= ss
->create(ss
, dummytop
);
1475 /* We don't handle early failures gracefully */
1476 BUG_ON(IS_ERR(css
));
1477 init_cgroup_css(css
, ss
, dummytop
);
1479 /* Update all tasks to contain a subsys pointer to this state
1480 * - since the subsystem is newly registered, all tasks are in
1481 * the subsystem's top cgroup. */
1483 /* If this subsystem requested that it be notified with fork
1484 * events, we should send it one now for every process in the
1487 read_lock(&tasklist_lock
);
1488 init_task
.cgroups
.subsys
[ss
->subsys_id
] = css
;
1490 ss
->fork(ss
, &init_task
);
1492 do_each_thread(g
, p
) {
1493 printk(KERN_INFO
"Setting task %p css to %p (%d)\n", css
, p
, p
->pid
);
1494 p
->cgroups
.subsys
[ss
->subsys_id
] = css
;
1497 } while_each_thread(g
, p
);
1498 read_unlock(&tasklist_lock
);
1500 need_forkexit_callback
|= ss
->fork
|| ss
->exit
;
1506 * cgroup_init_early - initialize cgroups at system boot, and
1507 * initialize any subsystems that request early init.
1509 int __init
cgroup_init_early(void)
1512 init_cgroup_root(&rootnode
);
1513 list_add(&rootnode
.root_list
, &roots
);
1515 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
1516 struct cgroup_subsys
*ss
= subsys
[i
];
1519 BUG_ON(strlen(ss
->name
) > MAX_CGROUP_TYPE_NAMELEN
);
1520 BUG_ON(!ss
->create
);
1521 BUG_ON(!ss
->destroy
);
1522 if (ss
->subsys_id
!= i
) {
1523 printk(KERN_ERR
"Subsys %s id == %d\n",
1524 ss
->name
, ss
->subsys_id
);
1529 cgroup_init_subsys(ss
);
1535 * cgroup_init - register cgroup filesystem and /proc file, and
1536 * initialize any subsystems that didn't request early init.
1538 int __init
cgroup_init(void)
1543 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
1544 struct cgroup_subsys
*ss
= subsys
[i
];
1545 if (!ss
->early_init
)
1546 cgroup_init_subsys(ss
);
1549 err
= register_filesystem(&cgroup_fs_type
);