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/proc_fs.h>
37 #include <linux/rcupdate.h>
38 #include <linux/sched.h>
39 #include <linux/backing-dev.h>
40 #include <linux/seq_file.h>
41 #include <linux/slab.h>
42 #include <linux/magic.h>
43 #include <linux/spinlock.h>
44 #include <linux/string.h>
45 #include <linux/sort.h>
46 #include <linux/kmod.h>
47 #include <linux/delayacct.h>
48 #include <linux/cgroupstats.h>
50 #include <asm/atomic.h>
52 static DEFINE_MUTEX(cgroup_mutex
);
54 /* Generate an array of cgroup subsystem pointers */
55 #define SUBSYS(_x) &_x ## _subsys,
57 static struct cgroup_subsys
*subsys
[] = {
58 #include <linux/cgroup_subsys.h>
62 * A cgroupfs_root represents the root of a cgroup hierarchy,
63 * and may be associated with a superblock to form an active
66 struct cgroupfs_root
{
67 struct super_block
*sb
;
70 * The bitmask of subsystems intended to be attached to this
73 unsigned long subsys_bits
;
75 /* The bitmask of subsystems currently attached to this hierarchy */
76 unsigned long actual_subsys_bits
;
78 /* A list running through the attached subsystems */
79 struct list_head subsys_list
;
81 /* The root cgroup for this hierarchy */
82 struct cgroup top_cgroup
;
84 /* Tracks how many cgroups are currently defined in hierarchy.*/
85 int number_of_cgroups
;
87 /* A list running through the mounted hierarchies */
88 struct list_head root_list
;
90 /* Hierarchy-specific flags */
93 /* The path to use for release notifications. No locking
94 * between setting and use - so if userspace updates this
95 * while child cgroups exist, you could miss a
96 * notification. We ensure that it's always a valid
97 * NUL-terminated string */
98 char release_agent_path
[PATH_MAX
];
103 * The "rootnode" hierarchy is the "dummy hierarchy", reserved for the
104 * subsystems that are otherwise unattached - it never has more than a
105 * single cgroup, and all tasks are part of that cgroup.
107 static struct cgroupfs_root rootnode
;
109 /* The list of hierarchy roots */
111 static LIST_HEAD(roots
);
112 static int root_count
;
114 /* dummytop is a shorthand for the dummy hierarchy's top cgroup */
115 #define dummytop (&rootnode.top_cgroup)
117 /* This flag indicates whether tasks in the fork and exit paths should
118 * take callback_mutex and check for fork/exit handlers to call. This
119 * avoids us having to do extra work in the fork/exit path if none of the
120 * subsystems need to be called.
122 static int need_forkexit_callback
;
124 /* bits in struct cgroup flags field */
126 /* Control Group is dead */
128 /* Control Group has previously had a child cgroup or a task,
129 * but no longer (only if CGRP_NOTIFY_ON_RELEASE is set) */
131 /* Control Group requires release notifications to userspace */
132 CGRP_NOTIFY_ON_RELEASE
,
135 /* convenient tests for these bits */
136 inline int cgroup_is_removed(const struct cgroup
*cgrp
)
138 return test_bit(CGRP_REMOVED
, &cgrp
->flags
);
141 /* bits in struct cgroupfs_root flags field */
143 ROOT_NOPREFIX
, /* mounted subsystems have no named prefix */
146 inline int cgroup_is_releasable(const struct cgroup
*cgrp
)
149 (1 << CGRP_RELEASABLE
) |
150 (1 << CGRP_NOTIFY_ON_RELEASE
);
151 return (cgrp
->flags
& bits
) == bits
;
154 inline int notify_on_release(const struct cgroup
*cgrp
)
156 return test_bit(CGRP_NOTIFY_ON_RELEASE
, &cgrp
->flags
);
160 * for_each_subsys() allows you to iterate on each subsystem attached to
161 * an active hierarchy
163 #define for_each_subsys(_root, _ss) \
164 list_for_each_entry(_ss, &_root->subsys_list, sibling)
166 /* for_each_root() allows you to iterate across the active hierarchies */
167 #define for_each_root(_root) \
168 list_for_each_entry(_root, &roots, root_list)
170 /* the list of cgroups eligible for automatic release. Protected by
171 * release_list_lock */
172 static LIST_HEAD(release_list
);
173 static DEFINE_SPINLOCK(release_list_lock
);
174 static void cgroup_release_agent(struct work_struct
*work
);
175 static DECLARE_WORK(release_agent_work
, cgroup_release_agent
);
176 static void check_for_release(struct cgroup
*cgrp
);
178 /* Link structure for associating css_set objects with cgroups */
179 struct cg_cgroup_link
{
181 * List running through cg_cgroup_links associated with a
182 * cgroup, anchored on cgroup->css_sets
184 struct list_head cgrp_link_list
;
186 * List running through cg_cgroup_links pointing at a
187 * single css_set object, anchored on css_set->cg_links
189 struct list_head cg_link_list
;
193 /* The default css_set - used by init and its children prior to any
194 * hierarchies being mounted. It contains a pointer to the root state
195 * for each subsystem. Also used to anchor the list of css_sets. Not
196 * reference-counted, to improve performance when child cgroups
197 * haven't been created.
200 static struct css_set init_css_set
;
201 static struct cg_cgroup_link init_css_set_link
;
203 /* css_set_lock protects the list of css_set objects, and the
204 * chain of tasks off each css_set. Nests outside task->alloc_lock
205 * due to cgroup_iter_start() */
206 static DEFINE_RWLOCK(css_set_lock
);
207 static int css_set_count
;
209 /* We don't maintain the lists running through each css_set to its
210 * task until after the first call to cgroup_iter_start(). This
211 * reduces the fork()/exit() overhead for people who have cgroups
212 * compiled into their kernel but not actually in use */
213 static int use_task_css_set_links
;
215 /* When we create or destroy a css_set, the operation simply
216 * takes/releases a reference count on all the cgroups referenced
217 * by subsystems in this css_set. This can end up multiple-counting
218 * some cgroups, but that's OK - the ref-count is just a
219 * busy/not-busy indicator; ensuring that we only count each cgroup
220 * once would require taking a global lock to ensure that no
221 * subsystems moved between hierarchies while we were doing so.
223 * Possible TODO: decide at boot time based on the number of
224 * registered subsystems and the number of CPUs or NUMA nodes whether
225 * it's better for performance to ref-count every subsystem, or to
226 * take a global lock and only add one ref count to each hierarchy.
230 * unlink a css_set from the list and free it
232 static void unlink_css_set(struct css_set
*cg
)
234 write_lock(&css_set_lock
);
237 while (!list_empty(&cg
->cg_links
)) {
238 struct cg_cgroup_link
*link
;
239 link
= list_entry(cg
->cg_links
.next
,
240 struct cg_cgroup_link
, cg_link_list
);
241 list_del(&link
->cg_link_list
);
242 list_del(&link
->cgrp_link_list
);
245 write_unlock(&css_set_lock
);
248 static void __release_css_set(struct kref
*k
, int taskexit
)
251 struct css_set
*cg
= container_of(k
, struct css_set
, ref
);
256 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
257 struct cgroup
*cgrp
= cg
->subsys
[i
]->cgroup
;
258 if (atomic_dec_and_test(&cgrp
->count
) &&
259 notify_on_release(cgrp
)) {
261 set_bit(CGRP_RELEASABLE
, &cgrp
->flags
);
262 check_for_release(cgrp
);
269 static void release_css_set(struct kref
*k
)
271 __release_css_set(k
, 0);
274 static void release_css_set_taskexit(struct kref
*k
)
276 __release_css_set(k
, 1);
280 * refcounted get/put for css_set objects
282 static inline void get_css_set(struct css_set
*cg
)
287 static inline void put_css_set(struct css_set
*cg
)
289 kref_put(&cg
->ref
, release_css_set
);
292 static inline void put_css_set_taskexit(struct css_set
*cg
)
294 kref_put(&cg
->ref
, release_css_set_taskexit
);
298 * find_existing_css_set() is a helper for
299 * find_css_set(), and checks to see whether an existing
300 * css_set is suitable. This currently walks a linked-list for
301 * simplicity; a later patch will use a hash table for better
304 * oldcg: the cgroup group that we're using before the cgroup
307 * cgrp: the cgroup that we're moving into
309 * template: location in which to build the desired set of subsystem
310 * state objects for the new cgroup group
313 static struct css_set
*find_existing_css_set(
314 struct css_set
*oldcg
,
316 struct cgroup_subsys_state
*template[])
319 struct cgroupfs_root
*root
= cgrp
->root
;
320 struct list_head
*l
= &init_css_set
.list
;
322 /* Built the set of subsystem state objects that we want to
323 * see in the new css_set */
324 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
325 if (root
->subsys_bits
& (1ull << i
)) {
326 /* Subsystem is in this hierarchy. So we want
327 * the subsystem state from the new
329 template[i
] = cgrp
->subsys
[i
];
331 /* Subsystem is not in this hierarchy, so we
332 * don't want to change the subsystem state */
333 template[i
] = oldcg
->subsys
[i
];
337 /* Look through existing cgroup groups to find one to reuse */
340 list_entry(l
, struct css_set
, list
);
342 if (!memcmp(template, cg
->subsys
, sizeof(cg
->subsys
))) {
343 /* All subsystems matched */
346 /* Try the next cgroup group */
348 } while (l
!= &init_css_set
.list
);
350 /* No existing cgroup group matched */
355 * allocate_cg_links() allocates "count" cg_cgroup_link structures
356 * and chains them on tmp through their cgrp_link_list fields. Returns 0 on
357 * success or a negative error
360 static int allocate_cg_links(int count
, struct list_head
*tmp
)
362 struct cg_cgroup_link
*link
;
365 for (i
= 0; i
< count
; i
++) {
366 link
= kmalloc(sizeof(*link
), GFP_KERNEL
);
368 while (!list_empty(tmp
)) {
369 link
= list_entry(tmp
->next
,
370 struct cg_cgroup_link
,
372 list_del(&link
->cgrp_link_list
);
377 list_add(&link
->cgrp_link_list
, tmp
);
382 static void free_cg_links(struct list_head
*tmp
)
384 while (!list_empty(tmp
)) {
385 struct cg_cgroup_link
*link
;
386 link
= list_entry(tmp
->next
,
387 struct cg_cgroup_link
,
389 list_del(&link
->cgrp_link_list
);
395 * find_css_set() takes an existing cgroup group and a
396 * cgroup object, and returns a css_set object that's
397 * equivalent to the old group, but with the given cgroup
398 * substituted into the appropriate hierarchy. Must be called with
402 static struct css_set
*find_css_set(
403 struct css_set
*oldcg
, struct cgroup
*cgrp
)
406 struct cgroup_subsys_state
*template[CGROUP_SUBSYS_COUNT
];
409 struct list_head tmp_cg_links
;
410 struct cg_cgroup_link
*link
;
412 /* First see if we already have a cgroup group that matches
414 write_lock(&css_set_lock
);
415 res
= find_existing_css_set(oldcg
, cgrp
, template);
418 write_unlock(&css_set_lock
);
423 res
= kmalloc(sizeof(*res
), GFP_KERNEL
);
427 /* Allocate all the cg_cgroup_link objects that we'll need */
428 if (allocate_cg_links(root_count
, &tmp_cg_links
) < 0) {
433 kref_init(&res
->ref
);
434 INIT_LIST_HEAD(&res
->cg_links
);
435 INIT_LIST_HEAD(&res
->tasks
);
437 /* Copy the set of subsystem state objects generated in
438 * find_existing_css_set() */
439 memcpy(res
->subsys
, template, sizeof(res
->subsys
));
441 write_lock(&css_set_lock
);
442 /* Add reference counts and links from the new css_set. */
443 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
444 struct cgroup
*cgrp
= res
->subsys
[i
]->cgroup
;
445 struct cgroup_subsys
*ss
= subsys
[i
];
446 atomic_inc(&cgrp
->count
);
448 * We want to add a link once per cgroup, so we
449 * only do it for the first subsystem in each
452 if (ss
->root
->subsys_list
.next
== &ss
->sibling
) {
453 BUG_ON(list_empty(&tmp_cg_links
));
454 link
= list_entry(tmp_cg_links
.next
,
455 struct cg_cgroup_link
,
457 list_del(&link
->cgrp_link_list
);
458 list_add(&link
->cgrp_link_list
, &cgrp
->css_sets
);
460 list_add(&link
->cg_link_list
, &res
->cg_links
);
463 if (list_empty(&rootnode
.subsys_list
)) {
464 link
= list_entry(tmp_cg_links
.next
,
465 struct cg_cgroup_link
,
467 list_del(&link
->cgrp_link_list
);
468 list_add(&link
->cgrp_link_list
, &dummytop
->css_sets
);
470 list_add(&link
->cg_link_list
, &res
->cg_links
);
473 BUG_ON(!list_empty(&tmp_cg_links
));
475 /* Link this cgroup group into the list */
476 list_add(&res
->list
, &init_css_set
.list
);
478 INIT_LIST_HEAD(&res
->tasks
);
479 write_unlock(&css_set_lock
);
485 * There is one global cgroup mutex. We also require taking
486 * task_lock() when dereferencing a task's cgroup subsys pointers.
487 * See "The task_lock() exception", at the end of this comment.
489 * A task must hold cgroup_mutex to modify cgroups.
491 * Any task can increment and decrement the count field without lock.
492 * So in general, code holding cgroup_mutex can't rely on the count
493 * field not changing. However, if the count goes to zero, then only
494 * attach_task() can increment it again. Because a count of zero
495 * means that no tasks are currently attached, therefore there is no
496 * way a task attached to that cgroup can fork (the other way to
497 * increment the count). So code holding cgroup_mutex can safely
498 * assume that if the count is zero, it will stay zero. Similarly, if
499 * a task holds cgroup_mutex on a cgroup with zero count, it
500 * knows that the cgroup won't be removed, as cgroup_rmdir()
503 * The cgroup_common_file_write handler for operations that modify
504 * the cgroup hierarchy holds cgroup_mutex across the entire operation,
505 * single threading all such cgroup modifications across the system.
507 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
508 * (usually) take cgroup_mutex. These are the two most performance
509 * critical pieces of code here. The exception occurs on cgroup_exit(),
510 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
511 * is taken, and if the cgroup count is zero, a usermode call made
512 * to /sbin/cgroup_release_agent with the name of the cgroup (path
513 * relative to the root of cgroup file system) as the argument.
515 * A cgroup can only be deleted if both its 'count' of using tasks
516 * is zero, and its list of 'children' cgroups is empty. Since all
517 * tasks in the system use _some_ cgroup, and since there is always at
518 * least one task in the system (init, pid == 1), therefore, top_cgroup
519 * always has either children cgroups and/or using tasks. So we don't
520 * need a special hack to ensure that top_cgroup cannot be deleted.
522 * The task_lock() exception
524 * The need for this exception arises from the action of
525 * attach_task(), which overwrites one tasks cgroup pointer with
526 * another. It does so using cgroup_mutexe, however there are
527 * several performance critical places that need to reference
528 * task->cgroup without the expense of grabbing a system global
529 * mutex. Therefore except as noted below, when dereferencing or, as
530 * in attach_task(), modifying a task'ss cgroup pointer we use
531 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
532 * the task_struct routinely used for such matters.
534 * P.S. One more locking exception. RCU is used to guard the
535 * update of a tasks cgroup pointer by attach_task()
539 * cgroup_lock - lock out any changes to cgroup structures
543 void cgroup_lock(void)
545 mutex_lock(&cgroup_mutex
);
549 * cgroup_unlock - release lock on cgroup changes
551 * Undo the lock taken in a previous cgroup_lock() call.
554 void cgroup_unlock(void)
556 mutex_unlock(&cgroup_mutex
);
560 * A couple of forward declarations required, due to cyclic reference loop:
561 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
562 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
566 static int cgroup_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
);
567 static int cgroup_rmdir(struct inode
*unused_dir
, struct dentry
*dentry
);
568 static int cgroup_populate_dir(struct cgroup
*cgrp
);
569 static struct inode_operations cgroup_dir_inode_operations
;
570 static struct file_operations proc_cgroupstats_operations
;
572 static struct backing_dev_info cgroup_backing_dev_info
= {
573 .capabilities
= BDI_CAP_NO_ACCT_DIRTY
| BDI_CAP_NO_WRITEBACK
,
576 static struct inode
*cgroup_new_inode(mode_t mode
, struct super_block
*sb
)
578 struct inode
*inode
= new_inode(sb
);
581 inode
->i_mode
= mode
;
582 inode
->i_uid
= current
->fsuid
;
583 inode
->i_gid
= current
->fsgid
;
585 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
586 inode
->i_mapping
->backing_dev_info
= &cgroup_backing_dev_info
;
591 static void cgroup_diput(struct dentry
*dentry
, struct inode
*inode
)
593 /* is dentry a directory ? if so, kfree() associated cgroup */
594 if (S_ISDIR(inode
->i_mode
)) {
595 struct cgroup
*cgrp
= dentry
->d_fsdata
;
596 BUG_ON(!(cgroup_is_removed(cgrp
)));
597 /* It's possible for external users to be holding css
598 * reference counts on a cgroup; css_put() needs to
599 * be able to access the cgroup after decrementing
600 * the reference count in order to know if it needs to
601 * queue the cgroup to be handled by the release
609 static void remove_dir(struct dentry
*d
)
611 struct dentry
*parent
= dget(d
->d_parent
);
614 simple_rmdir(parent
->d_inode
, d
);
618 static void cgroup_clear_directory(struct dentry
*dentry
)
620 struct list_head
*node
;
622 BUG_ON(!mutex_is_locked(&dentry
->d_inode
->i_mutex
));
623 spin_lock(&dcache_lock
);
624 node
= dentry
->d_subdirs
.next
;
625 while (node
!= &dentry
->d_subdirs
) {
626 struct dentry
*d
= list_entry(node
, struct dentry
, d_u
.d_child
);
629 /* This should never be called on a cgroup
630 * directory with child cgroups */
631 BUG_ON(d
->d_inode
->i_mode
& S_IFDIR
);
633 spin_unlock(&dcache_lock
);
635 simple_unlink(dentry
->d_inode
, d
);
637 spin_lock(&dcache_lock
);
639 node
= dentry
->d_subdirs
.next
;
641 spin_unlock(&dcache_lock
);
645 * NOTE : the dentry must have been dget()'ed
647 static void cgroup_d_remove_dir(struct dentry
*dentry
)
649 cgroup_clear_directory(dentry
);
651 spin_lock(&dcache_lock
);
652 list_del_init(&dentry
->d_u
.d_child
);
653 spin_unlock(&dcache_lock
);
657 static int rebind_subsystems(struct cgroupfs_root
*root
,
658 unsigned long final_bits
)
660 unsigned long added_bits
, removed_bits
;
661 struct cgroup
*cgrp
= &root
->top_cgroup
;
664 removed_bits
= root
->actual_subsys_bits
& ~final_bits
;
665 added_bits
= final_bits
& ~root
->actual_subsys_bits
;
666 /* Check that any added subsystems are currently free */
667 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
668 unsigned long long bit
= 1ull << i
;
669 struct cgroup_subsys
*ss
= subsys
[i
];
670 if (!(bit
& added_bits
))
672 if (ss
->root
!= &rootnode
) {
673 /* Subsystem isn't free */
678 /* Currently we don't handle adding/removing subsystems when
679 * any child cgroups exist. This is theoretically supportable
680 * but involves complex error handling, so it's being left until
682 if (!list_empty(&cgrp
->children
))
685 /* Process each subsystem */
686 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
687 struct cgroup_subsys
*ss
= subsys
[i
];
688 unsigned long bit
= 1UL << i
;
689 if (bit
& added_bits
) {
690 /* We're binding this subsystem to this hierarchy */
691 BUG_ON(cgrp
->subsys
[i
]);
692 BUG_ON(!dummytop
->subsys
[i
]);
693 BUG_ON(dummytop
->subsys
[i
]->cgroup
!= dummytop
);
694 cgrp
->subsys
[i
] = dummytop
->subsys
[i
];
695 cgrp
->subsys
[i
]->cgroup
= cgrp
;
696 list_add(&ss
->sibling
, &root
->subsys_list
);
697 rcu_assign_pointer(ss
->root
, root
);
701 } else if (bit
& removed_bits
) {
702 /* We're removing this subsystem */
703 BUG_ON(cgrp
->subsys
[i
] != dummytop
->subsys
[i
]);
704 BUG_ON(cgrp
->subsys
[i
]->cgroup
!= cgrp
);
706 ss
->bind(ss
, dummytop
);
707 dummytop
->subsys
[i
]->cgroup
= dummytop
;
708 cgrp
->subsys
[i
] = NULL
;
709 rcu_assign_pointer(subsys
[i
]->root
, &rootnode
);
710 list_del(&ss
->sibling
);
711 } else if (bit
& final_bits
) {
712 /* Subsystem state should already exist */
713 BUG_ON(!cgrp
->subsys
[i
]);
715 /* Subsystem state shouldn't exist */
716 BUG_ON(cgrp
->subsys
[i
]);
719 root
->subsys_bits
= root
->actual_subsys_bits
= final_bits
;
725 static int cgroup_show_options(struct seq_file
*seq
, struct vfsmount
*vfs
)
727 struct cgroupfs_root
*root
= vfs
->mnt_sb
->s_fs_info
;
728 struct cgroup_subsys
*ss
;
730 mutex_lock(&cgroup_mutex
);
731 for_each_subsys(root
, ss
)
732 seq_printf(seq
, ",%s", ss
->name
);
733 if (test_bit(ROOT_NOPREFIX
, &root
->flags
))
734 seq_puts(seq
, ",noprefix");
735 if (strlen(root
->release_agent_path
))
736 seq_printf(seq
, ",release_agent=%s", root
->release_agent_path
);
737 mutex_unlock(&cgroup_mutex
);
741 struct cgroup_sb_opts
{
742 unsigned long subsys_bits
;
747 /* Convert a hierarchy specifier into a bitmask of subsystems and
749 static int parse_cgroupfs_options(char *data
,
750 struct cgroup_sb_opts
*opts
)
752 char *token
, *o
= data
?: "all";
754 opts
->subsys_bits
= 0;
756 opts
->release_agent
= NULL
;
758 while ((token
= strsep(&o
, ",")) != NULL
) {
761 if (!strcmp(token
, "all")) {
762 opts
->subsys_bits
= (1 << CGROUP_SUBSYS_COUNT
) - 1;
763 } else if (!strcmp(token
, "noprefix")) {
764 set_bit(ROOT_NOPREFIX
, &opts
->flags
);
765 } else if (!strncmp(token
, "release_agent=", 14)) {
766 /* Specifying two release agents is forbidden */
767 if (opts
->release_agent
)
769 opts
->release_agent
= kzalloc(PATH_MAX
, GFP_KERNEL
);
770 if (!opts
->release_agent
)
772 strncpy(opts
->release_agent
, token
+ 14, PATH_MAX
- 1);
773 opts
->release_agent
[PATH_MAX
- 1] = 0;
775 struct cgroup_subsys
*ss
;
777 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
779 if (!strcmp(token
, ss
->name
)) {
780 set_bit(i
, &opts
->subsys_bits
);
784 if (i
== CGROUP_SUBSYS_COUNT
)
789 /* We can't have an empty hierarchy */
790 if (!opts
->subsys_bits
)
796 static int cgroup_remount(struct super_block
*sb
, int *flags
, char *data
)
799 struct cgroupfs_root
*root
= sb
->s_fs_info
;
800 struct cgroup
*cgrp
= &root
->top_cgroup
;
801 struct cgroup_sb_opts opts
;
803 mutex_lock(&cgrp
->dentry
->d_inode
->i_mutex
);
804 mutex_lock(&cgroup_mutex
);
806 /* See what subsystems are wanted */
807 ret
= parse_cgroupfs_options(data
, &opts
);
811 /* Don't allow flags to change at remount */
812 if (opts
.flags
!= root
->flags
) {
817 ret
= rebind_subsystems(root
, opts
.subsys_bits
);
819 /* (re)populate subsystem files */
821 cgroup_populate_dir(cgrp
);
823 if (opts
.release_agent
)
824 strcpy(root
->release_agent_path
, opts
.release_agent
);
826 if (opts
.release_agent
)
827 kfree(opts
.release_agent
);
828 mutex_unlock(&cgroup_mutex
);
829 mutex_unlock(&cgrp
->dentry
->d_inode
->i_mutex
);
833 static struct super_operations cgroup_ops
= {
834 .statfs
= simple_statfs
,
835 .drop_inode
= generic_delete_inode
,
836 .show_options
= cgroup_show_options
,
837 .remount_fs
= cgroup_remount
,
840 static void init_cgroup_root(struct cgroupfs_root
*root
)
842 struct cgroup
*cgrp
= &root
->top_cgroup
;
843 INIT_LIST_HEAD(&root
->subsys_list
);
844 INIT_LIST_HEAD(&root
->root_list
);
845 root
->number_of_cgroups
= 1;
847 cgrp
->top_cgroup
= cgrp
;
848 INIT_LIST_HEAD(&cgrp
->sibling
);
849 INIT_LIST_HEAD(&cgrp
->children
);
850 INIT_LIST_HEAD(&cgrp
->css_sets
);
851 INIT_LIST_HEAD(&cgrp
->release_list
);
854 static int cgroup_test_super(struct super_block
*sb
, void *data
)
856 struct cgroupfs_root
*new = data
;
857 struct cgroupfs_root
*root
= sb
->s_fs_info
;
859 /* First check subsystems */
860 if (new->subsys_bits
!= root
->subsys_bits
)
863 /* Next check flags */
864 if (new->flags
!= root
->flags
)
870 static int cgroup_set_super(struct super_block
*sb
, void *data
)
873 struct cgroupfs_root
*root
= data
;
875 ret
= set_anon_super(sb
, NULL
);
879 sb
->s_fs_info
= root
;
882 sb
->s_blocksize
= PAGE_CACHE_SIZE
;
883 sb
->s_blocksize_bits
= PAGE_CACHE_SHIFT
;
884 sb
->s_magic
= CGROUP_SUPER_MAGIC
;
885 sb
->s_op
= &cgroup_ops
;
890 static int cgroup_get_rootdir(struct super_block
*sb
)
892 struct inode
*inode
=
893 cgroup_new_inode(S_IFDIR
| S_IRUGO
| S_IXUGO
| S_IWUSR
, sb
);
894 struct dentry
*dentry
;
899 inode
->i_op
= &simple_dir_inode_operations
;
900 inode
->i_fop
= &simple_dir_operations
;
901 inode
->i_op
= &cgroup_dir_inode_operations
;
902 /* directories start off with i_nlink == 2 (for "." entry) */
904 dentry
= d_alloc_root(inode
);
913 static int cgroup_get_sb(struct file_system_type
*fs_type
,
914 int flags
, const char *unused_dev_name
,
915 void *data
, struct vfsmount
*mnt
)
917 struct cgroup_sb_opts opts
;
919 struct super_block
*sb
;
920 struct cgroupfs_root
*root
;
921 struct list_head tmp_cg_links
, *l
;
922 INIT_LIST_HEAD(&tmp_cg_links
);
924 /* First find the desired set of subsystems */
925 ret
= parse_cgroupfs_options(data
, &opts
);
927 if (opts
.release_agent
)
928 kfree(opts
.release_agent
);
932 root
= kzalloc(sizeof(*root
), GFP_KERNEL
);
936 init_cgroup_root(root
);
937 root
->subsys_bits
= opts
.subsys_bits
;
938 root
->flags
= opts
.flags
;
939 if (opts
.release_agent
) {
940 strcpy(root
->release_agent_path
, opts
.release_agent
);
941 kfree(opts
.release_agent
);
944 sb
= sget(fs_type
, cgroup_test_super
, cgroup_set_super
, root
);
951 if (sb
->s_fs_info
!= root
) {
952 /* Reusing an existing superblock */
953 BUG_ON(sb
->s_root
== NULL
);
958 struct cgroup
*cgrp
= &root
->top_cgroup
;
961 BUG_ON(sb
->s_root
!= NULL
);
963 ret
= cgroup_get_rootdir(sb
);
966 inode
= sb
->s_root
->d_inode
;
968 mutex_lock(&inode
->i_mutex
);
969 mutex_lock(&cgroup_mutex
);
972 * We're accessing css_set_count without locking
973 * css_set_lock here, but that's OK - it can only be
974 * increased by someone holding cgroup_lock, and
975 * that's us. The worst that can happen is that we
976 * have some link structures left over
978 ret
= allocate_cg_links(css_set_count
, &tmp_cg_links
);
980 mutex_unlock(&cgroup_mutex
);
981 mutex_unlock(&inode
->i_mutex
);
985 ret
= rebind_subsystems(root
, root
->subsys_bits
);
987 mutex_unlock(&cgroup_mutex
);
988 mutex_unlock(&inode
->i_mutex
);
992 /* EBUSY should be the only error here */
995 list_add(&root
->root_list
, &roots
);
998 sb
->s_root
->d_fsdata
= &root
->top_cgroup
;
999 root
->top_cgroup
.dentry
= sb
->s_root
;
1001 /* Link the top cgroup in this hierarchy into all
1002 * the css_set objects */
1003 write_lock(&css_set_lock
);
1004 l
= &init_css_set
.list
;
1007 struct cg_cgroup_link
*link
;
1008 cg
= list_entry(l
, struct css_set
, list
);
1009 BUG_ON(list_empty(&tmp_cg_links
));
1010 link
= list_entry(tmp_cg_links
.next
,
1011 struct cg_cgroup_link
,
1013 list_del(&link
->cgrp_link_list
);
1015 list_add(&link
->cgrp_link_list
,
1016 &root
->top_cgroup
.css_sets
);
1017 list_add(&link
->cg_link_list
, &cg
->cg_links
);
1019 } while (l
!= &init_css_set
.list
);
1020 write_unlock(&css_set_lock
);
1022 free_cg_links(&tmp_cg_links
);
1024 BUG_ON(!list_empty(&cgrp
->sibling
));
1025 BUG_ON(!list_empty(&cgrp
->children
));
1026 BUG_ON(root
->number_of_cgroups
!= 1);
1028 cgroup_populate_dir(cgrp
);
1029 mutex_unlock(&inode
->i_mutex
);
1030 mutex_unlock(&cgroup_mutex
);
1033 return simple_set_mnt(mnt
, sb
);
1036 up_write(&sb
->s_umount
);
1037 deactivate_super(sb
);
1038 free_cg_links(&tmp_cg_links
);
1042 static void cgroup_kill_sb(struct super_block
*sb
) {
1043 struct cgroupfs_root
*root
= sb
->s_fs_info
;
1044 struct cgroup
*cgrp
= &root
->top_cgroup
;
1049 BUG_ON(root
->number_of_cgroups
!= 1);
1050 BUG_ON(!list_empty(&cgrp
->children
));
1051 BUG_ON(!list_empty(&cgrp
->sibling
));
1053 mutex_lock(&cgroup_mutex
);
1055 /* Rebind all subsystems back to the default hierarchy */
1056 ret
= rebind_subsystems(root
, 0);
1057 /* Shouldn't be able to fail ... */
1061 * Release all the links from css_sets to this hierarchy's
1064 write_lock(&css_set_lock
);
1065 while (!list_empty(&cgrp
->css_sets
)) {
1066 struct cg_cgroup_link
*link
;
1067 link
= list_entry(cgrp
->css_sets
.next
,
1068 struct cg_cgroup_link
, cgrp_link_list
);
1069 list_del(&link
->cg_link_list
);
1070 list_del(&link
->cgrp_link_list
);
1073 write_unlock(&css_set_lock
);
1075 if (!list_empty(&root
->root_list
)) {
1076 list_del(&root
->root_list
);
1079 mutex_unlock(&cgroup_mutex
);
1082 kill_litter_super(sb
);
1085 static struct file_system_type cgroup_fs_type
= {
1087 .get_sb
= cgroup_get_sb
,
1088 .kill_sb
= cgroup_kill_sb
,
1091 static inline struct cgroup
*__d_cgrp(struct dentry
*dentry
)
1093 return dentry
->d_fsdata
;
1096 static inline struct cftype
*__d_cft(struct dentry
*dentry
)
1098 return dentry
->d_fsdata
;
1102 * Called with cgroup_mutex held. Writes path of cgroup into buf.
1103 * Returns 0 on success, -errno on error.
1105 int cgroup_path(const struct cgroup
*cgrp
, char *buf
, int buflen
)
1109 if (cgrp
== dummytop
) {
1111 * Inactive subsystems have no dentry for their root
1118 start
= buf
+ buflen
;
1122 int len
= cgrp
->dentry
->d_name
.len
;
1123 if ((start
-= len
) < buf
)
1124 return -ENAMETOOLONG
;
1125 memcpy(start
, cgrp
->dentry
->d_name
.name
, len
);
1126 cgrp
= cgrp
->parent
;
1132 return -ENAMETOOLONG
;
1135 memmove(buf
, start
, buf
+ buflen
- start
);
1140 * Return the first subsystem attached to a cgroup's hierarchy, and
1144 static void get_first_subsys(const struct cgroup
*cgrp
,
1145 struct cgroup_subsys_state
**css
, int *subsys_id
)
1147 const struct cgroupfs_root
*root
= cgrp
->root
;
1148 const struct cgroup_subsys
*test_ss
;
1149 BUG_ON(list_empty(&root
->subsys_list
));
1150 test_ss
= list_entry(root
->subsys_list
.next
,
1151 struct cgroup_subsys
, sibling
);
1153 *css
= cgrp
->subsys
[test_ss
->subsys_id
];
1157 *subsys_id
= test_ss
->subsys_id
;
1161 * Attach task 'tsk' to cgroup 'cgrp'
1163 * Call holding cgroup_mutex. May take task_lock of
1164 * the task 'pid' during call.
1166 static int attach_task(struct cgroup
*cgrp
, struct task_struct
*tsk
)
1169 struct cgroup_subsys
*ss
;
1170 struct cgroup
*oldcgrp
;
1171 struct css_set
*cg
= tsk
->cgroups
;
1172 struct css_set
*newcg
;
1173 struct cgroupfs_root
*root
= cgrp
->root
;
1176 get_first_subsys(cgrp
, NULL
, &subsys_id
);
1178 /* Nothing to do if the task is already in that cgroup */
1179 oldcgrp
= task_cgroup(tsk
, subsys_id
);
1180 if (cgrp
== oldcgrp
)
1183 for_each_subsys(root
, ss
) {
1184 if (ss
->can_attach
) {
1185 retval
= ss
->can_attach(ss
, cgrp
, tsk
);
1193 * Locate or allocate a new css_set for this task,
1194 * based on its final set of cgroups
1196 newcg
= find_css_set(cg
, cgrp
);
1202 if (tsk
->flags
& PF_EXITING
) {
1207 rcu_assign_pointer(tsk
->cgroups
, newcg
);
1210 /* Update the css_set linked lists if we're using them */
1211 write_lock(&css_set_lock
);
1212 if (!list_empty(&tsk
->cg_list
)) {
1213 list_del(&tsk
->cg_list
);
1214 list_add(&tsk
->cg_list
, &newcg
->tasks
);
1216 write_unlock(&css_set_lock
);
1218 for_each_subsys(root
, ss
) {
1220 ss
->attach(ss
, cgrp
, oldcgrp
, tsk
);
1223 set_bit(CGRP_RELEASABLE
, &oldcgrp
->flags
);
1230 * Attach task with pid 'pid' to cgroup 'cgrp'. Call with
1231 * cgroup_mutex, may take task_lock of task
1233 static int attach_task_by_pid(struct cgroup
*cgrp
, char *pidbuf
)
1236 struct task_struct
*tsk
;
1239 if (sscanf(pidbuf
, "%d", &pid
) != 1)
1244 tsk
= find_task_by_pid(pid
);
1245 if (!tsk
|| tsk
->flags
& PF_EXITING
) {
1249 get_task_struct(tsk
);
1252 if ((current
->euid
) && (current
->euid
!= tsk
->uid
)
1253 && (current
->euid
!= tsk
->suid
)) {
1254 put_task_struct(tsk
);
1259 get_task_struct(tsk
);
1262 ret
= attach_task(cgrp
, tsk
);
1263 put_task_struct(tsk
);
1267 /* The various types of files and directories in a cgroup file system */
1269 enum cgroup_filetype
{
1273 FILE_NOTIFY_ON_RELEASE
,
1278 static ssize_t
cgroup_write_uint(struct cgroup
*cgrp
, struct cftype
*cft
,
1280 const char __user
*userbuf
,
1281 size_t nbytes
, loff_t
*unused_ppos
)
1290 if (nbytes
>= sizeof(buffer
))
1292 if (copy_from_user(buffer
, userbuf
, nbytes
))
1295 buffer
[nbytes
] = 0; /* nul-terminate */
1297 /* strip newline if necessary */
1298 if (nbytes
&& (buffer
[nbytes
-1] == '\n'))
1299 buffer
[nbytes
-1] = 0;
1300 val
= simple_strtoull(buffer
, &end
, 0);
1304 /* Pass to subsystem */
1305 retval
= cft
->write_uint(cgrp
, cft
, val
);
1311 static ssize_t
cgroup_common_file_write(struct cgroup
*cgrp
,
1314 const char __user
*userbuf
,
1315 size_t nbytes
, loff_t
*unused_ppos
)
1317 enum cgroup_filetype type
= cft
->private;
1321 if (nbytes
>= PATH_MAX
)
1324 /* +1 for nul-terminator */
1325 buffer
= kmalloc(nbytes
+ 1, GFP_KERNEL
);
1329 if (copy_from_user(buffer
, userbuf
, nbytes
)) {
1333 buffer
[nbytes
] = 0; /* nul-terminate */
1335 mutex_lock(&cgroup_mutex
);
1337 if (cgroup_is_removed(cgrp
)) {
1344 retval
= attach_task_by_pid(cgrp
, buffer
);
1346 case FILE_NOTIFY_ON_RELEASE
:
1347 clear_bit(CGRP_RELEASABLE
, &cgrp
->flags
);
1348 if (simple_strtoul(buffer
, NULL
, 10) != 0)
1349 set_bit(CGRP_NOTIFY_ON_RELEASE
, &cgrp
->flags
);
1351 clear_bit(CGRP_NOTIFY_ON_RELEASE
, &cgrp
->flags
);
1353 case FILE_RELEASE_AGENT
:
1355 struct cgroupfs_root
*root
= cgrp
->root
;
1356 /* Strip trailing newline */
1357 if (nbytes
&& (buffer
[nbytes
-1] == '\n')) {
1358 buffer
[nbytes
-1] = 0;
1360 if (nbytes
< sizeof(root
->release_agent_path
)) {
1361 /* We never write anything other than '\0'
1362 * into the last char of release_agent_path,
1363 * so it always remains a NUL-terminated
1365 strncpy(root
->release_agent_path
, buffer
, nbytes
);
1366 root
->release_agent_path
[nbytes
] = 0;
1380 mutex_unlock(&cgroup_mutex
);
1386 static ssize_t
cgroup_file_write(struct file
*file
, const char __user
*buf
,
1387 size_t nbytes
, loff_t
*ppos
)
1389 struct cftype
*cft
= __d_cft(file
->f_dentry
);
1390 struct cgroup
*cgrp
= __d_cgrp(file
->f_dentry
->d_parent
);
1395 return cft
->write(cgrp
, cft
, file
, buf
, nbytes
, ppos
);
1396 if (cft
->write_uint
)
1397 return cgroup_write_uint(cgrp
, cft
, file
, buf
, nbytes
, ppos
);
1401 static ssize_t
cgroup_read_uint(struct cgroup
*cgrp
, struct cftype
*cft
,
1403 char __user
*buf
, size_t nbytes
,
1407 u64 val
= cft
->read_uint(cgrp
, cft
);
1408 int len
= sprintf(tmp
, "%llu\n", (unsigned long long) val
);
1410 return simple_read_from_buffer(buf
, nbytes
, ppos
, tmp
, len
);
1413 static ssize_t
cgroup_common_file_read(struct cgroup
*cgrp
,
1417 size_t nbytes
, loff_t
*ppos
)
1419 enum cgroup_filetype type
= cft
->private;
1424 if (!(page
= (char *)__get_free_page(GFP_KERNEL
)))
1430 case FILE_RELEASE_AGENT
:
1432 struct cgroupfs_root
*root
;
1434 mutex_lock(&cgroup_mutex
);
1436 n
= strnlen(root
->release_agent_path
,
1437 sizeof(root
->release_agent_path
));
1438 n
= min(n
, (size_t) PAGE_SIZE
);
1439 strncpy(s
, root
->release_agent_path
, n
);
1440 mutex_unlock(&cgroup_mutex
);
1450 retval
= simple_read_from_buffer(buf
, nbytes
, ppos
, page
, s
- page
);
1452 free_page((unsigned long)page
);
1456 static ssize_t
cgroup_file_read(struct file
*file
, char __user
*buf
,
1457 size_t nbytes
, loff_t
*ppos
)
1459 struct cftype
*cft
= __d_cft(file
->f_dentry
);
1460 struct cgroup
*cgrp
= __d_cgrp(file
->f_dentry
->d_parent
);
1466 return cft
->read(cgrp
, cft
, file
, buf
, nbytes
, ppos
);
1468 return cgroup_read_uint(cgrp
, cft
, file
, buf
, nbytes
, ppos
);
1472 static int cgroup_file_open(struct inode
*inode
, struct file
*file
)
1477 err
= generic_file_open(inode
, file
);
1481 cft
= __d_cft(file
->f_dentry
);
1485 err
= cft
->open(inode
, file
);
1492 static int cgroup_file_release(struct inode
*inode
, struct file
*file
)
1494 struct cftype
*cft
= __d_cft(file
->f_dentry
);
1496 return cft
->release(inode
, file
);
1501 * cgroup_rename - Only allow simple rename of directories in place.
1503 static int cgroup_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
1504 struct inode
*new_dir
, struct dentry
*new_dentry
)
1506 if (!S_ISDIR(old_dentry
->d_inode
->i_mode
))
1508 if (new_dentry
->d_inode
)
1510 if (old_dir
!= new_dir
)
1512 return simple_rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
1515 static struct file_operations cgroup_file_operations
= {
1516 .read
= cgroup_file_read
,
1517 .write
= cgroup_file_write
,
1518 .llseek
= generic_file_llseek
,
1519 .open
= cgroup_file_open
,
1520 .release
= cgroup_file_release
,
1523 static struct inode_operations cgroup_dir_inode_operations
= {
1524 .lookup
= simple_lookup
,
1525 .mkdir
= cgroup_mkdir
,
1526 .rmdir
= cgroup_rmdir
,
1527 .rename
= cgroup_rename
,
1530 static int cgroup_create_file(struct dentry
*dentry
, int mode
,
1531 struct super_block
*sb
)
1533 static struct dentry_operations cgroup_dops
= {
1534 .d_iput
= cgroup_diput
,
1537 struct inode
*inode
;
1541 if (dentry
->d_inode
)
1544 inode
= cgroup_new_inode(mode
, sb
);
1548 if (S_ISDIR(mode
)) {
1549 inode
->i_op
= &cgroup_dir_inode_operations
;
1550 inode
->i_fop
= &simple_dir_operations
;
1552 /* start off with i_nlink == 2 (for "." entry) */
1555 /* start with the directory inode held, so that we can
1556 * populate it without racing with another mkdir */
1557 mutex_lock_nested(&inode
->i_mutex
, I_MUTEX_CHILD
);
1558 } else if (S_ISREG(mode
)) {
1560 inode
->i_fop
= &cgroup_file_operations
;
1562 dentry
->d_op
= &cgroup_dops
;
1563 d_instantiate(dentry
, inode
);
1564 dget(dentry
); /* Extra count - pin the dentry in core */
1569 * cgroup_create_dir - create a directory for an object.
1570 * cgrp: the cgroup we create the directory for.
1571 * It must have a valid ->parent field
1572 * And we are going to fill its ->dentry field.
1573 * dentry: dentry of the new cgroup
1574 * mode: mode to set on new directory.
1576 static int cgroup_create_dir(struct cgroup
*cgrp
, struct dentry
*dentry
,
1579 struct dentry
*parent
;
1582 parent
= cgrp
->parent
->dentry
;
1583 error
= cgroup_create_file(dentry
, S_IFDIR
| mode
, cgrp
->root
->sb
);
1585 dentry
->d_fsdata
= cgrp
;
1586 inc_nlink(parent
->d_inode
);
1587 cgrp
->dentry
= dentry
;
1595 int cgroup_add_file(struct cgroup
*cgrp
,
1596 struct cgroup_subsys
*subsys
,
1597 const struct cftype
*cft
)
1599 struct dentry
*dir
= cgrp
->dentry
;
1600 struct dentry
*dentry
;
1603 char name
[MAX_CGROUP_TYPE_NAMELEN
+ MAX_CFTYPE_NAME
+ 2] = { 0 };
1604 if (subsys
&& !test_bit(ROOT_NOPREFIX
, &cgrp
->root
->flags
)) {
1605 strcpy(name
, subsys
->name
);
1608 strcat(name
, cft
->name
);
1609 BUG_ON(!mutex_is_locked(&dir
->d_inode
->i_mutex
));
1610 dentry
= lookup_one_len(name
, dir
, strlen(name
));
1611 if (!IS_ERR(dentry
)) {
1612 error
= cgroup_create_file(dentry
, 0644 | S_IFREG
,
1615 dentry
->d_fsdata
= (void *)cft
;
1618 error
= PTR_ERR(dentry
);
1622 int cgroup_add_files(struct cgroup
*cgrp
,
1623 struct cgroup_subsys
*subsys
,
1624 const struct cftype cft
[],
1628 for (i
= 0; i
< count
; i
++) {
1629 err
= cgroup_add_file(cgrp
, subsys
, &cft
[i
]);
1636 /* Count the number of tasks in a cgroup. */
1638 int cgroup_task_count(const struct cgroup
*cgrp
)
1641 struct list_head
*l
;
1643 read_lock(&css_set_lock
);
1644 l
= cgrp
->css_sets
.next
;
1645 while (l
!= &cgrp
->css_sets
) {
1646 struct cg_cgroup_link
*link
=
1647 list_entry(l
, struct cg_cgroup_link
, cgrp_link_list
);
1648 count
+= atomic_read(&link
->cg
->ref
.refcount
);
1651 read_unlock(&css_set_lock
);
1656 * Advance a list_head iterator. The iterator should be positioned at
1657 * the start of a css_set
1659 static void cgroup_advance_iter(struct cgroup
*cgrp
,
1660 struct cgroup_iter
*it
)
1662 struct list_head
*l
= it
->cg_link
;
1663 struct cg_cgroup_link
*link
;
1666 /* Advance to the next non-empty css_set */
1669 if (l
== &cgrp
->css_sets
) {
1673 link
= list_entry(l
, struct cg_cgroup_link
, cgrp_link_list
);
1675 } while (list_empty(&cg
->tasks
));
1677 it
->task
= cg
->tasks
.next
;
1680 void cgroup_iter_start(struct cgroup
*cgrp
, struct cgroup_iter
*it
)
1683 * The first time anyone tries to iterate across a cgroup,
1684 * we need to enable the list linking each css_set to its
1685 * tasks, and fix up all existing tasks.
1687 if (!use_task_css_set_links
) {
1688 struct task_struct
*p
, *g
;
1689 write_lock(&css_set_lock
);
1690 use_task_css_set_links
= 1;
1691 do_each_thread(g
, p
) {
1693 if (list_empty(&p
->cg_list
))
1694 list_add(&p
->cg_list
, &p
->cgroups
->tasks
);
1696 } while_each_thread(g
, p
);
1697 write_unlock(&css_set_lock
);
1699 read_lock(&css_set_lock
);
1700 it
->cg_link
= &cgrp
->css_sets
;
1701 cgroup_advance_iter(cgrp
, it
);
1704 struct task_struct
*cgroup_iter_next(struct cgroup
*cgrp
,
1705 struct cgroup_iter
*it
)
1707 struct task_struct
*res
;
1708 struct list_head
*l
= it
->task
;
1710 /* If the iterator cg is NULL, we have no tasks */
1713 res
= list_entry(l
, struct task_struct
, cg_list
);
1714 /* Advance iterator to find next entry */
1716 if (l
== &res
->cgroups
->tasks
) {
1717 /* We reached the end of this task list - move on to
1718 * the next cg_cgroup_link */
1719 cgroup_advance_iter(cgrp
, it
);
1726 void cgroup_iter_end(struct cgroup
*cgrp
, struct cgroup_iter
*it
)
1728 read_unlock(&css_set_lock
);
1732 * Stuff for reading the 'tasks' file.
1734 * Reading this file can return large amounts of data if a cgroup has
1735 * *lots* of attached tasks. So it may need several calls to read(),
1736 * but we cannot guarantee that the information we produce is correct
1737 * unless we produce it entirely atomically.
1739 * Upon tasks file open(), a struct ctr_struct is allocated, that
1740 * will have a pointer to an array (also allocated here). The struct
1741 * ctr_struct * is stored in file->private_data. Its resources will
1742 * be freed by release() when the file is closed. The array is used
1743 * to sprintf the PIDs and then used by read().
1751 * Load into 'pidarray' up to 'npids' of the tasks using cgroup
1752 * 'cgrp'. Return actual number of pids loaded. No need to
1753 * task_lock(p) when reading out p->cgroup, since we're in an RCU
1754 * read section, so the css_set can't go away, and is
1755 * immutable after creation.
1757 static int pid_array_load(pid_t
*pidarray
, int npids
, struct cgroup
*cgrp
)
1760 struct cgroup_iter it
;
1761 struct task_struct
*tsk
;
1762 cgroup_iter_start(cgrp
, &it
);
1763 while ((tsk
= cgroup_iter_next(cgrp
, &it
))) {
1764 if (unlikely(n
== npids
))
1766 pidarray
[n
++] = task_pid_nr(tsk
);
1768 cgroup_iter_end(cgrp
, &it
);
1773 * Build and fill cgroupstats so that taskstats can export it to user
1776 * @stats: cgroupstats to fill information into
1777 * @dentry: A dentry entry belonging to the cgroup for which stats have
1780 int cgroupstats_build(struct cgroupstats
*stats
, struct dentry
*dentry
)
1783 struct cgroup
*cgrp
;
1784 struct cgroup_iter it
;
1785 struct task_struct
*tsk
;
1787 * Validate dentry by checking the superblock operations
1789 if (dentry
->d_sb
->s_op
!= &cgroup_ops
)
1793 cgrp
= dentry
->d_fsdata
;
1796 cgroup_iter_start(cgrp
, &it
);
1797 while ((tsk
= cgroup_iter_next(cgrp
, &it
))) {
1798 switch (tsk
->state
) {
1800 stats
->nr_running
++;
1802 case TASK_INTERRUPTIBLE
:
1803 stats
->nr_sleeping
++;
1805 case TASK_UNINTERRUPTIBLE
:
1806 stats
->nr_uninterruptible
++;
1809 stats
->nr_stopped
++;
1812 if (delayacct_is_task_waiting_on_io(tsk
))
1813 stats
->nr_io_wait
++;
1817 cgroup_iter_end(cgrp
, &it
);
1824 static int cmppid(const void *a
, const void *b
)
1826 return *(pid_t
*)a
- *(pid_t
*)b
;
1830 * Convert array 'a' of 'npids' pid_t's to a string of newline separated
1831 * decimal pids in 'buf'. Don't write more than 'sz' chars, but return
1832 * count 'cnt' of how many chars would be written if buf were large enough.
1834 static int pid_array_to_buf(char *buf
, int sz
, pid_t
*a
, int npids
)
1839 for (i
= 0; i
< npids
; i
++)
1840 cnt
+= snprintf(buf
+ cnt
, max(sz
- cnt
, 0), "%d\n", a
[i
]);
1845 * Handle an open on 'tasks' file. Prepare a buffer listing the
1846 * process id's of tasks currently attached to the cgroup being opened.
1848 * Does not require any specific cgroup mutexes, and does not take any.
1850 static int cgroup_tasks_open(struct inode
*unused
, struct file
*file
)
1852 struct cgroup
*cgrp
= __d_cgrp(file
->f_dentry
->d_parent
);
1853 struct ctr_struct
*ctr
;
1858 if (!(file
->f_mode
& FMODE_READ
))
1861 ctr
= kmalloc(sizeof(*ctr
), GFP_KERNEL
);
1866 * If cgroup gets more users after we read count, we won't have
1867 * enough space - tough. This race is indistinguishable to the
1868 * caller from the case that the additional cgroup users didn't
1869 * show up until sometime later on.
1871 npids
= cgroup_task_count(cgrp
);
1873 pidarray
= kmalloc(npids
* sizeof(pid_t
), GFP_KERNEL
);
1877 npids
= pid_array_load(pidarray
, npids
, cgrp
);
1878 sort(pidarray
, npids
, sizeof(pid_t
), cmppid
, NULL
);
1880 /* Call pid_array_to_buf() twice, first just to get bufsz */
1881 ctr
->bufsz
= pid_array_to_buf(&c
, sizeof(c
), pidarray
, npids
) + 1;
1882 ctr
->buf
= kmalloc(ctr
->bufsz
, GFP_KERNEL
);
1885 ctr
->bufsz
= pid_array_to_buf(ctr
->buf
, ctr
->bufsz
, pidarray
, npids
);
1892 file
->private_data
= ctr
;
1903 static ssize_t
cgroup_tasks_read(struct cgroup
*cgrp
,
1905 struct file
*file
, char __user
*buf
,
1906 size_t nbytes
, loff_t
*ppos
)
1908 struct ctr_struct
*ctr
= file
->private_data
;
1910 return simple_read_from_buffer(buf
, nbytes
, ppos
, ctr
->buf
, ctr
->bufsz
);
1913 static int cgroup_tasks_release(struct inode
*unused_inode
,
1916 struct ctr_struct
*ctr
;
1918 if (file
->f_mode
& FMODE_READ
) {
1919 ctr
= file
->private_data
;
1926 static u64
cgroup_read_notify_on_release(struct cgroup
*cgrp
,
1929 return notify_on_release(cgrp
);
1932 static u64
cgroup_read_releasable(struct cgroup
*cgrp
, struct cftype
*cft
)
1934 return test_bit(CGRP_RELEASABLE
, &cgrp
->flags
);
1938 * for the common functions, 'private' gives the type of file
1940 static struct cftype files
[] = {
1943 .open
= cgroup_tasks_open
,
1944 .read
= cgroup_tasks_read
,
1945 .write
= cgroup_common_file_write
,
1946 .release
= cgroup_tasks_release
,
1947 .private = FILE_TASKLIST
,
1951 .name
= "notify_on_release",
1952 .read_uint
= cgroup_read_notify_on_release
,
1953 .write
= cgroup_common_file_write
,
1954 .private = FILE_NOTIFY_ON_RELEASE
,
1958 .name
= "releasable",
1959 .read_uint
= cgroup_read_releasable
,
1960 .private = FILE_RELEASABLE
,
1964 static struct cftype cft_release_agent
= {
1965 .name
= "release_agent",
1966 .read
= cgroup_common_file_read
,
1967 .write
= cgroup_common_file_write
,
1968 .private = FILE_RELEASE_AGENT
,
1971 static int cgroup_populate_dir(struct cgroup
*cgrp
)
1974 struct cgroup_subsys
*ss
;
1976 /* First clear out any existing files */
1977 cgroup_clear_directory(cgrp
->dentry
);
1979 err
= cgroup_add_files(cgrp
, NULL
, files
, ARRAY_SIZE(files
));
1983 if (cgrp
== cgrp
->top_cgroup
) {
1984 if ((err
= cgroup_add_file(cgrp
, NULL
, &cft_release_agent
)) < 0)
1988 for_each_subsys(cgrp
->root
, ss
) {
1989 if (ss
->populate
&& (err
= ss
->populate(ss
, cgrp
)) < 0)
1996 static void init_cgroup_css(struct cgroup_subsys_state
*css
,
1997 struct cgroup_subsys
*ss
,
1998 struct cgroup
*cgrp
)
2001 atomic_set(&css
->refcnt
, 0);
2003 if (cgrp
== dummytop
)
2004 set_bit(CSS_ROOT
, &css
->flags
);
2005 BUG_ON(cgrp
->subsys
[ss
->subsys_id
]);
2006 cgrp
->subsys
[ss
->subsys_id
] = css
;
2010 * cgroup_create - create a cgroup
2011 * parent: cgroup that will be parent of the new cgroup.
2012 * name: name of the new cgroup. Will be strcpy'ed.
2013 * mode: mode to set on new inode
2015 * Must be called with the mutex on the parent inode held
2018 static long cgroup_create(struct cgroup
*parent
, struct dentry
*dentry
,
2021 struct cgroup
*cgrp
;
2022 struct cgroupfs_root
*root
= parent
->root
;
2024 struct cgroup_subsys
*ss
;
2025 struct super_block
*sb
= root
->sb
;
2027 cgrp
= kzalloc(sizeof(*cgrp
), GFP_KERNEL
);
2031 /* Grab a reference on the superblock so the hierarchy doesn't
2032 * get deleted on unmount if there are child cgroups. This
2033 * can be done outside cgroup_mutex, since the sb can't
2034 * disappear while someone has an open control file on the
2036 atomic_inc(&sb
->s_active
);
2038 mutex_lock(&cgroup_mutex
);
2041 INIT_LIST_HEAD(&cgrp
->sibling
);
2042 INIT_LIST_HEAD(&cgrp
->children
);
2043 INIT_LIST_HEAD(&cgrp
->css_sets
);
2044 INIT_LIST_HEAD(&cgrp
->release_list
);
2046 cgrp
->parent
= parent
;
2047 cgrp
->root
= parent
->root
;
2048 cgrp
->top_cgroup
= parent
->top_cgroup
;
2050 for_each_subsys(root
, ss
) {
2051 struct cgroup_subsys_state
*css
= ss
->create(ss
, cgrp
);
2056 init_cgroup_css(css
, ss
, cgrp
);
2059 list_add(&cgrp
->sibling
, &cgrp
->parent
->children
);
2060 root
->number_of_cgroups
++;
2062 err
= cgroup_create_dir(cgrp
, dentry
, mode
);
2066 /* The cgroup directory was pre-locked for us */
2067 BUG_ON(!mutex_is_locked(&cgrp
->dentry
->d_inode
->i_mutex
));
2069 err
= cgroup_populate_dir(cgrp
);
2070 /* If err < 0, we have a half-filled directory - oh well ;) */
2072 mutex_unlock(&cgroup_mutex
);
2073 mutex_unlock(&cgrp
->dentry
->d_inode
->i_mutex
);
2079 list_del(&cgrp
->sibling
);
2080 root
->number_of_cgroups
--;
2084 for_each_subsys(root
, ss
) {
2085 if (cgrp
->subsys
[ss
->subsys_id
])
2086 ss
->destroy(ss
, cgrp
);
2089 mutex_unlock(&cgroup_mutex
);
2091 /* Release the reference count that we took on the superblock */
2092 deactivate_super(sb
);
2098 static int cgroup_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
2100 struct cgroup
*c_parent
= dentry
->d_parent
->d_fsdata
;
2102 /* the vfs holds inode->i_mutex already */
2103 return cgroup_create(c_parent
, dentry
, mode
| S_IFDIR
);
2106 static inline int cgroup_has_css_refs(struct cgroup
*cgrp
)
2108 /* Check the reference count on each subsystem. Since we
2109 * already established that there are no tasks in the
2110 * cgroup, if the css refcount is also 0, then there should
2111 * be no outstanding references, so the subsystem is safe to
2112 * destroy. We scan across all subsystems rather than using
2113 * the per-hierarchy linked list of mounted subsystems since
2114 * we can be called via check_for_release() with no
2115 * synchronization other than RCU, and the subsystem linked
2116 * list isn't RCU-safe */
2118 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
2119 struct cgroup_subsys
*ss
= subsys
[i
];
2120 struct cgroup_subsys_state
*css
;
2121 /* Skip subsystems not in this hierarchy */
2122 if (ss
->root
!= cgrp
->root
)
2124 css
= cgrp
->subsys
[ss
->subsys_id
];
2125 /* When called from check_for_release() it's possible
2126 * that by this point the cgroup has been removed
2127 * and the css deleted. But a false-positive doesn't
2128 * matter, since it can only happen if the cgroup
2129 * has been deleted and hence no longer needs the
2130 * release agent to be called anyway. */
2131 if (css
&& atomic_read(&css
->refcnt
)) {
2138 static int cgroup_rmdir(struct inode
*unused_dir
, struct dentry
*dentry
)
2140 struct cgroup
*cgrp
= dentry
->d_fsdata
;
2142 struct cgroup
*parent
;
2143 struct cgroup_subsys
*ss
;
2144 struct super_block
*sb
;
2145 struct cgroupfs_root
*root
;
2147 /* the vfs holds both inode->i_mutex already */
2149 mutex_lock(&cgroup_mutex
);
2150 if (atomic_read(&cgrp
->count
) != 0) {
2151 mutex_unlock(&cgroup_mutex
);
2154 if (!list_empty(&cgrp
->children
)) {
2155 mutex_unlock(&cgroup_mutex
);
2159 parent
= cgrp
->parent
;
2163 if (cgroup_has_css_refs(cgrp
)) {
2164 mutex_unlock(&cgroup_mutex
);
2168 for_each_subsys(root
, ss
) {
2169 if (cgrp
->subsys
[ss
->subsys_id
])
2170 ss
->destroy(ss
, cgrp
);
2173 spin_lock(&release_list_lock
);
2174 set_bit(CGRP_REMOVED
, &cgrp
->flags
);
2175 if (!list_empty(&cgrp
->release_list
))
2176 list_del(&cgrp
->release_list
);
2177 spin_unlock(&release_list_lock
);
2178 /* delete my sibling from parent->children */
2179 list_del(&cgrp
->sibling
);
2180 spin_lock(&cgrp
->dentry
->d_lock
);
2181 d
= dget(cgrp
->dentry
);
2182 cgrp
->dentry
= NULL
;
2183 spin_unlock(&d
->d_lock
);
2185 cgroup_d_remove_dir(d
);
2187 root
->number_of_cgroups
--;
2189 set_bit(CGRP_RELEASABLE
, &parent
->flags
);
2190 check_for_release(parent
);
2192 mutex_unlock(&cgroup_mutex
);
2193 /* Drop the active superblock reference that we took when we
2194 * created the cgroup */
2195 deactivate_super(sb
);
2199 static void cgroup_init_subsys(struct cgroup_subsys
*ss
)
2201 struct cgroup_subsys_state
*css
;
2202 struct list_head
*l
;
2203 printk(KERN_ERR
"Initializing cgroup subsys %s\n", ss
->name
);
2205 /* Create the top cgroup state for this subsystem */
2206 ss
->root
= &rootnode
;
2207 css
= ss
->create(ss
, dummytop
);
2208 /* We don't handle early failures gracefully */
2209 BUG_ON(IS_ERR(css
));
2210 init_cgroup_css(css
, ss
, dummytop
);
2212 /* Update all cgroup groups to contain a subsys
2213 * pointer to this state - since the subsystem is
2214 * newly registered, all tasks and hence all cgroup
2215 * groups are in the subsystem's top cgroup. */
2216 write_lock(&css_set_lock
);
2217 l
= &init_css_set
.list
;
2219 struct css_set
*cg
=
2220 list_entry(l
, struct css_set
, list
);
2221 cg
->subsys
[ss
->subsys_id
] = dummytop
->subsys
[ss
->subsys_id
];
2223 } while (l
!= &init_css_set
.list
);
2224 write_unlock(&css_set_lock
);
2226 /* If this subsystem requested that it be notified with fork
2227 * events, we should send it one now for every process in the
2230 struct task_struct
*g
, *p
;
2232 read_lock(&tasklist_lock
);
2233 do_each_thread(g
, p
) {
2235 } while_each_thread(g
, p
);
2236 read_unlock(&tasklist_lock
);
2239 need_forkexit_callback
|= ss
->fork
|| ss
->exit
;
2245 * cgroup_init_early - initialize cgroups at system boot, and
2246 * initialize any subsystems that request early init.
2248 int __init
cgroup_init_early(void)
2251 kref_init(&init_css_set
.ref
);
2252 kref_get(&init_css_set
.ref
);
2253 INIT_LIST_HEAD(&init_css_set
.list
);
2254 INIT_LIST_HEAD(&init_css_set
.cg_links
);
2255 INIT_LIST_HEAD(&init_css_set
.tasks
);
2257 init_cgroup_root(&rootnode
);
2258 list_add(&rootnode
.root_list
, &roots
);
2260 init_task
.cgroups
= &init_css_set
;
2262 init_css_set_link
.cg
= &init_css_set
;
2263 list_add(&init_css_set_link
.cgrp_link_list
,
2264 &rootnode
.top_cgroup
.css_sets
);
2265 list_add(&init_css_set_link
.cg_link_list
,
2266 &init_css_set
.cg_links
);
2268 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
2269 struct cgroup_subsys
*ss
= subsys
[i
];
2272 BUG_ON(strlen(ss
->name
) > MAX_CGROUP_TYPE_NAMELEN
);
2273 BUG_ON(!ss
->create
);
2274 BUG_ON(!ss
->destroy
);
2275 if (ss
->subsys_id
!= i
) {
2276 printk(KERN_ERR
"Subsys %s id == %d\n",
2277 ss
->name
, ss
->subsys_id
);
2282 cgroup_init_subsys(ss
);
2288 * cgroup_init - register cgroup filesystem and /proc file, and
2289 * initialize any subsystems that didn't request early init.
2291 int __init
cgroup_init(void)
2295 struct proc_dir_entry
*entry
;
2297 err
= bdi_init(&cgroup_backing_dev_info
);
2301 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
2302 struct cgroup_subsys
*ss
= subsys
[i
];
2303 if (!ss
->early_init
)
2304 cgroup_init_subsys(ss
);
2307 err
= register_filesystem(&cgroup_fs_type
);
2311 entry
= create_proc_entry("cgroups", 0, NULL
);
2313 entry
->proc_fops
= &proc_cgroupstats_operations
;
2317 bdi_destroy(&cgroup_backing_dev_info
);
2323 * proc_cgroup_show()
2324 * - Print task's cgroup paths into seq_file, one line for each hierarchy
2325 * - Used for /proc/<pid>/cgroup.
2326 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
2327 * doesn't really matter if tsk->cgroup changes after we read it,
2328 * and we take cgroup_mutex, keeping attach_task() from changing it
2329 * anyway. No need to check that tsk->cgroup != NULL, thanks to
2330 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
2331 * cgroup to top_cgroup.
2334 /* TODO: Use a proper seq_file iterator */
2335 static int proc_cgroup_show(struct seq_file
*m
, void *v
)
2338 struct task_struct
*tsk
;
2341 struct cgroupfs_root
*root
;
2344 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
2350 tsk
= get_pid_task(pid
, PIDTYPE_PID
);
2356 mutex_lock(&cgroup_mutex
);
2358 for_each_root(root
) {
2359 struct cgroup_subsys
*ss
;
2360 struct cgroup
*cgrp
;
2364 /* Skip this hierarchy if it has no active subsystems */
2365 if (!root
->actual_subsys_bits
)
2367 for_each_subsys(root
, ss
)
2368 seq_printf(m
, "%s%s", count
++ ? "," : "", ss
->name
);
2370 get_first_subsys(&root
->top_cgroup
, NULL
, &subsys_id
);
2371 cgrp
= task_cgroup(tsk
, subsys_id
);
2372 retval
= cgroup_path(cgrp
, buf
, PAGE_SIZE
);
2380 mutex_unlock(&cgroup_mutex
);
2381 put_task_struct(tsk
);
2388 static int cgroup_open(struct inode
*inode
, struct file
*file
)
2390 struct pid
*pid
= PROC_I(inode
)->pid
;
2391 return single_open(file
, proc_cgroup_show
, pid
);
2394 struct file_operations proc_cgroup_operations
= {
2395 .open
= cgroup_open
,
2397 .llseek
= seq_lseek
,
2398 .release
= single_release
,
2401 /* Display information about each subsystem and each hierarchy */
2402 static int proc_cgroupstats_show(struct seq_file
*m
, void *v
)
2406 seq_puts(m
, "#subsys_name\thierarchy\tnum_cgroups\n");
2407 mutex_lock(&cgroup_mutex
);
2408 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
2409 struct cgroup_subsys
*ss
= subsys
[i
];
2410 seq_printf(m
, "%s\t%lu\t%d\n",
2411 ss
->name
, ss
->root
->subsys_bits
,
2412 ss
->root
->number_of_cgroups
);
2414 mutex_unlock(&cgroup_mutex
);
2418 static int cgroupstats_open(struct inode
*inode
, struct file
*file
)
2420 return single_open(file
, proc_cgroupstats_show
, 0);
2423 static struct file_operations proc_cgroupstats_operations
= {
2424 .open
= cgroupstats_open
,
2426 .llseek
= seq_lseek
,
2427 .release
= single_release
,
2431 * cgroup_fork - attach newly forked task to its parents cgroup.
2432 * @tsk: pointer to task_struct of forking parent process.
2434 * Description: A task inherits its parent's cgroup at fork().
2436 * A pointer to the shared css_set was automatically copied in
2437 * fork.c by dup_task_struct(). However, we ignore that copy, since
2438 * it was not made under the protection of RCU or cgroup_mutex, so
2439 * might no longer be a valid cgroup pointer. attach_task() might
2440 * have already changed current->cgroups, allowing the previously
2441 * referenced cgroup group to be removed and freed.
2443 * At the point that cgroup_fork() is called, 'current' is the parent
2444 * task, and the passed argument 'child' points to the child task.
2446 void cgroup_fork(struct task_struct
*child
)
2449 child
->cgroups
= current
->cgroups
;
2450 get_css_set(child
->cgroups
);
2451 task_unlock(current
);
2452 INIT_LIST_HEAD(&child
->cg_list
);
2456 * cgroup_fork_callbacks - called on a new task very soon before
2457 * adding it to the tasklist. No need to take any locks since no-one
2458 * can be operating on this task
2460 void cgroup_fork_callbacks(struct task_struct
*child
)
2462 if (need_forkexit_callback
) {
2464 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
2465 struct cgroup_subsys
*ss
= subsys
[i
];
2467 ss
->fork(ss
, child
);
2473 * cgroup_post_fork - called on a new task after adding it to the
2474 * task list. Adds the task to the list running through its css_set
2475 * if necessary. Has to be after the task is visible on the task list
2476 * in case we race with the first call to cgroup_iter_start() - to
2477 * guarantee that the new task ends up on its list. */
2478 void cgroup_post_fork(struct task_struct
*child
)
2480 if (use_task_css_set_links
) {
2481 write_lock(&css_set_lock
);
2482 if (list_empty(&child
->cg_list
))
2483 list_add(&child
->cg_list
, &child
->cgroups
->tasks
);
2484 write_unlock(&css_set_lock
);
2488 * cgroup_exit - detach cgroup from exiting task
2489 * @tsk: pointer to task_struct of exiting process
2491 * Description: Detach cgroup from @tsk and release it.
2493 * Note that cgroups marked notify_on_release force every task in
2494 * them to take the global cgroup_mutex mutex when exiting.
2495 * This could impact scaling on very large systems. Be reluctant to
2496 * use notify_on_release cgroups where very high task exit scaling
2497 * is required on large systems.
2499 * the_top_cgroup_hack:
2501 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
2503 * We call cgroup_exit() while the task is still competent to
2504 * handle notify_on_release(), then leave the task attached to the
2505 * root cgroup in each hierarchy for the remainder of its exit.
2507 * To do this properly, we would increment the reference count on
2508 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
2509 * code we would add a second cgroup function call, to drop that
2510 * reference. This would just create an unnecessary hot spot on
2511 * the top_cgroup reference count, to no avail.
2513 * Normally, holding a reference to a cgroup without bumping its
2514 * count is unsafe. The cgroup could go away, or someone could
2515 * attach us to a different cgroup, decrementing the count on
2516 * the first cgroup that we never incremented. But in this case,
2517 * top_cgroup isn't going away, and either task has PF_EXITING set,
2518 * which wards off any attach_task() attempts, or task is a failed
2519 * fork, never visible to attach_task.
2522 void cgroup_exit(struct task_struct
*tsk
, int run_callbacks
)
2527 if (run_callbacks
&& need_forkexit_callback
) {
2528 for (i
= 0; i
< CGROUP_SUBSYS_COUNT
; i
++) {
2529 struct cgroup_subsys
*ss
= subsys
[i
];
2536 * Unlink from the css_set task list if necessary.
2537 * Optimistically check cg_list before taking
2540 if (!list_empty(&tsk
->cg_list
)) {
2541 write_lock(&css_set_lock
);
2542 if (!list_empty(&tsk
->cg_list
))
2543 list_del(&tsk
->cg_list
);
2544 write_unlock(&css_set_lock
);
2547 /* Reassign the task to the init_css_set. */
2550 tsk
->cgroups
= &init_css_set
;
2553 put_css_set_taskexit(cg
);
2557 * cgroup_clone - duplicate the current cgroup in the hierarchy
2558 * that the given subsystem is attached to, and move this task into
2561 int cgroup_clone(struct task_struct
*tsk
, struct cgroup_subsys
*subsys
)
2563 struct dentry
*dentry
;
2565 char nodename
[MAX_CGROUP_TYPE_NAMELEN
];
2566 struct cgroup
*parent
, *child
;
2567 struct inode
*inode
;
2569 struct cgroupfs_root
*root
;
2570 struct cgroup_subsys
*ss
;
2572 /* We shouldn't be called by an unregistered subsystem */
2573 BUG_ON(!subsys
->active
);
2575 /* First figure out what hierarchy and cgroup we're dealing
2576 * with, and pin them so we can drop cgroup_mutex */
2577 mutex_lock(&cgroup_mutex
);
2579 root
= subsys
->root
;
2580 if (root
== &rootnode
) {
2582 "Not cloning cgroup for unused subsystem %s\n",
2584 mutex_unlock(&cgroup_mutex
);
2588 parent
= task_cgroup(tsk
, subsys
->subsys_id
);
2590 snprintf(nodename
, MAX_CGROUP_TYPE_NAMELEN
, "node_%d", tsk
->pid
);
2592 /* Pin the hierarchy */
2593 atomic_inc(&parent
->root
->sb
->s_active
);
2595 /* Keep the cgroup alive */
2597 mutex_unlock(&cgroup_mutex
);
2599 /* Now do the VFS work to create a cgroup */
2600 inode
= parent
->dentry
->d_inode
;
2602 /* Hold the parent directory mutex across this operation to
2603 * stop anyone else deleting the new cgroup */
2604 mutex_lock(&inode
->i_mutex
);
2605 dentry
= lookup_one_len(nodename
, parent
->dentry
, strlen(nodename
));
2606 if (IS_ERR(dentry
)) {
2608 "Couldn't allocate dentry for %s: %ld\n", nodename
,
2610 ret
= PTR_ERR(dentry
);
2614 /* Create the cgroup directory, which also creates the cgroup */
2615 ret
= vfs_mkdir(inode
, dentry
, S_IFDIR
| 0755);
2616 child
= __d_cgrp(dentry
);
2620 "Failed to create cgroup %s: %d\n", nodename
,
2627 "Couldn't find new cgroup %s\n", nodename
);
2632 /* The cgroup now exists. Retake cgroup_mutex and check
2633 * that we're still in the same state that we thought we
2635 mutex_lock(&cgroup_mutex
);
2636 if ((root
!= subsys
->root
) ||
2637 (parent
!= task_cgroup(tsk
, subsys
->subsys_id
))) {
2638 /* Aargh, we raced ... */
2639 mutex_unlock(&inode
->i_mutex
);
2642 deactivate_super(parent
->root
->sb
);
2643 /* The cgroup is still accessible in the VFS, but
2644 * we're not going to try to rmdir() it at this
2647 "Race in cgroup_clone() - leaking cgroup %s\n",
2652 /* do any required auto-setup */
2653 for_each_subsys(root
, ss
) {
2655 ss
->post_clone(ss
, child
);
2658 /* All seems fine. Finish by moving the task into the new cgroup */
2659 ret
= attach_task(child
, tsk
);
2660 mutex_unlock(&cgroup_mutex
);
2663 mutex_unlock(&inode
->i_mutex
);
2665 mutex_lock(&cgroup_mutex
);
2667 mutex_unlock(&cgroup_mutex
);
2668 deactivate_super(parent
->root
->sb
);
2673 * See if "cgrp" is a descendant of the current task's cgroup in
2674 * the appropriate hierarchy
2676 * If we are sending in dummytop, then presumably we are creating
2677 * the top cgroup in the subsystem.
2679 * Called only by the ns (nsproxy) cgroup.
2681 int cgroup_is_descendant(const struct cgroup
*cgrp
)
2684 struct cgroup
*target
;
2687 if (cgrp
== dummytop
)
2690 get_first_subsys(cgrp
, NULL
, &subsys_id
);
2691 target
= task_cgroup(current
, subsys_id
);
2692 while (cgrp
!= target
&& cgrp
!= cgrp
->top_cgroup
)
2693 cgrp
= cgrp
->parent
;
2694 ret
= (cgrp
== target
);
2698 static void check_for_release(struct cgroup
*cgrp
)
2700 /* All of these checks rely on RCU to keep the cgroup
2701 * structure alive */
2702 if (cgroup_is_releasable(cgrp
) && !atomic_read(&cgrp
->count
)
2703 && list_empty(&cgrp
->children
) && !cgroup_has_css_refs(cgrp
)) {
2704 /* Control Group is currently removeable. If it's not
2705 * already queued for a userspace notification, queue
2707 int need_schedule_work
= 0;
2708 spin_lock(&release_list_lock
);
2709 if (!cgroup_is_removed(cgrp
) &&
2710 list_empty(&cgrp
->release_list
)) {
2711 list_add(&cgrp
->release_list
, &release_list
);
2712 need_schedule_work
= 1;
2714 spin_unlock(&release_list_lock
);
2715 if (need_schedule_work
)
2716 schedule_work(&release_agent_work
);
2720 void __css_put(struct cgroup_subsys_state
*css
)
2722 struct cgroup
*cgrp
= css
->cgroup
;
2724 if (atomic_dec_and_test(&css
->refcnt
) && notify_on_release(cgrp
)) {
2725 set_bit(CGRP_RELEASABLE
, &cgrp
->flags
);
2726 check_for_release(cgrp
);
2732 * Notify userspace when a cgroup is released, by running the
2733 * configured release agent with the name of the cgroup (path
2734 * relative to the root of cgroup file system) as the argument.
2736 * Most likely, this user command will try to rmdir this cgroup.
2738 * This races with the possibility that some other task will be
2739 * attached to this cgroup before it is removed, or that some other
2740 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
2741 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
2742 * unused, and this cgroup will be reprieved from its death sentence,
2743 * to continue to serve a useful existence. Next time it's released,
2744 * we will get notified again, if it still has 'notify_on_release' set.
2746 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
2747 * means only wait until the task is successfully execve()'d. The
2748 * separate release agent task is forked by call_usermodehelper(),
2749 * then control in this thread returns here, without waiting for the
2750 * release agent task. We don't bother to wait because the caller of
2751 * this routine has no use for the exit status of the release agent
2752 * task, so no sense holding our caller up for that.
2756 static void cgroup_release_agent(struct work_struct
*work
)
2758 BUG_ON(work
!= &release_agent_work
);
2759 mutex_lock(&cgroup_mutex
);
2760 spin_lock(&release_list_lock
);
2761 while (!list_empty(&release_list
)) {
2762 char *argv
[3], *envp
[3];
2765 struct cgroup
*cgrp
= list_entry(release_list
.next
,
2768 list_del_init(&cgrp
->release_list
);
2769 spin_unlock(&release_list_lock
);
2770 pathbuf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
2772 spin_lock(&release_list_lock
);
2776 if (cgroup_path(cgrp
, pathbuf
, PAGE_SIZE
) < 0) {
2778 spin_lock(&release_list_lock
);
2783 argv
[i
++] = cgrp
->root
->release_agent_path
;
2784 argv
[i
++] = (char *)pathbuf
;
2788 /* minimal command environment */
2789 envp
[i
++] = "HOME=/";
2790 envp
[i
++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
2793 /* Drop the lock while we invoke the usermode helper,
2794 * since the exec could involve hitting disk and hence
2795 * be a slow process */
2796 mutex_unlock(&cgroup_mutex
);
2797 call_usermodehelper(argv
[0], argv
, envp
, UMH_WAIT_EXEC
);
2799 mutex_lock(&cgroup_mutex
);
2800 spin_lock(&release_list_lock
);
2802 spin_unlock(&release_list_lock
);
2803 mutex_unlock(&cgroup_mutex
);